OSDN Git Service

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