Fix date
[official-gcc.git] / gcc / ada / sem_ch7.adb
blob7b0761b8200e66a9e4ef7955acdc5264b70ab78e
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-2017, 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 Elists; use Elists;
37 with Errout; use Errout;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Dist; use Exp_Dist;
40 with Exp_Dbug; use Exp_Dbug;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Lib; use Lib;
44 with Lib.Xref; use Lib.Xref;
45 with Namet; use Namet;
46 with Nmake; use Nmake;
47 with Nlists; use Nlists;
48 with Opt; use Opt;
49 with Output; use Output;
50 with Restrict; use Restrict;
51 with Rtsfind; use Rtsfind;
52 with Sem; use Sem;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Cat; use Sem_Cat;
55 with Sem_Ch3; use Sem_Ch3;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch8; use Sem_Ch8;
58 with Sem_Ch10; use Sem_Ch10;
59 with Sem_Ch12; use Sem_Ch12;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Disp; use Sem_Disp;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Prag; use Sem_Prag;
64 with Sem_Util; use Sem_Util;
65 with Sem_Warn; use Sem_Warn;
66 with Snames; use Snames;
67 with Stand; use Stand;
68 with Sinfo; use Sinfo;
69 with Sinput; use Sinput;
70 with Style;
71 with Uintp; use Uintp;
73 with GNAT.HTable;
75 package body Sem_Ch7 is
77 -----------------------------------
78 -- Handling private declarations --
79 -----------------------------------
81 -- The principle that each entity has a single defining occurrence clashes
82 -- with the presence of two separate definitions for private types: the
83 -- first is the private type declaration, and the second is the full type
84 -- declaration. It is important that all references to the type point to
85 -- the same defining occurrence, namely the first one. To enforce the two
86 -- separate views of the entity, the corresponding information is swapped
87 -- between the two declarations. Outside of the package, the defining
88 -- occurrence only contains the private declaration information, while in
89 -- the private part and the body of the package the defining occurrence
90 -- contains the full declaration. To simplify the swap, the defining
91 -- occurrence that currently holds the private declaration points to the
92 -- full declaration. During semantic processing the defining occurrence
93 -- also points to a list of private dependents, that is to say access types
94 -- or composite types whose designated types or component types are
95 -- subtypes or derived types of the private type in question. After the
96 -- full declaration has been seen, the private dependents are updated to
97 -- indicate that they have full definitions.
99 -----------------------
100 -- Local Subprograms --
101 -----------------------
103 procedure Analyze_Package_Body_Helper (N : Node_Id);
104 -- Does all the real work of Analyze_Package_Body
106 procedure Check_Anonymous_Access_Types
107 (Spec_Id : Entity_Id;
108 P_Body : Node_Id);
109 -- If the spec of a package has a limited_with_clause, it may declare
110 -- anonymous access types whose designated type is a limited view, such an
111 -- anonymous access return type for a function. This access type cannot be
112 -- elaborated in the spec itself, but it may need an itype reference if it
113 -- is used within a nested scope. In that case the itype reference is
114 -- created at the beginning of the corresponding package body and inserted
115 -- before other body declarations.
117 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id);
118 -- Called upon entering the private part of a public child package and the
119 -- body of a nested package, to potentially declare certain inherited
120 -- subprograms that were inherited by types in the visible part, but whose
121 -- declaration was deferred because the parent operation was private and
122 -- not visible at that point. These subprograms are located by traversing
123 -- the visible part declarations looking for non-private type extensions
124 -- and then examining each of the primitive operations of such types to
125 -- find those that were inherited but declared with a special internal
126 -- name. Each such operation is now declared as an operation with a normal
127 -- name (using the name of the parent operation) and replaces the previous
128 -- implicit operation in the primitive operations list of the type. If the
129 -- inherited private operation has been overridden, then it's replaced by
130 -- the overriding operation.
132 procedure Install_Package_Entity (Id : Entity_Id);
133 -- Supporting procedure for Install_{Visible,Private}_Declarations. Places
134 -- one entity on its visibility chain, and recurses on the visible part if
135 -- the entity is an inner package.
137 function Is_Private_Base_Type (E : Entity_Id) return Boolean;
138 -- True for a private type that is not a subtype
140 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean;
141 -- If the private dependent is a private type whose full view is derived
142 -- from the parent type, its full properties are revealed only if we are in
143 -- the immediate scope of the private dependent. Should this predicate be
144 -- tightened further???
146 function Requires_Completion_In_Body
147 (Id : Entity_Id;
148 Pack_Id : Entity_Id;
149 Do_Abstract_States : Boolean := False) return Boolean;
150 -- Subsidiary to routines Unit_Requires_Body and Unit_Requires_Body_Info.
151 -- Determine whether entity Id declared in package spec Pack_Id requires
152 -- completion in a package body. Flag Do_Abstract_Stats should be set when
153 -- abstract states are to be considered in the completion test.
155 procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id);
156 -- Outputs info messages showing why package Pack_Id requires a body. The
157 -- caller has checked that the switch requesting this information is set,
158 -- and that the package does indeed require a body.
160 --------------------------
161 -- Analyze_Package_Body --
162 --------------------------
164 procedure Analyze_Package_Body (N : Node_Id) is
165 Loc : constant Source_Ptr := Sloc (N);
167 begin
168 if Debug_Flag_C then
169 Write_Str ("==> package body ");
170 Write_Name (Chars (Defining_Entity (N)));
171 Write_Str (" from ");
172 Write_Location (Loc);
173 Write_Eol;
174 Indent;
175 end if;
177 -- The real work is split out into the helper, so it can do "return;"
178 -- without skipping the debug output.
180 Analyze_Package_Body_Helper (N);
182 if Debug_Flag_C then
183 Outdent;
184 Write_Str ("<== package body ");
185 Write_Name (Chars (Defining_Entity (N)));
186 Write_Str (" from ");
187 Write_Location (Loc);
188 Write_Eol;
189 end if;
190 end Analyze_Package_Body;
192 ------------------------------------------------------
193 -- Analyze_Package_Body_Helper Data and Subprograms --
194 ------------------------------------------------------
196 Entity_Table_Size : constant := 4093;
197 -- Number of headers in hash table
199 subtype Entity_Header_Num is Integer range 0 .. Entity_Table_Size - 1;
200 -- Range of headers in hash table
202 function Entity_Hash (Id : Entity_Id) return Entity_Header_Num;
203 -- Simple hash function for Entity_Ids
205 package Subprogram_Table is new GNAT.Htable.Simple_HTable
206 (Header_Num => Entity_Header_Num,
207 Element => Boolean,
208 No_Element => False,
209 Key => Entity_Id,
210 Hash => Entity_Hash,
211 Equal => "=");
212 -- Hash table to record which subprograms are referenced. It is declared
213 -- at library level to avoid elaborating it for every call to Analyze.
215 -----------------
216 -- Entity_Hash --
217 -----------------
219 function Entity_Hash (Id : Entity_Id) return Entity_Header_Num is
220 begin
221 return Entity_Header_Num (Id mod Entity_Table_Size);
222 end Entity_Hash;
224 ---------------------------------
225 -- Analyze_Package_Body_Helper --
226 ---------------------------------
228 -- WARNING: This routine manages Ghost regions. Return statements must be
229 -- replaced by gotos which jump to the end of the routine and restore the
230 -- Ghost mode.
232 procedure Analyze_Package_Body_Helper (N : Node_Id) is
233 procedure Hide_Public_Entities (Decls : List_Id);
234 -- Attempt to hide all public entities found in declarative list Decls
235 -- by resetting their Is_Public flag to False depending on whether the
236 -- entities are not referenced by inlined or generic bodies. This kind
237 -- of processing is a conservative approximation and will still leave
238 -- entities externally visible if the package is not simple enough.
240 procedure Install_Composite_Operations (P : Entity_Id);
241 -- Composite types declared in the current scope may depend on types
242 -- that were private at the point of declaration, and whose full view
243 -- is now in scope. Indicate that the corresponding operations on the
244 -- composite type are available.
246 --------------------------
247 -- Hide_Public_Entities --
248 --------------------------
250 procedure Hide_Public_Entities (Decls : List_Id) is
251 function Has_Referencer
252 (Decls : List_Id;
253 Top_Level : Boolean := False) return Boolean;
254 -- A "referencer" is a construct which may reference a previous
255 -- declaration. Examine all declarations in list Decls in reverse
256 -- and determine whether once such referencer exists. All entities
257 -- in the range Last (Decls) .. Referencer are hidden from external
258 -- visibility.
260 function Scan_Subprogram_Ref (N : Node_Id) return Traverse_Result;
261 -- Determine whether a node denotes a reference to a subprogram
263 procedure Scan_Subprogram_Refs is
264 new Traverse_Proc (Scan_Subprogram_Ref);
265 -- Subsidiary to routine Has_Referencer. Determine whether a node
266 -- contains references to a subprogram and record them.
267 -- WARNING: this is a very expensive routine as it performs a full
268 -- tree traversal.
270 --------------------
271 -- Has_Referencer --
272 --------------------
274 function Has_Referencer
275 (Decls : List_Id;
276 Top_Level : Boolean := False) return Boolean
278 Decl : Node_Id;
279 Decl_Id : Entity_Id;
280 Spec : Node_Id;
282 Has_Non_Subprograms_Referencer : Boolean := False;
283 -- Set if an inlined subprogram body was detected as a referencer.
284 -- In this case, we do not return True immediately but keep hiding
285 -- subprograms from external visibility.
287 begin
288 if No (Decls) then
289 return False;
290 end if;
292 -- Examine all declarations in reverse order, hiding all entities
293 -- from external visibility until a referencer has been found. The
294 -- algorithm recurses into nested packages.
296 Decl := Last (Decls);
297 while Present (Decl) loop
299 -- A stub is always considered a referencer
301 if Nkind (Decl) in N_Body_Stub then
302 return True;
304 -- Package declaration
306 elsif Nkind (Decl) = N_Package_Declaration then
307 Spec := Specification (Decl);
309 -- Inspect the declarations of a non-generic package to try
310 -- and hide more entities from external visibility.
312 if not Is_Generic_Unit (Defining_Entity (Spec)) then
313 if Has_Referencer (Private_Declarations (Spec))
314 or else Has_Referencer (Visible_Declarations (Spec))
315 then
316 return True;
317 end if;
318 end if;
320 -- Package body
322 elsif Nkind (Decl) = N_Package_Body
323 and then Present (Corresponding_Spec (Decl))
324 then
325 Decl_Id := Corresponding_Spec (Decl);
327 -- A generic package body is a referencer. It would seem
328 -- that we only have to consider generics that can be
329 -- exported, i.e. where the corresponding spec is the
330 -- spec of the current package, but because of nested
331 -- instantiations, a fully private generic body may export
332 -- other private body entities. Furthermore, regardless of
333 -- whether there was a previous inlined subprogram, (an
334 -- instantiation of) the generic package may reference any
335 -- entity declared before it.
337 if Is_Generic_Unit (Decl_Id) then
338 return True;
340 -- Inspect the declarations of a non-generic package body to
341 -- try and hide more entities from external visibility.
343 elsif Has_Referencer (Declarations (Decl)) then
344 return True;
345 end if;
347 -- Subprogram body
349 elsif Nkind (Decl) = N_Subprogram_Body then
350 if Present (Corresponding_Spec (Decl)) then
351 Decl_Id := Corresponding_Spec (Decl);
353 -- A generic subprogram body acts as a referencer
355 if Is_Generic_Unit (Decl_Id) then
356 return True;
357 end if;
359 -- An inlined subprogram body acts as a referencer
361 -- Note that we test Has_Pragma_Inline here in addition
362 -- to Is_Inlined. We are doing this for a client, since
363 -- we are computing which entities should be public, and
364 -- it is the client who will decide if actual inlining
365 -- should occur, so we need to catch all cases where the
366 -- subprogram may be inlined by the client.
368 if Is_Inlined (Decl_Id)
369 or else Has_Pragma_Inline (Decl_Id)
370 then
371 Has_Non_Subprograms_Referencer := True;
373 -- Inspect the statements of the subprogram body
374 -- to determine whether the body references other
375 -- subprograms.
377 Scan_Subprogram_Refs (Decl);
378 end if;
380 -- Otherwise this is a stand alone subprogram body
382 else
383 Decl_Id := Defining_Entity (Decl);
385 -- An inlined subprogram body acts as a referencer
387 if Is_Inlined (Decl_Id)
388 or else Has_Pragma_Inline (Decl_Id)
389 then
390 Has_Non_Subprograms_Referencer := True;
392 -- Inspect the statements of the subprogram body
393 -- to determine whether the body references other
394 -- subprograms.
396 Scan_Subprogram_Refs (Decl);
398 -- Otherwise we can reset Is_Public right away
400 elsif not Subprogram_Table.Get (Decl_Id) then
401 Set_Is_Public (Decl_Id, False);
402 end if;
403 end if;
405 -- Freeze node
407 elsif Nkind (Decl) = N_Freeze_Entity then
408 declare
409 Discard : Boolean;
410 pragma Unreferenced (Discard);
411 begin
412 -- Inspect the actions to find references to subprograms
414 Discard := Has_Referencer (Actions (Decl));
415 end;
417 -- Exceptions, objects and renamings do not need to be public
418 -- if they are not followed by a construct which can reference
419 -- and export them. The Is_Public flag is reset on top level
420 -- entities only as anything nested is local to its context.
421 -- Likewise for subprograms, but we work harder for them.
423 elsif Nkind_In (Decl, N_Exception_Declaration,
424 N_Object_Declaration,
425 N_Object_Renaming_Declaration,
426 N_Subprogram_Declaration,
427 N_Subprogram_Renaming_Declaration)
428 then
429 Decl_Id := Defining_Entity (Decl);
431 if Top_Level
432 and then not Is_Imported (Decl_Id)
433 and then not Is_Exported (Decl_Id)
434 and then No (Interface_Name (Decl_Id))
435 and then
436 (not Has_Non_Subprograms_Referencer
437 or else (Nkind (Decl) = N_Subprogram_Declaration
438 and then not Subprogram_Table.Get (Decl_Id)))
439 then
440 Set_Is_Public (Decl_Id, False);
441 end if;
443 -- For a subprogram renaming, if the entity is referenced,
444 -- then so is the renamed subprogram. But there is an issue
445 -- with generic bodies because instantiations are not done
446 -- yet and, therefore, cannot be scanned for referencers.
447 -- That's why we use an approximation and test that we have
448 -- at least one subprogram referenced by an inlined body
449 -- instead of precisely the entity of this renaming.
451 if Nkind (Decl) = N_Subprogram_Renaming_Declaration
452 and then Subprogram_Table.Get_First
453 and then Is_Entity_Name (Name (Decl))
454 and then Present (Entity (Name (Decl)))
455 and then Is_Subprogram (Entity (Name (Decl)))
456 then
457 Subprogram_Table.Set (Entity (Name (Decl)), True);
458 end if;
459 end if;
461 Prev (Decl);
462 end loop;
464 return Has_Non_Subprograms_Referencer;
465 end Has_Referencer;
467 -------------------------
468 -- Scan_Subprogram_Ref --
469 -------------------------
471 function Scan_Subprogram_Ref (N : Node_Id) return Traverse_Result is
472 begin
473 -- Detect a reference of the form
474 -- Subp_Call
476 if Nkind (N) in N_Subprogram_Call
477 and then Is_Entity_Name (Name (N))
478 and then Present (Entity (Name (N)))
479 and then Is_Subprogram (Entity (Name (N)))
480 then
481 Subprogram_Table.Set (Entity (Name (N)), True);
483 -- Detect a reference of the form
484 -- Subp'Some_Attribute
486 elsif Nkind (N) = N_Attribute_Reference
487 and then Is_Entity_Name (Prefix (N))
488 and then Present (Entity (Prefix (N)))
489 and then Is_Subprogram (Entity (Prefix (N)))
490 then
491 Subprogram_Table.Set (Entity (Prefix (N)), True);
493 -- Constants can be substituted by their value in gigi, which may
494 -- contain a reference, so scan the value recursively.
496 elsif Is_Entity_Name (N)
497 and then Present (Entity (N))
498 and then Ekind (Entity (N)) = E_Constant
499 then
500 declare
501 Val : constant Node_Id := Constant_Value (Entity (N));
502 begin
503 if Present (Val)
504 and then not Compile_Time_Known_Value (Val)
505 then
506 Scan_Subprogram_Refs (Val);
507 end if;
508 end;
509 end if;
511 return OK;
512 end Scan_Subprogram_Ref;
514 -- Local variables
516 Discard : Boolean;
517 pragma Unreferenced (Discard);
519 -- Start of processing for Hide_Public_Entities
521 begin
522 -- The algorithm examines the top level declarations of a package
523 -- body in reverse looking for a construct that may export entities
524 -- declared prior to it. If such a scenario is encountered, then all
525 -- entities in the range Last (Decls) .. construct are hidden from
526 -- external visibility. Consider:
528 -- package Pack is
529 -- generic
530 -- package Gen is
531 -- end Gen;
532 -- end Pack;
534 -- package body Pack is
535 -- External_Obj : ...; -- (1)
537 -- package body Gen is -- (2)
538 -- ... External_Obj ... -- (3)
539 -- end Gen;
541 -- Local_Obj : ...; -- (4)
542 -- end Pack;
544 -- In this example Local_Obj (4) must not be externally visible as
545 -- it cannot be exported by anything in Pack. The body of generic
546 -- package Gen (2) on the other hand acts as a "referencer" and may
547 -- export anything declared before it. Since the compiler does not
548 -- perform flow analysis, it is not possible to determine precisely
549 -- which entities will be exported when Gen is instantiated. In the
550 -- example above External_Obj (1) is exported at (3), but this may
551 -- not always be the case. The algorithm takes a conservative stance
552 -- and leaves entity External_Obj public.
554 -- This very conservative algorithm is supplemented by a more precise
555 -- processing for inlined bodies. For them, we traverse the syntactic
556 -- tree and record which subprograms are actually referenced from it.
557 -- This makes it possible to compute a much smaller set of externally
558 -- visible subprograms in the absence of generic bodies, which can
559 -- have a significant impact on the inlining decisions made in the
560 -- back end and the removal of out-of-line bodies from the object
561 -- code. We do it only for inlined bodies because they are supposed
562 -- to be reasonably small and tree traversal is very expensive.
564 -- Note that even this special processing is not optimal for inlined
565 -- bodies, because we treat all inlined subprograms alike. An optimal
566 -- algorithm would require computing the transitive closure of the
567 -- inlined subprograms that can really be referenced from other units
568 -- in the source code.
570 -- We could extend this processing for inlined bodies and record all
571 -- entities, not just subprograms, referenced from them, which would
572 -- make it possible to compute a much smaller set of all externally
573 -- visible entities in the absence of generic bodies. But this would
574 -- mean implementing a more thorough tree traversal of the bodies,
575 -- i.e. not just syntactic, and the gain would very likely be worth
576 -- neither the hassle nor the slowdown of the compiler.
578 -- Finally, an important thing to be aware of is that, at this point,
579 -- instantiations are not done yet so we cannot directly see inlined
580 -- bodies coming from them. That's not catastrophic because only the
581 -- actual parameters of the instantiations matter here, and they are
582 -- present in the declarations list of the instantiated packages.
584 Subprogram_Table.Reset;
585 Discard := Has_Referencer (Decls, Top_Level => True);
586 end Hide_Public_Entities;
588 ----------------------------------
589 -- Install_Composite_Operations --
590 ----------------------------------
592 procedure Install_Composite_Operations (P : Entity_Id) is
593 Id : Entity_Id;
595 begin
596 Id := First_Entity (P);
597 while Present (Id) loop
598 if Is_Type (Id)
599 and then (Is_Limited_Composite (Id)
600 or else Is_Private_Composite (Id))
601 and then No (Private_Component (Id))
602 then
603 Set_Is_Limited_Composite (Id, False);
604 Set_Is_Private_Composite (Id, False);
605 end if;
607 Next_Entity (Id);
608 end loop;
609 end Install_Composite_Operations;
611 -- Local variables
613 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
614 Saved_ISMP : constant Boolean :=
615 Ignore_SPARK_Mode_Pragmas_In_Instance;
616 -- Save the Ghost and SPARK mode-related data to restore on exit
618 Body_Id : Entity_Id;
619 HSS : Node_Id;
620 Last_Spec_Entity : Entity_Id;
621 New_N : Node_Id;
622 Pack_Decl : Node_Id;
623 Spec_Id : Entity_Id;
625 -- Start of processing for Analyze_Package_Body_Helper
627 begin
628 -- Find corresponding package specification, and establish the current
629 -- scope. The visible defining entity for the package is the defining
630 -- occurrence in the spec. On exit from the package body, all body
631 -- declarations are attached to the defining entity for the body, but
632 -- the later is never used for name resolution. In this fashion there
633 -- is only one visible entity that denotes the package.
635 -- Set Body_Id. Note that this will be reset to point to the generic
636 -- copy later on in the generic case.
638 Body_Id := Defining_Entity (N);
640 -- Body is body of package instantiation. Corresponding spec has already
641 -- been set.
643 if Present (Corresponding_Spec (N)) then
644 Spec_Id := Corresponding_Spec (N);
645 Pack_Decl := Unit_Declaration_Node (Spec_Id);
647 else
648 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
650 if Present (Spec_Id)
651 and then Is_Package_Or_Generic_Package (Spec_Id)
652 then
653 Pack_Decl := Unit_Declaration_Node (Spec_Id);
655 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
656 Error_Msg_N ("cannot supply body for package renaming", N);
657 return;
659 elsif Present (Corresponding_Body (Pack_Decl)) then
660 Error_Msg_N ("redefinition of package body", N);
661 return;
662 end if;
664 else
665 Error_Msg_N ("missing specification for package body", N);
666 return;
667 end if;
669 if Is_Package_Or_Generic_Package (Spec_Id)
670 and then (Scope (Spec_Id) = Standard_Standard
671 or else Is_Child_Unit (Spec_Id))
672 and then not Unit_Requires_Body (Spec_Id)
673 then
674 if Ada_Version = Ada_83 then
675 Error_Msg_N
676 ("optional package body (not allowed in Ada 95)??", N);
677 else
678 Error_Msg_N ("spec of this package does not allow a body", N);
679 end if;
680 end if;
681 end if;
683 -- A [generic] package body "freezes" the contract of the nearest
684 -- enclosing package body and all other contracts encountered in the
685 -- same declarative part up to and excluding the package body:
687 -- package body Nearest_Enclosing_Package
688 -- with Refined_State => (State => Constit)
689 -- is
690 -- Constit : ...;
692 -- package body Freezes_Enclosing_Package_Body
693 -- with Refined_State => (State_2 => Constit_2)
694 -- is
695 -- Constit_2 : ...;
697 -- procedure Proc
698 -- with Refined_Depends => (Input => (Constit, Constit_2)) ...
700 -- This ensures that any annotations referenced by the contract of a
701 -- [generic] subprogram body declared within the current package body
702 -- are available. This form of "freezing" is decoupled from the usual
703 -- Freeze_xxx mechanism because it must also work in the context of
704 -- generics where normal freezing is disabled.
706 -- Only bodies coming from source should cause this type of "freezing".
707 -- Instantiated generic bodies are excluded because their processing is
708 -- performed in a separate compilation pass which lacks enough semantic
709 -- information with respect to contract analysis. It is safe to suppress
710 -- the "freezing" of contracts in this case because this action already
711 -- took place at the end of the enclosing declarative part.
713 if Comes_From_Source (N)
714 and then not Is_Generic_Instance (Spec_Id)
715 then
716 Analyze_Previous_Contracts (N);
717 end if;
719 -- A package body is Ghost when the corresponding spec is Ghost. Set
720 -- the mode now to ensure that any nodes generated during analysis and
721 -- expansion are properly flagged as ignored Ghost.
723 Mark_And_Set_Ghost_Body (N, Spec_Id);
725 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
726 Style.Check_Identifier (Body_Id, Spec_Id);
728 if Is_Child_Unit (Spec_Id) then
729 if Nkind (Parent (N)) /= N_Compilation_Unit then
730 Error_Msg_NE
731 ("body of child unit& cannot be an inner package", N, Spec_Id);
732 end if;
734 Set_Is_Child_Unit (Body_Id);
735 end if;
737 -- Generic package case
739 if Ekind (Spec_Id) = E_Generic_Package then
741 -- Disable expansion and perform semantic analysis on copy. The
742 -- unannotated body will be used in all instantiations.
744 Body_Id := Defining_Entity (N);
745 Set_Ekind (Body_Id, E_Package_Body);
746 Set_Scope (Body_Id, Scope (Spec_Id));
747 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
748 Set_Body_Entity (Spec_Id, Body_Id);
749 Set_Spec_Entity (Body_Id, Spec_Id);
751 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
752 Rewrite (N, New_N);
754 -- Once the contents of the generic copy and the template are
755 -- swapped, do the same for their respective aspect specifications.
757 Exchange_Aspects (N, New_N);
759 -- Collect all contract-related source pragmas found within the
760 -- template and attach them to the contract of the package body.
761 -- This contract is used in the capture of global references within
762 -- annotations.
764 Create_Generic_Contract (N);
766 -- Update Body_Id to point to the copied node for the remainder of
767 -- the processing.
769 Body_Id := Defining_Entity (N);
770 Start_Generic;
771 end if;
773 -- The Body_Id is that of the copied node in the generic case, the
774 -- current node otherwise. Note that N was rewritten above, so we must
775 -- be sure to get the latest Body_Id value.
777 Set_Ekind (Body_Id, E_Package_Body);
778 Set_Body_Entity (Spec_Id, Body_Id);
779 Set_Spec_Entity (Body_Id, Spec_Id);
781 -- Defining name for the package body is not a visible entity: Only the
782 -- defining name for the declaration is visible.
784 Set_Etype (Body_Id, Standard_Void_Type);
785 Set_Scope (Body_Id, Scope (Spec_Id));
786 Set_Corresponding_Spec (N, Spec_Id);
787 Set_Corresponding_Body (Pack_Decl, Body_Id);
789 -- The body entity is not used for semantics or code generation, but
790 -- it is attached to the entity list of the enclosing scope to simplify
791 -- the listing of back-annotations for the types it main contain.
793 if Scope (Spec_Id) /= Standard_Standard then
794 Append_Entity (Body_Id, Scope (Spec_Id));
795 end if;
797 -- Indicate that we are currently compiling the body of the package
799 Set_In_Package_Body (Spec_Id);
800 Set_Has_Completion (Spec_Id);
801 Last_Spec_Entity := Last_Entity (Spec_Id);
803 if Has_Aspects (N) then
804 Analyze_Aspect_Specifications (N, Body_Id);
805 end if;
807 Push_Scope (Spec_Id);
809 -- Set SPARK_Mode only for non-generic package
811 if Ekind (Spec_Id) = E_Package then
812 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
813 Set_SPARK_Aux_Pragma (Body_Id, SPARK_Mode_Pragma);
814 Set_SPARK_Pragma_Inherited (Body_Id);
815 Set_SPARK_Aux_Pragma_Inherited (Body_Id);
817 -- A package body may be instantiated or inlined at a later pass.
818 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when
819 -- it applied to the package spec.
821 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
822 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
823 end if;
824 end if;
826 Set_Categorization_From_Pragmas (N);
828 Install_Visible_Declarations (Spec_Id);
829 Install_Private_Declarations (Spec_Id);
830 Install_Private_With_Clauses (Spec_Id);
831 Install_Composite_Operations (Spec_Id);
833 Check_Anonymous_Access_Types (Spec_Id, N);
835 if Ekind (Spec_Id) = E_Generic_Package then
836 Set_Use (Generic_Formal_Declarations (Pack_Decl));
837 end if;
839 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
840 Set_Use (Private_Declarations (Specification (Pack_Decl)));
842 -- This is a nested package, so it may be necessary to declare certain
843 -- inherited subprograms that are not yet visible because the parent
844 -- type's subprograms are now visible.
846 if Ekind (Scope (Spec_Id)) = E_Package
847 and then Scope (Spec_Id) /= Standard_Standard
848 then
849 Declare_Inherited_Private_Subprograms (Spec_Id);
850 end if;
852 -- A package body "freezes" the contract of its initial declaration.
853 -- This analysis depends on attribute Corresponding_Spec being set. Only
854 -- bodies coming from source shuld cause this type of "freezing".
856 if Present (Declarations (N)) then
857 Analyze_Declarations (Declarations (N));
858 Inspect_Deferred_Constant_Completion (Declarations (N));
859 end if;
861 -- Verify that the SPARK_Mode of the body agrees with that of its spec
863 if Present (SPARK_Pragma (Body_Id)) then
864 if Present (SPARK_Aux_Pragma (Spec_Id)) then
865 if Get_SPARK_Mode_From_Annotation (SPARK_Aux_Pragma (Spec_Id)) =
867 and then
868 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
869 then
870 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
871 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
872 Error_Msg_Sloc := Sloc (SPARK_Aux_Pragma (Spec_Id));
873 Error_Msg_NE
874 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
875 end if;
877 else
878 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
879 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
880 Error_Msg_Sloc := Sloc (Spec_Id);
881 Error_Msg_NE
882 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
883 end if;
884 end if;
886 -- Analyze_Declarations has caused freezing of all types. Now generate
887 -- bodies for RACW primitives and stream attributes, if any.
889 if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then
891 -- Attach subprogram bodies to support RACWs declared in spec
893 Append_RACW_Bodies (Declarations (N), Spec_Id);
894 Analyze_List (Declarations (N));
895 end if;
897 HSS := Handled_Statement_Sequence (N);
899 if Present (HSS) then
900 Process_End_Label (HSS, 't', Spec_Id);
901 Analyze (HSS);
903 -- Check that elaboration code in a preelaborable package body is
904 -- empty other than null statements and labels (RM 10.2.1(6)).
906 Validate_Null_Statement_Sequence (N);
907 end if;
909 Validate_Categorization_Dependency (N, Spec_Id);
910 Check_Completion (Body_Id);
912 -- Generate start of body reference. Note that we do this fairly late,
913 -- because the call will use In_Extended_Main_Source_Unit as a check,
914 -- and we want to make sure that Corresponding_Stub links are set
916 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
918 -- For a generic package, collect global references and mark them on
919 -- the original body so that they are not resolved again at the point
920 -- of instantiation.
922 if Ekind (Spec_Id) /= E_Package then
923 Save_Global_References (Original_Node (N));
924 End_Generic;
925 end if;
927 -- The entities of the package body have so far been chained onto the
928 -- declaration chain for the spec. That's been fine while we were in the
929 -- body, since we wanted them to be visible, but now that we are leaving
930 -- the package body, they are no longer visible, so we remove them from
931 -- the entity chain of the package spec entity, and copy them to the
932 -- entity chain of the package body entity, where they will never again
933 -- be visible.
935 if Present (Last_Spec_Entity) then
936 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
937 Set_Next_Entity (Last_Spec_Entity, Empty);
938 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
939 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
941 else
942 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
943 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
944 Set_First_Entity (Spec_Id, Empty);
945 Set_Last_Entity (Spec_Id, Empty);
946 end if;
948 End_Package_Scope (Spec_Id);
950 -- All entities declared in body are not visible
952 declare
953 E : Entity_Id;
955 begin
956 E := First_Entity (Body_Id);
957 while Present (E) loop
958 Set_Is_Immediately_Visible (E, False);
959 Set_Is_Potentially_Use_Visible (E, False);
960 Set_Is_Hidden (E);
962 -- Child units may appear on the entity list (e.g. if they appear
963 -- in the context of a subunit) but they are not body entities.
965 if not Is_Child_Unit (E) then
966 Set_Is_Package_Body_Entity (E);
967 end if;
969 Next_Entity (E);
970 end loop;
971 end;
973 Check_References (Body_Id);
975 -- For a generic unit, check that the formal parameters are referenced,
976 -- and that local variables are used, as for regular packages.
978 if Ekind (Spec_Id) = E_Generic_Package then
979 Check_References (Spec_Id);
980 end if;
982 -- At this point all entities of the package body are externally visible
983 -- to the linker as their Is_Public flag is set to True. This proactive
984 -- approach is necessary because an inlined or a generic body for which
985 -- code is generated in other units may need to see these entities. Cut
986 -- down the number of global symbols that do not neet public visibility
987 -- as this has two beneficial effects:
988 -- (1) It makes the compilation process more efficient.
989 -- (2) It gives the code generator more leeway to optimize within each
990 -- unit, especially subprograms.
992 -- This is done only for top-level library packages or child units as
993 -- the algorithm does a top-down traversal of the package body.
995 if (Scope (Spec_Id) = Standard_Standard or else Is_Child_Unit (Spec_Id))
996 and then not Is_Generic_Unit (Spec_Id)
997 then
998 Hide_Public_Entities (Declarations (N));
999 end if;
1001 -- If expander is not active, then here is where we turn off the
1002 -- In_Package_Body flag, otherwise it is turned off at the end of the
1003 -- corresponding expansion routine. If this is an instance body, we need
1004 -- to qualify names of local entities, because the body may have been
1005 -- compiled as a preliminary to another instantiation.
1007 if not Expander_Active then
1008 Set_In_Package_Body (Spec_Id, False);
1010 if Is_Generic_Instance (Spec_Id)
1011 and then Operating_Mode = Generate_Code
1012 then
1013 Qualify_Entity_Names (N);
1014 end if;
1015 end if;
1017 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
1018 Restore_Ghost_Mode (Saved_GM);
1019 end Analyze_Package_Body_Helper;
1021 ---------------------------------
1022 -- Analyze_Package_Declaration --
1023 ---------------------------------
1025 procedure Analyze_Package_Declaration (N : Node_Id) is
1026 Id : constant Node_Id := Defining_Entity (N);
1028 Is_Comp_Unit : constant Boolean :=
1029 Nkind (Parent (N)) = N_Compilation_Unit;
1031 Body_Required : Boolean;
1032 -- True when this package declaration requires a corresponding body
1034 begin
1035 if Debug_Flag_C then
1036 Write_Str ("==> package spec ");
1037 Write_Name (Chars (Id));
1038 Write_Str (" from ");
1039 Write_Location (Sloc (N));
1040 Write_Eol;
1041 Indent;
1042 end if;
1044 Generate_Definition (Id);
1045 Enter_Name (Id);
1046 Set_Ekind (Id, E_Package);
1047 Set_Etype (Id, Standard_Void_Type);
1049 -- Set SPARK_Mode from context
1051 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
1052 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
1053 Set_SPARK_Pragma_Inherited (Id);
1054 Set_SPARK_Aux_Pragma_Inherited (Id);
1056 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
1057 -- the body of this package is instantiated or inlined later and out of
1058 -- context. The body uses this attribute to restore the value of the
1059 -- global flag.
1061 if Ignore_SPARK_Mode_Pragmas_In_Instance then
1062 Set_Ignore_SPARK_Mode_Pragmas (Id);
1063 end if;
1065 -- Analyze aspect specifications immediately, since we need to recognize
1066 -- things like Pure early enough to diagnose violations during analysis.
1068 if Has_Aspects (N) then
1069 Analyze_Aspect_Specifications (N, Id);
1070 end if;
1072 -- Ada 2005 (AI-217): Check if the package has been illegally named in
1073 -- a limited-with clause of its own context. In this case the error has
1074 -- been previously notified by Analyze_Context.
1076 -- limited with Pkg; -- ERROR
1077 -- package Pkg is ...
1079 if From_Limited_With (Id) then
1080 return;
1081 end if;
1083 Push_Scope (Id);
1085 Set_Is_Pure (Id, Is_Pure (Enclosing_Lib_Unit_Entity));
1086 Set_Categorization_From_Pragmas (N);
1088 Analyze (Specification (N));
1089 Validate_Categorization_Dependency (N, Id);
1091 -- Determine whether the package requires a body. Abstract states are
1092 -- intentionally ignored because they do require refinement which can
1093 -- only come in a body, but at the same time they do not force the need
1094 -- for a body on their own (SPARK RM 7.1.4(4) and 7.2.2(3)).
1096 Body_Required := Unit_Requires_Body (Id);
1098 if not Body_Required then
1100 -- If the package spec does not require an explicit body, then there
1101 -- are not entities requiring completion in the language sense. Call
1102 -- Check_Completion now to ensure that nested package declarations
1103 -- that require an implicit body get one. (In the case where a body
1104 -- is required, Check_Completion is called at the end of the body's
1105 -- declarative part.)
1107 Check_Completion;
1109 -- If the package spec does not require an explicit body, then all
1110 -- abstract states declared in nested packages cannot possibly get
1111 -- a proper refinement (SPARK RM 7.2.2(3)). This check is performed
1112 -- only when the compilation unit is the main unit to allow for
1113 -- modular SPARK analysis where packages do not necessarily have
1114 -- bodies.
1116 if Is_Comp_Unit then
1117 Check_State_Refinements
1118 (Context => N,
1119 Is_Main_Unit => Parent (N) = Cunit (Main_Unit));
1120 end if;
1121 end if;
1123 if Is_Comp_Unit then
1125 -- Set Body_Required indication on the compilation unit node, and
1126 -- determine whether elaboration warnings may be meaningful on it.
1128 Set_Body_Required (Parent (N), Body_Required);
1130 if not Body_Required then
1131 Set_Suppress_Elaboration_Warnings (Id);
1132 end if;
1133 end if;
1135 End_Package_Scope (Id);
1137 -- For the declaration of a library unit that is a remote types package,
1138 -- check legality rules regarding availability of stream attributes for
1139 -- types that contain non-remote access values. This subprogram performs
1140 -- visibility tests that rely on the fact that we have exited the scope
1141 -- of Id.
1143 if Is_Comp_Unit then
1144 Validate_RT_RAT_Component (N);
1145 end if;
1147 if Debug_Flag_C then
1148 Outdent;
1149 Write_Str ("<== package spec ");
1150 Write_Name (Chars (Id));
1151 Write_Str (" from ");
1152 Write_Location (Sloc (N));
1153 Write_Eol;
1154 end if;
1155 end Analyze_Package_Declaration;
1157 -----------------------------------
1158 -- Analyze_Package_Specification --
1159 -----------------------------------
1161 -- Note that this code is shared for the analysis of generic package specs
1162 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
1164 procedure Analyze_Package_Specification (N : Node_Id) is
1165 Id : constant Entity_Id := Defining_Entity (N);
1166 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
1167 Vis_Decls : constant List_Id := Visible_Declarations (N);
1168 Priv_Decls : constant List_Id := Private_Declarations (N);
1169 E : Entity_Id;
1170 L : Entity_Id;
1171 Public_Child : Boolean;
1173 Private_With_Clauses_Installed : Boolean := False;
1174 -- In Ada 2005, private with_clauses are visible in the private part
1175 -- of a nested package, even if it appears in the public part of the
1176 -- enclosing package. This requires a separate step to install these
1177 -- private_with_clauses, and remove them at the end of the nested
1178 -- package.
1180 procedure Check_One_Tagged_Type_Or_Extension_At_Most;
1181 -- Issue an error in SPARK mode if a package specification contains
1182 -- more than one tagged type or type extension.
1184 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
1185 -- Clears constant indications (Never_Set_In_Source, Constant_Value, and
1186 -- Is_True_Constant) on all variables that are entities of Id, and on
1187 -- the chain whose first element is FE. A recursive call is made for all
1188 -- packages and generic packages.
1190 procedure Generate_Parent_References;
1191 -- For a child unit, generate references to parent units, for
1192 -- GPS navigation purposes.
1194 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
1195 -- Child and Unit are entities of compilation units. True if Child
1196 -- is a public child of Parent as defined in 10.1.1
1198 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
1199 -- Reject completion of an incomplete or private type declarations
1200 -- having a known discriminant part by an unchecked union.
1202 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
1203 -- Given the package entity of a generic package instantiation or
1204 -- formal package whose corresponding generic is a child unit, installs
1205 -- the private declarations of each of the child unit's parents.
1206 -- This has to be done at the point of entering the instance package's
1207 -- private part rather than being done in Sem_Ch12.Install_Parent
1208 -- (which is where the parents' visible declarations are installed).
1210 ------------------------------------------------
1211 -- Check_One_Tagged_Type_Or_Extension_At_Most --
1212 ------------------------------------------------
1214 procedure Check_One_Tagged_Type_Or_Extension_At_Most is
1215 Previous : Node_Id;
1217 procedure Check_Decls (Decls : List_Id);
1218 -- Check that either Previous is Empty and Decls does not contain
1219 -- more than one tagged type or type extension, or Previous is
1220 -- already set and Decls contains no tagged type or type extension.
1222 -----------------
1223 -- Check_Decls --
1224 -----------------
1226 procedure Check_Decls (Decls : List_Id) is
1227 Decl : Node_Id;
1229 begin
1230 Decl := First (Decls);
1231 while Present (Decl) loop
1232 if Nkind (Decl) = N_Full_Type_Declaration
1233 and then Is_Tagged_Type (Defining_Identifier (Decl))
1234 then
1235 if No (Previous) then
1236 Previous := Decl;
1238 else
1239 Error_Msg_Sloc := Sloc (Previous);
1240 Check_SPARK_05_Restriction
1241 ("at most one tagged type or type extension allowed",
1242 "\\ previous declaration#",
1243 Decl);
1244 end if;
1245 end if;
1247 Next (Decl);
1248 end loop;
1249 end Check_Decls;
1251 -- Start of processing for Check_One_Tagged_Type_Or_Extension_At_Most
1253 begin
1254 Previous := Empty;
1255 Check_Decls (Vis_Decls);
1257 if Present (Priv_Decls) then
1258 Check_Decls (Priv_Decls);
1259 end if;
1260 end Check_One_Tagged_Type_Or_Extension_At_Most;
1262 ---------------------
1263 -- Clear_Constants --
1264 ---------------------
1266 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
1267 E : Entity_Id;
1269 begin
1270 -- Ignore package renamings, not interesting and they can cause self
1271 -- referential loops in the code below.
1273 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
1274 return;
1275 end if;
1277 -- Note: in the loop below, the check for Next_Entity pointing back
1278 -- to the package entity may seem odd, but it is needed, because a
1279 -- package can contain a renaming declaration to itself, and such
1280 -- renamings are generated automatically within package instances.
1282 E := FE;
1283 while Present (E) and then E /= Id loop
1284 if Is_Assignable (E) then
1285 Set_Never_Set_In_Source (E, False);
1286 Set_Is_True_Constant (E, False);
1287 Set_Current_Value (E, Empty);
1288 Set_Is_Known_Null (E, False);
1289 Set_Last_Assignment (E, Empty);
1291 if not Can_Never_Be_Null (E) then
1292 Set_Is_Known_Non_Null (E, False);
1293 end if;
1295 elsif Is_Package_Or_Generic_Package (E) then
1296 Clear_Constants (E, First_Entity (E));
1297 Clear_Constants (E, First_Private_Entity (E));
1298 end if;
1300 Next_Entity (E);
1301 end loop;
1302 end Clear_Constants;
1304 --------------------------------
1305 -- Generate_Parent_References --
1306 --------------------------------
1308 procedure Generate_Parent_References is
1309 Decl : constant Node_Id := Parent (N);
1311 begin
1312 if Id = Cunit_Entity (Main_Unit)
1313 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
1314 then
1315 Generate_Reference (Id, Scope (Id), 'k', False);
1317 elsif not Nkind_In (Unit (Cunit (Main_Unit)), N_Subprogram_Body,
1318 N_Subunit)
1319 then
1320 -- If current unit is an ancestor of main unit, generate a
1321 -- reference to its own parent.
1323 declare
1324 U : Node_Id;
1325 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
1327 begin
1328 if Nkind (Main_Spec) = N_Package_Body then
1329 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
1330 end if;
1332 U := Parent_Spec (Main_Spec);
1333 while Present (U) loop
1334 if U = Parent (Decl) then
1335 Generate_Reference (Id, Scope (Id), 'k', False);
1336 exit;
1338 elsif Nkind (Unit (U)) = N_Package_Body then
1339 exit;
1341 else
1342 U := Parent_Spec (Unit (U));
1343 end if;
1344 end loop;
1345 end;
1346 end if;
1347 end Generate_Parent_References;
1349 ---------------------
1350 -- Is_Public_Child --
1351 ---------------------
1353 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
1354 begin
1355 if not Is_Private_Descendant (Child) then
1356 return True;
1357 else
1358 if Child = Unit then
1359 return not Private_Present (
1360 Parent (Unit_Declaration_Node (Child)));
1361 else
1362 return Is_Public_Child (Scope (Child), Unit);
1363 end if;
1364 end if;
1365 end Is_Public_Child;
1367 ----------------------------------------
1368 -- Inspect_Unchecked_Union_Completion --
1369 ----------------------------------------
1371 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
1372 Decl : Node_Id;
1374 begin
1375 Decl := First (Decls);
1376 while Present (Decl) loop
1378 -- We are looking at an incomplete or private type declaration
1379 -- with a known_discriminant_part whose full view is an
1380 -- Unchecked_Union.
1382 if Nkind_In (Decl, N_Incomplete_Type_Declaration,
1383 N_Private_Type_Declaration)
1384 and then Has_Discriminants (Defining_Identifier (Decl))
1385 and then Present (Full_View (Defining_Identifier (Decl)))
1386 and then
1387 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
1388 then
1389 Error_Msg_N
1390 ("completion of discriminated partial view "
1391 & "cannot be an unchecked union",
1392 Full_View (Defining_Identifier (Decl)));
1393 end if;
1395 Next (Decl);
1396 end loop;
1397 end Inspect_Unchecked_Union_Completion;
1399 -----------------------------------------
1400 -- Install_Parent_Private_Declarations --
1401 -----------------------------------------
1403 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
1404 Inst_Par : Entity_Id;
1405 Gen_Par : Entity_Id;
1406 Inst_Node : Node_Id;
1408 begin
1409 Inst_Par := Inst_Id;
1411 Gen_Par :=
1412 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
1413 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
1414 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
1416 if Nkind_In (Inst_Node, N_Package_Instantiation,
1417 N_Formal_Package_Declaration)
1418 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
1419 then
1420 Inst_Par := Entity (Prefix (Name (Inst_Node)));
1422 if Present (Renamed_Entity (Inst_Par)) then
1423 Inst_Par := Renamed_Entity (Inst_Par);
1424 end if;
1426 Gen_Par :=
1427 Generic_Parent
1428 (Specification (Unit_Declaration_Node (Inst_Par)));
1430 -- Install the private declarations and private use clauses
1431 -- of a parent instance of the child instance, unless the
1432 -- parent instance private declarations have already been
1433 -- installed earlier in Analyze_Package_Specification, which
1434 -- happens when a generic child is instantiated, and the
1435 -- instance is a child of the parent instance.
1437 -- Installing the use clauses of the parent instance twice
1438 -- is both unnecessary and wrong, because it would cause the
1439 -- clauses to be chained to themselves in the use clauses
1440 -- list of the scope stack entry. That in turn would cause
1441 -- an endless loop from End_Use_Clauses upon scope exit.
1443 -- The parent is now fully visible. It may be a hidden open
1444 -- scope if we are currently compiling some child instance
1445 -- declared within it, but while the current instance is being
1446 -- compiled the parent is immediately visible. In particular
1447 -- its entities must remain visible if a stack save/restore
1448 -- takes place through a call to Rtsfind.
1450 if Present (Gen_Par) then
1451 if not In_Private_Part (Inst_Par) then
1452 Install_Private_Declarations (Inst_Par);
1453 Set_Use (Private_Declarations
1454 (Specification
1455 (Unit_Declaration_Node (Inst_Par))));
1456 Set_Is_Hidden_Open_Scope (Inst_Par, False);
1457 end if;
1459 -- If we've reached the end of the generic instance parents,
1460 -- then finish off by looping through the nongeneric parents
1461 -- and installing their private declarations.
1463 -- If one of the non-generic parents is itself on the scope
1464 -- stack, do not install its private declarations: they are
1465 -- installed in due time when the private part of that parent
1466 -- is analyzed.
1468 else
1469 while Present (Inst_Par)
1470 and then Inst_Par /= Standard_Standard
1471 and then (not In_Open_Scopes (Inst_Par)
1472 or else not In_Private_Part (Inst_Par))
1473 loop
1474 if Nkind (Inst_Node) = N_Formal_Package_Declaration
1475 or else
1476 not Is_Ancestor_Package
1477 (Inst_Par, Cunit_Entity (Current_Sem_Unit))
1478 then
1479 Install_Private_Declarations (Inst_Par);
1480 Set_Use
1481 (Private_Declarations
1482 (Specification
1483 (Unit_Declaration_Node (Inst_Par))));
1484 Inst_Par := Scope (Inst_Par);
1485 else
1486 exit;
1487 end if;
1488 end loop;
1490 exit;
1491 end if;
1493 else
1494 exit;
1495 end if;
1496 end loop;
1497 end Install_Parent_Private_Declarations;
1499 -- Start of processing for Analyze_Package_Specification
1501 begin
1502 if Present (Vis_Decls) then
1503 Analyze_Declarations (Vis_Decls);
1504 end if;
1506 -- Inspect the entities defined in the package and ensure that all
1507 -- incomplete types have received full declarations. Build default
1508 -- initial condition and invariant procedures for all qualifying types.
1510 E := First_Entity (Id);
1511 while Present (E) loop
1513 -- Check on incomplete types
1515 -- AI05-0213: A formal incomplete type has no completion, and neither
1516 -- does the corresponding subtype in an instance.
1518 if Is_Incomplete_Type (E)
1519 and then No (Full_View (E))
1520 and then not Is_Generic_Type (E)
1521 and then not From_Limited_With (E)
1522 and then not Is_Generic_Actual_Type (E)
1523 then
1524 Error_Msg_N ("no declaration in visible part for incomplete}", E);
1525 end if;
1527 Next_Entity (E);
1528 end loop;
1530 if Is_Remote_Call_Interface (Id)
1531 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1532 then
1533 Validate_RCI_Declarations (Id);
1534 end if;
1536 -- Save global references in the visible declarations, before installing
1537 -- private declarations of parent unit if there is one, because the
1538 -- privacy status of types defined in the parent will change. This is
1539 -- only relevant for generic child units, but is done in all cases for
1540 -- uniformity.
1542 if Ekind (Id) = E_Generic_Package
1543 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1544 then
1545 declare
1546 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1547 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1549 begin
1550 -- Insert the freezing nodes after the visible declarations to
1551 -- ensure that we analyze its aspects; needed to ensure that
1552 -- global entities referenced in the aspects are properly handled.
1554 if Ada_Version >= Ada_2012
1555 and then Is_Non_Empty_List (Vis_Decls)
1556 and then Is_Empty_List (Priv_Decls)
1557 then
1558 Insert_List_After_And_Analyze
1559 (Last (Vis_Decls), Freeze_Entity (Id, Last (Vis_Decls)));
1560 end if;
1562 Set_Private_Declarations (Orig_Spec, Empty_List);
1563 Save_Global_References (Orig_Decl);
1564 Set_Private_Declarations (Orig_Spec, Save_Priv);
1565 end;
1566 end if;
1568 -- If package is a public child unit, then make the private declarations
1569 -- of the parent visible.
1571 Public_Child := False;
1573 declare
1574 Par : Entity_Id;
1575 Pack_Decl : Node_Id;
1576 Par_Spec : Node_Id;
1578 begin
1579 Par := Id;
1580 Par_Spec := Parent_Spec (Parent (N));
1582 -- If the package is formal package of an enclosing generic, it is
1583 -- transformed into a local generic declaration, and compiled to make
1584 -- its spec available. We need to retrieve the original generic to
1585 -- determine whether it is a child unit, and install its parents.
1587 if No (Par_Spec)
1588 and then
1589 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1590 then
1591 Par := Entity (Name (Original_Node (Parent (N))));
1592 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1593 end if;
1595 if Present (Par_Spec) then
1596 Generate_Parent_References;
1598 while Scope (Par) /= Standard_Standard
1599 and then Is_Public_Child (Id, Par)
1600 and then In_Open_Scopes (Par)
1601 loop
1602 Public_Child := True;
1603 Par := Scope (Par);
1604 Install_Private_Declarations (Par);
1605 Install_Private_With_Clauses (Par);
1606 Pack_Decl := Unit_Declaration_Node (Par);
1607 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1608 end loop;
1609 end if;
1610 end;
1612 if Is_Compilation_Unit (Id) then
1613 Install_Private_With_Clauses (Id);
1614 else
1615 -- The current compilation unit may include private with_clauses,
1616 -- which are visible in the private part of the current nested
1617 -- package, and have to be installed now. This is not done for
1618 -- nested instantiations, where the private with_clauses of the
1619 -- enclosing unit have no effect once the instantiation info is
1620 -- established and we start analyzing the package declaration.
1622 declare
1623 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1624 begin
1625 if Is_Package_Or_Generic_Package (Comp_Unit)
1626 and then not In_Private_Part (Comp_Unit)
1627 and then not In_Instance
1628 then
1629 Install_Private_With_Clauses (Comp_Unit);
1630 Private_With_Clauses_Installed := True;
1631 end if;
1632 end;
1633 end if;
1635 -- If this is a package associated with a generic instance or formal
1636 -- package, then the private declarations of each of the generic's
1637 -- parents must be installed at this point.
1639 if Is_Generic_Instance (Id) then
1640 Install_Parent_Private_Declarations (Id);
1641 end if;
1643 -- Analyze private part if present. The flag In_Private_Part is reset
1644 -- in End_Package_Scope.
1646 L := Last_Entity (Id);
1648 if Present (Priv_Decls) then
1649 Set_In_Private_Part (Id);
1651 -- Upon entering a public child's private part, it may be necessary
1652 -- to declare subprograms that were derived in the package's visible
1653 -- part but not yet made visible.
1655 if Public_Child then
1656 Declare_Inherited_Private_Subprograms (Id);
1657 end if;
1659 Analyze_Declarations (Priv_Decls);
1661 -- Check the private declarations for incomplete deferred constants
1663 Inspect_Deferred_Constant_Completion (Priv_Decls);
1665 -- The first private entity is the immediate follower of the last
1666 -- visible entity, if there was one.
1668 if Present (L) then
1669 Set_First_Private_Entity (Id, Next_Entity (L));
1670 else
1671 Set_First_Private_Entity (Id, First_Entity (Id));
1672 end if;
1674 -- There may be inherited private subprograms that need to be declared,
1675 -- even in the absence of an explicit private part. If there are any
1676 -- public declarations in the package and the package is a public child
1677 -- unit, then an implicit private part is assumed.
1679 elsif Present (L) and then Public_Child then
1680 Set_In_Private_Part (Id);
1681 Declare_Inherited_Private_Subprograms (Id);
1682 Set_First_Private_Entity (Id, Next_Entity (L));
1683 end if;
1685 E := First_Entity (Id);
1686 while Present (E) loop
1688 -- Check rule of 3.6(11), which in general requires waiting till all
1689 -- full types have been seen.
1691 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1692 Check_Aliased_Component_Types (E);
1693 end if;
1695 -- Check preelaborable initialization for full type completing a
1696 -- private type for which pragma Preelaborable_Initialization given.
1698 if Is_Type (E)
1699 and then Must_Have_Preelab_Init (E)
1700 and then not Has_Preelaborable_Initialization (E)
1701 then
1702 Error_Msg_N
1703 ("full view of & does not have preelaborable initialization", E);
1704 end if;
1706 Next_Entity (E);
1707 end loop;
1709 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1710 -- declaration having a known_discriminant_part shall not be an
1711 -- unchecked union type.
1713 if Present (Vis_Decls) then
1714 Inspect_Unchecked_Union_Completion (Vis_Decls);
1715 end if;
1717 if Present (Priv_Decls) then
1718 Inspect_Unchecked_Union_Completion (Priv_Decls);
1719 end if;
1721 if Ekind (Id) = E_Generic_Package
1722 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1723 and then Present (Priv_Decls)
1724 then
1725 -- Save global references in private declarations, ignoring the
1726 -- visible declarations that were processed earlier.
1728 declare
1729 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1730 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1731 Save_Form : constant List_Id :=
1732 Generic_Formal_Declarations (Orig_Decl);
1734 begin
1735 -- Insert the freezing nodes after the private declarations to
1736 -- ensure that we analyze its aspects; needed to ensure that
1737 -- global entities referenced in the aspects are properly handled.
1739 if Ada_Version >= Ada_2012
1740 and then Is_Non_Empty_List (Priv_Decls)
1741 then
1742 Insert_List_After_And_Analyze
1743 (Last (Priv_Decls), Freeze_Entity (Id, Last (Priv_Decls)));
1744 end if;
1746 Set_Visible_Declarations (Orig_Spec, Empty_List);
1747 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1748 Save_Global_References (Orig_Decl);
1749 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1750 Set_Visible_Declarations (Orig_Spec, Save_Vis);
1751 end;
1752 end if;
1754 Process_End_Label (N, 'e', Id);
1756 -- Remove private_with_clauses of enclosing compilation unit, if they
1757 -- were installed.
1759 if Private_With_Clauses_Installed then
1760 Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
1761 end if;
1763 -- For the case of a library level package, we must go through all the
1764 -- entities clearing the indications that the value may be constant and
1765 -- not modified. Why? Because any client of this package may modify
1766 -- these values freely from anywhere. This also applies to any nested
1767 -- packages or generic packages.
1769 -- For now we unconditionally clear constants for packages that are
1770 -- instances of generic packages. The reason is that we do not have the
1771 -- body yet, and we otherwise think things are unreferenced when they
1772 -- are not. This should be fixed sometime (the effect is not terrible,
1773 -- we just lose some warnings, and also some cases of value propagation)
1774 -- ???
1776 if Is_Library_Level_Entity (Id)
1777 or else Is_Generic_Instance (Id)
1778 then
1779 Clear_Constants (Id, First_Entity (Id));
1780 Clear_Constants (Id, First_Private_Entity (Id));
1781 end if;
1783 -- Issue an error in SPARK mode if a package specification contains
1784 -- more than one tagged type or type extension.
1786 Check_One_Tagged_Type_Or_Extension_At_Most;
1788 -- Output relevant information as to why the package requires a body.
1789 -- Do not consider generated packages as this exposes internal symbols
1790 -- and leads to confusing messages.
1792 if List_Body_Required_Info
1793 and then In_Extended_Main_Source_Unit (Id)
1794 and then Unit_Requires_Body (Id)
1795 and then Comes_From_Source (Id)
1796 then
1797 Unit_Requires_Body_Info (Id);
1798 end if;
1799 end Analyze_Package_Specification;
1801 --------------------------------------
1802 -- Analyze_Private_Type_Declaration --
1803 --------------------------------------
1805 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1806 Id : constant Entity_Id := Defining_Identifier (N);
1807 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
1809 begin
1810 Generate_Definition (Id);
1811 Set_Is_Pure (Id, PF);
1812 Init_Size_Align (Id);
1814 if not Is_Package_Or_Generic_Package (Current_Scope)
1815 or else In_Private_Part (Current_Scope)
1816 then
1817 Error_Msg_N ("invalid context for private declaration", N);
1818 end if;
1820 New_Private_Type (N, Id, N);
1821 Set_Depends_On_Private (Id);
1823 if Has_Aspects (N) then
1824 Analyze_Aspect_Specifications (N, Id);
1825 end if;
1826 end Analyze_Private_Type_Declaration;
1828 ----------------------------------
1829 -- Check_Anonymous_Access_Types --
1830 ----------------------------------
1832 procedure Check_Anonymous_Access_Types
1833 (Spec_Id : Entity_Id;
1834 P_Body : Node_Id)
1836 E : Entity_Id;
1837 IR : Node_Id;
1839 begin
1840 -- Itype references are only needed by gigi, to force elaboration of
1841 -- itypes. In the absence of code generation, they are not needed.
1843 if not Expander_Active then
1844 return;
1845 end if;
1847 E := First_Entity (Spec_Id);
1848 while Present (E) loop
1849 if Ekind (E) = E_Anonymous_Access_Type
1850 and then From_Limited_With (E)
1851 then
1852 IR := Make_Itype_Reference (Sloc (P_Body));
1853 Set_Itype (IR, E);
1855 if No (Declarations (P_Body)) then
1856 Set_Declarations (P_Body, New_List (IR));
1857 else
1858 Prepend (IR, Declarations (P_Body));
1859 end if;
1860 end if;
1862 Next_Entity (E);
1863 end loop;
1864 end Check_Anonymous_Access_Types;
1866 -------------------------------------------
1867 -- Declare_Inherited_Private_Subprograms --
1868 -------------------------------------------
1870 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1872 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1873 -- Check whether an inherited subprogram S is an operation of an
1874 -- untagged derived type T.
1876 ---------------------
1877 -- Is_Primitive_Of --
1878 ---------------------
1880 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1881 Formal : Entity_Id;
1883 begin
1884 -- If the full view is a scalar type, the type is the anonymous base
1885 -- type, but the operation mentions the first subtype, so check the
1886 -- signature against the base type.
1888 if Base_Type (Etype (S)) = Base_Type (T) then
1889 return True;
1891 else
1892 Formal := First_Formal (S);
1893 while Present (Formal) loop
1894 if Base_Type (Etype (Formal)) = Base_Type (T) then
1895 return True;
1896 end if;
1898 Next_Formal (Formal);
1899 end loop;
1901 return False;
1902 end if;
1903 end Is_Primitive_Of;
1905 -- Local variables
1907 E : Entity_Id;
1908 Op_List : Elist_Id;
1909 Op_Elmt : Elmt_Id;
1910 Op_Elmt_2 : Elmt_Id;
1911 Prim_Op : Entity_Id;
1912 New_Op : Entity_Id := Empty;
1913 Parent_Subp : Entity_Id;
1914 Tag : Entity_Id;
1916 -- Start of processing for Declare_Inherited_Private_Subprograms
1918 begin
1919 E := First_Entity (Id);
1920 while Present (E) loop
1922 -- If the entity is a nonprivate type extension whose parent type
1923 -- is declared in an open scope, then the type may have inherited
1924 -- operations that now need to be made visible. Ditto if the entity
1925 -- is a formal derived type in a child unit.
1927 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
1928 or else
1929 (Nkind (Parent (E)) = N_Private_Extension_Declaration
1930 and then Is_Generic_Type (E)))
1931 and then In_Open_Scopes (Scope (Etype (E)))
1932 and then Is_Base_Type (E)
1933 then
1934 if Is_Tagged_Type (E) then
1935 Op_List := Primitive_Operations (E);
1936 New_Op := Empty;
1937 Tag := First_Tag_Component (E);
1939 Op_Elmt := First_Elmt (Op_List);
1940 while Present (Op_Elmt) loop
1941 Prim_Op := Node (Op_Elmt);
1943 -- Search primitives that are implicit operations with an
1944 -- internal name whose parent operation has a normal name.
1946 if Present (Alias (Prim_Op))
1947 and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
1948 and then not Comes_From_Source (Prim_Op)
1949 and then Is_Internal_Name (Chars (Prim_Op))
1950 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1951 then
1952 Parent_Subp := Alias (Prim_Op);
1954 -- Case 1: Check if the type has also an explicit
1955 -- overriding for this primitive.
1957 Op_Elmt_2 := Next_Elmt (Op_Elmt);
1958 while Present (Op_Elmt_2) loop
1960 -- Skip entities with attribute Interface_Alias since
1961 -- they are not overriding primitives (these entities
1962 -- link an interface primitive with their covering
1963 -- primitive)
1965 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
1966 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
1967 and then No (Interface_Alias (Node (Op_Elmt_2)))
1968 then
1969 -- The private inherited operation has been
1970 -- overridden by an explicit subprogram:
1971 -- replace the former by the latter.
1973 New_Op := Node (Op_Elmt_2);
1974 Replace_Elmt (Op_Elmt, New_Op);
1975 Remove_Elmt (Op_List, Op_Elmt_2);
1976 Set_Overridden_Operation (New_Op, Parent_Subp);
1978 -- We don't need to inherit its dispatching slot.
1979 -- Set_All_DT_Position has previously ensured that
1980 -- the same slot was assigned to the two primitives
1982 if Present (Tag)
1983 and then Present (DTC_Entity (New_Op))
1984 and then Present (DTC_Entity (Prim_Op))
1985 then
1986 pragma Assert
1987 (DT_Position (New_Op) = DT_Position (Prim_Op));
1988 null;
1989 end if;
1991 goto Next_Primitive;
1992 end if;
1994 Next_Elmt (Op_Elmt_2);
1995 end loop;
1997 -- Case 2: We have not found any explicit overriding and
1998 -- hence we need to declare the operation (i.e., make it
1999 -- visible).
2001 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2003 -- Inherit the dispatching slot if E is already frozen
2005 if Is_Frozen (E)
2006 and then Present (DTC_Entity (Alias (Prim_Op)))
2007 then
2008 Set_DTC_Entity_Value (E, New_Op);
2009 Set_DT_Position_Value (New_Op,
2010 DT_Position (Alias (Prim_Op)));
2011 end if;
2013 pragma Assert
2014 (Is_Dispatching_Operation (New_Op)
2015 and then Node (Last_Elmt (Op_List)) = New_Op);
2017 -- Substitute the new operation for the old one in the
2018 -- type's primitive operations list. Since the new
2019 -- operation was also just added to the end of list,
2020 -- the last element must be removed.
2022 -- (Question: is there a simpler way of declaring the
2023 -- operation, say by just replacing the name of the
2024 -- earlier operation, reentering it in the in the symbol
2025 -- table (how?), and marking it as private???)
2027 Replace_Elmt (Op_Elmt, New_Op);
2028 Remove_Last_Elmt (Op_List);
2029 end if;
2031 <<Next_Primitive>>
2032 Next_Elmt (Op_Elmt);
2033 end loop;
2035 -- Generate listing showing the contents of the dispatch table
2037 if Debug_Flag_ZZ then
2038 Write_DT (E);
2039 end if;
2041 else
2042 -- For untagged type, scan forward to locate inherited hidden
2043 -- operations.
2045 Prim_Op := Next_Entity (E);
2046 while Present (Prim_Op) loop
2047 if Is_Subprogram (Prim_Op)
2048 and then Present (Alias (Prim_Op))
2049 and then not Comes_From_Source (Prim_Op)
2050 and then Is_Internal_Name (Chars (Prim_Op))
2051 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
2052 and then Is_Primitive_Of (E, Prim_Op)
2053 then
2054 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2055 end if;
2057 Next_Entity (Prim_Op);
2059 -- Derived operations appear immediately after the type
2060 -- declaration (or the following subtype indication for
2061 -- a derived scalar type). Further declarations cannot
2062 -- include inherited operations of the type.
2064 if Present (Prim_Op) then
2065 exit when Ekind (Prim_Op) not in Overloadable_Kind;
2066 end if;
2067 end loop;
2068 end if;
2069 end if;
2071 Next_Entity (E);
2072 end loop;
2073 end Declare_Inherited_Private_Subprograms;
2075 -----------------------
2076 -- End_Package_Scope --
2077 -----------------------
2079 procedure End_Package_Scope (P : Entity_Id) is
2080 begin
2081 Uninstall_Declarations (P);
2082 Pop_Scope;
2083 end End_Package_Scope;
2085 ---------------------------
2086 -- Exchange_Declarations --
2087 ---------------------------
2089 procedure Exchange_Declarations (Id : Entity_Id) is
2090 Full_Id : constant Entity_Id := Full_View (Id);
2091 H1 : constant Entity_Id := Homonym (Id);
2092 Next1 : constant Entity_Id := Next_Entity (Id);
2093 H2 : Entity_Id;
2094 Next2 : Entity_Id;
2096 begin
2097 -- If missing full declaration for type, nothing to exchange
2099 if No (Full_Id) then
2100 return;
2101 end if;
2103 -- Otherwise complete the exchange, and preserve semantic links
2105 Next2 := Next_Entity (Full_Id);
2106 H2 := Homonym (Full_Id);
2108 -- Reset full declaration pointer to reflect the switched entities and
2109 -- readjust the next entity chains.
2111 Exchange_Entities (Id, Full_Id);
2113 Set_Next_Entity (Id, Next1);
2114 Set_Homonym (Id, H1);
2116 Set_Full_View (Full_Id, Id);
2117 Set_Next_Entity (Full_Id, Next2);
2118 Set_Homonym (Full_Id, H2);
2119 end Exchange_Declarations;
2121 ----------------------------
2122 -- Install_Package_Entity --
2123 ----------------------------
2125 procedure Install_Package_Entity (Id : Entity_Id) is
2126 begin
2127 if not Is_Internal (Id) then
2128 if Debug_Flag_E then
2129 Write_Str ("Install: ");
2130 Write_Name (Chars (Id));
2131 Write_Eol;
2132 end if;
2134 if Is_Child_Unit (Id) then
2135 null;
2137 -- Do not enter implicitly inherited non-overridden subprograms of
2138 -- a tagged type back into visibility if they have non-conformant
2139 -- homographs (Ada RM 8.3 12.3/2).
2141 elsif Is_Hidden_Non_Overridden_Subpgm (Id) then
2142 null;
2144 else
2145 Set_Is_Immediately_Visible (Id);
2146 end if;
2147 end if;
2148 end Install_Package_Entity;
2150 ----------------------------------
2151 -- Install_Private_Declarations --
2152 ----------------------------------
2154 procedure Install_Private_Declarations (P : Entity_Id) is
2155 Id : Entity_Id;
2156 Full : Entity_Id;
2157 Priv_Deps : Elist_Id;
2159 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
2160 -- When the full view of a private type is made available, we do the
2161 -- same for its private dependents under proper visibility conditions.
2162 -- When compiling a grand-chid unit this needs to be done recursively.
2164 -----------------------------
2165 -- Swap_Private_Dependents --
2166 -----------------------------
2168 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
2169 Deps : Elist_Id;
2170 Priv : Entity_Id;
2171 Priv_Elmt : Elmt_Id;
2172 Is_Priv : Boolean;
2174 begin
2175 Priv_Elmt := First_Elmt (Priv_Deps);
2176 while Present (Priv_Elmt) loop
2177 Priv := Node (Priv_Elmt);
2179 -- Before the exchange, verify that the presence of the Full_View
2180 -- field. This field will be empty if the entity has already been
2181 -- installed due to a previous call.
2183 if Present (Full_View (Priv)) and then Is_Visible_Dependent (Priv)
2184 then
2185 if Is_Private_Type (Priv) then
2186 Deps := Private_Dependents (Priv);
2187 Is_Priv := True;
2188 else
2189 Is_Priv := False;
2190 end if;
2192 -- For each subtype that is swapped, we also swap the reference
2193 -- to it in Private_Dependents, to allow access to it when we
2194 -- swap them out in End_Package_Scope.
2196 Replace_Elmt (Priv_Elmt, Full_View (Priv));
2198 -- Ensure that both views of the dependent private subtype are
2199 -- immediately visible if within some open scope. Check full
2200 -- view before exchanging views.
2202 if In_Open_Scopes (Scope (Full_View (Priv))) then
2203 Set_Is_Immediately_Visible (Priv);
2204 end if;
2206 Exchange_Declarations (Priv);
2207 Set_Is_Immediately_Visible
2208 (Priv, In_Open_Scopes (Scope (Priv)));
2210 Set_Is_Potentially_Use_Visible
2211 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
2213 -- Within a child unit, recurse, except in generic child unit,
2214 -- which (unfortunately) handle private_dependents separately.
2216 if Is_Priv
2217 and then Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
2218 and then not Is_Empty_Elmt_List (Deps)
2219 and then not Inside_A_Generic
2220 then
2221 Swap_Private_Dependents (Deps);
2222 end if;
2223 end if;
2225 Next_Elmt (Priv_Elmt);
2226 end loop;
2227 end Swap_Private_Dependents;
2229 -- Start of processing for Install_Private_Declarations
2231 begin
2232 -- First exchange declarations for private types, so that the full
2233 -- declaration is visible. For each private type, we check its
2234 -- Private_Dependents list and also exchange any subtypes of or derived
2235 -- types from it. Finally, if this is a Taft amendment type, the
2236 -- incomplete declaration is irrelevant, and we want to link the
2237 -- eventual full declaration with the original private one so we
2238 -- also skip the exchange.
2240 Id := First_Entity (P);
2241 while Present (Id) and then Id /= First_Private_Entity (P) loop
2242 if Is_Private_Base_Type (Id)
2243 and then Present (Full_View (Id))
2244 and then Comes_From_Source (Full_View (Id))
2245 and then Scope (Full_View (Id)) = Scope (Id)
2246 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
2247 then
2248 -- If there is a use-type clause on the private type, set the full
2249 -- view accordingly.
2251 Set_In_Use (Full_View (Id), In_Use (Id));
2252 Full := Full_View (Id);
2254 if Is_Private_Base_Type (Full)
2255 and then Has_Private_Declaration (Full)
2256 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
2257 and then In_Open_Scopes (Scope (Etype (Full)))
2258 and then In_Package_Body (Current_Scope)
2259 and then not Is_Private_Type (Etype (Full))
2260 then
2261 -- This is the completion of a private type by a derivation
2262 -- from another private type which is not private anymore. This
2263 -- can only happen in a package nested within a child package,
2264 -- when the parent type is defined in the parent unit. At this
2265 -- point the current type is not private either, and we have
2266 -- to install the underlying full view, which is now visible.
2267 -- Save the current full view as well, so that all views can be
2268 -- restored on exit. It may seem that after compiling the child
2269 -- body there are not environments to restore, but the back-end
2270 -- expects those links to be valid, and freeze nodes depend on
2271 -- them.
2273 if No (Full_View (Full))
2274 and then Present (Underlying_Full_View (Full))
2275 then
2276 Set_Full_View (Id, Underlying_Full_View (Full));
2277 Set_Underlying_Full_View (Id, Full);
2278 Set_Is_Underlying_Full_View (Full);
2280 Set_Underlying_Full_View (Full, Empty);
2281 Set_Is_Frozen (Full_View (Id));
2282 end if;
2283 end if;
2285 Priv_Deps := Private_Dependents (Id);
2286 Exchange_Declarations (Id);
2287 Set_Is_Immediately_Visible (Id);
2288 Swap_Private_Dependents (Priv_Deps);
2289 end if;
2291 Next_Entity (Id);
2292 end loop;
2294 -- Next make other declarations in the private part visible as well
2296 Id := First_Private_Entity (P);
2297 while Present (Id) loop
2298 Install_Package_Entity (Id);
2299 Set_Is_Hidden (Id, False);
2300 Next_Entity (Id);
2301 end loop;
2303 -- An abstract state is partially refined when it has at least one
2304 -- Part_Of constituent. Since these constituents are being installed
2305 -- into visibility, update the partial refinement status of any state
2306 -- defined in the associated package, subject to at least one Part_Of
2307 -- constituent.
2309 if Ekind_In (P, E_Generic_Package, E_Package) then
2310 declare
2311 States : constant Elist_Id := Abstract_States (P);
2312 State_Elmt : Elmt_Id;
2313 State_Id : Entity_Id;
2315 begin
2316 if Present (States) then
2317 State_Elmt := First_Elmt (States);
2318 while Present (State_Elmt) loop
2319 State_Id := Node (State_Elmt);
2321 if Present (Part_Of_Constituents (State_Id)) then
2322 Set_Has_Partial_Visible_Refinement (State_Id);
2323 end if;
2325 Next_Elmt (State_Elmt);
2326 end loop;
2327 end if;
2328 end;
2329 end if;
2331 -- Indicate that the private part is currently visible, so it can be
2332 -- properly reset on exit.
2334 Set_In_Private_Part (P);
2335 end Install_Private_Declarations;
2337 ----------------------------------
2338 -- Install_Visible_Declarations --
2339 ----------------------------------
2341 procedure Install_Visible_Declarations (P : Entity_Id) is
2342 Id : Entity_Id;
2343 Last_Entity : Entity_Id;
2345 begin
2346 pragma Assert
2347 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
2349 if Is_Package_Or_Generic_Package (P) then
2350 Last_Entity := First_Private_Entity (P);
2351 else
2352 Last_Entity := Empty;
2353 end if;
2355 Id := First_Entity (P);
2356 while Present (Id) and then Id /= Last_Entity loop
2357 Install_Package_Entity (Id);
2358 Next_Entity (Id);
2359 end loop;
2360 end Install_Visible_Declarations;
2362 --------------------------
2363 -- Is_Private_Base_Type --
2364 --------------------------
2366 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
2367 begin
2368 return Ekind (E) = E_Private_Type
2369 or else Ekind (E) = E_Limited_Private_Type
2370 or else Ekind (E) = E_Record_Type_With_Private;
2371 end Is_Private_Base_Type;
2373 --------------------------
2374 -- Is_Visible_Dependent --
2375 --------------------------
2377 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
2379 S : constant Entity_Id := Scope (Dep);
2381 begin
2382 -- Renamings created for actual types have the visibility of the actual
2384 if Ekind (S) = E_Package
2385 and then Is_Generic_Instance (S)
2386 and then (Is_Generic_Actual_Type (Dep)
2387 or else Is_Generic_Actual_Type (Full_View (Dep)))
2388 then
2389 return True;
2391 elsif not (Is_Derived_Type (Dep))
2392 and then Is_Derived_Type (Full_View (Dep))
2393 then
2394 -- When instantiating a package body, the scope stack is empty, so
2395 -- check instead whether the dependent type is defined in the same
2396 -- scope as the instance itself.
2398 return In_Open_Scopes (S)
2399 or else (Is_Generic_Instance (Current_Scope)
2400 and then Scope (Dep) = Scope (Current_Scope));
2401 else
2402 return True;
2403 end if;
2404 end Is_Visible_Dependent;
2406 ----------------------------
2407 -- May_Need_Implicit_Body --
2408 ----------------------------
2410 procedure May_Need_Implicit_Body (E : Entity_Id) is
2411 P : constant Node_Id := Unit_Declaration_Node (E);
2412 S : constant Node_Id := Parent (P);
2413 B : Node_Id;
2414 Decls : List_Id;
2416 begin
2417 if not Has_Completion (E)
2418 and then Nkind (P) = N_Package_Declaration
2419 and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
2420 then
2421 B :=
2422 Make_Package_Body (Sloc (E),
2423 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
2424 Chars => Chars (E)),
2425 Declarations => New_List);
2427 if Nkind (S) = N_Package_Specification then
2428 if Present (Private_Declarations (S)) then
2429 Decls := Private_Declarations (S);
2430 else
2431 Decls := Visible_Declarations (S);
2432 end if;
2433 else
2434 Decls := Declarations (S);
2435 end if;
2437 Append (B, Decls);
2438 Analyze (B);
2439 end if;
2440 end May_Need_Implicit_Body;
2442 ----------------------
2443 -- New_Private_Type --
2444 ----------------------
2446 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
2447 begin
2448 -- For other than Ada 2012, enter the name in the current scope
2450 if Ada_Version < Ada_2012 then
2451 Enter_Name (Id);
2453 -- Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2454 -- there may be an incomplete previous view.
2456 else
2457 declare
2458 Prev : Entity_Id;
2459 begin
2460 Prev := Find_Type_Name (N);
2461 pragma Assert (Prev = Id
2462 or else (Ekind (Prev) = E_Incomplete_Type
2463 and then Present (Full_View (Prev))
2464 and then Full_View (Prev) = Id));
2465 end;
2466 end if;
2468 if Limited_Present (Def) then
2469 Set_Ekind (Id, E_Limited_Private_Type);
2470 else
2471 Set_Ekind (Id, E_Private_Type);
2472 end if;
2474 Set_Etype (Id, Id);
2475 Set_Has_Delayed_Freeze (Id);
2476 Set_Is_First_Subtype (Id);
2477 Init_Size_Align (Id);
2479 Set_Is_Constrained (Id,
2480 No (Discriminant_Specifications (N))
2481 and then not Unknown_Discriminants_Present (N));
2483 -- Set tagged flag before processing discriminants, to catch illegal
2484 -- usage.
2486 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
2488 Set_Discriminant_Constraint (Id, No_Elist);
2489 Set_Stored_Constraint (Id, No_Elist);
2491 if Present (Discriminant_Specifications (N)) then
2492 Push_Scope (Id);
2493 Process_Discriminants (N);
2494 End_Scope;
2496 elsif Unknown_Discriminants_Present (N) then
2497 Set_Has_Unknown_Discriminants (Id);
2498 end if;
2500 Set_Private_Dependents (Id, New_Elmt_List);
2502 if Tagged_Present (Def) then
2503 Set_Ekind (Id, E_Record_Type_With_Private);
2504 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2505 Set_Is_Abstract_Type (Id, Abstract_Present (Def));
2506 Set_Is_Limited_Record (Id, Limited_Present (Def));
2507 Set_Has_Delayed_Freeze (Id, True);
2509 -- Recognize Ada.Real_Time.Timing_Events.Timing_Events here
2511 if Is_RTE (Id, RE_Timing_Event) then
2512 Set_Has_Timing_Event (Id);
2513 end if;
2515 -- Create a class-wide type with the same attributes
2517 Make_Class_Wide_Type (Id);
2519 elsif Abstract_Present (Def) then
2520 Error_Msg_N ("only a tagged type can be abstract", N);
2521 end if;
2522 end New_Private_Type;
2524 ---------------------------------
2525 -- Requires_Completion_In_Body --
2526 ---------------------------------
2528 function Requires_Completion_In_Body
2529 (Id : Entity_Id;
2530 Pack_Id : Entity_Id;
2531 Do_Abstract_States : Boolean := False) return Boolean
2533 begin
2534 -- Always ignore child units. Child units get added to the entity list
2535 -- of a parent unit, but are not original entities of the parent, and
2536 -- so do not affect whether the parent needs a body.
2538 if Is_Child_Unit (Id) then
2539 return False;
2541 -- Ignore formal packages and their renamings
2543 elsif Ekind (Id) = E_Package
2544 and then Nkind (Original_Node (Unit_Declaration_Node (Id))) =
2545 N_Formal_Package_Declaration
2546 then
2547 return False;
2549 -- Otherwise test to see if entity requires a completion. Note that
2550 -- subprogram entities whose declaration does not come from source are
2551 -- ignored here on the basis that we assume the expander will provide an
2552 -- implicit completion at some point.
2554 elsif (Is_Overloadable (Id)
2555 and then not Ekind_In (Id, E_Enumeration_Literal, E_Operator)
2556 and then not Is_Abstract_Subprogram (Id)
2557 and then not Has_Completion (Id)
2558 and then Comes_From_Source (Parent (Id)))
2560 or else
2561 (Ekind (Id) = E_Package
2562 and then Id /= Pack_Id
2563 and then not Has_Completion (Id)
2564 and then Unit_Requires_Body (Id, Do_Abstract_States))
2566 or else
2567 (Ekind (Id) = E_Incomplete_Type
2568 and then No (Full_View (Id))
2569 and then not Is_Generic_Type (Id))
2571 or else
2572 (Ekind_In (Id, E_Task_Type, E_Protected_Type)
2573 and then not Has_Completion (Id))
2575 or else
2576 (Ekind (Id) = E_Generic_Package
2577 and then Id /= Pack_Id
2578 and then not Has_Completion (Id)
2579 and then Unit_Requires_Body (Id, Do_Abstract_States))
2581 or else
2582 (Is_Generic_Subprogram (Id)
2583 and then not Has_Completion (Id))
2584 then
2585 return True;
2587 -- Otherwise the entity does not require completion in a package body
2589 else
2590 return False;
2591 end if;
2592 end Requires_Completion_In_Body;
2594 ----------------------------
2595 -- Uninstall_Declarations --
2596 ----------------------------
2598 procedure Uninstall_Declarations (P : Entity_Id) is
2599 Decl : constant Node_Id := Unit_Declaration_Node (P);
2600 Id : Entity_Id;
2601 Full : Entity_Id;
2602 Priv_Elmt : Elmt_Id;
2603 Priv_Sub : Entity_Id;
2605 procedure Preserve_Full_Attributes (Priv : Entity_Id; Full : Entity_Id);
2606 -- Copy to the private declaration the attributes of the full view that
2607 -- need to be available for the partial view also.
2609 function Type_In_Use (T : Entity_Id) return Boolean;
2610 -- Check whether type or base type appear in an active use_type clause
2612 ------------------------------
2613 -- Preserve_Full_Attributes --
2614 ------------------------------
2616 procedure Preserve_Full_Attributes
2617 (Priv : Entity_Id;
2618 Full : Entity_Id)
2620 Full_Base : constant Entity_Id := Base_Type (Full);
2621 Priv_Is_Base_Type : constant Boolean := Is_Base_Type (Priv);
2623 begin
2624 Set_Size_Info (Priv, Full);
2625 Set_RM_Size (Priv, RM_Size (Full));
2626 Set_Size_Known_At_Compile_Time
2627 (Priv, Size_Known_At_Compile_Time (Full));
2628 Set_Is_Volatile (Priv, Is_Volatile (Full));
2629 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
2630 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full));
2631 Set_Is_Ada_2012_Only (Priv, Is_Ada_2012_Only (Full));
2632 Set_Has_Pragma_Unmodified (Priv, Has_Pragma_Unmodified (Full));
2633 Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced (Full));
2634 Set_Has_Pragma_Unreferenced_Objects
2635 (Priv, Has_Pragma_Unreferenced_Objects
2636 (Full));
2637 if Is_Unchecked_Union (Full) then
2638 Set_Is_Unchecked_Union (Base_Type (Priv));
2639 end if;
2640 -- Why is atomic not copied here ???
2642 if Referenced (Full) then
2643 Set_Referenced (Priv);
2644 end if;
2646 if Priv_Is_Base_Type then
2647 Set_Is_Controlled (Priv, Is_Controlled (Full_Base));
2648 Set_Finalize_Storage_Only
2649 (Priv, Finalize_Storage_Only (Full_Base));
2650 Set_Has_Controlled_Component
2651 (Priv, Has_Controlled_Component (Full_Base));
2653 Propagate_Concurrent_Flags (Priv, Base_Type (Full));
2654 end if;
2656 Set_Freeze_Node (Priv, Freeze_Node (Full));
2658 -- Propagate Default_Initial_Condition-related attributes from the
2659 -- base type of the full view to the full view and vice versa. This
2660 -- may seem strange, but is necessary depending on which type
2661 -- triggered the generation of the DIC procedure body. As a result,
2662 -- both the full view and its base type carry the same DIC-related
2663 -- information.
2665 Propagate_DIC_Attributes (Full, From_Typ => Full_Base);
2666 Propagate_DIC_Attributes (Full_Base, From_Typ => Full);
2668 -- Propagate Default_Initial_Condition-related attributes from the
2669 -- full view to the private view.
2671 Propagate_DIC_Attributes (Priv, From_Typ => Full);
2673 -- Propagate invariant-related attributes from the base type of the
2674 -- full view to the full view and vice versa. This may seem strange,
2675 -- but is necessary depending on which type triggered the generation
2676 -- of the invariant procedure body. As a result, both the full view
2677 -- and its base type carry the same invariant-related information.
2679 Propagate_Invariant_Attributes (Full, From_Typ => Full_Base);
2680 Propagate_Invariant_Attributes (Full_Base, From_Typ => Full);
2682 -- Propagate invariant-related attributes from the full view to the
2683 -- private view.
2685 Propagate_Invariant_Attributes (Priv, From_Typ => Full);
2687 if Is_Tagged_Type (Priv)
2688 and then Is_Tagged_Type (Full)
2689 and then not Error_Posted (Full)
2690 then
2691 if Is_Tagged_Type (Priv) then
2693 -- If the type is tagged, the tag itself must be available on
2694 -- the partial view, for expansion purposes.
2696 Set_First_Entity (Priv, First_Entity (Full));
2698 -- If there are discriminants in the partial view, these remain
2699 -- visible. Otherwise only the tag itself is visible, and there
2700 -- are no nameable components in the partial view.
2702 if No (Last_Entity (Priv)) then
2703 Set_Last_Entity (Priv, First_Entity (Priv));
2704 end if;
2705 end if;
2707 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2709 if Has_Discriminants (Full) then
2710 Set_Discriminant_Constraint (Priv,
2711 Discriminant_Constraint (Full));
2712 end if;
2713 end if;
2714 end Preserve_Full_Attributes;
2716 -----------------
2717 -- Type_In_Use --
2718 -----------------
2720 function Type_In_Use (T : Entity_Id) return Boolean is
2721 begin
2722 return Scope (Base_Type (T)) = P
2723 and then (In_Use (T) or else In_Use (Base_Type (T)));
2724 end Type_In_Use;
2726 -- Start of processing for Uninstall_Declarations
2728 begin
2729 Id := First_Entity (P);
2730 while Present (Id) and then Id /= First_Private_Entity (P) loop
2731 if Debug_Flag_E then
2732 Write_Str ("unlinking visible entity ");
2733 Write_Int (Int (Id));
2734 Write_Eol;
2735 end if;
2737 -- On exit from the package scope, we must preserve the visibility
2738 -- established by use clauses in the current scope. Two cases:
2740 -- a) If the entity is an operator, it may be a primitive operator of
2741 -- a type for which there is a visible use-type clause.
2743 -- b) for other entities, their use-visibility is determined by a
2744 -- visible use clause for the package itself. For a generic instance,
2745 -- the instantiation of the formals appears in the visible part,
2746 -- but the formals are private and remain so.
2748 if Ekind (Id) = E_Function
2749 and then Is_Operator_Symbol_Name (Chars (Id))
2750 and then not Is_Hidden (Id)
2751 and then not Error_Posted (Id)
2752 then
2753 Set_Is_Potentially_Use_Visible (Id,
2754 In_Use (P)
2755 or else Type_In_Use (Etype (Id))
2756 or else Type_In_Use (Etype (First_Formal (Id)))
2757 or else (Present (Next_Formal (First_Formal (Id)))
2758 and then
2759 Type_In_Use
2760 (Etype (Next_Formal (First_Formal (Id))))));
2761 else
2762 if In_Use (P) and then not Is_Hidden (Id) then
2764 -- A child unit of a use-visible package remains use-visible
2765 -- only if it is itself a visible child unit. Otherwise it
2766 -- would remain visible in other contexts where P is use-
2767 -- visible, because once compiled it stays in the entity list
2768 -- of its parent unit.
2770 if Is_Child_Unit (Id) then
2771 Set_Is_Potentially_Use_Visible
2772 (Id, Is_Visible_Lib_Unit (Id));
2773 else
2774 Set_Is_Potentially_Use_Visible (Id);
2775 end if;
2777 else
2778 Set_Is_Potentially_Use_Visible (Id, False);
2779 end if;
2780 end if;
2782 -- Local entities are not immediately visible outside of the package
2784 Set_Is_Immediately_Visible (Id, False);
2786 -- If this is a private type with a full view (for example a local
2787 -- subtype of a private type declared elsewhere), ensure that the
2788 -- full view is also removed from visibility: it may be exposed when
2789 -- swapping views in an instantiation. Similarly, ensure that the
2790 -- use-visibility is properly set on both views.
2792 if Is_Type (Id) and then Present (Full_View (Id)) then
2793 Set_Is_Immediately_Visible (Full_View (Id), False);
2794 Set_Is_Potentially_Use_Visible (Full_View (Id),
2795 Is_Potentially_Use_Visible (Id));
2796 end if;
2798 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2799 Check_Abstract_Overriding (Id);
2800 Check_Conventions (Id);
2801 end if;
2803 if Ekind_In (Id, E_Private_Type, E_Limited_Private_Type)
2804 and then No (Full_View (Id))
2805 and then not Is_Generic_Type (Id)
2806 and then not Is_Derived_Type (Id)
2807 then
2808 Error_Msg_N ("missing full declaration for private type&", Id);
2810 elsif Ekind (Id) = E_Record_Type_With_Private
2811 and then not Is_Generic_Type (Id)
2812 and then No (Full_View (Id))
2813 then
2814 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
2815 Error_Msg_N ("missing full declaration for private type&", Id);
2816 else
2817 Error_Msg_N
2818 ("missing full declaration for private extension", Id);
2819 end if;
2821 -- Case of constant, check for deferred constant declaration with
2822 -- no full view. Likely just a matter of a missing expression, or
2823 -- accidental use of the keyword constant.
2825 elsif Ekind (Id) = E_Constant
2827 -- OK if constant value present
2829 and then No (Constant_Value (Id))
2831 -- OK if full view present
2833 and then No (Full_View (Id))
2835 -- OK if imported, since that provides the completion
2837 and then not Is_Imported (Id)
2839 -- OK if object declaration replaced by renaming declaration as
2840 -- a result of OK_To_Rename processing (e.g. for concatenation)
2842 and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration
2844 -- OK if object declaration with the No_Initialization flag set
2846 and then not (Nkind (Parent (Id)) = N_Object_Declaration
2847 and then No_Initialization (Parent (Id)))
2848 then
2849 -- If no private declaration is present, we assume the user did
2850 -- not intend a deferred constant declaration and the problem
2851 -- is simply that the initializing expression is missing.
2853 if not Has_Private_Declaration (Etype (Id)) then
2855 -- We assume that the user did not intend a deferred constant
2856 -- declaration, and the expression is just missing.
2858 Error_Msg_N
2859 ("constant declaration requires initialization expression",
2860 Parent (Id));
2862 if Is_Limited_Type (Etype (Id)) then
2863 Error_Msg_N
2864 ("\if variable intended, remove CONSTANT from declaration",
2865 Parent (Id));
2866 end if;
2868 -- Otherwise if a private declaration is present, then we are
2869 -- missing the full declaration for the deferred constant.
2871 else
2872 Error_Msg_N
2873 ("missing full declaration for deferred constant (RM 7.4)",
2874 Id);
2876 if Is_Limited_Type (Etype (Id)) then
2877 Error_Msg_N
2878 ("\if variable intended, remove CONSTANT from declaration",
2879 Parent (Id));
2880 end if;
2881 end if;
2882 end if;
2884 Next_Entity (Id);
2885 end loop;
2887 -- If the specification was installed as the parent of a public child
2888 -- unit, the private declarations were not installed, and there is
2889 -- nothing to do.
2891 if not In_Private_Part (P) then
2892 return;
2893 else
2894 Set_In_Private_Part (P, False);
2895 end if;
2897 -- Make private entities invisible and exchange full and private
2898 -- declarations for private types. Id is now the first private entity
2899 -- in the package.
2901 while Present (Id) loop
2902 if Debug_Flag_E then
2903 Write_Str ("unlinking private entity ");
2904 Write_Int (Int (Id));
2905 Write_Eol;
2906 end if;
2908 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2909 Check_Abstract_Overriding (Id);
2910 Check_Conventions (Id);
2911 end if;
2913 Set_Is_Immediately_Visible (Id, False);
2915 if Is_Private_Base_Type (Id) and then Present (Full_View (Id)) then
2916 Full := Full_View (Id);
2918 -- If the partial view is not declared in the visible part of the
2919 -- package (as is the case when it is a type derived from some
2920 -- other private type in the private part of the current package),
2921 -- no exchange takes place.
2923 if No (Parent (Id))
2924 or else List_Containing (Parent (Id)) /=
2925 Visible_Declarations (Specification (Decl))
2926 then
2927 goto Next_Id;
2928 end if;
2930 -- The entry in the private part points to the full declaration,
2931 -- which is currently visible. Exchange them so only the private
2932 -- type declaration remains accessible, and link private and full
2933 -- declaration in the opposite direction. Before the actual
2934 -- exchange, we copy back attributes of the full view that must
2935 -- be available to the partial view too.
2937 Preserve_Full_Attributes (Id, Full);
2939 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
2941 -- The following test may be redundant, as this is already
2942 -- diagnosed in sem_ch3. ???
2944 if not Is_Definite_Subtype (Full)
2945 and then Is_Definite_Subtype (Id)
2946 then
2947 Error_Msg_Sloc := Sloc (Parent (Id));
2948 Error_Msg_NE
2949 ("full view of& not compatible with declaration#", Full, Id);
2950 end if;
2952 -- Swap out the subtypes and derived types of Id that
2953 -- were compiled in this scope, or installed previously
2954 -- by Install_Private_Declarations.
2956 -- Before we do the swap, we verify the presence of the Full_View
2957 -- field which may be empty due to a swap by a previous call to
2958 -- End_Package_Scope (e.g. from the freezing mechanism).
2960 Priv_Elmt := First_Elmt (Private_Dependents (Id));
2961 while Present (Priv_Elmt) loop
2962 Priv_Sub := Node (Priv_Elmt);
2964 if Present (Full_View (Priv_Sub)) then
2965 if Scope (Priv_Sub) = P
2966 or else not In_Open_Scopes (Scope (Priv_Sub))
2967 then
2968 Set_Is_Immediately_Visible (Priv_Sub, False);
2969 end if;
2971 if Is_Visible_Dependent (Priv_Sub) then
2972 Preserve_Full_Attributes
2973 (Priv_Sub, Full_View (Priv_Sub));
2974 Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub));
2975 Exchange_Declarations (Priv_Sub);
2976 end if;
2977 end if;
2979 Next_Elmt (Priv_Elmt);
2980 end loop;
2982 -- Now restore the type itself to its private view
2984 Exchange_Declarations (Id);
2986 -- If we have installed an underlying full view for a type derived
2987 -- from a private type in a child unit, restore the proper views
2988 -- of private and full view. See corresponding code in
2989 -- Install_Private_Declarations.
2991 -- After the exchange, Full denotes the private type in the
2992 -- visible part of the package.
2994 if Is_Private_Base_Type (Full)
2995 and then Present (Full_View (Full))
2996 and then Present (Underlying_Full_View (Full))
2997 and then In_Package_Body (Current_Scope)
2998 then
2999 Set_Full_View (Full, Underlying_Full_View (Full));
3000 Set_Underlying_Full_View (Full, Empty);
3001 end if;
3003 elsif Ekind (Id) = E_Incomplete_Type
3004 and then Comes_From_Source (Id)
3005 and then No (Full_View (Id))
3006 then
3007 -- Mark Taft amendment types. Verify that there are no primitive
3008 -- operations declared for the type (3.10.1(9)).
3010 Set_Has_Completion_In_Body (Id);
3012 declare
3013 Elmt : Elmt_Id;
3014 Subp : Entity_Id;
3016 begin
3017 Elmt := First_Elmt (Private_Dependents (Id));
3018 while Present (Elmt) loop
3019 Subp := Node (Elmt);
3021 -- Is_Primitive is tested because there can be cases where
3022 -- nonprimitive subprograms (in nested packages) are added
3023 -- to the Private_Dependents list.
3025 if Is_Overloadable (Subp) and then Is_Primitive (Subp) then
3026 Error_Msg_NE
3027 ("type& must be completed in the private part",
3028 Parent (Subp), Id);
3030 -- The result type of an access-to-function type cannot be a
3031 -- Taft-amendment type, unless the version is Ada 2012 or
3032 -- later (see AI05-151).
3034 elsif Ada_Version < Ada_2012
3035 and then Ekind (Subp) = E_Subprogram_Type
3036 then
3037 if Etype (Subp) = Id
3038 or else
3039 (Is_Class_Wide_Type (Etype (Subp))
3040 and then Etype (Etype (Subp)) = Id)
3041 then
3042 Error_Msg_NE
3043 ("type& must be completed in the private part",
3044 Associated_Node_For_Itype (Subp), Id);
3045 end if;
3046 end if;
3048 Next_Elmt (Elmt);
3049 end loop;
3050 end;
3052 elsif not Is_Child_Unit (Id)
3053 and then (not Is_Private_Type (Id) or else No (Full_View (Id)))
3054 then
3055 Set_Is_Hidden (Id);
3056 Set_Is_Potentially_Use_Visible (Id, False);
3057 end if;
3059 <<Next_Id>>
3060 Next_Entity (Id);
3061 end loop;
3062 end Uninstall_Declarations;
3064 ------------------------
3065 -- Unit_Requires_Body --
3066 ------------------------
3068 function Unit_Requires_Body
3069 (Pack_Id : Entity_Id;
3070 Do_Abstract_States : Boolean := False) return Boolean
3072 E : Entity_Id;
3074 Requires_Body : Boolean := False;
3075 -- Flag set when the unit has at least one construct that requries
3076 -- completion in a body.
3078 begin
3079 -- Imported entity never requires body. Right now, only subprograms can
3080 -- be imported, but perhaps in the future we will allow import of
3081 -- packages.
3083 if Is_Imported (Pack_Id) then
3084 return False;
3086 -- Body required if library package with pragma Elaborate_Body
3088 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3089 return True;
3091 -- Body required if subprogram
3093 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3094 return True;
3096 -- Treat a block as requiring a body
3098 elsif Ekind (Pack_Id) = E_Block then
3099 return True;
3101 elsif Ekind (Pack_Id) = E_Package
3102 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3103 and then Present (Generic_Parent (Parent (Pack_Id)))
3104 then
3105 declare
3106 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3107 begin
3108 if Has_Pragma_Elaborate_Body (G_P) then
3109 return True;
3110 end if;
3111 end;
3112 end if;
3114 -- Traverse the entity chain of the package and look for constructs that
3115 -- require a completion in a body.
3117 E := First_Entity (Pack_Id);
3118 while Present (E) loop
3120 -- Skip abstract states because their completion depends on several
3121 -- criteria (see below).
3123 if Ekind (E) = E_Abstract_State then
3124 null;
3126 elsif Requires_Completion_In_Body
3127 (E, Pack_Id, Do_Abstract_States)
3128 then
3129 Requires_Body := True;
3130 exit;
3131 end if;
3133 Next_Entity (E);
3134 end loop;
3136 -- A [generic] package that defines at least one non-null abstract state
3137 -- requires a completion only when at least one other construct requires
3138 -- a completion in a body (SPARK RM 7.1.4(4) and (6)). This check is not
3139 -- performed if the caller requests this behavior.
3141 if Do_Abstract_States
3142 and then Ekind_In (Pack_Id, E_Generic_Package, E_Package)
3143 and then Has_Non_Null_Abstract_State (Pack_Id)
3144 and then Requires_Body
3145 then
3146 return True;
3147 end if;
3149 return Requires_Body;
3150 end Unit_Requires_Body;
3152 -----------------------------
3153 -- Unit_Requires_Body_Info --
3154 -----------------------------
3156 procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id) is
3157 E : Entity_Id;
3159 begin
3160 -- An imported entity never requires body. Right now, only subprograms
3161 -- can be imported, but perhaps in the future we will allow import of
3162 -- packages.
3164 if Is_Imported (Pack_Id) then
3165 return;
3167 -- Body required if library package with pragma Elaborate_Body
3169 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3170 Error_Msg_N ("info: & requires body (Elaborate_Body)?Y?", Pack_Id);
3172 -- Body required if subprogram
3174 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3175 Error_Msg_N ("info: & requires body (subprogram case)?Y?", Pack_Id);
3177 -- Body required if generic parent has Elaborate_Body
3179 elsif Ekind (Pack_Id) = E_Package
3180 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3181 and then Present (Generic_Parent (Parent (Pack_Id)))
3182 then
3183 declare
3184 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3185 begin
3186 if Has_Pragma_Elaborate_Body (G_P) then
3187 Error_Msg_N
3188 ("info: & requires body (generic parent Elaborate_Body)?Y?",
3189 Pack_Id);
3190 end if;
3191 end;
3193 -- A [generic] package that introduces at least one non-null abstract
3194 -- state requires completion. However, there is a separate rule that
3195 -- requires that such a package have a reason other than this for a
3196 -- body being required (if necessary a pragma Elaborate_Body must be
3197 -- provided). If Ignore_Abstract_State is True, we don't do this check
3198 -- (so we can use Unit_Requires_Body to check for some other reason).
3200 elsif Ekind_In (Pack_Id, E_Generic_Package, E_Package)
3201 and then Present (Abstract_States (Pack_Id))
3202 and then not Is_Null_State
3203 (Node (First_Elmt (Abstract_States (Pack_Id))))
3204 then
3205 Error_Msg_N
3206 ("info: & requires body (non-null abstract state aspect)?Y?",
3207 Pack_Id);
3208 end if;
3210 -- Otherwise search entity chain for entity requiring completion
3212 E := First_Entity (Pack_Id);
3213 while Present (E) loop
3214 if Requires_Completion_In_Body (E, Pack_Id) then
3215 Error_Msg_Node_2 := E;
3216 Error_Msg_NE
3217 ("info: & requires body (& requires completion)?Y?", E, Pack_Id);
3218 end if;
3220 Next_Entity (E);
3221 end loop;
3222 end Unit_Requires_Body_Info;
3224 end Sem_Ch7;