OSDN Git Service

2009-04-29 Javier Miranda <miranda@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_util.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             S E M _ U T I L                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2009, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with Atree;    use Atree;
27 with Casing;   use Casing;
28 with Checks;   use Checks;
29 with Debug;    use Debug;
30 with Errout;   use Errout;
31 with Elists;   use Elists;
32 with Exp_Disp; use Exp_Disp;
33 with Exp_Tss;  use Exp_Tss;
34 with Exp_Util; use Exp_Util;
35 with Fname;    use Fname;
36 with Freeze;   use Freeze;
37 with Lib;      use Lib;
38 with Lib.Xref; use Lib.Xref;
39 with Nlists;   use Nlists;
40 with Output;   use Output;
41 with Opt;      use Opt;
42 with Rtsfind;  use Rtsfind;
43 with Scans;    use Scans;
44 with Scn;      use Scn;
45 with Sem;      use Sem;
46 with Sem_Aux;  use Sem_Aux;
47 with Sem_Attr; use Sem_Attr;
48 with Sem_Ch8;  use Sem_Ch8;
49 with Sem_Eval; use Sem_Eval;
50 with Sem_Res;  use Sem_Res;
51 with Sem_Type; use Sem_Type;
52 with Sinfo;    use Sinfo;
53 with Sinput;   use Sinput;
54 with Stand;    use Stand;
55 with Style;
56 with Stringt;  use Stringt;
57 with Targparm; use Targparm;
58 with Tbuild;   use Tbuild;
59 with Ttypes;   use Ttypes;
60 with Uname;    use Uname;
61
62 with GNAT.HTable; use GNAT.HTable;
63 package body Sem_Util is
64
65    ----------------------------------------
66    -- Global_Variables for New_Copy_Tree --
67    ----------------------------------------
68
69    --  These global variables are used by New_Copy_Tree. See description
70    --  of the body of this subprogram for details. Global variables can be
71    --  safely used by New_Copy_Tree, since there is no case of a recursive
72    --  call from the processing inside New_Copy_Tree.
73
74    NCT_Hash_Threshhold : constant := 20;
75    --  If there are more than this number of pairs of entries in the
76    --  map, then Hash_Tables_Used will be set, and the hash tables will
77    --  be initialized and used for the searches.
78
79    NCT_Hash_Tables_Used : Boolean := False;
80    --  Set to True if hash tables are in use
81
82    NCT_Table_Entries : Nat;
83    --  Count entries in table to see if threshhold is reached
84
85    NCT_Hash_Table_Setup : Boolean := False;
86    --  Set to True if hash table contains data. We set this True if we
87    --  setup the hash table with data, and leave it set permanently
88    --  from then on, this is a signal that second and subsequent users
89    --  of the hash table must clear the old entries before reuse.
90
91    subtype NCT_Header_Num is Int range 0 .. 511;
92    --  Defines range of headers in hash tables (512 headers)
93
94    -----------------------
95    -- Local Subprograms --
96    -----------------------
97
98    function Build_Component_Subtype
99      (C   : List_Id;
100       Loc : Source_Ptr;
101       T   : Entity_Id) return Node_Id;
102    --  This function builds the subtype for Build_Actual_Subtype_Of_Component
103    --  and Build_Discriminal_Subtype_Of_Component. C is a list of constraints,
104    --  Loc is the source location, T is the original subtype.
105
106    function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean;
107    --  Subsidiary to Is_Fully_Initialized_Type. For an unconstrained type
108    --  with discriminants whose default values are static, examine only the
109    --  components in the selected variant to determine whether all of them
110    --  have a default.
111
112    function Has_Null_Extension (T : Entity_Id) return Boolean;
113    --  T is a derived tagged type. Check whether the type extension is null.
114    --  If the parent type is fully initialized, T can be treated as such.
115
116    ------------------------------
117    --  Abstract_Interface_List --
118    ------------------------------
119
120    function Abstract_Interface_List (Typ : Entity_Id) return List_Id is
121       Nod : Node_Id;
122
123    begin
124       if Is_Concurrent_Type (Typ) then
125
126          --  If we are dealing with a synchronized subtype, go to the base
127          --  type, whose declaration has the interface list.
128
129          --  Shouldn't this be Declaration_Node???
130
131          Nod := Parent (Base_Type (Typ));
132
133          if Nkind (Nod) = N_Full_Type_Declaration then
134             return Empty_List;
135          end if;
136
137       elsif Ekind (Typ) = E_Record_Type_With_Private then
138          if Nkind (Parent (Typ)) = N_Full_Type_Declaration then
139             Nod := Type_Definition (Parent (Typ));
140
141          elsif Nkind (Parent (Typ)) = N_Private_Type_Declaration then
142             if Present (Full_View (Typ)) then
143                Nod := Type_Definition (Parent (Full_View (Typ)));
144
145             --  If the full-view is not available we cannot do anything else
146             --  here (the source has errors).
147
148             else
149                return Empty_List;
150             end if;
151
152          --  Support for generic formals with interfaces is still missing ???
153
154          elsif Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
155             return Empty_List;
156
157          else
158             pragma Assert
159               (Nkind (Parent (Typ)) = N_Private_Extension_Declaration);
160             Nod := Parent (Typ);
161          end if;
162
163       elsif Ekind (Typ) = E_Record_Subtype then
164          Nod := Type_Definition (Parent (Etype (Typ)));
165
166       elsif Ekind (Typ) = E_Record_Subtype_With_Private then
167
168          --  Recurse, because parent may still be a private extension. Also
169          --  note that the full view of the subtype or the full view of its
170          --  base type may (both) be unavailable.
171
172          return Abstract_Interface_List (Etype (Typ));
173
174       else pragma Assert ((Ekind (Typ)) = E_Record_Type);
175          if Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
176             Nod := Formal_Type_Definition (Parent (Typ));
177          else
178             Nod := Type_Definition (Parent (Typ));
179          end if;
180       end if;
181
182       return Interface_List (Nod);
183    end Abstract_Interface_List;
184
185    --------------------------------
186    -- Add_Access_Type_To_Process --
187    --------------------------------
188
189    procedure Add_Access_Type_To_Process (E : Entity_Id; A : Entity_Id) is
190       L : Elist_Id;
191
192    begin
193       Ensure_Freeze_Node (E);
194       L := Access_Types_To_Process (Freeze_Node (E));
195
196       if No (L) then
197          L := New_Elmt_List;
198          Set_Access_Types_To_Process (Freeze_Node (E), L);
199       end if;
200
201       Append_Elmt (A, L);
202    end Add_Access_Type_To_Process;
203
204    ----------------------------
205    -- Add_Global_Declaration --
206    ----------------------------
207
208    procedure Add_Global_Declaration (N : Node_Id) is
209       Aux_Node : constant Node_Id := Aux_Decls_Node (Cunit (Current_Sem_Unit));
210
211    begin
212       if No (Declarations (Aux_Node)) then
213          Set_Declarations (Aux_Node, New_List);
214       end if;
215
216       Append_To (Declarations (Aux_Node), N);
217       Analyze (N);
218    end Add_Global_Declaration;
219
220    -----------------------
221    -- Alignment_In_Bits --
222    -----------------------
223
224    function Alignment_In_Bits (E : Entity_Id) return Uint is
225    begin
226       return Alignment (E) * System_Storage_Unit;
227    end Alignment_In_Bits;
228
229    -----------------------------------------
230    -- Apply_Compile_Time_Constraint_Error --
231    -----------------------------------------
232
233    procedure Apply_Compile_Time_Constraint_Error
234      (N      : Node_Id;
235       Msg    : String;
236       Reason : RT_Exception_Code;
237       Ent    : Entity_Id  := Empty;
238       Typ    : Entity_Id  := Empty;
239       Loc    : Source_Ptr := No_Location;
240       Rep    : Boolean    := True;
241       Warn   : Boolean    := False)
242    is
243       Stat   : constant Boolean := Is_Static_Expression (N);
244       R_Stat : constant Node_Id :=
245                  Make_Raise_Constraint_Error (Sloc (N), Reason => Reason);
246       Rtyp   : Entity_Id;
247
248    begin
249       if No (Typ) then
250          Rtyp := Etype (N);
251       else
252          Rtyp := Typ;
253       end if;
254
255       Discard_Node
256         (Compile_Time_Constraint_Error (N, Msg, Ent, Loc, Warn => Warn));
257
258       if not Rep then
259          return;
260       end if;
261
262       --  Now we replace the node by an N_Raise_Constraint_Error node
263       --  This does not need reanalyzing, so set it as analyzed now.
264
265       Rewrite (N, R_Stat);
266       Set_Analyzed (N, True);
267
268       Set_Etype (N, Rtyp);
269       Set_Raises_Constraint_Error (N);
270
271       --  If the original expression was marked as static, the result is
272       --  still marked as static, but the Raises_Constraint_Error flag is
273       --  always set so that further static evaluation is not attempted.
274
275       if Stat then
276          Set_Is_Static_Expression (N);
277       end if;
278    end Apply_Compile_Time_Constraint_Error;
279
280    --------------------------
281    -- Build_Actual_Subtype --
282    --------------------------
283
284    function Build_Actual_Subtype
285      (T : Entity_Id;
286       N : Node_Or_Entity_Id) return Node_Id
287    is
288       Loc : Source_Ptr;
289       --  Normally Sloc (N), but may point to corresponding body in some cases
290
291       Constraints : List_Id;
292       Decl        : Node_Id;
293       Discr       : Entity_Id;
294       Hi          : Node_Id;
295       Lo          : Node_Id;
296       Subt        : Entity_Id;
297       Disc_Type   : Entity_Id;
298       Obj         : Node_Id;
299
300    begin
301       Loc := Sloc (N);
302
303       if Nkind (N) = N_Defining_Identifier then
304          Obj := New_Reference_To (N, Loc);
305
306          --  If this is a formal parameter of a subprogram declaration, and
307          --  we are compiling the body, we want the declaration for the
308          --  actual subtype to carry the source position of the body, to
309          --  prevent anomalies in gdb when stepping through the code.
310
311          if Is_Formal (N) then
312             declare
313                Decl : constant Node_Id := Unit_Declaration_Node (Scope (N));
314             begin
315                if Nkind (Decl) = N_Subprogram_Declaration
316                  and then Present (Corresponding_Body (Decl))
317                then
318                   Loc := Sloc (Corresponding_Body (Decl));
319                end if;
320             end;
321          end if;
322
323       else
324          Obj := N;
325       end if;
326
327       if Is_Array_Type (T) then
328          Constraints := New_List;
329          for J in 1 .. Number_Dimensions (T) loop
330
331             --  Build an array subtype declaration with the nominal subtype and
332             --  the bounds of the actual. Add the declaration in front of the
333             --  local declarations for the subprogram, for analysis before any
334             --  reference to the formal in the body.
335
336             Lo :=
337               Make_Attribute_Reference (Loc,
338                 Prefix         =>
339                   Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
340                 Attribute_Name => Name_First,
341                 Expressions    => New_List (
342                   Make_Integer_Literal (Loc, J)));
343
344             Hi :=
345               Make_Attribute_Reference (Loc,
346                 Prefix         =>
347                   Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
348                 Attribute_Name => Name_Last,
349                 Expressions    => New_List (
350                   Make_Integer_Literal (Loc, J)));
351
352             Append (Make_Range (Loc, Lo, Hi), Constraints);
353          end loop;
354
355       --  If the type has unknown discriminants there is no constrained
356       --  subtype to build. This is never called for a formal or for a
357       --  lhs, so returning the type is ok ???
358
359       elsif Has_Unknown_Discriminants (T) then
360          return T;
361
362       else
363          Constraints := New_List;
364
365          --  Type T is a generic derived type, inherit the discriminants from
366          --  the parent type.
367
368          if Is_Private_Type (T)
369            and then No (Full_View (T))
370
371             --  T was flagged as an error if it was declared as a formal
372             --  derived type with known discriminants. In this case there
373             --  is no need to look at the parent type since T already carries
374             --  its own discriminants.
375
376            and then not Error_Posted (T)
377          then
378             Disc_Type := Etype (Base_Type (T));
379          else
380             Disc_Type := T;
381          end if;
382
383          Discr := First_Discriminant (Disc_Type);
384          while Present (Discr) loop
385             Append_To (Constraints,
386               Make_Selected_Component (Loc,
387                 Prefix =>
388                   Duplicate_Subexpr_No_Checks (Obj),
389                 Selector_Name => New_Occurrence_Of (Discr, Loc)));
390             Next_Discriminant (Discr);
391          end loop;
392       end if;
393
394       Subt :=
395         Make_Defining_Identifier (Loc,
396           Chars => New_Internal_Name ('S'));
397       Set_Is_Internal (Subt);
398
399       Decl :=
400         Make_Subtype_Declaration (Loc,
401           Defining_Identifier => Subt,
402           Subtype_Indication =>
403             Make_Subtype_Indication (Loc,
404               Subtype_Mark => New_Reference_To (T,  Loc),
405               Constraint  =>
406                 Make_Index_Or_Discriminant_Constraint (Loc,
407                   Constraints => Constraints)));
408
409       Mark_Rewrite_Insertion (Decl);
410       return Decl;
411    end Build_Actual_Subtype;
412
413    ---------------------------------------
414    -- Build_Actual_Subtype_Of_Component --
415    ---------------------------------------
416
417    function Build_Actual_Subtype_Of_Component
418      (T : Entity_Id;
419       N : Node_Id) return Node_Id
420    is
421       Loc       : constant Source_Ptr := Sloc (N);
422       P         : constant Node_Id    := Prefix (N);
423       D         : Elmt_Id;
424       Id        : Node_Id;
425       Indx_Type : Entity_Id;
426
427       Deaccessed_T : Entity_Id;
428       --  This is either a copy of T, or if T is an access type, then it is
429       --  the directly designated type of this access type.
430
431       function Build_Actual_Array_Constraint return List_Id;
432       --  If one or more of the bounds of the component depends on
433       --  discriminants, build  actual constraint using the discriminants
434       --  of the prefix.
435
436       function Build_Actual_Record_Constraint return List_Id;
437       --  Similar to previous one, for discriminated components constrained
438       --  by the discriminant of the enclosing object.
439
440       -----------------------------------
441       -- Build_Actual_Array_Constraint --
442       -----------------------------------
443
444       function Build_Actual_Array_Constraint return List_Id is
445          Constraints : constant List_Id := New_List;
446          Indx        : Node_Id;
447          Hi          : Node_Id;
448          Lo          : Node_Id;
449          Old_Hi      : Node_Id;
450          Old_Lo      : Node_Id;
451
452       begin
453          Indx := First_Index (Deaccessed_T);
454          while Present (Indx) loop
455             Old_Lo := Type_Low_Bound  (Etype (Indx));
456             Old_Hi := Type_High_Bound (Etype (Indx));
457
458             if Denotes_Discriminant (Old_Lo) then
459                Lo :=
460                  Make_Selected_Component (Loc,
461                    Prefix => New_Copy_Tree (P),
462                    Selector_Name => New_Occurrence_Of (Entity (Old_Lo), Loc));
463
464             else
465                Lo := New_Copy_Tree (Old_Lo);
466
467                --  The new bound will be reanalyzed in the enclosing
468                --  declaration. For literal bounds that come from a type
469                --  declaration, the type of the context must be imposed, so
470                --  insure that analysis will take place. For non-universal
471                --  types this is not strictly necessary.
472
473                Set_Analyzed (Lo, False);
474             end if;
475
476             if Denotes_Discriminant (Old_Hi) then
477                Hi :=
478                  Make_Selected_Component (Loc,
479                    Prefix => New_Copy_Tree (P),
480                    Selector_Name => New_Occurrence_Of (Entity (Old_Hi), Loc));
481
482             else
483                Hi := New_Copy_Tree (Old_Hi);
484                Set_Analyzed (Hi, False);
485             end if;
486
487             Append (Make_Range (Loc, Lo, Hi), Constraints);
488             Next_Index (Indx);
489          end loop;
490
491          return Constraints;
492       end Build_Actual_Array_Constraint;
493
494       ------------------------------------
495       -- Build_Actual_Record_Constraint --
496       ------------------------------------
497
498       function Build_Actual_Record_Constraint return List_Id is
499          Constraints : constant List_Id := New_List;
500          D           : Elmt_Id;
501          D_Val       : Node_Id;
502
503       begin
504          D := First_Elmt (Discriminant_Constraint (Deaccessed_T));
505          while Present (D) loop
506             if Denotes_Discriminant (Node (D)) then
507                D_Val :=  Make_Selected_Component (Loc,
508                  Prefix => New_Copy_Tree (P),
509                 Selector_Name => New_Occurrence_Of (Entity (Node (D)), Loc));
510
511             else
512                D_Val := New_Copy_Tree (Node (D));
513             end if;
514
515             Append (D_Val, Constraints);
516             Next_Elmt (D);
517          end loop;
518
519          return Constraints;
520       end Build_Actual_Record_Constraint;
521
522    --  Start of processing for Build_Actual_Subtype_Of_Component
523
524    begin
525       --  Why the test for Spec_Expression mode here???
526
527       if In_Spec_Expression then
528          return Empty;
529
530       --  More comments for the rest of this body would be good ???
531
532       elsif Nkind (N) = N_Explicit_Dereference then
533          if Is_Composite_Type (T)
534            and then not Is_Constrained (T)
535            and then not (Is_Class_Wide_Type (T)
536                           and then Is_Constrained (Root_Type (T)))
537            and then not Has_Unknown_Discriminants (T)
538          then
539             --  If the type of the dereference is already constrained, it
540             --  is an actual subtype.
541
542             if Is_Array_Type (Etype (N))
543               and then Is_Constrained (Etype (N))
544             then
545                return Empty;
546             else
547                Remove_Side_Effects (P);
548                return Build_Actual_Subtype (T, N);
549             end if;
550          else
551             return Empty;
552          end if;
553       end if;
554
555       if Ekind (T) = E_Access_Subtype then
556          Deaccessed_T := Designated_Type (T);
557       else
558          Deaccessed_T := T;
559       end if;
560
561       if Ekind (Deaccessed_T) = E_Array_Subtype then
562          Id := First_Index (Deaccessed_T);
563          while Present (Id) loop
564             Indx_Type := Underlying_Type (Etype (Id));
565
566             if Denotes_Discriminant (Type_Low_Bound  (Indx_Type))
567                  or else
568                Denotes_Discriminant (Type_High_Bound (Indx_Type))
569             then
570                Remove_Side_Effects (P);
571                return
572                  Build_Component_Subtype
573                    (Build_Actual_Array_Constraint, Loc, Base_Type (T));
574             end if;
575
576             Next_Index (Id);
577          end loop;
578
579       elsif Is_Composite_Type (Deaccessed_T)
580         and then Has_Discriminants (Deaccessed_T)
581         and then not Has_Unknown_Discriminants (Deaccessed_T)
582       then
583          D := First_Elmt (Discriminant_Constraint (Deaccessed_T));
584          while Present (D) loop
585             if Denotes_Discriminant (Node (D)) then
586                Remove_Side_Effects (P);
587                return
588                  Build_Component_Subtype (
589                    Build_Actual_Record_Constraint, Loc, Base_Type (T));
590             end if;
591
592             Next_Elmt (D);
593          end loop;
594       end if;
595
596       --  If none of the above, the actual and nominal subtypes are the same
597
598       return Empty;
599    end Build_Actual_Subtype_Of_Component;
600
601    -----------------------------
602    -- Build_Component_Subtype --
603    -----------------------------
604
605    function Build_Component_Subtype
606      (C   : List_Id;
607       Loc : Source_Ptr;
608       T   : Entity_Id) return Node_Id
609    is
610       Subt : Entity_Id;
611       Decl : Node_Id;
612
613    begin
614       --  Unchecked_Union components do not require component subtypes
615
616       if Is_Unchecked_Union (T) then
617          return Empty;
618       end if;
619
620       Subt :=
621         Make_Defining_Identifier (Loc,
622           Chars => New_Internal_Name ('S'));
623       Set_Is_Internal (Subt);
624
625       Decl :=
626         Make_Subtype_Declaration (Loc,
627           Defining_Identifier => Subt,
628           Subtype_Indication =>
629             Make_Subtype_Indication (Loc,
630               Subtype_Mark => New_Reference_To (Base_Type (T),  Loc),
631               Constraint  =>
632                 Make_Index_Or_Discriminant_Constraint (Loc,
633                   Constraints => C)));
634
635       Mark_Rewrite_Insertion (Decl);
636       return Decl;
637    end Build_Component_Subtype;
638
639    ---------------------------
640    -- Build_Default_Subtype --
641    ---------------------------
642
643    function Build_Default_Subtype
644      (T : Entity_Id;
645       N : Node_Id) return Entity_Id
646    is
647       Loc  : constant Source_Ptr := Sloc (N);
648       Disc : Entity_Id;
649
650    begin
651       if not Has_Discriminants (T) or else Is_Constrained (T) then
652          return T;
653       end if;
654
655       Disc := First_Discriminant (T);
656
657       if No (Discriminant_Default_Value (Disc)) then
658          return T;
659       end if;
660
661       declare
662          Act : constant Entity_Id :=
663                  Make_Defining_Identifier (Loc,
664                    Chars => New_Internal_Name ('S'));
665
666          Constraints : constant List_Id := New_List;
667          Decl        : Node_Id;
668
669       begin
670          while Present (Disc) loop
671             Append_To (Constraints,
672               New_Copy_Tree (Discriminant_Default_Value (Disc)));
673             Next_Discriminant (Disc);
674          end loop;
675
676          Decl :=
677            Make_Subtype_Declaration (Loc,
678              Defining_Identifier => Act,
679              Subtype_Indication =>
680                Make_Subtype_Indication (Loc,
681                  Subtype_Mark => New_Occurrence_Of (T, Loc),
682                  Constraint =>
683                    Make_Index_Or_Discriminant_Constraint (Loc,
684                      Constraints => Constraints)));
685
686          Insert_Action (N, Decl);
687          Analyze (Decl);
688          return Act;
689       end;
690    end Build_Default_Subtype;
691
692    --------------------------------------------
693    -- Build_Discriminal_Subtype_Of_Component --
694    --------------------------------------------
695
696    function Build_Discriminal_Subtype_Of_Component
697      (T : Entity_Id) return Node_Id
698    is
699       Loc : constant Source_Ptr := Sloc (T);
700       D   : Elmt_Id;
701       Id  : Node_Id;
702
703       function Build_Discriminal_Array_Constraint return List_Id;
704       --  If one or more of the bounds of the component depends on
705       --  discriminants, build  actual constraint using the discriminants
706       --  of the prefix.
707
708       function Build_Discriminal_Record_Constraint return List_Id;
709       --  Similar to previous one, for discriminated components constrained
710       --  by the discriminant of the enclosing object.
711
712       ----------------------------------------
713       -- Build_Discriminal_Array_Constraint --
714       ----------------------------------------
715
716       function Build_Discriminal_Array_Constraint return List_Id is
717          Constraints : constant List_Id := New_List;
718          Indx        : Node_Id;
719          Hi          : Node_Id;
720          Lo          : Node_Id;
721          Old_Hi      : Node_Id;
722          Old_Lo      : Node_Id;
723
724       begin
725          Indx := First_Index (T);
726          while Present (Indx) loop
727             Old_Lo := Type_Low_Bound  (Etype (Indx));
728             Old_Hi := Type_High_Bound (Etype (Indx));
729
730             if Denotes_Discriminant (Old_Lo) then
731                Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
732
733             else
734                Lo := New_Copy_Tree (Old_Lo);
735             end if;
736
737             if Denotes_Discriminant (Old_Hi) then
738                Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
739
740             else
741                Hi := New_Copy_Tree (Old_Hi);
742             end if;
743
744             Append (Make_Range (Loc, Lo, Hi), Constraints);
745             Next_Index (Indx);
746          end loop;
747
748          return Constraints;
749       end Build_Discriminal_Array_Constraint;
750
751       -----------------------------------------
752       -- Build_Discriminal_Record_Constraint --
753       -----------------------------------------
754
755       function Build_Discriminal_Record_Constraint return List_Id is
756          Constraints : constant List_Id := New_List;
757          D           : Elmt_Id;
758          D_Val       : Node_Id;
759
760       begin
761          D := First_Elmt (Discriminant_Constraint (T));
762          while Present (D) loop
763             if Denotes_Discriminant (Node (D)) then
764                D_Val :=
765                  New_Occurrence_Of (Discriminal (Entity (Node (D))), Loc);
766
767             else
768                D_Val := New_Copy_Tree (Node (D));
769             end if;
770
771             Append (D_Val, Constraints);
772             Next_Elmt (D);
773          end loop;
774
775          return Constraints;
776       end Build_Discriminal_Record_Constraint;
777
778    --  Start of processing for Build_Discriminal_Subtype_Of_Component
779
780    begin
781       if Ekind (T) = E_Array_Subtype then
782          Id := First_Index (T);
783          while Present (Id) loop
784             if Denotes_Discriminant (Type_Low_Bound  (Etype (Id))) or else
785                Denotes_Discriminant (Type_High_Bound (Etype (Id)))
786             then
787                return Build_Component_Subtype
788                  (Build_Discriminal_Array_Constraint, Loc, T);
789             end if;
790
791             Next_Index (Id);
792          end loop;
793
794       elsif Ekind (T) = E_Record_Subtype
795         and then Has_Discriminants (T)
796         and then not Has_Unknown_Discriminants (T)
797       then
798          D := First_Elmt (Discriminant_Constraint (T));
799          while Present (D) loop
800             if Denotes_Discriminant (Node (D)) then
801                return Build_Component_Subtype
802                  (Build_Discriminal_Record_Constraint, Loc, T);
803             end if;
804
805             Next_Elmt (D);
806          end loop;
807       end if;
808
809       --  If none of the above, the actual and nominal subtypes are the same
810
811       return Empty;
812    end Build_Discriminal_Subtype_Of_Component;
813
814    ------------------------------
815    -- Build_Elaboration_Entity --
816    ------------------------------
817
818    procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
819       Loc      : constant Source_Ptr := Sloc (N);
820       Decl     : Node_Id;
821       Elab_Ent : Entity_Id;
822
823       procedure Set_Package_Name (Ent : Entity_Id);
824       --  Given an entity, sets the fully qualified name of the entity in
825       --  Name_Buffer, with components separated by double underscores. This
826       --  is a recursive routine that climbs the scope chain to Standard.
827
828       ----------------------
829       -- Set_Package_Name --
830       ----------------------
831
832       procedure Set_Package_Name (Ent : Entity_Id) is
833       begin
834          if Scope (Ent) /= Standard_Standard then
835             Set_Package_Name (Scope (Ent));
836
837             declare
838                Nam : constant String := Get_Name_String (Chars (Ent));
839             begin
840                Name_Buffer (Name_Len + 1) := '_';
841                Name_Buffer (Name_Len + 2) := '_';
842                Name_Buffer (Name_Len + 3 .. Name_Len + Nam'Length + 2) := Nam;
843                Name_Len := Name_Len + Nam'Length + 2;
844             end;
845
846          else
847             Get_Name_String (Chars (Ent));
848          end if;
849       end Set_Package_Name;
850
851    --  Start of processing for Build_Elaboration_Entity
852
853    begin
854       --  Ignore if already constructed
855
856       if Present (Elaboration_Entity (Spec_Id)) then
857          return;
858       end if;
859
860       --  Construct name of elaboration entity as xxx_E, where xxx is the unit
861       --  name with dots replaced by double underscore. We have to manually
862       --  construct this name, since it will be elaborated in the outer scope,
863       --  and thus will not have the unit name automatically prepended.
864
865       Set_Package_Name (Spec_Id);
866
867       --  Append _E
868
869       Name_Buffer (Name_Len + 1) := '_';
870       Name_Buffer (Name_Len + 2) := 'E';
871       Name_Len := Name_Len + 2;
872
873       --  Create elaboration flag
874
875       Elab_Ent :=
876         Make_Defining_Identifier (Loc, Chars => Name_Find);
877       Set_Elaboration_Entity (Spec_Id, Elab_Ent);
878
879       Decl :=
880          Make_Object_Declaration (Loc,
881            Defining_Identifier => Elab_Ent,
882            Object_Definition   =>
883              New_Occurrence_Of (Standard_Boolean, Loc),
884            Expression          =>
885              New_Occurrence_Of (Standard_False, Loc));
886
887       Push_Scope (Standard_Standard);
888       Add_Global_Declaration (Decl);
889       Pop_Scope;
890
891       --  Reset True_Constant indication, since we will indeed assign a value
892       --  to the variable in the binder main. We also kill the Current_Value
893       --  and Last_Assignment fields for the same reason.
894
895       Set_Is_True_Constant (Elab_Ent, False);
896       Set_Current_Value    (Elab_Ent, Empty);
897       Set_Last_Assignment  (Elab_Ent, Empty);
898
899       --  We do not want any further qualification of the name (if we did
900       --  not do this, we would pick up the name of the generic package
901       --  in the case of a library level generic instantiation).
902
903       Set_Has_Qualified_Name       (Elab_Ent);
904       Set_Has_Fully_Qualified_Name (Elab_Ent);
905    end Build_Elaboration_Entity;
906
907    -----------------------------------
908    -- Cannot_Raise_Constraint_Error --
909    -----------------------------------
910
911    function Cannot_Raise_Constraint_Error (Expr : Node_Id) return Boolean is
912    begin
913       if Compile_Time_Known_Value (Expr) then
914          return True;
915
916       elsif Do_Range_Check (Expr) then
917          return False;
918
919       elsif Raises_Constraint_Error (Expr) then
920          return False;
921
922       else
923          case Nkind (Expr) is
924             when N_Identifier =>
925                return True;
926
927             when N_Expanded_Name =>
928                return True;
929
930             when N_Selected_Component =>
931                return not Do_Discriminant_Check (Expr);
932
933             when N_Attribute_Reference =>
934                if Do_Overflow_Check (Expr) then
935                   return False;
936
937                elsif No (Expressions (Expr)) then
938                   return True;
939
940                else
941                   declare
942                      N : Node_Id;
943
944                   begin
945                      N := First (Expressions (Expr));
946                      while Present (N) loop
947                         if Cannot_Raise_Constraint_Error (N) then
948                            Next (N);
949                         else
950                            return False;
951                         end if;
952                      end loop;
953
954                      return True;
955                   end;
956                end if;
957
958             when N_Type_Conversion =>
959                if Do_Overflow_Check (Expr)
960                  or else Do_Length_Check (Expr)
961                  or else Do_Tag_Check (Expr)
962                then
963                   return False;
964                else
965                   return
966                     Cannot_Raise_Constraint_Error (Expression (Expr));
967                end if;
968
969             when N_Unchecked_Type_Conversion =>
970                return Cannot_Raise_Constraint_Error (Expression (Expr));
971
972             when N_Unary_Op =>
973                if Do_Overflow_Check (Expr) then
974                   return False;
975                else
976                   return
977                     Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
978                end if;
979
980             when N_Op_Divide |
981                  N_Op_Mod    |
982                  N_Op_Rem
983             =>
984                if Do_Division_Check (Expr)
985                  or else Do_Overflow_Check (Expr)
986                then
987                   return False;
988                else
989                   return
990                     Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
991                       and then
992                     Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
993                end if;
994
995             when N_Op_Add                    |
996                  N_Op_And                    |
997                  N_Op_Concat                 |
998                  N_Op_Eq                     |
999                  N_Op_Expon                  |
1000                  N_Op_Ge                     |
1001                  N_Op_Gt                     |
1002                  N_Op_Le                     |
1003                  N_Op_Lt                     |
1004                  N_Op_Multiply               |
1005                  N_Op_Ne                     |
1006                  N_Op_Or                     |
1007                  N_Op_Rotate_Left            |
1008                  N_Op_Rotate_Right           |
1009                  N_Op_Shift_Left             |
1010                  N_Op_Shift_Right            |
1011                  N_Op_Shift_Right_Arithmetic |
1012                  N_Op_Subtract               |
1013                  N_Op_Xor
1014             =>
1015                if Do_Overflow_Check (Expr) then
1016                   return False;
1017                else
1018                   return
1019                     Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1020                       and then
1021                     Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1022                end if;
1023
1024             when others =>
1025                return False;
1026          end case;
1027       end if;
1028    end Cannot_Raise_Constraint_Error;
1029
1030    --------------------------
1031    -- Check_Fully_Declared --
1032    --------------------------
1033
1034    procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
1035    begin
1036       if Ekind (T) = E_Incomplete_Type then
1037
1038          --  Ada 2005 (AI-50217): If the type is available through a limited
1039          --  with_clause, verify that its full view has been analyzed.
1040
1041          if From_With_Type (T)
1042            and then Present (Non_Limited_View (T))
1043            and then Ekind (Non_Limited_View (T)) /= E_Incomplete_Type
1044          then
1045             --  The non-limited view is fully declared
1046             null;
1047
1048          else
1049             Error_Msg_NE
1050               ("premature usage of incomplete}", N, First_Subtype (T));
1051          end if;
1052
1053       --  Need comments for these tests ???
1054
1055       elsif Has_Private_Component (T)
1056         and then not Is_Generic_Type (Root_Type (T))
1057         and then not In_Spec_Expression
1058       then
1059          --  Special case: if T is the anonymous type created for a single
1060          --  task or protected object, use the name of the source object.
1061
1062          if Is_Concurrent_Type (T)
1063            and then not Comes_From_Source (T)
1064            and then Nkind (N) = N_Object_Declaration
1065          then
1066             Error_Msg_NE ("type of& has incomplete component", N,
1067               Defining_Identifier (N));
1068
1069          else
1070             Error_Msg_NE
1071               ("premature usage of incomplete}", N, First_Subtype (T));
1072          end if;
1073       end if;
1074    end Check_Fully_Declared;
1075
1076    -------------------------
1077    -- Check_Nested_Access --
1078    -------------------------
1079
1080    procedure Check_Nested_Access (Ent : Entity_Id) is
1081       Scop         : constant Entity_Id := Current_Scope;
1082       Current_Subp : Entity_Id;
1083       Enclosing    : Entity_Id;
1084
1085    begin
1086       --  Currently only enabled for VM back-ends for efficiency, should we
1087       --  enable it more systematically ???
1088
1089       --  Check for Is_Imported needs commenting below ???
1090
1091       if VM_Target /= No_VM
1092         and then (Ekind (Ent) = E_Variable
1093                     or else
1094                   Ekind (Ent) = E_Constant
1095                     or else
1096                   Ekind (Ent) = E_Loop_Parameter)
1097         and then Scope (Ent) /= Empty
1098         and then not Is_Library_Level_Entity (Ent)
1099         and then not Is_Imported (Ent)
1100       then
1101          if Is_Subprogram (Scop)
1102            or else Is_Generic_Subprogram (Scop)
1103            or else Is_Entry (Scop)
1104          then
1105             Current_Subp := Scop;
1106          else
1107             Current_Subp := Current_Subprogram;
1108          end if;
1109
1110          Enclosing := Enclosing_Subprogram (Ent);
1111
1112          if Enclosing /= Empty
1113            and then Enclosing /= Current_Subp
1114          then
1115             Set_Has_Up_Level_Access (Ent, True);
1116          end if;
1117       end if;
1118    end Check_Nested_Access;
1119
1120    ------------------------------------------
1121    -- Check_Potentially_Blocking_Operation --
1122    ------------------------------------------
1123
1124    procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
1125       S : Entity_Id;
1126    begin
1127       --  N is one of the potentially blocking operations listed in 9.5.1(8).
1128       --  When pragma Detect_Blocking is active, the run time will raise
1129       --  Program_Error. Here we only issue a warning, since we generally
1130       --  support the use of potentially blocking operations in the absence
1131       --  of the pragma.
1132
1133       --  Indirect blocking through a subprogram call cannot be diagnosed
1134       --  statically without interprocedural analysis, so we do not attempt
1135       --  to do it here.
1136
1137       S := Scope (Current_Scope);
1138       while Present (S) and then S /= Standard_Standard loop
1139          if Is_Protected_Type (S) then
1140             Error_Msg_N
1141               ("potentially blocking operation in protected operation?", N);
1142
1143             return;
1144          end if;
1145
1146          S := Scope (S);
1147       end loop;
1148    end Check_Potentially_Blocking_Operation;
1149
1150    ------------------------------
1151    -- Check_Unprotected_Access --
1152    ------------------------------
1153
1154    procedure Check_Unprotected_Access
1155      (Context : Node_Id;
1156       Expr    : Node_Id)
1157    is
1158       Cont_Encl_Typ : Entity_Id;
1159       Pref_Encl_Typ : Entity_Id;
1160
1161       function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id;
1162       --  Check whether Obj is a private component of a protected object.
1163       --  Return the protected type where the component resides, Empty
1164       --  otherwise.
1165
1166       function Is_Public_Operation return Boolean;
1167       --  Verify that the enclosing operation is callable from outside the
1168       --  protected object, to minimize false positives.
1169
1170       ------------------------------
1171       -- Enclosing_Protected_Type --
1172       ------------------------------
1173
1174       function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id is
1175       begin
1176          if Is_Entity_Name (Obj) then
1177             declare
1178                Ent : Entity_Id := Entity (Obj);
1179
1180             begin
1181                --  The object can be a renaming of a private component, use
1182                --  the original record component.
1183
1184                if Is_Prival (Ent) then
1185                   Ent := Prival_Link (Ent);
1186                end if;
1187
1188                if Is_Protected_Type (Scope (Ent)) then
1189                   return Scope (Ent);
1190                end if;
1191             end;
1192          end if;
1193
1194          --  For indexed and selected components, recursively check the prefix
1195
1196          if Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
1197             return Enclosing_Protected_Type (Prefix (Obj));
1198
1199          --  The object does not denote a protected component
1200
1201          else
1202             return Empty;
1203          end if;
1204       end Enclosing_Protected_Type;
1205
1206       -------------------------
1207       -- Is_Public_Operation --
1208       -------------------------
1209
1210       function Is_Public_Operation return Boolean is
1211          S : Entity_Id;
1212          E : Entity_Id;
1213
1214       begin
1215          S := Current_Scope;
1216          while Present (S)
1217            and then S /= Pref_Encl_Typ
1218          loop
1219             if Scope (S) = Pref_Encl_Typ then
1220                E := First_Entity (Pref_Encl_Typ);
1221                while Present (E)
1222                  and then E /= First_Private_Entity (Pref_Encl_Typ)
1223                loop
1224                   if E = S then
1225                      return True;
1226                   end if;
1227                   Next_Entity (E);
1228                end loop;
1229             end if;
1230
1231             S := Scope (S);
1232          end loop;
1233
1234          return False;
1235       end Is_Public_Operation;
1236
1237    --  Start of processing for Check_Unprotected_Access
1238
1239    begin
1240       if Nkind (Expr) = N_Attribute_Reference
1241         and then Attribute_Name (Expr) = Name_Unchecked_Access
1242       then
1243          Cont_Encl_Typ := Enclosing_Protected_Type (Context);
1244          Pref_Encl_Typ := Enclosing_Protected_Type (Prefix (Expr));
1245
1246          --  Check whether we are trying to export a protected component to a
1247          --  context with an equal or lower access level.
1248
1249          if Present (Pref_Encl_Typ)
1250            and then No (Cont_Encl_Typ)
1251            and then Is_Public_Operation
1252            and then Scope_Depth (Pref_Encl_Typ) >=
1253                       Object_Access_Level (Context)
1254          then
1255             Error_Msg_N
1256               ("?possible unprotected access to protected data", Expr);
1257          end if;
1258       end if;
1259    end Check_Unprotected_Access;
1260
1261    ---------------
1262    -- Check_VMS --
1263    ---------------
1264
1265    procedure Check_VMS (Construct : Node_Id) is
1266    begin
1267       if not OpenVMS_On_Target then
1268          Error_Msg_N
1269            ("this construct is allowed only in Open'V'M'S", Construct);
1270       end if;
1271    end Check_VMS;
1272
1273    ------------------------
1274    -- Collect_Interfaces --
1275    ------------------------
1276
1277    procedure Collect_Interfaces
1278      (T               : Entity_Id;
1279       Ifaces_List     : out Elist_Id;
1280       Exclude_Parents : Boolean := False;
1281       Use_Full_View   : Boolean := True)
1282    is
1283       procedure Collect (Typ : Entity_Id);
1284       --  Subsidiary subprogram used to traverse the whole list
1285       --  of directly and indirectly implemented interfaces
1286
1287       -------------
1288       -- Collect --
1289       -------------
1290
1291       procedure Collect (Typ : Entity_Id) is
1292          Ancestor   : Entity_Id;
1293          Full_T     : Entity_Id;
1294          Id         : Node_Id;
1295          Iface      : Entity_Id;
1296
1297       begin
1298          Full_T := Typ;
1299
1300          --  Handle private types
1301
1302          if Use_Full_View
1303            and then Is_Private_Type (Typ)
1304            and then Present (Full_View (Typ))
1305          then
1306             Full_T := Full_View (Typ);
1307          end if;
1308
1309          --  Include the ancestor if we are generating the whole list of
1310          --  abstract interfaces.
1311
1312          if Etype (Full_T) /= Typ
1313
1314             --  Protect the frontend against wrong sources. For example:
1315
1316             --    package P is
1317             --      type A is tagged null record;
1318             --      type B is new A with private;
1319             --      type C is new A with private;
1320             --    private
1321             --      type B is new C with null record;
1322             --      type C is new B with null record;
1323             --    end P;
1324
1325            and then Etype (Full_T) /= T
1326          then
1327             Ancestor := Etype (Full_T);
1328             Collect (Ancestor);
1329
1330             if Is_Interface (Ancestor)
1331               and then not Exclude_Parents
1332             then
1333                Append_Unique_Elmt (Ancestor, Ifaces_List);
1334             end if;
1335          end if;
1336
1337          --  Traverse the graph of ancestor interfaces
1338
1339          if Is_Non_Empty_List (Abstract_Interface_List (Full_T)) then
1340             Id := First (Abstract_Interface_List (Full_T));
1341             while Present (Id) loop
1342                Iface := Etype (Id);
1343
1344                --  Protect against wrong uses. For example:
1345                --    type I is interface;
1346                --    type O is tagged null record;
1347                --    type Wrong is new I and O with null record; -- ERROR
1348
1349                if Is_Interface (Iface) then
1350                   if Exclude_Parents
1351                     and then Etype (T) /= T
1352                     and then Interface_Present_In_Ancestor (Etype (T), Iface)
1353                   then
1354                      null;
1355                   else
1356                      Collect (Iface);
1357                      Append_Unique_Elmt (Iface, Ifaces_List);
1358                   end if;
1359                end if;
1360
1361                Next (Id);
1362             end loop;
1363          end if;
1364       end Collect;
1365
1366    --  Start of processing for Collect_Interfaces
1367
1368    begin
1369       pragma Assert (Is_Tagged_Type (T) or else Is_Concurrent_Type (T));
1370       Ifaces_List := New_Elmt_List;
1371       Collect (T);
1372    end Collect_Interfaces;
1373
1374    ----------------------------------
1375    -- Collect_Interface_Components --
1376    ----------------------------------
1377
1378    procedure Collect_Interface_Components
1379      (Tagged_Type     : Entity_Id;
1380       Components_List : out Elist_Id)
1381    is
1382       procedure Collect (Typ : Entity_Id);
1383       --  Subsidiary subprogram used to climb to the parents
1384
1385       -------------
1386       -- Collect --
1387       -------------
1388
1389       procedure Collect (Typ : Entity_Id) is
1390          Tag_Comp   : Entity_Id;
1391          Parent_Typ : Entity_Id;
1392
1393       begin
1394          --  Handle private types
1395
1396          if Present (Full_View (Etype (Typ))) then
1397             Parent_Typ := Full_View (Etype (Typ));
1398          else
1399             Parent_Typ := Etype (Typ);
1400          end if;
1401
1402          if Parent_Typ /= Typ
1403
1404             --  Protect the frontend against wrong sources. For example:
1405
1406             --    package P is
1407             --      type A is tagged null record;
1408             --      type B is new A with private;
1409             --      type C is new A with private;
1410             --    private
1411             --      type B is new C with null record;
1412             --      type C is new B with null record;
1413             --    end P;
1414
1415            and then Parent_Typ /= Tagged_Type
1416          then
1417             Collect (Parent_Typ);
1418          end if;
1419
1420          --  Collect the components containing tags of secondary dispatch
1421          --  tables.
1422
1423          Tag_Comp := Next_Tag_Component (First_Tag_Component (Typ));
1424          while Present (Tag_Comp) loop
1425             pragma Assert (Present (Related_Type (Tag_Comp)));
1426             Append_Elmt (Tag_Comp, Components_List);
1427
1428             Tag_Comp := Next_Tag_Component (Tag_Comp);
1429          end loop;
1430       end Collect;
1431
1432    --  Start of processing for Collect_Interface_Components
1433
1434    begin
1435       pragma Assert (Ekind (Tagged_Type) = E_Record_Type
1436         and then Is_Tagged_Type (Tagged_Type));
1437
1438       Components_List := New_Elmt_List;
1439       Collect (Tagged_Type);
1440    end Collect_Interface_Components;
1441
1442    -----------------------------
1443    -- Collect_Interfaces_Info --
1444    -----------------------------
1445
1446    procedure Collect_Interfaces_Info
1447      (T               : Entity_Id;
1448       Ifaces_List     : out Elist_Id;
1449       Components_List : out Elist_Id;
1450       Tags_List       : out Elist_Id)
1451    is
1452       Comps_List : Elist_Id;
1453       Comp_Elmt  : Elmt_Id;
1454       Comp_Iface : Entity_Id;
1455       Iface_Elmt : Elmt_Id;
1456       Iface      : Entity_Id;
1457
1458       function Search_Tag (Iface : Entity_Id) return Entity_Id;
1459       --  Search for the secondary tag associated with the interface type
1460       --  Iface that is implemented by T.
1461
1462       ----------------
1463       -- Search_Tag --
1464       ----------------
1465
1466       function Search_Tag (Iface : Entity_Id) return Entity_Id is
1467          ADT : Elmt_Id;
1468
1469       begin
1470          ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (T))));
1471          while Present (ADT)
1472             and then Ekind (Node (ADT)) = E_Constant
1473             and then Related_Type (Node (ADT)) /= Iface
1474          loop
1475             --  Skip the secondary dispatch tables of Iface
1476
1477             Next_Elmt (ADT);
1478             Next_Elmt (ADT);
1479             Next_Elmt (ADT);
1480             Next_Elmt (ADT);
1481          end loop;
1482
1483          pragma Assert (Ekind (Node (ADT)) = E_Constant);
1484          return Node (ADT);
1485       end Search_Tag;
1486
1487    --  Start of processing for Collect_Interfaces_Info
1488
1489    begin
1490       Collect_Interfaces (T, Ifaces_List);
1491       Collect_Interface_Components (T, Comps_List);
1492
1493       --  Search for the record component and tag associated with each
1494       --  interface type of T.
1495
1496       Components_List := New_Elmt_List;
1497       Tags_List       := New_Elmt_List;
1498
1499       Iface_Elmt := First_Elmt (Ifaces_List);
1500       while Present (Iface_Elmt) loop
1501          Iface := Node (Iface_Elmt);
1502
1503          --  Associate the primary tag component and the primary dispatch table
1504          --  with all the interfaces that are parents of T
1505
1506          if Is_Ancestor (Iface, T) then
1507             Append_Elmt (First_Tag_Component (T), Components_List);
1508             Append_Elmt (Node (First_Elmt (Access_Disp_Table (T))), Tags_List);
1509
1510          --  Otherwise search for the tag component and secondary dispatch
1511          --  table of Iface
1512
1513          else
1514             Comp_Elmt := First_Elmt (Comps_List);
1515             while Present (Comp_Elmt) loop
1516                Comp_Iface := Related_Type (Node (Comp_Elmt));
1517
1518                if Comp_Iface = Iface
1519                  or else Is_Ancestor (Iface, Comp_Iface)
1520                then
1521                   Append_Elmt (Node (Comp_Elmt), Components_List);
1522                   Append_Elmt (Search_Tag (Comp_Iface), Tags_List);
1523                   exit;
1524                end if;
1525
1526                Next_Elmt (Comp_Elmt);
1527             end loop;
1528             pragma Assert (Present (Comp_Elmt));
1529          end if;
1530
1531          Next_Elmt (Iface_Elmt);
1532       end loop;
1533    end Collect_Interfaces_Info;
1534
1535    ----------------------------------
1536    -- Collect_Primitive_Operations --
1537    ----------------------------------
1538
1539    function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
1540       B_Type         : constant Entity_Id := Base_Type (T);
1541       B_Decl         : constant Node_Id   := Original_Node (Parent (B_Type));
1542       B_Scope        : Entity_Id          := Scope (B_Type);
1543       Op_List        : Elist_Id;
1544       Formal         : Entity_Id;
1545       Is_Prim        : Boolean;
1546       Formal_Derived : Boolean := False;
1547       Id             : Entity_Id;
1548
1549    begin
1550       --  For tagged types, the primitive operations are collected as they
1551       --  are declared, and held in an explicit list which is simply returned.
1552
1553       if Is_Tagged_Type (B_Type) then
1554          return Primitive_Operations (B_Type);
1555
1556       --  An untagged generic type that is a derived type inherits the
1557       --  primitive operations of its parent type. Other formal types only
1558       --  have predefined operators, which are not explicitly represented.
1559
1560       elsif Is_Generic_Type (B_Type) then
1561          if Nkind (B_Decl) = N_Formal_Type_Declaration
1562            and then Nkind (Formal_Type_Definition (B_Decl))
1563              = N_Formal_Derived_Type_Definition
1564          then
1565             Formal_Derived := True;
1566          else
1567             return New_Elmt_List;
1568          end if;
1569       end if;
1570
1571       Op_List := New_Elmt_List;
1572
1573       if B_Scope = Standard_Standard then
1574          if B_Type = Standard_String then
1575             Append_Elmt (Standard_Op_Concat, Op_List);
1576
1577          elsif B_Type = Standard_Wide_String then
1578             Append_Elmt (Standard_Op_Concatw, Op_List);
1579
1580          else
1581             null;
1582          end if;
1583
1584       elsif (Is_Package_Or_Generic_Package (B_Scope)
1585               and then
1586                 Nkind (Parent (Declaration_Node (First_Subtype (T)))) /=
1587                                                             N_Package_Body)
1588         or else Is_Derived_Type (B_Type)
1589       then
1590          --  The primitive operations appear after the base type, except
1591          --  if the derivation happens within the private part of B_Scope
1592          --  and the type is a private type, in which case both the type
1593          --  and some primitive operations may appear before the base
1594          --  type, and the list of candidates starts after the type.
1595
1596          if In_Open_Scopes (B_Scope)
1597            and then Scope (T) = B_Scope
1598            and then In_Private_Part (B_Scope)
1599          then
1600             Id := Next_Entity (T);
1601          else
1602             Id := Next_Entity (B_Type);
1603          end if;
1604
1605          while Present (Id) loop
1606
1607             --  Note that generic formal subprograms are not
1608             --  considered to be primitive operations and thus
1609             --  are never inherited.
1610
1611             if Is_Overloadable (Id)
1612               and then Nkind (Parent (Parent (Id)))
1613                          not in N_Formal_Subprogram_Declaration
1614             then
1615                Is_Prim := False;
1616
1617                if Base_Type (Etype (Id)) = B_Type then
1618                   Is_Prim := True;
1619                else
1620                   Formal := First_Formal (Id);
1621                   while Present (Formal) loop
1622                      if Base_Type (Etype (Formal)) = B_Type then
1623                         Is_Prim := True;
1624                         exit;
1625
1626                      elsif Ekind (Etype (Formal)) = E_Anonymous_Access_Type
1627                        and then Base_Type
1628                          (Designated_Type (Etype (Formal))) = B_Type
1629                      then
1630                         Is_Prim := True;
1631                         exit;
1632                      end if;
1633
1634                      Next_Formal (Formal);
1635                   end loop;
1636                end if;
1637
1638                --  For a formal derived type, the only primitives are the
1639                --  ones inherited from the parent type. Operations appearing
1640                --  in the package declaration are not primitive for it.
1641
1642                if Is_Prim
1643                  and then (not Formal_Derived
1644                             or else Present (Alias (Id)))
1645                then
1646                   Append_Elmt (Id, Op_List);
1647                end if;
1648             end if;
1649
1650             Next_Entity (Id);
1651
1652             --  For a type declared in System, some of its operations
1653             --  may appear in  the target-specific extension to System.
1654
1655             if No (Id)
1656               and then Chars (B_Scope) = Name_System
1657               and then Scope (B_Scope) = Standard_Standard
1658               and then Present_System_Aux
1659             then
1660                B_Scope := System_Aux_Id;
1661                Id := First_Entity (System_Aux_Id);
1662             end if;
1663          end loop;
1664       end if;
1665
1666       return Op_List;
1667    end Collect_Primitive_Operations;
1668
1669    -----------------------------------
1670    -- Compile_Time_Constraint_Error --
1671    -----------------------------------
1672
1673    function Compile_Time_Constraint_Error
1674      (N    : Node_Id;
1675       Msg  : String;
1676       Ent  : Entity_Id  := Empty;
1677       Loc  : Source_Ptr := No_Location;
1678       Warn : Boolean    := False) return Node_Id
1679    is
1680       Msgc : String (1 .. Msg'Length + 2);
1681       --  Copy of message, with room for possible ? and ! at end
1682
1683       Msgl : Natural;
1684       Wmsg : Boolean;
1685       P    : Node_Id;
1686       OldP : Node_Id;
1687       Msgs : Boolean;
1688       Eloc : Source_Ptr;
1689
1690    begin
1691       --  A static constraint error in an instance body is not a fatal error.
1692       --  we choose to inhibit the message altogether, because there is no
1693       --  obvious node (for now) on which to post it. On the other hand the
1694       --  offending node must be replaced with a constraint_error in any case.
1695
1696       --  No messages are generated if we already posted an error on this node
1697
1698       if not Error_Posted (N) then
1699          if Loc /= No_Location then
1700             Eloc := Loc;
1701          else
1702             Eloc := Sloc (N);
1703          end if;
1704
1705          Msgc (1 .. Msg'Length) := Msg;
1706          Msgl := Msg'Length;
1707
1708          --  Message is a warning, even in Ada 95 case
1709
1710          if Msg (Msg'Last) = '?' then
1711             Wmsg := True;
1712
1713          --  In Ada 83, all messages are warnings. In the private part and
1714          --  the body of an instance, constraint_checks are only warnings.
1715          --  We also make this a warning if the Warn parameter is set.
1716
1717          elsif Warn
1718            or else (Ada_Version = Ada_83 and then Comes_From_Source (N))
1719          then
1720             Msgl := Msgl + 1;
1721             Msgc (Msgl) := '?';
1722             Wmsg := True;
1723
1724          elsif In_Instance_Not_Visible then
1725             Msgl := Msgl + 1;
1726             Msgc (Msgl) := '?';
1727             Wmsg := True;
1728
1729          --  Otherwise we have a real error message (Ada 95 static case)
1730          --  and we make this an unconditional message. Note that in the
1731          --  warning case we do not make the message unconditional, it seems
1732          --  quite reasonable to delete messages like this (about exceptions
1733          --  that will be raised) in dead code.
1734
1735          else
1736             Wmsg := False;
1737             Msgl := Msgl + 1;
1738             Msgc (Msgl) := '!';
1739          end if;
1740
1741          --  Should we generate a warning? The answer is not quite yes. The
1742          --  very annoying exception occurs in the case of a short circuit
1743          --  operator where the left operand is static and decisive. Climb
1744          --  parents to see if that is the case we have here. Conditional
1745          --  expressions with decisive conditions are a similar situation.
1746
1747          Msgs := True;
1748          P := N;
1749          loop
1750             OldP := P;
1751             P := Parent (P);
1752
1753             --  And then with False as left operand
1754
1755             if Nkind (P) = N_And_Then
1756               and then Compile_Time_Known_Value (Left_Opnd (P))
1757               and then Is_False (Expr_Value (Left_Opnd (P)))
1758             then
1759                Msgs := False;
1760                exit;
1761
1762             --  OR ELSE with True as left operand
1763
1764             elsif Nkind (P) = N_Or_Else
1765               and then Compile_Time_Known_Value (Left_Opnd (P))
1766               and then Is_True (Expr_Value (Left_Opnd (P)))
1767             then
1768                Msgs := False;
1769                exit;
1770
1771             --  Conditional expression
1772
1773             elsif Nkind (P) = N_Conditional_Expression then
1774                declare
1775                   Cond : constant Node_Id := First (Expressions (P));
1776                   Texp : constant Node_Id := Next (Cond);
1777                   Fexp : constant Node_Id := Next (Texp);
1778
1779                begin
1780                   if Compile_Time_Known_Value (Cond) then
1781
1782                      --  Condition is True and we are in the right operand
1783
1784                      if Is_True (Expr_Value (Cond))
1785                        and then OldP = Fexp
1786                      then
1787                         Msgs := False;
1788                         exit;
1789
1790                      --  Condition is False and we are in the left operand
1791
1792                      elsif Is_False (Expr_Value (Cond))
1793                        and then OldP = Texp
1794                      then
1795                         Msgs := False;
1796                         exit;
1797                      end if;
1798                   end if;
1799                end;
1800
1801             --  Special case for component association in aggregates, where
1802             --  we want to keep climbing up to the parent aggregate.
1803
1804             elsif Nkind (P) = N_Component_Association
1805               and then Nkind (Parent (P)) = N_Aggregate
1806             then
1807                null;
1808
1809             --  Keep going if within subexpression
1810
1811             else
1812                exit when Nkind (P) not in N_Subexpr;
1813             end if;
1814          end loop;
1815
1816          if Msgs then
1817             if Present (Ent) then
1818                Error_Msg_NEL (Msgc (1 .. Msgl), N, Ent, Eloc);
1819             else
1820                Error_Msg_NEL (Msgc (1 .. Msgl), N, Etype (N), Eloc);
1821             end if;
1822
1823             if Wmsg then
1824                if Inside_Init_Proc then
1825                   Error_Msg_NEL
1826                     ("\?& will be raised for objects of this type",
1827                      N, Standard_Constraint_Error, Eloc);
1828                else
1829                   Error_Msg_NEL
1830                     ("\?& will be raised at run time",
1831                      N, Standard_Constraint_Error, Eloc);
1832                end if;
1833
1834             else
1835                Error_Msg
1836                  ("\static expression fails Constraint_Check", Eloc);
1837                Set_Error_Posted (N);
1838             end if;
1839          end if;
1840       end if;
1841
1842       return N;
1843    end Compile_Time_Constraint_Error;
1844
1845    -----------------------
1846    -- Conditional_Delay --
1847    -----------------------
1848
1849    procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
1850    begin
1851       if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
1852          Set_Has_Delayed_Freeze (New_Ent);
1853       end if;
1854    end Conditional_Delay;
1855
1856    -------------------------
1857    -- Copy_Parameter_List --
1858    -------------------------
1859
1860    function Copy_Parameter_List (Subp_Id : Entity_Id) return List_Id is
1861       Loc    : constant Source_Ptr := Sloc (Subp_Id);
1862       Plist  : List_Id;
1863       Formal : Entity_Id;
1864
1865    begin
1866       if No (First_Formal (Subp_Id)) then
1867          return No_List;
1868       else
1869          Plist := New_List;
1870          Formal := First_Formal (Subp_Id);
1871          while Present (Formal) loop
1872             Append
1873               (Make_Parameter_Specification (Loc,
1874                 Defining_Identifier =>
1875                   Make_Defining_Identifier (Sloc (Formal),
1876                     Chars => Chars (Formal)),
1877                 In_Present  => In_Present (Parent (Formal)),
1878                 Out_Present => Out_Present (Parent (Formal)),
1879              Parameter_Type =>
1880                   New_Reference_To (Etype (Formal), Loc),
1881                 Expression =>
1882                   New_Copy_Tree (Expression (Parent (Formal)))),
1883               Plist);
1884
1885             Next_Formal (Formal);
1886          end loop;
1887       end if;
1888
1889       return Plist;
1890    end Copy_Parameter_List;
1891
1892    --------------------
1893    -- Current_Entity --
1894    --------------------
1895
1896    --  The currently visible definition for a given identifier is the
1897    --  one most chained at the start of the visibility chain, i.e. the
1898    --  one that is referenced by the Node_Id value of the name of the
1899    --  given identifier.
1900
1901    function Current_Entity (N : Node_Id) return Entity_Id is
1902    begin
1903       return Get_Name_Entity_Id (Chars (N));
1904    end Current_Entity;
1905
1906    -----------------------------
1907    -- Current_Entity_In_Scope --
1908    -----------------------------
1909
1910    function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
1911       E  : Entity_Id;
1912       CS : constant Entity_Id := Current_Scope;
1913
1914       Transient_Case : constant Boolean := Scope_Is_Transient;
1915
1916    begin
1917       E := Get_Name_Entity_Id (Chars (N));
1918       while Present (E)
1919         and then Scope (E) /= CS
1920         and then (not Transient_Case or else Scope (E) /= Scope (CS))
1921       loop
1922          E := Homonym (E);
1923       end loop;
1924
1925       return E;
1926    end Current_Entity_In_Scope;
1927
1928    -------------------
1929    -- Current_Scope --
1930    -------------------
1931
1932    function Current_Scope return Entity_Id is
1933    begin
1934       if Scope_Stack.Last = -1 then
1935          return Standard_Standard;
1936       else
1937          declare
1938             C : constant Entity_Id :=
1939                   Scope_Stack.Table (Scope_Stack.Last).Entity;
1940          begin
1941             if Present (C) then
1942                return C;
1943             else
1944                return Standard_Standard;
1945             end if;
1946          end;
1947       end if;
1948    end Current_Scope;
1949
1950    ------------------------
1951    -- Current_Subprogram --
1952    ------------------------
1953
1954    function Current_Subprogram return Entity_Id is
1955       Scop : constant Entity_Id := Current_Scope;
1956    begin
1957       if Is_Subprogram (Scop) or else Is_Generic_Subprogram (Scop) then
1958          return Scop;
1959       else
1960          return Enclosing_Subprogram (Scop);
1961       end if;
1962    end Current_Subprogram;
1963
1964    ---------------------
1965    -- Defining_Entity --
1966    ---------------------
1967
1968    function Defining_Entity (N : Node_Id) return Entity_Id is
1969       K   : constant Node_Kind := Nkind (N);
1970       Err : Entity_Id := Empty;
1971
1972    begin
1973       case K is
1974          when
1975            N_Subprogram_Declaration                 |
1976            N_Abstract_Subprogram_Declaration        |
1977            N_Subprogram_Body                        |
1978            N_Package_Declaration                    |
1979            N_Subprogram_Renaming_Declaration        |
1980            N_Subprogram_Body_Stub                   |
1981            N_Generic_Subprogram_Declaration         |
1982            N_Generic_Package_Declaration            |
1983            N_Formal_Subprogram_Declaration
1984          =>
1985             return Defining_Entity (Specification (N));
1986
1987          when
1988            N_Component_Declaration                  |
1989            N_Defining_Program_Unit_Name             |
1990            N_Discriminant_Specification             |
1991            N_Entry_Body                             |
1992            N_Entry_Declaration                      |
1993            N_Entry_Index_Specification              |
1994            N_Exception_Declaration                  |
1995            N_Exception_Renaming_Declaration         |
1996            N_Formal_Object_Declaration              |
1997            N_Formal_Package_Declaration             |
1998            N_Formal_Type_Declaration                |
1999            N_Full_Type_Declaration                  |
2000            N_Implicit_Label_Declaration             |
2001            N_Incomplete_Type_Declaration            |
2002            N_Loop_Parameter_Specification           |
2003            N_Number_Declaration                     |
2004            N_Object_Declaration                     |
2005            N_Object_Renaming_Declaration            |
2006            N_Package_Body_Stub                      |
2007            N_Parameter_Specification                |
2008            N_Private_Extension_Declaration          |
2009            N_Private_Type_Declaration               |
2010            N_Protected_Body                         |
2011            N_Protected_Body_Stub                    |
2012            N_Protected_Type_Declaration             |
2013            N_Single_Protected_Declaration           |
2014            N_Single_Task_Declaration                |
2015            N_Subtype_Declaration                    |
2016            N_Task_Body                              |
2017            N_Task_Body_Stub                         |
2018            N_Task_Type_Declaration
2019          =>
2020             return Defining_Identifier (N);
2021
2022          when N_Subunit =>
2023             return Defining_Entity (Proper_Body (N));
2024
2025          when
2026            N_Function_Instantiation                 |
2027            N_Function_Specification                 |
2028            N_Generic_Function_Renaming_Declaration  |
2029            N_Generic_Package_Renaming_Declaration   |
2030            N_Generic_Procedure_Renaming_Declaration |
2031            N_Package_Body                           |
2032            N_Package_Instantiation                  |
2033            N_Package_Renaming_Declaration           |
2034            N_Package_Specification                  |
2035            N_Procedure_Instantiation                |
2036            N_Procedure_Specification
2037          =>
2038             declare
2039                Nam : constant Node_Id := Defining_Unit_Name (N);
2040
2041             begin
2042                if Nkind (Nam) in N_Entity then
2043                   return Nam;
2044
2045                --  For Error, make up a name and attach to declaration
2046                --  so we can continue semantic analysis
2047
2048                elsif Nam = Error then
2049                   Err :=
2050                     Make_Defining_Identifier (Sloc (N),
2051                       Chars => New_Internal_Name ('T'));
2052                   Set_Defining_Unit_Name (N, Err);
2053
2054                   return Err;
2055                --  If not an entity, get defining identifier
2056
2057                else
2058                   return Defining_Identifier (Nam);
2059                end if;
2060             end;
2061
2062          when N_Block_Statement =>
2063             return Entity (Identifier (N));
2064
2065          when others =>
2066             raise Program_Error;
2067
2068       end case;
2069    end Defining_Entity;
2070
2071    --------------------------
2072    -- Denotes_Discriminant --
2073    --------------------------
2074
2075    function Denotes_Discriminant
2076      (N                : Node_Id;
2077       Check_Concurrent : Boolean := False) return Boolean
2078    is
2079       E : Entity_Id;
2080    begin
2081       if not Is_Entity_Name (N)
2082         or else No (Entity (N))
2083       then
2084          return False;
2085       else
2086          E := Entity (N);
2087       end if;
2088
2089       --  If we are checking for a protected type, the discriminant may have
2090       --  been rewritten as the corresponding discriminal of the original type
2091       --  or of the corresponding concurrent record, depending on whether we
2092       --  are in the spec or body of the protected type.
2093
2094       return Ekind (E) = E_Discriminant
2095         or else
2096           (Check_Concurrent
2097             and then Ekind (E) = E_In_Parameter
2098             and then Present (Discriminal_Link (E))
2099             and then
2100               (Is_Concurrent_Type (Scope (Discriminal_Link (E)))
2101                 or else
2102                   Is_Concurrent_Record_Type (Scope (Discriminal_Link (E)))));
2103
2104    end Denotes_Discriminant;
2105
2106    ----------------------
2107    -- Denotes_Variable --
2108    ----------------------
2109
2110    function Denotes_Variable (N : Node_Id) return Boolean is
2111    begin
2112       return Is_Variable (N) and then Paren_Count (N) = 0;
2113    end Denotes_Variable;
2114
2115    -----------------------------
2116    -- Depends_On_Discriminant --
2117    -----------------------------
2118
2119    function Depends_On_Discriminant (N : Node_Id) return Boolean is
2120       L : Node_Id;
2121       H : Node_Id;
2122
2123    begin
2124       Get_Index_Bounds (N, L, H);
2125       return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
2126    end Depends_On_Discriminant;
2127
2128    -------------------------
2129    -- Designate_Same_Unit --
2130    -------------------------
2131
2132    function Designate_Same_Unit
2133      (Name1 : Node_Id;
2134       Name2 : Node_Id) return Boolean
2135    is
2136       K1 : constant Node_Kind := Nkind (Name1);
2137       K2 : constant Node_Kind := Nkind (Name2);
2138
2139       function Prefix_Node (N : Node_Id) return Node_Id;
2140       --  Returns the parent unit name node of a defining program unit name
2141       --  or the prefix if N is a selected component or an expanded name.
2142
2143       function Select_Node (N : Node_Id) return Node_Id;
2144       --  Returns the defining identifier node of a defining program unit
2145       --  name or  the selector node if N is a selected component or an
2146       --  expanded name.
2147
2148       -----------------
2149       -- Prefix_Node --
2150       -----------------
2151
2152       function Prefix_Node (N : Node_Id) return Node_Id is
2153       begin
2154          if Nkind (N) = N_Defining_Program_Unit_Name then
2155             return Name (N);
2156
2157          else
2158             return Prefix (N);
2159          end if;
2160       end Prefix_Node;
2161
2162       -----------------
2163       -- Select_Node --
2164       -----------------
2165
2166       function Select_Node (N : Node_Id) return Node_Id is
2167       begin
2168          if Nkind (N) = N_Defining_Program_Unit_Name then
2169             return Defining_Identifier (N);
2170
2171          else
2172             return Selector_Name (N);
2173          end if;
2174       end Select_Node;
2175
2176    --  Start of processing for Designate_Next_Unit
2177
2178    begin
2179       if (K1 = N_Identifier or else
2180           K1 = N_Defining_Identifier)
2181         and then
2182          (K2 = N_Identifier or else
2183           K2 = N_Defining_Identifier)
2184       then
2185          return Chars (Name1) = Chars (Name2);
2186
2187       elsif
2188          (K1 = N_Expanded_Name      or else
2189           K1 = N_Selected_Component or else
2190           K1 = N_Defining_Program_Unit_Name)
2191         and then
2192          (K2 = N_Expanded_Name      or else
2193           K2 = N_Selected_Component or else
2194           K2 = N_Defining_Program_Unit_Name)
2195       then
2196          return
2197            (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
2198              and then
2199                Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
2200
2201       else
2202          return False;
2203       end if;
2204    end Designate_Same_Unit;
2205
2206    ----------------------------
2207    -- Enclosing_Generic_Body --
2208    ----------------------------
2209
2210    function Enclosing_Generic_Body
2211      (N : Node_Id) return Node_Id
2212    is
2213       P    : Node_Id;
2214       Decl : Node_Id;
2215       Spec : Node_Id;
2216
2217    begin
2218       P := Parent (N);
2219       while Present (P) loop
2220          if Nkind (P) = N_Package_Body
2221            or else Nkind (P) = N_Subprogram_Body
2222          then
2223             Spec := Corresponding_Spec (P);
2224
2225             if Present (Spec) then
2226                Decl := Unit_Declaration_Node (Spec);
2227
2228                if Nkind (Decl) = N_Generic_Package_Declaration
2229                  or else Nkind (Decl) = N_Generic_Subprogram_Declaration
2230                then
2231                   return P;
2232                end if;
2233             end if;
2234          end if;
2235
2236          P := Parent (P);
2237       end loop;
2238
2239       return Empty;
2240    end Enclosing_Generic_Body;
2241
2242    ----------------------------
2243    -- Enclosing_Generic_Unit --
2244    ----------------------------
2245
2246    function Enclosing_Generic_Unit
2247      (N : Node_Id) return Node_Id
2248    is
2249       P    : Node_Id;
2250       Decl : Node_Id;
2251       Spec : Node_Id;
2252
2253    begin
2254       P := Parent (N);
2255       while Present (P) loop
2256          if Nkind (P) = N_Generic_Package_Declaration
2257            or else Nkind (P) = N_Generic_Subprogram_Declaration
2258          then
2259             return P;
2260
2261          elsif Nkind (P) = N_Package_Body
2262            or else Nkind (P) = N_Subprogram_Body
2263          then
2264             Spec := Corresponding_Spec (P);
2265
2266             if Present (Spec) then
2267                Decl := Unit_Declaration_Node (Spec);
2268
2269                if Nkind (Decl) = N_Generic_Package_Declaration
2270                  or else Nkind (Decl) = N_Generic_Subprogram_Declaration
2271                then
2272                   return Decl;
2273                end if;
2274             end if;
2275          end if;
2276
2277          P := Parent (P);
2278       end loop;
2279
2280       return Empty;
2281    end Enclosing_Generic_Unit;
2282
2283    -------------------------------
2284    -- Enclosing_Lib_Unit_Entity --
2285    -------------------------------
2286
2287    function Enclosing_Lib_Unit_Entity return Entity_Id is
2288       Unit_Entity : Entity_Id;
2289
2290    begin
2291       --  Look for enclosing library unit entity by following scope links.
2292       --  Equivalent to, but faster than indexing through the scope stack.
2293
2294       Unit_Entity := Current_Scope;
2295       while (Present (Scope (Unit_Entity))
2296         and then Scope (Unit_Entity) /= Standard_Standard)
2297         and not Is_Child_Unit (Unit_Entity)
2298       loop
2299          Unit_Entity := Scope (Unit_Entity);
2300       end loop;
2301
2302       return Unit_Entity;
2303    end Enclosing_Lib_Unit_Entity;
2304
2305    -----------------------------
2306    -- Enclosing_Lib_Unit_Node --
2307    -----------------------------
2308
2309    function Enclosing_Lib_Unit_Node (N : Node_Id) return Node_Id is
2310       Current_Node : Node_Id;
2311
2312    begin
2313       Current_Node := N;
2314       while Present (Current_Node)
2315         and then Nkind (Current_Node) /= N_Compilation_Unit
2316       loop
2317          Current_Node := Parent (Current_Node);
2318       end loop;
2319
2320       if Nkind (Current_Node) /= N_Compilation_Unit then
2321          return Empty;
2322       end if;
2323
2324       return Current_Node;
2325    end Enclosing_Lib_Unit_Node;
2326
2327    --------------------------
2328    -- Enclosing_Subprogram --
2329    --------------------------
2330
2331    function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
2332       Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
2333
2334    begin
2335       if Dynamic_Scope = Standard_Standard then
2336          return Empty;
2337
2338       elsif Dynamic_Scope = Empty then
2339          return Empty;
2340
2341       elsif Ekind (Dynamic_Scope) = E_Subprogram_Body then
2342          return Corresponding_Spec (Parent (Parent (Dynamic_Scope)));
2343
2344       elsif Ekind (Dynamic_Scope) = E_Block
2345         or else Ekind (Dynamic_Scope) = E_Return_Statement
2346       then
2347          return Enclosing_Subprogram (Dynamic_Scope);
2348
2349       elsif Ekind (Dynamic_Scope) = E_Task_Type then
2350          return Get_Task_Body_Procedure (Dynamic_Scope);
2351
2352       elsif Convention (Dynamic_Scope) = Convention_Protected then
2353          return Protected_Body_Subprogram (Dynamic_Scope);
2354
2355       else
2356          return Dynamic_Scope;
2357       end if;
2358    end Enclosing_Subprogram;
2359
2360    ------------------------
2361    -- Ensure_Freeze_Node --
2362    ------------------------
2363
2364    procedure Ensure_Freeze_Node (E : Entity_Id) is
2365       FN : Node_Id;
2366
2367    begin
2368       if No (Freeze_Node (E)) then
2369          FN := Make_Freeze_Entity (Sloc (E));
2370          Set_Has_Delayed_Freeze (E);
2371          Set_Freeze_Node (E, FN);
2372          Set_Access_Types_To_Process (FN, No_Elist);
2373          Set_TSS_Elist (FN, No_Elist);
2374          Set_Entity (FN, E);
2375       end if;
2376    end Ensure_Freeze_Node;
2377
2378    ----------------
2379    -- Enter_Name --
2380    ----------------
2381
2382    procedure Enter_Name (Def_Id : Entity_Id) is
2383       C : constant Entity_Id := Current_Entity (Def_Id);
2384       E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
2385       S : constant Entity_Id := Current_Scope;
2386
2387    begin
2388       Generate_Definition (Def_Id);
2389
2390       --  Add new name to current scope declarations. Check for duplicate
2391       --  declaration, which may or may not be a genuine error.
2392
2393       if Present (E) then
2394
2395          --  Case of previous entity entered because of a missing declaration
2396          --  or else a bad subtype indication. Best is to use the new entity,
2397          --  and make the previous one invisible.
2398
2399          if Etype (E) = Any_Type then
2400             Set_Is_Immediately_Visible (E, False);
2401
2402          --  Case of renaming declaration constructed for package instances.
2403          --  if there is an explicit declaration with the same identifier,
2404          --  the renaming is not immediately visible any longer, but remains
2405          --  visible through selected component notation.
2406
2407          elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
2408            and then not Comes_From_Source (E)
2409          then
2410             Set_Is_Immediately_Visible (E, False);
2411
2412          --  The new entity may be the package renaming, which has the same
2413          --  same name as a generic formal which has been seen already.
2414
2415          elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
2416             and then not Comes_From_Source (Def_Id)
2417          then
2418             Set_Is_Immediately_Visible (E, False);
2419
2420          --  For a fat pointer corresponding to a remote access to subprogram,
2421          --  we use the same identifier as the RAS type, so that the proper
2422          --  name appears in the stub. This type is only retrieved through
2423          --  the RAS type and never by visibility, and is not added to the
2424          --  visibility list (see below).
2425
2426          elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
2427            and then Present (Corresponding_Remote_Type (Def_Id))
2428          then
2429             null;
2430
2431          --  A controller component for a type extension overrides the
2432          --  inherited component.
2433
2434          elsif Chars (E) = Name_uController then
2435             null;
2436
2437          --  Case of an implicit operation or derived literal. The new entity
2438          --  hides the implicit one,  which is removed from all visibility,
2439          --  i.e. the entity list of its scope, and homonym chain of its name.
2440
2441          elsif (Is_Overloadable (E) and then Is_Inherited_Operation (E))
2442            or else Is_Internal (E)
2443          then
2444             declare
2445                Prev     : Entity_Id;
2446                Prev_Vis : Entity_Id;
2447                Decl     : constant Node_Id := Parent (E);
2448
2449             begin
2450                --  If E is an implicit declaration, it cannot be the first
2451                --  entity in the scope.
2452
2453                Prev := First_Entity (Current_Scope);
2454                while Present (Prev)
2455                  and then Next_Entity (Prev) /= E
2456                loop
2457                   Next_Entity (Prev);
2458                end loop;
2459
2460                if No (Prev) then
2461
2462                   --  If E is not on the entity chain of the current scope,
2463                   --  it is an implicit declaration in the generic formal
2464                   --  part of a generic subprogram. When analyzing the body,
2465                   --  the generic formals are visible but not on the entity
2466                   --  chain of the subprogram. The new entity will become
2467                   --  the visible one in the body.
2468
2469                   pragma Assert
2470                     (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
2471                   null;
2472
2473                else
2474                   Set_Next_Entity (Prev, Next_Entity (E));
2475
2476                   if No (Next_Entity (Prev)) then
2477                      Set_Last_Entity (Current_Scope, Prev);
2478                   end if;
2479
2480                   if E = Current_Entity (E) then
2481                      Prev_Vis := Empty;
2482
2483                   else
2484                      Prev_Vis := Current_Entity (E);
2485                      while Homonym (Prev_Vis) /= E loop
2486                         Prev_Vis := Homonym (Prev_Vis);
2487                      end loop;
2488                   end if;
2489
2490                   if Present (Prev_Vis)  then
2491
2492                      --  Skip E in the visibility chain
2493
2494                      Set_Homonym (Prev_Vis, Homonym (E));
2495
2496                   else
2497                      Set_Name_Entity_Id (Chars (E), Homonym (E));
2498                   end if;
2499                end if;
2500             end;
2501
2502          --  This section of code could use a comment ???
2503
2504          elsif Present (Etype (E))
2505            and then Is_Concurrent_Type (Etype (E))
2506            and then E = Def_Id
2507          then
2508             return;
2509
2510          --  If the homograph is a protected component renaming, it should not
2511          --  be hiding the current entity. Such renamings are treated as weak
2512          --  declarations.
2513
2514          elsif Is_Prival (E) then
2515             Set_Is_Immediately_Visible (E, False);
2516
2517          --  In this case the current entity is a protected component renaming.
2518          --  Perform minimal decoration by setting the scope and return since
2519          --  the prival should not be hiding other visible entities.
2520
2521          elsif Is_Prival (Def_Id) then
2522             Set_Scope (Def_Id, Current_Scope);
2523             return;
2524
2525          --  Analogous to privals, the discriminal generated for an entry
2526          --  index parameter acts as a weak declaration. Perform minimal
2527          --  decoration to avoid bogus errors.
2528
2529          elsif Is_Discriminal (Def_Id)
2530            and then Ekind (Discriminal_Link (Def_Id)) = E_Entry_Index_Parameter
2531          then
2532             Set_Scope (Def_Id, Current_Scope);
2533             return;
2534
2535          --  In the body or private part of an instance, a type extension
2536          --  may introduce a component with the same name as that of an
2537          --  actual. The legality rule is not enforced, but the semantics
2538          --  of the full type with two components of the same name are not
2539          --  clear at this point ???
2540
2541          elsif In_Instance_Not_Visible then
2542             null;
2543
2544          --  When compiling a package body, some child units may have become
2545          --  visible. They cannot conflict with local entities that hide them.
2546
2547          elsif Is_Child_Unit (E)
2548            and then In_Open_Scopes (Scope (E))
2549            and then not Is_Immediately_Visible (E)
2550          then
2551             null;
2552
2553          --  Conversely, with front-end inlining we may compile the parent
2554          --  body first, and a child unit subsequently. The context is now
2555          --  the parent spec, and body entities are not visible.
2556
2557          elsif Is_Child_Unit (Def_Id)
2558            and then Is_Package_Body_Entity (E)
2559            and then not In_Package_Body (Current_Scope)
2560          then
2561             null;
2562
2563          --  Case of genuine duplicate declaration
2564
2565          else
2566             Error_Msg_Sloc := Sloc (E);
2567
2568             --  If the previous declaration is an incomplete type declaration
2569             --  this may be an attempt to complete it with a private type.
2570             --  The following avoids confusing cascaded errors.
2571
2572             if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
2573               and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
2574             then
2575                Error_Msg_N
2576                  ("incomplete type cannot be completed with a private " &
2577                   "declaration", Parent (Def_Id));
2578                Set_Is_Immediately_Visible (E, False);
2579                Set_Full_View (E, Def_Id);
2580
2581             --  An inherited component of a record conflicts with a new
2582             --  discriminant. The discriminant is inserted first in the scope,
2583             --  but the error should be posted on it, not on the component.
2584
2585             elsif Ekind (E) = E_Discriminant
2586               and then Present (Scope (Def_Id))
2587               and then Scope (Def_Id) /= Current_Scope
2588             then
2589                Error_Msg_Sloc := Sloc (Def_Id);
2590                Error_Msg_N ("& conflicts with declaration#", E);
2591                return;
2592
2593             --  If the name of the unit appears in its own context clause,
2594             --  a dummy package with the name has already been created, and
2595             --  the error emitted. Try to continue quietly.
2596
2597             elsif Error_Posted (E)
2598               and then Sloc (E) = No_Location
2599               and then Nkind (Parent (E)) = N_Package_Specification
2600               and then Current_Scope = Standard_Standard
2601             then
2602                Set_Scope (Def_Id, Current_Scope);
2603                return;
2604
2605             else
2606                Error_Msg_N ("& conflicts with declaration#", Def_Id);
2607
2608                --  Avoid cascaded messages with duplicate components in
2609                --  derived types.
2610
2611                if Ekind (E) = E_Component
2612                  or else Ekind (E) = E_Discriminant
2613                then
2614                   return;
2615                end if;
2616             end if;
2617
2618             if Nkind (Parent (Parent (Def_Id))) =
2619                 N_Generic_Subprogram_Declaration
2620               and then Def_Id =
2621                 Defining_Entity (Specification (Parent (Parent (Def_Id))))
2622             then
2623                Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
2624             end if;
2625
2626             --  If entity is in standard, then we are in trouble, because
2627             --  it means that we have a library package with a duplicated
2628             --  name. That's hard to recover from, so abort!
2629
2630             if S = Standard_Standard then
2631                raise Unrecoverable_Error;
2632
2633             --  Otherwise we continue with the declaration. Having two
2634             --  identical declarations should not cause us too much trouble!
2635
2636             else
2637                null;
2638             end if;
2639          end if;
2640       end if;
2641
2642       --  If we fall through, declaration is OK , or OK enough to continue
2643
2644       --  If Def_Id is a discriminant or a record component we are in the
2645       --  midst of inheriting components in a derived record definition.
2646       --  Preserve their Ekind and Etype.
2647
2648       if Ekind (Def_Id) = E_Discriminant
2649         or else Ekind (Def_Id) = E_Component
2650       then
2651          null;
2652
2653       --  If a type is already set, leave it alone (happens whey a type
2654       --  declaration is reanalyzed following a call to the optimizer)
2655
2656       elsif Present (Etype (Def_Id)) then
2657          null;
2658
2659       --  Otherwise, the kind E_Void insures that premature uses of the entity
2660       --  will be detected. Any_Type insures that no cascaded errors will occur
2661
2662       else
2663          Set_Ekind (Def_Id, E_Void);
2664          Set_Etype (Def_Id, Any_Type);
2665       end if;
2666
2667       --  Inherited discriminants and components in derived record types are
2668       --  immediately visible. Itypes are not.
2669
2670       if Ekind (Def_Id) = E_Discriminant
2671         or else Ekind (Def_Id) = E_Component
2672         or else (No (Corresponding_Remote_Type (Def_Id))
2673                  and then not Is_Itype (Def_Id))
2674       then
2675          Set_Is_Immediately_Visible (Def_Id);
2676          Set_Current_Entity         (Def_Id);
2677       end if;
2678
2679       Set_Homonym       (Def_Id, C);
2680       Append_Entity     (Def_Id, S);
2681       Set_Public_Status (Def_Id);
2682
2683       --  Warn if new entity hides an old one
2684
2685       if Warn_On_Hiding and then Present (C)
2686
2687          --  Don't warn for record components since they always have a well
2688          --  defined scope which does not confuse other uses. Note that in
2689          --  some cases, Ekind has not been set yet.
2690
2691          and then Ekind (C) /= E_Component
2692          and then Ekind (C) /= E_Discriminant
2693          and then Nkind (Parent (C)) /= N_Component_Declaration
2694          and then Ekind (Def_Id) /= E_Component
2695          and then Ekind (Def_Id) /= E_Discriminant
2696          and then Nkind (Parent (Def_Id)) /= N_Component_Declaration
2697
2698          --  Don't warn for one character variables. It is too common to use
2699          --  such variables as locals and will just cause too many false hits.
2700
2701          and then Length_Of_Name (Chars (C)) /= 1
2702
2703          --  Don't warn for non-source entities
2704
2705          and then Comes_From_Source (C)
2706          and then Comes_From_Source (Def_Id)
2707
2708          --  Don't warn unless entity in question is in extended main source
2709
2710          and then In_Extended_Main_Source_Unit (Def_Id)
2711
2712          --  Finally, the hidden entity must be either immediately visible
2713          --  or use visible (from a used package)
2714
2715          and then
2716            (Is_Immediately_Visible (C)
2717               or else
2718             Is_Potentially_Use_Visible (C))
2719       then
2720          Error_Msg_Sloc := Sloc (C);
2721          Error_Msg_N ("declaration hides &#?", Def_Id);
2722       end if;
2723    end Enter_Name;
2724
2725    --------------------------
2726    -- Explain_Limited_Type --
2727    --------------------------
2728
2729    procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
2730       C : Entity_Id;
2731
2732    begin
2733       --  For array, component type must be limited
2734
2735       if Is_Array_Type (T) then
2736          Error_Msg_Node_2 := T;
2737          Error_Msg_NE
2738            ("\component type& of type& is limited", N, Component_Type (T));
2739          Explain_Limited_Type (Component_Type (T), N);
2740
2741       elsif Is_Record_Type (T) then
2742
2743          --  No need for extra messages if explicit limited record
2744
2745          if Is_Limited_Record (Base_Type (T)) then
2746             return;
2747          end if;
2748
2749          --  Otherwise find a limited component. Check only components that
2750          --  come from source, or inherited components that appear in the
2751          --  source of the ancestor.
2752
2753          C := First_Component (T);
2754          while Present (C) loop
2755             if Is_Limited_Type (Etype (C))
2756               and then
2757                 (Comes_From_Source (C)
2758                    or else
2759                      (Present (Original_Record_Component (C))
2760                        and then
2761                          Comes_From_Source (Original_Record_Component (C))))
2762             then
2763                Error_Msg_Node_2 := T;
2764                Error_Msg_NE ("\component& of type& has limited type", N, C);
2765                Explain_Limited_Type (Etype (C), N);
2766                return;
2767             end if;
2768
2769             Next_Component (C);
2770          end loop;
2771
2772          --  The type may be declared explicitly limited, even if no component
2773          --  of it is limited, in which case we fall out of the loop.
2774          return;
2775       end if;
2776    end Explain_Limited_Type;
2777
2778    -----------------
2779    -- Find_Actual --
2780    -----------------
2781
2782    procedure Find_Actual
2783      (N        : Node_Id;
2784       Formal   : out Entity_Id;
2785       Call     : out Node_Id)
2786    is
2787       Parnt  : constant Node_Id := Parent (N);
2788       Actual : Node_Id;
2789
2790    begin
2791       if (Nkind (Parnt) = N_Indexed_Component
2792             or else
2793           Nkind (Parnt) = N_Selected_Component)
2794         and then N = Prefix (Parnt)
2795       then
2796          Find_Actual (Parnt, Formal, Call);
2797          return;
2798
2799       elsif Nkind (Parnt) = N_Parameter_Association
2800         and then N = Explicit_Actual_Parameter (Parnt)
2801       then
2802          Call := Parent (Parnt);
2803
2804       elsif Nkind (Parnt) = N_Procedure_Call_Statement then
2805          Call := Parnt;
2806
2807       else
2808          Formal := Empty;
2809          Call   := Empty;
2810          return;
2811       end if;
2812
2813       --  If we have a call to a subprogram look for the parameter. Note that
2814       --  we exclude overloaded calls, since we don't know enough to be sure
2815       --  of giving the right answer in this case.
2816
2817       if Is_Entity_Name (Name (Call))
2818         and then Present (Entity (Name (Call)))
2819         and then Is_Overloadable (Entity (Name (Call)))
2820         and then not Is_Overloaded (Name (Call))
2821       then
2822          --  Fall here if we are definitely a parameter
2823
2824          Actual := First_Actual (Call);
2825          Formal := First_Formal (Entity (Name (Call)));
2826          while Present (Formal) and then Present (Actual) loop
2827             if Actual = N then
2828                return;
2829             else
2830                Actual := Next_Actual (Actual);
2831                Formal := Next_Formal (Formal);
2832             end if;
2833          end loop;
2834       end if;
2835
2836       --  Fall through here if we did not find matching actual
2837
2838       Formal := Empty;
2839       Call   := Empty;
2840    end Find_Actual;
2841
2842    -------------------------------------
2843    -- Find_Corresponding_Discriminant --
2844    -------------------------------------
2845
2846    function Find_Corresponding_Discriminant
2847      (Id  : Node_Id;
2848       Typ : Entity_Id) return Entity_Id
2849    is
2850       Par_Disc : Entity_Id;
2851       Old_Disc : Entity_Id;
2852       New_Disc : Entity_Id;
2853
2854    begin
2855       Par_Disc := Original_Record_Component (Original_Discriminant (Id));
2856
2857       --  The original type may currently be private, and the discriminant
2858       --  only appear on its full view.
2859
2860       if Is_Private_Type (Scope (Par_Disc))
2861         and then not Has_Discriminants (Scope (Par_Disc))
2862         and then Present (Full_View (Scope (Par_Disc)))
2863       then
2864          Old_Disc := First_Discriminant (Full_View (Scope (Par_Disc)));
2865       else
2866          Old_Disc := First_Discriminant (Scope (Par_Disc));
2867       end if;
2868
2869       if Is_Class_Wide_Type (Typ) then
2870          New_Disc := First_Discriminant (Root_Type (Typ));
2871       else
2872          New_Disc := First_Discriminant (Typ);
2873       end if;
2874
2875       while Present (Old_Disc) and then Present (New_Disc) loop
2876          if Old_Disc = Par_Disc  then
2877             return New_Disc;
2878          else
2879             Next_Discriminant (Old_Disc);
2880             Next_Discriminant (New_Disc);
2881          end if;
2882       end loop;
2883
2884       --  Should always find it
2885
2886       raise Program_Error;
2887    end Find_Corresponding_Discriminant;
2888
2889    --------------------------
2890    -- Find_Overlaid_Object --
2891    --------------------------
2892
2893    function Find_Overlaid_Object (N : Node_Id) return Entity_Id is
2894       Expr  : Node_Id;
2895
2896    begin
2897       --  We are looking for one of the two following forms:
2898
2899       --    for X'Address use Y'Address
2900
2901       --  or
2902
2903       --    Const : constant Address := expr;
2904       --    ...
2905       --    for X'Address use Const;
2906
2907       --  In the second case, the expr is either Y'Address, or recursively a
2908       --  constant that eventually references Y'Address.
2909
2910       if Nkind (N) = N_Attribute_Definition_Clause
2911         and then Chars (N) = Name_Address
2912       then
2913          --  This loop checks the form of the expression for Y'Address where Y
2914          --  is an object entity name. The first loop checks the original
2915          --  expression in the attribute definition clause. Subsequent loops
2916          --  check referenced constants.
2917
2918          Expr := Expression (N);
2919          loop
2920             --  Check for Y'Address where Y is an object entity
2921
2922             if Nkind (Expr) = N_Attribute_Reference
2923               and then Attribute_Name (Expr) = Name_Address
2924               and then Is_Entity_Name (Prefix (Expr))
2925               and then Is_Object (Entity (Prefix (Expr)))
2926             then
2927                return Entity (Prefix (Expr));
2928
2929                --  Check for Const where Const is a constant entity
2930
2931             elsif Is_Entity_Name (Expr)
2932               and then Ekind (Entity (Expr)) = E_Constant
2933             then
2934                Expr := Constant_Value (Entity (Expr));
2935
2936             --  Anything else does not need checking
2937
2938             else
2939                exit;
2940             end if;
2941          end loop;
2942       end if;
2943
2944       return Empty;
2945    end Find_Overlaid_Object;
2946
2947    -------------------------
2948    -- Find_Parameter_Type --
2949    -------------------------
2950
2951    function Find_Parameter_Type (Param : Node_Id) return Entity_Id is
2952    begin
2953       if Nkind (Param) /= N_Parameter_Specification then
2954          return Empty;
2955
2956       --  For an access parameter, obtain the type from the formal entity
2957       --  itself, because access to subprogram nodes do not carry a type.
2958       --  Shouldn't we always use the formal entity ???
2959
2960       elsif Nkind (Parameter_Type (Param)) = N_Access_Definition then
2961          return Etype (Defining_Identifier (Param));
2962
2963       else
2964          return Etype (Parameter_Type (Param));
2965       end if;
2966    end Find_Parameter_Type;
2967
2968    -----------------------------
2969    -- Find_Static_Alternative --
2970    -----------------------------
2971
2972    function Find_Static_Alternative (N : Node_Id) return Node_Id is
2973       Expr   : constant Node_Id := Expression (N);
2974       Val    : constant Uint    := Expr_Value (Expr);
2975       Alt    : Node_Id;
2976       Choice : Node_Id;
2977
2978    begin
2979       Alt := First (Alternatives (N));
2980
2981       Search : loop
2982          if Nkind (Alt) /= N_Pragma then
2983             Choice := First (Discrete_Choices (Alt));
2984             while Present (Choice) loop
2985
2986                --  Others choice, always matches
2987
2988                if Nkind (Choice) = N_Others_Choice then
2989                   exit Search;
2990
2991                --  Range, check if value is in the range
2992
2993                elsif Nkind (Choice) = N_Range then
2994                   exit Search when
2995                     Val >= Expr_Value (Low_Bound (Choice))
2996                       and then
2997                     Val <= Expr_Value (High_Bound (Choice));
2998
2999                --  Choice is a subtype name. Note that we know it must
3000                --  be a static subtype, since otherwise it would have
3001                --  been diagnosed as illegal.
3002
3003                elsif Is_Entity_Name (Choice)
3004                  and then Is_Type (Entity (Choice))
3005                then
3006                   exit Search when Is_In_Range (Expr, Etype (Choice),
3007                                                 Assume_Valid => False);
3008
3009                --  Choice is a subtype indication
3010
3011                elsif Nkind (Choice) = N_Subtype_Indication then
3012                   declare
3013                      C : constant Node_Id := Constraint (Choice);
3014                      R : constant Node_Id := Range_Expression (C);
3015
3016                   begin
3017                      exit Search when
3018                        Val >= Expr_Value (Low_Bound (R))
3019                          and then
3020                        Val <= Expr_Value (High_Bound (R));
3021                   end;
3022
3023                --  Choice is a simple expression
3024
3025                else
3026                   exit Search when Val = Expr_Value (Choice);
3027                end if;
3028
3029                Next (Choice);
3030             end loop;
3031          end if;
3032
3033          Next (Alt);
3034          pragma Assert (Present (Alt));
3035       end loop Search;
3036
3037       --  The above loop *must* terminate by finding a match, since
3038       --  we know the case statement is valid, and the value of the
3039       --  expression is known at compile time. When we fall out of
3040       --  the loop, Alt points to the alternative that we know will
3041       --  be selected at run time.
3042
3043       return Alt;
3044    end Find_Static_Alternative;
3045
3046    ------------------
3047    -- First_Actual --
3048    ------------------
3049
3050    function First_Actual (Node : Node_Id) return Node_Id is
3051       N : Node_Id;
3052
3053    begin
3054       if No (Parameter_Associations (Node)) then
3055          return Empty;
3056       end if;
3057
3058       N := First (Parameter_Associations (Node));
3059
3060       if Nkind (N) = N_Parameter_Association then
3061          return First_Named_Actual (Node);
3062       else
3063          return N;
3064       end if;
3065    end First_Actual;
3066
3067    -------------------------
3068    -- Full_Qualified_Name --
3069    -------------------------
3070
3071    function Full_Qualified_Name (E : Entity_Id) return String_Id is
3072       Res : String_Id;
3073       pragma Warnings (Off, Res);
3074
3075       function Internal_Full_Qualified_Name (E : Entity_Id) return String_Id;
3076       --  Compute recursively the qualified name without NUL at the end
3077
3078       ----------------------------------
3079       -- Internal_Full_Qualified_Name --
3080       ----------------------------------
3081
3082       function Internal_Full_Qualified_Name (E : Entity_Id) return String_Id is
3083          Ent         : Entity_Id := E;
3084          Parent_Name : String_Id := No_String;
3085
3086       begin
3087          --  Deals properly with child units
3088
3089          if Nkind (Ent) = N_Defining_Program_Unit_Name then
3090             Ent := Defining_Identifier (Ent);
3091          end if;
3092
3093          --  Compute qualification recursively (only "Standard" has no scope)
3094
3095          if Present (Scope (Scope (Ent))) then
3096             Parent_Name := Internal_Full_Qualified_Name (Scope (Ent));
3097          end if;
3098
3099          --  Every entity should have a name except some expanded blocks
3100          --  don't bother about those.
3101
3102          if Chars (Ent) = No_Name then
3103             return Parent_Name;
3104          end if;
3105
3106          --  Add a period between Name and qualification
3107
3108          if Parent_Name /= No_String then
3109             Start_String (Parent_Name);
3110             Store_String_Char (Get_Char_Code ('.'));
3111
3112          else
3113             Start_String;
3114          end if;
3115
3116          --  Generates the entity name in upper case
3117
3118          Get_Decoded_Name_String (Chars (Ent));
3119          Set_All_Upper_Case;
3120          Store_String_Chars (Name_Buffer (1 .. Name_Len));
3121          return End_String;
3122       end Internal_Full_Qualified_Name;
3123
3124    --  Start of processing for Full_Qualified_Name
3125
3126    begin
3127       Res := Internal_Full_Qualified_Name (E);
3128       Store_String_Char (Get_Char_Code (ASCII.NUL));
3129       return End_String;
3130    end Full_Qualified_Name;
3131
3132    -----------------------
3133    -- Gather_Components --
3134    -----------------------
3135
3136    procedure Gather_Components
3137      (Typ           : Entity_Id;
3138       Comp_List     : Node_Id;
3139       Governed_By   : List_Id;
3140       Into          : Elist_Id;
3141       Report_Errors : out Boolean)
3142    is
3143       Assoc           : Node_Id;
3144       Variant         : Node_Id;
3145       Discrete_Choice : Node_Id;
3146       Comp_Item       : Node_Id;
3147
3148       Discrim       : Entity_Id;
3149       Discrim_Name  : Node_Id;
3150       Discrim_Value : Node_Id;
3151
3152    begin
3153       Report_Errors := False;
3154
3155       if No (Comp_List) or else Null_Present (Comp_List) then
3156          return;
3157
3158       elsif Present (Component_Items (Comp_List)) then
3159          Comp_Item := First (Component_Items (Comp_List));
3160
3161       else
3162          Comp_Item := Empty;
3163       end if;
3164
3165       while Present (Comp_Item) loop
3166
3167          --  Skip the tag of a tagged record, the interface tags, as well
3168          --  as all items that are not user components (anonymous types,
3169          --  rep clauses, Parent field, controller field).
3170
3171          if Nkind (Comp_Item) = N_Component_Declaration then
3172             declare
3173                Comp : constant Entity_Id := Defining_Identifier (Comp_Item);
3174             begin
3175                if not Is_Tag (Comp)
3176                  and then Chars (Comp) /= Name_uParent
3177                  and then Chars (Comp) /= Name_uController
3178                then
3179                   Append_Elmt (Comp, Into);
3180                end if;
3181             end;
3182          end if;
3183
3184          Next (Comp_Item);
3185       end loop;
3186
3187       if No (Variant_Part (Comp_List)) then
3188          return;
3189       else
3190          Discrim_Name := Name (Variant_Part (Comp_List));
3191          Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
3192       end if;
3193
3194       --  Look for the discriminant that governs this variant part.
3195       --  The discriminant *must* be in the Governed_By List
3196
3197       Assoc := First (Governed_By);
3198       Find_Constraint : loop
3199          Discrim := First (Choices (Assoc));
3200          exit Find_Constraint when Chars (Discrim_Name) = Chars (Discrim)
3201            or else (Present (Corresponding_Discriminant (Entity (Discrim)))
3202                       and then
3203                     Chars (Corresponding_Discriminant (Entity (Discrim)))
3204                          = Chars  (Discrim_Name))
3205            or else Chars (Original_Record_Component (Entity (Discrim)))
3206                          = Chars (Discrim_Name);
3207
3208          if No (Next (Assoc)) then
3209             if not Is_Constrained (Typ)
3210               and then Is_Derived_Type (Typ)
3211               and then Present (Stored_Constraint (Typ))
3212             then
3213                --  If the type is a tagged type with inherited discriminants,
3214                --  use the stored constraint on the parent in order to find
3215                --  the values of discriminants that are otherwise hidden by an
3216                --  explicit constraint. Renamed discriminants are handled in
3217                --  the code above.
3218
3219                --  If several parent discriminants are renamed by a single
3220                --  discriminant of the derived type, the call to obtain the
3221                --  Corresponding_Discriminant field only retrieves the last
3222                --  of them. We recover the constraint on the others from the
3223                --  Stored_Constraint as well.
3224
3225                declare
3226                   D : Entity_Id;
3227                   C : Elmt_Id;
3228
3229                begin
3230                   D := First_Discriminant (Etype (Typ));
3231                   C := First_Elmt (Stored_Constraint (Typ));
3232                   while Present (D) and then Present (C) loop
3233                      if Chars (Discrim_Name) = Chars (D) then
3234                         if Is_Entity_Name (Node (C))
3235                           and then Entity (Node (C)) = Entity (Discrim)
3236                         then
3237                            --  D is renamed by Discrim, whose value is given in
3238                            --  Assoc.
3239
3240                            null;
3241
3242                         else
3243                            Assoc :=
3244                              Make_Component_Association (Sloc (Typ),
3245                                New_List
3246                                  (New_Occurrence_Of (D, Sloc (Typ))),
3247                                   Duplicate_Subexpr_No_Checks (Node (C)));
3248                         end if;
3249                         exit Find_Constraint;
3250                      end if;
3251
3252                      Next_Discriminant (D);
3253                      Next_Elmt (C);
3254                   end loop;
3255                end;
3256             end if;
3257          end if;
3258
3259          if No (Next (Assoc)) then
3260             Error_Msg_NE (" missing value for discriminant&",
3261               First (Governed_By), Discrim_Name);
3262             Report_Errors := True;
3263             return;
3264          end if;
3265
3266          Next (Assoc);
3267       end loop Find_Constraint;
3268
3269       Discrim_Value := Expression (Assoc);
3270
3271       if not Is_OK_Static_Expression (Discrim_Value) then
3272          Error_Msg_FE
3273            ("value for discriminant & must be static!",
3274             Discrim_Value, Discrim);
3275          Why_Not_Static (Discrim_Value);
3276          Report_Errors := True;
3277          return;
3278       end if;
3279
3280       Search_For_Discriminant_Value : declare
3281          Low  : Node_Id;
3282          High : Node_Id;
3283
3284          UI_High          : Uint;
3285          UI_Low           : Uint;
3286          UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
3287
3288       begin
3289          Find_Discrete_Value : while Present (Variant) loop
3290             Discrete_Choice := First (Discrete_Choices (Variant));
3291             while Present (Discrete_Choice) loop
3292
3293                exit Find_Discrete_Value when
3294                  Nkind (Discrete_Choice) = N_Others_Choice;
3295
3296                Get_Index_Bounds (Discrete_Choice, Low, High);
3297
3298                UI_Low  := Expr_Value (Low);
3299                UI_High := Expr_Value (High);
3300
3301                exit Find_Discrete_Value when
3302                  UI_Low <= UI_Discrim_Value
3303                    and then
3304                  UI_High >= UI_Discrim_Value;
3305
3306                Next (Discrete_Choice);
3307             end loop;
3308
3309             Next_Non_Pragma (Variant);
3310          end loop Find_Discrete_Value;
3311       end Search_For_Discriminant_Value;
3312
3313       if No (Variant) then
3314          Error_Msg_NE
3315            ("value of discriminant & is out of range", Discrim_Value, Discrim);
3316          Report_Errors := True;
3317          return;
3318       end  if;
3319
3320       --  If we have found the corresponding choice, recursively add its
3321       --  components to the Into list.
3322
3323       Gather_Components (Empty,
3324         Component_List (Variant), Governed_By, Into, Report_Errors);
3325    end Gather_Components;
3326
3327    ------------------------
3328    -- Get_Actual_Subtype --
3329    ------------------------
3330
3331    function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
3332       Typ  : constant Entity_Id := Etype (N);
3333       Utyp : Entity_Id := Underlying_Type (Typ);
3334       Decl : Node_Id;
3335       Atyp : Entity_Id;
3336
3337    begin
3338       if No (Utyp) then
3339          Utyp := Typ;
3340       end if;
3341
3342       --  If what we have is an identifier that references a subprogram
3343       --  formal, or a variable or constant object, then we get the actual
3344       --  subtype from the referenced entity if one has been built.
3345
3346       if Nkind (N) = N_Identifier
3347         and then
3348           (Is_Formal (Entity (N))
3349             or else Ekind (Entity (N)) = E_Constant
3350             or else Ekind (Entity (N)) = E_Variable)
3351         and then Present (Actual_Subtype (Entity (N)))
3352       then
3353          return Actual_Subtype (Entity (N));
3354
3355       --  Actual subtype of unchecked union is always itself. We never need
3356       --  the "real" actual subtype. If we did, we couldn't get it anyway
3357       --  because the discriminant is not available. The restrictions on
3358       --  Unchecked_Union are designed to make sure that this is OK.
3359
3360       elsif Is_Unchecked_Union (Base_Type (Utyp)) then
3361          return Typ;
3362
3363       --  Here for the unconstrained case, we must find actual subtype
3364       --  No actual subtype is available, so we must build it on the fly.
3365
3366       --  Checking the type, not the underlying type, for constrainedness
3367       --  seems to be necessary. Maybe all the tests should be on the type???
3368
3369       elsif (not Is_Constrained (Typ))
3370            and then (Is_Array_Type (Utyp)
3371                       or else (Is_Record_Type (Utyp)
3372                                 and then Has_Discriminants (Utyp)))
3373            and then not Has_Unknown_Discriminants (Utyp)
3374            and then not (Ekind (Utyp) = E_String_Literal_Subtype)
3375       then
3376          --  Nothing to do if in spec expression (why not???)
3377
3378          if In_Spec_Expression then
3379             return Typ;
3380
3381          elsif Is_Private_Type (Typ)
3382            and then not Has_Discriminants (Typ)
3383          then
3384             --  If the type has no discriminants, there is no subtype to
3385             --  build, even if the underlying type is discriminated.
3386
3387             return Typ;
3388
3389          --  Else build the actual subtype
3390
3391          else
3392             Decl := Build_Actual_Subtype (Typ, N);
3393             Atyp := Defining_Identifier (Decl);
3394
3395             --  If Build_Actual_Subtype generated a new declaration then use it
3396
3397             if Atyp /= Typ then
3398
3399                --  The actual subtype is an Itype, so analyze the declaration,
3400                --  but do not attach it to the tree, to get the type defined.
3401
3402                Set_Parent (Decl, N);
3403                Set_Is_Itype (Atyp);
3404                Analyze (Decl, Suppress => All_Checks);
3405                Set_Associated_Node_For_Itype (Atyp, N);
3406                Set_Has_Delayed_Freeze (Atyp, False);
3407
3408                --  We need to freeze the actual subtype immediately. This is
3409                --  needed, because otherwise this Itype will not get frozen
3410                --  at all, and it is always safe to freeze on creation because
3411                --  any associated types must be frozen at this point.
3412
3413                Freeze_Itype (Atyp, N);
3414                return Atyp;
3415
3416             --  Otherwise we did not build a declaration, so return original
3417
3418             else
3419                return Typ;
3420             end if;
3421          end if;
3422
3423       --  For all remaining cases, the actual subtype is the same as
3424       --  the nominal type.
3425
3426       else
3427          return Typ;
3428       end if;
3429    end Get_Actual_Subtype;
3430
3431    -------------------------------------
3432    -- Get_Actual_Subtype_If_Available --
3433    -------------------------------------
3434
3435    function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
3436       Typ  : constant Entity_Id := Etype (N);
3437
3438    begin
3439       --  If what we have is an identifier that references a subprogram
3440       --  formal, or a variable or constant object, then we get the actual
3441       --  subtype from the referenced entity if one has been built.
3442
3443       if Nkind (N) = N_Identifier
3444         and then
3445           (Is_Formal (Entity (N))
3446             or else Ekind (Entity (N)) = E_Constant
3447             or else Ekind (Entity (N)) = E_Variable)
3448         and then Present (Actual_Subtype (Entity (N)))
3449       then
3450          return Actual_Subtype (Entity (N));
3451
3452       --  Otherwise the Etype of N is returned unchanged
3453
3454       else
3455          return Typ;
3456       end if;
3457    end Get_Actual_Subtype_If_Available;
3458
3459    -------------------------------
3460    -- Get_Default_External_Name --
3461    -------------------------------
3462
3463    function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
3464    begin
3465       Get_Decoded_Name_String (Chars (E));
3466
3467       if Opt.External_Name_Imp_Casing = Uppercase then
3468          Set_Casing (All_Upper_Case);
3469       else
3470          Set_Casing (All_Lower_Case);
3471       end if;
3472
3473       return
3474         Make_String_Literal (Sloc (E),
3475           Strval => String_From_Name_Buffer);
3476    end Get_Default_External_Name;
3477
3478    ---------------------------
3479    -- Get_Enum_Lit_From_Pos --
3480    ---------------------------
3481
3482    function Get_Enum_Lit_From_Pos
3483      (T   : Entity_Id;
3484       Pos : Uint;
3485       Loc : Source_Ptr) return Node_Id
3486    is
3487       Lit : Node_Id;
3488
3489    begin
3490       --  In the case where the literal is of type Character, Wide_Character
3491       --  or Wide_Wide_Character or of a type derived from them, there needs
3492       --  to be some special handling since there is no explicit chain of
3493       --  literals to search. Instead, an N_Character_Literal node is created
3494       --  with the appropriate Char_Code and Chars fields.
3495
3496       if Is_Standard_Character_Type (T) then
3497          Set_Character_Literal_Name (UI_To_CC (Pos));
3498          return
3499            Make_Character_Literal (Loc,
3500              Chars              => Name_Find,
3501              Char_Literal_Value => Pos);
3502
3503       --  For all other cases, we have a complete table of literals, and
3504       --  we simply iterate through the chain of literal until the one
3505       --  with the desired position value is found.
3506       --
3507
3508       else
3509          Lit := First_Literal (Base_Type (T));
3510          for J in 1 .. UI_To_Int (Pos) loop
3511             Next_Literal (Lit);
3512          end loop;
3513
3514          return New_Occurrence_Of (Lit, Loc);
3515       end if;
3516    end Get_Enum_Lit_From_Pos;
3517
3518    ------------------------
3519    -- Get_Generic_Entity --
3520    ------------------------
3521
3522    function Get_Generic_Entity (N : Node_Id) return Entity_Id is
3523       Ent : constant Entity_Id := Entity (Name (N));
3524    begin
3525       if Present (Renamed_Object (Ent)) then
3526          return Renamed_Object (Ent);
3527       else
3528          return Ent;
3529       end if;
3530    end Get_Generic_Entity;
3531
3532    ----------------------
3533    -- Get_Index_Bounds --
3534    ----------------------
3535
3536    procedure Get_Index_Bounds (N : Node_Id; L, H : out Node_Id) is
3537       Kind : constant Node_Kind := Nkind (N);
3538       R    : Node_Id;
3539
3540    begin
3541       if Kind = N_Range then
3542          L := Low_Bound (N);
3543          H := High_Bound (N);
3544
3545       elsif Kind = N_Subtype_Indication then
3546          R := Range_Expression (Constraint (N));
3547
3548          if R = Error then
3549             L := Error;
3550             H := Error;
3551             return;
3552
3553          else
3554             L := Low_Bound  (Range_Expression (Constraint (N)));
3555             H := High_Bound (Range_Expression (Constraint (N)));
3556          end if;
3557
3558       elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
3559          if Error_Posted (Scalar_Range (Entity (N))) then
3560             L := Error;
3561             H := Error;
3562
3563          elsif Nkind (Scalar_Range (Entity (N))) = N_Subtype_Indication then
3564             Get_Index_Bounds (Scalar_Range (Entity (N)), L, H);
3565
3566          else
3567             L := Low_Bound  (Scalar_Range (Entity (N)));
3568             H := High_Bound (Scalar_Range (Entity (N)));
3569          end if;
3570
3571       else
3572          --  N is an expression, indicating a range with one value
3573
3574          L := N;
3575          H := N;
3576       end if;
3577    end Get_Index_Bounds;
3578
3579    ----------------------------------
3580    -- Get_Library_Unit_Name_string --
3581    ----------------------------------
3582
3583    procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
3584       Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
3585
3586    begin
3587       Get_Unit_Name_String (Unit_Name_Id);
3588
3589       --  Remove seven last character (" (spec)" or " (body)")
3590
3591       Name_Len := Name_Len - 7;
3592       pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
3593    end Get_Library_Unit_Name_String;
3594
3595    ------------------------
3596    -- Get_Name_Entity_Id --
3597    ------------------------
3598
3599    function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
3600    begin
3601       return Entity_Id (Get_Name_Table_Info (Id));
3602    end Get_Name_Entity_Id;
3603
3604    -------------------
3605    -- Get_Pragma_Id --
3606    -------------------
3607
3608    function Get_Pragma_Id (N : Node_Id) return Pragma_Id is
3609    begin
3610       return Get_Pragma_Id (Pragma_Name (N));
3611    end Get_Pragma_Id;
3612
3613    ---------------------------
3614    -- Get_Referenced_Object --
3615    ---------------------------
3616
3617    function Get_Referenced_Object (N : Node_Id) return Node_Id is
3618       R : Node_Id;
3619
3620    begin
3621       R := N;
3622       while Is_Entity_Name (R)
3623         and then Present (Renamed_Object (Entity (R)))
3624       loop
3625          R := Renamed_Object (Entity (R));
3626       end loop;
3627
3628       return R;
3629    end Get_Referenced_Object;
3630
3631    ------------------------
3632    -- Get_Renamed_Entity --
3633    ------------------------
3634
3635    function Get_Renamed_Entity (E : Entity_Id) return Entity_Id is
3636       R : Entity_Id;
3637
3638    begin
3639       R := E;
3640       while Present (Renamed_Entity (R)) loop
3641          R := Renamed_Entity (R);
3642       end loop;
3643
3644       return R;
3645    end Get_Renamed_Entity;
3646
3647    -------------------------
3648    -- Get_Subprogram_Body --
3649    -------------------------
3650
3651    function Get_Subprogram_Body (E : Entity_Id) return Node_Id is
3652       Decl : Node_Id;
3653
3654    begin
3655       Decl := Unit_Declaration_Node (E);
3656
3657       if Nkind (Decl) = N_Subprogram_Body then
3658          return Decl;
3659
3660       --  The below comment is bad, because it is possible for
3661       --  Nkind (Decl) to be an N_Subprogram_Body_Stub ???
3662
3663       else           --  Nkind (Decl) = N_Subprogram_Declaration
3664
3665          if Present (Corresponding_Body (Decl)) then
3666             return Unit_Declaration_Node (Corresponding_Body (Decl));
3667
3668          --  Imported subprogram case
3669
3670          else
3671             return Empty;
3672          end if;
3673       end if;
3674    end Get_Subprogram_Body;
3675
3676    ---------------------------
3677    -- Get_Subprogram_Entity --
3678    ---------------------------
3679
3680    function Get_Subprogram_Entity (Nod : Node_Id) return Entity_Id is
3681       Nam  : Node_Id;
3682       Proc : Entity_Id;
3683
3684    begin
3685       if Nkind (Nod) = N_Accept_Statement then
3686          Nam := Entry_Direct_Name (Nod);
3687
3688       --  For an entry call, the prefix of the call is a selected component.
3689       --  Need additional code for internal calls ???
3690
3691       elsif Nkind (Nod) = N_Entry_Call_Statement then
3692          if Nkind (Name (Nod)) = N_Selected_Component then
3693             Nam := Entity (Selector_Name (Name (Nod)));
3694          else
3695             Nam := Empty;
3696          end if;
3697
3698       else
3699          Nam := Name (Nod);
3700       end if;
3701
3702       if Nkind (Nam) = N_Explicit_Dereference then
3703          Proc := Etype (Prefix (Nam));
3704       elsif Is_Entity_Name (Nam) then
3705          Proc := Entity (Nam);
3706       else
3707          return Empty;
3708       end if;
3709
3710       if Is_Object (Proc) then
3711          Proc := Etype (Proc);
3712       end if;
3713
3714       if Ekind (Proc) = E_Access_Subprogram_Type then
3715          Proc := Directly_Designated_Type (Proc);
3716       end if;
3717
3718       if not Is_Subprogram (Proc)
3719         and then Ekind (Proc) /= E_Subprogram_Type
3720       then
3721          return Empty;
3722       else
3723          return Proc;
3724       end if;
3725    end Get_Subprogram_Entity;
3726
3727    -----------------------------
3728    -- Get_Task_Body_Procedure --
3729    -----------------------------
3730
3731    function Get_Task_Body_Procedure (E : Entity_Id) return Node_Id is
3732    begin
3733       --  Note: A task type may be the completion of a private type with
3734       --  discriminants. When performing elaboration checks on a task
3735       --  declaration, the current view of the type may be the private one,
3736       --  and the procedure that holds the body of the task is held in its
3737       --  underlying type.
3738
3739       --  This is an odd function, why not have Task_Body_Procedure do
3740       --  the following digging???
3741
3742       return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
3743    end Get_Task_Body_Procedure;
3744
3745    -----------------------
3746    -- Has_Access_Values --
3747    -----------------------
3748
3749    function Has_Access_Values (T : Entity_Id) return Boolean is
3750       Typ : constant Entity_Id := Underlying_Type (T);
3751
3752    begin
3753       --  Case of a private type which is not completed yet. This can only
3754       --  happen in the case of a generic format type appearing directly, or
3755       --  as a component of the type to which this function is being applied
3756       --  at the top level. Return False in this case, since we certainly do
3757       --  not know that the type contains access types.
3758
3759       if No (Typ) then
3760          return False;
3761
3762       elsif Is_Access_Type (Typ) then
3763          return True;
3764
3765       elsif Is_Array_Type (Typ) then
3766          return Has_Access_Values (Component_Type (Typ));
3767
3768       elsif Is_Record_Type (Typ) then
3769          declare
3770             Comp : Entity_Id;
3771
3772          begin
3773             --  Loop to Check components
3774
3775             Comp := First_Component_Or_Discriminant (Typ);
3776             while Present (Comp) loop
3777
3778                --  Check for access component, tag field does not count, even
3779                --  though it is implemented internally using an access type.
3780
3781                if Has_Access_Values (Etype (Comp))
3782                  and then Chars (Comp) /= Name_uTag
3783                then
3784                   return True;
3785                end if;
3786
3787                Next_Component_Or_Discriminant (Comp);
3788             end loop;
3789          end;
3790
3791          return False;
3792
3793       else
3794          return False;
3795       end if;
3796    end Has_Access_Values;
3797
3798    ------------------------------
3799    -- Has_Compatible_Alignment --
3800    ------------------------------
3801
3802    function Has_Compatible_Alignment
3803      (Obj  : Entity_Id;
3804       Expr : Node_Id) return Alignment_Result
3805    is
3806       function Has_Compatible_Alignment_Internal
3807         (Obj     : Entity_Id;
3808          Expr    : Node_Id;
3809          Default : Alignment_Result) return Alignment_Result;
3810       --  This is the internal recursive function that actually does the work.
3811       --  There is one additional parameter, which says what the result should
3812       --  be if no alignment information is found, and there is no definite
3813       --  indication of compatible alignments. At the outer level, this is set
3814       --  to Unknown, but for internal recursive calls in the case where types
3815       --  are known to be correct, it is set to Known_Compatible.
3816
3817       ---------------------------------------
3818       -- Has_Compatible_Alignment_Internal --
3819       ---------------------------------------
3820
3821       function Has_Compatible_Alignment_Internal
3822         (Obj     : Entity_Id;
3823          Expr    : Node_Id;
3824          Default : Alignment_Result) return Alignment_Result
3825       is
3826          Result : Alignment_Result := Known_Compatible;
3827          --  Set to result if Problem_Prefix or Problem_Offset returns True.
3828          --  Note that once a value of Known_Incompatible is set, it is sticky
3829          --  and does not get changed to Unknown (the value in Result only gets
3830          --  worse as we go along, never better).
3831
3832          procedure Check_Offset (Offs : Uint);
3833          --  Called when Expr is a selected or indexed component with Offs set
3834          --  to resp Component_First_Bit or Component_Size. Checks that if the
3835          --  offset is specified it is compatible with the object alignment
3836          --  requirements. The value in Result is modified accordingly.
3837
3838          procedure Check_Prefix;
3839          --  Checks the prefix recursively in the case where the expression
3840          --  is an indexed or selected component.
3841
3842          procedure Set_Result (R : Alignment_Result);
3843          --  If R represents a worse outcome (unknown instead of known
3844          --  compatible, or known incompatible), then set Result to R.
3845
3846          ------------------
3847          -- Check_Offset --
3848          ------------------
3849
3850          procedure Check_Offset (Offs : Uint) is
3851          begin
3852             --  Unspecified or zero offset is always OK
3853
3854             if Offs = No_Uint or else Offs = Uint_0 then
3855                null;
3856
3857             --  If we do not know required alignment, any non-zero offset is
3858             --  a potential problem (but certainly may be OK, so result is
3859             --  unknown).
3860
3861             elsif Unknown_Alignment (Obj) then
3862                Set_Result (Unknown);
3863
3864             --  If we know the required alignment, see if offset is compatible
3865
3866             else
3867                if Offs mod (System_Storage_Unit * Alignment (Obj)) /= 0 then
3868                   Set_Result (Known_Incompatible);
3869                end if;
3870             end if;
3871          end Check_Offset;
3872
3873          ------------------
3874          -- Check_Prefix --
3875          ------------------
3876
3877          procedure Check_Prefix is
3878          begin
3879             --  The subtlety here is that in doing a recursive call to check
3880             --  the prefix, we have to decide what to do in the case where we
3881             --  don't find any specific indication of an alignment problem.
3882
3883             --  At the outer level, we normally set Unknown as the result in
3884             --  this case, since we can only set Known_Compatible if we really
3885             --  know that the alignment value is OK, but for the recursive
3886             --  call, in the case where the types match, and we have not
3887             --  specified a peculiar alignment for the object, we are only
3888             --  concerned about suspicious rep clauses, the default case does
3889             --  not affect us, since the compiler will, in the absence of such
3890             --  rep clauses, ensure that the alignment is correct.
3891
3892             if Default = Known_Compatible
3893               or else
3894                 (Etype (Obj) = Etype (Expr)
3895                   and then (Unknown_Alignment (Obj)
3896                              or else
3897                                Alignment (Obj) = Alignment (Etype (Obj))))
3898             then
3899                Set_Result
3900                  (Has_Compatible_Alignment_Internal
3901                     (Obj, Prefix (Expr), Known_Compatible));
3902
3903             --  In all other cases, we need a full check on the prefix
3904
3905             else
3906                Set_Result
3907                  (Has_Compatible_Alignment_Internal
3908                     (Obj, Prefix (Expr), Unknown));
3909             end if;
3910          end Check_Prefix;
3911
3912          ----------------
3913          -- Set_Result --
3914          ----------------
3915
3916          procedure Set_Result (R : Alignment_Result) is
3917          begin
3918             if R > Result then
3919                Result := R;
3920             end if;
3921          end Set_Result;
3922
3923       --  Start of processing for Has_Compatible_Alignment_Internal
3924
3925       begin
3926          --  If Expr is a selected component, we must make sure there is no
3927          --  potentially troublesome component clause, and that the record is
3928          --  not packed.
3929
3930          if Nkind (Expr) = N_Selected_Component then
3931
3932             --  Packed record always generate unknown alignment
3933
3934             if Is_Packed (Etype (Prefix (Expr))) then
3935                Set_Result (Unknown);
3936             end if;
3937
3938             --  Check possible bad component offset and check prefix
3939
3940             Check_Offset
3941               (Component_Bit_Offset (Entity (Selector_Name (Expr))));
3942             Check_Prefix;
3943
3944          --  If Expr is an indexed component, we must make sure there is no
3945          --  potentially troublesome Component_Size clause and that the array
3946          --  is not bit-packed.
3947
3948          elsif Nkind (Expr) = N_Indexed_Component then
3949
3950             --  Bit packed array always generates unknown alignment
3951
3952             if Is_Bit_Packed_Array (Etype (Prefix (Expr))) then
3953                Set_Result (Unknown);
3954             end if;
3955
3956             --  Check possible bad component size and check prefix
3957
3958             Check_Offset (Component_Size (Etype (Prefix (Expr))));
3959             Check_Prefix;
3960          end if;
3961
3962          --  Case where we know the alignment of the object
3963
3964          if Known_Alignment (Obj) then
3965             declare
3966                ObjA : constant Uint := Alignment (Obj);
3967                ExpA : Uint := No_Uint;
3968                SizA : Uint := No_Uint;
3969
3970             begin
3971                --  If alignment of Obj is 1, then we are always OK
3972
3973                if ObjA = 1 then
3974                   Set_Result (Known_Compatible);
3975
3976                --  Alignment of Obj is greater than 1, so we need to check
3977
3978                else
3979                   --  See if Expr is an object with known alignment
3980
3981                   if Is_Entity_Name (Expr)
3982                     and then Known_Alignment (Entity (Expr))
3983                   then
3984                      ExpA := Alignment (Entity (Expr));
3985
3986                      --  Otherwise, we can use the alignment of the type of
3987                      --  Expr given that we already checked for
3988                      --  discombobulating rep clauses for the cases of indexed
3989                      --  and selected components above.
3990
3991                   elsif Known_Alignment (Etype (Expr)) then
3992                      ExpA := Alignment (Etype (Expr));
3993                   end if;
3994
3995                   --  If we got an alignment, see if it is acceptable
3996
3997                   if ExpA /= No_Uint then
3998                      if ExpA < ObjA then
3999                         Set_Result (Known_Incompatible);
4000                      end if;
4001
4002                      --  Case of Expr alignment unknown
4003
4004                   else
4005                      Set_Result (Default);
4006                   end if;
4007
4008                   --  See if size is given. If so, check that it is not too
4009                   --  small for the required alignment.
4010                   --  See if Expr is an object with known alignment
4011
4012                   if Is_Entity_Name (Expr)
4013                     and then Known_Static_Esize (Entity (Expr))
4014                   then
4015                      SizA := Esize (Entity (Expr));
4016
4017                      --  Otherwise, we check the object size of the Expr type
4018
4019                   elsif Known_Static_Esize (Etype (Expr)) then
4020                      SizA := Esize (Etype (Expr));
4021                   end if;
4022
4023                   --  If we got a size, see if it is a multiple of the Obj
4024                   --  alignment, if not, then the alignment cannot be
4025                   --  acceptable, since the size is always a multiple of the
4026                   --  alignment.
4027
4028                   if SizA /= No_Uint then
4029                      if SizA mod (ObjA * Ttypes.System_Storage_Unit) /= 0 then
4030                         Set_Result (Known_Incompatible);
4031                      end if;
4032                   end if;
4033                end if;
4034             end;
4035
4036          --  If we can't find the result by direct comparison of alignment
4037          --  values, then there is still one case that we can determine known
4038          --  result, and that is when we can determine that the types are the
4039          --  same, and no alignments are specified. Then we known that the
4040          --  alignments are compatible, even if we don't know the alignment
4041          --  value in the front end.
4042
4043          elsif Etype (Obj) = Etype (Expr) then
4044
4045             --  Types are the same, but we have to check for possible size
4046             --  and alignments on the Expr object that may make the alignment
4047             --  different, even though the types are the same.
4048
4049             if Is_Entity_Name (Expr) then
4050
4051                --  First check alignment of the Expr object. Any alignment less
4052                --  than Maximum_Alignment is worrisome since this is the case
4053                --  where we do not know the alignment of Obj.
4054
4055                if Known_Alignment (Entity (Expr))
4056                  and then
4057                    UI_To_Int (Alignment (Entity (Expr)))
4058                                  < Ttypes.Maximum_Alignment
4059                then
4060                   Set_Result (Unknown);
4061
4062                   --  Now check size of Expr object. Any size that is not an
4063                   --  even multiple of Maximum_Alignment is also worrisome
4064                   --  since it may cause the alignment of the object to be less
4065                   --  than the alignment of the type.
4066
4067                elsif Known_Static_Esize (Entity (Expr))
4068                  and then
4069                    (UI_To_Int (Esize (Entity (Expr))) mod
4070                      (Ttypes.Maximum_Alignment * Ttypes.System_Storage_Unit))
4071                          /= 0
4072                then
4073                   Set_Result (Unknown);
4074
4075                   --  Otherwise same type is decisive
4076
4077                else
4078                   Set_Result (Known_Compatible);
4079                end if;
4080             end if;
4081
4082          --  Another case to deal with is when there is an explicit size or
4083          --  alignment clause when the types are not the same. If so, then the
4084          --  result is Unknown. We don't need to do this test if the Default is
4085          --  Unknown, since that result will be set in any case.
4086
4087          elsif Default /= Unknown
4088            and then (Has_Size_Clause (Etype (Expr))
4089                       or else
4090                      Has_Alignment_Clause (Etype (Expr)))
4091          then
4092             Set_Result (Unknown);
4093
4094          --  If no indication found, set default
4095
4096          else
4097             Set_Result (Default);
4098          end if;
4099
4100          --  Return worst result found
4101
4102          return Result;
4103       end Has_Compatible_Alignment_Internal;
4104
4105    --  Start of processing for Has_Compatible_Alignment
4106
4107    begin
4108       --  If Obj has no specified alignment, then set alignment from the type
4109       --  alignment. Perhaps we should always do this, but for sure we should
4110       --  do it when there is an address clause since we can do more if the
4111       --  alignment is known.
4112
4113       if Unknown_Alignment (Obj) then
4114          Set_Alignment (Obj, Alignment (Etype (Obj)));
4115       end if;
4116
4117       --  Now do the internal call that does all the work
4118
4119       return Has_Compatible_Alignment_Internal (Obj, Expr, Unknown);
4120    end Has_Compatible_Alignment;
4121
4122    ----------------------
4123    -- Has_Declarations --
4124    ----------------------
4125
4126    function Has_Declarations (N : Node_Id) return Boolean is
4127       K : constant Node_Kind := Nkind (N);
4128    begin
4129       return    K = N_Accept_Statement
4130         or else K = N_Block_Statement
4131         or else K = N_Compilation_Unit_Aux
4132         or else K = N_Entry_Body
4133         or else K = N_Package_Body
4134         or else K = N_Protected_Body
4135         or else K = N_Subprogram_Body
4136         or else K = N_Task_Body
4137         or else K = N_Package_Specification;
4138    end Has_Declarations;
4139
4140    -------------------------------------------
4141    -- Has_Discriminant_Dependent_Constraint --
4142    -------------------------------------------
4143
4144    function Has_Discriminant_Dependent_Constraint
4145      (Comp : Entity_Id) return Boolean
4146    is
4147       Comp_Decl  : constant Node_Id := Parent (Comp);
4148       Subt_Indic : constant Node_Id :=
4149                      Subtype_Indication (Component_Definition (Comp_Decl));
4150       Constr     : Node_Id;
4151       Assn       : Node_Id;
4152
4153    begin
4154       if Nkind (Subt_Indic) = N_Subtype_Indication then
4155          Constr := Constraint (Subt_Indic);
4156
4157          if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
4158             Assn := First (Constraints (Constr));
4159             while Present (Assn) loop
4160                case Nkind (Assn) is
4161                   when N_Subtype_Indication |
4162                        N_Range              |
4163                        N_Identifier
4164                   =>
4165                      if Depends_On_Discriminant (Assn) then
4166                         return True;
4167                      end if;
4168
4169                   when N_Discriminant_Association =>
4170                      if Depends_On_Discriminant (Expression (Assn)) then
4171                         return True;
4172                      end if;
4173
4174                   when others =>
4175                      null;
4176
4177                end case;
4178
4179                Next (Assn);
4180             end loop;
4181          end if;
4182       end if;
4183
4184       return False;
4185    end Has_Discriminant_Dependent_Constraint;
4186
4187    --------------------
4188    -- Has_Infinities --
4189    --------------------
4190
4191    function Has_Infinities (E : Entity_Id) return Boolean is
4192    begin
4193       return
4194         Is_Floating_Point_Type (E)
4195           and then Nkind (Scalar_Range (E)) = N_Range
4196           and then Includes_Infinities (Scalar_Range (E));
4197    end Has_Infinities;
4198
4199    --------------------
4200    -- Has_Interfaces --
4201    --------------------
4202
4203    function Has_Interfaces
4204      (T             : Entity_Id;
4205       Use_Full_View : Boolean := True) return Boolean
4206    is
4207       Typ : Entity_Id;
4208
4209    begin
4210       --  Handle concurrent types
4211
4212       if Is_Concurrent_Type (T) then
4213          Typ := Corresponding_Record_Type (T);
4214       else
4215          Typ := T;
4216       end if;
4217
4218       if not Present (Typ)
4219         or else not Is_Record_Type (Typ)
4220         or else not Is_Tagged_Type (Typ)
4221       then
4222          return False;
4223       end if;
4224
4225       --  Handle private types
4226
4227       if Use_Full_View
4228         and then Present (Full_View (Typ))
4229       then
4230          Typ := Full_View (Typ);
4231       end if;
4232
4233       --  Handle concurrent record types
4234
4235       if Is_Concurrent_Record_Type (Typ)
4236         and then Is_Non_Empty_List (Abstract_Interface_List (Typ))
4237       then
4238          return True;
4239       end if;
4240
4241       loop
4242          if Is_Interface (Typ)
4243            or else
4244              (Is_Record_Type (Typ)
4245                and then Present (Interfaces (Typ))
4246                and then not Is_Empty_Elmt_List (Interfaces (Typ)))
4247          then
4248             return True;
4249          end if;
4250
4251          exit when Etype (Typ) = Typ
4252
4253             --  Handle private types
4254
4255             or else (Present (Full_View (Etype (Typ)))
4256                        and then Full_View (Etype (Typ)) = Typ)
4257
4258             --  Protect the frontend against wrong source with cyclic
4259             --  derivations
4260
4261             or else Etype (Typ) = T;
4262
4263          --  Climb to the ancestor type handling private types
4264
4265          if Present (Full_View (Etype (Typ))) then
4266             Typ := Full_View (Etype (Typ));
4267          else
4268             Typ := Etype (Typ);
4269          end if;
4270       end loop;
4271
4272       return False;
4273    end Has_Interfaces;
4274
4275    ------------------------
4276    -- Has_Null_Exclusion --
4277    ------------------------
4278
4279    function Has_Null_Exclusion (N : Node_Id) return Boolean is
4280    begin
4281       case Nkind (N) is
4282          when N_Access_Definition               |
4283               N_Access_Function_Definition      |
4284               N_Access_Procedure_Definition     |
4285               N_Access_To_Object_Definition     |
4286               N_Allocator                       |
4287               N_Derived_Type_Definition         |
4288               N_Function_Specification          |
4289               N_Subtype_Declaration             =>
4290             return Null_Exclusion_Present (N);
4291
4292          when N_Component_Definition            |
4293               N_Formal_Object_Declaration       |
4294               N_Object_Renaming_Declaration     =>
4295             if Present (Subtype_Mark (N)) then
4296                return Null_Exclusion_Present (N);
4297             else pragma Assert (Present (Access_Definition (N)));
4298                return Null_Exclusion_Present (Access_Definition (N));
4299             end if;
4300
4301          when N_Discriminant_Specification =>
4302             if Nkind (Discriminant_Type (N)) = N_Access_Definition then
4303                return Null_Exclusion_Present (Discriminant_Type (N));
4304             else
4305                return Null_Exclusion_Present (N);
4306             end if;
4307
4308          when N_Object_Declaration =>
4309             if Nkind (Object_Definition (N)) = N_Access_Definition then
4310                return Null_Exclusion_Present (Object_Definition (N));
4311             else
4312                return Null_Exclusion_Present (N);
4313             end if;
4314
4315          when N_Parameter_Specification =>
4316             if Nkind (Parameter_Type (N)) = N_Access_Definition then
4317                return Null_Exclusion_Present (Parameter_Type (N));
4318             else
4319                return Null_Exclusion_Present (N);
4320             end if;
4321
4322          when others =>
4323             return False;
4324
4325       end case;
4326    end Has_Null_Exclusion;
4327
4328    ------------------------
4329    -- Has_Null_Extension --
4330    ------------------------
4331
4332    function Has_Null_Extension (T : Entity_Id) return Boolean is
4333       B     : constant Entity_Id := Base_Type (T);
4334       Comps : Node_Id;
4335       Ext   : Node_Id;
4336
4337    begin
4338       if Nkind (Parent (B)) = N_Full_Type_Declaration
4339         and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
4340       then
4341          Ext := Record_Extension_Part (Type_Definition (Parent (B)));
4342
4343          if Present (Ext) then
4344             if Null_Present (Ext) then
4345                return True;
4346             else
4347                Comps := Component_List (Ext);
4348
4349                --  The null component list is rewritten during analysis to
4350                --  include the parent component. Any other component indicates
4351                --  that the extension was not originally null.
4352
4353                return Null_Present (Comps)
4354                  or else No (Next (First (Component_Items (Comps))));
4355             end if;
4356          else
4357             return False;
4358          end if;
4359
4360       else
4361          return False;
4362       end if;
4363    end Has_Null_Extension;
4364
4365    -------------------------------
4366    -- Has_Overriding_Initialize --
4367    -------------------------------
4368
4369    function Has_Overriding_Initialize (T : Entity_Id) return Boolean is
4370       BT   : constant Entity_Id := Base_Type (T);
4371       Comp : Entity_Id;
4372       P    : Elmt_Id;
4373
4374    begin
4375       if Is_Controlled (BT) then
4376
4377          --  For derived types, check immediate ancestor, excluding
4378          --  Controlled itself.
4379
4380          if Is_Derived_Type (BT)
4381            and then not In_Predefined_Unit (Etype (BT))
4382            and then Has_Overriding_Initialize (Etype (BT))
4383          then
4384             return True;
4385
4386          elsif Present (Primitive_Operations (BT)) then
4387             P := First_Elmt (Primitive_Operations (BT));
4388             while Present (P) loop
4389                if Chars (Node (P)) = Name_Initialize
4390                  and then Comes_From_Source (Node (P))
4391                then
4392                   return True;
4393                end if;
4394
4395                Next_Elmt (P);
4396             end loop;
4397          end if;
4398
4399          return False;
4400
4401       elsif Has_Controlled_Component (BT) then
4402          Comp := First_Component (BT);
4403          while Present (Comp) loop
4404             if Has_Overriding_Initialize (Etype (Comp)) then
4405                return True;
4406             end if;
4407
4408             Next_Component (Comp);
4409          end loop;
4410
4411          return False;
4412
4413       else
4414          return False;
4415       end if;
4416    end Has_Overriding_Initialize;
4417
4418    --------------------------------------
4419    -- Has_Preelaborable_Initialization --
4420    --------------------------------------
4421
4422    function Has_Preelaborable_Initialization (E : Entity_Id) return Boolean is
4423       Has_PE : Boolean;
4424
4425       procedure Check_Components (E : Entity_Id);
4426       --  Check component/discriminant chain, sets Has_PE False if a component
4427       --  or discriminant does not meet the preelaborable initialization rules.
4428
4429       ----------------------
4430       -- Check_Components --
4431       ----------------------
4432
4433       procedure Check_Components (E : Entity_Id) is
4434          Ent : Entity_Id;
4435          Exp : Node_Id;
4436
4437          function Is_Preelaborable_Expression (N : Node_Id) return Boolean;
4438          --  Returns True if and only if the expression denoted by N does not
4439          --  violate restrictions on preelaborable constructs (RM-10.2.1(5-9)).
4440
4441          ---------------------------------
4442          -- Is_Preelaborable_Expression --
4443          ---------------------------------
4444
4445          function Is_Preelaborable_Expression (N : Node_Id) return Boolean is
4446             Exp           : Node_Id;
4447             Assn          : Node_Id;
4448             Choice        : Node_Id;
4449             Comp_Type     : Entity_Id;
4450             Is_Array_Aggr : Boolean;
4451
4452          begin
4453             if Is_Static_Expression (N) then
4454                return True;
4455
4456             elsif Nkind (N) = N_Null then
4457                return True;
4458
4459             --  Attributes are allowed in general, even if their prefix is a
4460             --  formal type. (It seems that certain attributes known not to be
4461             --  static might not be allowed, but there are no rules to prevent
4462             --  them.)
4463
4464             elsif Nkind (N) = N_Attribute_Reference then
4465                return True;
4466
4467             --  The name of a discriminant evaluated within its parent type is
4468             --  defined to be preelaborable (10.2.1(8)). Note that we test for
4469             --  names that denote discriminals as well as discriminants to
4470             --  catch references occurring within init procs.
4471
4472             elsif Is_Entity_Name (N)
4473               and then
4474                 (Ekind (Entity (N)) = E_Discriminant
4475                   or else
4476                     ((Ekind (Entity (N)) = E_Constant
4477                        or else Ekind (Entity (N)) = E_In_Parameter)
4478                      and then Present (Discriminal_Link (Entity (N)))))
4479             then
4480                return True;
4481
4482             elsif Nkind (N) = N_Qualified_Expression then
4483                return Is_Preelaborable_Expression (Expression (N));
4484
4485             --  For aggregates we have to check that each of the associations
4486             --  is preelaborable.
4487
4488             elsif Nkind (N) = N_Aggregate
4489               or else Nkind (N) = N_Extension_Aggregate
4490             then
4491                Is_Array_Aggr := Is_Array_Type (Etype (N));
4492
4493                if Is_Array_Aggr then
4494                   Comp_Type := Component_Type (Etype (N));
4495                end if;
4496
4497                --  Check the ancestor part of extension aggregates, which must
4498                --  be either the name of a type that has preelaborable init or
4499                --  an expression that is preelaborable.
4500
4501                if Nkind (N) = N_Extension_Aggregate then
4502                   declare
4503                      Anc_Part : constant Node_Id := Ancestor_Part (N);
4504
4505                   begin
4506                      if Is_Entity_Name (Anc_Part)
4507                        and then Is_Type (Entity (Anc_Part))
4508                      then
4509                         if not Has_Preelaborable_Initialization
4510                                  (Entity (Anc_Part))
4511                         then
4512                            return False;
4513                         end if;
4514
4515                      elsif not Is_Preelaborable_Expression (Anc_Part) then
4516                         return False;
4517                      end if;
4518                   end;
4519                end if;
4520
4521                --  Check positional associations
4522
4523                Exp := First (Expressions (N));
4524                while Present (Exp) loop
4525                   if not Is_Preelaborable_Expression (Exp) then
4526                      return False;
4527                   end if;
4528
4529                   Next (Exp);
4530                end loop;
4531
4532                --  Check named associations
4533
4534                Assn := First (Component_Associations (N));
4535                while Present (Assn) loop
4536                   Choice := First (Choices (Assn));
4537                   while Present (Choice) loop
4538                      if Is_Array_Aggr then
4539                         if Nkind (Choice) = N_Others_Choice then
4540                            null;
4541
4542                         elsif Nkind (Choice) = N_Range then
4543                            if not Is_Static_Range (Choice) then
4544                               return False;
4545                            end if;
4546
4547                         elsif not Is_Static_Expression (Choice) then
4548                            return False;
4549                         end if;
4550
4551                      else
4552                         Comp_Type := Etype (Choice);
4553                      end if;
4554
4555                      Next (Choice);
4556                   end loop;
4557
4558                   --  If the association has a <> at this point, then we have
4559                   --  to check whether the component's type has preelaborable
4560                   --  initialization. Note that this only occurs when the
4561                   --  association's corresponding component does not have a
4562                   --  default expression, the latter case having already been
4563                   --  expanded as an expression for the association.
4564
4565                   if Box_Present (Assn) then
4566                      if not Has_Preelaborable_Initialization (Comp_Type) then
4567                         return False;
4568                      end if;
4569
4570                   --  In the expression case we check whether the expression
4571                   --  is preelaborable.
4572
4573                   elsif
4574                     not Is_Preelaborable_Expression (Expression (Assn))
4575                   then
4576                      return False;
4577                   end if;
4578
4579                   Next (Assn);
4580                end loop;
4581
4582                --  If we get here then aggregate as a whole is preelaborable
4583
4584                return True;
4585
4586             --  All other cases are not preelaborable
4587
4588             else
4589                return False;
4590             end if;
4591          end Is_Preelaborable_Expression;
4592
4593       --  Start of processing for Check_Components
4594
4595       begin
4596          --  Loop through entities of record or protected type
4597
4598          Ent := E;
4599          while Present (Ent) loop
4600
4601             --  We are interested only in components and discriminants
4602
4603             if Ekind (Ent) = E_Component
4604                 or else
4605                Ekind (Ent) = E_Discriminant
4606             then
4607                --  Get default expression if any. If there is no declaration
4608                --  node, it means we have an internal entity. The parent and
4609                --  tag fields are examples of such entities. For these cases,
4610                --  we just test the type of the entity.
4611
4612                if Present (Declaration_Node (Ent)) then
4613                   Exp := Expression (Declaration_Node (Ent));
4614                else
4615                   Exp := Empty;
4616                end if;
4617
4618                --  A component has PI if it has no default expression and the
4619                --  component type has PI.
4620
4621                if No (Exp) then
4622                   if not Has_Preelaborable_Initialization (Etype (Ent)) then
4623                      Has_PE := False;
4624                      exit;
4625                   end if;
4626
4627                --  Require the default expression to be preelaborable
4628
4629                elsif not Is_Preelaborable_Expression (Exp) then
4630                   Has_PE := False;
4631                   exit;
4632                end if;
4633             end if;
4634
4635             Next_Entity (Ent);
4636          end loop;
4637       end Check_Components;
4638
4639    --  Start of processing for Has_Preelaborable_Initialization
4640
4641    begin
4642       --  Immediate return if already marked as known preelaborable init. This
4643       --  covers types for which this function has already been called once
4644       --  and returned True (in which case the result is cached), and also
4645       --  types to which a pragma Preelaborable_Initialization applies.
4646
4647       if Known_To_Have_Preelab_Init (E) then
4648          return True;
4649       end if;
4650
4651       --  If the type is a subtype representing a generic actual type, then
4652       --  test whether its base type has preelaborable initialization since
4653       --  the subtype representing the actual does not inherit this attribute
4654       --  from the actual or formal. (but maybe it should???)
4655
4656       if Is_Generic_Actual_Type (E) then
4657          return Has_Preelaborable_Initialization (Base_Type (E));
4658       end if;
4659
4660       --  All elementary types have preelaborable initialization
4661
4662       if Is_Elementary_Type (E) then
4663          Has_PE := True;
4664
4665       --  Array types have PI if the component type has PI
4666
4667       elsif Is_Array_Type (E) then
4668          Has_PE := Has_Preelaborable_Initialization (Component_Type (E));
4669
4670       --  A derived type has preelaborable initialization if its parent type
4671       --  has preelaborable initialization and (in the case of a derived record
4672       --  extension) if the non-inherited components all have preelaborable
4673       --  initialization. However, a user-defined controlled type with an
4674       --  overriding Initialize procedure does not have preelaborable
4675       --  initialization.
4676
4677       elsif Is_Derived_Type (E) then
4678
4679          --  If the derived type is a private extension then it doesn't have
4680          --  preelaborable initialization.
4681
4682          if Ekind (Base_Type (E)) = E_Record_Type_With_Private then
4683             return False;
4684          end if;
4685
4686          --  First check whether ancestor type has preelaborable initialization
4687
4688          Has_PE := Has_Preelaborable_Initialization (Etype (Base_Type (E)));
4689
4690          --  If OK, check extension components (if any)
4691
4692          if Has_PE and then Is_Record_Type (E) then
4693             Check_Components (First_Entity (E));
4694          end if;
4695
4696          --  Check specifically for 10.2.1(11.4/2) exception: a controlled type
4697          --  with a user defined Initialize procedure does not have PI.
4698
4699          if Has_PE
4700            and then Is_Controlled (E)
4701            and then Has_Overriding_Initialize (E)
4702          then
4703             Has_PE := False;
4704          end if;
4705
4706       --  Private types not derived from a type having preelaborable init and
4707       --  that are not marked with pragma Preelaborable_Initialization do not
4708       --  have preelaborable initialization.
4709
4710       elsif Is_Private_Type (E) then
4711          return False;
4712
4713       --  Record type has PI if it is non private and all components have PI
4714
4715       elsif Is_Record_Type (E) then
4716          Has_PE := True;
4717          Check_Components (First_Entity (E));
4718
4719       --  Protected types must not have entries, and components must meet
4720       --  same set of rules as for record components.
4721
4722       elsif Is_Protected_Type (E) then
4723          if Has_Entries (E) then
4724             Has_PE := False;
4725          else
4726             Has_PE := True;
4727             Check_Components (First_Entity (E));
4728             Check_Components (First_Private_Entity (E));
4729          end if;
4730
4731       --  Type System.Address always has preelaborable initialization
4732
4733       elsif Is_RTE (E, RE_Address) then
4734          Has_PE := True;
4735
4736       --  In all other cases, type does not have preelaborable initialization
4737
4738       else
4739          return False;
4740       end if;
4741
4742       --  If type has preelaborable initialization, cache result
4743
4744       if Has_PE then
4745          Set_Known_To_Have_Preelab_Init (E);
4746       end if;
4747
4748       return Has_PE;
4749    end Has_Preelaborable_Initialization;
4750
4751    ---------------------------
4752    -- Has_Private_Component --
4753    ---------------------------
4754
4755    function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
4756       Btype     : Entity_Id := Base_Type (Type_Id);
4757       Component : Entity_Id;
4758
4759    begin
4760       if Error_Posted (Type_Id)
4761         or else Error_Posted (Btype)
4762       then
4763          return False;
4764       end if;
4765
4766       if Is_Class_Wide_Type (Btype) then
4767          Btype := Root_Type (Btype);
4768       end if;
4769
4770       if Is_Private_Type (Btype) then
4771          declare
4772             UT : constant Entity_Id := Underlying_Type (Btype);
4773          begin
4774             if No (UT) then
4775                if No (Full_View (Btype)) then
4776                   return not Is_Generic_Type (Btype)
4777                     and then not Is_Generic_Type (Root_Type (Btype));
4778                else
4779                   return not Is_Generic_Type (Root_Type (Full_View (Btype)));
4780                end if;
4781             else
4782                return not Is_Frozen (UT) and then Has_Private_Component (UT);
4783             end if;
4784          end;
4785
4786       elsif Is_Array_Type (Btype) then
4787          return Has_Private_Component (Component_Type (Btype));
4788
4789       elsif Is_Record_Type (Btype) then
4790          Component := First_Component (Btype);
4791          while Present (Component) loop
4792             if Has_Private_Component (Etype (Component)) then
4793                return True;
4794             end if;
4795
4796             Next_Component (Component);
4797          end loop;
4798
4799          return False;
4800
4801       elsif Is_Protected_Type (Btype)
4802         and then Present (Corresponding_Record_Type (Btype))
4803       then
4804          return Has_Private_Component (Corresponding_Record_Type (Btype));
4805
4806       else
4807          return False;
4808       end if;
4809    end Has_Private_Component;
4810
4811    ----------------
4812    -- Has_Stream --
4813    ----------------
4814
4815    function Has_Stream (T : Entity_Id) return Boolean is
4816       E : Entity_Id;
4817
4818    begin
4819       if No (T) then
4820          return False;
4821
4822       elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
4823          return True;
4824
4825       elsif Is_Array_Type (T) then
4826          return Has_Stream (Component_Type (T));
4827
4828       elsif Is_Record_Type (T) then
4829          E := First_Component (T);
4830          while Present (E) loop
4831             if Has_Stream (Etype (E)) then
4832                return True;
4833             else
4834                Next_Component (E);
4835             end if;
4836          end loop;
4837
4838          return False;
4839
4840       elsif Is_Private_Type (T) then
4841          return Has_Stream (Underlying_Type (T));
4842
4843       else
4844          return False;
4845       end if;
4846    end Has_Stream;
4847
4848    --------------------------
4849    -- Has_Tagged_Component --
4850    --------------------------
4851
4852    function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
4853       Comp : Entity_Id;
4854
4855    begin
4856       if Is_Private_Type (Typ)
4857         and then Present (Underlying_Type (Typ))
4858       then
4859          return Has_Tagged_Component (Underlying_Type (Typ));
4860
4861       elsif Is_Array_Type (Typ) then
4862          return Has_Tagged_Component (Component_Type (Typ));
4863
4864       elsif Is_Tagged_Type (Typ) then
4865          return True;
4866
4867       elsif Is_Record_Type (Typ) then
4868          Comp := First_Component (Typ);
4869          while Present (Comp) loop
4870             if Has_Tagged_Component (Etype (Comp)) then
4871                return True;
4872             end if;
4873
4874             Next_Component (Comp);
4875          end loop;
4876
4877          return False;
4878
4879       else
4880          return False;
4881       end if;
4882    end Has_Tagged_Component;
4883
4884    --------------------------
4885    -- Implements_Interface --
4886    --------------------------
4887
4888    function Implements_Interface
4889      (Typ_Ent         : Entity_Id;
4890       Iface_Ent       : Entity_Id;
4891       Exclude_Parents : Boolean := False) return Boolean
4892    is
4893       Ifaces_List : Elist_Id;
4894       Elmt        : Elmt_Id;
4895       Iface       : Entity_Id;
4896       Typ         : Entity_Id;
4897
4898    begin
4899       if Is_Class_Wide_Type (Typ_Ent) then
4900          Typ := Etype (Typ_Ent);
4901       else
4902          Typ := Typ_Ent;
4903       end if;
4904
4905       if Is_Class_Wide_Type (Iface_Ent) then
4906          Iface := Etype (Iface_Ent);
4907       else
4908          Iface := Iface_Ent;
4909       end if;
4910
4911       if not Has_Interfaces (Typ) then
4912          return False;
4913       end if;
4914
4915       Collect_Interfaces (Typ, Ifaces_List);
4916
4917       Elmt := First_Elmt (Ifaces_List);
4918       while Present (Elmt) loop
4919          if Is_Ancestor (Node (Elmt), Typ)
4920            and then Exclude_Parents
4921          then
4922             null;
4923
4924          elsif Node (Elmt) = Iface then
4925             return True;
4926          end if;
4927
4928          Next_Elmt (Elmt);
4929       end loop;
4930
4931       return False;
4932    end Implements_Interface;
4933
4934    -----------------
4935    -- In_Instance --
4936    -----------------
4937
4938    function In_Instance return Boolean is
4939       Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4940       S         : Entity_Id;
4941
4942    begin
4943       S := Current_Scope;
4944       while Present (S)
4945         and then S /= Standard_Standard
4946       loop
4947          if (Ekind (S) = E_Function
4948               or else Ekind (S) = E_Package
4949               or else Ekind (S) = E_Procedure)
4950            and then Is_Generic_Instance (S)
4951          then
4952             --  A child instance is always compiled in the context of a parent
4953             --  instance. Nevertheless, the actuals are not analyzed in an
4954             --  instance context. We detect this case by examining the current
4955             --  compilation unit, which must be a child instance, and checking
4956             --  that it is not currently on the scope stack.
4957
4958             if Is_Child_Unit (Curr_Unit)
4959               and then
4960                 Nkind (Unit (Cunit (Current_Sem_Unit)))
4961                   = N_Package_Instantiation
4962               and then not In_Open_Scopes (Curr_Unit)
4963             then
4964                return False;
4965             else
4966                return True;
4967             end if;
4968          end if;
4969
4970          S := Scope (S);
4971       end loop;
4972
4973       return False;
4974    end In_Instance;
4975
4976    ----------------------
4977    -- In_Instance_Body --
4978    ----------------------
4979
4980    function In_Instance_Body return Boolean is
4981       S : Entity_Id;
4982
4983    begin
4984       S := Current_Scope;
4985       while Present (S)
4986         and then S /= Standard_Standard
4987       loop
4988          if (Ekind (S) = E_Function
4989               or else Ekind (S) = E_Procedure)
4990            and then Is_Generic_Instance (S)
4991          then
4992             return True;
4993
4994          elsif Ekind (S) = E_Package
4995            and then In_Package_Body (S)
4996            and then Is_Generic_Instance (S)
4997          then
4998             return True;
4999          end if;
5000
5001          S := Scope (S);
5002       end loop;
5003
5004       return False;
5005    end In_Instance_Body;
5006
5007    -----------------------------
5008    -- In_Instance_Not_Visible --
5009    -----------------------------
5010
5011    function In_Instance_Not_Visible return Boolean is
5012       S : Entity_Id;
5013
5014    begin
5015       S := Current_Scope;
5016       while Present (S)
5017         and then S /= Standard_Standard
5018       loop
5019          if (Ekind (S) = E_Function
5020               or else Ekind (S) = E_Procedure)
5021            and then Is_Generic_Instance (S)
5022          then
5023             return True;
5024
5025          elsif Ekind (S) = E_Package
5026            and then (In_Package_Body (S) or else In_Private_Part (S))
5027            and then Is_Generic_Instance (S)
5028          then
5029             return True;
5030          end if;
5031
5032          S := Scope (S);
5033       end loop;
5034
5035       return False;
5036    end In_Instance_Not_Visible;
5037
5038    ------------------------------
5039    -- In_Instance_Visible_Part --
5040    ------------------------------
5041
5042    function In_Instance_Visible_Part return Boolean is
5043       S : Entity_Id;
5044
5045    begin
5046       S := Current_Scope;
5047       while Present (S)
5048         and then S /= Standard_Standard
5049       loop
5050          if Ekind (S) = E_Package
5051            and then Is_Generic_Instance (S)
5052            and then not In_Package_Body (S)
5053            and then not In_Private_Part (S)
5054          then
5055             return True;
5056          end if;
5057
5058          S := Scope (S);
5059       end loop;
5060
5061       return False;
5062    end In_Instance_Visible_Part;
5063
5064    ---------------------
5065    -- In_Package_Body --
5066    ---------------------
5067
5068    function In_Package_Body return Boolean is
5069       S : Entity_Id;
5070
5071    begin
5072       S := Current_Scope;
5073       while Present (S)
5074         and then S /= Standard_Standard
5075       loop
5076          if Ekind (S) = E_Package
5077            and then In_Package_Body (S)
5078          then
5079             return True;
5080          else
5081             S := Scope (S);
5082          end if;
5083       end loop;
5084
5085       return False;
5086    end In_Package_Body;
5087
5088    --------------------------------
5089    -- In_Parameter_Specification --
5090    --------------------------------
5091
5092    function In_Parameter_Specification (N : Node_Id) return Boolean is
5093       PN : Node_Id;
5094
5095    begin
5096       PN := Parent (N);
5097       while Present (PN) loop
5098          if Nkind (PN) = N_Parameter_Specification then
5099             return True;
5100          end if;
5101
5102          PN := Parent (PN);
5103       end loop;
5104
5105       return False;
5106    end In_Parameter_Specification;
5107
5108    --------------------------------------
5109    -- In_Subprogram_Or_Concurrent_Unit --
5110    --------------------------------------
5111
5112    function In_Subprogram_Or_Concurrent_Unit return Boolean is
5113       E : Entity_Id;
5114       K : Entity_Kind;
5115
5116    begin
5117       --  Use scope chain to check successively outer scopes
5118
5119       E := Current_Scope;
5120       loop
5121          K := Ekind (E);
5122
5123          if K in Subprogram_Kind
5124            or else K in Concurrent_Kind
5125            or else K in Generic_Subprogram_Kind
5126          then
5127             return True;
5128
5129          elsif E = Standard_Standard then
5130             return False;
5131          end if;
5132
5133          E := Scope (E);
5134       end loop;
5135    end In_Subprogram_Or_Concurrent_Unit;
5136
5137    ---------------------
5138    -- In_Visible_Part --
5139    ---------------------
5140
5141    function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
5142    begin
5143       return
5144         Is_Package_Or_Generic_Package (Scope_Id)
5145           and then In_Open_Scopes (Scope_Id)
5146           and then not In_Package_Body (Scope_Id)
5147           and then not In_Private_Part (Scope_Id);
5148    end In_Visible_Part;
5149
5150    ---------------------------------
5151    -- Insert_Explicit_Dereference --
5152    ---------------------------------
5153
5154    procedure Insert_Explicit_Dereference (N : Node_Id) is
5155       New_Prefix : constant Node_Id := Relocate_Node (N);
5156       Ent        : Entity_Id := Empty;
5157       Pref       : Node_Id;
5158       I          : Interp_Index;
5159       It         : Interp;
5160       T          : Entity_Id;
5161
5162    begin
5163       Save_Interps (N, New_Prefix);
5164       Rewrite (N, Make_Explicit_Dereference (Sloc (N), Prefix => New_Prefix));
5165
5166       Set_Etype (N, Designated_Type (Etype (New_Prefix)));
5167
5168       if Is_Overloaded (New_Prefix) then
5169
5170          --  The deference is also overloaded, and its interpretations are the
5171          --  designated types of the interpretations of the original node.
5172
5173          Set_Etype (N, Any_Type);
5174
5175          Get_First_Interp (New_Prefix, I, It);
5176          while Present (It.Nam) loop
5177             T := It.Typ;
5178
5179             if Is_Access_Type (T) then
5180                Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
5181             end if;
5182
5183             Get_Next_Interp (I, It);
5184          end loop;
5185
5186          End_Interp_List;
5187
5188       else
5189          --  Prefix is unambiguous: mark the original prefix (which might
5190          --  Come_From_Source) as a reference, since the new (relocated) one
5191          --  won't be taken into account.
5192
5193          if Is_Entity_Name (New_Prefix) then
5194             Ent := Entity (New_Prefix);
5195
5196          --  For a retrieval of a subcomponent of some composite object,
5197          --  retrieve the ultimate entity if there is one.
5198
5199          elsif Nkind (New_Prefix) = N_Selected_Component
5200            or else Nkind (New_Prefix) = N_Indexed_Component
5201          then
5202             Pref := Prefix (New_Prefix);
5203             while Present (Pref)
5204               and then
5205                 (Nkind (Pref) = N_Selected_Component
5206                   or else Nkind (Pref) = N_Indexed_Component)
5207             loop
5208                Pref := Prefix (Pref);
5209             end loop;
5210
5211             if Present (Pref) and then Is_Entity_Name (Pref) then
5212                Ent := Entity (Pref);
5213             end if;
5214          end if;
5215
5216          if Present (Ent) then
5217             Generate_Reference (Ent, New_Prefix);
5218          end if;
5219       end if;
5220    end Insert_Explicit_Dereference;
5221
5222    ------------------------------------------
5223    -- Inspect_Deferred_Constant_Completion --
5224    ------------------------------------------
5225
5226    procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
5227       Decl   : Node_Id;
5228
5229    begin
5230       Decl := First (Decls);
5231       while Present (Decl) loop
5232
5233          --  Deferred constant signature
5234
5235          if Nkind (Decl) = N_Object_Declaration
5236            and then Constant_Present (Decl)
5237            and then No (Expression (Decl))
5238
5239             --  No need to check internally generated constants
5240
5241            and then Comes_From_Source (Decl)
5242
5243             --  The constant is not completed. A full object declaration
5244             --  or a pragma Import complete a deferred constant.
5245
5246            and then not Has_Completion (Defining_Identifier (Decl))
5247          then
5248             Error_Msg_N
5249               ("constant declaration requires initialization expression",
5250               Defining_Identifier (Decl));
5251          end if;
5252
5253          Decl := Next (Decl);
5254       end loop;
5255    end Inspect_Deferred_Constant_Completion;
5256
5257    -------------------
5258    -- Is_AAMP_Float --
5259    -------------------
5260
5261    function Is_AAMP_Float (E : Entity_Id) return Boolean is
5262       pragma Assert (Is_Type (E));
5263    begin
5264       return AAMP_On_Target
5265          and then Is_Floating_Point_Type (E)
5266          and then E = Base_Type (E);
5267    end Is_AAMP_Float;
5268
5269    -------------------------
5270    -- Is_Actual_Parameter --
5271    -------------------------
5272
5273    function Is_Actual_Parameter (N : Node_Id) return Boolean is
5274       PK : constant Node_Kind := Nkind (Parent (N));
5275
5276    begin
5277       case PK is
5278          when N_Parameter_Association =>
5279             return N = Explicit_Actual_Parameter (Parent (N));
5280
5281          when N_Function_Call | N_Procedure_Call_Statement =>
5282             return Is_List_Member (N)
5283               and then
5284                 List_Containing (N) = Parameter_Associations (Parent (N));
5285
5286          when others =>
5287             return False;
5288       end case;
5289    end Is_Actual_Parameter;
5290
5291    ---------------------
5292    -- Is_Aliased_View --
5293    ---------------------
5294
5295    function Is_Aliased_View (Obj : Node_Id) return Boolean is
5296       E : Entity_Id;
5297
5298    begin
5299       if Is_Entity_Name (Obj) then
5300
5301          E := Entity (Obj);
5302
5303          return
5304            (Is_Object (E)
5305              and then
5306                (Is_Aliased (E)
5307                   or else (Present (Renamed_Object (E))
5308                              and then Is_Aliased_View (Renamed_Object (E)))))
5309
5310            or else ((Is_Formal (E)
5311                       or else Ekind (E) = E_Generic_In_Out_Parameter
5312                       or else Ekind (E) = E_Generic_In_Parameter)
5313                     and then Is_Tagged_Type (Etype (E)))
5314
5315            or else (Is_Concurrent_Type (E)
5316                      and then In_Open_Scopes (E))
5317
5318             --  Current instance of type, either directly or as rewritten
5319             --  reference to the current object.
5320
5321            or else (Is_Entity_Name (Original_Node (Obj))
5322                      and then Present (Entity (Original_Node (Obj)))
5323                      and then Is_Type (Entity (Original_Node (Obj))))
5324
5325            or else (Is_Type (E) and then E = Current_Scope)
5326
5327            or else (Is_Incomplete_Or_Private_Type (E)
5328                      and then Full_View (E) = Current_Scope);
5329
5330       elsif Nkind (Obj) = N_Selected_Component then
5331          return Is_Aliased (Entity (Selector_Name (Obj)));
5332
5333       elsif Nkind (Obj) = N_Indexed_Component then
5334          return Has_Aliased_Components (Etype (Prefix (Obj)))
5335            or else
5336              (Is_Access_Type (Etype (Prefix (Obj)))
5337                and then
5338               Has_Aliased_Components
5339                 (Designated_Type (Etype (Prefix (Obj)))));
5340
5341       elsif Nkind (Obj) = N_Unchecked_Type_Conversion
5342         or else Nkind (Obj) = N_Type_Conversion
5343       then
5344          return Is_Tagged_Type (Etype (Obj))
5345            and then Is_Aliased_View (Expression (Obj));
5346
5347       elsif Nkind (Obj) = N_Explicit_Dereference then
5348          return Nkind (Original_Node (Obj)) /= N_Function_Call;
5349
5350       else
5351          return False;
5352       end if;
5353    end Is_Aliased_View;
5354
5355    -------------------------
5356    -- Is_Ancestor_Package --
5357    -------------------------
5358
5359    function Is_Ancestor_Package
5360      (E1 : Entity_Id;
5361       E2 : Entity_Id) return Boolean
5362    is
5363       Par : Entity_Id;
5364
5365    begin
5366       Par := E2;
5367       while Present (Par)
5368         and then Par /= Standard_Standard
5369       loop
5370          if Par = E1 then
5371             return True;
5372          end if;
5373
5374          Par := Scope (Par);
5375       end loop;
5376
5377       return False;
5378    end Is_Ancestor_Package;
5379
5380    ----------------------
5381    -- Is_Atomic_Object --
5382    ----------------------
5383
5384    function Is_Atomic_Object (N : Node_Id) return Boolean is
5385
5386       function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
5387       --  Determines if given object has atomic components
5388
5389       function Is_Atomic_Prefix (N : Node_Id) return Boolean;
5390       --  If prefix is an implicit dereference, examine designated type
5391
5392       ----------------------
5393       -- Is_Atomic_Prefix --
5394       ----------------------
5395
5396       function Is_Atomic_Prefix (N : Node_Id) return Boolean is
5397       begin
5398          if Is_Access_Type (Etype (N)) then
5399             return
5400               Has_Atomic_Components (Designated_Type (Etype (N)));
5401          else
5402             return Object_Has_Atomic_Components (N);
5403          end if;
5404       end Is_Atomic_Prefix;
5405
5406       ----------------------------------
5407       -- Object_Has_Atomic_Components --
5408       ----------------------------------
5409
5410       function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
5411       begin
5412          if Has_Atomic_Components (Etype (N))
5413            or else Is_Atomic (Etype (N))
5414          then
5415             return True;
5416
5417          elsif Is_Entity_Name (N)
5418            and then (Has_Atomic_Components (Entity (N))
5419                       or else Is_Atomic (Entity (N)))
5420          then
5421             return True;
5422
5423          elsif Nkind (N) = N_Indexed_Component
5424            or else Nkind (N) = N_Selected_Component
5425          then
5426             return Is_Atomic_Prefix (Prefix (N));
5427
5428          else
5429             return False;
5430          end if;
5431       end Object_Has_Atomic_Components;
5432
5433    --  Start of processing for Is_Atomic_Object
5434
5435    begin
5436       if Is_Atomic (Etype (N))
5437         or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
5438       then
5439          return True;
5440
5441       elsif Nkind (N) = N_Indexed_Component
5442         or else Nkind (N) = N_Selected_Component
5443       then
5444          return Is_Atomic_Prefix (Prefix (N));
5445
5446       else
5447          return False;
5448       end if;
5449    end Is_Atomic_Object;
5450
5451    -------------------------
5452    -- Is_Coextension_Root --
5453    -------------------------
5454
5455    function Is_Coextension_Root (N : Node_Id) return Boolean is
5456    begin
5457       return
5458         Nkind (N) = N_Allocator
5459           and then Present (Coextensions (N))
5460
5461          --  Anonymous access discriminants carry a list of all nested
5462          --  controlled coextensions.
5463
5464           and then not Is_Dynamic_Coextension (N)
5465           and then not Is_Static_Coextension (N);
5466    end Is_Coextension_Root;
5467
5468    -----------------------------
5469    -- Is_Concurrent_Interface --
5470    -----------------------------
5471
5472    function Is_Concurrent_Interface (T : Entity_Id) return Boolean is
5473    begin
5474       return
5475         Is_Interface (T)
5476           and then
5477             (Is_Protected_Interface (T)
5478                or else Is_Synchronized_Interface (T)
5479                or else Is_Task_Interface (T));
5480    end Is_Concurrent_Interface;
5481
5482    --------------------------------------
5483    -- Is_Controlling_Limited_Procedure --
5484    --------------------------------------
5485
5486    function Is_Controlling_Limited_Procedure
5487      (Proc_Nam : Entity_Id) return Boolean
5488    is
5489       Param_Typ : Entity_Id := Empty;
5490
5491    begin
5492       if Ekind (Proc_Nam) = E_Procedure
5493         and then Present (Parameter_Specifications (Parent (Proc_Nam)))
5494       then
5495          Param_Typ := Etype (Parameter_Type (First (
5496                         Parameter_Specifications (Parent (Proc_Nam)))));
5497
5498       --  In this case where an Itype was created, the procedure call has been
5499       --  rewritten.
5500
5501       elsif Present (Associated_Node_For_Itype (Proc_Nam))
5502         and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
5503         and then
5504           Present (Parameter_Associations
5505                      (Associated_Node_For_Itype (Proc_Nam)))
5506       then
5507          Param_Typ :=
5508            Etype (First (Parameter_Associations
5509                           (Associated_Node_For_Itype (Proc_Nam))));
5510       end if;
5511
5512       if Present (Param_Typ) then
5513          return
5514            Is_Interface (Param_Typ)
5515              and then Is_Limited_Record (Param_Typ);
5516       end if;
5517
5518       return False;
5519    end Is_Controlling_Limited_Procedure;
5520
5521    -----------------------------
5522    -- Is_CPP_Constructor_Call --
5523    -----------------------------
5524
5525    function Is_CPP_Constructor_Call (N : Node_Id) return Boolean is
5526    begin
5527       return Nkind (N) = N_Function_Call
5528         and then Is_Class_Wide_Type (Etype (N))
5529         and then Is_CPP_Class (Etype (Etype (N)))
5530         and then Is_Constructor (Entity (Name (N)))
5531         and then Is_Imported (Entity (Name (N)));
5532    end Is_CPP_Constructor_Call;
5533
5534    ----------------------------------------------
5535    -- Is_Dependent_Component_Of_Mutable_Object --
5536    ----------------------------------------------
5537
5538    function Is_Dependent_Component_Of_Mutable_Object
5539      (Object : Node_Id) return   Boolean
5540    is
5541       P           : Node_Id;
5542       Prefix_Type : Entity_Id;
5543       P_Aliased   : Boolean := False;
5544       Comp        : Entity_Id;
5545
5546       function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean;
5547       --  Returns True if and only if Comp is declared within a variant part
5548
5549       --------------------------------
5550       -- Is_Declared_Within_Variant --
5551       --------------------------------
5552
5553       function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
5554          Comp_Decl : constant Node_Id   := Parent (Comp);
5555          Comp_List : constant Node_Id   := Parent (Comp_Decl);
5556       begin
5557          return Nkind (Parent (Comp_List)) = N_Variant;
5558       end Is_Declared_Within_Variant;
5559
5560    --  Start of processing for Is_Dependent_Component_Of_Mutable_Object
5561
5562    begin
5563       if Is_Variable (Object) then
5564
5565          if Nkind (Object) = N_Selected_Component then
5566             P := Prefix (Object);
5567             Prefix_Type := Etype (P);
5568
5569             if Is_Entity_Name (P) then
5570
5571                if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
5572                   Prefix_Type := Base_Type (Prefix_Type);
5573                end if;
5574
5575                if Is_Aliased (Entity (P)) then
5576                   P_Aliased := True;
5577                end if;
5578
5579             --  A discriminant check on a selected component may be
5580             --  expanded into a dereference when removing side-effects.
5581             --  Recover the original node and its type, which may be
5582             --  unconstrained.
5583
5584             elsif Nkind (P) = N_Explicit_Dereference
5585               and then not (Comes_From_Source (P))
5586             then
5587                P := Original_Node (P);
5588                Prefix_Type := Etype (P);
5589
5590             else
5591                --  Check for prefix being an aliased component ???
5592                null;
5593
5594             end if;
5595
5596             --  A heap object is constrained by its initial value
5597
5598             --  Ada 2005 (AI-363): Always assume the object could be mutable in
5599             --  the dereferenced case, since the access value might denote an
5600             --  unconstrained aliased object, whereas in Ada 95 the designated
5601             --  object is guaranteed to be constrained. A worst-case assumption
5602             --  has to apply in Ada 2005 because we can't tell at compile time
5603             --  whether the object is "constrained by its initial value"
5604             --  (despite the fact that 3.10.2(26/2) and 8.5.1(5/2) are
5605             --  semantic rules -- these rules are acknowledged to need fixing).
5606
5607             if Ada_Version < Ada_05 then
5608                if Is_Access_Type (Prefix_Type)
5609                  or else Nkind (P) = N_Explicit_Dereference
5610                then
5611                   return False;
5612                end if;
5613
5614             elsif Ada_Version >= Ada_05 then
5615                if Is_Access_Type (Prefix_Type) then
5616
5617                   --  If the access type is pool-specific, and there is no
5618                   --  constrained partial view of the designated type, then the
5619                   --  designated object is known to be constrained.
5620
5621                   if Ekind (Prefix_Type) = E_Access_Type
5622                     and then not Has_Constrained_Partial_View
5623                                    (Designated_Type (Prefix_Type))
5624                   then
5625                      return False;
5626
5627                   --  Otherwise (general access type, or there is a constrained
5628                   --  partial view of the designated type), we need to check
5629                   --  based on the designated type.
5630
5631                   else
5632                      Prefix_Type := Designated_Type (Prefix_Type);
5633                   end if;
5634                end if;
5635             end if;
5636
5637             Comp :=
5638               Original_Record_Component (Entity (Selector_Name (Object)));
5639
5640             --  As per AI-0017, the renaming is illegal in a generic body,
5641             --  even if the subtype is indefinite.
5642
5643             --  Ada 2005 (AI-363): In Ada 2005 an aliased object can be mutable
5644
5645             if not Is_Constrained (Prefix_Type)
5646               and then (not Is_Indefinite_Subtype (Prefix_Type)
5647                          or else
5648                           (Is_Generic_Type (Prefix_Type)
5649                             and then Ekind (Current_Scope) = E_Generic_Package
5650                             and then In_Package_Body (Current_Scope)))
5651
5652               and then (Is_Declared_Within_Variant (Comp)
5653                           or else Has_Discriminant_Dependent_Constraint (Comp))
5654               and then (not P_Aliased or else Ada_Version >= Ada_05)
5655             then
5656                return True;
5657
5658             else
5659                return
5660                  Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
5661
5662             end if;
5663
5664          elsif Nkind (Object) = N_Indexed_Component
5665            or else Nkind (Object) = N_Slice
5666          then
5667             return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
5668
5669          --  A type conversion that Is_Variable is a view conversion:
5670          --  go back to the denoted object.
5671
5672          elsif Nkind (Object) = N_Type_Conversion then
5673             return
5674               Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
5675          end if;
5676       end if;
5677
5678       return False;
5679    end Is_Dependent_Component_Of_Mutable_Object;
5680
5681    ---------------------
5682    -- Is_Dereferenced --
5683    ---------------------
5684
5685    function Is_Dereferenced (N : Node_Id) return Boolean is
5686       P : constant Node_Id := Parent (N);
5687    begin
5688       return
5689          (Nkind (P) = N_Selected_Component
5690             or else
5691           Nkind (P) = N_Explicit_Dereference
5692             or else
5693           Nkind (P) = N_Indexed_Component
5694             or else
5695           Nkind (P) = N_Slice)
5696         and then Prefix (P) = N;
5697    end Is_Dereferenced;
5698
5699    ----------------------
5700    -- Is_Descendent_Of --
5701    ----------------------
5702
5703    function Is_Descendent_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
5704       T    : Entity_Id;
5705       Etyp : Entity_Id;
5706
5707    begin
5708       pragma Assert (Nkind (T1) in N_Entity);
5709       pragma Assert (Nkind (T2) in N_Entity);
5710
5711       T := Base_Type (T1);
5712
5713       --  Immediate return if the types match
5714
5715       if T = T2 then
5716          return True;
5717
5718       --  Comment needed here ???
5719
5720       elsif Ekind (T) = E_Class_Wide_Type then
5721          return Etype (T) = T2;
5722
5723       --  All other cases
5724
5725       else
5726          loop
5727             Etyp := Etype (T);
5728
5729             --  Done if we found the type we are looking for
5730
5731             if Etyp = T2 then
5732                return True;
5733
5734             --  Done if no more derivations to check
5735
5736             elsif T = T1
5737               or else T = Etyp
5738             then
5739                return False;
5740
5741             --  Following test catches error cases resulting from prev errors
5742
5743             elsif No (Etyp) then
5744                return False;
5745
5746             elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
5747                return False;
5748
5749             elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
5750                return False;
5751             end if;
5752
5753             T := Base_Type (Etyp);
5754          end loop;
5755       end if;
5756    end Is_Descendent_Of;
5757
5758    --------------
5759    -- Is_False --
5760    --------------
5761
5762    function Is_False (U : Uint) return Boolean is
5763    begin
5764       return (U = 0);
5765    end Is_False;
5766
5767    ---------------------------
5768    -- Is_Fixed_Model_Number --
5769    ---------------------------
5770
5771    function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
5772       S : constant Ureal := Small_Value (T);
5773       M : Urealp.Save_Mark;
5774       R : Boolean;
5775    begin
5776       M := Urealp.Mark;
5777       R := (U = UR_Trunc (U / S) * S);
5778       Urealp.Release (M);
5779       return R;
5780    end Is_Fixed_Model_Number;
5781
5782    -------------------------------
5783    -- Is_Fully_Initialized_Type --
5784    -------------------------------
5785
5786    function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
5787    begin
5788       if Is_Scalar_Type (Typ) then
5789          return False;
5790
5791       elsif Is_Access_Type (Typ) then
5792          return True;
5793
5794       elsif Is_Array_Type (Typ) then
5795          if Is_Fully_Initialized_Type (Component_Type (Typ)) then
5796             return True;
5797          end if;
5798
5799          --  An interesting case, if we have a constrained type one of whose
5800          --  bounds is known to be null, then there are no elements to be
5801          --  initialized, so all the elements are initialized!
5802
5803          if Is_Constrained (Typ) then
5804             declare
5805                Indx     : Node_Id;
5806                Indx_Typ : Entity_Id;
5807                Lbd, Hbd : Node_Id;
5808
5809             begin
5810                Indx := First_Index (Typ);
5811                while Present (Indx) loop
5812                   if Etype (Indx) = Any_Type then
5813                      return False;
5814
5815                   --  If index is a range, use directly
5816
5817                   elsif Nkind (Indx) = N_Range then
5818                      Lbd := Low_Bound  (Indx);
5819                      Hbd := High_Bound (Indx);
5820
5821                   else
5822                      Indx_Typ := Etype (Indx);
5823
5824                      if Is_Private_Type (Indx_Typ)  then
5825                         Indx_Typ := Full_View (Indx_Typ);
5826                      end if;
5827
5828                      if No (Indx_Typ) or else Etype (Indx_Typ) = Any_Type then
5829                         return False;
5830                      else
5831                         Lbd := Type_Low_Bound  (Indx_Typ);
5832                         Hbd := Type_High_Bound (Indx_Typ);
5833                      end if;
5834                   end if;
5835
5836                   if Compile_Time_Known_Value (Lbd)
5837                     and then Compile_Time_Known_Value (Hbd)
5838                   then
5839                      if Expr_Value (Hbd) < Expr_Value (Lbd) then
5840                         return True;
5841                      end if;
5842                   end if;
5843
5844                   Next_Index (Indx);
5845                end loop;
5846             end;
5847          end if;
5848
5849          --  If no null indexes, then type is not fully initialized
5850
5851          return False;
5852
5853       --  Record types
5854
5855       elsif Is_Record_Type (Typ) then
5856          if Has_Discriminants (Typ)
5857            and then
5858              Present (Discriminant_Default_Value (First_Discriminant (Typ)))
5859            and then Is_Fully_Initialized_Variant (Typ)
5860          then
5861             return True;
5862          end if;
5863
5864          --  Controlled records are considered to be fully initialized if
5865          --  there is a user defined Initialize routine. This may not be
5866          --  entirely correct, but as the spec notes, we are guessing here
5867          --  what is best from the point of view of issuing warnings.
5868
5869          if Is_Controlled (Typ) then
5870             declare
5871                Utyp : constant Entity_Id := Underlying_Type (Typ);
5872
5873             begin
5874                if Present (Utyp) then
5875                   declare
5876                      Init : constant Entity_Id :=
5877                               (Find_Prim_Op
5878                                  (Underlying_Type (Typ), Name_Initialize));
5879
5880                   begin
5881                      if Present (Init)
5882                        and then Comes_From_Source (Init)
5883                        and then not
5884                          Is_Predefined_File_Name
5885                            (File_Name (Get_Source_File_Index (Sloc (Init))))
5886                      then
5887                         return True;
5888
5889                      elsif Has_Null_Extension (Typ)
5890                         and then
5891                           Is_Fully_Initialized_Type
5892                             (Etype (Base_Type (Typ)))
5893                      then
5894                         return True;
5895                      end if;
5896                   end;
5897                end if;
5898             end;
5899          end if;
5900
5901          --  Otherwise see if all record components are initialized
5902
5903          declare
5904             Ent : Entity_Id;
5905
5906          begin
5907             Ent := First_Entity (Typ);
5908             while Present (Ent) loop
5909                if Chars (Ent) = Name_uController then
5910                   null;
5911
5912                elsif Ekind (Ent) = E_Component
5913                  and then (No (Parent (Ent))
5914                              or else No (Expression (Parent (Ent))))
5915                  and then not Is_Fully_Initialized_Type (Etype (Ent))
5916
5917                   --  Special VM case for tag components, which need to be
5918                   --  defined in this case, but are never initialized as VMs
5919                   --  are using other dispatching mechanisms. Ignore this
5920                   --  uninitialized case. Note that this applies both to the
5921                   --  uTag entry and the main vtable pointer (CPP_Class case).
5922
5923                  and then (VM_Target = No_VM or else not Is_Tag (Ent))
5924                then
5925                   return False;
5926                end if;
5927
5928                Next_Entity (Ent);
5929             end loop;
5930          end;
5931
5932          --  No uninitialized components, so type is fully initialized.
5933          --  Note that this catches the case of no components as well.
5934
5935          return True;
5936
5937       elsif Is_Concurrent_Type (Typ) then
5938          return True;
5939
5940       elsif Is_Private_Type (Typ) then
5941          declare
5942             U : constant Entity_Id := Underlying_Type (Typ);
5943
5944          begin
5945             if No (U) then
5946                return False;
5947             else
5948                return Is_Fully_Initialized_Type (U);
5949             end if;
5950          end;
5951
5952       else
5953          return False;
5954       end if;
5955    end Is_Fully_Initialized_Type;
5956
5957    ----------------------------------
5958    -- Is_Fully_Initialized_Variant --
5959    ----------------------------------
5960
5961    function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
5962       Loc           : constant Source_Ptr := Sloc (Typ);
5963       Constraints   : constant List_Id    := New_List;
5964       Components    : constant Elist_Id   := New_Elmt_List;
5965       Comp_Elmt     : Elmt_Id;
5966       Comp_Id       : Node_Id;
5967       Comp_List     : Node_Id;
5968       Discr         : Entity_Id;
5969       Discr_Val     : Node_Id;
5970
5971       Report_Errors : Boolean;
5972       pragma Warnings (Off, Report_Errors);
5973
5974    begin
5975       if Serious_Errors_Detected > 0 then
5976          return False;
5977       end if;
5978
5979       if Is_Record_Type (Typ)
5980         and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
5981         and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
5982       then
5983          Comp_List := Component_List (Type_Definition (Parent (Typ)));
5984
5985          Discr := First_Discriminant (Typ);
5986          while Present (Discr) loop
5987             if Nkind (Parent (Discr)) = N_Discriminant_Specification then
5988                Discr_Val := Expression (Parent (Discr));
5989
5990                if Present (Discr_Val)
5991                  and then Is_OK_Static_Expression (Discr_Val)
5992                then
5993                   Append_To (Constraints,
5994                     Make_Component_Association (Loc,
5995                       Choices    => New_List (New_Occurrence_Of (Discr, Loc)),
5996                       Expression => New_Copy (Discr_Val)));
5997                else
5998                   return False;
5999                end if;
6000             else
6001                return False;
6002             end if;
6003
6004             Next_Discriminant (Discr);
6005          end loop;
6006
6007          Gather_Components
6008            (Typ           => Typ,
6009             Comp_List     => Comp_List,
6010             Governed_By   => Constraints,
6011             Into          => Components,
6012             Report_Errors => Report_Errors);
6013
6014          --  Check that each component present is fully initialized
6015
6016          Comp_Elmt := First_Elmt (Components);
6017          while Present (Comp_Elmt) loop
6018             Comp_Id := Node (Comp_Elmt);
6019
6020             if Ekind (Comp_Id) = E_Component
6021               and then (No (Parent (Comp_Id))
6022                          or else No (Expression (Parent (Comp_Id))))
6023               and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
6024             then
6025                return False;
6026             end if;
6027
6028             Next_Elmt (Comp_Elmt);
6029          end loop;
6030
6031          return True;
6032
6033       elsif Is_Private_Type (Typ) then
6034          declare
6035             U : constant Entity_Id := Underlying_Type (Typ);
6036
6037          begin
6038             if No (U) then
6039                return False;
6040             else
6041                return Is_Fully_Initialized_Variant (U);
6042             end if;
6043          end;
6044       else
6045          return False;
6046       end if;
6047    end Is_Fully_Initialized_Variant;
6048
6049    ----------------------------
6050    -- Is_Inherited_Operation --
6051    ----------------------------
6052
6053    function Is_Inherited_Operation (E : Entity_Id) return Boolean is
6054       Kind : constant Node_Kind := Nkind (Parent (E));
6055    begin
6056       pragma Assert (Is_Overloadable (E));
6057       return Kind = N_Full_Type_Declaration
6058         or else Kind = N_Private_Extension_Declaration
6059         or else Kind = N_Subtype_Declaration
6060         or else (Ekind (E) = E_Enumeration_Literal
6061                   and then Is_Derived_Type (Etype (E)));
6062    end Is_Inherited_Operation;
6063
6064    -----------------------------
6065    -- Is_Library_Level_Entity --
6066    -----------------------------
6067
6068    function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
6069    begin
6070       --  The following is a small optimization, and it also properly handles
6071       --  discriminals, which in task bodies might appear in expressions before
6072       --  the corresponding procedure has been created, and which therefore do
6073       --  not have an assigned scope.
6074
6075       if Ekind (E) in Formal_Kind then
6076          return False;
6077       end if;
6078
6079       --  Normal test is simply that the enclosing dynamic scope is Standard
6080
6081       return Enclosing_Dynamic_Scope (E) = Standard_Standard;
6082    end Is_Library_Level_Entity;
6083
6084    ---------------------------------
6085    -- Is_Local_Variable_Reference --
6086    ---------------------------------
6087
6088    function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
6089    begin
6090       if not Is_Entity_Name (Expr) then
6091          return False;
6092
6093       else
6094          declare
6095             Ent : constant Entity_Id := Entity (Expr);
6096             Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
6097          begin
6098             if Ekind (Ent) /= E_Variable
6099                  and then
6100                Ekind (Ent) /= E_In_Out_Parameter
6101             then
6102                return False;
6103             else
6104                return Present (Sub) and then Sub = Current_Subprogram;
6105             end if;
6106          end;
6107       end if;
6108    end Is_Local_Variable_Reference;
6109
6110    -------------------------
6111    -- Is_Object_Reference --
6112    -------------------------
6113
6114    function Is_Object_Reference (N : Node_Id) return Boolean is
6115    begin
6116       if Is_Entity_Name (N) then
6117          return Present (Entity (N)) and then Is_Object (Entity (N));
6118
6119       else
6120          case Nkind (N) is
6121             when N_Indexed_Component | N_Slice =>
6122                return
6123                  Is_Object_Reference (Prefix (N))
6124                    or else Is_Access_Type (Etype (Prefix (N)));
6125
6126             --  In Ada95, a function call is a constant object; a procedure
6127             --  call is not.
6128
6129             when N_Function_Call =>
6130                return Etype (N) /= Standard_Void_Type;
6131
6132             --  A reference to the stream attribute Input is a function call
6133
6134             when N_Attribute_Reference =>
6135                return Attribute_Name (N) = Name_Input;
6136
6137             when N_Selected_Component =>
6138                return
6139                  Is_Object_Reference (Selector_Name (N))
6140                    and then
6141                      (Is_Object_Reference (Prefix (N))
6142                         or else Is_Access_Type (Etype (Prefix (N))));
6143
6144             when N_Explicit_Dereference =>
6145                return True;
6146
6147             --  A view conversion of a tagged object is an object reference
6148
6149             when N_Type_Conversion =>
6150                return Is_Tagged_Type (Etype (Subtype_Mark (N)))
6151                  and then Is_Tagged_Type (Etype (Expression (N)))
6152                  and then Is_Object_Reference (Expression (N));
6153
6154             --  An unchecked type conversion is considered to be an object if
6155             --  the operand is an object (this construction arises only as a
6156             --  result of expansion activities).
6157
6158             when N_Unchecked_Type_Conversion =>
6159                return True;
6160
6161             when others =>
6162                return False;
6163          end case;
6164       end if;
6165    end Is_Object_Reference;
6166
6167    -----------------------------------
6168    -- Is_OK_Variable_For_Out_Formal --
6169    -----------------------------------
6170
6171    function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
6172    begin
6173       Note_Possible_Modification (AV, Sure => True);
6174
6175       --  We must reject parenthesized variable names. The check for
6176       --  Comes_From_Source is present because there are currently
6177       --  cases where the compiler violates this rule (e.g. passing
6178       --  a task object to its controlled Initialize routine).
6179
6180       if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
6181          return False;
6182
6183       --  A variable is always allowed
6184
6185       elsif Is_Variable (AV) then
6186          return True;
6187
6188       --  Unchecked conversions are allowed only if they come from the
6189       --  generated code, which sometimes uses unchecked conversions for out
6190       --  parameters in cases where code generation is unaffected. We tell
6191       --  source unchecked conversions by seeing if they are rewrites of an
6192       --  original Unchecked_Conversion function call, or of an explicit
6193       --  conversion of a function call.
6194
6195       elsif Nkind (AV) = N_Unchecked_Type_Conversion then
6196          if Nkind (Original_Node (AV)) = N_Function_Call then
6197             return False;
6198
6199          elsif Comes_From_Source (AV)
6200            and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
6201          then
6202             return False;
6203
6204          elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
6205             return Is_OK_Variable_For_Out_Formal (Expression (AV));
6206
6207          else
6208             return True;
6209          end if;
6210
6211       --  Normal type conversions are allowed if argument is a variable
6212
6213       elsif Nkind (AV) = N_Type_Conversion then
6214          if Is_Variable (Expression (AV))
6215            and then Paren_Count (Expression (AV)) = 0
6216          then
6217             Note_Possible_Modification (Expression (AV), Sure => True);
6218             return True;
6219
6220          --  We also allow a non-parenthesized expression that raises
6221          --  constraint error if it rewrites what used to be a variable
6222
6223          elsif Raises_Constraint_Error (Expression (AV))
6224             and then Paren_Count (Expression (AV)) = 0
6225             and then Is_Variable (Original_Node (Expression (AV)))
6226          then
6227             return True;
6228
6229          --  Type conversion of something other than a variable
6230
6231          else
6232             return False;
6233          end if;
6234
6235       --  If this node is rewritten, then test the original form, if that is
6236       --  OK, then we consider the rewritten node OK (for example, if the
6237       --  original node is a conversion, then Is_Variable will not be true
6238       --  but we still want to allow the conversion if it converts a variable).
6239
6240       elsif Original_Node (AV) /= AV then
6241          return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
6242
6243       --  All other non-variables are rejected
6244
6245       else
6246          return False;
6247       end if;
6248    end Is_OK_Variable_For_Out_Formal;
6249
6250    -----------------------------------
6251    -- Is_Partially_Initialized_Type --
6252    -----------------------------------
6253
6254    function Is_Partially_Initialized_Type (Typ : Entity_Id) return Boolean is
6255    begin
6256       if Is_Scalar_Type (Typ) then
6257          return False;
6258
6259       elsif Is_Access_Type (Typ) then
6260          return True;
6261
6262       elsif Is_Array_Type (Typ) then
6263
6264          --  If component type is partially initialized, so is array type
6265
6266          if Is_Partially_Initialized_Type (Component_Type (Typ)) then
6267             return True;
6268
6269          --  Otherwise we are only partially initialized if we are fully
6270          --  initialized (this is the empty array case, no point in us
6271          --  duplicating that code here).
6272
6273          else
6274             return Is_Fully_Initialized_Type (Typ);
6275          end if;
6276
6277       elsif Is_Record_Type (Typ) then
6278
6279          --  A discriminated type is always partially initialized
6280
6281          if Has_Discriminants (Typ) then
6282             return True;
6283
6284          --  A tagged type is always partially initialized
6285
6286          elsif Is_Tagged_Type (Typ) then
6287             return True;
6288
6289          --  Case of non-discriminated record
6290
6291          else
6292             declare
6293                Ent : Entity_Id;
6294
6295                Component_Present : Boolean := False;
6296                --  Set True if at least one component is present. If no
6297                --  components are present, then record type is fully
6298                --  initialized (another odd case, like the null array).
6299
6300             begin
6301                --  Loop through components
6302
6303                Ent := First_Entity (Typ);
6304                while Present (Ent) loop
6305                   if Ekind (Ent) = E_Component then
6306                      Component_Present := True;
6307
6308                      --  If a component has an initialization expression then
6309                      --  the enclosing record type is partially initialized
6310
6311                      if Present (Parent (Ent))
6312                        and then Present (Expression (Parent (Ent)))
6313                      then
6314                         return True;
6315
6316                      --  If a component is of a type which is itself partially
6317                      --  initialized, then the enclosing record type is also.
6318
6319                      elsif Is_Partially_Initialized_Type (Etype (Ent)) then
6320                         return True;
6321                      end if;
6322                   end if;
6323
6324                   Next_Entity (Ent);
6325                end loop;
6326
6327                --  No initialized components found. If we found any components
6328                --  they were all uninitialized so the result is false.
6329
6330                if Component_Present then
6331                   return False;
6332
6333                --  But if we found no components, then all the components are
6334                --  initialized so we consider the type to be initialized.
6335
6336                else
6337                   return True;
6338                end if;
6339             end;
6340          end if;
6341
6342       --  Concurrent types are always fully initialized
6343
6344       elsif Is_Concurrent_Type (Typ) then
6345          return True;
6346
6347       --  For a private type, go to underlying type. If there is no underlying
6348       --  type then just assume this partially initialized. Not clear if this
6349       --  can happen in a non-error case, but no harm in testing for this.
6350
6351       elsif Is_Private_Type (Typ) then
6352          declare
6353             U : constant Entity_Id := Underlying_Type (Typ);
6354          begin
6355             if No (U) then
6356                return True;
6357             else
6358                return Is_Partially_Initialized_Type (U);
6359             end if;
6360          end;
6361
6362       --  For any other type (are there any?) assume partially initialized
6363
6364       else
6365          return True;
6366       end if;
6367    end Is_Partially_Initialized_Type;
6368
6369    ------------------------------------
6370    -- Is_Potentially_Persistent_Type --
6371    ------------------------------------
6372
6373    function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
6374       Comp : Entity_Id;
6375       Indx : Node_Id;
6376
6377    begin
6378       --  For private type, test corresponding full type
6379
6380       if Is_Private_Type (T) then
6381          return Is_Potentially_Persistent_Type (Full_View (T));
6382
6383       --  Scalar types are potentially persistent
6384
6385       elsif Is_Scalar_Type (T) then
6386          return True;
6387
6388       --  Record type is potentially persistent if not tagged and the types of
6389       --  all it components are potentially persistent, and no component has
6390       --  an initialization expression.
6391
6392       elsif Is_Record_Type (T)
6393         and then not Is_Tagged_Type (T)
6394         and then not Is_Partially_Initialized_Type (T)
6395       then
6396          Comp := First_Component (T);
6397          while Present (Comp) loop
6398             if not Is_Potentially_Persistent_Type (Etype (Comp)) then
6399                return False;
6400             else
6401                Next_Entity (Comp);
6402             end if;
6403          end loop;
6404
6405          return True;
6406
6407       --  Array type is potentially persistent if its component type is
6408       --  potentially persistent and if all its constraints are static.
6409
6410       elsif Is_Array_Type (T) then
6411          if not Is_Potentially_Persistent_Type (Component_Type (T)) then
6412             return False;
6413          end if;
6414
6415          Indx := First_Index (T);
6416          while Present (Indx) loop
6417             if not Is_OK_Static_Subtype (Etype (Indx)) then
6418                return False;
6419             else
6420                Next_Index (Indx);
6421             end if;
6422          end loop;
6423
6424          return True;
6425
6426       --  All other types are not potentially persistent
6427
6428       else
6429          return False;
6430       end if;
6431    end Is_Potentially_Persistent_Type;
6432
6433    ---------------------------------
6434    -- Is_Protected_Self_Reference --
6435    ---------------------------------
6436
6437    function Is_Protected_Self_Reference (N : Node_Id) return Boolean is
6438
6439       function In_Access_Definition (N : Node_Id) return Boolean;
6440       --  Returns true if N belongs to an access definition
6441
6442       --------------------------
6443       -- In_Access_Definition --
6444       --------------------------
6445
6446       function In_Access_Definition (N : Node_Id) return Boolean is
6447          P : Node_Id;
6448
6449       begin
6450          P := Parent (N);
6451          while Present (P) loop
6452             if Nkind (P) = N_Access_Definition then
6453                return True;
6454             end if;
6455
6456             P := Parent (P);
6457          end loop;
6458
6459          return False;
6460       end In_Access_Definition;
6461
6462    --  Start of processing for Is_Protected_Self_Reference
6463
6464    begin
6465       --  Verify that prefix is analyzed and has the proper form. Note that
6466       --  the attributes Elab_Spec, Elab_Body, and UET_Address, which also
6467       --  produce the address of an entity, do not analyze their prefix
6468       --  because they denote entities that are not necessarily visible.
6469       --  Neither of them can apply to a protected type.
6470
6471       return Ada_Version >= Ada_05
6472         and then Is_Entity_Name (N)
6473         and then Present (Entity (N))
6474         and then Is_Protected_Type (Entity (N))
6475         and then In_Open_Scopes (Entity (N))
6476         and then not In_Access_Definition (N);
6477    end Is_Protected_Self_Reference;
6478
6479    -----------------------------
6480    -- Is_RCI_Pkg_Spec_Or_Body --
6481    -----------------------------
6482
6483    function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
6484
6485       function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
6486       --  Return True if the unit of Cunit is an RCI package declaration
6487
6488       ---------------------------
6489       -- Is_RCI_Pkg_Decl_Cunit --
6490       ---------------------------
6491
6492       function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
6493          The_Unit : constant Node_Id := Unit (Cunit);
6494
6495       begin
6496          if Nkind (The_Unit) /= N_Package_Declaration then
6497             return False;
6498          end if;
6499
6500          return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
6501       end Is_RCI_Pkg_Decl_Cunit;
6502
6503    --  Start of processing for Is_RCI_Pkg_Spec_Or_Body
6504
6505    begin
6506       return Is_RCI_Pkg_Decl_Cunit (Cunit)
6507         or else
6508          (Nkind (Unit (Cunit)) = N_Package_Body
6509            and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
6510    end Is_RCI_Pkg_Spec_Or_Body;
6511
6512    -----------------------------------------
6513    -- Is_Remote_Access_To_Class_Wide_Type --
6514    -----------------------------------------
6515
6516    function Is_Remote_Access_To_Class_Wide_Type
6517      (E : Entity_Id) return Boolean
6518    is
6519    begin
6520       --  A remote access to class-wide type is a general access to object type
6521       --  declared in the visible part of a Remote_Types or Remote_Call_
6522       --  Interface unit.
6523
6524       return Ekind (E) = E_General_Access_Type
6525         and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
6526    end Is_Remote_Access_To_Class_Wide_Type;
6527
6528    -----------------------------------------
6529    -- Is_Remote_Access_To_Subprogram_Type --
6530    -----------------------------------------
6531
6532    function Is_Remote_Access_To_Subprogram_Type
6533      (E : Entity_Id) return Boolean
6534    is
6535    begin
6536       return (Ekind (E) = E_Access_Subprogram_Type
6537                 or else (Ekind (E) = E_Record_Type
6538                            and then Present (Corresponding_Remote_Type (E))))
6539         and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
6540    end Is_Remote_Access_To_Subprogram_Type;
6541
6542    --------------------
6543    -- Is_Remote_Call --
6544    --------------------
6545
6546    function Is_Remote_Call (N : Node_Id) return Boolean is
6547    begin
6548       if Nkind (N) /= N_Procedure_Call_Statement
6549         and then Nkind (N) /= N_Function_Call
6550       then
6551          --  An entry call cannot be remote
6552
6553          return False;
6554
6555       elsif Nkind (Name (N)) in N_Has_Entity
6556         and then Is_Remote_Call_Interface (Entity (Name (N)))
6557       then
6558          --  A subprogram declared in the spec of a RCI package is remote
6559
6560          return True;
6561
6562       elsif Nkind (Name (N)) = N_Explicit_Dereference
6563         and then Is_Remote_Access_To_Subprogram_Type
6564                    (Etype (Prefix (Name (N))))
6565       then
6566          --  The dereference of a RAS is a remote call
6567
6568          return True;
6569
6570       elsif Present (Controlling_Argument (N))
6571         and then Is_Remote_Access_To_Class_Wide_Type
6572           (Etype (Controlling_Argument (N)))
6573       then
6574          --  Any primitive operation call with a controlling argument of
6575          --  a RACW type is a remote call.
6576
6577          return True;
6578       end if;
6579
6580       --  All other calls are local calls
6581
6582       return False;
6583    end Is_Remote_Call;
6584
6585    ----------------------
6586    -- Is_Renamed_Entry --
6587    ----------------------
6588
6589    function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
6590       Orig_Node : Node_Id := Empty;
6591       Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
6592
6593       function Is_Entry (Nam : Node_Id) return Boolean;
6594       --  Determine whether Nam is an entry. Traverse selectors if there are
6595       --  nested selected components.
6596
6597       --------------
6598       -- Is_Entry --
6599       --------------
6600
6601       function Is_Entry (Nam : Node_Id) return Boolean is
6602       begin
6603          if Nkind (Nam) = N_Selected_Component then
6604             return Is_Entry (Selector_Name (Nam));
6605          end if;
6606
6607          return Ekind (Entity (Nam)) = E_Entry;
6608       end Is_Entry;
6609
6610    --  Start of processing for Is_Renamed_Entry
6611
6612    begin
6613       if Present (Alias (Proc_Nam)) then
6614          Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
6615       end if;
6616
6617       --  Look for a rewritten subprogram renaming declaration
6618
6619       if Nkind (Subp_Decl) = N_Subprogram_Declaration
6620         and then Present (Original_Node (Subp_Decl))
6621       then
6622          Orig_Node := Original_Node (Subp_Decl);
6623       end if;
6624
6625       --  The rewritten subprogram is actually an entry
6626
6627       if Present (Orig_Node)
6628         and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
6629         and then Is_Entry (Name (Orig_Node))
6630       then
6631          return True;
6632       end if;
6633
6634       return False;
6635    end Is_Renamed_Entry;
6636
6637    ----------------------
6638    -- Is_Selector_Name --
6639    ----------------------
6640
6641    function Is_Selector_Name (N : Node_Id) return Boolean is
6642    begin
6643       if not Is_List_Member (N) then
6644          declare
6645             P : constant Node_Id   := Parent (N);
6646             K : constant Node_Kind := Nkind (P);
6647          begin
6648             return
6649               (K = N_Expanded_Name          or else
6650                K = N_Generic_Association    or else
6651                K = N_Parameter_Association  or else
6652                K = N_Selected_Component)
6653               and then Selector_Name (P) = N;
6654          end;
6655
6656       else
6657          declare
6658             L : constant List_Id := List_Containing (N);
6659             P : constant Node_Id := Parent (L);
6660          begin
6661             return (Nkind (P) = N_Discriminant_Association
6662                      and then Selector_Names (P) = L)
6663               or else
6664                    (Nkind (P) = N_Component_Association
6665                      and then Choices (P) = L);
6666          end;
6667       end if;
6668    end Is_Selector_Name;
6669
6670    ------------------
6671    -- Is_Statement --
6672    ------------------
6673
6674    function Is_Statement (N : Node_Id) return Boolean is
6675    begin
6676       return
6677         Nkind (N) in N_Statement_Other_Than_Procedure_Call
6678           or else Nkind (N) = N_Procedure_Call_Statement;
6679    end Is_Statement;
6680
6681    ---------------------------------
6682    -- Is_Synchronized_Tagged_Type --
6683    ---------------------------------
6684
6685    function Is_Synchronized_Tagged_Type (E : Entity_Id) return Boolean is
6686       Kind : constant Entity_Kind := Ekind (Base_Type (E));
6687
6688    begin
6689       --  A task or protected type derived from an interface is a tagged type.
6690       --  Such a tagged type is called a synchronized tagged type, as are
6691       --  synchronized interfaces and private extensions whose declaration
6692       --  includes the reserved word synchronized.
6693
6694       return (Is_Tagged_Type (E)
6695                 and then (Kind = E_Task_Type
6696                            or else Kind = E_Protected_Type))
6697             or else
6698              (Is_Interface (E)
6699                 and then Is_Synchronized_Interface (E))
6700             or else
6701              (Ekind (E) = E_Record_Type_With_Private
6702                 and then (Synchronized_Present (Parent (E))
6703                            or else Is_Synchronized_Interface (Etype (E))));
6704    end Is_Synchronized_Tagged_Type;
6705
6706    -----------------
6707    -- Is_Transfer --
6708    -----------------
6709
6710    function Is_Transfer (N : Node_Id) return Boolean is
6711       Kind : constant Node_Kind := Nkind (N);
6712
6713    begin
6714       if Kind = N_Simple_Return_Statement
6715            or else
6716          Kind = N_Extended_Return_Statement
6717            or else
6718          Kind = N_Goto_Statement
6719            or else
6720          Kind = N_Raise_Statement
6721            or else
6722          Kind = N_Requeue_Statement
6723       then
6724          return True;
6725
6726       elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
6727         and then No (Condition (N))
6728       then
6729          return True;
6730
6731       elsif Kind = N_Procedure_Call_Statement
6732         and then Is_Entity_Name (Name (N))
6733         and then Present (Entity (Name (N)))
6734         and then No_Return (Entity (Name (N)))
6735       then
6736          return True;
6737
6738       elsif Nkind (Original_Node (N)) = N_Raise_Statement then
6739          return True;
6740
6741       else
6742          return False;
6743       end if;
6744    end Is_Transfer;
6745
6746    -------------
6747    -- Is_True --
6748    -------------
6749
6750    function Is_True (U : Uint) return Boolean is
6751    begin
6752       return (U /= 0);
6753    end Is_True;
6754
6755    -------------------
6756    -- Is_Value_Type --
6757    -------------------
6758
6759    function Is_Value_Type (T : Entity_Id) return Boolean is
6760    begin
6761       return VM_Target = CLI_Target
6762         and then Chars (T) /= No_Name
6763         and then Get_Name_String (Chars (T)) = "valuetype";
6764    end Is_Value_Type;
6765
6766    -----------------
6767    -- Is_Variable --
6768    -----------------
6769
6770    function Is_Variable (N : Node_Id) return Boolean is
6771
6772       Orig_Node : constant Node_Id := Original_Node (N);
6773       --  We do the test on the original node, since this is basically a
6774       --  test of syntactic categories, so it must not be disturbed by
6775       --  whatever rewriting might have occurred. For example, an aggregate,
6776       --  which is certainly NOT a variable, could be turned into a variable
6777       --  by expansion.
6778
6779       function In_Protected_Function (E : Entity_Id) return Boolean;
6780       --  Within a protected function, the private components of the
6781       --  enclosing protected type are constants. A function nested within
6782       --  a (protected) procedure is not itself protected.
6783
6784       function Is_Variable_Prefix (P : Node_Id) return Boolean;
6785       --  Prefixes can involve implicit dereferences, in which case we
6786       --  must test for the case of a reference of a constant access
6787       --  type, which can never be a variable.
6788
6789       ---------------------------
6790       -- In_Protected_Function --
6791       ---------------------------
6792
6793       function In_Protected_Function (E : Entity_Id) return Boolean is
6794          Prot : constant Entity_Id := Scope (E);
6795          S    : Entity_Id;
6796
6797       begin
6798          if not Is_Protected_Type (Prot) then
6799             return False;
6800          else
6801             S := Current_Scope;
6802             while Present (S) and then S /= Prot loop
6803                if Ekind (S) = E_Function
6804                  and then Scope (S) = Prot
6805                then
6806                   return True;
6807                end if;
6808
6809                S := Scope (S);
6810             end loop;
6811
6812             return False;
6813          end if;
6814       end In_Protected_Function;
6815
6816       ------------------------
6817       -- Is_Variable_Prefix --
6818       ------------------------
6819
6820       function Is_Variable_Prefix (P : Node_Id) return Boolean is
6821       begin
6822          if Is_Access_Type (Etype (P)) then
6823             return not Is_Access_Constant (Root_Type (Etype (P)));
6824
6825          --  For the case of an indexed component whose prefix has a packed
6826          --  array type, the prefix has been rewritten into a type conversion.
6827          --  Determine variable-ness from the converted expression.
6828
6829          elsif Nkind (P) = N_Type_Conversion
6830            and then not Comes_From_Source (P)
6831            and then Is_Array_Type (Etype (P))
6832            and then Is_Packed (Etype (P))
6833          then
6834             return Is_Variable (Expression (P));
6835
6836          else
6837             return Is_Variable (P);
6838          end if;
6839       end Is_Variable_Prefix;
6840
6841    --  Start of processing for Is_Variable
6842
6843    begin
6844       --  Definitely OK if Assignment_OK is set. Since this is something that
6845       --  only gets set for expanded nodes, the test is on N, not Orig_Node.
6846
6847       if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
6848          return True;
6849
6850       --  Normally we go to the original node, but there is one exception
6851       --  where we use the rewritten node, namely when it is an explicit
6852       --  dereference. The generated code may rewrite a prefix which is an
6853       --  access type with an explicit dereference. The dereference is a
6854       --  variable, even though the original node may not be (since it could
6855       --  be a constant of the access type).
6856
6857       --  In Ada 2005 we have a further case to consider: the prefix may be
6858       --  a function call given in prefix notation. The original node appears
6859       --  to be a selected component, but we need to examine the call.
6860
6861       elsif Nkind (N) = N_Explicit_Dereference
6862         and then Nkind (Orig_Node) /= N_Explicit_Dereference
6863         and then Present (Etype (Orig_Node))
6864         and then Is_Access_Type (Etype (Orig_Node))
6865       then
6866          --  Note that if the prefix is an explicit dereference that does not
6867          --  come from source, we must check for a rewritten function call in
6868          --  prefixed notation before other forms of rewriting, to prevent a
6869          --  compiler crash.
6870
6871          return
6872            (Nkind (Orig_Node) = N_Function_Call
6873              and then not Is_Access_Constant (Etype (Prefix (N))))
6874            or else
6875              Is_Variable_Prefix (Original_Node (Prefix (N)));
6876
6877       --  A function call is never a variable
6878
6879       elsif Nkind (N) = N_Function_Call then
6880          return False;
6881
6882       --  All remaining checks use the original node
6883
6884       elsif Is_Entity_Name (Orig_Node)
6885         and then Present (Entity (Orig_Node))
6886       then
6887          declare
6888             E : constant Entity_Id := Entity (Orig_Node);
6889             K : constant Entity_Kind := Ekind (E);
6890
6891          begin
6892             return (K = E_Variable
6893                       and then Nkind (Parent (E)) /= N_Exception_Handler)
6894               or else  (K = E_Component
6895                           and then not In_Protected_Function (E))
6896               or else  K = E_Out_Parameter
6897               or else  K = E_In_Out_Parameter
6898               or else  K = E_Generic_In_Out_Parameter
6899
6900                --  Current instance of type:
6901
6902               or else (Is_Type (E) and then In_Open_Scopes (E))
6903               or else (Is_Incomplete_Or_Private_Type (E)
6904                         and then In_Open_Scopes (Full_View (E)));
6905          end;
6906
6907       else
6908          case Nkind (Orig_Node) is
6909             when N_Indexed_Component | N_Slice =>
6910                return Is_Variable_Prefix (Prefix (Orig_Node));
6911
6912             when N_Selected_Component =>
6913                return Is_Variable_Prefix (Prefix (Orig_Node))
6914                  and then Is_Variable (Selector_Name (Orig_Node));
6915
6916             --  For an explicit dereference, the type of the prefix cannot
6917             --  be an access to constant or an access to subprogram.
6918
6919             when N_Explicit_Dereference =>
6920                declare
6921                   Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
6922                begin
6923                   return Is_Access_Type (Typ)
6924                     and then not Is_Access_Constant (Root_Type (Typ))
6925                     and then Ekind (Typ) /= E_Access_Subprogram_Type;
6926                end;
6927
6928             --  The type conversion is the case where we do not deal with the
6929             --  context dependent special case of an actual parameter. Thus
6930             --  the type conversion is only considered a variable for the
6931             --  purposes of this routine if the target type is tagged. However,
6932             --  a type conversion is considered to be a variable if it does not
6933             --  come from source (this deals for example with the conversions
6934             --  of expressions to their actual subtypes).
6935
6936             when N_Type_Conversion =>
6937                return Is_Variable (Expression (Orig_Node))
6938                  and then
6939                    (not Comes_From_Source (Orig_Node)
6940                       or else
6941                         (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
6942                           and then
6943                          Is_Tagged_Type (Etype (Expression (Orig_Node)))));
6944
6945             --  GNAT allows an unchecked type conversion as a variable. This
6946             --  only affects the generation of internal expanded code, since
6947             --  calls to instantiations of Unchecked_Conversion are never
6948             --  considered variables (since they are function calls).
6949             --  This is also true for expression actions.
6950
6951             when N_Unchecked_Type_Conversion =>
6952                return Is_Variable (Expression (Orig_Node));
6953
6954             when others =>
6955                return False;
6956          end case;
6957       end if;
6958    end Is_Variable;
6959
6960    ------------------------
6961    -- Is_Volatile_Object --
6962    ------------------------
6963
6964    function Is_Volatile_Object (N : Node_Id) return Boolean is
6965
6966       function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
6967       --  Determines if given object has volatile components
6968
6969       function Is_Volatile_Prefix (N : Node_Id) return Boolean;
6970       --  If prefix is an implicit dereference, examine designated type
6971
6972       ------------------------
6973       -- Is_Volatile_Prefix --
6974       ------------------------
6975
6976       function Is_Volatile_Prefix (N : Node_Id) return Boolean is
6977          Typ  : constant Entity_Id := Etype (N);
6978
6979       begin
6980          if Is_Access_Type (Typ) then
6981             declare
6982                Dtyp : constant Entity_Id := Designated_Type (Typ);
6983
6984             begin
6985                return Is_Volatile (Dtyp)
6986                  or else Has_Volatile_Components (Dtyp);
6987             end;
6988
6989          else
6990             return Object_Has_Volatile_Components (N);
6991          end if;
6992       end Is_Volatile_Prefix;
6993
6994       ------------------------------------
6995       -- Object_Has_Volatile_Components --
6996       ------------------------------------
6997
6998       function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
6999          Typ : constant Entity_Id := Etype (N);
7000
7001       begin
7002          if Is_Volatile (Typ)
7003            or else Has_Volatile_Components (Typ)
7004          then
7005             return True;
7006
7007          elsif Is_Entity_Name (N)
7008            and then (Has_Volatile_Components (Entity (N))
7009                       or else Is_Volatile (Entity (N)))
7010          then
7011             return True;
7012
7013          elsif Nkind (N) = N_Indexed_Component
7014            or else Nkind (N) = N_Selected_Component
7015          then
7016             return Is_Volatile_Prefix (Prefix (N));
7017
7018          else
7019             return False;
7020          end if;
7021       end Object_Has_Volatile_Components;
7022
7023    --  Start of processing for Is_Volatile_Object
7024
7025    begin
7026       if Is_Volatile (Etype (N))
7027         or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
7028       then
7029          return True;
7030
7031       elsif Nkind (N) = N_Indexed_Component
7032         or else Nkind (N) = N_Selected_Component
7033       then
7034          return Is_Volatile_Prefix (Prefix (N));
7035
7036       else
7037          return False;
7038       end if;
7039    end Is_Volatile_Object;
7040
7041    -------------------------
7042    -- Kill_Current_Values --
7043    -------------------------
7044
7045    procedure Kill_Current_Values
7046      (Ent                  : Entity_Id;
7047       Last_Assignment_Only : Boolean := False)
7048    is
7049    begin
7050       if Is_Assignable (Ent) then
7051          Set_Last_Assignment (Ent, Empty);
7052       end if;
7053
7054       if not Last_Assignment_Only and then Is_Object (Ent) then
7055          Kill_Checks (Ent);
7056          Set_Current_Value (Ent, Empty);
7057
7058          if not Can_Never_Be_Null (Ent) then
7059             Set_Is_Known_Non_Null (Ent, False);
7060          end if;
7061
7062          Set_Is_Known_Null (Ent, False);
7063       end if;
7064    end Kill_Current_Values;
7065
7066    procedure Kill_Current_Values (Last_Assignment_Only : Boolean := False) is
7067       S : Entity_Id;
7068
7069       procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
7070       --  Clear current value for entity E and all entities chained to E
7071
7072       ------------------------------------------
7073       -- Kill_Current_Values_For_Entity_Chain --
7074       ------------------------------------------
7075
7076       procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
7077          Ent : Entity_Id;
7078       begin
7079          Ent := E;
7080          while Present (Ent) loop
7081             Kill_Current_Values (Ent, Last_Assignment_Only);
7082             Next_Entity (Ent);
7083          end loop;
7084       end Kill_Current_Values_For_Entity_Chain;
7085
7086    --  Start of processing for Kill_Current_Values
7087
7088    begin
7089       --  Kill all saved checks, a special case of killing saved values
7090
7091       if not Last_Assignment_Only then
7092          Kill_All_Checks;
7093       end if;
7094
7095       --  Loop through relevant scopes, which includes the current scope and
7096       --  any parent scopes if the current scope is a block or a package.
7097
7098       S := Current_Scope;
7099       Scope_Loop : loop
7100
7101          --  Clear current values of all entities in current scope
7102
7103          Kill_Current_Values_For_Entity_Chain (First_Entity (S));
7104
7105          --  If scope is a package, also clear current values of all
7106          --  private entities in the scope.
7107
7108          if Is_Package_Or_Generic_Package (S)
7109            or else Is_Concurrent_Type (S)
7110          then
7111             Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
7112          end if;
7113
7114          --  If this is a not a subprogram, deal with parents
7115
7116          if not Is_Subprogram (S) then
7117             S := Scope (S);
7118             exit Scope_Loop when S = Standard_Standard;
7119          else
7120             exit Scope_Loop;
7121          end if;
7122       end loop Scope_Loop;
7123    end Kill_Current_Values;
7124
7125    --------------------------
7126    -- Kill_Size_Check_Code --
7127    --------------------------
7128
7129    procedure Kill_Size_Check_Code (E : Entity_Id) is
7130    begin
7131       if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
7132         and then Present (Size_Check_Code (E))
7133       then
7134          Remove (Size_Check_Code (E));
7135          Set_Size_Check_Code (E, Empty);
7136       end if;
7137    end Kill_Size_Check_Code;
7138
7139    --------------------------
7140    -- Known_To_Be_Assigned --
7141    --------------------------
7142
7143    function Known_To_Be_Assigned (N : Node_Id) return Boolean is
7144       P : constant Node_Id := Parent (N);
7145
7146    begin
7147       case Nkind (P) is
7148
7149          --  Test left side of assignment
7150
7151          when N_Assignment_Statement =>
7152             return N = Name (P);
7153
7154             --  Function call arguments are never lvalues
7155
7156          when N_Function_Call =>
7157             return False;
7158
7159          --  Positional parameter for procedure or accept call
7160
7161          when N_Procedure_Call_Statement |
7162               N_Accept_Statement
7163           =>
7164             declare
7165                Proc : Entity_Id;
7166                Form : Entity_Id;
7167                Act  : Node_Id;
7168
7169             begin
7170                Proc := Get_Subprogram_Entity (P);
7171
7172                if No (Proc) then
7173                   return False;
7174                end if;
7175
7176                --  If we are not a list member, something is strange, so
7177                --  be conservative and return False.
7178
7179                if not Is_List_Member (N) then
7180                   return False;
7181                end if;
7182
7183                --  We are going to find the right formal by stepping forward
7184                --  through the formals, as we step backwards in the actuals.
7185
7186                Form := First_Formal (Proc);
7187                Act  := N;
7188                loop
7189                   --  If no formal, something is weird, so be conservative
7190                   --  and return False.
7191
7192                   if No (Form) then
7193                      return False;
7194                   end if;
7195
7196                   Prev (Act);
7197                   exit when No (Act);
7198                   Next_Formal (Form);
7199                end loop;
7200
7201                return Ekind (Form) /= E_In_Parameter;
7202             end;
7203
7204          --  Named parameter for procedure or accept call
7205
7206          when N_Parameter_Association =>
7207             declare
7208                Proc : Entity_Id;
7209                Form : Entity_Id;
7210
7211             begin
7212                Proc := Get_Subprogram_Entity (Parent (P));
7213
7214                if No (Proc) then
7215                   return False;
7216                end if;
7217
7218                --  Loop through formals to find the one that matches
7219
7220                Form := First_Formal (Proc);
7221                loop
7222                   --  If no matching formal, that's peculiar, some kind of
7223                   --  previous error, so return False to be conservative.
7224
7225                   if No (Form) then
7226                      return False;
7227                   end if;
7228
7229                   --  Else test for match
7230
7231                   if Chars (Form) = Chars (Selector_Name (P)) then
7232                      return Ekind (Form) /= E_In_Parameter;
7233                   end if;
7234
7235                   Next_Formal (Form);
7236                end loop;
7237             end;
7238
7239          --  Test for appearing in a conversion that itself appears
7240          --  in an lvalue context, since this should be an lvalue.
7241
7242          when N_Type_Conversion =>
7243             return Known_To_Be_Assigned (P);
7244
7245          --  All other references are definitely not known to be modifications
7246
7247          when others =>
7248             return False;
7249
7250       end case;
7251    end Known_To_Be_Assigned;
7252
7253    -------------------
7254    -- May_Be_Lvalue --
7255    -------------------
7256
7257    function May_Be_Lvalue (N : Node_Id) return Boolean is
7258       P : constant Node_Id := Parent (N);
7259
7260    begin
7261       case Nkind (P) is
7262
7263          --  Test left side of assignment
7264
7265          when N_Assignment_Statement =>
7266             return N = Name (P);
7267
7268          --  Test prefix of component or attribute. Note that the prefix of an
7269          --  explicit or implicit dereference cannot be an l-value.
7270
7271          when N_Attribute_Reference =>
7272             return N = Prefix (P)
7273               and then Name_Implies_Lvalue_Prefix (Attribute_Name (P));
7274
7275          --  For an expanded name, the name is an lvalue if the expanded name
7276          --  is an lvalue, but the prefix is never an lvalue, since it is just
7277          --  the scope where the name is found.
7278
7279          when N_Expanded_Name        =>
7280             if N = Prefix (P) then
7281                return May_Be_Lvalue (P);
7282             else
7283                return False;
7284             end if;
7285
7286          --  For a selected component A.B, A is certainly an Lvalue if A.B is
7287          --  an Lvalue. B is a little interesting, if we have A.B:=3, there is
7288          --  some discussion as to whether B is an Lvalue or not, we choose to
7289          --  say it is. Note however that A is not an Lvalue if it is of an
7290          --  access type since this is an implicit dereference.
7291
7292          when N_Selected_Component   =>
7293             if N = Prefix (P)
7294               and then Present (Etype (N))
7295               and then Is_Access_Type (Etype (N))
7296             then
7297                return False;
7298             else
7299                return May_Be_Lvalue (P);
7300             end if;
7301
7302          --  For an indexed component or slice, the index or slice bounds is
7303          --  never an Lvalue. The prefix is an lvalue if the indexed component
7304          --  or slice is an Lvalue, except if it is an access type, where we
7305          --  have an implicit dereference.
7306
7307          when N_Indexed_Component    =>
7308             if N /= Prefix (P)
7309               or else (Present (Etype (N)) and then Is_Access_Type (Etype (N)))
7310             then
7311                return False;
7312             else
7313                return May_Be_Lvalue (P);
7314             end if;
7315
7316          --  Prefix of a reference is an Lvalue if the reference is an Lvalue
7317
7318          when N_Reference            =>
7319             return May_Be_Lvalue (P);
7320
7321          --  Prefix of explicit dereference is never an Lvalue
7322
7323          when N_Explicit_Dereference =>
7324             return False;
7325
7326          --  Function call arguments are never Lvalues
7327
7328          when N_Function_Call =>
7329             return False;
7330
7331          --  Positional parameter for procedure, entry,  or accept call
7332
7333          when N_Procedure_Call_Statement |
7334               N_Entry_Call_Statement     |
7335               N_Accept_Statement
7336          =>
7337             declare
7338                Proc : Entity_Id;
7339                Form : Entity_Id;
7340                Act  : Node_Id;
7341
7342             begin
7343                Proc := Get_Subprogram_Entity (P);
7344
7345                if No (Proc) then
7346                   return True;
7347                end if;
7348
7349                --  If we are not a list member, something is strange, so
7350                --  be conservative and return True.
7351
7352                if not Is_List_Member (N) then
7353                   return True;
7354                end if;
7355
7356                --  We are going to find the right formal by stepping forward
7357                --  through the formals, as we step backwards in the actuals.
7358
7359                Form := First_Formal (Proc);
7360                Act  := N;
7361                loop
7362                   --  If no formal, something is weird, so be conservative
7363                   --  and return True.
7364
7365                   if No (Form) then
7366                      return True;
7367                   end if;
7368
7369                   Prev (Act);
7370                   exit when No (Act);
7371                   Next_Formal (Form);
7372                end loop;
7373
7374                return Ekind (Form) /= E_In_Parameter;
7375             end;
7376
7377          --  Named parameter for procedure or accept call
7378
7379          when N_Parameter_Association =>
7380             declare
7381                Proc : Entity_Id;
7382                Form : Entity_Id;
7383
7384             begin
7385                Proc := Get_Subprogram_Entity (Parent (P));
7386
7387                if No (Proc) then
7388                   return True;
7389                end if;
7390
7391                --  Loop through formals to find the one that matches
7392
7393                Form := First_Formal (Proc);
7394                loop
7395                   --  If no matching formal, that's peculiar, some kind of
7396                   --  previous error, so return True to be conservative.
7397
7398                   if No (Form) then
7399                      return True;
7400                   end if;
7401
7402                   --  Else test for match
7403
7404                   if Chars (Form) = Chars (Selector_Name (P)) then
7405                      return Ekind (Form) /= E_In_Parameter;
7406                   end if;
7407
7408                   Next_Formal (Form);
7409                end loop;
7410             end;
7411
7412          --  Test for appearing in a conversion that itself appears in an
7413          --  lvalue context, since this should be an lvalue.
7414
7415          when N_Type_Conversion =>
7416             return May_Be_Lvalue (P);
7417
7418          --  Test for appearance in object renaming declaration
7419
7420          when N_Object_Renaming_Declaration =>
7421             return True;
7422
7423          --  All other references are definitely not Lvalues
7424
7425          when others =>
7426             return False;
7427
7428       end case;
7429    end May_Be_Lvalue;
7430
7431    -----------------------
7432    -- Mark_Coextensions --
7433    -----------------------
7434
7435    procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
7436       Is_Dynamic : Boolean;
7437       --  Indicates whether the context causes nested coextensions to be
7438       --  dynamic or static
7439
7440       function Mark_Allocator (N : Node_Id) return Traverse_Result;
7441       --  Recognize an allocator node and label it as a dynamic coextension
7442
7443       --------------------
7444       -- Mark_Allocator --
7445       --------------------
7446
7447       function Mark_Allocator (N : Node_Id) return Traverse_Result is
7448       begin
7449          if Nkind (N) = N_Allocator then
7450             if Is_Dynamic then
7451                Set_Is_Dynamic_Coextension (N);
7452             else
7453                Set_Is_Static_Coextension (N);
7454             end if;
7455          end if;
7456
7457          return OK;
7458       end Mark_Allocator;
7459
7460       procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
7461
7462    --  Start of processing Mark_Coextensions
7463
7464    begin
7465       case Nkind (Context_Nod) is
7466          when N_Assignment_Statement    |
7467               N_Simple_Return_Statement =>
7468             Is_Dynamic := Nkind (Expression (Context_Nod)) = N_Allocator;
7469
7470          when N_Object_Declaration =>
7471             Is_Dynamic := Nkind (Root_Nod) = N_Allocator;
7472
7473          --  This routine should not be called for constructs which may not
7474          --  contain coextensions.
7475
7476          when others =>
7477             raise Program_Error;
7478       end case;
7479
7480       Mark_Allocators (Root_Nod);
7481    end Mark_Coextensions;
7482
7483    ----------------------
7484    -- Needs_One_Actual --
7485    ----------------------
7486
7487    function Needs_One_Actual (E : Entity_Id) return Boolean is
7488       Formal : Entity_Id;
7489
7490    begin
7491       if Ada_Version >= Ada_05
7492         and then Present (First_Formal (E))
7493       then
7494          Formal := Next_Formal (First_Formal (E));
7495          while Present (Formal) loop
7496             if No (Default_Value (Formal)) then
7497                return False;
7498             end if;
7499
7500             Next_Formal (Formal);
7501          end loop;
7502
7503          return True;
7504
7505       else
7506          return False;
7507       end if;
7508    end Needs_One_Actual;
7509
7510    ------------------------
7511    -- New_Copy_List_Tree --
7512    ------------------------
7513
7514    function New_Copy_List_Tree (List : List_Id) return List_Id is
7515       NL : List_Id;
7516       E  : Node_Id;
7517
7518    begin
7519       if List = No_List then
7520          return No_List;
7521
7522       else
7523          NL := New_List;
7524          E := First (List);
7525
7526          while Present (E) loop
7527             Append (New_Copy_Tree (E), NL);
7528             E := Next (E);
7529          end loop;
7530
7531          return NL;
7532       end if;
7533    end New_Copy_List_Tree;
7534
7535    -------------------
7536    -- New_Copy_Tree --
7537    -------------------
7538
7539    use Atree.Unchecked_Access;
7540    use Atree_Private_Part;
7541
7542    --  Our approach here requires a two pass traversal of the tree. The
7543    --  first pass visits all nodes that eventually will be copied looking
7544    --  for defining Itypes. If any defining Itypes are found, then they are
7545    --  copied, and an entry is added to the replacement map. In the second
7546    --  phase, the tree is copied, using the replacement map to replace any
7547    --  Itype references within the copied tree.
7548
7549    --  The following hash tables are used if the Map supplied has more
7550    --  than hash threshhold entries to speed up access to the map. If
7551    --  there are fewer entries, then the map is searched sequentially
7552    --  (because setting up a hash table for only a few entries takes
7553    --  more time than it saves.
7554
7555    function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num;
7556    --  Hash function used for hash operations
7557
7558    -------------------
7559    -- New_Copy_Hash --
7560    -------------------
7561
7562    function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num is
7563    begin
7564       return Nat (E) mod (NCT_Header_Num'Last + 1);
7565    end New_Copy_Hash;
7566
7567    ---------------
7568    -- NCT_Assoc --
7569    ---------------
7570
7571    --  The hash table NCT_Assoc associates old entities in the table
7572    --  with their corresponding new entities (i.e. the pairs of entries
7573    --  presented in the original Map argument are Key-Element pairs).
7574
7575    package NCT_Assoc is new Simple_HTable (
7576      Header_Num => NCT_Header_Num,
7577      Element    => Entity_Id,
7578      No_Element => Empty,
7579      Key        => Entity_Id,
7580      Hash       => New_Copy_Hash,
7581      Equal      => Types."=");
7582
7583    ---------------------
7584    -- NCT_Itype_Assoc --
7585    ---------------------
7586
7587    --  The hash table NCT_Itype_Assoc contains entries only for those
7588    --  old nodes which have a non-empty Associated_Node_For_Itype set.
7589    --  The key is the associated node, and the element is the new node
7590    --  itself (NOT the associated node for the new node).
7591
7592    package NCT_Itype_Assoc is new Simple_HTable (
7593      Header_Num => NCT_Header_Num,
7594      Element    => Entity_Id,
7595      No_Element => Empty,
7596      Key        => Entity_Id,
7597      Hash       => New_Copy_Hash,
7598      Equal      => Types."=");
7599
7600    --  Start of processing for New_Copy_Tree function
7601
7602    function New_Copy_Tree
7603      (Source    : Node_Id;
7604       Map       : Elist_Id := No_Elist;
7605       New_Sloc  : Source_Ptr := No_Location;
7606       New_Scope : Entity_Id := Empty) return Node_Id
7607    is
7608       Actual_Map : Elist_Id := Map;
7609       --  This is the actual map for the copy. It is initialized with the
7610       --  given elements, and then enlarged as required for Itypes that are
7611       --  copied during the first phase of the copy operation. The visit
7612       --  procedures add elements to this map as Itypes are encountered.
7613       --  The reason we cannot use Map directly, is that it may well be
7614       --  (and normally is) initialized to No_Elist, and if we have mapped
7615       --  entities, we have to reset it to point to a real Elist.
7616
7617       function Assoc (N : Node_Or_Entity_Id) return Node_Id;
7618       --  Called during second phase to map entities into their corresponding
7619       --  copies using Actual_Map. If the argument is not an entity, or is not
7620       --  in Actual_Map, then it is returned unchanged.
7621
7622       procedure Build_NCT_Hash_Tables;
7623       --  Builds hash tables (number of elements >= threshold value)
7624
7625       function Copy_Elist_With_Replacement
7626         (Old_Elist : Elist_Id) return Elist_Id;
7627       --  Called during second phase to copy element list doing replacements
7628
7629       procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id);
7630       --  Called during the second phase to process a copied Itype. The actual
7631       --  copy happened during the first phase (so that we could make the entry
7632       --  in the mapping), but we still have to deal with the descendents of
7633       --  the copied Itype and copy them where necessary.
7634
7635       function Copy_List_With_Replacement (Old_List : List_Id) return List_Id;
7636       --  Called during second phase to copy list doing replacements
7637
7638       function Copy_Node_With_Replacement (Old_Node : Node_Id) return Node_Id;
7639       --  Called during second phase to copy node doing replacements
7640
7641       procedure Visit_Elist (E : Elist_Id);
7642       --  Called during first phase to visit all elements of an Elist
7643
7644       procedure Visit_Field (F : Union_Id; N : Node_Id);
7645       --  Visit a single field, recursing to call Visit_Node or Visit_List
7646       --  if the field is a syntactic descendent of the current node (i.e.
7647       --  its parent is Node N).
7648
7649       procedure Visit_Itype (Old_Itype : Entity_Id);
7650       --  Called during first phase to visit subsidiary fields of a defining
7651       --  Itype, and also create a copy and make an entry in the replacement
7652       --  map for the new copy.
7653
7654       procedure Visit_List (L : List_Id);
7655       --  Called during first phase to visit all elements of a List
7656
7657       procedure Visit_Node (N : Node_Or_Entity_Id);
7658       --  Called during first phase to visit a node and all its subtrees
7659
7660       -----------
7661       -- Assoc --
7662       -----------
7663
7664       function Assoc (N : Node_Or_Entity_Id) return Node_Id is
7665          E   : Elmt_Id;
7666          Ent : Entity_Id;
7667
7668       begin
7669          if not Has_Extension (N) or else No (Actual_Map) then
7670             return N;
7671
7672          elsif NCT_Hash_Tables_Used then
7673             Ent := NCT_Assoc.Get (Entity_Id (N));
7674
7675             if Present (Ent) then
7676                return Ent;
7677             else
7678                return N;
7679             end if;
7680
7681          --  No hash table used, do serial search
7682
7683          else
7684             E := First_Elmt (Actual_Map);
7685             while Present (E) loop
7686                if Node (E) = N then
7687                   return Node (Next_Elmt (E));
7688                else
7689                   E := Next_Elmt (Next_Elmt (E));
7690                end if;
7691             end loop;
7692          end if;
7693
7694          return N;
7695       end Assoc;
7696
7697       ---------------------------
7698       -- Build_NCT_Hash_Tables --
7699       ---------------------------
7700
7701       procedure Build_NCT_Hash_Tables is
7702          Elmt : Elmt_Id;
7703          Ent  : Entity_Id;
7704       begin
7705          if NCT_Hash_Table_Setup then
7706             NCT_Assoc.Reset;
7707             NCT_Itype_Assoc.Reset;
7708          end if;
7709
7710          Elmt := First_Elmt (Actual_Map);
7711          while Present (Elmt) loop
7712             Ent := Node (Elmt);
7713
7714             --  Get new entity, and associate old and new
7715
7716             Next_Elmt (Elmt);
7717             NCT_Assoc.Set (Ent, Node (Elmt));
7718
7719             if Is_Type (Ent) then
7720                declare
7721                   Anode : constant Entity_Id :=
7722                             Associated_Node_For_Itype (Ent);
7723
7724                begin
7725                   if Present (Anode) then
7726
7727                      --  Enter a link between the associated node of the
7728                      --  old Itype and the new Itype, for updating later
7729                      --  when node is copied.
7730
7731                      NCT_Itype_Assoc.Set (Anode, Node (Elmt));
7732                   end if;
7733                end;
7734             end if;
7735
7736             Next_Elmt (Elmt);
7737          end loop;
7738
7739          NCT_Hash_Tables_Used := True;
7740          NCT_Hash_Table_Setup := True;
7741       end Build_NCT_Hash_Tables;
7742
7743       ---------------------------------
7744       -- Copy_Elist_With_Replacement --
7745       ---------------------------------
7746
7747       function Copy_Elist_With_Replacement
7748         (Old_Elist : Elist_Id) return Elist_Id
7749       is
7750          M         : Elmt_Id;
7751          New_Elist : Elist_Id;
7752
7753       begin
7754          if No (Old_Elist) then
7755             return No_Elist;
7756
7757          else
7758             New_Elist := New_Elmt_List;
7759
7760             M := First_Elmt (Old_Elist);
7761             while Present (M) loop
7762                Append_Elmt (Copy_Node_With_Replacement (Node (M)), New_Elist);
7763                Next_Elmt (M);
7764             end loop;
7765          end if;
7766
7767          return New_Elist;
7768       end Copy_Elist_With_Replacement;
7769
7770       ---------------------------------
7771       -- Copy_Itype_With_Replacement --
7772       ---------------------------------
7773
7774       --  This routine exactly parallels its phase one analog Visit_Itype,
7775
7776       procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id) is
7777       begin
7778          --  Translate Next_Entity, Scope and Etype fields, in case they
7779          --  reference entities that have been mapped into copies.
7780
7781          Set_Next_Entity (New_Itype, Assoc (Next_Entity (New_Itype)));
7782          Set_Etype       (New_Itype, Assoc (Etype       (New_Itype)));
7783
7784          if Present (New_Scope) then
7785             Set_Scope    (New_Itype, New_Scope);
7786          else
7787             Set_Scope    (New_Itype, Assoc (Scope       (New_Itype)));
7788          end if;
7789
7790          --  Copy referenced fields
7791
7792          if Is_Discrete_Type (New_Itype) then
7793             Set_Scalar_Range (New_Itype,
7794               Copy_Node_With_Replacement (Scalar_Range (New_Itype)));
7795
7796          elsif Has_Discriminants (Base_Type (New_Itype)) then
7797             Set_Discriminant_Constraint (New_Itype,
7798               Copy_Elist_With_Replacement
7799                 (Discriminant_Constraint (New_Itype)));
7800
7801          elsif Is_Array_Type (New_Itype) then
7802             if Present (First_Index (New_Itype)) then
7803                Set_First_Index (New_Itype,
7804                  First (Copy_List_With_Replacement
7805                          (List_Containing (First_Index (New_Itype)))));
7806             end if;
7807
7808             if Is_Packed (New_Itype) then
7809                Set_Packed_Array_Type (New_Itype,
7810                  Copy_Node_With_Replacement
7811                    (Packed_Array_Type (New_Itype)));
7812             end if;
7813          end if;
7814       end Copy_Itype_With_Replacement;
7815
7816       --------------------------------
7817       -- Copy_List_With_Replacement --
7818       --------------------------------
7819
7820       function Copy_List_With_Replacement
7821         (Old_List : List_Id) return List_Id
7822       is
7823          New_List : List_Id;
7824          E        : Node_Id;
7825
7826       begin
7827          if Old_List = No_List then
7828             return No_List;
7829
7830          else
7831             New_List := Empty_List;
7832
7833             E := First (Old_List);
7834             while Present (E) loop
7835                Append (Copy_Node_With_Replacement (E), New_List);
7836                Next (E);
7837             end loop;
7838
7839             return New_List;
7840          end if;
7841       end Copy_List_With_Replacement;
7842
7843       --------------------------------
7844       -- Copy_Node_With_Replacement --
7845       --------------------------------
7846
7847       function Copy_Node_With_Replacement
7848         (Old_Node : Node_Id) return Node_Id
7849       is
7850          New_Node : Node_Id;
7851
7852          procedure Adjust_Named_Associations
7853            (Old_Node : Node_Id;
7854             New_Node : Node_Id);
7855          --  If a call node has named associations, these are chained through
7856          --  the First_Named_Actual, Next_Named_Actual links. These must be
7857          --  propagated separately to the new parameter list, because these
7858          --  are not syntactic fields.
7859
7860          function Copy_Field_With_Replacement
7861            (Field : Union_Id) return Union_Id;
7862          --  Given Field, which is a field of Old_Node, return a copy of it
7863          --  if it is a syntactic field (i.e. its parent is Node), setting
7864          --  the parent of the copy to poit to New_Node. Otherwise returns
7865          --  the field (possibly mapped if it is an entity).
7866
7867          -------------------------------
7868          -- Adjust_Named_Associations --
7869          -------------------------------
7870
7871          procedure Adjust_Named_Associations
7872            (Old_Node : Node_Id;
7873             New_Node : Node_Id)
7874          is
7875             Old_E : Node_Id;
7876             New_E : Node_Id;
7877
7878             Old_Next : Node_Id;
7879             New_Next : Node_Id;
7880
7881          begin
7882             Old_E := First (Parameter_Associations (Old_Node));
7883             New_E := First (Parameter_Associations (New_Node));
7884             while Present (Old_E) loop
7885                if Nkind (Old_E) = N_Parameter_Association
7886                  and then Present (Next_Named_Actual (Old_E))
7887                then
7888                   if First_Named_Actual (Old_Node)
7889                     =  Explicit_Actual_Parameter (Old_E)
7890                   then
7891                      Set_First_Named_Actual
7892                        (New_Node, Explicit_Actual_Parameter (New_E));
7893                   end if;
7894
7895                   --  Now scan parameter list from the beginning,to locate
7896                   --  next named actual, which can be out of order.
7897
7898                   Old_Next := First (Parameter_Associations (Old_Node));
7899                   New_Next := First (Parameter_Associations (New_Node));
7900
7901                   while Nkind (Old_Next) /= N_Parameter_Association
7902                     or else  Explicit_Actual_Parameter (Old_Next)
7903                       /= Next_Named_Actual (Old_E)
7904                   loop
7905                      Next (Old_Next);
7906                      Next (New_Next);
7907                   end loop;
7908
7909                   Set_Next_Named_Actual
7910                     (New_E, Explicit_Actual_Parameter (New_Next));
7911                end if;
7912
7913                Next (Old_E);
7914                Next (New_E);
7915             end loop;
7916          end Adjust_Named_Associations;
7917
7918          ---------------------------------
7919          -- Copy_Field_With_Replacement --
7920          ---------------------------------
7921
7922          function Copy_Field_With_Replacement
7923            (Field : Union_Id) return Union_Id
7924          is
7925          begin
7926             if Field = Union_Id (Empty) then
7927                return Field;
7928
7929             elsif Field in Node_Range then
7930                declare
7931                   Old_N : constant Node_Id := Node_Id (Field);
7932                   New_N : Node_Id;
7933
7934                begin
7935                   --  If syntactic field, as indicated by the parent pointer
7936                   --  being set, then copy the referenced node recursively.
7937
7938                   if Parent (Old_N) = Old_Node then
7939                      New_N := Copy_Node_With_Replacement (Old_N);
7940
7941                      if New_N /= Old_N then
7942                         Set_Parent (New_N, New_Node);
7943                      end if;
7944
7945                   --  For semantic fields, update possible entity reference
7946                   --  from the replacement map.
7947
7948                   else
7949                      New_N := Assoc (Old_N);
7950                   end if;
7951
7952                   return Union_Id (New_N);
7953                end;
7954
7955             elsif Field in List_Range then
7956                declare
7957                   Old_L : constant List_Id := List_Id (Field);
7958                   New_L : List_Id;
7959
7960                begin
7961                   --  If syntactic field, as indicated by the parent pointer,
7962                   --  then recursively copy the entire referenced list.
7963
7964                   if Parent (Old_L) = Old_Node then
7965                      New_L := Copy_List_With_Replacement (Old_L);
7966                      Set_Parent (New_L, New_Node);
7967
7968                   --  For semantic list, just returned unchanged
7969
7970                   else
7971                      New_L := Old_L;
7972                   end if;
7973
7974                   return Union_Id (New_L);
7975                end;
7976
7977             --  Anything other than a list or a node is returned unchanged
7978
7979             else
7980                return Field;
7981             end if;
7982          end Copy_Field_With_Replacement;
7983
7984       --  Start of processing for Copy_Node_With_Replacement
7985
7986       begin
7987          if Old_Node <= Empty_Or_Error then
7988             return Old_Node;
7989
7990          elsif Has_Extension (Old_Node) then
7991             return Assoc (Old_Node);
7992
7993          else
7994             New_Node := New_Copy (Old_Node);
7995
7996             --  If the node we are copying is the associated node of a
7997             --  previously copied Itype, then adjust the associated node
7998             --  of the copy of that Itype accordingly.
7999
8000             if Present (Actual_Map) then
8001                declare
8002                   E   : Elmt_Id;
8003                   Ent : Entity_Id;
8004
8005                begin
8006                   --  Case of hash table used
8007
8008                   if NCT_Hash_Tables_Used then
8009                      Ent := NCT_Itype_Assoc.Get (Old_Node);
8010
8011                      if Present (Ent) then
8012                         Set_Associated_Node_For_Itype (Ent, New_Node);
8013                      end if;
8014
8015                   --  Case of no hash table used
8016
8017                   else
8018                      E := First_Elmt (Actual_Map);
8019                      while Present (E) loop
8020                         if Is_Itype (Node (E))
8021                           and then
8022                             Old_Node = Associated_Node_For_Itype (Node (E))
8023                         then
8024                            Set_Associated_Node_For_Itype
8025                              (Node (Next_Elmt (E)), New_Node);
8026                         end if;
8027
8028                         E := Next_Elmt (Next_Elmt (E));
8029                      end loop;
8030                   end if;
8031                end;
8032             end if;
8033
8034             --  Recursively copy descendents
8035
8036             Set_Field1
8037               (New_Node, Copy_Field_With_Replacement (Field1 (New_Node)));
8038             Set_Field2
8039               (New_Node, Copy_Field_With_Replacement (Field2 (New_Node)));
8040             Set_Field3
8041               (New_Node, Copy_Field_With_Replacement (Field3 (New_Node)));
8042             Set_Field4
8043               (New_Node, Copy_Field_With_Replacement (Field4 (New_Node)));
8044             Set_Field5
8045               (New_Node, Copy_Field_With_Replacement (Field5 (New_Node)));
8046
8047             --  Adjust Sloc of new node if necessary
8048
8049             if New_Sloc /= No_Location then
8050                Set_Sloc (New_Node, New_Sloc);
8051
8052                --  If we adjust the Sloc, then we are essentially making
8053                --  a completely new node, so the Comes_From_Source flag
8054                --  should be reset to the proper default value.
8055
8056                Nodes.Table (New_Node).Comes_From_Source :=
8057                  Default_Node.Comes_From_Source;
8058             end if;
8059
8060             --  If the node is call and has named associations,
8061             --  set the corresponding links in the copy.
8062
8063             if (Nkind (Old_Node) = N_Function_Call
8064                  or else Nkind (Old_Node) = N_Entry_Call_Statement
8065                  or else
8066                    Nkind (Old_Node) = N_Procedure_Call_Statement)
8067               and then Present (First_Named_Actual (Old_Node))
8068             then
8069                Adjust_Named_Associations (Old_Node, New_Node);
8070             end if;
8071
8072             --  Reset First_Real_Statement for Handled_Sequence_Of_Statements.
8073             --  The replacement mechanism applies to entities, and is not used
8074             --  here. Eventually we may need a more general graph-copying
8075             --  routine. For now, do a sequential search to find desired node.
8076
8077             if Nkind (Old_Node) = N_Handled_Sequence_Of_Statements
8078               and then Present (First_Real_Statement (Old_Node))
8079             then
8080                declare
8081                   Old_F  : constant Node_Id := First_Real_Statement (Old_Node);
8082                   N1, N2 : Node_Id;
8083
8084                begin
8085                   N1 := First (Statements (Old_Node));
8086                   N2 := First (Statements (New_Node));
8087
8088                   while N1 /= Old_F loop
8089                      Next (N1);
8090                      Next (N2);
8091                   end loop;
8092
8093                   Set_First_Real_Statement (New_Node, N2);
8094                end;
8095             end if;
8096          end if;
8097
8098          --  All done, return copied node
8099
8100          return New_Node;
8101       end Copy_Node_With_Replacement;
8102
8103       -----------------
8104       -- Visit_Elist --
8105       -----------------
8106
8107       procedure Visit_Elist (E : Elist_Id) is
8108          Elmt : Elmt_Id;
8109       begin
8110          if Present (E) then
8111             Elmt := First_Elmt (E);
8112
8113             while Elmt /= No_Elmt loop
8114                Visit_Node (Node (Elmt));
8115                Next_Elmt (Elmt);
8116             end loop;
8117          end if;
8118       end Visit_Elist;
8119
8120       -----------------
8121       -- Visit_Field --
8122       -----------------
8123
8124       procedure Visit_Field (F : Union_Id; N : Node_Id) is
8125       begin
8126          if F = Union_Id (Empty) then
8127             return;
8128
8129          elsif F in Node_Range then
8130
8131             --  Copy node if it is syntactic, i.e. its parent pointer is
8132             --  set to point to the field that referenced it (certain
8133             --  Itypes will also meet this criterion, which is fine, since
8134             --  these are clearly Itypes that do need to be copied, since
8135             --  we are copying their parent.)
8136
8137             if Parent (Node_Id (F)) = N then
8138                Visit_Node (Node_Id (F));
8139                return;
8140
8141             --  Another case, if we are pointing to an Itype, then we want
8142             --  to copy it if its associated node is somewhere in the tree
8143             --  being copied.
8144
8145             --  Note: the exclusion of self-referential copies is just an
8146             --  optimization, since the search of the already copied list
8147             --  would catch it, but it is a common case (Etype pointing
8148             --  to itself for an Itype that is a base type).
8149
8150             elsif Has_Extension (Node_Id (F))
8151               and then Is_Itype (Entity_Id (F))
8152               and then Node_Id (F) /= N
8153             then
8154                declare
8155                   P : Node_Id;
8156
8157                begin
8158                   P := Associated_Node_For_Itype (Node_Id (F));
8159                   while Present (P) loop
8160                      if P = Source then
8161                         Visit_Node (Node_Id (F));
8162                         return;
8163                      else
8164                         P := Parent (P);
8165                      end if;
8166                   end loop;
8167
8168                   --  An Itype whose parent is not being copied definitely
8169                   --  should NOT be copied, since it does not belong in any
8170                   --  sense to the copied subtree.
8171
8172                   return;
8173                end;
8174             end if;
8175
8176          elsif F in List_Range
8177            and then Parent (List_Id (F)) = N
8178          then
8179             Visit_List (List_Id (F));
8180             return;
8181          end if;
8182       end Visit_Field;
8183
8184       -----------------
8185       -- Visit_Itype --
8186       -----------------
8187
8188       procedure Visit_Itype (Old_Itype : Entity_Id) is
8189          New_Itype : Entity_Id;
8190          E         : Elmt_Id;
8191          Ent       : Entity_Id;
8192
8193       begin
8194          --  Itypes that describe the designated type of access to subprograms
8195          --  have the structure of subprogram declarations, with signatures,
8196          --  etc. Either we duplicate the signatures completely, or choose to
8197          --  share such itypes, which is fine because their elaboration will
8198          --  have no side effects.
8199
8200          if Ekind (Old_Itype) = E_Subprogram_Type then
8201             return;
8202          end if;
8203
8204          New_Itype := New_Copy (Old_Itype);
8205
8206          --  The new Itype has all the attributes of the old one, and
8207          --  we just copy the contents of the entity. However, the back-end
8208          --  needs different names for debugging purposes, so we create a
8209          --  new internal name for it in all cases.
8210
8211          Set_Chars (New_Itype, New_Internal_Name ('T'));
8212
8213          --  If our associated node is an entity that has already been copied,
8214          --  then set the associated node of the copy to point to the right
8215          --  copy. If we have copied an Itype that is itself the associated
8216          --  node of some previously copied Itype, then we set the right
8217          --  pointer in the other direction.
8218
8219          if Present (Actual_Map) then
8220
8221             --  Case of hash tables used
8222
8223             if NCT_Hash_Tables_Used then
8224
8225                Ent := NCT_Assoc.Get (Associated_Node_For_Itype (Old_Itype));
8226
8227                if Present (Ent) then
8228                   Set_Associated_Node_For_Itype (New_Itype, Ent);
8229                end if;
8230
8231                Ent := NCT_Itype_Assoc.Get (Old_Itype);
8232                if Present (Ent) then
8233                   Set_Associated_Node_For_Itype (Ent, New_Itype);
8234
8235                --  If the hash table has no association for this Itype and
8236                --  its associated node, enter one now.
8237
8238                else
8239                   NCT_Itype_Assoc.Set
8240                     (Associated_Node_For_Itype (Old_Itype), New_Itype);
8241                end if;
8242
8243             --  Case of hash tables not used
8244
8245             else
8246                E := First_Elmt (Actual_Map);
8247                while Present (E) loop
8248                   if Associated_Node_For_Itype (Old_Itype) = Node (E) then
8249                      Set_Associated_Node_For_Itype
8250                        (New_Itype, Node (Next_Elmt (E)));
8251                   end if;
8252
8253                   if Is_Type (Node (E))
8254                     and then
8255                       Old_Itype = Associated_Node_For_Itype (Node (E))
8256                   then
8257                      Set_Associated_Node_For_Itype
8258                        (Node (Next_Elmt (E)), New_Itype);
8259                   end if;
8260
8261                   E := Next_Elmt (Next_Elmt (E));
8262                end loop;
8263             end if;
8264          end if;
8265
8266          if Present (Freeze_Node (New_Itype)) then
8267             Set_Is_Frozen (New_Itype, False);
8268             Set_Freeze_Node (New_Itype, Empty);
8269          end if;
8270
8271          --  Add new association to map
8272
8273          if No (Actual_Map) then
8274             Actual_Map := New_Elmt_List;
8275          end if;
8276
8277          Append_Elmt (Old_Itype, Actual_Map);
8278          Append_Elmt (New_Itype, Actual_Map);
8279
8280          if NCT_Hash_Tables_Used then
8281             NCT_Assoc.Set (Old_Itype, New_Itype);
8282
8283          else
8284             NCT_Table_Entries := NCT_Table_Entries + 1;
8285
8286             if NCT_Table_Entries > NCT_Hash_Threshhold then
8287                Build_NCT_Hash_Tables;
8288             end if;
8289          end if;
8290
8291          --  If a record subtype is simply copied, the entity list will be
8292          --  shared. Thus cloned_Subtype must be set to indicate the sharing.
8293
8294          if Ekind (Old_Itype) = E_Record_Subtype
8295            or else Ekind (Old_Itype) = E_Class_Wide_Subtype
8296          then
8297             Set_Cloned_Subtype (New_Itype, Old_Itype);
8298          end if;
8299
8300          --  Visit descendents that eventually get copied
8301
8302          Visit_Field (Union_Id (Etype (Old_Itype)), Old_Itype);
8303
8304          if Is_Discrete_Type (Old_Itype) then
8305             Visit_Field (Union_Id (Scalar_Range (Old_Itype)), Old_Itype);
8306
8307          elsif Has_Discriminants (Base_Type (Old_Itype)) then
8308             --  ??? This should involve call to Visit_Field
8309             Visit_Elist (Discriminant_Constraint (Old_Itype));
8310
8311          elsif Is_Array_Type (Old_Itype) then
8312             if Present (First_Index (Old_Itype)) then
8313                Visit_Field (Union_Id (List_Containing
8314                                 (First_Index (Old_Itype))),
8315                             Old_Itype);
8316             end if;
8317
8318             if Is_Packed (Old_Itype) then
8319                Visit_Field (Union_Id (Packed_Array_Type (Old_Itype)),
8320                             Old_Itype);
8321             end if;
8322          end if;
8323       end Visit_Itype;
8324
8325       ----------------
8326       -- Visit_List --
8327       ----------------
8328
8329       procedure Visit_List (L : List_Id) is
8330          N : Node_Id;
8331       begin
8332          if L /= No_List then
8333             N := First (L);
8334
8335             while Present (N) loop
8336                Visit_Node (N);
8337                Next (N);
8338             end loop;
8339          end if;
8340       end Visit_List;
8341
8342       ----------------
8343       -- Visit_Node --
8344       ----------------
8345
8346       procedure Visit_Node (N : Node_Or_Entity_Id) is
8347
8348       --  Start of processing for Visit_Node
8349
8350       begin
8351          --  Handle case of an Itype, which must be copied
8352
8353          if Has_Extension (N)
8354            and then Is_Itype (N)
8355          then
8356             --  Nothing to do if already in the list. This can happen with an
8357             --  Itype entity that appears more than once in the tree.
8358             --  Note that we do not want to visit descendents in this case.
8359
8360             --  Test for already in list when hash table is used
8361
8362             if NCT_Hash_Tables_Used then
8363                if Present (NCT_Assoc.Get (Entity_Id (N))) then
8364                   return;
8365                end if;
8366
8367             --  Test for already in list when hash table not used
8368
8369             else
8370                declare
8371                   E : Elmt_Id;
8372                begin
8373                   if Present (Actual_Map) then
8374                      E := First_Elmt (Actual_Map);
8375                      while Present (E) loop
8376                         if Node (E) = N then
8377                            return;
8378                         else
8379                            E := Next_Elmt (Next_Elmt (E));
8380                         end if;
8381                      end loop;
8382                   end if;
8383                end;
8384             end if;
8385
8386             Visit_Itype (N);
8387          end if;
8388
8389          --  Visit descendents
8390
8391          Visit_Field (Field1 (N), N);
8392          Visit_Field (Field2 (N), N);
8393          Visit_Field (Field3 (N), N);
8394          Visit_Field (Field4 (N), N);
8395          Visit_Field (Field5 (N), N);
8396       end Visit_Node;
8397
8398    --  Start of processing for New_Copy_Tree
8399
8400    begin
8401       Actual_Map := Map;
8402
8403       --  See if we should use hash table
8404
8405       if No (Actual_Map) then
8406          NCT_Hash_Tables_Used := False;
8407
8408       else
8409          declare
8410             Elmt : Elmt_Id;
8411
8412          begin
8413             NCT_Table_Entries := 0;
8414
8415             Elmt := First_Elmt (Actual_Map);
8416             while Present (Elmt) loop
8417                NCT_Table_Entries := NCT_Table_Entries + 1;
8418                Next_Elmt (Elmt);
8419                Next_Elmt (Elmt);
8420             end loop;
8421
8422             if NCT_Table_Entries > NCT_Hash_Threshhold then
8423                Build_NCT_Hash_Tables;
8424             else
8425                NCT_Hash_Tables_Used := False;
8426             end if;
8427          end;
8428       end if;
8429
8430       --  Hash table set up if required, now start phase one by visiting
8431       --  top node (we will recursively visit the descendents).
8432
8433       Visit_Node (Source);
8434
8435       --  Now the second phase of the copy can start. First we process
8436       --  all the mapped entities, copying their descendents.
8437
8438       if Present (Actual_Map) then
8439          declare
8440             Elmt      : Elmt_Id;
8441             New_Itype : Entity_Id;
8442          begin
8443             Elmt := First_Elmt (Actual_Map);
8444             while Present (Elmt) loop
8445                Next_Elmt (Elmt);
8446                New_Itype := Node (Elmt);
8447                Copy_Itype_With_Replacement (New_Itype);
8448                Next_Elmt (Elmt);
8449             end loop;
8450          end;
8451       end if;
8452
8453       --  Now we can copy the actual tree
8454
8455       return Copy_Node_With_Replacement (Source);
8456    end New_Copy_Tree;
8457
8458    -------------------------
8459    -- New_External_Entity --
8460    -------------------------
8461
8462    function New_External_Entity
8463      (Kind         : Entity_Kind;
8464       Scope_Id     : Entity_Id;
8465       Sloc_Value   : Source_Ptr;
8466       Related_Id   : Entity_Id;
8467       Suffix       : Character;
8468       Suffix_Index : Nat := 0;
8469       Prefix       : Character := ' ') return Entity_Id
8470    is
8471       N : constant Entity_Id :=
8472             Make_Defining_Identifier (Sloc_Value,
8473               New_External_Name
8474                 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
8475
8476    begin
8477       Set_Ekind          (N, Kind);
8478       Set_Is_Internal    (N, True);
8479       Append_Entity      (N, Scope_Id);
8480       Set_Public_Status  (N);
8481
8482       if Kind in Type_Kind then
8483          Init_Size_Align (N);
8484       end if;
8485
8486       return N;
8487    end New_External_Entity;
8488
8489    -------------------------
8490    -- New_Internal_Entity --
8491    -------------------------
8492
8493    function New_Internal_Entity
8494      (Kind       : Entity_Kind;
8495       Scope_Id   : Entity_Id;
8496       Sloc_Value : Source_Ptr;
8497       Id_Char    : Character) return Entity_Id
8498    is
8499       N : constant Entity_Id :=
8500             Make_Defining_Identifier (Sloc_Value, New_Internal_Name (Id_Char));
8501
8502    begin
8503       Set_Ekind          (N, Kind);
8504       Set_Is_Internal    (N, True);
8505       Append_Entity      (N, Scope_Id);
8506
8507       if Kind in Type_Kind then
8508          Init_Size_Align (N);
8509       end if;
8510
8511       return N;
8512    end New_Internal_Entity;
8513
8514    -----------------
8515    -- Next_Actual --
8516    -----------------
8517
8518    function Next_Actual (Actual_Id : Node_Id) return Node_Id is
8519       N  : Node_Id;
8520
8521    begin
8522       --  If we are pointing at a positional parameter, it is a member of a
8523       --  node list (the list of parameters), and the next parameter is the
8524       --  next node on the list, unless we hit a parameter association, then
8525       --  we shift to using the chain whose head is the First_Named_Actual in
8526       --  the parent, and then is threaded using the Next_Named_Actual of the
8527       --  Parameter_Association. All this fiddling is because the original node
8528       --  list is in the textual call order, and what we need is the
8529       --  declaration order.
8530
8531       if Is_List_Member (Actual_Id) then
8532          N := Next (Actual_Id);
8533
8534          if Nkind (N) = N_Parameter_Association then
8535             return First_Named_Actual (Parent (Actual_Id));
8536          else
8537             return N;
8538          end if;
8539
8540       else
8541          return Next_Named_Actual (Parent (Actual_Id));
8542       end if;
8543    end Next_Actual;
8544
8545    procedure Next_Actual (Actual_Id : in out Node_Id) is
8546    begin
8547       Actual_Id := Next_Actual (Actual_Id);
8548    end Next_Actual;
8549
8550    -----------------------
8551    -- Normalize_Actuals --
8552    -----------------------
8553
8554    --  Chain actuals according to formals of subprogram. If there are no named
8555    --  associations, the chain is simply the list of Parameter Associations,
8556    --  since the order is the same as the declaration order. If there are named
8557    --  associations, then the First_Named_Actual field in the N_Function_Call
8558    --  or N_Procedure_Call_Statement node points to the Parameter_Association
8559    --  node for the parameter that comes first in declaration order. The
8560    --  remaining named parameters are then chained in declaration order using
8561    --  Next_Named_Actual.
8562
8563    --  This routine also verifies that the number of actuals is compatible with
8564    --  the number and default values of formals, but performs no type checking
8565    --  (type checking is done by the caller).
8566
8567    --  If the matching succeeds, Success is set to True and the caller proceeds
8568    --  with type-checking. If the match is unsuccessful, then Success is set to
8569    --  False, and the caller attempts a different interpretation, if there is
8570    --  one.
8571
8572    --  If the flag Report is on, the call is not overloaded, and a failure to
8573    --  match can be reported here, rather than in the caller.
8574
8575    procedure Normalize_Actuals
8576      (N       : Node_Id;
8577       S       : Entity_Id;
8578       Report  : Boolean;
8579       Success : out Boolean)
8580    is
8581       Actuals     : constant List_Id := Parameter_Associations (N);
8582       Actual      : Node_Id := Empty;
8583       Formal      : Entity_Id;
8584       Last        : Node_Id := Empty;
8585       First_Named : Node_Id := Empty;
8586       Found       : Boolean;
8587
8588       Formals_To_Match : Integer := 0;
8589       Actuals_To_Match : Integer := 0;
8590
8591       procedure Chain (A : Node_Id);
8592       --  Add named actual at the proper place in the list, using the
8593       --  Next_Named_Actual link.
8594
8595       function Reporting return Boolean;
8596       --  Determines if an error is to be reported. To report an error, we
8597       --  need Report to be True, and also we do not report errors caused
8598       --  by calls to init procs that occur within other init procs. Such
8599       --  errors must always be cascaded errors, since if all the types are
8600       --  declared correctly, the compiler will certainly build decent calls!
8601
8602       -----------
8603       -- Chain --
8604       -----------
8605
8606       procedure Chain (A : Node_Id) is
8607       begin
8608          if No (Last) then
8609
8610             --  Call node points to first actual in list
8611
8612             Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
8613
8614          else
8615             Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
8616          end if;
8617
8618          Last := A;
8619          Set_Next_Named_Actual (Last, Empty);
8620       end Chain;
8621
8622       ---------------
8623       -- Reporting --
8624       ---------------
8625
8626       function Reporting return Boolean is
8627       begin
8628          if not Report then
8629             return False;
8630
8631          elsif not Within_Init_Proc then
8632             return True;
8633
8634          elsif Is_Init_Proc (Entity (Name (N))) then
8635             return False;
8636
8637          else
8638             return True;
8639          end if;
8640       end Reporting;
8641
8642    --  Start of processing for Normalize_Actuals
8643
8644    begin
8645       if Is_Access_Type (S) then
8646
8647          --  The name in the call is a function call that returns an access
8648          --  to subprogram. The designated type has the list of formals.
8649
8650          Formal := First_Formal (Designated_Type (S));
8651       else
8652          Formal := First_Formal (S);
8653       end if;
8654
8655       while Present (Formal) loop
8656          Formals_To_Match := Formals_To_Match + 1;
8657          Next_Formal (Formal);
8658       end loop;
8659
8660       --  Find if there is a named association, and verify that no positional
8661       --  associations appear after named ones.
8662
8663       if Present (Actuals) then
8664          Actual := First (Actuals);
8665       end if;
8666
8667       while Present (Actual)
8668         and then Nkind (Actual) /= N_Parameter_Association
8669       loop
8670          Actuals_To_Match := Actuals_To_Match + 1;
8671          Next (Actual);
8672       end loop;
8673
8674       if No (Actual) and Actuals_To_Match = Formals_To_Match then
8675
8676          --  Most common case: positional notation, no defaults
8677
8678          Success := True;
8679          return;
8680
8681       elsif Actuals_To_Match > Formals_To_Match then
8682
8683          --  Too many actuals: will not work
8684
8685          if Reporting then
8686             if Is_Entity_Name (Name (N)) then
8687                Error_Msg_N ("too many arguments in call to&", Name (N));
8688             else
8689                Error_Msg_N ("too many arguments in call", N);
8690             end if;
8691          end if;
8692
8693          Success := False;
8694          return;
8695       end if;
8696
8697       First_Named := Actual;
8698
8699       while Present (Actual) loop
8700          if Nkind (Actual) /= N_Parameter_Association then
8701             Error_Msg_N
8702               ("positional parameters not allowed after named ones", Actual);
8703             Success := False;
8704             return;
8705
8706          else
8707             Actuals_To_Match := Actuals_To_Match + 1;
8708          end if;
8709
8710          Next (Actual);
8711       end loop;
8712
8713       if Present (Actuals) then
8714          Actual := First (Actuals);
8715       end if;
8716
8717       Formal := First_Formal (S);
8718       while Present (Formal) loop
8719
8720          --  Match the formals in order. If the corresponding actual is
8721          --  positional, nothing to do. Else scan the list of named actuals
8722          --  to find the one with the right name.
8723
8724          if Present (Actual)
8725            and then Nkind (Actual) /= N_Parameter_Association
8726          then
8727             Next (Actual);
8728             Actuals_To_Match := Actuals_To_Match - 1;
8729             Formals_To_Match := Formals_To_Match - 1;
8730
8731          else
8732             --  For named parameters, search the list of actuals to find
8733             --  one that matches the next formal name.
8734
8735             Actual := First_Named;
8736             Found  := False;
8737             while Present (Actual) loop
8738                if Chars (Selector_Name (Actual)) = Chars (Formal) then
8739                   Found := True;
8740                   Chain (Actual);
8741                   Actuals_To_Match := Actuals_To_Match - 1;
8742                   Formals_To_Match := Formals_To_Match - 1;
8743                   exit;
8744                end if;
8745
8746                Next (Actual);
8747             end loop;
8748
8749             if not Found then
8750                if Ekind (Formal) /= E_In_Parameter
8751                  or else No (Default_Value (Formal))
8752                then
8753                   if Reporting then
8754                      if (Comes_From_Source (S)
8755                           or else Sloc (S) = Standard_Location)
8756                        and then Is_Overloadable (S)
8757                      then
8758                         if No (Actuals)
8759                           and then
8760                            (Nkind (Parent (N)) = N_Procedure_Call_Statement
8761                              or else
8762                            (Nkind (Parent (N)) = N_Function_Call
8763                              or else
8764                             Nkind (Parent (N)) = N_Parameter_Association))
8765                           and then Ekind (S) /= E_Function
8766                         then
8767                            Set_Etype (N, Etype (S));
8768                         else
8769                            Error_Msg_Name_1 := Chars (S);
8770                            Error_Msg_Sloc := Sloc (S);
8771                            Error_Msg_NE
8772                              ("missing argument for parameter & " &
8773                                 "in call to % declared #", N, Formal);
8774                         end if;
8775
8776                      elsif Is_Overloadable (S) then
8777                         Error_Msg_Name_1 := Chars (S);
8778
8779                         --  Point to type derivation that generated the
8780                         --  operation.
8781
8782                         Error_Msg_Sloc := Sloc (Parent (S));
8783
8784                         Error_Msg_NE
8785                           ("missing argument for parameter & " &
8786                              "in call to % (inherited) #", N, Formal);
8787
8788                      else
8789                         Error_Msg_NE
8790                           ("missing argument for parameter &", N, Formal);
8791                      end if;
8792                   end if;
8793
8794                   Success := False;
8795                   return;
8796
8797                else
8798                   Formals_To_Match := Formals_To_Match - 1;
8799                end if;
8800             end if;
8801          end if;
8802
8803          Next_Formal (Formal);
8804       end loop;
8805
8806       if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
8807          Success := True;
8808          return;
8809
8810       else
8811          if Reporting then
8812
8813             --  Find some superfluous named actual that did not get
8814             --  attached to the list of associations.
8815
8816             Actual := First (Actuals);
8817             while Present (Actual) loop
8818                if Nkind (Actual) = N_Parameter_Association
8819                  and then Actual /= Last
8820                  and then No (Next_Named_Actual (Actual))
8821                then
8822                   Error_Msg_N ("unmatched actual & in call",
8823                     Selector_Name (Actual));
8824                   exit;
8825                end if;
8826
8827                Next (Actual);
8828             end loop;
8829          end if;
8830
8831          Success := False;
8832          return;
8833       end if;
8834    end Normalize_Actuals;
8835
8836    --------------------------------
8837    -- Note_Possible_Modification --
8838    --------------------------------
8839
8840    procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
8841       Modification_Comes_From_Source : constant Boolean :=
8842                                          Comes_From_Source (Parent (N));
8843
8844       Ent : Entity_Id;
8845       Exp : Node_Id;
8846
8847    begin
8848       --  Loop to find referenced entity, if there is one
8849
8850       Exp := N;
8851       loop
8852          <<Continue>>
8853          Ent := Empty;
8854
8855          if Is_Entity_Name (Exp) then
8856             Ent := Entity (Exp);
8857
8858             --  If the entity is missing, it is an undeclared identifier,
8859             --  and there is nothing to annotate.
8860
8861             if No (Ent) then
8862                return;
8863             end if;
8864
8865          elsif Nkind (Exp) = N_Explicit_Dereference then
8866             declare
8867                P : constant Node_Id := Prefix (Exp);
8868
8869             begin
8870                if Nkind (P) = N_Selected_Component
8871                  and then Present (
8872                    Entry_Formal (Entity (Selector_Name (P))))
8873                then
8874                   --  Case of a reference to an entry formal
8875
8876                   Ent := Entry_Formal (Entity (Selector_Name (P)));
8877
8878                elsif Nkind (P) = N_Identifier
8879                  and then Nkind (Parent (Entity (P))) = N_Object_Declaration
8880                  and then Present (Expression (Parent (Entity (P))))
8881                  and then Nkind (Expression (Parent (Entity (P))))
8882                    = N_Reference
8883                then
8884                   --  Case of a reference to a value on which side effects have
8885                   --  been removed.
8886
8887                   Exp := Prefix (Expression (Parent (Entity (P))));
8888                   goto Continue;
8889
8890                else
8891                   return;
8892
8893                end if;
8894             end;
8895
8896          elsif     Nkind (Exp) = N_Type_Conversion
8897            or else Nkind (Exp) = N_Unchecked_Type_Conversion
8898          then
8899             Exp := Expression (Exp);
8900             goto Continue;
8901
8902          elsif     Nkind (Exp) = N_Slice
8903            or else Nkind (Exp) = N_Indexed_Component
8904            or else Nkind (Exp) = N_Selected_Component
8905          then
8906             Exp := Prefix (Exp);
8907             goto Continue;
8908
8909          else
8910             return;
8911          end if;
8912
8913          --  Now look for entity being referenced
8914
8915          if Present (Ent) then
8916             if Is_Object (Ent) then
8917                if Comes_From_Source (Exp)
8918                  or else Modification_Comes_From_Source
8919                then
8920                   if Has_Pragma_Unmodified (Ent) then
8921                      Error_Msg_NE ("?pragma Unmodified given for &!", N, Ent);
8922                   end if;
8923
8924                   Set_Never_Set_In_Source (Ent, False);
8925                end if;
8926
8927                Set_Is_True_Constant (Ent, False);
8928                Set_Current_Value    (Ent, Empty);
8929                Set_Is_Known_Null    (Ent, False);
8930
8931                if not Can_Never_Be_Null (Ent) then
8932                   Set_Is_Known_Non_Null (Ent, False);
8933                end if;
8934
8935                --  Follow renaming chain
8936
8937                if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
8938                  and then Present (Renamed_Object (Ent))
8939                then
8940                   Exp := Renamed_Object (Ent);
8941                   goto Continue;
8942                end if;
8943
8944                --  Generate a reference only if the assignment comes from
8945                --  source. This excludes, for example, calls to a dispatching
8946                --  assignment operation when the left-hand side is tagged.
8947
8948                if Modification_Comes_From_Source then
8949                   Generate_Reference (Ent, Exp, 'm');
8950                end if;
8951
8952                Check_Nested_Access (Ent);
8953             end if;
8954
8955             Kill_Checks (Ent);
8956
8957             --  If we are sure this is a modification from source, and we know
8958             --  this modifies a constant, then give an appropriate warning.
8959
8960             if Overlays_Constant (Ent)
8961               and then Modification_Comes_From_Source
8962               and then Sure
8963             then
8964                declare
8965                   A : constant Node_Id := Address_Clause (Ent);
8966                begin
8967                   if Present (A) then
8968                      declare
8969                         Exp : constant Node_Id := Expression (A);
8970                      begin
8971                         if Nkind (Exp) = N_Attribute_Reference
8972                           and then Attribute_Name (Exp) = Name_Address
8973                           and then Is_Entity_Name (Prefix (Exp))
8974                         then
8975                            Error_Msg_Sloc := Sloc (A);
8976                            Error_Msg_NE
8977                              ("constant& may be modified via address clause#?",
8978                               N, Entity (Prefix (Exp)));
8979                         end if;
8980                      end;
8981                   end if;
8982                end;
8983             end if;
8984
8985             return;
8986          end if;
8987       end loop;
8988    end Note_Possible_Modification;
8989
8990    -------------------------
8991    -- Object_Access_Level --
8992    -------------------------
8993
8994    function Object_Access_Level (Obj : Node_Id) return Uint is
8995       E : Entity_Id;
8996
8997    --  Returns the static accessibility level of the view denoted by Obj. Note
8998    --  that the value returned is the result of a call to Scope_Depth. Only
8999    --  scope depths associated with dynamic scopes can actually be returned.
9000    --  Since only relative levels matter for accessibility checking, the fact
9001    --  that the distance between successive levels of accessibility is not
9002    --  always one is immaterial (invariant: if level(E2) is deeper than
9003    --  level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
9004
9005       function Reference_To (Obj : Node_Id) return Node_Id;
9006       --  An explicit dereference is created when removing side-effects from
9007       --  expressions for constraint checking purposes. In this case a local
9008       --  access type is created for it. The correct access level is that of
9009       --  the original source node. We detect this case by noting that the
9010       --  prefix of the dereference is created by an object declaration whose
9011       --  initial expression is a reference.
9012
9013       ------------------
9014       -- Reference_To --
9015       ------------------
9016
9017       function Reference_To (Obj : Node_Id) return Node_Id is
9018          Pref : constant Node_Id := Prefix (Obj);
9019       begin
9020          if Is_Entity_Name (Pref)
9021            and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
9022            and then Present (Expression (Parent (Entity (Pref))))
9023            and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
9024          then
9025             return (Prefix (Expression (Parent (Entity (Pref)))));
9026          else
9027             return Empty;
9028          end if;
9029       end Reference_To;
9030
9031    --  Start of processing for Object_Access_Level
9032
9033    begin
9034       if Is_Entity_Name (Obj) then
9035          E := Entity (Obj);
9036
9037          if Is_Prival (E) then
9038             E := Prival_Link (E);
9039          end if;
9040
9041          --  If E is a type then it denotes a current instance. For this case
9042          --  we add one to the normal accessibility level of the type to ensure
9043          --  that current instances are treated as always being deeper than
9044          --  than the level of any visible named access type (see 3.10.2(21)).
9045
9046          if Is_Type (E) then
9047             return Type_Access_Level (E) +  1;
9048
9049          elsif Present (Renamed_Object (E)) then
9050             return Object_Access_Level (Renamed_Object (E));
9051
9052          --  Similarly, if E is a component of the current instance of a
9053          --  protected type, any instance of it is assumed to be at a deeper
9054          --  level than the type. For a protected object (whose type is an
9055          --  anonymous protected type) its components are at the same level
9056          --  as the type itself.
9057
9058          elsif not Is_Overloadable (E)
9059            and then Ekind (Scope (E)) = E_Protected_Type
9060            and then Comes_From_Source (Scope (E))
9061          then
9062             return Type_Access_Level (Scope (E)) + 1;
9063
9064          else
9065             return Scope_Depth (Enclosing_Dynamic_Scope (E));
9066          end if;
9067
9068       elsif Nkind (Obj) = N_Selected_Component then
9069          if Is_Access_Type (Etype (Prefix (Obj))) then
9070             return Type_Access_Level (Etype (Prefix (Obj)));
9071          else
9072             return Object_Access_Level (Prefix (Obj));
9073          end if;
9074
9075       elsif Nkind (Obj) = N_Indexed_Component then
9076          if Is_Access_Type (Etype (Prefix (Obj))) then
9077             return Type_Access_Level (Etype (Prefix (Obj)));
9078          else
9079             return Object_Access_Level (Prefix (Obj));
9080          end if;
9081
9082       elsif Nkind (Obj) = N_Explicit_Dereference then
9083
9084          --  If the prefix is a selected access discriminant then we make a
9085          --  recursive call on the prefix, which will in turn check the level
9086          --  of the prefix object of the selected discriminant.
9087
9088          if Nkind (Prefix (Obj)) = N_Selected_Component
9089            and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
9090            and then
9091              Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
9092          then
9093             return Object_Access_Level (Prefix (Obj));
9094
9095          elsif not (Comes_From_Source (Obj)) then
9096             declare
9097                Ref : constant Node_Id := Reference_To (Obj);
9098             begin
9099                if Present (Ref) then
9100                   return Object_Access_Level (Ref);
9101                else
9102                   return Type_Access_Level (Etype (Prefix (Obj)));
9103                end if;
9104             end;
9105
9106          else
9107             return Type_Access_Level (Etype (Prefix (Obj)));
9108          end if;
9109
9110       elsif Nkind (Obj) = N_Type_Conversion
9111         or else Nkind (Obj) = N_Unchecked_Type_Conversion
9112       then
9113          return Object_Access_Level (Expression (Obj));
9114
9115       --  Function results are objects, so we get either the access level of
9116       --  the function or, in the case of an indirect call, the level of the
9117       --  access-to-subprogram type.
9118
9119       elsif Nkind (Obj) = N_Function_Call then
9120          if Is_Entity_Name (Name (Obj)) then
9121             return Subprogram_Access_Level (Entity (Name (Obj)));
9122          else
9123             return Type_Access_Level (Etype (Prefix (Name (Obj))));
9124          end if;
9125
9126       --  For convenience we handle qualified expressions, even though
9127       --  they aren't technically object names.
9128
9129       elsif Nkind (Obj) = N_Qualified_Expression then
9130          return Object_Access_Level (Expression (Obj));
9131
9132       --  Otherwise return the scope level of Standard.
9133       --  (If there are cases that fall through
9134       --  to this point they will be treated as
9135       --  having global accessibility for now. ???)
9136
9137       else
9138          return Scope_Depth (Standard_Standard);
9139       end if;
9140    end Object_Access_Level;
9141
9142    -----------------------
9143    -- Private_Component --
9144    -----------------------
9145
9146    function Private_Component (Type_Id : Entity_Id) return Entity_Id is
9147       Ancestor  : constant Entity_Id := Base_Type (Type_Id);
9148
9149       function Trace_Components
9150         (T     : Entity_Id;
9151          Check : Boolean) return Entity_Id;
9152       --  Recursive function that does the work, and checks against circular
9153       --  definition for each subcomponent type.
9154
9155       ----------------------
9156       -- Trace_Components --
9157       ----------------------
9158
9159       function Trace_Components
9160          (T     : Entity_Id;
9161           Check : Boolean) return Entity_Id
9162        is
9163          Btype     : constant Entity_Id := Base_Type (T);
9164          Component : Entity_Id;
9165          P         : Entity_Id;
9166          Candidate : Entity_Id := Empty;
9167
9168       begin
9169          if Check and then Btype = Ancestor then
9170             Error_Msg_N ("circular type definition", Type_Id);
9171             return Any_Type;
9172          end if;
9173
9174          if Is_Private_Type (Btype)
9175            and then not Is_Generic_Type (Btype)
9176          then
9177             if Present (Full_View (Btype))
9178               and then Is_Record_Type (Full_View (Btype))
9179               and then not Is_Frozen (Btype)
9180             then
9181                --  To indicate that the ancestor depends on a private type, the
9182                --  current Btype is sufficient. However, to check for circular
9183                --  definition we must recurse on the full view.
9184
9185                Candidate := Trace_Components (Full_View (Btype), True);
9186
9187                if Candidate = Any_Type then
9188                   return Any_Type;
9189                else
9190                   return Btype;
9191                end if;
9192
9193             else
9194                return Btype;
9195             end if;
9196
9197          elsif Is_Array_Type (Btype) then
9198             return Trace_Components (Component_Type (Btype), True);
9199
9200          elsif Is_Record_Type (Btype) then
9201             Component := First_Entity (Btype);
9202             while Present (Component) loop
9203
9204                --  Skip anonymous types generated by constrained components
9205
9206                if not Is_Type (Component) then
9207                   P := Trace_Components (Etype (Component), True);
9208
9209                   if Present (P) then
9210                      if P = Any_Type then
9211                         return P;
9212                      else
9213                         Candidate := P;
9214                      end if;
9215                   end if;
9216                end if;
9217
9218                Next_Entity (Component);
9219             end loop;
9220
9221             return Candidate;
9222
9223          else
9224             return Empty;
9225          end if;
9226       end Trace_Components;
9227
9228    --  Start of processing for Private_Component
9229
9230    begin
9231       return Trace_Components (Type_Id, False);
9232    end Private_Component;
9233
9234    ---------------------------
9235    -- Primitive_Names_Match --
9236    ---------------------------
9237
9238    function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
9239
9240       function Non_Internal_Name (E : Entity_Id) return Name_Id;
9241       --  Given an internal name, returns the corresponding non-internal name
9242
9243       ------------------------
9244       --  Non_Internal_Name --
9245       ------------------------
9246
9247       function Non_Internal_Name (E : Entity_Id) return Name_Id is
9248       begin
9249          Get_Name_String (Chars (E));
9250          Name_Len := Name_Len - 1;
9251          return Name_Find;
9252       end Non_Internal_Name;
9253
9254    --  Start of processing for Primitive_Names_Match
9255
9256    begin
9257       pragma Assert (Present (E1) and then Present (E2));
9258
9259       return Chars (E1) = Chars (E2)
9260         or else
9261            (not Is_Internal_Name (Chars (E1))
9262               and then Is_Internal_Name (Chars (E2))
9263               and then Non_Internal_Name (E2) = Chars (E1))
9264         or else
9265            (not Is_Internal_Name (Chars (E2))
9266               and then Is_Internal_Name (Chars (E1))
9267               and then Non_Internal_Name (E1) = Chars (E2))
9268         or else
9269            (Is_Predefined_Dispatching_Operation (E1)
9270               and then Is_Predefined_Dispatching_Operation (E2)
9271               and then Same_TSS (E1, E2))
9272         or else
9273            (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
9274    end Primitive_Names_Match;
9275
9276    -----------------------
9277    -- Process_End_Label --
9278    -----------------------
9279
9280    procedure Process_End_Label
9281      (N   : Node_Id;
9282       Typ : Character;
9283       Ent  : Entity_Id)
9284    is
9285       Loc  : Source_Ptr;
9286       Nam  : Node_Id;
9287       Scop : Entity_Id;
9288
9289       Label_Ref : Boolean;
9290       --  Set True if reference to end label itself is required
9291
9292       Endl : Node_Id;
9293       --  Gets set to the operator symbol or identifier that references the
9294       --  entity Ent. For the child unit case, this is the identifier from the
9295       --  designator. For other cases, this is simply Endl.
9296
9297       procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
9298       --  N is an identifier node that appears as a parent unit reference in
9299       --  the case where Ent is a child unit. This procedure generates an
9300       --  appropriate cross-reference entry. E is the corresponding entity.
9301
9302       -------------------------
9303       -- Generate_Parent_Ref --
9304       -------------------------
9305
9306       procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
9307       begin
9308          --  If names do not match, something weird, skip reference
9309
9310          if Chars (E) = Chars (N) then
9311
9312             --  Generate the reference. We do NOT consider this as a reference
9313             --  for unreferenced symbol purposes.
9314
9315             Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
9316
9317             if Style_Check then
9318                Style.Check_Identifier (N, E);
9319             end if;
9320          end if;
9321       end Generate_Parent_Ref;
9322
9323    --  Start of processing for Process_End_Label
9324
9325    begin
9326       --  If no node, ignore. This happens in some error situations, and
9327       --  also for some internally generated structures where no end label
9328       --  references are required in any case.
9329
9330       if No (N) then
9331          return;
9332       end if;
9333
9334       --  Nothing to do if no End_Label, happens for internally generated
9335       --  constructs where we don't want an end label reference anyway. Also
9336       --  nothing to do if Endl is a string literal, which means there was
9337       --  some prior error (bad operator symbol)
9338
9339       Endl := End_Label (N);
9340
9341       if No (Endl) or else Nkind (Endl) = N_String_Literal then
9342          return;
9343       end if;
9344
9345       --  Reference node is not in extended main source unit
9346
9347       if not In_Extended_Main_Source_Unit (N) then
9348
9349          --  Generally we do not collect references except for the extended
9350          --  main source unit. The one exception is the 'e' entry for a
9351          --  package spec, where it is useful for a client to have the
9352          --  ending information to define scopes.
9353
9354          if Typ /= 'e' then
9355             return;
9356
9357          else
9358             Label_Ref := False;
9359
9360             --  For this case, we can ignore any parent references, but we
9361             --  need the package name itself for the 'e' entry.
9362
9363             if Nkind (Endl) = N_Designator then
9364                Endl := Identifier (Endl);
9365             end if;
9366          end if;
9367
9368       --  Reference is in extended main source unit
9369
9370       else
9371          Label_Ref := True;
9372
9373          --  For designator, generate references for the parent entries
9374
9375          if Nkind (Endl) = N_Designator then
9376
9377             --  Generate references for the prefix if the END line comes from
9378             --  source (otherwise we do not need these references) We climb the
9379             --  scope stack to find the expected entities.
9380
9381             if Comes_From_Source (Endl) then
9382                Nam  := Name (Endl);
9383                Scop := Current_Scope;
9384                while Nkind (Nam) = N_Selected_Component loop
9385                   Scop := Scope (Scop);
9386                   exit when No (Scop);
9387                   Generate_Parent_Ref (Selector_Name (Nam), Scop);
9388                   Nam := Prefix (Nam);
9389                end loop;
9390
9391                if Present (Scop) then
9392                   Generate_Parent_Ref (Nam, Scope (Scop));
9393                end if;
9394             end if;
9395
9396             Endl := Identifier (Endl);
9397          end if;
9398       end if;
9399
9400       --  If the end label is not for the given entity, then either we have
9401       --  some previous error, or this is a generic instantiation for which
9402       --  we do not need to make a cross-reference in this case anyway. In
9403       --  either case we simply ignore the call.
9404
9405       if Chars (Ent) /= Chars (Endl) then
9406          return;
9407       end if;
9408
9409       --  If label was really there, then generate a normal reference and then
9410       --  adjust the location in the end label to point past the name (which
9411       --  should almost always be the semicolon).
9412
9413       Loc := Sloc (Endl);
9414
9415       if Comes_From_Source (Endl) then
9416
9417          --  If a label reference is required, then do the style check and
9418          --  generate an l-type cross-reference entry for the label
9419
9420          if Label_Ref then
9421             if Style_Check then
9422                Style.Check_Identifier (Endl, Ent);
9423             end if;
9424
9425             Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
9426          end if;
9427
9428          --  Set the location to point past the label (normally this will
9429          --  mean the semicolon immediately following the label). This is
9430          --  done for the sake of the 'e' or 't' entry generated below.
9431
9432          Get_Decoded_Name_String (Chars (Endl));
9433          Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
9434       end if;
9435
9436       --  Now generate the e/t reference
9437
9438       Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
9439
9440       --  Restore Sloc, in case modified above, since we have an identifier
9441       --  and the normal Sloc should be left set in the tree.
9442
9443       Set_Sloc (Endl, Loc);
9444    end Process_End_Label;
9445
9446    ------------------
9447    -- Real_Convert --
9448    ------------------
9449
9450    --  We do the conversion to get the value of the real string by using
9451    --  the scanner, see Sinput for details on use of the internal source
9452    --  buffer for scanning internal strings.
9453
9454    function Real_Convert (S : String) return Node_Id is
9455       Save_Src : constant Source_Buffer_Ptr := Source;
9456       Negative : Boolean;
9457
9458    begin
9459       Source := Internal_Source_Ptr;
9460       Scan_Ptr := 1;
9461
9462       for J in S'Range loop
9463          Source (Source_Ptr (J)) := S (J);
9464       end loop;
9465
9466       Source (S'Length + 1) := EOF;
9467
9468       if Source (Scan_Ptr) = '-' then
9469          Negative := True;
9470          Scan_Ptr := Scan_Ptr + 1;
9471       else
9472          Negative := False;
9473       end if;
9474
9475       Scan;
9476
9477       if Negative then
9478          Set_Realval (Token_Node, UR_Negate (Realval (Token_Node)));
9479       end if;
9480
9481       Source := Save_Src;
9482       return Token_Node;
9483    end Real_Convert;
9484
9485    --------------------
9486    -- Remove_Homonym --
9487    --------------------
9488
9489    procedure Remove_Homonym (E : Entity_Id) is
9490       Prev  : Entity_Id := Empty;
9491       H     : Entity_Id;
9492
9493    begin
9494       if E = Current_Entity (E) then
9495          if Present (Homonym (E)) then
9496             Set_Current_Entity (Homonym (E));
9497          else
9498             Set_Name_Entity_Id (Chars (E), Empty);
9499          end if;
9500       else
9501          H := Current_Entity (E);
9502          while Present (H) and then H /= E loop
9503             Prev := H;
9504             H    := Homonym (H);
9505          end loop;
9506
9507          Set_Homonym (Prev, Homonym (E));
9508       end if;
9509    end Remove_Homonym;
9510
9511    ---------------------
9512    -- Rep_To_Pos_Flag --
9513    ---------------------
9514
9515    function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
9516    begin
9517       return New_Occurrence_Of
9518                (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
9519    end Rep_To_Pos_Flag;
9520
9521    --------------------
9522    -- Require_Entity --
9523    --------------------
9524
9525    procedure Require_Entity (N : Node_Id) is
9526    begin
9527       if Is_Entity_Name (N) and then No (Entity (N)) then
9528          if Total_Errors_Detected /= 0 then
9529             Set_Entity (N, Any_Id);
9530          else
9531             raise Program_Error;
9532          end if;
9533       end if;
9534    end Require_Entity;
9535
9536    ------------------------------
9537    -- Requires_Transient_Scope --
9538    ------------------------------
9539
9540    --  A transient scope is required when variable-sized temporaries are
9541    --  allocated in the primary or secondary stack, or when finalization
9542    --  actions must be generated before the next instruction.
9543
9544    function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
9545       Typ : constant Entity_Id := Underlying_Type (Id);
9546
9547    --  Start of processing for Requires_Transient_Scope
9548
9549    begin
9550       --  This is a private type which is not completed yet. This can only
9551       --  happen in a default expression (of a formal parameter or of a
9552       --  record component). Do not expand transient scope in this case
9553
9554       if No (Typ) then
9555          return False;
9556
9557       --  Do not expand transient scope for non-existent procedure return
9558
9559       elsif Typ = Standard_Void_Type then
9560          return False;
9561
9562       --  Elementary types do not require a transient scope
9563
9564       elsif Is_Elementary_Type (Typ) then
9565          return False;
9566
9567       --  Generally, indefinite subtypes require a transient scope, since the
9568       --  back end cannot generate temporaries, since this is not a valid type
9569       --  for declaring an object. It might be possible to relax this in the
9570       --  future, e.g. by declaring the maximum possible space for the type.
9571
9572       elsif Is_Indefinite_Subtype (Typ) then
9573          return True;
9574
9575       --  Functions returning tagged types may dispatch on result so their
9576       --  returned value is allocated on the secondary stack. Controlled
9577       --  type temporaries need finalization.
9578
9579       elsif Is_Tagged_Type (Typ)
9580         or else Has_Controlled_Component (Typ)
9581       then
9582          return not Is_Value_Type (Typ);
9583
9584       --  Record type
9585
9586       elsif Is_Record_Type (Typ) then
9587          declare
9588             Comp : Entity_Id;
9589          begin
9590             Comp := First_Entity (Typ);
9591             while Present (Comp) loop
9592                if Ekind (Comp) = E_Component
9593                   and then Requires_Transient_Scope (Etype (Comp))
9594                then
9595                   return True;
9596                else
9597                   Next_Entity (Comp);
9598                end if;
9599             end loop;
9600          end;
9601
9602          return False;
9603
9604       --  String literal types never require transient scope
9605
9606       elsif Ekind (Typ) = E_String_Literal_Subtype then
9607          return False;
9608
9609       --  Array type. Note that we already know that this is a constrained
9610       --  array, since unconstrained arrays will fail the indefinite test.
9611
9612       elsif Is_Array_Type (Typ) then
9613
9614          --  If component type requires a transient scope, the array does too
9615
9616          if Requires_Transient_Scope (Component_Type (Typ)) then
9617             return True;
9618
9619          --  Otherwise, we only need a transient scope if the size is not
9620          --  known at compile time.
9621
9622          else
9623             return not Size_Known_At_Compile_Time (Typ);
9624          end if;
9625
9626       --  All other cases do not require a transient scope
9627
9628       else
9629          return False;
9630       end if;
9631    end Requires_Transient_Scope;
9632
9633    --------------------------
9634    -- Reset_Analyzed_Flags --
9635    --------------------------
9636
9637    procedure Reset_Analyzed_Flags (N : Node_Id) is
9638
9639       function Clear_Analyzed (N : Node_Id) return Traverse_Result;
9640       --  Function used to reset Analyzed flags in tree. Note that we do
9641       --  not reset Analyzed flags in entities, since there is no need to
9642       --  reanalyze entities, and indeed, it is wrong to do so, since it
9643       --  can result in generating auxiliary stuff more than once.
9644
9645       --------------------
9646       -- Clear_Analyzed --
9647       --------------------
9648
9649       function Clear_Analyzed (N : Node_Id) return Traverse_Result is
9650       begin
9651          if not Has_Extension (N) then
9652             Set_Analyzed (N, False);
9653          end if;
9654
9655          return OK;
9656       end Clear_Analyzed;
9657
9658       procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
9659
9660    --  Start of processing for Reset_Analyzed_Flags
9661
9662    begin
9663       Reset_Analyzed (N);
9664    end Reset_Analyzed_Flags;
9665
9666    ---------------------------
9667    -- Safe_To_Capture_Value --
9668    ---------------------------
9669
9670    function Safe_To_Capture_Value
9671      (N    : Node_Id;
9672       Ent  : Entity_Id;
9673       Cond : Boolean := False) return Boolean
9674    is
9675    begin
9676       --  The only entities for which we track constant values are variables
9677       --  which are not renamings, constants, out parameters, and in out
9678       --  parameters, so check if we have this case.
9679
9680       --  Note: it may seem odd to track constant values for constants, but in
9681       --  fact this routine is used for other purposes than simply capturing
9682       --  the value. In particular, the setting of Known[_Non]_Null.
9683
9684       if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
9685             or else
9686           Ekind (Ent) = E_Constant
9687             or else
9688           Ekind (Ent) = E_Out_Parameter
9689             or else
9690           Ekind (Ent) = E_In_Out_Parameter
9691       then
9692          null;
9693
9694       --  For conditionals, we also allow loop parameters and all formals,
9695       --  including in parameters.
9696
9697       elsif Cond
9698         and then
9699           (Ekind (Ent) = E_Loop_Parameter
9700              or else
9701            Ekind (Ent) = E_In_Parameter)
9702       then
9703          null;
9704
9705       --  For all other cases, not just unsafe, but impossible to capture
9706       --  Current_Value, since the above are the only entities which have
9707       --  Current_Value fields.
9708
9709       else
9710          return False;
9711       end if;
9712
9713       --  Skip if volatile or aliased, since funny things might be going on in
9714       --  these cases which we cannot necessarily track. Also skip any variable
9715       --  for which an address clause is given, or whose address is taken. Also
9716       --  never capture value of library level variables (an attempt to do so
9717       --  can occur in the case of package elaboration code).
9718
9719       if Treat_As_Volatile (Ent)
9720         or else Is_Aliased (Ent)
9721         or else Present (Address_Clause (Ent))
9722         or else Address_Taken (Ent)
9723         or else (Is_Library_Level_Entity (Ent)
9724                    and then Ekind (Ent) = E_Variable)
9725       then
9726          return False;
9727       end if;
9728
9729       --  OK, all above conditions are met. We also require that the scope of
9730       --  the reference be the same as the scope of the entity, not counting
9731       --  packages and blocks and loops.
9732
9733       declare
9734          E_Scope : constant Entity_Id := Scope (Ent);
9735          R_Scope : Entity_Id;
9736
9737       begin
9738          R_Scope := Current_Scope;
9739          while R_Scope /= Standard_Standard loop
9740             exit when R_Scope = E_Scope;
9741
9742             if Ekind (R_Scope) /= E_Package
9743                   and then
9744                 Ekind (R_Scope) /= E_Block
9745                   and then
9746                 Ekind (R_Scope) /= E_Loop
9747             then
9748                return False;
9749             else
9750                R_Scope := Scope (R_Scope);
9751             end if;
9752          end loop;
9753       end;
9754
9755       --  We also require that the reference does not appear in a context
9756       --  where it is not sure to be executed (i.e. a conditional context
9757       --  or an exception handler). We skip this if Cond is True, since the
9758       --  capturing of values from conditional tests handles this ok.
9759
9760       if Cond then
9761          return True;
9762       end if;
9763
9764       declare
9765          Desc : Node_Id;
9766          P    : Node_Id;
9767
9768       begin
9769          Desc := N;
9770
9771          P := Parent (N);
9772          while Present (P) loop
9773             if Nkind (P) = N_If_Statement
9774               or else  Nkind (P) = N_Case_Statement
9775               or else (Nkind (P) = N_And_Then and then Desc = Right_Opnd (P))
9776               or else (Nkind (P) = N_Or_Else and then Desc = Right_Opnd (P))
9777               or else  Nkind (P) = N_Exception_Handler
9778               or else  Nkind (P) = N_Selective_Accept
9779               or else  Nkind (P) = N_Conditional_Entry_Call
9780               or else  Nkind (P) = N_Timed_Entry_Call
9781               or else  Nkind (P) = N_Asynchronous_Select
9782             then
9783                return False;
9784             else
9785                Desc := P;
9786                P    := Parent (P);
9787             end if;
9788          end loop;
9789       end;
9790
9791       --  OK, looks safe to set value
9792
9793       return True;
9794    end Safe_To_Capture_Value;
9795
9796    ---------------
9797    -- Same_Name --
9798    ---------------
9799
9800    function Same_Name (N1, N2 : Node_Id) return Boolean is
9801       K1 : constant Node_Kind := Nkind (N1);
9802       K2 : constant Node_Kind := Nkind (N2);
9803
9804    begin
9805       if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
9806         and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
9807       then
9808          return Chars (N1) = Chars (N2);
9809
9810       elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
9811         and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
9812       then
9813          return Same_Name (Selector_Name (N1), Selector_Name (N2))
9814            and then Same_Name (Prefix (N1), Prefix (N2));
9815
9816       else
9817          return False;
9818       end if;
9819    end Same_Name;
9820
9821    -----------------
9822    -- Same_Object --
9823    -----------------
9824
9825    function Same_Object (Node1, Node2 : Node_Id) return Boolean is
9826       N1 : constant Node_Id := Original_Node (Node1);
9827       N2 : constant Node_Id := Original_Node (Node2);
9828       --  We do the tests on original nodes, since we are most interested
9829       --  in the original source, not any expansion that got in the way.
9830
9831       K1 : constant Node_Kind := Nkind (N1);
9832       K2 : constant Node_Kind := Nkind (N2);
9833
9834    begin
9835       --  First case, both are entities with same entity
9836
9837       if K1 in N_Has_Entity
9838         and then K2 in N_Has_Entity
9839         and then Present (Entity (N1))
9840         and then Present (Entity (N2))
9841         and then (Ekind (Entity (N1)) = E_Variable
9842                     or else
9843                   Ekind (Entity (N1)) = E_Constant)
9844         and then Entity (N1) = Entity (N2)
9845       then
9846          return True;
9847
9848       --  Second case, selected component with same selector, same record
9849
9850       elsif K1 = N_Selected_Component
9851         and then K2 = N_Selected_Component
9852         and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
9853       then
9854          return Same_Object (Prefix (N1), Prefix (N2));
9855
9856       --  Third case, indexed component with same subscripts, same array
9857
9858       elsif K1 = N_Indexed_Component
9859         and then K2 = N_Indexed_Component
9860         and then Same_Object (Prefix (N1), Prefix (N2))
9861       then
9862          declare
9863             E1, E2 : Node_Id;
9864          begin
9865             E1 := First (Expressions (N1));
9866             E2 := First (Expressions (N2));
9867             while Present (E1) loop
9868                if not Same_Value (E1, E2) then
9869                   return False;
9870                else
9871                   Next (E1);
9872                   Next (E2);
9873                end if;
9874             end loop;
9875
9876             return True;
9877          end;
9878
9879       --  Fourth case, slice of same array with same bounds
9880
9881       elsif K1 = N_Slice
9882         and then K2 = N_Slice
9883         and then Nkind (Discrete_Range (N1)) = N_Range
9884         and then Nkind (Discrete_Range (N2)) = N_Range
9885         and then Same_Value (Low_Bound (Discrete_Range (N1)),
9886                              Low_Bound (Discrete_Range (N2)))
9887         and then Same_Value (High_Bound (Discrete_Range (N1)),
9888                              High_Bound (Discrete_Range (N2)))
9889       then
9890          return Same_Name (Prefix (N1), Prefix (N2));
9891
9892       --  All other cases, not clearly the same object
9893
9894       else
9895          return False;
9896       end if;
9897    end Same_Object;
9898
9899    ---------------
9900    -- Same_Type --
9901    ---------------
9902
9903    function Same_Type (T1, T2 : Entity_Id) return Boolean is
9904    begin
9905       if T1 = T2 then
9906          return True;
9907
9908       elsif not Is_Constrained (T1)
9909         and then not Is_Constrained (T2)
9910         and then Base_Type (T1) = Base_Type (T2)
9911       then
9912          return True;
9913
9914       --  For now don't bother with case of identical constraints, to be
9915       --  fiddled with later on perhaps (this is only used for optimization
9916       --  purposes, so it is not critical to do a best possible job)
9917
9918       else
9919          return False;
9920       end if;
9921    end Same_Type;
9922
9923    ----------------
9924    -- Same_Value --
9925    ----------------
9926
9927    function Same_Value (Node1, Node2 : Node_Id) return Boolean is
9928    begin
9929       if Compile_Time_Known_Value (Node1)
9930         and then Compile_Time_Known_Value (Node2)
9931         and then Expr_Value (Node1) = Expr_Value (Node2)
9932       then
9933          return True;
9934       elsif Same_Object (Node1, Node2) then
9935          return True;
9936       else
9937          return False;
9938       end if;
9939    end Same_Value;
9940
9941    ------------------------
9942    -- Scope_Is_Transient --
9943    ------------------------
9944
9945    function Scope_Is_Transient return Boolean is
9946    begin
9947       return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
9948    end Scope_Is_Transient;
9949
9950    ------------------
9951    -- Scope_Within --
9952    ------------------
9953
9954    function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
9955       Scop : Entity_Id;
9956
9957    begin
9958       Scop := Scope1;
9959       while Scop /= Standard_Standard loop
9960          Scop := Scope (Scop);
9961
9962          if Scop = Scope2 then
9963             return True;
9964          end if;
9965       end loop;
9966
9967       return False;
9968    end Scope_Within;
9969
9970    --------------------------
9971    -- Scope_Within_Or_Same --
9972    --------------------------
9973
9974    function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
9975       Scop : Entity_Id;
9976
9977    begin
9978       Scop := Scope1;
9979       while Scop /= Standard_Standard loop
9980          if Scop = Scope2 then
9981             return True;
9982          else
9983             Scop := Scope (Scop);
9984          end if;
9985       end loop;
9986
9987       return False;
9988    end Scope_Within_Or_Same;
9989
9990    --------------------
9991    -- Set_Convention --
9992    --------------------
9993
9994    procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
9995    begin
9996       Basic_Set_Convention (E, Val);
9997
9998       if Is_Type (E)
9999         and then Is_Access_Subprogram_Type (Base_Type (E))
10000         and then Has_Foreign_Convention (E)
10001       then
10002          Set_Can_Use_Internal_Rep (E, False);
10003       end if;
10004    end Set_Convention;
10005
10006    ------------------------
10007    -- Set_Current_Entity --
10008    ------------------------
10009
10010    --  The given entity is to be set as the currently visible definition
10011    --  of its associated name (i.e. the Node_Id associated with its name).
10012    --  All we have to do is to get the name from the identifier, and
10013    --  then set the associated Node_Id to point to the given entity.
10014
10015    procedure Set_Current_Entity (E : Entity_Id) is
10016    begin
10017       Set_Name_Entity_Id (Chars (E), E);
10018    end Set_Current_Entity;
10019
10020    ---------------------------
10021    -- Set_Debug_Info_Needed --
10022    ---------------------------
10023
10024    procedure Set_Debug_Info_Needed (T : Entity_Id) is
10025
10026       procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
10027       pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
10028       --  Used to set debug info in a related node if not set already
10029
10030       --------------------------------------
10031       -- Set_Debug_Info_Needed_If_Not_Set --
10032       --------------------------------------
10033
10034       procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
10035       begin
10036          if Present (E)
10037            and then not Needs_Debug_Info (E)
10038          then
10039             Set_Debug_Info_Needed (E);
10040
10041             --  For a private type, indicate that the full view also needs
10042             --  debug information.
10043
10044             if Is_Type (E)
10045               and then Is_Private_Type (E)
10046               and then Present (Full_View (E))
10047             then
10048                Set_Debug_Info_Needed (Full_View (E));
10049             end if;
10050          end if;
10051       end Set_Debug_Info_Needed_If_Not_Set;
10052
10053    --  Start of processing for Set_Debug_Info_Needed
10054
10055    begin
10056       --  Nothing to do if argument is Empty or has Debug_Info_Off set, which
10057       --  indicates that Debug_Info_Needed is never required for the entity.
10058
10059       if No (T)
10060         or else Debug_Info_Off (T)
10061       then
10062          return;
10063       end if;
10064
10065       --  Set flag in entity itself. Note that we will go through the following
10066       --  circuitry even if the flag is already set on T. That's intentional,
10067       --  it makes sure that the flag will be set in subsidiary entities.
10068
10069       Set_Needs_Debug_Info (T);
10070
10071       --  Set flag on subsidiary entities if not set already
10072
10073       if Is_Object (T) then
10074          Set_Debug_Info_Needed_If_Not_Set (Etype (T));
10075
10076       elsif Is_Type (T) then
10077          Set_Debug_Info_Needed_If_Not_Set (Etype (T));
10078
10079          if Is_Record_Type (T) then
10080             declare
10081                Ent : Entity_Id := First_Entity (T);
10082             begin
10083                while Present (Ent) loop
10084                   Set_Debug_Info_Needed_If_Not_Set (Ent);
10085                   Next_Entity (Ent);
10086                end loop;
10087             end;
10088
10089          elsif Is_Array_Type (T) then
10090             Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
10091
10092             declare
10093                Indx : Node_Id := First_Index (T);
10094             begin
10095                while Present (Indx) loop
10096                   Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
10097                   Indx := Next_Index (Indx);
10098                end loop;
10099             end;
10100
10101             if Is_Packed (T) then
10102                Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Type (T));
10103             end if;
10104
10105          elsif Is_Access_Type (T) then
10106             Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
10107
10108          elsif Is_Private_Type (T) then
10109             Set_Debug_Info_Needed_If_Not_Set (Full_View (T));
10110
10111          elsif Is_Protected_Type (T) then
10112             Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
10113          end if;
10114       end if;
10115    end Set_Debug_Info_Needed;
10116
10117    ---------------------------------
10118    -- Set_Entity_With_Style_Check --
10119    ---------------------------------
10120
10121    procedure Set_Entity_With_Style_Check (N : Node_Id; Val : Entity_Id) is
10122       Val_Actual : Entity_Id;
10123       Nod        : Node_Id;
10124
10125    begin
10126       Set_Entity (N, Val);
10127
10128       if Style_Check
10129         and then not Suppress_Style_Checks (Val)
10130         and then not In_Instance
10131       then
10132          if Nkind (N) = N_Identifier then
10133             Nod := N;
10134          elsif Nkind (N) = N_Expanded_Name then
10135             Nod := Selector_Name (N);
10136          else
10137             return;
10138          end if;
10139
10140          --  A special situation arises for derived operations, where we want
10141          --  to do the check against the parent (since the Sloc of the derived
10142          --  operation points to the derived type declaration itself).
10143
10144          Val_Actual := Val;
10145          while not Comes_From_Source (Val_Actual)
10146            and then Nkind (Val_Actual) in N_Entity
10147            and then (Ekind (Val_Actual) = E_Enumeration_Literal
10148                       or else Is_Subprogram (Val_Actual)
10149                       or else Is_Generic_Subprogram (Val_Actual))
10150            and then Present (Alias (Val_Actual))
10151          loop
10152             Val_Actual := Alias (Val_Actual);
10153          end loop;
10154
10155          --  Renaming declarations for generic actuals do not come from source,
10156          --  and have a different name from that of the entity they rename, so
10157          --  there is no style check to perform here.
10158
10159          if Chars (Nod) = Chars (Val_Actual) then
10160             Style.Check_Identifier (Nod, Val_Actual);
10161          end if;
10162       end if;
10163
10164       Set_Entity (N, Val);
10165    end Set_Entity_With_Style_Check;
10166
10167    ------------------------
10168    -- Set_Name_Entity_Id --
10169    ------------------------
10170
10171    procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
10172    begin
10173       Set_Name_Table_Info (Id, Int (Val));
10174    end Set_Name_Entity_Id;
10175
10176    ---------------------
10177    -- Set_Next_Actual --
10178    ---------------------
10179
10180    procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
10181    begin
10182       if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
10183          Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
10184       end if;
10185    end Set_Next_Actual;
10186
10187    ----------------------------------
10188    -- Set_Optimize_Alignment_Flags --
10189    ----------------------------------
10190
10191    procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
10192    begin
10193       if Optimize_Alignment = 'S' then
10194          Set_Optimize_Alignment_Space (E);
10195       elsif Optimize_Alignment = 'T' then
10196          Set_Optimize_Alignment_Time (E);
10197       end if;
10198    end Set_Optimize_Alignment_Flags;
10199
10200    -----------------------
10201    -- Set_Public_Status --
10202    -----------------------
10203
10204    procedure Set_Public_Status (Id : Entity_Id) is
10205       S : constant Entity_Id := Current_Scope;
10206
10207       function Within_HSS_Or_If (E : Entity_Id) return Boolean;
10208       --  Determines if E is defined within handled statement sequence or
10209       --  an if statement, returns True if so, False otherwise.
10210
10211       ----------------------
10212       -- Within_HSS_Or_If --
10213       ----------------------
10214
10215       function Within_HSS_Or_If (E : Entity_Id) return Boolean is
10216          N : Node_Id;
10217       begin
10218          N := Declaration_Node (E);
10219          loop
10220             N := Parent (N);
10221
10222             if No (N) then
10223                return False;
10224
10225             elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
10226                                N_If_Statement)
10227             then
10228                return True;
10229             end if;
10230          end loop;
10231       end Within_HSS_Or_If;
10232
10233    --  Start of processing for Set_Public_Status
10234
10235    begin
10236       --  Everything in the scope of Standard is public
10237
10238       if S = Standard_Standard then
10239          Set_Is_Public (Id);
10240
10241       --  Entity is definitely not public if enclosing scope is not public
10242
10243       elsif not Is_Public (S) then
10244          return;
10245
10246       --  An object or function declaration that occurs in a handled sequence
10247       --  of statements or within an if statement is the declaration for a
10248       --  temporary object or local subprogram generated by the expander. It
10249       --  never needs to be made public and furthermore, making it public can
10250       --  cause back end problems.
10251
10252       elsif Nkind_In (Parent (Id), N_Object_Declaration,
10253                                    N_Function_Specification)
10254         and then Within_HSS_Or_If (Id)
10255       then
10256          return;
10257
10258       --  Entities in public packages or records are public
10259
10260       elsif Ekind (S) = E_Package or Is_Record_Type (S) then
10261          Set_Is_Public (Id);
10262
10263       --  The bounds of an entry family declaration can generate object
10264       --  declarations that are visible to the back-end, e.g. in the
10265       --  the declaration of a composite type that contains tasks.
10266
10267       elsif Is_Concurrent_Type (S)
10268         and then not Has_Completion (S)
10269         and then Nkind (Parent (Id)) = N_Object_Declaration
10270       then
10271          Set_Is_Public (Id);
10272       end if;
10273    end Set_Public_Status;
10274
10275    -----------------------------
10276    -- Set_Referenced_Modified --
10277    -----------------------------
10278
10279    procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
10280       Pref : Node_Id;
10281
10282    begin
10283       --  Deal with indexed or selected component where prefix is modified
10284
10285       if Nkind (N) = N_Indexed_Component
10286            or else
10287          Nkind (N) = N_Selected_Component
10288       then
10289          Pref := Prefix (N);
10290
10291          --  If prefix is access type, then it is the designated object that is
10292          --  being modified, which means we have no entity to set the flag on.
10293
10294          if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
10295             return;
10296
10297             --  Otherwise chase the prefix
10298
10299          else
10300             Set_Referenced_Modified (Pref, Out_Param);
10301          end if;
10302
10303       --  Otherwise see if we have an entity name (only other case to process)
10304
10305       elsif Is_Entity_Name (N) and then Present (Entity (N)) then
10306          Set_Referenced_As_LHS           (Entity (N), not Out_Param);
10307          Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
10308       end if;
10309    end Set_Referenced_Modified;
10310
10311    ----------------------------
10312    -- Set_Scope_Is_Transient --
10313    ----------------------------
10314
10315    procedure Set_Scope_Is_Transient (V : Boolean := True) is
10316    begin
10317       Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
10318    end Set_Scope_Is_Transient;
10319
10320    -------------------
10321    -- Set_Size_Info --
10322    -------------------
10323
10324    procedure Set_Size_Info (T1, T2 : Entity_Id) is
10325    begin
10326       --  We copy Esize, but not RM_Size, since in general RM_Size is
10327       --  subtype specific and does not get inherited by all subtypes.
10328
10329       Set_Esize                     (T1, Esize                     (T2));
10330       Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
10331
10332       if Is_Discrete_Or_Fixed_Point_Type (T1)
10333            and then
10334          Is_Discrete_Or_Fixed_Point_Type (T2)
10335       then
10336          Set_Is_Unsigned_Type       (T1, Is_Unsigned_Type          (T2));
10337       end if;
10338
10339       Set_Alignment                 (T1, Alignment                 (T2));
10340    end Set_Size_Info;
10341
10342    --------------------
10343    -- Static_Integer --
10344    --------------------
10345
10346    function Static_Integer (N : Node_Id) return Uint is
10347    begin
10348       Analyze_And_Resolve (N, Any_Integer);
10349
10350       if N = Error
10351         or else Error_Posted (N)
10352         or else Etype (N) = Any_Type
10353       then
10354          return No_Uint;
10355       end if;
10356
10357       if Is_Static_Expression (N) then
10358          if not Raises_Constraint_Error (N) then
10359             return Expr_Value (N);
10360          else
10361             return No_Uint;
10362          end if;
10363
10364       elsif Etype (N) = Any_Type then
10365          return No_Uint;
10366
10367       else
10368          Flag_Non_Static_Expr
10369            ("static integer expression required here", N);
10370          return No_Uint;
10371       end if;
10372    end Static_Integer;
10373
10374    --------------------------
10375    -- Statically_Different --
10376    --------------------------
10377
10378    function Statically_Different (E1, E2 : Node_Id) return Boolean is
10379       R1 : constant Node_Id := Get_Referenced_Object (E1);
10380       R2 : constant Node_Id := Get_Referenced_Object (E2);
10381    begin
10382       return     Is_Entity_Name (R1)
10383         and then Is_Entity_Name (R2)
10384         and then Entity (R1) /= Entity (R2)
10385         and then not Is_Formal (Entity (R1))
10386         and then not Is_Formal (Entity (R2));
10387    end Statically_Different;
10388
10389    -----------------------------
10390    -- Subprogram_Access_Level --
10391    -----------------------------
10392
10393    function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
10394    begin
10395       if Present (Alias (Subp)) then
10396          return Subprogram_Access_Level (Alias (Subp));
10397       else
10398          return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
10399       end if;
10400    end Subprogram_Access_Level;
10401
10402    -----------------
10403    -- Trace_Scope --
10404    -----------------
10405
10406    procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
10407    begin
10408       if Debug_Flag_W then
10409          for J in 0 .. Scope_Stack.Last loop
10410             Write_Str ("  ");
10411          end loop;
10412
10413          Write_Str (Msg);
10414          Write_Name (Chars (E));
10415          Write_Str (" from ");
10416          Write_Location (Sloc (N));
10417          Write_Eol;
10418       end if;
10419    end Trace_Scope;
10420
10421    -----------------------
10422    -- Transfer_Entities --
10423    -----------------------
10424
10425    procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
10426       Ent : Entity_Id := First_Entity (From);
10427
10428    begin
10429       if No (Ent) then
10430          return;
10431       end if;
10432
10433       if (Last_Entity (To)) = Empty then
10434          Set_First_Entity (To, Ent);
10435       else
10436          Set_Next_Entity (Last_Entity (To), Ent);
10437       end if;
10438
10439       Set_Last_Entity (To, Last_Entity (From));
10440
10441       while Present (Ent) loop
10442          Set_Scope (Ent, To);
10443
10444          if not Is_Public (Ent) then
10445             Set_Public_Status (Ent);
10446
10447             if Is_Public (Ent)
10448               and then Ekind (Ent) = E_Record_Subtype
10449
10450             then
10451                --  The components of the propagated Itype must be public
10452                --  as well.
10453
10454                declare
10455                   Comp : Entity_Id;
10456                begin
10457                   Comp := First_Entity (Ent);
10458                   while Present (Comp) loop
10459                      Set_Is_Public (Comp);
10460                      Next_Entity (Comp);
10461                   end loop;
10462                end;
10463             end if;
10464          end if;
10465
10466          Next_Entity (Ent);
10467       end loop;
10468
10469       Set_First_Entity (From, Empty);
10470       Set_Last_Entity (From, Empty);
10471    end Transfer_Entities;
10472
10473    -----------------------
10474    -- Type_Access_Level --
10475    -----------------------
10476
10477    function Type_Access_Level (Typ : Entity_Id) return Uint is
10478       Btyp : Entity_Id;
10479
10480    begin
10481       Btyp := Base_Type (Typ);
10482
10483       --  Ada 2005 (AI-230): For most cases of anonymous access types, we
10484       --  simply use the level where the type is declared. This is true for
10485       --  stand-alone object declarations, and for anonymous access types
10486       --  associated with components the level is the same as that of the
10487       --  enclosing composite type. However, special treatment is needed for
10488       --  the cases of access parameters, return objects of an anonymous access
10489       --  type, and, in Ada 95, access discriminants of limited types.
10490
10491       if Ekind (Btyp) in Access_Kind then
10492          if Ekind (Btyp) = E_Anonymous_Access_Type then
10493
10494             --  If the type is a nonlocal anonymous access type (such as for
10495             --  an access parameter) we treat it as being declared at the
10496             --  library level to ensure that names such as X.all'access don't
10497             --  fail static accessibility checks.
10498
10499             if not Is_Local_Anonymous_Access (Typ) then
10500                return Scope_Depth (Standard_Standard);
10501
10502             --  If this is a return object, the accessibility level is that of
10503             --  the result subtype of the enclosing function. The test here is
10504             --  little complicated, because we have to account for extended
10505             --  return statements that have been rewritten as blocks, in which
10506             --  case we have to find and the Is_Return_Object attribute of the
10507             --  itype's associated object. It would be nice to find a way to
10508             --  simplify this test, but it doesn't seem worthwhile to add a new
10509             --  flag just for purposes of this test. ???
10510
10511             elsif Ekind (Scope (Btyp)) = E_Return_Statement
10512               or else
10513                 (Is_Itype (Btyp)
10514                   and then Nkind (Associated_Node_For_Itype (Btyp)) =
10515                              N_Object_Declaration
10516                   and then Is_Return_Object
10517                              (Defining_Identifier
10518                                 (Associated_Node_For_Itype (Btyp))))
10519             then
10520                declare
10521                   Scop : Entity_Id;
10522
10523                begin
10524                   Scop := Scope (Scope (Btyp));
10525                   while Present (Scop) loop
10526                      exit when Ekind (Scop) = E_Function;
10527                      Scop := Scope (Scop);
10528                   end loop;
10529
10530                   --  Treat the return object's type as having the level of the
10531                   --  function's result subtype (as per RM05-6.5(5.3/2)).
10532
10533                   return Type_Access_Level (Etype (Scop));
10534                end;
10535             end if;
10536          end if;
10537
10538          Btyp := Root_Type (Btyp);
10539
10540          --  The accessibility level of anonymous access types associated with
10541          --  discriminants is that of the current instance of the type, and
10542          --  that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
10543
10544          --  AI-402: access discriminants have accessibility based on the
10545          --  object rather than the type in Ada 2005, so the above paragraph
10546          --  doesn't apply.
10547
10548          --  ??? Needs completion with rules from AI-416
10549
10550          if Ada_Version <= Ada_95
10551            and then Ekind (Typ) = E_Anonymous_Access_Type
10552            and then Present (Associated_Node_For_Itype (Typ))
10553            and then Nkind (Associated_Node_For_Itype (Typ)) =
10554                                                  N_Discriminant_Specification
10555          then
10556             return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
10557          end if;
10558       end if;
10559
10560       return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
10561    end Type_Access_Level;
10562
10563    --------------------
10564    -- Ultimate_Alias --
10565    --------------------
10566    --  To do: add occurrences calling this new subprogram
10567
10568    function Ultimate_Alias (Prim : Entity_Id) return Entity_Id is
10569       E : Entity_Id := Prim;
10570
10571    begin
10572       while Present (Alias (E)) loop
10573          E := Alias (E);
10574       end loop;
10575
10576       return E;
10577    end Ultimate_Alias;
10578
10579    --------------------------
10580    -- Unit_Declaration_Node --
10581    --------------------------
10582
10583    function Unit_Declaration_Node (Unit_Id : Entity_Id) return Node_Id is
10584       N : Node_Id := Parent (Unit_Id);
10585
10586    begin
10587       --  Predefined operators do not have a full function declaration
10588
10589       if Ekind (Unit_Id) = E_Operator then
10590          return N;
10591       end if;
10592
10593       --  Isn't there some better way to express the following ???
10594
10595       while Nkind (N) /= N_Abstract_Subprogram_Declaration
10596         and then Nkind (N) /= N_Formal_Package_Declaration
10597         and then Nkind (N) /= N_Function_Instantiation
10598         and then Nkind (N) /= N_Generic_Package_Declaration
10599         and then Nkind (N) /= N_Generic_Subprogram_Declaration
10600         and then Nkind (N) /= N_Package_Declaration
10601         and then Nkind (N) /= N_Package_Body
10602         and then Nkind (N) /= N_Package_Instantiation
10603         and then Nkind (N) /= N_Package_Renaming_Declaration
10604         and then Nkind (N) /= N_Procedure_Instantiation
10605         and then Nkind (N) /= N_Protected_Body
10606         and then Nkind (N) /= N_Subprogram_Declaration
10607         and then Nkind (N) /= N_Subprogram_Body
10608         and then Nkind (N) /= N_Subprogram_Body_Stub
10609         and then Nkind (N) /= N_Subprogram_Renaming_Declaration
10610         and then Nkind (N) /= N_Task_Body
10611         and then Nkind (N) /= N_Task_Type_Declaration
10612         and then Nkind (N) not in N_Formal_Subprogram_Declaration
10613         and then Nkind (N) not in N_Generic_Renaming_Declaration
10614       loop
10615          N := Parent (N);
10616          pragma Assert (Present (N));
10617       end loop;
10618
10619       return N;
10620    end Unit_Declaration_Node;
10621
10622    ------------------------------
10623    -- Universal_Interpretation --
10624    ------------------------------
10625
10626    function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
10627       Index : Interp_Index;
10628       It    : Interp;
10629
10630    begin
10631       --  The argument may be a formal parameter of an operator or subprogram
10632       --  with multiple interpretations, or else an expression for an actual.
10633
10634       if Nkind (Opnd) = N_Defining_Identifier
10635         or else not Is_Overloaded (Opnd)
10636       then
10637          if Etype (Opnd) = Universal_Integer
10638            or else Etype (Opnd) = Universal_Real
10639          then
10640             return Etype (Opnd);
10641          else
10642             return Empty;
10643          end if;
10644
10645       else
10646          Get_First_Interp (Opnd, Index, It);
10647          while Present (It.Typ) loop
10648             if It.Typ = Universal_Integer
10649               or else It.Typ = Universal_Real
10650             then
10651                return It.Typ;
10652             end if;
10653
10654             Get_Next_Interp (Index, It);
10655          end loop;
10656
10657          return Empty;
10658       end if;
10659    end Universal_Interpretation;
10660
10661    ---------------
10662    -- Unqualify --
10663    ---------------
10664
10665    function Unqualify (Expr : Node_Id) return Node_Id is
10666    begin
10667       --  Recurse to handle unlikely case of multiple levels of qualification
10668
10669       if Nkind (Expr) = N_Qualified_Expression then
10670          return Unqualify (Expression (Expr));
10671
10672       --  Normal case, not a qualified expression
10673
10674       else
10675          return Expr;
10676       end if;
10677    end Unqualify;
10678
10679    ----------------------
10680    -- Within_Init_Proc --
10681    ----------------------
10682
10683    function Within_Init_Proc return Boolean is
10684       S : Entity_Id;
10685
10686    begin
10687       S := Current_Scope;
10688       while not Is_Overloadable (S) loop
10689          if S = Standard_Standard then
10690             return False;
10691          else
10692             S := Scope (S);
10693          end if;
10694       end loop;
10695
10696       return Is_Init_Proc (S);
10697    end Within_Init_Proc;
10698
10699    ----------------
10700    -- Wrong_Type --
10701    ----------------
10702
10703    procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
10704       Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
10705       Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
10706
10707       function Has_One_Matching_Field return Boolean;
10708       --  Determines if Expec_Type is a record type with a single component or
10709       --  discriminant whose type matches the found type or is one dimensional
10710       --  array whose component type matches the found type.
10711
10712       ----------------------------
10713       -- Has_One_Matching_Field --
10714       ----------------------------
10715
10716       function Has_One_Matching_Field return Boolean is
10717          E : Entity_Id;
10718
10719       begin
10720          if Is_Array_Type (Expec_Type)
10721            and then Number_Dimensions (Expec_Type) = 1
10722            and then
10723              Covers (Etype (Component_Type (Expec_Type)), Found_Type)
10724          then
10725             return True;
10726
10727          elsif not Is_Record_Type (Expec_Type) then
10728             return False;
10729
10730          else
10731             E := First_Entity (Expec_Type);
10732             loop
10733                if No (E) then
10734                   return False;
10735
10736                elsif (Ekind (E) /= E_Discriminant
10737                        and then Ekind (E) /= E_Component)
10738                  or else (Chars (E) = Name_uTag
10739                            or else Chars (E) = Name_uParent)
10740                then
10741                   Next_Entity (E);
10742
10743                else
10744                   exit;
10745                end if;
10746             end loop;
10747
10748             if not Covers (Etype (E), Found_Type) then
10749                return False;
10750
10751             elsif Present (Next_Entity (E)) then
10752                return False;
10753
10754             else
10755                return True;
10756             end if;
10757          end if;
10758       end Has_One_Matching_Field;
10759
10760    --  Start of processing for Wrong_Type
10761
10762    begin
10763       --  Don't output message if either type is Any_Type, or if a message
10764       --  has already been posted for this node. We need to do the latter
10765       --  check explicitly (it is ordinarily done in Errout), because we
10766       --  are using ! to force the output of the error messages.
10767
10768       if Expec_Type = Any_Type
10769         or else Found_Type = Any_Type
10770         or else Error_Posted (Expr)
10771       then
10772          return;
10773
10774       --  In  an instance, there is an ongoing problem with completion of
10775       --  type derived from private types. Their structure is what Gigi
10776       --  expects, but the  Etype is the parent type rather than the
10777       --  derived private type itself. Do not flag error in this case. The
10778       --  private completion is an entity without a parent, like an Itype.
10779       --  Similarly, full and partial views may be incorrect in the instance.
10780       --  There is no simple way to insure that it is consistent ???
10781
10782       elsif In_Instance then
10783          if Etype (Etype (Expr)) = Etype (Expected_Type)
10784            and then
10785              (Has_Private_Declaration (Expected_Type)
10786                or else Has_Private_Declaration (Etype (Expr)))
10787            and then No (Parent (Expected_Type))
10788          then
10789             return;
10790          end if;
10791       end if;
10792
10793       --  An interesting special check. If the expression is parenthesized
10794       --  and its type corresponds to the type of the sole component of the
10795       --  expected record type, or to the component type of the expected one
10796       --  dimensional array type, then assume we have a bad aggregate attempt.
10797
10798       if Nkind (Expr) in N_Subexpr
10799         and then Paren_Count (Expr) /= 0
10800         and then Has_One_Matching_Field
10801       then
10802          Error_Msg_N ("positional aggregate cannot have one component", Expr);
10803
10804       --  Another special check, if we are looking for a pool-specific access
10805       --  type and we found an E_Access_Attribute_Type, then we have the case
10806       --  of an Access attribute being used in a context which needs a pool-
10807       --  specific type, which is never allowed. The one extra check we make
10808       --  is that the expected designated type covers the Found_Type.
10809
10810       elsif Is_Access_Type (Expec_Type)
10811         and then Ekind (Found_Type) = E_Access_Attribute_Type
10812         and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
10813         and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
10814         and then Covers
10815           (Designated_Type (Expec_Type), Designated_Type (Found_Type))
10816       then
10817          Error_Msg_N ("result must be general access type!", Expr);
10818          Error_Msg_NE ("add ALL to }!", Expr, Expec_Type);
10819
10820       --  Another special check, if the expected type is an integer type,
10821       --  but the expression is of type System.Address, and the parent is
10822       --  an addition or subtraction operation whose left operand is the
10823       --  expression in question and whose right operand is of an integral
10824       --  type, then this is an attempt at address arithmetic, so give
10825       --  appropriate message.
10826
10827       elsif Is_Integer_Type (Expec_Type)
10828         and then Is_RTE (Found_Type, RE_Address)
10829         and then (Nkind (Parent (Expr)) = N_Op_Add
10830                     or else
10831                   Nkind (Parent (Expr)) = N_Op_Subtract)
10832         and then Expr = Left_Opnd (Parent (Expr))
10833         and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
10834       then
10835          Error_Msg_N
10836            ("address arithmetic not predefined in package System",
10837             Parent (Expr));
10838          Error_Msg_N
10839            ("\possible missing with/use of System.Storage_Elements",
10840             Parent (Expr));
10841          return;
10842
10843       --  If the expected type is an anonymous access type, as for access
10844       --  parameters and discriminants, the error is on the designated types.
10845
10846       elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
10847          if Comes_From_Source (Expec_Type) then
10848             Error_Msg_NE ("expected}!", Expr, Expec_Type);
10849          else
10850             Error_Msg_NE
10851               ("expected an access type with designated}",
10852                  Expr, Designated_Type (Expec_Type));
10853          end if;
10854
10855          if Is_Access_Type (Found_Type)
10856            and then not Comes_From_Source (Found_Type)
10857          then
10858             Error_Msg_NE
10859               ("\\found an access type with designated}!",
10860                 Expr, Designated_Type (Found_Type));
10861          else
10862             if From_With_Type (Found_Type) then
10863                Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
10864                Error_Msg_Qual_Level := 99;
10865                Error_Msg_NE ("\\missing `WITH &;", Expr, Scope (Found_Type));
10866                Error_Msg_Qual_Level := 0;
10867             else
10868                Error_Msg_NE ("found}!", Expr, Found_Type);
10869             end if;
10870          end if;
10871
10872       --  Normal case of one type found, some other type expected
10873
10874       else
10875          --  If the names of the two types are the same, see if some number
10876          --  of levels of qualification will help. Don't try more than three
10877          --  levels, and if we get to standard, it's no use (and probably
10878          --  represents an error in the compiler) Also do not bother with
10879          --  internal scope names.
10880
10881          declare
10882             Expec_Scope : Entity_Id;
10883             Found_Scope : Entity_Id;
10884
10885          begin
10886             Expec_Scope := Expec_Type;
10887             Found_Scope := Found_Type;
10888
10889             for Levels in Int range 0 .. 3 loop
10890                if Chars (Expec_Scope) /= Chars (Found_Scope) then
10891                   Error_Msg_Qual_Level := Levels;
10892                   exit;
10893                end if;
10894
10895                Expec_Scope := Scope (Expec_Scope);
10896                Found_Scope := Scope (Found_Scope);
10897
10898                exit when Expec_Scope = Standard_Standard
10899                  or else Found_Scope = Standard_Standard
10900                  or else not Comes_From_Source (Expec_Scope)
10901                  or else not Comes_From_Source (Found_Scope);
10902             end loop;
10903          end;
10904
10905          if Is_Record_Type (Expec_Type)
10906            and then Present (Corresponding_Remote_Type (Expec_Type))
10907          then
10908             Error_Msg_NE ("expected}!", Expr,
10909                           Corresponding_Remote_Type (Expec_Type));
10910          else
10911             Error_Msg_NE ("expected}!", Expr, Expec_Type);
10912          end if;
10913
10914          if Is_Entity_Name (Expr)
10915            and then Is_Package_Or_Generic_Package (Entity (Expr))
10916          then
10917             Error_Msg_N ("\\found package name!", Expr);
10918
10919          elsif Is_Entity_Name (Expr)
10920            and then
10921              (Ekind (Entity (Expr)) = E_Procedure
10922                 or else
10923               Ekind (Entity (Expr)) = E_Generic_Procedure)
10924          then
10925             if Ekind (Expec_Type) = E_Access_Subprogram_Type then
10926                Error_Msg_N
10927                  ("found procedure name, possibly missing Access attribute!",
10928                    Expr);
10929             else
10930                Error_Msg_N
10931                  ("\\found procedure name instead of function!", Expr);
10932             end if;
10933
10934          elsif Nkind (Expr) = N_Function_Call
10935            and then Ekind (Expec_Type) = E_Access_Subprogram_Type
10936            and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
10937            and then No (Parameter_Associations (Expr))
10938          then
10939             Error_Msg_N
10940               ("found function name, possibly missing Access attribute!",
10941                Expr);
10942
10943          --  Catch common error: a prefix or infix operator which is not
10944          --  directly visible because the type isn't.
10945
10946          elsif Nkind (Expr) in N_Op
10947             and then Is_Overloaded (Expr)
10948             and then not Is_Immediately_Visible (Expec_Type)
10949             and then not Is_Potentially_Use_Visible (Expec_Type)
10950             and then not In_Use (Expec_Type)
10951             and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
10952          then
10953             Error_Msg_N
10954               ("operator of the type is not directly visible!", Expr);
10955
10956          elsif Ekind (Found_Type) = E_Void
10957            and then Present (Parent (Found_Type))
10958            and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
10959          then
10960             Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
10961
10962          else
10963             Error_Msg_NE ("\\found}!", Expr, Found_Type);
10964          end if;
10965
10966          Error_Msg_Qual_Level := 0;
10967       end if;
10968    end Wrong_Type;
10969
10970 end Sem_Util;