2014-11-20 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ada / sem_ch7.adb
blob1acef30dee00b5e573cda4329c57787028fe3797
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 Declare_Inherited_Private_Subprograms (Id : Entity_Id);
112 -- Called upon entering the private part of a public child package and the
113 -- body of a nested package, to potentially declare certain inherited
114 -- subprograms that were inherited by types in the visible part, but whose
115 -- declaration was deferred because the parent operation was private and
116 -- not visible at that point. These subprograms are located by traversing
117 -- the visible part declarations looking for non-private type extensions
118 -- and then examining each of the primitive operations of such types to
119 -- find those that were inherited but declared with a special internal
120 -- name. Each such operation is now declared as an operation with a normal
121 -- name (using the name of the parent operation) and replaces the previous
122 -- implicit operation in the primitive operations list of the type. If the
123 -- inherited private operation has been overridden, then it's replaced by
124 -- the overriding operation.
126 procedure Install_Package_Entity (Id : Entity_Id);
127 -- Supporting procedure for Install_{Visible,Private}_Declarations. Places
128 -- one entity on its visibility chain, and recurses on the visible part if
129 -- the entity is an inner package.
131 function Is_Private_Base_Type (E : Entity_Id) return Boolean;
132 -- True for a private type that is not a subtype
134 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean;
135 -- If the private dependent is a private type whose full view is derived
136 -- from the parent type, its full properties are revealed only if we are in
137 -- the immediate scope of the private dependent. Should this predicate be
138 -- tightened further???
140 function Requires_Completion_In_Body
141 (Id : Entity_Id;
142 Pack_Id : Entity_Id) return Boolean;
143 -- Subsidiary to routines Unit_Requires_Body and Unit_Requires_Body_Info.
144 -- Determine whether entity Id declared in package spec Pack_Id requires
145 -- completion in a package body.
147 procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id);
148 -- Outputs info messages showing why package Pack_Id requires a body. The
149 -- caller has checked that the switch requesting this information is set,
150 -- and that the package does indeed require a body.
152 --------------------------
153 -- Analyze_Package_Body --
154 --------------------------
156 procedure Analyze_Package_Body (N : Node_Id) is
157 Loc : constant Source_Ptr := Sloc (N);
159 begin
160 if Debug_Flag_C then
161 Write_Str ("==> package body ");
162 Write_Name (Chars (Defining_Entity (N)));
163 Write_Str (" from ");
164 Write_Location (Loc);
165 Write_Eol;
166 Indent;
167 end if;
169 -- The real work is split out into the helper, so it can do "return;"
170 -- without skipping the debug output.
172 Analyze_Package_Body_Helper (N);
174 if Debug_Flag_C then
175 Outdent;
176 Write_Str ("<== package body ");
177 Write_Name (Chars (Defining_Entity (N)));
178 Write_Str (" from ");
179 Write_Location (Loc);
180 Write_Eol;
181 end if;
182 end Analyze_Package_Body;
184 -----------------------------------
185 -- Analyze_Package_Body_Contract --
186 -----------------------------------
188 procedure Analyze_Package_Body_Contract (Body_Id : Entity_Id) is
189 Spec_Id : constant Entity_Id := Spec_Entity (Body_Id);
190 Mode : SPARK_Mode_Type;
191 Prag : Node_Id;
193 begin
194 -- Due to the timing of contract analysis, delayed pragmas may be
195 -- subject to the wrong SPARK_Mode, usually that of the enclosing
196 -- context. To remedy this, restore the original SPARK_Mode of the
197 -- related package body.
199 Save_SPARK_Mode_And_Set (Body_Id, Mode);
201 Prag := Get_Pragma (Body_Id, Pragma_Refined_State);
203 -- The analysis of pragma Refined_State detects whether the spec has
204 -- abstract states available for refinement.
206 if Present (Prag) then
207 Analyze_Refined_State_In_Decl_Part (Prag);
209 -- State refinement is required when the package declaration defines at
210 -- least one abstract state. Null states are not considered. Refinement
211 -- is not envorced when SPARK checks are turned off.
213 elsif SPARK_Mode /= Off
214 and then Requires_State_Refinement (Spec_Id, Body_Id)
215 then
216 Error_Msg_N ("package & requires state refinement", Spec_Id);
217 end if;
219 -- Restore the SPARK_Mode of the enclosing context after all delayed
220 -- pragmas have been analyzed.
222 Restore_SPARK_Mode (Mode);
223 end Analyze_Package_Body_Contract;
225 ---------------------------------
226 -- Analyze_Package_Body_Helper --
227 ---------------------------------
229 procedure Analyze_Package_Body_Helper (N : Node_Id) is
230 procedure Hide_Public_Entities (Decls : List_Id);
231 -- Attempt to hide all public entities found in declarative list Decls
232 -- by resetting their Is_Public flag to False depending on whether the
233 -- entities are not referenced by inlined or generic bodies. This kind
234 -- of processing is a conservative approximation and may still leave
235 -- certain entities externally visible.
237 procedure Install_Composite_Operations (P : Entity_Id);
238 -- Composite types declared in the current scope may depend on types
239 -- that were private at the point of declaration, and whose full view
240 -- is now in scope. Indicate that the corresponding operations on the
241 -- composite type are available.
243 --------------------------
244 -- Hide_Public_Entities --
245 --------------------------
247 procedure Hide_Public_Entities (Decls : List_Id) is
248 function Contains_Subp_Or_Const_Refs (N : Node_Id) return Boolean;
249 -- Subsidiary to routine Has_Referencer. Determine whether a node
250 -- contains a reference to a subprogram or a non-static constant.
251 -- WARNING: this is a very expensive routine as it performs a full
252 -- tree traversal.
254 function Has_Referencer
255 (Decls : List_Id;
256 Top_Level : Boolean := False) return Boolean;
257 -- A "referencer" is a construct which may reference a previous
258 -- declaration. Examine all declarations in list Decls in reverse
259 -- and determine whether once such referencer exists. All entities
260 -- in the range Last (Decls) .. Referencer are hidden from external
261 -- visibility.
263 ---------------------------------
264 -- Contains_Subp_Or_Const_Refs --
265 ---------------------------------
267 function Contains_Subp_Or_Const_Refs (N : Node_Id) return Boolean is
268 Reference_Seen : Boolean := False;
270 function Is_Subp_Or_Const_Ref
271 (N : Node_Id) return Traverse_Result;
272 -- Determine whether a node denotes a reference to a subprogram or
273 -- a non-static constant.
275 --------------------------
276 -- Is_Subp_Or_Const_Ref --
277 --------------------------
279 function Is_Subp_Or_Const_Ref
280 (N : Node_Id) return Traverse_Result
282 Val : Node_Id;
284 begin
285 -- Detect a reference of the form
286 -- Subp_Call
288 if Nkind (N) in N_Subprogram_Call
289 and then Is_Entity_Name (Name (N))
290 then
291 Reference_Seen := True;
292 return Abandon;
294 -- Detect a reference of the form
295 -- Subp'Some_Attribute
297 elsif Nkind (N) = N_Attribute_Reference
298 and then Is_Entity_Name (Prefix (N))
299 and then Is_Subprogram (Entity (Prefix (N)))
300 then
301 Reference_Seen := True;
302 return Abandon;
304 -- Detect the use of a non-static constant
306 elsif Is_Entity_Name (N)
307 and then Present (Entity (N))
308 and then Ekind (Entity (N)) = E_Constant
309 then
310 Val := Constant_Value (Entity (N));
312 if Present (Val)
313 and then not Compile_Time_Known_Value (Val)
314 then
315 Reference_Seen := True;
316 return Abandon;
317 end if;
318 end if;
320 return OK;
321 end Is_Subp_Or_Const_Ref;
323 procedure Find_Subp_Or_Const_Ref is
324 new Traverse_Proc (Is_Subp_Or_Const_Ref);
326 -- Start of processing for Contains_Subp_Or_Const_Refs
328 begin
329 Find_Subp_Or_Const_Ref (N);
331 return Reference_Seen;
332 end Contains_Subp_Or_Const_Refs;
334 --------------------
335 -- Has_Referencer --
336 --------------------
338 function Has_Referencer
339 (Decls : List_Id;
340 Top_Level : Boolean := False) return Boolean
342 Decl : Node_Id;
343 Decl_Id : Entity_Id;
344 Spec : Node_Id;
346 Has_Non_Subp_Const_Referencer : Boolean := False;
347 -- Flag set for inlined subprogram bodies that do not contain
348 -- references to other subprograms or non-static constants.
350 begin
351 if No (Decls) then
352 return False;
353 end if;
355 -- Examine all declarations in reverse order, hiding all entities
356 -- from external visibility until a referencer has been found. The
357 -- algorithm recurses into nested packages.
359 Decl := Last (Decls);
360 while Present (Decl) loop
362 -- A stub is always considered a referencer
364 if Nkind (Decl) in N_Body_Stub then
365 return True;
367 -- Package declaration
369 elsif Nkind (Decl) = N_Package_Declaration
370 and then not Has_Non_Subp_Const_Referencer
371 then
372 Spec := Specification (Decl);
374 -- Inspect the declarations of a non-generic package to try
375 -- and hide more entities from external visibility.
377 if not Is_Generic_Unit (Defining_Entity (Spec)) then
378 if Has_Referencer (Private_Declarations (Spec))
379 or else Has_Referencer (Visible_Declarations (Spec))
380 then
381 return True;
382 end if;
383 end if;
385 -- Package body
387 elsif Nkind (Decl) = N_Package_Body
388 and then Present (Corresponding_Spec (Decl))
389 then
390 Decl_Id := Corresponding_Spec (Decl);
392 -- A generic package body is a referencer. It would seem
393 -- that we only have to consider generics that can be
394 -- exported, i.e. where the corresponding spec is the
395 -- spec of the current package, but because of nested
396 -- instantiations, a fully private generic body may export
397 -- other private body entities. Furthermore, regardless of
398 -- whether there was a previous inlined subprogram, (an
399 -- instantiation of) the generic package may reference any
400 -- entity declared before it.
402 if Is_Generic_Unit (Decl_Id) then
403 return True;
405 -- Inspect the declarations of a non-generic package body to
406 -- try and hide more entities from external visibility.
408 elsif not Has_Non_Subp_Const_Referencer
409 and then Has_Referencer (Declarations (Decl))
410 then
411 return True;
412 end if;
414 -- Subprogram body
416 elsif Nkind (Decl) = N_Subprogram_Body then
417 if Present (Corresponding_Spec (Decl)) then
418 Decl_Id := Corresponding_Spec (Decl);
420 -- A generic subprogram body acts as a referencer
422 if Is_Generic_Unit (Decl_Id) then
423 return True;
424 end if;
426 -- An inlined subprogram body acts as a referencer
428 if Is_Inlined (Decl_Id)
429 or else Has_Pragma_Inline (Decl_Id)
430 then
431 -- Inspect the statements of the subprogram body
432 -- to determine whether the body references other
433 -- subprograms and/or non-static constants.
435 if Top_Level
436 and then not Contains_Subp_Or_Const_Refs (Decl)
437 then
438 Has_Non_Subp_Const_Referencer := True;
439 else
440 return True;
441 end if;
442 end if;
444 -- Otherwise this is a stand alone subprogram body
446 else
447 Decl_Id := Defining_Entity (Decl);
449 -- An inlined body acts as a referencer. Note that an
450 -- inlined subprogram remains Is_Public as gigi requires
451 -- the flag to be set.
453 -- Note that we test Has_Pragma_Inline here rather than
454 -- Is_Inlined. We are compiling this for a client, and
455 -- it is the client who will decide if actual inlining
456 -- should occur, so we need to assume that the procedure
457 -- could be inlined for the purpose of accessing global
458 -- entities.
460 if Has_Pragma_Inline (Decl_Id) then
461 if Top_Level
462 and then not Contains_Subp_Or_Const_Refs (Decl)
463 then
464 Has_Non_Subp_Const_Referencer := True;
465 else
466 return True;
467 end if;
468 else
469 Set_Is_Public (Decl_Id, False);
470 end if;
471 end if;
473 -- Exceptions, objects and renamings do not need to be public
474 -- if they are not followed by a construct which can reference
475 -- and export them. The Is_Public flag is reset on top level
476 -- entities only as anything nested is local to its context.
478 elsif Nkind_In (Decl, N_Exception_Declaration,
479 N_Object_Declaration,
480 N_Object_Renaming_Declaration,
481 N_Subprogram_Declaration,
482 N_Subprogram_Renaming_Declaration)
483 then
484 Decl_Id := Defining_Entity (Decl);
486 if Top_Level
487 and then not Is_Imported (Decl_Id)
488 and then not Is_Exported (Decl_Id)
489 and then No (Interface_Name (Decl_Id))
490 and then
491 (not Has_Non_Subp_Const_Referencer
492 or else Nkind (Decl) = N_Subprogram_Declaration)
493 then
494 Set_Is_Public (Decl_Id, False);
495 end if;
496 end if;
498 Prev (Decl);
499 end loop;
501 return Has_Non_Subp_Const_Referencer;
502 end Has_Referencer;
504 -- Local variables
506 Discard : Boolean := True;
507 pragma Unreferenced (Discard);
509 -- Start of processing for Hide_Public_Entities
511 begin
512 -- The algorithm examines the top level declarations of a package
513 -- body in reverse looking for a construct that may export entities
514 -- declared prior to it. If such a scenario is encountered, then all
515 -- entities in the range Last (Decls) .. construct are hidden from
516 -- external visibility. Consider:
518 -- package Pack is
519 -- generic
520 -- package Gen is
521 -- end Gen;
522 -- end Pack;
524 -- package body Pack is
525 -- External_Obj : ...; -- (1)
527 -- package body Gen is -- (2)
528 -- ... External_Obj ... -- (3)
529 -- end Gen;
531 -- Local_Obj : ...; -- (4)
532 -- end Pack;
534 -- In this example Local_Obj (4) must not be externally visible as
535 -- it cannot be exported by anything in Pack. The body of generic
536 -- package Gen (2) on the other hand acts as a "referencer" and may
537 -- export anything declared before it. Since the compiler does not
538 -- perform flow analysis, it is not possible to determine precisely
539 -- which entities will be exported when Gen is instantiated. In the
540 -- example above External_Obj (1) is exported at (3), but this may
541 -- not always be the case. The algorithm takes a conservative stance
542 -- and leaves entity External_Obj public.
544 Discard := Has_Referencer (Decls, Top_Level => True);
545 end Hide_Public_Entities;
547 ----------------------------------
548 -- Install_Composite_Operations --
549 ----------------------------------
551 procedure Install_Composite_Operations (P : Entity_Id) is
552 Id : Entity_Id;
554 begin
555 Id := First_Entity (P);
556 while Present (Id) loop
557 if Is_Type (Id)
558 and then (Is_Limited_Composite (Id)
559 or else Is_Private_Composite (Id))
560 and then No (Private_Component (Id))
561 then
562 Set_Is_Limited_Composite (Id, False);
563 Set_Is_Private_Composite (Id, False);
564 end if;
566 Next_Entity (Id);
567 end loop;
568 end Install_Composite_Operations;
570 -- Local variables
572 Body_Id : Entity_Id;
573 HSS : Node_Id;
574 Last_Spec_Entity : Entity_Id;
575 New_N : Node_Id;
576 Pack_Decl : Node_Id;
577 Spec_Id : Entity_Id;
579 -- Start of processing for Analyze_Package_Body_Helper
581 begin
582 -- Find corresponding package specification, and establish the current
583 -- scope. The visible defining entity for the package is the defining
584 -- occurrence in the spec. On exit from the package body, all body
585 -- declarations are attached to the defining entity for the body, but
586 -- the later is never used for name resolution. In this fashion there
587 -- is only one visible entity that denotes the package.
589 -- Set Body_Id. Note that this will be reset to point to the generic
590 -- copy later on in the generic case.
592 Body_Id := Defining_Entity (N);
594 -- Body is body of package instantiation. Corresponding spec has already
595 -- been set.
597 if Present (Corresponding_Spec (N)) then
598 Spec_Id := Corresponding_Spec (N);
599 Pack_Decl := Unit_Declaration_Node (Spec_Id);
601 else
602 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
604 if Present (Spec_Id)
605 and then Is_Package_Or_Generic_Package (Spec_Id)
606 then
607 Pack_Decl := Unit_Declaration_Node (Spec_Id);
609 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
610 Error_Msg_N ("cannot supply body for package renaming", N);
611 return;
613 elsif Present (Corresponding_Body (Pack_Decl)) then
614 Error_Msg_N ("redefinition of package body", N);
615 return;
616 end if;
618 else
619 Error_Msg_N ("missing specification for package body", N);
620 return;
621 end if;
623 if Is_Package_Or_Generic_Package (Spec_Id)
624 and then (Scope (Spec_Id) = Standard_Standard
625 or else Is_Child_Unit (Spec_Id))
626 and then not Unit_Requires_Body (Spec_Id)
627 then
628 if Ada_Version = Ada_83 then
629 Error_Msg_N
630 ("optional package body (not allowed in Ada 95)??", N);
631 else
632 Error_Msg_N ("spec of this package does not allow a body", N);
633 end if;
634 end if;
635 end if;
637 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
638 Style.Check_Identifier (Body_Id, Spec_Id);
640 if Is_Child_Unit (Spec_Id) then
641 if Nkind (Parent (N)) /= N_Compilation_Unit then
642 Error_Msg_NE
643 ("body of child unit& cannot be an inner package", N, Spec_Id);
644 end if;
646 Set_Is_Child_Unit (Body_Id);
647 end if;
649 -- Generic package case
651 if Ekind (Spec_Id) = E_Generic_Package then
653 -- Disable expansion and perform semantic analysis on copy. The
654 -- unannotated body will be used in all instantiations.
656 Body_Id := Defining_Entity (N);
657 Set_Ekind (Body_Id, E_Package_Body);
658 Set_Scope (Body_Id, Scope (Spec_Id));
659 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
660 Set_Body_Entity (Spec_Id, Body_Id);
661 Set_Spec_Entity (Body_Id, Spec_Id);
663 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
664 Rewrite (N, New_N);
666 -- Once the contents of the generic copy and the template are
667 -- swapped, do the same for their respective aspect specifications.
669 Exchange_Aspects (N, New_N);
671 -- Update Body_Id to point to the copied node for the remainder of
672 -- the processing.
674 Body_Id := Defining_Entity (N);
675 Start_Generic;
676 end if;
678 -- The Body_Id is that of the copied node in the generic case, the
679 -- current node otherwise. Note that N was rewritten above, so we must
680 -- be sure to get the latest Body_Id value.
682 Set_Ekind (Body_Id, E_Package_Body);
683 Set_Body_Entity (Spec_Id, Body_Id);
684 Set_Spec_Entity (Body_Id, Spec_Id);
685 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
687 -- Defining name for the package body is not a visible entity: Only the
688 -- defining name for the declaration is visible.
690 Set_Etype (Body_Id, Standard_Void_Type);
691 Set_Scope (Body_Id, Scope (Spec_Id));
692 Set_Corresponding_Spec (N, Spec_Id);
693 Set_Corresponding_Body (Pack_Decl, Body_Id);
695 -- The body entity is not used for semantics or code generation, but
696 -- it is attached to the entity list of the enclosing scope to simplify
697 -- the listing of back-annotations for the types it main contain.
699 if Scope (Spec_Id) /= Standard_Standard then
700 Append_Entity (Body_Id, Scope (Spec_Id));
701 end if;
703 -- Indicate that we are currently compiling the body of the package
705 Set_In_Package_Body (Spec_Id);
706 Set_Has_Completion (Spec_Id);
707 Last_Spec_Entity := Last_Entity (Spec_Id);
709 if Has_Aspects (N) then
710 Analyze_Aspect_Specifications (N, Body_Id);
711 end if;
713 Push_Scope (Spec_Id);
715 -- Set SPARK_Mode only for non-generic package
717 if Ekind (Spec_Id) = E_Package then
719 -- Set SPARK_Mode from context
721 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
722 Set_SPARK_Pragma_Inherited (Body_Id, True);
724 -- Set elaboration code SPARK mode the same for now
726 Set_SPARK_Aux_Pragma (Body_Id, SPARK_Pragma (Body_Id));
727 Set_SPARK_Aux_Pragma_Inherited (Body_Id, True);
728 end if;
730 -- Inherit the "ghostness" of the subprogram spec. Note that this
731 -- property is not directly inherited as the body may be subject to a
732 -- different Ghost assertion policy.
734 if Is_Ghost_Entity (Spec_Id) or else Within_Ghost_Scope then
735 Set_Is_Ghost_Entity (Body_Id);
737 -- The Ghost policy in effect at the point of declaration and at the
738 -- point of completion must match (SPARK RM 6.9(15)).
740 Check_Ghost_Completion (Spec_Id, Body_Id);
741 end if;
743 Set_Categorization_From_Pragmas (N);
745 Install_Visible_Declarations (Spec_Id);
746 Install_Private_Declarations (Spec_Id);
747 Install_Private_With_Clauses (Spec_Id);
748 Install_Composite_Operations (Spec_Id);
750 Check_Anonymous_Access_Types (Spec_Id, N);
752 if Ekind (Spec_Id) = E_Generic_Package then
753 Set_Use (Generic_Formal_Declarations (Pack_Decl));
754 end if;
756 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
757 Set_Use (Private_Declarations (Specification (Pack_Decl)));
759 -- This is a nested package, so it may be necessary to declare certain
760 -- inherited subprograms that are not yet visible because the parent
761 -- type's subprograms are now visible.
763 if Ekind (Scope (Spec_Id)) = E_Package
764 and then Scope (Spec_Id) /= Standard_Standard
765 then
766 Declare_Inherited_Private_Subprograms (Spec_Id);
767 end if;
769 if Present (Declarations (N)) then
770 Analyze_Declarations (Declarations (N));
771 Inspect_Deferred_Constant_Completion (Declarations (N));
772 end if;
774 -- Verify that the SPARK_Mode of the body agrees with that of its spec
776 if Present (SPARK_Pragma (Body_Id)) then
777 if Present (SPARK_Aux_Pragma (Spec_Id)) then
778 if Get_SPARK_Mode_From_Pragma (SPARK_Aux_Pragma (Spec_Id)) = Off
779 and then
780 Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Body_Id)) = On
781 then
782 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
783 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
784 Error_Msg_Sloc := Sloc (SPARK_Aux_Pragma (Spec_Id));
785 Error_Msg_NE
786 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
787 end if;
789 else
790 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
791 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
792 Error_Msg_Sloc := Sloc (Spec_Id);
793 Error_Msg_NE
794 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
795 end if;
796 end if;
798 -- Analyze_Declarations has caused freezing of all types. Now generate
799 -- bodies for RACW primitives and stream attributes, if any.
801 if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then
803 -- Attach subprogram bodies to support RACWs declared in spec
805 Append_RACW_Bodies (Declarations (N), Spec_Id);
806 Analyze_List (Declarations (N));
807 end if;
809 HSS := Handled_Statement_Sequence (N);
811 if Present (HSS) then
812 Process_End_Label (HSS, 't', Spec_Id);
813 Analyze (HSS);
815 -- Check that elaboration code in a preelaborable package body is
816 -- empty other than null statements and labels (RM 10.2.1(6)).
818 Validate_Null_Statement_Sequence (N);
819 end if;
821 Validate_Categorization_Dependency (N, Spec_Id);
822 Check_Completion (Body_Id);
824 -- Generate start of body reference. Note that we do this fairly late,
825 -- because the call will use In_Extended_Main_Source_Unit as a check,
826 -- and we want to make sure that Corresponding_Stub links are set
828 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
830 -- For a generic package, collect global references and mark them on
831 -- the original body so that they are not resolved again at the point
832 -- of instantiation.
834 if Ekind (Spec_Id) /= E_Package then
835 Save_Global_References (Original_Node (N));
836 End_Generic;
837 end if;
839 -- The entities of the package body have so far been chained onto the
840 -- declaration chain for the spec. That's been fine while we were in the
841 -- body, since we wanted them to be visible, but now that we are leaving
842 -- the package body, they are no longer visible, so we remove them from
843 -- the entity chain of the package spec entity, and copy them to the
844 -- entity chain of the package body entity, where they will never again
845 -- be visible.
847 if Present (Last_Spec_Entity) then
848 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
849 Set_Next_Entity (Last_Spec_Entity, Empty);
850 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
851 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
853 else
854 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
855 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
856 Set_First_Entity (Spec_Id, Empty);
857 Set_Last_Entity (Spec_Id, Empty);
858 end if;
860 End_Package_Scope (Spec_Id);
862 -- All entities declared in body are not visible
864 declare
865 E : Entity_Id;
867 begin
868 E := First_Entity (Body_Id);
869 while Present (E) loop
870 Set_Is_Immediately_Visible (E, False);
871 Set_Is_Potentially_Use_Visible (E, False);
872 Set_Is_Hidden (E);
874 -- Child units may appear on the entity list (e.g. if they appear
875 -- in the context of a subunit) but they are not body entities.
877 if not Is_Child_Unit (E) then
878 Set_Is_Package_Body_Entity (E);
879 end if;
881 Next_Entity (E);
882 end loop;
883 end;
885 Check_References (Body_Id);
887 -- For a generic unit, check that the formal parameters are referenced,
888 -- and that local variables are used, as for regular packages.
890 if Ekind (Spec_Id) = E_Generic_Package then
891 Check_References (Spec_Id);
892 end if;
894 -- At this point all entities of the package body are externally visible
895 -- to the linker as their Is_Public flag is set to True. This proactive
896 -- approach is necessary because an inlined or a generic body for which
897 -- code is generated in other units may need to see these entities. Cut
898 -- down the number of global symbols that do not neet public visibility
899 -- as this has two beneficial effects:
900 -- (1) It makes the compilation process more efficient.
901 -- (2) It gives the code generatormore freedom to optimize within each
902 -- unit, especially subprograms.
904 -- This is done only for top level library packages or child units as
905 -- the algorithm does a top down traversal of the package body.
907 if (Scope (Spec_Id) = Standard_Standard or else Is_Child_Unit (Spec_Id))
908 and then not Is_Generic_Unit (Spec_Id)
909 then
910 Hide_Public_Entities (Declarations (N));
911 end if;
913 -- If expander is not active, then here is where we turn off the
914 -- In_Package_Body flag, otherwise it is turned off at the end of the
915 -- corresponding expansion routine. If this is an instance body, we need
916 -- to qualify names of local entities, because the body may have been
917 -- compiled as a preliminary to another instantiation.
919 if not Expander_Active then
920 Set_In_Package_Body (Spec_Id, False);
922 if Is_Generic_Instance (Spec_Id)
923 and then Operating_Mode = Generate_Code
924 then
925 Qualify_Entity_Names (N);
926 end if;
927 end if;
928 end Analyze_Package_Body_Helper;
930 ------------------------------
931 -- Analyze_Package_Contract --
932 ------------------------------
934 procedure Analyze_Package_Contract (Pack_Id : Entity_Id) is
935 Mode : SPARK_Mode_Type;
936 Prag : Node_Id;
938 begin
939 -- Due to the timing of contract analysis, delayed pragmas may be
940 -- subject to the wrong SPARK_Mode, usually that of the enclosing
941 -- context. To remedy this, restore the original SPARK_Mode of the
942 -- related package.
944 Save_SPARK_Mode_And_Set (Pack_Id, Mode);
946 -- Analyze the initialization related pragmas. Initializes must come
947 -- before Initial_Condition due to item dependencies.
949 Prag := Get_Pragma (Pack_Id, Pragma_Initializes);
951 if Present (Prag) then
952 Analyze_Initializes_In_Decl_Part (Prag);
953 end if;
955 Prag := Get_Pragma (Pack_Id, Pragma_Initial_Condition);
957 if Present (Prag) then
958 Analyze_Initial_Condition_In_Decl_Part (Prag);
959 end if;
961 -- Check whether the lack of indicator Part_Of agrees with the placement
962 -- of the package instantiation with respect to the state space.
964 if Is_Generic_Instance (Pack_Id) then
965 Prag := Get_Pragma (Pack_Id, Pragma_Part_Of);
967 if No (Prag) then
968 Check_Missing_Part_Of (Pack_Id);
969 end if;
970 end if;
972 -- Restore the SPARK_Mode of the enclosing context after all delayed
973 -- pragmas have been analyzed.
975 Restore_SPARK_Mode (Mode);
976 end Analyze_Package_Contract;
978 ---------------------------------
979 -- Analyze_Package_Declaration --
980 ---------------------------------
982 procedure Analyze_Package_Declaration (N : Node_Id) is
983 Id : constant Node_Id := Defining_Entity (N);
985 PF : Boolean;
986 -- True when in the context of a declared pure library unit
988 Body_Required : Boolean;
989 -- True when this package declaration requires a corresponding body
991 Comp_Unit : Boolean;
992 -- True when this package declaration is not a nested declaration
994 begin
995 if Debug_Flag_C then
996 Write_Str ("==> package spec ");
997 Write_Name (Chars (Id));
998 Write_Str (" from ");
999 Write_Location (Sloc (N));
1000 Write_Eol;
1001 Indent;
1002 end if;
1004 Generate_Definition (Id);
1005 Enter_Name (Id);
1006 Set_Ekind (Id, E_Package);
1007 Set_Etype (Id, Standard_Void_Type);
1008 Set_Contract (Id, Make_Contract (Sloc (Id)));
1010 -- Set SPARK_Mode from context only for non-generic package
1012 if Ekind (Id) = E_Package then
1013 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
1014 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
1015 Set_SPARK_Pragma_Inherited (Id, True);
1016 Set_SPARK_Aux_Pragma_Inherited (Id, True);
1017 end if;
1019 -- Analyze aspect specifications immediately, since we need to recognize
1020 -- things like Pure early enough to diagnose violations during analysis.
1022 if Has_Aspects (N) then
1023 Analyze_Aspect_Specifications (N, Id);
1024 end if;
1026 -- Ada 2005 (AI-217): Check if the package has been illegally named
1027 -- in a limited-with clause of its own context. In this case the error
1028 -- has been previously notified by Analyze_Context.
1030 -- limited with Pkg; -- ERROR
1031 -- package Pkg is ...
1033 if From_Limited_With (Id) then
1034 return;
1035 end if;
1037 Push_Scope (Id);
1039 PF := Is_Pure (Enclosing_Lib_Unit_Entity);
1040 Set_Is_Pure (Id, PF);
1042 Set_Categorization_From_Pragmas (N);
1044 Analyze (Specification (N));
1045 Validate_Categorization_Dependency (N, Id);
1047 Body_Required := Unit_Requires_Body (Id);
1049 -- When this spec does not require an explicit body, we know that there
1050 -- are no entities requiring completion in the language sense; we call
1051 -- Check_Completion here only to ensure that any nested package
1052 -- declaration that requires an implicit body gets one. (In the case
1053 -- where a body is required, Check_Completion is called at the end of
1054 -- the body's declarative part.)
1056 if not Body_Required then
1057 Check_Completion;
1058 end if;
1060 Comp_Unit := Nkind (Parent (N)) = N_Compilation_Unit;
1061 if Comp_Unit then
1063 -- Set Body_Required indication on the compilation unit node, and
1064 -- determine whether elaboration warnings may be meaningful on it.
1066 Set_Body_Required (Parent (N), Body_Required);
1068 if not Body_Required then
1069 Set_Suppress_Elaboration_Warnings (Id);
1070 end if;
1072 end if;
1074 End_Package_Scope (Id);
1076 -- For the declaration of a library unit that is a remote types package,
1077 -- check legality rules regarding availability of stream attributes for
1078 -- types that contain non-remote access values. This subprogram performs
1079 -- visibility tests that rely on the fact that we have exited the scope
1080 -- of Id.
1082 if Comp_Unit then
1083 Validate_RT_RAT_Component (N);
1084 end if;
1086 if Debug_Flag_C then
1087 Outdent;
1088 Write_Str ("<== package spec ");
1089 Write_Name (Chars (Id));
1090 Write_Str (" from ");
1091 Write_Location (Sloc (N));
1092 Write_Eol;
1093 end if;
1094 end Analyze_Package_Declaration;
1096 -----------------------------------
1097 -- Analyze_Package_Specification --
1098 -----------------------------------
1100 -- Note that this code is shared for the analysis of generic package specs
1101 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
1103 procedure Analyze_Package_Specification (N : Node_Id) is
1104 Id : constant Entity_Id := Defining_Entity (N);
1105 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
1106 Vis_Decls : constant List_Id := Visible_Declarations (N);
1107 Priv_Decls : constant List_Id := Private_Declarations (N);
1108 E : Entity_Id;
1109 L : Entity_Id;
1110 Public_Child : Boolean;
1112 Private_With_Clauses_Installed : Boolean := False;
1113 -- In Ada 2005, private with_clauses are visible in the private part
1114 -- of a nested package, even if it appears in the public part of the
1115 -- enclosing package. This requires a separate step to install these
1116 -- private_with_clauses, and remove them at the end of the nested
1117 -- package.
1119 procedure Check_One_Tagged_Type_Or_Extension_At_Most;
1120 -- Issue an error in SPARK mode if a package specification contains
1121 -- more than one tagged type or type extension.
1123 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
1124 -- Clears constant indications (Never_Set_In_Source, Constant_Value, and
1125 -- Is_True_Constant) on all variables that are entities of Id, and on
1126 -- the chain whose first element is FE. A recursive call is made for all
1127 -- packages and generic packages.
1129 procedure Generate_Parent_References;
1130 -- For a child unit, generate references to parent units, for
1131 -- GPS navigation purposes.
1133 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
1134 -- Child and Unit are entities of compilation units. True if Child
1135 -- is a public child of Parent as defined in 10.1.1
1137 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
1138 -- Reject completion of an incomplete or private type declarations
1139 -- having a known discriminant part by an unchecked union.
1141 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
1142 -- Given the package entity of a generic package instantiation or
1143 -- formal package whose corresponding generic is a child unit, installs
1144 -- the private declarations of each of the child unit's parents.
1145 -- This has to be done at the point of entering the instance package's
1146 -- private part rather than being done in Sem_Ch12.Install_Parent
1147 -- (which is where the parents' visible declarations are installed).
1149 ------------------------------------------------
1150 -- Check_One_Tagged_Type_Or_Extension_At_Most --
1151 ------------------------------------------------
1153 procedure Check_One_Tagged_Type_Or_Extension_At_Most is
1154 Previous : Node_Id;
1156 procedure Check_Decls (Decls : List_Id);
1157 -- Check that either Previous is Empty and Decls does not contain
1158 -- more than one tagged type or type extension, or Previous is
1159 -- already set and Decls contains no tagged type or type extension.
1161 -----------------
1162 -- Check_Decls --
1163 -----------------
1165 procedure Check_Decls (Decls : List_Id) is
1166 Decl : Node_Id;
1168 begin
1169 Decl := First (Decls);
1170 while Present (Decl) loop
1171 if Nkind (Decl) = N_Full_Type_Declaration
1172 and then Is_Tagged_Type (Defining_Identifier (Decl))
1173 then
1174 if No (Previous) then
1175 Previous := Decl;
1177 else
1178 Error_Msg_Sloc := Sloc (Previous);
1179 Check_SPARK_05_Restriction
1180 ("at most one tagged type or type extension allowed",
1181 "\\ previous declaration#",
1182 Decl);
1183 end if;
1184 end if;
1186 Next (Decl);
1187 end loop;
1188 end Check_Decls;
1190 -- Start of processing for Check_One_Tagged_Type_Or_Extension_At_Most
1192 begin
1193 Previous := Empty;
1194 Check_Decls (Vis_Decls);
1196 if Present (Priv_Decls) then
1197 Check_Decls (Priv_Decls);
1198 end if;
1199 end Check_One_Tagged_Type_Or_Extension_At_Most;
1201 ---------------------
1202 -- Clear_Constants --
1203 ---------------------
1205 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
1206 E : Entity_Id;
1208 begin
1209 -- Ignore package renamings, not interesting and they can cause self
1210 -- referential loops in the code below.
1212 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
1213 return;
1214 end if;
1216 -- Note: in the loop below, the check for Next_Entity pointing back
1217 -- to the package entity may seem odd, but it is needed, because a
1218 -- package can contain a renaming declaration to itself, and such
1219 -- renamings are generated automatically within package instances.
1221 E := FE;
1222 while Present (E) and then E /= Id loop
1223 if Is_Assignable (E) then
1224 Set_Never_Set_In_Source (E, False);
1225 Set_Is_True_Constant (E, False);
1226 Set_Current_Value (E, Empty);
1227 Set_Is_Known_Null (E, False);
1228 Set_Last_Assignment (E, Empty);
1230 if not Can_Never_Be_Null (E) then
1231 Set_Is_Known_Non_Null (E, False);
1232 end if;
1234 elsif Is_Package_Or_Generic_Package (E) then
1235 Clear_Constants (E, First_Entity (E));
1236 Clear_Constants (E, First_Private_Entity (E));
1237 end if;
1239 Next_Entity (E);
1240 end loop;
1241 end Clear_Constants;
1243 --------------------------------
1244 -- Generate_Parent_References --
1245 --------------------------------
1247 procedure Generate_Parent_References is
1248 Decl : constant Node_Id := Parent (N);
1250 begin
1251 if Id = Cunit_Entity (Main_Unit)
1252 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
1253 then
1254 Generate_Reference (Id, Scope (Id), 'k', False);
1256 elsif not Nkind_In (Unit (Cunit (Main_Unit)), N_Subprogram_Body,
1257 N_Subunit)
1258 then
1259 -- If current unit is an ancestor of main unit, generate a
1260 -- reference to its own parent.
1262 declare
1263 U : Node_Id;
1264 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
1266 begin
1267 if Nkind (Main_Spec) = N_Package_Body then
1268 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
1269 end if;
1271 U := Parent_Spec (Main_Spec);
1272 while Present (U) loop
1273 if U = Parent (Decl) then
1274 Generate_Reference (Id, Scope (Id), 'k', False);
1275 exit;
1277 elsif Nkind (Unit (U)) = N_Package_Body then
1278 exit;
1280 else
1281 U := Parent_Spec (Unit (U));
1282 end if;
1283 end loop;
1284 end;
1285 end if;
1286 end Generate_Parent_References;
1288 ---------------------
1289 -- Is_Public_Child --
1290 ---------------------
1292 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
1293 begin
1294 if not Is_Private_Descendant (Child) then
1295 return True;
1296 else
1297 if Child = Unit then
1298 return not Private_Present (
1299 Parent (Unit_Declaration_Node (Child)));
1300 else
1301 return Is_Public_Child (Scope (Child), Unit);
1302 end if;
1303 end if;
1304 end Is_Public_Child;
1306 ----------------------------------------
1307 -- Inspect_Unchecked_Union_Completion --
1308 ----------------------------------------
1310 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
1311 Decl : Node_Id;
1313 begin
1314 Decl := First (Decls);
1315 while Present (Decl) loop
1317 -- We are looking at an incomplete or private type declaration
1318 -- with a known_discriminant_part whose full view is an
1319 -- Unchecked_Union.
1321 if Nkind_In (Decl, N_Incomplete_Type_Declaration,
1322 N_Private_Type_Declaration)
1323 and then Has_Discriminants (Defining_Identifier (Decl))
1324 and then Present (Full_View (Defining_Identifier (Decl)))
1325 and then
1326 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
1327 then
1328 Error_Msg_N
1329 ("completion of discriminated partial view "
1330 & "cannot be an unchecked union",
1331 Full_View (Defining_Identifier (Decl)));
1332 end if;
1334 Next (Decl);
1335 end loop;
1336 end Inspect_Unchecked_Union_Completion;
1338 -----------------------------------------
1339 -- Install_Parent_Private_Declarations --
1340 -----------------------------------------
1342 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
1343 Inst_Par : Entity_Id;
1344 Gen_Par : Entity_Id;
1345 Inst_Node : Node_Id;
1347 begin
1348 Inst_Par := Inst_Id;
1350 Gen_Par :=
1351 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
1352 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
1353 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
1355 if Nkind_In (Inst_Node, N_Package_Instantiation,
1356 N_Formal_Package_Declaration)
1357 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
1358 then
1359 Inst_Par := Entity (Prefix (Name (Inst_Node)));
1361 if Present (Renamed_Entity (Inst_Par)) then
1362 Inst_Par := Renamed_Entity (Inst_Par);
1363 end if;
1365 Gen_Par :=
1366 Generic_Parent
1367 (Specification (Unit_Declaration_Node (Inst_Par)));
1369 -- Install the private declarations and private use clauses
1370 -- of a parent instance of the child instance, unless the
1371 -- parent instance private declarations have already been
1372 -- installed earlier in Analyze_Package_Specification, which
1373 -- happens when a generic child is instantiated, and the
1374 -- instance is a child of the parent instance.
1376 -- Installing the use clauses of the parent instance twice
1377 -- is both unnecessary and wrong, because it would cause the
1378 -- clauses to be chained to themselves in the use clauses
1379 -- list of the scope stack entry. That in turn would cause
1380 -- an endless loop from End_Use_Clauses upon scope exit.
1382 -- The parent is now fully visible. It may be a hidden open
1383 -- scope if we are currently compiling some child instance
1384 -- declared within it, but while the current instance is being
1385 -- compiled the parent is immediately visible. In particular
1386 -- its entities must remain visible if a stack save/restore
1387 -- takes place through a call to Rtsfind.
1389 if Present (Gen_Par) then
1390 if not In_Private_Part (Inst_Par) then
1391 Install_Private_Declarations (Inst_Par);
1392 Set_Use (Private_Declarations
1393 (Specification
1394 (Unit_Declaration_Node (Inst_Par))));
1395 Set_Is_Hidden_Open_Scope (Inst_Par, False);
1396 end if;
1398 -- If we've reached the end of the generic instance parents,
1399 -- then finish off by looping through the nongeneric parents
1400 -- and installing their private declarations.
1402 -- If one of the non-generic parents is itself on the scope
1403 -- stack, do not install its private declarations: they are
1404 -- installed in due time when the private part of that parent
1405 -- is analyzed. This is delicate ???
1407 else
1408 while Present (Inst_Par)
1409 and then Inst_Par /= Standard_Standard
1410 and then (not In_Open_Scopes (Inst_Par)
1411 or else not In_Private_Part (Inst_Par))
1412 loop
1413 Install_Private_Declarations (Inst_Par);
1414 Set_Use (Private_Declarations
1415 (Specification
1416 (Unit_Declaration_Node (Inst_Par))));
1417 Inst_Par := Scope (Inst_Par);
1418 end loop;
1420 exit;
1421 end if;
1423 else
1424 exit;
1425 end if;
1426 end loop;
1427 end Install_Parent_Private_Declarations;
1429 -- Start of processing for Analyze_Package_Specification
1431 begin
1432 if Present (Vis_Decls) then
1433 Analyze_Declarations (Vis_Decls);
1434 end if;
1436 -- Inspect the entities defined in the package and ensure that all
1437 -- incomplete types have received full declarations. Build default
1438 -- initial condition and invariant procedures for all qualifying types.
1440 E := First_Entity (Id);
1441 while Present (E) loop
1443 -- Check on incomplete types
1445 -- AI05-0213: A formal incomplete type has no completion
1447 if Ekind (E) = E_Incomplete_Type
1448 and then No (Full_View (E))
1449 and then not Is_Generic_Type (E)
1450 then
1451 Error_Msg_N ("no declaration in visible part for incomplete}", E);
1452 end if;
1454 if Is_Type (E) then
1456 -- Each private type subject to pragma Default_Initial_Condition
1457 -- declares a specialized procedure which verifies the assumption
1458 -- of the pragma. The declaration appears in the visible part of
1459 -- the package to allow for being called from the outside.
1461 if Has_Default_Init_Cond (E) then
1462 Build_Default_Init_Cond_Procedure_Declaration (E);
1464 -- A private extension inherits the default initial condition
1465 -- procedure from its parent type.
1467 elsif Has_Inherited_Default_Init_Cond (E) then
1468 Inherit_Default_Init_Cond_Procedure (E);
1469 end if;
1471 -- If invariants are present, build the invariant procedure for a
1472 -- private type, but not any of its subtypes.
1474 if Has_Invariants (E) then
1475 if Ekind (E) = E_Private_Subtype then
1476 null;
1477 else
1478 Build_Invariant_Procedure (E, N);
1479 end if;
1480 end if;
1481 end if;
1483 Next_Entity (E);
1484 end loop;
1486 if Is_Remote_Call_Interface (Id)
1487 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1488 then
1489 Validate_RCI_Declarations (Id);
1490 end if;
1492 -- Save global references in the visible declarations, before installing
1493 -- private declarations of parent unit if there is one, because the
1494 -- privacy status of types defined in the parent will change. This is
1495 -- only relevant for generic child units, but is done in all cases for
1496 -- uniformity.
1498 if Ekind (Id) = E_Generic_Package
1499 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1500 then
1501 declare
1502 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1503 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1504 begin
1505 Set_Private_Declarations (Orig_Spec, Empty_List);
1506 Save_Global_References (Orig_Decl);
1507 Set_Private_Declarations (Orig_Spec, Save_Priv);
1508 end;
1509 end if;
1511 -- If package is a public child unit, then make the private declarations
1512 -- of the parent visible.
1514 Public_Child := False;
1516 declare
1517 Par : Entity_Id;
1518 Pack_Decl : Node_Id;
1519 Par_Spec : Node_Id;
1521 begin
1522 Par := Id;
1523 Par_Spec := Parent_Spec (Parent (N));
1525 -- If the package is formal package of an enclosing generic, it is
1526 -- transformed into a local generic declaration, and compiled to make
1527 -- its spec available. We need to retrieve the original generic to
1528 -- determine whether it is a child unit, and install its parents.
1530 if No (Par_Spec)
1531 and then
1532 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1533 then
1534 Par := Entity (Name (Original_Node (Parent (N))));
1535 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1536 end if;
1538 if Present (Par_Spec) then
1539 Generate_Parent_References;
1541 while Scope (Par) /= Standard_Standard
1542 and then Is_Public_Child (Id, Par)
1543 and then In_Open_Scopes (Par)
1544 loop
1545 Public_Child := True;
1546 Par := Scope (Par);
1547 Install_Private_Declarations (Par);
1548 Install_Private_With_Clauses (Par);
1549 Pack_Decl := Unit_Declaration_Node (Par);
1550 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1551 end loop;
1552 end if;
1553 end;
1555 if Is_Compilation_Unit (Id) then
1556 Install_Private_With_Clauses (Id);
1557 else
1559 -- The current compilation unit may include private with_clauses,
1560 -- which are visible in the private part of the current nested
1561 -- package, and have to be installed now. This is not done for
1562 -- nested instantiations, where the private with_clauses of the
1563 -- enclosing unit have no effect once the instantiation info is
1564 -- established and we start analyzing the package declaration.
1566 declare
1567 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1568 begin
1569 if Is_Package_Or_Generic_Package (Comp_Unit)
1570 and then not In_Private_Part (Comp_Unit)
1571 and then not In_Instance
1572 then
1573 Install_Private_With_Clauses (Comp_Unit);
1574 Private_With_Clauses_Installed := True;
1575 end if;
1576 end;
1577 end if;
1579 -- If this is a package associated with a generic instance or formal
1580 -- package, then the private declarations of each of the generic's
1581 -- parents must be installed at this point.
1583 if Is_Generic_Instance (Id) then
1584 Install_Parent_Private_Declarations (Id);
1585 end if;
1587 -- Analyze private part if present. The flag In_Private_Part is reset
1588 -- in End_Package_Scope.
1590 L := Last_Entity (Id);
1592 if Present (Priv_Decls) then
1593 Set_In_Private_Part (Id);
1595 -- Upon entering a public child's private part, it may be necessary
1596 -- to declare subprograms that were derived in the package's visible
1597 -- part but not yet made visible.
1599 if Public_Child then
1600 Declare_Inherited_Private_Subprograms (Id);
1601 end if;
1603 Analyze_Declarations (Priv_Decls);
1605 -- Check the private declarations for incomplete deferred constants
1607 Inspect_Deferred_Constant_Completion (Priv_Decls);
1609 -- The first private entity is the immediate follower of the last
1610 -- visible entity, if there was one.
1612 if Present (L) then
1613 Set_First_Private_Entity (Id, Next_Entity (L));
1614 else
1615 Set_First_Private_Entity (Id, First_Entity (Id));
1616 end if;
1618 -- There may be inherited private subprograms that need to be declared,
1619 -- even in the absence of an explicit private part. If there are any
1620 -- public declarations in the package and the package is a public child
1621 -- unit, then an implicit private part is assumed.
1623 elsif Present (L) and then Public_Child then
1624 Set_In_Private_Part (Id);
1625 Declare_Inherited_Private_Subprograms (Id);
1626 Set_First_Private_Entity (Id, Next_Entity (L));
1627 end if;
1629 E := First_Entity (Id);
1630 while Present (E) loop
1632 -- Check rule of 3.6(11), which in general requires waiting till all
1633 -- full types have been seen.
1635 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1636 Check_Aliased_Component_Types (E);
1637 end if;
1639 -- Check preelaborable initialization for full type completing a
1640 -- private type for which pragma Preelaborable_Initialization given.
1642 if Is_Type (E)
1643 and then Must_Have_Preelab_Init (E)
1644 and then not Has_Preelaborable_Initialization (E)
1645 then
1646 Error_Msg_N
1647 ("full view of & does not have preelaborable initialization", E);
1648 end if;
1650 -- An invariant may appear on a full view of a type
1652 if Is_Type (E)
1653 and then Has_Private_Declaration (E)
1654 and then Nkind (Parent (E)) = N_Full_Type_Declaration
1655 and then Has_Aspects (Parent (E))
1656 then
1657 declare
1658 ASN : Node_Id;
1660 begin
1661 ASN := First (Aspect_Specifications (Parent (E)));
1662 while Present (ASN) loop
1663 if Nam_In (Chars (Identifier (ASN)), Name_Invariant,
1664 Name_Type_Invariant)
1665 then
1666 Build_Invariant_Procedure (E, N);
1667 exit;
1668 end if;
1670 Next (ASN);
1671 end loop;
1672 end;
1673 end if;
1675 Next_Entity (E);
1676 end loop;
1678 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1679 -- declaration having a known_discriminant_part shall not be an
1680 -- unchecked union type.
1682 if Present (Vis_Decls) then
1683 Inspect_Unchecked_Union_Completion (Vis_Decls);
1684 end if;
1686 if Present (Priv_Decls) then
1687 Inspect_Unchecked_Union_Completion (Priv_Decls);
1688 end if;
1690 if Ekind (Id) = E_Generic_Package
1691 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1692 and then Present (Priv_Decls)
1693 then
1694 -- Save global references in private declarations, ignoring the
1695 -- visible declarations that were processed earlier.
1697 declare
1698 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1699 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1700 Save_Form : constant List_Id :=
1701 Generic_Formal_Declarations (Orig_Decl);
1703 begin
1704 Set_Visible_Declarations (Orig_Spec, Empty_List);
1705 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1706 Save_Global_References (Orig_Decl);
1707 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1708 Set_Visible_Declarations (Orig_Spec, Save_Vis);
1709 end;
1710 end if;
1712 Process_End_Label (N, 'e', Id);
1714 -- Remove private_with_clauses of enclosing compilation unit, if they
1715 -- were installed.
1717 if Private_With_Clauses_Installed then
1718 Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
1719 end if;
1721 -- For the case of a library level package, we must go through all the
1722 -- entities clearing the indications that the value may be constant and
1723 -- not modified. Why? Because any client of this package may modify
1724 -- these values freely from anywhere. This also applies to any nested
1725 -- packages or generic packages.
1727 -- For now we unconditionally clear constants for packages that are
1728 -- instances of generic packages. The reason is that we do not have the
1729 -- body yet, and we otherwise think things are unreferenced when they
1730 -- are not. This should be fixed sometime (the effect is not terrible,
1731 -- we just lose some warnings, and also some cases of value propagation)
1732 -- ???
1734 if Is_Library_Level_Entity (Id)
1735 or else Is_Generic_Instance (Id)
1736 then
1737 Clear_Constants (Id, First_Entity (Id));
1738 Clear_Constants (Id, First_Private_Entity (Id));
1739 end if;
1741 -- Issue an error in SPARK mode if a package specification contains
1742 -- more than one tagged type or type extension.
1744 Check_One_Tagged_Type_Or_Extension_At_Most;
1746 -- If switch set, output information on why body required
1748 if List_Body_Required_Info
1749 and then In_Extended_Main_Source_Unit (Id)
1750 and then Unit_Requires_Body (Id)
1751 then
1752 Unit_Requires_Body_Info (Id);
1753 end if;
1754 end Analyze_Package_Specification;
1756 --------------------------------------
1757 -- Analyze_Private_Type_Declaration --
1758 --------------------------------------
1760 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1761 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
1762 Id : constant Entity_Id := Defining_Identifier (N);
1764 begin
1765 Generate_Definition (Id);
1766 Set_Is_Pure (Id, PF);
1767 Init_Size_Align (Id);
1769 if not Is_Package_Or_Generic_Package (Current_Scope)
1770 or else In_Private_Part (Current_Scope)
1771 then
1772 Error_Msg_N ("invalid context for private declaration", N);
1773 end if;
1775 New_Private_Type (N, Id, N);
1776 Set_Depends_On_Private (Id);
1778 -- A type declared within a Ghost scope is automatically Ghost
1779 -- (SPARK RM 6.9(2)).
1781 if Within_Ghost_Scope then
1782 Set_Is_Ghost_Entity (Id);
1783 end if;
1785 if Has_Aspects (N) then
1786 Analyze_Aspect_Specifications (N, Id);
1787 end if;
1788 end Analyze_Private_Type_Declaration;
1790 ----------------------------------
1791 -- Check_Anonymous_Access_Types --
1792 ----------------------------------
1794 procedure Check_Anonymous_Access_Types
1795 (Spec_Id : Entity_Id;
1796 P_Body : Node_Id)
1798 E : Entity_Id;
1799 IR : Node_Id;
1801 begin
1802 -- Itype references are only needed by gigi, to force elaboration of
1803 -- itypes. In the absence of code generation, they are not needed.
1805 if not Expander_Active then
1806 return;
1807 end if;
1809 E := First_Entity (Spec_Id);
1810 while Present (E) loop
1811 if Ekind (E) = E_Anonymous_Access_Type
1812 and then From_Limited_With (E)
1813 then
1814 IR := Make_Itype_Reference (Sloc (P_Body));
1815 Set_Itype (IR, E);
1817 if No (Declarations (P_Body)) then
1818 Set_Declarations (P_Body, New_List (IR));
1819 else
1820 Prepend (IR, Declarations (P_Body));
1821 end if;
1822 end if;
1824 Next_Entity (E);
1825 end loop;
1826 end Check_Anonymous_Access_Types;
1828 -------------------------------------------
1829 -- Declare_Inherited_Private_Subprograms --
1830 -------------------------------------------
1832 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1834 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1835 -- Check whether an inherited subprogram S is an operation of an
1836 -- untagged derived type T.
1838 ---------------------
1839 -- Is_Primitive_Of --
1840 ---------------------
1842 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1843 Formal : Entity_Id;
1845 begin
1846 -- If the full view is a scalar type, the type is the anonymous base
1847 -- type, but the operation mentions the first subtype, so check the
1848 -- signature against the base type.
1850 if Base_Type (Etype (S)) = Base_Type (T) then
1851 return True;
1853 else
1854 Formal := First_Formal (S);
1855 while Present (Formal) loop
1856 if Base_Type (Etype (Formal)) = Base_Type (T) then
1857 return True;
1858 end if;
1860 Next_Formal (Formal);
1861 end loop;
1863 return False;
1864 end if;
1865 end Is_Primitive_Of;
1867 -- Local variables
1869 E : Entity_Id;
1870 Op_List : Elist_Id;
1871 Op_Elmt : Elmt_Id;
1872 Op_Elmt_2 : Elmt_Id;
1873 Prim_Op : Entity_Id;
1874 New_Op : Entity_Id := Empty;
1875 Parent_Subp : Entity_Id;
1876 Tag : Entity_Id;
1878 -- Start of processing for Declare_Inherited_Private_Subprograms
1880 begin
1881 E := First_Entity (Id);
1882 while Present (E) loop
1884 -- If the entity is a nonprivate type extension whose parent type
1885 -- is declared in an open scope, then the type may have inherited
1886 -- operations that now need to be made visible. Ditto if the entity
1887 -- is a formal derived type in a child unit.
1889 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
1890 or else
1891 (Nkind (Parent (E)) = N_Private_Extension_Declaration
1892 and then Is_Generic_Type (E)))
1893 and then In_Open_Scopes (Scope (Etype (E)))
1894 and then Is_Base_Type (E)
1895 then
1896 if Is_Tagged_Type (E) then
1897 Op_List := Primitive_Operations (E);
1898 New_Op := Empty;
1899 Tag := First_Tag_Component (E);
1901 Op_Elmt := First_Elmt (Op_List);
1902 while Present (Op_Elmt) loop
1903 Prim_Op := Node (Op_Elmt);
1905 -- Search primitives that are implicit operations with an
1906 -- internal name whose parent operation has a normal name.
1908 if Present (Alias (Prim_Op))
1909 and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
1910 and then not Comes_From_Source (Prim_Op)
1911 and then Is_Internal_Name (Chars (Prim_Op))
1912 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1913 then
1914 Parent_Subp := Alias (Prim_Op);
1916 -- Case 1: Check if the type has also an explicit
1917 -- overriding for this primitive.
1919 Op_Elmt_2 := Next_Elmt (Op_Elmt);
1920 while Present (Op_Elmt_2) loop
1922 -- Skip entities with attribute Interface_Alias since
1923 -- they are not overriding primitives (these entities
1924 -- link an interface primitive with their covering
1925 -- primitive)
1927 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
1928 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
1929 and then No (Interface_Alias (Node (Op_Elmt_2)))
1930 then
1931 -- The private inherited operation has been
1932 -- overridden by an explicit subprogram:
1933 -- replace the former by the latter.
1935 New_Op := Node (Op_Elmt_2);
1936 Replace_Elmt (Op_Elmt, New_Op);
1937 Remove_Elmt (Op_List, Op_Elmt_2);
1938 Set_Overridden_Operation (New_Op, Parent_Subp);
1940 -- We don't need to inherit its dispatching slot.
1941 -- Set_All_DT_Position has previously ensured that
1942 -- the same slot was assigned to the two primitives
1944 if Present (Tag)
1945 and then Present (DTC_Entity (New_Op))
1946 and then Present (DTC_Entity (Prim_Op))
1947 then
1948 pragma Assert
1949 (DT_Position (New_Op) = DT_Position (Prim_Op));
1950 null;
1951 end if;
1953 goto Next_Primitive;
1954 end if;
1956 Next_Elmt (Op_Elmt_2);
1957 end loop;
1959 -- Case 2: We have not found any explicit overriding and
1960 -- hence we need to declare the operation (i.e., make it
1961 -- visible).
1963 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1965 -- Inherit the dispatching slot if E is already frozen
1967 if Is_Frozen (E)
1968 and then Present (DTC_Entity (Alias (Prim_Op)))
1969 then
1970 Set_DTC_Entity_Value (E, New_Op);
1971 Set_DT_Position (New_Op,
1972 DT_Position (Alias (Prim_Op)));
1973 end if;
1975 pragma Assert
1976 (Is_Dispatching_Operation (New_Op)
1977 and then Node (Last_Elmt (Op_List)) = New_Op);
1979 -- Substitute the new operation for the old one in the
1980 -- type's primitive operations list. Since the new
1981 -- operation was also just added to the end of list,
1982 -- the last element must be removed.
1984 -- (Question: is there a simpler way of declaring the
1985 -- operation, say by just replacing the name of the
1986 -- earlier operation, reentering it in the in the symbol
1987 -- table (how?), and marking it as private???)
1989 Replace_Elmt (Op_Elmt, New_Op);
1990 Remove_Last_Elmt (Op_List);
1991 end if;
1993 <<Next_Primitive>>
1994 Next_Elmt (Op_Elmt);
1995 end loop;
1997 -- Generate listing showing the contents of the dispatch table
1999 if Debug_Flag_ZZ then
2000 Write_DT (E);
2001 end if;
2003 else
2004 -- For untagged type, scan forward to locate inherited hidden
2005 -- operations.
2007 Prim_Op := Next_Entity (E);
2008 while Present (Prim_Op) loop
2009 if Is_Subprogram (Prim_Op)
2010 and then Present (Alias (Prim_Op))
2011 and then not Comes_From_Source (Prim_Op)
2012 and then Is_Internal_Name (Chars (Prim_Op))
2013 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
2014 and then Is_Primitive_Of (E, Prim_Op)
2015 then
2016 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2017 end if;
2019 Next_Entity (Prim_Op);
2021 -- Derived operations appear immediately after the type
2022 -- declaration (or the following subtype indication for
2023 -- a derived scalar type). Further declarations cannot
2024 -- include inherited operations of the type.
2026 if Present (Prim_Op) then
2027 exit when Ekind (Prim_Op) not in Overloadable_Kind;
2028 end if;
2029 end loop;
2030 end if;
2031 end if;
2033 Next_Entity (E);
2034 end loop;
2035 end Declare_Inherited_Private_Subprograms;
2037 -----------------------
2038 -- End_Package_Scope --
2039 -----------------------
2041 procedure End_Package_Scope (P : Entity_Id) is
2042 begin
2043 Uninstall_Declarations (P);
2044 Pop_Scope;
2045 end End_Package_Scope;
2047 ---------------------------
2048 -- Exchange_Declarations --
2049 ---------------------------
2051 procedure Exchange_Declarations (Id : Entity_Id) is
2052 Full_Id : constant Entity_Id := Full_View (Id);
2053 H1 : constant Entity_Id := Homonym (Id);
2054 Next1 : constant Entity_Id := Next_Entity (Id);
2055 H2 : Entity_Id;
2056 Next2 : Entity_Id;
2058 begin
2059 -- If missing full declaration for type, nothing to exchange
2061 if No (Full_Id) then
2062 return;
2063 end if;
2065 -- Otherwise complete the exchange, and preserve semantic links
2067 Next2 := Next_Entity (Full_Id);
2068 H2 := Homonym (Full_Id);
2070 -- Reset full declaration pointer to reflect the switched entities and
2071 -- readjust the next entity chains.
2073 Exchange_Entities (Id, Full_Id);
2075 Set_Next_Entity (Id, Next1);
2076 Set_Homonym (Id, H1);
2078 Set_Full_View (Full_Id, Id);
2079 Set_Next_Entity (Full_Id, Next2);
2080 Set_Homonym (Full_Id, H2);
2081 end Exchange_Declarations;
2083 ----------------------------
2084 -- Install_Package_Entity --
2085 ----------------------------
2087 procedure Install_Package_Entity (Id : Entity_Id) is
2088 begin
2089 if not Is_Internal (Id) then
2090 if Debug_Flag_E then
2091 Write_Str ("Install: ");
2092 Write_Name (Chars (Id));
2093 Write_Eol;
2094 end if;
2096 if Is_Child_Unit (Id) then
2097 null;
2099 -- Do not enter implicitly inherited non-overridden subprograms of
2100 -- a tagged type back into visibility if they have non-conformant
2101 -- homographs (Ada RM 8.3 12.3/2).
2103 elsif Is_Hidden_Non_Overridden_Subpgm (Id) then
2104 null;
2106 else
2107 Set_Is_Immediately_Visible (Id);
2108 end if;
2109 end if;
2110 end Install_Package_Entity;
2112 ----------------------------------
2113 -- Install_Private_Declarations --
2114 ----------------------------------
2116 procedure Install_Private_Declarations (P : Entity_Id) is
2117 Id : Entity_Id;
2118 Full : Entity_Id;
2119 Priv_Deps : Elist_Id;
2121 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
2122 -- When the full view of a private type is made available, we do the
2123 -- same for its private dependents under proper visibility conditions.
2124 -- When compiling a grand-chid unit this needs to be done recursively.
2126 -----------------------------
2127 -- Swap_Private_Dependents --
2128 -----------------------------
2130 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
2131 Deps : Elist_Id;
2132 Priv : Entity_Id;
2133 Priv_Elmt : Elmt_Id;
2134 Is_Priv : Boolean;
2136 begin
2137 Priv_Elmt := First_Elmt (Priv_Deps);
2138 while Present (Priv_Elmt) loop
2139 Priv := Node (Priv_Elmt);
2141 -- Before the exchange, verify that the presence of the Full_View
2142 -- field. This field will be empty if the entity has already been
2143 -- installed due to a previous call.
2145 if Present (Full_View (Priv)) and then Is_Visible_Dependent (Priv)
2146 then
2147 if Is_Private_Type (Priv) then
2148 Deps := Private_Dependents (Priv);
2149 Is_Priv := True;
2150 else
2151 Is_Priv := False;
2152 end if;
2154 -- For each subtype that is swapped, we also swap the reference
2155 -- to it in Private_Dependents, to allow access to it when we
2156 -- swap them out in End_Package_Scope.
2158 Replace_Elmt (Priv_Elmt, Full_View (Priv));
2159 Exchange_Declarations (Priv);
2160 Set_Is_Immediately_Visible
2161 (Priv, In_Open_Scopes (Scope (Priv)));
2162 Set_Is_Potentially_Use_Visible
2163 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
2165 -- Within a child unit, recurse, except in generic child unit,
2166 -- which (unfortunately) handle private_dependents separately.
2168 if Is_Priv
2169 and then Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
2170 and then not Is_Empty_Elmt_List (Deps)
2171 and then not Inside_A_Generic
2172 then
2173 Swap_Private_Dependents (Deps);
2174 end if;
2175 end if;
2177 Next_Elmt (Priv_Elmt);
2178 end loop;
2179 end Swap_Private_Dependents;
2181 -- Start of processing for Install_Private_Declarations
2183 begin
2184 -- First exchange declarations for private types, so that the full
2185 -- declaration is visible. For each private type, we check its
2186 -- Private_Dependents list and also exchange any subtypes of or derived
2187 -- types from it. Finally, if this is a Taft amendment type, the
2188 -- incomplete declaration is irrelevant, and we want to link the
2189 -- eventual full declaration with the original private one so we
2190 -- also skip the exchange.
2192 Id := First_Entity (P);
2193 while Present (Id) and then Id /= First_Private_Entity (P) loop
2194 if Is_Private_Base_Type (Id)
2195 and then Present (Full_View (Id))
2196 and then Comes_From_Source (Full_View (Id))
2197 and then Scope (Full_View (Id)) = Scope (Id)
2198 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
2199 then
2200 -- If there is a use-type clause on the private type, set the full
2201 -- view accordingly.
2203 Set_In_Use (Full_View (Id), In_Use (Id));
2204 Full := Full_View (Id);
2206 if Is_Private_Base_Type (Full)
2207 and then Has_Private_Declaration (Full)
2208 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
2209 and then In_Open_Scopes (Scope (Etype (Full)))
2210 and then In_Package_Body (Current_Scope)
2211 and then not Is_Private_Type (Etype (Full))
2212 then
2213 -- This is the completion of a private type by a derivation
2214 -- from another private type which is not private anymore. This
2215 -- can only happen in a package nested within a child package,
2216 -- when the parent type is defined in the parent unit. At this
2217 -- point the current type is not private either, and we have
2218 -- to install the underlying full view, which is now visible.
2219 -- Save the current full view as well, so that all views can be
2220 -- restored on exit. It may seem that after compiling the child
2221 -- body there are not environments to restore, but the back-end
2222 -- expects those links to be valid, and freeze nodes depend on
2223 -- them.
2225 if No (Full_View (Full))
2226 and then Present (Underlying_Full_View (Full))
2227 then
2228 Set_Full_View (Id, Underlying_Full_View (Full));
2229 Set_Underlying_Full_View (Id, Full);
2231 Set_Underlying_Full_View (Full, Empty);
2232 Set_Is_Frozen (Full_View (Id));
2233 end if;
2234 end if;
2236 Priv_Deps := Private_Dependents (Id);
2237 Exchange_Declarations (Id);
2238 Set_Is_Immediately_Visible (Id);
2239 Swap_Private_Dependents (Priv_Deps);
2240 end if;
2242 Next_Entity (Id);
2243 end loop;
2245 -- Next make other declarations in the private part visible as well
2247 Id := First_Private_Entity (P);
2248 while Present (Id) loop
2249 Install_Package_Entity (Id);
2250 Set_Is_Hidden (Id, False);
2251 Next_Entity (Id);
2252 end loop;
2254 -- Indicate that the private part is currently visible, so it can be
2255 -- properly reset on exit.
2257 Set_In_Private_Part (P);
2258 end Install_Private_Declarations;
2260 ----------------------------------
2261 -- Install_Visible_Declarations --
2262 ----------------------------------
2264 procedure Install_Visible_Declarations (P : Entity_Id) is
2265 Id : Entity_Id;
2266 Last_Entity : Entity_Id;
2268 begin
2269 pragma Assert
2270 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
2272 if Is_Package_Or_Generic_Package (P) then
2273 Last_Entity := First_Private_Entity (P);
2274 else
2275 Last_Entity := Empty;
2276 end if;
2278 Id := First_Entity (P);
2279 while Present (Id) and then Id /= Last_Entity loop
2280 Install_Package_Entity (Id);
2281 Next_Entity (Id);
2282 end loop;
2283 end Install_Visible_Declarations;
2285 --------------------------
2286 -- Is_Private_Base_Type --
2287 --------------------------
2289 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
2290 begin
2291 return Ekind (E) = E_Private_Type
2292 or else Ekind (E) = E_Limited_Private_Type
2293 or else Ekind (E) = E_Record_Type_With_Private;
2294 end Is_Private_Base_Type;
2296 --------------------------
2297 -- Is_Visible_Dependent --
2298 --------------------------
2300 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
2302 S : constant Entity_Id := Scope (Dep);
2304 begin
2305 -- Renamings created for actual types have the visibility of the actual
2307 if Ekind (S) = E_Package
2308 and then Is_Generic_Instance (S)
2309 and then (Is_Generic_Actual_Type (Dep)
2310 or else Is_Generic_Actual_Type (Full_View (Dep)))
2311 then
2312 return True;
2314 elsif not (Is_Derived_Type (Dep))
2315 and then Is_Derived_Type (Full_View (Dep))
2316 then
2317 -- When instantiating a package body, the scope stack is empty, so
2318 -- check instead whether the dependent type is defined in the same
2319 -- scope as the instance itself.
2321 return In_Open_Scopes (S)
2322 or else (Is_Generic_Instance (Current_Scope)
2323 and then Scope (Dep) = Scope (Current_Scope));
2324 else
2325 return True;
2326 end if;
2327 end Is_Visible_Dependent;
2329 ----------------------------
2330 -- May_Need_Implicit_Body --
2331 ----------------------------
2333 procedure May_Need_Implicit_Body (E : Entity_Id) is
2334 P : constant Node_Id := Unit_Declaration_Node (E);
2335 S : constant Node_Id := Parent (P);
2336 B : Node_Id;
2337 Decls : List_Id;
2339 begin
2340 if not Has_Completion (E)
2341 and then Nkind (P) = N_Package_Declaration
2342 and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
2343 then
2344 B :=
2345 Make_Package_Body (Sloc (E),
2346 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
2347 Chars => Chars (E)),
2348 Declarations => New_List);
2350 if Nkind (S) = N_Package_Specification then
2351 if Present (Private_Declarations (S)) then
2352 Decls := Private_Declarations (S);
2353 else
2354 Decls := Visible_Declarations (S);
2355 end if;
2356 else
2357 Decls := Declarations (S);
2358 end if;
2360 Append (B, Decls);
2361 Analyze (B);
2362 end if;
2363 end May_Need_Implicit_Body;
2365 ----------------------
2366 -- New_Private_Type --
2367 ----------------------
2369 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
2370 begin
2371 -- For other than Ada 2012, enter the name in the current scope
2373 if Ada_Version < Ada_2012 then
2374 Enter_Name (Id);
2376 -- Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2377 -- there may be an incomplete previous view.
2379 else
2380 declare
2381 Prev : Entity_Id;
2382 begin
2383 Prev := Find_Type_Name (N);
2384 pragma Assert (Prev = Id
2385 or else (Ekind (Prev) = E_Incomplete_Type
2386 and then Present (Full_View (Prev))
2387 and then Full_View (Prev) = Id));
2388 end;
2389 end if;
2391 if Limited_Present (Def) then
2392 Set_Ekind (Id, E_Limited_Private_Type);
2393 else
2394 Set_Ekind (Id, E_Private_Type);
2395 end if;
2397 Set_Etype (Id, Id);
2398 Set_Has_Delayed_Freeze (Id);
2399 Set_Is_First_Subtype (Id);
2400 Init_Size_Align (Id);
2402 Set_Is_Constrained (Id,
2403 No (Discriminant_Specifications (N))
2404 and then not Unknown_Discriminants_Present (N));
2406 -- Set tagged flag before processing discriminants, to catch illegal
2407 -- usage.
2409 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
2411 Set_Discriminant_Constraint (Id, No_Elist);
2412 Set_Stored_Constraint (Id, No_Elist);
2414 if Present (Discriminant_Specifications (N)) then
2415 Push_Scope (Id);
2416 Process_Discriminants (N);
2417 End_Scope;
2419 elsif Unknown_Discriminants_Present (N) then
2420 Set_Has_Unknown_Discriminants (Id);
2421 end if;
2423 Set_Private_Dependents (Id, New_Elmt_List);
2425 if Tagged_Present (Def) then
2426 Set_Ekind (Id, E_Record_Type_With_Private);
2427 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2428 Set_Is_Abstract_Type (Id, Abstract_Present (Def));
2429 Set_Is_Limited_Record (Id, Limited_Present (Def));
2430 Set_Has_Delayed_Freeze (Id, True);
2432 -- Create a class-wide type with the same attributes
2434 Make_Class_Wide_Type (Id);
2436 elsif Abstract_Present (Def) then
2437 Error_Msg_N ("only a tagged type can be abstract", N);
2438 end if;
2439 end New_Private_Type;
2441 ---------------------------------
2442 -- Requires_Completion_In_Body --
2443 ---------------------------------
2445 function Requires_Completion_In_Body
2446 (Id : Entity_Id;
2447 Pack_Id : Entity_Id) return Boolean
2449 begin
2450 -- Always ignore child units. Child units get added to the entity list
2451 -- of a parent unit, but are not original entities of the parent, and
2452 -- so do not affect whether the parent needs a body.
2454 if Is_Child_Unit (Id) then
2455 return False;
2457 -- Ignore formal packages and their renamings
2459 elsif Ekind (Id) = E_Package
2460 and then Nkind (Original_Node (Unit_Declaration_Node (Id))) =
2461 N_Formal_Package_Declaration
2462 then
2463 return False;
2465 -- A Ghost entity declared in a non-Ghost package does not force the
2466 -- need for a body (SPARK RM 6.9(11)).
2468 elsif not Is_Ghost_Entity (Pack_Id) and then Is_Ghost_Entity (Id) then
2469 return False;
2471 -- Otherwise test to see if entity requires a completion. Note that
2472 -- subprogram entities whose declaration does not come from source are
2473 -- ignored here on the basis that we assume the expander will provide an
2474 -- implicit completion at some point.
2476 elsif (Is_Overloadable (Id)
2477 and then Ekind (Id) /= E_Enumeration_Literal
2478 and then Ekind (Id) /= E_Operator
2479 and then not Is_Abstract_Subprogram (Id)
2480 and then not Has_Completion (Id)
2481 and then Comes_From_Source (Parent (Id)))
2483 or else
2484 (Ekind (Id) = E_Package
2485 and then Id /= Pack_Id
2486 and then not Has_Completion (Id)
2487 and then Unit_Requires_Body (Id))
2489 or else
2490 (Ekind (Id) = E_Incomplete_Type
2491 and then No (Full_View (Id))
2492 and then not Is_Generic_Type (Id))
2494 or else
2495 (Ekind_In (Id, E_Task_Type, E_Protected_Type)
2496 and then not Has_Completion (Id))
2498 or else
2499 (Ekind (Id) = E_Generic_Package
2500 and then Id /= Pack_Id
2501 and then not Has_Completion (Id)
2502 and then Unit_Requires_Body (Id))
2504 or else
2505 (Is_Generic_Subprogram (Id)
2506 and then not Has_Completion (Id))
2508 then
2509 return True;
2511 -- Otherwise the entity does not require completion in a package body
2513 else
2514 return False;
2515 end if;
2516 end Requires_Completion_In_Body;
2518 ----------------------------
2519 -- Uninstall_Declarations --
2520 ----------------------------
2522 procedure Uninstall_Declarations (P : Entity_Id) is
2523 Decl : constant Node_Id := Unit_Declaration_Node (P);
2524 Id : Entity_Id;
2525 Full : Entity_Id;
2526 Priv_Elmt : Elmt_Id;
2527 Priv_Sub : Entity_Id;
2529 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id);
2530 -- Copy to the private declaration the attributes of the full view that
2531 -- need to be available for the partial view also.
2533 function Type_In_Use (T : Entity_Id) return Boolean;
2534 -- Check whether type or base type appear in an active use_type clause
2536 ------------------------------
2537 -- Preserve_Full_Attributes --
2538 ------------------------------
2540 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id) is
2541 Priv_Is_Base_Type : constant Boolean := Is_Base_Type (Priv);
2543 begin
2544 Set_Size_Info (Priv, (Full));
2545 Set_RM_Size (Priv, RM_Size (Full));
2546 Set_Size_Known_At_Compile_Time
2547 (Priv, Size_Known_At_Compile_Time (Full));
2548 Set_Is_Volatile (Priv, Is_Volatile (Full));
2549 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
2550 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full));
2551 Set_Is_Ada_2012_Only (Priv, Is_Ada_2012_Only (Full));
2552 Set_Has_Pragma_Unmodified (Priv, Has_Pragma_Unmodified (Full));
2553 Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced (Full));
2554 Set_Has_Pragma_Unreferenced_Objects
2555 (Priv, Has_Pragma_Unreferenced_Objects
2556 (Full));
2557 if Is_Unchecked_Union (Full) then
2558 Set_Is_Unchecked_Union (Base_Type (Priv));
2559 end if;
2560 -- Why is atomic not copied here ???
2562 if Referenced (Full) then
2563 Set_Referenced (Priv);
2564 end if;
2566 if Priv_Is_Base_Type then
2567 Set_Is_Controlled (Priv, Is_Controlled (Base_Type (Full)));
2568 Set_Finalize_Storage_Only
2569 (Priv, Finalize_Storage_Only
2570 (Base_Type (Full)));
2571 Set_Has_Task (Priv, Has_Task (Base_Type (Full)));
2572 Set_Has_Protected (Priv, Has_Protected (Base_Type (Full)));
2573 Set_Has_Controlled_Component
2574 (Priv, Has_Controlled_Component
2575 (Base_Type (Full)));
2576 end if;
2578 Set_Freeze_Node (Priv, Freeze_Node (Full));
2580 -- Propagate information of type invariants, which may be specified
2581 -- for the full view.
2583 if Has_Invariants (Full) and not Has_Invariants (Priv) then
2584 Set_Has_Invariants (Priv);
2585 Set_Subprograms_For_Type (Priv, Subprograms_For_Type (Full));
2586 end if;
2588 if Is_Tagged_Type (Priv)
2589 and then Is_Tagged_Type (Full)
2590 and then not Error_Posted (Full)
2591 then
2592 if Is_Tagged_Type (Priv) then
2594 -- If the type is tagged, the tag itself must be available on
2595 -- the partial view, for expansion purposes.
2597 Set_First_Entity (Priv, First_Entity (Full));
2599 -- If there are discriminants in the partial view, these remain
2600 -- visible. Otherwise only the tag itself is visible, and there
2601 -- are no nameable components in the partial view.
2603 if No (Last_Entity (Priv)) then
2604 Set_Last_Entity (Priv, First_Entity (Priv));
2605 end if;
2606 end if;
2608 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2610 if Has_Discriminants (Full) then
2611 Set_Discriminant_Constraint (Priv,
2612 Discriminant_Constraint (Full));
2613 end if;
2614 end if;
2615 end Preserve_Full_Attributes;
2617 -----------------
2618 -- Type_In_Use --
2619 -----------------
2621 function Type_In_Use (T : Entity_Id) return Boolean is
2622 begin
2623 return Scope (Base_Type (T)) = P
2624 and then (In_Use (T) or else In_Use (Base_Type (T)));
2625 end Type_In_Use;
2627 -- Start of processing for Uninstall_Declarations
2629 begin
2630 Id := First_Entity (P);
2631 while Present (Id) and then Id /= First_Private_Entity (P) loop
2632 if Debug_Flag_E then
2633 Write_Str ("unlinking visible entity ");
2634 Write_Int (Int (Id));
2635 Write_Eol;
2636 end if;
2638 -- On exit from the package scope, we must preserve the visibility
2639 -- established by use clauses in the current scope. Two cases:
2641 -- a) If the entity is an operator, it may be a primitive operator of
2642 -- a type for which there is a visible use-type clause.
2644 -- b) for other entities, their use-visibility is determined by a
2645 -- visible use clause for the package itself. For a generic instance,
2646 -- the instantiation of the formals appears in the visible part,
2647 -- but the formals are private and remain so.
2649 if Ekind (Id) = E_Function
2650 and then Is_Operator_Symbol_Name (Chars (Id))
2651 and then not Is_Hidden (Id)
2652 and then not Error_Posted (Id)
2653 then
2654 Set_Is_Potentially_Use_Visible (Id,
2655 In_Use (P)
2656 or else Type_In_Use (Etype (Id))
2657 or else Type_In_Use (Etype (First_Formal (Id)))
2658 or else (Present (Next_Formal (First_Formal (Id)))
2659 and then
2660 Type_In_Use
2661 (Etype (Next_Formal (First_Formal (Id))))));
2662 else
2663 if In_Use (P) and then not Is_Hidden (Id) then
2665 -- A child unit of a use-visible package remains use-visible
2666 -- only if it is itself a visible child unit. Otherwise it
2667 -- would remain visible in other contexts where P is use-
2668 -- visible, because once compiled it stays in the entity list
2669 -- of its parent unit.
2671 if Is_Child_Unit (Id) then
2672 Set_Is_Potentially_Use_Visible
2673 (Id, Is_Visible_Lib_Unit (Id));
2674 else
2675 Set_Is_Potentially_Use_Visible (Id);
2676 end if;
2678 else
2679 Set_Is_Potentially_Use_Visible (Id, False);
2680 end if;
2681 end if;
2683 -- Local entities are not immediately visible outside of the package
2685 Set_Is_Immediately_Visible (Id, False);
2687 -- If this is a private type with a full view (for example a local
2688 -- subtype of a private type declared elsewhere), ensure that the
2689 -- full view is also removed from visibility: it may be exposed when
2690 -- swapping views in an instantiation.
2692 if Is_Type (Id) and then Present (Full_View (Id)) then
2693 Set_Is_Immediately_Visible (Full_View (Id), False);
2694 end if;
2696 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2697 Check_Abstract_Overriding (Id);
2698 Check_Conventions (Id);
2699 end if;
2701 if Ekind_In (Id, E_Private_Type, E_Limited_Private_Type)
2702 and then No (Full_View (Id))
2703 and then not Is_Generic_Type (Id)
2704 and then not Is_Derived_Type (Id)
2705 then
2706 Error_Msg_N ("missing full declaration for private type&", Id);
2708 elsif Ekind (Id) = E_Record_Type_With_Private
2709 and then not Is_Generic_Type (Id)
2710 and then No (Full_View (Id))
2711 then
2712 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
2713 Error_Msg_N ("missing full declaration for private type&", Id);
2714 else
2715 Error_Msg_N
2716 ("missing full declaration for private extension", Id);
2717 end if;
2719 -- Case of constant, check for deferred constant declaration with
2720 -- no full view. Likely just a matter of a missing expression, or
2721 -- accidental use of the keyword constant.
2723 elsif Ekind (Id) = E_Constant
2725 -- OK if constant value present
2727 and then No (Constant_Value (Id))
2729 -- OK if full view present
2731 and then No (Full_View (Id))
2733 -- OK if imported, since that provides the completion
2735 and then not Is_Imported (Id)
2737 -- OK if object declaration replaced by renaming declaration as
2738 -- a result of OK_To_Rename processing (e.g. for concatenation)
2740 and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration
2742 -- OK if object declaration with the No_Initialization flag set
2744 and then not (Nkind (Parent (Id)) = N_Object_Declaration
2745 and then No_Initialization (Parent (Id)))
2746 then
2747 -- If no private declaration is present, we assume the user did
2748 -- not intend a deferred constant declaration and the problem
2749 -- is simply that the initializing expression is missing.
2751 if not Has_Private_Declaration (Etype (Id)) then
2753 -- We assume that the user did not intend a deferred constant
2754 -- declaration, and the expression is just missing.
2756 Error_Msg_N
2757 ("constant declaration requires initialization expression",
2758 Parent (Id));
2760 if Is_Limited_Type (Etype (Id)) then
2761 Error_Msg_N
2762 ("\if variable intended, remove CONSTANT from declaration",
2763 Parent (Id));
2764 end if;
2766 -- Otherwise if a private declaration is present, then we are
2767 -- missing the full declaration for the deferred constant.
2769 else
2770 Error_Msg_N
2771 ("missing full declaration for deferred constant (RM 7.4)",
2772 Id);
2774 if Is_Limited_Type (Etype (Id)) then
2775 Error_Msg_N
2776 ("\if variable intended, remove CONSTANT from declaration",
2777 Parent (Id));
2778 end if;
2779 end if;
2780 end if;
2782 Next_Entity (Id);
2783 end loop;
2785 -- If the specification was installed as the parent of a public child
2786 -- unit, the private declarations were not installed, and there is
2787 -- nothing to do.
2789 if not In_Private_Part (P) then
2790 return;
2791 else
2792 Set_In_Private_Part (P, False);
2793 end if;
2795 -- Make private entities invisible and exchange full and private
2796 -- declarations for private types. Id is now the first private entity
2797 -- in the package.
2799 while Present (Id) loop
2800 if Debug_Flag_E then
2801 Write_Str ("unlinking private entity ");
2802 Write_Int (Int (Id));
2803 Write_Eol;
2804 end if;
2806 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2807 Check_Abstract_Overriding (Id);
2808 Check_Conventions (Id);
2809 end if;
2811 Set_Is_Immediately_Visible (Id, False);
2813 if Is_Private_Base_Type (Id) and then Present (Full_View (Id)) then
2814 Full := Full_View (Id);
2816 -- If the partial view is not declared in the visible part of the
2817 -- package (as is the case when it is a type derived from some
2818 -- other private type in the private part of the current package),
2819 -- no exchange takes place.
2821 if No (Parent (Id))
2822 or else List_Containing (Parent (Id)) /=
2823 Visible_Declarations (Specification (Decl))
2824 then
2825 goto Next_Id;
2826 end if;
2828 -- The entry in the private part points to the full declaration,
2829 -- which is currently visible. Exchange them so only the private
2830 -- type declaration remains accessible, and link private and full
2831 -- declaration in the opposite direction. Before the actual
2832 -- exchange, we copy back attributes of the full view that must
2833 -- be available to the partial view too.
2835 Preserve_Full_Attributes (Id, Full);
2837 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
2839 -- The following test may be redundant, as this is already
2840 -- diagnosed in sem_ch3. ???
2842 if Is_Indefinite_Subtype (Full)
2843 and then not Is_Indefinite_Subtype (Id)
2844 then
2845 Error_Msg_Sloc := Sloc (Parent (Id));
2846 Error_Msg_NE
2847 ("full view of& not compatible with declaration#", Full, Id);
2848 end if;
2850 -- Swap out the subtypes and derived types of Id that
2851 -- were compiled in this scope, or installed previously
2852 -- by Install_Private_Declarations.
2854 -- Before we do the swap, we verify the presence of the Full_View
2855 -- field which may be empty due to a swap by a previous call to
2856 -- End_Package_Scope (e.g. from the freezing mechanism).
2858 Priv_Elmt := First_Elmt (Private_Dependents (Id));
2859 while Present (Priv_Elmt) loop
2860 Priv_Sub := Node (Priv_Elmt);
2862 if Present (Full_View (Priv_Sub)) then
2863 if Scope (Priv_Sub) = P
2864 or else not In_Open_Scopes (Scope (Priv_Sub))
2865 then
2866 Set_Is_Immediately_Visible (Priv_Sub, False);
2867 end if;
2869 if Is_Visible_Dependent (Priv_Sub) then
2870 Preserve_Full_Attributes
2871 (Priv_Sub, Full_View (Priv_Sub));
2872 Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub));
2873 Exchange_Declarations (Priv_Sub);
2874 end if;
2875 end if;
2877 Next_Elmt (Priv_Elmt);
2878 end loop;
2880 -- Now restore the type itself to its private view
2882 Exchange_Declarations (Id);
2884 -- If we have installed an underlying full view for a type derived
2885 -- from a private type in a child unit, restore the proper views
2886 -- of private and full view. See corresponding code in
2887 -- Install_Private_Declarations.
2889 -- After the exchange, Full denotes the private type in the
2890 -- visible part of the package.
2892 if Is_Private_Base_Type (Full)
2893 and then Present (Full_View (Full))
2894 and then Present (Underlying_Full_View (Full))
2895 and then In_Package_Body (Current_Scope)
2896 then
2897 Set_Full_View (Full, Underlying_Full_View (Full));
2898 Set_Underlying_Full_View (Full, Empty);
2899 end if;
2901 elsif Ekind (Id) = E_Incomplete_Type
2902 and then Comes_From_Source (Id)
2903 and then No (Full_View (Id))
2904 then
2905 -- Mark Taft amendment types. Verify that there are no primitive
2906 -- operations declared for the type (3.10.1(9)).
2908 Set_Has_Completion_In_Body (Id);
2910 declare
2911 Elmt : Elmt_Id;
2912 Subp : Entity_Id;
2914 begin
2915 Elmt := First_Elmt (Private_Dependents (Id));
2916 while Present (Elmt) loop
2917 Subp := Node (Elmt);
2919 -- Is_Primitive is tested because there can be cases where
2920 -- nonprimitive subprograms (in nested packages) are added
2921 -- to the Private_Dependents list.
2923 if Is_Overloadable (Subp) and then Is_Primitive (Subp) then
2924 Error_Msg_NE
2925 ("type& must be completed in the private part",
2926 Parent (Subp), Id);
2928 -- The result type of an access-to-function type cannot be a
2929 -- Taft-amendment type, unless the version is Ada 2012 or
2930 -- later (see AI05-151).
2932 elsif Ada_Version < Ada_2012
2933 and then Ekind (Subp) = E_Subprogram_Type
2934 then
2935 if Etype (Subp) = Id
2936 or else
2937 (Is_Class_Wide_Type (Etype (Subp))
2938 and then Etype (Etype (Subp)) = Id)
2939 then
2940 Error_Msg_NE
2941 ("type& must be completed in the private part",
2942 Associated_Node_For_Itype (Subp), Id);
2943 end if;
2944 end if;
2946 Next_Elmt (Elmt);
2947 end loop;
2948 end;
2950 elsif not Is_Child_Unit (Id)
2951 and then (not Is_Private_Type (Id) or else No (Full_View (Id)))
2952 then
2953 Set_Is_Hidden (Id);
2954 Set_Is_Potentially_Use_Visible (Id, False);
2955 end if;
2957 <<Next_Id>>
2958 Next_Entity (Id);
2959 end loop;
2960 end Uninstall_Declarations;
2962 ------------------------
2963 -- Unit_Requires_Body --
2964 ------------------------
2966 function Unit_Requires_Body
2967 (Pack_Id : Entity_Id;
2968 Ignore_Abstract_State : Boolean := False) return Boolean
2970 E : Entity_Id;
2972 begin
2973 -- Imported entity never requires body. Right now, only subprograms can
2974 -- be imported, but perhaps in the future we will allow import of
2975 -- packages.
2977 if Is_Imported (Pack_Id) then
2978 return False;
2980 -- Body required if library package with pragma Elaborate_Body
2982 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
2983 return True;
2985 -- Body required if subprogram
2987 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
2988 return True;
2990 -- Treat a block as requiring a body
2992 elsif Ekind (Pack_Id) = E_Block then
2993 return True;
2995 elsif Ekind (Pack_Id) = E_Package
2996 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
2997 and then Present (Generic_Parent (Parent (Pack_Id)))
2998 then
2999 declare
3000 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3001 begin
3002 if Has_Pragma_Elaborate_Body (G_P) then
3003 return True;
3004 end if;
3005 end;
3007 -- A [generic] package that introduces at least one non-null abstract
3008 -- state requires completion. However, there is a separate rule that
3009 -- requires that such a package have a reason other than this for a
3010 -- body being required (if necessary a pragma Elaborate_Body must be
3011 -- provided). If Ignore_Abstract_State is True, we don't do this check
3012 -- (so we can use Unit_Requires_Body to check for some other reason).
3014 elsif Ekind_In (Pack_Id, E_Generic_Package, E_Package)
3015 and then not Ignore_Abstract_State
3016 and then Present (Abstract_States (Pack_Id))
3017 and then not Is_Null_State
3018 (Node (First_Elmt (Abstract_States (Pack_Id))))
3019 then
3020 return True;
3021 end if;
3023 -- Otherwise search entity chain for entity requiring completion
3025 E := First_Entity (Pack_Id);
3026 while Present (E) loop
3027 if Requires_Completion_In_Body (E, Pack_Id) then
3028 return True;
3029 end if;
3031 Next_Entity (E);
3032 end loop;
3034 return False;
3035 end Unit_Requires_Body;
3037 -----------------------------
3038 -- Unit_Requires_Body_Info --
3039 -----------------------------
3041 procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id) is
3042 E : Entity_Id;
3044 begin
3045 -- An imported entity never requires body. Right now, only subprograms
3046 -- can be imported, but perhaps in the future we will allow import of
3047 -- packages.
3049 if Is_Imported (Pack_Id) then
3050 return;
3052 -- Body required if library package with pragma Elaborate_Body
3054 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3055 Error_Msg_N ("info: & requires body (Elaborate_Body)?Y?", Pack_Id);
3057 -- Body required if subprogram
3059 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3060 Error_Msg_N ("info: & requires body (subprogram case)?Y?", Pack_Id);
3062 -- Body required if generic parent has Elaborate_Body
3064 elsif Ekind (Pack_Id) = E_Package
3065 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3066 and then Present (Generic_Parent (Parent (Pack_Id)))
3067 then
3068 declare
3069 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3070 begin
3071 if Has_Pragma_Elaborate_Body (G_P) then
3072 Error_Msg_N
3073 ("info: & requires body (generic parent Elaborate_Body)?Y?",
3074 Pack_Id);
3075 end if;
3076 end;
3078 -- A [generic] package that introduces at least one non-null abstract
3079 -- state requires completion. However, there is a separate rule that
3080 -- requires that such a package have a reason other than this for a
3081 -- body being required (if necessary a pragma Elaborate_Body must be
3082 -- provided). If Ignore_Abstract_State is True, we don't do this check
3083 -- (so we can use Unit_Requires_Body to check for some other reason).
3085 elsif Ekind_In (Pack_Id, E_Generic_Package, E_Package)
3086 and then Present (Abstract_States (Pack_Id))
3087 and then not Is_Null_State
3088 (Node (First_Elmt (Abstract_States (Pack_Id))))
3089 then
3090 Error_Msg_N
3091 ("info: & requires body (non-null abstract state aspect)?Y?",
3092 Pack_Id);
3093 end if;
3095 -- Otherwise search entity chain for entity requiring completion
3097 E := First_Entity (Pack_Id);
3098 while Present (E) loop
3099 if Requires_Completion_In_Body (E, Pack_Id) then
3100 Error_Msg_Node_2 := E;
3101 Error_Msg_NE
3102 ("info: & requires body (& requires completion)?Y?", E, Pack_Id);
3103 end if;
3105 Next_Entity (E);
3106 end loop;
3107 end Unit_Requires_Body_Info;
3108 end Sem_Ch7;