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