OSDN Git Service

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