Add assember CFI directives to millicode division and remainder routines.
[official-gcc.git] / gcc / ada / sem_ch7.adb
blob7cb7c862d35f8feb4777a1b5f7a8942d95f3f921
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 since inlining is then
323 -- handled by the C compiler.
325 -- Note that we test Has_Pragma_Inline here in addition
326 -- to Is_Inlined. We are doing this for a client, since
327 -- we are computing which entities should be public, and
328 -- it is the client who will decide if actual inlining
329 -- should occur, so we need to catch all cases where the
330 -- subprogram may be inlined by the client.
332 if (not CCG_Mode or else Has_Pragma_Inline_Always (Decl_Id))
333 and then (Is_Inlined (Decl_Id)
334 or else Has_Pragma_Inline (Decl_Id))
335 then
336 Has_Referencer_Of_Non_Subprograms := True;
338 -- Inspect the statements of the subprogram body
339 -- to determine whether the body references other
340 -- subprograms.
342 Scan_Subprogram_Refs (Decl);
343 return True;
344 else
345 return False;
346 end if;
347 end Set_Referencer_Of_Non_Subprograms;
349 begin
350 if No (Decls) then
351 return False;
352 end if;
354 -- Examine all declarations in reverse order, hiding all entities
355 -- from external visibility until a referencer has been found. The
356 -- algorithm recurses into nested packages.
358 Decl := Last (Decls);
359 while Present (Decl) loop
361 -- A stub is always considered a referencer
363 if Nkind (Decl) in N_Body_Stub then
364 return True;
366 -- Package declaration
368 elsif Nkind (Decl) = N_Package_Declaration then
369 Spec := Specification (Decl);
370 Decl_Id := Defining_Entity (Spec);
372 -- Inspect the declarations of a non-generic package to try
373 -- and hide more entities from external visibility.
375 if not Is_Generic_Unit (Decl_Id) then
376 if In_Nested_Instance then
377 In_Instance := True;
378 elsif Is_Generic_Instance (Decl_Id) then
379 In_Instance :=
380 Has_Completion (Decl_Id)
381 or else Unit_Requires_Body (Generic_Parent (Spec));
382 else
383 In_Instance := False;
384 end if;
386 if Has_Referencer (Private_Declarations (Spec),
387 In_Instance,
388 Has_Referencer_Of_Non_Subprograms)
389 or else
390 Has_Referencer (Visible_Declarations (Spec),
391 In_Instance,
392 Has_Referencer_Of_Non_Subprograms)
393 then
394 return True;
395 end if;
396 end if;
398 -- Package body
400 elsif Nkind (Decl) = N_Package_Body
401 and then Present (Corresponding_Spec (Decl))
402 then
403 Decl_Id := Corresponding_Spec (Decl);
405 -- A generic package body is a referencer. It would seem
406 -- that we only have to consider generics that can be
407 -- exported, i.e. where the corresponding spec is the
408 -- spec of the current package, but because of nested
409 -- instantiations, a fully private generic body may export
410 -- other private body entities. Furthermore, regardless of
411 -- whether there was a previous inlined subprogram, (an
412 -- instantiation of) the generic package may reference any
413 -- entity declared before it.
415 if Is_Generic_Unit (Decl_Id) then
416 return True;
418 -- Inspect the declarations of a non-generic package body to
419 -- try and hide more entities from external visibility.
421 elsif Has_Referencer (Declarations (Decl),
422 In_Nested_Instance
423 or else
424 Is_Generic_Instance (Decl_Id),
425 Has_Referencer_Of_Non_Subprograms)
426 then
427 return True;
428 end if;
430 -- Subprogram body
432 elsif Nkind (Decl) = N_Subprogram_Body then
433 if Present (Corresponding_Spec (Decl)) then
434 Decl_Id := Corresponding_Spec (Decl);
436 -- A generic subprogram body acts as a referencer
438 if Is_Generic_Unit (Decl_Id) then
439 return True;
440 end if;
442 Ignore := Set_Referencer_Of_Non_Subprograms;
444 -- Otherwise this is a stand alone subprogram body
446 else
447 Decl_Id := Defining_Entity (Decl);
449 if not Set_Referencer_Of_Non_Subprograms
450 and then not Subprogram_Table.Get (Decl_Id)
451 then
452 -- We can reset Is_Public right away
453 Set_Is_Public (Decl_Id, False);
454 end if;
455 end if;
457 -- Freeze node
459 elsif Nkind (Decl) = N_Freeze_Entity then
460 declare
461 Discard : Boolean;
462 pragma Unreferenced (Discard);
463 begin
464 -- Inspect the actions to find references to subprograms.
465 -- We assume that the actions do not contain other kinds
466 -- of references and, therefore, we do not stop the scan
467 -- or set Has_Referencer_Of_Non_Subprograms here. Doing
468 -- it would pessimize common cases for which the actions
469 -- contain the declaration of an init procedure, since
470 -- such a procedure is automatically marked inline.
472 Discard :=
473 Has_Referencer (Actions (Decl),
474 In_Nested_Instance,
475 Has_Referencer_Of_Non_Subprograms);
476 end;
478 -- Exceptions, objects and renamings do not need to be public
479 -- if they are not followed by a construct which can reference
480 -- and export them.
482 elsif Nkind (Decl) in N_Exception_Declaration
483 | N_Object_Declaration
484 | N_Object_Renaming_Declaration
485 then
486 Decl_Id := Defining_Entity (Decl);
488 -- We cannot say anything for objects declared in nested
489 -- instances because instantiations are not done yet so the
490 -- bodies are not visible and could contain references to
491 -- them.
493 if not In_Nested_Instance
494 and then not Is_Imported (Decl_Id)
495 and then not Is_Exported (Decl_Id)
496 and then No (Interface_Name (Decl_Id))
497 and then not Has_Referencer_Of_Non_Subprograms
498 then
499 Set_Is_Public (Decl_Id, False);
500 end if;
502 -- Likewise for subprograms and renamings, but we work harder
503 -- for them to see whether they are referenced on an individual
504 -- basis by looking into the table of referenced subprograms.
506 elsif Nkind (Decl) in N_Subprogram_Declaration
507 | N_Subprogram_Renaming_Declaration
508 then
509 Decl_Id := Defining_Entity (Decl);
511 -- We cannot say anything for subprograms declared in nested
512 -- instances because instantiations are not done yet so the
513 -- bodies are not visible and could contain references to
514 -- them, except if we still have no subprograms at all which
515 -- are referenced by an inlined body.
517 if (not In_Nested_Instance
518 or else not Subprogram_Table.Get_First)
519 and then not Is_Imported (Decl_Id)
520 and then not Is_Exported (Decl_Id)
521 and then No (Interface_Name (Decl_Id))
522 and then not Subprogram_Table.Get (Decl_Id)
523 then
524 Set_Is_Public (Decl_Id, False);
525 end if;
527 -- For a subprogram renaming, if the entity is referenced,
528 -- then so is the renamed subprogram. But there is an issue
529 -- with generic bodies because instantiations are not done
530 -- yet and, therefore, cannot be scanned for referencers.
531 -- That's why we use an approximation and test that we have
532 -- at least one subprogram referenced by an inlined body
533 -- instead of precisely the entity of this renaming.
535 if Nkind (Decl) = N_Subprogram_Renaming_Declaration
536 and then Subprogram_Table.Get_First
537 and then Is_Entity_Name (Name (Decl))
538 and then Present (Entity (Name (Decl)))
539 and then Is_Subprogram (Entity (Name (Decl)))
540 then
541 Subprogram_Table.Set (Entity (Name (Decl)), True);
542 end if;
543 end if;
545 Prev (Decl);
546 end loop;
548 return Has_Referencer_Of_Non_Subprograms;
549 end Has_Referencer;
551 -------------------------
552 -- Scan_Subprogram_Ref --
553 -------------------------
555 function Scan_Subprogram_Ref (N : Node_Id) return Traverse_Result is
556 begin
557 -- Detect a reference of the form
558 -- Subp_Call
560 if Nkind (N) in N_Subprogram_Call
561 and then Is_Entity_Name (Name (N))
562 and then Present (Entity (Name (N)))
563 and then Is_Subprogram (Entity (Name (N)))
564 then
565 Subprogram_Table.Set (Entity (Name (N)), True);
567 -- Detect a reference of the form
568 -- Subp'Some_Attribute
570 elsif Nkind (N) = N_Attribute_Reference
571 and then Is_Entity_Name (Prefix (N))
572 and then Present (Entity (Prefix (N)))
573 and then Is_Subprogram (Entity (Prefix (N)))
574 then
575 Subprogram_Table.Set (Entity (Prefix (N)), True);
577 -- Constants can be substituted by their value in gigi, which may
578 -- contain a reference, so scan the value recursively.
580 elsif Is_Entity_Name (N)
581 and then Present (Entity (N))
582 and then Ekind (Entity (N)) = E_Constant
583 then
584 declare
585 Val : constant Node_Id := Constant_Value (Entity (N));
586 begin
587 if Present (Val)
588 and then not Compile_Time_Known_Value (Val)
589 then
590 Scan_Subprogram_Refs (Val);
591 end if;
592 end;
593 end if;
595 return OK;
596 end Scan_Subprogram_Ref;
598 --------------------------
599 -- Scan_Subprogram_Refs --
600 --------------------------
602 procedure Scan_Subprogram_Refs (Node : Node_Id) is
603 begin
604 if not Traversed_Table.Get (Node) then
605 Traversed_Table.Set (Node, True);
606 Traverse_And_Scan_Subprogram_Refs (Node);
607 end if;
608 end Scan_Subprogram_Refs;
610 -- Local variables
612 Discard : Boolean;
613 pragma Unreferenced (Discard);
615 -- Start of processing for Hide_Public_Entities
617 begin
618 -- The algorithm examines the top level declarations of a package
619 -- body in reverse looking for a construct that may export entities
620 -- declared prior to it. If such a scenario is encountered, then all
621 -- entities in the range Last (Decls) .. construct are hidden from
622 -- external visibility. Consider:
624 -- package Pack is
625 -- generic
626 -- package Gen is
627 -- end Gen;
628 -- end Pack;
630 -- package body Pack is
631 -- External_Obj : ...; -- (1)
633 -- package body Gen is -- (2)
634 -- ... External_Obj ... -- (3)
635 -- end Gen;
637 -- Local_Obj : ...; -- (4)
638 -- end Pack;
640 -- In this example Local_Obj (4) must not be externally visible as
641 -- it cannot be exported by anything in Pack. The body of generic
642 -- package Gen (2) on the other hand acts as a "referencer" and may
643 -- export anything declared before it. Since the compiler does not
644 -- perform flow analysis, it is not possible to determine precisely
645 -- which entities will be exported when Gen is instantiated. In the
646 -- example above External_Obj (1) is exported at (3), but this may
647 -- not always be the case. The algorithm takes a conservative stance
648 -- and leaves entity External_Obj public.
650 -- This very conservative algorithm is supplemented by a more precise
651 -- processing for inlined bodies. For them, we traverse the syntactic
652 -- tree and record which subprograms are actually referenced from it.
653 -- This makes it possible to compute a much smaller set of externally
654 -- visible subprograms in the absence of generic bodies, which can
655 -- have a significant impact on the inlining decisions made in the
656 -- back end and the removal of out-of-line bodies from the object
657 -- code. We do it only for inlined bodies because they are supposed
658 -- to be reasonably small and tree traversal is very expensive.
660 -- Note that even this special processing is not optimal for inlined
661 -- bodies, because we treat all inlined subprograms alike. An optimal
662 -- algorithm would require computing the transitive closure of the
663 -- inlined subprograms that can really be referenced from other units
664 -- in the source code.
666 -- We could extend this processing for inlined bodies and record all
667 -- entities, not just subprograms, referenced from them, which would
668 -- make it possible to compute a much smaller set of all externally
669 -- visible entities in the absence of generic bodies. But this would
670 -- mean implementing a more thorough tree traversal of the bodies,
671 -- i.e. not just syntactic, and the gain would very likely be worth
672 -- neither the hassle nor the slowdown of the compiler.
674 -- Finally, an important thing to be aware of is that, at this point,
675 -- instantiations are not done yet so we cannot directly see inlined
676 -- bodies coming from them. That's not catastrophic because only the
677 -- actual parameters of the instantiations matter here, and they are
678 -- present in the declarations list of the instantiated packages.
680 Traversed_Table.Reset;
681 Subprogram_Table.Reset;
682 Discard := Has_Referencer (Decls, False, False);
683 end Hide_Public_Entities;
685 ----------------------------------
686 -- Install_Composite_Operations --
687 ----------------------------------
689 procedure Install_Composite_Operations (P : Entity_Id) is
690 Id : Entity_Id;
692 begin
693 Id := First_Entity (P);
694 while Present (Id) loop
695 if Is_Type (Id)
696 and then (Is_Limited_Composite (Id)
697 or else Is_Private_Composite (Id))
698 and then No (Private_Component (Id))
699 then
700 Set_Is_Limited_Composite (Id, False);
701 Set_Is_Private_Composite (Id, False);
702 end if;
704 Next_Entity (Id);
705 end loop;
706 end Install_Composite_Operations;
708 -- Local variables
710 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
711 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
712 Saved_EA : constant Boolean := Expander_Active;
713 Saved_ISMP : constant Boolean :=
714 Ignore_SPARK_Mode_Pragmas_In_Instance;
715 -- Save the Ghost and SPARK mode-related data to restore on exit
717 Body_Id : Entity_Id;
718 HSS : Node_Id;
719 Last_Spec_Entity : Entity_Id;
720 New_N : Node_Id;
721 Pack_Decl : Node_Id;
722 Spec_Id : Entity_Id;
724 -- Start of processing for Analyze_Package_Body_Helper
726 begin
727 -- Find corresponding package specification, and establish the current
728 -- scope. The visible defining entity for the package is the defining
729 -- occurrence in the spec. On exit from the package body, all body
730 -- declarations are attached to the defining entity for the body, but
731 -- the later is never used for name resolution. In this fashion there
732 -- is only one visible entity that denotes the package.
734 -- Set Body_Id. Note that this will be reset to point to the generic
735 -- copy later on in the generic case.
737 Body_Id := Defining_Entity (N);
739 -- Body is body of package instantiation. Corresponding spec has already
740 -- been set.
742 if Present (Corresponding_Spec (N)) then
743 Spec_Id := Corresponding_Spec (N);
744 Pack_Decl := Unit_Declaration_Node (Spec_Id);
746 else
747 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
749 if Present (Spec_Id)
750 and then Is_Package_Or_Generic_Package (Spec_Id)
751 then
752 Pack_Decl := Unit_Declaration_Node (Spec_Id);
754 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
755 Error_Msg_N ("cannot supply body for package renaming", N);
756 return;
758 elsif Present (Corresponding_Body (Pack_Decl)) then
759 Error_Msg_N ("redefinition of package body", N);
760 return;
761 end if;
763 else
764 Error_Msg_N ("missing specification for package body", N);
765 return;
766 end if;
768 if Is_Package_Or_Generic_Package (Spec_Id)
769 and then (Scope (Spec_Id) = Standard_Standard
770 or else Is_Child_Unit (Spec_Id))
771 and then not Unit_Requires_Body (Spec_Id)
772 then
773 if Ada_Version = Ada_83 then
774 Error_Msg_N
775 ("optional package body (not allowed in Ada 95)??", N);
776 else
777 Error_Msg_N ("spec of this package does not allow a body", N);
778 Error_Msg_N ("\either remove the body or add pragma "
779 & "Elaborate_Body in the spec", N);
780 end if;
781 end if;
782 end if;
784 -- A [generic] package body freezes the contract of the nearest
785 -- enclosing package body and all other contracts encountered in
786 -- the same declarative part up to and excluding the package body:
788 -- package body Nearest_Enclosing_Package
789 -- with Refined_State => (State => Constit)
790 -- is
791 -- Constit : ...;
793 -- package body Freezes_Enclosing_Package_Body
794 -- with Refined_State => (State_2 => Constit_2)
795 -- is
796 -- Constit_2 : ...;
798 -- procedure Proc
799 -- with Refined_Depends => (Input => (Constit, Constit_2)) ...
801 -- This ensures that any annotations referenced by the contract of a
802 -- [generic] subprogram body declared within the current package body
803 -- are available. This form of freezing is decoupled from the usual
804 -- Freeze_xxx mechanism because it must also work in the context of
805 -- generics where normal freezing is disabled.
807 -- Only bodies coming from source should cause this type of freezing.
808 -- Instantiated generic bodies are excluded because their processing is
809 -- performed in a separate compilation pass which lacks enough semantic
810 -- information with respect to contract analysis. It is safe to suppress
811 -- the freezing of contracts in this case because this action already
812 -- took place at the end of the enclosing declarative part.
814 if Comes_From_Source (N)
815 and then not Is_Generic_Instance (Spec_Id)
816 then
817 Freeze_Previous_Contracts (N);
818 end if;
820 -- A package body is Ghost when the corresponding spec is Ghost. Set
821 -- the mode now to ensure that any nodes generated during analysis and
822 -- expansion are properly flagged as ignored Ghost.
824 Mark_And_Set_Ghost_Body (N, Spec_Id);
826 -- Deactivate expansion inside the body of ignored Ghost entities,
827 -- as this code will ultimately be ignored. This avoids requiring the
828 -- presence of run-time units which are not needed. Only do this for
829 -- user entities, as internally generated entities might still need
830 -- to be expanded (e.g. those generated for types).
832 if Present (Ignored_Ghost_Region)
833 and then Comes_From_Source (Body_Id)
834 then
835 Expander_Active := False;
836 end if;
838 -- If the body completes the initial declaration of a compilation unit
839 -- which is subject to pragma Elaboration_Checks, set the model of the
840 -- pragma because it applies to all parts of the unit.
842 Install_Elaboration_Model (Spec_Id);
844 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
845 Style.Check_Identifier (Body_Id, Spec_Id);
847 if Is_Child_Unit (Spec_Id) then
848 if Nkind (Parent (N)) /= N_Compilation_Unit then
849 Error_Msg_NE
850 ("body of child unit& cannot be an inner package", N, Spec_Id);
851 end if;
853 Set_Is_Child_Unit (Body_Id);
854 end if;
856 -- Generic package case
858 if Ekind (Spec_Id) = E_Generic_Package then
860 -- Disable expansion and perform semantic analysis on copy. The
861 -- unannotated body will be used in all instantiations.
863 Body_Id := Defining_Entity (N);
864 Mutate_Ekind (Body_Id, E_Package_Body);
865 Set_Scope (Body_Id, Scope (Spec_Id));
866 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
867 Set_Body_Entity (Spec_Id, Body_Id);
868 Set_Spec_Entity (Body_Id, Spec_Id);
870 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
871 Rewrite (N, New_N);
873 -- Once the contents of the generic copy and the template are
874 -- swapped, do the same for their respective aspect specifications.
876 Exchange_Aspects (N, New_N);
878 -- Collect all contract-related source pragmas found within the
879 -- template and attach them to the contract of the package body.
880 -- This contract is used in the capture of global references within
881 -- annotations.
883 Create_Generic_Contract (N);
885 -- Update Body_Id to point to the copied node for the remainder of
886 -- the processing.
888 Body_Id := Defining_Entity (N);
889 Start_Generic;
890 end if;
892 -- The Body_Id is that of the copied node in the generic case, the
893 -- current node otherwise. Note that N was rewritten above, so we must
894 -- be sure to get the latest Body_Id value.
896 Mutate_Ekind (Body_Id, E_Package_Body);
897 Set_Body_Entity (Spec_Id, Body_Id);
898 Set_Spec_Entity (Body_Id, Spec_Id);
900 -- Defining name for the package body is not a visible entity: Only the
901 -- defining name for the declaration is visible.
903 Set_Etype (Body_Id, Standard_Void_Type);
904 Set_Scope (Body_Id, Scope (Spec_Id));
905 Set_Corresponding_Spec (N, Spec_Id);
906 Set_Corresponding_Body (Pack_Decl, Body_Id);
908 -- The body entity is not used for semantics or code generation, but
909 -- it is attached to the entity list of the enclosing scope to simplify
910 -- the listing of back-annotations for the types it main contain.
912 if Scope (Spec_Id) /= Standard_Standard then
913 Append_Entity (Body_Id, Scope (Spec_Id));
914 end if;
916 -- Indicate that we are currently compiling the body of the package
918 Set_In_Package_Body (Spec_Id);
919 Set_Has_Completion (Spec_Id);
920 Last_Spec_Entity := Last_Entity (Spec_Id);
922 if Has_Aspects (N) then
923 Analyze_Aspect_Specifications (N, Body_Id);
924 end if;
926 Push_Scope (Spec_Id);
928 -- Set SPARK_Mode only for non-generic package
930 if Ekind (Spec_Id) = E_Package then
931 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
932 Set_SPARK_Aux_Pragma (Body_Id, SPARK_Mode_Pragma);
933 Set_SPARK_Pragma_Inherited (Body_Id);
934 Set_SPARK_Aux_Pragma_Inherited (Body_Id);
936 -- A package body may be instantiated or inlined at a later pass.
937 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when
938 -- it applied to the package spec.
940 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
941 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
942 end if;
943 end if;
945 Set_Categorization_From_Pragmas (N);
947 Install_Visible_Declarations (Spec_Id);
948 Install_Private_Declarations (Spec_Id);
949 Install_Private_With_Clauses (Spec_Id);
950 Install_Composite_Operations (Spec_Id);
952 Check_Anonymous_Access_Types (Spec_Id, N);
954 if Ekind (Spec_Id) = E_Generic_Package then
955 Set_Use (Generic_Formal_Declarations (Pack_Decl));
956 end if;
958 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
959 Set_Use (Private_Declarations (Specification (Pack_Decl)));
961 -- This is a nested package, so it may be necessary to declare certain
962 -- inherited subprograms that are not yet visible because the parent
963 -- type's subprograms are now visible.
964 -- Note that for child units these operations were generated when
965 -- analyzing the package specification.
967 if Ekind (Scope (Spec_Id)) = E_Package
968 and then Scope (Spec_Id) /= Standard_Standard
969 and then not Is_Child_Unit (Spec_Id)
970 then
971 Declare_Inherited_Private_Subprograms (Spec_Id);
972 end if;
974 if Present (Declarations (N)) then
975 Analyze_Declarations (Declarations (N));
976 Inspect_Deferred_Constant_Completion (Declarations (N));
977 end if;
979 -- Verify that the SPARK_Mode of the body agrees with that of its spec
981 if Present (SPARK_Pragma (Body_Id)) then
982 if Present (SPARK_Aux_Pragma (Spec_Id)) then
983 if Get_SPARK_Mode_From_Annotation (SPARK_Aux_Pragma (Spec_Id)) =
985 and then
986 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
987 then
988 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
989 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
990 Error_Msg_Sloc := Sloc (SPARK_Aux_Pragma (Spec_Id));
991 Error_Msg_NE
992 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
993 end if;
995 -- SPARK_Mode Off could complete no SPARK_Mode in a generic, either
996 -- as specified in source code, or because SPARK_Mode On is ignored
997 -- in an instance where the context is SPARK_Mode Off/Auto.
999 elsif Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = Off
1000 and then (Is_Generic_Unit (Spec_Id) or else In_Instance)
1001 then
1002 null;
1004 else
1005 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
1006 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
1007 Error_Msg_Sloc := Sloc (Spec_Id);
1008 Error_Msg_NE
1009 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
1010 end if;
1011 end if;
1013 -- Analyze_Declarations has caused freezing of all types. Now generate
1014 -- bodies for RACW primitives and stream attributes, if any.
1016 if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then
1018 -- Attach subprogram bodies to support RACWs declared in spec
1020 Append_RACW_Bodies (Declarations (N), Spec_Id);
1021 Analyze_List (Declarations (N));
1022 end if;
1024 HSS := Handled_Statement_Sequence (N);
1026 if Present (HSS) then
1027 Process_End_Label (HSS, 't', Spec_Id);
1028 Analyze (HSS);
1030 -- Check that elaboration code in a preelaborable package body is
1031 -- empty other than null statements and labels (RM 10.2.1(6)).
1033 Validate_Null_Statement_Sequence (N);
1034 end if;
1036 Validate_Categorization_Dependency (N, Spec_Id);
1037 Check_Completion (Body_Id);
1039 -- Generate start of body reference. Note that we do this fairly late,
1040 -- because the call will use In_Extended_Main_Source_Unit as a check,
1041 -- and we want to make sure that Corresponding_Stub links are set
1043 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
1045 -- For a generic package, collect global references and mark them on
1046 -- the original body so that they are not resolved again at the point
1047 -- of instantiation.
1049 if Ekind (Spec_Id) /= E_Package then
1050 Save_Global_References (Original_Node (N));
1051 End_Generic;
1052 end if;
1054 -- The entities of the package body have so far been chained onto the
1055 -- declaration chain for the spec. That's been fine while we were in the
1056 -- body, since we wanted them to be visible, but now that we are leaving
1057 -- the package body, they are no longer visible, so we remove them from
1058 -- the entity chain of the package spec entity, and copy them to the
1059 -- entity chain of the package body entity, where they will never again
1060 -- be visible.
1062 if Present (Last_Spec_Entity) then
1063 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
1064 Set_Next_Entity (Last_Spec_Entity, Empty);
1065 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1066 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
1068 else
1069 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
1070 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1071 Set_First_Entity (Spec_Id, Empty);
1072 Set_Last_Entity (Spec_Id, Empty);
1073 end if;
1075 Update_Use_Clause_Chain;
1076 End_Package_Scope (Spec_Id);
1078 -- All entities declared in body are not visible
1080 declare
1081 E : Entity_Id;
1083 begin
1084 E := First_Entity (Body_Id);
1085 while Present (E) loop
1086 Set_Is_Immediately_Visible (E, False);
1087 Set_Is_Potentially_Use_Visible (E, False);
1088 Set_Is_Hidden (E);
1090 -- Child units may appear on the entity list (e.g. if they appear
1091 -- in the context of a subunit) but they are not body entities.
1093 if not Is_Child_Unit (E) then
1094 Set_Is_Package_Body_Entity (E);
1095 end if;
1097 Next_Entity (E);
1098 end loop;
1099 end;
1101 Check_References (Body_Id);
1103 -- For a generic unit, check that the formal parameters are referenced,
1104 -- and that local variables are used, as for regular packages.
1106 if Ekind (Spec_Id) = E_Generic_Package then
1107 Check_References (Spec_Id);
1108 end if;
1110 -- At this point all entities of the package body are externally visible
1111 -- to the linker as their Is_Public flag is set to True. This proactive
1112 -- approach is necessary because an inlined or a generic body for which
1113 -- code is generated in other units may need to see these entities. Cut
1114 -- down the number of global symbols that do not need public visibility
1115 -- as this has two beneficial effects:
1116 -- (1) It makes the compilation process more efficient.
1117 -- (2) It gives the code generator more leeway to optimize within each
1118 -- unit, especially subprograms.
1120 -- This is done only for top-level library packages or child units as
1121 -- the algorithm does a top-down traversal of the package body. This is
1122 -- also done for instances because instantiations are still pending by
1123 -- the time the enclosing package body is analyzed.
1125 if (Scope (Spec_Id) = Standard_Standard
1126 or else Is_Child_Unit (Spec_Id)
1127 or else Is_Generic_Instance (Spec_Id))
1128 and then not Is_Generic_Unit (Spec_Id)
1129 then
1130 Hide_Public_Entities (Declarations (N));
1131 end if;
1133 -- If expander is not active, then here is where we turn off the
1134 -- In_Package_Body flag, otherwise it is turned off at the end of the
1135 -- corresponding expansion routine. If this is an instance body, we need
1136 -- to qualify names of local entities, because the body may have been
1137 -- compiled as a preliminary to another instantiation.
1139 if not Expander_Active then
1140 Set_In_Package_Body (Spec_Id, False);
1142 if Is_Generic_Instance (Spec_Id)
1143 and then Operating_Mode = Generate_Code
1144 then
1145 Qualify_Entity_Names (N);
1146 end if;
1147 end if;
1149 if Present (Ignored_Ghost_Region) then
1150 Expander_Active := Saved_EA;
1151 end if;
1153 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
1154 Restore_Ghost_Region (Saved_GM, Saved_IGR);
1155 end Analyze_Package_Body_Helper;
1157 ---------------------------------
1158 -- Analyze_Package_Declaration --
1159 ---------------------------------
1161 procedure Analyze_Package_Declaration (N : Node_Id) is
1162 Id : constant Node_Id := Defining_Entity (N);
1164 Is_Comp_Unit : constant Boolean :=
1165 Nkind (Parent (N)) = N_Compilation_Unit;
1167 Body_Required : Boolean;
1168 -- True when this package declaration requires a corresponding body
1170 begin
1171 if Debug_Flag_C then
1172 Write_Str ("==> package spec ");
1173 Write_Name (Chars (Id));
1174 Write_Str (" from ");
1175 Write_Location (Sloc (N));
1176 Write_Eol;
1177 Indent;
1178 end if;
1180 Generate_Definition (Id);
1181 Enter_Name (Id);
1182 Mutate_Ekind (Id, E_Package);
1183 Set_Etype (Id, Standard_Void_Type);
1185 -- Set SPARK_Mode from context
1187 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
1188 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
1189 Set_SPARK_Pragma_Inherited (Id);
1190 Set_SPARK_Aux_Pragma_Inherited (Id);
1192 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
1193 -- the body of this package is instantiated or inlined later and out of
1194 -- context. The body uses this attribute to restore the value of the
1195 -- global flag.
1197 if Ignore_SPARK_Mode_Pragmas_In_Instance then
1198 Set_Ignore_SPARK_Mode_Pragmas (Id);
1199 end if;
1201 -- Analyze aspect specifications immediately, since we need to recognize
1202 -- things like Pure early enough to diagnose violations during analysis.
1204 if Has_Aspects (N) then
1205 Analyze_Aspect_Specifications (N, Id);
1206 end if;
1208 -- Ada 2005 (AI-217): Check if the package has been illegally named in
1209 -- a limited-with clause of its own context. In this case the error has
1210 -- been previously notified by Analyze_Context.
1212 -- limited with Pkg; -- ERROR
1213 -- package Pkg is ...
1215 if From_Limited_With (Id) then
1216 return;
1217 end if;
1219 Push_Scope (Id);
1221 Set_Is_Pure (Id, Is_Pure (Enclosing_Lib_Unit_Entity));
1222 Set_Categorization_From_Pragmas (N);
1224 Analyze (Specification (N));
1225 Validate_Categorization_Dependency (N, Id);
1227 -- Determine whether the package requires a body. Abstract states are
1228 -- intentionally ignored because they do require refinement which can
1229 -- only come in a body, but at the same time they do not force the need
1230 -- for a body on their own (SPARK RM 7.1.4(4) and 7.2.2(3)).
1232 Body_Required := Unit_Requires_Body (Id);
1234 if not Body_Required then
1236 -- If the package spec does not require an explicit body, then there
1237 -- are not entities requiring completion in the language sense. Call
1238 -- Check_Completion now to ensure that nested package declarations
1239 -- that require an implicit body get one. (In the case where a body
1240 -- is required, Check_Completion is called at the end of the body's
1241 -- declarative part.)
1243 Check_Completion;
1245 -- If the package spec does not require an explicit body, then all
1246 -- abstract states declared in nested packages cannot possibly get a
1247 -- proper refinement (SPARK RM 7.1.4(4) and SPARK RM 7.2.2(3)). This
1248 -- check is performed only when the compilation unit is the main
1249 -- unit to allow for modular SPARK analysis where packages do not
1250 -- necessarily have bodies.
1252 if Is_Comp_Unit then
1253 Check_State_Refinements
1254 (Context => N,
1255 Is_Main_Unit => Parent (N) = Cunit (Main_Unit));
1256 end if;
1258 -- Warn about references to unset objects, which is straightforward
1259 -- for packages with no bodies. For packages with bodies this is more
1260 -- complicated, because some of the objects might be set between spec
1261 -- and body elaboration, in nested or child packages, etc.
1263 Check_References (Id);
1264 end if;
1266 -- Set Body_Required indication on the compilation unit node
1268 if Is_Comp_Unit then
1269 Set_Body_Required (Parent (N), Body_Required);
1271 if Legacy_Elaboration_Checks and not Body_Required then
1272 Set_Suppress_Elaboration_Warnings (Id);
1273 end if;
1274 end if;
1276 End_Package_Scope (Id);
1278 -- For the declaration of a library unit that is a remote types package,
1279 -- check legality rules regarding availability of stream attributes for
1280 -- types that contain non-remote access values. This subprogram performs
1281 -- visibility tests that rely on the fact that we have exited the scope
1282 -- of Id.
1284 if Is_Comp_Unit then
1285 Validate_RT_RAT_Component (N);
1286 end if;
1288 if Debug_Flag_C then
1289 Outdent;
1290 Write_Str ("<== package spec ");
1291 Write_Name (Chars (Id));
1292 Write_Str (" from ");
1293 Write_Location (Sloc (N));
1294 Write_Eol;
1295 end if;
1296 end Analyze_Package_Declaration;
1298 -----------------------------------
1299 -- Analyze_Package_Specification --
1300 -----------------------------------
1302 -- Note that this code is shared for the analysis of generic package specs
1303 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
1305 procedure Analyze_Package_Specification (N : Node_Id) is
1306 Id : constant Entity_Id := Defining_Entity (N);
1307 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
1308 Vis_Decls : constant List_Id := Visible_Declarations (N);
1309 Priv_Decls : constant List_Id := Private_Declarations (N);
1310 E : Entity_Id;
1311 L : Entity_Id;
1312 Public_Child : Boolean;
1314 Private_With_Clauses_Installed : Boolean := False;
1315 -- In Ada 2005, private with_clauses are visible in the private part
1316 -- of a nested package, even if it appears in the public part of the
1317 -- enclosing package. This requires a separate step to install these
1318 -- private_with_clauses, and remove them at the end of the nested
1319 -- package.
1321 procedure Clear_Constants (Id : Entity_Id);
1322 -- Clears constant indications (Never_Set_In_Source, Constant_Value,
1323 -- and Is_True_Constant) on all variables that are entities of Id.
1324 -- A recursive call is made for all packages and generic packages.
1326 procedure Generate_Parent_References;
1327 -- For a child unit, generate references to parent units, for
1328 -- GNAT Studio navigation purposes.
1330 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
1331 -- Child and Unit are entities of compilation units. True if Child
1332 -- is a public child of Parent as defined in 10.1.1
1334 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
1335 -- Reject completion of an incomplete or private type declarations
1336 -- having a known discriminant part by an unchecked union.
1338 procedure Inspect_Untagged_Record_Completion (Decls : List_Id);
1339 -- Find out whether a nonlimited untagged record completion has got a
1340 -- primitive equality operator and, if so, make it so that it will be
1341 -- used as the predefined operator of the private view of the record.
1343 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
1344 -- Given the package entity of a generic package instantiation or
1345 -- formal package whose corresponding generic is a child unit, installs
1346 -- the private declarations of each of the child unit's parents.
1347 -- This has to be done at the point of entering the instance package's
1348 -- private part rather than being done in Sem_Ch12.Install_Parent
1349 -- (which is where the parents' visible declarations are installed).
1351 ---------------------
1352 -- Clear_Constants --
1353 ---------------------
1355 procedure Clear_Constants (Id : Entity_Id) is
1356 E : Entity_Id;
1358 begin
1359 -- Ignore package renamings, not interesting and they can cause self
1360 -- referential loops in the code below.
1362 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
1363 return;
1364 end if;
1366 -- Note: in the loop below, the check for Next_Entity pointing back
1367 -- to the package entity may seem odd, but it is needed, because a
1368 -- package can contain a renaming declaration to itself, and such
1369 -- renamings are generated automatically within package instances.
1371 E := First_Entity (Id);
1372 while Present (E) and then E /= Id loop
1373 if Ekind (E) = E_Variable then
1374 Set_Never_Set_In_Source (E, False);
1375 Set_Is_True_Constant (E, False);
1376 Set_Current_Value (E, Empty);
1377 Set_Is_Known_Null (E, False);
1378 Set_Last_Assignment (E, Empty);
1380 if not Can_Never_Be_Null (E) then
1381 Set_Is_Known_Non_Null (E, False);
1382 end if;
1384 elsif Is_Package_Or_Generic_Package (E) then
1385 Clear_Constants (E);
1386 end if;
1388 Next_Entity (E);
1389 end loop;
1390 end Clear_Constants;
1392 --------------------------------
1393 -- Generate_Parent_References --
1394 --------------------------------
1396 procedure Generate_Parent_References is
1397 Decl : constant Node_Id := Parent (N);
1399 begin
1400 if Id = Cunit_Entity (Main_Unit)
1401 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
1402 then
1403 Generate_Reference (Id, Scope (Id), 'k', False);
1405 elsif Nkind (Unit (Cunit (Main_Unit))) not in
1406 N_Subprogram_Body | N_Subunit
1407 then
1408 -- If current unit is an ancestor of main unit, generate a
1409 -- reference to its own parent.
1411 declare
1412 U : Node_Id;
1413 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
1415 begin
1416 if Nkind (Main_Spec) = N_Package_Body then
1417 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
1418 end if;
1420 U := Parent_Spec (Main_Spec);
1421 while Present (U) loop
1422 if U = Parent (Decl) then
1423 Generate_Reference (Id, Scope (Id), 'k', False);
1424 exit;
1426 elsif Nkind (Unit (U)) = N_Package_Body then
1427 exit;
1429 else
1430 U := Parent_Spec (Unit (U));
1431 end if;
1432 end loop;
1433 end;
1434 end if;
1435 end Generate_Parent_References;
1437 ---------------------
1438 -- Is_Public_Child --
1439 ---------------------
1441 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
1442 begin
1443 if not Is_Private_Descendant (Child) then
1444 return True;
1445 else
1446 if Child = Unit then
1447 return not Private_Present (
1448 Parent (Unit_Declaration_Node (Child)));
1449 else
1450 return Is_Public_Child (Scope (Child), Unit);
1451 end if;
1452 end if;
1453 end Is_Public_Child;
1455 ----------------------------------------
1456 -- Inspect_Unchecked_Union_Completion --
1457 ----------------------------------------
1459 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
1460 Decl : Node_Id;
1462 begin
1463 Decl := First (Decls);
1464 while Present (Decl) loop
1466 -- We are looking for an incomplete or private type declaration
1467 -- with a known_discriminant_part whose full view is an
1468 -- Unchecked_Union. The seemingly useless check with Is_Type
1469 -- prevents cascaded errors when routines defined only for type
1470 -- entities are called with non-type entities.
1472 if Nkind (Decl) in N_Incomplete_Type_Declaration
1473 | N_Private_Type_Declaration
1474 and then Is_Type (Defining_Identifier (Decl))
1475 and then Has_Discriminants (Defining_Identifier (Decl))
1476 and then Present (Full_View (Defining_Identifier (Decl)))
1477 and then
1478 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
1479 then
1480 Error_Msg_N
1481 ("completion of discriminated partial view "
1482 & "cannot be an unchecked union",
1483 Full_View (Defining_Identifier (Decl)));
1484 end if;
1486 Next (Decl);
1487 end loop;
1488 end Inspect_Unchecked_Union_Completion;
1490 ----------------------------------------
1491 -- Inspect_Untagged_Record_Completion --
1492 ----------------------------------------
1494 procedure Inspect_Untagged_Record_Completion (Decls : List_Id) is
1495 Decl : Node_Id;
1497 begin
1498 Decl := First (Decls);
1499 while Present (Decl) loop
1501 -- We are looking for a full type declaration of an untagged
1502 -- record with a private declaration and primitive operations.
1504 if Nkind (Decl) in N_Full_Type_Declaration
1505 and then Is_Record_Type (Defining_Identifier (Decl))
1506 and then not Is_Limited_Type (Defining_Identifier (Decl))
1507 and then not Is_Tagged_Type (Defining_Identifier (Decl))
1508 and then Has_Private_Declaration (Defining_Identifier (Decl))
1509 and then Has_Primitive_Operations (Defining_Identifier (Decl))
1510 then
1511 declare
1512 Prim_List : constant Elist_Id :=
1513 Collect_Primitive_Operations (Defining_Identifier (Decl));
1515 E : Entity_Id;
1516 Ne_Id : Entity_Id;
1517 Op_Decl : Node_Id;
1518 Op_Id : Entity_Id;
1519 Prim : Elmt_Id;
1521 begin
1522 Prim := First_Elmt (Prim_List);
1523 while Present (Prim) loop
1524 Op_Id := Node (Prim);
1525 Op_Decl := Declaration_Node (Op_Id);
1526 if Nkind (Op_Decl) in N_Subprogram_Specification then
1527 Op_Decl := Parent (Op_Decl);
1528 end if;
1530 -- We are looking for an equality operator immediately
1531 -- visible and declared in the private part followed by
1532 -- the synthesized inequality operator.
1534 if Is_User_Defined_Equality (Op_Id)
1535 and then Is_Immediately_Visible (Op_Id)
1536 and then List_Containing (Op_Decl) = Decls
1537 then
1538 Ne_Id := Next_Entity (Op_Id);
1539 pragma Assert (Ekind (Ne_Id) = E_Function
1540 and then Corresponding_Equality (Ne_Id) = Op_Id);
1542 E := First_Private_Entity (Id);
1544 -- Move them from the private part of the entity list
1545 -- up to the end of the visible part of the same list.
1547 Remove_Entity (Op_Id);
1548 Remove_Entity (Ne_Id);
1550 Link_Entities (Prev_Entity (E), Op_Id);
1551 Link_Entities (Op_Id, Ne_Id);
1552 Link_Entities (Ne_Id, E);
1554 -- And if the private part contains another equality
1555 -- operator, move the equality operator to after it
1556 -- in the homonym chain, so that all its next homonyms
1557 -- in the same scope, if any, also are in the visible
1558 -- part. This is relied upon to resolve expanded names
1559 -- in Collect_Interps for example.
1561 while Present (E) loop
1562 exit when Ekind (E) = E_Function
1563 and then Chars (E) = Name_Op_Eq;
1565 Next_Entity (E);
1566 end loop;
1568 if Present (E) then
1569 Remove_Homonym (Op_Id);
1571 Set_Homonym (Op_Id, Homonym (E));
1572 Set_Homonym (E, Op_Id);
1573 end if;
1575 exit;
1576 end if;
1578 Next_Elmt (Prim);
1579 end loop;
1580 end;
1581 end if;
1583 Next (Decl);
1584 end loop;
1585 end Inspect_Untagged_Record_Completion;
1587 -----------------------------------------
1588 -- Install_Parent_Private_Declarations --
1589 -----------------------------------------
1591 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
1592 Inst_Par : Entity_Id;
1593 Gen_Par : Entity_Id;
1594 Inst_Node : Node_Id;
1596 begin
1597 Inst_Par := Inst_Id;
1599 Gen_Par :=
1600 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
1601 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
1602 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
1604 if Nkind (Inst_Node) in
1605 N_Package_Instantiation | N_Formal_Package_Declaration
1606 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
1607 then
1608 Inst_Par := Entity (Prefix (Name (Inst_Node)));
1610 if Present (Renamed_Entity (Inst_Par)) then
1611 Inst_Par := Renamed_Entity (Inst_Par);
1612 end if;
1614 -- The instance may appear in a sibling generic unit, in
1615 -- which case the prefix must include the common (generic)
1616 -- ancestor, which is treated as a current instance.
1618 if Inside_A_Generic
1619 and then Ekind (Inst_Par) = E_Generic_Package
1620 then
1621 Gen_Par := Inst_Par;
1622 pragma Assert (In_Open_Scopes (Gen_Par));
1624 else
1625 Gen_Par :=
1626 Generic_Parent
1627 (Specification (Unit_Declaration_Node (Inst_Par)));
1628 end if;
1630 -- Install the private declarations and private use clauses
1631 -- of a parent instance of the child instance, unless the
1632 -- parent instance private declarations have already been
1633 -- installed earlier in Analyze_Package_Specification, which
1634 -- happens when a generic child is instantiated, and the
1635 -- instance is a child of the parent instance.
1637 -- Installing the use clauses of the parent instance twice
1638 -- is both unnecessary and wrong, because it would cause the
1639 -- clauses to be chained to themselves in the use clauses
1640 -- list of the scope stack entry. That in turn would cause
1641 -- an endless loop from End_Use_Clauses upon scope exit.
1643 -- The parent is now fully visible. It may be a hidden open
1644 -- scope if we are currently compiling some child instance
1645 -- declared within it, but while the current instance is being
1646 -- compiled the parent is immediately visible. In particular
1647 -- its entities must remain visible if a stack save/restore
1648 -- takes place through a call to Rtsfind.
1650 if Present (Gen_Par) then
1651 if not In_Private_Part (Inst_Par) then
1652 Install_Private_Declarations (Inst_Par);
1653 Set_Use (Private_Declarations
1654 (Specification
1655 (Unit_Declaration_Node (Inst_Par))));
1656 Set_Is_Hidden_Open_Scope (Inst_Par, False);
1657 end if;
1659 -- If we've reached the end of the generic instance parents,
1660 -- then finish off by looping through the nongeneric parents
1661 -- and installing their private declarations.
1663 -- If one of the non-generic parents is itself on the scope
1664 -- stack, do not install its private declarations: they are
1665 -- installed in due time when the private part of that parent
1666 -- is analyzed.
1668 else
1669 while Present (Inst_Par)
1670 and then Inst_Par /= Standard_Standard
1671 and then (not In_Open_Scopes (Inst_Par)
1672 or else not In_Private_Part (Inst_Par))
1673 loop
1674 if Nkind (Inst_Node) = N_Formal_Package_Declaration
1675 or else
1676 not Is_Ancestor_Package
1677 (Inst_Par, Cunit_Entity (Current_Sem_Unit))
1678 then
1679 Install_Private_Declarations (Inst_Par);
1680 Set_Use
1681 (Private_Declarations
1682 (Specification
1683 (Unit_Declaration_Node (Inst_Par))));
1684 Inst_Par := Scope (Inst_Par);
1685 else
1686 exit;
1687 end if;
1688 end loop;
1690 exit;
1691 end if;
1693 else
1694 exit;
1695 end if;
1696 end loop;
1697 end Install_Parent_Private_Declarations;
1699 -- Start of processing for Analyze_Package_Specification
1701 begin
1702 if Present (Vis_Decls) then
1703 Analyze_Declarations (Vis_Decls);
1704 end if;
1706 -- Inspect the entities defined in the package and ensure that all
1707 -- incomplete types have received full declarations. Build default
1708 -- initial condition and invariant procedures for all qualifying types.
1710 E := First_Entity (Id);
1711 while Present (E) loop
1713 -- Check on incomplete types
1715 -- AI05-0213: A formal incomplete type has no completion, and neither
1716 -- does the corresponding subtype in an instance.
1718 if Is_Incomplete_Type (E)
1719 and then No (Full_View (E))
1720 and then not Is_Generic_Type (E)
1721 and then not From_Limited_With (E)
1722 and then not Is_Generic_Actual_Type (E)
1723 then
1724 Error_Msg_N ("no declaration in visible part for incomplete}", E);
1725 end if;
1727 Next_Entity (E);
1728 end loop;
1730 if Is_Remote_Call_Interface (Id)
1731 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1732 then
1733 Validate_RCI_Declarations (Id);
1734 end if;
1736 -- Save global references in the visible declarations, before installing
1737 -- private declarations of parent unit if there is one, because the
1738 -- privacy status of types defined in the parent will change. This is
1739 -- only relevant for generic child units, but is done in all cases for
1740 -- uniformity.
1742 if Ekind (Id) = E_Generic_Package
1743 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1744 then
1745 declare
1746 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1747 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1749 begin
1750 -- Insert the freezing nodes after the visible declarations to
1751 -- ensure that we analyze its aspects; needed to ensure that
1752 -- global entities referenced in the aspects are properly handled.
1754 if Ada_Version >= Ada_2012
1755 and then Is_Non_Empty_List (Vis_Decls)
1756 and then Is_Empty_List (Priv_Decls)
1757 then
1758 Insert_List_After_And_Analyze
1759 (Last (Vis_Decls), Freeze_Entity (Id, Last (Vis_Decls)));
1760 end if;
1762 Set_Private_Declarations (Orig_Spec, Empty_List);
1763 Save_Global_References (Orig_Decl);
1764 Set_Private_Declarations (Orig_Spec, Save_Priv);
1765 end;
1766 end if;
1768 -- If package is a public child unit, then make the private declarations
1769 -- of the parent visible.
1771 Public_Child := False;
1773 declare
1774 Par : Entity_Id;
1775 Pack_Decl : Node_Id;
1776 Par_Spec : Node_Id;
1778 begin
1779 Par := Id;
1780 Par_Spec := Parent_Spec (Parent (N));
1782 -- If the package is formal package of an enclosing generic, it is
1783 -- transformed into a local generic declaration, and compiled to make
1784 -- its spec available. We need to retrieve the original generic to
1785 -- determine whether it is a child unit, and install its parents.
1787 if No (Par_Spec)
1788 and then
1789 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1790 then
1791 Par := Entity (Name (Original_Node (Parent (N))));
1792 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1793 end if;
1795 if Present (Par_Spec) then
1796 Generate_Parent_References;
1798 while Scope (Par) /= Standard_Standard
1799 and then Is_Public_Child (Id, Par)
1800 and then In_Open_Scopes (Par)
1801 loop
1802 Public_Child := True;
1803 Par := Scope (Par);
1804 Install_Private_Declarations (Par);
1805 Install_Private_With_Clauses (Par);
1806 Pack_Decl := Unit_Declaration_Node (Par);
1807 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1808 end loop;
1809 end if;
1810 end;
1812 if Is_Compilation_Unit (Id) then
1813 Install_Private_With_Clauses (Id);
1814 else
1815 -- The current compilation unit may include private with_clauses,
1816 -- which are visible in the private part of the current nested
1817 -- package, and have to be installed now. This is not done for
1818 -- nested instantiations, where the private with_clauses of the
1819 -- enclosing unit have no effect once the instantiation info is
1820 -- established and we start analyzing the package declaration.
1822 declare
1823 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1824 begin
1825 if Is_Package_Or_Generic_Package (Comp_Unit)
1826 and then not In_Private_Part (Comp_Unit)
1827 and then not In_Instance
1828 then
1829 Install_Private_With_Clauses (Comp_Unit);
1830 Private_With_Clauses_Installed := True;
1831 end if;
1832 end;
1833 end if;
1835 -- If this is a package associated with a generic instance or formal
1836 -- package, then the private declarations of each of the generic's
1837 -- parents must be installed at this point, but not if this is the
1838 -- abbreviated instance created to check a formal package, see the
1839 -- same condition in Analyze_Package_Instantiation.
1841 if Is_Generic_Instance (Id)
1842 and then not Is_Abbreviated_Instance (Id)
1843 then
1844 Install_Parent_Private_Declarations (Id);
1845 end if;
1847 -- Analyze private part if present. The flag In_Private_Part is reset
1848 -- in Uninstall_Declarations.
1850 L := Last_Entity (Id);
1852 if Present (Priv_Decls) then
1853 Set_In_Private_Part (Id);
1855 -- Upon entering a public child's private part, it may be necessary
1856 -- to declare subprograms that were derived in the package's visible
1857 -- part but not yet made visible.
1859 if Public_Child then
1860 Declare_Inherited_Private_Subprograms (Id);
1861 end if;
1863 Analyze_Declarations (Priv_Decls);
1865 -- Check the private declarations for incomplete deferred constants
1867 Inspect_Deferred_Constant_Completion (Priv_Decls);
1869 -- The first private entity is the immediate follower of the last
1870 -- visible entity, if there was one.
1872 if Present (L) then
1873 Set_First_Private_Entity (Id, Next_Entity (L));
1874 else
1875 Set_First_Private_Entity (Id, First_Entity (Id));
1876 end if;
1878 -- There may be inherited private subprograms that need to be declared,
1879 -- even in the absence of an explicit private part. If there are any
1880 -- public declarations in the package and the package is a public child
1881 -- unit, then an implicit private part is assumed.
1883 elsif Present (L) and then Public_Child then
1884 Set_In_Private_Part (Id);
1885 Declare_Inherited_Private_Subprograms (Id);
1886 Set_First_Private_Entity (Id, Next_Entity (L));
1887 end if;
1889 E := First_Entity (Id);
1890 while Present (E) loop
1892 -- Check rule of 3.6(11), which in general requires waiting till all
1893 -- full types have been seen.
1895 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1896 Check_Aliased_Component_Types (E);
1897 end if;
1899 -- Check preelaborable initialization for full type completing a
1900 -- private type when aspect Preelaborable_Initialization is True
1901 -- or is specified by Preelaborable_Initialization attributes
1902 -- (in the case of a private type in a generic unit). We pass
1903 -- the expression of the aspect (when present) to the parameter
1904 -- Preelab_Init_Expr to take into account the rule that presumes
1905 -- that subcomponents of generic formal types mentioned in the
1906 -- type's P_I aspect have preelaborable initialization (see
1907 -- AI12-0409 and RM 10.2.1(11.8/5)).
1909 if Is_Type (E) and then Must_Have_Preelab_Init (E) then
1910 declare
1911 PI_Aspect : constant Node_Id :=
1912 Find_Aspect
1913 (E, Aspect_Preelaborable_Initialization);
1914 PI_Expr : Node_Id := Empty;
1915 begin
1916 if Present (PI_Aspect) then
1917 PI_Expr := Expression (PI_Aspect);
1918 end if;
1920 if not Has_Preelaborable_Initialization
1921 (E, Preelab_Init_Expr => PI_Expr)
1922 then
1923 Error_Msg_N
1924 ("full view of & does not have "
1925 & "preelaborable initialization", E);
1926 end if;
1927 end;
1928 end if;
1930 Next_Entity (E);
1931 end loop;
1933 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1934 -- declaration having a known_discriminant_part shall not be an
1935 -- unchecked union type.
1937 if Present (Vis_Decls) then
1938 Inspect_Unchecked_Union_Completion (Vis_Decls);
1939 end if;
1941 if Present (Priv_Decls) then
1942 Inspect_Unchecked_Union_Completion (Priv_Decls);
1943 end if;
1945 -- Implement AI12-0101 (which only removes a legality rule) and then
1946 -- AI05-0123 (which directly applies in the previously illegal case)
1947 -- in Ada 2012. Note that AI12-0101 is a binding interpretation.
1949 if Present (Priv_Decls) and then Ada_Version >= Ada_2012 then
1950 Inspect_Untagged_Record_Completion (Priv_Decls);
1951 end if;
1953 if Ekind (Id) = E_Generic_Package
1954 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1955 and then Present (Priv_Decls)
1956 then
1957 -- Save global references in private declarations, ignoring the
1958 -- visible declarations that were processed earlier.
1960 declare
1961 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1962 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1963 Save_Form : constant List_Id :=
1964 Generic_Formal_Declarations (Orig_Decl);
1966 begin
1967 -- Insert the freezing nodes after the private declarations to
1968 -- ensure that we analyze its aspects; needed to ensure that
1969 -- global entities referenced in the aspects are properly handled.
1971 if Ada_Version >= Ada_2012
1972 and then Is_Non_Empty_List (Priv_Decls)
1973 then
1974 Insert_List_After_And_Analyze
1975 (Last (Priv_Decls), Freeze_Entity (Id, Last (Priv_Decls)));
1976 end if;
1978 Set_Visible_Declarations (Orig_Spec, Empty_List);
1979 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1980 Save_Global_References (Orig_Decl);
1981 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1982 Set_Visible_Declarations (Orig_Spec, Save_Vis);
1983 end;
1984 end if;
1986 Process_End_Label (N, 'e', Id);
1988 -- Remove private_with_clauses of enclosing compilation unit, if they
1989 -- were installed.
1991 if Private_With_Clauses_Installed then
1992 Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
1993 end if;
1995 -- For the case of a library level package, we must go through all the
1996 -- entities clearing the indications that the value may be constant and
1997 -- not modified. Why? Because any client of this package may modify
1998 -- these values freely from anywhere. This also applies to any nested
1999 -- packages or generic packages.
2001 -- For now we unconditionally clear constants for packages that are
2002 -- instances of generic packages. The reason is that we do not have the
2003 -- body yet, and we otherwise think things are unreferenced when they
2004 -- are not. This should be fixed sometime (the effect is not terrible,
2005 -- we just lose some warnings, and also some cases of value propagation)
2006 -- ???
2008 if Is_Library_Level_Entity (Id)
2009 or else Is_Generic_Instance (Id)
2010 then
2011 Clear_Constants (Id);
2012 end if;
2014 -- Output relevant information as to why the package requires a body.
2015 -- Do not consider generated packages as this exposes internal symbols
2016 -- and leads to confusing messages.
2018 if List_Body_Required_Info
2019 and then In_Extended_Main_Source_Unit (Id)
2020 and then Unit_Requires_Body (Id)
2021 and then Comes_From_Source (Id)
2022 then
2023 Unit_Requires_Body_Info (Id);
2024 end if;
2026 -- Nested package specs that do not require bodies are not checked for
2027 -- ineffective use clauses due to the possibility of subunits. This is
2028 -- because at this stage it is impossible to tell whether there will be
2029 -- a separate body.
2031 if not Unit_Requires_Body (Id)
2032 and then Is_Compilation_Unit (Id)
2033 and then not Is_Private_Descendant (Id)
2034 then
2035 Update_Use_Clause_Chain;
2036 end if;
2037 end Analyze_Package_Specification;
2039 --------------------------------------
2040 -- Analyze_Private_Type_Declaration --
2041 --------------------------------------
2043 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
2044 Id : constant Entity_Id := Defining_Identifier (N);
2045 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
2047 begin
2048 Generate_Definition (Id);
2049 Set_Is_Pure (Id, PF);
2050 Reinit_Size_Align (Id);
2052 if not Is_Package_Or_Generic_Package (Current_Scope)
2053 or else In_Private_Part (Current_Scope)
2054 then
2055 Error_Msg_N ("invalid context for private declaration", N);
2056 end if;
2058 New_Private_Type (N, Id, N);
2059 Set_Depends_On_Private (Id);
2061 -- Set the SPARK mode from the current context
2063 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
2064 Set_SPARK_Pragma_Inherited (Id);
2066 if Has_Aspects (N) then
2067 Analyze_Aspect_Specifications (N, Id);
2068 end if;
2069 end Analyze_Private_Type_Declaration;
2071 ----------------------------------
2072 -- Check_Anonymous_Access_Types --
2073 ----------------------------------
2075 procedure Check_Anonymous_Access_Types
2076 (Spec_Id : Entity_Id;
2077 P_Body : Node_Id)
2079 E : Entity_Id;
2080 IR : Node_Id;
2082 begin
2083 -- Itype references are only needed by gigi, to force elaboration of
2084 -- itypes. In the absence of code generation, they are not needed.
2086 if not Expander_Active then
2087 return;
2088 end if;
2090 E := First_Entity (Spec_Id);
2091 while Present (E) loop
2092 if Ekind (E) = E_Anonymous_Access_Type
2093 and then From_Limited_With (E)
2094 then
2095 IR := Make_Itype_Reference (Sloc (P_Body));
2096 Set_Itype (IR, E);
2098 if No (Declarations (P_Body)) then
2099 Set_Declarations (P_Body, New_List (IR));
2100 else
2101 Prepend (IR, Declarations (P_Body));
2102 end if;
2103 end if;
2105 Next_Entity (E);
2106 end loop;
2107 end Check_Anonymous_Access_Types;
2109 -------------------------------------------
2110 -- Declare_Inherited_Private_Subprograms --
2111 -------------------------------------------
2113 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
2115 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
2116 -- Check whether an inherited subprogram S is an operation of an
2117 -- untagged derived type T.
2119 ---------------------
2120 -- Is_Primitive_Of --
2121 ---------------------
2123 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
2124 Formal : Entity_Id;
2126 begin
2127 -- If the full view is a scalar type, the type is the anonymous base
2128 -- type, but the operation mentions the first subtype, so check the
2129 -- signature against the base type.
2131 if Base_Type (Etype (S)) = Base_Type (T) then
2132 return True;
2134 else
2135 Formal := First_Formal (S);
2136 while Present (Formal) loop
2137 if Base_Type (Etype (Formal)) = Base_Type (T) then
2138 return True;
2139 end if;
2141 Next_Formal (Formal);
2142 end loop;
2144 return False;
2145 end if;
2146 end Is_Primitive_Of;
2148 -- Local variables
2150 E : Entity_Id;
2151 Op_List : Elist_Id;
2152 Op_Elmt : Elmt_Id;
2153 Op_Elmt_2 : Elmt_Id;
2154 Prim_Op : Entity_Id;
2155 New_Op : Entity_Id := Empty;
2156 Parent_Subp : Entity_Id;
2157 Tag : Entity_Id;
2159 -- Start of processing for Declare_Inherited_Private_Subprograms
2161 begin
2162 E := First_Entity (Id);
2163 while Present (E) loop
2165 -- If the entity is a nonprivate type extension whose parent type
2166 -- is declared in an open scope, then the type may have inherited
2167 -- operations that now need to be made visible. Ditto if the entity
2168 -- is a formal derived type in a child unit.
2170 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
2171 or else
2172 (Nkind (Parent (E)) = N_Private_Extension_Declaration
2173 and then Is_Generic_Type (E)))
2174 and then In_Open_Scopes (Scope (Etype (E)))
2175 and then Is_Base_Type (E)
2176 then
2177 if Is_Tagged_Type (E) then
2178 Op_List := Primitive_Operations (E);
2179 New_Op := Empty;
2180 Tag := First_Tag_Component (E);
2182 Op_Elmt := First_Elmt (Op_List);
2183 while Present (Op_Elmt) loop
2184 Prim_Op := Node (Op_Elmt);
2186 -- Search primitives that are implicit operations with an
2187 -- internal name whose parent operation has a normal name.
2189 if Present (Alias (Prim_Op))
2190 and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
2191 and then not Comes_From_Source (Prim_Op)
2192 and then Is_Internal_Name (Chars (Prim_Op))
2193 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
2194 then
2195 Parent_Subp := Alias (Prim_Op);
2197 -- Case 1: Check if the type has also an explicit
2198 -- overriding for this primitive.
2200 Op_Elmt_2 := Next_Elmt (Op_Elmt);
2201 while Present (Op_Elmt_2) loop
2203 -- Skip entities with attribute Interface_Alias since
2204 -- they are not overriding primitives (these entities
2205 -- link an interface primitive with their covering
2206 -- primitive)
2208 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
2209 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
2210 and then No (Interface_Alias (Node (Op_Elmt_2)))
2211 then
2212 -- The private inherited operation has been
2213 -- overridden by an explicit subprogram:
2214 -- replace the former by the latter.
2216 New_Op := Node (Op_Elmt_2);
2217 Replace_Elmt (Op_Elmt, New_Op);
2218 Remove_Elmt (Op_List, Op_Elmt_2);
2219 Set_Overridden_Operation (New_Op, Parent_Subp);
2220 Set_Is_Ada_2022_Only (New_Op,
2221 Is_Ada_2022_Only (Parent_Subp));
2223 -- We don't need to inherit its dispatching slot.
2224 -- Set_All_DT_Position has previously ensured that
2225 -- the same slot was assigned to the two primitives
2227 if Present (Tag)
2228 and then Present (DTC_Entity (New_Op))
2229 and then Present (DTC_Entity (Prim_Op))
2230 then
2231 pragma Assert
2232 (DT_Position (New_Op) = DT_Position (Prim_Op));
2233 null;
2234 end if;
2236 goto Next_Primitive;
2237 end if;
2239 Next_Elmt (Op_Elmt_2);
2240 end loop;
2242 -- Case 2: We have not found any explicit overriding and
2243 -- hence we need to declare the operation (i.e., make it
2244 -- visible).
2246 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2248 -- Inherit the dispatching slot if E is already frozen
2250 if Is_Frozen (E)
2251 and then Present (DTC_Entity (Alias (Prim_Op)))
2252 then
2253 Set_DTC_Entity_Value (E, New_Op);
2254 Set_DT_Position_Value (New_Op,
2255 DT_Position (Alias (Prim_Op)));
2256 end if;
2258 pragma Assert
2259 (Is_Dispatching_Operation (New_Op)
2260 and then Node (Last_Elmt (Op_List)) = New_Op);
2262 -- Substitute the new operation for the old one in the
2263 -- type's primitive operations list. Since the new
2264 -- operation was also just added to the end of list,
2265 -- the last element must be removed.
2267 -- (Question: is there a simpler way of declaring the
2268 -- operation, say by just replacing the name of the
2269 -- earlier operation, reentering it in the in the symbol
2270 -- table (how?), and marking it as private???)
2272 Replace_Elmt (Op_Elmt, New_Op);
2273 Remove_Last_Elmt (Op_List);
2274 end if;
2276 <<Next_Primitive>>
2277 Next_Elmt (Op_Elmt);
2278 end loop;
2280 -- Generate listing showing the contents of the dispatch table
2282 if Debug_Flag_ZZ then
2283 Write_DT (E);
2284 end if;
2286 else
2287 -- For untagged type, scan forward to locate inherited hidden
2288 -- operations.
2290 Prim_Op := Next_Entity (E);
2291 while Present (Prim_Op) loop
2292 if Is_Subprogram (Prim_Op)
2293 and then Present (Alias (Prim_Op))
2294 and then not Comes_From_Source (Prim_Op)
2295 and then Is_Internal_Name (Chars (Prim_Op))
2296 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
2297 and then Is_Primitive_Of (E, Prim_Op)
2298 then
2299 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2300 end if;
2302 Next_Entity (Prim_Op);
2304 -- Derived operations appear immediately after the type
2305 -- declaration (or the following subtype indication for
2306 -- a derived scalar type). Further declarations cannot
2307 -- include inherited operations of the type.
2309 exit when Present (Prim_Op)
2310 and then not Is_Overloadable (Prim_Op);
2311 end loop;
2312 end if;
2313 end if;
2315 Next_Entity (E);
2316 end loop;
2317 end Declare_Inherited_Private_Subprograms;
2319 -----------------------
2320 -- End_Package_Scope --
2321 -----------------------
2323 procedure End_Package_Scope (P : Entity_Id) is
2324 begin
2325 Uninstall_Declarations (P);
2326 Pop_Scope;
2327 end End_Package_Scope;
2329 ---------------------------
2330 -- Exchange_Declarations --
2331 ---------------------------
2333 procedure Exchange_Declarations (Id : Entity_Id) is
2334 Full_Id : constant Entity_Id := Full_View (Id);
2335 H1 : constant Entity_Id := Homonym (Id);
2336 Next1 : constant Entity_Id := Next_Entity (Id);
2337 H2 : Entity_Id;
2338 Next2 : Entity_Id;
2340 begin
2341 -- If missing full declaration for type, nothing to exchange
2343 if No (Full_Id) then
2344 return;
2345 end if;
2347 -- Otherwise complete the exchange, and preserve semantic links
2349 Next2 := Next_Entity (Full_Id);
2350 H2 := Homonym (Full_Id);
2352 -- Reset full declaration pointer to reflect the switched entities and
2353 -- readjust the next entity chains.
2355 Exchange_Entities (Id, Full_Id);
2357 Link_Entities (Id, Next1);
2358 Set_Homonym (Id, H1);
2360 Set_Full_View (Full_Id, Id);
2361 Link_Entities (Full_Id, Next2);
2362 Set_Homonym (Full_Id, H2);
2363 end Exchange_Declarations;
2365 ----------------------------
2366 -- Install_Package_Entity --
2367 ----------------------------
2369 procedure Install_Package_Entity (Id : Entity_Id) is
2370 begin
2371 if not Is_Internal (Id) then
2372 if Debug_Flag_E then
2373 Write_Str ("Install: ");
2374 Write_Name (Chars (Id));
2375 Write_Eol;
2376 end if;
2378 if Is_Child_Unit (Id) then
2379 null;
2381 -- Do not enter implicitly inherited non-overridden subprograms of
2382 -- a tagged type back into visibility if they have non-conformant
2383 -- homographs (Ada RM 8.3 12.3/2).
2385 elsif Is_Hidden_Non_Overridden_Subpgm (Id) then
2386 null;
2388 else
2389 Set_Is_Immediately_Visible (Id);
2390 end if;
2391 end if;
2392 end Install_Package_Entity;
2394 ----------------------------------
2395 -- Install_Private_Declarations --
2396 ----------------------------------
2398 procedure Install_Private_Declarations (P : Entity_Id) is
2399 Id : Entity_Id;
2400 Full : Entity_Id;
2401 Priv_Deps : Elist_Id;
2403 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
2404 -- When the full view of a private type is made available, we do the
2405 -- same for its private dependents under proper visibility conditions.
2406 -- When compiling a child unit this needs to be done recursively.
2408 -----------------------------
2409 -- Swap_Private_Dependents --
2410 -----------------------------
2412 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
2413 Cunit : Entity_Id;
2414 Deps : Elist_Id;
2415 Priv : Entity_Id;
2416 Priv_Elmt : Elmt_Id;
2417 Is_Priv : Boolean;
2419 begin
2420 Priv_Elmt := First_Elmt (Priv_Deps);
2421 while Present (Priv_Elmt) loop
2422 Priv := Node (Priv_Elmt);
2424 -- Before the exchange, verify that the presence of the Full_View
2425 -- field. This field will be empty if the entity has already been
2426 -- installed due to a previous call.
2428 if Present (Full_View (Priv)) and then Is_Visible_Dependent (Priv)
2429 then
2430 if Is_Private_Type (Priv) then
2431 Cunit := Cunit_Entity (Current_Sem_Unit);
2432 Deps := Private_Dependents (Priv);
2433 Is_Priv := True;
2434 else
2435 Is_Priv := False;
2436 end if;
2438 -- For each subtype that is swapped, we also swap the reference
2439 -- to it in Private_Dependents, to allow access to it when we
2440 -- swap them out in End_Package_Scope.
2442 Replace_Elmt (Priv_Elmt, Full_View (Priv));
2444 -- Ensure that both views of the dependent private subtype are
2445 -- immediately visible if within some open scope. Check full
2446 -- view before exchanging views.
2448 if In_Open_Scopes (Scope (Full_View (Priv))) then
2449 Set_Is_Immediately_Visible (Priv);
2450 end if;
2452 Exchange_Declarations (Priv);
2453 Set_Is_Immediately_Visible
2454 (Priv, In_Open_Scopes (Scope (Priv)));
2456 Set_Is_Potentially_Use_Visible
2457 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
2459 -- Recurse for child units, except in generic child units,
2460 -- which unfortunately handle private_dependents separately.
2461 -- Note that the current unit may not have been analyzed,
2462 -- for example a package body, so we cannot rely solely on
2463 -- the Is_Child_Unit flag, but that's only an optimization.
2465 if Is_Priv
2466 and then (No (Etype (Cunit)) or else Is_Child_Unit (Cunit))
2467 and then not Is_Empty_Elmt_List (Deps)
2468 and then not Inside_A_Generic
2469 then
2470 Swap_Private_Dependents (Deps);
2471 end if;
2472 end if;
2474 Next_Elmt (Priv_Elmt);
2475 end loop;
2476 end Swap_Private_Dependents;
2478 -- Start of processing for Install_Private_Declarations
2480 begin
2481 -- First exchange declarations for private types, so that the full
2482 -- declaration is visible. For each private type, we check its
2483 -- Private_Dependents list and also exchange any subtypes of or derived
2484 -- types from it. Finally, if this is a Taft amendment type, the
2485 -- incomplete declaration is irrelevant, and we want to link the
2486 -- eventual full declaration with the original private one so we
2487 -- also skip the exchange.
2489 Id := First_Entity (P);
2490 while Present (Id) and then Id /= First_Private_Entity (P) loop
2491 if Is_Private_Base_Type (Id)
2492 and then Present (Full_View (Id))
2493 and then Comes_From_Source (Full_View (Id))
2494 and then Scope (Full_View (Id)) = Scope (Id)
2495 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
2496 then
2497 -- If there is a use-type clause on the private type, set the full
2498 -- view accordingly.
2500 Set_In_Use (Full_View (Id), In_Use (Id));
2501 Full := Full_View (Id);
2503 if Is_Private_Base_Type (Full)
2504 and then Has_Private_Declaration (Full)
2505 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
2506 and then In_Open_Scopes (Scope (Etype (Full)))
2507 and then In_Package_Body (Current_Scope)
2508 and then not Is_Private_Type (Etype (Full))
2509 then
2510 -- This is the completion of a private type by a derivation
2511 -- from another private type which is not private anymore. This
2512 -- can only happen in a package nested within a child package,
2513 -- when the parent type is defined in the parent unit. At this
2514 -- point the current type is not private either, and we have
2515 -- to install the underlying full view, which is now visible.
2516 -- Save the current full view as well, so that all views can be
2517 -- restored on exit. It may seem that after compiling the child
2518 -- body there are not environments to restore, but the back-end
2519 -- expects those links to be valid, and freeze nodes depend on
2520 -- them.
2522 if No (Full_View (Full))
2523 and then Present (Underlying_Full_View (Full))
2524 then
2525 Set_Full_View (Id, Underlying_Full_View (Full));
2526 Set_Underlying_Full_View (Id, Full);
2527 Set_Is_Underlying_Full_View (Full);
2529 Set_Underlying_Full_View (Full, Empty);
2530 Set_Is_Frozen (Full_View (Id));
2531 end if;
2532 end if;
2534 Priv_Deps := Private_Dependents (Id);
2535 Exchange_Declarations (Id);
2536 Set_Is_Immediately_Visible (Id);
2537 Swap_Private_Dependents (Priv_Deps);
2538 end if;
2540 Next_Entity (Id);
2541 end loop;
2543 -- Next make other declarations in the private part visible as well
2545 Id := First_Private_Entity (P);
2546 while Present (Id) loop
2547 Install_Package_Entity (Id);
2548 Set_Is_Hidden (Id, False);
2549 Next_Entity (Id);
2550 end loop;
2552 -- An abstract state is partially refined when it has at least one
2553 -- Part_Of constituent. Since these constituents are being installed
2554 -- into visibility, update the partial refinement status of any state
2555 -- defined in the associated package, subject to at least one Part_Of
2556 -- constituent.
2558 if Is_Package_Or_Generic_Package (P) then
2559 declare
2560 States : constant Elist_Id := Abstract_States (P);
2561 State_Elmt : Elmt_Id;
2562 State_Id : Entity_Id;
2564 begin
2565 if Present (States) then
2566 State_Elmt := First_Elmt (States);
2567 while Present (State_Elmt) loop
2568 State_Id := Node (State_Elmt);
2570 if Present (Part_Of_Constituents (State_Id)) then
2571 Set_Has_Partial_Visible_Refinement (State_Id);
2572 end if;
2574 Next_Elmt (State_Elmt);
2575 end loop;
2576 end if;
2577 end;
2578 end if;
2580 -- Indicate that the private part is currently visible, so it can be
2581 -- properly reset on exit.
2583 Set_In_Private_Part (P);
2584 end Install_Private_Declarations;
2586 ----------------------------------
2587 -- Install_Visible_Declarations --
2588 ----------------------------------
2590 procedure Install_Visible_Declarations (P : Entity_Id) is
2591 Id : Entity_Id;
2592 Last_Entity : Entity_Id;
2594 begin
2595 pragma Assert
2596 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
2598 if Is_Package_Or_Generic_Package (P) then
2599 Last_Entity := First_Private_Entity (P);
2600 else
2601 Last_Entity := Empty;
2602 end if;
2604 Id := First_Entity (P);
2605 while Present (Id) and then Id /= Last_Entity loop
2606 Install_Package_Entity (Id);
2607 Next_Entity (Id);
2608 end loop;
2609 end Install_Visible_Declarations;
2611 --------------------------
2612 -- Is_Private_Base_Type --
2613 --------------------------
2615 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
2616 begin
2617 return Ekind (E) = E_Private_Type
2618 or else Ekind (E) = E_Limited_Private_Type
2619 or else Ekind (E) = E_Record_Type_With_Private;
2620 end Is_Private_Base_Type;
2622 --------------------------
2623 -- Is_Visible_Dependent --
2624 --------------------------
2626 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
2628 S : constant Entity_Id := Scope (Dep);
2630 begin
2631 -- Renamings created for actual types have the visibility of the actual
2633 if Ekind (S) = E_Package
2634 and then Is_Generic_Instance (S)
2635 and then (Is_Generic_Actual_Type (Dep)
2636 or else Is_Generic_Actual_Type (Full_View (Dep)))
2637 then
2638 return True;
2640 elsif not (Is_Derived_Type (Dep))
2641 and then Is_Derived_Type (Full_View (Dep))
2642 then
2643 -- When instantiating a package body, the scope stack is empty, so
2644 -- check instead whether the dependent type is defined in the same
2645 -- scope as the instance itself.
2647 return In_Open_Scopes (S)
2648 or else (Is_Generic_Instance (Current_Scope)
2649 and then Scope (Dep) = Scope (Current_Scope));
2650 else
2651 return True;
2652 end if;
2653 end Is_Visible_Dependent;
2655 ----------------------------
2656 -- May_Need_Implicit_Body --
2657 ----------------------------
2659 procedure May_Need_Implicit_Body (E : Entity_Id) is
2660 P : constant Node_Id := Unit_Declaration_Node (E);
2661 S : constant Node_Id := Parent (P);
2662 B : Node_Id;
2663 Decls : List_Id;
2665 begin
2666 if not Has_Completion (E)
2667 and then Nkind (P) = N_Package_Declaration
2668 and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
2669 then
2670 B :=
2671 Make_Package_Body (Sloc (E),
2672 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
2673 Chars => Chars (E)),
2674 Declarations => New_List);
2676 if Nkind (S) = N_Package_Specification then
2677 if Present (Private_Declarations (S)) then
2678 Decls := Private_Declarations (S);
2679 else
2680 Decls := Visible_Declarations (S);
2681 end if;
2682 else
2683 Decls := Declarations (S);
2684 end if;
2686 Append (B, Decls);
2687 Analyze (B);
2688 end if;
2689 end May_Need_Implicit_Body;
2691 ----------------------
2692 -- New_Private_Type --
2693 ----------------------
2695 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
2696 begin
2697 -- For other than Ada 2012, enter the name in the current scope
2699 if Ada_Version < Ada_2012 then
2700 Enter_Name (Id);
2702 -- Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2703 -- there may be an incomplete previous view.
2705 else
2706 declare
2707 Prev : Entity_Id;
2708 begin
2709 Prev := Find_Type_Name (N);
2710 pragma Assert (Prev = Id
2711 or else (Ekind (Prev) = E_Incomplete_Type
2712 and then Present (Full_View (Prev))
2713 and then Full_View (Prev) = Id));
2714 end;
2715 end if;
2717 if Limited_Present (Def) then
2718 Mutate_Ekind (Id, E_Limited_Private_Type);
2719 else
2720 Mutate_Ekind (Id, E_Private_Type);
2721 end if;
2723 Set_Etype (Id, Id);
2724 Set_Has_Delayed_Freeze (Id);
2725 Set_Is_First_Subtype (Id);
2726 Reinit_Size_Align (Id);
2728 Set_Is_Constrained (Id,
2729 No (Discriminant_Specifications (N))
2730 and then not Unknown_Discriminants_Present (N));
2732 -- Set tagged flag before processing discriminants, to catch illegal
2733 -- usage.
2735 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
2737 Set_Discriminant_Constraint (Id, No_Elist);
2738 Set_Stored_Constraint (Id, No_Elist);
2740 if Present (Discriminant_Specifications (N)) then
2741 Push_Scope (Id);
2742 Process_Discriminants (N);
2743 End_Scope;
2745 elsif Unknown_Discriminants_Present (N) then
2746 Set_Has_Unknown_Discriminants (Id);
2747 end if;
2749 Set_Private_Dependents (Id, New_Elmt_List);
2751 if Tagged_Present (Def) then
2752 Mutate_Ekind (Id, E_Record_Type_With_Private);
2753 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2754 Set_Is_Abstract_Type (Id, Abstract_Present (Def));
2755 Set_Is_Limited_Record (Id, Limited_Present (Def));
2756 Set_Has_Delayed_Freeze (Id, True);
2758 -- Recognize Ada.Real_Time.Timing_Events.Timing_Events here
2760 if Is_RTE (Id, RE_Timing_Event) then
2761 Set_Has_Timing_Event (Id);
2762 end if;
2764 -- Create a class-wide type with the same attributes
2766 Make_Class_Wide_Type (Id);
2768 elsif Abstract_Present (Def) then
2769 Error_Msg_N ("only a tagged type can be abstract", N);
2771 -- We initialize the primitive operations list of an untagged private
2772 -- type to an empty element list. Do this even when Extensions_Allowed
2773 -- is False to issue better error messages. (Note: This could be done
2774 -- for all private types and shared with the tagged case above, but
2775 -- for now we do it separately.)
2777 else
2778 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2779 end if;
2780 end New_Private_Type;
2782 ---------------------------------
2783 -- Requires_Completion_In_Body --
2784 ---------------------------------
2786 function Requires_Completion_In_Body
2787 (Id : Entity_Id;
2788 Pack_Id : Entity_Id;
2789 Do_Abstract_States : Boolean := False) return Boolean
2791 begin
2792 -- Always ignore child units. Child units get added to the entity list
2793 -- of a parent unit, but are not original entities of the parent, and
2794 -- so do not affect whether the parent needs a body.
2796 if Is_Child_Unit (Id) then
2797 return False;
2799 -- Ignore formal packages and their renamings
2801 elsif Ekind (Id) = E_Package
2802 and then Nkind (Original_Node (Unit_Declaration_Node (Id))) =
2803 N_Formal_Package_Declaration
2804 then
2805 return False;
2807 -- Otherwise test to see if entity requires a completion. Note that
2808 -- subprogram entities whose declaration does not come from source are
2809 -- ignored here on the basis that we assume the expander will provide an
2810 -- implicit completion at some point.
2812 elsif (Is_Overloadable (Id)
2813 and then Ekind (Id) not in E_Enumeration_Literal | E_Operator
2814 and then not Is_Abstract_Subprogram (Id)
2815 and then not Has_Completion (Id)
2816 and then Comes_From_Source (Parent (Id)))
2818 or else
2819 (Ekind (Id) = E_Package
2820 and then Id /= Pack_Id
2821 and then not Has_Completion (Id)
2822 and then Unit_Requires_Body (Id, Do_Abstract_States))
2824 or else
2825 (Ekind (Id) = E_Incomplete_Type
2826 and then No (Full_View (Id))
2827 and then not Is_Generic_Type (Id))
2829 or else
2830 (Ekind (Id) in E_Task_Type | E_Protected_Type
2831 and then not Has_Completion (Id))
2833 or else
2834 (Ekind (Id) = E_Generic_Package
2835 and then Id /= Pack_Id
2836 and then not Has_Completion (Id)
2837 and then Unit_Requires_Body (Id, Do_Abstract_States))
2839 or else
2840 (Is_Generic_Subprogram (Id)
2841 and then not Has_Completion (Id))
2842 then
2843 return True;
2845 -- Otherwise the entity does not require completion in a package body
2847 else
2848 return False;
2849 end if;
2850 end Requires_Completion_In_Body;
2852 ----------------------------
2853 -- Uninstall_Declarations --
2854 ----------------------------
2856 procedure Uninstall_Declarations (P : Entity_Id) is
2857 Decl : constant Node_Id := Unit_Declaration_Node (P);
2858 Id : Entity_Id;
2859 Full : Entity_Id;
2861 procedure Preserve_Full_Attributes (Priv : Entity_Id; Full : Entity_Id);
2862 -- Copy to the private declaration the attributes of the full view that
2863 -- need to be available for the partial view also.
2865 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
2866 -- When the full view of a private type is made unavailable, we do the
2867 -- same for its private dependents under proper visibility conditions.
2868 -- When compiling a child unit this needs to be done recursively.
2870 function Type_In_Use (T : Entity_Id) return Boolean;
2871 -- Check whether type or base type appear in an active use_type clause
2873 ------------------------------
2874 -- Preserve_Full_Attributes --
2875 ------------------------------
2877 procedure Preserve_Full_Attributes
2878 (Priv : Entity_Id;
2879 Full : Entity_Id)
2881 Full_Base : constant Entity_Id := Base_Type (Full);
2882 Priv_Is_Base_Type : constant Boolean := Is_Base_Type (Priv);
2884 begin
2885 Set_Size_Info (Priv, Full);
2886 Copy_RM_Size (To => Priv, From => Full);
2887 Set_Size_Known_At_Compile_Time
2888 (Priv, Size_Known_At_Compile_Time (Full));
2889 Set_Is_Volatile (Priv, Is_Volatile (Full));
2890 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
2891 Set_Is_Atomic (Priv, Is_Atomic (Full));
2892 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full));
2893 Set_Is_Ada_2012_Only (Priv, Is_Ada_2012_Only (Full));
2894 Set_Is_Ada_2022_Only (Priv, Is_Ada_2022_Only (Full));
2895 Set_Has_Pragma_Unmodified (Priv, Has_Pragma_Unmodified (Full));
2896 Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced (Full));
2897 Set_Has_Pragma_Unreferenced_Objects
2898 (Priv, Has_Pragma_Unreferenced_Objects
2899 (Full));
2900 Set_Predicates_Ignored (Priv, Predicates_Ignored (Full));
2901 if Is_Unchecked_Union (Full) then
2902 Set_Is_Unchecked_Union (Base_Type (Priv));
2903 end if;
2905 if Referenced (Full) then
2906 Set_Referenced (Priv);
2907 end if;
2909 if Priv_Is_Base_Type then
2910 Set_Is_Controlled_Active
2911 (Priv, Is_Controlled_Active (Full_Base));
2912 Set_Finalize_Storage_Only
2913 (Priv, Finalize_Storage_Only (Full_Base));
2914 Set_Has_Controlled_Component
2915 (Priv, Has_Controlled_Component (Full_Base));
2917 Propagate_Concurrent_Flags (Priv, Base_Type (Full));
2918 end if;
2920 -- As explained in Freeze_Entity, private types are required to point
2921 -- to the same freeze node as their corresponding full view, if any.
2922 -- But we ought not to overwrite a node already inserted in the tree.
2924 pragma Assert
2925 (Serious_Errors_Detected /= 0
2926 or else No (Freeze_Node (Priv))
2927 or else No (Parent (Freeze_Node (Priv)))
2928 or else Freeze_Node (Priv) = Freeze_Node (Full));
2930 Set_Freeze_Node (Priv, Freeze_Node (Full));
2932 -- Propagate Default_Initial_Condition-related attributes from the
2933 -- full view to the private view.
2935 Propagate_DIC_Attributes (Priv, From_Typ => Full);
2937 -- Propagate invariant-related attributes from the full view to the
2938 -- private view.
2940 Propagate_Invariant_Attributes (Priv, From_Typ => Full);
2942 -- Propagate predicate-related attributes from the full view to the
2943 -- private view.
2945 Propagate_Predicate_Attributes (Priv, From_Typ => Full);
2947 if Is_Tagged_Type (Priv)
2948 and then Is_Tagged_Type (Full)
2949 and then not Error_Posted (Full)
2950 then
2951 if Is_Tagged_Type (Priv) then
2953 -- If the type is tagged, the tag itself must be available on
2954 -- the partial view, for expansion purposes.
2956 Set_First_Entity (Priv, First_Entity (Full));
2958 -- If there are discriminants in the partial view, these remain
2959 -- visible. Otherwise only the tag itself is visible, and there
2960 -- are no nameable components in the partial view.
2962 if No (Last_Entity (Priv)) then
2963 Set_Last_Entity (Priv, First_Entity (Priv));
2964 end if;
2965 end if;
2967 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2969 if Has_Discriminants (Full) then
2970 Set_Discriminant_Constraint (Priv,
2971 Discriminant_Constraint (Full));
2972 end if;
2973 end if;
2974 end Preserve_Full_Attributes;
2976 -----------------------------
2977 -- Swap_Private_Dependents --
2978 -----------------------------
2980 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
2981 Cunit : Entity_Id;
2982 Deps : Elist_Id;
2983 Priv : Entity_Id;
2984 Priv_Elmt : Elmt_Id;
2985 Is_Priv : Boolean;
2987 begin
2988 Priv_Elmt := First_Elmt (Priv_Deps);
2989 while Present (Priv_Elmt) loop
2990 Priv := Node (Priv_Elmt);
2992 -- Before we do the swap, we verify the presence of the Full_View
2993 -- field, which may be empty due to a swap by a previous call to
2994 -- End_Package_Scope (e.g. from the freezing mechanism).
2996 if Present (Full_View (Priv)) then
2997 if Is_Private_Type (Priv) then
2998 Cunit := Cunit_Entity (Current_Sem_Unit);
2999 Deps := Private_Dependents (Priv);
3000 Is_Priv := True;
3001 else
3002 Is_Priv := False;
3003 end if;
3005 if Scope (Priv) = P
3006 or else not In_Open_Scopes (Scope (Priv))
3007 then
3008 Set_Is_Immediately_Visible (Priv, False);
3009 end if;
3011 if Is_Visible_Dependent (Priv) then
3012 Preserve_Full_Attributes (Priv, Full_View (Priv));
3013 Replace_Elmt (Priv_Elmt, Full_View (Priv));
3014 Exchange_Declarations (Priv);
3016 -- Recurse for child units, except in generic child units,
3017 -- which unfortunately handle private_dependents separately.
3018 -- Note that the current unit may not have been analyzed,
3019 -- for example a package body, so we cannot rely solely on
3020 -- the Is_Child_Unit flag, but that's only an optimization.
3022 if Is_Priv
3023 and then (No (Etype (Cunit)) or else Is_Child_Unit (Cunit))
3024 and then not Is_Empty_Elmt_List (Deps)
3025 and then not Inside_A_Generic
3026 then
3027 Swap_Private_Dependents (Deps);
3028 end if;
3029 end if;
3030 end if;
3032 Next_Elmt (Priv_Elmt);
3033 end loop;
3034 end Swap_Private_Dependents;
3036 -----------------
3037 -- Type_In_Use --
3038 -----------------
3040 function Type_In_Use (T : Entity_Id) return Boolean is
3041 begin
3042 return Scope (Base_Type (T)) = P
3043 and then (In_Use (T) or else In_Use (Base_Type (T)));
3044 end Type_In_Use;
3046 -- Start of processing for Uninstall_Declarations
3048 begin
3049 Id := First_Entity (P);
3050 while Present (Id) and then Id /= First_Private_Entity (P) loop
3051 if Debug_Flag_E then
3052 Write_Str ("unlinking visible entity ");
3053 Write_Int (Int (Id));
3054 Write_Eol;
3055 end if;
3057 -- On exit from the package scope, we must preserve the visibility
3058 -- established by use clauses in the current scope. Two cases:
3060 -- a) If the entity is an operator, it may be a primitive operator of
3061 -- a type for which there is a visible use-type clause.
3063 -- b) For other entities, their use-visibility is determined by a
3064 -- visible use clause for the package itself or a use-all-type clause
3065 -- applied directly to the entity's type. For a generic instance,
3066 -- the instantiation of the formals appears in the visible part,
3067 -- but the formals are private and remain so.
3069 if Ekind (Id) = E_Function
3070 and then Is_Operator_Symbol_Name (Chars (Id))
3071 and then not Is_Hidden (Id)
3072 and then not Error_Posted (Id)
3073 then
3074 Set_Is_Potentially_Use_Visible (Id,
3075 In_Use (P)
3076 or else Type_In_Use (Etype (Id))
3077 or else Type_In_Use (Etype (First_Formal (Id)))
3078 or else (Present (Next_Formal (First_Formal (Id)))
3079 and then
3080 Type_In_Use
3081 (Etype (Next_Formal (First_Formal (Id))))));
3082 else
3083 if In_Use (P) and then not Is_Hidden (Id) then
3085 -- A child unit of a use-visible package remains use-visible
3086 -- only if it is itself a visible child unit. Otherwise it
3087 -- would remain visible in other contexts where P is use-
3088 -- visible, because once compiled it stays in the entity list
3089 -- of its parent unit.
3091 if Is_Child_Unit (Id) then
3092 Set_Is_Potentially_Use_Visible
3093 (Id, Is_Visible_Lib_Unit (Id));
3094 else
3095 Set_Is_Potentially_Use_Visible (Id);
3096 end if;
3098 -- Avoid crash caused by previous errors
3100 elsif No (Etype (Id)) and then Serious_Errors_Detected /= 0 then
3101 null;
3103 -- We need to avoid incorrectly marking enumeration literals as
3104 -- non-visible when a visible use-all-type clause is in effect.
3106 elsif Type_In_Use (Etype (Id))
3107 and then Nkind (Current_Use_Clause (Etype (Id))) =
3108 N_Use_Type_Clause
3109 and then All_Present (Current_Use_Clause (Etype (Id)))
3110 then
3111 null;
3113 else
3114 Set_Is_Potentially_Use_Visible (Id, False);
3115 end if;
3116 end if;
3118 -- Local entities are not immediately visible outside of the package
3120 Set_Is_Immediately_Visible (Id, False);
3122 -- If this is a private type with a full view (for example a local
3123 -- subtype of a private type declared elsewhere), ensure that the
3124 -- full view is also removed from visibility: it may be exposed when
3125 -- swapping views in an instantiation. Similarly, ensure that the
3126 -- use-visibility is properly set on both views.
3128 if Is_Type (Id) and then Present (Full_View (Id)) then
3129 Set_Is_Immediately_Visible (Full_View (Id), False);
3130 Set_Is_Potentially_Use_Visible (Full_View (Id),
3131 Is_Potentially_Use_Visible (Id));
3132 end if;
3134 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
3135 Check_Abstract_Overriding (Id);
3136 Check_Conventions (Id);
3137 end if;
3139 if Ekind (Id) in E_Private_Type | E_Limited_Private_Type
3140 and then No (Full_View (Id))
3141 and then not Is_Generic_Type (Id)
3142 and then not Is_Derived_Type (Id)
3143 then
3144 Error_Msg_N ("missing full declaration for private type&", Id);
3146 elsif Ekind (Id) = E_Record_Type_With_Private
3147 and then not Is_Generic_Type (Id)
3148 and then No (Full_View (Id))
3149 then
3150 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
3151 Error_Msg_N ("missing full declaration for private type&", Id);
3152 else
3153 Error_Msg_N
3154 ("missing full declaration for private extension", Id);
3155 end if;
3157 -- Case of constant, check for deferred constant declaration with
3158 -- no full view. Likely just a matter of a missing expression, or
3159 -- accidental use of the keyword constant.
3161 elsif Ekind (Id) = E_Constant
3163 -- OK if constant value present
3165 and then No (Constant_Value (Id))
3167 -- OK if full view present
3169 and then No (Full_View (Id))
3171 -- OK if imported, since that provides the completion
3173 and then not Is_Imported (Id)
3175 -- OK if object declaration replaced by renaming declaration as
3176 -- a result of OK_To_Rename processing (e.g. for concatenation)
3178 and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration
3180 -- OK if object declaration with the No_Initialization flag set
3182 and then not (Nkind (Parent (Id)) = N_Object_Declaration
3183 and then No_Initialization (Parent (Id)))
3184 then
3185 -- If no private declaration is present, we assume the user did
3186 -- not intend a deferred constant declaration and the problem
3187 -- is simply that the initializing expression is missing.
3189 if not Has_Private_Declaration (Etype (Id)) then
3191 -- We assume that the user did not intend a deferred constant
3192 -- declaration, and the expression is just missing.
3194 Error_Msg_N
3195 ("constant declaration requires initialization expression",
3196 Parent (Id));
3198 if Is_Limited_Type (Etype (Id)) then
3199 Error_Msg_N
3200 ("\if variable intended, remove CONSTANT from declaration",
3201 Parent (Id));
3202 end if;
3204 -- Otherwise if a private declaration is present, then we are
3205 -- missing the full declaration for the deferred constant.
3207 else
3208 Error_Msg_N
3209 ("missing full declaration for deferred constant (RM 7.4)",
3210 Id);
3212 if Is_Limited_Type (Etype (Id)) then
3213 Error_Msg_N
3214 ("\if variable intended, remove CONSTANT from declaration",
3215 Parent (Id));
3216 end if;
3217 end if;
3218 end if;
3220 Next_Entity (Id);
3221 end loop;
3223 -- If the specification was installed as the parent of a public child
3224 -- unit, the private declarations were not installed, and there is
3225 -- nothing to do.
3227 if not In_Private_Part (P) then
3228 return;
3229 end if;
3231 -- Reset the flag now
3233 Set_In_Private_Part (P, False);
3235 -- Make private entities invisible and exchange full and private
3236 -- declarations for private types. Id is now the first private entity
3237 -- in the package.
3239 while Present (Id) loop
3240 if Debug_Flag_E then
3241 Write_Str ("unlinking private entity ");
3242 Write_Int (Int (Id));
3243 Write_Eol;
3244 end if;
3246 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
3247 Check_Abstract_Overriding (Id);
3248 Check_Conventions (Id);
3249 end if;
3251 Set_Is_Immediately_Visible (Id, False);
3253 if Is_Private_Base_Type (Id) and then Present (Full_View (Id)) then
3254 Full := Full_View (Id);
3256 -- If the partial view is not declared in the visible part of the
3257 -- package (as is the case when it is a type derived from some
3258 -- other private type in the private part of the current package),
3259 -- no exchange takes place.
3261 if No (Parent (Id))
3262 or else List_Containing (Parent (Id)) /=
3263 Visible_Declarations (Specification (Decl))
3264 then
3265 goto Next_Id;
3266 end if;
3268 -- The entry in the private part points to the full declaration,
3269 -- which is currently visible. Exchange them so only the private
3270 -- type declaration remains accessible, and link private and full
3271 -- declaration in the opposite direction. Before the actual
3272 -- exchange, we copy back attributes of the full view that must
3273 -- be available to the partial view too.
3275 Preserve_Full_Attributes (Id, Full);
3277 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
3279 -- The following test may be redundant, as this is already
3280 -- diagnosed in sem_ch3. ???
3282 if not Is_Definite_Subtype (Full)
3283 and then Is_Definite_Subtype (Id)
3284 then
3285 Error_Msg_Sloc := Sloc (Parent (Id));
3286 Error_Msg_NE
3287 ("full view of& not compatible with declaration#", Full, Id);
3288 end if;
3290 -- Swap out the subtypes and derived types of Id that
3291 -- were compiled in this scope, or installed previously
3292 -- by Install_Private_Declarations.
3294 Swap_Private_Dependents (Private_Dependents (Id));
3296 -- Now restore the type itself to its private view
3298 Exchange_Declarations (Id);
3300 -- If we have installed an underlying full view for a type derived
3301 -- from a private type in a child unit, restore the proper views
3302 -- of private and full view. See corresponding code in
3303 -- Install_Private_Declarations.
3305 -- After the exchange, Full denotes the private type in the
3306 -- visible part of the package.
3308 if Is_Private_Base_Type (Full)
3309 and then Present (Full_View (Full))
3310 and then Present (Underlying_Full_View (Full))
3311 and then In_Package_Body (Current_Scope)
3312 then
3313 Set_Full_View (Full, Underlying_Full_View (Full));
3314 Set_Underlying_Full_View (Full, Empty);
3315 end if;
3317 elsif Ekind (Id) = E_Incomplete_Type
3318 and then Comes_From_Source (Id)
3319 and then No (Full_View (Id))
3320 then
3321 -- Mark Taft amendment types. Verify that there are no primitive
3322 -- operations declared for the type (3.10.1(9)).
3324 Set_Has_Completion_In_Body (Id);
3326 declare
3327 Elmt : Elmt_Id;
3328 Subp : Entity_Id;
3330 begin
3331 Elmt := First_Elmt (Private_Dependents (Id));
3332 while Present (Elmt) loop
3333 Subp := Node (Elmt);
3335 -- Is_Primitive is tested because there can be cases where
3336 -- nonprimitive subprograms (in nested packages) are added
3337 -- to the Private_Dependents list.
3339 if Is_Overloadable (Subp) and then Is_Primitive (Subp) then
3340 Error_Msg_NE
3341 ("type& must be completed in the private part",
3342 Parent (Subp), Id);
3344 -- The result type of an access-to-function type cannot be a
3345 -- Taft-amendment type, unless the version is Ada 2012 or
3346 -- later (see AI05-151).
3348 elsif Ada_Version < Ada_2012
3349 and then Ekind (Subp) = E_Subprogram_Type
3350 then
3351 if Etype (Subp) = Id
3352 or else
3353 (Is_Class_Wide_Type (Etype (Subp))
3354 and then Etype (Etype (Subp)) = Id)
3355 then
3356 Error_Msg_NE
3357 ("type& must be completed in the private part",
3358 Associated_Node_For_Itype (Subp), Id);
3359 end if;
3360 end if;
3362 Next_Elmt (Elmt);
3363 end loop;
3364 end;
3366 -- For subtypes of private types the frontend generates two entities:
3367 -- one associated with the partial view and the other associated with
3368 -- the full view. When the subtype declaration is public the frontend
3369 -- places the former entity in the list of public entities of the
3370 -- package and the latter entity in the private part of the package.
3371 -- When the subtype declaration is private it generates these two
3372 -- entities but both are placed in the private part of the package
3373 -- (and the full view has the same source location as the partial
3374 -- view and no parent; see Prepare_Private_Subtype_Completion).
3376 elsif Ekind (Id) in E_Private_Subtype
3377 | E_Limited_Private_Subtype
3378 and then Present (Full_View (Id))
3379 and then Sloc (Id) = Sloc (Full_View (Id))
3380 and then No (Parent (Full_View (Id)))
3381 then
3382 Set_Is_Hidden (Id);
3383 Set_Is_Potentially_Use_Visible (Id, False);
3385 elsif not Is_Child_Unit (Id)
3386 and then (not Is_Private_Type (Id) or else No (Full_View (Id)))
3387 then
3388 Set_Is_Hidden (Id);
3389 Set_Is_Potentially_Use_Visible (Id, False);
3390 end if;
3392 <<Next_Id>>
3393 Next_Entity (Id);
3394 end loop;
3395 end Uninstall_Declarations;
3397 ------------------------
3398 -- Unit_Requires_Body --
3399 ------------------------
3401 function Unit_Requires_Body
3402 (Pack_Id : Entity_Id;
3403 Do_Abstract_States : Boolean := False) return Boolean
3405 E : Entity_Id;
3407 Requires_Body : Boolean := False;
3408 -- Flag set when the unit has at least one construct that requires
3409 -- completion in a body.
3411 begin
3412 -- Imported entity never requires body. Right now, only subprograms can
3413 -- be imported, but perhaps in the future we will allow import of
3414 -- packages.
3416 if Is_Imported (Pack_Id) then
3417 return False;
3419 -- Body required if library package with pragma Elaborate_Body
3421 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3422 return True;
3424 -- Body required if subprogram
3426 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3427 return True;
3429 -- Treat a block as requiring a body
3431 elsif Ekind (Pack_Id) = E_Block then
3432 return True;
3434 elsif Ekind (Pack_Id) = E_Package
3435 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3436 and then Present (Generic_Parent (Parent (Pack_Id)))
3437 then
3438 declare
3439 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3440 begin
3441 if Has_Pragma_Elaborate_Body (G_P) then
3442 return True;
3443 end if;
3444 end;
3445 end if;
3447 -- Traverse the entity chain of the package and look for constructs that
3448 -- require a completion in a body.
3450 E := First_Entity (Pack_Id);
3451 while Present (E) loop
3453 -- Skip abstract states because their completion depends on several
3454 -- criteria (see below).
3456 if Ekind (E) = E_Abstract_State then
3457 null;
3459 elsif Requires_Completion_In_Body
3460 (E, Pack_Id, Do_Abstract_States)
3461 then
3462 Requires_Body := True;
3463 exit;
3464 end if;
3466 Next_Entity (E);
3467 end loop;
3469 -- A [generic] package that defines at least one non-null abstract state
3470 -- requires a completion only when at least one other construct requires
3471 -- a completion in a body (SPARK RM 7.1.4(4) and (5)). This check is not
3472 -- performed if the caller requests this behavior.
3474 if Do_Abstract_States
3475 and then Is_Package_Or_Generic_Package (Pack_Id)
3476 and then Has_Non_Null_Abstract_State (Pack_Id)
3477 and then Requires_Body
3478 then
3479 return True;
3480 end if;
3482 return Requires_Body;
3483 end Unit_Requires_Body;
3485 -----------------------------
3486 -- Unit_Requires_Body_Info --
3487 -----------------------------
3489 procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id) is
3490 E : Entity_Id;
3492 begin
3493 -- An imported entity never requires body. Right now, only subprograms
3494 -- can be imported, but perhaps in the future we will allow import of
3495 -- packages.
3497 if Is_Imported (Pack_Id) then
3498 return;
3500 -- Body required if library package with pragma Elaborate_Body
3502 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3503 Error_Msg_N ("info: & requires body (Elaborate_Body)?.y?", Pack_Id);
3505 -- Body required if subprogram
3507 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3508 Error_Msg_N ("info: & requires body (subprogram case)?.y?", Pack_Id);
3510 -- Body required if generic parent has Elaborate_Body
3512 elsif Ekind (Pack_Id) = E_Package
3513 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3514 and then Present (Generic_Parent (Parent (Pack_Id)))
3515 then
3516 declare
3517 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3518 begin
3519 if Has_Pragma_Elaborate_Body (G_P) then
3520 Error_Msg_N
3521 ("info: & requires body (generic parent Elaborate_Body)?.y?",
3522 Pack_Id);
3523 end if;
3524 end;
3526 -- A [generic] package that introduces at least one non-null abstract
3527 -- state requires completion. However, there is a separate rule that
3528 -- requires that such a package have a reason other than this for a
3529 -- body being required (if necessary a pragma Elaborate_Body must be
3530 -- provided). If Ignore_Abstract_State is True, we don't do this check
3531 -- (so we can use Unit_Requires_Body to check for some other reason).
3533 elsif Is_Package_Or_Generic_Package (Pack_Id)
3534 and then Present (Abstract_States (Pack_Id))
3535 and then not Is_Null_State
3536 (Node (First_Elmt (Abstract_States (Pack_Id))))
3537 then
3538 Error_Msg_N
3539 ("info: & requires body (non-null abstract state aspect)?.y?",
3540 Pack_Id);
3541 end if;
3543 -- Otherwise search entity chain for entity requiring completion
3545 E := First_Entity (Pack_Id);
3546 while Present (E) loop
3547 if Requires_Completion_In_Body (E, Pack_Id) then
3548 Error_Msg_Node_2 := E;
3549 Error_Msg_NE
3550 ("info: & requires body (& requires completion)?.y?", E,
3551 Pack_Id);
3552 end if;
3554 Next_Entity (E);
3555 end loop;
3556 end Unit_Requires_Body_Info;
3558 end Sem_Ch7;