2016-09-19 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ada / sem_ch7.adb
blob4a3b2de04297a81405008c4a4957e79c801e8cb1
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-2016, 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 Contracts; use Contracts;
34 with Debug; use Debug;
35 with Einfo; use Einfo;
36 with Elists; use Elists;
37 with Errout; use Errout;
38 with Exp_Ch7; use Exp_Ch7;
39 with Exp_Disp; use Exp_Disp;
40 with Exp_Dist; use Exp_Dist;
41 with Exp_Dbug; use Exp_Dbug;
42 with Freeze; use Freeze;
43 with Ghost; use Ghost;
44 with Lib; use Lib;
45 with Lib.Xref; use Lib.Xref;
46 with Namet; use Namet;
47 with Nmake; use Nmake;
48 with Nlists; use Nlists;
49 with Opt; use Opt;
50 with Output; use Output;
51 with Restrict; use Restrict;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch3; use Sem_Ch3;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch10; use Sem_Ch10;
60 with Sem_Ch12; use Sem_Ch12;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Disp; use Sem_Disp;
63 with Sem_Eval; use Sem_Eval;
64 with Sem_Prag; use Sem_Prag;
65 with Sem_Util; use Sem_Util;
66 with Sem_Warn; use Sem_Warn;
67 with Snames; use Snames;
68 with Stand; use Stand;
69 with Sinfo; use Sinfo;
70 with Sinput; use Sinput;
71 with Style;
72 with Uintp; use Uintp;
74 package body Sem_Ch7 is
76 -----------------------------------
77 -- Handling private declarations --
78 -----------------------------------
80 -- The principle that each entity has a single defining occurrence clashes
81 -- with the presence of two separate definitions for private types: the
82 -- first is the private type declaration, and the second is the full type
83 -- declaration. It is important that all references to the type point to
84 -- the same defining occurrence, namely the first one. To enforce the two
85 -- separate views of the entity, the corresponding information is swapped
86 -- between the two declarations. Outside of the package, the defining
87 -- occurrence only contains the private declaration information, while in
88 -- the private part and the body of the package the defining occurrence
89 -- contains the full declaration. To simplify the swap, the defining
90 -- occurrence that currently holds the private declaration points to the
91 -- full declaration. During semantic processing the defining occurrence
92 -- also points to a list of private dependents, that is to say access types
93 -- or composite types whose designated types or component types are
94 -- subtypes or derived types of the private type in question. After the
95 -- full declaration has been seen, the private dependents are updated to
96 -- indicate that they have full definitions.
98 -----------------------
99 -- Local Subprograms --
100 -----------------------
102 procedure Analyze_Package_Body_Helper (N : Node_Id);
103 -- Does all the real work of Analyze_Package_Body
105 procedure Check_Anonymous_Access_Types
106 (Spec_Id : Entity_Id;
107 P_Body : Node_Id);
108 -- If the spec of a package has a limited_with_clause, it may declare
109 -- anonymous access types whose designated type is a limited view, such an
110 -- anonymous access return type for a function. This access type cannot be
111 -- elaborated in the spec itself, but it may need an itype reference if it
112 -- is used within a nested scope. In that case the itype reference is
113 -- created at the beginning of the corresponding package body and inserted
114 -- before other body declarations.
116 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id);
117 -- Called upon entering the private part of a public child package and the
118 -- body of a nested package, to potentially declare certain inherited
119 -- subprograms that were inherited by types in the visible part, but whose
120 -- declaration was deferred because the parent operation was private and
121 -- not visible at that point. These subprograms are located by traversing
122 -- the visible part declarations looking for non-private type extensions
123 -- and then examining each of the primitive operations of such types to
124 -- find those that were inherited but declared with a special internal
125 -- name. Each such operation is now declared as an operation with a normal
126 -- name (using the name of the parent operation) and replaces the previous
127 -- implicit operation in the primitive operations list of the type. If the
128 -- inherited private operation has been overridden, then it's replaced by
129 -- the overriding operation.
131 procedure Install_Package_Entity (Id : Entity_Id);
132 -- Supporting procedure for Install_{Visible,Private}_Declarations. Places
133 -- one entity on its visibility chain, and recurses on the visible part if
134 -- the entity is an inner package.
136 function Is_Private_Base_Type (E : Entity_Id) return Boolean;
137 -- True for a private type that is not a subtype
139 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean;
140 -- If the private dependent is a private type whose full view is derived
141 -- from the parent type, its full properties are revealed only if we are in
142 -- the immediate scope of the private dependent. Should this predicate be
143 -- tightened further???
145 function Requires_Completion_In_Body
146 (Id : Entity_Id;
147 Pack_Id : Entity_Id;
148 Do_Abstract_States : Boolean := False) return Boolean;
149 -- Subsidiary to routines Unit_Requires_Body and Unit_Requires_Body_Info.
150 -- Determine whether entity Id declared in package spec Pack_Id requires
151 -- completion in a package body. Flag Do_Abstract_Stats should be set when
152 -- abstract states are to be considered in the completion test.
154 procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id);
155 -- Outputs info messages showing why package Pack_Id requires a body. The
156 -- caller has checked that the switch requesting this information is set,
157 -- and that the package does indeed require a body.
159 --------------------------
160 -- Analyze_Package_Body --
161 --------------------------
163 procedure Analyze_Package_Body (N : Node_Id) is
164 Loc : constant Source_Ptr := Sloc (N);
166 begin
167 if Debug_Flag_C then
168 Write_Str ("==> package body ");
169 Write_Name (Chars (Defining_Entity (N)));
170 Write_Str (" from ");
171 Write_Location (Loc);
172 Write_Eol;
173 Indent;
174 end if;
176 -- The real work is split out into the helper, so it can do "return;"
177 -- without skipping the debug output.
179 Analyze_Package_Body_Helper (N);
181 if Debug_Flag_C then
182 Outdent;
183 Write_Str ("<== package body ");
184 Write_Name (Chars (Defining_Entity (N)));
185 Write_Str (" from ");
186 Write_Location (Loc);
187 Write_Eol;
188 end if;
189 end Analyze_Package_Body;
191 ---------------------------------
192 -- Analyze_Package_Body_Helper --
193 ---------------------------------
195 procedure Analyze_Package_Body_Helper (N : Node_Id) is
196 procedure Hide_Public_Entities (Decls : List_Id);
197 -- Attempt to hide all public entities found in declarative list Decls
198 -- by resetting their Is_Public flag to False depending on whether the
199 -- entities are not referenced by inlined or generic bodies. This kind
200 -- of processing is a conservative approximation and may still leave
201 -- certain entities externally visible.
203 procedure Install_Composite_Operations (P : Entity_Id);
204 -- Composite types declared in the current scope may depend on types
205 -- that were private at the point of declaration, and whose full view
206 -- is now in scope. Indicate that the corresponding operations on the
207 -- composite type are available.
209 --------------------------
210 -- Hide_Public_Entities --
211 --------------------------
213 procedure Hide_Public_Entities (Decls : List_Id) is
214 function Contains_Subp_Or_Const_Refs (N : Node_Id) return Boolean;
215 -- Subsidiary to routine Has_Referencer. Determine whether a node
216 -- contains a reference to a subprogram or a non-static constant.
217 -- WARNING: this is a very expensive routine as it performs a full
218 -- tree traversal.
220 function Has_Referencer
221 (Decls : List_Id;
222 Top_Level : Boolean := False) return Boolean;
223 -- A "referencer" is a construct which may reference a previous
224 -- declaration. Examine all declarations in list Decls in reverse
225 -- and determine whether once such referencer exists. All entities
226 -- in the range Last (Decls) .. Referencer are hidden from external
227 -- visibility.
229 ---------------------------------
230 -- Contains_Subp_Or_Const_Refs --
231 ---------------------------------
233 function Contains_Subp_Or_Const_Refs (N : Node_Id) return Boolean is
234 Reference_Seen : Boolean := False;
236 function Is_Subp_Or_Const_Ref
237 (N : Node_Id) return Traverse_Result;
238 -- Determine whether a node denotes a reference to a subprogram or
239 -- a non-static constant.
241 --------------------------
242 -- Is_Subp_Or_Const_Ref --
243 --------------------------
245 function Is_Subp_Or_Const_Ref
246 (N : Node_Id) return Traverse_Result
248 Val : Node_Id;
250 begin
251 -- Detect a reference of the form
252 -- Subp_Call
254 if Nkind (N) in N_Subprogram_Call
255 and then Is_Entity_Name (Name (N))
256 then
257 Reference_Seen := True;
258 return Abandon;
260 -- Detect a reference of the form
261 -- Subp'Some_Attribute
263 elsif Nkind (N) = N_Attribute_Reference
264 and then Is_Entity_Name (Prefix (N))
265 and then Present (Entity (Prefix (N)))
266 and then Is_Subprogram (Entity (Prefix (N)))
267 then
268 Reference_Seen := True;
269 return Abandon;
271 -- Detect the use of a non-static constant
273 elsif Is_Entity_Name (N)
274 and then Present (Entity (N))
275 and then Ekind (Entity (N)) = E_Constant
276 then
277 Val := Constant_Value (Entity (N));
279 if Present (Val)
280 and then not Compile_Time_Known_Value (Val)
281 then
282 Reference_Seen := True;
283 return Abandon;
284 end if;
285 end if;
287 return OK;
288 end Is_Subp_Or_Const_Ref;
290 procedure Find_Subp_Or_Const_Ref is
291 new Traverse_Proc (Is_Subp_Or_Const_Ref);
293 -- Start of processing for Contains_Subp_Or_Const_Refs
295 begin
296 Find_Subp_Or_Const_Ref (N);
298 return Reference_Seen;
299 end Contains_Subp_Or_Const_Refs;
301 --------------------
302 -- Has_Referencer --
303 --------------------
305 function Has_Referencer
306 (Decls : List_Id;
307 Top_Level : Boolean := False) return Boolean
309 Decl : Node_Id;
310 Decl_Id : Entity_Id;
311 Spec : Node_Id;
313 Has_Non_Subp_Const_Referencer : Boolean := False;
314 -- Flag set for inlined subprogram bodies that do not contain
315 -- references to other subprograms or non-static constants.
317 begin
318 if No (Decls) then
319 return False;
320 end if;
322 -- Examine all declarations in reverse order, hiding all entities
323 -- from external visibility until a referencer has been found. The
324 -- algorithm recurses into nested packages.
326 Decl := Last (Decls);
327 while Present (Decl) loop
329 -- A stub is always considered a referencer
331 if Nkind (Decl) in N_Body_Stub then
332 return True;
334 -- Package declaration
336 elsif Nkind (Decl) = N_Package_Declaration
337 and then not Has_Non_Subp_Const_Referencer
338 then
339 Spec := Specification (Decl);
341 -- Inspect the declarations of a non-generic package to try
342 -- and hide more entities from external visibility.
344 if not Is_Generic_Unit (Defining_Entity (Spec)) then
345 if Has_Referencer (Private_Declarations (Spec))
346 or else Has_Referencer (Visible_Declarations (Spec))
347 then
348 return True;
349 end if;
350 end if;
352 -- Package body
354 elsif Nkind (Decl) = N_Package_Body
355 and then Present (Corresponding_Spec (Decl))
356 then
357 Decl_Id := Corresponding_Spec (Decl);
359 -- A generic package body is a referencer. It would seem
360 -- that we only have to consider generics that can be
361 -- exported, i.e. where the corresponding spec is the
362 -- spec of the current package, but because of nested
363 -- instantiations, a fully private generic body may export
364 -- other private body entities. Furthermore, regardless of
365 -- whether there was a previous inlined subprogram, (an
366 -- instantiation of) the generic package may reference any
367 -- entity declared before it.
369 if Is_Generic_Unit (Decl_Id) then
370 return True;
372 -- Inspect the declarations of a non-generic package body to
373 -- try and hide more entities from external visibility.
375 elsif not Has_Non_Subp_Const_Referencer
376 and then Has_Referencer (Declarations (Decl))
377 then
378 return True;
379 end if;
381 -- Subprogram body
383 elsif Nkind (Decl) = N_Subprogram_Body then
384 if Present (Corresponding_Spec (Decl)) then
385 Decl_Id := Corresponding_Spec (Decl);
387 -- A generic subprogram body acts as a referencer
389 if Is_Generic_Unit (Decl_Id) then
390 return True;
391 end if;
393 -- An inlined subprogram body acts as a referencer
395 if Is_Inlined (Decl_Id)
396 or else Has_Pragma_Inline (Decl_Id)
397 then
398 -- Inspect the statements of the subprogram body
399 -- to determine whether the body references other
400 -- subprograms and/or non-static constants.
402 if Top_Level
403 and then not Contains_Subp_Or_Const_Refs (Decl)
404 then
405 Has_Non_Subp_Const_Referencer := True;
406 else
407 return True;
408 end if;
409 end if;
411 -- Otherwise this is a stand alone subprogram body
413 else
414 Decl_Id := Defining_Entity (Decl);
416 -- An inlined body acts as a referencer. Note that an
417 -- inlined subprogram remains Is_Public as gigi requires
418 -- the flag to be set.
420 -- Note that we test Has_Pragma_Inline here rather than
421 -- Is_Inlined. We are compiling this for a client, and
422 -- it is the client who will decide if actual inlining
423 -- should occur, so we need to assume that the procedure
424 -- could be inlined for the purpose of accessing global
425 -- entities.
427 if Has_Pragma_Inline (Decl_Id) then
428 if Top_Level
429 and then not Contains_Subp_Or_Const_Refs (Decl)
430 then
431 Has_Non_Subp_Const_Referencer := True;
432 else
433 return True;
434 end if;
435 else
436 Set_Is_Public (Decl_Id, False);
437 end if;
438 end if;
440 -- Exceptions, objects and renamings do not need to be public
441 -- if they are not followed by a construct which can reference
442 -- and export them. The Is_Public flag is reset on top level
443 -- entities only as anything nested is local to its context.
445 elsif Nkind_In (Decl, N_Exception_Declaration,
446 N_Object_Declaration,
447 N_Object_Renaming_Declaration,
448 N_Subprogram_Declaration,
449 N_Subprogram_Renaming_Declaration)
450 then
451 Decl_Id := Defining_Entity (Decl);
453 if Top_Level
454 and then not Is_Imported (Decl_Id)
455 and then not Is_Exported (Decl_Id)
456 and then No (Interface_Name (Decl_Id))
457 and then
458 (not Has_Non_Subp_Const_Referencer
459 or else Nkind (Decl) = N_Subprogram_Declaration)
460 then
461 Set_Is_Public (Decl_Id, False);
462 end if;
463 end if;
465 Prev (Decl);
466 end loop;
468 return Has_Non_Subp_Const_Referencer;
469 end Has_Referencer;
471 -- Local variables
473 Discard : Boolean := True;
474 pragma Unreferenced (Discard);
476 -- Start of processing for Hide_Public_Entities
478 begin
479 -- The algorithm examines the top level declarations of a package
480 -- body in reverse looking for a construct that may export entities
481 -- declared prior to it. If such a scenario is encountered, then all
482 -- entities in the range Last (Decls) .. construct are hidden from
483 -- external visibility. Consider:
485 -- package Pack is
486 -- generic
487 -- package Gen is
488 -- end Gen;
489 -- end Pack;
491 -- package body Pack is
492 -- External_Obj : ...; -- (1)
494 -- package body Gen is -- (2)
495 -- ... External_Obj ... -- (3)
496 -- end Gen;
498 -- Local_Obj : ...; -- (4)
499 -- end Pack;
501 -- In this example Local_Obj (4) must not be externally visible as
502 -- it cannot be exported by anything in Pack. The body of generic
503 -- package Gen (2) on the other hand acts as a "referencer" and may
504 -- export anything declared before it. Since the compiler does not
505 -- perform flow analysis, it is not possible to determine precisely
506 -- which entities will be exported when Gen is instantiated. In the
507 -- example above External_Obj (1) is exported at (3), but this may
508 -- not always be the case. The algorithm takes a conservative stance
509 -- and leaves entity External_Obj public.
511 Discard := Has_Referencer (Decls, Top_Level => True);
512 end Hide_Public_Entities;
514 ----------------------------------
515 -- Install_Composite_Operations --
516 ----------------------------------
518 procedure Install_Composite_Operations (P : Entity_Id) is
519 Id : Entity_Id;
521 begin
522 Id := First_Entity (P);
523 while Present (Id) loop
524 if Is_Type (Id)
525 and then (Is_Limited_Composite (Id)
526 or else Is_Private_Composite (Id))
527 and then No (Private_Component (Id))
528 then
529 Set_Is_Limited_Composite (Id, False);
530 Set_Is_Private_Composite (Id, False);
531 end if;
533 Next_Entity (Id);
534 end loop;
535 end Install_Composite_Operations;
537 -- Local variables
539 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
540 Body_Id : Entity_Id;
541 HSS : Node_Id;
542 Last_Spec_Entity : Entity_Id;
543 New_N : Node_Id;
544 Pack_Decl : Node_Id;
545 Spec_Id : Entity_Id;
547 -- Start of processing for Analyze_Package_Body_Helper
549 begin
550 -- Find corresponding package specification, and establish the current
551 -- scope. The visible defining entity for the package is the defining
552 -- occurrence in the spec. On exit from the package body, all body
553 -- declarations are attached to the defining entity for the body, but
554 -- the later is never used for name resolution. In this fashion there
555 -- is only one visible entity that denotes the package.
557 -- Set Body_Id. Note that this will be reset to point to the generic
558 -- copy later on in the generic case.
560 Body_Id := Defining_Entity (N);
562 -- Body is body of package instantiation. Corresponding spec has already
563 -- been set.
565 if Present (Corresponding_Spec (N)) then
566 Spec_Id := Corresponding_Spec (N);
567 Pack_Decl := Unit_Declaration_Node (Spec_Id);
569 else
570 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
572 if Present (Spec_Id)
573 and then Is_Package_Or_Generic_Package (Spec_Id)
574 then
575 Pack_Decl := Unit_Declaration_Node (Spec_Id);
577 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
578 Error_Msg_N ("cannot supply body for package renaming", N);
579 return;
581 elsif Present (Corresponding_Body (Pack_Decl)) then
582 Error_Msg_N ("redefinition of package body", N);
583 return;
584 end if;
586 else
587 Error_Msg_N ("missing specification for package body", N);
588 return;
589 end if;
591 if Is_Package_Or_Generic_Package (Spec_Id)
592 and then (Scope (Spec_Id) = Standard_Standard
593 or else Is_Child_Unit (Spec_Id))
594 and then not Unit_Requires_Body (Spec_Id)
595 then
596 if Ada_Version = Ada_83 then
597 Error_Msg_N
598 ("optional package body (not allowed in Ada 95)??", N);
599 else
600 Error_Msg_N ("spec of this package does not allow a body", N);
601 end if;
602 end if;
603 end if;
605 -- A [generic] package body "freezes" the contract of the nearest
606 -- enclosing package body and all other contracts encountered in the
607 -- same declarative part up to and excluding the package body:
609 -- package body Nearest_Enclosing_Package
610 -- with Refined_State => (State => Constit)
611 -- is
612 -- Constit : ...;
614 -- package body Freezes_Enclosing_Package_Body
615 -- with Refined_State => (State_2 => Constit_2)
616 -- is
617 -- Constit_2 : ...;
619 -- procedure Proc
620 -- with Refined_Depends => (Input => (Constit, Constit_2)) ...
622 -- This ensures that any annotations referenced by the contract of a
623 -- [generic] subprogram body declared within the current package body
624 -- are available. This form of "freezing" is decoupled from the usual
625 -- Freeze_xxx mechanism because it must also work in the context of
626 -- generics where normal freezing is disabled.
628 -- Only bodies coming from source should cause this type of "freezing".
629 -- Instantiated generic bodies are excluded because their processing is
630 -- performed in a separate compilation pass which lacks enough semantic
631 -- information with respect to contract analysis. It is safe to suppress
632 -- the "freezing" of contracts in this case because this action already
633 -- took place at the end of the enclosing declarative part.
635 if Comes_From_Source (N)
636 and then not Is_Generic_Instance (Spec_Id)
637 then
638 Analyze_Previous_Contracts (N);
639 end if;
641 -- A package body is Ghost when the corresponding spec is Ghost. Set
642 -- the mode now to ensure that any nodes generated during analysis and
643 -- expansion are properly flagged as ignored Ghost.
645 Set_Ghost_Mode (N, Spec_Id);
647 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
648 Style.Check_Identifier (Body_Id, Spec_Id);
650 if Is_Child_Unit (Spec_Id) then
651 if Nkind (Parent (N)) /= N_Compilation_Unit then
652 Error_Msg_NE
653 ("body of child unit& cannot be an inner package", N, Spec_Id);
654 end if;
656 Set_Is_Child_Unit (Body_Id);
657 end if;
659 -- Generic package case
661 if Ekind (Spec_Id) = E_Generic_Package then
663 -- Disable expansion and perform semantic analysis on copy. The
664 -- unannotated body will be used in all instantiations.
666 Body_Id := Defining_Entity (N);
667 Set_Ekind (Body_Id, E_Package_Body);
668 Set_Scope (Body_Id, Scope (Spec_Id));
669 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
670 Set_Body_Entity (Spec_Id, Body_Id);
671 Set_Spec_Entity (Body_Id, Spec_Id);
673 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
674 Rewrite (N, New_N);
676 -- Once the contents of the generic copy and the template are
677 -- swapped, do the same for their respective aspect specifications.
679 Exchange_Aspects (N, New_N);
681 -- Collect all contract-related source pragmas found within the
682 -- template and attach them to the contract of the package body.
683 -- This contract is used in the capture of global references within
684 -- annotations.
686 Create_Generic_Contract (N);
688 -- Update Body_Id to point to the copied node for the remainder of
689 -- the processing.
691 Body_Id := Defining_Entity (N);
692 Start_Generic;
693 end if;
695 -- The Body_Id is that of the copied node in the generic case, the
696 -- current node otherwise. Note that N was rewritten above, so we must
697 -- be sure to get the latest Body_Id value.
699 Set_Ekind (Body_Id, E_Package_Body);
700 Set_Body_Entity (Spec_Id, Body_Id);
701 Set_Spec_Entity (Body_Id, Spec_Id);
703 -- Defining name for the package body is not a visible entity: Only the
704 -- defining name for the declaration is visible.
706 Set_Etype (Body_Id, Standard_Void_Type);
707 Set_Scope (Body_Id, Scope (Spec_Id));
708 Set_Corresponding_Spec (N, Spec_Id);
709 Set_Corresponding_Body (Pack_Decl, Body_Id);
711 -- The body entity is not used for semantics or code generation, but
712 -- it is attached to the entity list of the enclosing scope to simplify
713 -- the listing of back-annotations for the types it main contain.
715 if Scope (Spec_Id) /= Standard_Standard then
716 Append_Entity (Body_Id, Scope (Spec_Id));
717 end if;
719 -- Indicate that we are currently compiling the body of the package
721 Set_In_Package_Body (Spec_Id);
722 Set_Has_Completion (Spec_Id);
723 Last_Spec_Entity := Last_Entity (Spec_Id);
725 if Has_Aspects (N) then
726 Analyze_Aspect_Specifications (N, Body_Id);
727 end if;
729 Push_Scope (Spec_Id);
731 -- Set SPARK_Mode only for non-generic package
733 if Ekind (Spec_Id) = E_Package then
734 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
735 Set_SPARK_Aux_Pragma (Body_Id, SPARK_Mode_Pragma);
736 Set_SPARK_Pragma_Inherited (Body_Id);
737 Set_SPARK_Aux_Pragma_Inherited (Body_Id);
738 end if;
740 -- Inherit the "ghostness" of the package spec. Note that this property
741 -- is not directly inherited as the body may be subject to a different
742 -- Ghost assertion policy.
744 if Ghost_Mode > None or else Is_Ghost_Entity (Spec_Id) then
745 Set_Is_Ghost_Entity (Body_Id);
747 -- The Ghost policy in effect at the point of declaration and at the
748 -- point of completion must match (SPARK RM 6.9(14)).
750 Check_Ghost_Completion (Spec_Id, Body_Id);
751 end if;
753 Set_Categorization_From_Pragmas (N);
755 Install_Visible_Declarations (Spec_Id);
756 Install_Private_Declarations (Spec_Id);
757 Install_Private_With_Clauses (Spec_Id);
758 Install_Composite_Operations (Spec_Id);
760 Check_Anonymous_Access_Types (Spec_Id, N);
762 if Ekind (Spec_Id) = E_Generic_Package then
763 Set_Use (Generic_Formal_Declarations (Pack_Decl));
764 end if;
766 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
767 Set_Use (Private_Declarations (Specification (Pack_Decl)));
769 -- This is a nested package, so it may be necessary to declare certain
770 -- inherited subprograms that are not yet visible because the parent
771 -- type's subprograms are now visible.
773 if Ekind (Scope (Spec_Id)) = E_Package
774 and then Scope (Spec_Id) /= Standard_Standard
775 then
776 Declare_Inherited_Private_Subprograms (Spec_Id);
777 end if;
779 -- A package body "freezes" the contract of its initial declaration.
780 -- This analysis depends on attribute Corresponding_Spec being set. Only
781 -- bodies coming from source shuld cause this type of "freezing".
783 if Present (Declarations (N)) then
784 Analyze_Declarations (Declarations (N));
785 Inspect_Deferred_Constant_Completion (Declarations (N));
786 end if;
788 -- Verify that the SPARK_Mode of the body agrees with that of its spec
790 if Present (SPARK_Pragma (Body_Id)) then
791 if Present (SPARK_Aux_Pragma (Spec_Id)) then
792 if Get_SPARK_Mode_From_Annotation (SPARK_Aux_Pragma (Spec_Id)) =
794 and then
795 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
796 then
797 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
798 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
799 Error_Msg_Sloc := Sloc (SPARK_Aux_Pragma (Spec_Id));
800 Error_Msg_NE
801 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
802 end if;
804 else
805 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
806 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
807 Error_Msg_Sloc := Sloc (Spec_Id);
808 Error_Msg_NE
809 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
810 end if;
811 end if;
813 -- Analyze_Declarations has caused freezing of all types. Now generate
814 -- bodies for RACW primitives and stream attributes, if any.
816 if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then
818 -- Attach subprogram bodies to support RACWs declared in spec
820 Append_RACW_Bodies (Declarations (N), Spec_Id);
821 Analyze_List (Declarations (N));
822 end if;
824 HSS := Handled_Statement_Sequence (N);
826 if Present (HSS) then
827 Process_End_Label (HSS, 't', Spec_Id);
828 Analyze (HSS);
830 -- Check that elaboration code in a preelaborable package body is
831 -- empty other than null statements and labels (RM 10.2.1(6)).
833 Validate_Null_Statement_Sequence (N);
834 end if;
836 Validate_Categorization_Dependency (N, Spec_Id);
837 Check_Completion (Body_Id);
839 -- Generate start of body reference. Note that we do this fairly late,
840 -- because the call will use In_Extended_Main_Source_Unit as a check,
841 -- and we want to make sure that Corresponding_Stub links are set
843 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
845 -- For a generic package, collect global references and mark them on
846 -- the original body so that they are not resolved again at the point
847 -- of instantiation.
849 if Ekind (Spec_Id) /= E_Package then
850 Save_Global_References (Original_Node (N));
851 End_Generic;
852 end if;
854 -- The entities of the package body have so far been chained onto the
855 -- declaration chain for the spec. That's been fine while we were in the
856 -- body, since we wanted them to be visible, but now that we are leaving
857 -- the package body, they are no longer visible, so we remove them from
858 -- the entity chain of the package spec entity, and copy them to the
859 -- entity chain of the package body entity, where they will never again
860 -- be visible.
862 if Present (Last_Spec_Entity) then
863 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
864 Set_Next_Entity (Last_Spec_Entity, Empty);
865 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
866 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
868 else
869 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
870 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
871 Set_First_Entity (Spec_Id, Empty);
872 Set_Last_Entity (Spec_Id, Empty);
873 end if;
875 End_Package_Scope (Spec_Id);
877 -- All entities declared in body are not visible
879 declare
880 E : Entity_Id;
882 begin
883 E := First_Entity (Body_Id);
884 while Present (E) loop
885 Set_Is_Immediately_Visible (E, False);
886 Set_Is_Potentially_Use_Visible (E, False);
887 Set_Is_Hidden (E);
889 -- Child units may appear on the entity list (e.g. if they appear
890 -- in the context of a subunit) but they are not body entities.
892 if not Is_Child_Unit (E) then
893 Set_Is_Package_Body_Entity (E);
894 end if;
896 Next_Entity (E);
897 end loop;
898 end;
900 Check_References (Body_Id);
902 -- For a generic unit, check that the formal parameters are referenced,
903 -- and that local variables are used, as for regular packages.
905 if Ekind (Spec_Id) = E_Generic_Package then
906 Check_References (Spec_Id);
907 end if;
909 -- At this point all entities of the package body are externally visible
910 -- to the linker as their Is_Public flag is set to True. This proactive
911 -- approach is necessary because an inlined or a generic body for which
912 -- code is generated in other units may need to see these entities. Cut
913 -- down the number of global symbols that do not neet public visibility
914 -- as this has two beneficial effects:
915 -- (1) It makes the compilation process more efficient.
916 -- (2) It gives the code generatormore freedom to optimize within each
917 -- unit, especially subprograms.
919 -- This is done only for top level library packages or child units as
920 -- the algorithm does a top down traversal of the package body.
922 if (Scope (Spec_Id) = Standard_Standard or else Is_Child_Unit (Spec_Id))
923 and then not Is_Generic_Unit (Spec_Id)
924 then
925 Hide_Public_Entities (Declarations (N));
926 end if;
928 -- If expander is not active, then here is where we turn off the
929 -- In_Package_Body flag, otherwise it is turned off at the end of the
930 -- corresponding expansion routine. If this is an instance body, we need
931 -- to qualify names of local entities, because the body may have been
932 -- compiled as a preliminary to another instantiation.
934 if not Expander_Active then
935 Set_In_Package_Body (Spec_Id, False);
937 if Is_Generic_Instance (Spec_Id)
938 and then Operating_Mode = Generate_Code
939 then
940 Qualify_Entity_Names (N);
941 end if;
942 end if;
944 Ghost_Mode := Save_Ghost_Mode;
945 end Analyze_Package_Body_Helper;
947 ---------------------------------
948 -- Analyze_Package_Declaration --
949 ---------------------------------
951 procedure Analyze_Package_Declaration (N : Node_Id) is
952 Id : constant Node_Id := Defining_Entity (N);
953 Par : constant Node_Id := Parent_Spec (N);
955 Is_Comp_Unit : constant Boolean :=
956 Nkind (Parent (N)) = N_Compilation_Unit;
958 Body_Required : Boolean;
959 -- True when this package declaration requires a corresponding body
961 begin
962 if Debug_Flag_C then
963 Write_Str ("==> package spec ");
964 Write_Name (Chars (Id));
965 Write_Str (" from ");
966 Write_Location (Sloc (N));
967 Write_Eol;
968 Indent;
969 end if;
971 Generate_Definition (Id);
972 Enter_Name (Id);
973 Set_Ekind (Id, E_Package);
974 Set_Etype (Id, Standard_Void_Type);
976 -- Set SPARK_Mode from context only for non-generic package
978 if Ekind (Id) = E_Package then
979 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
980 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
981 Set_SPARK_Pragma_Inherited (Id);
982 Set_SPARK_Aux_Pragma_Inherited (Id);
983 end if;
985 -- A package declared within a Ghost refion is automatically Ghost. A
986 -- child package is Ghost when its parent is Ghost (SPARK RM 6.9(2)).
988 if Ghost_Mode > None
989 or else (Present (Par)
990 and then Is_Ghost_Entity (Defining_Entity (Unit (Par))))
991 then
992 Set_Is_Ghost_Entity (Id);
993 end if;
995 -- Analyze aspect specifications immediately, since we need to recognize
996 -- things like Pure early enough to diagnose violations during analysis.
998 if Has_Aspects (N) then
999 Analyze_Aspect_Specifications (N, Id);
1000 end if;
1002 -- Ada 2005 (AI-217): Check if the package has been illegally named in
1003 -- a limited-with clause of its own context. In this case the error has
1004 -- been previously notified by Analyze_Context.
1006 -- limited with Pkg; -- ERROR
1007 -- package Pkg is ...
1009 if From_Limited_With (Id) then
1010 return;
1011 end if;
1013 Push_Scope (Id);
1015 Set_Is_Pure (Id, Is_Pure (Enclosing_Lib_Unit_Entity));
1016 Set_Categorization_From_Pragmas (N);
1018 Analyze (Specification (N));
1019 Validate_Categorization_Dependency (N, Id);
1021 -- Determine whether the package requires a body. Abstract states are
1022 -- intentionally ignored because they do require refinement which can
1023 -- only come in a body, but at the same time they do not force the need
1024 -- for a body on their own (SPARK RM 7.1.4(4) and 7.2.2(3)).
1026 Body_Required := Unit_Requires_Body (Id);
1028 if not Body_Required then
1030 -- If the package spec does not require an explicit body, then there
1031 -- are not entities requiring completion in the language sense. Call
1032 -- Check_Completion now to ensure that nested package declarations
1033 -- that require an implicit body get one. (In the case where a body
1034 -- is required, Check_Completion is called at the end of the body's
1035 -- declarative part.)
1037 Check_Completion;
1039 -- If the package spec does not require an explicit body, then all
1040 -- abstract states declared in nested packages cannot possibly get
1041 -- a proper refinement (SPARK RM 7.2.2(3)). This check is performed
1042 -- only when the compilation unit is the main unit to allow for
1043 -- modular SPARK analysis where packages do not necessarily have
1044 -- bodies.
1046 if Is_Comp_Unit then
1047 Check_State_Refinements
1048 (Context => N,
1049 Is_Main_Unit => Parent (N) = Cunit (Main_Unit));
1050 end if;
1051 end if;
1053 if Is_Comp_Unit then
1055 -- Set Body_Required indication on the compilation unit node, and
1056 -- determine whether elaboration warnings may be meaningful on it.
1058 Set_Body_Required (Parent (N), Body_Required);
1060 if not Body_Required then
1061 Set_Suppress_Elaboration_Warnings (Id);
1062 end if;
1063 end if;
1065 End_Package_Scope (Id);
1067 -- For the declaration of a library unit that is a remote types package,
1068 -- check legality rules regarding availability of stream attributes for
1069 -- types that contain non-remote access values. This subprogram performs
1070 -- visibility tests that rely on the fact that we have exited the scope
1071 -- of Id.
1073 if Is_Comp_Unit then
1074 Validate_RT_RAT_Component (N);
1075 end if;
1077 if Debug_Flag_C then
1078 Outdent;
1079 Write_Str ("<== package spec ");
1080 Write_Name (Chars (Id));
1081 Write_Str (" from ");
1082 Write_Location (Sloc (N));
1083 Write_Eol;
1084 end if;
1085 end Analyze_Package_Declaration;
1087 -----------------------------------
1088 -- Analyze_Package_Specification --
1089 -----------------------------------
1091 -- Note that this code is shared for the analysis of generic package specs
1092 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
1094 procedure Analyze_Package_Specification (N : Node_Id) is
1095 Id : constant Entity_Id := Defining_Entity (N);
1096 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
1097 Vis_Decls : constant List_Id := Visible_Declarations (N);
1098 Priv_Decls : constant List_Id := Private_Declarations (N);
1099 E : Entity_Id;
1100 L : Entity_Id;
1101 Public_Child : Boolean;
1103 Private_With_Clauses_Installed : Boolean := False;
1104 -- In Ada 2005, private with_clauses are visible in the private part
1105 -- of a nested package, even if it appears in the public part of the
1106 -- enclosing package. This requires a separate step to install these
1107 -- private_with_clauses, and remove them at the end of the nested
1108 -- package.
1110 procedure Check_One_Tagged_Type_Or_Extension_At_Most;
1111 -- Issue an error in SPARK mode if a package specification contains
1112 -- more than one tagged type or type extension.
1114 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
1115 -- Clears constant indications (Never_Set_In_Source, Constant_Value, and
1116 -- Is_True_Constant) on all variables that are entities of Id, and on
1117 -- the chain whose first element is FE. A recursive call is made for all
1118 -- packages and generic packages.
1120 procedure Generate_Parent_References;
1121 -- For a child unit, generate references to parent units, for
1122 -- GPS navigation purposes.
1124 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
1125 -- Child and Unit are entities of compilation units. True if Child
1126 -- is a public child of Parent as defined in 10.1.1
1128 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
1129 -- Reject completion of an incomplete or private type declarations
1130 -- having a known discriminant part by an unchecked union.
1132 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
1133 -- Given the package entity of a generic package instantiation or
1134 -- formal package whose corresponding generic is a child unit, installs
1135 -- the private declarations of each of the child unit's parents.
1136 -- This has to be done at the point of entering the instance package's
1137 -- private part rather than being done in Sem_Ch12.Install_Parent
1138 -- (which is where the parents' visible declarations are installed).
1140 ------------------------------------------------
1141 -- Check_One_Tagged_Type_Or_Extension_At_Most --
1142 ------------------------------------------------
1144 procedure Check_One_Tagged_Type_Or_Extension_At_Most is
1145 Previous : Node_Id;
1147 procedure Check_Decls (Decls : List_Id);
1148 -- Check that either Previous is Empty and Decls does not contain
1149 -- more than one tagged type or type extension, or Previous is
1150 -- already set and Decls contains no tagged type or type extension.
1152 -----------------
1153 -- Check_Decls --
1154 -----------------
1156 procedure Check_Decls (Decls : List_Id) is
1157 Decl : Node_Id;
1159 begin
1160 Decl := First (Decls);
1161 while Present (Decl) loop
1162 if Nkind (Decl) = N_Full_Type_Declaration
1163 and then Is_Tagged_Type (Defining_Identifier (Decl))
1164 then
1165 if No (Previous) then
1166 Previous := Decl;
1168 else
1169 Error_Msg_Sloc := Sloc (Previous);
1170 Check_SPARK_05_Restriction
1171 ("at most one tagged type or type extension allowed",
1172 "\\ previous declaration#",
1173 Decl);
1174 end if;
1175 end if;
1177 Next (Decl);
1178 end loop;
1179 end Check_Decls;
1181 -- Start of processing for Check_One_Tagged_Type_Or_Extension_At_Most
1183 begin
1184 Previous := Empty;
1185 Check_Decls (Vis_Decls);
1187 if Present (Priv_Decls) then
1188 Check_Decls (Priv_Decls);
1189 end if;
1190 end Check_One_Tagged_Type_Or_Extension_At_Most;
1192 ---------------------
1193 -- Clear_Constants --
1194 ---------------------
1196 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
1197 E : Entity_Id;
1199 begin
1200 -- Ignore package renamings, not interesting and they can cause self
1201 -- referential loops in the code below.
1203 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
1204 return;
1205 end if;
1207 -- Note: in the loop below, the check for Next_Entity pointing back
1208 -- to the package entity may seem odd, but it is needed, because a
1209 -- package can contain a renaming declaration to itself, and such
1210 -- renamings are generated automatically within package instances.
1212 E := FE;
1213 while Present (E) and then E /= Id loop
1214 if Is_Assignable (E) then
1215 Set_Never_Set_In_Source (E, False);
1216 Set_Is_True_Constant (E, False);
1217 Set_Current_Value (E, Empty);
1218 Set_Is_Known_Null (E, False);
1219 Set_Last_Assignment (E, Empty);
1221 if not Can_Never_Be_Null (E) then
1222 Set_Is_Known_Non_Null (E, False);
1223 end if;
1225 elsif Is_Package_Or_Generic_Package (E) then
1226 Clear_Constants (E, First_Entity (E));
1227 Clear_Constants (E, First_Private_Entity (E));
1228 end if;
1230 Next_Entity (E);
1231 end loop;
1232 end Clear_Constants;
1234 --------------------------------
1235 -- Generate_Parent_References --
1236 --------------------------------
1238 procedure Generate_Parent_References is
1239 Decl : constant Node_Id := Parent (N);
1241 begin
1242 if Id = Cunit_Entity (Main_Unit)
1243 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
1244 then
1245 Generate_Reference (Id, Scope (Id), 'k', False);
1247 elsif not Nkind_In (Unit (Cunit (Main_Unit)), N_Subprogram_Body,
1248 N_Subunit)
1249 then
1250 -- If current unit is an ancestor of main unit, generate a
1251 -- reference to its own parent.
1253 declare
1254 U : Node_Id;
1255 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
1257 begin
1258 if Nkind (Main_Spec) = N_Package_Body then
1259 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
1260 end if;
1262 U := Parent_Spec (Main_Spec);
1263 while Present (U) loop
1264 if U = Parent (Decl) then
1265 Generate_Reference (Id, Scope (Id), 'k', False);
1266 exit;
1268 elsif Nkind (Unit (U)) = N_Package_Body then
1269 exit;
1271 else
1272 U := Parent_Spec (Unit (U));
1273 end if;
1274 end loop;
1275 end;
1276 end if;
1277 end Generate_Parent_References;
1279 ---------------------
1280 -- Is_Public_Child --
1281 ---------------------
1283 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
1284 begin
1285 if not Is_Private_Descendant (Child) then
1286 return True;
1287 else
1288 if Child = Unit then
1289 return not Private_Present (
1290 Parent (Unit_Declaration_Node (Child)));
1291 else
1292 return Is_Public_Child (Scope (Child), Unit);
1293 end if;
1294 end if;
1295 end Is_Public_Child;
1297 ----------------------------------------
1298 -- Inspect_Unchecked_Union_Completion --
1299 ----------------------------------------
1301 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
1302 Decl : Node_Id;
1304 begin
1305 Decl := First (Decls);
1306 while Present (Decl) loop
1308 -- We are looking at an incomplete or private type declaration
1309 -- with a known_discriminant_part whose full view is an
1310 -- Unchecked_Union.
1312 if Nkind_In (Decl, N_Incomplete_Type_Declaration,
1313 N_Private_Type_Declaration)
1314 and then Has_Discriminants (Defining_Identifier (Decl))
1315 and then Present (Full_View (Defining_Identifier (Decl)))
1316 and then
1317 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
1318 then
1319 Error_Msg_N
1320 ("completion of discriminated partial view "
1321 & "cannot be an unchecked union",
1322 Full_View (Defining_Identifier (Decl)));
1323 end if;
1325 Next (Decl);
1326 end loop;
1327 end Inspect_Unchecked_Union_Completion;
1329 -----------------------------------------
1330 -- Install_Parent_Private_Declarations --
1331 -----------------------------------------
1333 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
1334 Inst_Par : Entity_Id;
1335 Gen_Par : Entity_Id;
1336 Inst_Node : Node_Id;
1338 begin
1339 Inst_Par := Inst_Id;
1341 Gen_Par :=
1342 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
1343 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
1344 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
1346 if Nkind_In (Inst_Node, N_Package_Instantiation,
1347 N_Formal_Package_Declaration)
1348 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
1349 then
1350 Inst_Par := Entity (Prefix (Name (Inst_Node)));
1352 if Present (Renamed_Entity (Inst_Par)) then
1353 Inst_Par := Renamed_Entity (Inst_Par);
1354 end if;
1356 Gen_Par :=
1357 Generic_Parent
1358 (Specification (Unit_Declaration_Node (Inst_Par)));
1360 -- Install the private declarations and private use clauses
1361 -- of a parent instance of the child instance, unless the
1362 -- parent instance private declarations have already been
1363 -- installed earlier in Analyze_Package_Specification, which
1364 -- happens when a generic child is instantiated, and the
1365 -- instance is a child of the parent instance.
1367 -- Installing the use clauses of the parent instance twice
1368 -- is both unnecessary and wrong, because it would cause the
1369 -- clauses to be chained to themselves in the use clauses
1370 -- list of the scope stack entry. That in turn would cause
1371 -- an endless loop from End_Use_Clauses upon scope exit.
1373 -- The parent is now fully visible. It may be a hidden open
1374 -- scope if we are currently compiling some child instance
1375 -- declared within it, but while the current instance is being
1376 -- compiled the parent is immediately visible. In particular
1377 -- its entities must remain visible if a stack save/restore
1378 -- takes place through a call to Rtsfind.
1380 if Present (Gen_Par) then
1381 if not In_Private_Part (Inst_Par) then
1382 Install_Private_Declarations (Inst_Par);
1383 Set_Use (Private_Declarations
1384 (Specification
1385 (Unit_Declaration_Node (Inst_Par))));
1386 Set_Is_Hidden_Open_Scope (Inst_Par, False);
1387 end if;
1389 -- If we've reached the end of the generic instance parents,
1390 -- then finish off by looping through the nongeneric parents
1391 -- and installing their private declarations.
1393 -- If one of the non-generic parents is itself on the scope
1394 -- stack, do not install its private declarations: they are
1395 -- installed in due time when the private part of that parent
1396 -- is analyzed.
1398 else
1399 while Present (Inst_Par)
1400 and then Inst_Par /= Standard_Standard
1401 and then (not In_Open_Scopes (Inst_Par)
1402 or else not In_Private_Part (Inst_Par))
1403 loop
1404 if Nkind (Inst_Node) = N_Formal_Package_Declaration
1405 or else
1406 not Is_Ancestor_Package
1407 (Inst_Par, Cunit_Entity (Current_Sem_Unit))
1408 then
1409 Install_Private_Declarations (Inst_Par);
1410 Set_Use
1411 (Private_Declarations
1412 (Specification
1413 (Unit_Declaration_Node (Inst_Par))));
1414 Inst_Par := Scope (Inst_Par);
1415 else
1416 exit;
1417 end if;
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 -- Preanalyze and resolve the invariants of a private type at the
1472 -- end of the visible declarations to catch potential errors. Note
1473 -- that inherited class-wide invariants are not considered because
1474 -- they have already been resolved.
1476 if Ekind_In (E, E_Limited_Private_Type,
1477 E_Private_Type,
1478 E_Record_Type_With_Private)
1479 and then Has_Own_Invariants (E)
1480 then
1481 Build_Invariant_Procedure_Body (E, Partial_Invariant => True);
1482 end if;
1483 end if;
1485 Next_Entity (E);
1486 end loop;
1488 if Is_Remote_Call_Interface (Id)
1489 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1490 then
1491 Validate_RCI_Declarations (Id);
1492 end if;
1494 -- Save global references in the visible declarations, before installing
1495 -- private declarations of parent unit if there is one, because the
1496 -- privacy status of types defined in the parent will change. This is
1497 -- only relevant for generic child units, but is done in all cases for
1498 -- uniformity.
1500 if Ekind (Id) = E_Generic_Package
1501 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1502 then
1503 declare
1504 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1505 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1507 begin
1508 -- Insert the freezing nodes after the visible declarations to
1509 -- ensure that we analyze its aspects; needed to ensure that
1510 -- global entities referenced in the aspects are properly handled.
1512 if Ada_Version >= Ada_2012
1513 and then Is_Non_Empty_List (Vis_Decls)
1514 and then Is_Empty_List (Priv_Decls)
1515 then
1516 Insert_List_After_And_Analyze
1517 (Last (Vis_Decls), Freeze_Entity (Id, Last (Vis_Decls)));
1518 end if;
1520 Set_Private_Declarations (Orig_Spec, Empty_List);
1521 Save_Global_References (Orig_Decl);
1522 Set_Private_Declarations (Orig_Spec, Save_Priv);
1523 end;
1524 end if;
1526 -- If package is a public child unit, then make the private declarations
1527 -- of the parent visible.
1529 Public_Child := False;
1531 declare
1532 Par : Entity_Id;
1533 Pack_Decl : Node_Id;
1534 Par_Spec : Node_Id;
1536 begin
1537 Par := Id;
1538 Par_Spec := Parent_Spec (Parent (N));
1540 -- If the package is formal package of an enclosing generic, it is
1541 -- transformed into a local generic declaration, and compiled to make
1542 -- its spec available. We need to retrieve the original generic to
1543 -- determine whether it is a child unit, and install its parents.
1545 if No (Par_Spec)
1546 and then
1547 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1548 then
1549 Par := Entity (Name (Original_Node (Parent (N))));
1550 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1551 end if;
1553 if Present (Par_Spec) then
1554 Generate_Parent_References;
1556 while Scope (Par) /= Standard_Standard
1557 and then Is_Public_Child (Id, Par)
1558 and then In_Open_Scopes (Par)
1559 loop
1560 Public_Child := True;
1561 Par := Scope (Par);
1562 Install_Private_Declarations (Par);
1563 Install_Private_With_Clauses (Par);
1564 Pack_Decl := Unit_Declaration_Node (Par);
1565 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1566 end loop;
1567 end if;
1568 end;
1570 if Is_Compilation_Unit (Id) then
1571 Install_Private_With_Clauses (Id);
1572 else
1573 -- The current compilation unit may include private with_clauses,
1574 -- which are visible in the private part of the current nested
1575 -- package, and have to be installed now. This is not done for
1576 -- nested instantiations, where the private with_clauses of the
1577 -- enclosing unit have no effect once the instantiation info is
1578 -- established and we start analyzing the package declaration.
1580 declare
1581 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1582 begin
1583 if Is_Package_Or_Generic_Package (Comp_Unit)
1584 and then not In_Private_Part (Comp_Unit)
1585 and then not In_Instance
1586 then
1587 Install_Private_With_Clauses (Comp_Unit);
1588 Private_With_Clauses_Installed := True;
1589 end if;
1590 end;
1591 end if;
1593 -- If this is a package associated with a generic instance or formal
1594 -- package, then the private declarations of each of the generic's
1595 -- parents must be installed at this point.
1597 if Is_Generic_Instance (Id) then
1598 Install_Parent_Private_Declarations (Id);
1599 end if;
1601 -- Analyze private part if present. The flag In_Private_Part is reset
1602 -- in End_Package_Scope.
1604 L := Last_Entity (Id);
1606 if Present (Priv_Decls) then
1607 Set_In_Private_Part (Id);
1609 -- Upon entering a public child's private part, it may be necessary
1610 -- to declare subprograms that were derived in the package's visible
1611 -- part but not yet made visible.
1613 if Public_Child then
1614 Declare_Inherited_Private_Subprograms (Id);
1615 end if;
1617 Analyze_Declarations (Priv_Decls);
1619 -- Check the private declarations for incomplete deferred constants
1621 Inspect_Deferred_Constant_Completion (Priv_Decls);
1623 -- The first private entity is the immediate follower of the last
1624 -- visible entity, if there was one.
1626 if Present (L) then
1627 Set_First_Private_Entity (Id, Next_Entity (L));
1628 else
1629 Set_First_Private_Entity (Id, First_Entity (Id));
1630 end if;
1632 -- There may be inherited private subprograms that need to be declared,
1633 -- even in the absence of an explicit private part. If there are any
1634 -- public declarations in the package and the package is a public child
1635 -- unit, then an implicit private part is assumed.
1637 elsif Present (L) and then Public_Child then
1638 Set_In_Private_Part (Id);
1639 Declare_Inherited_Private_Subprograms (Id);
1640 Set_First_Private_Entity (Id, Next_Entity (L));
1641 end if;
1643 E := First_Entity (Id);
1644 while Present (E) loop
1646 -- Check rule of 3.6(11), which in general requires waiting till all
1647 -- full types have been seen.
1649 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1650 Check_Aliased_Component_Types (E);
1651 end if;
1653 -- Check preelaborable initialization for full type completing a
1654 -- private type for which pragma Preelaborable_Initialization given.
1656 if Is_Type (E)
1657 and then Must_Have_Preelab_Init (E)
1658 and then not Has_Preelaborable_Initialization (E)
1659 then
1660 Error_Msg_N
1661 ("full view of & does not have preelaborable initialization", E);
1662 end if;
1664 -- Preanalyze and resolve the invariants of a private type's full
1665 -- view at the end of the private declarations in case freezing did
1666 -- not take place either due to errors or because the context is a
1667 -- generic unit.
1669 if Is_Type (E)
1670 and then not Is_Private_Type (E)
1671 and then Has_Private_Declaration (E)
1672 and then Has_Invariants (E)
1673 and then Serious_Errors_Detected > 0
1674 then
1675 Build_Invariant_Procedure_Body (E);
1676 end if;
1678 Next_Entity (E);
1679 end loop;
1681 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1682 -- declaration having a known_discriminant_part shall not be an
1683 -- unchecked union type.
1685 if Present (Vis_Decls) then
1686 Inspect_Unchecked_Union_Completion (Vis_Decls);
1687 end if;
1689 if Present (Priv_Decls) then
1690 Inspect_Unchecked_Union_Completion (Priv_Decls);
1691 end if;
1693 if Ekind (Id) = E_Generic_Package
1694 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1695 and then Present (Priv_Decls)
1696 then
1697 -- Save global references in private declarations, ignoring the
1698 -- visible declarations that were processed earlier.
1700 declare
1701 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1702 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1703 Save_Form : constant List_Id :=
1704 Generic_Formal_Declarations (Orig_Decl);
1706 begin
1707 -- Insert the freezing nodes after the private declarations to
1708 -- ensure that we analyze its aspects; needed to ensure that
1709 -- global entities referenced in the aspects are properly handled.
1711 if Ada_Version >= Ada_2012
1712 and then Is_Non_Empty_List (Priv_Decls)
1713 then
1714 Insert_List_After_And_Analyze
1715 (Last (Priv_Decls), Freeze_Entity (Id, Last (Priv_Decls)));
1716 end if;
1718 Set_Visible_Declarations (Orig_Spec, Empty_List);
1719 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1720 Save_Global_References (Orig_Decl);
1721 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1722 Set_Visible_Declarations (Orig_Spec, Save_Vis);
1723 end;
1724 end if;
1726 Process_End_Label (N, 'e', Id);
1728 -- Remove private_with_clauses of enclosing compilation unit, if they
1729 -- were installed.
1731 if Private_With_Clauses_Installed then
1732 Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
1733 end if;
1735 -- For the case of a library level package, we must go through all the
1736 -- entities clearing the indications that the value may be constant and
1737 -- not modified. Why? Because any client of this package may modify
1738 -- these values freely from anywhere. This also applies to any nested
1739 -- packages or generic packages.
1741 -- For now we unconditionally clear constants for packages that are
1742 -- instances of generic packages. The reason is that we do not have the
1743 -- body yet, and we otherwise think things are unreferenced when they
1744 -- are not. This should be fixed sometime (the effect is not terrible,
1745 -- we just lose some warnings, and also some cases of value propagation)
1746 -- ???
1748 if Is_Library_Level_Entity (Id)
1749 or else Is_Generic_Instance (Id)
1750 then
1751 Clear_Constants (Id, First_Entity (Id));
1752 Clear_Constants (Id, First_Private_Entity (Id));
1753 end if;
1755 -- Issue an error in SPARK mode if a package specification contains
1756 -- more than one tagged type or type extension.
1758 Check_One_Tagged_Type_Or_Extension_At_Most;
1760 -- If switch set, output information on why body required
1762 if List_Body_Required_Info
1763 and then In_Extended_Main_Source_Unit (Id)
1764 and then Unit_Requires_Body (Id)
1765 then
1766 Unit_Requires_Body_Info (Id);
1767 end if;
1768 end Analyze_Package_Specification;
1770 --------------------------------------
1771 -- Analyze_Private_Type_Declaration --
1772 --------------------------------------
1774 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1775 Id : constant Entity_Id := Defining_Identifier (N);
1776 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
1778 begin
1779 Generate_Definition (Id);
1780 Set_Is_Pure (Id, PF);
1781 Init_Size_Align (Id);
1783 if not Is_Package_Or_Generic_Package (Current_Scope)
1784 or else In_Private_Part (Current_Scope)
1785 then
1786 Error_Msg_N ("invalid context for private declaration", N);
1787 end if;
1789 New_Private_Type (N, Id, N);
1790 Set_Depends_On_Private (Id);
1792 -- A type declared within a Ghost region is automatically Ghost
1793 -- (SPARK RM 6.9(2)).
1795 if Ghost_Mode > None then
1796 Set_Is_Ghost_Entity (Id);
1797 end if;
1799 if Has_Aspects (N) then
1800 Analyze_Aspect_Specifications (N, Id);
1801 end if;
1802 end Analyze_Private_Type_Declaration;
1804 ----------------------------------
1805 -- Check_Anonymous_Access_Types --
1806 ----------------------------------
1808 procedure Check_Anonymous_Access_Types
1809 (Spec_Id : Entity_Id;
1810 P_Body : Node_Id)
1812 E : Entity_Id;
1813 IR : Node_Id;
1815 begin
1816 -- Itype references are only needed by gigi, to force elaboration of
1817 -- itypes. In the absence of code generation, they are not needed.
1819 if not Expander_Active then
1820 return;
1821 end if;
1823 E := First_Entity (Spec_Id);
1824 while Present (E) loop
1825 if Ekind (E) = E_Anonymous_Access_Type
1826 and then From_Limited_With (E)
1827 then
1828 IR := Make_Itype_Reference (Sloc (P_Body));
1829 Set_Itype (IR, E);
1831 if No (Declarations (P_Body)) then
1832 Set_Declarations (P_Body, New_List (IR));
1833 else
1834 Prepend (IR, Declarations (P_Body));
1835 end if;
1836 end if;
1838 Next_Entity (E);
1839 end loop;
1840 end Check_Anonymous_Access_Types;
1842 -------------------------------------------
1843 -- Declare_Inherited_Private_Subprograms --
1844 -------------------------------------------
1846 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1848 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1849 -- Check whether an inherited subprogram S is an operation of an
1850 -- untagged derived type T.
1852 ---------------------
1853 -- Is_Primitive_Of --
1854 ---------------------
1856 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1857 Formal : Entity_Id;
1859 begin
1860 -- If the full view is a scalar type, the type is the anonymous base
1861 -- type, but the operation mentions the first subtype, so check the
1862 -- signature against the base type.
1864 if Base_Type (Etype (S)) = Base_Type (T) then
1865 return True;
1867 else
1868 Formal := First_Formal (S);
1869 while Present (Formal) loop
1870 if Base_Type (Etype (Formal)) = Base_Type (T) then
1871 return True;
1872 end if;
1874 Next_Formal (Formal);
1875 end loop;
1877 return False;
1878 end if;
1879 end Is_Primitive_Of;
1881 -- Local variables
1883 E : Entity_Id;
1884 Op_List : Elist_Id;
1885 Op_Elmt : Elmt_Id;
1886 Op_Elmt_2 : Elmt_Id;
1887 Prim_Op : Entity_Id;
1888 New_Op : Entity_Id := Empty;
1889 Parent_Subp : Entity_Id;
1890 Tag : Entity_Id;
1892 -- Start of processing for Declare_Inherited_Private_Subprograms
1894 begin
1895 E := First_Entity (Id);
1896 while Present (E) loop
1898 -- If the entity is a nonprivate type extension whose parent type
1899 -- is declared in an open scope, then the type may have inherited
1900 -- operations that now need to be made visible. Ditto if the entity
1901 -- is a formal derived type in a child unit.
1903 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
1904 or else
1905 (Nkind (Parent (E)) = N_Private_Extension_Declaration
1906 and then Is_Generic_Type (E)))
1907 and then In_Open_Scopes (Scope (Etype (E)))
1908 and then Is_Base_Type (E)
1909 then
1910 if Is_Tagged_Type (E) then
1911 Op_List := Primitive_Operations (E);
1912 New_Op := Empty;
1913 Tag := First_Tag_Component (E);
1915 Op_Elmt := First_Elmt (Op_List);
1916 while Present (Op_Elmt) loop
1917 Prim_Op := Node (Op_Elmt);
1919 -- Search primitives that are implicit operations with an
1920 -- internal name whose parent operation has a normal name.
1922 if Present (Alias (Prim_Op))
1923 and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
1924 and then not Comes_From_Source (Prim_Op)
1925 and then Is_Internal_Name (Chars (Prim_Op))
1926 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1927 then
1928 Parent_Subp := Alias (Prim_Op);
1930 -- Case 1: Check if the type has also an explicit
1931 -- overriding for this primitive.
1933 Op_Elmt_2 := Next_Elmt (Op_Elmt);
1934 while Present (Op_Elmt_2) loop
1936 -- Skip entities with attribute Interface_Alias since
1937 -- they are not overriding primitives (these entities
1938 -- link an interface primitive with their covering
1939 -- primitive)
1941 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
1942 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
1943 and then No (Interface_Alias (Node (Op_Elmt_2)))
1944 then
1945 -- The private inherited operation has been
1946 -- overridden by an explicit subprogram:
1947 -- replace the former by the latter.
1949 New_Op := Node (Op_Elmt_2);
1950 Replace_Elmt (Op_Elmt, New_Op);
1951 Remove_Elmt (Op_List, Op_Elmt_2);
1952 Set_Overridden_Operation (New_Op, Parent_Subp);
1954 -- We don't need to inherit its dispatching slot.
1955 -- Set_All_DT_Position has previously ensured that
1956 -- the same slot was assigned to the two primitives
1958 if Present (Tag)
1959 and then Present (DTC_Entity (New_Op))
1960 and then Present (DTC_Entity (Prim_Op))
1961 then
1962 pragma Assert
1963 (DT_Position (New_Op) = DT_Position (Prim_Op));
1964 null;
1965 end if;
1967 goto Next_Primitive;
1968 end if;
1970 Next_Elmt (Op_Elmt_2);
1971 end loop;
1973 -- Case 2: We have not found any explicit overriding and
1974 -- hence we need to declare the operation (i.e., make it
1975 -- visible).
1977 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1979 -- Inherit the dispatching slot if E is already frozen
1981 if Is_Frozen (E)
1982 and then Present (DTC_Entity (Alias (Prim_Op)))
1983 then
1984 Set_DTC_Entity_Value (E, New_Op);
1985 Set_DT_Position_Value (New_Op,
1986 DT_Position (Alias (Prim_Op)));
1987 end if;
1989 pragma Assert
1990 (Is_Dispatching_Operation (New_Op)
1991 and then Node (Last_Elmt (Op_List)) = New_Op);
1993 -- Substitute the new operation for the old one in the
1994 -- type's primitive operations list. Since the new
1995 -- operation was also just added to the end of list,
1996 -- the last element must be removed.
1998 -- (Question: is there a simpler way of declaring the
1999 -- operation, say by just replacing the name of the
2000 -- earlier operation, reentering it in the in the symbol
2001 -- table (how?), and marking it as private???)
2003 Replace_Elmt (Op_Elmt, New_Op);
2004 Remove_Last_Elmt (Op_List);
2005 end if;
2007 <<Next_Primitive>>
2008 Next_Elmt (Op_Elmt);
2009 end loop;
2011 -- Generate listing showing the contents of the dispatch table
2013 if Debug_Flag_ZZ then
2014 Write_DT (E);
2015 end if;
2017 else
2018 -- For untagged type, scan forward to locate inherited hidden
2019 -- operations.
2021 Prim_Op := Next_Entity (E);
2022 while Present (Prim_Op) loop
2023 if Is_Subprogram (Prim_Op)
2024 and then Present (Alias (Prim_Op))
2025 and then not Comes_From_Source (Prim_Op)
2026 and then Is_Internal_Name (Chars (Prim_Op))
2027 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
2028 and then Is_Primitive_Of (E, Prim_Op)
2029 then
2030 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2031 end if;
2033 Next_Entity (Prim_Op);
2035 -- Derived operations appear immediately after the type
2036 -- declaration (or the following subtype indication for
2037 -- a derived scalar type). Further declarations cannot
2038 -- include inherited operations of the type.
2040 if Present (Prim_Op) then
2041 exit when Ekind (Prim_Op) not in Overloadable_Kind;
2042 end if;
2043 end loop;
2044 end if;
2045 end if;
2047 Next_Entity (E);
2048 end loop;
2049 end Declare_Inherited_Private_Subprograms;
2051 -----------------------
2052 -- End_Package_Scope --
2053 -----------------------
2055 procedure End_Package_Scope (P : Entity_Id) is
2056 begin
2057 Uninstall_Declarations (P);
2058 Pop_Scope;
2059 end End_Package_Scope;
2061 ---------------------------
2062 -- Exchange_Declarations --
2063 ---------------------------
2065 procedure Exchange_Declarations (Id : Entity_Id) is
2066 Full_Id : constant Entity_Id := Full_View (Id);
2067 H1 : constant Entity_Id := Homonym (Id);
2068 Next1 : constant Entity_Id := Next_Entity (Id);
2069 H2 : Entity_Id;
2070 Next2 : Entity_Id;
2072 begin
2073 -- If missing full declaration for type, nothing to exchange
2075 if No (Full_Id) then
2076 return;
2077 end if;
2079 -- Otherwise complete the exchange, and preserve semantic links
2081 Next2 := Next_Entity (Full_Id);
2082 H2 := Homonym (Full_Id);
2084 -- Reset full declaration pointer to reflect the switched entities and
2085 -- readjust the next entity chains.
2087 Exchange_Entities (Id, Full_Id);
2089 Set_Next_Entity (Id, Next1);
2090 Set_Homonym (Id, H1);
2092 Set_Full_View (Full_Id, Id);
2093 Set_Next_Entity (Full_Id, Next2);
2094 Set_Homonym (Full_Id, H2);
2095 end Exchange_Declarations;
2097 ----------------------------
2098 -- Install_Package_Entity --
2099 ----------------------------
2101 procedure Install_Package_Entity (Id : Entity_Id) is
2102 begin
2103 if not Is_Internal (Id) then
2104 if Debug_Flag_E then
2105 Write_Str ("Install: ");
2106 Write_Name (Chars (Id));
2107 Write_Eol;
2108 end if;
2110 if Is_Child_Unit (Id) then
2111 null;
2113 -- Do not enter implicitly inherited non-overridden subprograms of
2114 -- a tagged type back into visibility if they have non-conformant
2115 -- homographs (Ada RM 8.3 12.3/2).
2117 elsif Is_Hidden_Non_Overridden_Subpgm (Id) then
2118 null;
2120 else
2121 Set_Is_Immediately_Visible (Id);
2122 end if;
2123 end if;
2124 end Install_Package_Entity;
2126 ----------------------------------
2127 -- Install_Private_Declarations --
2128 ----------------------------------
2130 procedure Install_Private_Declarations (P : Entity_Id) is
2131 Id : Entity_Id;
2132 Full : Entity_Id;
2133 Priv_Deps : Elist_Id;
2135 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
2136 -- When the full view of a private type is made available, we do the
2137 -- same for its private dependents under proper visibility conditions.
2138 -- When compiling a grand-chid unit this needs to be done recursively.
2140 -----------------------------
2141 -- Swap_Private_Dependents --
2142 -----------------------------
2144 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
2145 Deps : Elist_Id;
2146 Priv : Entity_Id;
2147 Priv_Elmt : Elmt_Id;
2148 Is_Priv : Boolean;
2150 begin
2151 Priv_Elmt := First_Elmt (Priv_Deps);
2152 while Present (Priv_Elmt) loop
2153 Priv := Node (Priv_Elmt);
2155 -- Before the exchange, verify that the presence of the Full_View
2156 -- field. This field will be empty if the entity has already been
2157 -- installed due to a previous call.
2159 if Present (Full_View (Priv)) and then Is_Visible_Dependent (Priv)
2160 then
2161 if Is_Private_Type (Priv) then
2162 Deps := Private_Dependents (Priv);
2163 Is_Priv := True;
2164 else
2165 Is_Priv := False;
2166 end if;
2168 -- For each subtype that is swapped, we also swap the reference
2169 -- to it in Private_Dependents, to allow access to it when we
2170 -- swap them out in End_Package_Scope.
2172 Replace_Elmt (Priv_Elmt, Full_View (Priv));
2174 -- Ensure that both views of the dependent private subtype are
2175 -- immediately visible if within some open scope. Check full
2176 -- view before exchanging views.
2178 if In_Open_Scopes (Scope (Full_View (Priv))) then
2179 Set_Is_Immediately_Visible (Priv);
2180 end if;
2182 Exchange_Declarations (Priv);
2183 Set_Is_Immediately_Visible
2184 (Priv, In_Open_Scopes (Scope (Priv)));
2186 Set_Is_Potentially_Use_Visible
2187 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
2189 -- Within a child unit, recurse, except in generic child unit,
2190 -- which (unfortunately) handle private_dependents separately.
2192 if Is_Priv
2193 and then Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
2194 and then not Is_Empty_Elmt_List (Deps)
2195 and then not Inside_A_Generic
2196 then
2197 Swap_Private_Dependents (Deps);
2198 end if;
2199 end if;
2201 Next_Elmt (Priv_Elmt);
2202 end loop;
2203 end Swap_Private_Dependents;
2205 -- Start of processing for Install_Private_Declarations
2207 begin
2208 -- First exchange declarations for private types, so that the full
2209 -- declaration is visible. For each private type, we check its
2210 -- Private_Dependents list and also exchange any subtypes of or derived
2211 -- types from it. Finally, if this is a Taft amendment type, the
2212 -- incomplete declaration is irrelevant, and we want to link the
2213 -- eventual full declaration with the original private one so we
2214 -- also skip the exchange.
2216 Id := First_Entity (P);
2217 while Present (Id) and then Id /= First_Private_Entity (P) loop
2218 if Is_Private_Base_Type (Id)
2219 and then Present (Full_View (Id))
2220 and then Comes_From_Source (Full_View (Id))
2221 and then Scope (Full_View (Id)) = Scope (Id)
2222 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
2223 then
2224 -- If there is a use-type clause on the private type, set the full
2225 -- view accordingly.
2227 Set_In_Use (Full_View (Id), In_Use (Id));
2228 Full := Full_View (Id);
2230 if Is_Private_Base_Type (Full)
2231 and then Has_Private_Declaration (Full)
2232 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
2233 and then In_Open_Scopes (Scope (Etype (Full)))
2234 and then In_Package_Body (Current_Scope)
2235 and then not Is_Private_Type (Etype (Full))
2236 then
2237 -- This is the completion of a private type by a derivation
2238 -- from another private type which is not private anymore. This
2239 -- can only happen in a package nested within a child package,
2240 -- when the parent type is defined in the parent unit. At this
2241 -- point the current type is not private either, and we have
2242 -- to install the underlying full view, which is now visible.
2243 -- Save the current full view as well, so that all views can be
2244 -- restored on exit. It may seem that after compiling the child
2245 -- body there are not environments to restore, but the back-end
2246 -- expects those links to be valid, and freeze nodes depend on
2247 -- them.
2249 if No (Full_View (Full))
2250 and then Present (Underlying_Full_View (Full))
2251 then
2252 Set_Full_View (Id, Underlying_Full_View (Full));
2253 Set_Underlying_Full_View (Id, Full);
2255 Set_Underlying_Full_View (Full, Empty);
2256 Set_Is_Frozen (Full_View (Id));
2257 end if;
2258 end if;
2260 Priv_Deps := Private_Dependents (Id);
2261 Exchange_Declarations (Id);
2262 Set_Is_Immediately_Visible (Id);
2263 Swap_Private_Dependents (Priv_Deps);
2264 end if;
2266 Next_Entity (Id);
2267 end loop;
2269 -- Next make other declarations in the private part visible as well
2271 Id := First_Private_Entity (P);
2272 while Present (Id) loop
2273 Install_Package_Entity (Id);
2274 Set_Is_Hidden (Id, False);
2275 Next_Entity (Id);
2276 end loop;
2278 -- Indicate that the private part is currently visible, so it can be
2279 -- properly reset on exit.
2281 Set_In_Private_Part (P);
2282 end Install_Private_Declarations;
2284 ----------------------------------
2285 -- Install_Visible_Declarations --
2286 ----------------------------------
2288 procedure Install_Visible_Declarations (P : Entity_Id) is
2289 Id : Entity_Id;
2290 Last_Entity : Entity_Id;
2292 begin
2293 pragma Assert
2294 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
2296 if Is_Package_Or_Generic_Package (P) then
2297 Last_Entity := First_Private_Entity (P);
2298 else
2299 Last_Entity := Empty;
2300 end if;
2302 Id := First_Entity (P);
2303 while Present (Id) and then Id /= Last_Entity loop
2304 Install_Package_Entity (Id);
2305 Next_Entity (Id);
2306 end loop;
2307 end Install_Visible_Declarations;
2309 --------------------------
2310 -- Is_Private_Base_Type --
2311 --------------------------
2313 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
2314 begin
2315 return Ekind (E) = E_Private_Type
2316 or else Ekind (E) = E_Limited_Private_Type
2317 or else Ekind (E) = E_Record_Type_With_Private;
2318 end Is_Private_Base_Type;
2320 --------------------------
2321 -- Is_Visible_Dependent --
2322 --------------------------
2324 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
2326 S : constant Entity_Id := Scope (Dep);
2328 begin
2329 -- Renamings created for actual types have the visibility of the actual
2331 if Ekind (S) = E_Package
2332 and then Is_Generic_Instance (S)
2333 and then (Is_Generic_Actual_Type (Dep)
2334 or else Is_Generic_Actual_Type (Full_View (Dep)))
2335 then
2336 return True;
2338 elsif not (Is_Derived_Type (Dep))
2339 and then Is_Derived_Type (Full_View (Dep))
2340 then
2341 -- When instantiating a package body, the scope stack is empty, so
2342 -- check instead whether the dependent type is defined in the same
2343 -- scope as the instance itself.
2345 return In_Open_Scopes (S)
2346 or else (Is_Generic_Instance (Current_Scope)
2347 and then Scope (Dep) = Scope (Current_Scope));
2348 else
2349 return True;
2350 end if;
2351 end Is_Visible_Dependent;
2353 ----------------------------
2354 -- May_Need_Implicit_Body --
2355 ----------------------------
2357 procedure May_Need_Implicit_Body (E : Entity_Id) is
2358 P : constant Node_Id := Unit_Declaration_Node (E);
2359 S : constant Node_Id := Parent (P);
2360 B : Node_Id;
2361 Decls : List_Id;
2363 begin
2364 if not Has_Completion (E)
2365 and then Nkind (P) = N_Package_Declaration
2366 and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
2367 then
2368 B :=
2369 Make_Package_Body (Sloc (E),
2370 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
2371 Chars => Chars (E)),
2372 Declarations => New_List);
2374 if Nkind (S) = N_Package_Specification then
2375 if Present (Private_Declarations (S)) then
2376 Decls := Private_Declarations (S);
2377 else
2378 Decls := Visible_Declarations (S);
2379 end if;
2380 else
2381 Decls := Declarations (S);
2382 end if;
2384 Append (B, Decls);
2385 Analyze (B);
2386 end if;
2387 end May_Need_Implicit_Body;
2389 ----------------------
2390 -- New_Private_Type --
2391 ----------------------
2393 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
2394 begin
2395 -- For other than Ada 2012, enter the name in the current scope
2397 if Ada_Version < Ada_2012 then
2398 Enter_Name (Id);
2400 -- Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2401 -- there may be an incomplete previous view.
2403 else
2404 declare
2405 Prev : Entity_Id;
2406 begin
2407 Prev := Find_Type_Name (N);
2408 pragma Assert (Prev = Id
2409 or else (Ekind (Prev) = E_Incomplete_Type
2410 and then Present (Full_View (Prev))
2411 and then Full_View (Prev) = Id));
2412 end;
2413 end if;
2415 if Limited_Present (Def) then
2416 Set_Ekind (Id, E_Limited_Private_Type);
2417 else
2418 Set_Ekind (Id, E_Private_Type);
2419 end if;
2421 Set_Etype (Id, Id);
2422 Set_Has_Delayed_Freeze (Id);
2423 Set_Is_First_Subtype (Id);
2424 Init_Size_Align (Id);
2426 Set_Is_Constrained (Id,
2427 No (Discriminant_Specifications (N))
2428 and then not Unknown_Discriminants_Present (N));
2430 -- Set tagged flag before processing discriminants, to catch illegal
2431 -- usage.
2433 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
2435 Set_Discriminant_Constraint (Id, No_Elist);
2436 Set_Stored_Constraint (Id, No_Elist);
2438 if Present (Discriminant_Specifications (N)) then
2439 Push_Scope (Id);
2440 Process_Discriminants (N);
2441 End_Scope;
2443 elsif Unknown_Discriminants_Present (N) then
2444 Set_Has_Unknown_Discriminants (Id);
2445 end if;
2447 Set_Private_Dependents (Id, New_Elmt_List);
2449 if Tagged_Present (Def) then
2450 Set_Ekind (Id, E_Record_Type_With_Private);
2451 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2452 Set_Is_Abstract_Type (Id, Abstract_Present (Def));
2453 Set_Is_Limited_Record (Id, Limited_Present (Def));
2454 Set_Has_Delayed_Freeze (Id, True);
2456 -- Recognize Ada.Real_Time.Timing_Events.Timing_Events here
2458 if Is_RTE (Id, RE_Timing_Event) then
2459 Set_Has_Timing_Event (Id);
2460 end if;
2462 -- Create a class-wide type with the same attributes
2464 Make_Class_Wide_Type (Id);
2466 elsif Abstract_Present (Def) then
2467 Error_Msg_N ("only a tagged type can be abstract", N);
2468 end if;
2469 end New_Private_Type;
2471 ---------------------------------
2472 -- Requires_Completion_In_Body --
2473 ---------------------------------
2475 function Requires_Completion_In_Body
2476 (Id : Entity_Id;
2477 Pack_Id : Entity_Id;
2478 Do_Abstract_States : Boolean := False) return Boolean
2480 begin
2481 -- Always ignore child units. Child units get added to the entity list
2482 -- of a parent unit, but are not original entities of the parent, and
2483 -- so do not affect whether the parent needs a body.
2485 if Is_Child_Unit (Id) then
2486 return False;
2488 -- Ignore formal packages and their renamings
2490 elsif Ekind (Id) = E_Package
2491 and then Nkind (Original_Node (Unit_Declaration_Node (Id))) =
2492 N_Formal_Package_Declaration
2493 then
2494 return False;
2496 -- Otherwise test to see if entity requires a completion. Note that
2497 -- subprogram entities whose declaration does not come from source are
2498 -- ignored here on the basis that we assume the expander will provide an
2499 -- implicit completion at some point.
2501 elsif (Is_Overloadable (Id)
2502 and then not Ekind_In (Id, E_Enumeration_Literal, E_Operator)
2503 and then not Is_Abstract_Subprogram (Id)
2504 and then not Has_Completion (Id)
2505 and then Comes_From_Source (Parent (Id)))
2507 or else
2508 (Ekind (Id) = E_Package
2509 and then Id /= Pack_Id
2510 and then not Has_Completion (Id)
2511 and then Unit_Requires_Body (Id, Do_Abstract_States))
2513 or else
2514 (Ekind (Id) = E_Incomplete_Type
2515 and then No (Full_View (Id))
2516 and then not Is_Generic_Type (Id))
2518 or else
2519 (Ekind_In (Id, E_Task_Type, E_Protected_Type)
2520 and then not Has_Completion (Id))
2522 or else
2523 (Ekind (Id) = E_Generic_Package
2524 and then Id /= Pack_Id
2525 and then not Has_Completion (Id)
2526 and then Unit_Requires_Body (Id, Do_Abstract_States))
2528 or else
2529 (Is_Generic_Subprogram (Id)
2530 and then not Has_Completion (Id))
2531 then
2532 return True;
2534 -- Otherwise the entity does not require completion in a package body
2536 else
2537 return False;
2538 end if;
2539 end Requires_Completion_In_Body;
2541 ----------------------------
2542 -- Uninstall_Declarations --
2543 ----------------------------
2545 procedure Uninstall_Declarations (P : Entity_Id) is
2546 Decl : constant Node_Id := Unit_Declaration_Node (P);
2547 Id : Entity_Id;
2548 Full : Entity_Id;
2549 Priv_Elmt : Elmt_Id;
2550 Priv_Sub : Entity_Id;
2552 procedure Preserve_Full_Attributes (Priv : Entity_Id; Full : Entity_Id);
2553 -- Copy to the private declaration the attributes of the full view that
2554 -- need to be available for the partial view also.
2556 function Type_In_Use (T : Entity_Id) return Boolean;
2557 -- Check whether type or base type appear in an active use_type clause
2559 ------------------------------
2560 -- Preserve_Full_Attributes --
2561 ------------------------------
2563 procedure Preserve_Full_Attributes
2564 (Priv : Entity_Id;
2565 Full : Entity_Id)
2567 Full_Base : constant Entity_Id := Base_Type (Full);
2568 Priv_Is_Base_Type : constant Boolean := Is_Base_Type (Priv);
2570 begin
2571 Set_Size_Info (Priv, Full);
2572 Set_RM_Size (Priv, RM_Size (Full));
2573 Set_Size_Known_At_Compile_Time
2574 (Priv, Size_Known_At_Compile_Time (Full));
2575 Set_Is_Volatile (Priv, Is_Volatile (Full));
2576 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
2577 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full));
2578 Set_Is_Ada_2012_Only (Priv, Is_Ada_2012_Only (Full));
2579 Set_Has_Pragma_Unmodified (Priv, Has_Pragma_Unmodified (Full));
2580 Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced (Full));
2581 Set_Has_Pragma_Unreferenced_Objects
2582 (Priv, Has_Pragma_Unreferenced_Objects
2583 (Full));
2584 if Is_Unchecked_Union (Full) then
2585 Set_Is_Unchecked_Union (Base_Type (Priv));
2586 end if;
2587 -- Why is atomic not copied here ???
2589 if Referenced (Full) then
2590 Set_Referenced (Priv);
2591 end if;
2593 if Priv_Is_Base_Type then
2594 Set_Is_Controlled (Priv, Is_Controlled (Full_Base));
2595 Set_Finalize_Storage_Only
2596 (Priv, Finalize_Storage_Only (Full_Base));
2597 Set_Has_Controlled_Component
2598 (Priv, Has_Controlled_Component (Full_Base));
2600 Propagate_Concurrent_Flags (Priv, Base_Type (Full));
2601 end if;
2603 Set_Freeze_Node (Priv, Freeze_Node (Full));
2605 -- Propagate invariant-related attributes from the base type of the
2606 -- full view to the full view and vice versa. This may seem strange,
2607 -- but is necessary depending on which type triggered the generation
2608 -- of the invariant procedure body. As a result, both the full view
2609 -- and its base type carry the same invariant-related information.
2611 Propagate_Invariant_Attributes (Full, From_Typ => Full_Base);
2612 Propagate_Invariant_Attributes (Full_Base, From_Typ => Full);
2614 -- Propagate invariant-related attributes from the full view to the
2615 -- private view.
2617 Propagate_Invariant_Attributes (Priv, From_Typ => Full);
2619 if Is_Tagged_Type (Priv)
2620 and then Is_Tagged_Type (Full)
2621 and then not Error_Posted (Full)
2622 then
2623 if Is_Tagged_Type (Priv) then
2625 -- If the type is tagged, the tag itself must be available on
2626 -- the partial view, for expansion purposes.
2628 Set_First_Entity (Priv, First_Entity (Full));
2630 -- If there are discriminants in the partial view, these remain
2631 -- visible. Otherwise only the tag itself is visible, and there
2632 -- are no nameable components in the partial view.
2634 if No (Last_Entity (Priv)) then
2635 Set_Last_Entity (Priv, First_Entity (Priv));
2636 end if;
2637 end if;
2639 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2641 if Has_Discriminants (Full) then
2642 Set_Discriminant_Constraint (Priv,
2643 Discriminant_Constraint (Full));
2644 end if;
2645 end if;
2646 end Preserve_Full_Attributes;
2648 -----------------
2649 -- Type_In_Use --
2650 -----------------
2652 function Type_In_Use (T : Entity_Id) return Boolean is
2653 begin
2654 return Scope (Base_Type (T)) = P
2655 and then (In_Use (T) or else In_Use (Base_Type (T)));
2656 end Type_In_Use;
2658 -- Start of processing for Uninstall_Declarations
2660 begin
2661 Id := First_Entity (P);
2662 while Present (Id) and then Id /= First_Private_Entity (P) loop
2663 if Debug_Flag_E then
2664 Write_Str ("unlinking visible entity ");
2665 Write_Int (Int (Id));
2666 Write_Eol;
2667 end if;
2669 -- On exit from the package scope, we must preserve the visibility
2670 -- established by use clauses in the current scope. Two cases:
2672 -- a) If the entity is an operator, it may be a primitive operator of
2673 -- a type for which there is a visible use-type clause.
2675 -- b) for other entities, their use-visibility is determined by a
2676 -- visible use clause for the package itself. For a generic instance,
2677 -- the instantiation of the formals appears in the visible part,
2678 -- but the formals are private and remain so.
2680 if Ekind (Id) = E_Function
2681 and then Is_Operator_Symbol_Name (Chars (Id))
2682 and then not Is_Hidden (Id)
2683 and then not Error_Posted (Id)
2684 then
2685 Set_Is_Potentially_Use_Visible (Id,
2686 In_Use (P)
2687 or else Type_In_Use (Etype (Id))
2688 or else Type_In_Use (Etype (First_Formal (Id)))
2689 or else (Present (Next_Formal (First_Formal (Id)))
2690 and then
2691 Type_In_Use
2692 (Etype (Next_Formal (First_Formal (Id))))));
2693 else
2694 if In_Use (P) and then not Is_Hidden (Id) then
2696 -- A child unit of a use-visible package remains use-visible
2697 -- only if it is itself a visible child unit. Otherwise it
2698 -- would remain visible in other contexts where P is use-
2699 -- visible, because once compiled it stays in the entity list
2700 -- of its parent unit.
2702 if Is_Child_Unit (Id) then
2703 Set_Is_Potentially_Use_Visible
2704 (Id, Is_Visible_Lib_Unit (Id));
2705 else
2706 Set_Is_Potentially_Use_Visible (Id);
2707 end if;
2709 else
2710 Set_Is_Potentially_Use_Visible (Id, False);
2711 end if;
2712 end if;
2714 -- Local entities are not immediately visible outside of the package
2716 Set_Is_Immediately_Visible (Id, False);
2718 -- If this is a private type with a full view (for example a local
2719 -- subtype of a private type declared elsewhere), ensure that the
2720 -- full view is also removed from visibility: it may be exposed when
2721 -- swapping views in an instantiation. Similarly, ensure that the
2722 -- use-visibility is properly set on both views.
2724 if Is_Type (Id) and then Present (Full_View (Id)) then
2725 Set_Is_Immediately_Visible (Full_View (Id), False);
2726 Set_Is_Potentially_Use_Visible (Full_View (Id),
2727 Is_Potentially_Use_Visible (Id));
2728 end if;
2730 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2731 Check_Abstract_Overriding (Id);
2732 Check_Conventions (Id);
2733 end if;
2735 if Ekind_In (Id, E_Private_Type, E_Limited_Private_Type)
2736 and then No (Full_View (Id))
2737 and then not Is_Generic_Type (Id)
2738 and then not Is_Derived_Type (Id)
2739 then
2740 Error_Msg_N ("missing full declaration for private type&", Id);
2742 elsif Ekind (Id) = E_Record_Type_With_Private
2743 and then not Is_Generic_Type (Id)
2744 and then No (Full_View (Id))
2745 then
2746 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
2747 Error_Msg_N ("missing full declaration for private type&", Id);
2748 else
2749 Error_Msg_N
2750 ("missing full declaration for private extension", Id);
2751 end if;
2753 -- Case of constant, check for deferred constant declaration with
2754 -- no full view. Likely just a matter of a missing expression, or
2755 -- accidental use of the keyword constant.
2757 elsif Ekind (Id) = E_Constant
2759 -- OK if constant value present
2761 and then No (Constant_Value (Id))
2763 -- OK if full view present
2765 and then No (Full_View (Id))
2767 -- OK if imported, since that provides the completion
2769 and then not Is_Imported (Id)
2771 -- OK if object declaration replaced by renaming declaration as
2772 -- a result of OK_To_Rename processing (e.g. for concatenation)
2774 and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration
2776 -- OK if object declaration with the No_Initialization flag set
2778 and then not (Nkind (Parent (Id)) = N_Object_Declaration
2779 and then No_Initialization (Parent (Id)))
2780 then
2781 -- If no private declaration is present, we assume the user did
2782 -- not intend a deferred constant declaration and the problem
2783 -- is simply that the initializing expression is missing.
2785 if not Has_Private_Declaration (Etype (Id)) then
2787 -- We assume that the user did not intend a deferred constant
2788 -- declaration, and the expression is just missing.
2790 Error_Msg_N
2791 ("constant declaration requires initialization expression",
2792 Parent (Id));
2794 if Is_Limited_Type (Etype (Id)) then
2795 Error_Msg_N
2796 ("\if variable intended, remove CONSTANT from declaration",
2797 Parent (Id));
2798 end if;
2800 -- Otherwise if a private declaration is present, then we are
2801 -- missing the full declaration for the deferred constant.
2803 else
2804 Error_Msg_N
2805 ("missing full declaration for deferred constant (RM 7.4)",
2806 Id);
2808 if Is_Limited_Type (Etype (Id)) then
2809 Error_Msg_N
2810 ("\if variable intended, remove CONSTANT from declaration",
2811 Parent (Id));
2812 end if;
2813 end if;
2814 end if;
2816 Next_Entity (Id);
2817 end loop;
2819 -- If the specification was installed as the parent of a public child
2820 -- unit, the private declarations were not installed, and there is
2821 -- nothing to do.
2823 if not In_Private_Part (P) then
2824 return;
2825 else
2826 Set_In_Private_Part (P, False);
2827 end if;
2829 -- Make private entities invisible and exchange full and private
2830 -- declarations for private types. Id is now the first private entity
2831 -- in the package.
2833 while Present (Id) loop
2834 if Debug_Flag_E then
2835 Write_Str ("unlinking private entity ");
2836 Write_Int (Int (Id));
2837 Write_Eol;
2838 end if;
2840 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2841 Check_Abstract_Overriding (Id);
2842 Check_Conventions (Id);
2843 end if;
2845 Set_Is_Immediately_Visible (Id, False);
2847 if Is_Private_Base_Type (Id) and then Present (Full_View (Id)) then
2848 Full := Full_View (Id);
2850 -- If the partial view is not declared in the visible part of the
2851 -- package (as is the case when it is a type derived from some
2852 -- other private type in the private part of the current package),
2853 -- no exchange takes place.
2855 if No (Parent (Id))
2856 or else List_Containing (Parent (Id)) /=
2857 Visible_Declarations (Specification (Decl))
2858 then
2859 goto Next_Id;
2860 end if;
2862 -- The entry in the private part points to the full declaration,
2863 -- which is currently visible. Exchange them so only the private
2864 -- type declaration remains accessible, and link private and full
2865 -- declaration in the opposite direction. Before the actual
2866 -- exchange, we copy back attributes of the full view that must
2867 -- be available to the partial view too.
2869 Preserve_Full_Attributes (Id, Full);
2871 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
2873 -- The following test may be redundant, as this is already
2874 -- diagnosed in sem_ch3. ???
2876 if not Is_Definite_Subtype (Full)
2877 and then Is_Definite_Subtype (Id)
2878 then
2879 Error_Msg_Sloc := Sloc (Parent (Id));
2880 Error_Msg_NE
2881 ("full view of& not compatible with declaration#", Full, Id);
2882 end if;
2884 -- Swap out the subtypes and derived types of Id that
2885 -- were compiled in this scope, or installed previously
2886 -- by Install_Private_Declarations.
2888 -- Before we do the swap, we verify the presence of the Full_View
2889 -- field which may be empty due to a swap by a previous call to
2890 -- End_Package_Scope (e.g. from the freezing mechanism).
2892 Priv_Elmt := First_Elmt (Private_Dependents (Id));
2893 while Present (Priv_Elmt) loop
2894 Priv_Sub := Node (Priv_Elmt);
2896 if Present (Full_View (Priv_Sub)) then
2897 if Scope (Priv_Sub) = P
2898 or else not In_Open_Scopes (Scope (Priv_Sub))
2899 then
2900 Set_Is_Immediately_Visible (Priv_Sub, False);
2901 end if;
2903 if Is_Visible_Dependent (Priv_Sub) then
2904 Preserve_Full_Attributes
2905 (Priv_Sub, Full_View (Priv_Sub));
2906 Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub));
2907 Exchange_Declarations (Priv_Sub);
2908 end if;
2909 end if;
2911 Next_Elmt (Priv_Elmt);
2912 end loop;
2914 -- Now restore the type itself to its private view
2916 Exchange_Declarations (Id);
2918 -- If we have installed an underlying full view for a type derived
2919 -- from a private type in a child unit, restore the proper views
2920 -- of private and full view. See corresponding code in
2921 -- Install_Private_Declarations.
2923 -- After the exchange, Full denotes the private type in the
2924 -- visible part of the package.
2926 if Is_Private_Base_Type (Full)
2927 and then Present (Full_View (Full))
2928 and then Present (Underlying_Full_View (Full))
2929 and then In_Package_Body (Current_Scope)
2930 then
2931 Set_Full_View (Full, Underlying_Full_View (Full));
2932 Set_Underlying_Full_View (Full, Empty);
2933 end if;
2935 elsif Ekind (Id) = E_Incomplete_Type
2936 and then Comes_From_Source (Id)
2937 and then No (Full_View (Id))
2938 then
2939 -- Mark Taft amendment types. Verify that there are no primitive
2940 -- operations declared for the type (3.10.1(9)).
2942 Set_Has_Completion_In_Body (Id);
2944 declare
2945 Elmt : Elmt_Id;
2946 Subp : Entity_Id;
2948 begin
2949 Elmt := First_Elmt (Private_Dependents (Id));
2950 while Present (Elmt) loop
2951 Subp := Node (Elmt);
2953 -- Is_Primitive is tested because there can be cases where
2954 -- nonprimitive subprograms (in nested packages) are added
2955 -- to the Private_Dependents list.
2957 if Is_Overloadable (Subp) and then Is_Primitive (Subp) then
2958 Error_Msg_NE
2959 ("type& must be completed in the private part",
2960 Parent (Subp), Id);
2962 -- The result type of an access-to-function type cannot be a
2963 -- Taft-amendment type, unless the version is Ada 2012 or
2964 -- later (see AI05-151).
2966 elsif Ada_Version < Ada_2012
2967 and then Ekind (Subp) = E_Subprogram_Type
2968 then
2969 if Etype (Subp) = Id
2970 or else
2971 (Is_Class_Wide_Type (Etype (Subp))
2972 and then Etype (Etype (Subp)) = Id)
2973 then
2974 Error_Msg_NE
2975 ("type& must be completed in the private part",
2976 Associated_Node_For_Itype (Subp), Id);
2977 end if;
2978 end if;
2980 Next_Elmt (Elmt);
2981 end loop;
2982 end;
2984 elsif not Is_Child_Unit (Id)
2985 and then (not Is_Private_Type (Id) or else No (Full_View (Id)))
2986 then
2987 Set_Is_Hidden (Id);
2988 Set_Is_Potentially_Use_Visible (Id, False);
2989 end if;
2991 <<Next_Id>>
2992 Next_Entity (Id);
2993 end loop;
2994 end Uninstall_Declarations;
2996 ------------------------
2997 -- Unit_Requires_Body --
2998 ------------------------
3000 function Unit_Requires_Body
3001 (Pack_Id : Entity_Id;
3002 Do_Abstract_States : Boolean := False) return Boolean
3004 E : Entity_Id;
3006 Requires_Body : Boolean := False;
3007 -- Flag set when the unit has at least one construct that requries
3008 -- completion in a body.
3010 begin
3011 -- Imported entity never requires body. Right now, only subprograms can
3012 -- be imported, but perhaps in the future we will allow import of
3013 -- packages.
3015 if Is_Imported (Pack_Id) then
3016 return False;
3018 -- Body required if library package with pragma Elaborate_Body
3020 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3021 return True;
3023 -- Body required if subprogram
3025 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3026 return True;
3028 -- Treat a block as requiring a body
3030 elsif Ekind (Pack_Id) = E_Block then
3031 return True;
3033 elsif Ekind (Pack_Id) = E_Package
3034 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3035 and then Present (Generic_Parent (Parent (Pack_Id)))
3036 then
3037 declare
3038 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3039 begin
3040 if Has_Pragma_Elaborate_Body (G_P) then
3041 return True;
3042 end if;
3043 end;
3044 end if;
3046 -- Traverse the entity chain of the package and look for constructs that
3047 -- require a completion in a body.
3049 E := First_Entity (Pack_Id);
3050 while Present (E) loop
3052 -- Skip abstract states because their completion depends on several
3053 -- criteria (see below).
3055 if Ekind (E) = E_Abstract_State then
3056 null;
3058 elsif Requires_Completion_In_Body
3059 (E, Pack_Id, Do_Abstract_States)
3060 then
3061 Requires_Body := True;
3062 exit;
3063 end if;
3065 Next_Entity (E);
3066 end loop;
3068 -- A [generic] package that defines at least one non-null abstract state
3069 -- requires a completion only when at least one other construct requires
3070 -- a completion in a body (SPARK RM 7.1.4(4) and (6)). This check is not
3071 -- performed if the caller requests this behavior.
3073 if Do_Abstract_States
3074 and then Ekind_In (Pack_Id, E_Generic_Package, E_Package)
3075 and then Has_Non_Null_Abstract_State (Pack_Id)
3076 and then Requires_Body
3077 then
3078 return True;
3079 end if;
3081 return Requires_Body;
3082 end Unit_Requires_Body;
3084 -----------------------------
3085 -- Unit_Requires_Body_Info --
3086 -----------------------------
3088 procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id) is
3089 E : Entity_Id;
3091 begin
3092 -- An imported entity never requires body. Right now, only subprograms
3093 -- can be imported, but perhaps in the future we will allow import of
3094 -- packages.
3096 if Is_Imported (Pack_Id) then
3097 return;
3099 -- Body required if library package with pragma Elaborate_Body
3101 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3102 Error_Msg_N ("info: & requires body (Elaborate_Body)?Y?", Pack_Id);
3104 -- Body required if subprogram
3106 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3107 Error_Msg_N ("info: & requires body (subprogram case)?Y?", Pack_Id);
3109 -- Body required if generic parent has Elaborate_Body
3111 elsif Ekind (Pack_Id) = E_Package
3112 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3113 and then Present (Generic_Parent (Parent (Pack_Id)))
3114 then
3115 declare
3116 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3117 begin
3118 if Has_Pragma_Elaborate_Body (G_P) then
3119 Error_Msg_N
3120 ("info: & requires body (generic parent Elaborate_Body)?Y?",
3121 Pack_Id);
3122 end if;
3123 end;
3125 -- A [generic] package that introduces at least one non-null abstract
3126 -- state requires completion. However, there is a separate rule that
3127 -- requires that such a package have a reason other than this for a
3128 -- body being required (if necessary a pragma Elaborate_Body must be
3129 -- provided). If Ignore_Abstract_State is True, we don't do this check
3130 -- (so we can use Unit_Requires_Body to check for some other reason).
3132 elsif Ekind_In (Pack_Id, E_Generic_Package, E_Package)
3133 and then Present (Abstract_States (Pack_Id))
3134 and then not Is_Null_State
3135 (Node (First_Elmt (Abstract_States (Pack_Id))))
3136 then
3137 Error_Msg_N
3138 ("info: & requires body (non-null abstract state aspect)?Y?",
3139 Pack_Id);
3140 end if;
3142 -- Otherwise search entity chain for entity requiring completion
3144 E := First_Entity (Pack_Id);
3145 while Present (E) loop
3146 if Requires_Completion_In_Body (E, Pack_Id) then
3147 Error_Msg_Node_2 := E;
3148 Error_Msg_NE
3149 ("info: & requires body (& requires completion)?Y?", E, Pack_Id);
3150 end if;
3152 Next_Entity (E);
3153 end loop;
3154 end Unit_Requires_Body_Info;
3155 end Sem_Ch7;