OSDN Git Service

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