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