[Ada] Improve messages on incorrect state refinement in SPARK
[official-gcc.git] / gcc / ada / sem_ch7.adb
blob6e799490c10b5e2d3daaff39c17e66e1ddb78c94
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 ------------------------------------------------------------------------------
26 -- This package contains the routines to process package specifications and
27 -- bodies. The most important semantic aspects of package processing are the
28 -- handling of private and full declarations, and the construction of dispatch
29 -- tables for tagged types.
31 with Aspects; use Aspects;
32 with Atree; use Atree;
33 with Contracts; use Contracts;
34 with Debug; use Debug;
35 with Einfo; use Einfo;
36 with Einfo.Entities; use Einfo.Entities;
37 with Einfo.Utils; use Einfo.Utils;
38 with Elists; use Elists;
39 with Errout; use Errout;
40 with Exp_Disp; use Exp_Disp;
41 with Exp_Dist; use Exp_Dist;
42 with Exp_Dbug; use Exp_Dbug;
43 with Freeze; use Freeze;
44 with Ghost; use Ghost;
45 with Lib; use Lib;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
49 with Nlists; use Nlists;
50 with Opt; use Opt;
51 with Output; use Output;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch3; use Sem_Ch3;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch10; use Sem_Ch10;
60 with Sem_Ch12; use Sem_Ch12;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Disp; use Sem_Disp;
63 with Sem_Eval; use Sem_Eval;
64 with Sem_Prag; use Sem_Prag;
65 with Sem_Util; use Sem_Util;
66 with Sem_Warn; use Sem_Warn;
67 with Snames; use Snames;
68 with Stand; use Stand;
69 with Sinfo; use Sinfo;
70 with Sinfo.Nodes; use Sinfo.Nodes;
71 with Sinfo.Utils; use Sinfo.Utils;
72 with Sinput; use Sinput;
73 with Style;
74 with Uintp; use Uintp;
76 with GNAT.HTable;
78 package body Sem_Ch7 is
80 -----------------------------------
81 -- Handling private declarations --
82 -----------------------------------
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.
102 -----------------------
103 -- Local Subprograms --
104 -----------------------
106 procedure Analyze_Package_Body_Helper (N : Node_Id);
107 -- Does all the real work of Analyze_Package_Body
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.
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.
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.
140 function Is_Private_Base_Type (E : Entity_Id) return Boolean;
141 -- True for a private type that is not a subtype
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???
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.
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.
163 --------------------------
164 -- Analyze_Package_Body --
165 --------------------------
167 procedure Analyze_Package_Body (N : Node_Id) is
168 Loc : constant Source_Ptr := Sloc (N);
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;
180 -- The real work is split out into the helper, so it can do "return;"
181 -- without skipping the debug output.
183 Analyze_Package_Body_Helper (N);
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;
195 ------------------------------------------------------
196 -- Analyze_Package_Body_Helper Data and Subprograms --
197 ------------------------------------------------------
199 Entity_Table_Size : constant := 4093;
200 -- Number of headers in hash table
202 subtype Entity_Header_Num is Integer range 0 .. Entity_Table_Size - 1;
203 -- Range of headers in hash table
205 function Node_Hash (Id : Entity_Id) return Entity_Header_Num;
206 -- Simple hash function for Entity_Ids
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.
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.
228 -----------------
229 -- Node_Hash --
230 -----------------
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;
237 ---------------------------------
238 -- Analyze_Package_Body_Helper --
239 ---------------------------------
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.
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.
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.
259 --------------------------
260 -- Hide_Public_Entities --
261 --------------------------
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.
274 function Scan_Subprogram_Ref (N : Node_Id) return Traverse_Result;
275 -- Determine whether a node denotes a reference to a subprogram
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.
284 procedure Scan_Subprogram_Refs (Node : Node_Id);
285 -- If we haven't already traversed Node, then mark it and traverse
286 -- it.
288 --------------------
289 -- Has_Referencer --
290 --------------------
292 function Has_Referencer
293 (Decls : List_Id;
294 In_Nested_Instance : Boolean;
295 Has_Outer_Referencer_Of_Non_Subprograms : Boolean) return Boolean
297 Decl : Node_Id;
298 Decl_Id : Entity_Id;
299 Spec : Node_Id;
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.
307 begin
308 if No (Decls) then
309 return False;
310 end if;
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.
316 Decl := Last (Decls);
317 while Present (Decl) loop
319 -- A stub is always considered a referencer
321 if Nkind (Decl) in N_Body_Stub then
322 return True;
324 -- Package declaration
326 elsif Nkind (Decl) = N_Package_Declaration then
327 Spec := Specification (Decl);
328 Decl_Id := Defining_Entity (Spec);
330 -- Inspect the declarations of a non-generic package to try
331 -- and hide more entities from external visibility.
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;
350 -- Package body
352 elsif Nkind (Decl) = N_Package_Body
353 and then Present (Corresponding_Spec (Decl))
354 then
355 Decl_Id := Corresponding_Spec (Decl);
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.
367 if Is_Generic_Unit (Decl_Id) then
368 return True;
370 -- Inspect the declarations of a non-generic package body to
371 -- try and hide more entities from external visibility.
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;
382 -- Subprogram body
384 elsif Nkind (Decl) = N_Subprogram_Body then
385 if Present (Corresponding_Spec (Decl)) then
386 Decl_Id := Corresponding_Spec (Decl);
388 -- A generic subprogram body acts as a referencer
390 if Is_Generic_Unit (Decl_Id) then
391 return True;
392 end if;
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.
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.
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;
411 -- Inspect the statements of the subprogram body
412 -- to determine whether the body references other
413 -- subprograms.
415 Scan_Subprogram_Refs (Decl);
416 end if;
418 -- Otherwise this is a stand alone subprogram body
420 else
421 Decl_Id := Defining_Entity (Decl);
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.
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;
433 -- Inspect the statements of the subprogram body
434 -- to determine whether the body references other
435 -- subprograms.
437 Scan_Subprogram_Refs (Decl);
439 -- Otherwise we can reset Is_Public right away
441 elsif not Subprogram_Table.Get (Decl_Id) then
442 Set_Is_Public (Decl_Id, False);
443 end if;
444 end if;
446 -- Freeze node
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.
461 Discard :=
462 Has_Referencer (Actions (Decl),
463 In_Nested_Instance,
464 Has_Referencer_Of_Non_Subprograms);
465 end;
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.
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);
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;
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.
490 elsif Nkind (Decl) in N_Subprogram_Declaration
491 | N_Subprogram_Renaming_Declaration
492 then
493 Decl_Id := Defining_Entity (Decl);
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.
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;
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.
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;
529 Prev (Decl);
530 end loop;
532 return Has_Referencer_Of_Non_Subprograms;
533 end Has_Referencer;
535 -------------------------
536 -- Scan_Subprogram_Ref --
537 -------------------------
539 function Scan_Subprogram_Ref (N : Node_Id) return Traverse_Result is
540 begin
541 -- Detect a reference of the form
542 -- Subp_Call
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);
551 -- Detect a reference of the form
552 -- Subp'Some_Attribute
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);
561 -- Constants can be substituted by their value in gigi, which may
562 -- contain a reference, so scan the value recursively.
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;
579 return OK;
580 end Scan_Subprogram_Ref;
582 --------------------------
583 -- Scan_Subprogram_Refs --
584 --------------------------
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;
594 -- Local variables
596 Discard : Boolean;
597 pragma Unreferenced (Discard);
599 -- Start of processing for Hide_Public_Entities
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:
608 -- package Pack is
609 -- generic
610 -- package Gen is
611 -- end Gen;
612 -- end Pack;
614 -- package body Pack is
615 -- External_Obj : ...; -- (1)
617 -- package body Gen is -- (2)
618 -- ... External_Obj ... -- (3)
619 -- end Gen;
621 -- Local_Obj : ...; -- (4)
622 -- end Pack;
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.
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.
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.
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.
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.
664 Traversed_Table.Reset;
665 Subprogram_Table.Reset;
666 Discard := Has_Referencer (Decls, False, False);
667 end Hide_Public_Entities;
669 ----------------------------------
670 -- Install_Composite_Operations --
671 ----------------------------------
673 procedure Install_Composite_Operations (P : Entity_Id) is
674 Id : Entity_Id;
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;
688 Next_Entity (Id);
689 end loop;
690 end Install_Composite_Operations;
692 -- Local variables
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
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;
708 -- Start of processing for Analyze_Package_Body_Helper
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.
718 -- Set Body_Id. Note that this will be reset to point to the generic
719 -- copy later on in the generic case.
721 Body_Id := Defining_Entity (N);
723 -- Body is body of package instantiation. Corresponding spec has already
724 -- been set.
726 if Present (Corresponding_Spec (N)) then
727 Spec_Id := Corresponding_Spec (N);
728 Pack_Decl := Unit_Declaration_Node (Spec_Id);
730 else
731 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
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);
738 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
739 Error_Msg_N ("cannot supply body for package renaming", N);
740 return;
742 elsif Present (Corresponding_Body (Pack_Decl)) then
743 Error_Msg_N ("redefinition of package body", N);
744 return;
745 end if;
747 else
748 Error_Msg_N ("missing specification for package body", N);
749 return;
750 end if;
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 Error_Msg_N ("\either remove the body or add pragma "
763 & "Elaborate_Body in the spec", N);
764 end if;
765 end if;
766 end if;
768 -- A [generic] package body freezes the contract of the nearest
769 -- enclosing package body and all other contracts encountered in
770 -- the same declarative part up to and excluding the package body:
772 -- package body Nearest_Enclosing_Package
773 -- with Refined_State => (State => Constit)
774 -- is
775 -- Constit : ...;
777 -- package body Freezes_Enclosing_Package_Body
778 -- with Refined_State => (State_2 => Constit_2)
779 -- is
780 -- Constit_2 : ...;
782 -- procedure Proc
783 -- with Refined_Depends => (Input => (Constit, Constit_2)) ...
785 -- This ensures that any annotations referenced by the contract of a
786 -- [generic] subprogram body declared within the current package body
787 -- are available. This form of freezing is decoupled from the usual
788 -- Freeze_xxx mechanism because it must also work in the context of
789 -- generics where normal freezing is disabled.
791 -- Only bodies coming from source should cause this type of freezing.
792 -- Instantiated generic bodies are excluded because their processing is
793 -- performed in a separate compilation pass which lacks enough semantic
794 -- information with respect to contract analysis. It is safe to suppress
795 -- the freezing of contracts in this case because this action already
796 -- took place at the end of the enclosing declarative part.
798 if Comes_From_Source (N)
799 and then not Is_Generic_Instance (Spec_Id)
800 then
801 Freeze_Previous_Contracts (N);
802 end if;
804 -- A package body is Ghost when the corresponding spec is Ghost. Set
805 -- the mode now to ensure that any nodes generated during analysis and
806 -- expansion are properly flagged as ignored Ghost.
808 Mark_And_Set_Ghost_Body (N, Spec_Id);
810 -- Deactivate expansion inside the body of ignored Ghost entities,
811 -- as this code will ultimately be ignored. This avoids requiring the
812 -- presence of run-time units which are not needed. Only do this for
813 -- user entities, as internally generated entities might still need
814 -- to be expanded (e.g. those generated for types).
816 if Present (Ignored_Ghost_Region)
817 and then Comes_From_Source (Body_Id)
818 then
819 Expander_Active := False;
820 end if;
822 -- If the body completes the initial declaration of a compilation unit
823 -- which is subject to pragma Elaboration_Checks, set the model of the
824 -- pragma because it applies to all parts of the unit.
826 Install_Elaboration_Model (Spec_Id);
828 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
829 Style.Check_Identifier (Body_Id, Spec_Id);
831 if Is_Child_Unit (Spec_Id) then
832 if Nkind (Parent (N)) /= N_Compilation_Unit then
833 Error_Msg_NE
834 ("body of child unit& cannot be an inner package", N, Spec_Id);
835 end if;
837 Set_Is_Child_Unit (Body_Id);
838 end if;
840 -- Generic package case
842 if Ekind (Spec_Id) = E_Generic_Package then
844 -- Disable expansion and perform semantic analysis on copy. The
845 -- unannotated body will be used in all instantiations.
847 Body_Id := Defining_Entity (N);
848 Mutate_Ekind (Body_Id, E_Package_Body);
849 Set_Scope (Body_Id, Scope (Spec_Id));
850 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
851 Set_Body_Entity (Spec_Id, Body_Id);
852 Set_Spec_Entity (Body_Id, Spec_Id);
854 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
855 Rewrite (N, New_N);
857 -- Once the contents of the generic copy and the template are
858 -- swapped, do the same for their respective aspect specifications.
860 Exchange_Aspects (N, New_N);
862 -- Collect all contract-related source pragmas found within the
863 -- template and attach them to the contract of the package body.
864 -- This contract is used in the capture of global references within
865 -- annotations.
867 Create_Generic_Contract (N);
869 -- Update Body_Id to point to the copied node for the remainder of
870 -- the processing.
872 Body_Id := Defining_Entity (N);
873 Start_Generic;
874 end if;
876 -- The Body_Id is that of the copied node in the generic case, the
877 -- current node otherwise. Note that N was rewritten above, so we must
878 -- be sure to get the latest Body_Id value.
880 Mutate_Ekind (Body_Id, E_Package_Body);
881 Set_Body_Entity (Spec_Id, Body_Id);
882 Set_Spec_Entity (Body_Id, Spec_Id);
884 -- Defining name for the package body is not a visible entity: Only the
885 -- defining name for the declaration is visible.
887 Set_Etype (Body_Id, Standard_Void_Type);
888 Set_Scope (Body_Id, Scope (Spec_Id));
889 Set_Corresponding_Spec (N, Spec_Id);
890 Set_Corresponding_Body (Pack_Decl, Body_Id);
892 -- The body entity is not used for semantics or code generation, but
893 -- it is attached to the entity list of the enclosing scope to simplify
894 -- the listing of back-annotations for the types it main contain.
896 if Scope (Spec_Id) /= Standard_Standard then
897 Append_Entity (Body_Id, Scope (Spec_Id));
898 end if;
900 -- Indicate that we are currently compiling the body of the package
902 Set_In_Package_Body (Spec_Id);
903 Set_Has_Completion (Spec_Id);
904 Last_Spec_Entity := Last_Entity (Spec_Id);
906 if Has_Aspects (N) then
907 Analyze_Aspect_Specifications (N, Body_Id);
908 end if;
910 Push_Scope (Spec_Id);
912 -- Set SPARK_Mode only for non-generic package
914 if Ekind (Spec_Id) = E_Package then
915 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
916 Set_SPARK_Aux_Pragma (Body_Id, SPARK_Mode_Pragma);
917 Set_SPARK_Pragma_Inherited (Body_Id);
918 Set_SPARK_Aux_Pragma_Inherited (Body_Id);
920 -- A package body may be instantiated or inlined at a later pass.
921 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when
922 -- it applied to the package spec.
924 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
925 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
926 end if;
927 end if;
929 Set_Categorization_From_Pragmas (N);
931 Install_Visible_Declarations (Spec_Id);
932 Install_Private_Declarations (Spec_Id);
933 Install_Private_With_Clauses (Spec_Id);
934 Install_Composite_Operations (Spec_Id);
936 Check_Anonymous_Access_Types (Spec_Id, N);
938 if Ekind (Spec_Id) = E_Generic_Package then
939 Set_Use (Generic_Formal_Declarations (Pack_Decl));
940 end if;
942 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
943 Set_Use (Private_Declarations (Specification (Pack_Decl)));
945 -- This is a nested package, so it may be necessary to declare certain
946 -- inherited subprograms that are not yet visible because the parent
947 -- type's subprograms are now visible.
948 -- Note that for child units these operations were generated when
949 -- analyzing the package specification.
951 if Ekind (Scope (Spec_Id)) = E_Package
952 and then Scope (Spec_Id) /= Standard_Standard
953 and then not Is_Child_Unit (Spec_Id)
954 then
955 Declare_Inherited_Private_Subprograms (Spec_Id);
956 end if;
958 if Present (Declarations (N)) then
959 Analyze_Declarations (Declarations (N));
960 Inspect_Deferred_Constant_Completion (Declarations (N));
961 end if;
963 -- Verify that the SPARK_Mode of the body agrees with that of its spec
965 if Present (SPARK_Pragma (Body_Id)) then
966 if Present (SPARK_Aux_Pragma (Spec_Id)) then
967 if Get_SPARK_Mode_From_Annotation (SPARK_Aux_Pragma (Spec_Id)) =
969 and then
970 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
971 then
972 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
973 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
974 Error_Msg_Sloc := Sloc (SPARK_Aux_Pragma (Spec_Id));
975 Error_Msg_NE
976 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
977 end if;
979 -- SPARK_Mode Off could complete no SPARK_Mode in a generic, either
980 -- as specified in source code, or because SPARK_Mode On is ignored
981 -- in an instance where the context is SPARK_Mode Off/Auto.
983 elsif Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = Off
984 and then (Is_Generic_Unit (Spec_Id) or else In_Instance)
985 then
986 null;
988 else
989 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
990 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
991 Error_Msg_Sloc := Sloc (Spec_Id);
992 Error_Msg_NE
993 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
994 end if;
995 end if;
997 -- Analyze_Declarations has caused freezing of all types. Now generate
998 -- bodies for RACW primitives and stream attributes, if any.
1000 if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then
1002 -- Attach subprogram bodies to support RACWs declared in spec
1004 Append_RACW_Bodies (Declarations (N), Spec_Id);
1005 Analyze_List (Declarations (N));
1006 end if;
1008 HSS := Handled_Statement_Sequence (N);
1010 if Present (HSS) then
1011 Process_End_Label (HSS, 't', Spec_Id);
1012 Analyze (HSS);
1014 -- Check that elaboration code in a preelaborable package body is
1015 -- empty other than null statements and labels (RM 10.2.1(6)).
1017 Validate_Null_Statement_Sequence (N);
1018 end if;
1020 Validate_Categorization_Dependency (N, Spec_Id);
1021 Check_Completion (Body_Id);
1023 -- Generate start of body reference. Note that we do this fairly late,
1024 -- because the call will use In_Extended_Main_Source_Unit as a check,
1025 -- and we want to make sure that Corresponding_Stub links are set
1027 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
1029 -- For a generic package, collect global references and mark them on
1030 -- the original body so that they are not resolved again at the point
1031 -- of instantiation.
1033 if Ekind (Spec_Id) /= E_Package then
1034 Save_Global_References (Original_Node (N));
1035 End_Generic;
1036 end if;
1038 -- The entities of the package body have so far been chained onto the
1039 -- declaration chain for the spec. That's been fine while we were in the
1040 -- body, since we wanted them to be visible, but now that we are leaving
1041 -- the package body, they are no longer visible, so we remove them from
1042 -- the entity chain of the package spec entity, and copy them to the
1043 -- entity chain of the package body entity, where they will never again
1044 -- be visible.
1046 if Present (Last_Spec_Entity) then
1047 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
1048 Set_Next_Entity (Last_Spec_Entity, Empty);
1049 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1050 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
1052 else
1053 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
1054 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1055 Set_First_Entity (Spec_Id, Empty);
1056 Set_Last_Entity (Spec_Id, Empty);
1057 end if;
1059 Update_Use_Clause_Chain;
1060 End_Package_Scope (Spec_Id);
1062 -- All entities declared in body are not visible
1064 declare
1065 E : Entity_Id;
1067 begin
1068 E := First_Entity (Body_Id);
1069 while Present (E) loop
1070 Set_Is_Immediately_Visible (E, False);
1071 Set_Is_Potentially_Use_Visible (E, False);
1072 Set_Is_Hidden (E);
1074 -- Child units may appear on the entity list (e.g. if they appear
1075 -- in the context of a subunit) but they are not body entities.
1077 if not Is_Child_Unit (E) then
1078 Set_Is_Package_Body_Entity (E);
1079 end if;
1081 Next_Entity (E);
1082 end loop;
1083 end;
1085 Check_References (Body_Id);
1087 -- For a generic unit, check that the formal parameters are referenced,
1088 -- and that local variables are used, as for regular packages.
1090 if Ekind (Spec_Id) = E_Generic_Package then
1091 Check_References (Spec_Id);
1092 end if;
1094 -- At this point all entities of the package body are externally visible
1095 -- to the linker as their Is_Public flag is set to True. This proactive
1096 -- approach is necessary because an inlined or a generic body for which
1097 -- code is generated in other units may need to see these entities. Cut
1098 -- down the number of global symbols that do not need public visibility
1099 -- as this has two beneficial effects:
1100 -- (1) It makes the compilation process more efficient.
1101 -- (2) It gives the code generator more leeway to optimize within each
1102 -- unit, especially subprograms.
1104 -- This is done only for top-level library packages or child units as
1105 -- the algorithm does a top-down traversal of the package body. This is
1106 -- also done for instances because instantiations are still pending by
1107 -- the time the enclosing package body is analyzed.
1109 if (Scope (Spec_Id) = Standard_Standard
1110 or else Is_Child_Unit (Spec_Id)
1111 or else Is_Generic_Instance (Spec_Id))
1112 and then not Is_Generic_Unit (Spec_Id)
1113 then
1114 Hide_Public_Entities (Declarations (N));
1115 end if;
1117 -- If expander is not active, then here is where we turn off the
1118 -- In_Package_Body flag, otherwise it is turned off at the end of the
1119 -- corresponding expansion routine. If this is an instance body, we need
1120 -- to qualify names of local entities, because the body may have been
1121 -- compiled as a preliminary to another instantiation.
1123 if not Expander_Active then
1124 Set_In_Package_Body (Spec_Id, False);
1126 if Is_Generic_Instance (Spec_Id)
1127 and then Operating_Mode = Generate_Code
1128 then
1129 Qualify_Entity_Names (N);
1130 end if;
1131 end if;
1133 if Present (Ignored_Ghost_Region) then
1134 Expander_Active := Saved_EA;
1135 end if;
1137 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
1138 Restore_Ghost_Region (Saved_GM, Saved_IGR);
1139 end Analyze_Package_Body_Helper;
1141 ---------------------------------
1142 -- Analyze_Package_Declaration --
1143 ---------------------------------
1145 procedure Analyze_Package_Declaration (N : Node_Id) is
1146 Id : constant Node_Id := Defining_Entity (N);
1148 Is_Comp_Unit : constant Boolean :=
1149 Nkind (Parent (N)) = N_Compilation_Unit;
1151 Body_Required : Boolean;
1152 -- True when this package declaration requires a corresponding body
1154 begin
1155 if Debug_Flag_C then
1156 Write_Str ("==> package spec ");
1157 Write_Name (Chars (Id));
1158 Write_Str (" from ");
1159 Write_Location (Sloc (N));
1160 Write_Eol;
1161 Indent;
1162 end if;
1164 Generate_Definition (Id);
1165 Enter_Name (Id);
1166 Mutate_Ekind (Id, E_Package);
1167 Set_Etype (Id, Standard_Void_Type);
1169 -- Set SPARK_Mode from context
1171 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
1172 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
1173 Set_SPARK_Pragma_Inherited (Id);
1174 Set_SPARK_Aux_Pragma_Inherited (Id);
1176 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
1177 -- the body of this package is instantiated or inlined later and out of
1178 -- context. The body uses this attribute to restore the value of the
1179 -- global flag.
1181 if Ignore_SPARK_Mode_Pragmas_In_Instance then
1182 Set_Ignore_SPARK_Mode_Pragmas (Id);
1183 end if;
1185 -- Analyze aspect specifications immediately, since we need to recognize
1186 -- things like Pure early enough to diagnose violations during analysis.
1188 if Has_Aspects (N) then
1189 Analyze_Aspect_Specifications (N, Id);
1190 end if;
1192 -- Ada 2005 (AI-217): Check if the package has been illegally named in
1193 -- a limited-with clause of its own context. In this case the error has
1194 -- been previously notified by Analyze_Context.
1196 -- limited with Pkg; -- ERROR
1197 -- package Pkg is ...
1199 if From_Limited_With (Id) then
1200 return;
1201 end if;
1203 Push_Scope (Id);
1205 Set_Is_Pure (Id, Is_Pure (Enclosing_Lib_Unit_Entity));
1206 Set_Categorization_From_Pragmas (N);
1208 Analyze (Specification (N));
1209 Validate_Categorization_Dependency (N, Id);
1211 -- Determine whether the package requires a body. Abstract states are
1212 -- intentionally ignored because they do require refinement which can
1213 -- only come in a body, but at the same time they do not force the need
1214 -- for a body on their own (SPARK RM 7.1.4(4) and 7.2.2(3)).
1216 Body_Required := Unit_Requires_Body (Id);
1218 if not Body_Required then
1220 -- If the package spec does not require an explicit body, then there
1221 -- are not entities requiring completion in the language sense. Call
1222 -- Check_Completion now to ensure that nested package declarations
1223 -- that require an implicit body get one. (In the case where a body
1224 -- is required, Check_Completion is called at the end of the body's
1225 -- declarative part.)
1227 Check_Completion;
1229 -- If the package spec does not require an explicit body, then all
1230 -- abstract states declared in nested packages cannot possibly get
1231 -- a proper refinement (SPARK RM 7.2.2(3)). This check is performed
1232 -- only when the compilation unit is the main unit to allow for
1233 -- modular SPARK analysis where packages do not necessarily have
1234 -- bodies.
1236 if Is_Comp_Unit then
1237 Check_State_Refinements
1238 (Context => N,
1239 Is_Main_Unit => Parent (N) = Cunit (Main_Unit));
1240 end if;
1241 end if;
1243 -- Set Body_Required indication on the compilation unit node
1245 if Is_Comp_Unit then
1246 Set_Body_Required (Parent (N), Body_Required);
1248 if Legacy_Elaboration_Checks and not Body_Required then
1249 Set_Suppress_Elaboration_Warnings (Id);
1250 end if;
1251 end if;
1253 End_Package_Scope (Id);
1255 -- For the declaration of a library unit that is a remote types package,
1256 -- check legality rules regarding availability of stream attributes for
1257 -- types that contain non-remote access values. This subprogram performs
1258 -- visibility tests that rely on the fact that we have exited the scope
1259 -- of Id.
1261 if Is_Comp_Unit then
1262 Validate_RT_RAT_Component (N);
1263 end if;
1265 if Debug_Flag_C then
1266 Outdent;
1267 Write_Str ("<== package spec ");
1268 Write_Name (Chars (Id));
1269 Write_Str (" from ");
1270 Write_Location (Sloc (N));
1271 Write_Eol;
1272 end if;
1273 end Analyze_Package_Declaration;
1275 -----------------------------------
1276 -- Analyze_Package_Specification --
1277 -----------------------------------
1279 -- Note that this code is shared for the analysis of generic package specs
1280 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
1282 procedure Analyze_Package_Specification (N : Node_Id) is
1283 Id : constant Entity_Id := Defining_Entity (N);
1284 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
1285 Vis_Decls : constant List_Id := Visible_Declarations (N);
1286 Priv_Decls : constant List_Id := Private_Declarations (N);
1287 E : Entity_Id;
1288 L : Entity_Id;
1289 Public_Child : Boolean;
1291 Private_With_Clauses_Installed : Boolean := False;
1292 -- In Ada 2005, private with_clauses are visible in the private part
1293 -- of a nested package, even if it appears in the public part of the
1294 -- enclosing package. This requires a separate step to install these
1295 -- private_with_clauses, and remove them at the end of the nested
1296 -- package.
1298 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
1299 -- Clears constant indications (Never_Set_In_Source, Constant_Value, and
1300 -- Is_True_Constant) on all variables that are entities of Id, and on
1301 -- the chain whose first element is FE. A recursive call is made for all
1302 -- packages and generic packages.
1304 procedure Generate_Parent_References;
1305 -- For a child unit, generate references to parent units, for
1306 -- GNAT Studio navigation purposes.
1308 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
1309 -- Child and Unit are entities of compilation units. True if Child
1310 -- is a public child of Parent as defined in 10.1.1
1312 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
1313 -- Reject completion of an incomplete or private type declarations
1314 -- having a known discriminant part by an unchecked union.
1316 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
1317 -- Given the package entity of a generic package instantiation or
1318 -- formal package whose corresponding generic is a child unit, installs
1319 -- the private declarations of each of the child unit's parents.
1320 -- This has to be done at the point of entering the instance package's
1321 -- private part rather than being done in Sem_Ch12.Install_Parent
1322 -- (which is where the parents' visible declarations are installed).
1324 ---------------------
1325 -- Clear_Constants --
1326 ---------------------
1328 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
1329 E : Entity_Id;
1331 begin
1332 -- Ignore package renamings, not interesting and they can cause self
1333 -- referential loops in the code below.
1335 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
1336 return;
1337 end if;
1339 -- Note: in the loop below, the check for Next_Entity pointing back
1340 -- to the package entity may seem odd, but it is needed, because a
1341 -- package can contain a renaming declaration to itself, and such
1342 -- renamings are generated automatically within package instances.
1344 E := FE;
1345 while Present (E) and then E /= Id loop
1346 if Is_Assignable (E) then
1347 Set_Never_Set_In_Source (E, False);
1348 Set_Is_True_Constant (E, False);
1349 Set_Current_Value (E, Empty);
1350 Set_Is_Known_Null (E, False);
1351 Set_Last_Assignment (E, Empty);
1353 if not Can_Never_Be_Null (E) then
1354 Set_Is_Known_Non_Null (E, False);
1355 end if;
1357 elsif Is_Package_Or_Generic_Package (E) then
1358 Clear_Constants (E, First_Entity (E));
1359 Clear_Constants (E, First_Private_Entity (E));
1360 end if;
1362 Next_Entity (E);
1363 end loop;
1364 end Clear_Constants;
1366 --------------------------------
1367 -- Generate_Parent_References --
1368 --------------------------------
1370 procedure Generate_Parent_References is
1371 Decl : constant Node_Id := Parent (N);
1373 begin
1374 if Id = Cunit_Entity (Main_Unit)
1375 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
1376 then
1377 Generate_Reference (Id, Scope (Id), 'k', False);
1379 elsif Nkind (Unit (Cunit (Main_Unit))) not in
1380 N_Subprogram_Body | N_Subunit
1381 then
1382 -- If current unit is an ancestor of main unit, generate a
1383 -- reference to its own parent.
1385 declare
1386 U : Node_Id;
1387 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
1389 begin
1390 if Nkind (Main_Spec) = N_Package_Body then
1391 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
1392 end if;
1394 U := Parent_Spec (Main_Spec);
1395 while Present (U) loop
1396 if U = Parent (Decl) then
1397 Generate_Reference (Id, Scope (Id), 'k', False);
1398 exit;
1400 elsif Nkind (Unit (U)) = N_Package_Body then
1401 exit;
1403 else
1404 U := Parent_Spec (Unit (U));
1405 end if;
1406 end loop;
1407 end;
1408 end if;
1409 end Generate_Parent_References;
1411 ---------------------
1412 -- Is_Public_Child --
1413 ---------------------
1415 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
1416 begin
1417 if not Is_Private_Descendant (Child) then
1418 return True;
1419 else
1420 if Child = Unit then
1421 return not Private_Present (
1422 Parent (Unit_Declaration_Node (Child)));
1423 else
1424 return Is_Public_Child (Scope (Child), Unit);
1425 end if;
1426 end if;
1427 end Is_Public_Child;
1429 ----------------------------------------
1430 -- Inspect_Unchecked_Union_Completion --
1431 ----------------------------------------
1433 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
1434 Decl : Node_Id;
1436 begin
1437 Decl := First (Decls);
1438 while Present (Decl) loop
1440 -- We are looking at an incomplete or private type declaration
1441 -- with a known_discriminant_part whose full view is an
1442 -- Unchecked_Union. The seemingly useless check with Is_Type
1443 -- prevents cascaded errors when routines defined only for type
1444 -- entities are called with non-type entities.
1446 if Nkind (Decl) in N_Incomplete_Type_Declaration
1447 | N_Private_Type_Declaration
1448 and then Is_Type (Defining_Identifier (Decl))
1449 and then Has_Discriminants (Defining_Identifier (Decl))
1450 and then Present (Full_View (Defining_Identifier (Decl)))
1451 and then
1452 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
1453 then
1454 Error_Msg_N
1455 ("completion of discriminated partial view "
1456 & "cannot be an unchecked union",
1457 Full_View (Defining_Identifier (Decl)));
1458 end if;
1460 Next (Decl);
1461 end loop;
1462 end Inspect_Unchecked_Union_Completion;
1464 -----------------------------------------
1465 -- Install_Parent_Private_Declarations --
1466 -----------------------------------------
1468 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
1469 Inst_Par : Entity_Id;
1470 Gen_Par : Entity_Id;
1471 Inst_Node : Node_Id;
1473 begin
1474 Inst_Par := Inst_Id;
1476 Gen_Par :=
1477 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
1478 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
1479 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
1481 if Nkind (Inst_Node) in
1482 N_Package_Instantiation | N_Formal_Package_Declaration
1483 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
1484 then
1485 Inst_Par := Entity (Prefix (Name (Inst_Node)));
1487 if Present (Renamed_Entity (Inst_Par)) then
1488 Inst_Par := Renamed_Entity (Inst_Par);
1489 end if;
1491 -- The instance may appear in a sibling generic unit, in
1492 -- which case the prefix must include the common (generic)
1493 -- ancestor, which is treated as a current instance.
1495 if Inside_A_Generic
1496 and then Ekind (Inst_Par) = E_Generic_Package
1497 then
1498 Gen_Par := Inst_Par;
1499 pragma Assert (In_Open_Scopes (Gen_Par));
1501 else
1502 Gen_Par :=
1503 Generic_Parent
1504 (Specification (Unit_Declaration_Node (Inst_Par)));
1505 end if;
1507 -- Install the private declarations and private use clauses
1508 -- of a parent instance of the child instance, unless the
1509 -- parent instance private declarations have already been
1510 -- installed earlier in Analyze_Package_Specification, which
1511 -- happens when a generic child is instantiated, and the
1512 -- instance is a child of the parent instance.
1514 -- Installing the use clauses of the parent instance twice
1515 -- is both unnecessary and wrong, because it would cause the
1516 -- clauses to be chained to themselves in the use clauses
1517 -- list of the scope stack entry. That in turn would cause
1518 -- an endless loop from End_Use_Clauses upon scope exit.
1520 -- The parent is now fully visible. It may be a hidden open
1521 -- scope if we are currently compiling some child instance
1522 -- declared within it, but while the current instance is being
1523 -- compiled the parent is immediately visible. In particular
1524 -- its entities must remain visible if a stack save/restore
1525 -- takes place through a call to Rtsfind.
1527 if Present (Gen_Par) then
1528 if not In_Private_Part (Inst_Par) then
1529 Install_Private_Declarations (Inst_Par);
1530 Set_Use (Private_Declarations
1531 (Specification
1532 (Unit_Declaration_Node (Inst_Par))));
1533 Set_Is_Hidden_Open_Scope (Inst_Par, False);
1534 end if;
1536 -- If we've reached the end of the generic instance parents,
1537 -- then finish off by looping through the nongeneric parents
1538 -- and installing their private declarations.
1540 -- If one of the non-generic parents is itself on the scope
1541 -- stack, do not install its private declarations: they are
1542 -- installed in due time when the private part of that parent
1543 -- is analyzed.
1545 else
1546 while Present (Inst_Par)
1547 and then Inst_Par /= Standard_Standard
1548 and then (not In_Open_Scopes (Inst_Par)
1549 or else not In_Private_Part (Inst_Par))
1550 loop
1551 if Nkind (Inst_Node) = N_Formal_Package_Declaration
1552 or else
1553 not Is_Ancestor_Package
1554 (Inst_Par, Cunit_Entity (Current_Sem_Unit))
1555 then
1556 Install_Private_Declarations (Inst_Par);
1557 Set_Use
1558 (Private_Declarations
1559 (Specification
1560 (Unit_Declaration_Node (Inst_Par))));
1561 Inst_Par := Scope (Inst_Par);
1562 else
1563 exit;
1564 end if;
1565 end loop;
1567 exit;
1568 end if;
1570 else
1571 exit;
1572 end if;
1573 end loop;
1574 end Install_Parent_Private_Declarations;
1576 -- Start of processing for Analyze_Package_Specification
1578 begin
1579 if Present (Vis_Decls) then
1580 Analyze_Declarations (Vis_Decls);
1581 end if;
1583 -- Inspect the entities defined in the package and ensure that all
1584 -- incomplete types have received full declarations. Build default
1585 -- initial condition and invariant procedures for all qualifying types.
1587 E := First_Entity (Id);
1588 while Present (E) loop
1590 -- Check on incomplete types
1592 -- AI05-0213: A formal incomplete type has no completion, and neither
1593 -- does the corresponding subtype in an instance.
1595 if Is_Incomplete_Type (E)
1596 and then No (Full_View (E))
1597 and then not Is_Generic_Type (E)
1598 and then not From_Limited_With (E)
1599 and then not Is_Generic_Actual_Type (E)
1600 then
1601 Error_Msg_N ("no declaration in visible part for incomplete}", E);
1602 end if;
1604 Next_Entity (E);
1605 end loop;
1607 if Is_Remote_Call_Interface (Id)
1608 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1609 then
1610 Validate_RCI_Declarations (Id);
1611 end if;
1613 -- Save global references in the visible declarations, before installing
1614 -- private declarations of parent unit if there is one, because the
1615 -- privacy status of types defined in the parent will change. This is
1616 -- only relevant for generic child units, but is done in all cases for
1617 -- uniformity.
1619 if Ekind (Id) = E_Generic_Package
1620 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1621 then
1622 declare
1623 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1624 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1626 begin
1627 -- Insert the freezing nodes after the visible declarations to
1628 -- ensure that we analyze its aspects; needed to ensure that
1629 -- global entities referenced in the aspects are properly handled.
1631 if Ada_Version >= Ada_2012
1632 and then Is_Non_Empty_List (Vis_Decls)
1633 and then Is_Empty_List (Priv_Decls)
1634 then
1635 Insert_List_After_And_Analyze
1636 (Last (Vis_Decls), Freeze_Entity (Id, Last (Vis_Decls)));
1637 end if;
1639 Set_Private_Declarations (Orig_Spec, Empty_List);
1640 Save_Global_References (Orig_Decl);
1641 Set_Private_Declarations (Orig_Spec, Save_Priv);
1642 end;
1643 end if;
1645 -- If package is a public child unit, then make the private declarations
1646 -- of the parent visible.
1648 Public_Child := False;
1650 declare
1651 Par : Entity_Id;
1652 Pack_Decl : Node_Id;
1653 Par_Spec : Node_Id;
1655 begin
1656 Par := Id;
1657 Par_Spec := Parent_Spec (Parent (N));
1659 -- If the package is formal package of an enclosing generic, it is
1660 -- transformed into a local generic declaration, and compiled to make
1661 -- its spec available. We need to retrieve the original generic to
1662 -- determine whether it is a child unit, and install its parents.
1664 if No (Par_Spec)
1665 and then
1666 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1667 then
1668 Par := Entity (Name (Original_Node (Parent (N))));
1669 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1670 end if;
1672 if Present (Par_Spec) then
1673 Generate_Parent_References;
1675 while Scope (Par) /= Standard_Standard
1676 and then Is_Public_Child (Id, Par)
1677 and then In_Open_Scopes (Par)
1678 loop
1679 Public_Child := True;
1680 Par := Scope (Par);
1681 Install_Private_Declarations (Par);
1682 Install_Private_With_Clauses (Par);
1683 Pack_Decl := Unit_Declaration_Node (Par);
1684 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1685 end loop;
1686 end if;
1687 end;
1689 if Is_Compilation_Unit (Id) then
1690 Install_Private_With_Clauses (Id);
1691 else
1692 -- The current compilation unit may include private with_clauses,
1693 -- which are visible in the private part of the current nested
1694 -- package, and have to be installed now. This is not done for
1695 -- nested instantiations, where the private with_clauses of the
1696 -- enclosing unit have no effect once the instantiation info is
1697 -- established and we start analyzing the package declaration.
1699 declare
1700 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1701 begin
1702 if Is_Package_Or_Generic_Package (Comp_Unit)
1703 and then not In_Private_Part (Comp_Unit)
1704 and then not In_Instance
1705 then
1706 Install_Private_With_Clauses (Comp_Unit);
1707 Private_With_Clauses_Installed := True;
1708 end if;
1709 end;
1710 end if;
1712 -- If this is a package associated with a generic instance or formal
1713 -- package, then the private declarations of each of the generic's
1714 -- parents must be installed at this point.
1716 if Is_Generic_Instance (Id) then
1717 Install_Parent_Private_Declarations (Id);
1718 end if;
1720 -- Analyze private part if present. The flag In_Private_Part is reset
1721 -- in End_Package_Scope.
1723 L := Last_Entity (Id);
1725 if Present (Priv_Decls) then
1726 Set_In_Private_Part (Id);
1728 -- Upon entering a public child's private part, it may be necessary
1729 -- to declare subprograms that were derived in the package's visible
1730 -- part but not yet made visible.
1732 if Public_Child then
1733 Declare_Inherited_Private_Subprograms (Id);
1734 end if;
1736 Analyze_Declarations (Priv_Decls);
1738 -- Check the private declarations for incomplete deferred constants
1740 Inspect_Deferred_Constant_Completion (Priv_Decls);
1742 -- The first private entity is the immediate follower of the last
1743 -- visible entity, if there was one.
1745 if Present (L) then
1746 Set_First_Private_Entity (Id, Next_Entity (L));
1747 else
1748 Set_First_Private_Entity (Id, First_Entity (Id));
1749 end if;
1751 -- There may be inherited private subprograms that need to be declared,
1752 -- even in the absence of an explicit private part. If there are any
1753 -- public declarations in the package and the package is a public child
1754 -- unit, then an implicit private part is assumed.
1756 elsif Present (L) and then Public_Child then
1757 Set_In_Private_Part (Id);
1758 Declare_Inherited_Private_Subprograms (Id);
1759 Set_First_Private_Entity (Id, Next_Entity (L));
1760 end if;
1762 E := First_Entity (Id);
1763 while Present (E) loop
1765 -- Check rule of 3.6(11), which in general requires waiting till all
1766 -- full types have been seen.
1768 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1769 Check_Aliased_Component_Types (E);
1770 end if;
1772 -- Check preelaborable initialization for full type completing a
1773 -- private type when aspect Preelaborable_Initialization is True
1774 -- or is specified by Preelaborable_Initialization attributes
1775 -- (in the case of a private type in a generic unit). We pass
1776 -- the expression of the aspect (when present) to the parameter
1777 -- Preelab_Init_Expr to take into account the rule that presumes
1778 -- that subcomponents of generic formal types mentioned in the
1779 -- type's P_I aspect have preelaborable initialization (see
1780 -- AI12-0409 and RM 10.2.1(11.8/5)).
1782 if Is_Type (E) and then Must_Have_Preelab_Init (E) then
1783 declare
1784 PI_Aspect : constant Node_Id :=
1785 Find_Aspect
1786 (E, Aspect_Preelaborable_Initialization);
1787 PI_Expr : Node_Id := Empty;
1788 begin
1789 if Present (PI_Aspect) then
1790 PI_Expr := Expression (PI_Aspect);
1791 end if;
1793 if not Has_Preelaborable_Initialization
1794 (E, Preelab_Init_Expr => PI_Expr)
1795 then
1796 Error_Msg_N
1797 ("full view of & does not have "
1798 & "preelaborable initialization", E);
1799 end if;
1800 end;
1801 end if;
1803 Next_Entity (E);
1804 end loop;
1806 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1807 -- declaration having a known_discriminant_part shall not be an
1808 -- unchecked union type.
1810 if Present (Vis_Decls) then
1811 Inspect_Unchecked_Union_Completion (Vis_Decls);
1812 end if;
1814 if Present (Priv_Decls) then
1815 Inspect_Unchecked_Union_Completion (Priv_Decls);
1816 end if;
1818 if Ekind (Id) = E_Generic_Package
1819 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1820 and then Present (Priv_Decls)
1821 then
1822 -- Save global references in private declarations, ignoring the
1823 -- visible declarations that were processed earlier.
1825 declare
1826 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1827 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1828 Save_Form : constant List_Id :=
1829 Generic_Formal_Declarations (Orig_Decl);
1831 begin
1832 -- Insert the freezing nodes after the private declarations to
1833 -- ensure that we analyze its aspects; needed to ensure that
1834 -- global entities referenced in the aspects are properly handled.
1836 if Ada_Version >= Ada_2012
1837 and then Is_Non_Empty_List (Priv_Decls)
1838 then
1839 Insert_List_After_And_Analyze
1840 (Last (Priv_Decls), Freeze_Entity (Id, Last (Priv_Decls)));
1841 end if;
1843 Set_Visible_Declarations (Orig_Spec, Empty_List);
1844 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1845 Save_Global_References (Orig_Decl);
1846 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1847 Set_Visible_Declarations (Orig_Spec, Save_Vis);
1848 end;
1849 end if;
1851 Process_End_Label (N, 'e', Id);
1853 -- Remove private_with_clauses of enclosing compilation unit, if they
1854 -- were installed.
1856 if Private_With_Clauses_Installed then
1857 Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
1858 end if;
1860 -- For the case of a library level package, we must go through all the
1861 -- entities clearing the indications that the value may be constant and
1862 -- not modified. Why? Because any client of this package may modify
1863 -- these values freely from anywhere. This also applies to any nested
1864 -- packages or generic packages.
1866 -- For now we unconditionally clear constants for packages that are
1867 -- instances of generic packages. The reason is that we do not have the
1868 -- body yet, and we otherwise think things are unreferenced when they
1869 -- are not. This should be fixed sometime (the effect is not terrible,
1870 -- we just lose some warnings, and also some cases of value propagation)
1871 -- ???
1873 if Is_Library_Level_Entity (Id)
1874 or else Is_Generic_Instance (Id)
1875 then
1876 Clear_Constants (Id, First_Entity (Id));
1877 Clear_Constants (Id, First_Private_Entity (Id));
1878 end if;
1880 -- Output relevant information as to why the package requires a body.
1881 -- Do not consider generated packages as this exposes internal symbols
1882 -- and leads to confusing messages.
1884 if List_Body_Required_Info
1885 and then In_Extended_Main_Source_Unit (Id)
1886 and then Unit_Requires_Body (Id)
1887 and then Comes_From_Source (Id)
1888 then
1889 Unit_Requires_Body_Info (Id);
1890 end if;
1892 -- Nested package specs that do not require bodies are not checked for
1893 -- ineffective use clauses due to the possibility of subunits. This is
1894 -- because at this stage it is impossible to tell whether there will be
1895 -- a separate body.
1897 if not Unit_Requires_Body (Id)
1898 and then Is_Compilation_Unit (Id)
1899 and then not Is_Private_Descendant (Id)
1900 then
1901 Update_Use_Clause_Chain;
1902 end if;
1903 end Analyze_Package_Specification;
1905 --------------------------------------
1906 -- Analyze_Private_Type_Declaration --
1907 --------------------------------------
1909 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1910 Id : constant Entity_Id := Defining_Identifier (N);
1911 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
1913 begin
1914 Generate_Definition (Id);
1915 Set_Is_Pure (Id, PF);
1916 Reinit_Size_Align (Id);
1918 if not Is_Package_Or_Generic_Package (Current_Scope)
1919 or else In_Private_Part (Current_Scope)
1920 then
1921 Error_Msg_N ("invalid context for private declaration", N);
1922 end if;
1924 New_Private_Type (N, Id, N);
1925 Set_Depends_On_Private (Id);
1927 -- Set the SPARK mode from the current context
1929 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
1930 Set_SPARK_Pragma_Inherited (Id);
1932 if Has_Aspects (N) then
1933 Analyze_Aspect_Specifications (N, Id);
1934 end if;
1935 end Analyze_Private_Type_Declaration;
1937 ----------------------------------
1938 -- Check_Anonymous_Access_Types --
1939 ----------------------------------
1941 procedure Check_Anonymous_Access_Types
1942 (Spec_Id : Entity_Id;
1943 P_Body : Node_Id)
1945 E : Entity_Id;
1946 IR : Node_Id;
1948 begin
1949 -- Itype references are only needed by gigi, to force elaboration of
1950 -- itypes. In the absence of code generation, they are not needed.
1952 if not Expander_Active then
1953 return;
1954 end if;
1956 E := First_Entity (Spec_Id);
1957 while Present (E) loop
1958 if Ekind (E) = E_Anonymous_Access_Type
1959 and then From_Limited_With (E)
1960 then
1961 IR := Make_Itype_Reference (Sloc (P_Body));
1962 Set_Itype (IR, E);
1964 if No (Declarations (P_Body)) then
1965 Set_Declarations (P_Body, New_List (IR));
1966 else
1967 Prepend (IR, Declarations (P_Body));
1968 end if;
1969 end if;
1971 Next_Entity (E);
1972 end loop;
1973 end Check_Anonymous_Access_Types;
1975 -------------------------------------------
1976 -- Declare_Inherited_Private_Subprograms --
1977 -------------------------------------------
1979 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1981 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1982 -- Check whether an inherited subprogram S is an operation of an
1983 -- untagged derived type T.
1985 ---------------------
1986 -- Is_Primitive_Of --
1987 ---------------------
1989 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1990 Formal : Entity_Id;
1992 begin
1993 -- If the full view is a scalar type, the type is the anonymous base
1994 -- type, but the operation mentions the first subtype, so check the
1995 -- signature against the base type.
1997 if Base_Type (Etype (S)) = Base_Type (T) then
1998 return True;
2000 else
2001 Formal := First_Formal (S);
2002 while Present (Formal) loop
2003 if Base_Type (Etype (Formal)) = Base_Type (T) then
2004 return True;
2005 end if;
2007 Next_Formal (Formal);
2008 end loop;
2010 return False;
2011 end if;
2012 end Is_Primitive_Of;
2014 -- Local variables
2016 E : Entity_Id;
2017 Op_List : Elist_Id;
2018 Op_Elmt : Elmt_Id;
2019 Op_Elmt_2 : Elmt_Id;
2020 Prim_Op : Entity_Id;
2021 New_Op : Entity_Id := Empty;
2022 Parent_Subp : Entity_Id;
2023 Tag : Entity_Id;
2025 -- Start of processing for Declare_Inherited_Private_Subprograms
2027 begin
2028 E := First_Entity (Id);
2029 while Present (E) loop
2031 -- If the entity is a nonprivate type extension whose parent type
2032 -- is declared in an open scope, then the type may have inherited
2033 -- operations that now need to be made visible. Ditto if the entity
2034 -- is a formal derived type in a child unit.
2036 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
2037 or else
2038 (Nkind (Parent (E)) = N_Private_Extension_Declaration
2039 and then Is_Generic_Type (E)))
2040 and then In_Open_Scopes (Scope (Etype (E)))
2041 and then Is_Base_Type (E)
2042 then
2043 if Is_Tagged_Type (E) then
2044 Op_List := Primitive_Operations (E);
2045 New_Op := Empty;
2046 Tag := First_Tag_Component (E);
2048 Op_Elmt := First_Elmt (Op_List);
2049 while Present (Op_Elmt) loop
2050 Prim_Op := Node (Op_Elmt);
2052 -- Search primitives that are implicit operations with an
2053 -- internal name whose parent operation has a normal name.
2055 if Present (Alias (Prim_Op))
2056 and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
2057 and then not Comes_From_Source (Prim_Op)
2058 and then Is_Internal_Name (Chars (Prim_Op))
2059 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
2060 then
2061 Parent_Subp := Alias (Prim_Op);
2063 -- Case 1: Check if the type has also an explicit
2064 -- overriding for this primitive.
2066 Op_Elmt_2 := Next_Elmt (Op_Elmt);
2067 while Present (Op_Elmt_2) loop
2069 -- Skip entities with attribute Interface_Alias since
2070 -- they are not overriding primitives (these entities
2071 -- link an interface primitive with their covering
2072 -- primitive)
2074 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
2075 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
2076 and then No (Interface_Alias (Node (Op_Elmt_2)))
2077 then
2078 -- The private inherited operation has been
2079 -- overridden by an explicit subprogram:
2080 -- replace the former by the latter.
2082 New_Op := Node (Op_Elmt_2);
2083 Replace_Elmt (Op_Elmt, New_Op);
2084 Remove_Elmt (Op_List, Op_Elmt_2);
2085 Set_Overridden_Operation (New_Op, Parent_Subp);
2086 Set_Is_Ada_2022_Only (New_Op,
2087 Is_Ada_2022_Only (Parent_Subp));
2089 -- We don't need to inherit its dispatching slot.
2090 -- Set_All_DT_Position has previously ensured that
2091 -- the same slot was assigned to the two primitives
2093 if Present (Tag)
2094 and then Present (DTC_Entity (New_Op))
2095 and then Present (DTC_Entity (Prim_Op))
2096 then
2097 pragma Assert
2098 (DT_Position (New_Op) = DT_Position (Prim_Op));
2099 null;
2100 end if;
2102 goto Next_Primitive;
2103 end if;
2105 Next_Elmt (Op_Elmt_2);
2106 end loop;
2108 -- Case 2: We have not found any explicit overriding and
2109 -- hence we need to declare the operation (i.e., make it
2110 -- visible).
2112 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2114 -- Inherit the dispatching slot if E is already frozen
2116 if Is_Frozen (E)
2117 and then Present (DTC_Entity (Alias (Prim_Op)))
2118 then
2119 Set_DTC_Entity_Value (E, New_Op);
2120 Set_DT_Position_Value (New_Op,
2121 DT_Position (Alias (Prim_Op)));
2122 end if;
2124 pragma Assert
2125 (Is_Dispatching_Operation (New_Op)
2126 and then Node (Last_Elmt (Op_List)) = New_Op);
2128 -- Substitute the new operation for the old one in the
2129 -- type's primitive operations list. Since the new
2130 -- operation was also just added to the end of list,
2131 -- the last element must be removed.
2133 -- (Question: is there a simpler way of declaring the
2134 -- operation, say by just replacing the name of the
2135 -- earlier operation, reentering it in the in the symbol
2136 -- table (how?), and marking it as private???)
2138 Replace_Elmt (Op_Elmt, New_Op);
2139 Remove_Last_Elmt (Op_List);
2140 end if;
2142 <<Next_Primitive>>
2143 Next_Elmt (Op_Elmt);
2144 end loop;
2146 -- Generate listing showing the contents of the dispatch table
2148 if Debug_Flag_ZZ then
2149 Write_DT (E);
2150 end if;
2152 else
2153 -- For untagged type, scan forward to locate inherited hidden
2154 -- operations.
2156 Prim_Op := Next_Entity (E);
2157 while Present (Prim_Op) loop
2158 if Is_Subprogram (Prim_Op)
2159 and then Present (Alias (Prim_Op))
2160 and then not Comes_From_Source (Prim_Op)
2161 and then Is_Internal_Name (Chars (Prim_Op))
2162 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
2163 and then Is_Primitive_Of (E, Prim_Op)
2164 then
2165 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2166 end if;
2168 Next_Entity (Prim_Op);
2170 -- Derived operations appear immediately after the type
2171 -- declaration (or the following subtype indication for
2172 -- a derived scalar type). Further declarations cannot
2173 -- include inherited operations of the type.
2175 if Present (Prim_Op) then
2176 exit when Ekind (Prim_Op) not in Overloadable_Kind;
2177 end if;
2178 end loop;
2179 end if;
2180 end if;
2182 Next_Entity (E);
2183 end loop;
2184 end Declare_Inherited_Private_Subprograms;
2186 -----------------------
2187 -- End_Package_Scope --
2188 -----------------------
2190 procedure End_Package_Scope (P : Entity_Id) is
2191 begin
2192 Uninstall_Declarations (P);
2193 Pop_Scope;
2194 end End_Package_Scope;
2196 ---------------------------
2197 -- Exchange_Declarations --
2198 ---------------------------
2200 procedure Exchange_Declarations (Id : Entity_Id) is
2201 Full_Id : constant Entity_Id := Full_View (Id);
2202 H1 : constant Entity_Id := Homonym (Id);
2203 Next1 : constant Entity_Id := Next_Entity (Id);
2204 H2 : Entity_Id;
2205 Next2 : Entity_Id;
2207 begin
2208 -- If missing full declaration for type, nothing to exchange
2210 if No (Full_Id) then
2211 return;
2212 end if;
2214 -- Otherwise complete the exchange, and preserve semantic links
2216 Next2 := Next_Entity (Full_Id);
2217 H2 := Homonym (Full_Id);
2219 -- Reset full declaration pointer to reflect the switched entities and
2220 -- readjust the next entity chains.
2222 Exchange_Entities (Id, Full_Id);
2224 Link_Entities (Id, Next1);
2225 Set_Homonym (Id, H1);
2227 Set_Full_View (Full_Id, Id);
2228 Link_Entities (Full_Id, Next2);
2229 Set_Homonym (Full_Id, H2);
2230 end Exchange_Declarations;
2232 ----------------------------
2233 -- Install_Package_Entity --
2234 ----------------------------
2236 procedure Install_Package_Entity (Id : Entity_Id) is
2237 begin
2238 if not Is_Internal (Id) then
2239 if Debug_Flag_E then
2240 Write_Str ("Install: ");
2241 Write_Name (Chars (Id));
2242 Write_Eol;
2243 end if;
2245 if Is_Child_Unit (Id) then
2246 null;
2248 -- Do not enter implicitly inherited non-overridden subprograms of
2249 -- a tagged type back into visibility if they have non-conformant
2250 -- homographs (Ada RM 8.3 12.3/2).
2252 elsif Is_Hidden_Non_Overridden_Subpgm (Id) then
2253 null;
2255 else
2256 Set_Is_Immediately_Visible (Id);
2257 end if;
2258 end if;
2259 end Install_Package_Entity;
2261 ----------------------------------
2262 -- Install_Private_Declarations --
2263 ----------------------------------
2265 procedure Install_Private_Declarations (P : Entity_Id) is
2266 Id : Entity_Id;
2267 Full : Entity_Id;
2268 Priv_Deps : Elist_Id;
2270 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
2271 -- When the full view of a private type is made available, we do the
2272 -- same for its private dependents under proper visibility conditions.
2273 -- When compiling a child unit this needs to be done recursively.
2275 -----------------------------
2276 -- Swap_Private_Dependents --
2277 -----------------------------
2279 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
2280 Cunit : Entity_Id;
2281 Deps : Elist_Id;
2282 Priv : Entity_Id;
2283 Priv_Elmt : Elmt_Id;
2284 Is_Priv : Boolean;
2286 begin
2287 Priv_Elmt := First_Elmt (Priv_Deps);
2288 while Present (Priv_Elmt) loop
2289 Priv := Node (Priv_Elmt);
2291 -- Before the exchange, verify that the presence of the Full_View
2292 -- field. This field will be empty if the entity has already been
2293 -- installed due to a previous call.
2295 if Present (Full_View (Priv)) and then Is_Visible_Dependent (Priv)
2296 then
2297 if Is_Private_Type (Priv) then
2298 Cunit := Cunit_Entity (Current_Sem_Unit);
2299 Deps := Private_Dependents (Priv);
2300 Is_Priv := True;
2301 else
2302 Is_Priv := False;
2303 end if;
2305 -- For each subtype that is swapped, we also swap the reference
2306 -- to it in Private_Dependents, to allow access to it when we
2307 -- swap them out in End_Package_Scope.
2309 Replace_Elmt (Priv_Elmt, Full_View (Priv));
2311 -- Ensure that both views of the dependent private subtype are
2312 -- immediately visible if within some open scope. Check full
2313 -- view before exchanging views.
2315 if In_Open_Scopes (Scope (Full_View (Priv))) then
2316 Set_Is_Immediately_Visible (Priv);
2317 end if;
2319 Exchange_Declarations (Priv);
2320 Set_Is_Immediately_Visible
2321 (Priv, In_Open_Scopes (Scope (Priv)));
2323 Set_Is_Potentially_Use_Visible
2324 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
2326 -- Recurse for child units, except in generic child units,
2327 -- which unfortunately handle private_dependents separately.
2328 -- Note that the current unit may not have been analyzed,
2329 -- for example a package body, so we cannot rely solely on
2330 -- the Is_Child_Unit flag, but that's only an optimization.
2332 if Is_Priv
2333 and then (No (Etype (Cunit)) or else Is_Child_Unit (Cunit))
2334 and then not Is_Empty_Elmt_List (Deps)
2335 and then not Inside_A_Generic
2336 then
2337 Swap_Private_Dependents (Deps);
2338 end if;
2339 end if;
2341 Next_Elmt (Priv_Elmt);
2342 end loop;
2343 end Swap_Private_Dependents;
2345 -- Start of processing for Install_Private_Declarations
2347 begin
2348 -- First exchange declarations for private types, so that the full
2349 -- declaration is visible. For each private type, we check its
2350 -- Private_Dependents list and also exchange any subtypes of or derived
2351 -- types from it. Finally, if this is a Taft amendment type, the
2352 -- incomplete declaration is irrelevant, and we want to link the
2353 -- eventual full declaration with the original private one so we
2354 -- also skip the exchange.
2356 Id := First_Entity (P);
2357 while Present (Id) and then Id /= First_Private_Entity (P) loop
2358 if Is_Private_Base_Type (Id)
2359 and then Present (Full_View (Id))
2360 and then Comes_From_Source (Full_View (Id))
2361 and then Scope (Full_View (Id)) = Scope (Id)
2362 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
2363 then
2364 -- If there is a use-type clause on the private type, set the full
2365 -- view accordingly.
2367 Set_In_Use (Full_View (Id), In_Use (Id));
2368 Full := Full_View (Id);
2370 if Is_Private_Base_Type (Full)
2371 and then Has_Private_Declaration (Full)
2372 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
2373 and then In_Open_Scopes (Scope (Etype (Full)))
2374 and then In_Package_Body (Current_Scope)
2375 and then not Is_Private_Type (Etype (Full))
2376 then
2377 -- This is the completion of a private type by a derivation
2378 -- from another private type which is not private anymore. This
2379 -- can only happen in a package nested within a child package,
2380 -- when the parent type is defined in the parent unit. At this
2381 -- point the current type is not private either, and we have
2382 -- to install the underlying full view, which is now visible.
2383 -- Save the current full view as well, so that all views can be
2384 -- restored on exit. It may seem that after compiling the child
2385 -- body there are not environments to restore, but the back-end
2386 -- expects those links to be valid, and freeze nodes depend on
2387 -- them.
2389 if No (Full_View (Full))
2390 and then Present (Underlying_Full_View (Full))
2391 then
2392 Set_Full_View (Id, Underlying_Full_View (Full));
2393 Set_Underlying_Full_View (Id, Full);
2394 Set_Is_Underlying_Full_View (Full);
2396 Set_Underlying_Full_View (Full, Empty);
2397 Set_Is_Frozen (Full_View (Id));
2398 end if;
2399 end if;
2401 Priv_Deps := Private_Dependents (Id);
2402 Exchange_Declarations (Id);
2403 Set_Is_Immediately_Visible (Id);
2404 Swap_Private_Dependents (Priv_Deps);
2405 end if;
2407 Next_Entity (Id);
2408 end loop;
2410 -- Next make other declarations in the private part visible as well
2412 Id := First_Private_Entity (P);
2413 while Present (Id) loop
2414 Install_Package_Entity (Id);
2415 Set_Is_Hidden (Id, False);
2416 Next_Entity (Id);
2417 end loop;
2419 -- An abstract state is partially refined when it has at least one
2420 -- Part_Of constituent. Since these constituents are being installed
2421 -- into visibility, update the partial refinement status of any state
2422 -- defined in the associated package, subject to at least one Part_Of
2423 -- constituent.
2425 if Is_Package_Or_Generic_Package (P) then
2426 declare
2427 States : constant Elist_Id := Abstract_States (P);
2428 State_Elmt : Elmt_Id;
2429 State_Id : Entity_Id;
2431 begin
2432 if Present (States) then
2433 State_Elmt := First_Elmt (States);
2434 while Present (State_Elmt) loop
2435 State_Id := Node (State_Elmt);
2437 if Present (Part_Of_Constituents (State_Id)) then
2438 Set_Has_Partial_Visible_Refinement (State_Id);
2439 end if;
2441 Next_Elmt (State_Elmt);
2442 end loop;
2443 end if;
2444 end;
2445 end if;
2447 -- Indicate that the private part is currently visible, so it can be
2448 -- properly reset on exit.
2450 Set_In_Private_Part (P);
2451 end Install_Private_Declarations;
2453 ----------------------------------
2454 -- Install_Visible_Declarations --
2455 ----------------------------------
2457 procedure Install_Visible_Declarations (P : Entity_Id) is
2458 Id : Entity_Id;
2459 Last_Entity : Entity_Id;
2461 begin
2462 pragma Assert
2463 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
2465 if Is_Package_Or_Generic_Package (P) then
2466 Last_Entity := First_Private_Entity (P);
2467 else
2468 Last_Entity := Empty;
2469 end if;
2471 Id := First_Entity (P);
2472 while Present (Id) and then Id /= Last_Entity loop
2473 Install_Package_Entity (Id);
2474 Next_Entity (Id);
2475 end loop;
2476 end Install_Visible_Declarations;
2478 --------------------------
2479 -- Is_Private_Base_Type --
2480 --------------------------
2482 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
2483 begin
2484 return Ekind (E) = E_Private_Type
2485 or else Ekind (E) = E_Limited_Private_Type
2486 or else Ekind (E) = E_Record_Type_With_Private;
2487 end Is_Private_Base_Type;
2489 --------------------------
2490 -- Is_Visible_Dependent --
2491 --------------------------
2493 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
2495 S : constant Entity_Id := Scope (Dep);
2497 begin
2498 -- Renamings created for actual types have the visibility of the actual
2500 if Ekind (S) = E_Package
2501 and then Is_Generic_Instance (S)
2502 and then (Is_Generic_Actual_Type (Dep)
2503 or else Is_Generic_Actual_Type (Full_View (Dep)))
2504 then
2505 return True;
2507 elsif not (Is_Derived_Type (Dep))
2508 and then Is_Derived_Type (Full_View (Dep))
2509 then
2510 -- When instantiating a package body, the scope stack is empty, so
2511 -- check instead whether the dependent type is defined in the same
2512 -- scope as the instance itself.
2514 return In_Open_Scopes (S)
2515 or else (Is_Generic_Instance (Current_Scope)
2516 and then Scope (Dep) = Scope (Current_Scope));
2517 else
2518 return True;
2519 end if;
2520 end Is_Visible_Dependent;
2522 ----------------------------
2523 -- May_Need_Implicit_Body --
2524 ----------------------------
2526 procedure May_Need_Implicit_Body (E : Entity_Id) is
2527 P : constant Node_Id := Unit_Declaration_Node (E);
2528 S : constant Node_Id := Parent (P);
2529 B : Node_Id;
2530 Decls : List_Id;
2532 begin
2533 if not Has_Completion (E)
2534 and then Nkind (P) = N_Package_Declaration
2535 and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
2536 then
2537 B :=
2538 Make_Package_Body (Sloc (E),
2539 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
2540 Chars => Chars (E)),
2541 Declarations => New_List);
2543 if Nkind (S) = N_Package_Specification then
2544 if Present (Private_Declarations (S)) then
2545 Decls := Private_Declarations (S);
2546 else
2547 Decls := Visible_Declarations (S);
2548 end if;
2549 else
2550 Decls := Declarations (S);
2551 end if;
2553 Append (B, Decls);
2554 Analyze (B);
2555 end if;
2556 end May_Need_Implicit_Body;
2558 ----------------------
2559 -- New_Private_Type --
2560 ----------------------
2562 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
2563 begin
2564 -- For other than Ada 2012, enter the name in the current scope
2566 if Ada_Version < Ada_2012 then
2567 Enter_Name (Id);
2569 -- Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2570 -- there may be an incomplete previous view.
2572 else
2573 declare
2574 Prev : Entity_Id;
2575 begin
2576 Prev := Find_Type_Name (N);
2577 pragma Assert (Prev = Id
2578 or else (Ekind (Prev) = E_Incomplete_Type
2579 and then Present (Full_View (Prev))
2580 and then Full_View (Prev) = Id));
2581 end;
2582 end if;
2584 if Limited_Present (Def) then
2585 Mutate_Ekind (Id, E_Limited_Private_Type);
2586 else
2587 Mutate_Ekind (Id, E_Private_Type);
2588 end if;
2590 Set_Etype (Id, Id);
2591 Set_Has_Delayed_Freeze (Id);
2592 Set_Is_First_Subtype (Id);
2593 Reinit_Size_Align (Id);
2595 Set_Is_Constrained (Id,
2596 No (Discriminant_Specifications (N))
2597 and then not Unknown_Discriminants_Present (N));
2599 -- Set tagged flag before processing discriminants, to catch illegal
2600 -- usage.
2602 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
2604 Set_Discriminant_Constraint (Id, No_Elist);
2605 Set_Stored_Constraint (Id, No_Elist);
2607 if Present (Discriminant_Specifications (N)) then
2608 Push_Scope (Id);
2609 Process_Discriminants (N);
2610 End_Scope;
2612 elsif Unknown_Discriminants_Present (N) then
2613 Set_Has_Unknown_Discriminants (Id);
2614 end if;
2616 Set_Private_Dependents (Id, New_Elmt_List);
2618 if Tagged_Present (Def) then
2619 Mutate_Ekind (Id, E_Record_Type_With_Private);
2620 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2621 Set_Is_Abstract_Type (Id, Abstract_Present (Def));
2622 Set_Is_Limited_Record (Id, Limited_Present (Def));
2623 Set_Has_Delayed_Freeze (Id, True);
2625 -- Recognize Ada.Real_Time.Timing_Events.Timing_Events here
2627 if Is_RTE (Id, RE_Timing_Event) then
2628 Set_Has_Timing_Event (Id);
2629 end if;
2631 -- Create a class-wide type with the same attributes
2633 Make_Class_Wide_Type (Id);
2635 elsif Abstract_Present (Def) then
2636 Error_Msg_N ("only a tagged type can be abstract", N);
2638 -- We initialize the primitive operations list of an untagged private
2639 -- type to an empty element list. Do this even when Extensions_Allowed
2640 -- is False to issue better error messages. (Note: This could be done
2641 -- for all private types and shared with the tagged case above, but
2642 -- for now we do it separately.)
2644 else
2645 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2646 end if;
2647 end New_Private_Type;
2649 ---------------------------------
2650 -- Requires_Completion_In_Body --
2651 ---------------------------------
2653 function Requires_Completion_In_Body
2654 (Id : Entity_Id;
2655 Pack_Id : Entity_Id;
2656 Do_Abstract_States : Boolean := False) return Boolean
2658 begin
2659 -- Always ignore child units. Child units get added to the entity list
2660 -- of a parent unit, but are not original entities of the parent, and
2661 -- so do not affect whether the parent needs a body.
2663 if Is_Child_Unit (Id) then
2664 return False;
2666 -- Ignore formal packages and their renamings
2668 elsif Ekind (Id) = E_Package
2669 and then Nkind (Original_Node (Unit_Declaration_Node (Id))) =
2670 N_Formal_Package_Declaration
2671 then
2672 return False;
2674 -- Otherwise test to see if entity requires a completion. Note that
2675 -- subprogram entities whose declaration does not come from source are
2676 -- ignored here on the basis that we assume the expander will provide an
2677 -- implicit completion at some point.
2679 elsif (Is_Overloadable (Id)
2680 and then Ekind (Id) not in E_Enumeration_Literal | E_Operator
2681 and then not Is_Abstract_Subprogram (Id)
2682 and then not Has_Completion (Id)
2683 and then Comes_From_Source (Parent (Id)))
2685 or else
2686 (Ekind (Id) = E_Package
2687 and then Id /= Pack_Id
2688 and then not Has_Completion (Id)
2689 and then Unit_Requires_Body (Id, Do_Abstract_States))
2691 or else
2692 (Ekind (Id) = E_Incomplete_Type
2693 and then No (Full_View (Id))
2694 and then not Is_Generic_Type (Id))
2696 or else
2697 (Ekind (Id) in E_Task_Type | E_Protected_Type
2698 and then not Has_Completion (Id))
2700 or else
2701 (Ekind (Id) = E_Generic_Package
2702 and then Id /= Pack_Id
2703 and then not Has_Completion (Id)
2704 and then Unit_Requires_Body (Id, Do_Abstract_States))
2706 or else
2707 (Is_Generic_Subprogram (Id)
2708 and then not Has_Completion (Id))
2709 then
2710 return True;
2712 -- Otherwise the entity does not require completion in a package body
2714 else
2715 return False;
2716 end if;
2717 end Requires_Completion_In_Body;
2719 ----------------------------
2720 -- Uninstall_Declarations --
2721 ----------------------------
2723 procedure Uninstall_Declarations (P : Entity_Id) is
2724 Decl : constant Node_Id := Unit_Declaration_Node (P);
2725 Id : Entity_Id;
2726 Full : Entity_Id;
2728 procedure Preserve_Full_Attributes (Priv : Entity_Id; Full : Entity_Id);
2729 -- Copy to the private declaration the attributes of the full view that
2730 -- need to be available for the partial view also.
2732 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
2733 -- When the full view of a private type is made unavailable, we do the
2734 -- same for its private dependents under proper visibility conditions.
2735 -- When compiling a child unit this needs to be done recursively.
2737 function Type_In_Use (T : Entity_Id) return Boolean;
2738 -- Check whether type or base type appear in an active use_type clause
2740 ------------------------------
2741 -- Preserve_Full_Attributes --
2742 ------------------------------
2744 procedure Preserve_Full_Attributes
2745 (Priv : Entity_Id;
2746 Full : Entity_Id)
2748 Full_Base : constant Entity_Id := Base_Type (Full);
2749 Priv_Is_Base_Type : constant Boolean := Is_Base_Type (Priv);
2751 begin
2752 Set_Size_Info (Priv, Full);
2753 Copy_RM_Size (To => Priv, From => Full);
2754 Set_Size_Known_At_Compile_Time
2755 (Priv, Size_Known_At_Compile_Time (Full));
2756 Set_Is_Volatile (Priv, Is_Volatile (Full));
2757 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
2758 Set_Is_Atomic (Priv, Is_Atomic (Full));
2759 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full));
2760 Set_Is_Ada_2012_Only (Priv, Is_Ada_2012_Only (Full));
2761 Set_Is_Ada_2022_Only (Priv, Is_Ada_2022_Only (Full));
2762 Set_Has_Pragma_Unmodified (Priv, Has_Pragma_Unmodified (Full));
2763 Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced (Full));
2764 Set_Has_Pragma_Unreferenced_Objects
2765 (Priv, Has_Pragma_Unreferenced_Objects
2766 (Full));
2767 Set_Predicates_Ignored (Priv, Predicates_Ignored (Full));
2768 if Is_Unchecked_Union (Full) then
2769 Set_Is_Unchecked_Union (Base_Type (Priv));
2770 end if;
2772 if Referenced (Full) then
2773 Set_Referenced (Priv);
2774 end if;
2776 if Priv_Is_Base_Type then
2777 Set_Is_Controlled_Active
2778 (Priv, Is_Controlled_Active (Full_Base));
2779 Set_Finalize_Storage_Only
2780 (Priv, Finalize_Storage_Only (Full_Base));
2781 Set_Has_Controlled_Component
2782 (Priv, Has_Controlled_Component (Full_Base));
2784 Propagate_Concurrent_Flags (Priv, Base_Type (Full));
2785 end if;
2787 -- As explained in Freeze_Entity, private types are required to point
2788 -- to the same freeze node as their corresponding full view, if any.
2789 -- But we ought not to overwrite a node already inserted in the tree.
2791 pragma Assert
2792 (Serious_Errors_Detected /= 0
2793 or else No (Freeze_Node (Priv))
2794 or else No (Parent (Freeze_Node (Priv)))
2795 or else Freeze_Node (Priv) = Freeze_Node (Full));
2797 Set_Freeze_Node (Priv, Freeze_Node (Full));
2799 -- Propagate Default_Initial_Condition-related attributes from the
2800 -- full view to the private view.
2802 Propagate_DIC_Attributes (Priv, From_Typ => Full);
2804 -- Propagate invariant-related attributes from the full view to the
2805 -- private view.
2807 Propagate_Invariant_Attributes (Priv, From_Typ => Full);
2809 -- Propagate predicate-related attributes from the full view to the
2810 -- private view.
2812 Propagate_Predicate_Attributes (Priv, From_Typ => Full);
2814 if Is_Tagged_Type (Priv)
2815 and then Is_Tagged_Type (Full)
2816 and then not Error_Posted (Full)
2817 then
2818 if Is_Tagged_Type (Priv) then
2820 -- If the type is tagged, the tag itself must be available on
2821 -- the partial view, for expansion purposes.
2823 Set_First_Entity (Priv, First_Entity (Full));
2825 -- If there are discriminants in the partial view, these remain
2826 -- visible. Otherwise only the tag itself is visible, and there
2827 -- are no nameable components in the partial view.
2829 if No (Last_Entity (Priv)) then
2830 Set_Last_Entity (Priv, First_Entity (Priv));
2831 end if;
2832 end if;
2834 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2836 if Has_Discriminants (Full) then
2837 Set_Discriminant_Constraint (Priv,
2838 Discriminant_Constraint (Full));
2839 end if;
2840 end if;
2841 end Preserve_Full_Attributes;
2843 -----------------------------
2844 -- Swap_Private_Dependents --
2845 -----------------------------
2847 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
2848 Cunit : Entity_Id;
2849 Deps : Elist_Id;
2850 Priv : Entity_Id;
2851 Priv_Elmt : Elmt_Id;
2852 Is_Priv : Boolean;
2854 begin
2855 Priv_Elmt := First_Elmt (Priv_Deps);
2856 while Present (Priv_Elmt) loop
2857 Priv := Node (Priv_Elmt);
2859 -- Before we do the swap, we verify the presence of the Full_View
2860 -- field, which may be empty due to a swap by a previous call to
2861 -- End_Package_Scope (e.g. from the freezing mechanism).
2863 if Present (Full_View (Priv)) then
2864 if Is_Private_Type (Priv) then
2865 Cunit := Cunit_Entity (Current_Sem_Unit);
2866 Deps := Private_Dependents (Priv);
2867 Is_Priv := True;
2868 else
2869 Is_Priv := False;
2870 end if;
2872 if Scope (Priv) = P
2873 or else not In_Open_Scopes (Scope (Priv))
2874 then
2875 Set_Is_Immediately_Visible (Priv, False);
2876 end if;
2878 if Is_Visible_Dependent (Priv) then
2879 Preserve_Full_Attributes (Priv, Full_View (Priv));
2880 Replace_Elmt (Priv_Elmt, Full_View (Priv));
2881 Exchange_Declarations (Priv);
2883 -- Recurse for child units, except in generic child units,
2884 -- which unfortunately handle private_dependents separately.
2885 -- Note that the current unit may not have been analyzed,
2886 -- for example a package body, so we cannot rely solely on
2887 -- the Is_Child_Unit flag, but that's only an optimization.
2889 if Is_Priv
2890 and then (No (Etype (Cunit)) or else Is_Child_Unit (Cunit))
2891 and then not Is_Empty_Elmt_List (Deps)
2892 and then not Inside_A_Generic
2893 then
2894 Swap_Private_Dependents (Deps);
2895 end if;
2896 end if;
2897 end if;
2899 Next_Elmt (Priv_Elmt);
2900 end loop;
2901 end Swap_Private_Dependents;
2903 -----------------
2904 -- Type_In_Use --
2905 -----------------
2907 function Type_In_Use (T : Entity_Id) return Boolean is
2908 begin
2909 return Scope (Base_Type (T)) = P
2910 and then (In_Use (T) or else In_Use (Base_Type (T)));
2911 end Type_In_Use;
2913 -- Start of processing for Uninstall_Declarations
2915 begin
2916 Id := First_Entity (P);
2917 while Present (Id) and then Id /= First_Private_Entity (P) loop
2918 if Debug_Flag_E then
2919 Write_Str ("unlinking visible entity ");
2920 Write_Int (Int (Id));
2921 Write_Eol;
2922 end if;
2924 -- On exit from the package scope, we must preserve the visibility
2925 -- established by use clauses in the current scope. Two cases:
2927 -- a) If the entity is an operator, it may be a primitive operator of
2928 -- a type for which there is a visible use-type clause.
2930 -- b) For other entities, their use-visibility is determined by a
2931 -- visible use clause for the package itself or a use-all-type clause
2932 -- applied directly to the entity's type. For a generic instance,
2933 -- the instantiation of the formals appears in the visible part,
2934 -- but the formals are private and remain so.
2936 if Ekind (Id) = E_Function
2937 and then Is_Operator_Symbol_Name (Chars (Id))
2938 and then not Is_Hidden (Id)
2939 and then not Error_Posted (Id)
2940 then
2941 Set_Is_Potentially_Use_Visible (Id,
2942 In_Use (P)
2943 or else Type_In_Use (Etype (Id))
2944 or else Type_In_Use (Etype (First_Formal (Id)))
2945 or else (Present (Next_Formal (First_Formal (Id)))
2946 and then
2947 Type_In_Use
2948 (Etype (Next_Formal (First_Formal (Id))))));
2949 else
2950 if In_Use (P) and then not Is_Hidden (Id) then
2952 -- A child unit of a use-visible package remains use-visible
2953 -- only if it is itself a visible child unit. Otherwise it
2954 -- would remain visible in other contexts where P is use-
2955 -- visible, because once compiled it stays in the entity list
2956 -- of its parent unit.
2958 if Is_Child_Unit (Id) then
2959 Set_Is_Potentially_Use_Visible
2960 (Id, Is_Visible_Lib_Unit (Id));
2961 else
2962 Set_Is_Potentially_Use_Visible (Id);
2963 end if;
2965 -- Avoid crash caused by previous errors
2967 elsif No (Etype (Id)) and then Serious_Errors_Detected /= 0 then
2968 null;
2970 -- We need to avoid incorrectly marking enumeration literals as
2971 -- non-visible when a visible use-all-type clause is in effect.
2973 elsif Type_In_Use (Etype (Id))
2974 and then Nkind (Current_Use_Clause (Etype (Id))) =
2975 N_Use_Type_Clause
2976 and then All_Present (Current_Use_Clause (Etype (Id)))
2977 then
2978 null;
2980 else
2981 Set_Is_Potentially_Use_Visible (Id, False);
2982 end if;
2983 end if;
2985 -- Local entities are not immediately visible outside of the package
2987 Set_Is_Immediately_Visible (Id, False);
2989 -- If this is a private type with a full view (for example a local
2990 -- subtype of a private type declared elsewhere), ensure that the
2991 -- full view is also removed from visibility: it may be exposed when
2992 -- swapping views in an instantiation. Similarly, ensure that the
2993 -- use-visibility is properly set on both views.
2995 if Is_Type (Id) and then Present (Full_View (Id)) then
2996 Set_Is_Immediately_Visible (Full_View (Id), False);
2997 Set_Is_Potentially_Use_Visible (Full_View (Id),
2998 Is_Potentially_Use_Visible (Id));
2999 end if;
3001 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
3002 Check_Abstract_Overriding (Id);
3003 Check_Conventions (Id);
3004 end if;
3006 if Ekind (Id) in E_Private_Type | E_Limited_Private_Type
3007 and then No (Full_View (Id))
3008 and then not Is_Generic_Type (Id)
3009 and then not Is_Derived_Type (Id)
3010 then
3011 Error_Msg_N ("missing full declaration for private type&", Id);
3013 elsif Ekind (Id) = E_Record_Type_With_Private
3014 and then not Is_Generic_Type (Id)
3015 and then No (Full_View (Id))
3016 then
3017 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
3018 Error_Msg_N ("missing full declaration for private type&", Id);
3019 else
3020 Error_Msg_N
3021 ("missing full declaration for private extension", Id);
3022 end if;
3024 -- Case of constant, check for deferred constant declaration with
3025 -- no full view. Likely just a matter of a missing expression, or
3026 -- accidental use of the keyword constant.
3028 elsif Ekind (Id) = E_Constant
3030 -- OK if constant value present
3032 and then No (Constant_Value (Id))
3034 -- OK if full view present
3036 and then No (Full_View (Id))
3038 -- OK if imported, since that provides the completion
3040 and then not Is_Imported (Id)
3042 -- OK if object declaration replaced by renaming declaration as
3043 -- a result of OK_To_Rename processing (e.g. for concatenation)
3045 and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration
3047 -- OK if object declaration with the No_Initialization flag set
3049 and then not (Nkind (Parent (Id)) = N_Object_Declaration
3050 and then No_Initialization (Parent (Id)))
3051 then
3052 -- If no private declaration is present, we assume the user did
3053 -- not intend a deferred constant declaration and the problem
3054 -- is simply that the initializing expression is missing.
3056 if not Has_Private_Declaration (Etype (Id)) then
3058 -- We assume that the user did not intend a deferred constant
3059 -- declaration, and the expression is just missing.
3061 Error_Msg_N
3062 ("constant declaration requires initialization expression",
3063 Parent (Id));
3065 if Is_Limited_Type (Etype (Id)) then
3066 Error_Msg_N
3067 ("\if variable intended, remove CONSTANT from declaration",
3068 Parent (Id));
3069 end if;
3071 -- Otherwise if a private declaration is present, then we are
3072 -- missing the full declaration for the deferred constant.
3074 else
3075 Error_Msg_N
3076 ("missing full declaration for deferred constant (RM 7.4)",
3077 Id);
3079 if Is_Limited_Type (Etype (Id)) then
3080 Error_Msg_N
3081 ("\if variable intended, remove CONSTANT from declaration",
3082 Parent (Id));
3083 end if;
3084 end if;
3085 end if;
3087 Next_Entity (Id);
3088 end loop;
3090 -- If the specification was installed as the parent of a public child
3091 -- unit, the private declarations were not installed, and there is
3092 -- nothing to do.
3094 if not In_Private_Part (P) then
3095 return;
3096 else
3097 Set_In_Private_Part (P, False);
3098 end if;
3100 -- Make private entities invisible and exchange full and private
3101 -- declarations for private types. Id is now the first private entity
3102 -- in the package.
3104 while Present (Id) loop
3105 if Debug_Flag_E then
3106 Write_Str ("unlinking private entity ");
3107 Write_Int (Int (Id));
3108 Write_Eol;
3109 end if;
3111 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
3112 Check_Abstract_Overriding (Id);
3113 Check_Conventions (Id);
3114 end if;
3116 Set_Is_Immediately_Visible (Id, False);
3118 if Is_Private_Base_Type (Id) and then Present (Full_View (Id)) then
3119 Full := Full_View (Id);
3121 -- If the partial view is not declared in the visible part of the
3122 -- package (as is the case when it is a type derived from some
3123 -- other private type in the private part of the current package),
3124 -- no exchange takes place.
3126 if No (Parent (Id))
3127 or else List_Containing (Parent (Id)) /=
3128 Visible_Declarations (Specification (Decl))
3129 then
3130 goto Next_Id;
3131 end if;
3133 -- The entry in the private part points to the full declaration,
3134 -- which is currently visible. Exchange them so only the private
3135 -- type declaration remains accessible, and link private and full
3136 -- declaration in the opposite direction. Before the actual
3137 -- exchange, we copy back attributes of the full view that must
3138 -- be available to the partial view too.
3140 Preserve_Full_Attributes (Id, Full);
3142 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
3144 -- The following test may be redundant, as this is already
3145 -- diagnosed in sem_ch3. ???
3147 if not Is_Definite_Subtype (Full)
3148 and then Is_Definite_Subtype (Id)
3149 then
3150 Error_Msg_Sloc := Sloc (Parent (Id));
3151 Error_Msg_NE
3152 ("full view of& not compatible with declaration#", Full, Id);
3153 end if;
3155 -- Swap out the subtypes and derived types of Id that
3156 -- were compiled in this scope, or installed previously
3157 -- by Install_Private_Declarations.
3159 Swap_Private_Dependents (Private_Dependents (Id));
3161 -- Now restore the type itself to its private view
3163 Exchange_Declarations (Id);
3165 -- If we have installed an underlying full view for a type derived
3166 -- from a private type in a child unit, restore the proper views
3167 -- of private and full view. See corresponding code in
3168 -- Install_Private_Declarations.
3170 -- After the exchange, Full denotes the private type in the
3171 -- visible part of the package.
3173 if Is_Private_Base_Type (Full)
3174 and then Present (Full_View (Full))
3175 and then Present (Underlying_Full_View (Full))
3176 and then In_Package_Body (Current_Scope)
3177 then
3178 Set_Full_View (Full, Underlying_Full_View (Full));
3179 Set_Underlying_Full_View (Full, Empty);
3180 end if;
3182 elsif Ekind (Id) = E_Incomplete_Type
3183 and then Comes_From_Source (Id)
3184 and then No (Full_View (Id))
3185 then
3186 -- Mark Taft amendment types. Verify that there are no primitive
3187 -- operations declared for the type (3.10.1(9)).
3189 Set_Has_Completion_In_Body (Id);
3191 declare
3192 Elmt : Elmt_Id;
3193 Subp : Entity_Id;
3195 begin
3196 Elmt := First_Elmt (Private_Dependents (Id));
3197 while Present (Elmt) loop
3198 Subp := Node (Elmt);
3200 -- Is_Primitive is tested because there can be cases where
3201 -- nonprimitive subprograms (in nested packages) are added
3202 -- to the Private_Dependents list.
3204 if Is_Overloadable (Subp) and then Is_Primitive (Subp) then
3205 Error_Msg_NE
3206 ("type& must be completed in the private part",
3207 Parent (Subp), Id);
3209 -- The result type of an access-to-function type cannot be a
3210 -- Taft-amendment type, unless the version is Ada 2012 or
3211 -- later (see AI05-151).
3213 elsif Ada_Version < Ada_2012
3214 and then Ekind (Subp) = E_Subprogram_Type
3215 then
3216 if Etype (Subp) = Id
3217 or else
3218 (Is_Class_Wide_Type (Etype (Subp))
3219 and then Etype (Etype (Subp)) = Id)
3220 then
3221 Error_Msg_NE
3222 ("type& must be completed in the private part",
3223 Associated_Node_For_Itype (Subp), Id);
3224 end if;
3225 end if;
3227 Next_Elmt (Elmt);
3228 end loop;
3229 end;
3231 -- For subtypes of private types the frontend generates two entities:
3232 -- one associated with the partial view and the other associated with
3233 -- the full view. When the subtype declaration is public the frontend
3234 -- places the former entity in the list of public entities of the
3235 -- package and the latter entity in the private part of the package.
3236 -- When the subtype declaration is private it generates these two
3237 -- entities but both are placed in the private part of the package
3238 -- (and the full view has the same source location as the partial
3239 -- view and no parent; see Prepare_Private_Subtype_Completion).
3241 elsif Ekind (Id) in E_Private_Subtype
3242 | E_Limited_Private_Subtype
3243 and then Present (Full_View (Id))
3244 and then Sloc (Id) = Sloc (Full_View (Id))
3245 and then No (Parent (Full_View (Id)))
3246 then
3247 Set_Is_Hidden (Id);
3248 Set_Is_Potentially_Use_Visible (Id, False);
3250 elsif not Is_Child_Unit (Id)
3251 and then (not Is_Private_Type (Id) or else No (Full_View (Id)))
3252 then
3253 Set_Is_Hidden (Id);
3254 Set_Is_Potentially_Use_Visible (Id, False);
3255 end if;
3257 <<Next_Id>>
3258 Next_Entity (Id);
3259 end loop;
3260 end Uninstall_Declarations;
3262 ------------------------
3263 -- Unit_Requires_Body --
3264 ------------------------
3266 function Unit_Requires_Body
3267 (Pack_Id : Entity_Id;
3268 Do_Abstract_States : Boolean := False) return Boolean
3270 E : Entity_Id;
3272 Requires_Body : Boolean := False;
3273 -- Flag set when the unit has at least one construct that requires
3274 -- completion in a body.
3276 begin
3277 -- Imported entity never requires body. Right now, only subprograms can
3278 -- be imported, but perhaps in the future we will allow import of
3279 -- packages.
3281 if Is_Imported (Pack_Id) then
3282 return False;
3284 -- Body required if library package with pragma Elaborate_Body
3286 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3287 return True;
3289 -- Body required if subprogram
3291 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3292 return True;
3294 -- Treat a block as requiring a body
3296 elsif Ekind (Pack_Id) = E_Block then
3297 return True;
3299 elsif Ekind (Pack_Id) = E_Package
3300 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3301 and then Present (Generic_Parent (Parent (Pack_Id)))
3302 then
3303 declare
3304 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3305 begin
3306 if Has_Pragma_Elaborate_Body (G_P) then
3307 return True;
3308 end if;
3309 end;
3310 end if;
3312 -- Traverse the entity chain of the package and look for constructs that
3313 -- require a completion in a body.
3315 E := First_Entity (Pack_Id);
3316 while Present (E) loop
3318 -- Skip abstract states because their completion depends on several
3319 -- criteria (see below).
3321 if Ekind (E) = E_Abstract_State then
3322 null;
3324 elsif Requires_Completion_In_Body
3325 (E, Pack_Id, Do_Abstract_States)
3326 then
3327 Requires_Body := True;
3328 exit;
3329 end if;
3331 Next_Entity (E);
3332 end loop;
3334 -- A [generic] package that defines at least one non-null abstract state
3335 -- requires a completion only when at least one other construct requires
3336 -- a completion in a body (SPARK RM 7.1.4(4) and (5)). This check is not
3337 -- performed if the caller requests this behavior.
3339 if Do_Abstract_States
3340 and then Is_Package_Or_Generic_Package (Pack_Id)
3341 and then Has_Non_Null_Abstract_State (Pack_Id)
3342 and then Requires_Body
3343 then
3344 return True;
3345 end if;
3347 return Requires_Body;
3348 end Unit_Requires_Body;
3350 -----------------------------
3351 -- Unit_Requires_Body_Info --
3352 -----------------------------
3354 procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id) is
3355 E : Entity_Id;
3357 begin
3358 -- An imported entity never requires body. Right now, only subprograms
3359 -- can be imported, but perhaps in the future we will allow import of
3360 -- packages.
3362 if Is_Imported (Pack_Id) then
3363 return;
3365 -- Body required if library package with pragma Elaborate_Body
3367 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3368 Error_Msg_N ("info: & requires body (Elaborate_Body)?.y?", Pack_Id);
3370 -- Body required if subprogram
3372 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3373 Error_Msg_N ("info: & requires body (subprogram case)?.y?", Pack_Id);
3375 -- Body required if generic parent has Elaborate_Body
3377 elsif Ekind (Pack_Id) = E_Package
3378 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3379 and then Present (Generic_Parent (Parent (Pack_Id)))
3380 then
3381 declare
3382 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3383 begin
3384 if Has_Pragma_Elaborate_Body (G_P) then
3385 Error_Msg_N
3386 ("info: & requires body (generic parent Elaborate_Body)?.y?",
3387 Pack_Id);
3388 end if;
3389 end;
3391 -- A [generic] package that introduces at least one non-null abstract
3392 -- state requires completion. However, there is a separate rule that
3393 -- requires that such a package have a reason other than this for a
3394 -- body being required (if necessary a pragma Elaborate_Body must be
3395 -- provided). If Ignore_Abstract_State is True, we don't do this check
3396 -- (so we can use Unit_Requires_Body to check for some other reason).
3398 elsif Is_Package_Or_Generic_Package (Pack_Id)
3399 and then Present (Abstract_States (Pack_Id))
3400 and then not Is_Null_State
3401 (Node (First_Elmt (Abstract_States (Pack_Id))))
3402 then
3403 Error_Msg_N
3404 ("info: & requires body (non-null abstract state aspect)?.y?",
3405 Pack_Id);
3406 end if;
3408 -- Otherwise search entity chain for entity requiring completion
3410 E := First_Entity (Pack_Id);
3411 while Present (E) loop
3412 if Requires_Completion_In_Body (E, Pack_Id) then
3413 Error_Msg_Node_2 := E;
3414 Error_Msg_NE
3415 ("info: & requires body (& requires completion)?.y?", E,
3416 Pack_Id);
3417 end if;
3419 Next_Entity (E);
3420 end loop;
3421 end Unit_Requires_Body_Info;
3423 end Sem_Ch7;