1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 -- This package contains the routines to process package specifications and
27 -- bodies. The most important semantic aspects of package processing are the
28 -- handling of private and full declarations, and the construction of
29 -- dispatch tables for tagged types.
31 with Atree; use Atree;
32 with Debug; use Debug;
33 with Einfo; use Einfo;
34 with Elists; use Elists;
35 with Errout; use Errout;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Dist; use Exp_Dist;
38 with Exp_Dbug; use Exp_Dbug;
40 with Lib.Xref; use Lib.Xref;
41 with Namet; use Namet;
42 with Nmake; use Nmake;
43 with Nlists; use Nlists;
45 with Output; use Output;
47 with Sem_Cat; use Sem_Cat;
48 with Sem_Ch3; use Sem_Ch3;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Ch10; use Sem_Ch10;
52 with Sem_Ch12; use Sem_Ch12;
53 with Sem_Disp; use Sem_Disp;
54 with Sem_Prag; use Sem_Prag;
55 with Sem_Util; use Sem_Util;
56 with Sem_Warn; use Sem_Warn;
57 with Snames; use Snames;
58 with Stand; use Stand;
59 with Sinfo; use Sinfo;
60 with Sinput; use Sinput;
62 with Uintp; use Uintp;
64 package body Sem_Ch7 is
66 -----------------------------------
67 -- Handling private declarations --
68 -----------------------------------
70 -- The principle that each entity has a single defining occurrence clashes
71 -- with the presence of two separate definitions for private types: the
72 -- first is the private type declaration, and the second is the full type
73 -- declaration. It is important that all references to the type point to
74 -- the same defining occurrence, namely the first one. To enforce the two
75 -- separate views of the entity, the corresponding information is swapped
76 -- between the two declarations. Outside of the package, the defining
77 -- occurrence only contains the private declaration information, while in
78 -- the private part and the body of the package the defining occurrence
79 -- contains the full declaration. To simplify the swap, the defining
80 -- occurrence that currently holds the private declaration points to the
81 -- full declaration. During semantic processing the defining occurrence
82 -- also points to a list of private dependents, that is to say access types
83 -- or composite types whose designated types or component types are
84 -- subtypes or derived types of the private type in question. After the
85 -- full declaration has been seen, the private dependents are updated to
86 -- indicate that they have full definitions.
88 -----------------------
89 -- Local Subprograms --
90 -----------------------
92 procedure Check_Anonymous_Access_Types
95 -- If the spec of a package has a limited_with_clause, it may declare
96 -- anonymous access types whose designated type is a limited view, such an
97 -- anonymous access return type for a function. This access type cannot be
98 -- elaborated in the spec itself, but it may need an itype reference if it
99 -- is used within a nested scope. In that case the itype reference is
100 -- created at the beginning of the corresponding package body and inserted
101 -- before other body declarations.
103 procedure Inspect_Deferred_Constant_Completion (Decls : List_Id);
104 -- Examines the deferred constants in the private part of the package
105 -- specification, or in a package body. Emits the error message
106 -- "constant declaration requires initialization expression" if not
107 -- completed by an Import pragma.
109 procedure Install_Package_Entity (Id : Entity_Id);
110 -- Supporting procedure for Install_{Visible,Private}_Declarations.
111 -- Places one entity on its visibility chain, and recurses on the visible
112 -- part if the entity is an inner package.
114 function Is_Private_Base_Type (E : Entity_Id) return Boolean;
115 -- True for a private type that is not a subtype
117 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean;
118 -- If the private dependent is a private type whose full view is derived
119 -- from the parent type, its full properties are revealed only if we are in
120 -- the immediate scope of the private dependent. Should this predicate be
121 -- tightened further???
123 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id);
124 -- Called upon entering the private part of a public child package and the
125 -- body of a nested package, to potentially declare certain inherited
126 -- subprograms that were inherited by types in the visible part, but whose
127 -- declaration was deferred because the parent operation was private and
128 -- not visible at that point. These subprograms are located by traversing
129 -- the visible part declarations looking for non-private type extensions
130 -- and then examining each of the primitive operations of such types to
131 -- find those that were inherited but declared with a special internal
132 -- name. Each such operation is now declared as an operation with a normal
133 -- name (using the name of the parent operation) and replaces the previous
134 -- implicit operation in the primitive operations list of the type. If the
135 -- inherited private operation has been overridden, then it's replaced by
136 -- the overriding operation.
138 --------------------------
139 -- Analyze_Package_Body --
140 --------------------------
142 procedure Analyze_Package_Body (N : Node_Id) is
143 Loc : constant Source_Ptr := Sloc (N);
147 Last_Spec_Entity : Entity_Id;
151 procedure Install_Composite_Operations (P : Entity_Id);
152 -- Composite types declared in the current scope may depend on
153 -- types that were private at the point of declaration, and whose
154 -- full view is now in scope. Indicate that the corresponding
155 -- operations on the composite type are available.
157 ----------------------------------
158 -- Install_Composite_Operations --
159 ----------------------------------
161 procedure Install_Composite_Operations (P : Entity_Id) is
165 Id := First_Entity (P);
166 while Present (Id) loop
168 and then (Is_Limited_Composite (Id)
169 or else Is_Private_Composite (Id))
170 and then No (Private_Component (Id))
172 Set_Is_Limited_Composite (Id, False);
173 Set_Is_Private_Composite (Id, False);
178 end Install_Composite_Operations;
180 -- Start of processing for Analyze_Package_Body
183 -- Find corresponding package specification, and establish the
184 -- current scope. The visible defining entity for the package is the
185 -- defining occurrence in the spec. On exit from the package body, all
186 -- body declarations are attached to the defining entity for the body,
187 -- but the later is never used for name resolution. In this fashion
188 -- there is only one visible entity that denotes the package.
191 Write_Str ("==== Compiling package body ");
192 Write_Name (Chars (Defining_Entity (N)));
193 Write_Str (" from ");
194 Write_Location (Loc);
198 -- Set Body_Id. Note that this Will be reset to point to the
199 -- generic copy later on in the generic case.
201 Body_Id := Defining_Entity (N);
203 if Present (Corresponding_Spec (N)) then
205 -- Body is body of package instantiation. Corresponding spec
206 -- has already been set.
208 Spec_Id := Corresponding_Spec (N);
209 Pack_Decl := Unit_Declaration_Node (Spec_Id);
212 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
215 and then Is_Package_Or_Generic_Package (Spec_Id)
217 Pack_Decl := Unit_Declaration_Node (Spec_Id);
219 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
220 Error_Msg_N ("cannot supply body for package renaming", N);
223 elsif Present (Corresponding_Body (Pack_Decl)) then
224 Error_Msg_N ("redefinition of package body", N);
229 Error_Msg_N ("missing specification for package body", N);
233 if Is_Package_Or_Generic_Package (Spec_Id)
235 (Scope (Spec_Id) = Standard_Standard
236 or else Is_Child_Unit (Spec_Id))
237 and then not Unit_Requires_Body (Spec_Id)
239 if Ada_Version = Ada_83 then
241 ("optional package body (not allowed in Ada 95)?", N);
244 ("spec of this package does not allow a body", N);
249 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
250 Style.Check_Identifier (Body_Id, Spec_Id);
252 if Is_Child_Unit (Spec_Id) then
253 if Nkind (Parent (N)) /= N_Compilation_Unit then
255 ("body of child unit& cannot be an inner package", N, Spec_Id);
258 Set_Is_Child_Unit (Body_Id);
261 -- Generic package case
263 if Ekind (Spec_Id) = E_Generic_Package then
265 -- Disable expansion and perform semantic analysis on copy.
266 -- The unannotated body will be used in all instantiations.
268 Body_Id := Defining_Entity (N);
269 Set_Ekind (Body_Id, E_Package_Body);
270 Set_Scope (Body_Id, Scope (Spec_Id));
271 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
272 Set_Body_Entity (Spec_Id, Body_Id);
273 Set_Spec_Entity (Body_Id, Spec_Id);
275 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
278 -- Update Body_Id to point to the copied node for the remainder
279 -- of the processing.
281 Body_Id := Defining_Entity (N);
285 -- The Body_Id is that of the copied node in the generic case, the
286 -- current node otherwise. Note that N was rewritten above, so we
287 -- must be sure to get the latest Body_Id value.
289 Set_Ekind (Body_Id, E_Package_Body);
290 Set_Body_Entity (Spec_Id, Body_Id);
291 Set_Spec_Entity (Body_Id, Spec_Id);
293 -- Defining name for the package body is not a visible entity: Only
294 -- the defining name for the declaration is visible.
296 Set_Etype (Body_Id, Standard_Void_Type);
297 Set_Scope (Body_Id, Scope (Spec_Id));
298 Set_Corresponding_Spec (N, Spec_Id);
299 Set_Corresponding_Body (Pack_Decl, Body_Id);
301 -- The body entity is not used for semantics or code generation, but
302 -- it is attached to the entity list of the enclosing scope to simplify
303 -- the listing of back-annotations for the types it main contain.
305 if Scope (Spec_Id) /= Standard_Standard then
306 Append_Entity (Body_Id, Scope (Spec_Id));
309 -- Indicate that we are currently compiling the body of the package
311 Set_In_Package_Body (Spec_Id);
312 Set_Has_Completion (Spec_Id);
313 Last_Spec_Entity := Last_Entity (Spec_Id);
315 Push_Scope (Spec_Id);
317 Set_Categorization_From_Pragmas (N);
319 Install_Visible_Declarations (Spec_Id);
320 Install_Private_Declarations (Spec_Id);
321 Install_Private_With_Clauses (Spec_Id);
322 Install_Composite_Operations (Spec_Id);
324 Check_Anonymous_Access_Types (Spec_Id, N);
326 if Ekind (Spec_Id) = E_Generic_Package then
327 Set_Use (Generic_Formal_Declarations (Pack_Decl));
330 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
331 Set_Use (Private_Declarations (Specification (Pack_Decl)));
333 -- This is a nested package, so it may be necessary to declare certain
334 -- inherited subprograms that are not yet visible because the parent
335 -- type's subprograms are now visible.
337 if Ekind (Scope (Spec_Id)) = E_Package
338 and then Scope (Spec_Id) /= Standard_Standard
340 Declare_Inherited_Private_Subprograms (Spec_Id);
343 if Present (Declarations (N)) then
344 Analyze_Declarations (Declarations (N));
345 Inspect_Deferred_Constant_Completion (Declarations (N));
348 -- Analyze_Declarations has caused freezing of all types; now generate
349 -- bodies for RACW primitives and stream attributes, if any.
351 if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then
353 -- Attach subprogram bodies to support RACWs declared in spec
355 Append_RACW_Bodies (Declarations (N), Spec_Id);
356 Analyze_List (Declarations (N));
359 HSS := Handled_Statement_Sequence (N);
361 if Present (HSS) then
362 Process_End_Label (HSS, 't', Spec_Id);
365 -- Check that elaboration code in a preelaborable package body is
366 -- empty other than null statements and labels (RM 10.2.1(6)).
368 Validate_Null_Statement_Sequence (N);
371 Validate_Categorization_Dependency (N, Spec_Id);
372 Check_Completion (Body_Id);
374 -- Generate start of body reference. Note that we do this fairly late,
375 -- because the call will use In_Extended_Main_Source_Unit as a check,
376 -- and we want to make sure that Corresponding_Stub links are set
378 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
380 -- For a generic package, collect global references and mark them on
381 -- the original body so that they are not resolved again at the point
384 if Ekind (Spec_Id) /= E_Package then
385 Save_Global_References (Original_Node (N));
389 -- The entities of the package body have so far been chained onto the
390 -- declaration chain for the spec. That's been fine while we were in the
391 -- body, since we wanted them to be visible, but now that we are leaving
392 -- the package body, they are no longer visible, so we remove them from
393 -- the entity chain of the package spec entity, and copy them to the
394 -- entity chain of the package body entity, where they will never again
397 if Present (Last_Spec_Entity) then
398 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
399 Set_Next_Entity (Last_Spec_Entity, Empty);
400 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
401 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
404 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
405 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
406 Set_First_Entity (Spec_Id, Empty);
407 Set_Last_Entity (Spec_Id, Empty);
410 End_Package_Scope (Spec_Id);
412 -- All entities declared in body are not visible
418 E := First_Entity (Body_Id);
419 while Present (E) loop
420 Set_Is_Immediately_Visible (E, False);
421 Set_Is_Potentially_Use_Visible (E, False);
424 -- Child units may appear on the entity list (for example if
425 -- they appear in the context of a subunit) but they are not
428 if not Is_Child_Unit (E) then
429 Set_Is_Package_Body_Entity (E);
436 Check_References (Body_Id);
438 -- For a generic unit, check that the formal parameters are referenced,
439 -- and that local variables are used, as for regular packages.
441 if Ekind (Spec_Id) = E_Generic_Package then
442 Check_References (Spec_Id);
445 -- The processing so far has made all entities of the package body
446 -- public (i.e. externally visible to the linker). This is in general
447 -- necessary, since inlined or generic bodies, for which code is
448 -- generated in other units, may need to see these entities. The
449 -- following loop runs backwards from the end of the entities of the
450 -- package body making these entities invisible until we reach a
451 -- referencer, i.e. a declaration that could reference a previous
452 -- declaration, a generic body or an inlined body, or a stub (which
453 -- may contain either of these). This is of course an approximation,
454 -- but it is conservative and definitely correct.
456 -- We only do this at the outer (library) level non-generic packages.
457 -- The reason is simply to cut down on the number of external symbols
458 -- generated, so this is simply an optimization of the efficiency
459 -- of the compilation process. It has no other effect.
461 if (Scope (Spec_Id) = Standard_Standard or else Is_Child_Unit (Spec_Id))
462 and then not Is_Generic_Unit (Spec_Id)
463 and then Present (Declarations (N))
465 Make_Non_Public_Where_Possible : declare
467 function Has_Referencer
471 -- Traverse the given list of declarations in reverse order.
472 -- Return True as soon as a referencer is reached. Return
473 -- False if none is found. The Outer parameter is True for
474 -- the outer level call, and False for inner level calls for
475 -- nested packages. If Outer is True, then any entities up
476 -- to the point of hitting a referencer get their Is_Public
477 -- flag cleared, so that the entities will be treated as
478 -- static entities in the C sense, and need not have fully
479 -- qualified names. For inner levels, we need all names to
480 -- be fully qualified to deal with the same name appearing
481 -- in parallel packages (right now this is tied to their
488 function Has_Referencer
504 while Present (D) loop
507 if K in N_Body_Stub then
510 elsif K = N_Subprogram_Body then
511 if Acts_As_Spec (D) then
512 E := Defining_Entity (D);
514 -- An inlined body acts as a referencer. Note also
515 -- that we never reset Is_Public for an inlined
516 -- subprogram. Gigi requires Is_Public to be set.
518 -- Note that we test Has_Pragma_Inline here rather
519 -- than Is_Inlined. We are compiling this for a
520 -- client, and it is the client who will decide
521 -- if actual inlining should occur, so we need to
522 -- assume that the procedure could be inlined for
523 -- the purpose of accessing global entities.
525 if Has_Pragma_Inline (E) then
528 Set_Is_Public (E, False);
532 E := Corresponding_Spec (D);
535 and then (Is_Generic_Unit (E)
536 or else Has_Pragma_Inline (E)
537 or else Is_Inlined (E))
543 -- Processing for package bodies
545 elsif K = N_Package_Body
546 and then Present (Corresponding_Spec (D))
548 E := Corresponding_Spec (D);
550 -- Generic package body is a referencer. It would
551 -- seem that we only have to consider generics that
552 -- can be exported, i.e. where the corresponding spec
553 -- is the spec of the current package, but because of
554 -- nested instantiations, a fully private generic
555 -- body may export other private body entities.
557 if Is_Generic_Unit (E) then
560 -- For non-generic package body, recurse into body
561 -- unless this is an instance, we ignore instances
562 -- since they cannot have references that affect
565 elsif not Is_Generic_Instance (E) then
567 (Declarations (D), Outer => False)
573 -- Processing for package specs, recurse into declarations.
574 -- Again we skip this for the case of generic instances.
576 elsif K = N_Package_Declaration then
577 S := Specification (D);
579 if not Is_Generic_Unit (Defining_Entity (S)) then
581 (Private_Declarations (S), Outer => False)
585 (Visible_Declarations (S), Outer => False)
591 -- Objects and exceptions need not be public if we have
592 -- not encountered a referencer so far. We only reset
593 -- the flag for outer level entities that are not
594 -- imported/exported, and which have no interface name.
596 elsif Nkind_In (K, N_Object_Declaration,
597 N_Exception_Declaration,
598 N_Subprogram_Declaration)
600 E := Defining_Entity (D);
603 and then not Is_Imported (E)
604 and then not Is_Exported (E)
605 and then No (Interface_Name (E))
607 Set_Is_Public (E, False);
617 -- Start of processing for Make_Non_Public_Where_Possible
622 pragma Warnings (Off, Discard);
625 Discard := Has_Referencer (Declarations (N), Outer => True);
627 end Make_Non_Public_Where_Possible;
630 -- If expander is not active, then here is where we turn off the
631 -- In_Package_Body flag, otherwise it is turned off at the end of
632 -- the corresponding expansion routine. If this is an instance body,
633 -- we need to qualify names of local entities, because the body may
634 -- have been compiled as a preliminary to another instantiation.
636 if not Expander_Active then
637 Set_In_Package_Body (Spec_Id, False);
639 if Is_Generic_Instance (Spec_Id)
640 and then Operating_Mode = Generate_Code
642 Qualify_Entity_Names (N);
645 end Analyze_Package_Body;
647 ---------------------------------
648 -- Analyze_Package_Declaration --
649 ---------------------------------
651 procedure Analyze_Package_Declaration (N : Node_Id) is
652 Id : constant Node_Id := Defining_Entity (N);
655 -- True when in the context of a declared pure library unit
657 Body_Required : Boolean;
658 -- True when this package declaration requires a corresponding body
661 -- True when this package declaration is not a nested declaration
664 -- Ada 2005 (AI-217): Check if the package has been erroneously named
665 -- in a limited-with clause of its own context. In this case the error
666 -- has been previously notified by Analyze_Context.
668 -- limited with Pkg; -- ERROR
669 -- package Pkg is ...
671 if From_With_Type (Id) then
675 Generate_Definition (Id);
677 Set_Ekind (Id, E_Package);
678 Set_Etype (Id, Standard_Void_Type);
682 PF := Is_Pure (Enclosing_Lib_Unit_Entity);
683 Set_Is_Pure (Id, PF);
685 Set_Categorization_From_Pragmas (N);
688 Write_Str ("==== Compiling package spec ");
689 Write_Name (Chars (Id));
690 Write_Str (" from ");
691 Write_Location (Sloc (N));
695 Analyze (Specification (N));
696 Validate_Categorization_Dependency (N, Id);
698 Body_Required := Unit_Requires_Body (Id);
700 -- When this spec does not require an explicit body, we know that
701 -- there are no entities requiring completion in the language sense;
702 -- we call Check_Completion here only to ensure that any nested package
703 -- declaration that requires an implicit body gets one. (In the case
704 -- where a body is required, Check_Completion is called at the end of
705 -- the body's declarative part.)
707 if not Body_Required then
711 Comp_Unit := Nkind (Parent (N)) = N_Compilation_Unit;
714 -- Set Body_Required indication on the compilation unit node, and
715 -- determine whether elaboration warnings may be meaningful on it.
717 Set_Body_Required (Parent (N), Body_Required);
719 if not Body_Required then
720 Set_Suppress_Elaboration_Warnings (Id);
725 End_Package_Scope (Id);
727 -- For the declaration of a library unit that is a remote types package,
728 -- check legality rules regarding availability of stream attributes for
729 -- types that contain non-remote access values. This subprogram performs
730 -- visibility tests that rely on the fact that we have exited the scope
734 Validate_RT_RAT_Component (N);
736 end Analyze_Package_Declaration;
738 -----------------------------------
739 -- Analyze_Package_Specification --
740 -----------------------------------
742 -- Note that this code is shared for the analysis of generic package
743 -- specs (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
745 procedure Analyze_Package_Specification (N : Node_Id) is
746 Id : constant Entity_Id := Defining_Entity (N);
747 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
748 Vis_Decls : constant List_Id := Visible_Declarations (N);
749 Priv_Decls : constant List_Id := Private_Declarations (N);
752 Public_Child : Boolean;
754 Private_With_Clauses_Installed : Boolean := False;
755 -- In Ada 2005, private with_clauses are visible in the private part
756 -- of a nested package, even if it appears in the public part of the
757 -- enclosing package. This requires a separate step to install these
758 -- private_with_clauses, and remove them at the end of the nested
761 procedure Analyze_PPCs (Decls : List_Id);
762 -- Given a list of declarations, go through looking for subprogram
763 -- specs, and for each one found, analyze any pre/postconditions that
764 -- are chained to the spec. This is the implementation of the late
765 -- visibility analysis for preconditions and postconditions in specs.
767 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
768 -- Clears constant indications (Never_Set_In_Source, Constant_Value,
769 -- and Is_True_Constant) on all variables that are entities of Id,
770 -- and on the chain whose first element is FE. A recursive call is
771 -- made for all packages and generic packages.
773 procedure Generate_Parent_References;
774 -- For a child unit, generate references to parent units, for
775 -- GPS navigation purposes.
777 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
778 -- Child and Unit are entities of compilation units. True if Child
779 -- is a public child of Parent as defined in 10.1.1
781 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
782 -- Detects all incomplete or private type declarations having a known
783 -- discriminant part that are completed by an Unchecked_Union. Emits
784 -- the error message "Unchecked_Union may not complete discriminated
787 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
788 -- Given the package entity of a generic package instantiation or
789 -- formal package whose corresponding generic is a child unit, installs
790 -- the private declarations of each of the child unit's parents.
791 -- This has to be done at the point of entering the instance package's
792 -- private part rather than being done in Sem_Ch12.Install_Parent
793 -- (which is where the parents' visible declarations are installed).
799 procedure Analyze_PPCs (Decls : List_Id) is
806 Decl := First (Decls);
807 while Present (Decl) loop
808 if Nkind (Original_Node (Decl)) = N_Subprogram_Declaration then
809 Spec := Specification (Original_Node (Decl));
810 Sent := Defining_Unit_Name (Spec);
811 Prag := Spec_PPC_List (Sent);
812 while Present (Prag) loop
813 Analyze_PPC_In_Decl_Part (Prag, Sent);
814 Prag := Next_Pragma (Prag);
822 ---------------------
823 -- Clear_Constants --
824 ---------------------
826 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
830 -- Ignore package renamings, not interesting and they can
831 -- cause self referential loops in the code below.
833 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
837 -- Note: in the loop below, the check for Next_Entity pointing
838 -- back to the package entity may seem odd, but it is needed,
839 -- because a package can contain a renaming declaration to itself,
840 -- and such renamings are generated automatically within package
844 while Present (E) and then E /= Id loop
845 if Is_Assignable (E) then
846 Set_Never_Set_In_Source (E, False);
847 Set_Is_True_Constant (E, False);
848 Set_Current_Value (E, Empty);
849 Set_Is_Known_Null (E, False);
850 Set_Last_Assignment (E, Empty);
852 if not Can_Never_Be_Null (E) then
853 Set_Is_Known_Non_Null (E, False);
856 elsif Ekind (E) = E_Package
858 Ekind (E) = E_Generic_Package
860 Clear_Constants (E, First_Entity (E));
861 Clear_Constants (E, First_Private_Entity (E));
868 --------------------------------
869 -- Generate_Parent_References --
870 --------------------------------
872 procedure Generate_Parent_References is
873 Decl : constant Node_Id := Parent (N);
876 if Id = Cunit_Entity (Main_Unit)
877 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
879 Generate_Reference (Id, Scope (Id), 'k', False);
881 elsif not Nkind_In (Unit (Cunit (Main_Unit)), N_Subprogram_Body,
884 -- If current unit is an ancestor of main unit, generate
885 -- a reference to its own parent.
889 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
892 if Nkind (Main_Spec) = N_Package_Body then
893 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
896 U := Parent_Spec (Main_Spec);
897 while Present (U) loop
898 if U = Parent (Decl) then
899 Generate_Reference (Id, Scope (Id), 'k', False);
902 elsif Nkind (Unit (U)) = N_Package_Body then
906 U := Parent_Spec (Unit (U));
911 end Generate_Parent_References;
913 ---------------------
914 -- Is_Public_Child --
915 ---------------------
917 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
919 if not Is_Private_Descendant (Child) then
923 return not Private_Present (
924 Parent (Unit_Declaration_Node (Child)));
926 return Is_Public_Child (Scope (Child), Unit);
931 ----------------------------------------
932 -- Inspect_Unchecked_Union_Completion --
933 ----------------------------------------
935 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
939 Decl := First (Decls);
940 while Present (Decl) loop
942 -- We are looking at an incomplete or private type declaration
943 -- with a known_discriminant_part whose full view is an
946 if Nkind_In (Decl, N_Incomplete_Type_Declaration,
947 N_Private_Type_Declaration)
948 and then Has_Discriminants (Defining_Identifier (Decl))
949 and then Present (Full_View (Defining_Identifier (Decl)))
951 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
954 ("completion of discriminated partial view "
955 & "cannot be an Unchecked_Union",
956 Full_View (Defining_Identifier (Decl)));
961 end Inspect_Unchecked_Union_Completion;
963 -----------------------------------------
964 -- Install_Parent_Private_Declarations --
965 -----------------------------------------
967 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
968 Inst_Par : Entity_Id;
976 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
977 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
978 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
980 if Nkind_In (Inst_Node, N_Package_Instantiation,
981 N_Formal_Package_Declaration)
982 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
984 Inst_Par := Entity (Prefix (Name (Inst_Node)));
986 if Present (Renamed_Entity (Inst_Par)) then
987 Inst_Par := Renamed_Entity (Inst_Par);
992 (Specification (Unit_Declaration_Node (Inst_Par)));
994 -- Install the private declarations and private use clauses
995 -- of a parent instance of the child instance, unless the
996 -- parent instance private declarations have already been
997 -- installed earlier in Analyze_Package_Specification, which
998 -- happens when a generic child is instantiated, and the
999 -- instance is a child of the parent instance.
1001 -- Installing the use clauses of the parent instance twice
1002 -- is both unnecessary and wrong, because it would cause the
1003 -- clauses to be chained to themselves in the use clauses
1004 -- list of the scope stack entry. That in turn would cause
1005 -- an endless loop from End_Use_Clauses upon sccope exit.
1007 -- The parent is now fully visible. It may be a hidden open
1008 -- scope if we are currently compiling some child instance
1009 -- declared within it, but while the current instance is being
1010 -- compiled the parent is immediately visible. In particular
1011 -- its entities must remain visible if a stack save/restore
1012 -- takes place through a call to Rtsfind.
1014 if Present (Gen_Par) then
1015 if not In_Private_Part (Inst_Par) then
1016 Install_Private_Declarations (Inst_Par);
1017 Set_Use (Private_Declarations
1019 (Unit_Declaration_Node (Inst_Par))));
1020 Set_Is_Hidden_Open_Scope (Inst_Par, False);
1023 -- If we've reached the end of the generic instance parents,
1024 -- then finish off by looping through the nongeneric parents
1025 -- and installing their private declarations.
1028 while Present (Inst_Par)
1029 and then Inst_Par /= Standard_Standard
1030 and then (not In_Open_Scopes (Inst_Par)
1031 or else not In_Private_Part (Inst_Par))
1033 Install_Private_Declarations (Inst_Par);
1034 Set_Use (Private_Declarations
1036 (Unit_Declaration_Node (Inst_Par))));
1037 Inst_Par := Scope (Inst_Par);
1047 end Install_Parent_Private_Declarations;
1049 -- Start of processing for Analyze_Package_Specification
1052 if Present (Vis_Decls) then
1053 Analyze_Declarations (Vis_Decls);
1054 Analyze_PPCs (Vis_Decls);
1057 -- Verify that incomplete types have received full declarations
1059 E := First_Entity (Id);
1060 while Present (E) loop
1061 if Ekind (E) = E_Incomplete_Type
1062 and then No (Full_View (E))
1064 Error_Msg_N ("no declaration in visible part for incomplete}", E);
1070 if Is_Remote_Call_Interface (Id)
1071 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1073 Validate_RCI_Declarations (Id);
1076 -- Save global references in the visible declarations, before
1077 -- installing private declarations of parent unit if there is one,
1078 -- because the privacy status of types defined in the parent will
1079 -- change. This is only relevant for generic child units, but is
1080 -- done in all cases for uniformity.
1082 if Ekind (Id) = E_Generic_Package
1083 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1086 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1087 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1090 Set_Private_Declarations (Orig_Spec, Empty_List);
1091 Save_Global_References (Orig_Decl);
1092 Set_Private_Declarations (Orig_Spec, Save_Priv);
1096 -- If package is a public child unit, then make the private declarations
1097 -- of the parent visible.
1099 Public_Child := False;
1103 Pack_Decl : Node_Id;
1108 Par_Spec := Parent_Spec (Parent (N));
1110 -- If the package is formal package of an enclosing generic, it is
1111 -- transformed into a local generic declaration, and compiled to make
1112 -- its spec available. We need to retrieve the original generic to
1113 -- determine whether it is a child unit, and install its parents.
1117 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1119 Par := Entity (Name (Original_Node (Parent (N))));
1120 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1123 if Present (Par_Spec) then
1124 Generate_Parent_References;
1126 while Scope (Par) /= Standard_Standard
1127 and then Is_Public_Child (Id, Par)
1128 and then In_Open_Scopes (Par)
1130 Public_Child := True;
1132 Install_Private_Declarations (Par);
1133 Install_Private_With_Clauses (Par);
1134 Pack_Decl := Unit_Declaration_Node (Par);
1135 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1140 if Is_Compilation_Unit (Id) then
1141 Install_Private_With_Clauses (Id);
1144 -- The current compilation unit may include private with_clauses,
1145 -- which are visible in the private part of the current nested
1146 -- package, and have to be installed now. This is not done for
1147 -- nested instantiations, where the private with_clauses of the
1148 -- enclosing unit have no effect once the instantiation info is
1149 -- established and we start analyzing the package declaration.
1152 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1154 if (Ekind (Comp_Unit) = E_Package
1155 or else Ekind (Comp_Unit) = E_Generic_Package)
1156 and then not In_Private_Part (Comp_Unit)
1157 and then not In_Instance
1159 Install_Private_With_Clauses (Comp_Unit);
1160 Private_With_Clauses_Installed := True;
1165 -- If this is a package associated with a generic instance or formal
1166 -- package, then the private declarations of each of the generic's
1167 -- parents must be installed at this point.
1169 if Is_Generic_Instance (Id) then
1170 Install_Parent_Private_Declarations (Id);
1173 -- Analyze private part if present. The flag In_Private_Part is reset
1174 -- in End_Package_Scope.
1176 L := Last_Entity (Id);
1178 if Present (Priv_Decls) then
1179 Set_In_Private_Part (Id);
1181 -- Upon entering a public child's private part, it may be necessary
1182 -- to declare subprograms that were derived in the package's visible
1183 -- part but not yet made visible.
1185 if Public_Child then
1186 Declare_Inherited_Private_Subprograms (Id);
1189 Analyze_Declarations (Priv_Decls);
1190 Analyze_PPCs (Priv_Decls);
1192 -- Check the private declarations for incomplete deferred constants
1194 Inspect_Deferred_Constant_Completion (Priv_Decls);
1196 -- The first private entity is the immediate follower of the last
1197 -- visible entity, if there was one.
1200 Set_First_Private_Entity (Id, Next_Entity (L));
1202 Set_First_Private_Entity (Id, First_Entity (Id));
1205 -- There may be inherited private subprograms that need to be declared,
1206 -- even in the absence of an explicit private part. If there are any
1207 -- public declarations in the package and the package is a public child
1208 -- unit, then an implicit private part is assumed.
1210 elsif Present (L) and then Public_Child then
1211 Set_In_Private_Part (Id);
1212 Declare_Inherited_Private_Subprograms (Id);
1213 Set_First_Private_Entity (Id, Next_Entity (L));
1216 E := First_Entity (Id);
1217 while Present (E) loop
1219 -- Check rule of 3.6(11), which in general requires waiting till all
1220 -- full types have been seen.
1222 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1223 Check_Aliased_Component_Types (E);
1226 -- Check preelaborable initialization for full type completing a
1227 -- private type for which pragma Preelaborable_Initialization given.
1230 and then Must_Have_Preelab_Init (E)
1231 and then not Has_Preelaborable_Initialization (E)
1234 ("full view of & does not have preelaborable initialization", E);
1240 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1241 -- declaration having a known_discriminant_part shall not be an
1242 -- Unchecked_Union type.
1244 if Present (Vis_Decls) then
1245 Inspect_Unchecked_Union_Completion (Vis_Decls);
1248 if Present (Priv_Decls) then
1249 Inspect_Unchecked_Union_Completion (Priv_Decls);
1252 if Ekind (Id) = E_Generic_Package
1253 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1254 and then Present (Priv_Decls)
1256 -- Save global references in private declarations, ignoring the
1257 -- visible declarations that were processed earlier.
1260 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1261 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1262 Save_Form : constant List_Id :=
1263 Generic_Formal_Declarations (Orig_Decl);
1266 Set_Visible_Declarations (Orig_Spec, Empty_List);
1267 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1268 Save_Global_References (Orig_Decl);
1269 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1270 Set_Visible_Declarations (Orig_Spec, Save_Vis);
1274 Process_End_Label (N, 'e', Id);
1276 -- Remove private_with_clauses of enclosing compilation unit, if they
1279 if Private_With_Clauses_Installed then
1280 Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
1283 -- For the case of a library level package, we must go through all the
1284 -- entities clearing the indications that the value may be constant and
1285 -- not modified. Why? Because any client of this package may modify
1286 -- these values freely from anywhere. This also applies to any nested
1287 -- packages or generic packages.
1289 -- For now we unconditionally clear constants for packages that are
1290 -- instances of generic packages. The reason is that we do not have the
1291 -- body yet, and we otherwise think things are unreferenced when they
1292 -- are not. This should be fixed sometime (the effect is not terrible,
1293 -- we just lose some warnings, and also some cases of value propagation)
1296 if Is_Library_Level_Entity (Id)
1297 or else Is_Generic_Instance (Id)
1299 Clear_Constants (Id, First_Entity (Id));
1300 Clear_Constants (Id, First_Private_Entity (Id));
1302 end Analyze_Package_Specification;
1304 --------------------------------------
1305 -- Analyze_Private_Type_Declaration --
1306 --------------------------------------
1308 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1309 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
1310 Id : constant Entity_Id := Defining_Identifier (N);
1313 Generate_Definition (Id);
1314 Set_Is_Pure (Id, PF);
1315 Init_Size_Align (Id);
1317 if (Ekind (Current_Scope) /= E_Package
1318 and then Ekind (Current_Scope) /= E_Generic_Package)
1319 or else In_Private_Part (Current_Scope)
1321 Error_Msg_N ("invalid context for private declaration", N);
1324 New_Private_Type (N, Id, N);
1325 Set_Depends_On_Private (Id);
1326 end Analyze_Private_Type_Declaration;
1328 ----------------------------------
1329 -- Check_Anonymous_Access_Types --
1330 ----------------------------------
1332 procedure Check_Anonymous_Access_Types
1333 (Spec_Id : Entity_Id;
1340 -- Itype references are only needed by gigi, to force elaboration of
1341 -- itypes. In the absence of code generation, they are not needed.
1343 if not Expander_Active then
1347 E := First_Entity (Spec_Id);
1348 while Present (E) loop
1349 if Ekind (E) = E_Anonymous_Access_Type
1350 and then From_With_Type (E)
1352 IR := Make_Itype_Reference (Sloc (P_Body));
1355 if No (Declarations (P_Body)) then
1356 Set_Declarations (P_Body, New_List (IR));
1358 Prepend (IR, Declarations (P_Body));
1364 end Check_Anonymous_Access_Types;
1366 -------------------------------------------
1367 -- Declare_Inherited_Private_Subprograms --
1368 -------------------------------------------
1370 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1372 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1373 -- Check whether an inherited subprogram is an operation of an
1374 -- untagged derived type.
1376 ---------------------
1377 -- Is_Primitive_Of --
1378 ---------------------
1380 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1384 -- If the full view is a scalar type, the type is the anonymous
1385 -- base type, but the operation mentions the first subtype, so
1386 -- check the signature againt the base type.
1388 if Base_Type (Etype (S)) = Base_Type (T) then
1392 Formal := First_Formal (S);
1393 while Present (Formal) loop
1394 if Base_Type (Etype (Formal)) = Base_Type (T) then
1398 Next_Formal (Formal);
1403 end Is_Primitive_Of;
1410 Op_Elmt_2 : Elmt_Id;
1411 Prim_Op : Entity_Id;
1412 New_Op : Entity_Id := Empty;
1413 Parent_Subp : Entity_Id;
1416 -- Start of processing for Declare_Inherited_Private_Subprograms
1419 E := First_Entity (Id);
1420 while Present (E) loop
1422 -- If the entity is a nonprivate type extension whose parent
1423 -- type is declared in an open scope, then the type may have
1424 -- inherited operations that now need to be made visible.
1425 -- Ditto if the entity is a formal derived type in a child unit.
1427 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
1429 (Nkind (Parent (E)) = N_Private_Extension_Declaration
1430 and then Is_Generic_Type (E)))
1431 and then In_Open_Scopes (Scope (Etype (E)))
1432 and then E = Base_Type (E)
1434 if Is_Tagged_Type (E) then
1435 Op_List := Primitive_Operations (E);
1437 Tag := First_Tag_Component (E);
1439 Op_Elmt := First_Elmt (Op_List);
1440 while Present (Op_Elmt) loop
1441 Prim_Op := Node (Op_Elmt);
1443 -- Search primitives that are implicit operations with an
1444 -- internal name whose parent operation has a normal name.
1446 if Present (Alias (Prim_Op))
1447 and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
1448 and then not Comes_From_Source (Prim_Op)
1449 and then Is_Internal_Name (Chars (Prim_Op))
1450 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1452 Parent_Subp := Alias (Prim_Op);
1454 -- Case 1: Check if the type has also an explicit
1455 -- overriding for this primitive.
1457 Op_Elmt_2 := Next_Elmt (Op_Elmt);
1458 while Present (Op_Elmt_2) loop
1459 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
1460 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
1462 -- The private inherited operation has been
1463 -- overridden by an explicit subprogram: replace
1464 -- the former by the latter.
1466 New_Op := Node (Op_Elmt_2);
1467 Replace_Elmt (Op_Elmt, New_Op);
1468 Remove_Elmt (Op_List, Op_Elmt_2);
1469 Set_Is_Overriding_Operation (New_Op);
1470 Set_Overridden_Operation (New_Op, Parent_Subp);
1472 -- We don't need to inherit its dispatching slot.
1473 -- Set_All_DT_Position has previously ensured that
1474 -- the same slot was assigned to the two primitives
1477 and then Present (DTC_Entity (New_Op))
1478 and then Present (DTC_Entity (Prim_Op))
1480 pragma Assert (DT_Position (New_Op)
1481 = DT_Position (Prim_Op));
1485 goto Next_Primitive;
1488 Next_Elmt (Op_Elmt_2);
1491 -- Case 2: We have not found any explicit overriding and
1492 -- hence we need to declare the operation (i.e., make it
1495 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1497 -- Inherit the dispatching slot if E is already frozen
1500 and then Present (DTC_Entity (Alias (Prim_Op)))
1502 Set_DTC_Entity_Value (E, New_Op);
1503 Set_DT_Position (New_Op,
1504 DT_Position (Alias (Prim_Op)));
1508 (Is_Dispatching_Operation (New_Op)
1509 and then Node (Last_Elmt (Op_List)) = New_Op);
1511 -- Substitute the new operation for the old one
1512 -- in the type's primitive operations list. Since
1513 -- the new operation was also just added to the end
1514 -- of list, the last element must be removed.
1516 -- (Question: is there a simpler way of declaring
1517 -- the operation, say by just replacing the name
1518 -- of the earlier operation, reentering it in the
1519 -- in the symbol table (how?), and marking it as
1522 Replace_Elmt (Op_Elmt, New_Op);
1523 Remove_Last_Elmt (Op_List);
1527 Next_Elmt (Op_Elmt);
1530 -- Generate listing showing the contents of the dispatch table
1532 if Debug_Flag_ZZ then
1537 -- Non-tagged type, scan forward to locate
1538 -- inherited hidden operations.
1540 Prim_Op := Next_Entity (E);
1541 while Present (Prim_Op) loop
1542 if Is_Subprogram (Prim_Op)
1543 and then Present (Alias (Prim_Op))
1544 and then not Comes_From_Source (Prim_Op)
1545 and then Is_Internal_Name (Chars (Prim_Op))
1546 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1547 and then Is_Primitive_Of (E, Prim_Op)
1549 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1552 Next_Entity (Prim_Op);
1559 end Declare_Inherited_Private_Subprograms;
1561 -----------------------
1562 -- End_Package_Scope --
1563 -----------------------
1565 procedure End_Package_Scope (P : Entity_Id) is
1567 Uninstall_Declarations (P);
1569 end End_Package_Scope;
1571 ---------------------------
1572 -- Exchange_Declarations --
1573 ---------------------------
1575 procedure Exchange_Declarations (Id : Entity_Id) is
1576 Full_Id : constant Entity_Id := Full_View (Id);
1577 H1 : constant Entity_Id := Homonym (Id);
1578 Next1 : constant Entity_Id := Next_Entity (Id);
1583 -- If missing full declaration for type, nothing to exchange
1585 if No (Full_Id) then
1589 -- Otherwise complete the exchange, and preserve semantic links
1591 Next2 := Next_Entity (Full_Id);
1592 H2 := Homonym (Full_Id);
1594 -- Reset full declaration pointer to reflect the switched entities
1595 -- and readjust the next entity chains.
1597 Exchange_Entities (Id, Full_Id);
1599 Set_Next_Entity (Id, Next1);
1600 Set_Homonym (Id, H1);
1602 Set_Full_View (Full_Id, Id);
1603 Set_Next_Entity (Full_Id, Next2);
1604 Set_Homonym (Full_Id, H2);
1605 end Exchange_Declarations;
1607 ------------------------------------------
1608 -- Inspect_Deferred_Constant_Completion --
1609 ------------------------------------------
1611 procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
1615 Decl := First (Decls);
1616 while Present (Decl) loop
1618 -- Deferred constant signature
1620 if Nkind (Decl) = N_Object_Declaration
1621 and then Constant_Present (Decl)
1622 and then No (Expression (Decl))
1624 -- No need to check internally generated constants
1626 and then Comes_From_Source (Decl)
1628 -- The constant is not completed. A full object declaration
1629 -- or a pragma Import complete a deferred constant.
1631 and then not Has_Completion (Defining_Identifier (Decl))
1634 ("constant declaration requires initialization expression",
1635 Defining_Identifier (Decl));
1638 Decl := Next (Decl);
1640 end Inspect_Deferred_Constant_Completion;
1642 ----------------------------
1643 -- Install_Package_Entity --
1644 ----------------------------
1646 procedure Install_Package_Entity (Id : Entity_Id) is
1648 if not Is_Internal (Id) then
1649 if Debug_Flag_E then
1650 Write_Str ("Install: ");
1651 Write_Name (Chars (Id));
1655 if not Is_Child_Unit (Id) then
1656 Set_Is_Immediately_Visible (Id);
1660 end Install_Package_Entity;
1662 ----------------------------------
1663 -- Install_Private_Declarations --
1664 ----------------------------------
1666 procedure Install_Private_Declarations (P : Entity_Id) is
1668 Priv_Elmt : Elmt_Id;
1673 -- First exchange declarations for private types, so that the
1674 -- full declaration is visible. For each private type, we check
1675 -- its Private_Dependents list and also exchange any subtypes of
1676 -- or derived types from it. Finally, if this is a Taft amendment
1677 -- type, the incomplete declaration is irrelevant, and we want to
1678 -- link the eventual full declaration with the original private
1679 -- one so we also skip the exchange.
1681 Id := First_Entity (P);
1682 while Present (Id) and then Id /= First_Private_Entity (P) loop
1683 if Is_Private_Base_Type (Id)
1684 and then Comes_From_Source (Full_View (Id))
1685 and then Present (Full_View (Id))
1686 and then Scope (Full_View (Id)) = Scope (Id)
1687 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
1689 -- If there is a use-type clause on the private type, set the
1690 -- full view accordingly.
1692 Set_In_Use (Full_View (Id), In_Use (Id));
1693 Full := Full_View (Id);
1695 if Is_Private_Base_Type (Full)
1696 and then Has_Private_Declaration (Full)
1697 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
1698 and then In_Open_Scopes (Scope (Etype (Full)))
1699 and then In_Package_Body (Current_Scope)
1700 and then not Is_Private_Type (Etype (Full))
1702 -- This is the completion of a private type by a derivation
1703 -- from another private type which is not private anymore. This
1704 -- can only happen in a package nested within a child package,
1705 -- when the parent type is defined in the parent unit. At this
1706 -- point the current type is not private either, and we have to
1707 -- install the underlying full view, which is now visible.
1708 -- Save the current full view as well, so that all views can
1709 -- be restored on exit. It may seem that after compiling the
1710 -- child body there are not environments to restore, but the
1711 -- back-end expects those links to be valid, and freeze nodes
1714 if No (Full_View (Full))
1715 and then Present (Underlying_Full_View (Full))
1717 Set_Full_View (Id, Underlying_Full_View (Full));
1718 Set_Underlying_Full_View (Id, Full);
1720 Set_Underlying_Full_View (Full, Empty);
1721 Set_Is_Frozen (Full_View (Id));
1725 Priv_Elmt := First_Elmt (Private_Dependents (Id));
1727 Exchange_Declarations (Id);
1728 Set_Is_Immediately_Visible (Id);
1730 while Present (Priv_Elmt) loop
1731 Priv := Node (Priv_Elmt);
1733 -- Before the exchange, verify that the presence of the
1734 -- Full_View field. It will be empty if the entity
1735 -- has already been installed due to a previous call.
1737 if Present (Full_View (Priv))
1738 and then Is_Visible_Dependent (Priv)
1741 -- For each subtype that is swapped, we also swap the
1742 -- reference to it in Private_Dependents, to allow access
1743 -- to it when we swap them out in End_Package_Scope.
1745 Replace_Elmt (Priv_Elmt, Full_View (Priv));
1746 Exchange_Declarations (Priv);
1747 Set_Is_Immediately_Visible
1748 (Priv, In_Open_Scopes (Scope (Priv)));
1749 Set_Is_Potentially_Use_Visible
1750 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
1753 Next_Elmt (Priv_Elmt);
1760 -- Next make other declarations in the private part visible as well
1762 Id := First_Private_Entity (P);
1763 while Present (Id) loop
1764 Install_Package_Entity (Id);
1765 Set_Is_Hidden (Id, False);
1769 -- Indicate that the private part is currently visible, so it can be
1770 -- properly reset on exit.
1772 Set_In_Private_Part (P);
1773 end Install_Private_Declarations;
1775 ----------------------------------
1776 -- Install_Visible_Declarations --
1777 ----------------------------------
1779 procedure Install_Visible_Declarations (P : Entity_Id) is
1781 Last_Entity : Entity_Id;
1785 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
1787 if Is_Package_Or_Generic_Package (P) then
1788 Last_Entity := First_Private_Entity (P);
1790 Last_Entity := Empty;
1793 Id := First_Entity (P);
1794 while Present (Id) and then Id /= Last_Entity loop
1795 Install_Package_Entity (Id);
1798 end Install_Visible_Declarations;
1800 --------------------------
1801 -- Is_Private_Base_Type --
1802 --------------------------
1804 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
1806 return Ekind (E) = E_Private_Type
1807 or else Ekind (E) = E_Limited_Private_Type
1808 or else Ekind (E) = E_Record_Type_With_Private;
1809 end Is_Private_Base_Type;
1811 --------------------------
1812 -- Is_Visible_Dependent --
1813 --------------------------
1815 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
1817 S : constant Entity_Id := Scope (Dep);
1820 -- Renamings created for actual types have the visibility of the
1823 if Ekind (S) = E_Package
1824 and then Is_Generic_Instance (S)
1825 and then (Is_Generic_Actual_Type (Dep)
1826 or else Is_Generic_Actual_Type (Full_View (Dep)))
1830 elsif not (Is_Derived_Type (Dep))
1831 and then Is_Derived_Type (Full_View (Dep))
1833 -- When instantiating a package body, the scope stack is empty,
1834 -- so check instead whether the dependent type is defined in
1835 -- the same scope as the instance itself.
1837 return In_Open_Scopes (S)
1838 or else (Is_Generic_Instance (Current_Scope)
1839 and then Scope (Dep) = Scope (Current_Scope));
1843 end Is_Visible_Dependent;
1845 ----------------------------
1846 -- May_Need_Implicit_Body --
1847 ----------------------------
1849 procedure May_Need_Implicit_Body (E : Entity_Id) is
1850 P : constant Node_Id := Unit_Declaration_Node (E);
1851 S : constant Node_Id := Parent (P);
1856 if not Has_Completion (E)
1857 and then Nkind (P) = N_Package_Declaration
1858 and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
1861 Make_Package_Body (Sloc (E),
1862 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
1863 Chars => Chars (E)),
1864 Declarations => New_List);
1866 if Nkind (S) = N_Package_Specification then
1867 if Present (Private_Declarations (S)) then
1868 Decls := Private_Declarations (S);
1870 Decls := Visible_Declarations (S);
1873 Decls := Declarations (S);
1879 end May_Need_Implicit_Body;
1881 ----------------------
1882 -- New_Private_Type --
1883 ----------------------
1885 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
1889 if Limited_Present (Def) then
1890 Set_Ekind (Id, E_Limited_Private_Type);
1892 Set_Ekind (Id, E_Private_Type);
1896 Set_Has_Delayed_Freeze (Id);
1897 Set_Is_First_Subtype (Id);
1898 Init_Size_Align (Id);
1900 Set_Is_Constrained (Id,
1901 No (Discriminant_Specifications (N))
1902 and then not Unknown_Discriminants_Present (N));
1904 -- Set tagged flag before processing discriminants, to catch
1907 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
1909 Set_Discriminant_Constraint (Id, No_Elist);
1910 Set_Stored_Constraint (Id, No_Elist);
1912 if Present (Discriminant_Specifications (N)) then
1914 Process_Discriminants (N);
1917 elsif Unknown_Discriminants_Present (N) then
1918 Set_Has_Unknown_Discriminants (Id);
1921 Set_Private_Dependents (Id, New_Elmt_List);
1923 if Tagged_Present (Def) then
1924 Set_Ekind (Id, E_Record_Type_With_Private);
1925 Make_Class_Wide_Type (Id);
1926 Set_Primitive_Operations (Id, New_Elmt_List);
1927 Set_Is_Abstract_Type (Id, Abstract_Present (Def));
1928 Set_Is_Limited_Record (Id, Limited_Present (Def));
1929 Set_Has_Delayed_Freeze (Id, True);
1931 elsif Abstract_Present (Def) then
1932 Error_Msg_N ("only a tagged type can be abstract", N);
1934 end New_Private_Type;
1936 ----------------------------
1937 -- Uninstall_Declarations --
1938 ----------------------------
1940 procedure Uninstall_Declarations (P : Entity_Id) is
1941 Decl : constant Node_Id := Unit_Declaration_Node (P);
1944 Priv_Elmt : Elmt_Id;
1945 Priv_Sub : Entity_Id;
1947 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id);
1948 -- Copy to the private declaration the attributes of the full view
1949 -- that need to be available for the partial view also.
1951 function Type_In_Use (T : Entity_Id) return Boolean;
1952 -- Check whether type or base type appear in an active use_type clause
1954 ------------------------------
1955 -- Preserve_Full_Attributes --
1956 ------------------------------
1958 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id) is
1959 Priv_Is_Base_Type : constant Boolean := Priv = Base_Type (Priv);
1962 Set_Size_Info (Priv, (Full));
1963 Set_RM_Size (Priv, RM_Size (Full));
1964 Set_Size_Known_At_Compile_Time
1965 (Priv, Size_Known_At_Compile_Time (Full));
1966 Set_Is_Volatile (Priv, Is_Volatile (Full));
1967 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
1968 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full));
1969 Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced (Full));
1970 Set_Has_Pragma_Unreferenced_Objects
1971 (Priv, Has_Pragma_Unreferenced_Objects
1973 if Is_Unchecked_Union (Full) then
1974 Set_Is_Unchecked_Union (Base_Type (Priv));
1976 -- Why is atomic not copied here ???
1978 if Referenced (Full) then
1979 Set_Referenced (Priv);
1982 if Priv_Is_Base_Type then
1983 Set_Is_Controlled (Priv, Is_Controlled (Base_Type (Full)));
1984 Set_Finalize_Storage_Only (Priv, Finalize_Storage_Only
1985 (Base_Type (Full)));
1986 Set_Has_Task (Priv, Has_Task (Base_Type (Full)));
1987 Set_Has_Controlled_Component (Priv, Has_Controlled_Component
1988 (Base_Type (Full)));
1991 Set_Freeze_Node (Priv, Freeze_Node (Full));
1993 if Is_Tagged_Type (Priv)
1994 and then Is_Tagged_Type (Full)
1995 and then not Error_Posted (Full)
1997 if Priv_Is_Base_Type then
1999 -- Ada 2005 (AI-345): The full view of a type implementing
2000 -- an interface can be a task type.
2002 -- type T is new I with private;
2004 -- task type T is new I with ...
2006 if Is_Interface (Etype (Priv))
2007 and then Is_Concurrent_Type (Base_Type (Full))
2009 -- Protect the frontend against previous errors
2011 if Present (Corresponding_Record_Type
2014 Set_Access_Disp_Table
2015 (Priv, Access_Disp_Table
2016 (Corresponding_Record_Type (Base_Type (Full))));
2018 -- Generic context, or previous errors
2025 Set_Access_Disp_Table
2026 (Priv, Access_Disp_Table (Base_Type (Full)));
2030 if Is_Tagged_Type (Priv) then
2032 -- If the type is tagged, the tag itself must be available
2033 -- on the partial view, for expansion purposes.
2035 Set_First_Entity (Priv, First_Entity (Full));
2037 -- If there are discriminants in the partial view, these remain
2038 -- visible. Otherwise only the tag itself is visible, and there
2039 -- are no nameable components in the partial view.
2041 if No (Last_Entity (Priv)) then
2042 Set_Last_Entity (Priv, First_Entity (Priv));
2046 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2048 end Preserve_Full_Attributes;
2054 function Type_In_Use (T : Entity_Id) return Boolean is
2056 return Scope (Base_Type (T)) = P
2057 and then (In_Use (T) or else In_Use (Base_Type (T)));
2060 -- Start of processing for Uninstall_Declarations
2063 Id := First_Entity (P);
2064 while Present (Id) and then Id /= First_Private_Entity (P) loop
2065 if Debug_Flag_E then
2066 Write_Str ("unlinking visible entity ");
2067 Write_Int (Int (Id));
2071 -- On exit from the package scope, we must preserve the visibility
2072 -- established by use clauses in the current scope. Two cases:
2074 -- a) If the entity is an operator, it may be a primitive operator of
2075 -- a type for which there is a visible use-type clause.
2077 -- b) for other entities, their use-visibility is determined by a
2078 -- visible use clause for the package itself. For a generic instance,
2079 -- the instantiation of the formals appears in the visible part,
2080 -- but the formals are private and remain so.
2082 if Ekind (Id) = E_Function
2083 and then Is_Operator_Symbol_Name (Chars (Id))
2084 and then not Is_Hidden (Id)
2085 and then not Error_Posted (Id)
2087 Set_Is_Potentially_Use_Visible (Id,
2089 or else Type_In_Use (Etype (Id))
2090 or else Type_In_Use (Etype (First_Formal (Id)))
2091 or else (Present (Next_Formal (First_Formal (Id)))
2094 (Etype (Next_Formal (First_Formal (Id))))));
2096 if In_Use (P) and then not Is_Hidden (Id) then
2098 -- A child unit of a use-visible package remains use-visible
2099 -- only if it is itself a visible child unit. Otherwise it
2100 -- would remain visible in other contexts where P is use-
2101 -- visible, because once compiled it stays in the entity list
2102 -- of its parent unit.
2104 if Is_Child_Unit (Id) then
2105 Set_Is_Potentially_Use_Visible (Id,
2106 Is_Visible_Child_Unit (Id));
2108 Set_Is_Potentially_Use_Visible (Id);
2112 Set_Is_Potentially_Use_Visible (Id, False);
2116 -- Local entities are not immediately visible outside of the package
2118 Set_Is_Immediately_Visible (Id, False);
2120 -- If this is a private type with a full view (for example a local
2121 -- subtype of a private type declared elsewhere), ensure that the
2122 -- full view is also removed from visibility: it may be exposed when
2123 -- swapping views in an instantiation.
2126 and then Present (Full_View (Id))
2128 Set_Is_Immediately_Visible (Full_View (Id), False);
2131 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2132 Check_Abstract_Overriding (Id);
2133 Check_Conventions (Id);
2136 if (Ekind (Id) = E_Private_Type
2137 or else Ekind (Id) = E_Limited_Private_Type)
2138 and then No (Full_View (Id))
2139 and then not Is_Generic_Type (Id)
2140 and then not Is_Derived_Type (Id)
2142 Error_Msg_N ("missing full declaration for private type&", Id);
2144 elsif Ekind (Id) = E_Record_Type_With_Private
2145 and then not Is_Generic_Type (Id)
2146 and then No (Full_View (Id))
2148 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
2149 Error_Msg_N ("missing full declaration for private type&", Id);
2152 ("missing full declaration for private extension", Id);
2155 elsif Ekind (Id) = E_Constant
2156 and then No (Constant_Value (Id))
2157 and then No (Full_View (Id))
2158 and then not Is_Imported (Id)
2159 and then (Nkind (Parent (Id)) /= N_Object_Declaration
2160 or else not No_Initialization (Parent (Id)))
2162 if not Has_Private_Declaration (Etype (Id)) then
2164 -- We assume that the user did not not intend a deferred
2165 -- constant declaration, and the expression is just missing.
2168 ("constant declaration requires initialization expression",
2171 if Is_Limited_Type (Etype (Id)) then
2173 ("\if variable intended, remove CONSTANT from declaration",
2179 ("missing full declaration for deferred constant (RM 7.4)",
2182 if Is_Limited_Type (Etype (Id)) then
2184 ("\if variable intended, remove CONSTANT from declaration",
2193 -- If the specification was installed as the parent of a public child
2194 -- unit, the private declarations were not installed, and there is
2197 if not In_Private_Part (P) then
2200 Set_In_Private_Part (P, False);
2203 -- Make private entities invisible and exchange full and private
2204 -- declarations for private types. Id is now the first private
2205 -- entity in the package.
2207 while Present (Id) loop
2208 if Debug_Flag_E then
2209 Write_Str ("unlinking private entity ");
2210 Write_Int (Int (Id));
2214 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2215 Check_Abstract_Overriding (Id);
2216 Check_Conventions (Id);
2219 Set_Is_Immediately_Visible (Id, False);
2221 if Is_Private_Base_Type (Id)
2222 and then Present (Full_View (Id))
2224 Full := Full_View (Id);
2226 -- If the partial view is not declared in the visible part
2227 -- of the package (as is the case when it is a type derived
2228 -- from some other private type in the private part of the
2229 -- current package), no exchange takes place.
2232 or else List_Containing (Parent (Id))
2233 /= Visible_Declarations (Specification (Decl))
2238 -- The entry in the private part points to the full declaration,
2239 -- which is currently visible. Exchange them so only the private
2240 -- type declaration remains accessible, and link private and
2241 -- full declaration in the opposite direction. Before the actual
2242 -- exchange, we copy back attributes of the full view that
2243 -- must be available to the partial view too.
2245 Preserve_Full_Attributes (Id, Full);
2247 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
2249 if Is_Indefinite_Subtype (Full)
2250 and then not Is_Indefinite_Subtype (Id)
2253 ("full view of type must be definite subtype", Full);
2256 Priv_Elmt := First_Elmt (Private_Dependents (Id));
2258 -- Swap out the subtypes and derived types of Id that were
2259 -- compiled in this scope, or installed previously by
2260 -- Install_Private_Declarations.
2261 -- Before we do the swap, we verify the presence of the
2262 -- Full_View field which may be empty due to a swap by
2263 -- a previous call to End_Package_Scope (e.g. from the
2264 -- freezing mechanism).
2266 while Present (Priv_Elmt) loop
2267 Priv_Sub := Node (Priv_Elmt);
2269 if Present (Full_View (Priv_Sub)) then
2271 if Scope (Priv_Sub) = P
2272 or else not In_Open_Scopes (Scope (Priv_Sub))
2274 Set_Is_Immediately_Visible (Priv_Sub, False);
2277 if Is_Visible_Dependent (Priv_Sub) then
2278 Preserve_Full_Attributes
2279 (Priv_Sub, Full_View (Priv_Sub));
2280 Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub));
2281 Exchange_Declarations (Priv_Sub);
2285 Next_Elmt (Priv_Elmt);
2288 -- Now restore the type itself to its private view
2290 Exchange_Declarations (Id);
2292 -- If we have installed an underlying full view for a type
2293 -- derived from a private type in a child unit, restore the
2294 -- proper views of private and full view. See corresponding
2295 -- code in Install_Private_Declarations.
2296 -- After the exchange, Full denotes the private type in the
2297 -- visible part of the package.
2299 if Is_Private_Base_Type (Full)
2300 and then Present (Full_View (Full))
2301 and then Present (Underlying_Full_View (Full))
2302 and then In_Package_Body (Current_Scope)
2304 Set_Full_View (Full, Underlying_Full_View (Full));
2305 Set_Underlying_Full_View (Full, Empty);
2308 elsif Ekind (Id) = E_Incomplete_Type
2309 and then No (Full_View (Id))
2311 -- Mark Taft amendment types
2313 Set_Has_Completion_In_Body (Id);
2315 elsif not Is_Child_Unit (Id)
2316 and then (not Is_Private_Type (Id)
2317 or else No (Full_View (Id)))
2320 Set_Is_Potentially_Use_Visible (Id, False);
2326 end Uninstall_Declarations;
2328 ------------------------
2329 -- Unit_Requires_Body --
2330 ------------------------
2332 function Unit_Requires_Body (P : Entity_Id) return Boolean is
2336 -- Imported entity never requires body. Right now, only
2337 -- subprograms can be imported, but perhaps in the future
2338 -- we will allow import of packages.
2340 if Is_Imported (P) then
2343 -- Body required if library package with pragma Elaborate_Body
2345 elsif Has_Pragma_Elaborate_Body (P) then
2348 -- Body required if subprogram
2350 elsif Is_Subprogram (P) or else Is_Generic_Subprogram (P) then
2353 -- Treat a block as requiring a body
2355 elsif Ekind (P) = E_Block then
2358 elsif Ekind (P) = E_Package
2359 and then Nkind (Parent (P)) = N_Package_Specification
2360 and then Present (Generic_Parent (Parent (P)))
2363 G_P : constant Entity_Id := Generic_Parent (Parent (P));
2365 if Has_Pragma_Elaborate_Body (G_P) then
2371 -- Otherwise search entity chain for entity requiring completion
2373 E := First_Entity (P);
2374 while Present (E) loop
2376 -- Always ignore child units. Child units get added to the entity
2377 -- list of a parent unit, but are not original entities of the
2378 -- parent, and so do not affect whether the parent needs a body.
2380 if Is_Child_Unit (E) then
2383 -- Ignore formal packages and their renamings
2385 elsif Ekind (E) = E_Package
2386 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
2387 N_Formal_Package_Declaration
2391 -- Otherwise test to see if entity requires a completion.
2392 -- Note that subprogram entities whose declaration does not come
2393 -- from source are ignored here on the basis that we assume the
2394 -- expander will provide an implicit completion at some point.
2396 elsif (Is_Overloadable (E)
2397 and then Ekind (E) /= E_Enumeration_Literal
2398 and then Ekind (E) /= E_Operator
2399 and then not Is_Abstract_Subprogram (E)
2400 and then not Has_Completion (E)
2401 and then Comes_From_Source (Parent (E)))
2404 (Ekind (E) = E_Package
2406 and then not Has_Completion (E)
2407 and then Unit_Requires_Body (E))
2410 (Ekind (E) = E_Incomplete_Type and then No (Full_View (E)))
2413 ((Ekind (E) = E_Task_Type or else
2414 Ekind (E) = E_Protected_Type)
2415 and then not Has_Completion (E))
2418 (Ekind (E) = E_Generic_Package and then E /= P
2419 and then not Has_Completion (E)
2420 and then Unit_Requires_Body (E))
2423 (Is_Generic_Subprogram (E)
2424 and then not Has_Completion (E))
2429 -- Entity that does not require completion
2439 end Unit_Requires_Body;