MATCH: Improve `A CMP 0 ? A : -A` set of patterns to use bitwise_equal_p.
[official-gcc.git] / gcc / ada / sem_ch7.adb
blob1a49a53ad63b7c0e217c3e593b6f67641c5cf649
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-2023, 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 Einfo.Entities; use Einfo.Entities;
37 with Einfo.Utils; use Einfo.Utils;
38 with Elists; use Elists;
39 with Errout; use Errout;
40 with Exp_Disp; use Exp_Disp;
41 with Exp_Dist; use Exp_Dist;
42 with Exp_Dbug; use Exp_Dbug;
43 with Freeze; use Freeze;
44 with Ghost; use Ghost;
45 with Lib; use Lib;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
49 with Nlists; use Nlists;
50 with Opt; use Opt;
51 with Output; use Output;
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 Sinfo.Nodes; use Sinfo.Nodes;
71 with Sinfo.Utils; use Sinfo.Utils;
72 with Sinput; use Sinput;
73 with Style;
74 with Uintp; use Uintp;
75 with Warnsw; use Warnsw;
77 with GNAT.HTable;
79 package body Sem_Ch7 is
81 -----------------------------------
82 -- Handling private declarations --
83 -----------------------------------
85 -- The principle that each entity has a single defining occurrence clashes
86 -- with the presence of two separate definitions for private types: the
87 -- first is the private type declaration, and the second is the full type
88 -- declaration. It is important that all references to the type point to
89 -- the same defining occurrence, namely the first one. To enforce the two
90 -- separate views of the entity, the corresponding information is swapped
91 -- between the two declarations. Outside of the package, the defining
92 -- occurrence only contains the private declaration information, while in
93 -- the private part and the body of the package the defining occurrence
94 -- contains the full declaration. To simplify the swap, the defining
95 -- occurrence that currently holds the private declaration points to the
96 -- full declaration. During semantic processing the defining occurrence
97 -- also points to a list of private dependents, that is to say access types
98 -- or composite types whose designated types or component types are
99 -- subtypes or derived types of the private type in question. After the
100 -- full declaration has been seen, the private dependents are updated to
101 -- indicate that they have full definitions.
103 -----------------------
104 -- Local Subprograms --
105 -----------------------
107 procedure Analyze_Package_Body_Helper (N : Node_Id);
108 -- Does all the real work of Analyze_Package_Body
110 procedure Check_Anonymous_Access_Types
111 (Spec_Id : Entity_Id;
112 P_Body : Node_Id);
113 -- If the spec of a package has a limited_with_clause, it may declare
114 -- anonymous access types whose designated type is a limited view, such an
115 -- anonymous access return type for a function. This access type cannot be
116 -- elaborated in the spec itself, but it may need an itype reference if it
117 -- is used within a nested scope. In that case the itype reference is
118 -- created at the beginning of the corresponding package body and inserted
119 -- before other body declarations.
121 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id);
122 -- Called upon entering the private part of a public child package and the
123 -- body of a nested package, to potentially declare certain inherited
124 -- subprograms that were inherited by types in the visible part, but whose
125 -- declaration was deferred because the parent operation was private and
126 -- not visible at that point. These subprograms are located by traversing
127 -- the visible part declarations looking for non-private type extensions
128 -- and then examining each of the primitive operations of such types to
129 -- find those that were inherited but declared with a special internal
130 -- name. Each such operation is now declared as an operation with a normal
131 -- name (using the name of the parent operation) and replaces the previous
132 -- implicit operation in the primitive operations list of the type. If the
133 -- inherited private operation has been overridden, then it's replaced by
134 -- the overriding operation.
136 procedure Install_Package_Entity (Id : Entity_Id);
137 -- Supporting procedure for Install_{Visible,Private}_Declarations. Places
138 -- one entity on its visibility chain, and recurses on the visible part if
139 -- the entity is an inner package.
141 function Is_Private_Base_Type (E : Entity_Id) return Boolean;
142 -- True for a private type that is not a subtype
144 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean;
145 -- If the private dependent is a private type whose full view is derived
146 -- from the parent type, its full properties are revealed only if we are in
147 -- the immediate scope of the private dependent. Should this predicate be
148 -- tightened further???
150 function Requires_Completion_In_Body
151 (Id : Entity_Id;
152 Pack_Id : Entity_Id;
153 Do_Abstract_States : Boolean := False) return Boolean;
154 -- Subsidiary to routines Unit_Requires_Body and Unit_Requires_Body_Info.
155 -- Determine whether entity Id declared in package spec Pack_Id requires
156 -- completion in a package body. Flag Do_Abstract_Stats should be set when
157 -- abstract states are to be considered in the completion test.
159 procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id);
160 -- Outputs info messages showing why package Pack_Id requires a body. The
161 -- caller has checked that the switch requesting this information is set,
162 -- and that the package does indeed require a body.
164 --------------------------
165 -- Analyze_Package_Body --
166 --------------------------
168 procedure Analyze_Package_Body (N : Node_Id) is
169 Loc : constant Source_Ptr := Sloc (N);
171 begin
172 if Debug_Flag_C then
173 Write_Str ("==> package body ");
174 Write_Name (Chars (Defining_Entity (N)));
175 Write_Str (" from ");
176 Write_Location (Loc);
177 Write_Eol;
178 Indent;
179 end if;
181 -- The real work is split out into the helper, so it can do "return;"
182 -- without skipping the debug output.
184 Analyze_Package_Body_Helper (N);
186 if Debug_Flag_C then
187 Outdent;
188 Write_Str ("<== package body ");
189 Write_Name (Chars (Defining_Entity (N)));
190 Write_Str (" from ");
191 Write_Location (Loc);
192 Write_Eol;
193 end if;
194 end Analyze_Package_Body;
196 ------------------------------------------------------
197 -- Analyze_Package_Body_Helper Data and Subprograms --
198 ------------------------------------------------------
200 Entity_Table_Size : constant := 4093;
201 -- Number of headers in hash table
203 subtype Entity_Header_Num is Integer range 0 .. Entity_Table_Size - 1;
204 -- Range of headers in hash table
206 function Node_Hash (Id : Entity_Id) return Entity_Header_Num;
207 -- Simple hash function for Entity_Ids
209 package Subprogram_Table is new GNAT.Htable.Simple_HTable
210 (Header_Num => Entity_Header_Num,
211 Element => Boolean,
212 No_Element => False,
213 Key => Entity_Id,
214 Hash => Node_Hash,
215 Equal => "=");
216 -- Hash table to record which subprograms are referenced. It is declared
217 -- at library level to avoid elaborating it for every call to Analyze.
219 package Traversed_Table is new GNAT.Htable.Simple_HTable
220 (Header_Num => Entity_Header_Num,
221 Element => Boolean,
222 No_Element => False,
223 Key => Node_Id,
224 Hash => Node_Hash,
225 Equal => "=");
226 -- Hash table to record which nodes we have traversed, so we can avoid
227 -- traversing the same nodes repeatedly.
229 -----------------
230 -- Node_Hash --
231 -----------------
233 function Node_Hash (Id : Entity_Id) return Entity_Header_Num is
234 begin
235 return Entity_Header_Num (Id mod Entity_Table_Size);
236 end Node_Hash;
238 ---------------------------------
239 -- Analyze_Package_Body_Helper --
240 ---------------------------------
242 -- WARNING: This routine manages Ghost regions. Return statements must be
243 -- replaced by gotos which jump to the end of the routine and restore the
244 -- Ghost mode.
246 procedure Analyze_Package_Body_Helper (N : Node_Id) is
247 procedure Hide_Public_Entities (Decls : List_Id);
248 -- Attempt to hide all public entities found in declarative list Decls
249 -- by resetting their Is_Public flag to False depending on whether the
250 -- entities are not referenced by inlined or generic bodies. This kind
251 -- of processing is a conservative approximation and will still leave
252 -- entities externally visible if the package is not simple enough.
254 procedure Install_Composite_Operations (P : Entity_Id);
255 -- Composite types declared in the current scope may depend on types
256 -- that were private at the point of declaration, and whose full view
257 -- is now in scope. Indicate that the corresponding operations on the
258 -- composite type are available.
260 --------------------------
261 -- Hide_Public_Entities --
262 --------------------------
264 procedure Hide_Public_Entities (Decls : List_Id) is
265 function Has_Referencer
266 (Decls : List_Id;
267 In_Nested_Instance : Boolean;
268 Has_Outer_Referencer_Of_Non_Subprograms : Boolean) return Boolean;
269 -- A "referencer" is a construct which may reference a previous
270 -- declaration. Examine all declarations in list Decls in reverse
271 -- and determine whether one such referencer exists. All entities
272 -- in the range Last (Decls) .. Referencer are hidden from external
273 -- visibility. In_Nested_Instance is true if we are inside a package
274 -- instance that has a body.
276 function Scan_Subprogram_Ref (N : Node_Id) return Traverse_Result;
277 -- Determine whether a node denotes a reference to a subprogram
279 procedure Traverse_And_Scan_Subprogram_Refs is
280 new Traverse_Proc (Scan_Subprogram_Ref);
281 -- Subsidiary to routine Has_Referencer. Determine whether a node
282 -- contains references to a subprogram and record them.
283 -- WARNING: this is a very expensive routine as it performs a full
284 -- tree traversal.
286 procedure Scan_Subprogram_Refs (Node : Node_Id);
287 -- If we haven't already traversed Node, then mark and traverse it.
289 --------------------
290 -- Has_Referencer --
291 --------------------
293 function Has_Referencer
294 (Decls : List_Id;
295 In_Nested_Instance : Boolean;
296 Has_Outer_Referencer_Of_Non_Subprograms : Boolean) return Boolean
298 Has_Referencer_Of_Non_Subprograms : Boolean :=
299 Has_Outer_Referencer_Of_Non_Subprograms;
300 -- Set if an inlined subprogram body was detected as a referencer.
301 -- In this case, we do not return True immediately but keep hiding
302 -- subprograms from external visibility.
304 Decl : Node_Id;
305 Decl_Id : Entity_Id;
306 In_Instance : Boolean;
307 Spec : Node_Id;
308 Ignore : Boolean;
310 function Set_Referencer_Of_Non_Subprograms return Boolean;
311 -- Set Has_Referencer_Of_Non_Subprograms and call
312 -- Scan_Subprogram_Refs if relevant.
313 -- Return whether Scan_Subprogram_Refs was called.
315 ---------------------------------------
316 -- Set_Referencer_Of_Non_Subprograms --
317 ---------------------------------------
319 function Set_Referencer_Of_Non_Subprograms return Boolean is
320 begin
321 -- An inlined subprogram body acts as a referencer
322 -- unless we generate C code without -gnatn where we want
323 -- to favor generating static inline functions as much as
324 -- possible.
326 -- Note that we test Has_Pragma_Inline here in addition
327 -- to Is_Inlined. We are doing this for a client, since
328 -- we are computing which entities should be public, and
329 -- it is the client who will decide if actual inlining
330 -- should occur, so we need to catch all cases where the
331 -- subprogram may be inlined by the client.
333 if (not CCG_Mode
334 or else Has_Pragma_Inline_Always (Decl_Id)
335 or else Inline_Active)
336 and then (Is_Inlined (Decl_Id)
337 or else Has_Pragma_Inline (Decl_Id))
338 then
339 Has_Referencer_Of_Non_Subprograms := True;
341 -- Inspect the statements of the subprogram body
342 -- to determine whether the body references other
343 -- subprograms.
345 Scan_Subprogram_Refs (Decl);
346 return True;
347 else
348 return False;
349 end if;
350 end Set_Referencer_Of_Non_Subprograms;
352 begin
353 if No (Decls) then
354 return False;
355 end if;
357 -- Examine all declarations in reverse order, hiding all entities
358 -- from external visibility until a referencer has been found. The
359 -- algorithm recurses into nested packages.
361 Decl := Last (Decls);
362 while Present (Decl) loop
364 -- A stub is always considered a referencer
366 if Nkind (Decl) in N_Body_Stub then
367 return True;
369 -- Package declaration
371 elsif Nkind (Decl) = N_Package_Declaration then
372 Spec := Specification (Decl);
373 Decl_Id := Defining_Entity (Spec);
375 -- Inspect the declarations of a non-generic package to try
376 -- and hide more entities from external visibility.
378 if not Is_Generic_Unit (Decl_Id) then
379 if In_Nested_Instance then
380 In_Instance := True;
381 elsif Is_Generic_Instance (Decl_Id) then
382 In_Instance :=
383 Has_Completion (Decl_Id)
384 or else Unit_Requires_Body (Generic_Parent (Spec));
385 else
386 In_Instance := False;
387 end if;
389 if Has_Referencer (Private_Declarations (Spec),
390 In_Instance,
391 Has_Referencer_Of_Non_Subprograms)
392 or else
393 Has_Referencer (Visible_Declarations (Spec),
394 In_Instance,
395 Has_Referencer_Of_Non_Subprograms)
396 then
397 return True;
398 end if;
399 end if;
401 -- Package body
403 elsif Nkind (Decl) = N_Package_Body
404 and then Present (Corresponding_Spec (Decl))
405 then
406 Decl_Id := Corresponding_Spec (Decl);
408 -- A generic package body is a referencer. It would seem
409 -- that we only have to consider generics that can be
410 -- exported, i.e. where the corresponding spec is the
411 -- spec of the current package, but because of nested
412 -- instantiations, a fully private generic body may export
413 -- other private body entities. Furthermore, regardless of
414 -- whether there was a previous inlined subprogram, (an
415 -- instantiation of) the generic package may reference any
416 -- entity declared before it.
418 if Is_Generic_Unit (Decl_Id) then
419 return True;
421 -- Inspect the declarations of a non-generic package body to
422 -- try and hide more entities from external visibility.
424 elsif Has_Referencer (Declarations (Decl),
425 In_Nested_Instance
426 or else
427 Is_Generic_Instance (Decl_Id),
428 Has_Referencer_Of_Non_Subprograms)
429 then
430 return True;
431 end if;
433 -- Subprogram body
435 elsif Nkind (Decl) = N_Subprogram_Body then
436 if Present (Corresponding_Spec (Decl)) then
437 Decl_Id := Corresponding_Spec (Decl);
439 -- A generic subprogram body acts as a referencer
441 if Is_Generic_Unit (Decl_Id) then
442 return True;
443 end if;
445 Ignore := Set_Referencer_Of_Non_Subprograms;
447 -- Otherwise this is a stand alone subprogram body
449 else
450 Decl_Id := Defining_Entity (Decl);
452 -- See the N_Subprogram_Declaration case below
454 if not Set_Referencer_Of_Non_Subprograms
455 and then (not In_Nested_Instance
456 or else not Subprogram_Table.Get_First)
457 and then not Subprogram_Table.Get (Decl_Id)
458 then
459 -- We can reset Is_Public right away
460 Set_Is_Public (Decl_Id, False);
461 end if;
462 end if;
464 -- Freeze node
466 elsif Nkind (Decl) = N_Freeze_Entity then
467 declare
468 Discard : Boolean;
469 pragma Unreferenced (Discard);
470 begin
471 -- Inspect the actions to find references to subprograms.
472 -- We assume that the actions do not contain other kinds
473 -- of references and, therefore, we do not stop the scan
474 -- or set Has_Referencer_Of_Non_Subprograms here. Doing
475 -- it would pessimize common cases for which the actions
476 -- contain the declaration of an init procedure, since
477 -- such a procedure is automatically marked inline.
479 Discard :=
480 Has_Referencer (Actions (Decl),
481 In_Nested_Instance,
482 Has_Referencer_Of_Non_Subprograms);
483 end;
485 -- Exceptions, objects and renamings do not need to be public
486 -- if they are not followed by a construct which can reference
487 -- and export them.
489 elsif Nkind (Decl) in N_Exception_Declaration
490 | N_Object_Declaration
491 | N_Object_Renaming_Declaration
492 then
493 Decl_Id := Defining_Entity (Decl);
495 -- We cannot say anything for objects declared in nested
496 -- instances because instantiations are not done yet so the
497 -- bodies are not visible and could contain references to
498 -- them.
500 if not In_Nested_Instance
501 and then not Is_Imported (Decl_Id)
502 and then not Is_Exported (Decl_Id)
503 and then No (Interface_Name (Decl_Id))
504 and then not Has_Referencer_Of_Non_Subprograms
505 then
506 Set_Is_Public (Decl_Id, False);
507 end if;
509 -- Likewise for subprograms and renamings, but we work harder
510 -- for them to see whether they are referenced on an individual
511 -- basis by looking into the table of referenced subprograms.
513 elsif Nkind (Decl) in N_Subprogram_Declaration
514 | N_Subprogram_Renaming_Declaration
515 then
516 Decl_Id := Defining_Entity (Decl);
518 -- We cannot say anything for subprograms declared in nested
519 -- instances because instantiations are not done yet so the
520 -- bodies are not visible and could contain references to
521 -- them, except if we still have no subprograms at all which
522 -- are referenced by an inlined body.
524 if (not In_Nested_Instance
525 or else not Subprogram_Table.Get_First)
526 and then not Is_Imported (Decl_Id)
527 and then not Is_Exported (Decl_Id)
528 and then No (Interface_Name (Decl_Id))
529 and then not Subprogram_Table.Get (Decl_Id)
530 then
531 Set_Is_Public (Decl_Id, False);
532 end if;
534 -- For a subprogram renaming, if the entity is referenced,
535 -- then so is the renamed subprogram. But there is an issue
536 -- with generic bodies because instantiations are not done
537 -- yet and, therefore, cannot be scanned for referencers.
538 -- That's why we use an approximation and test that we have
539 -- at least one subprogram referenced by an inlined body
540 -- instead of precisely the entity of this renaming.
542 if Nkind (Decl) = N_Subprogram_Renaming_Declaration
543 and then Subprogram_Table.Get_First
544 and then Is_Entity_Name (Name (Decl))
545 and then Present (Entity (Name (Decl)))
546 and then Is_Subprogram (Entity (Name (Decl)))
547 then
548 Subprogram_Table.Set (Entity (Name (Decl)), True);
549 end if;
550 end if;
552 Prev (Decl);
553 end loop;
555 return Has_Referencer_Of_Non_Subprograms;
556 end Has_Referencer;
558 -------------------------
559 -- Scan_Subprogram_Ref --
560 -------------------------
562 function Scan_Subprogram_Ref (N : Node_Id) return Traverse_Result is
563 begin
564 -- Detect a reference of the form
565 -- Subp_Call
567 if Nkind (N) in N_Subprogram_Call
568 and then Is_Entity_Name (Name (N))
569 and then Present (Entity (Name (N)))
570 and then Is_Subprogram (Entity (Name (N)))
571 then
572 Subprogram_Table.Set (Entity (Name (N)), True);
574 -- Detect a reference of the form
575 -- Subp'Some_Attribute
577 elsif Nkind (N) = N_Attribute_Reference
578 and then Is_Entity_Name (Prefix (N))
579 and then Present (Entity (Prefix (N)))
580 and then Is_Subprogram (Entity (Prefix (N)))
581 then
582 Subprogram_Table.Set (Entity (Prefix (N)), True);
584 -- Constants can be substituted by their value in gigi, which may
585 -- contain a reference, so scan the value recursively.
587 elsif Is_Entity_Name (N)
588 and then Present (Entity (N))
589 and then Ekind (Entity (N)) = E_Constant
590 then
591 declare
592 Val : constant Node_Id := Constant_Value (Entity (N));
593 begin
594 if Present (Val)
595 and then not Compile_Time_Known_Value (Val)
596 then
597 Scan_Subprogram_Refs (Val);
598 end if;
599 end;
600 end if;
602 return OK;
603 end Scan_Subprogram_Ref;
605 --------------------------
606 -- Scan_Subprogram_Refs --
607 --------------------------
609 procedure Scan_Subprogram_Refs (Node : Node_Id) is
610 begin
611 if not Traversed_Table.Get (Node) then
612 Traversed_Table.Set (Node, True);
613 Traverse_And_Scan_Subprogram_Refs (Node);
614 end if;
615 end Scan_Subprogram_Refs;
617 -- Local variables
619 Discard : Boolean;
620 pragma Unreferenced (Discard);
622 -- Start of processing for Hide_Public_Entities
624 begin
625 -- The algorithm examines the top level declarations of a package
626 -- body in reverse looking for a construct that may export entities
627 -- declared prior to it. If such a scenario is encountered, then all
628 -- entities in the range Last (Decls) .. construct are hidden from
629 -- external visibility. Consider:
631 -- package Pack is
632 -- generic
633 -- package Gen is
634 -- end Gen;
635 -- end Pack;
637 -- package body Pack is
638 -- External_Obj : ...; -- (1)
640 -- package body Gen is -- (2)
641 -- ... External_Obj ... -- (3)
642 -- end Gen;
644 -- Local_Obj : ...; -- (4)
645 -- end Pack;
647 -- In this example Local_Obj (4) must not be externally visible as
648 -- it cannot be exported by anything in Pack. The body of generic
649 -- package Gen (2) on the other hand acts as a "referencer" and may
650 -- export anything declared before it. Since the compiler does not
651 -- perform flow analysis, it is not possible to determine precisely
652 -- which entities will be exported when Gen is instantiated. In the
653 -- example above External_Obj (1) is exported at (3), but this may
654 -- not always be the case. The algorithm takes a conservative stance
655 -- and leaves entity External_Obj public.
657 -- This very conservative algorithm is supplemented by a more precise
658 -- processing for inlined bodies. For them, we traverse the syntactic
659 -- tree and record which subprograms are actually referenced from it.
660 -- This makes it possible to compute a much smaller set of externally
661 -- visible subprograms in the absence of generic bodies, which can
662 -- have a significant impact on the inlining decisions made in the
663 -- back end and the removal of out-of-line bodies from the object
664 -- code. We do it only for inlined bodies because they are supposed
665 -- to be reasonably small and tree traversal is very expensive.
667 -- Note that even this special processing is not optimal for inlined
668 -- bodies, because we treat all inlined subprograms alike. An optimal
669 -- algorithm would require computing the transitive closure of the
670 -- inlined subprograms that can really be referenced from other units
671 -- in the source code.
673 -- We could extend this processing for inlined bodies and record all
674 -- entities, not just subprograms, referenced from them, which would
675 -- make it possible to compute a much smaller set of all externally
676 -- visible entities in the absence of generic bodies. But this would
677 -- mean implementing a more thorough tree traversal of the bodies,
678 -- i.e. not just syntactic, and the gain would very likely be worth
679 -- neither the hassle nor the slowdown of the compiler.
681 -- Finally, an important thing to be aware of is that, at this point,
682 -- instantiations are not done yet so we cannot directly see inlined
683 -- bodies coming from them. That's not catastrophic because only the
684 -- actual parameters of the instantiations matter here, and they are
685 -- present in the declarations list of the instantiated packages.
687 Traversed_Table.Reset;
688 Subprogram_Table.Reset;
689 Discard := Has_Referencer (Decls, False, False);
690 end Hide_Public_Entities;
692 ----------------------------------
693 -- Install_Composite_Operations --
694 ----------------------------------
696 procedure Install_Composite_Operations (P : Entity_Id) is
697 Id : Entity_Id;
699 begin
700 Id := First_Entity (P);
701 while Present (Id) loop
702 if Is_Type (Id)
703 and then (Is_Limited_Composite (Id)
704 or else Is_Private_Composite (Id))
705 and then No (Private_Component (Id))
706 then
707 Set_Is_Limited_Composite (Id, False);
708 Set_Is_Private_Composite (Id, False);
709 end if;
711 Next_Entity (Id);
712 end loop;
713 end Install_Composite_Operations;
715 -- Local variables
717 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
718 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
719 Saved_EA : constant Boolean := Expander_Active;
720 Saved_ISMP : constant Boolean :=
721 Ignore_SPARK_Mode_Pragmas_In_Instance;
722 -- Save the Ghost and SPARK mode-related data to restore on exit
724 Body_Id : Entity_Id;
725 HSS : Node_Id;
726 Last_Spec_Entity : Entity_Id;
727 New_N : Node_Id;
728 Pack_Decl : Node_Id;
729 Spec_Id : Entity_Id;
731 -- Start of processing for Analyze_Package_Body_Helper
733 begin
734 -- Find corresponding package specification, and establish the current
735 -- scope. The visible defining entity for the package is the defining
736 -- occurrence in the spec. On exit from the package body, all body
737 -- declarations are attached to the defining entity for the body, but
738 -- the later is never used for name resolution. In this fashion there
739 -- is only one visible entity that denotes the package.
741 -- Set Body_Id. Note that this will be reset to point to the generic
742 -- copy later on in the generic case.
744 Body_Id := Defining_Entity (N);
746 -- Body is body of package instantiation. Corresponding spec has already
747 -- been set.
749 if Present (Corresponding_Spec (N)) then
750 Spec_Id := Corresponding_Spec (N);
751 Pack_Decl := Unit_Declaration_Node (Spec_Id);
753 else
754 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
756 if Present (Spec_Id)
757 and then Is_Package_Or_Generic_Package (Spec_Id)
758 then
759 Pack_Decl := Unit_Declaration_Node (Spec_Id);
761 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
762 Error_Msg_N ("cannot supply body for package renaming", N);
763 return;
765 elsif Present (Corresponding_Body (Pack_Decl)) then
766 Error_Msg_N ("redefinition of package body", N);
767 return;
768 end if;
770 else
771 Error_Msg_N ("missing specification for package body", N);
772 return;
773 end if;
775 if Is_Package_Or_Generic_Package (Spec_Id)
776 and then (Scope (Spec_Id) = Standard_Standard
777 or else Is_Child_Unit (Spec_Id))
778 and then not Unit_Requires_Body (Spec_Id)
779 then
780 if Ada_Version = Ada_83 then
781 Error_Msg_N
782 ("optional package body (not allowed in Ada 95)??", N);
783 else
784 Error_Msg_N ("spec of this package does not allow a body", N);
785 Error_Msg_N ("\either remove the body or add pragma "
786 & "Elaborate_Body in the spec", N);
787 end if;
788 end if;
789 end if;
791 -- A [generic] package body freezes the contract of the nearest
792 -- enclosing package body and all other contracts encountered in
793 -- the same declarative part up to and excluding the package body:
795 -- package body Nearest_Enclosing_Package
796 -- with Refined_State => (State => Constit)
797 -- is
798 -- Constit : ...;
800 -- package body Freezes_Enclosing_Package_Body
801 -- with Refined_State => (State_2 => Constit_2)
802 -- is
803 -- Constit_2 : ...;
805 -- procedure Proc
806 -- with Refined_Depends => (Input => (Constit, Constit_2)) ...
808 -- This ensures that any annotations referenced by the contract of a
809 -- [generic] subprogram body declared within the current package body
810 -- are available. This form of freezing is decoupled from the usual
811 -- Freeze_xxx mechanism because it must also work in the context of
812 -- generics where normal freezing is disabled.
814 -- Only bodies coming from source should cause this type of freezing.
815 -- Instantiated generic bodies are excluded because their processing is
816 -- performed in a separate compilation pass which lacks enough semantic
817 -- information with respect to contract analysis. It is safe to suppress
818 -- the freezing of contracts in this case because this action already
819 -- took place at the end of the enclosing declarative part.
821 if Comes_From_Source (N)
822 and then not Is_Generic_Instance (Spec_Id)
823 then
824 Freeze_Previous_Contracts (N);
825 end if;
827 -- A package body is Ghost when the corresponding spec is Ghost. Set
828 -- the mode now to ensure that any nodes generated during analysis and
829 -- expansion are properly flagged as ignored Ghost.
831 Mark_And_Set_Ghost_Body (N, Spec_Id);
833 -- Deactivate expansion inside the body of ignored Ghost entities,
834 -- as this code will ultimately be ignored. This avoids requiring the
835 -- presence of run-time units which are not needed. Only do this for
836 -- user entities, as internally generated entities might still need
837 -- to be expanded (e.g. those generated for types).
839 if Present (Ignored_Ghost_Region)
840 and then Comes_From_Source (Body_Id)
841 then
842 Expander_Active := False;
843 end if;
845 -- If the body completes the initial declaration of a compilation unit
846 -- which is subject to pragma Elaboration_Checks, set the model of the
847 -- pragma because it applies to all parts of the unit.
849 Install_Elaboration_Model (Spec_Id);
851 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
852 Style.Check_Identifier (Body_Id, Spec_Id);
854 if Is_Child_Unit (Spec_Id) then
855 if Nkind (Parent (N)) /= N_Compilation_Unit then
856 Error_Msg_NE
857 ("body of child unit& cannot be an inner package", N, Spec_Id);
858 end if;
860 Set_Is_Child_Unit (Body_Id);
861 end if;
863 -- Generic package case
865 if Ekind (Spec_Id) = E_Generic_Package then
867 -- Disable expansion and perform semantic analysis on copy. The
868 -- unannotated body will be used in all instantiations.
870 Body_Id := Defining_Entity (N);
871 Mutate_Ekind (Body_Id, E_Package_Body);
872 Set_Scope (Body_Id, Scope (Spec_Id));
873 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
874 Set_Body_Entity (Spec_Id, Body_Id);
875 Set_Spec_Entity (Body_Id, Spec_Id);
877 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
878 Rewrite (N, New_N);
880 -- Once the contents of the generic copy and the template are
881 -- swapped, do the same for their respective aspect specifications.
883 Exchange_Aspects (N, New_N);
885 -- Collect all contract-related source pragmas found within the
886 -- template and attach them to the contract of the package body.
887 -- This contract is used in the capture of global references within
888 -- annotations.
890 Create_Generic_Contract (N);
892 -- Update Body_Id to point to the copied node for the remainder of
893 -- the processing.
895 Body_Id := Defining_Entity (N);
896 Start_Generic;
897 end if;
899 -- The Body_Id is that of the copied node in the generic case, the
900 -- current node otherwise. Note that N was rewritten above, so we must
901 -- be sure to get the latest Body_Id value.
903 if Ekind (Body_Id) = E_Package then
904 Reinit_Field_To_Zero (Body_Id, F_Body_Needed_For_Inlining);
905 end if;
906 Mutate_Ekind (Body_Id, E_Package_Body);
907 Set_Body_Entity (Spec_Id, Body_Id);
908 Set_Spec_Entity (Body_Id, Spec_Id);
910 -- Defining name for the package body is not a visible entity: Only the
911 -- defining name for the declaration is visible.
913 Set_Etype (Body_Id, Standard_Void_Type);
914 Set_Scope (Body_Id, Scope (Spec_Id));
915 Set_Corresponding_Spec (N, Spec_Id);
916 Set_Corresponding_Body (Pack_Decl, Body_Id);
918 -- The body entity is not used for semantics or code generation, but
919 -- it is attached to the entity list of the enclosing scope to simplify
920 -- the listing of back-annotations for the types it main contain.
922 if Scope (Spec_Id) /= Standard_Standard then
923 Append_Entity (Body_Id, Scope (Spec_Id));
924 end if;
926 -- Indicate that we are currently compiling the body of the package
928 Set_In_Package_Body (Spec_Id);
929 Set_Has_Completion (Spec_Id);
930 Last_Spec_Entity := Last_Entity (Spec_Id);
932 if Has_Aspects (N) then
933 Analyze_Aspect_Specifications (N, Body_Id);
934 end if;
936 Push_Scope (Spec_Id);
938 -- Set SPARK_Mode only for non-generic package
940 if Ekind (Spec_Id) = E_Package then
941 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
942 Set_SPARK_Aux_Pragma (Body_Id, SPARK_Mode_Pragma);
943 Set_SPARK_Pragma_Inherited (Body_Id);
944 Set_SPARK_Aux_Pragma_Inherited (Body_Id);
946 -- A package body may be instantiated or inlined at a later pass.
947 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when
948 -- it applied to the package spec.
950 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
951 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
952 end if;
953 end if;
955 Set_Categorization_From_Pragmas (N);
957 Install_Visible_Declarations (Spec_Id);
958 Install_Private_Declarations (Spec_Id);
959 Install_Private_With_Clauses (Spec_Id);
960 Install_Composite_Operations (Spec_Id);
962 Check_Anonymous_Access_Types (Spec_Id, N);
964 if Ekind (Spec_Id) = E_Generic_Package then
965 Set_Use (Generic_Formal_Declarations (Pack_Decl));
966 end if;
968 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
969 Set_Use (Private_Declarations (Specification (Pack_Decl)));
971 -- This is a nested package, so it may be necessary to declare certain
972 -- inherited subprograms that are not yet visible because the parent
973 -- type's subprograms are now visible.
974 -- Note that for child units these operations were generated when
975 -- analyzing the package specification.
977 if Ekind (Scope (Spec_Id)) = E_Package
978 and then Scope (Spec_Id) /= Standard_Standard
979 and then not Is_Child_Unit (Spec_Id)
980 then
981 Declare_Inherited_Private_Subprograms (Spec_Id);
982 end if;
984 if Present (Declarations (N)) then
985 Analyze_Declarations (Declarations (N));
986 Inspect_Deferred_Constant_Completion (Declarations (N));
987 end if;
989 -- Verify that the SPARK_Mode of the body agrees with that of its spec
991 if Present (SPARK_Pragma (Body_Id)) then
992 if Present (SPARK_Aux_Pragma (Spec_Id)) then
993 if Get_SPARK_Mode_From_Annotation (SPARK_Aux_Pragma (Spec_Id)) =
995 and then
996 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
997 then
998 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
999 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
1000 Error_Msg_Sloc := Sloc (SPARK_Aux_Pragma (Spec_Id));
1001 Error_Msg_NE
1002 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
1003 end if;
1005 -- SPARK_Mode Off could complete no SPARK_Mode in a generic, either
1006 -- as specified in source code, or because SPARK_Mode On is ignored
1007 -- in an instance where the context is SPARK_Mode Off/Auto.
1009 elsif Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = Off
1010 and then (Is_Generic_Unit (Spec_Id) or else In_Instance)
1011 then
1012 null;
1014 else
1015 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
1016 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
1017 Error_Msg_Sloc := Sloc (Spec_Id);
1018 Error_Msg_NE
1019 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
1020 end if;
1021 end if;
1023 -- Analyze_Declarations has caused freezing of all types. Now generate
1024 -- bodies for RACW primitives and stream attributes, if any.
1026 if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then
1028 -- Attach subprogram bodies to support RACWs declared in spec
1030 Append_RACW_Bodies (Declarations (N), Spec_Id);
1031 Analyze_List (Declarations (N));
1032 end if;
1034 HSS := Handled_Statement_Sequence (N);
1036 if Present (HSS) then
1037 Process_End_Label (HSS, 't', Spec_Id);
1038 Analyze (HSS);
1040 -- Check that elaboration code in a preelaborable package body is
1041 -- empty other than null statements and labels (RM 10.2.1(6)).
1043 Validate_Null_Statement_Sequence (N);
1044 end if;
1046 Validate_Categorization_Dependency (N, Spec_Id);
1047 Check_Completion (Body_Id);
1049 -- Generate start of body reference. Note that we do this fairly late,
1050 -- because the call will use In_Extended_Main_Source_Unit as a check,
1051 -- and we want to make sure that Corresponding_Stub links are set
1053 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
1055 -- For a generic package, collect global references and mark them on
1056 -- the original body so that they are not resolved again at the point
1057 -- of instantiation.
1059 if Ekind (Spec_Id) /= E_Package then
1060 Save_Global_References (Original_Node (N));
1061 End_Generic;
1062 end if;
1064 -- The entities of the package body have so far been chained onto the
1065 -- declaration chain for the spec. That's been fine while we were in the
1066 -- body, since we wanted them to be visible, but now that we are leaving
1067 -- the package body, they are no longer visible, so we remove them from
1068 -- the entity chain of the package spec entity, and copy them to the
1069 -- entity chain of the package body entity, where they will never again
1070 -- be visible.
1072 if Present (Last_Spec_Entity) then
1073 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
1074 Set_Next_Entity (Last_Spec_Entity, Empty);
1075 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1076 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
1078 else
1079 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
1080 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1081 Set_First_Entity (Spec_Id, Empty);
1082 Set_Last_Entity (Spec_Id, Empty);
1083 end if;
1085 Update_Use_Clause_Chain;
1086 End_Package_Scope (Spec_Id);
1088 -- All entities declared in body are not visible
1090 declare
1091 E : Entity_Id;
1093 begin
1094 E := First_Entity (Body_Id);
1095 while Present (E) loop
1096 Set_Is_Immediately_Visible (E, False);
1097 Set_Is_Potentially_Use_Visible (E, False);
1098 Set_Is_Hidden (E);
1100 -- Child units may appear on the entity list (e.g. if they appear
1101 -- in the context of a subunit) but they are not body entities.
1103 if not Is_Child_Unit (E) then
1104 Set_Is_Package_Body_Entity (E);
1105 end if;
1107 Next_Entity (E);
1108 end loop;
1109 end;
1111 Check_References (Body_Id);
1113 -- For a generic unit, check that the formal parameters are referenced,
1114 -- and that local variables are used, as for regular packages.
1116 if Ekind (Spec_Id) = E_Generic_Package then
1117 Check_References (Spec_Id);
1118 end if;
1120 -- At this point all entities of the package body are externally visible
1121 -- to the linker as their Is_Public flag is set to True. This proactive
1122 -- approach is necessary because an inlined or a generic body for which
1123 -- code is generated in other units may need to see these entities. Cut
1124 -- down the number of global symbols that do not need public visibility
1125 -- as this has two beneficial effects:
1126 -- (1) It makes the compilation process more efficient.
1127 -- (2) It gives the code generator more leeway to optimize within each
1128 -- unit, especially subprograms.
1130 -- This is done only for top-level library packages or child units as
1131 -- the algorithm does a top-down traversal of the package body. This is
1132 -- also done for instances because instantiations are still pending by
1133 -- the time the enclosing package body is analyzed.
1135 if (Scope (Spec_Id) = Standard_Standard
1136 or else Is_Child_Unit (Spec_Id)
1137 or else Is_Generic_Instance (Spec_Id))
1138 and then not Is_Generic_Unit (Spec_Id)
1139 then
1140 Hide_Public_Entities (Declarations (N));
1141 end if;
1143 -- If expander is not active, then here is where we turn off the
1144 -- In_Package_Body flag, otherwise it is turned off at the end of the
1145 -- corresponding expansion routine. If this is an instance body, we need
1146 -- to qualify names of local entities, because the body may have been
1147 -- compiled as a preliminary to another instantiation.
1149 if not Expander_Active then
1150 Set_In_Package_Body (Spec_Id, False);
1152 if Is_Generic_Instance (Spec_Id)
1153 and then Operating_Mode = Generate_Code
1154 then
1155 Qualify_Entity_Names (N);
1156 end if;
1157 end if;
1159 if Present (Ignored_Ghost_Region) then
1160 Expander_Active := Saved_EA;
1161 end if;
1163 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
1164 Restore_Ghost_Region (Saved_GM, Saved_IGR);
1165 end Analyze_Package_Body_Helper;
1167 ---------------------------------
1168 -- Analyze_Package_Declaration --
1169 ---------------------------------
1171 procedure Analyze_Package_Declaration (N : Node_Id) is
1172 Id : constant Node_Id := Defining_Entity (N);
1174 Is_Comp_Unit : constant Boolean :=
1175 Nkind (Parent (N)) = N_Compilation_Unit;
1177 Body_Required : Boolean;
1178 -- True when this package declaration requires a corresponding body
1180 begin
1181 if Debug_Flag_C then
1182 Write_Str ("==> package spec ");
1183 Write_Name (Chars (Id));
1184 Write_Str (" from ");
1185 Write_Location (Sloc (N));
1186 Write_Eol;
1187 Indent;
1188 end if;
1190 Generate_Definition (Id);
1191 Enter_Name (Id);
1192 Mutate_Ekind (Id, E_Package);
1193 Set_Is_Not_Self_Hidden (Id);
1194 -- Needed early because of Set_Categorization_From_Pragmas below
1195 Set_Etype (Id, Standard_Void_Type);
1197 -- Set SPARK_Mode from context
1199 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
1200 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
1201 Set_SPARK_Pragma_Inherited (Id);
1202 Set_SPARK_Aux_Pragma_Inherited (Id);
1204 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
1205 -- the body of this package is instantiated or inlined later and out of
1206 -- context. The body uses this attribute to restore the value of the
1207 -- global flag.
1209 if Ignore_SPARK_Mode_Pragmas_In_Instance then
1210 Set_Ignore_SPARK_Mode_Pragmas (Id);
1211 end if;
1213 -- Analyze aspect specifications immediately, since we need to recognize
1214 -- things like Pure early enough to diagnose violations during analysis.
1216 if Has_Aspects (N) then
1217 Analyze_Aspect_Specifications (N, Id);
1218 end if;
1220 -- Ada 2005 (AI-217): Check if the package has been illegally named in
1221 -- a limited-with clause of its own context. In this case the error has
1222 -- been previously notified by Analyze_Context.
1224 -- limited with Pkg; -- ERROR
1225 -- package Pkg is ...
1227 if From_Limited_With (Id) then
1228 return;
1229 end if;
1231 Push_Scope (Id);
1233 Set_Is_Pure (Id, Is_Pure (Enclosing_Lib_Unit_Entity));
1234 Set_Categorization_From_Pragmas (N);
1236 Analyze (Specification (N));
1237 Validate_Categorization_Dependency (N, Id);
1239 -- Determine whether the package requires a body. Abstract states are
1240 -- intentionally ignored because they do require refinement which can
1241 -- only come in a body, but at the same time they do not force the need
1242 -- for a body on their own (SPARK RM 7.1.4(4) and 7.2.2(3)).
1244 Body_Required := Unit_Requires_Body (Id);
1246 if not Body_Required then
1248 -- If the package spec does not require an explicit body, then there
1249 -- are not entities requiring completion in the language sense. Call
1250 -- Check_Completion now to ensure that nested package declarations
1251 -- that require an implicit body get one. (In the case where a body
1252 -- is required, Check_Completion is called at the end of the body's
1253 -- declarative part.)
1255 Check_Completion;
1257 -- If the package spec does not require an explicit body, then all
1258 -- abstract states declared in nested packages cannot possibly get a
1259 -- proper refinement (SPARK RM 7.1.4(4) and SPARK RM 7.2.2(3)). This
1260 -- check is performed only when the compilation unit is the main
1261 -- unit to allow for modular SPARK analysis where packages do not
1262 -- necessarily have bodies.
1264 if Is_Comp_Unit then
1265 Check_State_Refinements
1266 (Context => N,
1267 Is_Main_Unit => Parent (N) = Cunit (Main_Unit));
1268 end if;
1270 -- For package declarations at the library level, warn about
1271 -- references to unset objects, which is straightforward for packages
1272 -- with no bodies. For packages with bodies this is more complicated,
1273 -- because some of the objects might be set between spec and body
1274 -- elaboration, in nested or child packages, etc. Note that the
1275 -- recursive calls in Check_References will handle nested package
1276 -- specifications.
1278 if Is_Library_Level_Entity (Id) then
1279 Check_References (Id);
1280 end if;
1281 end if;
1283 -- Set Body_Required indication on the compilation unit node
1285 if Is_Comp_Unit then
1286 Set_Body_Required (Parent (N), Body_Required);
1288 if Legacy_Elaboration_Checks and not Body_Required then
1289 Set_Suppress_Elaboration_Warnings (Id);
1290 end if;
1291 end if;
1293 End_Package_Scope (Id);
1295 -- For the declaration of a library unit that is a remote types package,
1296 -- check legality rules regarding availability of stream attributes for
1297 -- types that contain non-remote access values. This subprogram performs
1298 -- visibility tests that rely on the fact that we have exited the scope
1299 -- of Id.
1301 if Is_Comp_Unit then
1302 Validate_RT_RAT_Component (N);
1303 end if;
1305 if Debug_Flag_C then
1306 Outdent;
1307 Write_Str ("<== package spec ");
1308 Write_Name (Chars (Id));
1309 Write_Str (" from ");
1310 Write_Location (Sloc (N));
1311 Write_Eol;
1312 end if;
1313 end Analyze_Package_Declaration;
1315 -----------------------------------
1316 -- Analyze_Package_Specification --
1317 -----------------------------------
1319 -- Note that this code is shared for the analysis of generic package specs
1320 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
1322 procedure Analyze_Package_Specification (N : Node_Id) is
1323 Id : constant Entity_Id := Defining_Entity (N);
1324 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
1325 Vis_Decls : constant List_Id := Visible_Declarations (N);
1326 Priv_Decls : constant List_Id := Private_Declarations (N);
1327 E : Entity_Id;
1328 L : Entity_Id;
1329 Public_Child : Boolean;
1331 Private_With_Clauses_Installed : Boolean := False;
1332 -- In Ada 2005, private with_clauses are visible in the private part
1333 -- of a nested package, even if it appears in the public part of the
1334 -- enclosing package. This requires a separate step to install these
1335 -- private_with_clauses, and remove them at the end of the nested
1336 -- package.
1338 procedure Clear_Constants (Id : Entity_Id);
1339 -- Clears constant indications (Never_Set_In_Source, Constant_Value,
1340 -- and Is_True_Constant) on all variables that are entities of Id.
1341 -- A recursive call is made for all packages and generic packages.
1343 procedure Generate_Parent_References;
1344 -- For a child unit, generate references to parent units, for
1345 -- GNAT Studio navigation purposes.
1347 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
1348 -- Child and Unit are entities of compilation units. True if Child
1349 -- is a public child of Parent as defined in 10.1.1
1351 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
1352 -- Reject completion of an incomplete or private type declarations
1353 -- having a known discriminant part by an unchecked union.
1355 procedure Inspect_Untagged_Record_Completion (Decls : List_Id);
1356 -- Find out whether a nonlimited untagged record completion has got a
1357 -- primitive equality operator and, if so, make it so that it will be
1358 -- used as the predefined operator of the private view of the record.
1360 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
1361 -- Given the package entity of a generic package instantiation or
1362 -- formal package whose corresponding generic is a child unit, installs
1363 -- the private declarations of each of the child unit's parents.
1364 -- This has to be done at the point of entering the instance package's
1365 -- private part rather than being done in Sem_Ch12.Install_Parent
1366 -- (which is where the parents' visible declarations are installed).
1368 ---------------------
1369 -- Clear_Constants --
1370 ---------------------
1372 procedure Clear_Constants (Id : Entity_Id) is
1373 E : Entity_Id;
1375 begin
1376 -- Ignore package renamings, not interesting and they can cause self
1377 -- referential loops in the code below.
1379 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
1380 return;
1381 end if;
1383 -- Note: in the loop below, the check for Next_Entity pointing back
1384 -- to the package entity may seem odd, but it is needed, because a
1385 -- package can contain a renaming declaration to itself, and such
1386 -- renamings are generated automatically within package instances.
1388 E := First_Entity (Id);
1389 while Present (E) and then E /= Id loop
1390 if Ekind (E) = E_Variable then
1391 Set_Never_Set_In_Source (E, False);
1392 Set_Is_True_Constant (E, False);
1393 Set_Current_Value (E, Empty);
1394 Set_Is_Known_Null (E, False);
1395 Set_Last_Assignment (E, Empty);
1397 if not Can_Never_Be_Null (E) then
1398 Set_Is_Known_Non_Null (E, False);
1399 end if;
1401 elsif Is_Package_Or_Generic_Package (E) then
1402 Clear_Constants (E);
1403 end if;
1405 Next_Entity (E);
1406 end loop;
1407 end Clear_Constants;
1409 --------------------------------
1410 -- Generate_Parent_References --
1411 --------------------------------
1413 procedure Generate_Parent_References is
1414 Decl : constant Node_Id := Parent (N);
1416 begin
1417 if Id = Cunit_Entity (Main_Unit)
1418 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
1419 then
1420 Generate_Reference (Id, Scope (Id), 'k', False);
1422 elsif Nkind (Unit (Cunit (Main_Unit))) not in
1423 N_Subprogram_Body | N_Subunit
1424 then
1425 -- If current unit is an ancestor of main unit, generate a
1426 -- reference to its own parent.
1428 declare
1429 U : Node_Id;
1430 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
1432 begin
1433 if Nkind (Main_Spec) = N_Package_Body then
1434 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
1435 end if;
1437 U := Parent_Spec (Main_Spec);
1438 while Present (U) loop
1439 if U = Parent (Decl) then
1440 Generate_Reference (Id, Scope (Id), 'k', False);
1441 exit;
1443 elsif Nkind (Unit (U)) = N_Package_Body then
1444 exit;
1446 else
1447 U := Parent_Spec (Unit (U));
1448 end if;
1449 end loop;
1450 end;
1451 end if;
1452 end Generate_Parent_References;
1454 ---------------------
1455 -- Is_Public_Child --
1456 ---------------------
1458 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
1459 begin
1460 if not Is_Private_Descendant (Child) then
1461 return True;
1462 else
1463 if Child = Unit then
1464 return not Private_Present (
1465 Parent (Unit_Declaration_Node (Child)));
1466 else
1467 return Is_Public_Child (Scope (Child), Unit);
1468 end if;
1469 end if;
1470 end Is_Public_Child;
1472 ----------------------------------------
1473 -- Inspect_Unchecked_Union_Completion --
1474 ----------------------------------------
1476 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
1477 Decl : Node_Id;
1479 begin
1480 Decl := First (Decls);
1481 while Present (Decl) loop
1483 -- We are looking for an incomplete or private type declaration
1484 -- with a known_discriminant_part whose full view is an
1485 -- Unchecked_Union. The seemingly useless check with Is_Type
1486 -- prevents cascaded errors when routines defined only for type
1487 -- entities are called with non-type entities.
1489 if Nkind (Decl) in N_Incomplete_Type_Declaration
1490 | N_Private_Type_Declaration
1491 and then Is_Type (Defining_Identifier (Decl))
1492 and then Has_Discriminants (Defining_Identifier (Decl))
1493 and then Present (Full_View (Defining_Identifier (Decl)))
1494 and then
1495 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
1496 then
1497 Error_Msg_N
1498 ("completion of discriminated partial view "
1499 & "cannot be an unchecked union",
1500 Full_View (Defining_Identifier (Decl)));
1501 end if;
1503 Next (Decl);
1504 end loop;
1505 end Inspect_Unchecked_Union_Completion;
1507 ----------------------------------------
1508 -- Inspect_Untagged_Record_Completion --
1509 ----------------------------------------
1511 procedure Inspect_Untagged_Record_Completion (Decls : List_Id) is
1512 Decl : Node_Id;
1514 begin
1515 Decl := First (Decls);
1516 while Present (Decl) loop
1518 -- We are looking for a full type declaration of an untagged
1519 -- record with a private declaration and primitive operations.
1521 if Nkind (Decl) in N_Full_Type_Declaration
1522 and then Is_Record_Type (Defining_Identifier (Decl))
1523 and then not Is_Limited_Type (Defining_Identifier (Decl))
1524 and then not Is_Tagged_Type (Defining_Identifier (Decl))
1525 and then Has_Private_Declaration (Defining_Identifier (Decl))
1526 and then Has_Primitive_Operations (Defining_Identifier (Decl))
1527 then
1528 declare
1529 Prim_List : constant Elist_Id :=
1530 Collect_Primitive_Operations (Defining_Identifier (Decl));
1532 E : Entity_Id;
1533 Ne_Id : Entity_Id;
1534 Op_Decl : Node_Id;
1535 Op_Id : Entity_Id;
1536 Prim : Elmt_Id;
1538 begin
1539 Prim := First_Elmt (Prim_List);
1540 while Present (Prim) loop
1541 Op_Id := Node (Prim);
1542 Op_Decl := Declaration_Node (Op_Id);
1543 if Nkind (Op_Decl) in N_Subprogram_Specification then
1544 Op_Decl := Parent (Op_Decl);
1545 end if;
1547 -- We are looking for an equality operator immediately
1548 -- visible and declared in the private part followed by
1549 -- the synthesized inequality operator.
1551 if Is_User_Defined_Equality (Op_Id)
1552 and then Is_Immediately_Visible (Op_Id)
1553 and then List_Containing (Op_Decl) = Decls
1554 then
1555 Ne_Id := Next_Entity (Op_Id);
1556 pragma Assert (Ekind (Ne_Id) = E_Function
1557 and then Corresponding_Equality (Ne_Id) = Op_Id);
1559 E := First_Private_Entity (Id);
1561 -- Move them from the private part of the entity list
1562 -- up to the end of the visible part of the same list.
1564 Remove_Entity (Op_Id);
1565 Remove_Entity (Ne_Id);
1567 Link_Entities (Prev_Entity (E), Op_Id);
1568 Link_Entities (Op_Id, Ne_Id);
1569 Link_Entities (Ne_Id, E);
1571 -- And if the private part contains another equality
1572 -- operator, move the equality operator to after it
1573 -- in the homonym chain, so that all its next homonyms
1574 -- in the same scope, if any, also are in the visible
1575 -- part. This is relied upon to resolve expanded names
1576 -- in Collect_Interps for example.
1578 while Present (E) loop
1579 exit when Ekind (E) = E_Function
1580 and then Chars (E) = Name_Op_Eq;
1582 Next_Entity (E);
1583 end loop;
1585 if Present (E) then
1586 Remove_Homonym (Op_Id);
1588 Set_Homonym (Op_Id, Homonym (E));
1589 Set_Homonym (E, Op_Id);
1590 end if;
1592 exit;
1593 end if;
1595 Next_Elmt (Prim);
1596 end loop;
1597 end;
1598 end if;
1600 Next (Decl);
1601 end loop;
1602 end Inspect_Untagged_Record_Completion;
1604 -----------------------------------------
1605 -- Install_Parent_Private_Declarations --
1606 -----------------------------------------
1608 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
1609 Inst_Par : Entity_Id;
1610 Gen_Par : Entity_Id;
1611 Inst_Node : Node_Id;
1613 begin
1614 Inst_Par := Inst_Id;
1616 Gen_Par :=
1617 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
1618 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
1619 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
1621 if Nkind (Inst_Node) in
1622 N_Package_Instantiation | N_Formal_Package_Declaration
1623 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
1624 then
1625 Inst_Par := Entity (Prefix (Name (Inst_Node)));
1627 if Present (Renamed_Entity (Inst_Par)) then
1628 Inst_Par := Renamed_Entity (Inst_Par);
1629 end if;
1631 -- The instance may appear in a sibling generic unit, in
1632 -- which case the prefix must include the common (generic)
1633 -- ancestor, which is treated as a current instance.
1635 if Inside_A_Generic
1636 and then Ekind (Inst_Par) = E_Generic_Package
1637 then
1638 Gen_Par := Inst_Par;
1639 pragma Assert (In_Open_Scopes (Gen_Par));
1641 else
1642 Gen_Par :=
1643 Generic_Parent
1644 (Specification (Unit_Declaration_Node (Inst_Par)));
1645 end if;
1647 -- Install the private declarations and private use clauses
1648 -- of a parent instance of the child instance, unless the
1649 -- parent instance private declarations have already been
1650 -- installed earlier in Analyze_Package_Specification, which
1651 -- happens when a generic child is instantiated, and the
1652 -- instance is a child of the parent instance.
1654 -- Installing the use clauses of the parent instance twice
1655 -- is both unnecessary and wrong, because it would cause the
1656 -- clauses to be chained to themselves in the use clauses
1657 -- list of the scope stack entry. That in turn would cause
1658 -- an endless loop from End_Use_Clauses upon scope exit.
1660 -- The parent is now fully visible. It may be a hidden open
1661 -- scope if we are currently compiling some child instance
1662 -- declared within it, but while the current instance is being
1663 -- compiled the parent is immediately visible. In particular
1664 -- its entities must remain visible if a stack save/restore
1665 -- takes place through a call to Rtsfind.
1667 if Present (Gen_Par) then
1668 if not In_Private_Part (Inst_Par) then
1669 Install_Private_Declarations (Inst_Par);
1670 Set_Use (Private_Declarations
1671 (Specification
1672 (Unit_Declaration_Node (Inst_Par))));
1673 Set_Is_Hidden_Open_Scope (Inst_Par, False);
1674 end if;
1676 -- If we've reached the end of the generic instance parents,
1677 -- then finish off by looping through the nongeneric parents
1678 -- and installing their private declarations.
1680 -- If one of the non-generic parents is itself on the scope
1681 -- stack, do not install its private declarations: they are
1682 -- installed in due time when the private part of that parent
1683 -- is analyzed.
1685 else
1686 while Present (Inst_Par)
1687 and then Inst_Par /= Standard_Standard
1688 and then (not In_Open_Scopes (Inst_Par)
1689 or else not In_Private_Part (Inst_Par))
1690 loop
1691 if Nkind (Inst_Node) = N_Formal_Package_Declaration
1692 or else
1693 not Is_Ancestor_Package
1694 (Inst_Par, Cunit_Entity (Current_Sem_Unit))
1695 then
1696 Install_Private_Declarations (Inst_Par);
1697 Set_Use
1698 (Private_Declarations
1699 (Specification
1700 (Unit_Declaration_Node (Inst_Par))));
1701 Inst_Par := Scope (Inst_Par);
1702 else
1703 exit;
1704 end if;
1705 end loop;
1707 exit;
1708 end if;
1710 else
1711 exit;
1712 end if;
1713 end loop;
1714 end Install_Parent_Private_Declarations;
1716 -- Start of processing for Analyze_Package_Specification
1718 begin
1719 if Present (Vis_Decls) then
1720 Analyze_Declarations (Vis_Decls);
1721 end if;
1723 -- Inspect the entities defined in the package and ensure that all
1724 -- incomplete types have received full declarations. Build default
1725 -- initial condition and invariant procedures for all qualifying types.
1727 E := First_Entity (Id);
1728 while Present (E) loop
1730 -- Check on incomplete types
1732 -- AI05-0213: A formal incomplete type has no completion, and neither
1733 -- does the corresponding subtype in an instance.
1735 if Is_Incomplete_Type (E)
1736 and then No (Full_View (E))
1737 and then not Is_Generic_Type (E)
1738 and then not From_Limited_With (E)
1739 and then not Is_Generic_Actual_Type (E)
1740 then
1741 Error_Msg_N ("no declaration in visible part for incomplete}", E);
1742 end if;
1744 Next_Entity (E);
1745 end loop;
1747 if Is_Remote_Call_Interface (Id)
1748 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1749 then
1750 Validate_RCI_Declarations (Id);
1751 end if;
1753 -- Save global references in the visible declarations, before installing
1754 -- private declarations of parent unit if there is one, because the
1755 -- privacy status of types defined in the parent will change. This is
1756 -- only relevant for generic child units, but is done in all cases for
1757 -- uniformity.
1759 if Ekind (Id) = E_Generic_Package
1760 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1761 then
1762 declare
1763 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1764 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1766 begin
1767 -- Insert the freezing nodes after the visible declarations to
1768 -- ensure that we analyze its aspects; needed to ensure that
1769 -- global entities referenced in the aspects are properly handled.
1771 if Ada_Version >= Ada_2012
1772 and then Is_Non_Empty_List (Vis_Decls)
1773 and then Is_Empty_List (Priv_Decls)
1774 then
1775 Insert_List_After_And_Analyze
1776 (Last (Vis_Decls), Freeze_Entity (Id, Last (Vis_Decls)));
1777 end if;
1779 Set_Private_Declarations (Orig_Spec, Empty_List);
1780 Save_Global_References (Orig_Decl);
1781 Set_Private_Declarations (Orig_Spec, Save_Priv);
1782 end;
1783 end if;
1785 -- If package is a public child unit, then make the private declarations
1786 -- of the parent visible.
1788 Public_Child := False;
1790 declare
1791 Par : Entity_Id;
1792 Pack_Decl : Node_Id;
1793 Par_Spec : Node_Id;
1795 begin
1796 Par := Id;
1797 Par_Spec := Parent_Spec (Parent (N));
1799 -- If the package is formal package of an enclosing generic, it is
1800 -- transformed into a local generic declaration, and compiled to make
1801 -- its spec available. We need to retrieve the original generic to
1802 -- determine whether it is a child unit, and install its parents.
1804 if No (Par_Spec)
1805 and then
1806 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1807 then
1808 Par := Entity (Name (Original_Node (Parent (N))));
1809 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1810 end if;
1812 if Present (Par_Spec) then
1813 Generate_Parent_References;
1815 while Scope (Par) /= Standard_Standard
1816 and then Is_Public_Child (Id, Par)
1817 and then In_Open_Scopes (Par)
1818 loop
1819 Public_Child := True;
1820 Par := Scope (Par);
1821 Install_Private_Declarations (Par);
1822 Install_Private_With_Clauses (Par);
1823 Pack_Decl := Unit_Declaration_Node (Par);
1824 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1825 end loop;
1826 end if;
1827 end;
1829 if Is_Compilation_Unit (Id) then
1830 Install_Private_With_Clauses (Id);
1831 else
1832 -- The current compilation unit may include private with_clauses,
1833 -- which are visible in the private part of the current nested
1834 -- package, and have to be installed now. This is not done for
1835 -- nested instantiations, where the private with_clauses of the
1836 -- enclosing unit have no effect once the instantiation info is
1837 -- established and we start analyzing the package declaration.
1839 declare
1840 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1841 begin
1842 if Is_Package_Or_Generic_Package (Comp_Unit)
1843 and then not In_Private_Part (Comp_Unit)
1844 and then not In_Instance
1845 then
1846 Install_Private_With_Clauses (Comp_Unit);
1847 Private_With_Clauses_Installed := True;
1848 end if;
1849 end;
1850 end if;
1852 -- If this is a package associated with a generic instance or formal
1853 -- package, then the private declarations of each of the generic's
1854 -- parents must be installed at this point, but not if this is the
1855 -- abbreviated instance created to check a formal package, see the
1856 -- same condition in Analyze_Package_Instantiation.
1858 if Is_Generic_Instance (Id)
1859 and then not Is_Abbreviated_Instance (Id)
1860 then
1861 Install_Parent_Private_Declarations (Id);
1862 end if;
1864 -- Analyze private part if present. The flag In_Private_Part is reset
1865 -- in Uninstall_Declarations.
1867 L := Last_Entity (Id);
1869 if Present (Priv_Decls) then
1870 Set_In_Private_Part (Id);
1872 -- Upon entering a public child's private part, it may be necessary
1873 -- to declare subprograms that were derived in the package's visible
1874 -- part but not yet made visible.
1876 if Public_Child then
1877 Declare_Inherited_Private_Subprograms (Id);
1878 end if;
1880 Analyze_Declarations (Priv_Decls);
1882 -- Check the private declarations for incomplete deferred constants
1884 Inspect_Deferred_Constant_Completion (Priv_Decls);
1886 -- The first private entity is the immediate follower of the last
1887 -- visible entity, if there was one.
1889 if Present (L) then
1890 Set_First_Private_Entity (Id, Next_Entity (L));
1891 else
1892 Set_First_Private_Entity (Id, First_Entity (Id));
1893 end if;
1895 -- There may be inherited private subprograms that need to be declared,
1896 -- even in the absence of an explicit private part. If there are any
1897 -- public declarations in the package and the package is a public child
1898 -- unit, then an implicit private part is assumed.
1900 elsif Present (L) and then Public_Child then
1901 Set_In_Private_Part (Id);
1902 Declare_Inherited_Private_Subprograms (Id);
1903 Set_First_Private_Entity (Id, Next_Entity (L));
1904 end if;
1906 E := First_Entity (Id);
1907 while Present (E) loop
1909 -- Check rule of 3.6(11), which in general requires waiting till all
1910 -- full types have been seen.
1912 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1913 Check_Aliased_Component_Types (E);
1914 end if;
1916 -- Check preelaborable initialization for full type completing a
1917 -- private type when aspect Preelaborable_Initialization is True
1918 -- or is specified by Preelaborable_Initialization attributes
1919 -- (in the case of a private type in a generic unit). We pass
1920 -- the expression of the aspect (when present) to the parameter
1921 -- Preelab_Init_Expr to take into account the rule that presumes
1922 -- that subcomponents of generic formal types mentioned in the
1923 -- type's P_I aspect have preelaborable initialization (see
1924 -- AI12-0409 and RM 10.2.1(11.8/5)).
1926 if Is_Type (E) and then Must_Have_Preelab_Init (E) then
1927 declare
1928 PI_Aspect : constant Node_Id :=
1929 Find_Aspect
1930 (E, Aspect_Preelaborable_Initialization);
1931 PI_Expr : Node_Id := Empty;
1932 begin
1933 if Present (PI_Aspect) then
1934 PI_Expr := Expression (PI_Aspect);
1935 end if;
1937 if not Has_Preelaborable_Initialization
1938 (E, Preelab_Init_Expr => PI_Expr)
1939 then
1940 Error_Msg_N
1941 ("full view of & does not have "
1942 & "preelaborable initialization", E);
1943 end if;
1944 end;
1945 end if;
1947 -- Preanalyze class-wide conditions of dispatching primitives defined
1948 -- in nested packages. For library packages, class-wide pre- and
1949 -- postconditions are preanalyzed when the primitives are frozen
1950 -- (see Merge_Class_Conditions); for nested packages, the end of the
1951 -- package does not cause freezing (and hence they must be analyzed
1952 -- now to ensure the correct visibility of referenced entities).
1954 if not Is_Compilation_Unit (Id)
1955 and then Is_Dispatching_Operation (E)
1956 and then Present (Contract (E))
1957 then
1958 Preanalyze_Class_Conditions (E);
1959 end if;
1961 Next_Entity (E);
1962 end loop;
1964 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1965 -- declaration having a known_discriminant_part shall not be an
1966 -- unchecked union type.
1968 if Present (Vis_Decls) then
1969 Inspect_Unchecked_Union_Completion (Vis_Decls);
1970 end if;
1972 if Present (Priv_Decls) then
1973 Inspect_Unchecked_Union_Completion (Priv_Decls);
1974 end if;
1976 -- Implement AI12-0101 (which only removes a legality rule) and then
1977 -- AI05-0123 (which directly applies in the previously illegal case)
1978 -- in Ada 2012. Note that AI12-0101 is a binding interpretation.
1980 if Present (Priv_Decls) and then Ada_Version >= Ada_2012 then
1981 Inspect_Untagged_Record_Completion (Priv_Decls);
1982 end if;
1984 if Ekind (Id) = E_Generic_Package
1985 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1986 and then Present (Priv_Decls)
1987 then
1988 -- Save global references in private declarations, ignoring the
1989 -- visible declarations that were processed earlier.
1991 declare
1992 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1993 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1994 Save_Form : constant List_Id :=
1995 Generic_Formal_Declarations (Orig_Decl);
1997 begin
1998 -- Insert the freezing nodes after the private declarations to
1999 -- ensure that we analyze its aspects; needed to ensure that
2000 -- global entities referenced in the aspects are properly handled.
2002 if Ada_Version >= Ada_2012
2003 and then Is_Non_Empty_List (Priv_Decls)
2004 then
2005 Insert_List_After_And_Analyze
2006 (Last (Priv_Decls), Freeze_Entity (Id, Last (Priv_Decls)));
2007 end if;
2009 Set_Visible_Declarations (Orig_Spec, Empty_List);
2010 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
2011 Save_Global_References (Orig_Decl);
2012 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
2013 Set_Visible_Declarations (Orig_Spec, Save_Vis);
2014 end;
2015 end if;
2017 Process_End_Label (N, 'e', Id);
2019 -- Remove private_with_clauses of enclosing compilation unit, if they
2020 -- were installed.
2022 if Private_With_Clauses_Installed then
2023 Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
2024 end if;
2026 -- For the case of a library level package, we must go through all the
2027 -- entities clearing the indications that the value may be constant and
2028 -- not modified. Why? Because any client of this package may modify
2029 -- these values freely from anywhere. This also applies to any nested
2030 -- packages or generic packages.
2032 -- For now we unconditionally clear constants for packages that are
2033 -- instances of generic packages. The reason is that we do not have the
2034 -- body yet, and we otherwise think things are unreferenced when they
2035 -- are not. This should be fixed sometime (the effect is not terrible,
2036 -- we just lose some warnings, and also some cases of value propagation)
2037 -- ???
2039 if Is_Library_Level_Entity (Id)
2040 or else Is_Generic_Instance (Id)
2041 then
2042 Clear_Constants (Id);
2043 end if;
2045 -- Output relevant information as to why the package requires a body.
2046 -- Do not consider generated packages as this exposes internal symbols
2047 -- and leads to confusing messages.
2049 if List_Body_Required_Info
2050 and then In_Extended_Main_Source_Unit (Id)
2051 and then Unit_Requires_Body (Id)
2052 and then Comes_From_Source (Id)
2053 then
2054 Unit_Requires_Body_Info (Id);
2055 end if;
2057 -- Nested package specs that do not require bodies are not checked for
2058 -- ineffective use clauses due to the possibility of subunits. This is
2059 -- because at this stage it is impossible to tell whether there will be
2060 -- a separate body.
2062 if not Unit_Requires_Body (Id)
2063 and then Is_Compilation_Unit (Id)
2064 and then not Is_Private_Descendant (Id)
2065 then
2066 Update_Use_Clause_Chain;
2067 end if;
2068 end Analyze_Package_Specification;
2070 --------------------------------------
2071 -- Analyze_Private_Type_Declaration --
2072 --------------------------------------
2074 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
2075 Id : constant Entity_Id := Defining_Identifier (N);
2076 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
2078 begin
2079 Generate_Definition (Id);
2080 Set_Is_Pure (Id, PF);
2081 Reinit_Size_Align (Id);
2083 if not Is_Package_Or_Generic_Package (Current_Scope)
2084 or else In_Private_Part (Current_Scope)
2085 then
2086 Error_Msg_N ("invalid context for private declaration", N);
2087 end if;
2089 New_Private_Type (N, Id, N);
2090 Set_Depends_On_Private (Id);
2092 -- Set the SPARK mode from the current context
2094 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
2095 Set_SPARK_Pragma_Inherited (Id);
2097 if Has_Aspects (N) then
2098 Analyze_Aspect_Specifications (N, Id);
2099 end if;
2100 end Analyze_Private_Type_Declaration;
2102 ----------------------------------
2103 -- Check_Anonymous_Access_Types --
2104 ----------------------------------
2106 procedure Check_Anonymous_Access_Types
2107 (Spec_Id : Entity_Id;
2108 P_Body : Node_Id)
2110 E : Entity_Id;
2111 IR : Node_Id;
2113 begin
2114 -- Itype references are only needed by gigi, to force elaboration of
2115 -- itypes. In the absence of code generation, they are not needed.
2117 if not Expander_Active then
2118 return;
2119 end if;
2121 E := First_Entity (Spec_Id);
2122 while Present (E) loop
2123 if Ekind (E) = E_Anonymous_Access_Type
2124 and then From_Limited_With (E)
2125 then
2126 IR := Make_Itype_Reference (Sloc (P_Body));
2127 Set_Itype (IR, E);
2129 if No (Declarations (P_Body)) then
2130 Set_Declarations (P_Body, New_List (IR));
2131 else
2132 Prepend (IR, Declarations (P_Body));
2133 end if;
2134 end if;
2136 Next_Entity (E);
2137 end loop;
2138 end Check_Anonymous_Access_Types;
2140 -------------------------------------------
2141 -- Declare_Inherited_Private_Subprograms --
2142 -------------------------------------------
2144 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
2146 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
2147 -- Check whether an inherited subprogram S is an operation of an
2148 -- untagged derived type T.
2150 ---------------------
2151 -- Is_Primitive_Of --
2152 ---------------------
2154 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
2155 Formal : Entity_Id;
2157 begin
2158 -- If the full view is a scalar type, the type is the anonymous base
2159 -- type, but the operation mentions the first subtype, so check the
2160 -- signature against the base type.
2162 if Base_Type (Etype (S)) = Base_Type (T) then
2163 return True;
2165 else
2166 Formal := First_Formal (S);
2167 while Present (Formal) loop
2168 if Base_Type (Etype (Formal)) = Base_Type (T) then
2169 return True;
2170 end if;
2172 Next_Formal (Formal);
2173 end loop;
2175 return False;
2176 end if;
2177 end Is_Primitive_Of;
2179 -- Local variables
2181 E : Entity_Id;
2182 Op_List : Elist_Id;
2183 Op_Elmt : Elmt_Id;
2184 Op_Elmt_2 : Elmt_Id;
2185 Prim_Op : Entity_Id;
2186 New_Op : Entity_Id := Empty;
2187 Parent_Subp : Entity_Id;
2188 Tag : Entity_Id;
2190 -- Start of processing for Declare_Inherited_Private_Subprograms
2192 begin
2193 E := First_Entity (Id);
2194 while Present (E) loop
2196 -- If the entity is a nonprivate type extension whose parent type
2197 -- is declared in an open scope, then the type may have inherited
2198 -- operations that now need to be made visible. Ditto if the entity
2199 -- is a formal derived type in a child unit.
2201 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
2202 or else
2203 (Nkind (Parent (E)) = N_Private_Extension_Declaration
2204 and then Is_Generic_Type (E)))
2205 and then In_Open_Scopes (Scope (Etype (E)))
2206 and then Is_Base_Type (E)
2207 then
2208 if Is_Tagged_Type (E) then
2209 Op_List := Primitive_Operations (E);
2210 New_Op := Empty;
2211 Tag := First_Tag_Component (E);
2213 Op_Elmt := First_Elmt (Op_List);
2214 while Present (Op_Elmt) loop
2215 Prim_Op := Node (Op_Elmt);
2217 -- Search primitives that are implicit operations with an
2218 -- internal name whose parent operation has a normal name.
2220 if Present (Alias (Prim_Op))
2221 and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
2222 and then not Comes_From_Source (Prim_Op)
2223 and then Is_Internal_Name (Chars (Prim_Op))
2224 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
2225 then
2226 Parent_Subp := Alias (Prim_Op);
2228 -- Case 1: Check if the type has also an explicit
2229 -- overriding for this primitive.
2231 Op_Elmt_2 := Next_Elmt (Op_Elmt);
2232 while Present (Op_Elmt_2) loop
2234 -- Skip entities with attribute Interface_Alias since
2235 -- they are not overriding primitives (these entities
2236 -- link an interface primitive with their covering
2237 -- primitive)
2239 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
2240 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
2241 and then No (Interface_Alias (Node (Op_Elmt_2)))
2242 then
2243 -- The private inherited operation has been
2244 -- overridden by an explicit subprogram:
2245 -- replace the former by the latter.
2247 New_Op := Node (Op_Elmt_2);
2248 Replace_Elmt (Op_Elmt, New_Op);
2249 Remove_Elmt (Op_List, Op_Elmt_2);
2250 Set_Overridden_Operation (New_Op, Parent_Subp);
2251 Set_Is_Ada_2022_Only (New_Op,
2252 Is_Ada_2022_Only (Parent_Subp));
2254 -- We don't need to inherit its dispatching slot.
2255 -- Set_All_DT_Position has previously ensured that
2256 -- the same slot was assigned to the two primitives
2258 if Present (Tag)
2259 and then Present (DTC_Entity (New_Op))
2260 and then Present (DTC_Entity (Prim_Op))
2261 then
2262 pragma Assert
2263 (DT_Position (New_Op) = DT_Position (Prim_Op));
2264 null;
2265 end if;
2267 goto Next_Primitive;
2268 end if;
2270 Next_Elmt (Op_Elmt_2);
2271 end loop;
2273 -- Case 2: We have not found any explicit overriding and
2274 -- hence we need to declare the operation (i.e., make it
2275 -- visible).
2277 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2279 -- Inherit the dispatching slot if E is already frozen
2281 if Is_Frozen (E)
2282 and then Present (DTC_Entity (Alias (Prim_Op)))
2283 then
2284 Set_DTC_Entity_Value (E, New_Op);
2285 Set_DT_Position_Value (New_Op,
2286 DT_Position (Alias (Prim_Op)));
2287 end if;
2289 pragma Assert
2290 (Is_Dispatching_Operation (New_Op)
2291 and then Node (Last_Elmt (Op_List)) = New_Op);
2293 -- Substitute the new operation for the old one in the
2294 -- type's primitive operations list. Since the new
2295 -- operation was also just added to the end of list,
2296 -- the last element must be removed.
2298 -- (Question: is there a simpler way of declaring the
2299 -- operation, say by just replacing the name of the
2300 -- earlier operation, reentering it in the in the symbol
2301 -- table (how?), and marking it as private???)
2303 Replace_Elmt (Op_Elmt, New_Op);
2304 Remove_Last_Elmt (Op_List);
2305 end if;
2307 <<Next_Primitive>>
2308 Next_Elmt (Op_Elmt);
2309 end loop;
2311 -- Generate listing showing the contents of the dispatch table
2313 if Debug_Flag_ZZ then
2314 Write_DT (E);
2315 end if;
2317 else
2318 -- For untagged type, scan forward to locate inherited hidden
2319 -- operations.
2321 Prim_Op := Next_Entity (E);
2322 while Present (Prim_Op) loop
2323 if Is_Subprogram (Prim_Op)
2324 and then Present (Alias (Prim_Op))
2325 and then not Comes_From_Source (Prim_Op)
2326 and then Is_Internal_Name (Chars (Prim_Op))
2327 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
2328 and then Is_Primitive_Of (E, Prim_Op)
2329 then
2330 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2331 end if;
2333 Next_Entity (Prim_Op);
2335 -- Derived operations appear immediately after the type
2336 -- declaration (or the following subtype indication for
2337 -- a derived scalar type). Further declarations cannot
2338 -- include inherited operations of the type.
2340 exit when Present (Prim_Op)
2341 and then not Is_Overloadable (Prim_Op);
2342 end loop;
2343 end if;
2344 end if;
2346 Next_Entity (E);
2347 end loop;
2348 end Declare_Inherited_Private_Subprograms;
2350 -----------------------
2351 -- End_Package_Scope --
2352 -----------------------
2354 procedure End_Package_Scope (P : Entity_Id) is
2355 begin
2356 Uninstall_Declarations (P);
2357 Pop_Scope;
2358 end End_Package_Scope;
2360 ---------------------------
2361 -- Exchange_Declarations --
2362 ---------------------------
2364 procedure Exchange_Declarations (Id : Entity_Id) is
2365 Full_Id : constant Entity_Id := Full_View (Id);
2366 H1 : constant Entity_Id := Homonym (Id);
2367 Next1 : constant Entity_Id := Next_Entity (Id);
2368 H2 : Entity_Id;
2369 Next2 : Entity_Id;
2371 begin
2372 -- If missing full declaration for type, nothing to exchange
2374 if No (Full_Id) then
2375 return;
2376 end if;
2378 -- Otherwise complete the exchange, and preserve semantic links
2380 Next2 := Next_Entity (Full_Id);
2381 H2 := Homonym (Full_Id);
2383 -- Reset full declaration pointer to reflect the switched entities and
2384 -- readjust the next entity chains.
2386 Exchange_Entities (Id, Full_Id);
2388 Link_Entities (Id, Next1);
2389 Set_Homonym (Id, H1);
2391 Set_Full_View (Full_Id, Id);
2392 Link_Entities (Full_Id, Next2);
2393 Set_Homonym (Full_Id, H2);
2394 end Exchange_Declarations;
2396 ----------------------------
2397 -- Install_Package_Entity --
2398 ----------------------------
2400 procedure Install_Package_Entity (Id : Entity_Id) is
2401 begin
2402 if not Is_Internal (Id) then
2403 if Debug_Flag_E then
2404 Write_Str ("Install: ");
2405 Write_Name (Chars (Id));
2406 Write_Eol;
2407 end if;
2409 if Is_Child_Unit (Id) then
2410 null;
2412 -- Do not enter implicitly inherited non-overridden subprograms of
2413 -- a tagged type back into visibility if they have non-conformant
2414 -- homographs (Ada RM 8.3 12.3/2).
2416 elsif Is_Hidden_Non_Overridden_Subpgm (Id) then
2417 null;
2419 else
2420 Set_Is_Immediately_Visible (Id);
2421 end if;
2422 end if;
2423 end Install_Package_Entity;
2425 ----------------------------------
2426 -- Install_Private_Declarations --
2427 ----------------------------------
2429 procedure Install_Private_Declarations (P : Entity_Id) is
2430 Id : Entity_Id;
2431 Full : Entity_Id;
2432 Priv_Deps : Elist_Id;
2434 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
2435 -- When the full view of a private type is made available, we do the
2436 -- same for its private dependents under proper visibility conditions.
2437 -- When compiling a child unit this needs to be done recursively.
2439 -----------------------------
2440 -- Swap_Private_Dependents --
2441 -----------------------------
2443 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
2444 Cunit : Entity_Id;
2445 Deps : Elist_Id;
2446 Priv : Entity_Id;
2447 Priv_Elmt : Elmt_Id;
2448 Is_Priv : Boolean;
2450 begin
2451 Priv_Elmt := First_Elmt (Priv_Deps);
2452 while Present (Priv_Elmt) loop
2453 Priv := Node (Priv_Elmt);
2455 -- Before the exchange, verify that the presence of the Full_View
2456 -- field. This field will be empty if the entity has already been
2457 -- installed due to a previous call.
2459 if Present (Full_View (Priv)) and then Is_Visible_Dependent (Priv)
2460 then
2461 if Is_Private_Type (Priv) then
2462 Cunit := Cunit_Entity (Current_Sem_Unit);
2463 Deps := Private_Dependents (Priv);
2464 Is_Priv := True;
2465 else
2466 Is_Priv := False;
2467 end if;
2469 -- For each subtype that is swapped, we also swap the reference
2470 -- to it in Private_Dependents, to allow access to it when we
2471 -- swap them out in End_Package_Scope.
2473 Replace_Elmt (Priv_Elmt, Full_View (Priv));
2475 -- Ensure that both views of the dependent private subtype are
2476 -- immediately visible if within some open scope. Check full
2477 -- view before exchanging views.
2479 if In_Open_Scopes (Scope (Full_View (Priv))) then
2480 Set_Is_Immediately_Visible (Priv);
2481 end if;
2483 Exchange_Declarations (Priv);
2484 Set_Is_Immediately_Visible
2485 (Priv, In_Open_Scopes (Scope (Priv)));
2487 Set_Is_Potentially_Use_Visible
2488 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
2490 -- Recurse for child units, except in generic child units,
2491 -- which unfortunately handle private_dependents separately.
2492 -- Note that the current unit may not have been analyzed,
2493 -- for example a package body, so we cannot rely solely on
2494 -- the Is_Child_Unit flag, but that's only an optimization.
2496 if Is_Priv
2497 and then (No (Etype (Cunit)) or else Is_Child_Unit (Cunit))
2498 and then not Is_Empty_Elmt_List (Deps)
2499 and then not Inside_A_Generic
2500 then
2501 Swap_Private_Dependents (Deps);
2502 end if;
2503 end if;
2505 Next_Elmt (Priv_Elmt);
2506 end loop;
2507 end Swap_Private_Dependents;
2509 -- Start of processing for Install_Private_Declarations
2511 begin
2512 -- First exchange declarations for private types, so that the full
2513 -- declaration is visible. For each private type, we check its
2514 -- Private_Dependents list and also exchange any subtypes of or derived
2515 -- types from it. Finally, if this is a Taft amendment type, the
2516 -- incomplete declaration is irrelevant, and we want to link the
2517 -- eventual full declaration with the original private one so we
2518 -- also skip the exchange.
2520 Id := First_Entity (P);
2521 while Present (Id) and then Id /= First_Private_Entity (P) loop
2522 if Is_Private_Base_Type (Id)
2523 and then Present (Full_View (Id))
2524 and then Comes_From_Source (Full_View (Id))
2525 and then Scope (Full_View (Id)) = Scope (Id)
2526 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
2527 then
2528 -- If there is a use-type clause on the private type, set the full
2529 -- view accordingly.
2531 Set_In_Use (Full_View (Id), In_Use (Id));
2532 Full := Full_View (Id);
2534 if Is_Private_Base_Type (Full)
2535 and then Has_Private_Declaration (Full)
2536 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
2537 and then In_Open_Scopes (Scope (Etype (Full)))
2538 and then In_Package_Body (Current_Scope)
2539 and then not Is_Private_Type (Etype (Full))
2540 then
2541 -- This is the completion of a private type by a derivation
2542 -- from another private type which is not private anymore. This
2543 -- can only happen in a package nested within a child package,
2544 -- when the parent type is defined in the parent unit. At this
2545 -- point the current type is not private either, and we have
2546 -- to install the underlying full view, which is now visible.
2547 -- Save the current full view as well, so that all views can be
2548 -- restored on exit. It may seem that after compiling the child
2549 -- body there are not environments to restore, but the back-end
2550 -- expects those links to be valid, and freeze nodes depend on
2551 -- them.
2553 if No (Full_View (Full))
2554 and then Present (Underlying_Full_View (Full))
2555 then
2556 Set_Full_View (Id, Underlying_Full_View (Full));
2557 Set_Underlying_Full_View (Id, Full);
2558 Set_Is_Underlying_Full_View (Full);
2560 Set_Underlying_Full_View (Full, Empty);
2561 Set_Is_Frozen (Full_View (Id));
2562 end if;
2563 end if;
2565 Priv_Deps := Private_Dependents (Id);
2566 Exchange_Declarations (Id);
2567 Set_Is_Immediately_Visible (Id);
2568 Swap_Private_Dependents (Priv_Deps);
2569 end if;
2571 Next_Entity (Id);
2572 end loop;
2574 -- Next make other declarations in the private part visible as well
2576 Id := First_Private_Entity (P);
2577 while Present (Id) loop
2578 Install_Package_Entity (Id);
2579 Set_Is_Hidden (Id, False);
2580 Next_Entity (Id);
2581 end loop;
2583 -- An abstract state is partially refined when it has at least one
2584 -- Part_Of constituent. Since these constituents are being installed
2585 -- into visibility, update the partial refinement status of any state
2586 -- defined in the associated package, subject to at least one Part_Of
2587 -- constituent.
2589 if Is_Package_Or_Generic_Package (P) then
2590 declare
2591 States : constant Elist_Id := Abstract_States (P);
2592 State_Elmt : Elmt_Id;
2593 State_Id : Entity_Id;
2595 begin
2596 if Present (States) then
2597 State_Elmt := First_Elmt (States);
2598 while Present (State_Elmt) loop
2599 State_Id := Node (State_Elmt);
2601 if Present (Part_Of_Constituents (State_Id)) then
2602 Set_Has_Partial_Visible_Refinement (State_Id);
2603 end if;
2605 Next_Elmt (State_Elmt);
2606 end loop;
2607 end if;
2608 end;
2609 end if;
2611 -- Indicate that the private part is currently visible, so it can be
2612 -- properly reset on exit.
2614 Set_In_Private_Part (P);
2615 end Install_Private_Declarations;
2617 ----------------------------------
2618 -- Install_Visible_Declarations --
2619 ----------------------------------
2621 procedure Install_Visible_Declarations (P : Entity_Id) is
2622 Id : Entity_Id;
2623 Last_Entity : Entity_Id;
2625 begin
2626 pragma Assert
2627 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
2629 if Is_Package_Or_Generic_Package (P) then
2630 Last_Entity := First_Private_Entity (P);
2631 else
2632 Last_Entity := Empty;
2633 end if;
2635 Id := First_Entity (P);
2636 while Present (Id) and then Id /= Last_Entity loop
2637 Install_Package_Entity (Id);
2638 Next_Entity (Id);
2639 end loop;
2640 end Install_Visible_Declarations;
2642 --------------------------
2643 -- Is_Private_Base_Type --
2644 --------------------------
2646 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
2647 begin
2648 return Ekind (E) = E_Private_Type
2649 or else Ekind (E) = E_Limited_Private_Type
2650 or else Ekind (E) = E_Record_Type_With_Private;
2651 end Is_Private_Base_Type;
2653 --------------------------
2654 -- Is_Visible_Dependent --
2655 --------------------------
2657 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
2659 S : constant Entity_Id := Scope (Dep);
2661 begin
2662 -- Renamings created for actual types have the visibility of the actual
2664 if Ekind (S) = E_Package
2665 and then Is_Generic_Instance (S)
2666 and then (Is_Generic_Actual_Type (Dep)
2667 or else Is_Generic_Actual_Type (Full_View (Dep)))
2668 then
2669 return True;
2671 elsif not (Is_Derived_Type (Dep))
2672 and then Is_Derived_Type (Full_View (Dep))
2673 then
2674 -- When instantiating a package body, the scope stack is empty, so
2675 -- check instead whether the dependent type is defined in the same
2676 -- scope as the instance itself.
2678 return In_Open_Scopes (S)
2679 or else (Is_Generic_Instance (Current_Scope)
2680 and then Scope (Dep) = Scope (Current_Scope));
2681 else
2682 return True;
2683 end if;
2684 end Is_Visible_Dependent;
2686 ----------------------------
2687 -- May_Need_Implicit_Body --
2688 ----------------------------
2690 procedure May_Need_Implicit_Body (E : Entity_Id) is
2691 P : constant Node_Id := Unit_Declaration_Node (E);
2692 S : constant Node_Id := Parent (P);
2693 B : Node_Id;
2694 Decls : List_Id;
2696 begin
2697 if not Has_Completion (E)
2698 and then Nkind (P) = N_Package_Declaration
2699 and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
2700 then
2701 B :=
2702 Make_Package_Body (Sloc (E),
2703 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
2704 Chars => Chars (E)),
2705 Declarations => New_List);
2707 if Nkind (S) = N_Package_Specification then
2708 if Present (Private_Declarations (S)) then
2709 Decls := Private_Declarations (S);
2710 else
2711 Decls := Visible_Declarations (S);
2712 end if;
2713 else
2714 Decls := Declarations (S);
2715 end if;
2717 Append (B, Decls);
2718 Analyze (B);
2719 end if;
2720 end May_Need_Implicit_Body;
2722 ----------------------
2723 -- New_Private_Type --
2724 ----------------------
2726 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
2727 begin
2728 -- For other than Ada 2012, enter the name in the current scope
2730 if Ada_Version < Ada_2012 then
2731 Enter_Name (Id);
2733 -- Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2734 -- there may be an incomplete previous view.
2736 else
2737 declare
2738 Prev : Entity_Id;
2739 begin
2740 Prev := Find_Type_Name (N);
2741 pragma Assert (Prev = Id
2742 or else (Ekind (Prev) = E_Incomplete_Type
2743 and then Present (Full_View (Prev))
2744 and then Full_View (Prev) = Id));
2745 end;
2746 end if;
2748 if Limited_Present (Def) then
2749 Mutate_Ekind (Id, E_Limited_Private_Type);
2750 else
2751 Mutate_Ekind (Id, E_Private_Type);
2752 end if;
2754 Set_Is_Not_Self_Hidden (Id);
2755 Set_Etype (Id, Id);
2756 Set_Has_Delayed_Freeze (Id);
2757 Set_Is_First_Subtype (Id);
2758 Reinit_Size_Align (Id);
2760 Set_Is_Constrained (Id,
2761 No (Discriminant_Specifications (N))
2762 and then not Unknown_Discriminants_Present (N));
2764 -- Set tagged flag before processing discriminants, to catch illegal
2765 -- usage.
2767 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
2769 Set_Discriminant_Constraint (Id, No_Elist);
2770 Set_Stored_Constraint (Id, No_Elist);
2772 if Present (Discriminant_Specifications (N)) then
2773 Push_Scope (Id);
2774 Process_Discriminants (N);
2775 End_Scope;
2777 elsif Unknown_Discriminants_Present (N) then
2778 Set_Has_Unknown_Discriminants (Id);
2779 end if;
2781 Set_Private_Dependents (Id, New_Elmt_List);
2783 if Tagged_Present (Def) then
2784 Mutate_Ekind (Id, E_Record_Type_With_Private);
2785 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2786 Set_Is_Abstract_Type (Id, Abstract_Present (Def));
2787 Set_Is_Limited_Record (Id, Limited_Present (Def));
2788 Set_Has_Delayed_Freeze (Id, True);
2790 -- Recognize Ada.Real_Time.Timing_Events.Timing_Events here
2792 if Is_RTE (Id, RE_Timing_Event) then
2793 Set_Has_Timing_Event (Id);
2794 end if;
2796 -- Create a class-wide type with the same attributes
2798 Make_Class_Wide_Type (Id);
2800 elsif Abstract_Present (Def) then
2801 Error_Msg_N ("only a tagged type can be abstract", N);
2803 -- We initialize the primitive operations list of an untagged private
2804 -- type to an empty element list. Do this even when Extensions_Allowed
2805 -- is False to issue better error messages. (Note: This could be done
2806 -- for all private types and shared with the tagged case above, but
2807 -- for now we do it separately.)
2809 else
2810 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2811 end if;
2812 end New_Private_Type;
2814 ---------------------------------
2815 -- Requires_Completion_In_Body --
2816 ---------------------------------
2818 function Requires_Completion_In_Body
2819 (Id : Entity_Id;
2820 Pack_Id : Entity_Id;
2821 Do_Abstract_States : Boolean := False) return Boolean
2823 begin
2824 -- Always ignore child units. Child units get added to the entity list
2825 -- of a parent unit, but are not original entities of the parent, and
2826 -- so do not affect whether the parent needs a body.
2828 if Is_Child_Unit (Id) then
2829 return False;
2831 -- Ignore formal packages and their renamings
2833 elsif Ekind (Id) = E_Package
2834 and then Nkind (Original_Node (Unit_Declaration_Node (Id))) =
2835 N_Formal_Package_Declaration
2836 then
2837 return False;
2839 -- Otherwise test to see if entity requires a completion. Note that
2840 -- subprogram entities whose declaration does not come from source are
2841 -- ignored here on the basis that we assume the expander will provide an
2842 -- implicit completion at some point.
2844 elsif (Is_Overloadable (Id)
2845 and then Ekind (Id) not in E_Enumeration_Literal | E_Operator
2846 and then not Is_Abstract_Subprogram (Id)
2847 and then not Has_Completion (Id)
2848 and then Comes_From_Source (Parent (Id)))
2850 or else
2851 (Ekind (Id) = E_Package
2852 and then Id /= Pack_Id
2853 and then not Has_Completion (Id)
2854 and then Unit_Requires_Body (Id, Do_Abstract_States))
2856 or else
2857 (Ekind (Id) = E_Incomplete_Type
2858 and then No (Full_View (Id))
2859 and then not Is_Generic_Type (Id))
2861 or else
2862 (Ekind (Id) in E_Task_Type | E_Protected_Type
2863 and then not Has_Completion (Id))
2865 or else
2866 (Ekind (Id) = E_Generic_Package
2867 and then Id /= Pack_Id
2868 and then not Has_Completion (Id)
2869 and then Unit_Requires_Body (Id, Do_Abstract_States))
2871 or else
2872 (Is_Generic_Subprogram (Id)
2873 and then not Has_Completion (Id))
2874 then
2875 return True;
2877 -- Otherwise the entity does not require completion in a package body
2879 else
2880 return False;
2881 end if;
2882 end Requires_Completion_In_Body;
2884 ----------------------------
2885 -- Uninstall_Declarations --
2886 ----------------------------
2888 procedure Uninstall_Declarations (P : Entity_Id) is
2889 Decl : constant Node_Id := Unit_Declaration_Node (P);
2890 Id : Entity_Id;
2891 Full : Entity_Id;
2893 procedure Preserve_Full_Attributes (Priv : Entity_Id; Full : Entity_Id);
2894 -- Copy to the private declaration the attributes of the full view that
2895 -- need to be available for the partial view also.
2897 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
2898 -- When the full view of a private type is made unavailable, we do the
2899 -- same for its private dependents under proper visibility conditions.
2900 -- When compiling a child unit this needs to be done recursively.
2902 function Type_In_Use (T : Entity_Id) return Boolean;
2903 -- Check whether type or base type appear in an active use_type clause
2905 ------------------------------
2906 -- Preserve_Full_Attributes --
2907 ------------------------------
2909 procedure Preserve_Full_Attributes
2910 (Priv : Entity_Id;
2911 Full : Entity_Id)
2913 Full_Base : constant Entity_Id := Base_Type (Full);
2914 Priv_Is_Base_Type : constant Boolean := Is_Base_Type (Priv);
2916 begin
2917 Set_Size_Info (Priv, Full);
2918 Copy_RM_Size (To => Priv, From => Full);
2919 Set_Size_Known_At_Compile_Time
2920 (Priv, Size_Known_At_Compile_Time (Full));
2921 Set_Is_Volatile (Priv, Is_Volatile (Full));
2922 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
2923 Set_Is_Atomic (Priv, Is_Atomic (Full));
2924 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full));
2925 Set_Is_Ada_2012_Only (Priv, Is_Ada_2012_Only (Full));
2926 Set_Is_Ada_2022_Only (Priv, Is_Ada_2022_Only (Full));
2927 Set_Has_Pragma_Unmodified (Priv, Has_Pragma_Unmodified (Full));
2928 Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced (Full));
2929 Set_Has_Pragma_Unreferenced_Objects
2930 (Priv, Has_Pragma_Unreferenced_Objects
2931 (Full));
2932 Set_Predicates_Ignored (Priv, Predicates_Ignored (Full));
2933 if Is_Unchecked_Union (Full) then
2934 Set_Is_Unchecked_Union (Base_Type (Priv));
2935 end if;
2937 if Referenced (Full) then
2938 Set_Referenced (Priv);
2939 end if;
2941 if Priv_Is_Base_Type then
2942 Set_Is_Controlled_Active
2943 (Priv, Is_Controlled_Active (Full_Base));
2944 Set_Finalize_Storage_Only
2945 (Priv, Finalize_Storage_Only (Full_Base));
2946 Set_Has_Controlled_Component
2947 (Priv, Has_Controlled_Component (Full_Base));
2949 Propagate_Concurrent_Flags (Priv, Base_Type (Full));
2950 end if;
2952 -- As explained in Freeze_Entity, private types are required to point
2953 -- to the same freeze node as their corresponding full view, if any.
2954 -- But we ought not to overwrite a node already inserted in the tree.
2956 pragma Assert
2957 (Serious_Errors_Detected /= 0
2958 or else No (Freeze_Node (Priv))
2959 or else No (Parent (Freeze_Node (Priv)))
2960 or else Freeze_Node (Priv) = Freeze_Node (Full));
2962 Set_Freeze_Node (Priv, Freeze_Node (Full));
2964 -- Propagate Default_Initial_Condition-related attributes from the
2965 -- full view to the private view.
2967 Propagate_DIC_Attributes (Priv, From_Typ => Full);
2969 -- Propagate invariant-related attributes from the full view to the
2970 -- private view.
2972 Propagate_Invariant_Attributes (Priv, From_Typ => Full);
2974 -- Propagate predicate-related attributes from the full view to the
2975 -- private view.
2977 Propagate_Predicate_Attributes (Priv, From_Typ => Full);
2979 if Is_Tagged_Type (Priv)
2980 and then Is_Tagged_Type (Full)
2981 and then not Error_Posted (Full)
2982 then
2983 if Is_Tagged_Type (Priv) then
2985 -- If the type is tagged, the tag itself must be available on
2986 -- the partial view, for expansion purposes.
2988 Set_First_Entity (Priv, First_Entity (Full));
2990 -- If there are discriminants in the partial view, these remain
2991 -- visible. Otherwise only the tag itself is visible, and there
2992 -- are no nameable components in the partial view.
2994 if No (Last_Entity (Priv)) then
2995 Set_Last_Entity (Priv, First_Entity (Priv));
2996 end if;
2997 end if;
2999 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
3001 if Has_Discriminants (Full) then
3002 Set_Discriminant_Constraint (Priv,
3003 Discriminant_Constraint (Full));
3004 end if;
3005 end if;
3006 end Preserve_Full_Attributes;
3008 -----------------------------
3009 -- Swap_Private_Dependents --
3010 -----------------------------
3012 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
3013 Cunit : Entity_Id;
3014 Deps : Elist_Id;
3015 Priv : Entity_Id;
3016 Priv_Elmt : Elmt_Id;
3017 Is_Priv : Boolean;
3019 begin
3020 Priv_Elmt := First_Elmt (Priv_Deps);
3021 while Present (Priv_Elmt) loop
3022 Priv := Node (Priv_Elmt);
3024 -- Before we do the swap, we verify the presence of the Full_View
3025 -- field, which may be empty due to a swap by a previous call to
3026 -- End_Package_Scope (e.g. from the freezing mechanism).
3028 if Present (Full_View (Priv)) then
3029 if Is_Private_Type (Priv) then
3030 Cunit := Cunit_Entity (Current_Sem_Unit);
3031 Deps := Private_Dependents (Priv);
3032 Is_Priv := True;
3033 else
3034 Is_Priv := False;
3035 end if;
3037 if Scope (Priv) = P
3038 or else not In_Open_Scopes (Scope (Priv))
3039 then
3040 Set_Is_Immediately_Visible (Priv, False);
3041 end if;
3043 if Is_Visible_Dependent (Priv) then
3044 Preserve_Full_Attributes (Priv, Full_View (Priv));
3045 Replace_Elmt (Priv_Elmt, Full_View (Priv));
3046 Exchange_Declarations (Priv);
3048 -- Recurse for child units, except in generic child units,
3049 -- which unfortunately handle private_dependents separately.
3050 -- Note that the current unit may not have been analyzed,
3051 -- for example a package body, so we cannot rely solely on
3052 -- the Is_Child_Unit flag, but that's only an optimization.
3054 if Is_Priv
3055 and then (No (Etype (Cunit)) or else Is_Child_Unit (Cunit))
3056 and then not Is_Empty_Elmt_List (Deps)
3057 and then not Inside_A_Generic
3058 then
3059 Swap_Private_Dependents (Deps);
3060 end if;
3061 end if;
3062 end if;
3064 Next_Elmt (Priv_Elmt);
3065 end loop;
3066 end Swap_Private_Dependents;
3068 -----------------
3069 -- Type_In_Use --
3070 -----------------
3072 function Type_In_Use (T : Entity_Id) return Boolean is
3073 begin
3074 return Scope (Base_Type (T)) = P
3075 and then (In_Use (T) or else In_Use (Base_Type (T)));
3076 end Type_In_Use;
3078 -- Start of processing for Uninstall_Declarations
3080 begin
3081 Id := First_Entity (P);
3082 while Present (Id) and then Id /= First_Private_Entity (P) loop
3083 if Debug_Flag_E then
3084 Write_Str ("unlinking visible entity ");
3085 Write_Int (Int (Id));
3086 Write_Eol;
3087 end if;
3089 -- On exit from the package scope, we must preserve the visibility
3090 -- established by use clauses in the current scope. Two cases:
3092 -- a) If the entity is an operator, it may be a primitive operator of
3093 -- a type for which there is a visible use-type clause.
3095 -- b) For other entities, their use-visibility is determined by a
3096 -- visible use clause for the package itself or a use-all-type clause
3097 -- applied directly to the entity's type. For a generic instance,
3098 -- the instantiation of the formals appears in the visible part,
3099 -- but the formals are private and remain so.
3101 if Ekind (Id) = E_Function
3102 and then Is_Operator_Symbol_Name (Chars (Id))
3103 and then not Is_Hidden (Id)
3104 and then not Error_Posted (Id)
3105 then
3106 Set_Is_Potentially_Use_Visible (Id,
3107 In_Use (P)
3108 or else Type_In_Use (Etype (Id))
3109 or else Type_In_Use (Etype (First_Formal (Id)))
3110 or else (Present (Next_Formal (First_Formal (Id)))
3111 and then
3112 Type_In_Use
3113 (Etype (Next_Formal (First_Formal (Id))))));
3114 else
3115 if In_Use (P) and then not Is_Hidden (Id) then
3117 -- A child unit of a use-visible package remains use-visible
3118 -- only if it is itself a visible child unit. Otherwise it
3119 -- would remain visible in other contexts where P is use-
3120 -- visible, because once compiled it stays in the entity list
3121 -- of its parent unit.
3123 if Is_Child_Unit (Id) then
3124 Set_Is_Potentially_Use_Visible
3125 (Id, Is_Visible_Lib_Unit (Id));
3126 else
3127 Set_Is_Potentially_Use_Visible (Id);
3128 end if;
3130 -- Avoid crash caused by previous errors
3132 elsif No (Etype (Id)) and then Serious_Errors_Detected /= 0 then
3133 null;
3135 -- We need to avoid incorrectly marking enumeration literals as
3136 -- non-visible when a visible use-all-type clause is in effect.
3138 elsif Type_In_Use (Etype (Id))
3139 and then Nkind (Current_Use_Clause (Etype (Id))) =
3140 N_Use_Type_Clause
3141 and then All_Present (Current_Use_Clause (Etype (Id)))
3142 then
3143 null;
3145 else
3146 Set_Is_Potentially_Use_Visible (Id, False);
3147 end if;
3148 end if;
3150 -- Local entities are not immediately visible outside of the package
3152 Set_Is_Immediately_Visible (Id, False);
3154 -- If this is a private type with a full view (for example a local
3155 -- subtype of a private type declared elsewhere), ensure that the
3156 -- full view is also removed from visibility: it may be exposed when
3157 -- swapping views in an instantiation. Similarly, ensure that the
3158 -- use-visibility is properly set on both views.
3160 if Is_Type (Id) and then Present (Full_View (Id)) then
3161 Set_Is_Immediately_Visible (Full_View (Id), False);
3162 Set_Is_Potentially_Use_Visible (Full_View (Id),
3163 Is_Potentially_Use_Visible (Id));
3164 end if;
3166 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
3167 Check_Abstract_Overriding (Id);
3168 Check_Conventions (Id);
3169 end if;
3171 if Ekind (Id) in E_Private_Type | E_Limited_Private_Type
3172 and then No (Full_View (Id))
3173 and then not Is_Generic_Type (Id)
3174 and then not Is_Derived_Type (Id)
3175 then
3176 Error_Msg_N ("missing full declaration for private type&", Id);
3178 elsif Ekind (Id) = E_Record_Type_With_Private
3179 and then not Is_Generic_Type (Id)
3180 and then No (Full_View (Id))
3181 then
3182 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
3183 Error_Msg_N ("missing full declaration for private type&", Id);
3184 else
3185 Error_Msg_N
3186 ("missing full declaration for private extension", Id);
3187 end if;
3189 -- Case of constant, check for deferred constant declaration with
3190 -- no full view. Likely just a matter of a missing expression, or
3191 -- accidental use of the keyword constant.
3193 elsif Ekind (Id) = E_Constant
3195 -- OK if constant value present
3197 and then No (Constant_Value (Id))
3199 -- OK if full view present
3201 and then No (Full_View (Id))
3203 -- OK if imported, since that provides the completion
3205 and then not Is_Imported (Id)
3207 -- OK if object declaration replaced by renaming declaration as
3208 -- a result of OK_To_Rename processing (e.g. for concatenation)
3210 and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration
3212 -- OK if object declaration with the No_Initialization flag set
3214 and then not (Nkind (Parent (Id)) = N_Object_Declaration
3215 and then No_Initialization (Parent (Id)))
3216 then
3217 -- If no private declaration is present, we assume the user did
3218 -- not intend a deferred constant declaration and the problem
3219 -- is simply that the initializing expression is missing.
3221 if not Has_Private_Declaration (Etype (Id)) then
3222 Error_Msg_N
3223 ("constant declaration requires initialization expression",
3224 Parent (Id));
3226 if Is_Limited_Type (Etype (Id)) then
3227 Error_Msg_N
3228 ("\if variable intended, remove CONSTANT from declaration",
3229 Parent (Id));
3230 end if;
3232 -- Otherwise if a private declaration is present, then we are
3233 -- missing the full declaration for the deferred constant.
3235 else
3236 Error_Msg_N
3237 ("missing full declaration for deferred constant (RM 7.4)",
3238 Id);
3240 if Is_Limited_Type (Etype (Id)) then
3241 Error_Msg_N
3242 ("\if variable intended, remove CONSTANT from declaration",
3243 Parent (Id));
3244 end if;
3245 end if;
3246 end if;
3248 Next_Entity (Id);
3249 end loop;
3251 -- If the specification was installed as the parent of a public child
3252 -- unit, the private declarations were not installed, and there is
3253 -- nothing to do.
3255 if not In_Private_Part (P) then
3256 return;
3257 end if;
3259 -- Reset the flag now
3261 Set_In_Private_Part (P, False);
3263 -- Make private entities invisible and exchange full and private
3264 -- declarations for private types. Id is now the first private entity
3265 -- in the package.
3267 while Present (Id) loop
3268 if Debug_Flag_E then
3269 Write_Str ("unlinking private entity ");
3270 Write_Int (Int (Id));
3271 Write_Eol;
3272 end if;
3274 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
3275 Check_Abstract_Overriding (Id);
3276 Check_Conventions (Id);
3277 end if;
3279 Set_Is_Immediately_Visible (Id, False);
3281 if Is_Private_Base_Type (Id) and then Present (Full_View (Id)) then
3282 Full := Full_View (Id);
3284 -- If the partial view is not declared in the visible part of the
3285 -- package (as is the case when it is a type derived from some
3286 -- other private type in the private part of the current package),
3287 -- no exchange takes place.
3289 if No (Parent (Id))
3290 or else List_Containing (Parent (Id)) /=
3291 Visible_Declarations (Specification (Decl))
3292 then
3293 goto Next_Id;
3294 end if;
3296 -- The entry in the private part points to the full declaration,
3297 -- which is currently visible. Exchange them so only the private
3298 -- type declaration remains accessible, and link private and full
3299 -- declaration in the opposite direction. Before the actual
3300 -- exchange, we copy back attributes of the full view that must
3301 -- be available to the partial view too.
3303 Preserve_Full_Attributes (Id, Full);
3305 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
3307 -- The following test may be redundant, as this is already
3308 -- diagnosed in sem_ch3. ???
3310 if not Is_Definite_Subtype (Full)
3311 and then Is_Definite_Subtype (Id)
3312 then
3313 Error_Msg_Sloc := Sloc (Parent (Id));
3314 Error_Msg_NE
3315 ("full view of& not compatible with declaration#", Full, Id);
3316 end if;
3318 -- Swap out the subtypes and derived types of Id that
3319 -- were compiled in this scope, or installed previously
3320 -- by Install_Private_Declarations.
3322 Swap_Private_Dependents (Private_Dependents (Id));
3324 -- Now restore the type itself to its private view
3326 Exchange_Declarations (Id);
3328 -- If we have installed an underlying full view for a type derived
3329 -- from a private type in a child unit, restore the proper views
3330 -- of private and full view. See corresponding code in
3331 -- Install_Private_Declarations.
3333 -- After the exchange, Full denotes the private type in the
3334 -- visible part of the package.
3336 if Is_Private_Base_Type (Full)
3337 and then Present (Full_View (Full))
3338 and then Present (Underlying_Full_View (Full))
3339 and then In_Package_Body (Current_Scope)
3340 then
3341 Set_Full_View (Full, Underlying_Full_View (Full));
3342 Set_Underlying_Full_View (Full, Empty);
3343 end if;
3345 elsif Ekind (Id) = E_Incomplete_Type
3346 and then Comes_From_Source (Id)
3347 and then No (Full_View (Id))
3348 then
3349 -- Mark Taft amendment types. Verify that there are no primitive
3350 -- operations declared for the type (3.10.1(9)).
3352 Set_Has_Completion_In_Body (Id);
3354 declare
3355 Elmt : Elmt_Id;
3356 Subp : Entity_Id;
3358 begin
3359 Elmt := First_Elmt (Private_Dependents (Id));
3360 while Present (Elmt) loop
3361 Subp := Node (Elmt);
3363 -- Is_Primitive is tested because there can be cases where
3364 -- nonprimitive subprograms (in nested packages) are added
3365 -- to the Private_Dependents list.
3367 if Is_Overloadable (Subp) and then Is_Primitive (Subp) then
3368 Error_Msg_NE
3369 ("type& must be completed in the private part",
3370 Parent (Subp), Id);
3372 -- The result type of an access-to-function type cannot be a
3373 -- Taft-amendment type, unless the version is Ada 2012 or
3374 -- later (see AI05-151).
3376 elsif Ada_Version < Ada_2012
3377 and then Ekind (Subp) = E_Subprogram_Type
3378 then
3379 if Etype (Subp) = Id
3380 or else
3381 (Is_Class_Wide_Type (Etype (Subp))
3382 and then Etype (Etype (Subp)) = Id)
3383 then
3384 Error_Msg_NE
3385 ("type& must be completed in the private part",
3386 Associated_Node_For_Itype (Subp), Id);
3387 end if;
3388 end if;
3390 Next_Elmt (Elmt);
3391 end loop;
3392 end;
3394 -- For subtypes of private types the frontend generates two entities:
3395 -- one associated with the partial view and the other associated with
3396 -- the full view. When the subtype declaration is public the frontend
3397 -- places the former entity in the list of public entities of the
3398 -- package and the latter entity in the private part of the package.
3399 -- When the subtype declaration is private it generates these two
3400 -- entities but both are placed in the private part of the package
3401 -- (and the full view has the same source location as the partial
3402 -- view and no parent; see Prepare_Private_Subtype_Completion).
3404 elsif Ekind (Id) in E_Private_Subtype
3405 | E_Limited_Private_Subtype
3406 and then Present (Full_View (Id))
3407 and then Sloc (Id) = Sloc (Full_View (Id))
3408 and then No (Parent (Full_View (Id)))
3409 then
3410 Set_Is_Hidden (Id);
3411 Set_Is_Potentially_Use_Visible (Id, False);
3413 elsif not Is_Child_Unit (Id)
3414 and then (not Is_Private_Type (Id) or else No (Full_View (Id)))
3415 then
3416 Set_Is_Hidden (Id);
3417 Set_Is_Potentially_Use_Visible (Id, False);
3418 end if;
3420 <<Next_Id>>
3421 Next_Entity (Id);
3422 end loop;
3423 end Uninstall_Declarations;
3425 ------------------------
3426 -- Unit_Requires_Body --
3427 ------------------------
3429 function Unit_Requires_Body
3430 (Pack_Id : Entity_Id;
3431 Do_Abstract_States : Boolean := False) return Boolean
3433 E : Entity_Id;
3435 Requires_Body : Boolean := False;
3436 -- Flag set when the unit has at least one construct that requires
3437 -- completion in a body.
3439 begin
3440 -- Imported entity never requires body. Right now, only subprograms can
3441 -- be imported, but perhaps in the future we will allow import of
3442 -- packages.
3444 if Is_Imported (Pack_Id) then
3445 return False;
3447 -- Body required if library package with pragma Elaborate_Body
3449 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3450 return True;
3452 -- Body required if subprogram
3454 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3455 return True;
3457 -- Treat a block as requiring a body
3459 elsif Ekind (Pack_Id) = E_Block then
3460 return True;
3462 elsif Ekind (Pack_Id) = E_Package
3463 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3464 and then Present (Generic_Parent (Parent (Pack_Id)))
3465 then
3466 declare
3467 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3468 begin
3469 if Has_Pragma_Elaborate_Body (G_P) then
3470 return True;
3471 end if;
3472 end;
3473 end if;
3475 -- Traverse the entity chain of the package and look for constructs that
3476 -- require a completion in a body.
3478 E := First_Entity (Pack_Id);
3479 while Present (E) loop
3481 -- Skip abstract states because their completion depends on several
3482 -- criteria (see below).
3484 if Ekind (E) = E_Abstract_State then
3485 null;
3487 elsif Requires_Completion_In_Body
3488 (E, Pack_Id, Do_Abstract_States)
3489 then
3490 Requires_Body := True;
3491 exit;
3492 end if;
3494 Next_Entity (E);
3495 end loop;
3497 -- A [generic] package that defines at least one non-null abstract state
3498 -- requires a completion only when at least one other construct requires
3499 -- a completion in a body (SPARK RM 7.1.4(4) and (5)). This check is not
3500 -- performed if the caller requests this behavior.
3502 if Do_Abstract_States
3503 and then Is_Package_Or_Generic_Package (Pack_Id)
3504 and then Has_Non_Null_Abstract_State (Pack_Id)
3505 and then Requires_Body
3506 then
3507 return True;
3508 end if;
3510 return Requires_Body;
3511 end Unit_Requires_Body;
3513 -----------------------------
3514 -- Unit_Requires_Body_Info --
3515 -----------------------------
3517 procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id) is
3518 E : Entity_Id;
3520 begin
3521 -- An imported entity never requires body. Right now, only subprograms
3522 -- can be imported, but perhaps in the future we will allow import of
3523 -- packages.
3525 if Is_Imported (Pack_Id) then
3526 return;
3528 -- Body required if library package with pragma Elaborate_Body
3530 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3531 Error_Msg_N ("info: & requires body (Elaborate_Body)?.y?", Pack_Id);
3533 -- Body required if subprogram
3535 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3536 Error_Msg_N ("info: & requires body (subprogram case)?.y?", Pack_Id);
3538 -- Body required if generic parent has Elaborate_Body
3540 elsif Ekind (Pack_Id) = E_Package
3541 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3542 and then Present (Generic_Parent (Parent (Pack_Id)))
3543 then
3544 declare
3545 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3546 begin
3547 if Has_Pragma_Elaborate_Body (G_P) then
3548 Error_Msg_N
3549 ("info: & requires body (generic parent Elaborate_Body)?.y?",
3550 Pack_Id);
3551 end if;
3552 end;
3554 -- A [generic] package that introduces at least one non-null abstract
3555 -- state requires completion. However, there is a separate rule that
3556 -- requires that such a package have a reason other than this for a
3557 -- body being required (if necessary a pragma Elaborate_Body must be
3558 -- provided). If Ignore_Abstract_State is True, we don't do this check
3559 -- (so we can use Unit_Requires_Body to check for some other reason).
3561 elsif Is_Package_Or_Generic_Package (Pack_Id)
3562 and then Present (Abstract_States (Pack_Id))
3563 and then not Is_Null_State
3564 (Node (First_Elmt (Abstract_States (Pack_Id))))
3565 then
3566 Error_Msg_N
3567 ("info: & requires body (non-null abstract state aspect)?.y?",
3568 Pack_Id);
3569 end if;
3571 -- Otherwise search entity chain for entity requiring completion
3573 E := First_Entity (Pack_Id);
3574 while Present (E) loop
3575 if Requires_Completion_In_Body (E, Pack_Id) then
3576 Error_Msg_Node_2 := E;
3577 Error_Msg_NE
3578 ("info: & requires body (& requires completion)?.y?", E,
3579 Pack_Id);
3580 end if;
3582 Next_Entity (E);
3583 end loop;
3584 end Unit_Requires_Body_Info;
3586 end Sem_Ch7;