OSDN Git Service

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