OSDN Git Service

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