Debian Codegraph @

gcc/ada/sem_ch7.adb

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