1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                              S E M _ C H 7                               --
6--                                                                          --
7--                                 B o d y                                  --
8--                                                                          --
9--          Copyright (C) 1992-2019, 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--  This package contains the routines to process package specifications and
27--  bodies. The most important semantic aspects of package processing are the
28--  handling of private and full declarations, and the construction of dispatch
29--  tables for tagged types.
30
31with Aspects;   use Aspects;
32with Atree;     use Atree;
33with Contracts; use Contracts;
34with Debug;     use Debug;
35with Einfo;     use Einfo;
36with Elists;    use Elists;
37with Errout;    use Errout;
38with Exp_Disp;  use Exp_Disp;
39with Exp_Dist;  use Exp_Dist;
40with Exp_Dbug;  use Exp_Dbug;
41with Freeze;    use Freeze;
42with Ghost;     use Ghost;
43with Lib;       use Lib;
44with Lib.Xref;  use Lib.Xref;
45with Namet;     use Namet;
46with Nmake;     use Nmake;
47with Nlists;    use Nlists;
48with Opt;       use Opt;
49with Output;    use Output;
50with Restrict;  use Restrict;
51with Rtsfind;   use Rtsfind;
52with Sem;       use Sem;
53with Sem_Aux;   use Sem_Aux;
54with Sem_Cat;   use Sem_Cat;
55with Sem_Ch3;   use Sem_Ch3;
56with Sem_Ch6;   use Sem_Ch6;
57with Sem_Ch8;   use Sem_Ch8;
58with Sem_Ch10;  use Sem_Ch10;
59with Sem_Ch12;  use Sem_Ch12;
60with Sem_Ch13;  use Sem_Ch13;
61with Sem_Disp;  use Sem_Disp;
62with Sem_Eval;  use Sem_Eval;
63with Sem_Prag;  use Sem_Prag;
64with Sem_Util;  use Sem_Util;
65with Sem_Warn;  use Sem_Warn;
66with Snames;    use Snames;
67with Stand;     use Stand;
68with Sinfo;     use Sinfo;
69with Sinput;    use Sinput;
70with Style;
71with Uintp;     use Uintp;
72
73with GNAT.HTable;
74
75package body Sem_Ch7 is
76
77   -----------------------------------
78   -- Handling private declarations --
79   -----------------------------------
80
81   --  The principle that each entity has a single defining occurrence clashes
82   --  with the presence of two separate definitions for private types: the
83   --  first is the private type declaration, and the second is the full type
84   --  declaration. It is important that all references to the type point to
85   --  the same defining occurrence, namely the first one. To enforce the two
86   --  separate views of the entity, the corresponding information is swapped
87   --  between the two declarations. Outside of the package, the defining
88   --  occurrence only contains the private declaration information, while in
89   --  the private part and the body of the package the defining occurrence
90   --  contains the full declaration. To simplify the swap, the defining
91   --  occurrence that currently holds the private declaration points to the
92   --  full declaration. During semantic processing the defining occurrence
93   --  also points to a list of private dependents, that is to say access types
94   --  or composite types whose designated types or component types are
95   --  subtypes or derived types of the private type in question. After the
96   --  full declaration has been seen, the private dependents are updated to
97   --  indicate that they have full definitions.
98
99   -----------------------
100   -- Local Subprograms --
101   -----------------------
102
103   procedure Analyze_Package_Body_Helper (N : Node_Id);
104   --  Does all the real work of Analyze_Package_Body
105
106   procedure Check_Anonymous_Access_Types
107     (Spec_Id : Entity_Id;
108      P_Body  : Node_Id);
109   --  If the spec of a package has a limited_with_clause, it may declare
110   --  anonymous access types whose designated type is a limited view, such an
111   --  anonymous access return type for a function. This access type cannot be
112   --  elaborated in the spec itself, but it may need an itype reference if it
113   --  is used within a nested scope. In that case the itype reference is
114   --  created at the beginning of the corresponding package body and inserted
115   --  before other body declarations.
116
117   procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id);
118   --  Called upon entering the private part of a public child package and the
119   --  body of a nested package, to potentially declare certain inherited
120   --  subprograms that were inherited by types in the visible part, but whose
121   --  declaration was deferred because the parent operation was private and
122   --  not visible at that point. These subprograms are located by traversing
123   --  the visible part declarations looking for non-private type extensions
124   --  and then examining each of the primitive operations of such types to
125   --  find those that were inherited but declared with a special internal
126   --  name. Each such operation is now declared as an operation with a normal
127   --  name (using the name of the parent operation) and replaces the previous
128   --  implicit operation in the primitive operations list of the type. If the
129   --  inherited private operation has been overridden, then it's replaced by
130   --  the overriding operation.
131
132   procedure Install_Package_Entity (Id : Entity_Id);
133   --  Supporting procedure for Install_{Visible,Private}_Declarations. Places
134   --  one entity on its visibility chain, and recurses on the visible part if
135   --  the entity is an inner package.
136
137   function Is_Private_Base_Type (E : Entity_Id) return Boolean;
138   --  True for a private type that is not a subtype
139
140   function Is_Visible_Dependent (Dep : Entity_Id) return Boolean;
141   --  If the private dependent is a private type whose full view is derived
142   --  from the parent type, its full properties are revealed only if we are in
143   --  the immediate scope of the private dependent. Should this predicate be
144   --  tightened further???
145
146   function Requires_Completion_In_Body
147     (Id                 : Entity_Id;
148      Pack_Id            : Entity_Id;
149      Do_Abstract_States : Boolean := False) return Boolean;
150   --  Subsidiary to routines Unit_Requires_Body and Unit_Requires_Body_Info.
151   --  Determine whether entity Id declared in package spec Pack_Id requires
152   --  completion in a package body. Flag Do_Abstract_Stats should be set when
153   --  abstract states are to be considered in the completion test.
154
155   procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id);
156   --  Outputs info messages showing why package Pack_Id requires a body. The
157   --  caller has checked that the switch requesting this information is set,
158   --  and that the package does indeed require a body.
159
160   --------------------------
161   -- Analyze_Package_Body --
162   --------------------------
163
164   procedure Analyze_Package_Body (N : Node_Id) is
165      Loc : constant Source_Ptr := Sloc (N);
166
167   begin
168      if Debug_Flag_C then
169         Write_Str ("==> package body ");
170         Write_Name (Chars (Defining_Entity (N)));
171         Write_Str (" from ");
172         Write_Location (Loc);
173         Write_Eol;
174         Indent;
175      end if;
176
177      --  The real work is split out into the helper, so it can do "return;"
178      --  without skipping the debug output.
179
180      Analyze_Package_Body_Helper (N);
181
182      if Debug_Flag_C then
183         Outdent;
184         Write_Str ("<== package body ");
185         Write_Name (Chars (Defining_Entity (N)));
186         Write_Str (" from ");
187         Write_Location (Loc);
188         Write_Eol;
189      end if;
190   end Analyze_Package_Body;
191
192   ------------------------------------------------------
193   -- Analyze_Package_Body_Helper Data and Subprograms --
194   ------------------------------------------------------
195
196   Entity_Table_Size : constant := 4093;
197   --  Number of headers in hash table
198
199   subtype Entity_Header_Num is Integer range 0 .. Entity_Table_Size - 1;
200   --  Range of headers in hash table
201
202   function Node_Hash (Id : Entity_Id) return Entity_Header_Num;
203   --  Simple hash function for Entity_Ids
204
205   package Subprogram_Table is new GNAT.Htable.Simple_HTable
206     (Header_Num => Entity_Header_Num,
207      Element    => Boolean,
208      No_Element => False,
209      Key        => Entity_Id,
210      Hash       => Node_Hash,
211      Equal      => "=");
212   --  Hash table to record which subprograms are referenced. It is declared
213   --  at library level to avoid elaborating it for every call to Analyze.
214
215   package Traversed_Table is new GNAT.Htable.Simple_HTable
216     (Header_Num => Entity_Header_Num,
217      Element    => Boolean,
218      No_Element => False,
219      Key        => Node_Id,
220      Hash       => Node_Hash,
221      Equal      => "=");
222   --  Hash table to record which nodes we have traversed, so we can avoid
223   --  traversing the same nodes repeatedly.
224
225   -----------------
226   -- Node_Hash --
227   -----------------
228
229   function Node_Hash (Id : Entity_Id) return Entity_Header_Num is
230   begin
231      return Entity_Header_Num (Id mod Entity_Table_Size);
232   end Node_Hash;
233
234   ---------------------------------
235   -- Analyze_Package_Body_Helper --
236   ---------------------------------
237
238   --  WARNING: This routine manages Ghost regions. Return statements must be
239   --  replaced by gotos which jump to the end of the routine and restore the
240   --  Ghost mode.
241
242   procedure Analyze_Package_Body_Helper (N : Node_Id) is
243      procedure Hide_Public_Entities (Decls : List_Id);
244      --  Attempt to hide all public entities found in declarative list Decls
245      --  by resetting their Is_Public flag to False depending on whether the
246      --  entities are not referenced by inlined or generic bodies. This kind
247      --  of processing is a conservative approximation and will still leave
248      --  entities externally visible if the package is not simple enough.
249
250      procedure Install_Composite_Operations (P : Entity_Id);
251      --  Composite types declared in the current scope may depend on types
252      --  that were private at the point of declaration, and whose full view
253      --  is now in scope. Indicate that the corresponding operations on the
254      --  composite type are available.
255
256      --------------------------
257      -- Hide_Public_Entities --
258      --------------------------
259
260      procedure Hide_Public_Entities (Decls : List_Id) is
261         function Has_Referencer
262           (Decls                                   : List_Id;
263            In_Nested_Instance                      : Boolean;
264            Has_Outer_Referencer_Of_Non_Subprograms : Boolean) return Boolean;
265         --  A "referencer" is a construct which may reference a previous
266         --  declaration. Examine all declarations in list Decls in reverse
267         --  and determine whether one such referencer exists. All entities
268         --  in the range Last (Decls) .. Referencer are hidden from external
269         --  visibility.
270
271         function Scan_Subprogram_Ref (N : Node_Id) return Traverse_Result;
272         --  Determine whether a node denotes a reference to a subprogram
273
274         procedure Traverse_And_Scan_Subprogram_Refs is
275           new Traverse_Proc (Scan_Subprogram_Ref);
276         --  Subsidiary to routine Has_Referencer. Determine whether a node
277         --  contains references to a subprogram and record them.
278         --  WARNING: this is a very expensive routine as it performs a full
279         --  tree traversal.
280
281         procedure Scan_Subprogram_Refs (Node : Node_Id);
282         --  If we haven't already traversed Node, then mark it and traverse
283         --  it.
284
285         --------------------
286         -- Has_Referencer --
287         --------------------
288
289         function Has_Referencer
290           (Decls                                   : List_Id;
291            In_Nested_Instance                      : Boolean;
292            Has_Outer_Referencer_Of_Non_Subprograms : Boolean) return Boolean
293         is
294            Decl    : Node_Id;
295            Decl_Id : Entity_Id;
296            Spec    : Node_Id;
297
298            Has_Referencer_Of_Non_Subprograms : Boolean :=
299                                       Has_Outer_Referencer_Of_Non_Subprograms;
300            --  Set if an inlined subprogram body was detected as a referencer.
301            --  In this case, we do not return True immediately but keep hiding
302            --  subprograms from external visibility.
303
304         begin
305            if No (Decls) then
306               return False;
307            end if;
308
309            --  Examine all declarations in reverse order, hiding all entities
310            --  from external visibility until a referencer has been found. The
311            --  algorithm recurses into nested packages.
312
313            Decl := Last (Decls);
314            while Present (Decl) loop
315
316               --  A stub is always considered a referencer
317
318               if Nkind (Decl) in N_Body_Stub then
319                  return True;
320
321               --  Package declaration
322
323               elsif Nkind (Decl) = N_Package_Declaration then
324                  Spec := Specification (Decl);
325                  Decl_Id := Defining_Entity (Spec);
326
327                  --  Inspect the declarations of a non-generic package to try
328                  --  and hide more entities from external visibility.
329
330                  if not Is_Generic_Unit (Decl_Id) then
331                     if Has_Referencer (Private_Declarations (Spec),
332                                        In_Nested_Instance
333                                          or else
334                                        Is_Generic_Instance (Decl_Id),
335                                        Has_Referencer_Of_Non_Subprograms)
336                       or else
337                        Has_Referencer (Visible_Declarations (Spec),
338                                        In_Nested_Instance
339                                          or else
340                                        Is_Generic_Instance (Decl_Id),
341                                        Has_Referencer_Of_Non_Subprograms)
342                     then
343                        return True;
344                     end if;
345                  end if;
346
347               --  Package body
348
349               elsif Nkind (Decl) = N_Package_Body
350                 and then Present (Corresponding_Spec (Decl))
351               then
352                  Decl_Id := Corresponding_Spec (Decl);
353
354                  --  A generic package body is a referencer. It would seem
355                  --  that we only have to consider generics that can be
356                  --  exported, i.e. where the corresponding spec is the
357                  --  spec of the current package, but because of nested
358                  --  instantiations, a fully private generic body may export
359                  --  other private body entities. Furthermore, regardless of
360                  --  whether there was a previous inlined subprogram, (an
361                  --  instantiation of) the generic package may reference any
362                  --  entity declared before it.
363
364                  if Is_Generic_Unit (Decl_Id) then
365                     return True;
366
367                  --  Inspect the declarations of a non-generic package body to
368                  --  try and hide more entities from external visibility.
369
370                  elsif Has_Referencer (Declarations (Decl),
371                                        In_Nested_Instance
372                                          or else
373                                        Is_Generic_Instance (Decl_Id),
374                                        Has_Referencer_Of_Non_Subprograms)
375                  then
376                     return True;
377                  end if;
378
379               --  Subprogram body
380
381               elsif Nkind (Decl) = N_Subprogram_Body then
382                  if Present (Corresponding_Spec (Decl)) then
383                     Decl_Id := Corresponding_Spec (Decl);
384
385                     --  A generic subprogram body acts as a referencer
386
387                     if Is_Generic_Unit (Decl_Id) then
388                        return True;
389                     end if;
390
391                     --  An inlined subprogram body acts as a referencer
392
393                     --  Note that we test Has_Pragma_Inline here in addition
394                     --  to Is_Inlined. We are doing this for a client, since
395                     --  we are computing which entities should be public, and
396                     --  it is the client who will decide if actual inlining
397                     --  should occur, so we need to catch all cases where the
398                     --  subprogram may be inlined by the client.
399
400                     if Is_Inlined (Decl_Id)
401                       or else Has_Pragma_Inline (Decl_Id)
402                     then
403                        Has_Referencer_Of_Non_Subprograms := True;
404
405                        --  Inspect the statements of the subprogram body
406                        --  to determine whether the body references other
407                        --  subprograms.
408
409                        Scan_Subprogram_Refs (Decl);
410                     end if;
411
412                  --  Otherwise this is a stand alone subprogram body
413
414                  else
415                     Decl_Id := Defining_Entity (Decl);
416
417                     --  An inlined subprogram body acts as a referencer
418
419                     if Is_Inlined (Decl_Id)
420                       or else Has_Pragma_Inline (Decl_Id)
421                     then
422                        Has_Referencer_Of_Non_Subprograms := True;
423
424                        --  Inspect the statements of the subprogram body
425                        --  to determine whether the body references other
426                        --  subprograms.
427
428                        Scan_Subprogram_Refs (Decl);
429
430                     --  Otherwise we can reset Is_Public right away
431
432                     elsif not Subprogram_Table.Get (Decl_Id) then
433                        Set_Is_Public (Decl_Id, False);
434                     end if;
435                  end if;
436
437               --  Freeze node
438
439               elsif Nkind (Decl) = N_Freeze_Entity then
440                  declare
441                     Discard : Boolean;
442                     pragma Unreferenced (Discard);
443                  begin
444                     --  Inspect the actions to find references to subprograms.
445                     --  We assume that the actions do not contain other kinds
446                     --  of references and, therefore, we do not stop the scan
447                     --  or set Has_Referencer_Of_Non_Subprograms here. Doing
448                     --  it would pessimize common cases for which the actions
449                     --  contain the declaration of an init procedure, since
450                     --  such a procedure is automatically marked inline.
451
452                     Discard :=
453                       Has_Referencer (Actions (Decl),
454                                       In_Nested_Instance,
455                                       Has_Referencer_Of_Non_Subprograms);
456                  end;
457
458               --  Exceptions, objects and renamings do not need to be public
459               --  if they are not followed by a construct which can reference
460               --  and export them. Likewise for subprograms but we work harder
461               --  for them to see whether they are referenced on an individual
462               --  basis by looking into the table of referenced subprograms.
463               --  But we cannot say anything for entities declared in nested
464               --  instances because instantiations are not done yet so the
465               --  bodies are not visible and could contain references to them.
466               elsif Nkind_In (Decl, N_Exception_Declaration,
467                                     N_Object_Declaration,
468                                     N_Object_Renaming_Declaration,
469                                     N_Subprogram_Declaration,
470                                     N_Subprogram_Renaming_Declaration)
471               then
472                  Decl_Id := Defining_Entity (Decl);
473
474                  if not In_Nested_Instance
475                    and then not Is_Imported (Decl_Id)
476                    and then not Is_Exported (Decl_Id)
477                    and then No (Interface_Name (Decl_Id))
478                    and then
479                      ((Nkind (Decl) /= N_Subprogram_Declaration
480                         and then not Has_Referencer_Of_Non_Subprograms)
481                        or else (Nkind (Decl) = N_Subprogram_Declaration
482                                  and then not Subprogram_Table.Get (Decl_Id)))
483                  then
484                     Set_Is_Public (Decl_Id, False);
485                  end if;
486
487                  --  For a subprogram renaming, if the entity is referenced,
488                  --  then so is the renamed subprogram. But there is an issue
489                  --  with generic bodies because instantiations are not done
490                  --  yet and, therefore, cannot be scanned for referencers.
491                  --  That's why we use an approximation and test that we have
492                  --  at least one subprogram referenced by an inlined body
493                  --  instead of precisely the entity of this renaming.
494
495                  if Nkind (Decl) = N_Subprogram_Renaming_Declaration
496                    and then Subprogram_Table.Get_First
497                    and then Is_Entity_Name (Name (Decl))
498                    and then Present (Entity (Name (Decl)))
499                    and then Is_Subprogram (Entity (Name (Decl)))
500                  then
501                     Subprogram_Table.Set (Entity (Name (Decl)), True);
502                  end if;
503               end if;
504
505               Prev (Decl);
506            end loop;
507
508            return Has_Referencer_Of_Non_Subprograms;
509         end Has_Referencer;
510
511         -------------------------
512         -- Scan_Subprogram_Ref --
513         -------------------------
514
515         function Scan_Subprogram_Ref (N : Node_Id) return Traverse_Result is
516         begin
517            --  Detect a reference of the form
518            --    Subp_Call
519
520            if Nkind (N) in N_Subprogram_Call
521              and then Is_Entity_Name (Name (N))
522              and then Present (Entity (Name (N)))
523              and then Is_Subprogram (Entity (Name (N)))
524            then
525               Subprogram_Table.Set (Entity (Name (N)), True);
526
527            --  Detect a reference of the form
528            --    Subp'Some_Attribute
529
530            elsif Nkind (N) = N_Attribute_Reference
531              and then Is_Entity_Name (Prefix (N))
532              and then Present (Entity (Prefix (N)))
533              and then Is_Subprogram (Entity (Prefix (N)))
534            then
535               Subprogram_Table.Set (Entity (Prefix (N)), True);
536
537            --  Constants can be substituted by their value in gigi, which may
538            --  contain a reference, so scan the value recursively.
539
540            elsif Is_Entity_Name (N)
541              and then Present (Entity (N))
542              and then Ekind (Entity (N)) = E_Constant
543            then
544               declare
545                  Val : constant Node_Id := Constant_Value (Entity (N));
546               begin
547                  if Present (Val)
548                    and then not Compile_Time_Known_Value (Val)
549                  then
550                     Scan_Subprogram_Refs (Val);
551                  end if;
552               end;
553            end if;
554
555            return OK;
556         end Scan_Subprogram_Ref;
557
558         --------------------------
559         -- Scan_Subprogram_Refs --
560         --------------------------
561
562         procedure Scan_Subprogram_Refs (Node : Node_Id) is
563         begin
564            if not Traversed_Table.Get (Node) then
565               Traversed_Table.Set (Node, True);
566               Traverse_And_Scan_Subprogram_Refs (Node);
567            end if;
568         end Scan_Subprogram_Refs;
569
570         --  Local variables
571
572         Discard : Boolean;
573         pragma Unreferenced (Discard);
574
575      --  Start of processing for Hide_Public_Entities
576
577      begin
578         --  The algorithm examines the top level declarations of a package
579         --  body in reverse looking for a construct that may export entities
580         --  declared prior to it. If such a scenario is encountered, then all
581         --  entities in the range Last (Decls) .. construct are hidden from
582         --  external visibility. Consider:
583
584         --    package Pack is
585         --       generic
586         --       package Gen is
587         --       end Gen;
588         --    end Pack;
589
590         --    package body Pack is
591         --       External_Obj : ...;      --  (1)
592
593         --       package body Gen is      --  (2)
594         --          ... External_Obj ...  --  (3)
595         --       end Gen;
596
597         --       Local_Obj : ...;         --  (4)
598         --    end Pack;
599
600         --  In this example Local_Obj (4) must not be externally visible as
601         --  it cannot be exported by anything in Pack. The body of generic
602         --  package Gen (2) on the other hand acts as a "referencer" and may
603         --  export anything declared before it. Since the compiler does not
604         --  perform flow analysis, it is not possible to determine precisely
605         --  which entities will be exported when Gen is instantiated. In the
606         --  example above External_Obj (1) is exported at (3), but this may
607         --  not always be the case. The algorithm takes a conservative stance
608         --  and leaves entity External_Obj public.
609
610         --  This very conservative algorithm is supplemented by a more precise
611         --  processing for inlined bodies. For them, we traverse the syntactic
612         --  tree and record which subprograms are actually referenced from it.
613         --  This makes it possible to compute a much smaller set of externally
614         --  visible subprograms in the absence of generic bodies, which can
615         --  have a significant impact on the inlining decisions made in the
616         --  back end and the removal of out-of-line bodies from the object
617         --  code. We do it only for inlined bodies because they are supposed
618         --  to be reasonably small and tree traversal is very expensive.
619
620         --  Note that even this special processing is not optimal for inlined
621         --  bodies, because we treat all inlined subprograms alike. An optimal
622         --  algorithm would require computing the transitive closure of the
623         --  inlined subprograms that can really be referenced from other units
624         --  in the source code.
625
626         --  We could extend this processing for inlined bodies and record all
627         --  entities, not just subprograms, referenced from them, which would
628         --  make it possible to compute a much smaller set of all externally
629         --  visible entities in the absence of generic bodies. But this would
630         --  mean implementing a more thorough tree traversal of the bodies,
631         --  i.e. not just syntactic, and the gain would very likely be worth
632         --  neither the hassle nor the slowdown of the compiler.
633
634         --  Finally, an important thing to be aware of is that, at this point,
635         --  instantiations are not done yet so we cannot directly see inlined
636         --  bodies coming from them. That's not catastrophic because only the
637         --  actual parameters of the instantiations matter here, and they are
638         --  present in the declarations list of the instantiated packages.
639
640         Traversed_Table.Reset;
641         Subprogram_Table.Reset;
642         Discard := Has_Referencer (Decls, False, False);
643      end Hide_Public_Entities;
644
645      ----------------------------------
646      -- Install_Composite_Operations --
647      ----------------------------------
648
649      procedure Install_Composite_Operations (P : Entity_Id) is
650         Id : Entity_Id;
651
652      begin
653         Id := First_Entity (P);
654         while Present (Id) loop
655            if Is_Type (Id)
656              and then (Is_Limited_Composite (Id)
657                         or else Is_Private_Composite (Id))
658              and then No (Private_Component (Id))
659            then
660               Set_Is_Limited_Composite (Id, False);
661               Set_Is_Private_Composite (Id, False);
662            end if;
663
664            Next_Entity (Id);
665         end loop;
666      end Install_Composite_Operations;
667
668      --  Local variables
669
670      Saved_GM   : constant Ghost_Mode_Type := Ghost_Mode;
671      Saved_IGR  : constant Node_Id         := Ignored_Ghost_Region;
672      Saved_EA   : constant Boolean         := Expander_Active;
673      Saved_ISMP : constant Boolean         :=
674                     Ignore_SPARK_Mode_Pragmas_In_Instance;
675      --  Save the Ghost and SPARK mode-related data to restore on exit
676
677      Body_Id          : Entity_Id;
678      HSS              : Node_Id;
679      Last_Spec_Entity : Entity_Id;
680      New_N            : Node_Id;
681      Pack_Decl        : Node_Id;
682      Spec_Id          : Entity_Id;
683
684   --  Start of processing for Analyze_Package_Body_Helper
685
686   begin
687      --  Find corresponding package specification, and establish the current
688      --  scope. The visible defining entity for the package is the defining
689      --  occurrence in the spec. On exit from the package body, all body
690      --  declarations are attached to the defining entity for the body, but
691      --  the later is never used for name resolution. In this fashion there
692      --  is only one visible entity that denotes the package.
693
694      --  Set Body_Id. Note that this will be reset to point to the generic
695      --  copy later on in the generic case.
696
697      Body_Id := Defining_Entity (N);
698
699      --  Body is body of package instantiation. Corresponding spec has already
700      --  been set.
701
702      if Present (Corresponding_Spec (N)) then
703         Spec_Id   := Corresponding_Spec (N);
704         Pack_Decl := Unit_Declaration_Node (Spec_Id);
705
706      else
707         Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
708
709         if Present (Spec_Id)
710           and then Is_Package_Or_Generic_Package (Spec_Id)
711         then
712            Pack_Decl := Unit_Declaration_Node (Spec_Id);
713
714            if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
715               Error_Msg_N ("cannot supply body for package renaming", N);
716               return;
717
718            elsif Present (Corresponding_Body (Pack_Decl)) then
719               Error_Msg_N ("redefinition of package body", N);
720               return;
721            end if;
722
723         else
724            Error_Msg_N ("missing specification for package body", N);
725            return;
726         end if;
727
728         if Is_Package_Or_Generic_Package (Spec_Id)
729           and then (Scope (Spec_Id) = Standard_Standard
730                      or else Is_Child_Unit (Spec_Id))
731           and then not Unit_Requires_Body (Spec_Id)
732         then
733            if Ada_Version = Ada_83 then
734               Error_Msg_N
735                 ("optional package body (not allowed in Ada 95)??", N);
736            else
737               Error_Msg_N ("spec of this package does not allow a body", N);
738            end if;
739         end if;
740      end if;
741
742      --  A [generic] package body freezes the contract of the nearest
743      --  enclosing package body and all other contracts encountered in
744      --  the same declarative part up to and excluding the package body:
745
746      --    package body Nearest_Enclosing_Package
747      --      with Refined_State => (State => Constit)
748      --    is
749      --       Constit : ...;
750
751      --       package body Freezes_Enclosing_Package_Body
752      --         with Refined_State => (State_2 => Constit_2)
753      --       is
754      --          Constit_2 : ...;
755
756      --          procedure Proc
757      --            with Refined_Depends => (Input => (Constit, Constit_2)) ...
758
759      --  This ensures that any annotations referenced by the contract of a
760      --  [generic] subprogram body declared within the current package body
761      --  are available. This form of freezing is decoupled from the usual
762      --  Freeze_xxx mechanism because it must also work in the context of
763      --  generics where normal freezing is disabled.
764
765      --  Only bodies coming from source should cause this type of freezing.
766      --  Instantiated generic bodies are excluded because their processing is
767      --  performed in a separate compilation pass which lacks enough semantic
768      --  information with respect to contract analysis. It is safe to suppress
769      --  the freezing of contracts in this case because this action already
770      --  took place at the end of the enclosing declarative part.
771
772      if Comes_From_Source (N)
773        and then not Is_Generic_Instance (Spec_Id)
774      then
775         Freeze_Previous_Contracts (N);
776      end if;
777
778      --  A package body is Ghost when the corresponding spec is Ghost. Set
779      --  the mode now to ensure that any nodes generated during analysis and
780      --  expansion are properly flagged as ignored Ghost.
781
782      Mark_And_Set_Ghost_Body (N, Spec_Id);
783
784      --  Deactivate expansion inside the body of ignored Ghost entities,
785      --  as this code will ultimately be ignored. This avoids requiring the
786      --  presence of run-time units which are not needed. Only do this for
787      --  user entities, as internally generated entitities might still need
788      --  to be expanded (e.g. those generated for types).
789
790      if Present (Ignored_Ghost_Region)
791        and then Comes_From_Source (Body_Id)
792      then
793         Expander_Active := False;
794      end if;
795
796      --  If the body completes the initial declaration of a compilation unit
797      --  which is subject to pragma Elaboration_Checks, set the model of the
798      --  pragma because it applies to all parts of the unit.
799
800      Install_Elaboration_Model (Spec_Id);
801
802      Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
803      Style.Check_Identifier (Body_Id, Spec_Id);
804
805      if Is_Child_Unit (Spec_Id) then
806         if Nkind (Parent (N)) /= N_Compilation_Unit then
807            Error_Msg_NE
808              ("body of child unit& cannot be an inner package", N, Spec_Id);
809         end if;
810
811         Set_Is_Child_Unit (Body_Id);
812      end if;
813
814      --  Generic package case
815
816      if Ekind (Spec_Id) = E_Generic_Package then
817
818         --  Disable expansion and perform semantic analysis on copy. The
819         --  unannotated body will be used in all instantiations.
820
821         Body_Id := Defining_Entity (N);
822         Set_Ekind (Body_Id, E_Package_Body);
823         Set_Scope (Body_Id, Scope (Spec_Id));
824         Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
825         Set_Body_Entity (Spec_Id, Body_Id);
826         Set_Spec_Entity (Body_Id, Spec_Id);
827
828         New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
829         Rewrite (N, New_N);
830
831         --  Once the contents of the generic copy and the template are
832         --  swapped, do the same for their respective aspect specifications.
833
834         Exchange_Aspects (N, New_N);
835
836         --  Collect all contract-related source pragmas found within the
837         --  template and attach them to the contract of the package body.
838         --  This contract is used in the capture of global references within
839         --  annotations.
840
841         Create_Generic_Contract (N);
842
843         --  Update Body_Id to point to the copied node for the remainder of
844         --  the processing.
845
846         Body_Id := Defining_Entity (N);
847         Start_Generic;
848      end if;
849
850      --  The Body_Id is that of the copied node in the generic case, the
851      --  current node otherwise. Note that N was rewritten above, so we must
852      --  be sure to get the latest Body_Id value.
853
854      Set_Ekind (Body_Id, E_Package_Body);
855      Set_Body_Entity (Spec_Id, Body_Id);
856      Set_Spec_Entity (Body_Id, Spec_Id);
857
858      --  Defining name for the package body is not a visible entity: Only the
859      --  defining name for the declaration is visible.
860
861      Set_Etype (Body_Id, Standard_Void_Type);
862      Set_Scope (Body_Id, Scope (Spec_Id));
863      Set_Corresponding_Spec (N, Spec_Id);
864      Set_Corresponding_Body (Pack_Decl, Body_Id);
865
866      --  The body entity is not used for semantics or code generation, but
867      --  it is attached to the entity list of the enclosing scope to simplify
868      --  the listing of back-annotations for the types it main contain.
869
870      if Scope (Spec_Id) /= Standard_Standard then
871         Append_Entity (Body_Id, Scope (Spec_Id));
872      end if;
873
874      --  Indicate that we are currently compiling the body of the package
875
876      Set_In_Package_Body (Spec_Id);
877      Set_Has_Completion (Spec_Id);
878      Last_Spec_Entity := Last_Entity (Spec_Id);
879
880      if Has_Aspects (N) then
881         Analyze_Aspect_Specifications (N, Body_Id);
882      end if;
883
884      Push_Scope (Spec_Id);
885
886      --  Set SPARK_Mode only for non-generic package
887
888      if Ekind (Spec_Id) = E_Package then
889         Set_SPARK_Pragma               (Body_Id, SPARK_Mode_Pragma);
890         Set_SPARK_Aux_Pragma           (Body_Id, SPARK_Mode_Pragma);
891         Set_SPARK_Pragma_Inherited     (Body_Id);
892         Set_SPARK_Aux_Pragma_Inherited (Body_Id);
893
894         --  A package body may be instantiated or inlined at a later pass.
895         --  Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when
896         --  it applied to the package spec.
897
898         if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
899            Ignore_SPARK_Mode_Pragmas_In_Instance := True;
900         end if;
901      end if;
902
903      Set_Categorization_From_Pragmas (N);
904
905      Install_Visible_Declarations (Spec_Id);
906      Install_Private_Declarations (Spec_Id);
907      Install_Private_With_Clauses (Spec_Id);
908      Install_Composite_Operations (Spec_Id);
909
910      Check_Anonymous_Access_Types (Spec_Id, N);
911
912      if Ekind (Spec_Id) = E_Generic_Package then
913         Set_Use (Generic_Formal_Declarations (Pack_Decl));
914      end if;
915
916      Set_Use (Visible_Declarations (Specification (Pack_Decl)));
917      Set_Use (Private_Declarations (Specification (Pack_Decl)));
918
919      --  This is a nested package, so it may be necessary to declare certain
920      --  inherited subprograms that are not yet visible because the parent
921      --  type's subprograms are now visible.
922
923      if Ekind (Scope (Spec_Id)) = E_Package
924        and then Scope (Spec_Id) /= Standard_Standard
925      then
926         Declare_Inherited_Private_Subprograms (Spec_Id);
927      end if;
928
929      if Present (Declarations (N)) then
930         Analyze_Declarations (Declarations (N));
931         Inspect_Deferred_Constant_Completion (Declarations (N));
932      end if;
933
934      --  Verify that the SPARK_Mode of the body agrees with that of its spec
935
936      if Present (SPARK_Pragma (Body_Id)) then
937         if Present (SPARK_Aux_Pragma (Spec_Id)) then
938            if Get_SPARK_Mode_From_Annotation (SPARK_Aux_Pragma (Spec_Id)) =
939                 Off
940              and then
941                Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
942            then
943               Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
944               Error_Msg_N ("incorrect application of SPARK_Mode#", N);
945               Error_Msg_Sloc := Sloc (SPARK_Aux_Pragma (Spec_Id));
946               Error_Msg_NE
947                 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
948            end if;
949
950         else
951            Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
952            Error_Msg_N ("incorrect application of SPARK_Mode#", N);
953            Error_Msg_Sloc := Sloc (Spec_Id);
954            Error_Msg_NE
955              ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
956         end if;
957      end if;
958
959      --  Analyze_Declarations has caused freezing of all types. Now generate
960      --  bodies for RACW primitives and stream attributes, if any.
961
962      if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then
963
964         --  Attach subprogram bodies to support RACWs declared in spec
965
966         Append_RACW_Bodies (Declarations (N), Spec_Id);
967         Analyze_List (Declarations (N));
968      end if;
969
970      HSS := Handled_Statement_Sequence (N);
971
972      if Present (HSS) then
973         Process_End_Label (HSS, 't', Spec_Id);
974         Analyze (HSS);
975
976         --  Check that elaboration code in a preelaborable package body is
977         --  empty other than null statements and labels (RM 10.2.1(6)).
978
979         Validate_Null_Statement_Sequence (N);
980      end if;
981
982      Validate_Categorization_Dependency (N, Spec_Id);
983      Check_Completion (Body_Id);
984
985      --  Generate start of body reference. Note that we do this fairly late,
986      --  because the call will use In_Extended_Main_Source_Unit as a check,
987      --  and we want to make sure that Corresponding_Stub links are set
988
989      Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
990
991      --  For a generic package, collect global references and mark them on
992      --  the original body so that they are not resolved again at the point
993      --  of instantiation.
994
995      if Ekind (Spec_Id) /= E_Package then
996         Save_Global_References (Original_Node (N));
997         End_Generic;
998      end if;
999
1000      --  The entities of the package body have so far been chained onto the
1001      --  declaration chain for the spec. That's been fine while we were in the
1002      --  body, since we wanted them to be visible, but now that we are leaving
1003      --  the package body, they are no longer visible, so we remove them from
1004      --  the entity chain of the package spec entity, and copy them to the
1005      --  entity chain of the package body entity, where they will never again
1006      --  be visible.
1007
1008      if Present (Last_Spec_Entity) then
1009         Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
1010         Set_Next_Entity (Last_Spec_Entity, Empty);
1011         Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1012         Set_Last_Entity (Spec_Id, Last_Spec_Entity);
1013
1014      else
1015         Set_First_Entity (Body_Id, First_Entity (Spec_Id));
1016         Set_Last_Entity  (Body_Id, Last_Entity  (Spec_Id));
1017         Set_First_Entity (Spec_Id, Empty);
1018         Set_Last_Entity  (Spec_Id, Empty);
1019      end if;
1020
1021      Update_Use_Clause_Chain;
1022      End_Package_Scope (Spec_Id);
1023
1024      --  All entities declared in body are not visible
1025
1026      declare
1027         E : Entity_Id;
1028
1029      begin
1030         E := First_Entity (Body_Id);
1031         while Present (E) loop
1032            Set_Is_Immediately_Visible (E, False);
1033            Set_Is_Potentially_Use_Visible (E, False);
1034            Set_Is_Hidden (E);
1035
1036            --  Child units may appear on the entity list (e.g. if they appear
1037            --  in the context of a subunit) but they are not body entities.
1038
1039            if not Is_Child_Unit (E) then
1040               Set_Is_Package_Body_Entity (E);
1041            end if;
1042
1043            Next_Entity (E);
1044         end loop;
1045      end;
1046
1047      Check_References (Body_Id);
1048
1049      --  For a generic unit, check that the formal parameters are referenced,
1050      --  and that local variables are used, as for regular packages.
1051
1052      if Ekind (Spec_Id) = E_Generic_Package then
1053         Check_References (Spec_Id);
1054      end if;
1055
1056      --  At this point all entities of the package body are externally visible
1057      --  to the linker as their Is_Public flag is set to True. This proactive
1058      --  approach is necessary because an inlined or a generic body for which
1059      --  code is generated in other units may need to see these entities. Cut
1060      --  down the number of global symbols that do not neet public visibility
1061      --  as this has two beneficial effects:
1062      --    (1) It makes the compilation process more efficient.
1063      --    (2) It gives the code generator more leeway to optimize within each
1064      --        unit, especially subprograms.
1065
1066      --  This is done only for top-level library packages or child units as
1067      --  the algorithm does a top-down traversal of the package body.
1068
1069      if (Scope (Spec_Id) = Standard_Standard or else Is_Child_Unit (Spec_Id))
1070        and then not Is_Generic_Unit (Spec_Id)
1071      then
1072         Hide_Public_Entities (Declarations (N));
1073      end if;
1074
1075      --  If expander is not active, then here is where we turn off the
1076      --  In_Package_Body flag, otherwise it is turned off at the end of the
1077      --  corresponding expansion routine. If this is an instance body, we need
1078      --  to qualify names of local entities, because the body may have been
1079      --  compiled as a preliminary to another instantiation.
1080
1081      if not Expander_Active then
1082         Set_In_Package_Body (Spec_Id, False);
1083
1084         if Is_Generic_Instance (Spec_Id)
1085           and then Operating_Mode = Generate_Code
1086         then
1087            Qualify_Entity_Names (N);
1088         end if;
1089      end if;
1090
1091      if Present (Ignored_Ghost_Region) then
1092         Expander_Active := Saved_EA;
1093      end if;
1094
1095      Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
1096      Restore_Ghost_Region (Saved_GM, Saved_IGR);
1097   end Analyze_Package_Body_Helper;
1098
1099   ---------------------------------
1100   -- Analyze_Package_Declaration --
1101   ---------------------------------
1102
1103   procedure Analyze_Package_Declaration (N : Node_Id) is
1104      Id  : constant Node_Id := Defining_Entity (N);
1105
1106      Is_Comp_Unit : constant Boolean :=
1107                       Nkind (Parent (N)) = N_Compilation_Unit;
1108
1109      Body_Required : Boolean;
1110      --  True when this package declaration requires a corresponding body
1111
1112   begin
1113      if Debug_Flag_C then
1114         Write_Str ("==> package spec ");
1115         Write_Name (Chars (Id));
1116         Write_Str (" from ");
1117         Write_Location (Sloc (N));
1118         Write_Eol;
1119         Indent;
1120      end if;
1121
1122      Generate_Definition (Id);
1123      Enter_Name (Id);
1124      Set_Ekind  (Id, E_Package);
1125      Set_Etype  (Id, Standard_Void_Type);
1126
1127      --  Set SPARK_Mode from context
1128
1129      Set_SPARK_Pragma               (Id, SPARK_Mode_Pragma);
1130      Set_SPARK_Aux_Pragma           (Id, SPARK_Mode_Pragma);
1131      Set_SPARK_Pragma_Inherited     (Id);
1132      Set_SPARK_Aux_Pragma_Inherited (Id);
1133
1134      --  Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
1135      --  the body of this package is instantiated or inlined later and out of
1136      --  context. The body uses this attribute to restore the value of the
1137      --  global flag.
1138
1139      if Ignore_SPARK_Mode_Pragmas_In_Instance then
1140         Set_Ignore_SPARK_Mode_Pragmas (Id);
1141      end if;
1142
1143      --  Analyze aspect specifications immediately, since we need to recognize
1144      --  things like Pure early enough to diagnose violations during analysis.
1145
1146      if Has_Aspects (N) then
1147         Analyze_Aspect_Specifications (N, Id);
1148      end if;
1149
1150      --  Ada 2005 (AI-217): Check if the package has been illegally named in
1151      --  a limited-with clause of its own context. In this case the error has
1152      --  been previously notified by Analyze_Context.
1153
1154      --     limited with Pkg; -- ERROR
1155      --     package Pkg is ...
1156
1157      if From_Limited_With (Id) then
1158         return;
1159      end if;
1160
1161      Push_Scope (Id);
1162
1163      Set_Is_Pure (Id, Is_Pure (Enclosing_Lib_Unit_Entity));
1164      Set_Categorization_From_Pragmas (N);
1165
1166      Analyze (Specification (N));
1167      Validate_Categorization_Dependency (N, Id);
1168
1169      --  Determine whether the package requires a body. Abstract states are
1170      --  intentionally ignored because they do require refinement which can
1171      --  only come in a body, but at the same time they do not force the need
1172      --  for a body on their own (SPARK RM 7.1.4(4) and 7.2.2(3)).
1173
1174      Body_Required := Unit_Requires_Body (Id);
1175
1176      if not Body_Required then
1177
1178         --  If the package spec does not require an explicit body, then there
1179         --  are not entities requiring completion in the language sense. Call
1180         --  Check_Completion now to ensure that nested package declarations
1181         --  that require an implicit body get one. (In the case where a body
1182         --  is required, Check_Completion is called at the end of the body's
1183         --  declarative part.)
1184
1185         Check_Completion;
1186
1187         --  If the package spec does not require an explicit body, then all
1188         --  abstract states declared in nested packages cannot possibly get
1189         --  a proper refinement (SPARK RM 7.2.2(3)). This check is performed
1190         --  only when the compilation unit is the main unit to allow for
1191         --  modular SPARK analysis where packages do not necessarily have
1192         --  bodies.
1193
1194         if Is_Comp_Unit then
1195            Check_State_Refinements
1196              (Context      => N,
1197               Is_Main_Unit => Parent (N) = Cunit (Main_Unit));
1198         end if;
1199      end if;
1200
1201      --  Set Body_Required indication on the compilation unit node
1202
1203      if Is_Comp_Unit then
1204         Set_Body_Required (Parent (N), Body_Required);
1205
1206         if Legacy_Elaboration_Checks and not Body_Required then
1207            Set_Suppress_Elaboration_Warnings (Id);
1208         end if;
1209      end if;
1210
1211      End_Package_Scope (Id);
1212
1213      --  For the declaration of a library unit that is a remote types package,
1214      --  check legality rules regarding availability of stream attributes for
1215      --  types that contain non-remote access values. This subprogram performs
1216      --  visibility tests that rely on the fact that we have exited the scope
1217      --  of Id.
1218
1219      if Is_Comp_Unit then
1220         Validate_RT_RAT_Component (N);
1221      end if;
1222
1223      if Debug_Flag_C then
1224         Outdent;
1225         Write_Str ("<== package spec ");
1226         Write_Name (Chars (Id));
1227         Write_Str (" from ");
1228         Write_Location (Sloc (N));
1229         Write_Eol;
1230      end if;
1231   end Analyze_Package_Declaration;
1232
1233   -----------------------------------
1234   -- Analyze_Package_Specification --
1235   -----------------------------------
1236
1237   --  Note that this code is shared for the analysis of generic package specs
1238   --  (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
1239
1240   procedure Analyze_Package_Specification (N : Node_Id) is
1241      Id           : constant Entity_Id  := Defining_Entity (N);
1242      Orig_Decl    : constant Node_Id    := Original_Node (Parent (N));
1243      Vis_Decls    : constant List_Id    := Visible_Declarations (N);
1244      Priv_Decls   : constant List_Id    := Private_Declarations (N);
1245      E            : Entity_Id;
1246      L            : Entity_Id;
1247      Public_Child : Boolean;
1248
1249      Private_With_Clauses_Installed : Boolean := False;
1250      --  In Ada 2005, private with_clauses are visible in the private part
1251      --  of a nested package, even if it appears in the public part of the
1252      --  enclosing package. This requires a separate step to install these
1253      --  private_with_clauses, and remove them at the end of the nested
1254      --  package.
1255
1256      procedure Check_One_Tagged_Type_Or_Extension_At_Most;
1257      --  Issue an error in SPARK mode if a package specification contains
1258      --  more than one tagged type or type extension.
1259
1260      procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
1261      --  Clears constant indications (Never_Set_In_Source, Constant_Value, and
1262      --  Is_True_Constant) on all variables that are entities of Id, and on
1263      --  the chain whose first element is FE. A recursive call is made for all
1264      --  packages and generic packages.
1265
1266      procedure Generate_Parent_References;
1267      --  For a child unit, generate references to parent units, for
1268      --  GPS navigation purposes.
1269
1270      function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
1271      --  Child and Unit are entities of compilation units. True if Child
1272      --  is a public child of Parent as defined in 10.1.1
1273
1274      procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
1275      --  Reject completion of an incomplete or private type declarations
1276      --  having a known discriminant part by an unchecked union.
1277
1278      procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
1279      --  Given the package entity of a generic package instantiation or
1280      --  formal package whose corresponding generic is a child unit, installs
1281      --  the private declarations of each of the child unit's parents.
1282      --  This has to be done at the point of entering the instance package's
1283      --  private part rather than being done in Sem_Ch12.Install_Parent
1284      --  (which is where the parents' visible declarations are installed).
1285
1286      ------------------------------------------------
1287      -- Check_One_Tagged_Type_Or_Extension_At_Most --
1288      ------------------------------------------------
1289
1290      procedure Check_One_Tagged_Type_Or_Extension_At_Most is
1291         Previous : Node_Id;
1292
1293         procedure Check_Decls (Decls : List_Id);
1294         --  Check that either Previous is Empty and Decls does not contain
1295         --  more than one tagged type or type extension, or Previous is
1296         --  already set and Decls contains no tagged type or type extension.
1297
1298         -----------------
1299         -- Check_Decls --
1300         -----------------
1301
1302         procedure Check_Decls (Decls : List_Id) is
1303            Decl : Node_Id;
1304
1305         begin
1306            Decl := First (Decls);
1307            while Present (Decl) loop
1308               if Nkind (Decl) = N_Full_Type_Declaration
1309                 and then Is_Tagged_Type (Defining_Identifier (Decl))
1310               then
1311                  if No (Previous) then
1312                     Previous := Decl;
1313
1314                  else
1315                     Error_Msg_Sloc := Sloc (Previous);
1316                     Check_SPARK_05_Restriction
1317                       ("at most one tagged type or type extension allowed",
1318                        "\\ previous declaration#",
1319                        Decl);
1320                  end if;
1321               end if;
1322
1323               Next (Decl);
1324            end loop;
1325         end Check_Decls;
1326
1327      --  Start of processing for Check_One_Tagged_Type_Or_Extension_At_Most
1328
1329      begin
1330         Previous := Empty;
1331         Check_Decls (Vis_Decls);
1332
1333         if Present (Priv_Decls) then
1334            Check_Decls (Priv_Decls);
1335         end if;
1336      end Check_One_Tagged_Type_Or_Extension_At_Most;
1337
1338      ---------------------
1339      -- Clear_Constants --
1340      ---------------------
1341
1342      procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
1343         E : Entity_Id;
1344
1345      begin
1346         --  Ignore package renamings, not interesting and they can cause self
1347         --  referential loops in the code below.
1348
1349         if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
1350            return;
1351         end if;
1352
1353         --  Note: in the loop below, the check for Next_Entity pointing back
1354         --  to the package entity may seem odd, but it is needed, because a
1355         --  package can contain a renaming declaration to itself, and such
1356         --  renamings are generated automatically within package instances.
1357
1358         E := FE;
1359         while Present (E) and then E /= Id loop
1360            if Is_Assignable (E) then
1361               Set_Never_Set_In_Source (E, False);
1362               Set_Is_True_Constant    (E, False);
1363               Set_Current_Value       (E, Empty);
1364               Set_Is_Known_Null       (E, False);
1365               Set_Last_Assignment     (E, Empty);
1366
1367               if not Can_Never_Be_Null (E) then
1368                  Set_Is_Known_Non_Null (E, False);
1369               end if;
1370
1371            elsif Is_Package_Or_Generic_Package (E) then
1372               Clear_Constants (E, First_Entity (E));
1373               Clear_Constants (E, First_Private_Entity (E));
1374            end if;
1375
1376            Next_Entity (E);
1377         end loop;
1378      end Clear_Constants;
1379
1380      --------------------------------
1381      -- Generate_Parent_References --
1382      --------------------------------
1383
1384      procedure Generate_Parent_References is
1385         Decl : constant Node_Id := Parent (N);
1386
1387      begin
1388         if Id = Cunit_Entity (Main_Unit)
1389           or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
1390         then
1391            Generate_Reference (Id, Scope (Id), 'k', False);
1392
1393         elsif not Nkind_In (Unit (Cunit (Main_Unit)), N_Subprogram_Body,
1394                                                       N_Subunit)
1395         then
1396            --  If current unit is an ancestor of main unit, generate a
1397            --  reference to its own parent.
1398
1399            declare
1400               U         : Node_Id;
1401               Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
1402
1403            begin
1404               if Nkind (Main_Spec) = N_Package_Body then
1405                  Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
1406               end if;
1407
1408               U := Parent_Spec (Main_Spec);
1409               while Present (U) loop
1410                  if U = Parent (Decl) then
1411                     Generate_Reference (Id, Scope (Id), 'k',  False);
1412                     exit;
1413
1414                  elsif Nkind (Unit (U)) = N_Package_Body then
1415                     exit;
1416
1417                  else
1418                     U := Parent_Spec (Unit (U));
1419                  end if;
1420               end loop;
1421            end;
1422         end if;
1423      end Generate_Parent_References;
1424
1425      ---------------------
1426      -- Is_Public_Child --
1427      ---------------------
1428
1429      function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
1430      begin
1431         if not Is_Private_Descendant (Child) then
1432            return True;
1433         else
1434            if Child = Unit then
1435               return not Private_Present (
1436                 Parent (Unit_Declaration_Node (Child)));
1437            else
1438               return Is_Public_Child (Scope (Child), Unit);
1439            end if;
1440         end if;
1441      end Is_Public_Child;
1442
1443      ----------------------------------------
1444      -- Inspect_Unchecked_Union_Completion --
1445      ----------------------------------------
1446
1447      procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
1448         Decl : Node_Id;
1449
1450      begin
1451         Decl := First (Decls);
1452         while Present (Decl) loop
1453
1454            --  We are looking at an incomplete or private type declaration
1455            --  with a known_discriminant_part whose full view is an
1456            --  Unchecked_Union. The seemingly useless check with Is_Type
1457            --  prevents cascaded errors when routines defined only for type
1458            --  entities are called with non-type entities.
1459
1460            if Nkind_In (Decl, N_Incomplete_Type_Declaration,
1461                               N_Private_Type_Declaration)
1462              and then Is_Type (Defining_Identifier (Decl))
1463              and then Has_Discriminants (Defining_Identifier (Decl))
1464              and then Present (Full_View (Defining_Identifier (Decl)))
1465              and then
1466                Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
1467            then
1468               Error_Msg_N
1469                 ("completion of discriminated partial view "
1470                  & "cannot be an unchecked union",
1471                 Full_View (Defining_Identifier (Decl)));
1472            end if;
1473
1474            Next (Decl);
1475         end loop;
1476      end Inspect_Unchecked_Union_Completion;
1477
1478      -----------------------------------------
1479      -- Install_Parent_Private_Declarations --
1480      -----------------------------------------
1481
1482      procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
1483         Inst_Par  : Entity_Id;
1484         Gen_Par   : Entity_Id;
1485         Inst_Node : Node_Id;
1486
1487      begin
1488         Inst_Par := Inst_Id;
1489
1490         Gen_Par :=
1491           Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
1492         while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
1493            Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
1494
1495            if Nkind_In (Inst_Node, N_Package_Instantiation,
1496                                    N_Formal_Package_Declaration)
1497              and then Nkind (Name (Inst_Node)) = N_Expanded_Name
1498            then
1499               Inst_Par := Entity (Prefix (Name (Inst_Node)));
1500
1501               if Present (Renamed_Entity (Inst_Par)) then
1502                  Inst_Par := Renamed_Entity (Inst_Par);
1503               end if;
1504
1505               Gen_Par :=
1506                 Generic_Parent
1507                   (Specification (Unit_Declaration_Node (Inst_Par)));
1508
1509               --  Install the private declarations and private use clauses
1510               --  of a parent instance of the child instance, unless the
1511               --  parent instance private declarations have already been
1512               --  installed earlier in Analyze_Package_Specification, which
1513               --  happens when a generic child is instantiated, and the
1514               --  instance is a child of the parent instance.
1515
1516               --  Installing the use clauses of the parent instance twice
1517               --  is both unnecessary and wrong, because it would cause the
1518               --  clauses to be chained to themselves in the use clauses
1519               --  list of the scope stack entry. That in turn would cause
1520               --  an endless loop from End_Use_Clauses upon scope exit.
1521
1522               --  The parent is now fully visible. It may be a hidden open
1523               --  scope if we are currently compiling some child instance
1524               --  declared within it, but while the current instance is being
1525               --  compiled the parent is immediately visible. In particular
1526               --  its entities must remain visible if a stack save/restore
1527               --  takes place through a call to Rtsfind.
1528
1529               if Present (Gen_Par) then
1530                  if not In_Private_Part (Inst_Par) then
1531                     Install_Private_Declarations (Inst_Par);
1532                     Set_Use (Private_Declarations
1533                                (Specification
1534                                   (Unit_Declaration_Node (Inst_Par))));
1535                     Set_Is_Hidden_Open_Scope (Inst_Par, False);
1536                  end if;
1537
1538               --  If we've reached the end of the generic instance parents,
1539               --  then finish off by looping through the nongeneric parents
1540               --  and installing their private declarations.
1541
1542               --  If one of the non-generic parents is itself on the scope
1543               --  stack, do not install its private declarations: they are
1544               --  installed in due time when the private part of that parent
1545               --  is analyzed.
1546
1547               else
1548                  while Present (Inst_Par)
1549                    and then Inst_Par /= Standard_Standard
1550                    and then (not In_Open_Scopes (Inst_Par)
1551                               or else not In_Private_Part (Inst_Par))
1552                  loop
1553                     if Nkind (Inst_Node) = N_Formal_Package_Declaration
1554                       or else
1555                         not Is_Ancestor_Package
1556                               (Inst_Par, Cunit_Entity (Current_Sem_Unit))
1557                     then
1558                        Install_Private_Declarations (Inst_Par);
1559                        Set_Use
1560                          (Private_Declarations
1561                            (Specification
1562                              (Unit_Declaration_Node (Inst_Par))));
1563                        Inst_Par := Scope (Inst_Par);
1564                     else
1565                        exit;
1566                     end if;
1567                  end loop;
1568
1569                  exit;
1570               end if;
1571
1572            else
1573               exit;
1574            end if;
1575         end loop;
1576      end Install_Parent_Private_Declarations;
1577
1578   --  Start of processing for Analyze_Package_Specification
1579
1580   begin
1581      if Present (Vis_Decls) then
1582         Analyze_Declarations (Vis_Decls);
1583      end if;
1584
1585      --  Inspect the entities defined in the package and ensure that all
1586      --  incomplete types have received full declarations. Build default
1587      --  initial condition and invariant procedures for all qualifying types.
1588
1589      E := First_Entity (Id);
1590      while Present (E) loop
1591
1592         --  Check on incomplete types
1593
1594         --  AI05-0213: A formal incomplete type has no completion, and neither
1595         --  does the corresponding subtype in an instance.
1596
1597         if Is_Incomplete_Type (E)
1598           and then No (Full_View (E))
1599           and then not Is_Generic_Type (E)
1600           and then not From_Limited_With (E)
1601           and then not Is_Generic_Actual_Type (E)
1602         then
1603            Error_Msg_N ("no declaration in visible part for incomplete}", E);
1604         end if;
1605
1606         Next_Entity (E);
1607      end loop;
1608
1609      if Is_Remote_Call_Interface (Id)
1610        and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1611      then
1612         Validate_RCI_Declarations (Id);
1613      end if;
1614
1615      --  Save global references in the visible declarations, before installing
1616      --  private declarations of parent unit if there is one, because the
1617      --  privacy status of types defined in the parent will change. This is
1618      --  only relevant for generic child units, but is done in all cases for
1619      --  uniformity.
1620
1621      if Ekind (Id) = E_Generic_Package
1622        and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1623      then
1624         declare
1625            Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1626            Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1627
1628         begin
1629            --  Insert the freezing nodes after the visible declarations to
1630            --  ensure that we analyze its aspects; needed to ensure that
1631            --  global entities referenced in the aspects are properly handled.
1632
1633            if Ada_Version >= Ada_2012
1634              and then Is_Non_Empty_List (Vis_Decls)
1635              and then Is_Empty_List (Priv_Decls)
1636            then
1637               Insert_List_After_And_Analyze
1638                 (Last (Vis_Decls), Freeze_Entity (Id, Last (Vis_Decls)));
1639            end if;
1640
1641            Set_Private_Declarations (Orig_Spec, Empty_List);
1642            Save_Global_References   (Orig_Decl);
1643            Set_Private_Declarations (Orig_Spec, Save_Priv);
1644         end;
1645      end if;
1646
1647      --  If package is a public child unit, then make the private declarations
1648      --  of the parent visible.
1649
1650      Public_Child := False;
1651
1652      declare
1653         Par       : Entity_Id;
1654         Pack_Decl : Node_Id;
1655         Par_Spec  : Node_Id;
1656
1657      begin
1658         Par := Id;
1659         Par_Spec := Parent_Spec (Parent (N));
1660
1661         --  If the package is formal package of an enclosing generic, it is
1662         --  transformed into a local generic declaration, and compiled to make
1663         --  its spec available. We need to retrieve the original generic to
1664         --  determine whether it is a child unit, and install its parents.
1665
1666         if No (Par_Spec)
1667           and then
1668             Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1669         then
1670            Par := Entity (Name (Original_Node (Parent (N))));
1671            Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1672         end if;
1673
1674         if Present (Par_Spec) then
1675            Generate_Parent_References;
1676
1677            while Scope (Par) /= Standard_Standard
1678              and then Is_Public_Child (Id, Par)
1679              and then In_Open_Scopes (Par)
1680            loop
1681               Public_Child := True;
1682               Par := Scope (Par);
1683               Install_Private_Declarations (Par);
1684               Install_Private_With_Clauses (Par);
1685               Pack_Decl := Unit_Declaration_Node (Par);
1686               Set_Use (Private_Declarations (Specification (Pack_Decl)));
1687            end loop;
1688         end if;
1689      end;
1690
1691      if Is_Compilation_Unit (Id) then
1692         Install_Private_With_Clauses (Id);
1693      else
1694         --  The current compilation unit may include private with_clauses,
1695         --  which are visible in the private part of the current nested
1696         --  package, and have to be installed now. This is not done for
1697         --  nested instantiations, where the private with_clauses of the
1698         --  enclosing unit have no effect once the instantiation info is
1699         --  established and we start analyzing the package declaration.
1700
1701         declare
1702            Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1703         begin
1704            if Is_Package_Or_Generic_Package (Comp_Unit)
1705              and then not In_Private_Part (Comp_Unit)
1706              and then not In_Instance
1707            then
1708               Install_Private_With_Clauses (Comp_Unit);
1709               Private_With_Clauses_Installed := True;
1710            end if;
1711         end;
1712      end if;
1713
1714      --  If this is a package associated with a generic instance or formal
1715      --  package, then the private declarations of each of the generic's
1716      --  parents must be installed at this point.
1717
1718      if Is_Generic_Instance (Id) then
1719         Install_Parent_Private_Declarations (Id);
1720      end if;
1721
1722      --  Analyze private part if present. The flag In_Private_Part is reset
1723      --  in End_Package_Scope.
1724
1725      L := Last_Entity (Id);
1726
1727      if Present (Priv_Decls) then
1728         Set_In_Private_Part (Id);
1729
1730         --  Upon entering a public child's private part, it may be necessary
1731         --  to declare subprograms that were derived in the package's visible
1732         --  part but not yet made visible.
1733
1734         if Public_Child then
1735            Declare_Inherited_Private_Subprograms (Id);
1736         end if;
1737
1738         Analyze_Declarations (Priv_Decls);
1739
1740         --  Check the private declarations for incomplete deferred constants
1741
1742         Inspect_Deferred_Constant_Completion (Priv_Decls);
1743
1744         --  The first private entity is the immediate follower of the last
1745         --  visible entity, if there was one.
1746
1747         if Present (L) then
1748            Set_First_Private_Entity (Id, Next_Entity (L));
1749         else
1750            Set_First_Private_Entity (Id, First_Entity (Id));
1751         end if;
1752
1753      --  There may be inherited private subprograms that need to be declared,
1754      --  even in the absence of an explicit private part.  If there are any
1755      --  public declarations in the package and the package is a public child
1756      --  unit, then an implicit private part is assumed.
1757
1758      elsif Present (L) and then Public_Child then
1759         Set_In_Private_Part (Id);
1760         Declare_Inherited_Private_Subprograms (Id);
1761         Set_First_Private_Entity (Id, Next_Entity (L));
1762      end if;
1763
1764      E := First_Entity (Id);
1765      while Present (E) loop
1766
1767         --  Check rule of 3.6(11), which in general requires waiting till all
1768         --  full types have been seen.
1769
1770         if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1771            Check_Aliased_Component_Types (E);
1772         end if;
1773
1774         --  Check preelaborable initialization for full type completing a
1775         --  private type for which pragma Preelaborable_Initialization given.
1776
1777         if Is_Type (E)
1778           and then Must_Have_Preelab_Init (E)
1779           and then not Has_Preelaborable_Initialization (E)
1780         then
1781            Error_Msg_N
1782              ("full view of & does not have preelaborable initialization", E);
1783         end if;
1784
1785         Next_Entity (E);
1786      end loop;
1787
1788      --  Ada 2005 (AI-216): The completion of an incomplete or private type
1789      --  declaration having a known_discriminant_part shall not be an
1790      --  unchecked union type.
1791
1792      if Present (Vis_Decls) then
1793         Inspect_Unchecked_Union_Completion (Vis_Decls);
1794      end if;
1795
1796      if Present (Priv_Decls) then
1797         Inspect_Unchecked_Union_Completion (Priv_Decls);
1798      end if;
1799
1800      if Ekind (Id) = E_Generic_Package
1801        and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1802        and then Present (Priv_Decls)
1803      then
1804         --  Save global references in private declarations, ignoring the
1805         --  visible declarations that were processed earlier.
1806
1807         declare
1808            Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1809            Save_Vis  : constant List_Id := Visible_Declarations (Orig_Spec);
1810            Save_Form : constant List_Id :=
1811                          Generic_Formal_Declarations (Orig_Decl);
1812
1813         begin
1814            --  Insert the freezing nodes after the private declarations to
1815            --  ensure that we analyze its aspects; needed to ensure that
1816            --  global entities referenced in the aspects are properly handled.
1817
1818            if Ada_Version >= Ada_2012
1819              and then Is_Non_Empty_List (Priv_Decls)
1820            then
1821               Insert_List_After_And_Analyze
1822                 (Last (Priv_Decls), Freeze_Entity (Id, Last (Priv_Decls)));
1823            end if;
1824
1825            Set_Visible_Declarations        (Orig_Spec, Empty_List);
1826            Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1827            Save_Global_References          (Orig_Decl);
1828            Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1829            Set_Visible_Declarations        (Orig_Spec, Save_Vis);
1830         end;
1831      end if;
1832
1833      Process_End_Label (N, 'e', Id);
1834
1835      --  Remove private_with_clauses of enclosing compilation unit, if they
1836      --  were installed.
1837
1838      if Private_With_Clauses_Installed then
1839         Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
1840      end if;
1841
1842      --  For the case of a library level package, we must go through all the
1843      --  entities clearing the indications that the value may be constant and
1844      --  not modified. Why? Because any client of this package may modify
1845      --  these values freely from anywhere. This also applies to any nested
1846      --  packages or generic packages.
1847
1848      --  For now we unconditionally clear constants for packages that are
1849      --  instances of generic packages. The reason is that we do not have the
1850      --  body yet, and we otherwise think things are unreferenced when they
1851      --  are not. This should be fixed sometime (the effect is not terrible,
1852      --  we just lose some warnings, and also some cases of value propagation)
1853      --  ???
1854
1855      if Is_Library_Level_Entity (Id)
1856        or else Is_Generic_Instance (Id)
1857      then
1858         Clear_Constants (Id, First_Entity (Id));
1859         Clear_Constants (Id, First_Private_Entity (Id));
1860      end if;
1861
1862      --  Issue an error in SPARK mode if a package specification contains
1863      --  more than one tagged type or type extension.
1864
1865      Check_One_Tagged_Type_Or_Extension_At_Most;
1866
1867      --  Output relevant information as to why the package requires a body.
1868      --  Do not consider generated packages as this exposes internal symbols
1869      --  and leads to confusing messages.
1870
1871      if List_Body_Required_Info
1872        and then In_Extended_Main_Source_Unit (Id)
1873        and then Unit_Requires_Body (Id)
1874        and then Comes_From_Source (Id)
1875      then
1876         Unit_Requires_Body_Info (Id);
1877      end if;
1878
1879      --  Nested package specs that do not require bodies are not checked for
1880      --  ineffective use clauses due to the possbility of subunits. This is
1881      --  because at this stage it is impossible to tell whether there will be
1882      --  a separate body.
1883
1884      if not Unit_Requires_Body (Id)
1885        and then Is_Compilation_Unit (Id)
1886        and then not Is_Private_Descendant (Id)
1887      then
1888         Update_Use_Clause_Chain;
1889      end if;
1890   end Analyze_Package_Specification;
1891
1892   --------------------------------------
1893   -- Analyze_Private_Type_Declaration --
1894   --------------------------------------
1895
1896   procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1897      Id : constant Entity_Id := Defining_Identifier (N);
1898      PF : constant Boolean   := Is_Pure (Enclosing_Lib_Unit_Entity);
1899
1900   begin
1901      Generate_Definition (Id);
1902      Set_Is_Pure         (Id, PF);
1903      Init_Size_Align     (Id);
1904
1905      if not Is_Package_Or_Generic_Package (Current_Scope)
1906        or else In_Private_Part (Current_Scope)
1907      then
1908         Error_Msg_N ("invalid context for private declaration", N);
1909      end if;
1910
1911      New_Private_Type (N, Id, N);
1912      Set_Depends_On_Private (Id);
1913
1914      --  Set the SPARK mode from the current context
1915
1916      Set_SPARK_Pragma           (Id, SPARK_Mode_Pragma);
1917      Set_SPARK_Pragma_Inherited (Id);
1918
1919      if Has_Aspects (N) then
1920         Analyze_Aspect_Specifications (N, Id);
1921      end if;
1922   end Analyze_Private_Type_Declaration;
1923
1924   ----------------------------------
1925   -- Check_Anonymous_Access_Types --
1926   ----------------------------------
1927
1928   procedure Check_Anonymous_Access_Types
1929     (Spec_Id : Entity_Id;
1930      P_Body  : Node_Id)
1931   is
1932      E  : Entity_Id;
1933      IR : Node_Id;
1934
1935   begin
1936      --  Itype references are only needed by gigi, to force elaboration of
1937      --  itypes. In the absence of code generation, they are not needed.
1938
1939      if not Expander_Active then
1940         return;
1941      end if;
1942
1943      E := First_Entity (Spec_Id);
1944      while Present (E) loop
1945         if Ekind (E) = E_Anonymous_Access_Type
1946           and then From_Limited_With (E)
1947         then
1948            IR := Make_Itype_Reference (Sloc (P_Body));
1949            Set_Itype (IR, E);
1950
1951            if No (Declarations (P_Body)) then
1952               Set_Declarations (P_Body, New_List (IR));
1953            else
1954               Prepend (IR, Declarations (P_Body));
1955            end if;
1956         end if;
1957
1958         Next_Entity (E);
1959      end loop;
1960   end Check_Anonymous_Access_Types;
1961
1962   -------------------------------------------
1963   -- Declare_Inherited_Private_Subprograms --
1964   -------------------------------------------
1965
1966   procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1967
1968      function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1969      --  Check whether an inherited subprogram S is an operation of an
1970      --  untagged derived type T.
1971
1972      ---------------------
1973      -- Is_Primitive_Of --
1974      ---------------------
1975
1976      function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1977         Formal : Entity_Id;
1978
1979      begin
1980         --  If the full view is a scalar type, the type is the anonymous base
1981         --  type, but the operation mentions the first subtype, so check the
1982         --  signature against the base type.
1983
1984         if Base_Type (Etype (S)) = Base_Type (T) then
1985            return True;
1986
1987         else
1988            Formal := First_Formal (S);
1989            while Present (Formal) loop
1990               if Base_Type (Etype (Formal)) = Base_Type (T) then
1991                  return True;
1992               end if;
1993
1994               Next_Formal (Formal);
1995            end loop;
1996
1997            return False;
1998         end if;
1999      end Is_Primitive_Of;
2000
2001      --  Local variables
2002
2003      E           : Entity_Id;
2004      Op_List     : Elist_Id;
2005      Op_Elmt     : Elmt_Id;
2006      Op_Elmt_2   : Elmt_Id;
2007      Prim_Op     : Entity_Id;
2008      New_Op      : Entity_Id := Empty;
2009      Parent_Subp : Entity_Id;
2010      Tag         : Entity_Id;
2011
2012   --  Start of processing for Declare_Inherited_Private_Subprograms
2013
2014   begin
2015      E := First_Entity (Id);
2016      while Present (E) loop
2017
2018         --  If the entity is a nonprivate type extension whose parent type
2019         --  is declared in an open scope, then the type may have inherited
2020         --  operations that now need to be made visible. Ditto if the entity
2021         --  is a formal derived type in a child unit.
2022
2023         if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
2024               or else
2025                 (Nkind (Parent (E)) = N_Private_Extension_Declaration
2026                   and then Is_Generic_Type (E)))
2027           and then In_Open_Scopes (Scope (Etype (E)))
2028           and then Is_Base_Type (E)
2029         then
2030            if Is_Tagged_Type (E) then
2031               Op_List := Primitive_Operations (E);
2032               New_Op  := Empty;
2033               Tag     := First_Tag_Component (E);
2034
2035               Op_Elmt := First_Elmt (Op_List);
2036               while Present (Op_Elmt) loop
2037                  Prim_Op := Node (Op_Elmt);
2038
2039                  --  Search primitives that are implicit operations with an
2040                  --  internal name whose parent operation has a normal name.
2041
2042                  if Present (Alias (Prim_Op))
2043                    and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
2044                    and then not Comes_From_Source (Prim_Op)
2045                    and then Is_Internal_Name (Chars (Prim_Op))
2046                    and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
2047                  then
2048                     Parent_Subp := Alias (Prim_Op);
2049
2050                     --  Case 1: Check if the type has also an explicit
2051                     --  overriding for this primitive.
2052
2053                     Op_Elmt_2 := Next_Elmt (Op_Elmt);
2054                     while Present (Op_Elmt_2) loop
2055
2056                        --  Skip entities with attribute Interface_Alias since
2057                        --  they are not overriding primitives (these entities
2058                        --  link an interface primitive with their covering
2059                        --  primitive)
2060
2061                        if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
2062                          and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
2063                          and then No (Interface_Alias (Node (Op_Elmt_2)))
2064                        then
2065                           --  The private inherited operation has been
2066                           --  overridden by an explicit subprogram:
2067                           --  replace the former by the latter.
2068
2069                           New_Op := Node (Op_Elmt_2);
2070                           Replace_Elmt (Op_Elmt, New_Op);
2071                           Remove_Elmt  (Op_List, Op_Elmt_2);
2072                           Set_Overridden_Operation (New_Op, Parent_Subp);
2073
2074                           --  We don't need to inherit its dispatching slot.
2075                           --  Set_All_DT_Position has previously ensured that
2076                           --  the same slot was assigned to the two primitives
2077
2078                           if Present (Tag)
2079                             and then Present (DTC_Entity (New_Op))
2080                             and then Present (DTC_Entity (Prim_Op))
2081                           then
2082                              pragma Assert
2083                                (DT_Position (New_Op) = DT_Position (Prim_Op));
2084                              null;
2085                           end if;
2086
2087                           goto Next_Primitive;
2088                        end if;
2089
2090                        Next_Elmt (Op_Elmt_2);
2091                     end loop;
2092
2093                     --  Case 2: We have not found any explicit overriding and
2094                     --  hence we need to declare the operation (i.e., make it
2095                     --  visible).
2096
2097                     Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2098
2099                     --  Inherit the dispatching slot if E is already frozen
2100
2101                     if Is_Frozen (E)
2102                       and then Present (DTC_Entity (Alias (Prim_Op)))
2103                     then
2104                        Set_DTC_Entity_Value (E, New_Op);
2105                        Set_DT_Position_Value (New_Op,
2106                          DT_Position (Alias (Prim_Op)));
2107                     end if;
2108
2109                     pragma Assert
2110                       (Is_Dispatching_Operation (New_Op)
2111                         and then Node (Last_Elmt (Op_List)) = New_Op);
2112
2113                     --  Substitute the new operation for the old one in the
2114                     --  type's primitive operations list. Since the new
2115                     --  operation was also just added to the end of list,
2116                     --  the last element must be removed.
2117
2118                     --  (Question: is there a simpler way of declaring the
2119                     --  operation, say by just replacing the name of the
2120                     --  earlier operation, reentering it in the in the symbol
2121                     --  table (how?), and marking it as private???)
2122
2123                     Replace_Elmt (Op_Elmt, New_Op);
2124                     Remove_Last_Elmt (Op_List);
2125                  end if;
2126
2127                  <<Next_Primitive>>
2128                  Next_Elmt (Op_Elmt);
2129               end loop;
2130
2131               --  Generate listing showing the contents of the dispatch table
2132
2133               if Debug_Flag_ZZ then
2134                  Write_DT (E);
2135               end if;
2136
2137            else
2138               --  For untagged type, scan forward to locate inherited hidden
2139               --  operations.
2140
2141               Prim_Op := Next_Entity (E);
2142               while Present (Prim_Op) loop
2143                  if Is_Subprogram (Prim_Op)
2144                    and then Present (Alias (Prim_Op))
2145                    and then not Comes_From_Source (Prim_Op)
2146                    and then Is_Internal_Name (Chars (Prim_Op))
2147                    and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
2148                    and then Is_Primitive_Of (E, Prim_Op)
2149                  then
2150                     Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2151                  end if;
2152
2153                  Next_Entity (Prim_Op);
2154
2155                  --  Derived operations appear immediately after the type
2156                  --  declaration (or the following subtype indication for
2157                  --  a derived scalar type). Further declarations cannot
2158                  --  include inherited operations of the type.
2159
2160                  if Present (Prim_Op) then
2161                     exit when Ekind (Prim_Op) not in Overloadable_Kind;
2162                  end if;
2163               end loop;
2164            end if;
2165         end if;
2166
2167         Next_Entity (E);
2168      end loop;
2169   end Declare_Inherited_Private_Subprograms;
2170
2171   -----------------------
2172   -- End_Package_Scope --
2173   -----------------------
2174
2175   procedure End_Package_Scope (P : Entity_Id) is
2176   begin
2177      Uninstall_Declarations (P);
2178      Pop_Scope;
2179   end End_Package_Scope;
2180
2181   ---------------------------
2182   -- Exchange_Declarations --
2183   ---------------------------
2184
2185   procedure Exchange_Declarations (Id : Entity_Id) is
2186      Full_Id : constant Entity_Id := Full_View (Id);
2187      H1      : constant Entity_Id := Homonym (Id);
2188      Next1   : constant Entity_Id := Next_Entity (Id);
2189      H2      : Entity_Id;
2190      Next2   : Entity_Id;
2191
2192   begin
2193      --  If missing full declaration for type, nothing to exchange
2194
2195      if No (Full_Id) then
2196         return;
2197      end if;
2198
2199      --  Otherwise complete the exchange, and preserve semantic links
2200
2201      Next2 := Next_Entity (Full_Id);
2202      H2    := Homonym (Full_Id);
2203
2204      --  Reset full declaration pointer to reflect the switched entities and
2205      --  readjust the next entity chains.
2206
2207      Exchange_Entities (Id, Full_Id);
2208
2209      Link_Entities (Id, Next1);
2210      Set_Homonym   (Id, H1);
2211
2212      Set_Full_View (Full_Id, Id);
2213      Link_Entities (Full_Id, Next2);
2214      Set_Homonym   (Full_Id, H2);
2215   end Exchange_Declarations;
2216
2217   ----------------------------
2218   -- Install_Package_Entity --
2219   ----------------------------
2220
2221   procedure Install_Package_Entity (Id : Entity_Id) is
2222   begin
2223      if not Is_Internal (Id) then
2224         if Debug_Flag_E then
2225            Write_Str ("Install: ");
2226            Write_Name (Chars (Id));
2227            Write_Eol;
2228         end if;
2229
2230         if Is_Child_Unit (Id) then
2231            null;
2232
2233         --  Do not enter implicitly inherited non-overridden subprograms of
2234         --  a tagged type back into visibility if they have non-conformant
2235         --  homographs (Ada RM 8.3 12.3/2).
2236
2237         elsif Is_Hidden_Non_Overridden_Subpgm (Id) then
2238            null;
2239
2240         else
2241            Set_Is_Immediately_Visible (Id);
2242         end if;
2243      end if;
2244   end Install_Package_Entity;
2245
2246   ----------------------------------
2247   -- Install_Private_Declarations --
2248   ----------------------------------
2249
2250   procedure Install_Private_Declarations (P : Entity_Id) is
2251      Id        : Entity_Id;
2252      Full      : Entity_Id;
2253      Priv_Deps : Elist_Id;
2254
2255      procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
2256      --  When the full view of a private type is made available, we do the
2257      --  same for its private dependents under proper visibility conditions.
2258      --  When compiling a grand-chid unit this needs to be done recursively.
2259
2260      -----------------------------
2261      -- Swap_Private_Dependents --
2262      -----------------------------
2263
2264      procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
2265         Deps      : Elist_Id;
2266         Priv      : Entity_Id;
2267         Priv_Elmt : Elmt_Id;
2268         Is_Priv   : Boolean;
2269
2270      begin
2271         Priv_Elmt := First_Elmt (Priv_Deps);
2272         while Present (Priv_Elmt) loop
2273            Priv := Node (Priv_Elmt);
2274
2275            --  Before the exchange, verify that the presence of the Full_View
2276            --  field. This field will be empty if the entity has already been
2277            --  installed due to a previous call.
2278
2279            if Present (Full_View (Priv)) and then Is_Visible_Dependent (Priv)
2280            then
2281               if Is_Private_Type (Priv) then
2282                  Deps := Private_Dependents (Priv);
2283                  Is_Priv := True;
2284               else
2285                  Is_Priv := False;
2286               end if;
2287
2288               --  For each subtype that is swapped, we also swap the reference
2289               --  to it in Private_Dependents, to allow access to it when we
2290               --  swap them out in End_Package_Scope.
2291
2292               Replace_Elmt (Priv_Elmt, Full_View (Priv));
2293
2294               --  Ensure that both views of the dependent private subtype are
2295               --  immediately visible if within some open scope. Check full
2296               --  view before exchanging views.
2297
2298               if In_Open_Scopes (Scope (Full_View (Priv))) then
2299                  Set_Is_Immediately_Visible (Priv);
2300               end if;
2301
2302               Exchange_Declarations (Priv);
2303               Set_Is_Immediately_Visible
2304                 (Priv, In_Open_Scopes (Scope (Priv)));
2305
2306               Set_Is_Potentially_Use_Visible
2307                 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
2308
2309               --  Within a child unit, recurse, except in generic child unit,
2310               --  which (unfortunately) handle private_dependents separately.
2311
2312               if Is_Priv
2313                 and then Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
2314                 and then not Is_Empty_Elmt_List (Deps)
2315                 and then not Inside_A_Generic
2316               then
2317                  Swap_Private_Dependents (Deps);
2318               end if;
2319            end if;
2320
2321            Next_Elmt (Priv_Elmt);
2322         end loop;
2323      end Swap_Private_Dependents;
2324
2325   --  Start of processing for Install_Private_Declarations
2326
2327   begin
2328      --  First exchange declarations for private types, so that the full
2329      --  declaration is visible. For each private type, we check its
2330      --  Private_Dependents list and also exchange any subtypes of or derived
2331      --  types from it. Finally, if this is a Taft amendment type, the
2332      --  incomplete declaration is irrelevant, and we want to link the
2333      --  eventual full declaration with the original private one so we
2334      --  also skip the exchange.
2335
2336      Id := First_Entity (P);
2337      while Present (Id) and then Id /= First_Private_Entity (P) loop
2338         if Is_Private_Base_Type (Id)
2339           and then Present (Full_View (Id))
2340           and then Comes_From_Source (Full_View (Id))
2341           and then Scope (Full_View (Id)) = Scope (Id)
2342           and then Ekind (Full_View (Id)) /= E_Incomplete_Type
2343         then
2344            --  If there is a use-type clause on the private type, set the full
2345            --  view accordingly.
2346
2347            Set_In_Use (Full_View (Id), In_Use (Id));
2348            Full := Full_View (Id);
2349
2350            if Is_Private_Base_Type (Full)
2351              and then Has_Private_Declaration (Full)
2352              and then Nkind (Parent (Full)) = N_Full_Type_Declaration
2353              and then In_Open_Scopes (Scope (Etype (Full)))
2354              and then In_Package_Body (Current_Scope)
2355              and then not Is_Private_Type (Etype (Full))
2356            then
2357               --  This is the completion of a private type by a derivation
2358               --  from another private type which is not private anymore. This
2359               --  can only happen in a package nested within a child package,
2360               --  when the parent type is defined in the parent unit. At this
2361               --  point the current type is not private either, and we have
2362               --  to install the underlying full view, which is now visible.
2363               --  Save the current full view as well, so that all views can be
2364               --  restored on exit. It may seem that after compiling the child
2365               --  body there are not environments to restore, but the back-end
2366               --  expects those links to be valid, and freeze nodes depend on
2367               --  them.
2368
2369               if No (Full_View (Full))
2370                 and then Present (Underlying_Full_View (Full))
2371               then
2372                  Set_Full_View (Id, Underlying_Full_View (Full));
2373                  Set_Underlying_Full_View (Id, Full);
2374                  Set_Is_Underlying_Full_View (Full);
2375
2376                  Set_Underlying_Full_View (Full, Empty);
2377                  Set_Is_Frozen (Full_View (Id));
2378               end if;
2379            end if;
2380
2381            Priv_Deps := Private_Dependents (Id);
2382            Exchange_Declarations (Id);
2383            Set_Is_Immediately_Visible (Id);
2384            Swap_Private_Dependents (Priv_Deps);
2385         end if;
2386
2387         Next_Entity (Id);
2388      end loop;
2389
2390      --  Next make other declarations in the private part visible as well
2391
2392      Id := First_Private_Entity (P);
2393      while Present (Id) loop
2394         Install_Package_Entity (Id);
2395         Set_Is_Hidden (Id, False);
2396         Next_Entity (Id);
2397      end loop;
2398
2399      --  An abstract state is partially refined when it has at least one
2400      --  Part_Of constituent. Since these constituents are being installed
2401      --  into visibility, update the partial refinement status of any state
2402      --  defined in the associated package, subject to at least one Part_Of
2403      --  constituent.
2404
2405      if Ekind_In (P, E_Generic_Package, E_Package) then
2406         declare
2407            States     : constant Elist_Id := Abstract_States (P);
2408            State_Elmt : Elmt_Id;
2409            State_Id   : Entity_Id;
2410
2411         begin
2412            if Present (States) then
2413               State_Elmt := First_Elmt (States);
2414               while Present (State_Elmt) loop
2415                  State_Id := Node (State_Elmt);
2416
2417                  if Present (Part_Of_Constituents (State_Id)) then
2418                     Set_Has_Partial_Visible_Refinement (State_Id);
2419                  end if;
2420
2421                  Next_Elmt (State_Elmt);
2422               end loop;
2423            end if;
2424         end;
2425      end if;
2426
2427      --  Indicate that the private part is currently visible, so it can be
2428      --  properly reset on exit.
2429
2430      Set_In_Private_Part (P);
2431   end Install_Private_Declarations;
2432
2433   ----------------------------------
2434   -- Install_Visible_Declarations --
2435   ----------------------------------
2436
2437   procedure Install_Visible_Declarations (P : Entity_Id) is
2438      Id          : Entity_Id;
2439      Last_Entity : Entity_Id;
2440
2441   begin
2442      pragma Assert
2443        (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
2444
2445      if Is_Package_Or_Generic_Package (P) then
2446         Last_Entity := First_Private_Entity (P);
2447      else
2448         Last_Entity := Empty;
2449      end if;
2450
2451      Id := First_Entity (P);
2452      while Present (Id) and then Id /= Last_Entity loop
2453         Install_Package_Entity (Id);
2454         Next_Entity (Id);
2455      end loop;
2456   end Install_Visible_Declarations;
2457
2458   --------------------------
2459   -- Is_Private_Base_Type --
2460   --------------------------
2461
2462   function Is_Private_Base_Type (E : Entity_Id) return Boolean is
2463   begin
2464      return Ekind (E) = E_Private_Type
2465        or else Ekind (E) = E_Limited_Private_Type
2466        or else Ekind (E) = E_Record_Type_With_Private;
2467   end Is_Private_Base_Type;
2468
2469   --------------------------
2470   -- Is_Visible_Dependent --
2471   --------------------------
2472
2473   function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
2474   is
2475      S : constant Entity_Id := Scope (Dep);
2476
2477   begin
2478      --  Renamings created for actual types have the visibility of the actual
2479
2480      if Ekind (S) = E_Package
2481        and then Is_Generic_Instance (S)
2482        and then (Is_Generic_Actual_Type (Dep)
2483                   or else Is_Generic_Actual_Type (Full_View (Dep)))
2484      then
2485         return True;
2486
2487      elsif not (Is_Derived_Type (Dep))
2488        and then Is_Derived_Type (Full_View (Dep))
2489      then
2490         --  When instantiating a package body, the scope stack is empty, so
2491         --  check instead whether the dependent type is defined in the same
2492         --  scope as the instance itself.
2493
2494         return In_Open_Scopes (S)
2495           or else (Is_Generic_Instance (Current_Scope)
2496                     and then Scope (Dep) = Scope (Current_Scope));
2497      else
2498         return True;
2499      end if;
2500   end Is_Visible_Dependent;
2501
2502   ----------------------------
2503   -- May_Need_Implicit_Body --
2504   ----------------------------
2505
2506   procedure May_Need_Implicit_Body (E : Entity_Id) is
2507      P     : constant Node_Id := Unit_Declaration_Node (E);
2508      S     : constant Node_Id := Parent (P);
2509      B     : Node_Id;
2510      Decls : List_Id;
2511
2512   begin
2513      if not Has_Completion (E)
2514        and then Nkind (P) = N_Package_Declaration
2515        and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
2516      then
2517         B :=
2518           Make_Package_Body (Sloc (E),
2519             Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
2520               Chars => Chars (E)),
2521             Declarations  => New_List);
2522
2523         if Nkind (S) = N_Package_Specification then
2524            if Present (Private_Declarations (S)) then
2525               Decls := Private_Declarations (S);
2526            else
2527               Decls := Visible_Declarations (S);
2528            end if;
2529         else
2530            Decls := Declarations (S);
2531         end if;
2532
2533         Append (B, Decls);
2534         Analyze (B);
2535      end if;
2536   end May_Need_Implicit_Body;
2537
2538   ----------------------
2539   -- New_Private_Type --
2540   ----------------------
2541
2542   procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
2543   begin
2544      --  For other than Ada 2012, enter the name in the current scope
2545
2546      if Ada_Version < Ada_2012 then
2547         Enter_Name (Id);
2548
2549      --  Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2550      --  there may be an incomplete previous view.
2551
2552      else
2553         declare
2554            Prev : Entity_Id;
2555         begin
2556            Prev := Find_Type_Name (N);
2557            pragma Assert (Prev = Id
2558              or else (Ekind (Prev) = E_Incomplete_Type
2559                        and then Present (Full_View (Prev))
2560                        and then Full_View (Prev) = Id));
2561         end;
2562      end if;
2563
2564      if Limited_Present (Def) then
2565         Set_Ekind (Id, E_Limited_Private_Type);
2566      else
2567         Set_Ekind (Id, E_Private_Type);
2568      end if;
2569
2570      Set_Etype              (Id, Id);
2571      Set_Has_Delayed_Freeze (Id);
2572      Set_Is_First_Subtype   (Id);
2573      Init_Size_Align        (Id);
2574
2575      Set_Is_Constrained (Id,
2576        No (Discriminant_Specifications (N))
2577          and then not Unknown_Discriminants_Present (N));
2578
2579      --  Set tagged flag before processing discriminants, to catch illegal
2580      --  usage.
2581
2582      Set_Is_Tagged_Type (Id, Tagged_Present (Def));
2583
2584      Set_Discriminant_Constraint (Id, No_Elist);
2585      Set_Stored_Constraint (Id, No_Elist);
2586
2587      if Present (Discriminant_Specifications (N)) then
2588         Push_Scope (Id);
2589         Process_Discriminants (N);
2590         End_Scope;
2591
2592      elsif Unknown_Discriminants_Present (N) then
2593         Set_Has_Unknown_Discriminants (Id);
2594      end if;
2595
2596      Set_Private_Dependents (Id, New_Elmt_List);
2597
2598      if Tagged_Present (Def) then
2599         Set_Ekind                       (Id, E_Record_Type_With_Private);
2600         Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2601         Set_Is_Abstract_Type            (Id, Abstract_Present (Def));
2602         Set_Is_Limited_Record           (Id, Limited_Present (Def));
2603         Set_Has_Delayed_Freeze          (Id, True);
2604
2605         --  Recognize Ada.Real_Time.Timing_Events.Timing_Events here
2606
2607         if Is_RTE (Id, RE_Timing_Event) then
2608            Set_Has_Timing_Event (Id);
2609         end if;
2610
2611         --  Create a class-wide type with the same attributes
2612
2613         Make_Class_Wide_Type (Id);
2614
2615      elsif Abstract_Present (Def) then
2616         Error_Msg_N ("only a tagged type can be abstract", N);
2617      end if;
2618   end New_Private_Type;
2619
2620   ---------------------------------
2621   -- Requires_Completion_In_Body --
2622   ---------------------------------
2623
2624   function Requires_Completion_In_Body
2625     (Id                 : Entity_Id;
2626      Pack_Id            : Entity_Id;
2627      Do_Abstract_States : Boolean := False) return Boolean
2628   is
2629   begin
2630      --  Always ignore child units. Child units get added to the entity list
2631      --  of a parent unit, but are not original entities of the parent, and
2632      --  so do not affect whether the parent needs a body.
2633
2634      if Is_Child_Unit (Id) then
2635         return False;
2636
2637      --  Ignore formal packages and their renamings
2638
2639      elsif Ekind (Id) = E_Package
2640        and then Nkind (Original_Node (Unit_Declaration_Node (Id))) =
2641                   N_Formal_Package_Declaration
2642      then
2643         return False;
2644
2645      --  Otherwise test to see if entity requires a completion. Note that
2646      --  subprogram entities whose declaration does not come from source are
2647      --  ignored here on the basis that we assume the expander will provide an
2648      --  implicit completion at some point.
2649
2650      elsif (Is_Overloadable (Id)
2651              and then not Ekind_In (Id, E_Enumeration_Literal, E_Operator)
2652              and then not Is_Abstract_Subprogram (Id)
2653              and then not Has_Completion (Id)
2654              and then Comes_From_Source (Parent (Id)))
2655
2656        or else
2657          (Ekind (Id) = E_Package
2658            and then Id /= Pack_Id
2659            and then not Has_Completion (Id)
2660            and then Unit_Requires_Body (Id, Do_Abstract_States))
2661
2662        or else
2663          (Ekind (Id) = E_Incomplete_Type
2664            and then No (Full_View (Id))
2665            and then not Is_Generic_Type (Id))
2666
2667        or else
2668          (Ekind_In (Id, E_Task_Type, E_Protected_Type)
2669            and then not Has_Completion (Id))
2670
2671        or else
2672          (Ekind (Id) = E_Generic_Package
2673            and then Id /= Pack_Id
2674            and then not Has_Completion (Id)
2675            and then Unit_Requires_Body (Id, Do_Abstract_States))
2676
2677        or else
2678          (Is_Generic_Subprogram (Id)
2679            and then not Has_Completion (Id))
2680      then
2681         return True;
2682
2683      --  Otherwise the entity does not require completion in a package body
2684
2685      else
2686         return False;
2687      end if;
2688   end Requires_Completion_In_Body;
2689
2690   ----------------------------
2691   -- Uninstall_Declarations --
2692   ----------------------------
2693
2694   procedure Uninstall_Declarations (P : Entity_Id) is
2695      Decl      : constant Node_Id := Unit_Declaration_Node (P);
2696      Id        : Entity_Id;
2697      Full      : Entity_Id;
2698      Priv_Elmt : Elmt_Id;
2699      Priv_Sub  : Entity_Id;
2700
2701      procedure Preserve_Full_Attributes (Priv : Entity_Id; Full : Entity_Id);
2702      --  Copy to the private declaration the attributes of the full view that
2703      --  need to be available for the partial view also.
2704
2705      function Type_In_Use (T : Entity_Id) return Boolean;
2706      --  Check whether type or base type appear in an active use_type clause
2707
2708      ------------------------------
2709      -- Preserve_Full_Attributes --
2710      ------------------------------
2711
2712      procedure Preserve_Full_Attributes
2713        (Priv : Entity_Id;
2714         Full : Entity_Id)
2715      is
2716         Full_Base         : constant Entity_Id := Base_Type (Full);
2717         Priv_Is_Base_Type : constant Boolean   := Is_Base_Type (Priv);
2718
2719      begin
2720         Set_Size_Info               (Priv,                             Full);
2721         Set_RM_Size                 (Priv, RM_Size                    (Full));
2722         Set_Size_Known_At_Compile_Time
2723                                     (Priv, Size_Known_At_Compile_Time (Full));
2724         Set_Is_Volatile             (Priv, Is_Volatile                (Full));
2725         Set_Treat_As_Volatile       (Priv, Treat_As_Volatile          (Full));
2726         Set_Is_Ada_2005_Only        (Priv, Is_Ada_2005_Only           (Full));
2727         Set_Is_Ada_2012_Only        (Priv, Is_Ada_2012_Only           (Full));
2728         Set_Has_Pragma_Unmodified   (Priv, Has_Pragma_Unmodified      (Full));
2729         Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced    (Full));
2730         Set_Has_Pragma_Unreferenced_Objects
2731                                     (Priv, Has_Pragma_Unreferenced_Objects
2732                                                                       (Full));
2733         if Is_Unchecked_Union (Full) then
2734            Set_Is_Unchecked_Union (Base_Type (Priv));
2735         end if;
2736         --  Why is atomic not copied here ???
2737
2738         if Referenced (Full) then
2739            Set_Referenced (Priv);
2740         end if;
2741
2742         if Priv_Is_Base_Type then
2743            Set_Is_Controlled_Active
2744                              (Priv, Is_Controlled_Active     (Full_Base));
2745            Set_Finalize_Storage_Only
2746                              (Priv, Finalize_Storage_Only    (Full_Base));
2747            Set_Has_Controlled_Component
2748                              (Priv, Has_Controlled_Component (Full_Base));
2749
2750            Propagate_Concurrent_Flags (Priv, Base_Type (Full));
2751         end if;
2752
2753         --  As explained in Freeze_Entity, private types are required to point
2754         --  to the same freeze node as their corresponding full view, if any.
2755         --  But we ought not to overwrite a node already inserted in the tree.
2756
2757         pragma Assert
2758           (Serious_Errors_Detected /= 0
2759             or else No (Freeze_Node (Priv))
2760             or else No (Parent (Freeze_Node (Priv)))
2761             or else Freeze_Node (Priv) = Freeze_Node (Full));
2762
2763         Set_Freeze_Node (Priv, Freeze_Node (Full));
2764
2765         --  Propagate Default_Initial_Condition-related attributes from the
2766         --  base type of the full view to the full view and vice versa. This
2767         --  may seem strange, but is necessary depending on which type
2768         --  triggered the generation of the DIC procedure body. As a result,
2769         --  both the full view and its base type carry the same DIC-related
2770         --  information.
2771
2772         Propagate_DIC_Attributes (Full, From_Typ => Full_Base);
2773         Propagate_DIC_Attributes (Full_Base, From_Typ => Full);
2774
2775         --  Propagate Default_Initial_Condition-related attributes from the
2776         --  full view to the private view.
2777
2778         Propagate_DIC_Attributes (Priv, From_Typ => Full);
2779
2780         --  Propagate invariant-related attributes from the base type of the
2781         --  full view to the full view and vice versa. This may seem strange,
2782         --  but is necessary depending on which type triggered the generation
2783         --  of the invariant procedure body. As a result, both the full view
2784         --  and its base type carry the same invariant-related information.
2785
2786         Propagate_Invariant_Attributes (Full, From_Typ => Full_Base);
2787         Propagate_Invariant_Attributes (Full_Base, From_Typ => Full);
2788
2789         --  Propagate invariant-related attributes from the full view to the
2790         --  private view.
2791
2792         Propagate_Invariant_Attributes (Priv, From_Typ => Full);
2793
2794         if Is_Tagged_Type (Priv)
2795           and then Is_Tagged_Type (Full)
2796           and then not Error_Posted (Full)
2797         then
2798            if Is_Tagged_Type (Priv) then
2799
2800               --  If the type is tagged, the tag itself must be available on
2801               --  the partial view, for expansion purposes.
2802
2803               Set_First_Entity (Priv, First_Entity (Full));
2804
2805               --  If there are discriminants in the partial view, these remain
2806               --  visible. Otherwise only the tag itself is visible, and there
2807               --  are no nameable components in the partial view.
2808
2809               if No (Last_Entity (Priv)) then
2810                  Set_Last_Entity (Priv, First_Entity (Priv));
2811               end if;
2812            end if;
2813
2814            Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2815
2816            if Has_Discriminants (Full) then
2817               Set_Discriminant_Constraint (Priv,
2818                 Discriminant_Constraint (Full));
2819            end if;
2820         end if;
2821      end Preserve_Full_Attributes;
2822
2823      -----------------
2824      -- Type_In_Use --
2825      -----------------
2826
2827      function Type_In_Use (T : Entity_Id) return Boolean is
2828      begin
2829         return Scope (Base_Type (T)) = P
2830           and then (In_Use (T) or else In_Use (Base_Type (T)));
2831      end Type_In_Use;
2832
2833   --  Start of processing for Uninstall_Declarations
2834
2835   begin
2836      Id := First_Entity (P);
2837      while Present (Id) and then Id /= First_Private_Entity (P) loop
2838         if Debug_Flag_E then
2839            Write_Str ("unlinking visible entity ");
2840            Write_Int (Int (Id));
2841            Write_Eol;
2842         end if;
2843
2844         --  On exit from the package scope, we must preserve the visibility
2845         --  established by use clauses in the current scope. Two cases:
2846
2847         --  a) If the entity is an operator, it may be a primitive operator of
2848         --  a type for which there is a visible use-type clause.
2849
2850         --  b) For other entities, their use-visibility is determined by a
2851         --  visible use clause for the package itself or a use-all-type clause
2852         --  applied directly to the entity's type. For a generic instance,
2853         --  the instantiation of the formals appears in the visible part,
2854         --  but the formals are private and remain so.
2855
2856         if Ekind (Id) = E_Function
2857           and then Is_Operator_Symbol_Name (Chars (Id))
2858           and then not Is_Hidden (Id)
2859           and then not Error_Posted (Id)
2860         then
2861            Set_Is_Potentially_Use_Visible (Id,
2862              In_Use (P)
2863              or else Type_In_Use (Etype (Id))
2864              or else Type_In_Use (Etype (First_Formal (Id)))
2865              or else (Present (Next_Formal (First_Formal (Id)))
2866                        and then
2867                          Type_In_Use
2868                            (Etype (Next_Formal (First_Formal (Id))))));
2869         else
2870            if In_Use (P) and then not Is_Hidden (Id) then
2871
2872               --  A child unit of a use-visible package remains use-visible
2873               --  only if it is itself a visible child unit. Otherwise it
2874               --  would remain visible in other contexts where P is use-
2875               --  visible, because once compiled it stays in the entity list
2876               --  of its parent unit.
2877
2878               if Is_Child_Unit (Id) then
2879                  Set_Is_Potentially_Use_Visible
2880                    (Id, Is_Visible_Lib_Unit (Id));
2881               else
2882                  Set_Is_Potentially_Use_Visible (Id);
2883               end if;
2884
2885            --  We need to avoid incorrectly marking enumeration literals as
2886            --  non-visible when a visible use-all-type clause is in effect.
2887
2888            elsif Type_In_Use (Etype (Id))
2889              and then Nkind (Current_Use_Clause (Etype (Id))) =
2890                         N_Use_Type_Clause
2891              and then All_Present (Current_Use_Clause (Etype (Id)))
2892            then
2893               null;
2894
2895            else
2896               Set_Is_Potentially_Use_Visible (Id, False);
2897            end if;
2898         end if;
2899
2900         --  Local entities are not immediately visible outside of the package
2901
2902         Set_Is_Immediately_Visible (Id, False);
2903
2904         --  If this is a private type with a full view (for example a local
2905         --  subtype of a private type declared elsewhere), ensure that the
2906         --  full view is also removed from visibility: it may be exposed when
2907         --  swapping views in an instantiation. Similarly, ensure that the
2908         --  use-visibility is properly set on both views.
2909
2910         if Is_Type (Id) and then Present (Full_View (Id)) then
2911            Set_Is_Immediately_Visible     (Full_View (Id), False);
2912            Set_Is_Potentially_Use_Visible (Full_View (Id),
2913              Is_Potentially_Use_Visible (Id));
2914         end if;
2915
2916         if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2917            Check_Abstract_Overriding (Id);
2918            Check_Conventions (Id);
2919         end if;
2920
2921         if Ekind_In (Id, E_Private_Type, E_Limited_Private_Type)
2922           and then No (Full_View (Id))
2923           and then not Is_Generic_Type (Id)
2924           and then not Is_Derived_Type (Id)
2925         then
2926            Error_Msg_N ("missing full declaration for private type&", Id);
2927
2928         elsif Ekind (Id) = E_Record_Type_With_Private
2929           and then not Is_Generic_Type (Id)
2930           and then No (Full_View (Id))
2931         then
2932            if Nkind (Parent (Id)) = N_Private_Type_Declaration then
2933               Error_Msg_N ("missing full declaration for private type&", Id);
2934            else
2935               Error_Msg_N
2936                 ("missing full declaration for private extension", Id);
2937            end if;
2938
2939         --  Case of constant, check for deferred constant declaration with
2940         --  no full view. Likely just a matter of a missing expression, or
2941         --  accidental use of the keyword constant.
2942
2943         elsif Ekind (Id) = E_Constant
2944
2945           --  OK if constant value present
2946
2947           and then No (Constant_Value (Id))
2948
2949           --  OK if full view present
2950
2951           and then No (Full_View (Id))
2952
2953           --  OK if imported, since that provides the completion
2954
2955           and then not Is_Imported (Id)
2956
2957           --  OK if object declaration replaced by renaming declaration as
2958           --  a result of OK_To_Rename processing (e.g. for concatenation)
2959
2960           and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration
2961
2962           --  OK if object declaration with the No_Initialization flag set
2963
2964           and then not (Nkind (Parent (Id)) = N_Object_Declaration
2965                          and then No_Initialization (Parent (Id)))
2966         then
2967            --  If no private declaration is present, we assume the user did
2968            --  not intend a deferred constant declaration and the problem
2969            --  is simply that the initializing expression is missing.
2970
2971            if not Has_Private_Declaration (Etype (Id)) then
2972
2973               --  We assume that the user did not intend a deferred constant
2974               --  declaration, and the expression is just missing.
2975
2976               Error_Msg_N
2977                 ("constant declaration requires initialization expression",
2978                   Parent (Id));
2979
2980               if Is_Limited_Type (Etype (Id)) then
2981                  Error_Msg_N
2982                    ("\if variable intended, remove CONSTANT from declaration",
2983                    Parent (Id));
2984               end if;
2985
2986            --  Otherwise if a private declaration is present, then we are
2987            --  missing the full declaration for the deferred constant.
2988
2989            else
2990               Error_Msg_N
2991                 ("missing full declaration for deferred constant (RM 7.4)",
2992                  Id);
2993
2994               if Is_Limited_Type (Etype (Id)) then
2995                  Error_Msg_N
2996                    ("\if variable intended, remove CONSTANT from declaration",
2997                     Parent (Id));
2998               end if;
2999            end if;
3000         end if;
3001
3002         Next_Entity (Id);
3003      end loop;
3004
3005      --  If the specification was installed as the parent of a public child
3006      --  unit, the private declarations were not installed, and there is
3007      --  nothing to do.
3008
3009      if not In_Private_Part (P) then
3010         return;
3011      else
3012         Set_In_Private_Part (P, False);
3013      end if;
3014
3015      --  Make private entities invisible and exchange full and private
3016      --  declarations for private types. Id is now the first private entity
3017      --  in the package.
3018
3019      while Present (Id) loop
3020         if Debug_Flag_E then
3021            Write_Str ("unlinking private entity ");
3022            Write_Int (Int (Id));
3023            Write_Eol;
3024         end if;
3025
3026         if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
3027            Check_Abstract_Overriding (Id);
3028            Check_Conventions (Id);
3029         end if;
3030
3031         Set_Is_Immediately_Visible (Id, False);
3032
3033         if Is_Private_Base_Type (Id) and then Present (Full_View (Id)) then
3034            Full := Full_View (Id);
3035
3036            --  If the partial view is not declared in the visible part of the
3037            --  package (as is the case when it is a type derived from some
3038            --  other private type in the private part of the current package),
3039            --  no exchange takes place.
3040
3041            if No (Parent (Id))
3042              or else List_Containing (Parent (Id)) /=
3043                               Visible_Declarations (Specification (Decl))
3044            then
3045               goto Next_Id;
3046            end if;
3047
3048            --  The entry in the private part points to the full declaration,
3049            --  which is currently visible. Exchange them so only the private
3050            --  type declaration remains accessible, and link private and full
3051            --  declaration in the opposite direction. Before the actual
3052            --  exchange, we copy back attributes of the full view that must
3053            --  be available to the partial view too.
3054
3055            Preserve_Full_Attributes (Id, Full);
3056
3057            Set_Is_Potentially_Use_Visible (Id, In_Use (P));
3058
3059            --  The following test may be redundant, as this is already
3060            --  diagnosed in sem_ch3. ???
3061
3062            if not Is_Definite_Subtype (Full)
3063              and then Is_Definite_Subtype (Id)
3064            then
3065               Error_Msg_Sloc := Sloc (Parent (Id));
3066               Error_Msg_NE
3067                 ("full view of& not compatible with declaration#", Full, Id);
3068            end if;
3069
3070            --  Swap out the subtypes and derived types of Id that
3071            --  were compiled in this scope, or installed previously
3072            --  by Install_Private_Declarations.
3073
3074            --  Before we do the swap, we verify the presence of the Full_View
3075            --  field which may be empty due to a swap by a previous call to
3076            --  End_Package_Scope (e.g. from the freezing mechanism).
3077
3078            Priv_Elmt := First_Elmt (Private_Dependents (Id));
3079            while Present (Priv_Elmt) loop
3080               Priv_Sub := Node (Priv_Elmt);
3081
3082               if Present (Full_View (Priv_Sub)) then
3083                  if Scope (Priv_Sub) = P
3084                     or else not In_Open_Scopes (Scope (Priv_Sub))
3085                  then
3086                     Set_Is_Immediately_Visible (Priv_Sub, False);
3087                  end if;
3088
3089                  if Is_Visible_Dependent (Priv_Sub) then
3090                     Preserve_Full_Attributes
3091                       (Priv_Sub, Full_View (Priv_Sub));
3092                     Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub));
3093                     Exchange_Declarations (Priv_Sub);
3094                  end if;
3095               end if;
3096
3097               Next_Elmt (Priv_Elmt);
3098            end loop;
3099
3100            --  Now restore the type itself to its private view
3101
3102            Exchange_Declarations (Id);
3103
3104            --  If we have installed an underlying full view for a type derived
3105            --  from a private type in a child unit, restore the proper views
3106            --  of private and full view. See corresponding code in
3107            --  Install_Private_Declarations.
3108
3109            --  After the exchange, Full denotes the private type in the
3110            --  visible part of the package.
3111
3112            if Is_Private_Base_Type (Full)
3113              and then Present (Full_View (Full))
3114              and then Present (Underlying_Full_View (Full))
3115              and then In_Package_Body (Current_Scope)
3116            then
3117               Set_Full_View (Full, Underlying_Full_View (Full));
3118               Set_Underlying_Full_View (Full, Empty);
3119            end if;
3120
3121         elsif Ekind (Id) = E_Incomplete_Type
3122           and then Comes_From_Source (Id)
3123           and then No (Full_View (Id))
3124         then
3125            --  Mark Taft amendment types. Verify that there are no primitive
3126            --  operations declared for the type (3.10.1(9)).
3127
3128            Set_Has_Completion_In_Body (Id);
3129
3130            declare
3131               Elmt : Elmt_Id;
3132               Subp : Entity_Id;
3133
3134            begin
3135               Elmt := First_Elmt (Private_Dependents (Id));
3136               while Present (Elmt) loop
3137                  Subp := Node (Elmt);
3138
3139                  --  Is_Primitive is tested because there can be cases where
3140                  --  nonprimitive subprograms (in nested packages) are added
3141                  --  to the Private_Dependents list.
3142
3143                  if Is_Overloadable (Subp) and then Is_Primitive (Subp) then
3144                     Error_Msg_NE
3145                       ("type& must be completed in the private part",
3146                        Parent (Subp), Id);
3147
3148                  --  The result type of an access-to-function type cannot be a
3149                  --  Taft-amendment type, unless the version is Ada 2012 or
3150                  --  later (see AI05-151).
3151
3152                  elsif Ada_Version < Ada_2012
3153                    and then Ekind (Subp) = E_Subprogram_Type
3154                  then
3155                     if Etype (Subp) = Id
3156                       or else
3157                         (Is_Class_Wide_Type (Etype (Subp))
3158                           and then Etype (Etype (Subp)) = Id)
3159                     then
3160                        Error_Msg_NE
3161                          ("type& must be completed in the private part",
3162                             Associated_Node_For_Itype (Subp), Id);
3163                     end if;
3164                  end if;
3165
3166                  Next_Elmt (Elmt);
3167               end loop;
3168            end;
3169
3170         elsif not Is_Child_Unit (Id)
3171           and then (not Is_Private_Type (Id) or else No (Full_View (Id)))
3172         then
3173            Set_Is_Hidden (Id);
3174            Set_Is_Potentially_Use_Visible (Id, False);
3175         end if;
3176
3177         <<Next_Id>>
3178            Next_Entity (Id);
3179      end loop;
3180   end Uninstall_Declarations;
3181
3182   ------------------------
3183   -- Unit_Requires_Body --
3184   ------------------------
3185
3186   function Unit_Requires_Body
3187     (Pack_Id            : Entity_Id;
3188      Do_Abstract_States : Boolean := False) return Boolean
3189   is
3190      E : Entity_Id;
3191
3192      Requires_Body : Boolean := False;
3193      --  Flag set when the unit has at least one construct that requries
3194      --  completion in a body.
3195
3196   begin
3197      --  Imported entity never requires body. Right now, only subprograms can
3198      --  be imported, but perhaps in the future we will allow import of
3199      --  packages.
3200
3201      if Is_Imported (Pack_Id) then
3202         return False;
3203
3204      --  Body required if library package with pragma Elaborate_Body
3205
3206      elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3207         return True;
3208
3209      --  Body required if subprogram
3210
3211      elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3212         return True;
3213
3214      --  Treat a block as requiring a body
3215
3216      elsif Ekind (Pack_Id) = E_Block then
3217         return True;
3218
3219      elsif Ekind (Pack_Id) = E_Package
3220        and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3221        and then Present (Generic_Parent (Parent (Pack_Id)))
3222      then
3223         declare
3224            G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3225         begin
3226            if Has_Pragma_Elaborate_Body (G_P) then
3227               return True;
3228            end if;
3229         end;
3230      end if;
3231
3232      --  Traverse the entity chain of the package and look for constructs that
3233      --  require a completion in a body.
3234
3235      E := First_Entity (Pack_Id);
3236      while Present (E) loop
3237
3238         --  Skip abstract states because their completion depends on several
3239         --  criteria (see below).
3240
3241         if Ekind (E) = E_Abstract_State then
3242            null;
3243
3244         elsif Requires_Completion_In_Body
3245                 (E, Pack_Id, Do_Abstract_States)
3246         then
3247            Requires_Body := True;
3248            exit;
3249         end if;
3250
3251         Next_Entity (E);
3252      end loop;
3253
3254      --  A [generic] package that defines at least one non-null abstract state
3255      --  requires a completion only when at least one other construct requires
3256      --  a completion in a body (SPARK RM 7.1.4(4) and (6)). This check is not
3257      --  performed if the caller requests this behavior.
3258
3259      if Do_Abstract_States
3260        and then Ekind_In (Pack_Id, E_Generic_Package, E_Package)
3261        and then Has_Non_Null_Abstract_State (Pack_Id)
3262        and then Requires_Body
3263      then
3264         return True;
3265      end if;
3266
3267      return Requires_Body;
3268   end Unit_Requires_Body;
3269
3270   -----------------------------
3271   -- Unit_Requires_Body_Info --
3272   -----------------------------
3273
3274   procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id) is
3275      E : Entity_Id;
3276
3277   begin
3278      --  An imported entity never requires body. Right now, only subprograms
3279      --  can be imported, but perhaps in the future we will allow import of
3280      --  packages.
3281
3282      if Is_Imported (Pack_Id) then
3283         return;
3284
3285      --  Body required if library package with pragma Elaborate_Body
3286
3287      elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3288         Error_Msg_N ("info: & requires body (Elaborate_Body)?Y?", Pack_Id);
3289
3290      --  Body required if subprogram
3291
3292      elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3293         Error_Msg_N ("info: & requires body (subprogram case)?Y?", Pack_Id);
3294
3295      --  Body required if generic parent has Elaborate_Body
3296
3297      elsif Ekind (Pack_Id) = E_Package
3298        and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3299        and then Present (Generic_Parent (Parent (Pack_Id)))
3300      then
3301         declare
3302            G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3303         begin
3304            if Has_Pragma_Elaborate_Body (G_P) then
3305               Error_Msg_N
3306                 ("info: & requires body (generic parent Elaborate_Body)?Y?",
3307                  Pack_Id);
3308            end if;
3309         end;
3310
3311      --  A [generic] package that introduces at least one non-null abstract
3312      --  state requires completion. However, there is a separate rule that
3313      --  requires that such a package have a reason other than this for a
3314      --  body being required (if necessary a pragma Elaborate_Body must be
3315      --  provided). If Ignore_Abstract_State is True, we don't do this check
3316      --  (so we can use Unit_Requires_Body to check for some other reason).
3317
3318      elsif Ekind_In (Pack_Id, E_Generic_Package, E_Package)
3319        and then Present (Abstract_States (Pack_Id))
3320        and then not Is_Null_State
3321                       (Node (First_Elmt (Abstract_States (Pack_Id))))
3322      then
3323         Error_Msg_N
3324           ("info: & requires body (non-null abstract state aspect)?Y?",
3325            Pack_Id);
3326      end if;
3327
3328      --  Otherwise search entity chain for entity requiring completion
3329
3330      E := First_Entity (Pack_Id);
3331      while Present (E) loop
3332         if Requires_Completion_In_Body (E, Pack_Id) then
3333            Error_Msg_Node_2 := E;
3334            Error_Msg_NE
3335              ("info: & requires body (& requires completion)?Y?", E, Pack_Id);
3336         end if;
3337
3338         Next_Entity (E);
3339      end loop;
3340   end Unit_Requires_Body_Info;
3341
3342end Sem_Ch7;
3343