OSDN Git Service

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