OSDN Git Service

2010-10-05 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_Eval; use Sem_Eval;
47 with Sem_Res;  use Sem_Res;
48 with Sem_Type; use Sem_Type;
49 with Sem_Util; use Sem_Util;
50 with Snames;   use Snames;
51 with Stand;    use Stand;
52 with Stringt;  use Stringt;
53 with Targparm; use Targparm;
54 with Tbuild;   use Tbuild;
55 with Ttypes;   use Ttypes;
56 with Uintp;    use Uintp;
57 with Urealp;   use Urealp;
58 with Validsw;  use Validsw;
59
60 package body Exp_Util is
61
62    -----------------------
63    -- Local Subprograms --
64    -----------------------
65
66    function Build_Task_Array_Image
67      (Loc    : Source_Ptr;
68       Id_Ref : Node_Id;
69       A_Type : Entity_Id;
70       Dyn    : Boolean := False) return Node_Id;
71    --  Build function to generate the image string for a task that is an
72    --  array component, concatenating the images of each index. To avoid
73    --  storage leaks, the string is built with successive slice assignments.
74    --  The flag Dyn indicates whether this is called for the initialization
75    --  procedure of an array of tasks, or for the name of a dynamically
76    --  created task that is assigned to an indexed component.
77
78    function Build_Task_Image_Function
79      (Loc   : Source_Ptr;
80       Decls : List_Id;
81       Stats : List_Id;
82       Res   : Entity_Id) return Node_Id;
83    --  Common processing for Task_Array_Image and Task_Record_Image.
84    --  Build function body that computes image.
85
86    procedure Build_Task_Image_Prefix
87       (Loc    : Source_Ptr;
88        Len    : out Entity_Id;
89        Res    : out Entity_Id;
90        Pos    : out Entity_Id;
91        Prefix : Entity_Id;
92        Sum    : Node_Id;
93        Decls  : List_Id;
94        Stats  : List_Id);
95    --  Common processing for Task_Array_Image and Task_Record_Image.
96    --  Create local variables and assign prefix of name to result string.
97
98    function Build_Task_Record_Image
99      (Loc    : Source_Ptr;
100       Id_Ref : Node_Id;
101       Dyn    : Boolean := False) return Node_Id;
102    --  Build function to generate the image string for a task that is a
103    --  record component. Concatenate name of variable with that of selector.
104    --  The flag Dyn indicates whether this is called for the initialization
105    --  procedure of record with task components, or for a dynamically
106    --  created task that is assigned to a selected component.
107
108    function Make_CW_Equivalent_Type
109      (T : Entity_Id;
110       E : Node_Id) return Entity_Id;
111    --  T is a class-wide type entity, E is the initial expression node that
112    --  constrains T in case such as: " X: T := E" or "new T'(E)"
113    --  This function returns the entity of the Equivalent type and inserts
114    --  on the fly the necessary declaration such as:
115    --
116    --    type anon is record
117    --       _parent : Root_Type (T); constrained with E discriminants (if any)
118    --       Extension : String (1 .. expr to match size of E);
119    --    end record;
120    --
121    --  This record is compatible with any object of the class of T thanks
122    --  to the first field and has the same size as E thanks to the second.
123
124    function Make_Literal_Range
125      (Loc         : Source_Ptr;
126       Literal_Typ : Entity_Id) return Node_Id;
127    --  Produce a Range node whose bounds are:
128    --    Low_Bound (Literal_Type) ..
129    --        Low_Bound (Literal_Type) + (Length (Literal_Typ) - 1)
130    --  this is used for expanding declarations like X : String := "sdfgdfg";
131    --
132    --  If the index type of the target array is not integer, we generate:
133    --     Low_Bound (Literal_Type) ..
134    --        Literal_Type'Val
135    --          (Literal_Type'Pos (Low_Bound (Literal_Type))
136    --             + (Length (Literal_Typ) -1))
137
138    function Make_Non_Empty_Check
139      (Loc : Source_Ptr;
140       N   : Node_Id) return Node_Id;
141    --  Produce a boolean expression checking that the unidimensional array
142    --  node N is not empty.
143
144    function New_Class_Wide_Subtype
145      (CW_Typ : Entity_Id;
146       N      : Node_Id) return Entity_Id;
147    --  Create an implicit subtype of CW_Typ attached to node N
148
149    ----------------------
150    -- Adjust_Condition --
151    ----------------------
152
153    procedure Adjust_Condition (N : Node_Id) is
154    begin
155       if No (N) then
156          return;
157       end if;
158
159       declare
160          Loc : constant Source_Ptr := Sloc (N);
161          T   : constant Entity_Id  := Etype (N);
162          Ti  : Entity_Id;
163
164       begin
165          --  For now, we simply ignore a call where the argument has no
166          --  type (probably case of unanalyzed condition), or has a type
167          --  that is not Boolean. This is because this is a pretty marginal
168          --  piece of functionality, and violations of these rules are
169          --  likely to be truly marginal (how much code uses Fortran Logical
170          --  as the barrier to a protected entry?) and we do not want to
171          --  blow up existing programs. We can change this to an assertion
172          --  after 3.12a is released ???
173
174          if No (T) or else not Is_Boolean_Type (T) then
175             return;
176          end if;
177
178          --  Apply validity checking if needed
179
180          if Validity_Checks_On and Validity_Check_Tests then
181             Ensure_Valid (N);
182          end if;
183
184          --  Immediate return if standard boolean, the most common case,
185          --  where nothing needs to be done.
186
187          if Base_Type (T) = Standard_Boolean then
188             return;
189          end if;
190
191          --  Case of zero/non-zero semantics or non-standard enumeration
192          --  representation. In each case, we rewrite the node as:
193
194          --      ityp!(N) /= False'Enum_Rep
195
196          --  where ityp is an integer type with large enough size to hold
197          --  any value of type T.
198
199          if Nonzero_Is_True (T) or else Has_Non_Standard_Rep (T) then
200             if Esize (T) <= Esize (Standard_Integer) then
201                Ti := Standard_Integer;
202             else
203                Ti := Standard_Long_Long_Integer;
204             end if;
205
206             Rewrite (N,
207               Make_Op_Ne (Loc,
208                 Left_Opnd  => Unchecked_Convert_To (Ti, N),
209                 Right_Opnd =>
210                   Make_Attribute_Reference (Loc,
211                     Attribute_Name => Name_Enum_Rep,
212                     Prefix         =>
213                       New_Occurrence_Of (First_Literal (T), Loc))));
214             Analyze_And_Resolve (N, Standard_Boolean);
215
216          else
217             Rewrite (N, Convert_To (Standard_Boolean, N));
218             Analyze_And_Resolve (N, Standard_Boolean);
219          end if;
220       end;
221    end Adjust_Condition;
222
223    ------------------------
224    -- Adjust_Result_Type --
225    ------------------------
226
227    procedure Adjust_Result_Type (N : Node_Id; T : Entity_Id) is
228    begin
229       --  Ignore call if current type is not Standard.Boolean
230
231       if Etype (N) /= Standard_Boolean then
232          return;
233       end if;
234
235       --  If result is already of correct type, nothing to do. Note that
236       --  this will get the most common case where everything has a type
237       --  of Standard.Boolean.
238
239       if Base_Type (T) = Standard_Boolean then
240          return;
241
242       else
243          declare
244             KP : constant Node_Kind := Nkind (Parent (N));
245
246          begin
247             --  If result is to be used as a Condition in the syntax, no need
248             --  to convert it back, since if it was changed to Standard.Boolean
249             --  using Adjust_Condition, that is just fine for this usage.
250
251             if KP in N_Raise_xxx_Error or else KP in N_Has_Condition then
252                return;
253
254             --  If result is an operand of another logical operation, no need
255             --  to reset its type, since Standard.Boolean is just fine, and
256             --  such operations always do Adjust_Condition on their operands.
257
258             elsif     KP in N_Op_Boolean
259               or else KP in N_Short_Circuit
260               or else KP = N_Op_Not
261             then
262                return;
263
264             --  Otherwise we perform a conversion from the current type,
265             --  which must be Standard.Boolean, to the desired type.
266
267             else
268                Set_Analyzed (N);
269                Rewrite (N, Convert_To (T, N));
270                Analyze_And_Resolve (N, T);
271             end if;
272          end;
273       end if;
274    end Adjust_Result_Type;
275
276    --------------------------
277    -- Append_Freeze_Action --
278    --------------------------
279
280    procedure Append_Freeze_Action (T : Entity_Id; N : Node_Id) is
281       Fnode : Node_Id;
282
283    begin
284       Ensure_Freeze_Node (T);
285       Fnode := Freeze_Node (T);
286
287       if No (Actions (Fnode)) then
288          Set_Actions (Fnode, New_List);
289       end if;
290
291       Append (N, Actions (Fnode));
292    end Append_Freeze_Action;
293
294    ---------------------------
295    -- Append_Freeze_Actions --
296    ---------------------------
297
298    procedure Append_Freeze_Actions (T : Entity_Id; L : List_Id) is
299       Fnode : constant Node_Id := Freeze_Node (T);
300
301    begin
302       if No (L) then
303          return;
304
305       else
306          if No (Actions (Fnode)) then
307             Set_Actions (Fnode, L);
308          else
309             Append_List (L, Actions (Fnode));
310          end if;
311       end if;
312    end Append_Freeze_Actions;
313
314    ------------------------
315    -- Build_Runtime_Call --
316    ------------------------
317
318    function Build_Runtime_Call (Loc : Source_Ptr; RE : RE_Id) return Node_Id is
319    begin
320       --  If entity is not available, we can skip making the call (this avoids
321       --  junk duplicated error messages in a number of cases).
322
323       if not RTE_Available (RE) then
324          return Make_Null_Statement (Loc);
325       else
326          return
327            Make_Procedure_Call_Statement (Loc,
328              Name => New_Reference_To (RTE (RE), Loc));
329       end if;
330    end Build_Runtime_Call;
331
332    ----------------------------
333    -- Build_Task_Array_Image --
334    ----------------------------
335
336    --  This function generates the body for a function that constructs the
337    --  image string for a task that is an array component. The function is
338    --  local to the init proc for the array type, and is called for each one
339    --  of the components. The constructed image has the form of an indexed
340    --  component, whose prefix is the outer variable of the array type.
341    --  The n-dimensional array type has known indices Index, Index2...
342    --  Id_Ref is an indexed component form created by the enclosing init proc.
343    --  Its successive indices are Val1, Val2, ... which are the loop variables
344    --  in the loops that call the individual task init proc on each component.
345
346    --  The generated function has the following structure:
347
348    --  function F return String is
349    --     Pref : string renames Task_Name;
350    --     T1   : String := Index1'Image (Val1);
351    --     ...
352    --     Tn   : String := indexn'image (Valn);
353    --     Len  : Integer := T1'Length + ... + Tn'Length + n + 1;
354    --     --  Len includes commas and the end parentheses.
355    --     Res  : String (1..Len);
356    --     Pos  : Integer := Pref'Length;
357    --
358    --  begin
359    --     Res (1 .. Pos) := Pref;
360    --     Pos := Pos + 1;
361    --     Res (Pos)    := '(';
362    --     Pos := Pos + 1;
363    --     Res (Pos .. Pos + T1'Length - 1) := T1;
364    --     Pos := Pos + T1'Length;
365    --     Res (Pos) := '.';
366    --     Pos := Pos + 1;
367    --     ...
368    --     Res (Pos .. Pos + Tn'Length - 1) := Tn;
369    --     Res (Len) := ')';
370    --
371    --     return Res;
372    --  end F;
373    --
374    --  Needless to say, multidimensional arrays of tasks are rare enough
375    --  that the bulkiness of this code is not really a concern.
376
377    function Build_Task_Array_Image
378      (Loc    : Source_Ptr;
379       Id_Ref : Node_Id;
380       A_Type : Entity_Id;
381       Dyn    : Boolean := False) return Node_Id
382    is
383       Dims : constant Nat := Number_Dimensions (A_Type);
384       --  Number of dimensions for array of tasks
385
386       Temps : array (1 .. Dims) of Entity_Id;
387       --  Array of temporaries to hold string for each index
388
389       Indx : Node_Id;
390       --  Index expression
391
392       Len : Entity_Id;
393       --  Total length of generated name
394
395       Pos : Entity_Id;
396       --  Running index for substring assignments
397
398       Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
399       --  Name of enclosing variable, prefix of resulting name
400
401       Res : Entity_Id;
402       --  String to hold result
403
404       Val : Node_Id;
405       --  Value of successive indices
406
407       Sum : Node_Id;
408       --  Expression to compute total size of string
409
410       T : Entity_Id;
411       --  Entity for name at one index position
412
413       Decls : constant List_Id := New_List;
414       Stats : constant List_Id := New_List;
415
416    begin
417       --  For a dynamic task, the name comes from the target variable.
418       --  For a static one it is a formal of the enclosing init proc.
419
420       if Dyn then
421          Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
422          Append_To (Decls,
423            Make_Object_Declaration (Loc,
424              Defining_Identifier => Pref,
425              Object_Definition => New_Occurrence_Of (Standard_String, Loc),
426              Expression =>
427                Make_String_Literal (Loc,
428                  Strval => String_From_Name_Buffer)));
429
430       else
431          Append_To (Decls,
432            Make_Object_Renaming_Declaration (Loc,
433              Defining_Identifier => Pref,
434              Subtype_Mark        => New_Occurrence_Of (Standard_String, Loc),
435              Name                => Make_Identifier (Loc, Name_uTask_Name)));
436       end if;
437
438       Indx := First_Index (A_Type);
439       Val  := First (Expressions (Id_Ref));
440
441       for J in 1 .. Dims loop
442          T := Make_Temporary (Loc, 'T');
443          Temps (J) := T;
444
445          Append_To (Decls,
446             Make_Object_Declaration (Loc,
447                Defining_Identifier => T,
448                Object_Definition => New_Occurrence_Of (Standard_String, Loc),
449                Expression =>
450                  Make_Attribute_Reference (Loc,
451                    Attribute_Name => Name_Image,
452                    Prefix         => New_Occurrence_Of (Etype (Indx), Loc),
453                    Expressions    => New_List (New_Copy_Tree (Val)))));
454
455          Next_Index (Indx);
456          Next (Val);
457       end loop;
458
459       Sum := Make_Integer_Literal (Loc, Dims + 1);
460
461       Sum :=
462         Make_Op_Add (Loc,
463           Left_Opnd => Sum,
464           Right_Opnd =>
465            Make_Attribute_Reference (Loc,
466              Attribute_Name => Name_Length,
467              Prefix =>
468                New_Occurrence_Of (Pref, Loc),
469              Expressions => New_List (Make_Integer_Literal (Loc, 1))));
470
471       for J in 1 .. Dims loop
472          Sum :=
473             Make_Op_Add (Loc,
474              Left_Opnd => Sum,
475              Right_Opnd =>
476               Make_Attribute_Reference (Loc,
477                 Attribute_Name => Name_Length,
478                 Prefix =>
479                   New_Occurrence_Of (Temps (J), Loc),
480                 Expressions => New_List (Make_Integer_Literal (Loc, 1))));
481       end loop;
482
483       Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
484
485       Set_Character_Literal_Name (Char_Code (Character'Pos ('(')));
486
487       Append_To (Stats,
488          Make_Assignment_Statement (Loc,
489            Name => Make_Indexed_Component (Loc,
490               Prefix => New_Occurrence_Of (Res, Loc),
491               Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
492            Expression =>
493              Make_Character_Literal (Loc,
494                Chars => Name_Find,
495                Char_Literal_Value =>
496                  UI_From_Int (Character'Pos ('(')))));
497
498       Append_To (Stats,
499          Make_Assignment_Statement (Loc,
500             Name => New_Occurrence_Of (Pos, Loc),
501             Expression =>
502               Make_Op_Add (Loc,
503                 Left_Opnd => New_Occurrence_Of (Pos, Loc),
504                 Right_Opnd => Make_Integer_Literal (Loc, 1))));
505
506       for J in 1 .. Dims loop
507
508          Append_To (Stats,
509             Make_Assignment_Statement (Loc,
510               Name => Make_Slice (Loc,
511                  Prefix => New_Occurrence_Of (Res, Loc),
512                  Discrete_Range  =>
513                    Make_Range (Loc,
514                       Low_Bound => New_Occurrence_Of  (Pos, Loc),
515                       High_Bound => Make_Op_Subtract (Loc,
516                         Left_Opnd =>
517                           Make_Op_Add (Loc,
518                             Left_Opnd => New_Occurrence_Of (Pos, Loc),
519                             Right_Opnd =>
520                               Make_Attribute_Reference (Loc,
521                                 Attribute_Name => Name_Length,
522                                 Prefix =>
523                                   New_Occurrence_Of (Temps (J), Loc),
524                                 Expressions =>
525                                   New_List (Make_Integer_Literal (Loc, 1)))),
526                          Right_Opnd => Make_Integer_Literal (Loc, 1)))),
527
528               Expression => New_Occurrence_Of (Temps (J), Loc)));
529
530          if J < Dims then
531             Append_To (Stats,
532                Make_Assignment_Statement (Loc,
533                   Name => New_Occurrence_Of (Pos, Loc),
534                   Expression =>
535                     Make_Op_Add (Loc,
536                       Left_Opnd => New_Occurrence_Of (Pos, Loc),
537                       Right_Opnd =>
538                         Make_Attribute_Reference (Loc,
539                           Attribute_Name => Name_Length,
540                             Prefix => New_Occurrence_Of (Temps (J), Loc),
541                             Expressions =>
542                               New_List (Make_Integer_Literal (Loc, 1))))));
543
544             Set_Character_Literal_Name (Char_Code (Character'Pos (',')));
545
546             Append_To (Stats,
547                Make_Assignment_Statement (Loc,
548                  Name => Make_Indexed_Component (Loc,
549                     Prefix => New_Occurrence_Of (Res, Loc),
550                     Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
551                  Expression =>
552                    Make_Character_Literal (Loc,
553                      Chars => Name_Find,
554                      Char_Literal_Value =>
555                        UI_From_Int (Character'Pos (',')))));
556
557             Append_To (Stats,
558               Make_Assignment_Statement (Loc,
559                 Name => New_Occurrence_Of (Pos, Loc),
560                   Expression =>
561                     Make_Op_Add (Loc,
562                       Left_Opnd => New_Occurrence_Of (Pos, Loc),
563                       Right_Opnd => Make_Integer_Literal (Loc, 1))));
564          end if;
565       end loop;
566
567       Set_Character_Literal_Name (Char_Code (Character'Pos (')')));
568
569       Append_To (Stats,
570          Make_Assignment_Statement (Loc,
571            Name => Make_Indexed_Component (Loc,
572               Prefix => New_Occurrence_Of (Res, Loc),
573               Expressions => New_List (New_Occurrence_Of (Len, Loc))),
574            Expression =>
575              Make_Character_Literal (Loc,
576                Chars => Name_Find,
577                Char_Literal_Value =>
578                  UI_From_Int (Character'Pos (')')))));
579       return Build_Task_Image_Function (Loc, Decls, Stats, Res);
580    end Build_Task_Array_Image;
581
582    ----------------------------
583    -- Build_Task_Image_Decls --
584    ----------------------------
585
586    function Build_Task_Image_Decls
587      (Loc          : Source_Ptr;
588       Id_Ref       : Node_Id;
589       A_Type       : Entity_Id;
590       In_Init_Proc : Boolean := False) return List_Id
591    is
592       Decls  : constant List_Id   := New_List;
593       T_Id   : Entity_Id := Empty;
594       Decl   : Node_Id;
595       Expr   : Node_Id   := Empty;
596       Fun    : Node_Id   := Empty;
597       Is_Dyn : constant Boolean :=
598                  Nkind (Parent (Id_Ref)) = N_Assignment_Statement
599                    and then
600                  Nkind (Expression (Parent (Id_Ref))) = N_Allocator;
601
602    begin
603       --  If Discard_Names or No_Implicit_Heap_Allocations are in effect,
604       --  generate a dummy declaration only.
605
606       if Restriction_Active (No_Implicit_Heap_Allocations)
607         or else Global_Discard_Names
608       then
609          T_Id := Make_Temporary (Loc, 'J');
610          Name_Len := 0;
611
612          return
613            New_List (
614              Make_Object_Declaration (Loc,
615                Defining_Identifier => T_Id,
616                Object_Definition => New_Occurrence_Of (Standard_String, Loc),
617                Expression =>
618                  Make_String_Literal (Loc,
619                    Strval => String_From_Name_Buffer)));
620
621       else
622          if Nkind (Id_Ref) = N_Identifier
623            or else Nkind (Id_Ref) = N_Defining_Identifier
624          then
625             --  For a simple variable, the image of the task is built from
626             --  the name of the variable. To avoid possible conflict with
627             --  the anonymous type created for a single protected object,
628             --  add a numeric suffix.
629
630             T_Id :=
631               Make_Defining_Identifier (Loc,
632                 New_External_Name (Chars (Id_Ref), 'T', 1));
633
634             Get_Name_String (Chars (Id_Ref));
635
636             Expr :=
637               Make_String_Literal (Loc,
638                 Strval => String_From_Name_Buffer);
639
640          elsif Nkind (Id_Ref) = N_Selected_Component then
641             T_Id :=
642               Make_Defining_Identifier (Loc,
643                 New_External_Name (Chars (Selector_Name (Id_Ref)), 'T'));
644             Fun := Build_Task_Record_Image (Loc, Id_Ref, Is_Dyn);
645
646          elsif Nkind (Id_Ref) = N_Indexed_Component then
647             T_Id :=
648               Make_Defining_Identifier (Loc,
649                 New_External_Name (Chars (A_Type), 'N'));
650
651             Fun := Build_Task_Array_Image (Loc, Id_Ref, A_Type, Is_Dyn);
652          end if;
653       end if;
654
655       if Present (Fun) then
656          Append (Fun, Decls);
657          Expr := Make_Function_Call (Loc,
658            Name => New_Occurrence_Of (Defining_Entity (Fun), Loc));
659
660          if not In_Init_Proc and then VM_Target = No_VM then
661             Set_Uses_Sec_Stack (Defining_Entity (Fun));
662          end if;
663       end if;
664
665       Decl := Make_Object_Declaration (Loc,
666         Defining_Identifier => T_Id,
667         Object_Definition   => New_Occurrence_Of (Standard_String, Loc),
668         Constant_Present    => True,
669         Expression          => Expr);
670
671       Append (Decl, Decls);
672       return Decls;
673    end Build_Task_Image_Decls;
674
675    -------------------------------
676    -- Build_Task_Image_Function --
677    -------------------------------
678
679    function Build_Task_Image_Function
680      (Loc   : Source_Ptr;
681       Decls : List_Id;
682       Stats : List_Id;
683       Res   : Entity_Id) return Node_Id
684    is
685       Spec : Node_Id;
686
687    begin
688       Append_To (Stats,
689         Make_Simple_Return_Statement (Loc,
690           Expression => New_Occurrence_Of (Res, Loc)));
691
692       Spec := Make_Function_Specification (Loc,
693         Defining_Unit_Name => Make_Temporary (Loc, 'F'),
694         Result_Definition  => New_Occurrence_Of (Standard_String, Loc));
695
696       --  Calls to 'Image use the secondary stack, which must be cleaned
697       --  up after the task name is built.
698
699       return Make_Subprogram_Body (Loc,
700          Specification => Spec,
701          Declarations => Decls,
702          Handled_Statement_Sequence =>
703            Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats));
704    end Build_Task_Image_Function;
705
706    -----------------------------
707    -- Build_Task_Image_Prefix --
708    -----------------------------
709
710    procedure Build_Task_Image_Prefix
711       (Loc    : Source_Ptr;
712        Len    : out Entity_Id;
713        Res    : out Entity_Id;
714        Pos    : out Entity_Id;
715        Prefix : Entity_Id;
716        Sum    : Node_Id;
717        Decls  : List_Id;
718        Stats  : List_Id)
719    is
720    begin
721       Len := Make_Temporary (Loc, 'L', Sum);
722
723       Append_To (Decls,
724         Make_Object_Declaration (Loc,
725           Defining_Identifier => Len,
726           Object_Definition   => New_Occurrence_Of (Standard_Integer, Loc),
727           Expression          => Sum));
728
729       Res := Make_Temporary (Loc, 'R');
730
731       Append_To (Decls,
732          Make_Object_Declaration (Loc,
733             Defining_Identifier => Res,
734             Object_Definition =>
735                Make_Subtype_Indication (Loc,
736                   Subtype_Mark => New_Occurrence_Of (Standard_String, Loc),
737                Constraint =>
738                  Make_Index_Or_Discriminant_Constraint (Loc,
739                    Constraints =>
740                      New_List (
741                        Make_Range (Loc,
742                          Low_Bound => Make_Integer_Literal (Loc, 1),
743                          High_Bound => New_Occurrence_Of (Len, Loc)))))));
744
745       Pos := Make_Temporary (Loc, 'P');
746
747       Append_To (Decls,
748          Make_Object_Declaration (Loc,
749             Defining_Identifier => Pos,
750             Object_Definition   => New_Occurrence_Of (Standard_Integer, Loc)));
751
752       --  Pos := Prefix'Length;
753
754       Append_To (Stats,
755          Make_Assignment_Statement (Loc,
756             Name => New_Occurrence_Of (Pos, Loc),
757             Expression =>
758               Make_Attribute_Reference (Loc,
759                 Attribute_Name => Name_Length,
760                 Prefix         => New_Occurrence_Of (Prefix, Loc),
761                 Expressions    => New_List (Make_Integer_Literal (Loc, 1)))));
762
763       --  Res (1 .. Pos) := Prefix;
764
765       Append_To (Stats,
766         Make_Assignment_Statement (Loc,
767           Name =>
768             Make_Slice (Loc,
769               Prefix          => New_Occurrence_Of (Res, Loc),
770               Discrete_Range  =>
771                 Make_Range (Loc,
772                    Low_Bound  => Make_Integer_Literal (Loc, 1),
773                    High_Bound => New_Occurrence_Of (Pos, Loc))),
774
775           Expression => New_Occurrence_Of (Prefix, Loc)));
776
777       Append_To (Stats,
778          Make_Assignment_Statement (Loc,
779             Name       => New_Occurrence_Of (Pos, Loc),
780             Expression =>
781               Make_Op_Add (Loc,
782                 Left_Opnd  => New_Occurrence_Of (Pos, Loc),
783                 Right_Opnd => Make_Integer_Literal (Loc, 1))));
784    end Build_Task_Image_Prefix;
785
786    -----------------------------
787    -- Build_Task_Record_Image --
788    -----------------------------
789
790    function Build_Task_Record_Image
791      (Loc    : Source_Ptr;
792       Id_Ref : Node_Id;
793       Dyn    : Boolean := False) return Node_Id
794    is
795       Len : Entity_Id;
796       --  Total length of generated name
797
798       Pos : Entity_Id;
799       --  Index into result
800
801       Res : Entity_Id;
802       --  String to hold result
803
804       Pref : constant Entity_Id := Make_Temporary (Loc, 'P');
805       --  Name of enclosing variable, prefix of resulting name
806
807       Sum : Node_Id;
808       --  Expression to compute total size of string
809
810       Sel : Entity_Id;
811       --  Entity for selector name
812
813       Decls : constant List_Id := New_List;
814       Stats : constant List_Id := New_List;
815
816    begin
817       --  For a dynamic task, the name comes from the target variable. For a
818       --  static one it is a formal of the enclosing init proc.
819
820       if Dyn then
821          Get_Name_String (Chars (Entity (Prefix (Id_Ref))));
822          Append_To (Decls,
823            Make_Object_Declaration (Loc,
824              Defining_Identifier => Pref,
825              Object_Definition => New_Occurrence_Of (Standard_String, Loc),
826              Expression =>
827                Make_String_Literal (Loc,
828                  Strval => String_From_Name_Buffer)));
829
830       else
831          Append_To (Decls,
832            Make_Object_Renaming_Declaration (Loc,
833              Defining_Identifier => Pref,
834              Subtype_Mark        => New_Occurrence_Of (Standard_String, Loc),
835              Name                => Make_Identifier (Loc, Name_uTask_Name)));
836       end if;
837
838       Sel := Make_Temporary (Loc, 'S');
839
840       Get_Name_String (Chars (Selector_Name (Id_Ref)));
841
842       Append_To (Decls,
843          Make_Object_Declaration (Loc,
844            Defining_Identifier => Sel,
845            Object_Definition   => New_Occurrence_Of (Standard_String, Loc),
846            Expression          =>
847              Make_String_Literal (Loc,
848                Strval => String_From_Name_Buffer)));
849
850       Sum := Make_Integer_Literal (Loc, Nat (Name_Len + 1));
851
852       Sum :=
853         Make_Op_Add (Loc,
854           Left_Opnd => Sum,
855           Right_Opnd =>
856            Make_Attribute_Reference (Loc,
857              Attribute_Name => Name_Length,
858              Prefix =>
859                New_Occurrence_Of (Pref, Loc),
860              Expressions => New_List (Make_Integer_Literal (Loc, 1))));
861
862       Build_Task_Image_Prefix (Loc, Len, Res, Pos, Pref, Sum, Decls, Stats);
863
864       Set_Character_Literal_Name (Char_Code (Character'Pos ('.')));
865
866       --  Res (Pos) := '.';
867
868       Append_To (Stats,
869          Make_Assignment_Statement (Loc,
870            Name => Make_Indexed_Component (Loc,
871               Prefix => New_Occurrence_Of (Res, Loc),
872               Expressions => New_List (New_Occurrence_Of (Pos, Loc))),
873            Expression =>
874              Make_Character_Literal (Loc,
875                Chars => Name_Find,
876                Char_Literal_Value =>
877                  UI_From_Int (Character'Pos ('.')))));
878
879       Append_To (Stats,
880         Make_Assignment_Statement (Loc,
881           Name => New_Occurrence_Of (Pos, Loc),
882           Expression =>
883             Make_Op_Add (Loc,
884               Left_Opnd => New_Occurrence_Of (Pos, Loc),
885               Right_Opnd => Make_Integer_Literal (Loc, 1))));
886
887       --  Res (Pos .. Len) := Selector;
888
889       Append_To (Stats,
890         Make_Assignment_Statement (Loc,
891           Name => Make_Slice (Loc,
892              Prefix => New_Occurrence_Of (Res, Loc),
893              Discrete_Range  =>
894                Make_Range (Loc,
895                  Low_Bound  => New_Occurrence_Of (Pos, Loc),
896                  High_Bound => New_Occurrence_Of (Len, Loc))),
897           Expression => New_Occurrence_Of (Sel, Loc)));
898
899       return Build_Task_Image_Function (Loc, Decls, Stats, Res);
900    end Build_Task_Record_Image;
901
902    ----------------------------------
903    -- Component_May_Be_Bit_Aligned --
904    ----------------------------------
905
906    function Component_May_Be_Bit_Aligned (Comp : Entity_Id) return Boolean is
907       UT : Entity_Id;
908
909    begin
910       --  If no component clause, then everything is fine, since the back end
911       --  never bit-misaligns by default, even if there is a pragma Packed for
912       --  the record.
913
914       if No (Comp) or else No (Component_Clause (Comp)) then
915          return False;
916       end if;
917
918       UT := Underlying_Type (Etype (Comp));
919
920       --  It is only array and record types that cause trouble
921
922       if not Is_Record_Type (UT)
923         and then not Is_Array_Type (UT)
924       then
925          return False;
926
927       --  If we know that we have a small (64 bits or less) record or small
928       --  bit-packed array, then everything is fine, since the back end can
929       --  handle these cases correctly.
930
931       elsif Esize (Comp) <= 64
932         and then (Is_Record_Type (UT)
933                    or else Is_Bit_Packed_Array (UT))
934       then
935          return False;
936
937       --  Otherwise if the component is not byte aligned, we know we have the
938       --  nasty unaligned case.
939
940       elsif Normalized_First_Bit (Comp) /= Uint_0
941         or else Esize (Comp) mod System_Storage_Unit /= Uint_0
942       then
943          return True;
944
945       --  If we are large and byte aligned, then OK at this level
946
947       else
948          return False;
949       end if;
950    end Component_May_Be_Bit_Aligned;
951
952    -----------------------------------
953    -- Corresponding_Runtime_Package --
954    -----------------------------------
955
956    function Corresponding_Runtime_Package (Typ : Entity_Id) return RTU_Id is
957       Pkg_Id : RTU_Id := RTU_Null;
958
959    begin
960       pragma Assert (Is_Concurrent_Type (Typ));
961
962       if Ekind (Typ) in Protected_Kind then
963          if Has_Entries (Typ)
964            or else Has_Interrupt_Handler (Typ)
965            or else (Has_Attach_Handler (Typ)
966                       and then not Restricted_Profile)
967
968             --  A protected type without entries that covers an interface and
969             --  overrides the abstract routines with protected procedures is
970             --  considered equivalent to a protected type with entries in the
971             --  context of dispatching select statements. It is sufficient to
972             --  check for the presence of an interface list in the declaration
973             --  node to recognize this case.
974
975            or else Present (Interface_List (Parent (Typ)))
976          then
977             if Abort_Allowed
978               or else Restriction_Active (No_Entry_Queue) = False
979               or else Number_Entries (Typ) > 1
980               or else (Has_Attach_Handler (Typ)
981                          and then not Restricted_Profile)
982             then
983                Pkg_Id := System_Tasking_Protected_Objects_Entries;
984             else
985                Pkg_Id := System_Tasking_Protected_Objects_Single_Entry;
986             end if;
987
988          else
989             Pkg_Id := System_Tasking_Protected_Objects;
990          end if;
991       end if;
992
993       return Pkg_Id;
994    end Corresponding_Runtime_Package;
995
996    -------------------------------
997    -- Convert_To_Actual_Subtype --
998    -------------------------------
999
1000    procedure Convert_To_Actual_Subtype (Exp : Entity_Id) is
1001       Act_ST : Entity_Id;
1002
1003    begin
1004       Act_ST := Get_Actual_Subtype (Exp);
1005
1006       if Act_ST = Etype (Exp) then
1007          return;
1008
1009       else
1010          Rewrite (Exp,
1011            Convert_To (Act_ST, Relocate_Node (Exp)));
1012          Analyze_And_Resolve (Exp, Act_ST);
1013       end if;
1014    end Convert_To_Actual_Subtype;
1015
1016    -----------------------------------
1017    -- Current_Sem_Unit_Declarations --
1018    -----------------------------------
1019
1020    function Current_Sem_Unit_Declarations return List_Id is
1021       U     : Node_Id := Unit (Cunit (Current_Sem_Unit));
1022       Decls : List_Id;
1023
1024    begin
1025       --  If the current unit is a package body, locate the visible
1026       --  declarations of the package spec.
1027
1028       if Nkind (U) = N_Package_Body then
1029          U := Unit (Library_Unit (Cunit (Current_Sem_Unit)));
1030       end if;
1031
1032       if Nkind (U) = N_Package_Declaration then
1033          U := Specification (U);
1034          Decls := Visible_Declarations (U);
1035
1036          if No (Decls) then
1037             Decls := New_List;
1038             Set_Visible_Declarations (U, Decls);
1039          end if;
1040
1041       else
1042          Decls := Declarations (U);
1043
1044          if No (Decls) then
1045             Decls := New_List;
1046             Set_Declarations (U, Decls);
1047          end if;
1048       end if;
1049
1050       return Decls;
1051    end Current_Sem_Unit_Declarations;
1052
1053    -----------------------
1054    -- Duplicate_Subexpr --
1055    -----------------------
1056
1057    function Duplicate_Subexpr
1058      (Exp      : Node_Id;
1059       Name_Req : Boolean := False) return Node_Id
1060    is
1061    begin
1062       Remove_Side_Effects (Exp, Name_Req);
1063       return New_Copy_Tree (Exp);
1064    end Duplicate_Subexpr;
1065
1066    ---------------------------------
1067    -- Duplicate_Subexpr_No_Checks --
1068    ---------------------------------
1069
1070    function Duplicate_Subexpr_No_Checks
1071      (Exp      : Node_Id;
1072       Name_Req : Boolean := False) return Node_Id
1073    is
1074       New_Exp : Node_Id;
1075
1076    begin
1077       Remove_Side_Effects (Exp, Name_Req);
1078       New_Exp := New_Copy_Tree (Exp);
1079       Remove_Checks (New_Exp);
1080       return New_Exp;
1081    end Duplicate_Subexpr_No_Checks;
1082
1083    -----------------------------------
1084    -- Duplicate_Subexpr_Move_Checks --
1085    -----------------------------------
1086
1087    function Duplicate_Subexpr_Move_Checks
1088      (Exp      : Node_Id;
1089       Name_Req : Boolean := False) return Node_Id
1090    is
1091       New_Exp : Node_Id;
1092
1093    begin
1094       Remove_Side_Effects (Exp, Name_Req);
1095       New_Exp := New_Copy_Tree (Exp);
1096       Remove_Checks (Exp);
1097       return New_Exp;
1098    end Duplicate_Subexpr_Move_Checks;
1099
1100    --------------------
1101    -- Ensure_Defined --
1102    --------------------
1103
1104    procedure Ensure_Defined (Typ : Entity_Id; N : Node_Id) is
1105       IR : Node_Id;
1106
1107    begin
1108       --  An itype reference must only be created if this is a local itype, so
1109       --  that gigi can elaborate it on the proper objstack.
1110
1111       if Is_Itype (Typ)
1112         and then Scope (Typ) = Current_Scope
1113       then
1114          IR := Make_Itype_Reference (Sloc (N));
1115          Set_Itype (IR, Typ);
1116          Insert_Action (N, IR);
1117       end if;
1118    end Ensure_Defined;
1119
1120    --------------------
1121    -- Entry_Names_OK --
1122    --------------------
1123
1124    function Entry_Names_OK return Boolean is
1125    begin
1126       return
1127         not Restricted_Profile
1128           and then not Global_Discard_Names
1129           and then not Restriction_Active (No_Implicit_Heap_Allocations)
1130           and then not Restriction_Active (No_Local_Allocators);
1131    end Entry_Names_OK;
1132
1133    ---------------------
1134    -- Evolve_And_Then --
1135    ---------------------
1136
1137    procedure Evolve_And_Then (Cond : in out Node_Id; Cond1 : Node_Id) is
1138    begin
1139       if No (Cond) then
1140          Cond := Cond1;
1141       else
1142          Cond :=
1143            Make_And_Then (Sloc (Cond1),
1144              Left_Opnd  => Cond,
1145              Right_Opnd => Cond1);
1146       end if;
1147    end Evolve_And_Then;
1148
1149    --------------------
1150    -- Evolve_Or_Else --
1151    --------------------
1152
1153    procedure Evolve_Or_Else (Cond : in out Node_Id; Cond1 : Node_Id) is
1154    begin
1155       if No (Cond) then
1156          Cond := Cond1;
1157       else
1158          Cond :=
1159            Make_Or_Else (Sloc (Cond1),
1160              Left_Opnd  => Cond,
1161              Right_Opnd => Cond1);
1162       end if;
1163    end Evolve_Or_Else;
1164
1165    ------------------------------
1166    -- Expand_Subtype_From_Expr --
1167    ------------------------------
1168
1169    --  This function is applicable for both static and dynamic allocation of
1170    --  objects which are constrained by an initial expression. Basically it
1171    --  transforms an unconstrained subtype indication into a constrained one.
1172    --  The expression may also be transformed in certain cases in order to
1173    --  avoid multiple evaluation. In the static allocation case, the general
1174    --  scheme is:
1175
1176    --     Val : T := Expr;
1177
1178    --        is transformed into
1179
1180    --     Val : Constrained_Subtype_of_T := Maybe_Modified_Expr;
1181    --
1182    --  Here are the main cases :
1183    --
1184    --  <if Expr is a Slice>
1185    --    Val : T ([Index_Subtype (Expr)]) := Expr;
1186    --
1187    --  <elsif Expr is a String Literal>
1188    --    Val : T (T'First .. T'First + Length (string literal) - 1) := Expr;
1189    --
1190    --  <elsif Expr is Constrained>
1191    --    subtype T is Type_Of_Expr
1192    --    Val : T := Expr;
1193    --
1194    --  <elsif Expr is an entity_name>
1195    --    Val : T (constraints taken from Expr) := Expr;
1196    --
1197    --  <else>
1198    --    type Axxx is access all T;
1199    --    Rval : Axxx := Expr'ref;
1200    --    Val  : T (constraints taken from Rval) := Rval.all;
1201
1202    --    ??? note: when the Expression is allocated in the secondary stack
1203    --              we could use it directly instead of copying it by declaring
1204    --              Val : T (...) renames Rval.all
1205
1206    procedure Expand_Subtype_From_Expr
1207      (N             : Node_Id;
1208       Unc_Type      : Entity_Id;
1209       Subtype_Indic : Node_Id;
1210       Exp           : Node_Id)
1211    is
1212       Loc     : constant Source_Ptr := Sloc (N);
1213       Exp_Typ : constant Entity_Id  := Etype (Exp);
1214       T       : Entity_Id;
1215
1216    begin
1217       --  In general we cannot build the subtype if expansion is disabled,
1218       --  because internal entities may not have been defined. However, to
1219       --  avoid some cascaded errors, we try to continue when the expression is
1220       --  an array (or string), because it is safe to compute the bounds. It is
1221       --  in fact required to do so even in a generic context, because there
1222       --  may be constants that depend on the bounds of a string literal, both
1223       --  standard string types and more generally arrays of characters.
1224
1225       if not Expander_Active
1226         and then (No (Etype (Exp))
1227                    or else not Is_String_Type (Etype (Exp)))
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 limited,
1361       --  because in this case the expression cannot be copied, and its use can
1362       --  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 of an
1828             --  AND or AND THEN (since we are not doing sufficiently general
1829             --  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,
2007             --  and 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                   --  If the enclosing expression is already analyzed, as
2391                   --  is the case for nested elaboration checks, insert the
2392                   --  conditional further out.
2393
2394                   if Analyzed (P) then
2395                      null;
2396
2397                   --  Actions belong to the then expression, temporarily place
2398                   --  them as Then_Actions of the conditional expr. They will
2399                   --  be moved to the proper place later when the conditional
2400                   --  expression is expanded.
2401
2402                   elsif N = ThenX then
2403                      if Present (Then_Actions (P)) then
2404                         Insert_List_After_And_Analyze
2405                           (Last (Then_Actions (P)), Ins_Actions);
2406                      else
2407                         Set_Then_Actions (P, Ins_Actions);
2408                         Analyze_List (Then_Actions (P));
2409                      end if;
2410
2411                      return;
2412
2413                   --  Actions belong to the else expression, temporarily
2414                   --  place them as Else_Actions of the conditional expr.
2415                   --  They will be moved to the proper place later when
2416                   --  the conditional expression is expanded.
2417
2418                   elsif N = ElseX then
2419                      if Present (Else_Actions (P)) then
2420                         Insert_List_After_And_Analyze
2421                           (Last (Else_Actions (P)), Ins_Actions);
2422                      else
2423                         Set_Else_Actions (P, Ins_Actions);
2424                         Analyze_List (Else_Actions (P));
2425                      end if;
2426
2427                      return;
2428
2429                   --  Actions belong to the condition. In this case they are
2430                   --  unconditionally executed, and so we can continue the
2431                   --  search for the proper insert point.
2432
2433                   else
2434                      null;
2435                   end if;
2436                end;
2437
2438             --  Alternative of case expression, we place the action in the
2439             --  Actions field of the case expression alternative, this will
2440             --  be handled when the case expression is expanded.
2441
2442             when N_Case_Expression_Alternative =>
2443                if Present (Actions (P)) then
2444                   Insert_List_After_And_Analyze
2445                     (Last (Actions (P)), Ins_Actions);
2446                else
2447                   Set_Actions (P, Ins_Actions);
2448                   Analyze_List (Then_Actions (P));
2449                end if;
2450
2451                return;
2452
2453             --  Case of appearing within an Expressions_With_Actions node. We
2454             --  prepend the actions to the list of actions already there.
2455
2456             when N_Expression_With_Actions =>
2457                Prepend_List (Ins_Actions, Actions (P));
2458                return;
2459
2460             --  Case of appearing in the condition of a while expression or
2461             --  elsif. We insert the actions into the Condition_Actions field.
2462             --  They will be moved further out when the while loop or elsif
2463             --  is analyzed.
2464
2465             when N_Iteration_Scheme |
2466                  N_Elsif_Part
2467             =>
2468                if N = Condition (P) then
2469                   if Present (Condition_Actions (P)) then
2470                      Insert_List_After_And_Analyze
2471                        (Last (Condition_Actions (P)), Ins_Actions);
2472                   else
2473                      Set_Condition_Actions (P, Ins_Actions);
2474
2475                      --  Set the parent of the insert actions explicitly. This
2476                      --  is not a syntactic field, but we need the parent field
2477                      --  set, in particular so that freeze can understand that
2478                      --  it is dealing with condition actions, and properly
2479                      --  insert the freezing actions.
2480
2481                      Set_Parent (Ins_Actions, P);
2482                      Analyze_List (Condition_Actions (P));
2483                   end if;
2484
2485                   return;
2486                end if;
2487
2488             --  Statements, declarations, pragmas, representation clauses
2489
2490             when
2491                --  Statements
2492
2493                N_Procedure_Call_Statement               |
2494                N_Statement_Other_Than_Procedure_Call    |
2495
2496                --  Pragmas
2497
2498                N_Pragma                                 |
2499
2500                --  Representation_Clause
2501
2502                N_At_Clause                              |
2503                N_Attribute_Definition_Clause            |
2504                N_Enumeration_Representation_Clause      |
2505                N_Record_Representation_Clause           |
2506
2507                --  Declarations
2508
2509                N_Abstract_Subprogram_Declaration        |
2510                N_Entry_Body                             |
2511                N_Exception_Declaration                  |
2512                N_Exception_Renaming_Declaration         |
2513                N_Formal_Abstract_Subprogram_Declaration |
2514                N_Formal_Concrete_Subprogram_Declaration |
2515                N_Formal_Object_Declaration              |
2516                N_Formal_Type_Declaration                |
2517                N_Full_Type_Declaration                  |
2518                N_Function_Instantiation                 |
2519                N_Generic_Function_Renaming_Declaration  |
2520                N_Generic_Package_Declaration            |
2521                N_Generic_Package_Renaming_Declaration   |
2522                N_Generic_Procedure_Renaming_Declaration |
2523                N_Generic_Subprogram_Declaration         |
2524                N_Implicit_Label_Declaration             |
2525                N_Incomplete_Type_Declaration            |
2526                N_Number_Declaration                     |
2527                N_Object_Declaration                     |
2528                N_Object_Renaming_Declaration            |
2529                N_Package_Body                           |
2530                N_Package_Body_Stub                      |
2531                N_Package_Declaration                    |
2532                N_Package_Instantiation                  |
2533                N_Package_Renaming_Declaration           |
2534                N_Private_Extension_Declaration          |
2535                N_Private_Type_Declaration               |
2536                N_Procedure_Instantiation                |
2537                N_Protected_Body                         |
2538                N_Protected_Body_Stub                    |
2539                N_Protected_Type_Declaration             |
2540                N_Single_Task_Declaration                |
2541                N_Subprogram_Body                        |
2542                N_Subprogram_Body_Stub                   |
2543                N_Subprogram_Declaration                 |
2544                N_Subprogram_Renaming_Declaration        |
2545                N_Subtype_Declaration                    |
2546                N_Task_Body                              |
2547                N_Task_Body_Stub                         |
2548                N_Task_Type_Declaration                  |
2549
2550                --  Freeze entity behaves like a declaration or statement
2551
2552                N_Freeze_Entity
2553             =>
2554                --  Do not insert here if the item is not a list member (this
2555                --  happens for example with a triggering statement, and the
2556                --  proper approach is to insert before the entire select).
2557
2558                if not Is_List_Member (P) then
2559                   null;
2560
2561                --  Do not insert if parent of P is an N_Component_Association
2562                --  node (i.e. we are in the context of an N_Aggregate or
2563                --  N_Extension_Aggregate node. In this case we want to insert
2564                --  before the entire aggregate.
2565
2566                elsif Nkind (Parent (P)) = N_Component_Association then
2567                   null;
2568
2569                --  Do not insert if the parent of P is either an N_Variant
2570                --  node or an N_Record_Definition node, meaning in either
2571                --  case that P is a member of a component list, and that
2572                --  therefore the actions should be inserted outside the
2573                --  complete record declaration.
2574
2575                elsif Nkind (Parent (P)) = N_Variant
2576                  or else Nkind (Parent (P)) = N_Record_Definition
2577                then
2578                   null;
2579
2580                --  Do not insert freeze nodes within the loop generated for
2581                --  an aggregate, because they may be elaborated too late for
2582                --  subsequent use in the back end: within a package spec the
2583                --  loop is part of the elaboration procedure and is only
2584                --  elaborated during the second pass.
2585
2586                --  If the loop comes from source, or the entity is local to
2587                --  the loop itself it must remain within.
2588
2589                elsif Nkind (Parent (P)) = N_Loop_Statement
2590                  and then not Comes_From_Source (Parent (P))
2591                  and then Nkind (First (Ins_Actions)) = N_Freeze_Entity
2592                  and then
2593                    Scope (Entity (First (Ins_Actions))) /= Current_Scope
2594                then
2595                   null;
2596
2597                --  Otherwise we can go ahead and do the insertion
2598
2599                elsif P = Wrapped_Node then
2600                   Store_Before_Actions_In_Scope (Ins_Actions);
2601                   return;
2602
2603                else
2604                   Insert_List_Before_And_Analyze (P, Ins_Actions);
2605                   return;
2606                end if;
2607
2608             --  A special case, N_Raise_xxx_Error can act either as a statement
2609             --  or a subexpression. We tell the difference by looking at the
2610             --  Etype. It is set to Standard_Void_Type in the statement case.
2611
2612             when
2613                N_Raise_xxx_Error =>
2614                   if Etype (P) = Standard_Void_Type then
2615                      if  P = Wrapped_Node then
2616                         Store_Before_Actions_In_Scope (Ins_Actions);
2617                      else
2618                         Insert_List_Before_And_Analyze (P, Ins_Actions);
2619                      end if;
2620
2621                      return;
2622
2623                   --  In the subexpression case, keep climbing
2624
2625                   else
2626                      null;
2627                   end if;
2628
2629             --  If a component association appears within a loop created for
2630             --  an array aggregate, attach the actions to the association so
2631             --  they can be subsequently inserted within the loop. For other
2632             --  component associations insert outside of the aggregate. For
2633             --  an association that will generate a loop, its Loop_Actions
2634             --  attribute is already initialized (see exp_aggr.adb).
2635
2636             --  The list of loop_actions can in turn generate additional ones,
2637             --  that are inserted before the associated node. If the associated
2638             --  node is outside the aggregate, the new actions are collected
2639             --  at the end of the loop actions, to respect the order in which
2640             --  they are to be elaborated.
2641
2642             when
2643                N_Component_Association =>
2644                   if Nkind (Parent (P)) = N_Aggregate
2645                     and then Present (Loop_Actions (P))
2646                   then
2647                      if Is_Empty_List (Loop_Actions (P)) then
2648                         Set_Loop_Actions (P, Ins_Actions);
2649                         Analyze_List (Ins_Actions);
2650
2651                      else
2652                         declare
2653                            Decl : Node_Id;
2654
2655                         begin
2656                            --  Check whether these actions were generated by a
2657                            --  declaration that is part of the loop_ actions
2658                            --  for the component_association.
2659
2660                            Decl := Assoc_Node;
2661                            while Present (Decl) loop
2662                               exit when Parent (Decl) = P
2663                                 and then Is_List_Member (Decl)
2664                                 and then
2665                                   List_Containing (Decl) = Loop_Actions (P);
2666                               Decl := Parent (Decl);
2667                            end loop;
2668
2669                            if Present (Decl) then
2670                               Insert_List_Before_And_Analyze
2671                                 (Decl, Ins_Actions);
2672                            else
2673                               Insert_List_After_And_Analyze
2674                                 (Last (Loop_Actions (P)), Ins_Actions);
2675                            end if;
2676                         end;
2677                      end if;
2678
2679                      return;
2680
2681                   else
2682                      null;
2683                   end if;
2684
2685             --  Another special case, an attribute denoting a procedure call
2686
2687             when
2688                N_Attribute_Reference =>
2689                   if Is_Procedure_Attribute_Name (Attribute_Name (P)) then
2690                      if P = Wrapped_Node then
2691                         Store_Before_Actions_In_Scope (Ins_Actions);
2692                      else
2693                         Insert_List_Before_And_Analyze (P, Ins_Actions);
2694                      end if;
2695
2696                      return;
2697
2698                   --  In the subexpression case, keep climbing
2699
2700                   else
2701                      null;
2702                   end if;
2703
2704             --  For all other node types, keep climbing tree
2705
2706             when
2707                N_Abortable_Part                         |
2708                N_Accept_Alternative                     |
2709                N_Access_Definition                      |
2710                N_Access_Function_Definition             |
2711                N_Access_Procedure_Definition            |
2712                N_Access_To_Object_Definition            |
2713                N_Aggregate                              |
2714                N_Allocator                              |
2715                N_Case_Expression                        |
2716                N_Case_Statement_Alternative             |
2717                N_Character_Literal                      |
2718                N_Compilation_Unit                       |
2719                N_Compilation_Unit_Aux                   |
2720                N_Component_Clause                       |
2721                N_Component_Declaration                  |
2722                N_Component_Definition                   |
2723                N_Component_List                         |
2724                N_Constrained_Array_Definition           |
2725                N_Decimal_Fixed_Point_Definition         |
2726                N_Defining_Character_Literal             |
2727                N_Defining_Identifier                    |
2728                N_Defining_Operator_Symbol               |
2729                N_Defining_Program_Unit_Name             |
2730                N_Delay_Alternative                      |
2731                N_Delta_Constraint                       |
2732                N_Derived_Type_Definition                |
2733                N_Designator                             |
2734                N_Digits_Constraint                      |
2735                N_Discriminant_Association               |
2736                N_Discriminant_Specification             |
2737                N_Empty                                  |
2738                N_Entry_Body_Formal_Part                 |
2739                N_Entry_Call_Alternative                 |
2740                N_Entry_Declaration                      |
2741                N_Entry_Index_Specification              |
2742                N_Enumeration_Type_Definition            |
2743                N_Error                                  |
2744                N_Exception_Handler                      |
2745                N_Expanded_Name                          |
2746                N_Explicit_Dereference                   |
2747                N_Extension_Aggregate                    |
2748                N_Floating_Point_Definition              |
2749                N_Formal_Decimal_Fixed_Point_Definition  |
2750                N_Formal_Derived_Type_Definition         |
2751                N_Formal_Discrete_Type_Definition        |
2752                N_Formal_Floating_Point_Definition       |
2753                N_Formal_Modular_Type_Definition         |
2754                N_Formal_Ordinary_Fixed_Point_Definition |
2755                N_Formal_Package_Declaration             |
2756                N_Formal_Private_Type_Definition         |
2757                N_Formal_Signed_Integer_Type_Definition  |
2758                N_Function_Call                          |
2759                N_Function_Specification                 |
2760                N_Generic_Association                    |
2761                N_Handled_Sequence_Of_Statements         |
2762                N_Identifier                             |
2763                N_In                                     |
2764                N_Index_Or_Discriminant_Constraint       |
2765                N_Indexed_Component                      |
2766                N_Integer_Literal                        |
2767                N_Itype_Reference                        |
2768                N_Label                                  |
2769                N_Loop_Parameter_Specification           |
2770                N_Mod_Clause                             |
2771                N_Modular_Type_Definition                |
2772                N_Not_In                                 |
2773                N_Null                                   |
2774                N_Op_Abs                                 |
2775                N_Op_Add                                 |
2776                N_Op_And                                 |
2777                N_Op_Concat                              |
2778                N_Op_Divide                              |
2779                N_Op_Eq                                  |
2780                N_Op_Expon                               |
2781                N_Op_Ge                                  |
2782                N_Op_Gt                                  |
2783                N_Op_Le                                  |
2784                N_Op_Lt                                  |
2785                N_Op_Minus                               |
2786                N_Op_Mod                                 |
2787                N_Op_Multiply                            |
2788                N_Op_Ne                                  |
2789                N_Op_Not                                 |
2790                N_Op_Or                                  |
2791                N_Op_Plus                                |
2792                N_Op_Rem                                 |
2793                N_Op_Rotate_Left                         |
2794                N_Op_Rotate_Right                        |
2795                N_Op_Shift_Left                          |
2796                N_Op_Shift_Right                         |
2797                N_Op_Shift_Right_Arithmetic              |
2798                N_Op_Subtract                            |
2799                N_Op_Xor                                 |
2800                N_Operator_Symbol                        |
2801                N_Ordinary_Fixed_Point_Definition        |
2802                N_Others_Choice                          |
2803                N_Package_Specification                  |
2804                N_Parameter_Association                  |
2805                N_Parameter_Specification                |
2806                N_Pop_Constraint_Error_Label             |
2807                N_Pop_Program_Error_Label                |
2808                N_Pop_Storage_Error_Label                |
2809                N_Pragma_Argument_Association            |
2810                N_Procedure_Specification                |
2811                N_Protected_Definition                   |
2812                N_Push_Constraint_Error_Label            |
2813                N_Push_Program_Error_Label               |
2814                N_Push_Storage_Error_Label               |
2815                N_Qualified_Expression                   |
2816                N_Range                                  |
2817                N_Range_Constraint                       |
2818                N_Real_Literal                           |
2819                N_Real_Range_Specification               |
2820                N_Record_Definition                      |
2821                N_Reference                              |
2822                N_SCIL_Dispatch_Table_Tag_Init           |
2823                N_SCIL_Dispatching_Call                  |
2824                N_SCIL_Membership_Test                   |
2825                N_Selected_Component                     |
2826                N_Signed_Integer_Type_Definition         |
2827                N_Single_Protected_Declaration           |
2828                N_Slice                                  |
2829                N_String_Literal                         |
2830                N_Subprogram_Info                        |
2831                N_Subtype_Indication                     |
2832                N_Subunit                                |
2833                N_Task_Definition                        |
2834                N_Terminate_Alternative                  |
2835                N_Triggering_Alternative                 |
2836                N_Type_Conversion                        |
2837                N_Unchecked_Expression                   |
2838                N_Unchecked_Type_Conversion              |
2839                N_Unconstrained_Array_Definition         |
2840                N_Unused_At_End                          |
2841                N_Unused_At_Start                        |
2842                N_Use_Package_Clause                     |
2843                N_Use_Type_Clause                        |
2844                N_Variant                                |
2845                N_Variant_Part                           |
2846                N_Validate_Unchecked_Conversion          |
2847                N_With_Clause
2848             =>
2849                null;
2850
2851          end case;
2852
2853          --  Make sure that inserted actions stay in the transient scope
2854
2855          if P = Wrapped_Node then
2856             Store_Before_Actions_In_Scope (Ins_Actions);
2857             return;
2858          end if;
2859
2860          --  If we fall through above tests, keep climbing tree
2861
2862          N := P;
2863
2864          if Nkind (Parent (N)) = N_Subunit then
2865
2866             --  This is the proper body corresponding to a stub. Insertion must
2867             --  be done at the point of the stub, which is in the declarative
2868             --  part of the parent unit.
2869
2870             P := Corresponding_Stub (Parent (N));
2871
2872          else
2873             P := Parent (N);
2874          end if;
2875       end loop;
2876    end Insert_Actions;
2877
2878    --  Version with check(s) suppressed
2879
2880    procedure Insert_Actions
2881      (Assoc_Node  : Node_Id;
2882       Ins_Actions : List_Id;
2883       Suppress    : Check_Id)
2884    is
2885    begin
2886       if Suppress = All_Checks then
2887          declare
2888             Svg : constant Suppress_Array := Scope_Suppress;
2889          begin
2890             Scope_Suppress := (others => True);
2891             Insert_Actions (Assoc_Node, Ins_Actions);
2892             Scope_Suppress := Svg;
2893          end;
2894
2895       else
2896          declare
2897             Svg : constant Boolean := Scope_Suppress (Suppress);
2898          begin
2899             Scope_Suppress (Suppress) := True;
2900             Insert_Actions (Assoc_Node, Ins_Actions);
2901             Scope_Suppress (Suppress) := Svg;
2902          end;
2903       end if;
2904    end Insert_Actions;
2905
2906    --------------------------
2907    -- Insert_Actions_After --
2908    --------------------------
2909
2910    procedure Insert_Actions_After
2911      (Assoc_Node  : Node_Id;
2912       Ins_Actions : List_Id)
2913    is
2914    begin
2915       if Scope_Is_Transient
2916         and then Assoc_Node = Node_To_Be_Wrapped
2917       then
2918          Store_After_Actions_In_Scope (Ins_Actions);
2919       else
2920          Insert_List_After_And_Analyze (Assoc_Node, Ins_Actions);
2921       end if;
2922    end Insert_Actions_After;
2923
2924    ---------------------------------
2925    -- Insert_Library_Level_Action --
2926    ---------------------------------
2927
2928    procedure Insert_Library_Level_Action (N : Node_Id) is
2929       Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
2930
2931    begin
2932       Push_Scope (Cunit_Entity (Main_Unit));
2933       --  ??? should this be Current_Sem_Unit instead of Main_Unit?
2934
2935       if No (Actions (Aux)) then
2936          Set_Actions (Aux, New_List (N));
2937       else
2938          Append (N, Actions (Aux));
2939       end if;
2940
2941       Analyze (N);
2942       Pop_Scope;
2943    end Insert_Library_Level_Action;
2944
2945    ----------------------------------
2946    -- Insert_Library_Level_Actions --
2947    ----------------------------------
2948
2949    procedure Insert_Library_Level_Actions (L : List_Id) is
2950       Aux : constant Node_Id := Aux_Decls_Node (Cunit (Main_Unit));
2951
2952    begin
2953       if Is_Non_Empty_List (L) then
2954          Push_Scope (Cunit_Entity (Main_Unit));
2955          --  ??? should this be Current_Sem_Unit instead of Main_Unit?
2956
2957          if No (Actions (Aux)) then
2958             Set_Actions (Aux, L);
2959             Analyze_List (L);
2960          else
2961             Insert_List_After_And_Analyze (Last (Actions (Aux)), L);
2962          end if;
2963
2964          Pop_Scope;
2965       end if;
2966    end Insert_Library_Level_Actions;
2967
2968    ----------------------
2969    -- Inside_Init_Proc --
2970    ----------------------
2971
2972    function Inside_Init_Proc return Boolean is
2973       S : Entity_Id;
2974
2975    begin
2976       S := Current_Scope;
2977       while Present (S)
2978         and then S /= Standard_Standard
2979       loop
2980          if Is_Init_Proc (S) then
2981             return True;
2982          else
2983             S := Scope (S);
2984          end if;
2985       end loop;
2986
2987       return False;
2988    end Inside_Init_Proc;
2989
2990    ----------------------------
2991    -- Is_All_Null_Statements --
2992    ----------------------------
2993
2994    function Is_All_Null_Statements (L : List_Id) return Boolean is
2995       Stm : Node_Id;
2996
2997    begin
2998       Stm := First (L);
2999       while Present (Stm) loop
3000          if Nkind (Stm) /= N_Null_Statement then
3001             return False;
3002          end if;
3003
3004          Next (Stm);
3005       end loop;
3006
3007       return True;
3008    end Is_All_Null_Statements;
3009
3010    ---------------------------------
3011    -- Is_Fully_Repped_Tagged_Type --
3012    ---------------------------------
3013
3014    function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean is
3015       U    : constant Entity_Id := Underlying_Type (T);
3016       Comp : Entity_Id;
3017
3018    begin
3019       if No (U) or else not Is_Tagged_Type (U) then
3020          return False;
3021       elsif Has_Discriminants (U) then
3022          return False;
3023       elsif not Has_Specified_Layout (U) then
3024          return False;
3025       end if;
3026
3027       --  Here we have a tagged type, see if it has any unlayed out fields
3028       --  other than a possible tag and parent fields. If so, we return False.
3029
3030       Comp := First_Component (U);
3031       while Present (Comp) loop
3032          if not Is_Tag (Comp)
3033            and then Chars (Comp) /= Name_uParent
3034            and then No (Component_Clause (Comp))
3035          then
3036             return False;
3037          else
3038             Next_Component (Comp);
3039          end if;
3040       end loop;
3041
3042       --  All components are layed out
3043
3044       return True;
3045    end Is_Fully_Repped_Tagged_Type;
3046
3047    ----------------------------------
3048    -- Is_Library_Level_Tagged_Type --
3049    ----------------------------------
3050
3051    function Is_Library_Level_Tagged_Type (Typ : Entity_Id) return Boolean is
3052    begin
3053       return Is_Tagged_Type (Typ)
3054         and then Is_Library_Level_Entity (Typ);
3055    end Is_Library_Level_Tagged_Type;
3056
3057    ----------------------------------
3058    -- Is_Possibly_Unaligned_Object --
3059    ----------------------------------
3060
3061    function Is_Possibly_Unaligned_Object (N : Node_Id) return Boolean is
3062       T  : constant Entity_Id := Etype (N);
3063
3064    begin
3065       --  If renamed object, apply test to underlying object
3066
3067       if Is_Entity_Name (N)
3068         and then Is_Object (Entity (N))
3069         and then Present (Renamed_Object (Entity (N)))
3070       then
3071          return Is_Possibly_Unaligned_Object (Renamed_Object (Entity (N)));
3072       end if;
3073
3074       --  Tagged and controlled types and aliased types are always aligned,
3075       --  as are concurrent types.
3076
3077       if Is_Aliased (T)
3078         or else Has_Controlled_Component (T)
3079         or else Is_Concurrent_Type (T)
3080         or else Is_Tagged_Type (T)
3081         or else Is_Controlled (T)
3082       then
3083          return False;
3084       end if;
3085
3086       --  If this is an element of a packed array, may be unaligned
3087
3088       if Is_Ref_To_Bit_Packed_Array (N) then
3089          return True;
3090       end if;
3091
3092       --  Case of component reference
3093
3094       if Nkind (N) = N_Selected_Component then
3095          declare
3096             P : constant Node_Id   := Prefix (N);
3097             C : constant Entity_Id := Entity (Selector_Name (N));
3098             M : Nat;
3099             S : Nat;
3100
3101          begin
3102             --  If component reference is for an array with non-static bounds,
3103             --  then it is always aligned: we can only process unaligned
3104             --  arrays with static bounds (more accurately bounds known at
3105             --  compile time).
3106
3107             if Is_Array_Type (T)
3108               and then not Compile_Time_Known_Bounds (T)
3109             then
3110                return False;
3111             end if;
3112
3113             --  If component is aliased, it is definitely properly aligned
3114
3115             if Is_Aliased (C) then
3116                return False;
3117             end if;
3118
3119             --  If component is for a type implemented as a scalar, and the
3120             --  record is packed, and the component is other than the first
3121             --  component of the record, then the component may be unaligned.
3122
3123             if Is_Packed (Etype (P))
3124               and then Represented_As_Scalar (Etype (C))
3125               and then First_Entity (Scope (C)) /= C
3126             then
3127                return True;
3128             end if;
3129
3130             --  Compute maximum possible alignment for T
3131
3132             --  If alignment is known, then that settles things
3133
3134             if Known_Alignment (T) then
3135                M := UI_To_Int (Alignment (T));
3136
3137             --  If alignment is not known, tentatively set max alignment
3138
3139             else
3140                M := Ttypes.Maximum_Alignment;
3141
3142                --  We can reduce this if the Esize is known since the default
3143                --  alignment will never be more than the smallest power of 2
3144                --  that does not exceed this Esize value.
3145
3146                if Known_Esize (T) then
3147                   S := UI_To_Int (Esize (T));
3148
3149                   while (M / 2) >= S loop
3150                      M := M / 2;
3151                   end loop;
3152                end if;
3153             end if;
3154
3155             --  The following code is historical, it used to be present but it
3156             --  is too cautious, because the front-end does not know the proper
3157             --  default alignments for the target. Also, if the alignment is
3158             --  not known, the front end can't know in any case! If a copy is
3159             --  needed, the back-end will take care of it. This whole section
3160             --  including this comment can be removed later ???
3161
3162             --  If the component reference is for a record that has a specified
3163             --  alignment, and we either know it is too small, or cannot tell,
3164             --  then the component may be unaligned.
3165
3166             --  if Known_Alignment (Etype (P))
3167             --    and then Alignment (Etype (P)) < Ttypes.Maximum_Alignment
3168             --    and then M > Alignment (Etype (P))
3169             --  then
3170             --     return True;
3171             --  end if;
3172
3173             --  Case of component clause present which may specify an
3174             --  unaligned position.
3175
3176             if Present (Component_Clause (C)) then
3177
3178                --  Otherwise we can do a test to make sure that the actual
3179                --  start position in the record, and the length, are both
3180                --  consistent with the required alignment. If not, we know
3181                --  that we are unaligned.
3182
3183                declare
3184                   Align_In_Bits : constant Nat := M * System_Storage_Unit;
3185                begin
3186                   if Component_Bit_Offset (C) mod Align_In_Bits /= 0
3187                     or else Esize (C) mod Align_In_Bits /= 0
3188                   then
3189                      return True;
3190                   end if;
3191                end;
3192             end if;
3193
3194             --  Otherwise, for a component reference, test prefix
3195
3196             return Is_Possibly_Unaligned_Object (P);
3197          end;
3198
3199       --  If not a component reference, must be aligned
3200
3201       else
3202          return False;
3203       end if;
3204    end Is_Possibly_Unaligned_Object;
3205
3206    ---------------------------------
3207    -- Is_Possibly_Unaligned_Slice --
3208    ---------------------------------
3209
3210    function Is_Possibly_Unaligned_Slice (N : Node_Id) return Boolean is
3211    begin
3212       --  Go to renamed object
3213
3214       if Is_Entity_Name (N)
3215         and then Is_Object (Entity (N))
3216         and then Present (Renamed_Object (Entity (N)))
3217       then
3218          return Is_Possibly_Unaligned_Slice (Renamed_Object (Entity (N)));
3219       end if;
3220
3221       --  The reference must be a slice
3222
3223       if Nkind (N) /= N_Slice then
3224          return False;
3225       end if;
3226
3227       --  Always assume the worst for a nested record component with a
3228       --  component clause, which gigi/gcc does not appear to handle well.
3229       --  It is not clear why this special test is needed at all ???
3230
3231       if Nkind (Prefix (N)) = N_Selected_Component
3232         and then Nkind (Prefix (Prefix (N))) = N_Selected_Component
3233         and then
3234           Present (Component_Clause (Entity (Selector_Name (Prefix (N)))))
3235       then
3236          return True;
3237       end if;
3238
3239       --  We only need to worry if the target has strict alignment
3240
3241       if not Target_Strict_Alignment then
3242          return False;
3243       end if;
3244
3245       --  If it is a slice, then look at the array type being sliced
3246
3247       declare
3248          Sarr : constant Node_Id := Prefix (N);
3249          --  Prefix of the slice, i.e. the array being sliced
3250
3251          Styp : constant Entity_Id := Etype (Prefix (N));
3252          --  Type of the array being sliced
3253
3254          Pref : Node_Id;
3255          Ptyp : Entity_Id;
3256
3257       begin
3258          --  The problems arise if the array object that is being sliced
3259          --  is a component of a record or array, and we cannot guarantee
3260          --  the alignment of the array within its containing object.
3261
3262          --  To investigate this, we look at successive prefixes to see
3263          --  if we have a worrisome indexed or selected component.
3264
3265          Pref := Sarr;
3266          loop
3267             --  Case of array is part of an indexed component reference
3268
3269             if Nkind (Pref) = N_Indexed_Component then
3270                Ptyp := Etype (Prefix (Pref));
3271
3272                --  The only problematic case is when the array is packed,
3273                --  in which case we really know nothing about the alignment
3274                --  of individual components.
3275
3276                if Is_Bit_Packed_Array (Ptyp) then
3277                   return True;
3278                end if;
3279
3280             --  Case of array is part of a selected component reference
3281
3282             elsif Nkind (Pref) = N_Selected_Component then
3283                Ptyp := Etype (Prefix (Pref));
3284
3285                --  We are definitely in trouble if the record in question
3286                --  has an alignment, and either we know this alignment is
3287                --  inconsistent with the alignment of the slice, or we
3288                --  don't know what the alignment of the slice should be.
3289
3290                if Known_Alignment (Ptyp)
3291                  and then (Unknown_Alignment (Styp)
3292                              or else Alignment (Styp) > Alignment (Ptyp))
3293                then
3294                   return True;
3295                end if;
3296
3297                --  We are in potential trouble if the record type is packed.
3298                --  We could special case when we know that the array is the
3299                --  first component, but that's not such a simple case ???
3300
3301                if Is_Packed (Ptyp) then
3302                   return True;
3303                end if;
3304
3305                --  We are in trouble if there is a component clause, and
3306                --  either we do not know the alignment of the slice, or
3307                --  the alignment of the slice is inconsistent with the
3308                --  bit position specified by the component clause.
3309
3310                declare
3311                   Field : constant Entity_Id := Entity (Selector_Name (Pref));
3312                begin
3313                   if Present (Component_Clause (Field))
3314                     and then
3315                       (Unknown_Alignment (Styp)
3316                         or else
3317                          (Component_Bit_Offset (Field) mod
3318                            (System_Storage_Unit * Alignment (Styp))) /= 0)
3319                   then
3320                      return True;
3321                   end if;
3322                end;
3323
3324             --  For cases other than selected or indexed components we
3325             --  know we are OK, since no issues arise over alignment.
3326
3327             else
3328                return False;
3329             end if;
3330
3331             --  We processed an indexed component or selected component
3332             --  reference that looked safe, so keep checking prefixes.
3333
3334             Pref := Prefix (Pref);
3335          end loop;
3336       end;
3337    end Is_Possibly_Unaligned_Slice;
3338
3339    --------------------------------
3340    -- Is_Ref_To_Bit_Packed_Array --
3341    --------------------------------
3342
3343    function Is_Ref_To_Bit_Packed_Array (N : Node_Id) return Boolean is
3344       Result : Boolean;
3345       Expr   : Node_Id;
3346
3347    begin
3348       if Is_Entity_Name (N)
3349         and then Is_Object (Entity (N))
3350         and then Present (Renamed_Object (Entity (N)))
3351       then
3352          return Is_Ref_To_Bit_Packed_Array (Renamed_Object (Entity (N)));
3353       end if;
3354
3355       if Nkind (N) = N_Indexed_Component
3356            or else
3357          Nkind (N) = N_Selected_Component
3358       then
3359          if Is_Bit_Packed_Array (Etype (Prefix (N))) then
3360             Result := True;
3361          else
3362             Result := Is_Ref_To_Bit_Packed_Array (Prefix (N));
3363          end if;
3364
3365          if Result and then Nkind (N) = N_Indexed_Component then
3366             Expr := First (Expressions (N));
3367             while Present (Expr) loop
3368                Force_Evaluation (Expr);
3369                Next (Expr);
3370             end loop;
3371          end if;
3372
3373          return Result;
3374
3375       else
3376          return False;
3377       end if;
3378    end Is_Ref_To_Bit_Packed_Array;
3379
3380    --------------------------------
3381    -- Is_Ref_To_Bit_Packed_Slice --
3382    --------------------------------
3383
3384    function Is_Ref_To_Bit_Packed_Slice (N : Node_Id) return Boolean is
3385    begin
3386       if Nkind (N) = N_Type_Conversion then
3387          return Is_Ref_To_Bit_Packed_Slice (Expression (N));
3388
3389       elsif Is_Entity_Name (N)
3390         and then Is_Object (Entity (N))
3391         and then Present (Renamed_Object (Entity (N)))
3392       then
3393          return Is_Ref_To_Bit_Packed_Slice (Renamed_Object (Entity (N)));
3394
3395       elsif Nkind (N) = N_Slice
3396         and then Is_Bit_Packed_Array (Etype (Prefix (N)))
3397       then
3398          return True;
3399
3400       elsif Nkind (N) = N_Indexed_Component
3401            or else
3402          Nkind (N) = N_Selected_Component
3403       then
3404          return Is_Ref_To_Bit_Packed_Slice (Prefix (N));
3405
3406       else
3407          return False;
3408       end if;
3409    end Is_Ref_To_Bit_Packed_Slice;
3410
3411    -----------------------
3412    -- Is_Renamed_Object --
3413    -----------------------
3414
3415    function Is_Renamed_Object (N : Node_Id) return Boolean is
3416       Pnod : constant Node_Id   := Parent (N);
3417       Kind : constant Node_Kind := Nkind (Pnod);
3418    begin
3419       if Kind = N_Object_Renaming_Declaration then
3420          return True;
3421       elsif Nkind_In (Kind, N_Indexed_Component, N_Selected_Component) then
3422          return Is_Renamed_Object (Pnod);
3423       else
3424          return False;
3425       end if;
3426    end Is_Renamed_Object;
3427
3428    ----------------------------
3429    -- Is_Untagged_Derivation --
3430    ----------------------------
3431
3432    function Is_Untagged_Derivation (T : Entity_Id) return Boolean is
3433    begin
3434       return (not Is_Tagged_Type (T) and then Is_Derived_Type (T))
3435                or else
3436              (Is_Private_Type (T) and then Present (Full_View (T))
3437                and then not Is_Tagged_Type (Full_View (T))
3438                and then Is_Derived_Type (Full_View (T))
3439                and then Etype (Full_View (T)) /= T);
3440    end Is_Untagged_Derivation;
3441
3442    ---------------------------
3443    -- Is_Volatile_Reference --
3444    ---------------------------
3445
3446    function Is_Volatile_Reference (N : Node_Id) return Boolean is
3447    begin
3448       if Nkind (N) in N_Has_Etype
3449         and then Present (Etype (N))
3450         and then Treat_As_Volatile (Etype (N))
3451       then
3452          return True;
3453
3454       elsif Is_Entity_Name (N) then
3455          return Treat_As_Volatile (Entity (N));
3456
3457       elsif Nkind (N) = N_Slice then
3458          return Is_Volatile_Reference (Prefix (N));
3459
3460       elsif Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
3461          if (Is_Entity_Name (Prefix (N))
3462                and then Has_Volatile_Components (Entity (Prefix (N))))
3463            or else (Present (Etype (Prefix (N)))
3464                       and then Has_Volatile_Components (Etype (Prefix (N))))
3465          then
3466             return True;
3467          else
3468             return Is_Volatile_Reference (Prefix (N));
3469          end if;
3470
3471       else
3472          return False;
3473       end if;
3474    end Is_Volatile_Reference;
3475
3476    --------------------
3477    -- Kill_Dead_Code --
3478    --------------------
3479
3480    procedure Kill_Dead_Code (N : Node_Id; Warn : Boolean := False) is
3481       W : Boolean := Warn;
3482       --  Set False if warnings suppressed
3483
3484    begin
3485       if Present (N) then
3486          Remove_Warning_Messages (N);
3487
3488          --  Generate warning if appropriate
3489
3490          if W then
3491
3492             --  We suppress the warning if this code is under control of an
3493             --  if statement, whose condition is a simple identifier, and
3494             --  either we are in an instance, or warnings off is set for this
3495             --  identifier. The reason for killing it in the instance case is
3496             --  that it is common and reasonable for code to be deleted in
3497             --  instances for various reasons.
3498
3499             if Nkind (Parent (N)) = N_If_Statement then
3500                declare
3501                   C : constant Node_Id := Condition (Parent (N));
3502                begin
3503                   if Nkind (C) = N_Identifier
3504                     and then
3505                       (In_Instance
3506                         or else (Present (Entity (C))
3507                                    and then Has_Warnings_Off (Entity (C))))
3508                   then
3509                      W := False;
3510                   end if;
3511                end;
3512             end if;
3513
3514             --  Generate warning if not suppressed
3515
3516             if W then
3517                Error_Msg_F
3518                  ("?this code can never be executed and has been deleted!", N);
3519             end if;
3520          end if;
3521
3522          --  Recurse into block statements and bodies to process declarations
3523          --  and statements.
3524
3525          if Nkind (N) = N_Block_Statement
3526            or else Nkind (N) = N_Subprogram_Body
3527            or else Nkind (N) = N_Package_Body
3528          then
3529             Kill_Dead_Code (Declarations (N), False);
3530             Kill_Dead_Code (Statements (Handled_Statement_Sequence (N)));
3531
3532             if Nkind (N) = N_Subprogram_Body then
3533                Set_Is_Eliminated (Defining_Entity (N));
3534             end if;
3535
3536          elsif Nkind (N) = N_Package_Declaration then
3537             Kill_Dead_Code (Visible_Declarations (Specification (N)));
3538             Kill_Dead_Code (Private_Declarations (Specification (N)));
3539
3540             --  ??? After this point, Delete_Tree has been called on all
3541             --  declarations in Specification (N), so references to
3542             --  entities therein look suspicious.
3543
3544             declare
3545                E : Entity_Id := First_Entity (Defining_Entity (N));
3546             begin
3547                while Present (E) loop
3548                   if Ekind (E) = E_Operator then
3549                      Set_Is_Eliminated (E);
3550                   end if;
3551
3552                   Next_Entity (E);
3553                end loop;
3554             end;
3555
3556          --  Recurse into composite statement to kill individual statements,
3557          --  in particular instantiations.
3558
3559          elsif Nkind (N) = N_If_Statement then
3560             Kill_Dead_Code (Then_Statements (N));
3561             Kill_Dead_Code (Elsif_Parts (N));
3562             Kill_Dead_Code (Else_Statements (N));
3563
3564          elsif Nkind (N) = N_Loop_Statement then
3565             Kill_Dead_Code (Statements (N));
3566
3567          elsif Nkind (N) = N_Case_Statement then
3568             declare
3569                Alt : Node_Id;
3570             begin
3571                Alt := First (Alternatives (N));
3572                while Present (Alt) loop
3573                   Kill_Dead_Code (Statements (Alt));
3574                   Next (Alt);
3575                end loop;
3576             end;
3577
3578          elsif Nkind (N) = N_Case_Statement_Alternative then
3579             Kill_Dead_Code (Statements (N));
3580
3581          --  Deal with dead instances caused by deleting instantiations
3582
3583          elsif Nkind (N) in N_Generic_Instantiation then
3584             Remove_Dead_Instance (N);
3585          end if;
3586       end if;
3587    end Kill_Dead_Code;
3588
3589    --  Case where argument is a list of nodes to be killed
3590
3591    procedure Kill_Dead_Code (L : List_Id; Warn : Boolean := False) is
3592       N : Node_Id;
3593       W : Boolean;
3594    begin
3595       W := Warn;
3596       if Is_Non_Empty_List (L) then
3597          N := First (L);
3598          while Present (N) loop
3599             Kill_Dead_Code (N, W);
3600             W := False;
3601             Next (N);
3602          end loop;
3603       end if;
3604    end Kill_Dead_Code;
3605
3606    ------------------------
3607    -- Known_Non_Negative --
3608    ------------------------
3609
3610    function Known_Non_Negative (Opnd : Node_Id) return Boolean is
3611    begin
3612       if Is_OK_Static_Expression (Opnd)
3613         and then Expr_Value (Opnd) >= 0
3614       then
3615          return True;
3616
3617       else
3618          declare
3619             Lo : constant Node_Id := Type_Low_Bound (Etype (Opnd));
3620
3621          begin
3622             return
3623               Is_OK_Static_Expression (Lo) and then Expr_Value (Lo) >= 0;
3624          end;
3625       end if;
3626    end Known_Non_Negative;
3627
3628    --------------------
3629    -- Known_Non_Null --
3630    --------------------
3631
3632    function Known_Non_Null (N : Node_Id) return Boolean is
3633    begin
3634       --  Checks for case where N is an entity reference
3635
3636       if Is_Entity_Name (N) and then Present (Entity (N)) then
3637          declare
3638             E   : constant Entity_Id := Entity (N);
3639             Op  : Node_Kind;
3640             Val : Node_Id;
3641
3642          begin
3643             --  First check if we are in decisive conditional
3644
3645             Get_Current_Value_Condition (N, Op, Val);
3646
3647             if Known_Null (Val) then
3648                if Op = N_Op_Eq then
3649                   return False;
3650                elsif Op = N_Op_Ne then
3651                   return True;
3652                end if;
3653             end if;
3654
3655             --  If OK to do replacement, test Is_Known_Non_Null flag
3656
3657             if OK_To_Do_Constant_Replacement (E) then
3658                return Is_Known_Non_Null (E);
3659
3660             --  Otherwise if not safe to do replacement, then say so
3661
3662             else
3663                return False;
3664             end if;
3665          end;
3666
3667       --  True if access attribute
3668
3669       elsif Nkind (N) = N_Attribute_Reference
3670         and then (Attribute_Name (N) = Name_Access
3671                     or else
3672                   Attribute_Name (N) = Name_Unchecked_Access
3673                     or else
3674                   Attribute_Name (N) = Name_Unrestricted_Access)
3675       then
3676          return True;
3677
3678       --  True if allocator
3679
3680       elsif Nkind (N) = N_Allocator then
3681          return True;
3682
3683       --  For a conversion, true if expression is known non-null
3684
3685       elsif Nkind (N) = N_Type_Conversion then
3686          return Known_Non_Null (Expression (N));
3687
3688       --  Above are all cases where the value could be determined to be
3689       --  non-null. In all other cases, we don't know, so return False.
3690
3691       else
3692          return False;
3693       end if;
3694    end Known_Non_Null;
3695
3696    ----------------
3697    -- Known_Null --
3698    ----------------
3699
3700    function Known_Null (N : Node_Id) return Boolean is
3701    begin
3702       --  Checks for case where N is an entity reference
3703
3704       if Is_Entity_Name (N) and then Present (Entity (N)) then
3705          declare
3706             E   : constant Entity_Id := Entity (N);
3707             Op  : Node_Kind;
3708             Val : Node_Id;
3709
3710          begin
3711             --  Constant null value is for sure null
3712
3713             if Ekind (E) = E_Constant
3714               and then Known_Null (Constant_Value (E))
3715             then
3716                return True;
3717             end if;
3718
3719             --  First check if we are in decisive conditional
3720
3721             Get_Current_Value_Condition (N, Op, Val);
3722
3723             if Known_Null (Val) then
3724                if Op = N_Op_Eq then
3725                   return True;
3726                elsif Op = N_Op_Ne then
3727                   return False;
3728                end if;
3729             end if;
3730
3731             --  If OK to do replacement, test Is_Known_Null flag
3732
3733             if OK_To_Do_Constant_Replacement (E) then
3734                return Is_Known_Null (E);
3735
3736             --  Otherwise if not safe to do replacement, then say so
3737
3738             else
3739                return False;
3740             end if;
3741          end;
3742
3743       --  True if explicit reference to null
3744
3745       elsif Nkind (N) = N_Null then
3746          return True;
3747
3748       --  For a conversion, true if expression is known null
3749
3750       elsif Nkind (N) = N_Type_Conversion then
3751          return Known_Null (Expression (N));
3752
3753       --  Above are all cases where the value could be determined to be null.
3754       --  In all other cases, we don't know, so return False.
3755
3756       else
3757          return False;
3758       end if;
3759    end Known_Null;
3760
3761    -----------------------------
3762    -- Make_CW_Equivalent_Type --
3763    -----------------------------
3764
3765    --  Create a record type used as an equivalent of any member of the class
3766    --  which takes its size from exp.
3767
3768    --  Generate the following code:
3769
3770    --   type Equiv_T is record
3771    --     _parent :  T (List of discriminant constraints taken from Exp);
3772    --     Ext__50 : Storage_Array (1 .. (Exp'size - Typ'object_size)/8);
3773    --   end Equiv_T;
3774    --
3775    --   ??? Note that this type does not guarantee same alignment as all
3776    --   derived types
3777
3778    function Make_CW_Equivalent_Type
3779      (T : Entity_Id;
3780       E : Node_Id) return Entity_Id
3781    is
3782       Loc         : constant Source_Ptr := Sloc (E);
3783       Root_Typ    : constant Entity_Id  := Root_Type (T);
3784       List_Def    : constant List_Id    := Empty_List;
3785       Comp_List   : constant List_Id    := New_List;
3786       Equiv_Type  : Entity_Id;
3787       Range_Type  : Entity_Id;
3788       Str_Type    : Entity_Id;
3789       Constr_Root : Entity_Id;
3790       Sizexpr     : Node_Id;
3791
3792    begin
3793       --  If the root type is already constrained, there are no discriminants
3794       --  in the expression.
3795
3796       if not Has_Discriminants (Root_Typ)
3797         or else Is_Constrained (Root_Typ)
3798       then
3799          Constr_Root := Root_Typ;
3800       else
3801          Constr_Root := Make_Temporary (Loc, 'R');
3802
3803          --  subtype cstr__n is T (List of discr constraints taken from Exp)
3804
3805          Append_To (List_Def,
3806            Make_Subtype_Declaration (Loc,
3807              Defining_Identifier => Constr_Root,
3808              Subtype_Indication  => Make_Subtype_From_Expr (E, Root_Typ)));
3809       end if;
3810
3811       --  Generate the range subtype declaration
3812
3813       Range_Type := Make_Temporary (Loc, 'G');
3814
3815       if not Is_Interface (Root_Typ) then
3816
3817          --  subtype rg__xx is
3818          --    Storage_Offset range 1 .. (Expr'size - typ'size) / Storage_Unit
3819
3820          Sizexpr :=
3821            Make_Op_Subtract (Loc,
3822              Left_Opnd =>
3823                Make_Attribute_Reference (Loc,
3824                  Prefix =>
3825                    OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
3826                  Attribute_Name => Name_Size),
3827              Right_Opnd =>
3828                Make_Attribute_Reference (Loc,
3829                  Prefix => New_Reference_To (Constr_Root, Loc),
3830                  Attribute_Name => Name_Object_Size));
3831       else
3832          --  subtype rg__xx is
3833          --    Storage_Offset range 1 .. Expr'size / Storage_Unit
3834
3835          Sizexpr :=
3836            Make_Attribute_Reference (Loc,
3837              Prefix =>
3838                OK_Convert_To (T, Duplicate_Subexpr_No_Checks (E)),
3839              Attribute_Name => Name_Size);
3840       end if;
3841
3842       Set_Paren_Count (Sizexpr, 1);
3843
3844       Append_To (List_Def,
3845         Make_Subtype_Declaration (Loc,
3846           Defining_Identifier => Range_Type,
3847           Subtype_Indication =>
3848             Make_Subtype_Indication (Loc,
3849               Subtype_Mark => New_Reference_To (RTE (RE_Storage_Offset), Loc),
3850               Constraint => Make_Range_Constraint (Loc,
3851                 Range_Expression =>
3852                   Make_Range (Loc,
3853                     Low_Bound => Make_Integer_Literal (Loc, 1),
3854                     High_Bound =>
3855                       Make_Op_Divide (Loc,
3856                         Left_Opnd => Sizexpr,
3857                         Right_Opnd => Make_Integer_Literal (Loc,
3858                             Intval => System_Storage_Unit)))))));
3859
3860       --  subtype str__nn is Storage_Array (rg__x);
3861
3862       Str_Type := Make_Temporary (Loc, 'S');
3863       Append_To (List_Def,
3864         Make_Subtype_Declaration (Loc,
3865           Defining_Identifier => Str_Type,
3866           Subtype_Indication =>
3867             Make_Subtype_Indication (Loc,
3868               Subtype_Mark => New_Reference_To (RTE (RE_Storage_Array), Loc),
3869               Constraint =>
3870                 Make_Index_Or_Discriminant_Constraint (Loc,
3871                   Constraints =>
3872                     New_List (New_Reference_To (Range_Type, Loc))))));
3873
3874       --  type Equiv_T is record
3875       --    [ _parent : Tnn; ]
3876       --    E : Str_Type;
3877       --  end Equiv_T;
3878
3879       Equiv_Type := Make_Temporary (Loc, 'T');
3880       Set_Ekind (Equiv_Type, E_Record_Type);
3881       Set_Parent_Subtype (Equiv_Type, Constr_Root);
3882
3883       --  Set Is_Class_Wide_Equivalent_Type very early to trigger the special
3884       --  treatment for this type. In particular, even though _parent's type
3885       --  is a controlled type or contains controlled components, we do not
3886       --  want to set Has_Controlled_Component on it to avoid making it gain
3887       --  an unwanted _controller component.
3888
3889       Set_Is_Class_Wide_Equivalent_Type (Equiv_Type);
3890
3891       if not Is_Interface (Root_Typ) then
3892          Append_To (Comp_List,
3893            Make_Component_Declaration (Loc,
3894              Defining_Identifier =>
3895                Make_Defining_Identifier (Loc, Name_uParent),
3896              Component_Definition =>
3897                Make_Component_Definition (Loc,
3898                  Aliased_Present    => False,
3899                  Subtype_Indication => New_Reference_To (Constr_Root, Loc))));
3900       end if;
3901
3902       Append_To (Comp_List,
3903         Make_Component_Declaration (Loc,
3904           Defining_Identifier  => Make_Temporary (Loc, 'C'),
3905           Component_Definition =>
3906             Make_Component_Definition (Loc,
3907               Aliased_Present    => False,
3908               Subtype_Indication => New_Reference_To (Str_Type, Loc))));
3909
3910       Append_To (List_Def,
3911         Make_Full_Type_Declaration (Loc,
3912           Defining_Identifier => Equiv_Type,
3913           Type_Definition =>
3914             Make_Record_Definition (Loc,
3915               Component_List =>
3916                 Make_Component_List (Loc,
3917                   Component_Items => Comp_List,
3918                   Variant_Part    => Empty))));
3919
3920       --  Suppress all checks during the analysis of the expanded code
3921       --  to avoid the generation of spurious warnings under ZFP run-time.
3922
3923       Insert_Actions (E, List_Def, Suppress => All_Checks);
3924       return Equiv_Type;
3925    end Make_CW_Equivalent_Type;
3926
3927    ------------------------
3928    -- Make_Literal_Range --
3929    ------------------------
3930
3931    function Make_Literal_Range
3932      (Loc         : Source_Ptr;
3933       Literal_Typ : Entity_Id) return Node_Id
3934    is
3935       Lo          : constant Node_Id :=
3936                       New_Copy_Tree (String_Literal_Low_Bound (Literal_Typ));
3937       Index       : constant Entity_Id := Etype (Lo);
3938
3939       Hi          : Node_Id;
3940       Length_Expr : constant Node_Id :=
3941                       Make_Op_Subtract (Loc,
3942                         Left_Opnd =>
3943                           Make_Integer_Literal (Loc,
3944                             Intval => String_Literal_Length (Literal_Typ)),
3945                         Right_Opnd =>
3946                           Make_Integer_Literal (Loc, 1));
3947
3948    begin
3949       Set_Analyzed (Lo, False);
3950
3951          if Is_Integer_Type (Index) then
3952             Hi :=
3953               Make_Op_Add (Loc,
3954                 Left_Opnd  => New_Copy_Tree (Lo),
3955                 Right_Opnd => Length_Expr);
3956          else
3957             Hi :=
3958               Make_Attribute_Reference (Loc,
3959                 Attribute_Name => Name_Val,
3960                 Prefix => New_Occurrence_Of (Index, Loc),
3961                 Expressions => New_List (
3962                  Make_Op_Add (Loc,
3963                    Left_Opnd =>
3964                      Make_Attribute_Reference (Loc,
3965                        Attribute_Name => Name_Pos,
3966                        Prefix => New_Occurrence_Of (Index, Loc),
3967                        Expressions => New_List (New_Copy_Tree (Lo))),
3968                   Right_Opnd => Length_Expr)));
3969          end if;
3970
3971          return
3972            Make_Range (Loc,
3973              Low_Bound  => Lo,
3974              High_Bound => Hi);
3975    end Make_Literal_Range;
3976
3977    --------------------------
3978    -- Make_Non_Empty_Check --
3979    --------------------------
3980
3981    function Make_Non_Empty_Check
3982      (Loc : Source_Ptr;
3983       N   : Node_Id) return Node_Id
3984    is
3985    begin
3986       return
3987         Make_Op_Ne (Loc,
3988           Left_Opnd =>
3989             Make_Attribute_Reference (Loc,
3990               Attribute_Name => Name_Length,
3991               Prefix => Duplicate_Subexpr_No_Checks (N, Name_Req => True)),
3992           Right_Opnd =>
3993             Make_Integer_Literal (Loc, 0));
3994    end Make_Non_Empty_Check;
3995
3996    ----------------------------
3997    -- Make_Subtype_From_Expr --
3998    ----------------------------
3999
4000    --  1. If Expr is an unconstrained array expression, creates
4001    --    Unc_Type(Expr'first(1)..Expr'last(1),..., Expr'first(n)..Expr'last(n))
4002
4003    --  2. If Expr is a unconstrained discriminated type expression, creates
4004    --    Unc_Type(Expr.Discr1, ... , Expr.Discr_n)
4005
4006    --  3. If Expr is class-wide, creates an implicit class wide subtype
4007
4008    function Make_Subtype_From_Expr
4009      (E       : Node_Id;
4010       Unc_Typ : Entity_Id) return Node_Id
4011    is
4012       Loc         : constant Source_Ptr := Sloc (E);
4013       List_Constr : constant List_Id    := New_List;
4014       D           : Entity_Id;
4015
4016       Full_Subtyp  : Entity_Id;
4017       Priv_Subtyp  : Entity_Id;
4018       Utyp         : Entity_Id;
4019       Full_Exp     : Node_Id;
4020
4021    begin
4022       if Is_Private_Type (Unc_Typ)
4023         and then Has_Unknown_Discriminants (Unc_Typ)
4024       then
4025          --  Prepare the subtype completion, Go to base type to
4026          --  find underlying type, because the type may be a generic
4027          --  actual or an explicit subtype.
4028
4029          Utyp        := Underlying_Type (Base_Type (Unc_Typ));
4030          Full_Subtyp := Make_Temporary (Loc, 'C');
4031          Full_Exp    :=
4032            Unchecked_Convert_To (Utyp, Duplicate_Subexpr_No_Checks (E));
4033          Set_Parent (Full_Exp, Parent (E));
4034
4035          Priv_Subtyp := Make_Temporary (Loc, 'P');
4036
4037          Insert_Action (E,
4038            Make_Subtype_Declaration (Loc,
4039              Defining_Identifier => Full_Subtyp,
4040              Subtype_Indication  => Make_Subtype_From_Expr (Full_Exp, Utyp)));
4041
4042          --  Define the dummy private subtype
4043
4044          Set_Ekind          (Priv_Subtyp, Subtype_Kind (Ekind (Unc_Typ)));
4045          Set_Etype          (Priv_Subtyp, Base_Type (Unc_Typ));
4046          Set_Scope          (Priv_Subtyp, Full_Subtyp);
4047          Set_Is_Constrained (Priv_Subtyp);
4048          Set_Is_Tagged_Type (Priv_Subtyp, Is_Tagged_Type (Unc_Typ));
4049          Set_Is_Itype       (Priv_Subtyp);
4050          Set_Associated_Node_For_Itype (Priv_Subtyp, E);
4051
4052          if Is_Tagged_Type  (Priv_Subtyp) then
4053             Set_Class_Wide_Type
4054               (Base_Type (Priv_Subtyp), Class_Wide_Type (Unc_Typ));
4055             Set_Primitive_Operations (Priv_Subtyp,
4056               Primitive_Operations (Unc_Typ));
4057          end if;
4058
4059          Set_Full_View (Priv_Subtyp, Full_Subtyp);
4060
4061          return New_Reference_To (Priv_Subtyp, Loc);
4062
4063       elsif Is_Array_Type (Unc_Typ) then
4064          for J in 1 .. Number_Dimensions (Unc_Typ) loop
4065             Append_To (List_Constr,
4066               Make_Range (Loc,
4067                 Low_Bound =>
4068                   Make_Attribute_Reference (Loc,
4069                     Prefix => Duplicate_Subexpr_No_Checks (E),
4070                     Attribute_Name => Name_First,
4071                     Expressions => New_List (
4072                       Make_Integer_Literal (Loc, J))),
4073
4074                 High_Bound =>
4075                   Make_Attribute_Reference (Loc,
4076                     Prefix         => Duplicate_Subexpr_No_Checks (E),
4077                     Attribute_Name => Name_Last,
4078                     Expressions    => New_List (
4079                       Make_Integer_Literal (Loc, J)))));
4080          end loop;
4081
4082       elsif Is_Class_Wide_Type (Unc_Typ) then
4083          declare
4084             CW_Subtype : Entity_Id;
4085             EQ_Typ     : Entity_Id := Empty;
4086
4087          begin
4088             --  A class-wide equivalent type is not needed when VM_Target
4089             --  because the VM back-ends handle the class-wide object
4090             --  initialization itself (and doesn't need or want the
4091             --  additional intermediate type to handle the assignment).
4092
4093             if Expander_Active and then Tagged_Type_Expansion then
4094
4095                --  If this is the class_wide type of a completion that is
4096                --  a record subtype, set the type of the class_wide type
4097                --  to be the full base type, for use in the expanded code
4098                --  for the equivalent type. Should this be done earlier when
4099                --  the completion is analyzed ???
4100
4101                if Is_Private_Type (Etype (Unc_Typ))
4102                  and then
4103                    Ekind (Full_View (Etype (Unc_Typ))) = E_Record_Subtype
4104                then
4105                   Set_Etype (Unc_Typ, Base_Type (Full_View (Etype (Unc_Typ))));
4106                end if;
4107
4108                EQ_Typ := Make_CW_Equivalent_Type (Unc_Typ, E);
4109             end if;
4110
4111             CW_Subtype := New_Class_Wide_Subtype (Unc_Typ, E);
4112             Set_Equivalent_Type (CW_Subtype, EQ_Typ);
4113             Set_Cloned_Subtype (CW_Subtype, Base_Type (Unc_Typ));
4114
4115             return New_Occurrence_Of (CW_Subtype, Loc);
4116          end;
4117
4118       --  Indefinite record type with discriminants
4119
4120       else
4121          D := First_Discriminant (Unc_Typ);
4122          while Present (D) loop
4123             Append_To (List_Constr,
4124               Make_Selected_Component (Loc,
4125                 Prefix        => Duplicate_Subexpr_No_Checks (E),
4126                 Selector_Name => New_Reference_To (D, Loc)));
4127
4128             Next_Discriminant (D);
4129          end loop;
4130       end if;
4131
4132       return
4133         Make_Subtype_Indication (Loc,
4134           Subtype_Mark => New_Reference_To (Unc_Typ, Loc),
4135           Constraint   =>
4136             Make_Index_Or_Discriminant_Constraint (Loc,
4137               Constraints => List_Constr));
4138    end Make_Subtype_From_Expr;
4139
4140    -----------------------------
4141    -- May_Generate_Large_Temp --
4142    -----------------------------
4143
4144    --  At the current time, the only types that we return False for (i.e.
4145    --  where we decide we know they cannot generate large temps) are ones
4146    --  where we know the size is 256 bits or less at compile time, and we
4147    --  are still not doing a thorough job on arrays and records ???
4148
4149    function May_Generate_Large_Temp (Typ : Entity_Id) return Boolean is
4150    begin
4151       if not Size_Known_At_Compile_Time (Typ) then
4152          return False;
4153
4154       elsif Esize (Typ) /= 0 and then Esize (Typ) <= 256 then
4155          return False;
4156
4157       elsif Is_Array_Type (Typ)
4158         and then Present (Packed_Array_Type (Typ))
4159       then
4160          return May_Generate_Large_Temp (Packed_Array_Type (Typ));
4161
4162       --  We could do more here to find other small types ???
4163
4164       else
4165          return True;
4166       end if;
4167    end May_Generate_Large_Temp;
4168
4169    ----------------------------
4170    -- Needs_Constant_Address --
4171    ----------------------------
4172
4173    function Needs_Constant_Address
4174      (Decl : Node_Id;
4175       Typ  : Entity_Id) return Boolean
4176    is
4177    begin
4178
4179       --  If we have no initialization of any kind, then we don't need to
4180       --  place any restrictions on the address clause, because the object
4181       --  will be elaborated after the address clause is evaluated. This
4182       --  happens if the declaration has no initial expression, or the type
4183       --  has no implicit initialization, or the object is imported.
4184
4185       --  The same holds for all initialized scalar types and all access
4186       --  types. Packed bit arrays of size up to 64 are represented using a
4187       --  modular type with an initialization (to zero) and can be processed
4188       --  like other initialized scalar types.
4189
4190       --  If the type is controlled, code to attach the object to a
4191       --  finalization chain is generated at the point of declaration,
4192       --  and therefore the elaboration of the object cannot be delayed:
4193       --  the address expression must be a constant.
4194
4195       if No (Expression (Decl))
4196         and then not Needs_Finalization (Typ)
4197         and then
4198           (not Has_Non_Null_Base_Init_Proc (Typ)
4199             or else Is_Imported (Defining_Identifier (Decl)))
4200       then
4201          return False;
4202
4203       elsif (Present (Expression (Decl)) and then Is_Scalar_Type (Typ))
4204         or else Is_Access_Type (Typ)
4205         or else
4206           (Is_Bit_Packed_Array (Typ)
4207              and then Is_Modular_Integer_Type (Packed_Array_Type (Typ)))
4208       then
4209          return False;
4210
4211       else
4212
4213          --  Otherwise, we require the address clause to be constant because
4214          --  the call to the initialization procedure (or the attach code) has
4215          --  to happen at the point of the declaration.
4216
4217          --  Actually the IP call has been moved to the freeze actions
4218          --  anyway, so maybe we can relax this restriction???
4219
4220          return True;
4221       end if;
4222    end Needs_Constant_Address;
4223
4224    ----------------------------
4225    -- New_Class_Wide_Subtype --
4226    ----------------------------
4227
4228    function New_Class_Wide_Subtype
4229      (CW_Typ : Entity_Id;
4230       N      : Node_Id) return Entity_Id
4231    is
4232       Res       : constant Entity_Id := Create_Itype (E_Void, N);
4233       Res_Name  : constant Name_Id   := Chars (Res);
4234       Res_Scope : constant Entity_Id := Scope (Res);
4235
4236    begin
4237       Copy_Node (CW_Typ, Res);
4238       Set_Comes_From_Source (Res, False);
4239       Set_Sloc (Res, Sloc (N));
4240       Set_Is_Itype (Res);
4241       Set_Associated_Node_For_Itype (Res, N);
4242       Set_Is_Public (Res, False);   --  By default, may be changed below.
4243       Set_Public_Status (Res);
4244       Set_Chars (Res, Res_Name);
4245       Set_Scope (Res, Res_Scope);
4246       Set_Ekind (Res, E_Class_Wide_Subtype);
4247       Set_Next_Entity (Res, Empty);
4248       Set_Etype (Res, Base_Type (CW_Typ));
4249       Set_Is_Frozen (Res, False);
4250       Set_Freeze_Node (Res, Empty);
4251       return (Res);
4252    end New_Class_Wide_Subtype;
4253
4254    --------------------------------
4255    -- Non_Limited_Designated_Type --
4256    ---------------------------------
4257
4258    function Non_Limited_Designated_Type (T : Entity_Id) return Entity_Id is
4259       Desig : constant Entity_Id := Designated_Type (T);
4260    begin
4261       if Ekind (Desig) = E_Incomplete_Type
4262         and then Present (Non_Limited_View (Desig))
4263       then
4264          return Non_Limited_View (Desig);
4265       else
4266          return Desig;
4267       end if;
4268    end Non_Limited_Designated_Type;
4269
4270    -----------------------------------
4271    -- OK_To_Do_Constant_Replacement --
4272    -----------------------------------
4273
4274    function OK_To_Do_Constant_Replacement (E : Entity_Id) return Boolean is
4275       ES : constant Entity_Id := Scope (E);
4276       CS : Entity_Id;
4277
4278    begin
4279       --  Do not replace statically allocated objects, because they may be
4280       --  modified outside the current scope.
4281
4282       if Is_Statically_Allocated (E) then
4283          return False;
4284
4285       --  Do not replace aliased or volatile objects, since we don't know what
4286       --  else might change the value.
4287
4288       elsif Is_Aliased (E) or else Treat_As_Volatile (E) then
4289          return False;
4290
4291       --  Debug flag -gnatdM disconnects this optimization
4292
4293       elsif Debug_Flag_MM then
4294          return False;
4295
4296       --  Otherwise check scopes
4297
4298       else
4299          CS := Current_Scope;
4300
4301          loop
4302             --  If we are in right scope, replacement is safe
4303
4304             if CS = ES then
4305                return True;
4306
4307             --  Packages do not affect the determination of safety
4308
4309             elsif Ekind (CS) = E_Package then
4310                exit when CS = Standard_Standard;
4311                CS := Scope (CS);
4312
4313             --  Blocks do not affect the determination of safety
4314
4315             elsif Ekind (CS) = E_Block then
4316                CS := Scope (CS);
4317
4318             --  Loops do not affect the determination of safety. Note that we
4319             --  kill all current values on entry to a loop, so we are just
4320             --  talking about processing within a loop here.
4321
4322             elsif Ekind (CS) = E_Loop then
4323                CS := Scope (CS);
4324
4325             --  Otherwise, the reference is dubious, and we cannot be sure that
4326             --  it is safe to do the replacement.
4327
4328             else
4329                exit;
4330             end if;
4331          end loop;
4332
4333          return False;
4334       end if;
4335    end OK_To_Do_Constant_Replacement;
4336
4337    ------------------------------------
4338    -- Possible_Bit_Aligned_Component --
4339    ------------------------------------
4340
4341    function Possible_Bit_Aligned_Component (N : Node_Id) return Boolean is
4342    begin
4343       case Nkind (N) is
4344
4345          --  Case of indexed component
4346
4347          when N_Indexed_Component =>
4348             declare
4349                P    : constant Node_Id   := Prefix (N);
4350                Ptyp : constant Entity_Id := Etype (P);
4351
4352             begin
4353                --  If we know the component size and it is less than 64, then
4354                --  we are definitely OK. The back end always does assignment of
4355                --  misaligned small objects correctly.
4356
4357                if Known_Static_Component_Size (Ptyp)
4358                  and then Component_Size (Ptyp) <= 64
4359                then
4360                   return False;
4361
4362                --  Otherwise, we need to test the prefix, to see if we are
4363                --  indexing from a possibly unaligned component.
4364
4365                else
4366                   return Possible_Bit_Aligned_Component (P);
4367                end if;
4368             end;
4369
4370          --  Case of selected component
4371
4372          when N_Selected_Component =>
4373             declare
4374                P    : constant Node_Id   := Prefix (N);
4375                Comp : constant Entity_Id := Entity (Selector_Name (N));
4376
4377             begin
4378                --  If there is no component clause, then we are in the clear
4379                --  since the back end will never misalign a large component
4380                --  unless it is forced to do so. In the clear means we need
4381                --  only the recursive test on the prefix.
4382
4383                if Component_May_Be_Bit_Aligned (Comp) then
4384                   return True;
4385                else
4386                   return Possible_Bit_Aligned_Component (P);
4387                end if;
4388             end;
4389
4390          --  For a slice, test the prefix, if that is possibly misaligned,
4391          --  then for sure the slice is!
4392
4393          when N_Slice =>
4394             return Possible_Bit_Aligned_Component (Prefix (N));
4395
4396          --  If we have none of the above, it means that we have fallen off the
4397          --  top testing prefixes recursively, and we now have a stand alone
4398          --  object, where we don't have a problem.
4399
4400          when others =>
4401             return False;
4402
4403       end case;
4404    end Possible_Bit_Aligned_Component;
4405
4406    -------------------------
4407    -- Remove_Side_Effects --
4408    -------------------------
4409
4410    procedure Remove_Side_Effects
4411      (Exp          : Node_Id;
4412       Name_Req     : Boolean := False;
4413       Variable_Ref : Boolean := False)
4414    is
4415       Loc          : constant Source_Ptr     := Sloc (Exp);
4416       Exp_Type     : constant Entity_Id      := Etype (Exp);
4417       Svg_Suppress : constant Suppress_Array := Scope_Suppress;
4418       Def_Id       : Entity_Id;
4419       Ref_Type     : Entity_Id;
4420       Res          : Node_Id;
4421       Ptr_Typ_Decl : Node_Id;
4422       New_Exp      : Node_Id;
4423       E            : Node_Id;
4424
4425       function Side_Effect_Free (N : Node_Id) return Boolean;
4426       --  Determines if the tree N represents an expression that is known not
4427       --  to have side effects, and for which no processing is required.
4428
4429       function Side_Effect_Free (L : List_Id) return Boolean;
4430       --  Determines if all elements of the list L are side effect free
4431
4432       function Safe_Prefixed_Reference (N : Node_Id) return Boolean;
4433       --  The argument N is a construct where the Prefix is dereferenced if it
4434       --  is an access type and the result is a variable. The call returns True
4435       --  if the construct is side effect free (not considering side effects in
4436       --  other than the prefix which are to be tested by the caller).
4437
4438       function Within_In_Parameter (N : Node_Id) return Boolean;
4439       --  Determines if N is a subcomponent of a composite in-parameter. If so,
4440       --  N is not side-effect free when the actual is global and modifiable
4441       --  indirectly from within a subprogram, because it may be passed by
4442       --  reference. The front-end must be conservative here and assume that
4443       --  this may happen with any array or record type. On the other hand, we
4444       --  cannot create temporaries for all expressions for which this
4445       --  condition is true, for various reasons that might require clearing up
4446       --  ??? For example, discriminant references that appear out of place, or
4447       --  spurious type errors with class-wide expressions. As a result, we
4448       --  limit the transformation to loop bounds, which is so far the only
4449       --  case that requires it.
4450
4451       -----------------------------
4452       -- Safe_Prefixed_Reference --
4453       -----------------------------
4454
4455       function Safe_Prefixed_Reference (N : Node_Id) return Boolean is
4456       begin
4457          --  If prefix is not side effect free, definitely not safe
4458
4459          if not Side_Effect_Free (Prefix (N)) then
4460             return False;
4461
4462          --  If the prefix is of an access type that is not access-to-constant,
4463          --  then this construct is a variable reference, which means it is to
4464          --  be considered to have side effects if Variable_Ref is set True
4465          --  Exception is an access to an entity that is a constant or an
4466          --  in-parameter which does not come from source, and is the result
4467          --  of a previous removal of side-effects.
4468
4469          elsif Is_Access_Type (Etype (Prefix (N)))
4470            and then not Is_Access_Constant (Etype (Prefix (N)))
4471            and then Variable_Ref
4472          then
4473             if not Is_Entity_Name (Prefix (N)) then
4474                return False;
4475             else
4476                return Ekind (Entity (Prefix (N))) = E_Constant
4477                  or else Ekind (Entity (Prefix (N))) = E_In_Parameter;
4478             end if;
4479
4480          --  The following test is the simplest way of solving a complex
4481          --  problem uncovered by BB08-010: Side effect on loop bound that
4482          --  is a subcomponent of a global variable:
4483          --    If a loop bound is a subcomponent of a global variable, a
4484          --    modification of that variable within the loop may incorrectly
4485          --    affect the execution of the loop.
4486
4487          elsif not
4488            (Nkind (Parent (Parent (N))) /= N_Loop_Parameter_Specification
4489               or else not Within_In_Parameter (Prefix (N)))
4490          then
4491             return False;
4492
4493          --  All other cases are side effect free
4494
4495          else
4496             return True;
4497          end if;
4498       end Safe_Prefixed_Reference;
4499
4500       ----------------------
4501       -- Side_Effect_Free --
4502       ----------------------
4503
4504       function Side_Effect_Free (N : Node_Id) return Boolean is
4505       begin
4506          --  Note on checks that could raise Constraint_Error. Strictly, if
4507          --  we take advantage of 11.6, these checks do not count as side
4508          --  effects. However, we would just as soon consider that they are
4509          --  side effects, since the backend CSE does not work very well on
4510          --  expressions which can raise Constraint_Error. On the other
4511          --  hand, if we do not consider them to be side effect free, then
4512          --  we get some awkward expansions in -gnato mode, resulting in
4513          --  code insertions at a point where we do not have a clear model
4514          --  for performing the insertions.
4515
4516          --  Special handling for entity names
4517
4518          if Is_Entity_Name (N) then
4519
4520             --  If the entity is a constant, it is definitely side effect
4521             --  free. Note that the test of Is_Variable (N) below might
4522             --  be expected to catch this case, but it does not, because
4523             --  this test goes to the original tree, and we may have
4524             --  already rewritten a variable node with a constant as
4525             --  a result of an earlier Force_Evaluation call.
4526
4527             if Ekind_In (Entity (N), E_Constant, E_In_Parameter) then
4528                return True;
4529
4530             --  Functions are not side effect free
4531
4532             elsif Ekind (Entity (N)) = E_Function then
4533                return False;
4534
4535             --  Variables are considered to be a side effect if Variable_Ref
4536             --  is set or if we have a volatile reference and Name_Req is off.
4537             --  If Name_Req is True then we can't help returning a name which
4538             --  effectively allows multiple references in any case.
4539
4540             elsif Is_Variable (N) then
4541                return not Variable_Ref
4542                  and then (not Is_Volatile_Reference (N) or else Name_Req);
4543
4544             --  Any other entity (e.g. a subtype name) is definitely side
4545             --  effect free.
4546
4547             else
4548                return True;
4549             end if;
4550
4551          --  A value known at compile time is always side effect free
4552
4553          elsif Compile_Time_Known_Value (N) then
4554             return True;
4555
4556          --  A variable renaming is not side-effect free, because the
4557          --  renaming will function like a macro in the front-end in
4558          --  some cases, and an assignment can modify the component
4559          --  designated by N, so we need to create a temporary for it.
4560
4561          elsif Is_Entity_Name (Original_Node (N))
4562            and then Is_Renaming_Of_Object (Entity (Original_Node (N)))
4563            and then Ekind (Entity (Original_Node (N))) /= E_Constant
4564          then
4565             return False;
4566          end if;
4567
4568          --  For other than entity names and compile time known values,
4569          --  check the node kind for special processing.
4570
4571          case Nkind (N) is
4572
4573             --  An attribute reference is side effect free if its expressions
4574             --  are side effect free and its prefix is side effect free or
4575             --  is an entity reference.
4576
4577             --  Is this right? what about x'first where x is a variable???
4578
4579             when N_Attribute_Reference =>
4580                return Side_Effect_Free (Expressions (N))
4581                  and then Attribute_Name (N) /= Name_Input
4582                  and then (Is_Entity_Name (Prefix (N))
4583                             or else Side_Effect_Free (Prefix (N)));
4584
4585             --  A binary operator is side effect free if and both operands
4586             --  are side effect free. For this purpose binary operators
4587             --  include membership tests and short circuit forms
4588
4589             when N_Binary_Op | N_Membership_Test | N_Short_Circuit =>
4590                return Side_Effect_Free (Left_Opnd  (N))
4591                         and then
4592                       Side_Effect_Free (Right_Opnd (N));
4593
4594             --  An explicit dereference is side effect free only if it is
4595             --  a side effect free prefixed reference.
4596
4597             when N_Explicit_Dereference =>
4598                return Safe_Prefixed_Reference (N);
4599
4600             --  A call to _rep_to_pos is side effect free, since we generate
4601             --  this pure function call ourselves. Moreover it is critically
4602             --  important to make this exception, since otherwise we can
4603             --  have discriminants in array components which don't look
4604             --  side effect free in the case of an array whose index type
4605             --  is an enumeration type with an enumeration rep clause.
4606
4607             --  All other function calls are not side effect free
4608
4609             when N_Function_Call =>
4610                return Nkind (Name (N)) = N_Identifier
4611                  and then Is_TSS (Name (N), TSS_Rep_To_Pos)
4612                  and then
4613                    Side_Effect_Free (First (Parameter_Associations (N)));
4614
4615             --  An indexed component is side effect free if it is a side
4616             --  effect free prefixed reference and all the indexing
4617             --  expressions are side effect free.
4618
4619             when N_Indexed_Component =>
4620                return Side_Effect_Free (Expressions (N))
4621                  and then Safe_Prefixed_Reference (N);
4622
4623             --  A type qualification is side effect free if the expression
4624             --  is side effect free.
4625
4626             when N_Qualified_Expression =>
4627                return Side_Effect_Free (Expression (N));
4628
4629             --  A selected component is side effect free only if it is a
4630             --  side effect free prefixed reference. If it designates a
4631             --  component with a rep. clause it must be treated has having
4632             --  a potential side effect, because it may be modified through
4633             --  a renaming, and a subsequent use of the renaming as a macro
4634             --  will yield the wrong value. This complex interaction between
4635             --  renaming and removing side effects is a reminder that the
4636             --  latter has become a headache to maintain, and that it should
4637             --  be removed in favor of the gcc mechanism to capture values ???
4638
4639             when N_Selected_Component =>
4640                if Nkind (Parent (N)) = N_Explicit_Dereference
4641                  and then Has_Non_Standard_Rep (Designated_Type (Etype (N)))
4642                then
4643                   return False;
4644                else
4645                   return Safe_Prefixed_Reference (N);
4646                end if;
4647
4648             --  A range is side effect free if the bounds are side effect free
4649
4650             when N_Range =>
4651                return Side_Effect_Free (Low_Bound (N))
4652                  and then Side_Effect_Free (High_Bound (N));
4653
4654             --  A slice is side effect free if it is a side effect free
4655             --  prefixed reference and the bounds are side effect free.
4656
4657             when N_Slice =>
4658                return Side_Effect_Free (Discrete_Range (N))
4659                  and then Safe_Prefixed_Reference (N);
4660
4661             --  A type conversion is side effect free if the expression to be
4662             --  converted is side effect free.
4663
4664             when N_Type_Conversion =>
4665                return Side_Effect_Free (Expression (N));
4666
4667             --  A unary operator is side effect free if the operand
4668             --  is side effect free.
4669
4670             when N_Unary_Op =>
4671                return Side_Effect_Free (Right_Opnd (N));
4672
4673             --  An unchecked type conversion is side effect free only if it
4674             --  is safe and its argument is side effect free.
4675
4676             when N_Unchecked_Type_Conversion =>
4677                return Safe_Unchecked_Type_Conversion (N)
4678                  and then Side_Effect_Free (Expression (N));
4679
4680             --  An unchecked expression is side effect free if its expression
4681             --  is side effect free.
4682
4683             when N_Unchecked_Expression =>
4684                return Side_Effect_Free (Expression (N));
4685
4686             --  A literal is side effect free
4687
4688             when N_Character_Literal    |
4689                  N_Integer_Literal      |
4690                  N_Real_Literal         |
4691                  N_String_Literal       =>
4692                return True;
4693
4694             --  We consider that anything else has side effects. This is a bit
4695             --  crude, but we are pretty close for most common cases, and we
4696             --  are certainly correct (i.e. we never return True when the
4697             --  answer should be False).
4698
4699             when others =>
4700                return False;
4701          end case;
4702       end Side_Effect_Free;
4703
4704       --  A list is side effect free if all elements of the list are
4705       --  side effect free.
4706
4707       function Side_Effect_Free (L : List_Id) return Boolean is
4708          N : Node_Id;
4709
4710       begin
4711          if L = No_List or else L = Error_List then
4712             return True;
4713
4714          else
4715             N := First (L);
4716             while Present (N) loop
4717                if not Side_Effect_Free (N) then
4718                   return False;
4719                else
4720                   Next (N);
4721                end if;
4722             end loop;
4723
4724             return True;
4725          end if;
4726       end Side_Effect_Free;
4727
4728       -------------------------
4729       -- Within_In_Parameter --
4730       -------------------------
4731
4732       function Within_In_Parameter (N : Node_Id) return Boolean is
4733       begin
4734          if not Comes_From_Source (N) then
4735             return False;
4736
4737          elsif Is_Entity_Name (N) then
4738             return Ekind (Entity (N)) = E_In_Parameter;
4739
4740          elsif Nkind (N) = N_Indexed_Component
4741            or else Nkind (N) = N_Selected_Component
4742          then
4743             return Within_In_Parameter (Prefix (N));
4744          else
4745
4746             return False;
4747          end if;
4748       end Within_In_Parameter;
4749
4750    --  Start of processing for Remove_Side_Effects
4751
4752    begin
4753       --  If we are side effect free already or expansion is disabled,
4754       --  there is nothing to do.
4755
4756       if Side_Effect_Free (Exp) or else not Expander_Active then
4757          return;
4758       end if;
4759
4760       --  All this must not have any checks
4761
4762       Scope_Suppress := (others => True);
4763
4764       --  If it is a scalar type and we need to capture the value, just make
4765       --  a copy. Likewise for a function call, an attribute reference, an
4766       --  allocator, or an operator. And if we have a volatile reference and
4767       --  Name_Req is not set (see comments above for Side_Effect_Free).
4768
4769       if Is_Elementary_Type (Exp_Type)
4770         and then (Variable_Ref
4771                    or else Nkind (Exp) = N_Function_Call
4772                    or else Nkind (Exp) = N_Attribute_Reference
4773                    or else Nkind (Exp) = N_Allocator
4774                    or else Nkind (Exp) in N_Op
4775                    or else (not Name_Req and then Is_Volatile_Reference (Exp)))
4776       then
4777          Def_Id := Make_Temporary (Loc, 'R', Exp);
4778          Set_Etype (Def_Id, Exp_Type);
4779          Res := New_Reference_To (Def_Id, Loc);
4780
4781          --  If the expression is a packed reference, it must be reanalyzed
4782          --  and expanded, depending on context. This is the case for actuals
4783          --  where a constraint check may capture the actual before expansion
4784          --  of the call is complete.
4785
4786          if Nkind (Exp) = N_Indexed_Component
4787            and then Is_Packed (Etype (Prefix (Exp)))
4788          then
4789             Set_Analyzed (Exp, False);
4790             Set_Analyzed (Prefix (Exp), False);
4791          end if;
4792
4793          E :=
4794            Make_Object_Declaration (Loc,
4795              Defining_Identifier => Def_Id,
4796              Object_Definition   => New_Reference_To (Exp_Type, Loc),
4797              Constant_Present    => True,
4798              Expression          => Relocate_Node (Exp));
4799
4800          Set_Assignment_OK (E);
4801          Insert_Action (Exp, E);
4802
4803       --  If the expression has the form v.all then we can just capture
4804       --  the pointer, and then do an explicit dereference on the result.
4805
4806       elsif Nkind (Exp) = N_Explicit_Dereference then
4807          Def_Id := Make_Temporary (Loc, 'R', Exp);
4808          Res :=
4809            Make_Explicit_Dereference (Loc, New_Reference_To (Def_Id, Loc));
4810
4811          Insert_Action (Exp,
4812            Make_Object_Declaration (Loc,
4813              Defining_Identifier => Def_Id,
4814              Object_Definition   =>
4815                New_Reference_To (Etype (Prefix (Exp)), Loc),
4816              Constant_Present    => True,
4817              Expression          => Relocate_Node (Prefix (Exp))));
4818
4819       --  Similar processing for an unchecked conversion of an expression
4820       --  of the form v.all, where we want the same kind of treatment.
4821
4822       elsif Nkind (Exp) = N_Unchecked_Type_Conversion
4823         and then Nkind (Expression (Exp)) = N_Explicit_Dereference
4824       then
4825          Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
4826          Scope_Suppress := Svg_Suppress;
4827          return;
4828
4829       --  If this is a type conversion, leave the type conversion and remove
4830       --  the side effects in the expression. This is important in several
4831       --  circumstances: for change of representations, and also when this is
4832       --  a view conversion to a smaller object, where gigi can end up creating
4833       --  its own temporary of the wrong size.
4834
4835       elsif Nkind (Exp) = N_Type_Conversion then
4836          Remove_Side_Effects (Expression (Exp), Name_Req, Variable_Ref);
4837          Scope_Suppress := Svg_Suppress;
4838          return;
4839
4840       --  If this is an unchecked conversion that Gigi can't handle, make
4841       --  a copy or a use a renaming to capture the value.
4842
4843       elsif Nkind (Exp) = N_Unchecked_Type_Conversion
4844         and then not Safe_Unchecked_Type_Conversion (Exp)
4845       then
4846          if CW_Or_Has_Controlled_Part (Exp_Type) then
4847
4848             --  Use a renaming to capture the expression, rather than create
4849             --  a controlled temporary.
4850
4851             Def_Id := Make_Temporary (Loc, 'R', Exp);
4852             Res := New_Reference_To (Def_Id, Loc);
4853
4854             Insert_Action (Exp,
4855               Make_Object_Renaming_Declaration (Loc,
4856                 Defining_Identifier => Def_Id,
4857                 Subtype_Mark        => New_Reference_To (Exp_Type, Loc),
4858                 Name                => Relocate_Node (Exp)));
4859
4860          else
4861             Def_Id := Make_Temporary (Loc, 'R', Exp);
4862             Set_Etype (Def_Id, Exp_Type);
4863             Res := New_Reference_To (Def_Id, Loc);
4864
4865             E :=
4866               Make_Object_Declaration (Loc,
4867                 Defining_Identifier => Def_Id,
4868                 Object_Definition   => New_Reference_To (Exp_Type, Loc),
4869                 Constant_Present    => not Is_Variable (Exp),
4870                 Expression          => Relocate_Node (Exp));
4871
4872             Set_Assignment_OK (E);
4873             Insert_Action (Exp, E);
4874          end if;
4875
4876       --  For expressions that denote objects, we can use a renaming scheme.
4877       --  This is needed for correctness in the case of a volatile object
4878       --  of a non-volatile type because the Make_Reference call of the
4879       --  "default" approach would generate an illegal access value (an access
4880       --  value cannot designate such an object - see Analyze_Reference).
4881       --  We skip using this scheme if we have an object of a volatile type
4882       --  and we do not have Name_Req set true (see comments above for
4883       --  Side_Effect_Free).
4884
4885       elsif Is_Object_Reference (Exp)
4886         and then Nkind (Exp) /= N_Function_Call
4887         and then (Name_Req or else not Treat_As_Volatile (Exp_Type))
4888       then
4889          Def_Id := Make_Temporary (Loc, 'R', Exp);
4890
4891          if Nkind (Exp) = N_Selected_Component
4892            and then Nkind (Prefix (Exp)) = N_Function_Call
4893            and then Is_Array_Type (Exp_Type)
4894          then
4895             --  Avoid generating a variable-sized temporary, by generating
4896             --  the renaming declaration just for the function call. The
4897             --  transformation could be refined to apply only when the array
4898             --  component is constrained by a discriminant???
4899
4900             Res :=
4901               Make_Selected_Component (Loc,
4902                 Prefix => New_Occurrence_Of (Def_Id, Loc),
4903                 Selector_Name => Selector_Name (Exp));
4904
4905             Insert_Action (Exp,
4906               Make_Object_Renaming_Declaration (Loc,
4907                 Defining_Identifier => Def_Id,
4908                 Subtype_Mark        =>
4909                   New_Reference_To (Base_Type (Etype (Prefix (Exp))), Loc),
4910                 Name                => Relocate_Node (Prefix (Exp))));
4911
4912          else
4913             Res := New_Reference_To (Def_Id, Loc);
4914
4915             Insert_Action (Exp,
4916               Make_Object_Renaming_Declaration (Loc,
4917                 Defining_Identifier => Def_Id,
4918                 Subtype_Mark        => New_Reference_To (Exp_Type, Loc),
4919                 Name                => Relocate_Node (Exp)));
4920          end if;
4921
4922          --  If this is a packed reference, or a selected component with a
4923          --  non-standard representation, a reference to the temporary will
4924          --  be replaced by a copy of the original expression (see
4925          --  Exp_Ch2.Expand_Renaming). Otherwise the temporary must be
4926          --  elaborated by gigi, and is of course not to be replaced in-line
4927          --  by the expression it renames, which would defeat the purpose of
4928          --  removing the side-effect.
4929
4930          if (Nkind (Exp) = N_Selected_Component
4931               or else Nkind (Exp) = N_Indexed_Component)
4932            and then Has_Non_Standard_Rep (Etype (Prefix (Exp)))
4933          then
4934             null;
4935          else
4936             Set_Is_Renaming_Of_Object (Def_Id, False);
4937          end if;
4938
4939       --  Otherwise we generate a reference to the value
4940
4941       else
4942          --  Special processing for function calls that return a limited type.
4943          --  We need to build a declaration that will enable build-in-place
4944          --  expansion of the call. This is not done if the context is already
4945          --  an object declaration, to prevent infinite recursion.
4946
4947          --  This is relevant only in Ada 2005 mode. In Ada 95 programs we have
4948          --  to accommodate functions returning limited objects by reference.
4949
4950          if Nkind (Exp) = N_Function_Call
4951            and then Is_Inherently_Limited_Type (Etype (Exp))
4952            and then Nkind (Parent (Exp)) /= N_Object_Declaration
4953            and then Ada_Version >= Ada_05
4954          then
4955             declare
4956                Obj  : constant Entity_Id := Make_Temporary (Loc, 'F', Exp);
4957                Decl : Node_Id;
4958
4959             begin
4960                Decl :=
4961                  Make_Object_Declaration (Loc,
4962                    Defining_Identifier => Obj,
4963                    Object_Definition   => New_Occurrence_Of (Exp_Type, Loc),
4964                    Expression          => Relocate_Node (Exp));
4965
4966                Insert_Action (Exp, Decl);
4967                Set_Etype (Obj, Exp_Type);
4968                Rewrite (Exp, New_Occurrence_Of (Obj, Loc));
4969                return;
4970             end;
4971          end if;
4972
4973          Ref_Type := Make_Temporary (Loc, 'A');
4974
4975          Ptr_Typ_Decl :=
4976            Make_Full_Type_Declaration (Loc,
4977              Defining_Identifier => Ref_Type,
4978              Type_Definition =>
4979                Make_Access_To_Object_Definition (Loc,
4980                  All_Present => True,
4981                  Subtype_Indication =>
4982                    New_Reference_To (Exp_Type, Loc)));
4983
4984          E := Exp;
4985          Insert_Action (Exp, Ptr_Typ_Decl);
4986
4987          Def_Id := Make_Temporary (Loc, 'R', Exp);
4988          Set_Etype (Def_Id, Exp_Type);
4989
4990          Res :=
4991            Make_Explicit_Dereference (Loc,
4992              Prefix => New_Reference_To (Def_Id, Loc));
4993
4994          if Nkind (E) = N_Explicit_Dereference then
4995             New_Exp := Relocate_Node (Prefix (E));
4996          else
4997             E := Relocate_Node (E);
4998             New_Exp := Make_Reference (Loc, E);
4999          end if;
5000
5001          if Is_Delayed_Aggregate (E) then
5002
5003             --  The expansion of nested aggregates is delayed until the
5004             --  enclosing aggregate is expanded. As aggregates are often
5005             --  qualified, the predicate applies to qualified expressions
5006             --  as well, indicating that the enclosing aggregate has not
5007             --  been expanded yet. At this point the aggregate is part of
5008             --  a stand-alone declaration, and must be fully expanded.
5009
5010             if Nkind (E) = N_Qualified_Expression then
5011                Set_Expansion_Delayed (Expression (E), False);
5012                Set_Analyzed (Expression (E), False);
5013             else
5014                Set_Expansion_Delayed (E, False);
5015             end if;
5016
5017             Set_Analyzed (E, False);
5018          end if;
5019
5020          Insert_Action (Exp,
5021            Make_Object_Declaration (Loc,
5022              Defining_Identifier => Def_Id,
5023              Object_Definition   => New_Reference_To (Ref_Type, Loc),
5024              Constant_Present    => True,
5025              Expression          => New_Exp));
5026       end if;
5027
5028       --  Preserve the Assignment_OK flag in all copies, since at least
5029       --  one copy may be used in a context where this flag must be set
5030       --  (otherwise why would the flag be set in the first place).
5031
5032       Set_Assignment_OK (Res, Assignment_OK (Exp));
5033
5034       --  Finally rewrite the original expression and we are done
5035
5036       Rewrite (Exp, Res);
5037       Analyze_And_Resolve (Exp, Exp_Type);
5038       Scope_Suppress := Svg_Suppress;
5039    end Remove_Side_Effects;
5040
5041    ---------------------------
5042    -- Represented_As_Scalar --
5043    ---------------------------
5044
5045    function Represented_As_Scalar (T : Entity_Id) return Boolean is
5046       UT : constant Entity_Id := Underlying_Type (T);
5047    begin
5048       return Is_Scalar_Type (UT)
5049         or else (Is_Bit_Packed_Array (UT)
5050                    and then Is_Scalar_Type (Packed_Array_Type (UT)));
5051    end Represented_As_Scalar;
5052
5053    ------------------------------------
5054    -- Safe_Unchecked_Type_Conversion --
5055    ------------------------------------
5056
5057    --  Note: this function knows quite a bit about the exact requirements
5058    --  of Gigi with respect to unchecked type conversions, and its code
5059    --  must be coordinated with any changes in Gigi in this area.
5060
5061    --  The above requirements should be documented in Sinfo ???
5062
5063    function Safe_Unchecked_Type_Conversion (Exp : Node_Id) return Boolean is
5064       Otyp   : Entity_Id;
5065       Ityp   : Entity_Id;
5066       Oalign : Uint;
5067       Ialign : Uint;
5068       Pexp   : constant Node_Id := Parent (Exp);
5069
5070    begin
5071       --  If the expression is the RHS of an assignment or object declaration
5072       --   we are always OK because there will always be a target.
5073
5074       --  Object renaming declarations, (generated for view conversions of
5075       --  actuals in inlined calls), like object declarations, provide an
5076       --  explicit type, and are safe as well.
5077
5078       if (Nkind (Pexp) = N_Assignment_Statement
5079            and then Expression (Pexp) = Exp)
5080         or else Nkind (Pexp) = N_Object_Declaration
5081         or else Nkind (Pexp) = N_Object_Renaming_Declaration
5082       then
5083          return True;
5084
5085       --  If the expression is the prefix of an N_Selected_Component
5086       --  we should also be OK because GCC knows to look inside the
5087       --  conversion except if the type is discriminated. We assume
5088       --  that we are OK anyway if the type is not set yet or if it is
5089       --  controlled since we can't afford to introduce a temporary in
5090       --  this case.
5091
5092       elsif Nkind (Pexp) = N_Selected_Component
5093          and then Prefix (Pexp) = Exp
5094       then
5095          if No (Etype (Pexp)) then
5096             return True;
5097          else
5098             return
5099               not Has_Discriminants (Etype (Pexp))
5100                 or else Is_Constrained (Etype (Pexp));
5101          end if;
5102       end if;
5103
5104       --  Set the output type, this comes from Etype if it is set, otherwise
5105       --  we take it from the subtype mark, which we assume was already
5106       --  fully analyzed.
5107
5108       if Present (Etype (Exp)) then
5109          Otyp := Etype (Exp);
5110       else
5111          Otyp := Entity (Subtype_Mark (Exp));
5112       end if;
5113
5114       --  The input type always comes from the expression, and we assume
5115       --  this is indeed always analyzed, so we can simply get the Etype.
5116
5117       Ityp := Etype (Expression (Exp));
5118
5119       --  Initialize alignments to unknown so far
5120
5121       Oalign := No_Uint;
5122       Ialign := No_Uint;
5123
5124       --  Replace a concurrent type by its corresponding record type
5125       --  and each type by its underlying type and do the tests on those.
5126       --  The original type may be a private type whose completion is a
5127       --  concurrent type, so find the underlying type first.
5128
5129       if Present (Underlying_Type (Otyp)) then
5130          Otyp := Underlying_Type (Otyp);
5131       end if;
5132
5133       if Present (Underlying_Type (Ityp)) then
5134          Ityp := Underlying_Type (Ityp);
5135       end if;
5136
5137       if Is_Concurrent_Type (Otyp) then
5138          Otyp := Corresponding_Record_Type (Otyp);
5139       end if;
5140
5141       if Is_Concurrent_Type (Ityp) then
5142          Ityp := Corresponding_Record_Type (Ityp);
5143       end if;
5144
5145       --  If the base types are the same, we know there is no problem since
5146       --  this conversion will be a noop.
5147
5148       if Implementation_Base_Type (Otyp) = Implementation_Base_Type (Ityp) then
5149          return True;
5150
5151       --  Same if this is an upwards conversion of an untagged type, and there
5152       --  are no constraints involved (could be more general???)
5153
5154       elsif Etype (Ityp) = Otyp
5155         and then not Is_Tagged_Type (Ityp)
5156         and then not Has_Discriminants (Ityp)
5157         and then No (First_Rep_Item (Base_Type (Ityp)))
5158       then
5159          return True;
5160
5161       --  If the expression has an access type (object or subprogram) we
5162       --  assume that the conversion is safe, because the size of the target
5163       --  is safe, even if it is a record (which might be treated as having
5164       --  unknown size at this point).
5165
5166       elsif Is_Access_Type (Ityp) then
5167          return True;
5168
5169       --  If the size of output type is known at compile time, there is
5170       --  never a problem.  Note that unconstrained records are considered
5171       --  to be of known size, but we can't consider them that way here,
5172       --  because we are talking about the actual size of the object.
5173
5174       --  We also make sure that in addition to the size being known, we do
5175       --  not have a case which might generate an embarrassingly large temp
5176       --  in stack checking mode.
5177
5178       elsif Size_Known_At_Compile_Time (Otyp)
5179         and then
5180           (not Stack_Checking_Enabled
5181              or else not May_Generate_Large_Temp (Otyp))
5182         and then not (Is_Record_Type (Otyp) and then not Is_Constrained (Otyp))
5183       then
5184          return True;
5185
5186       --  If either type is tagged, then we know the alignment is OK so
5187       --  Gigi will be able to use pointer punning.
5188
5189       elsif Is_Tagged_Type (Otyp) or else Is_Tagged_Type (Ityp) then
5190          return True;
5191
5192       --  If either type is a limited record type, we cannot do a copy, so
5193       --  say safe since there's nothing else we can do.
5194
5195       elsif Is_Limited_Record (Otyp) or else Is_Limited_Record (Ityp) then
5196          return True;
5197
5198       --  Conversions to and from packed array types are always ignored and
5199       --  hence are safe.
5200
5201       elsif Is_Packed_Array_Type (Otyp)
5202         or else Is_Packed_Array_Type (Ityp)
5203       then
5204          return True;
5205       end if;
5206
5207       --  The only other cases known to be safe is if the input type's
5208       --  alignment is known to be at least the maximum alignment for the
5209       --  target or if both alignments are known and the output type's
5210       --  alignment is no stricter than the input's.  We can use the alignment
5211       --  of the component type of an array if a type is an unpacked
5212       --  array type.
5213
5214       if Present (Alignment_Clause (Otyp)) then
5215          Oalign := Expr_Value (Expression (Alignment_Clause (Otyp)));
5216
5217       elsif Is_Array_Type (Otyp)
5218         and then Present (Alignment_Clause (Component_Type (Otyp)))
5219       then
5220          Oalign := Expr_Value (Expression (Alignment_Clause
5221                                            (Component_Type (Otyp))));
5222       end if;
5223
5224       if Present (Alignment_Clause (Ityp)) then
5225          Ialign := Expr_Value (Expression (Alignment_Clause (Ityp)));
5226
5227       elsif Is_Array_Type (Ityp)
5228         and then Present (Alignment_Clause (Component_Type (Ityp)))
5229       then
5230          Ialign := Expr_Value (Expression (Alignment_Clause
5231                                            (Component_Type (Ityp))));
5232       end if;
5233
5234       if Ialign /= No_Uint and then Ialign > Maximum_Alignment then
5235          return True;
5236
5237       elsif Ialign /= No_Uint and then Oalign /= No_Uint
5238         and then Ialign <= Oalign
5239       then
5240          return True;
5241
5242       --   Otherwise, Gigi cannot handle this and we must make a temporary
5243
5244       else
5245          return False;
5246       end if;
5247    end Safe_Unchecked_Type_Conversion;
5248
5249    ---------------------------------
5250    -- Set_Current_Value_Condition --
5251    ---------------------------------
5252
5253    --  Note: the implementation of this procedure is very closely tied to the
5254    --  implementation of Get_Current_Value_Condition. Here we set required
5255    --  Current_Value fields, and in Get_Current_Value_Condition, we interpret
5256    --  them, so they must have a consistent view.
5257
5258    procedure Set_Current_Value_Condition (Cnode : Node_Id) is
5259
5260       procedure Set_Entity_Current_Value (N : Node_Id);
5261       --  If N is an entity reference, where the entity is of an appropriate
5262       --  kind, then set the current value of this entity to Cnode, unless
5263       --  there is already a definite value set there.
5264
5265       procedure Set_Expression_Current_Value (N : Node_Id);
5266       --  If N is of an appropriate form, sets an appropriate entry in current
5267       --  value fields of relevant entities. Multiple entities can be affected
5268       --  in the case of an AND or AND THEN.
5269
5270       ------------------------------
5271       -- Set_Entity_Current_Value --
5272       ------------------------------
5273
5274       procedure Set_Entity_Current_Value (N : Node_Id) is
5275       begin
5276          if Is_Entity_Name (N) then
5277             declare
5278                Ent : constant Entity_Id := Entity (N);
5279
5280             begin
5281                --  Don't capture if not safe to do so
5282
5283                if not Safe_To_Capture_Value (N, Ent, Cond => True) then
5284                   return;
5285                end if;
5286
5287                --  Here we have a case where the Current_Value field may
5288                --  need to be set. We set it if it is not already set to a
5289                --  compile time expression value.
5290
5291                --  Note that this represents a decision that one condition
5292                --  blots out another previous one. That's certainly right
5293                --  if they occur at the same level. If the second one is
5294                --  nested, then the decision is neither right nor wrong (it
5295                --  would be equally OK to leave the outer one in place, or
5296                --  take the new inner one. Really we should record both, but
5297                --  our data structures are not that elaborate.
5298
5299                if Nkind (Current_Value (Ent)) not in N_Subexpr then
5300                   Set_Current_Value (Ent, Cnode);
5301                end if;
5302             end;
5303          end if;
5304       end Set_Entity_Current_Value;
5305
5306       ----------------------------------
5307       -- Set_Expression_Current_Value --
5308       ----------------------------------
5309
5310       procedure Set_Expression_Current_Value (N : Node_Id) is
5311          Cond : Node_Id;
5312
5313       begin
5314          Cond := N;
5315
5316          --  Loop to deal with (ignore for now) any NOT operators present. The
5317          --  presence of NOT operators will be handled properly when we call
5318          --  Get_Current_Value_Condition.
5319
5320          while Nkind (Cond) = N_Op_Not loop
5321             Cond := Right_Opnd (Cond);
5322          end loop;
5323
5324          --  For an AND or AND THEN, recursively process operands
5325
5326          if Nkind (Cond) = N_Op_And or else Nkind (Cond) = N_And_Then then
5327             Set_Expression_Current_Value (Left_Opnd (Cond));
5328             Set_Expression_Current_Value (Right_Opnd (Cond));
5329             return;
5330          end if;
5331
5332          --  Check possible relational operator
5333
5334          if Nkind (Cond) in N_Op_Compare then
5335             if Compile_Time_Known_Value (Right_Opnd (Cond)) then
5336                Set_Entity_Current_Value (Left_Opnd (Cond));
5337             elsif Compile_Time_Known_Value (Left_Opnd (Cond)) then
5338                Set_Entity_Current_Value (Right_Opnd (Cond));
5339             end if;
5340
5341             --  Check possible boolean variable reference
5342
5343          else
5344             Set_Entity_Current_Value (Cond);
5345          end if;
5346       end Set_Expression_Current_Value;
5347
5348    --  Start of processing for Set_Current_Value_Condition
5349
5350    begin
5351       Set_Expression_Current_Value (Condition (Cnode));
5352    end Set_Current_Value_Condition;
5353
5354    --------------------------
5355    -- Set_Elaboration_Flag --
5356    --------------------------
5357
5358    procedure Set_Elaboration_Flag (N : Node_Id; Spec_Id : Entity_Id) is
5359       Loc : constant Source_Ptr := Sloc (N);
5360       Ent : constant Entity_Id  := Elaboration_Entity (Spec_Id);
5361       Asn : Node_Id;
5362
5363    begin
5364       if Present (Ent) then
5365
5366          --  Nothing to do if at the compilation unit level, because in this
5367          --  case the flag is set by the binder generated elaboration routine.
5368
5369          if Nkind (Parent (N)) = N_Compilation_Unit then
5370             null;
5371
5372          --  Here we do need to generate an assignment statement
5373
5374          else
5375             Check_Restriction (No_Elaboration_Code, N);
5376             Asn :=
5377               Make_Assignment_Statement (Loc,
5378                 Name       => New_Occurrence_Of (Ent, Loc),
5379                 Expression => New_Occurrence_Of (Standard_True, Loc));
5380
5381             if Nkind (Parent (N)) = N_Subunit then
5382                Insert_After (Corresponding_Stub (Parent (N)), Asn);
5383             else
5384                Insert_After (N, Asn);
5385             end if;
5386
5387             Analyze (Asn);
5388
5389             --  Kill current value indication. This is necessary because the
5390             --  tests of this flag are inserted out of sequence and must not
5391             --  pick up bogus indications of the wrong constant value.
5392
5393             Set_Current_Value (Ent, Empty);
5394          end if;
5395       end if;
5396    end Set_Elaboration_Flag;
5397
5398    ----------------------------
5399    -- Set_Renamed_Subprogram --
5400    ----------------------------
5401
5402    procedure Set_Renamed_Subprogram (N : Node_Id; E : Entity_Id) is
5403    begin
5404       --  If input node is an identifier, we can just reset it
5405
5406       if Nkind (N) = N_Identifier then
5407          Set_Chars  (N, Chars (E));
5408          Set_Entity (N, E);
5409
5410          --  Otherwise we have to do a rewrite, preserving Comes_From_Source
5411
5412       else
5413          declare
5414             CS : constant Boolean := Comes_From_Source (N);
5415          begin
5416             Rewrite (N, Make_Identifier (Sloc (N), Chars => Chars (E)));
5417             Set_Entity (N, E);
5418             Set_Comes_From_Source (N, CS);
5419             Set_Analyzed (N, True);
5420          end;
5421       end if;
5422    end Set_Renamed_Subprogram;
5423
5424    ----------------------------------
5425    -- Silly_Boolean_Array_Not_Test --
5426    ----------------------------------
5427
5428    --  This procedure implements an odd and silly test. We explicitly check
5429    --  for the case where the 'First of the component type is equal to the
5430    --  'Last of this component type, and if this is the case, we make sure
5431    --  that constraint error is raised. The reason is that the NOT is bound
5432    --  to cause CE in this case, and we will not otherwise catch it.
5433
5434    --  No such check is required for AND and OR, since for both these cases
5435    --  False op False = False, and True op True = True. For the XOR case,
5436    --  see Silly_Boolean_Array_Xor_Test.
5437
5438    --  Believe it or not, this was reported as a bug. Note that nearly
5439    --  always, the test will evaluate statically to False, so the code will
5440    --  be statically removed, and no extra overhead caused.
5441
5442    procedure Silly_Boolean_Array_Not_Test (N : Node_Id; T : Entity_Id) is
5443       Loc : constant Source_Ptr := Sloc (N);
5444       CT  : constant Entity_Id  := Component_Type (T);
5445
5446    begin
5447       --  The check we install is
5448
5449       --    constraint_error when
5450       --      component_type'first = component_type'last
5451       --        and then array_type'Length /= 0)
5452
5453       --  We need the last guard because we don't want to raise CE for empty
5454       --  arrays since no out of range values result. (Empty arrays with a
5455       --  component type of True .. True -- very useful -- even the ACATS
5456       --  does not test that marginal case!)
5457
5458       Insert_Action (N,
5459         Make_Raise_Constraint_Error (Loc,
5460           Condition =>
5461             Make_And_Then (Loc,
5462               Left_Opnd =>
5463                 Make_Op_Eq (Loc,
5464                   Left_Opnd =>
5465                     Make_Attribute_Reference (Loc,
5466                       Prefix         => New_Occurrence_Of (CT, Loc),
5467                       Attribute_Name => Name_First),
5468
5469                   Right_Opnd =>
5470                     Make_Attribute_Reference (Loc,
5471                       Prefix         => New_Occurrence_Of (CT, Loc),
5472                       Attribute_Name => Name_Last)),
5473
5474               Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
5475           Reason => CE_Range_Check_Failed));
5476    end Silly_Boolean_Array_Not_Test;
5477
5478    ----------------------------------
5479    -- Silly_Boolean_Array_Xor_Test --
5480    ----------------------------------
5481
5482    --  This procedure implements an odd and silly test. We explicitly check
5483    --  for the XOR case where the component type is True .. True, since this
5484    --  will raise constraint error. A special check is required since CE
5485    --  will not be generated otherwise (cf Expand_Packed_Not).
5486
5487    --  No such check is required for AND and OR, since for both these cases
5488    --  False op False = False, and True op True = True, and no check is
5489    --  required for the case of False .. False, since False xor False = False.
5490    --  See also Silly_Boolean_Array_Not_Test
5491
5492    procedure Silly_Boolean_Array_Xor_Test (N : Node_Id; T : Entity_Id) is
5493       Loc : constant Source_Ptr := Sloc (N);
5494       CT  : constant Entity_Id  := Component_Type (T);
5495
5496    begin
5497       --  The check we install is
5498
5499       --    constraint_error when
5500       --      Boolean (component_type'First)
5501       --        and then Boolean (component_type'Last)
5502       --        and then array_type'Length /= 0)
5503
5504       --  We need the last guard because we don't want to raise CE for empty
5505       --  arrays since no out of range values result (Empty arrays with a
5506       --  component type of True .. True -- very useful -- even the ACATS
5507       --  does not test that marginal case!).
5508
5509       Insert_Action (N,
5510         Make_Raise_Constraint_Error (Loc,
5511           Condition =>
5512             Make_And_Then (Loc,
5513               Left_Opnd =>
5514                 Make_And_Then (Loc,
5515                   Left_Opnd =>
5516                     Convert_To (Standard_Boolean,
5517                       Make_Attribute_Reference (Loc,
5518                         Prefix         => New_Occurrence_Of (CT, Loc),
5519                         Attribute_Name => Name_First)),
5520
5521                   Right_Opnd =>
5522                     Convert_To (Standard_Boolean,
5523                       Make_Attribute_Reference (Loc,
5524                         Prefix         => New_Occurrence_Of (CT, Loc),
5525                         Attribute_Name => Name_Last))),
5526
5527               Right_Opnd => Make_Non_Empty_Check (Loc, Right_Opnd (N))),
5528           Reason => CE_Range_Check_Failed));
5529    end Silly_Boolean_Array_Xor_Test;
5530
5531    --------------------------
5532    -- Target_Has_Fixed_Ops --
5533    --------------------------
5534
5535    Integer_Sized_Small : Ureal;
5536    --  Set to 2.0 ** -(Integer'Size - 1) the first time that this
5537    --  function is called (we don't want to compute it more than once!)
5538
5539    Long_Integer_Sized_Small : Ureal;
5540    --  Set to 2.0 ** -(Long_Integer'Size - 1) the first time that this
5541    --  function is called (we don't want to compute it more than once)
5542
5543    First_Time_For_THFO : Boolean := True;
5544    --  Set to False after first call (if Fractional_Fixed_Ops_On_Target)
5545
5546    function Target_Has_Fixed_Ops
5547      (Left_Typ   : Entity_Id;
5548       Right_Typ  : Entity_Id;
5549       Result_Typ : Entity_Id) return Boolean
5550    is
5551       function Is_Fractional_Type (Typ : Entity_Id) return Boolean;
5552       --  Return True if the given type is a fixed-point type with a small
5553       --  value equal to 2 ** (-(T'Object_Size - 1)) and whose values have
5554       --  an absolute value less than 1.0. This is currently limited
5555       --  to fixed-point types that map to Integer or Long_Integer.
5556
5557       ------------------------
5558       -- Is_Fractional_Type --
5559       ------------------------
5560
5561       function Is_Fractional_Type (Typ : Entity_Id) return Boolean is
5562       begin
5563          if Esize (Typ) = Standard_Integer_Size then
5564             return Small_Value (Typ) = Integer_Sized_Small;
5565
5566          elsif Esize (Typ) = Standard_Long_Integer_Size then
5567             return Small_Value (Typ) = Long_Integer_Sized_Small;
5568
5569          else
5570             return False;
5571          end if;
5572       end Is_Fractional_Type;
5573
5574    --  Start of processing for Target_Has_Fixed_Ops
5575
5576    begin
5577       --  Return False if Fractional_Fixed_Ops_On_Target is false
5578
5579       if not Fractional_Fixed_Ops_On_Target then
5580          return False;
5581       end if;
5582
5583       --  Here the target has Fractional_Fixed_Ops, if first time, compute
5584       --  standard constants used by Is_Fractional_Type.
5585
5586       if First_Time_For_THFO then
5587          First_Time_For_THFO := False;
5588
5589          Integer_Sized_Small :=
5590            UR_From_Components
5591              (Num   => Uint_1,
5592               Den   => UI_From_Int (Standard_Integer_Size - 1),
5593               Rbase => 2);
5594
5595          Long_Integer_Sized_Small :=
5596            UR_From_Components
5597              (Num   => Uint_1,
5598               Den   => UI_From_Int (Standard_Long_Integer_Size - 1),
5599               Rbase => 2);
5600       end if;
5601
5602       --  Return True if target supports fixed-by-fixed multiply/divide
5603       --  for fractional fixed-point types (see Is_Fractional_Type) and
5604       --  the operand and result types are equivalent fractional types.
5605
5606       return Is_Fractional_Type (Base_Type (Left_Typ))
5607         and then Is_Fractional_Type (Base_Type (Right_Typ))
5608         and then Is_Fractional_Type (Base_Type (Result_Typ))
5609         and then Esize (Left_Typ) = Esize (Right_Typ)
5610         and then Esize (Left_Typ) = Esize (Result_Typ);
5611    end Target_Has_Fixed_Ops;
5612
5613    ------------------------------------------
5614    -- Type_May_Have_Bit_Aligned_Components --
5615    ------------------------------------------
5616
5617    function Type_May_Have_Bit_Aligned_Components
5618      (Typ : Entity_Id) return Boolean
5619    is
5620    begin
5621       --  Array type, check component type
5622
5623       if Is_Array_Type (Typ) then
5624          return
5625            Type_May_Have_Bit_Aligned_Components (Component_Type (Typ));
5626
5627       --  Record type, check components
5628
5629       elsif Is_Record_Type (Typ) then
5630          declare
5631             E : Entity_Id;
5632
5633          begin
5634             E := First_Component_Or_Discriminant (Typ);
5635             while Present (E) loop
5636                if Component_May_Be_Bit_Aligned (E)
5637                  or else Type_May_Have_Bit_Aligned_Components (Etype (E))
5638                then
5639                   return True;
5640                end if;
5641
5642                Next_Component_Or_Discriminant (E);
5643             end loop;
5644
5645             return False;
5646          end;
5647
5648       --  Type other than array or record is always OK
5649
5650       else
5651          return False;
5652       end if;
5653    end Type_May_Have_Bit_Aligned_Components;
5654
5655    ----------------------------
5656    -- Wrap_Cleanup_Procedure --
5657    ----------------------------
5658
5659    procedure Wrap_Cleanup_Procedure (N : Node_Id) is
5660       Loc   : constant Source_Ptr := Sloc (N);
5661       Stseq : constant Node_Id    := Handled_Statement_Sequence (N);
5662       Stmts : constant List_Id    := Statements (Stseq);
5663
5664    begin
5665       if Abort_Allowed then
5666          Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
5667          Append_To  (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
5668       end if;
5669    end Wrap_Cleanup_Procedure;
5670
5671 end Exp_Util;