OSDN Git Service

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