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