OSDN Git Service

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