OSDN Git Service

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