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