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