2015-09-28 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ada / sem_ch7.adb
blobf39da2c0066b2a2808cdfa9709245919ed7fe7eb
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-2015, 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 Ref_State : 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 Ref_State := 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 (Ref_State) then
208 Analyze_Refined_State_In_Decl_Part (Ref_State);
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 Present (Entity (Prefix (N)))
301 and then Is_Subprogram (Entity (Prefix (N)))
302 then
303 Reference_Seen := True;
304 return Abandon;
306 -- Detect the use of a non-static constant
308 elsif Is_Entity_Name (N)
309 and then Present (Entity (N))
310 and then Ekind (Entity (N)) = E_Constant
311 then
312 Val := Constant_Value (Entity (N));
314 if Present (Val)
315 and then not Compile_Time_Known_Value (Val)
316 then
317 Reference_Seen := True;
318 return Abandon;
319 end if;
320 end if;
322 return OK;
323 end Is_Subp_Or_Const_Ref;
325 procedure Find_Subp_Or_Const_Ref is
326 new Traverse_Proc (Is_Subp_Or_Const_Ref);
328 -- Start of processing for Contains_Subp_Or_Const_Refs
330 begin
331 Find_Subp_Or_Const_Ref (N);
333 return Reference_Seen;
334 end Contains_Subp_Or_Const_Refs;
336 --------------------
337 -- Has_Referencer --
338 --------------------
340 function Has_Referencer
341 (Decls : List_Id;
342 Top_Level : Boolean := False) return Boolean
344 Decl : Node_Id;
345 Decl_Id : Entity_Id;
346 Spec : Node_Id;
348 Has_Non_Subp_Const_Referencer : Boolean := False;
349 -- Flag set for inlined subprogram bodies that do not contain
350 -- references to other subprograms or non-static constants.
352 begin
353 if No (Decls) then
354 return False;
355 end if;
357 -- Examine all declarations in reverse order, hiding all entities
358 -- from external visibility until a referencer has been found. The
359 -- algorithm recurses into nested packages.
361 Decl := Last (Decls);
362 while Present (Decl) loop
364 -- A stub is always considered a referencer
366 if Nkind (Decl) in N_Body_Stub then
367 return True;
369 -- Package declaration
371 elsif Nkind (Decl) = N_Package_Declaration
372 and then not Has_Non_Subp_Const_Referencer
373 then
374 Spec := Specification (Decl);
376 -- Inspect the declarations of a non-generic package to try
377 -- and hide more entities from external visibility.
379 if not Is_Generic_Unit (Defining_Entity (Spec)) then
380 if Has_Referencer (Private_Declarations (Spec))
381 or else Has_Referencer (Visible_Declarations (Spec))
382 then
383 return True;
384 end if;
385 end if;
387 -- Package body
389 elsif Nkind (Decl) = N_Package_Body
390 and then Present (Corresponding_Spec (Decl))
391 then
392 Decl_Id := Corresponding_Spec (Decl);
394 -- A generic package body is a referencer. It would seem
395 -- that we only have to consider generics that can be
396 -- exported, i.e. where the corresponding spec is the
397 -- spec of the current package, but because of nested
398 -- instantiations, a fully private generic body may export
399 -- other private body entities. Furthermore, regardless of
400 -- whether there was a previous inlined subprogram, (an
401 -- instantiation of) the generic package may reference any
402 -- entity declared before it.
404 if Is_Generic_Unit (Decl_Id) then
405 return True;
407 -- Inspect the declarations of a non-generic package body to
408 -- try and hide more entities from external visibility.
410 elsif not Has_Non_Subp_Const_Referencer
411 and then Has_Referencer (Declarations (Decl))
412 then
413 return True;
414 end if;
416 -- Subprogram body
418 elsif Nkind (Decl) = N_Subprogram_Body then
419 if Present (Corresponding_Spec (Decl)) then
420 Decl_Id := Corresponding_Spec (Decl);
422 -- A generic subprogram body acts as a referencer
424 if Is_Generic_Unit (Decl_Id) then
425 return True;
426 end if;
428 -- An inlined subprogram body acts as a referencer
430 if Is_Inlined (Decl_Id)
431 or else Has_Pragma_Inline (Decl_Id)
432 then
433 -- Inspect the statements of the subprogram body
434 -- to determine whether the body references other
435 -- subprograms and/or non-static constants.
437 if Top_Level
438 and then not Contains_Subp_Or_Const_Refs (Decl)
439 then
440 Has_Non_Subp_Const_Referencer := True;
441 else
442 return True;
443 end if;
444 end if;
446 -- Otherwise this is a stand alone subprogram body
448 else
449 Decl_Id := Defining_Entity (Decl);
451 -- An inlined body acts as a referencer. Note that an
452 -- inlined subprogram remains Is_Public as gigi requires
453 -- the flag to be set.
455 -- Note that we test Has_Pragma_Inline here rather than
456 -- Is_Inlined. We are compiling this for a client, and
457 -- it is the client who will decide if actual inlining
458 -- should occur, so we need to assume that the procedure
459 -- could be inlined for the purpose of accessing global
460 -- entities.
462 if Has_Pragma_Inline (Decl_Id) then
463 if Top_Level
464 and then not Contains_Subp_Or_Const_Refs (Decl)
465 then
466 Has_Non_Subp_Const_Referencer := True;
467 else
468 return True;
469 end if;
470 else
471 Set_Is_Public (Decl_Id, False);
472 end if;
473 end if;
475 -- Exceptions, objects and renamings do not need to be public
476 -- if they are not followed by a construct which can reference
477 -- and export them. The Is_Public flag is reset on top level
478 -- entities only as anything nested is local to its context.
480 elsif Nkind_In (Decl, N_Exception_Declaration,
481 N_Object_Declaration,
482 N_Object_Renaming_Declaration,
483 N_Subprogram_Declaration,
484 N_Subprogram_Renaming_Declaration)
485 then
486 Decl_Id := Defining_Entity (Decl);
488 if Top_Level
489 and then not Is_Imported (Decl_Id)
490 and then not Is_Exported (Decl_Id)
491 and then No (Interface_Name (Decl_Id))
492 and then
493 (not Has_Non_Subp_Const_Referencer
494 or else Nkind (Decl) = N_Subprogram_Declaration)
495 then
496 Set_Is_Public (Decl_Id, False);
497 end if;
498 end if;
500 Prev (Decl);
501 end loop;
503 return Has_Non_Subp_Const_Referencer;
504 end Has_Referencer;
506 -- Local variables
508 Discard : Boolean := True;
509 pragma Unreferenced (Discard);
511 -- Start of processing for Hide_Public_Entities
513 begin
514 -- The algorithm examines the top level declarations of a package
515 -- body in reverse looking for a construct that may export entities
516 -- declared prior to it. If such a scenario is encountered, then all
517 -- entities in the range Last (Decls) .. construct are hidden from
518 -- external visibility. Consider:
520 -- package Pack is
521 -- generic
522 -- package Gen is
523 -- end Gen;
524 -- end Pack;
526 -- package body Pack is
527 -- External_Obj : ...; -- (1)
529 -- package body Gen is -- (2)
530 -- ... External_Obj ... -- (3)
531 -- end Gen;
533 -- Local_Obj : ...; -- (4)
534 -- end Pack;
536 -- In this example Local_Obj (4) must not be externally visible as
537 -- it cannot be exported by anything in Pack. The body of generic
538 -- package Gen (2) on the other hand acts as a "referencer" and may
539 -- export anything declared before it. Since the compiler does not
540 -- perform flow analysis, it is not possible to determine precisely
541 -- which entities will be exported when Gen is instantiated. In the
542 -- example above External_Obj (1) is exported at (3), but this may
543 -- not always be the case. The algorithm takes a conservative stance
544 -- and leaves entity External_Obj public.
546 Discard := Has_Referencer (Decls, Top_Level => True);
547 end Hide_Public_Entities;
549 ----------------------------------
550 -- Install_Composite_Operations --
551 ----------------------------------
553 procedure Install_Composite_Operations (P : Entity_Id) is
554 Id : Entity_Id;
556 begin
557 Id := First_Entity (P);
558 while Present (Id) loop
559 if Is_Type (Id)
560 and then (Is_Limited_Composite (Id)
561 or else Is_Private_Composite (Id))
562 and then No (Private_Component (Id))
563 then
564 Set_Is_Limited_Composite (Id, False);
565 Set_Is_Private_Composite (Id, False);
566 end if;
568 Next_Entity (Id);
569 end loop;
570 end Install_Composite_Operations;
572 -- Local variables
574 GM : constant Ghost_Mode_Type := Ghost_Mode;
575 Body_Id : Entity_Id;
576 HSS : Node_Id;
577 Last_Spec_Entity : Entity_Id;
578 New_N : Node_Id;
579 Pack_Decl : Node_Id;
580 Spec_Id : Entity_Id;
582 -- Start of processing for Analyze_Package_Body_Helper
584 begin
585 -- Find corresponding package specification, and establish the current
586 -- scope. The visible defining entity for the package is the defining
587 -- occurrence in the spec. On exit from the package body, all body
588 -- declarations are attached to the defining entity for the body, but
589 -- the later is never used for name resolution. In this fashion there
590 -- is only one visible entity that denotes the package.
592 -- Set Body_Id. Note that this will be reset to point to the generic
593 -- copy later on in the generic case.
595 Body_Id := Defining_Entity (N);
597 -- Body is body of package instantiation. Corresponding spec has already
598 -- been set.
600 if Present (Corresponding_Spec (N)) then
601 Spec_Id := Corresponding_Spec (N);
602 Pack_Decl := Unit_Declaration_Node (Spec_Id);
604 else
605 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
607 if Present (Spec_Id)
608 and then Is_Package_Or_Generic_Package (Spec_Id)
609 then
610 Pack_Decl := Unit_Declaration_Node (Spec_Id);
612 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
613 Error_Msg_N ("cannot supply body for package renaming", N);
614 return;
616 elsif Present (Corresponding_Body (Pack_Decl)) then
617 Error_Msg_N ("redefinition of package body", N);
618 return;
619 end if;
621 else
622 Error_Msg_N ("missing specification for package body", N);
623 return;
624 end if;
626 if Is_Package_Or_Generic_Package (Spec_Id)
627 and then (Scope (Spec_Id) = Standard_Standard
628 or else Is_Child_Unit (Spec_Id))
629 and then not Unit_Requires_Body (Spec_Id)
630 then
631 if Ada_Version = Ada_83 then
632 Error_Msg_N
633 ("optional package body (not allowed in Ada 95)??", N);
634 else
635 Error_Msg_N ("spec of this package does not allow a body", N);
636 end if;
637 end if;
638 end if;
640 -- The corresponding spec of the package body may be subject to pragma
641 -- Ghost with policy Ignore. Set the mode now to ensure that any nodes
642 -- generated during analysis and expansion are properly flagged as
643 -- ignored Ghost.
645 Set_Ghost_Mode (N, Spec_Id);
647 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
648 Style.Check_Identifier (Body_Id, Spec_Id);
650 if Is_Child_Unit (Spec_Id) then
651 if Nkind (Parent (N)) /= N_Compilation_Unit then
652 Error_Msg_NE
653 ("body of child unit& cannot be an inner package", N, Spec_Id);
654 end if;
656 Set_Is_Child_Unit (Body_Id);
657 end if;
659 -- Generic package case
661 if Ekind (Spec_Id) = E_Generic_Package then
663 -- Disable expansion and perform semantic analysis on copy. The
664 -- unannotated body will be used in all instantiations.
666 Body_Id := Defining_Entity (N);
667 Set_Ekind (Body_Id, E_Package_Body);
668 Set_Scope (Body_Id, Scope (Spec_Id));
669 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
670 Set_Body_Entity (Spec_Id, Body_Id);
671 Set_Spec_Entity (Body_Id, Spec_Id);
673 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
674 Rewrite (N, New_N);
676 -- Once the contents of the generic copy and the template are
677 -- swapped, do the same for their respective aspect specifications.
679 Exchange_Aspects (N, New_N);
681 -- Collect all contract-related source pragmas found within the
682 -- template and attach them to the contract of the package body.
683 -- This contract is used in the capture of global references within
684 -- annotations.
686 Create_Generic_Contract (N);
688 -- Update Body_Id to point to the copied node for the remainder of
689 -- the processing.
691 Body_Id := Defining_Entity (N);
692 Start_Generic;
693 end if;
695 -- The Body_Id is that of the copied node in the generic case, the
696 -- current node otherwise. Note that N was rewritten above, so we must
697 -- be sure to get the latest Body_Id value.
699 Set_Ekind (Body_Id, E_Package_Body);
700 Set_Body_Entity (Spec_Id, Body_Id);
701 Set_Spec_Entity (Body_Id, Spec_Id);
703 -- Defining name for the package body is not a visible entity: Only the
704 -- defining name for the declaration is visible.
706 Set_Etype (Body_Id, Standard_Void_Type);
707 Set_Scope (Body_Id, Scope (Spec_Id));
708 Set_Corresponding_Spec (N, Spec_Id);
709 Set_Corresponding_Body (Pack_Decl, Body_Id);
711 -- The body entity is not used for semantics or code generation, but
712 -- it is attached to the entity list of the enclosing scope to simplify
713 -- the listing of back-annotations for the types it main contain.
715 if Scope (Spec_Id) /= Standard_Standard then
716 Append_Entity (Body_Id, Scope (Spec_Id));
717 end if;
719 -- Indicate that we are currently compiling the body of the package
721 Set_In_Package_Body (Spec_Id);
722 Set_Has_Completion (Spec_Id);
723 Last_Spec_Entity := Last_Entity (Spec_Id);
725 if Has_Aspects (N) then
726 Analyze_Aspect_Specifications (N, Body_Id);
727 end if;
729 Push_Scope (Spec_Id);
731 -- Set SPARK_Mode only for non-generic package
733 if Ekind (Spec_Id) = E_Package then
735 -- Set SPARK_Mode from context
737 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
738 Set_SPARK_Pragma_Inherited (Body_Id, True);
740 -- Set elaboration code SPARK mode the same for now
742 Set_SPARK_Aux_Pragma (Body_Id, SPARK_Pragma (Body_Id));
743 Set_SPARK_Aux_Pragma_Inherited (Body_Id, True);
744 end if;
746 -- Inherit the "ghostness" of the subprogram spec. Note that this
747 -- property is not directly inherited as the body may be subject to a
748 -- different Ghost assertion policy.
750 if Is_Ghost_Entity (Spec_Id) or else Ghost_Mode > None then
751 Set_Is_Ghost_Entity (Body_Id);
753 -- The Ghost policy in effect at the point of declaration and at the
754 -- point of completion must match (SPARK RM 6.9(14)).
756 Check_Ghost_Completion (Spec_Id, Body_Id);
757 end if;
759 Set_Categorization_From_Pragmas (N);
761 Install_Visible_Declarations (Spec_Id);
762 Install_Private_Declarations (Spec_Id);
763 Install_Private_With_Clauses (Spec_Id);
764 Install_Composite_Operations (Spec_Id);
766 Check_Anonymous_Access_Types (Spec_Id, N);
768 if Ekind (Spec_Id) = E_Generic_Package then
769 Set_Use (Generic_Formal_Declarations (Pack_Decl));
770 end if;
772 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
773 Set_Use (Private_Declarations (Specification (Pack_Decl)));
775 -- This is a nested package, so it may be necessary to declare certain
776 -- inherited subprograms that are not yet visible because the parent
777 -- type's subprograms are now visible.
779 if Ekind (Scope (Spec_Id)) = E_Package
780 and then Scope (Spec_Id) /= Standard_Standard
781 then
782 Declare_Inherited_Private_Subprograms (Spec_Id);
783 end if;
785 if Present (Declarations (N)) then
786 Analyze_Declarations (Declarations (N));
787 Inspect_Deferred_Constant_Completion (Declarations (N));
788 end if;
790 -- Verify that the SPARK_Mode of the body agrees with that of its spec
792 if Present (SPARK_Pragma (Body_Id)) then
793 if Present (SPARK_Aux_Pragma (Spec_Id)) then
794 if Get_SPARK_Mode_From_Pragma (SPARK_Aux_Pragma (Spec_Id)) = Off
795 and then
796 Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Body_Id)) = On
797 then
798 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
799 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
800 Error_Msg_Sloc := Sloc (SPARK_Aux_Pragma (Spec_Id));
801 Error_Msg_NE
802 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
803 end if;
805 else
806 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
807 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
808 Error_Msg_Sloc := Sloc (Spec_Id);
809 Error_Msg_NE
810 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
811 end if;
812 end if;
814 -- Analyze_Declarations has caused freezing of all types. Now generate
815 -- bodies for RACW primitives and stream attributes, if any.
817 if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then
819 -- Attach subprogram bodies to support RACWs declared in spec
821 Append_RACW_Bodies (Declarations (N), Spec_Id);
822 Analyze_List (Declarations (N));
823 end if;
825 HSS := Handled_Statement_Sequence (N);
827 if Present (HSS) then
828 Process_End_Label (HSS, 't', Spec_Id);
829 Analyze (HSS);
831 -- Check that elaboration code in a preelaborable package body is
832 -- empty other than null statements and labels (RM 10.2.1(6)).
834 Validate_Null_Statement_Sequence (N);
835 end if;
837 Validate_Categorization_Dependency (N, Spec_Id);
838 Check_Completion (Body_Id);
840 -- Generate start of body reference. Note that we do this fairly late,
841 -- because the call will use In_Extended_Main_Source_Unit as a check,
842 -- and we want to make sure that Corresponding_Stub links are set
844 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
846 -- For a generic package, collect global references and mark them on
847 -- the original body so that they are not resolved again at the point
848 -- of instantiation.
850 if Ekind (Spec_Id) /= E_Package then
851 Save_Global_References (Original_Node (N));
852 End_Generic;
853 end if;
855 -- The entities of the package body have so far been chained onto the
856 -- declaration chain for the spec. That's been fine while we were in the
857 -- body, since we wanted them to be visible, but now that we are leaving
858 -- the package body, they are no longer visible, so we remove them from
859 -- the entity chain of the package spec entity, and copy them to the
860 -- entity chain of the package body entity, where they will never again
861 -- be visible.
863 if Present (Last_Spec_Entity) then
864 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
865 Set_Next_Entity (Last_Spec_Entity, Empty);
866 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
867 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
869 else
870 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
871 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
872 Set_First_Entity (Spec_Id, Empty);
873 Set_Last_Entity (Spec_Id, Empty);
874 end if;
876 End_Package_Scope (Spec_Id);
878 -- All entities declared in body are not visible
880 declare
881 E : Entity_Id;
883 begin
884 E := First_Entity (Body_Id);
885 while Present (E) loop
886 Set_Is_Immediately_Visible (E, False);
887 Set_Is_Potentially_Use_Visible (E, False);
888 Set_Is_Hidden (E);
890 -- Child units may appear on the entity list (e.g. if they appear
891 -- in the context of a subunit) but they are not body entities.
893 if not Is_Child_Unit (E) then
894 Set_Is_Package_Body_Entity (E);
895 end if;
897 Next_Entity (E);
898 end loop;
899 end;
901 Check_References (Body_Id);
903 -- For a generic unit, check that the formal parameters are referenced,
904 -- and that local variables are used, as for regular packages.
906 if Ekind (Spec_Id) = E_Generic_Package then
907 Check_References (Spec_Id);
908 end if;
910 -- At this point all entities of the package body are externally visible
911 -- to the linker as their Is_Public flag is set to True. This proactive
912 -- approach is necessary because an inlined or a generic body for which
913 -- code is generated in other units may need to see these entities. Cut
914 -- down the number of global symbols that do not neet public visibility
915 -- as this has two beneficial effects:
916 -- (1) It makes the compilation process more efficient.
917 -- (2) It gives the code generatormore freedom to optimize within each
918 -- unit, especially subprograms.
920 -- This is done only for top level library packages or child units as
921 -- the algorithm does a top down traversal of the package body.
923 if (Scope (Spec_Id) = Standard_Standard or else Is_Child_Unit (Spec_Id))
924 and then not Is_Generic_Unit (Spec_Id)
925 then
926 Hide_Public_Entities (Declarations (N));
927 end if;
929 -- If expander is not active, then here is where we turn off the
930 -- In_Package_Body flag, otherwise it is turned off at the end of the
931 -- corresponding expansion routine. If this is an instance body, we need
932 -- to qualify names of local entities, because the body may have been
933 -- compiled as a preliminary to another instantiation.
935 if not Expander_Active then
936 Set_In_Package_Body (Spec_Id, False);
938 if Is_Generic_Instance (Spec_Id)
939 and then Operating_Mode = Generate_Code
940 then
941 Qualify_Entity_Names (N);
942 end if;
943 end if;
945 -- Restore the original Ghost mode once analysis and expansion have
946 -- taken place.
948 Ghost_Mode := GM;
949 end Analyze_Package_Body_Helper;
951 ------------------------------
952 -- Analyze_Package_Contract --
953 ------------------------------
955 procedure Analyze_Package_Contract (Pack_Id : Entity_Id) is
956 Items : constant Node_Id := Contract (Pack_Id);
957 Init : Node_Id := Empty;
958 Init_Cond : Node_Id := Empty;
959 Mode : SPARK_Mode_Type;
960 Prag : Node_Id;
961 Prag_Nam : Name_Id;
963 begin
964 -- Due to the timing of contract analysis, delayed pragmas may be
965 -- subject to the wrong SPARK_Mode, usually that of the enclosing
966 -- context. To remedy this, restore the original SPARK_Mode of the
967 -- related package.
969 Save_SPARK_Mode_And_Set (Pack_Id, Mode);
971 if Present (Items) then
973 -- Locate and store pragmas Initial_Condition and Initializes since
974 -- their order of analysis matters.
976 Prag := Classifications (Items);
977 while Present (Prag) loop
978 Prag_Nam := Pragma_Name (Prag);
980 if Prag_Nam = Name_Initial_Condition then
981 Init_Cond := Prag;
983 elsif Prag_Nam = Name_Initializes then
984 Init := Prag;
985 end if;
987 Prag := Next_Pragma (Prag);
988 end loop;
990 -- Analyze the initialization related pragmas. Initializes must come
991 -- before Initial_Condition due to item dependencies.
993 if Present (Init) then
994 Analyze_Initializes_In_Decl_Part (Init);
995 end if;
997 if Present (Init_Cond) then
998 Analyze_Initial_Condition_In_Decl_Part (Init_Cond);
999 end if;
1000 end if;
1002 -- Check whether the lack of indicator Part_Of agrees with the placement
1003 -- of the package instantiation with respect to the state space.
1005 if Is_Generic_Instance (Pack_Id) then
1006 Prag := Get_Pragma (Pack_Id, Pragma_Part_Of);
1008 if No (Prag) then
1009 Check_Missing_Part_Of (Pack_Id);
1010 end if;
1011 end if;
1013 -- Restore the SPARK_Mode of the enclosing context after all delayed
1014 -- pragmas have been analyzed.
1016 Restore_SPARK_Mode (Mode);
1017 end Analyze_Package_Contract;
1019 ---------------------------------
1020 -- Analyze_Package_Declaration --
1021 ---------------------------------
1023 procedure Analyze_Package_Declaration (N : Node_Id) is
1024 GM : constant Ghost_Mode_Type := Ghost_Mode;
1026 procedure Restore_Globals;
1027 -- Restore the values of all saved global variables
1029 ---------------------
1030 -- Restore_Globals --
1031 ---------------------
1033 procedure Restore_Globals is
1034 begin
1035 Ghost_Mode := GM;
1036 end Restore_Globals;
1038 -- Local variables
1040 Id : constant Node_Id := Defining_Entity (N);
1042 Body_Required : Boolean;
1043 -- True when this package declaration requires a corresponding body
1045 Comp_Unit : Boolean;
1046 -- True when this package declaration is not a nested declaration
1048 PF : Boolean;
1049 -- True when in the context of a declared pure library unit
1051 -- Start of processing for Analyze_Package_Declaration
1053 begin
1054 if Debug_Flag_C then
1055 Write_Str ("==> package spec ");
1056 Write_Name (Chars (Id));
1057 Write_Str (" from ");
1058 Write_Location (Sloc (N));
1059 Write_Eol;
1060 Indent;
1061 end if;
1063 -- The package declaration may be subject to pragma Ghost with policy
1064 -- Ignore. Set the mode now to ensure that any nodes generated during
1065 -- analysis and expansion are properly flagged as ignored Ghost.
1067 Set_Ghost_Mode (N);
1069 Generate_Definition (Id);
1070 Enter_Name (Id);
1071 Set_Ekind (Id, E_Package);
1072 Set_Etype (Id, Standard_Void_Type);
1074 -- Set SPARK_Mode from context only for non-generic package
1076 if Ekind (Id) = E_Package then
1077 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
1078 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
1079 Set_SPARK_Pragma_Inherited (Id, True);
1080 Set_SPARK_Aux_Pragma_Inherited (Id, True);
1081 end if;
1083 -- A package declared within a Ghost refion is automatically Ghost
1084 -- (SPARK RM 6.9(2)).
1086 if Ghost_Mode > None then
1087 Set_Is_Ghost_Entity (Id);
1088 end if;
1090 -- Analyze aspect specifications immediately, since we need to recognize
1091 -- things like Pure early enough to diagnose violations during analysis.
1093 if Has_Aspects (N) then
1094 Analyze_Aspect_Specifications (N, Id);
1095 end if;
1097 -- Ada 2005 (AI-217): Check if the package has been illegally named
1098 -- in a limited-with clause of its own context. In this case the error
1099 -- has been previously notified by Analyze_Context.
1101 -- limited with Pkg; -- ERROR
1102 -- package Pkg is ...
1104 if From_Limited_With (Id) then
1105 Restore_Globals;
1106 return;
1107 end if;
1109 Push_Scope (Id);
1111 PF := Is_Pure (Enclosing_Lib_Unit_Entity);
1112 Set_Is_Pure (Id, PF);
1114 Set_Categorization_From_Pragmas (N);
1116 Analyze (Specification (N));
1117 Validate_Categorization_Dependency (N, Id);
1119 Body_Required := Unit_Requires_Body (Id);
1121 -- When this spec does not require an explicit body, we know that there
1122 -- are no entities requiring completion in the language sense; we call
1123 -- Check_Completion here only to ensure that any nested package
1124 -- declaration that requires an implicit body gets one. (In the case
1125 -- where a body is required, Check_Completion is called at the end of
1126 -- the body's declarative part.)
1128 if not Body_Required then
1129 Check_Completion;
1130 end if;
1132 Comp_Unit := Nkind (Parent (N)) = N_Compilation_Unit;
1134 if Comp_Unit then
1136 -- Set Body_Required indication on the compilation unit node, and
1137 -- determine whether elaboration warnings may be meaningful on it.
1139 Set_Body_Required (Parent (N), Body_Required);
1141 if not Body_Required then
1142 Set_Suppress_Elaboration_Warnings (Id);
1143 end if;
1144 end if;
1146 End_Package_Scope (Id);
1148 -- For the declaration of a library unit that is a remote types package,
1149 -- check legality rules regarding availability of stream attributes for
1150 -- types that contain non-remote access values. This subprogram performs
1151 -- visibility tests that rely on the fact that we have exited the scope
1152 -- of Id.
1154 if Comp_Unit then
1155 Validate_RT_RAT_Component (N);
1156 end if;
1158 if Debug_Flag_C then
1159 Outdent;
1160 Write_Str ("<== package spec ");
1161 Write_Name (Chars (Id));
1162 Write_Str (" from ");
1163 Write_Location (Sloc (N));
1164 Write_Eol;
1165 end if;
1167 Restore_Globals;
1168 end Analyze_Package_Declaration;
1170 -----------------------------------
1171 -- Analyze_Package_Specification --
1172 -----------------------------------
1174 -- Note that this code is shared for the analysis of generic package specs
1175 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
1177 procedure Analyze_Package_Specification (N : Node_Id) is
1178 Id : constant Entity_Id := Defining_Entity (N);
1179 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
1180 Vis_Decls : constant List_Id := Visible_Declarations (N);
1181 Priv_Decls : constant List_Id := Private_Declarations (N);
1182 E : Entity_Id;
1183 L : Entity_Id;
1184 Public_Child : Boolean;
1186 Private_With_Clauses_Installed : Boolean := False;
1187 -- In Ada 2005, private with_clauses are visible in the private part
1188 -- of a nested package, even if it appears in the public part of the
1189 -- enclosing package. This requires a separate step to install these
1190 -- private_with_clauses, and remove them at the end of the nested
1191 -- package.
1193 procedure Check_One_Tagged_Type_Or_Extension_At_Most;
1194 -- Issue an error in SPARK mode if a package specification contains
1195 -- more than one tagged type or type extension.
1197 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
1198 -- Clears constant indications (Never_Set_In_Source, Constant_Value, and
1199 -- Is_True_Constant) on all variables that are entities of Id, and on
1200 -- the chain whose first element is FE. A recursive call is made for all
1201 -- packages and generic packages.
1203 procedure Generate_Parent_References;
1204 -- For a child unit, generate references to parent units, for
1205 -- GPS navigation purposes.
1207 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
1208 -- Child and Unit are entities of compilation units. True if Child
1209 -- is a public child of Parent as defined in 10.1.1
1211 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
1212 -- Reject completion of an incomplete or private type declarations
1213 -- having a known discriminant part by an unchecked union.
1215 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
1216 -- Given the package entity of a generic package instantiation or
1217 -- formal package whose corresponding generic is a child unit, installs
1218 -- the private declarations of each of the child unit's parents.
1219 -- This has to be done at the point of entering the instance package's
1220 -- private part rather than being done in Sem_Ch12.Install_Parent
1221 -- (which is where the parents' visible declarations are installed).
1223 ------------------------------------------------
1224 -- Check_One_Tagged_Type_Or_Extension_At_Most --
1225 ------------------------------------------------
1227 procedure Check_One_Tagged_Type_Or_Extension_At_Most is
1228 Previous : Node_Id;
1230 procedure Check_Decls (Decls : List_Id);
1231 -- Check that either Previous is Empty and Decls does not contain
1232 -- more than one tagged type or type extension, or Previous is
1233 -- already set and Decls contains no tagged type or type extension.
1235 -----------------
1236 -- Check_Decls --
1237 -----------------
1239 procedure Check_Decls (Decls : List_Id) is
1240 Decl : Node_Id;
1242 begin
1243 Decl := First (Decls);
1244 while Present (Decl) loop
1245 if Nkind (Decl) = N_Full_Type_Declaration
1246 and then Is_Tagged_Type (Defining_Identifier (Decl))
1247 then
1248 if No (Previous) then
1249 Previous := Decl;
1251 else
1252 Error_Msg_Sloc := Sloc (Previous);
1253 Check_SPARK_05_Restriction
1254 ("at most one tagged type or type extension allowed",
1255 "\\ previous declaration#",
1256 Decl);
1257 end if;
1258 end if;
1260 Next (Decl);
1261 end loop;
1262 end Check_Decls;
1264 -- Start of processing for Check_One_Tagged_Type_Or_Extension_At_Most
1266 begin
1267 Previous := Empty;
1268 Check_Decls (Vis_Decls);
1270 if Present (Priv_Decls) then
1271 Check_Decls (Priv_Decls);
1272 end if;
1273 end Check_One_Tagged_Type_Or_Extension_At_Most;
1275 ---------------------
1276 -- Clear_Constants --
1277 ---------------------
1279 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
1280 E : Entity_Id;
1282 begin
1283 -- Ignore package renamings, not interesting and they can cause self
1284 -- referential loops in the code below.
1286 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
1287 return;
1288 end if;
1290 -- Note: in the loop below, the check for Next_Entity pointing back
1291 -- to the package entity may seem odd, but it is needed, because a
1292 -- package can contain a renaming declaration to itself, and such
1293 -- renamings are generated automatically within package instances.
1295 E := FE;
1296 while Present (E) and then E /= Id loop
1297 if Is_Assignable (E) then
1298 Set_Never_Set_In_Source (E, False);
1299 Set_Is_True_Constant (E, False);
1300 Set_Current_Value (E, Empty);
1301 Set_Is_Known_Null (E, False);
1302 Set_Last_Assignment (E, Empty);
1304 if not Can_Never_Be_Null (E) then
1305 Set_Is_Known_Non_Null (E, False);
1306 end if;
1308 elsif Is_Package_Or_Generic_Package (E) then
1309 Clear_Constants (E, First_Entity (E));
1310 Clear_Constants (E, First_Private_Entity (E));
1311 end if;
1313 Next_Entity (E);
1314 end loop;
1315 end Clear_Constants;
1317 --------------------------------
1318 -- Generate_Parent_References --
1319 --------------------------------
1321 procedure Generate_Parent_References is
1322 Decl : constant Node_Id := Parent (N);
1324 begin
1325 if Id = Cunit_Entity (Main_Unit)
1326 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
1327 then
1328 Generate_Reference (Id, Scope (Id), 'k', False);
1330 elsif not Nkind_In (Unit (Cunit (Main_Unit)), N_Subprogram_Body,
1331 N_Subunit)
1332 then
1333 -- If current unit is an ancestor of main unit, generate a
1334 -- reference to its own parent.
1336 declare
1337 U : Node_Id;
1338 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
1340 begin
1341 if Nkind (Main_Spec) = N_Package_Body then
1342 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
1343 end if;
1345 U := Parent_Spec (Main_Spec);
1346 while Present (U) loop
1347 if U = Parent (Decl) then
1348 Generate_Reference (Id, Scope (Id), 'k', False);
1349 exit;
1351 elsif Nkind (Unit (U)) = N_Package_Body then
1352 exit;
1354 else
1355 U := Parent_Spec (Unit (U));
1356 end if;
1357 end loop;
1358 end;
1359 end if;
1360 end Generate_Parent_References;
1362 ---------------------
1363 -- Is_Public_Child --
1364 ---------------------
1366 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
1367 begin
1368 if not Is_Private_Descendant (Child) then
1369 return True;
1370 else
1371 if Child = Unit then
1372 return not Private_Present (
1373 Parent (Unit_Declaration_Node (Child)));
1374 else
1375 return Is_Public_Child (Scope (Child), Unit);
1376 end if;
1377 end if;
1378 end Is_Public_Child;
1380 ----------------------------------------
1381 -- Inspect_Unchecked_Union_Completion --
1382 ----------------------------------------
1384 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
1385 Decl : Node_Id;
1387 begin
1388 Decl := First (Decls);
1389 while Present (Decl) loop
1391 -- We are looking at an incomplete or private type declaration
1392 -- with a known_discriminant_part whose full view is an
1393 -- Unchecked_Union.
1395 if Nkind_In (Decl, N_Incomplete_Type_Declaration,
1396 N_Private_Type_Declaration)
1397 and then Has_Discriminants (Defining_Identifier (Decl))
1398 and then Present (Full_View (Defining_Identifier (Decl)))
1399 and then
1400 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
1401 then
1402 Error_Msg_N
1403 ("completion of discriminated partial view "
1404 & "cannot be an unchecked union",
1405 Full_View (Defining_Identifier (Decl)));
1406 end if;
1408 Next (Decl);
1409 end loop;
1410 end Inspect_Unchecked_Union_Completion;
1412 -----------------------------------------
1413 -- Install_Parent_Private_Declarations --
1414 -----------------------------------------
1416 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
1417 Inst_Par : Entity_Id;
1418 Gen_Par : Entity_Id;
1419 Inst_Node : Node_Id;
1421 begin
1422 Inst_Par := Inst_Id;
1424 Gen_Par :=
1425 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
1426 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
1427 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
1429 if Nkind_In (Inst_Node, N_Package_Instantiation,
1430 N_Formal_Package_Declaration)
1431 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
1432 then
1433 Inst_Par := Entity (Prefix (Name (Inst_Node)));
1435 if Present (Renamed_Entity (Inst_Par)) then
1436 Inst_Par := Renamed_Entity (Inst_Par);
1437 end if;
1439 Gen_Par :=
1440 Generic_Parent
1441 (Specification (Unit_Declaration_Node (Inst_Par)));
1443 -- Install the private declarations and private use clauses
1444 -- of a parent instance of the child instance, unless the
1445 -- parent instance private declarations have already been
1446 -- installed earlier in Analyze_Package_Specification, which
1447 -- happens when a generic child is instantiated, and the
1448 -- instance is a child of the parent instance.
1450 -- Installing the use clauses of the parent instance twice
1451 -- is both unnecessary and wrong, because it would cause the
1452 -- clauses to be chained to themselves in the use clauses
1453 -- list of the scope stack entry. That in turn would cause
1454 -- an endless loop from End_Use_Clauses upon scope exit.
1456 -- The parent is now fully visible. It may be a hidden open
1457 -- scope if we are currently compiling some child instance
1458 -- declared within it, but while the current instance is being
1459 -- compiled the parent is immediately visible. In particular
1460 -- its entities must remain visible if a stack save/restore
1461 -- takes place through a call to Rtsfind.
1463 if Present (Gen_Par) then
1464 if not In_Private_Part (Inst_Par) then
1465 Install_Private_Declarations (Inst_Par);
1466 Set_Use (Private_Declarations
1467 (Specification
1468 (Unit_Declaration_Node (Inst_Par))));
1469 Set_Is_Hidden_Open_Scope (Inst_Par, False);
1470 end if;
1472 -- If we've reached the end of the generic instance parents,
1473 -- then finish off by looping through the nongeneric parents
1474 -- and installing their private declarations.
1476 -- If one of the non-generic parents is itself on the scope
1477 -- stack, do not install its private declarations: they are
1478 -- installed in due time when the private part of that parent
1479 -- is analyzed. This is delicate ???
1481 else
1482 while Present (Inst_Par)
1483 and then Inst_Par /= Standard_Standard
1484 and then (not In_Open_Scopes (Inst_Par)
1485 or else not In_Private_Part (Inst_Par))
1486 loop
1487 Install_Private_Declarations (Inst_Par);
1488 Set_Use (Private_Declarations
1489 (Specification
1490 (Unit_Declaration_Node (Inst_Par))));
1491 Inst_Par := Scope (Inst_Par);
1492 end loop;
1494 exit;
1495 end if;
1497 else
1498 exit;
1499 end if;
1500 end loop;
1501 end Install_Parent_Private_Declarations;
1503 -- Start of processing for Analyze_Package_Specification
1505 begin
1506 if Present (Vis_Decls) then
1507 Analyze_Declarations (Vis_Decls);
1508 end if;
1510 -- Inspect the entities defined in the package and ensure that all
1511 -- incomplete types have received full declarations. Build default
1512 -- initial condition and invariant procedures for all qualifying types.
1514 E := First_Entity (Id);
1515 while Present (E) loop
1517 -- Check on incomplete types
1519 -- AI05-0213: A formal incomplete type has no completion
1521 if Ekind (E) = E_Incomplete_Type
1522 and then No (Full_View (E))
1523 and then not Is_Generic_Type (E)
1524 then
1525 Error_Msg_N ("no declaration in visible part for incomplete}", E);
1526 end if;
1528 if Is_Type (E) then
1530 -- Each private type subject to pragma Default_Initial_Condition
1531 -- declares a specialized procedure which verifies the assumption
1532 -- of the pragma. The declaration appears in the visible part of
1533 -- the package to allow for being called from the outside.
1535 if Has_Default_Init_Cond (E) then
1536 Build_Default_Init_Cond_Procedure_Declaration (E);
1538 -- A private extension inherits the default initial condition
1539 -- procedure from its parent type.
1541 elsif Has_Inherited_Default_Init_Cond (E) then
1542 Inherit_Default_Init_Cond_Procedure (E);
1543 end if;
1545 -- If invariants are present, build the invariant procedure for a
1546 -- private type, but not any of its subtypes or interface types.
1548 if Has_Invariants (E) then
1549 if Ekind (E) = E_Private_Subtype then
1550 null;
1551 else
1552 Build_Invariant_Procedure (E, N);
1553 end if;
1554 end if;
1555 end if;
1557 Next_Entity (E);
1558 end loop;
1560 if Is_Remote_Call_Interface (Id)
1561 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1562 then
1563 Validate_RCI_Declarations (Id);
1564 end if;
1566 -- Save global references in the visible declarations, before installing
1567 -- private declarations of parent unit if there is one, because the
1568 -- privacy status of types defined in the parent will change. This is
1569 -- only relevant for generic child units, but is done in all cases for
1570 -- uniformity.
1572 if Ekind (Id) = E_Generic_Package
1573 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1574 then
1575 declare
1576 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1577 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1578 begin
1579 Set_Private_Declarations (Orig_Spec, Empty_List);
1580 Save_Global_References (Orig_Decl);
1581 Set_Private_Declarations (Orig_Spec, Save_Priv);
1582 end;
1583 end if;
1585 -- If package is a public child unit, then make the private declarations
1586 -- of the parent visible.
1588 Public_Child := False;
1590 declare
1591 Par : Entity_Id;
1592 Pack_Decl : Node_Id;
1593 Par_Spec : Node_Id;
1595 begin
1596 Par := Id;
1597 Par_Spec := Parent_Spec (Parent (N));
1599 -- If the package is formal package of an enclosing generic, it is
1600 -- transformed into a local generic declaration, and compiled to make
1601 -- its spec available. We need to retrieve the original generic to
1602 -- determine whether it is a child unit, and install its parents.
1604 if No (Par_Spec)
1605 and then
1606 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1607 then
1608 Par := Entity (Name (Original_Node (Parent (N))));
1609 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1610 end if;
1612 if Present (Par_Spec) then
1613 Generate_Parent_References;
1615 while Scope (Par) /= Standard_Standard
1616 and then Is_Public_Child (Id, Par)
1617 and then In_Open_Scopes (Par)
1618 loop
1619 Public_Child := True;
1620 Par := Scope (Par);
1621 Install_Private_Declarations (Par);
1622 Install_Private_With_Clauses (Par);
1623 Pack_Decl := Unit_Declaration_Node (Par);
1624 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1625 end loop;
1626 end if;
1627 end;
1629 if Is_Compilation_Unit (Id) then
1630 Install_Private_With_Clauses (Id);
1631 else
1633 -- The current compilation unit may include private with_clauses,
1634 -- which are visible in the private part of the current nested
1635 -- package, and have to be installed now. This is not done for
1636 -- nested instantiations, where the private with_clauses of the
1637 -- enclosing unit have no effect once the instantiation info is
1638 -- established and we start analyzing the package declaration.
1640 declare
1641 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1642 begin
1643 if Is_Package_Or_Generic_Package (Comp_Unit)
1644 and then not In_Private_Part (Comp_Unit)
1645 and then not In_Instance
1646 then
1647 Install_Private_With_Clauses (Comp_Unit);
1648 Private_With_Clauses_Installed := True;
1649 end if;
1650 end;
1651 end if;
1653 -- If this is a package associated with a generic instance or formal
1654 -- package, then the private declarations of each of the generic's
1655 -- parents must be installed at this point.
1657 if Is_Generic_Instance (Id) then
1658 Install_Parent_Private_Declarations (Id);
1659 end if;
1661 -- Analyze private part if present. The flag In_Private_Part is reset
1662 -- in End_Package_Scope.
1664 L := Last_Entity (Id);
1666 if Present (Priv_Decls) then
1667 Set_In_Private_Part (Id);
1669 -- Upon entering a public child's private part, it may be necessary
1670 -- to declare subprograms that were derived in the package's visible
1671 -- part but not yet made visible.
1673 if Public_Child then
1674 Declare_Inherited_Private_Subprograms (Id);
1675 end if;
1677 Analyze_Declarations (Priv_Decls);
1679 -- Check the private declarations for incomplete deferred constants
1681 Inspect_Deferred_Constant_Completion (Priv_Decls);
1683 -- The first private entity is the immediate follower of the last
1684 -- visible entity, if there was one.
1686 if Present (L) then
1687 Set_First_Private_Entity (Id, Next_Entity (L));
1688 else
1689 Set_First_Private_Entity (Id, First_Entity (Id));
1690 end if;
1692 -- There may be inherited private subprograms that need to be declared,
1693 -- even in the absence of an explicit private part. If there are any
1694 -- public declarations in the package and the package is a public child
1695 -- unit, then an implicit private part is assumed.
1697 elsif Present (L) and then Public_Child then
1698 Set_In_Private_Part (Id);
1699 Declare_Inherited_Private_Subprograms (Id);
1700 Set_First_Private_Entity (Id, Next_Entity (L));
1701 end if;
1703 E := First_Entity (Id);
1704 while Present (E) loop
1706 -- Check rule of 3.6(11), which in general requires waiting till all
1707 -- full types have been seen.
1709 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1710 Check_Aliased_Component_Types (E);
1711 end if;
1713 -- Check preelaborable initialization for full type completing a
1714 -- private type for which pragma Preelaborable_Initialization given.
1716 if Is_Type (E)
1717 and then Must_Have_Preelab_Init (E)
1718 and then not Has_Preelaborable_Initialization (E)
1719 then
1720 Error_Msg_N
1721 ("full view of & does not have preelaborable initialization", E);
1722 end if;
1724 -- An invariant may appear on a full view of a type
1726 if Is_Type (E)
1727 and then Has_Private_Declaration (E)
1728 and then Nkind (Parent (E)) = N_Full_Type_Declaration
1729 then
1730 declare
1731 IP_Built : Boolean := False;
1733 begin
1734 if Has_Aspects (Parent (E)) then
1735 declare
1736 ASN : Node_Id;
1738 begin
1739 ASN := First (Aspect_Specifications (Parent (E)));
1740 while Present (ASN) loop
1741 if Nam_In (Chars (Identifier (ASN)),
1742 Name_Invariant,
1743 Name_Type_Invariant)
1744 then
1745 Build_Invariant_Procedure (E, N);
1746 IP_Built := True;
1747 exit;
1748 end if;
1750 Next (ASN);
1751 end loop;
1752 end;
1753 end if;
1755 -- Invariants may have been inherited from progenitors
1757 if not IP_Built
1758 and then Has_Interfaces (E)
1759 and then Has_Inheritable_Invariants (E)
1760 and then not Is_Interface (E)
1761 and then not Is_Class_Wide_Type (E)
1762 then
1763 Build_Invariant_Procedure (E, N);
1764 end if;
1765 end;
1766 end if;
1768 Next_Entity (E);
1769 end loop;
1771 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1772 -- declaration having a known_discriminant_part shall not be an
1773 -- unchecked union type.
1775 if Present (Vis_Decls) then
1776 Inspect_Unchecked_Union_Completion (Vis_Decls);
1777 end if;
1779 if Present (Priv_Decls) then
1780 Inspect_Unchecked_Union_Completion (Priv_Decls);
1781 end if;
1783 if Ekind (Id) = E_Generic_Package
1784 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1785 and then Present (Priv_Decls)
1786 then
1787 -- Save global references in private declarations, ignoring the
1788 -- visible declarations that were processed earlier.
1790 declare
1791 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1792 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1793 Save_Form : constant List_Id :=
1794 Generic_Formal_Declarations (Orig_Decl);
1796 begin
1797 Set_Visible_Declarations (Orig_Spec, Empty_List);
1798 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1799 Save_Global_References (Orig_Decl);
1800 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1801 Set_Visible_Declarations (Orig_Spec, Save_Vis);
1802 end;
1803 end if;
1805 Process_End_Label (N, 'e', Id);
1807 -- Remove private_with_clauses of enclosing compilation unit, if they
1808 -- were installed.
1810 if Private_With_Clauses_Installed then
1811 Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
1812 end if;
1814 -- For the case of a library level package, we must go through all the
1815 -- entities clearing the indications that the value may be constant and
1816 -- not modified. Why? Because any client of this package may modify
1817 -- these values freely from anywhere. This also applies to any nested
1818 -- packages or generic packages.
1820 -- For now we unconditionally clear constants for packages that are
1821 -- instances of generic packages. The reason is that we do not have the
1822 -- body yet, and we otherwise think things are unreferenced when they
1823 -- are not. This should be fixed sometime (the effect is not terrible,
1824 -- we just lose some warnings, and also some cases of value propagation)
1825 -- ???
1827 if Is_Library_Level_Entity (Id)
1828 or else Is_Generic_Instance (Id)
1829 then
1830 Clear_Constants (Id, First_Entity (Id));
1831 Clear_Constants (Id, First_Private_Entity (Id));
1832 end if;
1834 -- Issue an error in SPARK mode if a package specification contains
1835 -- more than one tagged type or type extension.
1837 Check_One_Tagged_Type_Or_Extension_At_Most;
1839 -- If switch set, output information on why body required
1841 if List_Body_Required_Info
1842 and then In_Extended_Main_Source_Unit (Id)
1843 and then Unit_Requires_Body (Id)
1844 then
1845 Unit_Requires_Body_Info (Id);
1846 end if;
1847 end Analyze_Package_Specification;
1849 --------------------------------------
1850 -- Analyze_Private_Type_Declaration --
1851 --------------------------------------
1853 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1854 GM : constant Ghost_Mode_Type := Ghost_Mode;
1855 Id : constant Entity_Id := Defining_Identifier (N);
1856 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
1858 begin
1859 -- The private type declaration may be subject to pragma Ghost with
1860 -- policy Ignore. Set the mode now to ensure that any nodes generated
1861 -- during analysis and expansion are properly flagged as ignored Ghost.
1863 Set_Ghost_Mode (N);
1865 Generate_Definition (Id);
1866 Set_Is_Pure (Id, PF);
1867 Init_Size_Align (Id);
1869 if not Is_Package_Or_Generic_Package (Current_Scope)
1870 or else In_Private_Part (Current_Scope)
1871 then
1872 Error_Msg_N ("invalid context for private declaration", N);
1873 end if;
1875 New_Private_Type (N, Id, N);
1876 Set_Depends_On_Private (Id);
1878 -- A type declared within a Ghost region is automatically Ghost
1879 -- (SPARK RM 6.9(2)).
1881 if Ghost_Mode > None then
1882 Set_Is_Ghost_Entity (Id);
1883 end if;
1885 if Has_Aspects (N) then
1886 Analyze_Aspect_Specifications (N, Id);
1887 end if;
1889 -- Restore the original Ghost mode once analysis and expansion have
1890 -- taken place.
1892 Ghost_Mode := GM;
1893 end Analyze_Private_Type_Declaration;
1895 ----------------------------------
1896 -- Check_Anonymous_Access_Types --
1897 ----------------------------------
1899 procedure Check_Anonymous_Access_Types
1900 (Spec_Id : Entity_Id;
1901 P_Body : Node_Id)
1903 E : Entity_Id;
1904 IR : Node_Id;
1906 begin
1907 -- Itype references are only needed by gigi, to force elaboration of
1908 -- itypes. In the absence of code generation, they are not needed.
1910 if not Expander_Active then
1911 return;
1912 end if;
1914 E := First_Entity (Spec_Id);
1915 while Present (E) loop
1916 if Ekind (E) = E_Anonymous_Access_Type
1917 and then From_Limited_With (E)
1918 then
1919 IR := Make_Itype_Reference (Sloc (P_Body));
1920 Set_Itype (IR, E);
1922 if No (Declarations (P_Body)) then
1923 Set_Declarations (P_Body, New_List (IR));
1924 else
1925 Prepend (IR, Declarations (P_Body));
1926 end if;
1927 end if;
1929 Next_Entity (E);
1930 end loop;
1931 end Check_Anonymous_Access_Types;
1933 -------------------------------------------
1934 -- Declare_Inherited_Private_Subprograms --
1935 -------------------------------------------
1937 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1939 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1940 -- Check whether an inherited subprogram S is an operation of an
1941 -- untagged derived type T.
1943 ---------------------
1944 -- Is_Primitive_Of --
1945 ---------------------
1947 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1948 Formal : Entity_Id;
1950 begin
1951 -- If the full view is a scalar type, the type is the anonymous base
1952 -- type, but the operation mentions the first subtype, so check the
1953 -- signature against the base type.
1955 if Base_Type (Etype (S)) = Base_Type (T) then
1956 return True;
1958 else
1959 Formal := First_Formal (S);
1960 while Present (Formal) loop
1961 if Base_Type (Etype (Formal)) = Base_Type (T) then
1962 return True;
1963 end if;
1965 Next_Formal (Formal);
1966 end loop;
1968 return False;
1969 end if;
1970 end Is_Primitive_Of;
1972 -- Local variables
1974 E : Entity_Id;
1975 Op_List : Elist_Id;
1976 Op_Elmt : Elmt_Id;
1977 Op_Elmt_2 : Elmt_Id;
1978 Prim_Op : Entity_Id;
1979 New_Op : Entity_Id := Empty;
1980 Parent_Subp : Entity_Id;
1981 Tag : Entity_Id;
1983 -- Start of processing for Declare_Inherited_Private_Subprograms
1985 begin
1986 E := First_Entity (Id);
1987 while Present (E) loop
1989 -- If the entity is a nonprivate type extension whose parent type
1990 -- is declared in an open scope, then the type may have inherited
1991 -- operations that now need to be made visible. Ditto if the entity
1992 -- is a formal derived type in a child unit.
1994 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
1995 or else
1996 (Nkind (Parent (E)) = N_Private_Extension_Declaration
1997 and then Is_Generic_Type (E)))
1998 and then In_Open_Scopes (Scope (Etype (E)))
1999 and then Is_Base_Type (E)
2000 then
2001 if Is_Tagged_Type (E) then
2002 Op_List := Primitive_Operations (E);
2003 New_Op := Empty;
2004 Tag := First_Tag_Component (E);
2006 Op_Elmt := First_Elmt (Op_List);
2007 while Present (Op_Elmt) loop
2008 Prim_Op := Node (Op_Elmt);
2010 -- Search primitives that are implicit operations with an
2011 -- internal name whose parent operation has a normal name.
2013 if Present (Alias (Prim_Op))
2014 and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
2015 and then not Comes_From_Source (Prim_Op)
2016 and then Is_Internal_Name (Chars (Prim_Op))
2017 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
2018 then
2019 Parent_Subp := Alias (Prim_Op);
2021 -- Case 1: Check if the type has also an explicit
2022 -- overriding for this primitive.
2024 Op_Elmt_2 := Next_Elmt (Op_Elmt);
2025 while Present (Op_Elmt_2) loop
2027 -- Skip entities with attribute Interface_Alias since
2028 -- they are not overriding primitives (these entities
2029 -- link an interface primitive with their covering
2030 -- primitive)
2032 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
2033 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
2034 and then No (Interface_Alias (Node (Op_Elmt_2)))
2035 then
2036 -- The private inherited operation has been
2037 -- overridden by an explicit subprogram:
2038 -- replace the former by the latter.
2040 New_Op := Node (Op_Elmt_2);
2041 Replace_Elmt (Op_Elmt, New_Op);
2042 Remove_Elmt (Op_List, Op_Elmt_2);
2043 Set_Overridden_Operation (New_Op, Parent_Subp);
2045 -- We don't need to inherit its dispatching slot.
2046 -- Set_All_DT_Position has previously ensured that
2047 -- the same slot was assigned to the two primitives
2049 if Present (Tag)
2050 and then Present (DTC_Entity (New_Op))
2051 and then Present (DTC_Entity (Prim_Op))
2052 then
2053 pragma Assert
2054 (DT_Position (New_Op) = DT_Position (Prim_Op));
2055 null;
2056 end if;
2058 goto Next_Primitive;
2059 end if;
2061 Next_Elmt (Op_Elmt_2);
2062 end loop;
2064 -- Case 2: We have not found any explicit overriding and
2065 -- hence we need to declare the operation (i.e., make it
2066 -- visible).
2068 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2070 -- Inherit the dispatching slot if E is already frozen
2072 if Is_Frozen (E)
2073 and then Present (DTC_Entity (Alias (Prim_Op)))
2074 then
2075 Set_DTC_Entity_Value (E, New_Op);
2076 Set_DT_Position_Value (New_Op,
2077 DT_Position (Alias (Prim_Op)));
2078 end if;
2080 pragma Assert
2081 (Is_Dispatching_Operation (New_Op)
2082 and then Node (Last_Elmt (Op_List)) = New_Op);
2084 -- Substitute the new operation for the old one in the
2085 -- type's primitive operations list. Since the new
2086 -- operation was also just added to the end of list,
2087 -- the last element must be removed.
2089 -- (Question: is there a simpler way of declaring the
2090 -- operation, say by just replacing the name of the
2091 -- earlier operation, reentering it in the in the symbol
2092 -- table (how?), and marking it as private???)
2094 Replace_Elmt (Op_Elmt, New_Op);
2095 Remove_Last_Elmt (Op_List);
2096 end if;
2098 <<Next_Primitive>>
2099 Next_Elmt (Op_Elmt);
2100 end loop;
2102 -- Generate listing showing the contents of the dispatch table
2104 if Debug_Flag_ZZ then
2105 Write_DT (E);
2106 end if;
2108 else
2109 -- For untagged type, scan forward to locate inherited hidden
2110 -- operations.
2112 Prim_Op := Next_Entity (E);
2113 while Present (Prim_Op) loop
2114 if Is_Subprogram (Prim_Op)
2115 and then Present (Alias (Prim_Op))
2116 and then not Comes_From_Source (Prim_Op)
2117 and then Is_Internal_Name (Chars (Prim_Op))
2118 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
2119 and then Is_Primitive_Of (E, Prim_Op)
2120 then
2121 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2122 end if;
2124 Next_Entity (Prim_Op);
2126 -- Derived operations appear immediately after the type
2127 -- declaration (or the following subtype indication for
2128 -- a derived scalar type). Further declarations cannot
2129 -- include inherited operations of the type.
2131 if Present (Prim_Op) then
2132 exit when Ekind (Prim_Op) not in Overloadable_Kind;
2133 end if;
2134 end loop;
2135 end if;
2136 end if;
2138 Next_Entity (E);
2139 end loop;
2140 end Declare_Inherited_Private_Subprograms;
2142 -----------------------
2143 -- End_Package_Scope --
2144 -----------------------
2146 procedure End_Package_Scope (P : Entity_Id) is
2147 begin
2148 Uninstall_Declarations (P);
2149 Pop_Scope;
2150 end End_Package_Scope;
2152 ---------------------------
2153 -- Exchange_Declarations --
2154 ---------------------------
2156 procedure Exchange_Declarations (Id : Entity_Id) is
2157 Full_Id : constant Entity_Id := Full_View (Id);
2158 H1 : constant Entity_Id := Homonym (Id);
2159 Next1 : constant Entity_Id := Next_Entity (Id);
2160 H2 : Entity_Id;
2161 Next2 : Entity_Id;
2163 begin
2164 -- If missing full declaration for type, nothing to exchange
2166 if No (Full_Id) then
2167 return;
2168 end if;
2170 -- Otherwise complete the exchange, and preserve semantic links
2172 Next2 := Next_Entity (Full_Id);
2173 H2 := Homonym (Full_Id);
2175 -- Reset full declaration pointer to reflect the switched entities and
2176 -- readjust the next entity chains.
2178 Exchange_Entities (Id, Full_Id);
2180 Set_Next_Entity (Id, Next1);
2181 Set_Homonym (Id, H1);
2183 Set_Full_View (Full_Id, Id);
2184 Set_Next_Entity (Full_Id, Next2);
2185 Set_Homonym (Full_Id, H2);
2186 end Exchange_Declarations;
2188 ----------------------------
2189 -- Install_Package_Entity --
2190 ----------------------------
2192 procedure Install_Package_Entity (Id : Entity_Id) is
2193 begin
2194 if not Is_Internal (Id) then
2195 if Debug_Flag_E then
2196 Write_Str ("Install: ");
2197 Write_Name (Chars (Id));
2198 Write_Eol;
2199 end if;
2201 if Is_Child_Unit (Id) then
2202 null;
2204 -- Do not enter implicitly inherited non-overridden subprograms of
2205 -- a tagged type back into visibility if they have non-conformant
2206 -- homographs (Ada RM 8.3 12.3/2).
2208 elsif Is_Hidden_Non_Overridden_Subpgm (Id) then
2209 null;
2211 else
2212 Set_Is_Immediately_Visible (Id);
2213 end if;
2214 end if;
2215 end Install_Package_Entity;
2217 ----------------------------------
2218 -- Install_Private_Declarations --
2219 ----------------------------------
2221 procedure Install_Private_Declarations (P : Entity_Id) is
2222 Id : Entity_Id;
2223 Full : Entity_Id;
2224 Priv_Deps : Elist_Id;
2226 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
2227 -- When the full view of a private type is made available, we do the
2228 -- same for its private dependents under proper visibility conditions.
2229 -- When compiling a grand-chid unit this needs to be done recursively.
2231 -----------------------------
2232 -- Swap_Private_Dependents --
2233 -----------------------------
2235 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
2236 Deps : Elist_Id;
2237 Priv : Entity_Id;
2238 Priv_Elmt : Elmt_Id;
2239 Is_Priv : Boolean;
2241 begin
2242 Priv_Elmt := First_Elmt (Priv_Deps);
2243 while Present (Priv_Elmt) loop
2244 Priv := Node (Priv_Elmt);
2246 -- Before the exchange, verify that the presence of the Full_View
2247 -- field. This field will be empty if the entity has already been
2248 -- installed due to a previous call.
2250 if Present (Full_View (Priv)) and then Is_Visible_Dependent (Priv)
2251 then
2252 if Is_Private_Type (Priv) then
2253 Deps := Private_Dependents (Priv);
2254 Is_Priv := True;
2255 else
2256 Is_Priv := False;
2257 end if;
2259 -- For each subtype that is swapped, we also swap the reference
2260 -- to it in Private_Dependents, to allow access to it when we
2261 -- swap them out in End_Package_Scope.
2263 Replace_Elmt (Priv_Elmt, Full_View (Priv));
2265 -- Ensure that both views of the dependent private subtype are
2266 -- immediately visible if within some open scope. Check full
2267 -- view before exchanging views.
2269 if In_Open_Scopes (Scope (Full_View (Priv))) then
2270 Set_Is_Immediately_Visible (Priv);
2271 end if;
2273 Exchange_Declarations (Priv);
2274 Set_Is_Immediately_Visible
2275 (Priv, In_Open_Scopes (Scope (Priv)));
2277 Set_Is_Potentially_Use_Visible
2278 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
2280 -- Within a child unit, recurse, except in generic child unit,
2281 -- which (unfortunately) handle private_dependents separately.
2283 if Is_Priv
2284 and then Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
2285 and then not Is_Empty_Elmt_List (Deps)
2286 and then not Inside_A_Generic
2287 then
2288 Swap_Private_Dependents (Deps);
2289 end if;
2290 end if;
2292 Next_Elmt (Priv_Elmt);
2293 end loop;
2294 end Swap_Private_Dependents;
2296 -- Start of processing for Install_Private_Declarations
2298 begin
2299 -- First exchange declarations for private types, so that the full
2300 -- declaration is visible. For each private type, we check its
2301 -- Private_Dependents list and also exchange any subtypes of or derived
2302 -- types from it. Finally, if this is a Taft amendment type, the
2303 -- incomplete declaration is irrelevant, and we want to link the
2304 -- eventual full declaration with the original private one so we
2305 -- also skip the exchange.
2307 Id := First_Entity (P);
2308 while Present (Id) and then Id /= First_Private_Entity (P) loop
2309 if Is_Private_Base_Type (Id)
2310 and then Present (Full_View (Id))
2311 and then Comes_From_Source (Full_View (Id))
2312 and then Scope (Full_View (Id)) = Scope (Id)
2313 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
2314 then
2315 -- If there is a use-type clause on the private type, set the full
2316 -- view accordingly.
2318 Set_In_Use (Full_View (Id), In_Use (Id));
2319 Full := Full_View (Id);
2321 if Is_Private_Base_Type (Full)
2322 and then Has_Private_Declaration (Full)
2323 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
2324 and then In_Open_Scopes (Scope (Etype (Full)))
2325 and then In_Package_Body (Current_Scope)
2326 and then not Is_Private_Type (Etype (Full))
2327 then
2328 -- This is the completion of a private type by a derivation
2329 -- from another private type which is not private anymore. This
2330 -- can only happen in a package nested within a child package,
2331 -- when the parent type is defined in the parent unit. At this
2332 -- point the current type is not private either, and we have
2333 -- to install the underlying full view, which is now visible.
2334 -- Save the current full view as well, so that all views can be
2335 -- restored on exit. It may seem that after compiling the child
2336 -- body there are not environments to restore, but the back-end
2337 -- expects those links to be valid, and freeze nodes depend on
2338 -- them.
2340 if No (Full_View (Full))
2341 and then Present (Underlying_Full_View (Full))
2342 then
2343 Set_Full_View (Id, Underlying_Full_View (Full));
2344 Set_Underlying_Full_View (Id, Full);
2346 Set_Underlying_Full_View (Full, Empty);
2347 Set_Is_Frozen (Full_View (Id));
2348 end if;
2349 end if;
2351 Priv_Deps := Private_Dependents (Id);
2352 Exchange_Declarations (Id);
2353 Set_Is_Immediately_Visible (Id);
2354 Swap_Private_Dependents (Priv_Deps);
2355 end if;
2357 Next_Entity (Id);
2358 end loop;
2360 -- Next make other declarations in the private part visible as well
2362 Id := First_Private_Entity (P);
2363 while Present (Id) loop
2364 Install_Package_Entity (Id);
2365 Set_Is_Hidden (Id, False);
2366 Next_Entity (Id);
2367 end loop;
2369 -- Indicate that the private part is currently visible, so it can be
2370 -- properly reset on exit.
2372 Set_In_Private_Part (P);
2373 end Install_Private_Declarations;
2375 ----------------------------------
2376 -- Install_Visible_Declarations --
2377 ----------------------------------
2379 procedure Install_Visible_Declarations (P : Entity_Id) is
2380 Id : Entity_Id;
2381 Last_Entity : Entity_Id;
2383 begin
2384 pragma Assert
2385 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
2387 if Is_Package_Or_Generic_Package (P) then
2388 Last_Entity := First_Private_Entity (P);
2389 else
2390 Last_Entity := Empty;
2391 end if;
2393 Id := First_Entity (P);
2394 while Present (Id) and then Id /= Last_Entity loop
2395 Install_Package_Entity (Id);
2396 Next_Entity (Id);
2397 end loop;
2398 end Install_Visible_Declarations;
2400 --------------------------
2401 -- Is_Private_Base_Type --
2402 --------------------------
2404 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
2405 begin
2406 return Ekind (E) = E_Private_Type
2407 or else Ekind (E) = E_Limited_Private_Type
2408 or else Ekind (E) = E_Record_Type_With_Private;
2409 end Is_Private_Base_Type;
2411 --------------------------
2412 -- Is_Visible_Dependent --
2413 --------------------------
2415 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
2417 S : constant Entity_Id := Scope (Dep);
2419 begin
2420 -- Renamings created for actual types have the visibility of the actual
2422 if Ekind (S) = E_Package
2423 and then Is_Generic_Instance (S)
2424 and then (Is_Generic_Actual_Type (Dep)
2425 or else Is_Generic_Actual_Type (Full_View (Dep)))
2426 then
2427 return True;
2429 elsif not (Is_Derived_Type (Dep))
2430 and then Is_Derived_Type (Full_View (Dep))
2431 then
2432 -- When instantiating a package body, the scope stack is empty, so
2433 -- check instead whether the dependent type is defined in the same
2434 -- scope as the instance itself.
2436 return In_Open_Scopes (S)
2437 or else (Is_Generic_Instance (Current_Scope)
2438 and then Scope (Dep) = Scope (Current_Scope));
2439 else
2440 return True;
2441 end if;
2442 end Is_Visible_Dependent;
2444 ----------------------------
2445 -- May_Need_Implicit_Body --
2446 ----------------------------
2448 procedure May_Need_Implicit_Body (E : Entity_Id) is
2449 P : constant Node_Id := Unit_Declaration_Node (E);
2450 S : constant Node_Id := Parent (P);
2451 B : Node_Id;
2452 Decls : List_Id;
2454 begin
2455 if not Has_Completion (E)
2456 and then Nkind (P) = N_Package_Declaration
2457 and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
2458 then
2459 B :=
2460 Make_Package_Body (Sloc (E),
2461 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
2462 Chars => Chars (E)),
2463 Declarations => New_List);
2465 if Nkind (S) = N_Package_Specification then
2466 if Present (Private_Declarations (S)) then
2467 Decls := Private_Declarations (S);
2468 else
2469 Decls := Visible_Declarations (S);
2470 end if;
2471 else
2472 Decls := Declarations (S);
2473 end if;
2475 Append (B, Decls);
2476 Analyze (B);
2477 end if;
2478 end May_Need_Implicit_Body;
2480 ----------------------
2481 -- New_Private_Type --
2482 ----------------------
2484 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
2485 begin
2486 -- For other than Ada 2012, enter the name in the current scope
2488 if Ada_Version < Ada_2012 then
2489 Enter_Name (Id);
2491 -- Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2492 -- there may be an incomplete previous view.
2494 else
2495 declare
2496 Prev : Entity_Id;
2497 begin
2498 Prev := Find_Type_Name (N);
2499 pragma Assert (Prev = Id
2500 or else (Ekind (Prev) = E_Incomplete_Type
2501 and then Present (Full_View (Prev))
2502 and then Full_View (Prev) = Id));
2503 end;
2504 end if;
2506 if Limited_Present (Def) then
2507 Set_Ekind (Id, E_Limited_Private_Type);
2508 else
2509 Set_Ekind (Id, E_Private_Type);
2510 end if;
2512 Set_Etype (Id, Id);
2513 Set_Has_Delayed_Freeze (Id);
2514 Set_Is_First_Subtype (Id);
2515 Init_Size_Align (Id);
2517 Set_Is_Constrained (Id,
2518 No (Discriminant_Specifications (N))
2519 and then not Unknown_Discriminants_Present (N));
2521 -- Set tagged flag before processing discriminants, to catch illegal
2522 -- usage.
2524 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
2526 Set_Discriminant_Constraint (Id, No_Elist);
2527 Set_Stored_Constraint (Id, No_Elist);
2529 if Present (Discriminant_Specifications (N)) then
2530 Push_Scope (Id);
2531 Process_Discriminants (N);
2532 End_Scope;
2534 elsif Unknown_Discriminants_Present (N) then
2535 Set_Has_Unknown_Discriminants (Id);
2536 end if;
2538 Set_Private_Dependents (Id, New_Elmt_List);
2540 if Tagged_Present (Def) then
2541 Set_Ekind (Id, E_Record_Type_With_Private);
2542 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2543 Set_Is_Abstract_Type (Id, Abstract_Present (Def));
2544 Set_Is_Limited_Record (Id, Limited_Present (Def));
2545 Set_Has_Delayed_Freeze (Id, True);
2547 -- Create a class-wide type with the same attributes
2549 Make_Class_Wide_Type (Id);
2551 elsif Abstract_Present (Def) then
2552 Error_Msg_N ("only a tagged type can be abstract", N);
2553 end if;
2554 end New_Private_Type;
2556 ---------------------------------
2557 -- Requires_Completion_In_Body --
2558 ---------------------------------
2560 function Requires_Completion_In_Body
2561 (Id : Entity_Id;
2562 Pack_Id : Entity_Id) return Boolean
2564 begin
2565 -- Always ignore child units. Child units get added to the entity list
2566 -- of a parent unit, but are not original entities of the parent, and
2567 -- so do not affect whether the parent needs a body.
2569 if Is_Child_Unit (Id) then
2570 return False;
2572 -- Ignore formal packages and their renamings
2574 elsif Ekind (Id) = E_Package
2575 and then Nkind (Original_Node (Unit_Declaration_Node (Id))) =
2576 N_Formal_Package_Declaration
2577 then
2578 return False;
2580 -- Otherwise test to see if entity requires a completion. Note that
2581 -- subprogram entities whose declaration does not come from source are
2582 -- ignored here on the basis that we assume the expander will provide an
2583 -- implicit completion at some point.
2585 elsif (Is_Overloadable (Id)
2586 and then Ekind (Id) /= E_Enumeration_Literal
2587 and then Ekind (Id) /= E_Operator
2588 and then not Is_Abstract_Subprogram (Id)
2589 and then not Has_Completion (Id)
2590 and then Comes_From_Source (Parent (Id)))
2592 or else
2593 (Ekind (Id) = E_Package
2594 and then Id /= Pack_Id
2595 and then not Has_Completion (Id)
2596 and then Unit_Requires_Body (Id))
2598 or else
2599 (Ekind (Id) = E_Incomplete_Type
2600 and then No (Full_View (Id))
2601 and then not Is_Generic_Type (Id))
2603 or else
2604 (Ekind_In (Id, E_Task_Type, E_Protected_Type)
2605 and then not Has_Completion (Id))
2607 or else
2608 (Ekind (Id) = E_Generic_Package
2609 and then Id /= Pack_Id
2610 and then not Has_Completion (Id)
2611 and then Unit_Requires_Body (Id))
2613 or else
2614 (Is_Generic_Subprogram (Id)
2615 and then not Has_Completion (Id))
2617 then
2618 return True;
2620 -- Otherwise the entity does not require completion in a package body
2622 else
2623 return False;
2624 end if;
2625 end Requires_Completion_In_Body;
2627 ----------------------------
2628 -- Uninstall_Declarations --
2629 ----------------------------
2631 procedure Uninstall_Declarations (P : Entity_Id) is
2632 Decl : constant Node_Id := Unit_Declaration_Node (P);
2633 Id : Entity_Id;
2634 Full : Entity_Id;
2635 Priv_Elmt : Elmt_Id;
2636 Priv_Sub : Entity_Id;
2638 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id);
2639 -- Copy to the private declaration the attributes of the full view that
2640 -- need to be available for the partial view also.
2642 function Type_In_Use (T : Entity_Id) return Boolean;
2643 -- Check whether type or base type appear in an active use_type clause
2645 ------------------------------
2646 -- Preserve_Full_Attributes --
2647 ------------------------------
2649 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id) is
2650 Priv_Is_Base_Type : constant Boolean := Is_Base_Type (Priv);
2652 begin
2653 Set_Size_Info (Priv, (Full));
2654 Set_RM_Size (Priv, RM_Size (Full));
2655 Set_Size_Known_At_Compile_Time
2656 (Priv, Size_Known_At_Compile_Time (Full));
2657 Set_Is_Volatile (Priv, Is_Volatile (Full));
2658 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
2659 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full));
2660 Set_Is_Ada_2012_Only (Priv, Is_Ada_2012_Only (Full));
2661 Set_Has_Pragma_Unmodified (Priv, Has_Pragma_Unmodified (Full));
2662 Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced (Full));
2663 Set_Has_Pragma_Unreferenced_Objects
2664 (Priv, Has_Pragma_Unreferenced_Objects
2665 (Full));
2666 if Is_Unchecked_Union (Full) then
2667 Set_Is_Unchecked_Union (Base_Type (Priv));
2668 end if;
2669 -- Why is atomic not copied here ???
2671 if Referenced (Full) then
2672 Set_Referenced (Priv);
2673 end if;
2675 if Priv_Is_Base_Type then
2676 Set_Is_Controlled (Priv, Is_Controlled (Base_Type (Full)));
2677 Set_Finalize_Storage_Only
2678 (Priv, Finalize_Storage_Only
2679 (Base_Type (Full)));
2680 Set_Has_Task (Priv, Has_Task (Base_Type (Full)));
2681 Set_Has_Protected (Priv, Has_Protected (Base_Type (Full)));
2682 Set_Has_Controlled_Component
2683 (Priv, Has_Controlled_Component
2684 (Base_Type (Full)));
2685 end if;
2687 Set_Freeze_Node (Priv, Freeze_Node (Full));
2689 -- Propagate information of type invariants, which may be specified
2690 -- for the full view.
2692 if Has_Invariants (Full) and not Has_Invariants (Priv) then
2693 Set_Has_Invariants (Priv);
2694 Set_Subprograms_For_Type (Priv, Subprograms_For_Type (Full));
2695 end if;
2697 if Is_Tagged_Type (Priv)
2698 and then Is_Tagged_Type (Full)
2699 and then not Error_Posted (Full)
2700 then
2701 if Is_Tagged_Type (Priv) then
2703 -- If the type is tagged, the tag itself must be available on
2704 -- the partial view, for expansion purposes.
2706 Set_First_Entity (Priv, First_Entity (Full));
2708 -- If there are discriminants in the partial view, these remain
2709 -- visible. Otherwise only the tag itself is visible, and there
2710 -- are no nameable components in the partial view.
2712 if No (Last_Entity (Priv)) then
2713 Set_Last_Entity (Priv, First_Entity (Priv));
2714 end if;
2715 end if;
2717 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2719 if Has_Discriminants (Full) then
2720 Set_Discriminant_Constraint (Priv,
2721 Discriminant_Constraint (Full));
2722 end if;
2723 end if;
2724 end Preserve_Full_Attributes;
2726 -----------------
2727 -- Type_In_Use --
2728 -----------------
2730 function Type_In_Use (T : Entity_Id) return Boolean is
2731 begin
2732 return Scope (Base_Type (T)) = P
2733 and then (In_Use (T) or else In_Use (Base_Type (T)));
2734 end Type_In_Use;
2736 -- Start of processing for Uninstall_Declarations
2738 begin
2739 Id := First_Entity (P);
2740 while Present (Id) and then Id /= First_Private_Entity (P) loop
2741 if Debug_Flag_E then
2742 Write_Str ("unlinking visible entity ");
2743 Write_Int (Int (Id));
2744 Write_Eol;
2745 end if;
2747 -- On exit from the package scope, we must preserve the visibility
2748 -- established by use clauses in the current scope. Two cases:
2750 -- a) If the entity is an operator, it may be a primitive operator of
2751 -- a type for which there is a visible use-type clause.
2753 -- b) for other entities, their use-visibility is determined by a
2754 -- visible use clause for the package itself. For a generic instance,
2755 -- the instantiation of the formals appears in the visible part,
2756 -- but the formals are private and remain so.
2758 if Ekind (Id) = E_Function
2759 and then Is_Operator_Symbol_Name (Chars (Id))
2760 and then not Is_Hidden (Id)
2761 and then not Error_Posted (Id)
2762 then
2763 Set_Is_Potentially_Use_Visible (Id,
2764 In_Use (P)
2765 or else Type_In_Use (Etype (Id))
2766 or else Type_In_Use (Etype (First_Formal (Id)))
2767 or else (Present (Next_Formal (First_Formal (Id)))
2768 and then
2769 Type_In_Use
2770 (Etype (Next_Formal (First_Formal (Id))))));
2771 else
2772 if In_Use (P) and then not Is_Hidden (Id) then
2774 -- A child unit of a use-visible package remains use-visible
2775 -- only if it is itself a visible child unit. Otherwise it
2776 -- would remain visible in other contexts where P is use-
2777 -- visible, because once compiled it stays in the entity list
2778 -- of its parent unit.
2780 if Is_Child_Unit (Id) then
2781 Set_Is_Potentially_Use_Visible
2782 (Id, Is_Visible_Lib_Unit (Id));
2783 else
2784 Set_Is_Potentially_Use_Visible (Id);
2785 end if;
2787 else
2788 Set_Is_Potentially_Use_Visible (Id, False);
2789 end if;
2790 end if;
2792 -- Local entities are not immediately visible outside of the package
2794 Set_Is_Immediately_Visible (Id, False);
2796 -- If this is a private type with a full view (for example a local
2797 -- subtype of a private type declared elsewhere), ensure that the
2798 -- full view is also removed from visibility: it may be exposed when
2799 -- swapping views in an instantiation.
2801 if Is_Type (Id) and then Present (Full_View (Id)) then
2802 Set_Is_Immediately_Visible (Full_View (Id), False);
2803 end if;
2805 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2806 Check_Abstract_Overriding (Id);
2807 Check_Conventions (Id);
2808 end if;
2810 if Ekind_In (Id, E_Private_Type, E_Limited_Private_Type)
2811 and then No (Full_View (Id))
2812 and then not Is_Generic_Type (Id)
2813 and then not Is_Derived_Type (Id)
2814 then
2815 Error_Msg_N ("missing full declaration for private type&", Id);
2817 elsif Ekind (Id) = E_Record_Type_With_Private
2818 and then not Is_Generic_Type (Id)
2819 and then No (Full_View (Id))
2820 then
2821 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
2822 Error_Msg_N ("missing full declaration for private type&", Id);
2823 else
2824 Error_Msg_N
2825 ("missing full declaration for private extension", Id);
2826 end if;
2828 -- Case of constant, check for deferred constant declaration with
2829 -- no full view. Likely just a matter of a missing expression, or
2830 -- accidental use of the keyword constant.
2832 elsif Ekind (Id) = E_Constant
2834 -- OK if constant value present
2836 and then No (Constant_Value (Id))
2838 -- OK if full view present
2840 and then No (Full_View (Id))
2842 -- OK if imported, since that provides the completion
2844 and then not Is_Imported (Id)
2846 -- OK if object declaration replaced by renaming declaration as
2847 -- a result of OK_To_Rename processing (e.g. for concatenation)
2849 and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration
2851 -- OK if object declaration with the No_Initialization flag set
2853 and then not (Nkind (Parent (Id)) = N_Object_Declaration
2854 and then No_Initialization (Parent (Id)))
2855 then
2856 -- If no private declaration is present, we assume the user did
2857 -- not intend a deferred constant declaration and the problem
2858 -- is simply that the initializing expression is missing.
2860 if not Has_Private_Declaration (Etype (Id)) then
2862 -- We assume that the user did not intend a deferred constant
2863 -- declaration, and the expression is just missing.
2865 Error_Msg_N
2866 ("constant declaration requires initialization expression",
2867 Parent (Id));
2869 if Is_Limited_Type (Etype (Id)) then
2870 Error_Msg_N
2871 ("\if variable intended, remove CONSTANT from declaration",
2872 Parent (Id));
2873 end if;
2875 -- Otherwise if a private declaration is present, then we are
2876 -- missing the full declaration for the deferred constant.
2878 else
2879 Error_Msg_N
2880 ("missing full declaration for deferred constant (RM 7.4)",
2881 Id);
2883 if Is_Limited_Type (Etype (Id)) then
2884 Error_Msg_N
2885 ("\if variable intended, remove CONSTANT from declaration",
2886 Parent (Id));
2887 end if;
2888 end if;
2889 end if;
2891 Next_Entity (Id);
2892 end loop;
2894 -- If the specification was installed as the parent of a public child
2895 -- unit, the private declarations were not installed, and there is
2896 -- nothing to do.
2898 if not In_Private_Part (P) then
2899 return;
2900 else
2901 Set_In_Private_Part (P, False);
2902 end if;
2904 -- Make private entities invisible and exchange full and private
2905 -- declarations for private types. Id is now the first private entity
2906 -- in the package.
2908 while Present (Id) loop
2909 if Debug_Flag_E then
2910 Write_Str ("unlinking private entity ");
2911 Write_Int (Int (Id));
2912 Write_Eol;
2913 end if;
2915 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2916 Check_Abstract_Overriding (Id);
2917 Check_Conventions (Id);
2918 end if;
2920 Set_Is_Immediately_Visible (Id, False);
2922 if Is_Private_Base_Type (Id) and then Present (Full_View (Id)) then
2923 Full := Full_View (Id);
2925 -- If the partial view is not declared in the visible part of the
2926 -- package (as is the case when it is a type derived from some
2927 -- other private type in the private part of the current package),
2928 -- no exchange takes place.
2930 if No (Parent (Id))
2931 or else List_Containing (Parent (Id)) /=
2932 Visible_Declarations (Specification (Decl))
2933 then
2934 goto Next_Id;
2935 end if;
2937 -- The entry in the private part points to the full declaration,
2938 -- which is currently visible. Exchange them so only the private
2939 -- type declaration remains accessible, and link private and full
2940 -- declaration in the opposite direction. Before the actual
2941 -- exchange, we copy back attributes of the full view that must
2942 -- be available to the partial view too.
2944 Preserve_Full_Attributes (Id, Full);
2946 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
2948 -- The following test may be redundant, as this is already
2949 -- diagnosed in sem_ch3. ???
2951 if not Is_Definite_Subtype (Full)
2952 and then Is_Definite_Subtype (Id)
2953 then
2954 Error_Msg_Sloc := Sloc (Parent (Id));
2955 Error_Msg_NE
2956 ("full view of& not compatible with declaration#", Full, Id);
2957 end if;
2959 -- Swap out the subtypes and derived types of Id that
2960 -- were compiled in this scope, or installed previously
2961 -- by Install_Private_Declarations.
2963 -- Before we do the swap, we verify the presence of the Full_View
2964 -- field which may be empty due to a swap by a previous call to
2965 -- End_Package_Scope (e.g. from the freezing mechanism).
2967 Priv_Elmt := First_Elmt (Private_Dependents (Id));
2968 while Present (Priv_Elmt) loop
2969 Priv_Sub := Node (Priv_Elmt);
2971 if Present (Full_View (Priv_Sub)) then
2972 if Scope (Priv_Sub) = P
2973 or else not In_Open_Scopes (Scope (Priv_Sub))
2974 then
2975 Set_Is_Immediately_Visible (Priv_Sub, False);
2976 end if;
2978 if Is_Visible_Dependent (Priv_Sub) then
2979 Preserve_Full_Attributes
2980 (Priv_Sub, Full_View (Priv_Sub));
2981 Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub));
2982 Exchange_Declarations (Priv_Sub);
2983 end if;
2984 end if;
2986 Next_Elmt (Priv_Elmt);
2987 end loop;
2989 -- Now restore the type itself to its private view
2991 Exchange_Declarations (Id);
2993 -- If we have installed an underlying full view for a type derived
2994 -- from a private type in a child unit, restore the proper views
2995 -- of private and full view. See corresponding code in
2996 -- Install_Private_Declarations.
2998 -- After the exchange, Full denotes the private type in the
2999 -- visible part of the package.
3001 if Is_Private_Base_Type (Full)
3002 and then Present (Full_View (Full))
3003 and then Present (Underlying_Full_View (Full))
3004 and then In_Package_Body (Current_Scope)
3005 then
3006 Set_Full_View (Full, Underlying_Full_View (Full));
3007 Set_Underlying_Full_View (Full, Empty);
3008 end if;
3010 elsif Ekind (Id) = E_Incomplete_Type
3011 and then Comes_From_Source (Id)
3012 and then No (Full_View (Id))
3013 then
3014 -- Mark Taft amendment types. Verify that there are no primitive
3015 -- operations declared for the type (3.10.1(9)).
3017 Set_Has_Completion_In_Body (Id);
3019 declare
3020 Elmt : Elmt_Id;
3021 Subp : Entity_Id;
3023 begin
3024 Elmt := First_Elmt (Private_Dependents (Id));
3025 while Present (Elmt) loop
3026 Subp := Node (Elmt);
3028 -- Is_Primitive is tested because there can be cases where
3029 -- nonprimitive subprograms (in nested packages) are added
3030 -- to the Private_Dependents list.
3032 if Is_Overloadable (Subp) and then Is_Primitive (Subp) then
3033 Error_Msg_NE
3034 ("type& must be completed in the private part",
3035 Parent (Subp), Id);
3037 -- The result type of an access-to-function type cannot be a
3038 -- Taft-amendment type, unless the version is Ada 2012 or
3039 -- later (see AI05-151).
3041 elsif Ada_Version < Ada_2012
3042 and then Ekind (Subp) = E_Subprogram_Type
3043 then
3044 if Etype (Subp) = Id
3045 or else
3046 (Is_Class_Wide_Type (Etype (Subp))
3047 and then Etype (Etype (Subp)) = Id)
3048 then
3049 Error_Msg_NE
3050 ("type& must be completed in the private part",
3051 Associated_Node_For_Itype (Subp), Id);
3052 end if;
3053 end if;
3055 Next_Elmt (Elmt);
3056 end loop;
3057 end;
3059 elsif not Is_Child_Unit (Id)
3060 and then (not Is_Private_Type (Id) or else No (Full_View (Id)))
3061 then
3062 Set_Is_Hidden (Id);
3063 Set_Is_Potentially_Use_Visible (Id, False);
3064 end if;
3066 <<Next_Id>>
3067 Next_Entity (Id);
3068 end loop;
3069 end Uninstall_Declarations;
3071 ------------------------
3072 -- Unit_Requires_Body --
3073 ------------------------
3075 function Unit_Requires_Body
3076 (Pack_Id : Entity_Id;
3077 Ignore_Abstract_State : Boolean := False) return Boolean
3079 E : Entity_Id;
3081 begin
3082 -- Imported entity never requires body. Right now, only subprograms can
3083 -- be imported, but perhaps in the future we will allow import of
3084 -- packages.
3086 if Is_Imported (Pack_Id) then
3087 return False;
3089 -- Body required if library package with pragma Elaborate_Body
3091 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3092 return True;
3094 -- Body required if subprogram
3096 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3097 return True;
3099 -- Treat a block as requiring a body
3101 elsif Ekind (Pack_Id) = E_Block then
3102 return True;
3104 elsif Ekind (Pack_Id) = E_Package
3105 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3106 and then Present (Generic_Parent (Parent (Pack_Id)))
3107 then
3108 declare
3109 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3110 begin
3111 if Has_Pragma_Elaborate_Body (G_P) then
3112 return True;
3113 end if;
3114 end;
3116 -- A [generic] package that introduces at least one non-null abstract
3117 -- state requires completion. However, there is a separate rule that
3118 -- requires that such a package have a reason other than this for a
3119 -- body being required (if necessary a pragma Elaborate_Body must be
3120 -- provided). If Ignore_Abstract_State is True, we don't do this check
3121 -- (so we can use Unit_Requires_Body to check for some other reason).
3123 elsif Ekind_In (Pack_Id, E_Generic_Package, E_Package)
3124 and then not Ignore_Abstract_State
3125 and then Present (Abstract_States (Pack_Id))
3126 and then not Is_Null_State
3127 (Node (First_Elmt (Abstract_States (Pack_Id))))
3128 then
3129 return True;
3130 end if;
3132 -- Otherwise search entity chain for entity requiring completion
3134 E := First_Entity (Pack_Id);
3135 while Present (E) loop
3136 if Requires_Completion_In_Body (E, Pack_Id) then
3137 return True;
3138 end if;
3140 Next_Entity (E);
3141 end loop;
3143 return False;
3144 end Unit_Requires_Body;
3146 -----------------------------
3147 -- Unit_Requires_Body_Info --
3148 -----------------------------
3150 procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id) is
3151 E : Entity_Id;
3153 begin
3154 -- An imported entity never requires body. Right now, only subprograms
3155 -- can be imported, but perhaps in the future we will allow import of
3156 -- packages.
3158 if Is_Imported (Pack_Id) then
3159 return;
3161 -- Body required if library package with pragma Elaborate_Body
3163 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3164 Error_Msg_N ("info: & requires body (Elaborate_Body)?Y?", Pack_Id);
3166 -- Body required if subprogram
3168 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3169 Error_Msg_N ("info: & requires body (subprogram case)?Y?", Pack_Id);
3171 -- Body required if generic parent has Elaborate_Body
3173 elsif Ekind (Pack_Id) = E_Package
3174 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3175 and then Present (Generic_Parent (Parent (Pack_Id)))
3176 then
3177 declare
3178 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3179 begin
3180 if Has_Pragma_Elaborate_Body (G_P) then
3181 Error_Msg_N
3182 ("info: & requires body (generic parent Elaborate_Body)?Y?",
3183 Pack_Id);
3184 end if;
3185 end;
3187 -- A [generic] package that introduces at least one non-null abstract
3188 -- state requires completion. However, there is a separate rule that
3189 -- requires that such a package have a reason other than this for a
3190 -- body being required (if necessary a pragma Elaborate_Body must be
3191 -- provided). If Ignore_Abstract_State is True, we don't do this check
3192 -- (so we can use Unit_Requires_Body to check for some other reason).
3194 elsif Ekind_In (Pack_Id, E_Generic_Package, E_Package)
3195 and then Present (Abstract_States (Pack_Id))
3196 and then not Is_Null_State
3197 (Node (First_Elmt (Abstract_States (Pack_Id))))
3198 then
3199 Error_Msg_N
3200 ("info: & requires body (non-null abstract state aspect)?Y?",
3201 Pack_Id);
3202 end if;
3204 -- Otherwise search entity chain for entity requiring completion
3206 E := First_Entity (Pack_Id);
3207 while Present (E) loop
3208 if Requires_Completion_In_Body (E, Pack_Id) then
3209 Error_Msg_Node_2 := E;
3210 Error_Msg_NE
3211 ("info: & requires body (& requires completion)?Y?", E, Pack_Id);
3212 end if;
3214 Next_Entity (E);
3215 end loop;
3216 end Unit_Requires_Body_Info;
3217 end Sem_Ch7;