OSDN Git Service

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