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