Disable tests for strdup/strndup on __hpux__
[official-gcc.git] / gcc / ada / sem_ch7.adb
blob21a22267590fdb0e381142332561b47df0d8d222
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 -- Collect all contract-related source pragmas found within the
881 -- template and attach them to the contract of the package body.
882 -- This contract is used in the capture of global references within
883 -- annotations.
885 Create_Generic_Contract (N);
887 -- Update Body_Id to point to the copied node for the remainder of
888 -- the processing.
890 Body_Id := Defining_Entity (N);
891 Start_Generic;
892 end if;
894 -- The Body_Id is that of the copied node in the generic case, the
895 -- current node otherwise. Note that N was rewritten above, so we must
896 -- be sure to get the latest Body_Id value.
898 if Ekind (Body_Id) = E_Package then
899 Reinit_Field_To_Zero (Body_Id, F_Body_Needed_For_Inlining);
900 end if;
901 Mutate_Ekind (Body_Id, E_Package_Body);
902 Set_Body_Entity (Spec_Id, Body_Id);
903 Set_Spec_Entity (Body_Id, Spec_Id);
905 -- Defining name for the package body is not a visible entity: Only the
906 -- defining name for the declaration is visible.
908 Set_Etype (Body_Id, Standard_Void_Type);
909 Set_Scope (Body_Id, Scope (Spec_Id));
910 Set_Corresponding_Spec (N, Spec_Id);
911 Set_Corresponding_Body (Pack_Decl, Body_Id);
913 -- The body entity is not used for semantics or code generation, but
914 -- it is attached to the entity list of the enclosing scope to simplify
915 -- the listing of back-annotations for the types it main contain.
917 if Scope (Spec_Id) /= Standard_Standard then
918 Append_Entity (Body_Id, Scope (Spec_Id));
919 end if;
921 -- Indicate that we are currently compiling the body of the package
923 Set_In_Package_Body (Spec_Id);
924 Set_Has_Completion (Spec_Id);
925 Last_Spec_Entity := Last_Entity (Spec_Id);
927 Analyze_Aspect_Specifications (N, Body_Id);
929 Push_Scope (Spec_Id);
931 -- Set SPARK_Mode only for non-generic package
933 if Ekind (Spec_Id) = E_Package then
934 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
935 Set_SPARK_Aux_Pragma (Body_Id, SPARK_Mode_Pragma);
936 Set_SPARK_Pragma_Inherited (Body_Id);
937 Set_SPARK_Aux_Pragma_Inherited (Body_Id);
939 -- A package body may be instantiated or inlined at a later pass.
940 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when
941 -- it applied to the package spec.
943 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
944 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
945 end if;
946 end if;
948 Set_Categorization_From_Pragmas (N);
950 Install_Visible_Declarations (Spec_Id);
951 Install_Private_Declarations (Spec_Id);
952 Install_Private_With_Clauses (Spec_Id);
953 Install_Composite_Operations (Spec_Id);
955 Check_Anonymous_Access_Types (Spec_Id, N);
957 if Ekind (Spec_Id) = E_Generic_Package then
958 Set_Use (Generic_Formal_Declarations (Pack_Decl));
959 end if;
961 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
962 Set_Use (Private_Declarations (Specification (Pack_Decl)));
964 -- This is a nested package, so it may be necessary to declare certain
965 -- inherited subprograms that are not yet visible because the parent
966 -- type's subprograms are now visible.
967 -- Note that for child units these operations were generated when
968 -- analyzing the package specification.
970 if Ekind (Scope (Spec_Id)) = E_Package
971 and then Scope (Spec_Id) /= Standard_Standard
972 and then not Is_Child_Unit (Spec_Id)
973 then
974 Declare_Inherited_Private_Subprograms (Spec_Id);
975 end if;
977 if Present (Declarations (N)) then
978 Analyze_Declarations (Declarations (N));
979 Inspect_Deferred_Constant_Completion (Declarations (N));
980 end if;
982 -- Verify that the SPARK_Mode of the body agrees with that of its spec
984 if Present (SPARK_Pragma (Body_Id)) then
985 if Present (SPARK_Aux_Pragma (Spec_Id)) then
986 if Get_SPARK_Mode_From_Annotation (SPARK_Aux_Pragma (Spec_Id)) =
988 and then
989 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
990 then
991 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
992 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
993 Error_Msg_Sloc := Sloc (SPARK_Aux_Pragma (Spec_Id));
994 Error_Msg_NE
995 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
996 end if;
998 -- SPARK_Mode Off could complete no SPARK_Mode in a generic, either
999 -- as specified in source code, or because SPARK_Mode On is ignored
1000 -- in an instance where the context is SPARK_Mode Off/Auto.
1002 elsif Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = Off
1003 and then (Is_Generic_Unit (Spec_Id) or else In_Instance)
1004 then
1005 null;
1007 else
1008 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
1009 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
1010 Error_Msg_Sloc := Sloc (Spec_Id);
1011 Error_Msg_NE
1012 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
1013 end if;
1014 end if;
1016 -- Analyze_Declarations has caused freezing of all types. Now generate
1017 -- bodies for RACW primitives and stream attributes, if any.
1019 if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then
1021 -- Attach subprogram bodies to support RACWs declared in spec
1023 Append_RACW_Bodies (Declarations (N), Spec_Id);
1024 Analyze_List (Declarations (N));
1025 end if;
1027 HSS := Handled_Statement_Sequence (N);
1029 if Present (HSS) then
1030 Process_End_Label (HSS, 't', Spec_Id);
1031 Analyze (HSS);
1033 -- Check that elaboration code in a preelaborable package body is
1034 -- empty other than null statements and labels (RM 10.2.1(6)).
1036 Validate_Null_Statement_Sequence (N);
1037 end if;
1039 Validate_Categorization_Dependency (N, Spec_Id);
1040 Check_Completion (Body_Id);
1042 -- Generate start of body reference. Note that we do this fairly late,
1043 -- because the call will use In_Extended_Main_Source_Unit as a check,
1044 -- and we want to make sure that Corresponding_Stub links are set
1046 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
1048 -- For a generic package, collect global references and mark them on
1049 -- the original body so that they are not resolved again at the point
1050 -- of instantiation.
1052 if Ekind (Spec_Id) /= E_Package then
1053 Save_Global_References (Original_Node (N));
1054 End_Generic;
1055 end if;
1057 -- The entities of the package body have so far been chained onto the
1058 -- declaration chain for the spec. That's been fine while we were in the
1059 -- body, since we wanted them to be visible, but now that we are leaving
1060 -- the package body, they are no longer visible, so we remove them from
1061 -- the entity chain of the package spec entity, and copy them to the
1062 -- entity chain of the package body entity, where they will never again
1063 -- be visible.
1065 if Present (Last_Spec_Entity) then
1066 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
1067 Set_Next_Entity (Last_Spec_Entity, Empty);
1068 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1069 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
1071 else
1072 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
1073 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1074 Set_First_Entity (Spec_Id, Empty);
1075 Set_Last_Entity (Spec_Id, Empty);
1076 end if;
1078 Update_Use_Clause_Chain;
1079 End_Package_Scope (Spec_Id);
1081 -- All entities declared in body are not visible
1083 declare
1084 E : Entity_Id;
1086 begin
1087 E := First_Entity (Body_Id);
1088 while Present (E) loop
1089 Set_Is_Immediately_Visible (E, False);
1090 Set_Is_Potentially_Use_Visible (E, False);
1091 Set_Is_Hidden (E);
1093 -- Child units may appear on the entity list (e.g. if they appear
1094 -- in the context of a subunit) but they are not body entities.
1096 if not Is_Child_Unit (E) then
1097 Set_Is_Package_Body_Entity (E);
1098 end if;
1100 Next_Entity (E);
1101 end loop;
1102 end;
1104 Check_References (Body_Id);
1106 -- For a generic unit, check that the formal parameters are referenced,
1107 -- and that local variables are used, as for regular packages.
1109 if Ekind (Spec_Id) = E_Generic_Package then
1110 Check_References (Spec_Id);
1111 end if;
1113 -- At this point all entities of the package body are externally visible
1114 -- to the linker as their Is_Public flag is set to True. This proactive
1115 -- approach is necessary because an inlined or a generic body for which
1116 -- code is generated in other units may need to see these entities. Cut
1117 -- down the number of global symbols that do not need public visibility
1118 -- as this has two beneficial effects:
1119 -- (1) It makes the compilation process more efficient.
1120 -- (2) It gives the code generator more leeway to optimize within each
1121 -- unit, especially subprograms.
1123 -- This is done only for top-level library packages or child units as
1124 -- the algorithm does a top-down traversal of the package body. This is
1125 -- also done for instances because instantiations are still pending by
1126 -- the time the enclosing package body is analyzed.
1128 if (Scope (Spec_Id) = Standard_Standard
1129 or else Is_Child_Unit (Spec_Id)
1130 or else Is_Generic_Instance (Spec_Id))
1131 and then not Is_Generic_Unit (Spec_Id)
1132 then
1133 Hide_Public_Entities (Declarations (N));
1134 end if;
1136 -- If expander is not active, then here is where we turn off the
1137 -- In_Package_Body flag, otherwise it is turned off at the end of the
1138 -- corresponding expansion routine. If this is an instance body, we need
1139 -- to qualify names of local entities, because the body may have been
1140 -- compiled as a preliminary to another instantiation.
1142 if not Expander_Active then
1143 Set_In_Package_Body (Spec_Id, False);
1145 if Is_Generic_Instance (Spec_Id)
1146 and then Operating_Mode = Generate_Code
1147 then
1148 Qualify_Entity_Names (N);
1149 end if;
1150 end if;
1152 if Present (Ignored_Ghost_Region) then
1153 Expander_Active := Saved_EA;
1154 end if;
1156 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
1157 Restore_Ghost_Region (Saved_GM, Saved_IGR);
1158 end Analyze_Package_Body_Helper;
1160 ---------------------------------
1161 -- Analyze_Package_Declaration --
1162 ---------------------------------
1164 procedure Analyze_Package_Declaration (N : Node_Id) is
1165 Id : constant Node_Id := Defining_Entity (N);
1167 Is_Comp_Unit : constant Boolean :=
1168 Nkind (Parent (N)) = N_Compilation_Unit;
1170 Body_Required : Boolean;
1171 -- True when this package declaration requires a corresponding body
1173 begin
1174 if Debug_Flag_C then
1175 Write_Str ("==> package spec ");
1176 Write_Name (Chars (Id));
1177 Write_Str (" from ");
1178 Write_Location (Sloc (N));
1179 Write_Eol;
1180 Indent;
1181 end if;
1183 Generate_Definition (Id);
1184 Enter_Name (Id);
1185 Mutate_Ekind (Id, E_Package);
1186 Set_Is_Not_Self_Hidden (Id);
1187 -- Needed early because of Set_Categorization_From_Pragmas below
1188 Set_Etype (Id, Standard_Void_Type);
1190 -- Set SPARK_Mode from context
1192 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
1193 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
1194 Set_SPARK_Pragma_Inherited (Id);
1195 Set_SPARK_Aux_Pragma_Inherited (Id);
1197 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
1198 -- the body of this package is instantiated or inlined later and out of
1199 -- context. The body uses this attribute to restore the value of the
1200 -- global flag.
1202 if Ignore_SPARK_Mode_Pragmas_In_Instance then
1203 Set_Ignore_SPARK_Mode_Pragmas (Id);
1204 end if;
1206 -- Analyze aspect specifications immediately, since we need to recognize
1207 -- things like Pure early enough to diagnose violations during analysis.
1209 Analyze_Aspect_Specifications (N, Id);
1211 -- Ada 2005 (AI-217): Check if the package has been illegally named in
1212 -- a limited-with clause of its own context. In this case the error has
1213 -- been previously notified by Analyze_Context.
1215 -- limited with Pkg; -- ERROR
1216 -- package Pkg is ...
1218 if From_Limited_With (Id) then
1219 return;
1220 end if;
1222 Push_Scope (Id);
1224 Set_Is_Pure (Id, Is_Pure (Enclosing_Lib_Unit_Entity));
1225 Set_Categorization_From_Pragmas (N);
1227 Analyze (Specification (N));
1228 Validate_Categorization_Dependency (N, Id);
1230 -- Determine whether the package requires a body. Abstract states are
1231 -- intentionally ignored because they do require refinement which can
1232 -- only come in a body, but at the same time they do not force the need
1233 -- for a body on their own (SPARK RM 7.1.4(4) and 7.2.2(3)).
1235 Body_Required := Unit_Requires_Body (Id);
1237 if not Body_Required then
1239 -- If the package spec does not require an explicit body, then there
1240 -- are not entities requiring completion in the language sense. Call
1241 -- Check_Completion now to ensure that nested package declarations
1242 -- that require an implicit body get one. (In the case where a body
1243 -- is required, Check_Completion is called at the end of the body's
1244 -- declarative part.)
1246 Check_Completion;
1248 -- If the package spec does not require an explicit body, then all
1249 -- abstract states declared in nested packages cannot possibly get a
1250 -- proper refinement (SPARK RM 7.1.4(4) and SPARK RM 7.2.2(3)). This
1251 -- check is performed only when the compilation unit is the main
1252 -- unit to allow for modular SPARK analysis where packages do not
1253 -- necessarily have bodies.
1255 if Is_Comp_Unit then
1256 Check_State_Refinements
1257 (Context => N,
1258 Is_Main_Unit => Parent (N) = Cunit (Main_Unit));
1259 end if;
1261 -- For package declarations at the library level, warn about
1262 -- references to unset objects, which is straightforward for packages
1263 -- with no bodies. For packages with bodies this is more complicated,
1264 -- because some of the objects might be set between spec and body
1265 -- elaboration, in nested or child packages, etc. Note that the
1266 -- recursive calls in Check_References will handle nested package
1267 -- specifications.
1269 if Is_Library_Level_Entity (Id) then
1270 Check_References (Id);
1271 end if;
1272 end if;
1274 -- Set Body_Required indication on the compilation unit node
1276 if Is_Comp_Unit then
1277 Set_Body_Required (Parent (N), Body_Required);
1279 if Legacy_Elaboration_Checks and not Body_Required then
1280 Set_Suppress_Elaboration_Warnings (Id);
1281 end if;
1282 end if;
1284 End_Package_Scope (Id);
1286 -- For the declaration of a library unit that is a remote types package,
1287 -- check legality rules regarding availability of stream attributes for
1288 -- types that contain non-remote access values. This subprogram performs
1289 -- visibility tests that rely on the fact that we have exited the scope
1290 -- of Id.
1292 if Is_Comp_Unit then
1293 Validate_RT_RAT_Component (N);
1294 end if;
1296 if Debug_Flag_C then
1297 Outdent;
1298 Write_Str ("<== package spec ");
1299 Write_Name (Chars (Id));
1300 Write_Str (" from ");
1301 Write_Location (Sloc (N));
1302 Write_Eol;
1303 end if;
1304 end Analyze_Package_Declaration;
1306 -----------------------------------
1307 -- Analyze_Package_Specification --
1308 -----------------------------------
1310 -- Note that this code is shared for the analysis of generic package specs
1311 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
1313 procedure Analyze_Package_Specification (N : Node_Id) is
1314 Id : constant Entity_Id := Defining_Entity (N);
1315 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
1316 Vis_Decls : constant List_Id := Visible_Declarations (N);
1317 Priv_Decls : constant List_Id := Private_Declarations (N);
1318 E : Entity_Id;
1319 L : Entity_Id;
1320 Public_Child : Boolean;
1322 Private_With_Clauses_Installed : Boolean := False;
1323 -- In Ada 2005, private with_clauses are visible in the private part
1324 -- of a nested package, even if it appears in the public part of the
1325 -- enclosing package. This requires a separate step to install these
1326 -- private_with_clauses, and remove them at the end of the nested
1327 -- package.
1329 procedure Clear_Constants (Id : Entity_Id);
1330 -- Clears constant indications (Never_Set_In_Source, Constant_Value,
1331 -- and Is_True_Constant) on all variables that are entities of Id.
1332 -- A recursive call is made for all packages and generic packages.
1334 procedure Generate_Parent_References;
1335 -- For a child unit, generate references to parent units, for
1336 -- GNAT Studio navigation purposes.
1338 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
1339 -- Child and Unit are entities of compilation units. True if Child
1340 -- is a public child of Parent as defined in 10.1.1
1342 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
1343 -- Reject completion of an incomplete or private type declarations
1344 -- having a known discriminant part by an unchecked union.
1346 procedure Inspect_Untagged_Record_Completion (Decls : List_Id);
1347 -- Find out whether a nonlimited untagged record completion has got a
1348 -- primitive equality operator and, if so, make it so that it will be
1349 -- used as the predefined operator of the private view of the record.
1351 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
1352 -- Given the package entity of a generic package instantiation or
1353 -- formal package whose corresponding generic is a child unit, installs
1354 -- the private declarations of each of the child unit's parents.
1355 -- This has to be done at the point of entering the instance package's
1356 -- private part rather than being done in Sem_Ch12.Install_Parent
1357 -- (which is where the parents' visible declarations are installed).
1359 ---------------------
1360 -- Clear_Constants --
1361 ---------------------
1363 procedure Clear_Constants (Id : Entity_Id) is
1364 E : Entity_Id;
1366 begin
1367 -- Ignore package renamings, not interesting and they can cause self
1368 -- referential loops in the code below.
1370 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
1371 return;
1372 end if;
1374 -- Note: in the loop below, the check for Next_Entity pointing back
1375 -- to the package entity may seem odd, but it is needed, because a
1376 -- package can contain a renaming declaration to itself, and such
1377 -- renamings are generated automatically within package instances.
1379 E := First_Entity (Id);
1380 while Present (E) and then E /= Id loop
1381 if Ekind (E) = E_Variable then
1382 Set_Never_Set_In_Source (E, False);
1383 Set_Is_True_Constant (E, False);
1384 Set_Current_Value (E, Empty);
1385 Set_Is_Known_Null (E, False);
1386 Set_Last_Assignment (E, Empty);
1388 if not Can_Never_Be_Null (E) then
1389 Set_Is_Known_Non_Null (E, False);
1390 end if;
1392 elsif Is_Package_Or_Generic_Package (E) then
1393 Clear_Constants (E);
1394 end if;
1396 Next_Entity (E);
1397 end loop;
1398 end Clear_Constants;
1400 --------------------------------
1401 -- Generate_Parent_References --
1402 --------------------------------
1404 procedure Generate_Parent_References is
1405 Decl : constant Node_Id := Parent (N);
1407 begin
1408 if Id = Cunit_Entity (Main_Unit)
1409 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
1410 then
1411 Generate_Reference (Id, Scope (Id), 'k', False);
1413 elsif Nkind (Unit (Cunit (Main_Unit))) not in
1414 N_Subprogram_Body | N_Subunit
1415 then
1416 -- If current unit is an ancestor of main unit, generate a
1417 -- reference to its own parent.
1419 declare
1420 U : Node_Id;
1421 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
1423 begin
1424 if Nkind (Main_Spec) = N_Package_Body then
1425 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
1426 end if;
1428 U := Parent_Spec (Main_Spec);
1429 while Present (U) loop
1430 if U = Parent (Decl) then
1431 Generate_Reference (Id, Scope (Id), 'k', False);
1432 exit;
1434 elsif Nkind (Unit (U)) = N_Package_Body then
1435 exit;
1437 else
1438 U := Parent_Spec (Unit (U));
1439 end if;
1440 end loop;
1441 end;
1442 end if;
1443 end Generate_Parent_References;
1445 ---------------------
1446 -- Is_Public_Child --
1447 ---------------------
1449 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
1450 begin
1451 if not Is_Private_Descendant (Child) then
1452 return True;
1453 else
1454 if Child = Unit then
1455 return not Private_Present (
1456 Parent (Unit_Declaration_Node (Child)));
1457 else
1458 return Is_Public_Child (Scope (Child), Unit);
1459 end if;
1460 end if;
1461 end Is_Public_Child;
1463 ----------------------------------------
1464 -- Inspect_Unchecked_Union_Completion --
1465 ----------------------------------------
1467 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
1468 Decl : Node_Id;
1470 begin
1471 Decl := First (Decls);
1472 while Present (Decl) loop
1474 -- We are looking for an incomplete or private type declaration
1475 -- with a known_discriminant_part whose full view is an
1476 -- Unchecked_Union. The seemingly useless check with Is_Type
1477 -- prevents cascaded errors when routines defined only for type
1478 -- entities are called with non-type entities.
1480 if Nkind (Decl) in N_Incomplete_Type_Declaration
1481 | N_Private_Type_Declaration
1482 and then Is_Type (Defining_Identifier (Decl))
1483 and then Has_Discriminants (Defining_Identifier (Decl))
1484 and then Present (Full_View (Defining_Identifier (Decl)))
1485 and then
1486 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
1487 then
1488 Error_Msg_N
1489 ("completion of discriminated partial view "
1490 & "cannot be an unchecked union",
1491 Full_View (Defining_Identifier (Decl)));
1492 end if;
1494 Next (Decl);
1495 end loop;
1496 end Inspect_Unchecked_Union_Completion;
1498 ----------------------------------------
1499 -- Inspect_Untagged_Record_Completion --
1500 ----------------------------------------
1502 procedure Inspect_Untagged_Record_Completion (Decls : List_Id) is
1503 Decl : Node_Id;
1505 begin
1506 Decl := First (Decls);
1507 while Present (Decl) loop
1509 -- We are looking for a full type declaration of an untagged
1510 -- record with a private declaration and primitive operations.
1512 if Nkind (Decl) in N_Full_Type_Declaration
1513 and then Is_Record_Type (Defining_Identifier (Decl))
1514 and then not Is_Limited_Type (Defining_Identifier (Decl))
1515 and then not Is_Tagged_Type (Defining_Identifier (Decl))
1516 and then Has_Private_Declaration (Defining_Identifier (Decl))
1517 and then Has_Primitive_Operations (Defining_Identifier (Decl))
1518 then
1519 declare
1520 Prim_List : constant Elist_Id :=
1521 Collect_Primitive_Operations (Defining_Identifier (Decl));
1523 E : Entity_Id;
1524 Ne_Id : Entity_Id;
1525 Op_Decl : Node_Id;
1526 Op_Id : Entity_Id;
1527 Prim : Elmt_Id;
1529 begin
1530 Prim := First_Elmt (Prim_List);
1531 while Present (Prim) loop
1532 Op_Id := Node (Prim);
1533 Op_Decl := Declaration_Node (Op_Id);
1534 if Nkind (Op_Decl) in N_Subprogram_Specification then
1535 Op_Decl := Parent (Op_Decl);
1536 end if;
1538 -- We are looking for an equality operator immediately
1539 -- visible and declared in the private part followed by
1540 -- the synthesized inequality operator.
1542 if Is_User_Defined_Equality (Op_Id)
1543 and then Is_Immediately_Visible (Op_Id)
1544 and then List_Containing (Op_Decl) = Decls
1545 then
1546 Ne_Id := Next_Entity (Op_Id);
1547 pragma Assert (Ekind (Ne_Id) = E_Function
1548 and then Corresponding_Equality (Ne_Id) = Op_Id);
1550 E := First_Private_Entity (Id);
1552 -- Move them from the private part of the entity list
1553 -- up to the end of the visible part of the same list.
1555 Remove_Entity (Op_Id);
1556 Remove_Entity (Ne_Id);
1558 Link_Entities (Prev_Entity (E), Op_Id);
1559 Link_Entities (Op_Id, Ne_Id);
1560 Link_Entities (Ne_Id, E);
1562 -- And if the private part contains another equality
1563 -- operator, move the equality operator to after it
1564 -- in the homonym chain, so that all its next homonyms
1565 -- in the same scope, if any, also are in the visible
1566 -- part. This is relied upon to resolve expanded names
1567 -- in Collect_Interps for example.
1569 while Present (E) loop
1570 exit when Ekind (E) = E_Function
1571 and then Chars (E) = Name_Op_Eq;
1573 Next_Entity (E);
1574 end loop;
1576 if Present (E) then
1577 Remove_Homonym (Op_Id);
1579 Set_Homonym (Op_Id, Homonym (E));
1580 Set_Homonym (E, Op_Id);
1581 end if;
1583 exit;
1584 end if;
1586 Next_Elmt (Prim);
1587 end loop;
1588 end;
1589 end if;
1591 Next (Decl);
1592 end loop;
1593 end Inspect_Untagged_Record_Completion;
1595 -----------------------------------------
1596 -- Install_Parent_Private_Declarations --
1597 -----------------------------------------
1599 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
1600 Inst_Par : Entity_Id;
1601 Gen_Par : Entity_Id;
1602 Inst_Node : Node_Id;
1604 begin
1605 Inst_Par := Inst_Id;
1607 Gen_Par :=
1608 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
1609 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
1610 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
1612 if Nkind (Inst_Node) in
1613 N_Package_Instantiation | N_Formal_Package_Declaration
1614 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
1615 then
1616 Inst_Par := Entity (Prefix (Name (Inst_Node)));
1618 if Present (Renamed_Entity (Inst_Par)) then
1619 Inst_Par := Renamed_Entity (Inst_Par);
1620 end if;
1622 -- The instance may appear in a sibling generic unit, in
1623 -- which case the prefix must include the common (generic)
1624 -- ancestor, which is treated as a current instance.
1626 if Inside_A_Generic
1627 and then Ekind (Inst_Par) = E_Generic_Package
1628 then
1629 Gen_Par := Inst_Par;
1630 pragma Assert (In_Open_Scopes (Gen_Par));
1632 else
1633 Gen_Par :=
1634 Generic_Parent
1635 (Specification (Unit_Declaration_Node (Inst_Par)));
1636 end if;
1638 -- Install the private declarations and private use clauses
1639 -- of a parent instance of the child instance, unless the
1640 -- parent instance private declarations have already been
1641 -- installed earlier in Analyze_Package_Specification, which
1642 -- happens when a generic child is instantiated, and the
1643 -- instance is a child of the parent instance.
1645 -- Installing the use clauses of the parent instance twice
1646 -- is both unnecessary and wrong, because it would cause the
1647 -- clauses to be chained to themselves in the use clauses
1648 -- list of the scope stack entry. That in turn would cause
1649 -- an endless loop from End_Use_Clauses upon scope exit.
1651 -- The parent is now fully visible. It may be a hidden open
1652 -- scope if we are currently compiling some child instance
1653 -- declared within it, but while the current instance is being
1654 -- compiled the parent is immediately visible. In particular
1655 -- its entities must remain visible if a stack save/restore
1656 -- takes place through a call to Rtsfind.
1658 if Present (Gen_Par) then
1659 if not In_Private_Part (Inst_Par) then
1660 Install_Private_Declarations (Inst_Par);
1661 Set_Use (Private_Declarations
1662 (Specification
1663 (Unit_Declaration_Node (Inst_Par))));
1664 Set_Is_Hidden_Open_Scope (Inst_Par, False);
1665 end if;
1667 -- If we've reached the end of the generic instance parents,
1668 -- then finish off by looping through the nongeneric parents
1669 -- and installing their private declarations.
1671 -- If one of the non-generic parents is itself on the scope
1672 -- stack, do not install its private declarations: they are
1673 -- installed in due time when the private part of that parent
1674 -- is analyzed.
1676 else
1677 while Present (Inst_Par)
1678 and then Inst_Par /= Standard_Standard
1679 and then (not In_Open_Scopes (Inst_Par)
1680 or else not In_Private_Part (Inst_Par))
1681 loop
1682 if Nkind (Inst_Node) = N_Formal_Package_Declaration
1683 or else
1684 not Is_Ancestor_Package
1685 (Inst_Par, Cunit_Entity (Current_Sem_Unit))
1686 then
1687 Install_Private_Declarations (Inst_Par);
1688 Set_Use
1689 (Private_Declarations
1690 (Specification
1691 (Unit_Declaration_Node (Inst_Par))));
1692 Inst_Par := Scope (Inst_Par);
1693 else
1694 exit;
1695 end if;
1696 end loop;
1698 exit;
1699 end if;
1701 else
1702 exit;
1703 end if;
1704 end loop;
1705 end Install_Parent_Private_Declarations;
1707 -- Start of processing for Analyze_Package_Specification
1709 begin
1710 if Present (Vis_Decls) then
1711 Analyze_Declarations (Vis_Decls);
1712 end if;
1714 -- Inspect the entities defined in the package and ensure that all
1715 -- incomplete types have received full declarations. Build default
1716 -- initial condition and invariant procedures for all qualifying types.
1718 E := First_Entity (Id);
1719 while Present (E) loop
1721 -- Check on incomplete types
1723 -- AI05-0213: A formal incomplete type has no completion, and neither
1724 -- does the corresponding subtype in an instance.
1726 if Is_Incomplete_Type (E)
1727 and then No (Full_View (E))
1728 and then not Is_Generic_Type (E)
1729 and then not From_Limited_With (E)
1730 and then not Is_Generic_Actual_Type (E)
1731 then
1732 Error_Msg_N ("no declaration in visible part for incomplete}", E);
1733 end if;
1735 Next_Entity (E);
1736 end loop;
1738 if Is_Remote_Call_Interface (Id)
1739 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1740 then
1741 Validate_RCI_Declarations (Id);
1742 end if;
1744 -- Save global references in the visible declarations, before installing
1745 -- private declarations of parent unit if there is one, because the
1746 -- privacy status of types defined in the parent will change. This is
1747 -- only relevant for generic child units, but is done in all cases for
1748 -- uniformity.
1750 if Ekind (Id) = E_Generic_Package
1751 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1752 then
1753 declare
1754 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1755 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1757 begin
1758 -- Insert the freezing nodes after the visible declarations to
1759 -- ensure that we analyze its aspects; needed to ensure that
1760 -- global entities referenced in the aspects are properly handled.
1762 if Ada_Version >= Ada_2012
1763 and then Is_Non_Empty_List (Vis_Decls)
1764 and then Is_Empty_List (Priv_Decls)
1765 then
1766 Insert_List_After_And_Analyze
1767 (Last (Vis_Decls), Freeze_Entity (Id, Last (Vis_Decls)));
1768 end if;
1770 Set_Private_Declarations (Orig_Spec, Empty_List);
1771 Save_Global_References (Orig_Decl);
1772 Set_Private_Declarations (Orig_Spec, Save_Priv);
1773 end;
1774 end if;
1776 -- If package is a public child unit, then make the private declarations
1777 -- of the parent visible.
1779 Public_Child := False;
1781 declare
1782 Par : Entity_Id;
1783 Pack_Decl : Node_Id;
1784 Par_Spec : Node_Id;
1786 begin
1787 Par := Id;
1788 Par_Spec := Parent_Spec (Parent (N));
1790 -- If the package is formal package of an enclosing generic, it is
1791 -- transformed into a local generic declaration, and compiled to make
1792 -- its spec available. We need to retrieve the original generic to
1793 -- determine whether it is a child unit, and install its parents.
1795 if No (Par_Spec)
1796 and then
1797 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1798 then
1799 Par := Entity (Name (Original_Node (Parent (N))));
1800 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1801 end if;
1803 if Present (Par_Spec) then
1804 Generate_Parent_References;
1806 while Scope (Par) /= Standard_Standard
1807 and then Is_Public_Child (Id, Par)
1808 and then In_Open_Scopes (Par)
1809 loop
1810 Public_Child := True;
1811 Par := Scope (Par);
1812 Install_Private_Declarations (Par);
1813 Install_Private_With_Clauses (Par);
1814 Pack_Decl := Unit_Declaration_Node (Par);
1815 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1816 end loop;
1817 end if;
1818 end;
1820 if Is_Compilation_Unit (Id) then
1821 Install_Private_With_Clauses (Id);
1822 else
1823 -- The current compilation unit may include private with_clauses,
1824 -- which are visible in the private part of the current nested
1825 -- package, and have to be installed now. This is not done for
1826 -- nested instantiations, where the private with_clauses of the
1827 -- enclosing unit have no effect once the instantiation info is
1828 -- established and we start analyzing the package declaration.
1830 declare
1831 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1832 begin
1833 if Is_Package_Or_Generic_Package (Comp_Unit)
1834 and then not In_Private_Part (Comp_Unit)
1835 and then not In_Instance
1836 then
1837 Install_Private_With_Clauses (Comp_Unit);
1838 Private_With_Clauses_Installed := True;
1839 end if;
1840 end;
1841 end if;
1843 -- If this is a package associated with a generic instance or formal
1844 -- package, then the private declarations of each of the generic's
1845 -- parents must be installed at this point, but not if this is the
1846 -- abbreviated instance created to check a formal package, see the
1847 -- same condition in Analyze_Package_Instantiation.
1849 if Is_Generic_Instance (Id)
1850 and then not Is_Abbreviated_Instance (Id)
1851 then
1852 Install_Parent_Private_Declarations (Id);
1853 end if;
1855 -- Analyze private part if present. The flag In_Private_Part is reset
1856 -- in Uninstall_Declarations.
1858 L := Last_Entity (Id);
1860 if Present (Priv_Decls) then
1861 Set_In_Private_Part (Id);
1863 -- Upon entering a public child's private part, it may be necessary
1864 -- to declare subprograms that were derived in the package's visible
1865 -- part but not yet made visible.
1867 if Public_Child then
1868 Declare_Inherited_Private_Subprograms (Id);
1869 end if;
1871 Analyze_Declarations (Priv_Decls);
1873 -- Check the private declarations for incomplete deferred constants
1875 Inspect_Deferred_Constant_Completion (Priv_Decls);
1877 -- The first private entity is the immediate follower of the last
1878 -- visible entity, if there was one.
1880 if Present (L) then
1881 Set_First_Private_Entity (Id, Next_Entity (L));
1882 else
1883 Set_First_Private_Entity (Id, First_Entity (Id));
1884 end if;
1886 -- There may be inherited private subprograms that need to be declared,
1887 -- even in the absence of an explicit private part. If there are any
1888 -- public declarations in the package and the package is a public child
1889 -- unit, then an implicit private part is assumed.
1891 elsif Present (L) and then Public_Child then
1892 Set_In_Private_Part (Id);
1893 Declare_Inherited_Private_Subprograms (Id);
1894 Set_First_Private_Entity (Id, Next_Entity (L));
1895 end if;
1897 E := First_Entity (Id);
1898 while Present (E) loop
1900 -- Check rule of 3.6(11), which in general requires waiting till all
1901 -- full types have been seen.
1903 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1904 Check_Aliased_Component_Types (E);
1905 end if;
1907 -- Check preelaborable initialization for full type completing a
1908 -- private type when aspect Preelaborable_Initialization is True
1909 -- or is specified by Preelaborable_Initialization attributes
1910 -- (in the case of a private type in a generic unit). We pass
1911 -- the expression of the aspect (when present) to the parameter
1912 -- Preelab_Init_Expr to take into account the rule that presumes
1913 -- that subcomponents of generic formal types mentioned in the
1914 -- type's P_I aspect have preelaborable initialization (see
1915 -- AI12-0409 and RM 10.2.1(11.8/5)).
1917 if Is_Type (E) and then Must_Have_Preelab_Init (E) then
1918 declare
1919 PI_Aspect : constant Node_Id :=
1920 Find_Aspect
1921 (E, Aspect_Preelaborable_Initialization);
1922 PI_Expr : Node_Id := Empty;
1923 begin
1924 if Present (PI_Aspect) then
1925 PI_Expr := Expression (PI_Aspect);
1926 end if;
1928 if not Has_Preelaborable_Initialization
1929 (E, Preelab_Init_Expr => PI_Expr)
1930 then
1931 Error_Msg_N
1932 ("full view of & does not have "
1933 & "preelaborable initialization", E);
1934 end if;
1935 end;
1936 end if;
1938 -- Preanalyze class-wide conditions of dispatching primitives defined
1939 -- in nested packages. For library packages, class-wide pre- and
1940 -- postconditions are preanalyzed when the primitives are frozen
1941 -- (see Merge_Class_Conditions); for nested packages, the end of the
1942 -- package does not cause freezing (and hence they must be analyzed
1943 -- now to ensure the correct visibility of referenced entities).
1945 if not Is_Compilation_Unit (Id)
1946 and then Is_Dispatching_Operation (E)
1947 and then Present (Contract (E))
1948 then
1949 Preanalyze_Class_Conditions (E);
1950 end if;
1952 Next_Entity (E);
1953 end loop;
1955 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1956 -- declaration having a known_discriminant_part shall not be an
1957 -- unchecked union type.
1959 if Present (Vis_Decls) then
1960 Inspect_Unchecked_Union_Completion (Vis_Decls);
1961 end if;
1963 if Present (Priv_Decls) then
1964 Inspect_Unchecked_Union_Completion (Priv_Decls);
1965 end if;
1967 -- Implement AI12-0101 (which only removes a legality rule) and then
1968 -- AI05-0123 (which directly applies in the previously illegal case)
1969 -- in Ada 2012. Note that AI12-0101 is a binding interpretation.
1971 if Present (Priv_Decls) and then Ada_Version >= Ada_2012 then
1972 Inspect_Untagged_Record_Completion (Priv_Decls);
1973 end if;
1975 if Ekind (Id) = E_Generic_Package
1976 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1977 and then Present (Priv_Decls)
1978 then
1979 -- Save global references in private declarations, ignoring the
1980 -- visible declarations that were processed earlier.
1982 declare
1983 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1984 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1985 Save_Form : constant List_Id :=
1986 Generic_Formal_Declarations (Orig_Decl);
1988 begin
1989 -- Insert the freezing nodes after the private declarations to
1990 -- ensure that we analyze its aspects; needed to ensure that
1991 -- global entities referenced in the aspects are properly handled.
1993 if Ada_Version >= Ada_2012
1994 and then Is_Non_Empty_List (Priv_Decls)
1995 then
1996 Insert_List_After_And_Analyze
1997 (Last (Priv_Decls), Freeze_Entity (Id, Last (Priv_Decls)));
1998 end if;
2000 Set_Visible_Declarations (Orig_Spec, Empty_List);
2001 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
2002 Save_Global_References (Orig_Decl);
2003 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
2004 Set_Visible_Declarations (Orig_Spec, Save_Vis);
2005 end;
2006 end if;
2008 Process_End_Label (N, 'e', Id);
2010 -- Remove private_with_clauses of enclosing compilation unit, if they
2011 -- were installed.
2013 if Private_With_Clauses_Installed then
2014 Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
2015 end if;
2017 -- For the case of a library level package, we must go through all the
2018 -- entities clearing the indications that the value may be constant and
2019 -- not modified. Why? Because any client of this package may modify
2020 -- these values freely from anywhere. This also applies to any nested
2021 -- packages or generic packages.
2023 -- For now we unconditionally clear constants for packages that are
2024 -- instances of generic packages. The reason is that we do not have the
2025 -- body yet, and we otherwise think things are unreferenced when they
2026 -- are not. This should be fixed sometime (the effect is not terrible,
2027 -- we just lose some warnings, and also some cases of value propagation)
2028 -- ???
2030 if Is_Library_Level_Entity (Id)
2031 or else Is_Generic_Instance (Id)
2032 then
2033 Clear_Constants (Id);
2034 end if;
2036 -- Output relevant information as to why the package requires a body.
2037 -- Do not consider generated packages as this exposes internal symbols
2038 -- and leads to confusing messages.
2040 if List_Body_Required_Info
2041 and then In_Extended_Main_Source_Unit (Id)
2042 and then Unit_Requires_Body (Id)
2043 and then Comes_From_Source (Id)
2044 then
2045 Unit_Requires_Body_Info (Id);
2046 end if;
2048 -- Nested package specs that do not require bodies are not checked for
2049 -- ineffective use clauses due to the possibility of subunits. This is
2050 -- because at this stage it is impossible to tell whether there will be
2051 -- a separate body.
2053 if not Unit_Requires_Body (Id)
2054 and then Is_Compilation_Unit (Id)
2055 and then not Is_Private_Descendant (Id)
2056 then
2057 Update_Use_Clause_Chain;
2058 end if;
2059 end Analyze_Package_Specification;
2061 --------------------------------------
2062 -- Analyze_Private_Type_Declaration --
2063 --------------------------------------
2065 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
2066 Id : constant Entity_Id := Defining_Identifier (N);
2067 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
2069 begin
2070 Generate_Definition (Id);
2071 Set_Is_Pure (Id, PF);
2072 Reinit_Size_Align (Id);
2074 if not Is_Package_Or_Generic_Package (Current_Scope)
2075 or else In_Private_Part (Current_Scope)
2076 then
2077 Error_Msg_N ("invalid context for private declaration", N);
2078 end if;
2080 New_Private_Type (N, Id, N);
2081 Set_Depends_On_Private (Id);
2083 -- Set the SPARK mode from the current context
2085 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
2086 Set_SPARK_Pragma_Inherited (Id);
2088 Analyze_Aspect_Specifications (N, Id);
2089 end Analyze_Private_Type_Declaration;
2091 ----------------------------------
2092 -- Check_Anonymous_Access_Types --
2093 ----------------------------------
2095 procedure Check_Anonymous_Access_Types
2096 (Spec_Id : Entity_Id;
2097 P_Body : Node_Id)
2099 E : Entity_Id;
2100 IR : Node_Id;
2102 begin
2103 -- Itype references are only needed by gigi, to force elaboration of
2104 -- itypes. In the absence of code generation, they are not needed.
2106 if not Expander_Active then
2107 return;
2108 end if;
2110 E := First_Entity (Spec_Id);
2111 while Present (E) loop
2112 if Ekind (E) = E_Anonymous_Access_Type
2113 and then From_Limited_With (E)
2114 then
2115 IR := Make_Itype_Reference (Sloc (P_Body));
2116 Set_Itype (IR, E);
2118 if No (Declarations (P_Body)) then
2119 Set_Declarations (P_Body, New_List (IR));
2120 else
2121 Prepend (IR, Declarations (P_Body));
2122 end if;
2123 end if;
2125 Next_Entity (E);
2126 end loop;
2127 end Check_Anonymous_Access_Types;
2129 -------------------------------------------
2130 -- Declare_Inherited_Private_Subprograms --
2131 -------------------------------------------
2133 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
2135 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
2136 -- Check whether an inherited subprogram S is an operation of an
2137 -- untagged derived type T.
2139 ---------------------
2140 -- Is_Primitive_Of --
2141 ---------------------
2143 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
2144 Formal : Entity_Id;
2146 begin
2147 -- If the full view is a scalar type, the type is the anonymous base
2148 -- type, but the operation mentions the first subtype, so check the
2149 -- signature against the base type.
2151 if Base_Type (Etype (S)) = Base_Type (T) then
2152 return True;
2154 else
2155 Formal := First_Formal (S);
2156 while Present (Formal) loop
2157 if Base_Type (Etype (Formal)) = Base_Type (T) then
2158 return True;
2159 end if;
2161 Next_Formal (Formal);
2162 end loop;
2164 return False;
2165 end if;
2166 end Is_Primitive_Of;
2168 -- Local variables
2170 E : Entity_Id;
2171 Op_List : Elist_Id;
2172 Op_Elmt : Elmt_Id;
2173 Op_Elmt_2 : Elmt_Id;
2174 Prim_Op : Entity_Id;
2175 New_Op : Entity_Id := Empty;
2176 Parent_Subp : Entity_Id;
2177 Tag : Entity_Id;
2179 -- Start of processing for Declare_Inherited_Private_Subprograms
2181 begin
2182 E := First_Entity (Id);
2183 while Present (E) loop
2185 -- If the entity is a nonprivate type extension whose parent type
2186 -- is declared in an open scope, then the type may have inherited
2187 -- operations that now need to be made visible. Ditto if the entity
2188 -- is a formal derived type in a child unit.
2190 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
2191 or else
2192 (Nkind (Parent (E)) = N_Private_Extension_Declaration
2193 and then Is_Generic_Type (E)))
2194 and then In_Open_Scopes (Scope (Etype (E)))
2195 and then Is_Base_Type (E)
2196 then
2197 if Is_Tagged_Type (E) then
2198 Op_List := Primitive_Operations (E);
2199 New_Op := Empty;
2200 Tag := First_Tag_Component (E);
2202 Op_Elmt := First_Elmt (Op_List);
2203 while Present (Op_Elmt) loop
2204 Prim_Op := Node (Op_Elmt);
2206 -- Search primitives that are implicit operations with an
2207 -- internal name whose parent operation has a normal name.
2209 if Present (Alias (Prim_Op))
2210 and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
2211 and then not Comes_From_Source (Prim_Op)
2212 and then Is_Internal_Name (Chars (Prim_Op))
2213 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
2214 then
2215 Parent_Subp := Alias (Prim_Op);
2217 -- Case 1: Check if the type has also an explicit
2218 -- overriding for this primitive.
2220 Op_Elmt_2 := Next_Elmt (Op_Elmt);
2221 while Present (Op_Elmt_2) loop
2223 -- Skip entities with attribute Interface_Alias since
2224 -- they are not overriding primitives (these entities
2225 -- link an interface primitive with their covering
2226 -- primitive)
2228 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
2229 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
2230 and then No (Interface_Alias (Node (Op_Elmt_2)))
2231 then
2232 -- The private inherited operation has been
2233 -- overridden by an explicit subprogram:
2234 -- replace the former by the latter.
2236 New_Op := Node (Op_Elmt_2);
2237 Replace_Elmt (Op_Elmt, New_Op);
2238 Remove_Elmt (Op_List, Op_Elmt_2);
2239 Set_Overridden_Operation (New_Op, Parent_Subp);
2240 Set_Is_Ada_2022_Only (New_Op,
2241 Is_Ada_2022_Only (Parent_Subp));
2243 -- We don't need to inherit its dispatching slot.
2244 -- Set_All_DT_Position has previously ensured that
2245 -- the same slot was assigned to the two primitives
2247 if Present (Tag)
2248 and then Present (DTC_Entity (New_Op))
2249 and then Present (DTC_Entity (Prim_Op))
2250 then
2251 pragma Assert
2252 (DT_Position (New_Op) = DT_Position (Prim_Op));
2253 null;
2254 end if;
2256 goto Next_Primitive;
2257 end if;
2259 Next_Elmt (Op_Elmt_2);
2260 end loop;
2262 -- Case 2: We have not found any explicit overriding and
2263 -- hence we need to declare the operation (i.e., make it
2264 -- visible).
2266 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2268 -- Inherit the dispatching slot if E is already frozen
2270 if Is_Frozen (E)
2271 and then Present (DTC_Entity (Alias (Prim_Op)))
2272 then
2273 Set_DTC_Entity_Value (E, New_Op);
2274 Set_DT_Position_Value (New_Op,
2275 DT_Position (Alias (Prim_Op)));
2276 end if;
2278 pragma Assert
2279 (Is_Dispatching_Operation (New_Op)
2280 and then Node (Last_Elmt (Op_List)) = New_Op);
2282 -- Substitute the new operation for the old one in the
2283 -- type's primitive operations list. Since the new
2284 -- operation was also just added to the end of list,
2285 -- the last element must be removed.
2287 -- (Question: is there a simpler way of declaring the
2288 -- operation, say by just replacing the name of the
2289 -- earlier operation, reentering it in the in the symbol
2290 -- table (how?), and marking it as private???)
2292 Replace_Elmt (Op_Elmt, New_Op);
2293 Remove_Last_Elmt (Op_List);
2294 end if;
2296 <<Next_Primitive>>
2297 Next_Elmt (Op_Elmt);
2298 end loop;
2300 -- Generate listing showing the contents of the dispatch table
2302 if Debug_Flag_ZZ then
2303 Write_DT (E);
2304 end if;
2306 else
2307 -- For untagged type, scan forward to locate inherited hidden
2308 -- operations.
2310 Prim_Op := Next_Entity (E);
2311 while Present (Prim_Op) loop
2312 if Is_Subprogram (Prim_Op)
2313 and then Present (Alias (Prim_Op))
2314 and then not Comes_From_Source (Prim_Op)
2315 and then Is_Internal_Name (Chars (Prim_Op))
2316 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
2317 and then Is_Primitive_Of (E, Prim_Op)
2318 then
2319 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2320 end if;
2322 Next_Entity (Prim_Op);
2324 -- Derived operations appear immediately after the type
2325 -- declaration (or the following subtype indication for
2326 -- a derived scalar type). Further declarations cannot
2327 -- include inherited operations of the type.
2329 exit when Present (Prim_Op)
2330 and then not Is_Overloadable (Prim_Op);
2331 end loop;
2332 end if;
2333 end if;
2335 Next_Entity (E);
2336 end loop;
2337 end Declare_Inherited_Private_Subprograms;
2339 -----------------------
2340 -- End_Package_Scope --
2341 -----------------------
2343 procedure End_Package_Scope (P : Entity_Id) is
2344 begin
2345 Uninstall_Declarations (P);
2346 Pop_Scope;
2347 end End_Package_Scope;
2349 ---------------------------
2350 -- Exchange_Declarations --
2351 ---------------------------
2353 procedure Exchange_Declarations (Id : Entity_Id) is
2354 Full_Id : constant Entity_Id := Full_View (Id);
2355 H1 : constant Entity_Id := Homonym (Id);
2356 Next1 : constant Entity_Id := Next_Entity (Id);
2357 H2 : Entity_Id;
2358 Next2 : Entity_Id;
2360 begin
2361 -- If missing full declaration for type, nothing to exchange
2363 if No (Full_Id) then
2364 return;
2365 end if;
2367 -- Otherwise complete the exchange, and preserve semantic links
2369 Next2 := Next_Entity (Full_Id);
2370 H2 := Homonym (Full_Id);
2372 -- Reset full declaration pointer to reflect the switched entities and
2373 -- readjust the next entity chains.
2375 Exchange_Entities (Id, Full_Id);
2377 Link_Entities (Id, Next1);
2378 Set_Homonym (Id, H1);
2380 Set_Full_View (Full_Id, Id);
2381 Link_Entities (Full_Id, Next2);
2382 Set_Homonym (Full_Id, H2);
2383 end Exchange_Declarations;
2385 ----------------------------
2386 -- Install_Package_Entity --
2387 ----------------------------
2389 procedure Install_Package_Entity (Id : Entity_Id) is
2390 begin
2391 if not Is_Internal (Id) then
2392 if Debug_Flag_E then
2393 Write_Str ("Install: ");
2394 Write_Name (Chars (Id));
2395 Write_Eol;
2396 end if;
2398 if Is_Child_Unit (Id) then
2399 null;
2401 -- Do not enter implicitly inherited non-overridden subprograms of
2402 -- a tagged type back into visibility if they have non-conformant
2403 -- homographs (Ada RM 8.3 12.3/2).
2405 elsif Is_Hidden_Non_Overridden_Subpgm (Id) then
2406 null;
2408 else
2409 Set_Is_Immediately_Visible (Id);
2410 end if;
2411 end if;
2412 end Install_Package_Entity;
2414 ----------------------------------
2415 -- Install_Private_Declarations --
2416 ----------------------------------
2418 procedure Install_Private_Declarations (P : Entity_Id) is
2419 Id : Entity_Id;
2420 Full : Entity_Id;
2421 Priv_Deps : Elist_Id;
2423 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
2424 -- When the full view of a private type is made available, we do the
2425 -- same for its private dependents under proper visibility conditions.
2426 -- When compiling a child unit this needs to be done recursively.
2428 -----------------------------
2429 -- Swap_Private_Dependents --
2430 -----------------------------
2432 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
2433 Cunit : Entity_Id;
2434 Deps : Elist_Id;
2435 Priv : Entity_Id;
2436 Priv_Elmt : Elmt_Id;
2437 Is_Priv : Boolean;
2439 begin
2440 Priv_Elmt := First_Elmt (Priv_Deps);
2441 while Present (Priv_Elmt) loop
2442 Priv := Node (Priv_Elmt);
2444 -- Before the exchange, verify that the presence of the Full_View
2445 -- field. This field will be empty if the entity has already been
2446 -- installed due to a previous call.
2448 if Present (Full_View (Priv)) and then Is_Visible_Dependent (Priv)
2449 then
2450 if Is_Private_Type (Priv) then
2451 Cunit := Cunit_Entity (Current_Sem_Unit);
2452 Deps := Private_Dependents (Priv);
2453 Is_Priv := True;
2454 else
2455 Is_Priv := False;
2456 end if;
2458 -- For each subtype that is swapped, we also swap the reference
2459 -- to it in Private_Dependents, to allow access to it when we
2460 -- swap them out in End_Package_Scope.
2462 Replace_Elmt (Priv_Elmt, Full_View (Priv));
2464 -- Ensure that both views of the dependent private subtype are
2465 -- immediately visible if within some open scope. Check full
2466 -- view before exchanging views.
2468 if In_Open_Scopes (Scope (Full_View (Priv))) then
2469 Set_Is_Immediately_Visible (Priv);
2470 end if;
2472 Exchange_Declarations (Priv);
2473 Set_Is_Immediately_Visible
2474 (Priv, In_Open_Scopes (Scope (Priv)));
2476 Set_Is_Potentially_Use_Visible
2477 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
2479 -- Recurse for child units, except in generic child units,
2480 -- which unfortunately handle private_dependents separately.
2481 -- Note that the current unit may not have been analyzed,
2482 -- for example a package body, so we cannot rely solely on
2483 -- the Is_Child_Unit flag, but that's only an optimization.
2485 if Is_Priv
2486 and then (No (Etype (Cunit)) or else Is_Child_Unit (Cunit))
2487 and then not Is_Empty_Elmt_List (Deps)
2488 and then not Inside_A_Generic
2489 then
2490 Swap_Private_Dependents (Deps);
2491 end if;
2492 end if;
2494 Next_Elmt (Priv_Elmt);
2495 end loop;
2496 end Swap_Private_Dependents;
2498 -- Start of processing for Install_Private_Declarations
2500 begin
2501 -- First exchange declarations for private types, so that the full
2502 -- declaration is visible. For each private type, we check its
2503 -- Private_Dependents list and also exchange any subtypes of or derived
2504 -- types from it. Finally, if this is a Taft amendment type, the
2505 -- incomplete declaration is irrelevant, and we want to link the
2506 -- eventual full declaration with the original private one so we
2507 -- also skip the exchange.
2509 Id := First_Entity (P);
2510 while Present (Id) and then Id /= First_Private_Entity (P) loop
2511 if Is_Private_Base_Type (Id)
2512 and then Present (Full_View (Id))
2513 and then Comes_From_Source (Full_View (Id))
2514 and then Scope (Full_View (Id)) = Scope (Id)
2515 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
2516 then
2517 -- If there is a use-type clause on the private type, set the full
2518 -- view accordingly.
2520 Set_In_Use (Full_View (Id), In_Use (Id));
2521 Full := Full_View (Id);
2523 if Is_Private_Base_Type (Full)
2524 and then Has_Private_Declaration (Full)
2525 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
2526 and then In_Open_Scopes (Scope (Etype (Full)))
2527 and then In_Package_Body (Current_Scope)
2528 and then not Is_Private_Type (Etype (Full))
2529 then
2530 -- This is the completion of a private type by a derivation
2531 -- from another private type which is not private anymore. This
2532 -- can only happen in a package nested within a child package,
2533 -- when the parent type is defined in the parent unit. At this
2534 -- point the current type is not private either, and we have
2535 -- to install the underlying full view, which is now visible.
2536 -- Save the current full view as well, so that all views can be
2537 -- restored on exit. It may seem that after compiling the child
2538 -- body there are not environments to restore, but the back-end
2539 -- expects those links to be valid, and freeze nodes depend on
2540 -- them.
2542 if No (Full_View (Full))
2543 and then Present (Underlying_Full_View (Full))
2544 then
2545 Set_Full_View (Id, Underlying_Full_View (Full));
2546 Set_Underlying_Full_View (Id, Full);
2547 Set_Is_Underlying_Full_View (Full);
2549 Set_Underlying_Full_View (Full, Empty);
2550 Set_Is_Frozen (Full_View (Id));
2551 end if;
2552 end if;
2554 Priv_Deps := Private_Dependents (Id);
2555 Exchange_Declarations (Id);
2556 Set_Is_Immediately_Visible (Id);
2557 Swap_Private_Dependents (Priv_Deps);
2558 end if;
2560 Next_Entity (Id);
2561 end loop;
2563 -- Next make other declarations in the private part visible as well
2565 Id := First_Private_Entity (P);
2566 while Present (Id) loop
2567 Install_Package_Entity (Id);
2568 Set_Is_Hidden (Id, False);
2569 Next_Entity (Id);
2570 end loop;
2572 -- An abstract state is partially refined when it has at least one
2573 -- Part_Of constituent. Since these constituents are being installed
2574 -- into visibility, update the partial refinement status of any state
2575 -- defined in the associated package, subject to at least one Part_Of
2576 -- constituent.
2578 if Is_Package_Or_Generic_Package (P) then
2579 declare
2580 States : constant Elist_Id := Abstract_States (P);
2581 State_Elmt : Elmt_Id;
2582 State_Id : Entity_Id;
2584 begin
2585 if Present (States) then
2586 State_Elmt := First_Elmt (States);
2587 while Present (State_Elmt) loop
2588 State_Id := Node (State_Elmt);
2590 if Present (Part_Of_Constituents (State_Id)) then
2591 Set_Has_Partial_Visible_Refinement (State_Id);
2592 end if;
2594 Next_Elmt (State_Elmt);
2595 end loop;
2596 end if;
2597 end;
2598 end if;
2600 -- Indicate that the private part is currently visible, so it can be
2601 -- properly reset on exit.
2603 Set_In_Private_Part (P);
2604 end Install_Private_Declarations;
2606 ----------------------------------
2607 -- Install_Visible_Declarations --
2608 ----------------------------------
2610 procedure Install_Visible_Declarations (P : Entity_Id) is
2611 Id : Entity_Id;
2612 Last_Entity : Entity_Id;
2614 begin
2615 pragma Assert
2616 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
2618 if Is_Package_Or_Generic_Package (P) then
2619 Last_Entity := First_Private_Entity (P);
2620 else
2621 Last_Entity := Empty;
2622 end if;
2624 Id := First_Entity (P);
2625 while Present (Id) and then Id /= Last_Entity loop
2626 Install_Package_Entity (Id);
2627 Next_Entity (Id);
2628 end loop;
2629 end Install_Visible_Declarations;
2631 --------------------------
2632 -- Is_Private_Base_Type --
2633 --------------------------
2635 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
2636 begin
2637 return Ekind (E) = E_Private_Type
2638 or else Ekind (E) = E_Limited_Private_Type
2639 or else Ekind (E) = E_Record_Type_With_Private;
2640 end Is_Private_Base_Type;
2642 --------------------------
2643 -- Is_Visible_Dependent --
2644 --------------------------
2646 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
2648 S : constant Entity_Id := Scope (Dep);
2650 begin
2651 -- Renamings created for actual types have the visibility of the actual
2653 if Ekind (S) = E_Package
2654 and then Is_Generic_Instance (S)
2655 and then (Is_Generic_Actual_Type (Dep)
2656 or else Is_Generic_Actual_Type (Full_View (Dep)))
2657 then
2658 return True;
2660 elsif not (Is_Derived_Type (Dep))
2661 and then Is_Derived_Type (Full_View (Dep))
2662 then
2663 -- When instantiating a package body, the scope stack is empty, so
2664 -- check instead whether the dependent type is defined in the same
2665 -- scope as the instance itself.
2667 return In_Open_Scopes (S)
2668 or else (Is_Generic_Instance (Current_Scope)
2669 and then Scope (Dep) = Scope (Current_Scope));
2670 else
2671 return True;
2672 end if;
2673 end Is_Visible_Dependent;
2675 ----------------------------
2676 -- May_Need_Implicit_Body --
2677 ----------------------------
2679 procedure May_Need_Implicit_Body (E : Entity_Id) is
2680 P : constant Node_Id := Unit_Declaration_Node (E);
2681 S : constant Node_Id := Parent (P);
2682 B : Node_Id;
2683 Decls : List_Id;
2685 begin
2686 if not Has_Completion (E)
2687 and then Nkind (P) = N_Package_Declaration
2688 and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
2689 then
2690 B :=
2691 Make_Package_Body (Sloc (E),
2692 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
2693 Chars => Chars (E)),
2694 Declarations => New_List);
2696 if Nkind (S) = N_Package_Specification then
2697 if Present (Private_Declarations (S)) then
2698 Decls := Private_Declarations (S);
2699 else
2700 Decls := Visible_Declarations (S);
2701 end if;
2702 else
2703 Decls := Declarations (S);
2704 end if;
2706 Append (B, Decls);
2707 Analyze (B);
2708 end if;
2709 end May_Need_Implicit_Body;
2711 ----------------------
2712 -- New_Private_Type --
2713 ----------------------
2715 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
2716 begin
2717 -- For other than Ada 2012, enter the name in the current scope
2719 if Ada_Version < Ada_2012 then
2720 Enter_Name (Id);
2722 -- Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2723 -- there may be an incomplete previous view.
2725 else
2726 declare
2727 Prev : Entity_Id;
2728 begin
2729 Prev := Find_Type_Name (N);
2730 pragma Assert (Prev = Id
2731 or else (Ekind (Prev) = E_Incomplete_Type
2732 and then Present (Full_View (Prev))
2733 and then Full_View (Prev) = Id));
2734 end;
2735 end if;
2737 if Limited_Present (Def) then
2738 Mutate_Ekind (Id, E_Limited_Private_Type);
2739 else
2740 Mutate_Ekind (Id, E_Private_Type);
2741 end if;
2743 Set_Is_Not_Self_Hidden (Id);
2744 Set_Etype (Id, Id);
2745 Set_Has_Delayed_Freeze (Id);
2746 Set_Is_First_Subtype (Id);
2747 Reinit_Size_Align (Id);
2749 Set_Is_Constrained (Id,
2750 No (Discriminant_Specifications (N))
2751 and then not Unknown_Discriminants_Present (N));
2753 -- Set tagged flag before processing discriminants, to catch illegal
2754 -- usage.
2756 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
2758 Set_Discriminant_Constraint (Id, No_Elist);
2759 Set_Stored_Constraint (Id, No_Elist);
2761 if Present (Discriminant_Specifications (N)) then
2762 Push_Scope (Id);
2763 Process_Discriminants (N);
2764 End_Scope;
2766 elsif Unknown_Discriminants_Present (N) then
2767 Set_Has_Unknown_Discriminants (Id);
2768 end if;
2770 Set_Private_Dependents (Id, New_Elmt_List);
2772 if Tagged_Present (Def) then
2773 Mutate_Ekind (Id, E_Record_Type_With_Private);
2774 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2775 Set_Is_Abstract_Type (Id, Abstract_Present (Def));
2776 Set_Is_Limited_Record (Id, Limited_Present (Def));
2777 Set_Has_Delayed_Freeze (Id, True);
2779 -- Recognize Ada.Real_Time.Timing_Events.Timing_Events here
2781 if Is_RTE (Id, RE_Timing_Event) then
2782 Set_Has_Timing_Event (Id);
2783 end if;
2785 -- Create a class-wide type with the same attributes
2787 Make_Class_Wide_Type (Id);
2789 elsif Abstract_Present (Def) then
2790 Error_Msg_N ("only a tagged type can be abstract", N);
2792 -- We initialize the primitive operations list of an untagged private
2793 -- type to an empty element list. Do this even when Extensions_Allowed
2794 -- is False to issue better error messages. (Note: This could be done
2795 -- for all private types and shared with the tagged case above, but
2796 -- for now we do it separately.)
2798 else
2799 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2800 end if;
2801 end New_Private_Type;
2803 ---------------------------------
2804 -- Requires_Completion_In_Body --
2805 ---------------------------------
2807 function Requires_Completion_In_Body
2808 (Id : Entity_Id;
2809 Pack_Id : Entity_Id;
2810 Do_Abstract_States : Boolean := False) return Boolean
2812 begin
2813 -- Always ignore child units. Child units get added to the entity list
2814 -- of a parent unit, but are not original entities of the parent, and
2815 -- so do not affect whether the parent needs a body.
2817 if Is_Child_Unit (Id) then
2818 return False;
2820 -- Ignore formal packages and their renamings
2822 elsif Ekind (Id) = E_Package
2823 and then Nkind (Original_Node (Unit_Declaration_Node (Id))) =
2824 N_Formal_Package_Declaration
2825 then
2826 return False;
2828 -- Otherwise test to see if entity requires a completion. Note that
2829 -- subprogram entities whose declaration does not come from source are
2830 -- ignored here on the basis that we assume the expander will provide an
2831 -- implicit completion at some point.
2833 elsif (Is_Overloadable (Id)
2834 and then Ekind (Id) not in E_Enumeration_Literal | E_Operator
2835 and then not Is_Abstract_Subprogram (Id)
2836 and then not Has_Completion (Id)
2837 and then Comes_From_Source (Parent (Id)))
2839 or else
2840 (Ekind (Id) = E_Package
2841 and then Id /= Pack_Id
2842 and then not Has_Completion (Id)
2843 and then Unit_Requires_Body (Id, Do_Abstract_States))
2845 or else
2846 (Ekind (Id) = E_Incomplete_Type
2847 and then No (Full_View (Id))
2848 and then not Is_Generic_Type (Id))
2850 or else
2851 (Ekind (Id) in E_Task_Type | E_Protected_Type
2852 and then not Has_Completion (Id))
2854 or else
2855 (Ekind (Id) = E_Generic_Package
2856 and then Id /= Pack_Id
2857 and then not Has_Completion (Id)
2858 and then Unit_Requires_Body (Id, Do_Abstract_States))
2860 or else
2861 (Is_Generic_Subprogram (Id)
2862 and then not Has_Completion (Id))
2863 then
2864 return True;
2866 -- Otherwise the entity does not require completion in a package body
2868 else
2869 return False;
2870 end if;
2871 end Requires_Completion_In_Body;
2873 ----------------------------
2874 -- Uninstall_Declarations --
2875 ----------------------------
2877 procedure Uninstall_Declarations (P : Entity_Id) is
2878 Decl : constant Node_Id := Unit_Declaration_Node (P);
2879 Id : Entity_Id;
2880 Full : Entity_Id;
2882 procedure Preserve_Full_Attributes (Priv : Entity_Id; Full : Entity_Id);
2883 -- Copy to the private declaration the attributes of the full view that
2884 -- need to be available for the partial view also.
2886 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
2887 -- When the full view of a private type is made unavailable, we do the
2888 -- same for its private dependents under proper visibility conditions.
2889 -- When compiling a child unit this needs to be done recursively.
2891 function Type_In_Use (T : Entity_Id) return Boolean;
2892 -- Check whether type or base type appear in an active use_type clause
2894 ------------------------------
2895 -- Preserve_Full_Attributes --
2896 ------------------------------
2898 procedure Preserve_Full_Attributes
2899 (Priv : Entity_Id;
2900 Full : Entity_Id)
2902 Full_Base : constant Entity_Id := Base_Type (Full);
2903 Priv_Is_Base_Type : constant Boolean := Is_Base_Type (Priv);
2905 begin
2906 Set_Size_Info (Priv, Full);
2907 Copy_RM_Size (To => Priv, From => Full);
2908 Set_Size_Known_At_Compile_Time
2909 (Priv, Size_Known_At_Compile_Time (Full));
2910 Set_Is_Volatile (Priv, Is_Volatile (Full));
2911 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
2912 Set_Is_Atomic (Priv, Is_Atomic (Full));
2913 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full));
2914 Set_Is_Ada_2012_Only (Priv, Is_Ada_2012_Only (Full));
2915 Set_Is_Ada_2022_Only (Priv, Is_Ada_2022_Only (Full));
2916 Set_Has_Pragma_Unmodified (Priv, Has_Pragma_Unmodified (Full));
2917 Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced (Full));
2918 Set_Has_Pragma_Unreferenced_Objects
2919 (Priv, Has_Pragma_Unreferenced_Objects
2920 (Full));
2921 Set_Predicates_Ignored (Priv, Predicates_Ignored (Full));
2922 if Is_Unchecked_Union (Full) then
2923 Set_Is_Unchecked_Union (Base_Type (Priv));
2924 end if;
2926 if Referenced (Full) then
2927 Set_Referenced (Priv);
2928 end if;
2930 if Priv_Is_Base_Type then
2931 Set_Is_Controlled_Active
2932 (Priv, Is_Controlled_Active (Full_Base));
2933 Set_Finalize_Storage_Only
2934 (Priv, Finalize_Storage_Only (Full_Base));
2935 Set_Has_Controlled_Component
2936 (Priv, Has_Controlled_Component (Full_Base));
2938 Propagate_Concurrent_Flags (Priv, Base_Type (Full));
2939 end if;
2941 -- As explained in Freeze_Entity, private types are required to point
2942 -- to the same freeze node as their corresponding full view, if any.
2943 -- But we ought not to overwrite a node already inserted in the tree.
2945 pragma Assert
2946 (Serious_Errors_Detected /= 0
2947 or else No (Freeze_Node (Priv))
2948 or else No (Parent (Freeze_Node (Priv)))
2949 or else Freeze_Node (Priv) = Freeze_Node (Full));
2951 Set_Freeze_Node (Priv, Freeze_Node (Full));
2953 -- Propagate Default_Initial_Condition-related attributes from the
2954 -- full view to the private view.
2956 Propagate_DIC_Attributes (Priv, From_Typ => Full);
2958 -- Propagate invariant-related attributes from the full view to the
2959 -- private view.
2961 Propagate_Invariant_Attributes (Priv, From_Typ => Full);
2963 -- Propagate predicate-related attributes from the full view to the
2964 -- private view.
2966 Propagate_Predicate_Attributes (Priv, From_Typ => Full);
2968 if Is_Tagged_Type (Priv)
2969 and then Is_Tagged_Type (Full)
2970 and then not Error_Posted (Full)
2971 then
2972 if Is_Tagged_Type (Priv) then
2974 -- If the type is tagged, the tag itself must be available on
2975 -- the partial view, for expansion purposes.
2977 Set_First_Entity (Priv, First_Entity (Full));
2979 -- If there are discriminants in the partial view, these remain
2980 -- visible. Otherwise only the tag itself is visible, and there
2981 -- are no nameable components in the partial view.
2983 if No (Last_Entity (Priv)) then
2984 Set_Last_Entity (Priv, First_Entity (Priv));
2985 end if;
2986 end if;
2988 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2990 if Has_Discriminants (Full) then
2991 Set_Discriminant_Constraint (Priv,
2992 Discriminant_Constraint (Full));
2993 end if;
2994 end if;
2995 end Preserve_Full_Attributes;
2997 -----------------------------
2998 -- Swap_Private_Dependents --
2999 -----------------------------
3001 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
3002 Cunit : Entity_Id;
3003 Deps : Elist_Id;
3004 Priv : Entity_Id;
3005 Priv_Elmt : Elmt_Id;
3006 Is_Priv : Boolean;
3008 begin
3009 Priv_Elmt := First_Elmt (Priv_Deps);
3010 while Present (Priv_Elmt) loop
3011 Priv := Node (Priv_Elmt);
3013 -- Before we do the swap, we verify the presence of the Full_View
3014 -- field, which may be empty due to a swap by a previous call to
3015 -- End_Package_Scope (e.g. from the freezing mechanism).
3017 if Present (Full_View (Priv)) then
3018 if Is_Private_Type (Priv) then
3019 Cunit := Cunit_Entity (Current_Sem_Unit);
3020 Deps := Private_Dependents (Priv);
3021 Is_Priv := True;
3022 else
3023 Is_Priv := False;
3024 end if;
3026 if Scope (Priv) = P
3027 or else not In_Open_Scopes (Scope (Priv))
3028 then
3029 Set_Is_Immediately_Visible (Priv, False);
3030 end if;
3032 if Is_Visible_Dependent (Priv) then
3033 Preserve_Full_Attributes (Priv, Full_View (Priv));
3034 Replace_Elmt (Priv_Elmt, Full_View (Priv));
3035 Exchange_Declarations (Priv);
3037 -- Recurse for child units, except in generic child units,
3038 -- which unfortunately handle private_dependents separately.
3039 -- Note that the current unit may not have been analyzed,
3040 -- for example a package body, so we cannot rely solely on
3041 -- the Is_Child_Unit flag, but that's only an optimization.
3043 if Is_Priv
3044 and then (No (Etype (Cunit)) or else Is_Child_Unit (Cunit))
3045 and then not Is_Empty_Elmt_List (Deps)
3046 and then not Inside_A_Generic
3047 then
3048 Swap_Private_Dependents (Deps);
3049 end if;
3050 end if;
3051 end if;
3053 Next_Elmt (Priv_Elmt);
3054 end loop;
3055 end Swap_Private_Dependents;
3057 -----------------
3058 -- Type_In_Use --
3059 -----------------
3061 function Type_In_Use (T : Entity_Id) return Boolean is
3062 begin
3063 return Scope (Base_Type (T)) = P
3064 and then (In_Use (T) or else In_Use (Base_Type (T)));
3065 end Type_In_Use;
3067 -- Start of processing for Uninstall_Declarations
3069 begin
3070 Id := First_Entity (P);
3071 while Present (Id) and then Id /= First_Private_Entity (P) loop
3072 if Debug_Flag_E then
3073 Write_Str ("unlinking visible entity ");
3074 Write_Int (Int (Id));
3075 Write_Eol;
3076 end if;
3078 -- On exit from the package scope, we must preserve the visibility
3079 -- established by use clauses in the current scope. Two cases:
3081 -- a) If the entity is an operator, it may be a primitive operator of
3082 -- a type for which there is a visible use-type clause.
3084 -- b) For other entities, their use-visibility is determined by a
3085 -- visible use clause for the package itself or a use-all-type clause
3086 -- applied directly to the entity's type. For a generic instance,
3087 -- the instantiation of the formals appears in the visible part,
3088 -- but the formals are private and remain so.
3090 if Ekind (Id) = E_Function
3091 and then Is_Operator_Symbol_Name (Chars (Id))
3092 and then not Is_Hidden (Id)
3093 and then not Error_Posted (Id)
3094 then
3095 Set_Is_Potentially_Use_Visible (Id,
3096 In_Use (P)
3097 or else Type_In_Use (Etype (Id))
3098 or else Type_In_Use (Etype (First_Formal (Id)))
3099 or else (Present (Next_Formal (First_Formal (Id)))
3100 and then
3101 Type_In_Use
3102 (Etype (Next_Formal (First_Formal (Id))))));
3103 else
3104 if In_Use (P) and then not Is_Hidden (Id) then
3106 -- A child unit of a use-visible package remains use-visible
3107 -- only if it is itself a visible child unit. Otherwise it
3108 -- would remain visible in other contexts where P is use-
3109 -- visible, because once compiled it stays in the entity list
3110 -- of its parent unit.
3112 if Is_Child_Unit (Id) then
3113 Set_Is_Potentially_Use_Visible
3114 (Id, Is_Visible_Lib_Unit (Id));
3115 else
3116 Set_Is_Potentially_Use_Visible (Id);
3117 end if;
3119 -- Avoid crash caused by previous errors
3121 elsif No (Etype (Id)) and then Serious_Errors_Detected /= 0 then
3122 null;
3124 -- We need to avoid incorrectly marking enumeration literals as
3125 -- non-visible when a visible use-all-type clause is in effect.
3127 elsif Type_In_Use (Etype (Id))
3128 and then Nkind (Current_Use_Clause (Etype (Id))) =
3129 N_Use_Type_Clause
3130 and then All_Present (Current_Use_Clause (Etype (Id)))
3131 then
3132 null;
3134 else
3135 Set_Is_Potentially_Use_Visible (Id, False);
3136 end if;
3137 end if;
3139 -- Local entities are not immediately visible outside of the package
3141 Set_Is_Immediately_Visible (Id, False);
3143 -- If this is a private type with a full view (for example a local
3144 -- subtype of a private type declared elsewhere), ensure that the
3145 -- full view is also removed from visibility: it may be exposed when
3146 -- swapping views in an instantiation. Similarly, ensure that the
3147 -- use-visibility is properly set on both views.
3149 if Is_Type (Id) and then Present (Full_View (Id)) then
3150 Set_Is_Immediately_Visible (Full_View (Id), False);
3151 Set_Is_Potentially_Use_Visible (Full_View (Id),
3152 Is_Potentially_Use_Visible (Id));
3153 end if;
3155 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
3156 Check_Abstract_Overriding (Id);
3157 Check_Conventions (Id);
3158 end if;
3160 if Ekind (Id) in E_Private_Type | E_Limited_Private_Type
3161 and then No (Full_View (Id))
3162 and then not Is_Generic_Type (Id)
3163 and then not Is_Derived_Type (Id)
3164 then
3165 Error_Msg_N ("missing full declaration for private type&", Id);
3167 elsif Ekind (Id) = E_Record_Type_With_Private
3168 and then not Is_Generic_Type (Id)
3169 and then No (Full_View (Id))
3170 then
3171 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
3172 Error_Msg_N ("missing full declaration for private type&", Id);
3173 else
3174 Error_Msg_N
3175 ("missing full declaration for private extension", Id);
3176 end if;
3178 -- Case of constant, check for deferred constant declaration with
3179 -- no full view. Likely just a matter of a missing expression, or
3180 -- accidental use of the keyword constant.
3182 elsif Ekind (Id) = E_Constant
3184 -- OK if constant value present
3186 and then No (Constant_Value (Id))
3188 -- OK if full view present
3190 and then No (Full_View (Id))
3192 -- OK if imported, since that provides the completion
3194 and then not Is_Imported (Id)
3196 -- OK if object declaration replaced by renaming declaration as
3197 -- a result of OK_To_Rename processing (e.g. for concatenation)
3199 and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration
3201 -- OK if object declaration with the No_Initialization flag set
3203 and then not (Nkind (Parent (Id)) = N_Object_Declaration
3204 and then No_Initialization (Parent (Id)))
3205 then
3206 -- If no private declaration is present, we assume the user did
3207 -- not intend a deferred constant declaration and the problem
3208 -- is simply that the initializing expression is missing.
3210 if not Has_Private_Declaration (Etype (Id)) then
3211 Error_Msg_N
3212 ("constant declaration requires initialization expression",
3213 Parent (Id));
3215 if Is_Limited_Type (Etype (Id)) then
3216 Error_Msg_N
3217 ("\if variable intended, remove CONSTANT from declaration",
3218 Parent (Id));
3219 end if;
3221 -- Otherwise if a private declaration is present, then we are
3222 -- missing the full declaration for the deferred constant.
3224 else
3225 Error_Msg_N
3226 ("missing full declaration for deferred constant (RM 7.4)",
3227 Id);
3229 if Is_Limited_Type (Etype (Id)) then
3230 Error_Msg_N
3231 ("\if variable intended, remove CONSTANT from declaration",
3232 Parent (Id));
3233 end if;
3234 end if;
3235 end if;
3237 Next_Entity (Id);
3238 end loop;
3240 -- If the specification was installed as the parent of a public child
3241 -- unit, the private declarations were not installed, and there is
3242 -- nothing to do.
3244 if not In_Private_Part (P) then
3245 return;
3246 end if;
3248 -- Reset the flag now
3250 Set_In_Private_Part (P, False);
3252 -- Make private entities invisible and exchange full and private
3253 -- declarations for private types. Id is now the first private entity
3254 -- in the package.
3256 while Present (Id) loop
3257 if Debug_Flag_E then
3258 Write_Str ("unlinking private entity ");
3259 Write_Int (Int (Id));
3260 Write_Eol;
3261 end if;
3263 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
3264 Check_Abstract_Overriding (Id);
3265 Check_Conventions (Id);
3266 end if;
3268 Set_Is_Immediately_Visible (Id, False);
3270 if Is_Private_Base_Type (Id) and then Present (Full_View (Id)) then
3271 Full := Full_View (Id);
3273 -- If the partial view is not declared in the visible part of the
3274 -- package (as is the case when it is a type derived from some
3275 -- other private type in the private part of the current package),
3276 -- no exchange takes place.
3278 if No (Parent (Id))
3279 or else List_Containing (Parent (Id)) /=
3280 Visible_Declarations (Specification (Decl))
3281 then
3282 goto Next_Id;
3283 end if;
3285 -- The entry in the private part points to the full declaration,
3286 -- which is currently visible. Exchange them so only the private
3287 -- type declaration remains accessible, and link private and full
3288 -- declaration in the opposite direction. Before the actual
3289 -- exchange, we copy back attributes of the full view that must
3290 -- be available to the partial view too.
3292 Preserve_Full_Attributes (Id, Full);
3294 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
3296 -- The following test may be redundant, as this is already
3297 -- diagnosed in sem_ch3. ???
3299 if not Is_Definite_Subtype (Full)
3300 and then Is_Definite_Subtype (Id)
3301 then
3302 Error_Msg_Sloc := Sloc (Parent (Id));
3303 Error_Msg_NE
3304 ("full view of& not compatible with declaration#", Full, Id);
3305 end if;
3307 -- Swap out the subtypes and derived types of Id that
3308 -- were compiled in this scope, or installed previously
3309 -- by Install_Private_Declarations.
3311 Swap_Private_Dependents (Private_Dependents (Id));
3313 -- Now restore the type itself to its private view
3315 Exchange_Declarations (Id);
3317 -- If we have installed an underlying full view for a type derived
3318 -- from a private type in a child unit, restore the proper views
3319 -- of private and full view. See corresponding code in
3320 -- Install_Private_Declarations.
3322 -- After the exchange, Full denotes the private type in the
3323 -- visible part of the package.
3325 if Is_Private_Base_Type (Full)
3326 and then Present (Full_View (Full))
3327 and then Present (Underlying_Full_View (Full))
3328 and then In_Package_Body (Current_Scope)
3329 then
3330 Set_Full_View (Full, Underlying_Full_View (Full));
3331 Set_Underlying_Full_View (Full, Empty);
3332 end if;
3334 elsif Ekind (Id) = E_Incomplete_Type
3335 and then Comes_From_Source (Id)
3336 and then No (Full_View (Id))
3337 then
3338 -- Mark Taft amendment types. Verify that there are no primitive
3339 -- operations declared for the type (3.10.1(9)).
3341 Set_Has_Completion_In_Body (Id);
3343 declare
3344 Elmt : Elmt_Id;
3345 Subp : Entity_Id;
3347 begin
3348 Elmt := First_Elmt (Private_Dependents (Id));
3349 while Present (Elmt) loop
3350 Subp := Node (Elmt);
3352 -- Is_Primitive is tested because there can be cases where
3353 -- nonprimitive subprograms (in nested packages) are added
3354 -- to the Private_Dependents list.
3356 if Is_Overloadable (Subp) and then Is_Primitive (Subp) then
3357 Error_Msg_NE
3358 ("type& must be completed in the private part",
3359 Parent (Subp), Id);
3361 -- The result type of an access-to-function type cannot be a
3362 -- Taft-amendment type, unless the version is Ada 2012 or
3363 -- later (see AI05-151).
3365 elsif Ada_Version < Ada_2012
3366 and then Ekind (Subp) = E_Subprogram_Type
3367 then
3368 if Etype (Subp) = Id
3369 or else
3370 (Is_Class_Wide_Type (Etype (Subp))
3371 and then Etype (Etype (Subp)) = Id)
3372 then
3373 Error_Msg_NE
3374 ("type& must be completed in the private part",
3375 Associated_Node_For_Itype (Subp), Id);
3376 end if;
3377 end if;
3379 Next_Elmt (Elmt);
3380 end loop;
3381 end;
3383 -- For subtypes of private types the frontend generates two entities:
3384 -- one associated with the partial view and the other associated with
3385 -- the full view. When the subtype declaration is public the frontend
3386 -- places the former entity in the list of public entities of the
3387 -- package and the latter entity in the private part of the package.
3388 -- When the subtype declaration is private it generates these two
3389 -- entities but both are placed in the private part of the package
3390 -- (and the full view has the same source location as the partial
3391 -- view and no parent; see Prepare_Private_Subtype_Completion).
3393 elsif Ekind (Id) in E_Private_Subtype
3394 | E_Limited_Private_Subtype
3395 and then Present (Full_View (Id))
3396 and then Sloc (Id) = Sloc (Full_View (Id))
3397 and then No (Parent (Full_View (Id)))
3398 then
3399 Set_Is_Hidden (Id);
3400 Set_Is_Potentially_Use_Visible (Id, False);
3402 elsif not Is_Child_Unit (Id)
3403 and then (not Is_Private_Type (Id) or else No (Full_View (Id)))
3404 then
3405 Set_Is_Hidden (Id);
3406 Set_Is_Potentially_Use_Visible (Id, False);
3407 end if;
3409 <<Next_Id>>
3410 Next_Entity (Id);
3411 end loop;
3412 end Uninstall_Declarations;
3414 ------------------------
3415 -- Unit_Requires_Body --
3416 ------------------------
3418 function Unit_Requires_Body
3419 (Pack_Id : Entity_Id;
3420 Do_Abstract_States : Boolean := False) return Boolean
3422 E : Entity_Id;
3424 Requires_Body : Boolean := False;
3425 -- Flag set when the unit has at least one construct that requires
3426 -- completion in a body.
3428 begin
3429 -- Imported entity never requires body. Right now, only subprograms can
3430 -- be imported, but perhaps in the future we will allow import of
3431 -- packages.
3433 if Is_Imported (Pack_Id) then
3434 return False;
3436 -- Body required if library package with pragma Elaborate_Body
3438 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3439 return True;
3441 -- Body required if subprogram
3443 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3444 return True;
3446 -- Treat a block as requiring a body
3448 elsif Ekind (Pack_Id) = E_Block then
3449 return True;
3451 elsif Ekind (Pack_Id) = E_Package
3452 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3453 and then Present (Generic_Parent (Parent (Pack_Id)))
3454 then
3455 declare
3456 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3457 begin
3458 if Has_Pragma_Elaborate_Body (G_P) then
3459 return True;
3460 end if;
3461 end;
3462 end if;
3464 -- Traverse the entity chain of the package and look for constructs that
3465 -- require a completion in a body.
3467 E := First_Entity (Pack_Id);
3468 while Present (E) loop
3470 -- Skip abstract states because their completion depends on several
3471 -- criteria (see below).
3473 if Ekind (E) = E_Abstract_State then
3474 null;
3476 elsif Requires_Completion_In_Body
3477 (E, Pack_Id, Do_Abstract_States)
3478 then
3479 Requires_Body := True;
3480 exit;
3481 end if;
3483 Next_Entity (E);
3484 end loop;
3486 -- A [generic] package that defines at least one non-null abstract state
3487 -- requires a completion only when at least one other construct requires
3488 -- a completion in a body (SPARK RM 7.1.4(4) and (5)). This check is not
3489 -- performed if the caller requests this behavior.
3491 if Do_Abstract_States
3492 and then Is_Package_Or_Generic_Package (Pack_Id)
3493 and then Has_Non_Null_Abstract_State (Pack_Id)
3494 and then Requires_Body
3495 then
3496 return True;
3497 end if;
3499 return Requires_Body;
3500 end Unit_Requires_Body;
3502 -----------------------------
3503 -- Unit_Requires_Body_Info --
3504 -----------------------------
3506 procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id) is
3507 E : Entity_Id;
3509 begin
3510 -- An imported entity never requires body. Right now, only subprograms
3511 -- can be imported, but perhaps in the future we will allow import of
3512 -- packages.
3514 if Is_Imported (Pack_Id) then
3515 return;
3517 -- Body required if library package with pragma Elaborate_Body
3519 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3520 Error_Msg_N ("info: & requires body (Elaborate_Body)?.y?", Pack_Id);
3522 -- Body required if subprogram
3524 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3525 Error_Msg_N ("info: & requires body (subprogram case)?.y?", Pack_Id);
3527 -- Body required if generic parent has Elaborate_Body
3529 elsif Ekind (Pack_Id) = E_Package
3530 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3531 and then Present (Generic_Parent (Parent (Pack_Id)))
3532 then
3533 declare
3534 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3535 begin
3536 if Has_Pragma_Elaborate_Body (G_P) then
3537 Error_Msg_N
3538 ("info: & requires body (generic parent Elaborate_Body)?.y?",
3539 Pack_Id);
3540 end if;
3541 end;
3543 -- A [generic] package that introduces at least one non-null abstract
3544 -- state requires completion. However, there is a separate rule that
3545 -- requires that such a package have a reason other than this for a
3546 -- body being required (if necessary a pragma Elaborate_Body must be
3547 -- provided). If Ignore_Abstract_State is True, we don't do this check
3548 -- (so we can use Unit_Requires_Body to check for some other reason).
3550 elsif Is_Package_Or_Generic_Package (Pack_Id)
3551 and then Present (Abstract_States (Pack_Id))
3552 and then not Is_Null_State
3553 (Node (First_Elmt (Abstract_States (Pack_Id))))
3554 then
3555 Error_Msg_N
3556 ("info: & requires body (non-null abstract state aspect)?.y?",
3557 Pack_Id);
3558 end if;
3560 -- Otherwise search entity chain for entity requiring completion
3562 E := First_Entity (Pack_Id);
3563 while Present (E) loop
3564 if Requires_Completion_In_Body (E, Pack_Id) then
3565 Error_Msg_Node_2 := E;
3566 Error_Msg_NE
3567 ("info: & requires body (& requires completion)?.y?", E,
3568 Pack_Id);
3569 end if;
3571 Next_Entity (E);
3572 end loop;
3573 end Unit_Requires_Body_Info;
3575 end Sem_Ch7;