OSDN Git Service

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