OSDN Git Service

2008-04-08 Ed Schonberg <schonberg@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_ch7.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              S E M . C H 7                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2008, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 --  This package contains the routines to process package specifications and
27 --  bodies. The most important semantic aspects of package processing are the
28 --  handling of private and full declarations, and the construction of
29 --  dispatch tables for tagged types.
30
31 with Atree;    use Atree;
32 with Debug;    use Debug;
33 with Einfo;    use Einfo;
34 with Elists;   use Elists;
35 with Errout;   use Errout;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Dist; use Exp_Dist;
38 with Exp_Dbug; use Exp_Dbug;
39 with Lib;      use Lib;
40 with Lib.Xref; use Lib.Xref;
41 with Namet;    use Namet;
42 with Nmake;    use Nmake;
43 with Nlists;   use Nlists;
44 with Opt;      use Opt;
45 with Output;   use Output;
46 with Sem;      use Sem;
47 with Sem_Cat;  use Sem_Cat;
48 with Sem_Ch3;  use Sem_Ch3;
49 with Sem_Ch6;  use Sem_Ch6;
50 with Sem_Ch8;  use Sem_Ch8;
51 with Sem_Ch10; use Sem_Ch10;
52 with Sem_Ch12; use Sem_Ch12;
53 with Sem_Disp; use Sem_Disp;
54 with Sem_Prag; use Sem_Prag;
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 Nkind_In (K, N_Object_Declaration,
597                                      N_Exception_Declaration,
598                                      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 Analyze_PPCs (Decls : List_Id);
762       --  Given a list of declarations, go through looking for subprogram
763       --  specs, and for each one found, analyze any pre/postconditions that
764       --  are chained to the spec. This is the implementation of the late
765       --  visibility analysis for preconditions and postconditions in specs.
766
767       procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
768       --  Clears constant indications (Never_Set_In_Source, Constant_Value,
769       --  and Is_True_Constant) on all variables that are entities of Id,
770       --  and on the chain whose first element is FE. A recursive call is
771       --  made for all packages and generic packages.
772
773       procedure Generate_Parent_References;
774       --  For a child unit, generate references to parent units, for
775       --  GPS navigation purposes.
776
777       function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
778       --  Child and Unit are entities of compilation units. True if Child
779       --  is a public child of Parent as defined in 10.1.1
780
781       procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
782       --  Detects all incomplete or private type declarations having a known
783       --  discriminant part that are completed by an Unchecked_Union. Emits
784       --  the error message "Unchecked_Union may not complete discriminated
785       --  partial view".
786
787       procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
788       --  Given the package entity of a generic package instantiation or
789       --  formal package whose corresponding generic is a child unit, installs
790       --  the private declarations of each of the child unit's parents.
791       --  This has to be done at the point of entering the instance package's
792       --  private part rather than being done in Sem_Ch12.Install_Parent
793       --  (which is where the parents' visible declarations are installed).
794
795       ------------------
796       -- Analyze_PPCs --
797       ------------------
798
799       procedure Analyze_PPCs (Decls : List_Id) is
800          Decl : Node_Id;
801          Spec : Node_Id;
802          Sent : Entity_Id;
803          Prag : Node_Id;
804
805       begin
806          Decl := First (Decls);
807          while Present (Decl) loop
808             if Nkind (Original_Node (Decl)) = N_Subprogram_Declaration then
809                Spec := Specification (Original_Node (Decl));
810                Sent := Defining_Unit_Name (Spec);
811                Prag := Spec_PPC_List (Sent);
812                while Present (Prag) loop
813                   Analyze_PPC_In_Decl_Part (Prag, Sent);
814                   Prag := Next_Pragma (Prag);
815                end loop;
816             end if;
817
818             Next (Decl);
819          end loop;
820       end Analyze_PPCs;
821
822       ---------------------
823       -- Clear_Constants --
824       ---------------------
825
826       procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
827          E : Entity_Id;
828
829       begin
830          --  Ignore package renamings, not interesting and they can
831          --  cause self referential loops in the code below.
832
833          if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
834             return;
835          end if;
836
837          --  Note: in the loop below, the check for Next_Entity pointing
838          --  back to the package entity may seem odd, but it is needed,
839          --  because a package can contain a renaming declaration to itself,
840          --  and such renamings are generated automatically within package
841          --  instances.
842
843          E := FE;
844          while Present (E) and then E /= Id loop
845             if Is_Assignable (E) then
846                Set_Never_Set_In_Source (E, False);
847                Set_Is_True_Constant    (E, False);
848                Set_Current_Value       (E, Empty);
849                Set_Is_Known_Null       (E, False);
850                Set_Last_Assignment     (E, Empty);
851
852                if not Can_Never_Be_Null (E) then
853                   Set_Is_Known_Non_Null (E, False);
854                end if;
855
856             elsif Ekind (E) = E_Package
857                     or else
858                   Ekind (E) = E_Generic_Package
859             then
860                Clear_Constants (E, First_Entity (E));
861                Clear_Constants (E, First_Private_Entity (E));
862             end if;
863
864             Next_Entity (E);
865          end loop;
866       end Clear_Constants;
867
868       --------------------------------
869       -- Generate_Parent_References --
870       --------------------------------
871
872       procedure Generate_Parent_References is
873          Decl : constant Node_Id := Parent (N);
874
875       begin
876          if Id = Cunit_Entity (Main_Unit)
877            or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
878          then
879             Generate_Reference (Id, Scope (Id), 'k', False);
880
881          elsif not Nkind_In (Unit (Cunit (Main_Unit)), N_Subprogram_Body,
882                                                        N_Subunit)
883          then
884             --  If current unit is an ancestor of main unit, generate
885             --  a reference to its own parent.
886
887             declare
888                U         : Node_Id;
889                Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
890
891             begin
892                if Nkind (Main_Spec) = N_Package_Body then
893                   Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
894                end if;
895
896                U := Parent_Spec (Main_Spec);
897                while Present (U) loop
898                   if U = Parent (Decl) then
899                      Generate_Reference (Id, Scope (Id), 'k',  False);
900                      exit;
901
902                   elsif Nkind (Unit (U)) = N_Package_Body then
903                      exit;
904
905                   else
906                      U := Parent_Spec (Unit (U));
907                   end if;
908                end loop;
909             end;
910          end if;
911       end Generate_Parent_References;
912
913       ---------------------
914       -- Is_Public_Child --
915       ---------------------
916
917       function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
918       begin
919          if not Is_Private_Descendant (Child) then
920             return True;
921          else
922             if Child = Unit then
923                return not Private_Present (
924                  Parent (Unit_Declaration_Node (Child)));
925             else
926                return Is_Public_Child (Scope (Child), Unit);
927             end if;
928          end if;
929       end Is_Public_Child;
930
931       ----------------------------------------
932       -- Inspect_Unchecked_Union_Completion --
933       ----------------------------------------
934
935       procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
936          Decl : Node_Id;
937
938       begin
939          Decl := First (Decls);
940          while Present (Decl) loop
941
942             --  We are looking at an incomplete or private type declaration
943             --  with a known_discriminant_part whose full view is an
944             --  Unchecked_Union.
945
946             if Nkind_In (Decl, N_Incomplete_Type_Declaration,
947                                N_Private_Type_Declaration)
948               and then Has_Discriminants (Defining_Identifier (Decl))
949               and then Present (Full_View (Defining_Identifier (Decl)))
950               and then
951                 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
952             then
953                Error_Msg_N
954                  ("completion of discriminated partial view "
955                   & "cannot be an Unchecked_Union",
956                  Full_View (Defining_Identifier (Decl)));
957             end if;
958
959             Next (Decl);
960          end loop;
961       end Inspect_Unchecked_Union_Completion;
962
963       -----------------------------------------
964       -- Install_Parent_Private_Declarations --
965       -----------------------------------------
966
967       procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
968          Inst_Par  : Entity_Id;
969          Gen_Par   : Entity_Id;
970          Inst_Node : Node_Id;
971
972       begin
973          Inst_Par := Inst_Id;
974
975          Gen_Par :=
976            Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
977          while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
978             Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
979
980             if Nkind_In (Inst_Node, N_Package_Instantiation,
981                                     N_Formal_Package_Declaration)
982               and then Nkind (Name (Inst_Node)) = N_Expanded_Name
983             then
984                Inst_Par := Entity (Prefix (Name (Inst_Node)));
985
986                if Present (Renamed_Entity (Inst_Par)) then
987                   Inst_Par := Renamed_Entity (Inst_Par);
988                end if;
989
990                Gen_Par :=
991                  Generic_Parent
992                    (Specification (Unit_Declaration_Node (Inst_Par)));
993
994                --  Install the private declarations and private use clauses
995                --  of a parent instance of the child instance, unless the
996                --  parent instance private declarations have already been
997                --  installed earlier in Analyze_Package_Specification, which
998                --  happens when a generic child is instantiated, and the
999                --  instance is a child of the parent instance.
1000
1001                --  Installing the use clauses of the parent instance twice
1002                --  is both unnecessary and wrong, because it would cause the
1003                --  clauses to be chained to themselves in the use clauses
1004                --  list of the scope stack entry. That in turn would cause
1005                --  an endless loop from End_Use_Clauses upon sccope exit.
1006
1007                --  The parent is now fully visible. It may be a hidden open
1008                --  scope if we are currently compiling some child instance
1009                --  declared within it, but while the current instance is being
1010                --  compiled the parent is immediately visible. In particular
1011                --  its entities must remain visible if a stack save/restore
1012                --  takes place through a call to Rtsfind.
1013
1014                if Present (Gen_Par) then
1015                   if not In_Private_Part (Inst_Par) then
1016                      Install_Private_Declarations (Inst_Par);
1017                      Set_Use (Private_Declarations
1018                                 (Specification
1019                                    (Unit_Declaration_Node (Inst_Par))));
1020                      Set_Is_Hidden_Open_Scope (Inst_Par, False);
1021                   end if;
1022
1023                --  If we've reached the end of the generic instance parents,
1024                --  then finish off by looping through the nongeneric parents
1025                --  and installing their private declarations.
1026
1027                else
1028                   while Present (Inst_Par)
1029                     and then Inst_Par /= Standard_Standard
1030                     and then (not In_Open_Scopes (Inst_Par)
1031                                 or else not In_Private_Part (Inst_Par))
1032                   loop
1033                      Install_Private_Declarations (Inst_Par);
1034                      Set_Use (Private_Declarations
1035                                 (Specification
1036                                    (Unit_Declaration_Node (Inst_Par))));
1037                      Inst_Par := Scope (Inst_Par);
1038                   end loop;
1039
1040                   exit;
1041                end if;
1042
1043             else
1044                exit;
1045             end if;
1046          end loop;
1047       end Install_Parent_Private_Declarations;
1048
1049    --  Start of processing for Analyze_Package_Specification
1050
1051    begin
1052       if Present (Vis_Decls) then
1053          Analyze_Declarations (Vis_Decls);
1054          Analyze_PPCs (Vis_Decls);
1055       end if;
1056
1057       --  Verify that incomplete types have received full declarations
1058
1059       E := First_Entity (Id);
1060       while Present (E) loop
1061          if Ekind (E) = E_Incomplete_Type
1062            and then No (Full_View (E))
1063          then
1064             Error_Msg_N ("no declaration in visible part for incomplete}", E);
1065          end if;
1066
1067          Next_Entity (E);
1068       end loop;
1069
1070       if Is_Remote_Call_Interface (Id)
1071          and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1072       then
1073          Validate_RCI_Declarations (Id);
1074       end if;
1075
1076       --  Save global references in the visible declarations, before
1077       --  installing private declarations of parent unit if there is one,
1078       --  because the privacy status of types defined in the parent will
1079       --  change. This is only relevant for generic child units, but is
1080       --  done in all cases for uniformity.
1081
1082       if Ekind (Id) = E_Generic_Package
1083         and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1084       then
1085          declare
1086             Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1087             Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1088
1089          begin
1090             Set_Private_Declarations (Orig_Spec, Empty_List);
1091             Save_Global_References   (Orig_Decl);
1092             Set_Private_Declarations (Orig_Spec, Save_Priv);
1093          end;
1094       end if;
1095
1096       --  If package is a public child unit, then make the private declarations
1097       --  of the parent visible.
1098
1099       Public_Child := False;
1100
1101       declare
1102          Par       : Entity_Id;
1103          Pack_Decl : Node_Id;
1104          Par_Spec  : Node_Id;
1105
1106       begin
1107          Par := Id;
1108          Par_Spec := Parent_Spec (Parent (N));
1109
1110          --  If the package is formal package of an enclosing generic, it is
1111          --  transformed into a local generic declaration, and compiled to make
1112          --  its spec available. We need to retrieve the original generic to
1113          --  determine whether it is a child unit, and install its parents.
1114
1115          if No (Par_Spec)
1116            and then
1117              Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1118          then
1119             Par := Entity (Name (Original_Node (Parent (N))));
1120             Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1121          end if;
1122
1123          if Present (Par_Spec) then
1124             Generate_Parent_References;
1125
1126             while Scope (Par) /= Standard_Standard
1127               and then Is_Public_Child (Id, Par)
1128               and then In_Open_Scopes (Par)
1129             loop
1130                Public_Child := True;
1131                Par := Scope (Par);
1132                Install_Private_Declarations (Par);
1133                Install_Private_With_Clauses (Par);
1134                Pack_Decl := Unit_Declaration_Node (Par);
1135                Set_Use (Private_Declarations (Specification (Pack_Decl)));
1136             end loop;
1137          end if;
1138       end;
1139
1140       if Is_Compilation_Unit (Id) then
1141          Install_Private_With_Clauses (Id);
1142       else
1143
1144          --  The current compilation unit may include private with_clauses,
1145          --  which are visible in the private part of the current nested
1146          --  package, and have to be installed now. This is not done for
1147          --  nested instantiations, where the private with_clauses of the
1148          --  enclosing unit have no effect once the instantiation info is
1149          --  established and we start analyzing the package declaration.
1150
1151          declare
1152             Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1153          begin
1154             if (Ekind (Comp_Unit) = E_Package
1155                  or else Ekind (Comp_Unit) = E_Generic_Package)
1156               and then not In_Private_Part (Comp_Unit)
1157               and then not In_Instance
1158             then
1159                Install_Private_With_Clauses (Comp_Unit);
1160                Private_With_Clauses_Installed := True;
1161             end if;
1162          end;
1163       end if;
1164
1165       --  If this is a package associated with a generic instance or formal
1166       --  package, then the private declarations of each of the generic's
1167       --  parents must be installed at this point.
1168
1169       if Is_Generic_Instance (Id) then
1170          Install_Parent_Private_Declarations (Id);
1171       end if;
1172
1173       --  Analyze private part if present. The flag In_Private_Part is reset
1174       --  in End_Package_Scope.
1175
1176       L := Last_Entity (Id);
1177
1178       if Present (Priv_Decls) then
1179          Set_In_Private_Part (Id);
1180
1181          --  Upon entering a public child's private part, it may be necessary
1182          --  to declare subprograms that were derived in the package's visible
1183          --  part but not yet made visible.
1184
1185          if Public_Child then
1186             Declare_Inherited_Private_Subprograms (Id);
1187          end if;
1188
1189          Analyze_Declarations (Priv_Decls);
1190          Analyze_PPCs (Priv_Decls);
1191
1192          --  Check the private declarations for incomplete deferred constants
1193
1194          Inspect_Deferred_Constant_Completion (Priv_Decls);
1195
1196          --  The first private entity is the immediate follower of the last
1197          --  visible entity, if there was one.
1198
1199          if Present (L) then
1200             Set_First_Private_Entity (Id, Next_Entity (L));
1201          else
1202             Set_First_Private_Entity (Id, First_Entity (Id));
1203          end if;
1204
1205       --  There may be inherited private subprograms that need to be declared,
1206       --  even in the absence of an explicit private part.  If there are any
1207       --  public declarations in the package and the package is a public child
1208       --  unit, then an implicit private part is assumed.
1209
1210       elsif Present (L) and then Public_Child then
1211          Set_In_Private_Part (Id);
1212          Declare_Inherited_Private_Subprograms (Id);
1213          Set_First_Private_Entity (Id, Next_Entity (L));
1214       end if;
1215
1216       E := First_Entity (Id);
1217       while Present (E) loop
1218
1219          --  Check rule of 3.6(11), which in general requires waiting till all
1220          --  full types have been seen.
1221
1222          if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1223             Check_Aliased_Component_Types (E);
1224          end if;
1225
1226          --  Check preelaborable initialization for full type completing a
1227          --  private type for which pragma Preelaborable_Initialization given.
1228
1229          if Is_Type (E)
1230            and then Must_Have_Preelab_Init (E)
1231            and then not Has_Preelaborable_Initialization (E)
1232          then
1233             Error_Msg_N
1234               ("full view of & does not have preelaborable initialization", E);
1235          end if;
1236
1237          Next_Entity (E);
1238       end loop;
1239
1240       --  Ada 2005 (AI-216): The completion of an incomplete or private type
1241       --  declaration having a known_discriminant_part shall not be an
1242       --  Unchecked_Union type.
1243
1244       if Present (Vis_Decls) then
1245          Inspect_Unchecked_Union_Completion (Vis_Decls);
1246       end if;
1247
1248       if Present (Priv_Decls) then
1249          Inspect_Unchecked_Union_Completion (Priv_Decls);
1250       end if;
1251
1252       if Ekind (Id) = E_Generic_Package
1253         and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1254         and then Present (Priv_Decls)
1255       then
1256          --  Save global references in private declarations, ignoring the
1257          --  visible declarations that were processed earlier.
1258
1259          declare
1260             Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1261             Save_Vis  : constant List_Id := Visible_Declarations (Orig_Spec);
1262             Save_Form : constant List_Id :=
1263                           Generic_Formal_Declarations (Orig_Decl);
1264
1265          begin
1266             Set_Visible_Declarations        (Orig_Spec, Empty_List);
1267             Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1268             Save_Global_References          (Orig_Decl);
1269             Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1270             Set_Visible_Declarations        (Orig_Spec, Save_Vis);
1271          end;
1272       end if;
1273
1274       Process_End_Label (N, 'e', Id);
1275
1276       --  Remove private_with_clauses of enclosing compilation unit, if they
1277       --  were installed.
1278
1279       if Private_With_Clauses_Installed then
1280          Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
1281       end if;
1282
1283       --  For the case of a library level package, we must go through all the
1284       --  entities clearing the indications that the value may be constant and
1285       --  not modified. Why? Because any client of this package may modify
1286       --  these values freely from anywhere. This also applies to any nested
1287       --  packages or generic packages.
1288
1289       --  For now we unconditionally clear constants for packages that are
1290       --  instances of generic packages. The reason is that we do not have the
1291       --  body yet, and we otherwise think things are unreferenced when they
1292       --  are not. This should be fixed sometime (the effect is not terrible,
1293       --  we just lose some warnings, and also some cases of value propagation)
1294       --  ???
1295
1296       if Is_Library_Level_Entity (Id)
1297         or else Is_Generic_Instance (Id)
1298       then
1299          Clear_Constants (Id, First_Entity (Id));
1300          Clear_Constants (Id, First_Private_Entity (Id));
1301       end if;
1302    end Analyze_Package_Specification;
1303
1304    --------------------------------------
1305    -- Analyze_Private_Type_Declaration --
1306    --------------------------------------
1307
1308    procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1309       PF : constant Boolean   := Is_Pure (Enclosing_Lib_Unit_Entity);
1310       Id : constant Entity_Id := Defining_Identifier (N);
1311
1312    begin
1313       Generate_Definition (Id);
1314       Set_Is_Pure         (Id, PF);
1315       Init_Size_Align     (Id);
1316
1317       if (Ekind (Current_Scope) /= E_Package
1318           and then Ekind (Current_Scope) /= E_Generic_Package)
1319         or else In_Private_Part (Current_Scope)
1320       then
1321          Error_Msg_N ("invalid context for private declaration", N);
1322       end if;
1323
1324       New_Private_Type (N, Id, N);
1325       Set_Depends_On_Private (Id);
1326    end Analyze_Private_Type_Declaration;
1327
1328    ----------------------------------
1329    -- Check_Anonymous_Access_Types --
1330    ----------------------------------
1331
1332    procedure Check_Anonymous_Access_Types
1333      (Spec_Id : Entity_Id;
1334       P_Body  : Node_Id)
1335    is
1336       E  : Entity_Id;
1337       IR : Node_Id;
1338
1339    begin
1340       --  Itype references are only needed by gigi, to force elaboration of
1341       --  itypes. In the absence of code generation, they are not needed.
1342
1343       if not Expander_Active then
1344          return;
1345       end if;
1346
1347       E := First_Entity (Spec_Id);
1348       while Present (E) loop
1349          if Ekind (E) = E_Anonymous_Access_Type
1350            and then From_With_Type (E)
1351          then
1352             IR := Make_Itype_Reference (Sloc (P_Body));
1353             Set_Itype (IR, E);
1354
1355             if No (Declarations (P_Body)) then
1356                Set_Declarations (P_Body, New_List (IR));
1357             else
1358                Prepend (IR, Declarations (P_Body));
1359             end if;
1360          end if;
1361
1362          Next_Entity (E);
1363       end loop;
1364    end Check_Anonymous_Access_Types;
1365
1366    -------------------------------------------
1367    -- Declare_Inherited_Private_Subprograms --
1368    -------------------------------------------
1369
1370    procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1371
1372       function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1373       --  Check whether an inherited subprogram is an operation of an
1374       --  untagged derived type.
1375
1376       ---------------------
1377       -- Is_Primitive_Of --
1378       ---------------------
1379
1380       function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1381          Formal : Entity_Id;
1382
1383       begin
1384          --  If the full view is a scalar type, the type is the anonymous
1385          --  base type, but the operation mentions the first subtype, so
1386          --  check the signature againt the base type.
1387
1388          if Base_Type (Etype (S)) = Base_Type (T) then
1389             return True;
1390
1391          else
1392             Formal := First_Formal (S);
1393             while Present (Formal) loop
1394                if Base_Type (Etype (Formal)) = Base_Type (T) then
1395                   return True;
1396                end if;
1397
1398                Next_Formal (Formal);
1399             end loop;
1400
1401             return False;
1402          end if;
1403       end Is_Primitive_Of;
1404
1405       --  Local variables
1406
1407       E           : Entity_Id;
1408       Op_List     : Elist_Id;
1409       Op_Elmt     : Elmt_Id;
1410       Op_Elmt_2   : Elmt_Id;
1411       Prim_Op     : Entity_Id;
1412       New_Op      : Entity_Id := Empty;
1413       Parent_Subp : Entity_Id;
1414       Tag         : Entity_Id;
1415
1416    --  Start of processing for Declare_Inherited_Private_Subprograms
1417
1418    begin
1419       E := First_Entity (Id);
1420       while Present (E) loop
1421
1422          --  If the entity is a nonprivate type extension whose parent
1423          --  type is declared in an open scope, then the type may have
1424          --  inherited operations that now need to be made visible.
1425          --  Ditto if the entity is a formal derived type in a child unit.
1426
1427          if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
1428                or else
1429                  (Nkind (Parent (E)) = N_Private_Extension_Declaration
1430                    and then Is_Generic_Type (E)))
1431            and then In_Open_Scopes (Scope (Etype (E)))
1432            and then E = Base_Type (E)
1433          then
1434             if Is_Tagged_Type (E) then
1435                Op_List := Primitive_Operations (E);
1436                New_Op  := Empty;
1437                Tag     := First_Tag_Component (E);
1438
1439                Op_Elmt := First_Elmt (Op_List);
1440                while Present (Op_Elmt) loop
1441                   Prim_Op := Node (Op_Elmt);
1442
1443                   --  Search primitives that are implicit operations with an
1444                   --  internal name whose parent operation has a normal name.
1445
1446                   if Present (Alias (Prim_Op))
1447                     and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
1448                     and then not Comes_From_Source (Prim_Op)
1449                     and then Is_Internal_Name (Chars (Prim_Op))
1450                     and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1451                   then
1452                      Parent_Subp := Alias (Prim_Op);
1453
1454                      --  Case 1: Check if the type has also an explicit
1455                      --  overriding for this primitive.
1456
1457                      Op_Elmt_2 := Next_Elmt (Op_Elmt);
1458                      while Present (Op_Elmt_2) loop
1459                         if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
1460                           and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
1461                         then
1462                            --  The private inherited operation has been
1463                            --  overridden by an explicit subprogram: replace
1464                            --  the former by the latter.
1465
1466                            New_Op := Node (Op_Elmt_2);
1467                            Replace_Elmt (Op_Elmt, New_Op);
1468                            Remove_Elmt  (Op_List, Op_Elmt_2);
1469                            Set_Is_Overriding_Operation (New_Op);
1470                            Set_Overridden_Operation (New_Op, Parent_Subp);
1471
1472                            --  We don't need to inherit its dispatching slot.
1473                            --  Set_All_DT_Position has previously ensured that
1474                            --  the same slot was assigned to the two primitives
1475
1476                            if Present (Tag)
1477                              and then Present (DTC_Entity (New_Op))
1478                              and then Present (DTC_Entity (Prim_Op))
1479                            then
1480                               pragma Assert (DT_Position (New_Op)
1481                                               = DT_Position (Prim_Op));
1482                               null;
1483                            end if;
1484
1485                            goto Next_Primitive;
1486                         end if;
1487
1488                         Next_Elmt (Op_Elmt_2);
1489                      end loop;
1490
1491                      --   Case 2: We have not found any explicit overriding and
1492                      --   hence we need to declare the operation (i.e., make it
1493                      --   visible).
1494
1495                      Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1496
1497                      --  Inherit the dispatching slot if E is already frozen
1498
1499                      if Is_Frozen (E)
1500                        and then Present (DTC_Entity (Alias (Prim_Op)))
1501                      then
1502                         Set_DTC_Entity_Value (E, New_Op);
1503                         Set_DT_Position (New_Op,
1504                           DT_Position (Alias (Prim_Op)));
1505                      end if;
1506
1507                      pragma Assert
1508                        (Is_Dispatching_Operation (New_Op)
1509                          and then Node (Last_Elmt (Op_List)) = New_Op);
1510
1511                      --  Substitute the new operation for the old one
1512                      --  in the type's primitive operations list. Since
1513                      --  the new operation was also just added to the end
1514                      --  of list, the last element must be removed.
1515
1516                      --  (Question: is there a simpler way of declaring
1517                      --  the operation, say by just replacing the name
1518                      --  of the earlier operation, reentering it in the
1519                      --  in the symbol table (how?), and marking it as
1520                      --  private???)
1521
1522                      Replace_Elmt (Op_Elmt, New_Op);
1523                      Remove_Last_Elmt (Op_List);
1524                   end if;
1525
1526                   <<Next_Primitive>>
1527                   Next_Elmt (Op_Elmt);
1528                end loop;
1529
1530                --  Generate listing showing the contents of the dispatch table
1531
1532                if Debug_Flag_ZZ then
1533                   Write_DT (E);
1534                end if;
1535
1536             else
1537                --   Non-tagged type, scan forward to locate
1538                --   inherited hidden operations.
1539
1540                Prim_Op := Next_Entity (E);
1541                while Present (Prim_Op) loop
1542                   if Is_Subprogram (Prim_Op)
1543                     and then Present (Alias (Prim_Op))
1544                     and then not Comes_From_Source (Prim_Op)
1545                     and then Is_Internal_Name (Chars (Prim_Op))
1546                     and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1547                     and then Is_Primitive_Of (E, Prim_Op)
1548                   then
1549                      Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1550                   end if;
1551
1552                   Next_Entity (Prim_Op);
1553                end loop;
1554             end if;
1555          end if;
1556
1557          Next_Entity (E);
1558       end loop;
1559    end Declare_Inherited_Private_Subprograms;
1560
1561    -----------------------
1562    -- End_Package_Scope --
1563    -----------------------
1564
1565    procedure End_Package_Scope (P : Entity_Id) is
1566    begin
1567       Uninstall_Declarations (P);
1568       Pop_Scope;
1569    end End_Package_Scope;
1570
1571    ---------------------------
1572    -- Exchange_Declarations --
1573    ---------------------------
1574
1575    procedure Exchange_Declarations (Id : Entity_Id) is
1576       Full_Id : constant Entity_Id := Full_View (Id);
1577       H1      : constant Entity_Id := Homonym (Id);
1578       Next1   : constant Entity_Id := Next_Entity (Id);
1579       H2      : Entity_Id;
1580       Next2   : Entity_Id;
1581
1582    begin
1583       --  If missing full declaration for type, nothing to exchange
1584
1585       if No (Full_Id) then
1586          return;
1587       end if;
1588
1589       --  Otherwise complete the exchange, and preserve semantic links
1590
1591       Next2 := Next_Entity (Full_Id);
1592       H2    := Homonym (Full_Id);
1593
1594       --  Reset full declaration pointer to reflect the switched entities
1595       --  and readjust the next entity chains.
1596
1597       Exchange_Entities (Id, Full_Id);
1598
1599       Set_Next_Entity (Id, Next1);
1600       Set_Homonym     (Id, H1);
1601
1602       Set_Full_View   (Full_Id, Id);
1603       Set_Next_Entity (Full_Id, Next2);
1604       Set_Homonym     (Full_Id, H2);
1605    end Exchange_Declarations;
1606
1607    ------------------------------------------
1608    -- Inspect_Deferred_Constant_Completion --
1609    ------------------------------------------
1610
1611    procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
1612       Decl   : Node_Id;
1613
1614    begin
1615       Decl := First (Decls);
1616       while Present (Decl) loop
1617
1618          --  Deferred constant signature
1619
1620          if Nkind (Decl) = N_Object_Declaration
1621            and then Constant_Present (Decl)
1622            and then No (Expression (Decl))
1623
1624             --  No need to check internally generated constants
1625
1626            and then Comes_From_Source (Decl)
1627
1628             --  The constant is not completed. A full object declaration
1629             --  or a pragma Import complete a deferred constant.
1630
1631            and then not Has_Completion (Defining_Identifier (Decl))
1632          then
1633             Error_Msg_N
1634               ("constant declaration requires initialization expression",
1635               Defining_Identifier (Decl));
1636          end if;
1637
1638          Decl := Next (Decl);
1639       end loop;
1640    end Inspect_Deferred_Constant_Completion;
1641
1642    ----------------------------
1643    -- Install_Package_Entity --
1644    ----------------------------
1645
1646    procedure Install_Package_Entity (Id : Entity_Id) is
1647    begin
1648       if not Is_Internal (Id) then
1649          if Debug_Flag_E then
1650             Write_Str ("Install: ");
1651             Write_Name (Chars (Id));
1652             Write_Eol;
1653          end if;
1654
1655          if not Is_Child_Unit (Id) then
1656             Set_Is_Immediately_Visible (Id);
1657          end if;
1658
1659       end if;
1660    end Install_Package_Entity;
1661
1662    ----------------------------------
1663    -- Install_Private_Declarations --
1664    ----------------------------------
1665
1666    procedure Install_Private_Declarations (P : Entity_Id) is
1667       Id        : Entity_Id;
1668       Priv_Elmt : Elmt_Id;
1669       Priv      : Entity_Id;
1670       Full      : Entity_Id;
1671
1672    begin
1673       --  First exchange declarations for private types, so that the
1674       --  full declaration is visible. For each private type, we check
1675       --  its Private_Dependents list and also exchange any subtypes of
1676       --  or derived types from it. Finally, if this is a Taft amendment
1677       --  type, the incomplete declaration is irrelevant, and we want to
1678       --  link the eventual full declaration with the original private
1679       --  one so we also skip the exchange.
1680
1681       Id := First_Entity (P);
1682       while Present (Id) and then Id /= First_Private_Entity (P) loop
1683          if Is_Private_Base_Type (Id)
1684            and then Comes_From_Source (Full_View (Id))
1685            and then Present (Full_View (Id))
1686            and then Scope (Full_View (Id)) = Scope (Id)
1687            and then Ekind (Full_View (Id)) /= E_Incomplete_Type
1688          then
1689             --  If there is a use-type clause on the private type, set the
1690             --  full view accordingly.
1691
1692             Set_In_Use (Full_View (Id), In_Use (Id));
1693             Full := Full_View (Id);
1694
1695             if Is_Private_Base_Type (Full)
1696               and then Has_Private_Declaration (Full)
1697               and then Nkind (Parent (Full)) = N_Full_Type_Declaration
1698               and then In_Open_Scopes (Scope (Etype (Full)))
1699               and then In_Package_Body (Current_Scope)
1700               and then not Is_Private_Type (Etype (Full))
1701             then
1702                --  This is the completion of a private type by a derivation
1703                --  from another private type which is not private anymore. This
1704                --  can only happen in a package nested within a child package,
1705                --  when the parent type is defined in the parent unit. At this
1706                --  point the current type is not private either, and we have to
1707                --  install the underlying full view, which is now visible.
1708                --  Save the current full view as well, so that all views can
1709                --  be restored on exit. It may seem that after compiling the
1710                --  child body there are not environments to restore, but the
1711                --  back-end expects those links to be valid, and freeze nodes
1712                --  depend on them.
1713
1714                if No (Full_View (Full))
1715                  and then Present (Underlying_Full_View (Full))
1716                then
1717                   Set_Full_View (Id, Underlying_Full_View (Full));
1718                   Set_Underlying_Full_View (Id, Full);
1719
1720                   Set_Underlying_Full_View (Full, Empty);
1721                   Set_Is_Frozen (Full_View (Id));
1722                end if;
1723             end if;
1724
1725             Priv_Elmt := First_Elmt (Private_Dependents (Id));
1726
1727             Exchange_Declarations (Id);
1728             Set_Is_Immediately_Visible (Id);
1729
1730             while Present (Priv_Elmt) loop
1731                Priv := Node (Priv_Elmt);
1732
1733                --  Before the exchange, verify that the presence of the
1734                --  Full_View field. It will be empty if the entity
1735                --  has already been installed due to a previous call.
1736
1737                if Present (Full_View (Priv))
1738                  and then Is_Visible_Dependent (Priv)
1739                then
1740
1741                   --  For each subtype that is swapped, we also swap the
1742                   --  reference to it in Private_Dependents, to allow access
1743                   --  to it when we swap them out in End_Package_Scope.
1744
1745                   Replace_Elmt (Priv_Elmt, Full_View (Priv));
1746                   Exchange_Declarations (Priv);
1747                   Set_Is_Immediately_Visible
1748                     (Priv, In_Open_Scopes (Scope (Priv)));
1749                   Set_Is_Potentially_Use_Visible
1750                     (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
1751                end if;
1752
1753                Next_Elmt (Priv_Elmt);
1754             end loop;
1755          end if;
1756
1757          Next_Entity (Id);
1758       end loop;
1759
1760       --  Next make other declarations in the private part visible as well
1761
1762       Id := First_Private_Entity (P);
1763       while Present (Id) loop
1764          Install_Package_Entity (Id);
1765          Set_Is_Hidden (Id, False);
1766          Next_Entity (Id);
1767       end loop;
1768
1769       --  Indicate that the private part is currently visible, so it can be
1770       --  properly reset on exit.
1771
1772       Set_In_Private_Part (P);
1773    end Install_Private_Declarations;
1774
1775    ----------------------------------
1776    -- Install_Visible_Declarations --
1777    ----------------------------------
1778
1779    procedure Install_Visible_Declarations (P : Entity_Id) is
1780       Id          : Entity_Id;
1781       Last_Entity : Entity_Id;
1782
1783    begin
1784       pragma Assert
1785         (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
1786
1787       if Is_Package_Or_Generic_Package (P) then
1788          Last_Entity := First_Private_Entity (P);
1789       else
1790          Last_Entity := Empty;
1791       end if;
1792
1793       Id := First_Entity (P);
1794       while Present (Id) and then Id /= Last_Entity loop
1795          Install_Package_Entity (Id);
1796          Next_Entity (Id);
1797       end loop;
1798    end Install_Visible_Declarations;
1799
1800    --------------------------
1801    -- Is_Private_Base_Type --
1802    --------------------------
1803
1804    function Is_Private_Base_Type (E : Entity_Id) return Boolean is
1805    begin
1806       return Ekind (E) = E_Private_Type
1807         or else Ekind (E) = E_Limited_Private_Type
1808         or else Ekind (E) = E_Record_Type_With_Private;
1809    end Is_Private_Base_Type;
1810
1811    --------------------------
1812    -- Is_Visible_Dependent --
1813    --------------------------
1814
1815    function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
1816    is
1817       S : constant Entity_Id := Scope (Dep);
1818
1819    begin
1820       --  Renamings created for actual types have the visibility of the
1821       --  actual.
1822
1823       if Ekind (S) = E_Package
1824         and then Is_Generic_Instance (S)
1825         and then (Is_Generic_Actual_Type (Dep)
1826                    or else Is_Generic_Actual_Type (Full_View (Dep)))
1827       then
1828          return True;
1829
1830       elsif not (Is_Derived_Type (Dep))
1831         and then Is_Derived_Type (Full_View (Dep))
1832       then
1833          --  When instantiating a package body, the scope stack is empty,
1834          --  so check instead whether the dependent type is defined in
1835          --  the same scope as the instance itself.
1836
1837          return In_Open_Scopes (S)
1838            or else (Is_Generic_Instance (Current_Scope)
1839               and then Scope (Dep) = Scope (Current_Scope));
1840       else
1841          return True;
1842       end if;
1843    end Is_Visible_Dependent;
1844
1845    ----------------------------
1846    -- May_Need_Implicit_Body --
1847    ----------------------------
1848
1849    procedure May_Need_Implicit_Body (E : Entity_Id) is
1850       P     : constant Node_Id := Unit_Declaration_Node (E);
1851       S     : constant Node_Id := Parent (P);
1852       B     : Node_Id;
1853       Decls : List_Id;
1854
1855    begin
1856       if not Has_Completion (E)
1857         and then Nkind (P) = N_Package_Declaration
1858         and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
1859       then
1860          B :=
1861            Make_Package_Body (Sloc (E),
1862              Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
1863                Chars => Chars (E)),
1864              Declarations  => New_List);
1865
1866          if Nkind (S) = N_Package_Specification then
1867             if Present (Private_Declarations (S)) then
1868                Decls := Private_Declarations (S);
1869             else
1870                Decls := Visible_Declarations (S);
1871             end if;
1872          else
1873             Decls := Declarations (S);
1874          end if;
1875
1876          Append (B, Decls);
1877          Analyze (B);
1878       end if;
1879    end May_Need_Implicit_Body;
1880
1881    ----------------------
1882    -- New_Private_Type --
1883    ----------------------
1884
1885    procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
1886    begin
1887       Enter_Name (Id);
1888
1889       if Limited_Present (Def) then
1890          Set_Ekind (Id, E_Limited_Private_Type);
1891       else
1892          Set_Ekind (Id, E_Private_Type);
1893       end if;
1894
1895       Set_Etype              (Id, Id);
1896       Set_Has_Delayed_Freeze (Id);
1897       Set_Is_First_Subtype   (Id);
1898       Init_Size_Align        (Id);
1899
1900       Set_Is_Constrained (Id,
1901         No (Discriminant_Specifications (N))
1902           and then not Unknown_Discriminants_Present (N));
1903
1904       --  Set tagged flag before processing discriminants, to catch
1905       --  illegal usage.
1906
1907       Set_Is_Tagged_Type (Id, Tagged_Present (Def));
1908
1909       Set_Discriminant_Constraint (Id, No_Elist);
1910       Set_Stored_Constraint (Id, No_Elist);
1911
1912       if Present (Discriminant_Specifications (N)) then
1913          Push_Scope (Id);
1914          Process_Discriminants (N);
1915          End_Scope;
1916
1917       elsif Unknown_Discriminants_Present (N) then
1918          Set_Has_Unknown_Discriminants (Id);
1919       end if;
1920
1921       Set_Private_Dependents (Id, New_Elmt_List);
1922
1923       if Tagged_Present (Def) then
1924          Set_Ekind                (Id, E_Record_Type_With_Private);
1925          Make_Class_Wide_Type     (Id);
1926          Set_Primitive_Operations (Id, New_Elmt_List);
1927          Set_Is_Abstract_Type     (Id, Abstract_Present (Def));
1928          Set_Is_Limited_Record    (Id, Limited_Present (Def));
1929          Set_Has_Delayed_Freeze   (Id, True);
1930
1931       elsif Abstract_Present (Def) then
1932          Error_Msg_N ("only a tagged type can be abstract", N);
1933       end if;
1934    end New_Private_Type;
1935
1936    ----------------------------
1937    -- Uninstall_Declarations --
1938    ----------------------------
1939
1940    procedure Uninstall_Declarations (P : Entity_Id) is
1941       Decl      : constant Node_Id := Unit_Declaration_Node (P);
1942       Id        : Entity_Id;
1943       Full      : Entity_Id;
1944       Priv_Elmt : Elmt_Id;
1945       Priv_Sub  : Entity_Id;
1946
1947       procedure Preserve_Full_Attributes (Priv, Full : Entity_Id);
1948       --  Copy to the private declaration the attributes of the full view
1949       --  that need to be available for the partial view also.
1950
1951       function Type_In_Use (T : Entity_Id) return Boolean;
1952       --  Check whether type or base type appear in an active use_type clause
1953
1954       ------------------------------
1955       -- Preserve_Full_Attributes --
1956       ------------------------------
1957
1958       procedure Preserve_Full_Attributes (Priv, Full : Entity_Id) is
1959          Priv_Is_Base_Type : constant Boolean := Priv = Base_Type (Priv);
1960
1961       begin
1962          Set_Size_Info (Priv, (Full));
1963          Set_RM_Size                 (Priv, RM_Size (Full));
1964          Set_Size_Known_At_Compile_Time
1965                                      (Priv, Size_Known_At_Compile_Time (Full));
1966          Set_Is_Volatile             (Priv, Is_Volatile                (Full));
1967          Set_Treat_As_Volatile       (Priv, Treat_As_Volatile          (Full));
1968          Set_Is_Ada_2005_Only        (Priv, Is_Ada_2005_Only           (Full));
1969          Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced    (Full));
1970          Set_Has_Pragma_Unreferenced_Objects
1971                                      (Priv, Has_Pragma_Unreferenced_Objects
1972                                                                        (Full));
1973          if Is_Unchecked_Union (Full) then
1974             Set_Is_Unchecked_Union (Base_Type (Priv));
1975          end if;
1976          --  Why is atomic not copied here ???
1977
1978          if Referenced (Full) then
1979             Set_Referenced (Priv);
1980          end if;
1981
1982          if Priv_Is_Base_Type then
1983             Set_Is_Controlled (Priv, Is_Controlled (Base_Type (Full)));
1984             Set_Finalize_Storage_Only (Priv, Finalize_Storage_Only
1985                                                            (Base_Type (Full)));
1986             Set_Has_Task (Priv, Has_Task (Base_Type (Full)));
1987             Set_Has_Controlled_Component (Priv, Has_Controlled_Component
1988                                                            (Base_Type (Full)));
1989          end if;
1990
1991          Set_Freeze_Node (Priv, Freeze_Node (Full));
1992
1993          if Is_Tagged_Type (Priv)
1994            and then Is_Tagged_Type (Full)
1995            and then not Error_Posted (Full)
1996          then
1997             if Priv_Is_Base_Type then
1998
1999                --  Ada 2005 (AI-345): The full view of a type implementing
2000                --  an interface can be a task type.
2001
2002                --    type T is new I with private;
2003                --  private
2004                --    task type T is new I with ...
2005
2006                if Is_Interface (Etype (Priv))
2007                  and then Is_Concurrent_Type (Base_Type (Full))
2008                then
2009                   --  Protect the frontend against previous errors
2010
2011                   if Present (Corresponding_Record_Type
2012                                (Base_Type (Full)))
2013                   then
2014                      Set_Access_Disp_Table
2015                        (Priv, Access_Disp_Table
2016                                (Corresponding_Record_Type (Base_Type (Full))));
2017
2018                   --  Generic context, or previous errors
2019
2020                   else
2021                      null;
2022                   end if;
2023
2024                else
2025                   Set_Access_Disp_Table
2026                     (Priv, Access_Disp_Table (Base_Type (Full)));
2027                end if;
2028             end if;
2029
2030             if Is_Tagged_Type (Priv) then
2031
2032                --  If the type is tagged, the tag itself must be available
2033                --  on the partial view, for expansion purposes.
2034
2035                Set_First_Entity (Priv, First_Entity (Full));
2036
2037                --  If there are discriminants in the partial view, these remain
2038                --  visible. Otherwise only the tag itself is visible, and there
2039                --  are no nameable components in the partial view.
2040
2041                if No (Last_Entity (Priv)) then
2042                   Set_Last_Entity (Priv, First_Entity (Priv));
2043                end if;
2044             end if;
2045
2046             Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2047          end if;
2048       end Preserve_Full_Attributes;
2049
2050       -----------------
2051       -- Type_In_Use --
2052       -----------------
2053
2054       function Type_In_Use (T : Entity_Id) return Boolean is
2055       begin
2056          return Scope (Base_Type (T)) = P
2057            and then (In_Use (T) or else In_Use (Base_Type (T)));
2058       end Type_In_Use;
2059
2060    --  Start of processing for Uninstall_Declarations
2061
2062    begin
2063       Id := First_Entity (P);
2064       while Present (Id) and then Id /= First_Private_Entity (P) loop
2065          if Debug_Flag_E then
2066             Write_Str ("unlinking visible entity ");
2067             Write_Int (Int (Id));
2068             Write_Eol;
2069          end if;
2070
2071          --  On  exit from the package scope, we must preserve the visibility
2072          --  established by use clauses in the current scope. Two cases:
2073
2074          --  a) If the entity is an operator, it may be a primitive operator of
2075          --  a type for which there is a visible use-type clause.
2076
2077          --  b) for other entities, their use-visibility is determined by a
2078          --  visible use clause for the package itself. For a generic instance,
2079          --  the instantiation of the formals appears in the visible part,
2080          --  but the formals are private and remain so.
2081
2082          if Ekind (Id) = E_Function
2083            and then  Is_Operator_Symbol_Name (Chars (Id))
2084            and then not Is_Hidden (Id)
2085            and then not Error_Posted (Id)
2086          then
2087             Set_Is_Potentially_Use_Visible (Id,
2088               In_Use (P)
2089               or else Type_In_Use (Etype (Id))
2090               or else Type_In_Use (Etype (First_Formal (Id)))
2091               or else (Present (Next_Formal (First_Formal (Id)))
2092                          and then
2093                            Type_In_Use
2094                              (Etype (Next_Formal (First_Formal (Id))))));
2095          else
2096             if In_Use (P) and then not Is_Hidden (Id) then
2097
2098                --  A child unit of a use-visible package remains use-visible
2099                --  only if it is itself a visible child unit. Otherwise it
2100                --  would remain visible in other contexts where P is use-
2101                --  visible, because once compiled it stays in the entity list
2102                --  of its parent unit.
2103
2104                if Is_Child_Unit (Id) then
2105                   Set_Is_Potentially_Use_Visible (Id,
2106                     Is_Visible_Child_Unit (Id));
2107                else
2108                   Set_Is_Potentially_Use_Visible (Id);
2109                end if;
2110
2111             else
2112                Set_Is_Potentially_Use_Visible (Id, False);
2113             end if;
2114          end if;
2115
2116          --  Local entities are not immediately visible outside of the package
2117
2118          Set_Is_Immediately_Visible (Id, False);
2119
2120          --  If this is a private type with a full view (for example a local
2121          --  subtype of a private type declared elsewhere), ensure that the
2122          --  full view is also removed from visibility: it may be exposed when
2123          --  swapping views in an instantiation.
2124
2125          if Is_Type (Id)
2126            and then Present (Full_View (Id))
2127          then
2128             Set_Is_Immediately_Visible (Full_View (Id), False);
2129          end if;
2130
2131          if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2132             Check_Abstract_Overriding (Id);
2133             Check_Conventions (Id);
2134          end if;
2135
2136          if (Ekind (Id) = E_Private_Type
2137                or else Ekind (Id) = E_Limited_Private_Type)
2138            and then No (Full_View (Id))
2139            and then not Is_Generic_Type (Id)
2140            and then not Is_Derived_Type (Id)
2141          then
2142             Error_Msg_N ("missing full declaration for private type&", Id);
2143
2144          elsif Ekind (Id) = E_Record_Type_With_Private
2145            and then not Is_Generic_Type (Id)
2146            and then No (Full_View (Id))
2147          then
2148             if Nkind (Parent (Id)) = N_Private_Type_Declaration then
2149                Error_Msg_N ("missing full declaration for private type&", Id);
2150             else
2151                Error_Msg_N
2152                  ("missing full declaration for private extension", Id);
2153             end if;
2154
2155          elsif Ekind (Id) = E_Constant
2156            and then No (Constant_Value (Id))
2157            and then No (Full_View (Id))
2158            and then not Is_Imported (Id)
2159            and then (Nkind (Parent (Id)) /= N_Object_Declaration
2160                       or else not No_Initialization (Parent (Id)))
2161          then
2162             if not Has_Private_Declaration (Etype (Id)) then
2163
2164                --  We assume that the user did not not intend a deferred
2165                --  constant declaration, and the expression is just missing.
2166
2167                Error_Msg_N
2168                  ("constant declaration requires initialization expression",
2169                    Parent (Id));
2170
2171                if Is_Limited_Type (Etype (Id)) then
2172                   Error_Msg_N
2173                     ("\if variable intended, remove CONSTANT from declaration",
2174                     Parent (Id));
2175                end if;
2176
2177             else
2178                Error_Msg_N
2179                   ("missing full declaration for deferred constant (RM 7.4)",
2180                      Id);
2181
2182                if Is_Limited_Type (Etype (Id)) then
2183                   Error_Msg_N
2184                     ("\if variable intended, remove CONSTANT from declaration",
2185                     Parent (Id));
2186                end if;
2187             end if;
2188          end if;
2189
2190          Next_Entity (Id);
2191       end loop;
2192
2193       --  If the specification was installed as the parent of a public child
2194       --  unit, the private declarations were not installed, and there is
2195       --  nothing to do.
2196
2197       if not In_Private_Part (P) then
2198          return;
2199       else
2200          Set_In_Private_Part (P, False);
2201       end if;
2202
2203       --  Make private entities invisible and exchange full and private
2204       --  declarations for private types. Id is now the first private
2205       --  entity in the package.
2206
2207       while Present (Id) loop
2208          if Debug_Flag_E then
2209             Write_Str ("unlinking private entity ");
2210             Write_Int (Int (Id));
2211             Write_Eol;
2212          end if;
2213
2214          if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2215             Check_Abstract_Overriding (Id);
2216             Check_Conventions (Id);
2217          end if;
2218
2219          Set_Is_Immediately_Visible (Id, False);
2220
2221          if Is_Private_Base_Type (Id)
2222            and then Present (Full_View (Id))
2223          then
2224             Full := Full_View (Id);
2225
2226             --  If the partial view is not declared in the visible part
2227             --  of the package (as is the case when it is a type derived
2228             --  from some other private type in the private part of the
2229             --  current package), no exchange takes place.
2230
2231             if No (Parent (Id))
2232               or else List_Containing (Parent (Id))
2233                 /= Visible_Declarations (Specification (Decl))
2234             then
2235                goto Next_Id;
2236             end if;
2237
2238             --  The entry in the private part points to the full declaration,
2239             --  which is currently visible. Exchange them so only the private
2240             --  type declaration remains accessible, and link private and
2241             --  full declaration in the opposite direction. Before the actual
2242             --  exchange, we copy back attributes of the full view that
2243             --  must be available to the partial view too.
2244
2245             Preserve_Full_Attributes (Id, Full);
2246
2247             Set_Is_Potentially_Use_Visible (Id, In_Use (P));
2248
2249             if  Is_Indefinite_Subtype (Full)
2250               and then not Is_Indefinite_Subtype (Id)
2251             then
2252                Error_Msg_N
2253                  ("full view of type must be definite subtype", Full);
2254             end if;
2255
2256             Priv_Elmt := First_Elmt (Private_Dependents (Id));
2257
2258             --  Swap out the subtypes and derived types of Id that were
2259             --  compiled in this scope, or installed previously by
2260             --  Install_Private_Declarations.
2261             --  Before we do the swap, we verify the presence of the
2262             --  Full_View field which may be empty due to a swap by
2263             --  a previous call to End_Package_Scope (e.g. from the
2264             --  freezing mechanism).
2265
2266             while Present (Priv_Elmt) loop
2267                Priv_Sub := Node (Priv_Elmt);
2268
2269                if Present (Full_View (Priv_Sub)) then
2270
2271                   if Scope (Priv_Sub) = P
2272                      or else not In_Open_Scopes (Scope (Priv_Sub))
2273                   then
2274                      Set_Is_Immediately_Visible (Priv_Sub, False);
2275                   end if;
2276
2277                   if Is_Visible_Dependent (Priv_Sub) then
2278                      Preserve_Full_Attributes
2279                        (Priv_Sub, Full_View (Priv_Sub));
2280                      Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub));
2281                      Exchange_Declarations (Priv_Sub);
2282                   end if;
2283                end if;
2284
2285                Next_Elmt (Priv_Elmt);
2286             end loop;
2287
2288             --  Now restore the type itself to its private view
2289
2290             Exchange_Declarations (Id);
2291
2292             --  If we have installed an underlying full view for a type
2293             --  derived from a private type in a child unit, restore the
2294             --  proper views of private and full view. See corresponding
2295             --  code in Install_Private_Declarations.
2296             --  After the exchange, Full denotes the private type in the
2297             --  visible part of the package.
2298
2299             if Is_Private_Base_Type (Full)
2300               and then Present (Full_View (Full))
2301               and then Present (Underlying_Full_View (Full))
2302               and then In_Package_Body (Current_Scope)
2303             then
2304                Set_Full_View (Full, Underlying_Full_View (Full));
2305                Set_Underlying_Full_View (Full, Empty);
2306             end if;
2307
2308          elsif Ekind (Id) = E_Incomplete_Type
2309            and then No (Full_View (Id))
2310          then
2311             --  Mark Taft amendment types
2312
2313             Set_Has_Completion_In_Body (Id);
2314
2315          elsif not Is_Child_Unit (Id)
2316            and then (not Is_Private_Type (Id)
2317                       or else No (Full_View (Id)))
2318          then
2319             Set_Is_Hidden (Id);
2320             Set_Is_Potentially_Use_Visible (Id, False);
2321          end if;
2322
2323          <<Next_Id>>
2324             Next_Entity (Id);
2325       end loop;
2326    end Uninstall_Declarations;
2327
2328    ------------------------
2329    -- Unit_Requires_Body --
2330    ------------------------
2331
2332    function Unit_Requires_Body (P : Entity_Id) return Boolean is
2333       E : Entity_Id;
2334
2335    begin
2336       --  Imported entity never requires body. Right now, only
2337       --  subprograms can be imported, but perhaps in the future
2338       --  we will allow import of packages.
2339
2340       if Is_Imported (P) then
2341          return False;
2342
2343       --  Body required if library package with pragma Elaborate_Body
2344
2345       elsif Has_Pragma_Elaborate_Body (P) then
2346          return True;
2347
2348       --  Body required if subprogram
2349
2350       elsif Is_Subprogram (P) or else Is_Generic_Subprogram (P) then
2351          return True;
2352
2353       --  Treat a block as requiring a body
2354
2355       elsif Ekind (P) = E_Block then
2356          return True;
2357
2358       elsif Ekind (P) = E_Package
2359         and then Nkind (Parent (P)) = N_Package_Specification
2360         and then Present (Generic_Parent (Parent (P)))
2361       then
2362          declare
2363             G_P : constant Entity_Id := Generic_Parent (Parent (P));
2364          begin
2365             if Has_Pragma_Elaborate_Body (G_P) then
2366                return True;
2367             end if;
2368          end;
2369       end if;
2370
2371       --  Otherwise search entity chain for entity requiring completion
2372
2373       E := First_Entity (P);
2374       while Present (E) loop
2375
2376          --  Always ignore child units. Child units get added to the entity
2377          --  list of a parent unit, but are not original entities of the
2378          --  parent, and so do not affect whether the parent needs a body.
2379
2380          if Is_Child_Unit (E) then
2381             null;
2382
2383          --  Ignore formal packages and their renamings
2384
2385          elsif Ekind (E) = E_Package
2386            and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
2387                                                 N_Formal_Package_Declaration
2388          then
2389             null;
2390
2391          --  Otherwise test to see if entity requires a completion.
2392          --  Note that subprogram entities whose declaration does not come
2393          --  from source are ignored here on the basis that we assume the
2394          --  expander will provide an implicit completion at some point.
2395
2396          elsif (Is_Overloadable (E)
2397                and then Ekind (E) /= E_Enumeration_Literal
2398                and then Ekind (E) /= E_Operator
2399                and then not Is_Abstract_Subprogram (E)
2400                and then not Has_Completion (E)
2401                and then Comes_From_Source (Parent (E)))
2402
2403            or else
2404              (Ekind (E) = E_Package
2405                and then E /= P
2406                and then not Has_Completion (E)
2407                and then Unit_Requires_Body (E))
2408
2409            or else
2410              (Ekind (E) = E_Incomplete_Type and then No (Full_View (E)))
2411
2412            or else
2413             ((Ekind (E) = E_Task_Type or else
2414               Ekind (E) = E_Protected_Type)
2415                and then not Has_Completion (E))
2416
2417            or else
2418              (Ekind (E) = E_Generic_Package and then E /= P
2419                and then not Has_Completion (E)
2420                and then Unit_Requires_Body (E))
2421
2422            or else
2423              (Is_Generic_Subprogram (E)
2424                and then not Has_Completion (E))
2425
2426          then
2427             return True;
2428
2429          --  Entity that does not require completion
2430
2431          else
2432             null;
2433          end if;
2434
2435          Next_Entity (E);
2436       end loop;
2437
2438       return False;
2439    end Unit_Requires_Body;
2440
2441 end Sem_Ch7;