* tree-loop-distribution.c (struct partition): New field recording
[official-gcc.git] / gcc / ada / sem_ch7.adb
blob0b415d737cb33a628e1c81950c9c5f238db7e303
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 package body Sem_Ch7 is
75 -----------------------------------
76 -- Handling private declarations --
77 -----------------------------------
79 -- The principle that each entity has a single defining occurrence clashes
80 -- with the presence of two separate definitions for private types: the
81 -- first is the private type declaration, and the second is the full type
82 -- declaration. It is important that all references to the type point to
83 -- the same defining occurrence, namely the first one. To enforce the two
84 -- separate views of the entity, the corresponding information is swapped
85 -- between the two declarations. Outside of the package, the defining
86 -- occurrence only contains the private declaration information, while in
87 -- the private part and the body of the package the defining occurrence
88 -- contains the full declaration. To simplify the swap, the defining
89 -- occurrence that currently holds the private declaration points to the
90 -- full declaration. During semantic processing the defining occurrence
91 -- also points to a list of private dependents, that is to say access types
92 -- or composite types whose designated types or component types are
93 -- subtypes or derived types of the private type in question. After the
94 -- full declaration has been seen, the private dependents are updated to
95 -- indicate that they have full definitions.
97 -----------------------
98 -- Local Subprograms --
99 -----------------------
101 procedure Analyze_Package_Body_Helper (N : Node_Id);
102 -- Does all the real work of Analyze_Package_Body
104 procedure Check_Anonymous_Access_Types
105 (Spec_Id : Entity_Id;
106 P_Body : Node_Id);
107 -- If the spec of a package has a limited_with_clause, it may declare
108 -- anonymous access types whose designated type is a limited view, such an
109 -- anonymous access return type for a function. This access type cannot be
110 -- elaborated in the spec itself, but it may need an itype reference if it
111 -- is used within a nested scope. In that case the itype reference is
112 -- created at the beginning of the corresponding package body and inserted
113 -- before other body declarations.
115 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id);
116 -- Called upon entering the private part of a public child package and the
117 -- body of a nested package, to potentially declare certain inherited
118 -- subprograms that were inherited by types in the visible part, but whose
119 -- declaration was deferred because the parent operation was private and
120 -- not visible at that point. These subprograms are located by traversing
121 -- the visible part declarations looking for non-private type extensions
122 -- and then examining each of the primitive operations of such types to
123 -- find those that were inherited but declared with a special internal
124 -- name. Each such operation is now declared as an operation with a normal
125 -- name (using the name of the parent operation) and replaces the previous
126 -- implicit operation in the primitive operations list of the type. If the
127 -- inherited private operation has been overridden, then it's replaced by
128 -- the overriding operation.
130 procedure Install_Package_Entity (Id : Entity_Id);
131 -- Supporting procedure for Install_{Visible,Private}_Declarations. Places
132 -- one entity on its visibility chain, and recurses on the visible part if
133 -- the entity is an inner package.
135 function Is_Private_Base_Type (E : Entity_Id) return Boolean;
136 -- True for a private type that is not a subtype
138 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean;
139 -- If the private dependent is a private type whose full view is derived
140 -- from the parent type, its full properties are revealed only if we are in
141 -- the immediate scope of the private dependent. Should this predicate be
142 -- tightened further???
144 function Requires_Completion_In_Body
145 (Id : Entity_Id;
146 Pack_Id : Entity_Id;
147 Do_Abstract_States : Boolean := False) return Boolean;
148 -- Subsidiary to routines Unit_Requires_Body and Unit_Requires_Body_Info.
149 -- Determine whether entity Id declared in package spec Pack_Id requires
150 -- completion in a package body. Flag Do_Abstract_Stats should be set when
151 -- abstract states are to be considered in the completion test.
153 procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id);
154 -- Outputs info messages showing why package Pack_Id requires a body. The
155 -- caller has checked that the switch requesting this information is set,
156 -- and that the package does indeed require a body.
158 --------------------------
159 -- Analyze_Package_Body --
160 --------------------------
162 procedure Analyze_Package_Body (N : Node_Id) is
163 Loc : constant Source_Ptr := Sloc (N);
165 begin
166 if Debug_Flag_C then
167 Write_Str ("==> package body ");
168 Write_Name (Chars (Defining_Entity (N)));
169 Write_Str (" from ");
170 Write_Location (Loc);
171 Write_Eol;
172 Indent;
173 end if;
175 -- The real work is split out into the helper, so it can do "return;"
176 -- without skipping the debug output.
178 Analyze_Package_Body_Helper (N);
180 if Debug_Flag_C then
181 Outdent;
182 Write_Str ("<== package body ");
183 Write_Name (Chars (Defining_Entity (N)));
184 Write_Str (" from ");
185 Write_Location (Loc);
186 Write_Eol;
187 end if;
188 end Analyze_Package_Body;
190 ---------------------------------
191 -- Analyze_Package_Body_Helper --
192 ---------------------------------
194 -- WARNING: This routine manages Ghost regions. Return statements must be
195 -- replaced by gotos which jump to the end of the routine and restore the
196 -- Ghost mode.
198 procedure Analyze_Package_Body_Helper (N : Node_Id) is
199 procedure Hide_Public_Entities (Decls : List_Id);
200 -- Attempt to hide all public entities found in declarative list Decls
201 -- by resetting their Is_Public flag to False depending on whether the
202 -- entities are not referenced by inlined or generic bodies. This kind
203 -- of processing is a conservative approximation and may still leave
204 -- certain entities externally visible.
206 procedure Install_Composite_Operations (P : Entity_Id);
207 -- Composite types declared in the current scope may depend on types
208 -- that were private at the point of declaration, and whose full view
209 -- is now in scope. Indicate that the corresponding operations on the
210 -- composite type are available.
212 --------------------------
213 -- Hide_Public_Entities --
214 --------------------------
216 procedure Hide_Public_Entities (Decls : List_Id) is
217 function Contains_Subprograms_Refs (N : Node_Id) return Boolean;
218 -- Subsidiary to routine Has_Referencer. Determine whether a node
219 -- contains a reference to a subprogram.
220 -- WARNING: this is a very expensive routine as it performs a full
221 -- tree traversal.
223 function Has_Referencer
224 (Decls : List_Id;
225 Top_Level : Boolean := False) return Boolean;
226 -- A "referencer" is a construct which may reference a previous
227 -- declaration. Examine all declarations in list Decls in reverse
228 -- and determine whether once such referencer exists. All entities
229 -- in the range Last (Decls) .. Referencer are hidden from external
230 -- visibility.
232 -------------------------------
233 -- Contains_Subprograms_Refs --
234 -------------------------------
236 function Contains_Subprograms_Refs (N : Node_Id) return Boolean is
237 Reference_Seen : Boolean := False;
239 function Is_Subprogram_Ref (N : Node_Id) return Traverse_Result;
240 -- Determine whether a node denotes a reference to a subprogram
242 -----------------------
243 -- Is_Subprogram_Ref --
244 -----------------------
246 function Is_Subprogram_Ref
247 (N : Node_Id) return Traverse_Result
249 Val : Node_Id;
251 begin
252 -- Detect a reference of the form
253 -- Subp_Call
255 if Nkind (N) in N_Subprogram_Call
256 and then Is_Entity_Name (Name (N))
257 then
258 Reference_Seen := True;
259 return Abandon;
261 -- Detect a reference of the form
262 -- Subp'Some_Attribute
264 elsif Nkind (N) = N_Attribute_Reference
265 and then Is_Entity_Name (Prefix (N))
266 and then Present (Entity (Prefix (N)))
267 and then Is_Subprogram (Entity (Prefix (N)))
268 then
269 Reference_Seen := True;
270 return Abandon;
272 -- Constants can be substituted by their value in gigi, which
273 -- may contain a reference, so be conservative for them.
275 elsif Is_Entity_Name (N)
276 and then Present (Entity (N))
277 and then Ekind (Entity (N)) = E_Constant
278 then
279 Val := Constant_Value (Entity (N));
281 if Present (Val)
282 and then not Compile_Time_Known_Value (Val)
283 then
284 Reference_Seen := True;
285 return Abandon;
286 end if;
287 end if;
289 return OK;
290 end Is_Subprogram_Ref;
292 procedure Find_Subprograms_Ref is
293 new Traverse_Proc (Is_Subprogram_Ref);
295 -- Start of processing for Contains_Subprograms_Refs
297 begin
298 Find_Subprograms_Ref (N);
300 return Reference_Seen;
301 end Contains_Subprograms_Refs;
303 --------------------
304 -- Has_Referencer --
305 --------------------
307 function Has_Referencer
308 (Decls : List_Id;
309 Top_Level : Boolean := False) return Boolean
311 Decl : Node_Id;
312 Decl_Id : Entity_Id;
313 Spec : Node_Id;
315 Has_Non_Subprograms_Referencer : Boolean := False;
316 -- Flag set if a subprogram body was detected as a referencer but
317 -- does not contain references to other subprograms. In this case,
318 -- if we still are top level, we do not return True immediately,
319 -- but keep hiding subprograms from external visibility.
321 begin
322 if No (Decls) then
323 return False;
324 end if;
326 -- Examine all declarations in reverse order, hiding all entities
327 -- from external visibility until a referencer has been found. The
328 -- algorithm recurses into nested packages.
330 Decl := Last (Decls);
331 while Present (Decl) loop
333 -- A stub is always considered a referencer
335 if Nkind (Decl) in N_Body_Stub then
336 return True;
338 -- Package declaration
340 elsif Nkind (Decl) = N_Package_Declaration then
341 Spec := Specification (Decl);
343 -- Inspect the declarations of a non-generic package to try
344 -- and hide more entities from external visibility.
346 if not Is_Generic_Unit (Defining_Entity (Spec)) then
347 if Has_Referencer (Private_Declarations (Spec))
348 or else Has_Referencer (Visible_Declarations (Spec))
349 then
350 return True;
351 end if;
352 end if;
354 -- Package body
356 elsif Nkind (Decl) = N_Package_Body
357 and then Present (Corresponding_Spec (Decl))
358 then
359 Decl_Id := Corresponding_Spec (Decl);
361 -- A generic package body is a referencer. It would seem
362 -- that we only have to consider generics that can be
363 -- exported, i.e. where the corresponding spec is the
364 -- spec of the current package, but because of nested
365 -- instantiations, a fully private generic body may export
366 -- other private body entities. Furthermore, regardless of
367 -- whether there was a previous inlined subprogram, (an
368 -- instantiation of) the generic package may reference any
369 -- entity declared before it.
371 if Is_Generic_Unit (Decl_Id) then
372 return True;
374 -- Inspect the declarations of a non-generic package body to
375 -- try and hide more entities from external visibility.
377 elsif Has_Referencer (Declarations (Decl)) then
378 return True;
379 end if;
381 -- Subprogram body
383 elsif Nkind (Decl) = N_Subprogram_Body then
384 if Present (Corresponding_Spec (Decl)) then
385 Decl_Id := Corresponding_Spec (Decl);
387 -- A generic subprogram body acts as a referencer
389 if Is_Generic_Unit (Decl_Id) then
390 return True;
391 end if;
393 -- An inlined subprogram body acts as a referencer
395 if Is_Inlined (Decl_Id)
396 or else Has_Pragma_Inline (Decl_Id)
397 then
398 -- Inspect the statements of the subprogram body
399 -- to determine whether the body references other
400 -- subprograms.
402 if Top_Level
403 and then not Contains_Subprograms_Refs (Decl)
404 then
405 Has_Non_Subprograms_Referencer := True;
406 else
407 return True;
408 end if;
409 end if;
411 -- Otherwise this is a stand alone subprogram body
413 else
414 Decl_Id := Defining_Entity (Decl);
416 -- An inlined body acts as a referencer. Note that an
417 -- inlined subprogram remains Is_Public as gigi requires
418 -- the flag to be set.
420 -- Note that we test Has_Pragma_Inline here rather than
421 -- Is_Inlined. We are compiling this for a client, and
422 -- it is the client who will decide if actual inlining
423 -- should occur, so we need to assume that the procedure
424 -- could be inlined for the purpose of accessing global
425 -- entities.
427 if Has_Pragma_Inline (Decl_Id) then
428 if Top_Level
429 and then not Contains_Subprograms_Refs (Decl)
430 then
431 Has_Non_Subprograms_Referencer := True;
432 else
433 return True;
434 end if;
435 else
436 Set_Is_Public (Decl_Id, False);
437 end if;
438 end if;
440 -- Exceptions, objects and renamings do not need to be public
441 -- if they are not followed by a construct which can reference
442 -- and export them. The Is_Public flag is reset on top level
443 -- entities only as anything nested is local to its context.
444 -- Likewise for subprograms, but we work harder for them as
445 -- their visibility can have a significant impact on inlining
446 -- decisions in the back end.
448 elsif Nkind_In (Decl, N_Exception_Declaration,
449 N_Object_Declaration,
450 N_Object_Renaming_Declaration,
451 N_Subprogram_Declaration,
452 N_Subprogram_Renaming_Declaration)
453 then
454 Decl_Id := Defining_Entity (Decl);
456 if Top_Level
457 and then not Is_Imported (Decl_Id)
458 and then not Is_Exported (Decl_Id)
459 and then No (Interface_Name (Decl_Id))
460 and then
461 (not Has_Non_Subprograms_Referencer
462 or else Nkind (Decl) = N_Subprogram_Declaration)
463 then
464 Set_Is_Public (Decl_Id, False);
465 end if;
466 end if;
468 Prev (Decl);
469 end loop;
471 return Has_Non_Subprograms_Referencer;
472 end Has_Referencer;
474 -- Local variables
476 Discard : Boolean := True;
477 pragma Unreferenced (Discard);
479 -- Start of processing for Hide_Public_Entities
481 begin
482 -- The algorithm examines the top level declarations of a package
483 -- body in reverse looking for a construct that may export entities
484 -- declared prior to it. If such a scenario is encountered, then all
485 -- entities in the range Last (Decls) .. construct are hidden from
486 -- external visibility. Consider:
488 -- package Pack is
489 -- generic
490 -- package Gen is
491 -- end Gen;
492 -- end Pack;
494 -- package body Pack is
495 -- External_Obj : ...; -- (1)
497 -- package body Gen is -- (2)
498 -- ... External_Obj ... -- (3)
499 -- end Gen;
501 -- Local_Obj : ...; -- (4)
502 -- end Pack;
504 -- In this example Local_Obj (4) must not be externally visible as
505 -- it cannot be exported by anything in Pack. The body of generic
506 -- package Gen (2) on the other hand acts as a "referencer" and may
507 -- export anything declared before it. Since the compiler does not
508 -- perform flow analysis, it is not possible to determine precisely
509 -- which entities will be exported when Gen is instantiated. In the
510 -- example above External_Obj (1) is exported at (3), but this may
511 -- not always be the case. The algorithm takes a conservative stance
512 -- and leaves entity External_Obj public.
514 Discard := Has_Referencer (Decls, Top_Level => True);
515 end Hide_Public_Entities;
517 ----------------------------------
518 -- Install_Composite_Operations --
519 ----------------------------------
521 procedure Install_Composite_Operations (P : Entity_Id) is
522 Id : Entity_Id;
524 begin
525 Id := First_Entity (P);
526 while Present (Id) loop
527 if Is_Type (Id)
528 and then (Is_Limited_Composite (Id)
529 or else Is_Private_Composite (Id))
530 and then No (Private_Component (Id))
531 then
532 Set_Is_Limited_Composite (Id, False);
533 Set_Is_Private_Composite (Id, False);
534 end if;
536 Next_Entity (Id);
537 end loop;
538 end Install_Composite_Operations;
540 -- Local variables
542 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
543 Saved_ISMP : constant Boolean :=
544 Ignore_SPARK_Mode_Pragmas_In_Instance;
545 -- Save the Ghost and SPARK mode-related data to restore on exit
547 Body_Id : Entity_Id;
548 HSS : Node_Id;
549 Last_Spec_Entity : Entity_Id;
550 New_N : Node_Id;
551 Pack_Decl : Node_Id;
552 Spec_Id : Entity_Id;
554 -- Start of processing for Analyze_Package_Body_Helper
556 begin
557 -- Find corresponding package specification, and establish the current
558 -- scope. The visible defining entity for the package is the defining
559 -- occurrence in the spec. On exit from the package body, all body
560 -- declarations are attached to the defining entity for the body, but
561 -- the later is never used for name resolution. In this fashion there
562 -- is only one visible entity that denotes the package.
564 -- Set Body_Id. Note that this will be reset to point to the generic
565 -- copy later on in the generic case.
567 Body_Id := Defining_Entity (N);
569 -- Body is body of package instantiation. Corresponding spec has already
570 -- been set.
572 if Present (Corresponding_Spec (N)) then
573 Spec_Id := Corresponding_Spec (N);
574 Pack_Decl := Unit_Declaration_Node (Spec_Id);
576 else
577 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
579 if Present (Spec_Id)
580 and then Is_Package_Or_Generic_Package (Spec_Id)
581 then
582 Pack_Decl := Unit_Declaration_Node (Spec_Id);
584 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
585 Error_Msg_N ("cannot supply body for package renaming", N);
586 return;
588 elsif Present (Corresponding_Body (Pack_Decl)) then
589 Error_Msg_N ("redefinition of package body", N);
590 return;
591 end if;
593 else
594 Error_Msg_N ("missing specification for package body", N);
595 return;
596 end if;
598 if Is_Package_Or_Generic_Package (Spec_Id)
599 and then (Scope (Spec_Id) = Standard_Standard
600 or else Is_Child_Unit (Spec_Id))
601 and then not Unit_Requires_Body (Spec_Id)
602 then
603 if Ada_Version = Ada_83 then
604 Error_Msg_N
605 ("optional package body (not allowed in Ada 95)??", N);
606 else
607 Error_Msg_N ("spec of this package does not allow a body", N);
608 end if;
609 end if;
610 end if;
612 -- A [generic] package body "freezes" the contract of the nearest
613 -- enclosing package body and all other contracts encountered in the
614 -- same declarative part up to and excluding the package body:
616 -- package body Nearest_Enclosing_Package
617 -- with Refined_State => (State => Constit)
618 -- is
619 -- Constit : ...;
621 -- package body Freezes_Enclosing_Package_Body
622 -- with Refined_State => (State_2 => Constit_2)
623 -- is
624 -- Constit_2 : ...;
626 -- procedure Proc
627 -- with Refined_Depends => (Input => (Constit, Constit_2)) ...
629 -- This ensures that any annotations referenced by the contract of a
630 -- [generic] subprogram body declared within the current package body
631 -- are available. This form of "freezing" is decoupled from the usual
632 -- Freeze_xxx mechanism because it must also work in the context of
633 -- generics where normal freezing is disabled.
635 -- Only bodies coming from source should cause this type of "freezing".
636 -- Instantiated generic bodies are excluded because their processing is
637 -- performed in a separate compilation pass which lacks enough semantic
638 -- information with respect to contract analysis. It is safe to suppress
639 -- the "freezing" of contracts in this case because this action already
640 -- took place at the end of the enclosing declarative part.
642 if Comes_From_Source (N)
643 and then not Is_Generic_Instance (Spec_Id)
644 then
645 Analyze_Previous_Contracts (N);
646 end if;
648 -- A package body is Ghost when the corresponding spec is Ghost. Set
649 -- the mode now to ensure that any nodes generated during analysis and
650 -- expansion are properly flagged as ignored Ghost.
652 Mark_And_Set_Ghost_Body (N, Spec_Id);
654 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
655 Style.Check_Identifier (Body_Id, Spec_Id);
657 if Is_Child_Unit (Spec_Id) then
658 if Nkind (Parent (N)) /= N_Compilation_Unit then
659 Error_Msg_NE
660 ("body of child unit& cannot be an inner package", N, Spec_Id);
661 end if;
663 Set_Is_Child_Unit (Body_Id);
664 end if;
666 -- Generic package case
668 if Ekind (Spec_Id) = E_Generic_Package then
670 -- Disable expansion and perform semantic analysis on copy. The
671 -- unannotated body will be used in all instantiations.
673 Body_Id := Defining_Entity (N);
674 Set_Ekind (Body_Id, E_Package_Body);
675 Set_Scope (Body_Id, Scope (Spec_Id));
676 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
677 Set_Body_Entity (Spec_Id, Body_Id);
678 Set_Spec_Entity (Body_Id, Spec_Id);
680 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
681 Rewrite (N, New_N);
683 -- Once the contents of the generic copy and the template are
684 -- swapped, do the same for their respective aspect specifications.
686 Exchange_Aspects (N, New_N);
688 -- Collect all contract-related source pragmas found within the
689 -- template and attach them to the contract of the package body.
690 -- This contract is used in the capture of global references within
691 -- annotations.
693 Create_Generic_Contract (N);
695 -- Update Body_Id to point to the copied node for the remainder of
696 -- the processing.
698 Body_Id := Defining_Entity (N);
699 Start_Generic;
700 end if;
702 -- The Body_Id is that of the copied node in the generic case, the
703 -- current node otherwise. Note that N was rewritten above, so we must
704 -- be sure to get the latest Body_Id value.
706 Set_Ekind (Body_Id, E_Package_Body);
707 Set_Body_Entity (Spec_Id, Body_Id);
708 Set_Spec_Entity (Body_Id, Spec_Id);
710 -- Defining name for the package body is not a visible entity: Only the
711 -- defining name for the declaration is visible.
713 Set_Etype (Body_Id, Standard_Void_Type);
714 Set_Scope (Body_Id, Scope (Spec_Id));
715 Set_Corresponding_Spec (N, Spec_Id);
716 Set_Corresponding_Body (Pack_Decl, Body_Id);
718 -- The body entity is not used for semantics or code generation, but
719 -- it is attached to the entity list of the enclosing scope to simplify
720 -- the listing of back-annotations for the types it main contain.
722 if Scope (Spec_Id) /= Standard_Standard then
723 Append_Entity (Body_Id, Scope (Spec_Id));
724 end if;
726 -- Indicate that we are currently compiling the body of the package
728 Set_In_Package_Body (Spec_Id);
729 Set_Has_Completion (Spec_Id);
730 Last_Spec_Entity := Last_Entity (Spec_Id);
732 if Has_Aspects (N) then
733 Analyze_Aspect_Specifications (N, Body_Id);
734 end if;
736 Push_Scope (Spec_Id);
738 -- Set SPARK_Mode only for non-generic package
740 if Ekind (Spec_Id) = E_Package then
741 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
742 Set_SPARK_Aux_Pragma (Body_Id, SPARK_Mode_Pragma);
743 Set_SPARK_Pragma_Inherited (Body_Id);
744 Set_SPARK_Aux_Pragma_Inherited (Body_Id);
746 -- A package body may be instantiated or inlined at a later pass.
747 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when
748 -- it applied to the package spec.
750 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
751 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
752 end if;
753 end if;
755 Set_Categorization_From_Pragmas (N);
757 Install_Visible_Declarations (Spec_Id);
758 Install_Private_Declarations (Spec_Id);
759 Install_Private_With_Clauses (Spec_Id);
760 Install_Composite_Operations (Spec_Id);
762 Check_Anonymous_Access_Types (Spec_Id, N);
764 if Ekind (Spec_Id) = E_Generic_Package then
765 Set_Use (Generic_Formal_Declarations (Pack_Decl));
766 end if;
768 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
769 Set_Use (Private_Declarations (Specification (Pack_Decl)));
771 -- This is a nested package, so it may be necessary to declare certain
772 -- inherited subprograms that are not yet visible because the parent
773 -- type's subprograms are now visible.
775 if Ekind (Scope (Spec_Id)) = E_Package
776 and then Scope (Spec_Id) /= Standard_Standard
777 then
778 Declare_Inherited_Private_Subprograms (Spec_Id);
779 end if;
781 -- A package body "freezes" the contract of its initial declaration.
782 -- This analysis depends on attribute Corresponding_Spec being set. Only
783 -- bodies coming from source shuld cause this type of "freezing".
785 if Present (Declarations (N)) then
786 Analyze_Declarations (Declarations (N));
787 Inspect_Deferred_Constant_Completion (Declarations (N));
788 end if;
790 -- Verify that the SPARK_Mode of the body agrees with that of its spec
792 if Present (SPARK_Pragma (Body_Id)) then
793 if Present (SPARK_Aux_Pragma (Spec_Id)) then
794 if Get_SPARK_Mode_From_Annotation (SPARK_Aux_Pragma (Spec_Id)) =
796 and then
797 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
798 then
799 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
800 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
801 Error_Msg_Sloc := Sloc (SPARK_Aux_Pragma (Spec_Id));
802 Error_Msg_NE
803 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
804 end if;
806 else
807 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
808 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
809 Error_Msg_Sloc := Sloc (Spec_Id);
810 Error_Msg_NE
811 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
812 end if;
813 end if;
815 -- Analyze_Declarations has caused freezing of all types. Now generate
816 -- bodies for RACW primitives and stream attributes, if any.
818 if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then
820 -- Attach subprogram bodies to support RACWs declared in spec
822 Append_RACW_Bodies (Declarations (N), Spec_Id);
823 Analyze_List (Declarations (N));
824 end if;
826 HSS := Handled_Statement_Sequence (N);
828 if Present (HSS) then
829 Process_End_Label (HSS, 't', Spec_Id);
830 Analyze (HSS);
832 -- Check that elaboration code in a preelaborable package body is
833 -- empty other than null statements and labels (RM 10.2.1(6)).
835 Validate_Null_Statement_Sequence (N);
836 end if;
838 Validate_Categorization_Dependency (N, Spec_Id);
839 Check_Completion (Body_Id);
841 -- Generate start of body reference. Note that we do this fairly late,
842 -- because the call will use In_Extended_Main_Source_Unit as a check,
843 -- and we want to make sure that Corresponding_Stub links are set
845 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
847 -- For a generic package, collect global references and mark them on
848 -- the original body so that they are not resolved again at the point
849 -- of instantiation.
851 if Ekind (Spec_Id) /= E_Package then
852 Save_Global_References (Original_Node (N));
853 End_Generic;
854 end if;
856 -- The entities of the package body have so far been chained onto the
857 -- declaration chain for the spec. That's been fine while we were in the
858 -- body, since we wanted them to be visible, but now that we are leaving
859 -- the package body, they are no longer visible, so we remove them from
860 -- the entity chain of the package spec entity, and copy them to the
861 -- entity chain of the package body entity, where they will never again
862 -- be visible.
864 if Present (Last_Spec_Entity) then
865 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
866 Set_Next_Entity (Last_Spec_Entity, Empty);
867 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
868 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
870 else
871 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
872 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
873 Set_First_Entity (Spec_Id, Empty);
874 Set_Last_Entity (Spec_Id, Empty);
875 end if;
877 End_Package_Scope (Spec_Id);
879 -- All entities declared in body are not visible
881 declare
882 E : Entity_Id;
884 begin
885 E := First_Entity (Body_Id);
886 while Present (E) loop
887 Set_Is_Immediately_Visible (E, False);
888 Set_Is_Potentially_Use_Visible (E, False);
889 Set_Is_Hidden (E);
891 -- Child units may appear on the entity list (e.g. if they appear
892 -- in the context of a subunit) but they are not body entities.
894 if not Is_Child_Unit (E) then
895 Set_Is_Package_Body_Entity (E);
896 end if;
898 Next_Entity (E);
899 end loop;
900 end;
902 Check_References (Body_Id);
904 -- For a generic unit, check that the formal parameters are referenced,
905 -- and that local variables are used, as for regular packages.
907 if Ekind (Spec_Id) = E_Generic_Package then
908 Check_References (Spec_Id);
909 end if;
911 -- At this point all entities of the package body are externally visible
912 -- to the linker as their Is_Public flag is set to True. This proactive
913 -- approach is necessary because an inlined or a generic body for which
914 -- code is generated in other units may need to see these entities. Cut
915 -- down the number of global symbols that do not neet public visibility
916 -- as this has two beneficial effects:
917 -- (1) It makes the compilation process more efficient.
918 -- (2) It gives the code generatormore freedom to optimize within each
919 -- unit, especially subprograms.
921 -- This is done only for top level library packages or child units as
922 -- the algorithm does a top down traversal of the package body.
924 if (Scope (Spec_Id) = Standard_Standard or else Is_Child_Unit (Spec_Id))
925 and then not Is_Generic_Unit (Spec_Id)
926 then
927 Hide_Public_Entities (Declarations (N));
928 end if;
930 -- If expander is not active, then here is where we turn off the
931 -- In_Package_Body flag, otherwise it is turned off at the end of the
932 -- corresponding expansion routine. If this is an instance body, we need
933 -- to qualify names of local entities, because the body may have been
934 -- compiled as a preliminary to another instantiation.
936 if not Expander_Active then
937 Set_In_Package_Body (Spec_Id, False);
939 if Is_Generic_Instance (Spec_Id)
940 and then Operating_Mode = Generate_Code
941 then
942 Qualify_Entity_Names (N);
943 end if;
944 end if;
946 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
947 Restore_Ghost_Mode (Saved_GM);
948 end Analyze_Package_Body_Helper;
950 ---------------------------------
951 -- Analyze_Package_Declaration --
952 ---------------------------------
954 procedure Analyze_Package_Declaration (N : Node_Id) is
955 Id : constant Node_Id := Defining_Entity (N);
957 Is_Comp_Unit : constant Boolean :=
958 Nkind (Parent (N)) = N_Compilation_Unit;
960 Body_Required : Boolean;
961 -- True when this package declaration requires a corresponding body
963 begin
964 if Debug_Flag_C then
965 Write_Str ("==> package spec ");
966 Write_Name (Chars (Id));
967 Write_Str (" from ");
968 Write_Location (Sloc (N));
969 Write_Eol;
970 Indent;
971 end if;
973 Generate_Definition (Id);
974 Enter_Name (Id);
975 Set_Ekind (Id, E_Package);
976 Set_Etype (Id, Standard_Void_Type);
978 -- Set SPARK_Mode from context
980 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
981 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
982 Set_SPARK_Pragma_Inherited (Id);
983 Set_SPARK_Aux_Pragma_Inherited (Id);
985 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
986 -- the body of this package is instantiated or inlined later and out of
987 -- context. The body uses this attribute to restore the value of the
988 -- global flag.
990 if Ignore_SPARK_Mode_Pragmas_In_Instance then
991 Set_Ignore_SPARK_Mode_Pragmas (Id);
992 end if;
994 -- Analyze aspect specifications immediately, since we need to recognize
995 -- things like Pure early enough to diagnose violations during analysis.
997 if Has_Aspects (N) then
998 Analyze_Aspect_Specifications (N, Id);
999 end if;
1001 -- Ada 2005 (AI-217): Check if the package has been illegally named in
1002 -- a limited-with clause of its own context. In this case the error has
1003 -- been previously notified by Analyze_Context.
1005 -- limited with Pkg; -- ERROR
1006 -- package Pkg is ...
1008 if From_Limited_With (Id) then
1009 return;
1010 end if;
1012 Push_Scope (Id);
1014 Set_Is_Pure (Id, Is_Pure (Enclosing_Lib_Unit_Entity));
1015 Set_Categorization_From_Pragmas (N);
1017 Analyze (Specification (N));
1018 Validate_Categorization_Dependency (N, Id);
1020 -- Determine whether the package requires a body. Abstract states are
1021 -- intentionally ignored because they do require refinement which can
1022 -- only come in a body, but at the same time they do not force the need
1023 -- for a body on their own (SPARK RM 7.1.4(4) and 7.2.2(3)).
1025 Body_Required := Unit_Requires_Body (Id);
1027 if not Body_Required then
1029 -- If the package spec does not require an explicit body, then there
1030 -- are not entities requiring completion in the language sense. Call
1031 -- Check_Completion now to ensure that nested package declarations
1032 -- that require an implicit body get one. (In the case where a body
1033 -- is required, Check_Completion is called at the end of the body's
1034 -- declarative part.)
1036 Check_Completion;
1038 -- If the package spec does not require an explicit body, then all
1039 -- abstract states declared in nested packages cannot possibly get
1040 -- a proper refinement (SPARK RM 7.2.2(3)). This check is performed
1041 -- only when the compilation unit is the main unit to allow for
1042 -- modular SPARK analysis where packages do not necessarily have
1043 -- bodies.
1045 if Is_Comp_Unit then
1046 Check_State_Refinements
1047 (Context => N,
1048 Is_Main_Unit => Parent (N) = Cunit (Main_Unit));
1049 end if;
1050 end if;
1052 if Is_Comp_Unit then
1054 -- Set Body_Required indication on the compilation unit node, and
1055 -- determine whether elaboration warnings may be meaningful on it.
1057 Set_Body_Required (Parent (N), Body_Required);
1059 if not Body_Required then
1060 Set_Suppress_Elaboration_Warnings (Id);
1061 end if;
1062 end if;
1064 End_Package_Scope (Id);
1066 -- For the declaration of a library unit that is a remote types package,
1067 -- check legality rules regarding availability of stream attributes for
1068 -- types that contain non-remote access values. This subprogram performs
1069 -- visibility tests that rely on the fact that we have exited the scope
1070 -- of Id.
1072 if Is_Comp_Unit then
1073 Validate_RT_RAT_Component (N);
1074 end if;
1076 if Debug_Flag_C then
1077 Outdent;
1078 Write_Str ("<== package spec ");
1079 Write_Name (Chars (Id));
1080 Write_Str (" from ");
1081 Write_Location (Sloc (N));
1082 Write_Eol;
1083 end if;
1084 end Analyze_Package_Declaration;
1086 -----------------------------------
1087 -- Analyze_Package_Specification --
1088 -----------------------------------
1090 -- Note that this code is shared for the analysis of generic package specs
1091 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
1093 procedure Analyze_Package_Specification (N : Node_Id) is
1094 Id : constant Entity_Id := Defining_Entity (N);
1095 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
1096 Vis_Decls : constant List_Id := Visible_Declarations (N);
1097 Priv_Decls : constant List_Id := Private_Declarations (N);
1098 E : Entity_Id;
1099 L : Entity_Id;
1100 Public_Child : Boolean;
1102 Private_With_Clauses_Installed : Boolean := False;
1103 -- In Ada 2005, private with_clauses are visible in the private part
1104 -- of a nested package, even if it appears in the public part of the
1105 -- enclosing package. This requires a separate step to install these
1106 -- private_with_clauses, and remove them at the end of the nested
1107 -- package.
1109 procedure Check_One_Tagged_Type_Or_Extension_At_Most;
1110 -- Issue an error in SPARK mode if a package specification contains
1111 -- more than one tagged type or type extension.
1113 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
1114 -- Clears constant indications (Never_Set_In_Source, Constant_Value, and
1115 -- Is_True_Constant) on all variables that are entities of Id, and on
1116 -- the chain whose first element is FE. A recursive call is made for all
1117 -- packages and generic packages.
1119 procedure Generate_Parent_References;
1120 -- For a child unit, generate references to parent units, for
1121 -- GPS navigation purposes.
1123 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
1124 -- Child and Unit are entities of compilation units. True if Child
1125 -- is a public child of Parent as defined in 10.1.1
1127 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
1128 -- Reject completion of an incomplete or private type declarations
1129 -- having a known discriminant part by an unchecked union.
1131 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
1132 -- Given the package entity of a generic package instantiation or
1133 -- formal package whose corresponding generic is a child unit, installs
1134 -- the private declarations of each of the child unit's parents.
1135 -- This has to be done at the point of entering the instance package's
1136 -- private part rather than being done in Sem_Ch12.Install_Parent
1137 -- (which is where the parents' visible declarations are installed).
1139 ------------------------------------------------
1140 -- Check_One_Tagged_Type_Or_Extension_At_Most --
1141 ------------------------------------------------
1143 procedure Check_One_Tagged_Type_Or_Extension_At_Most is
1144 Previous : Node_Id;
1146 procedure Check_Decls (Decls : List_Id);
1147 -- Check that either Previous is Empty and Decls does not contain
1148 -- more than one tagged type or type extension, or Previous is
1149 -- already set and Decls contains no tagged type or type extension.
1151 -----------------
1152 -- Check_Decls --
1153 -----------------
1155 procedure Check_Decls (Decls : List_Id) is
1156 Decl : Node_Id;
1158 begin
1159 Decl := First (Decls);
1160 while Present (Decl) loop
1161 if Nkind (Decl) = N_Full_Type_Declaration
1162 and then Is_Tagged_Type (Defining_Identifier (Decl))
1163 then
1164 if No (Previous) then
1165 Previous := Decl;
1167 else
1168 Error_Msg_Sloc := Sloc (Previous);
1169 Check_SPARK_05_Restriction
1170 ("at most one tagged type or type extension allowed",
1171 "\\ previous declaration#",
1172 Decl);
1173 end if;
1174 end if;
1176 Next (Decl);
1177 end loop;
1178 end Check_Decls;
1180 -- Start of processing for Check_One_Tagged_Type_Or_Extension_At_Most
1182 begin
1183 Previous := Empty;
1184 Check_Decls (Vis_Decls);
1186 if Present (Priv_Decls) then
1187 Check_Decls (Priv_Decls);
1188 end if;
1189 end Check_One_Tagged_Type_Or_Extension_At_Most;
1191 ---------------------
1192 -- Clear_Constants --
1193 ---------------------
1195 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
1196 E : Entity_Id;
1198 begin
1199 -- Ignore package renamings, not interesting and they can cause self
1200 -- referential loops in the code below.
1202 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
1203 return;
1204 end if;
1206 -- Note: in the loop below, the check for Next_Entity pointing back
1207 -- to the package entity may seem odd, but it is needed, because a
1208 -- package can contain a renaming declaration to itself, and such
1209 -- renamings are generated automatically within package instances.
1211 E := FE;
1212 while Present (E) and then E /= Id loop
1213 if Is_Assignable (E) then
1214 Set_Never_Set_In_Source (E, False);
1215 Set_Is_True_Constant (E, False);
1216 Set_Current_Value (E, Empty);
1217 Set_Is_Known_Null (E, False);
1218 Set_Last_Assignment (E, Empty);
1220 if not Can_Never_Be_Null (E) then
1221 Set_Is_Known_Non_Null (E, False);
1222 end if;
1224 elsif Is_Package_Or_Generic_Package (E) then
1225 Clear_Constants (E, First_Entity (E));
1226 Clear_Constants (E, First_Private_Entity (E));
1227 end if;
1229 Next_Entity (E);
1230 end loop;
1231 end Clear_Constants;
1233 --------------------------------
1234 -- Generate_Parent_References --
1235 --------------------------------
1237 procedure Generate_Parent_References is
1238 Decl : constant Node_Id := Parent (N);
1240 begin
1241 if Id = Cunit_Entity (Main_Unit)
1242 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
1243 then
1244 Generate_Reference (Id, Scope (Id), 'k', False);
1246 elsif not Nkind_In (Unit (Cunit (Main_Unit)), N_Subprogram_Body,
1247 N_Subunit)
1248 then
1249 -- If current unit is an ancestor of main unit, generate a
1250 -- reference to its own parent.
1252 declare
1253 U : Node_Id;
1254 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
1256 begin
1257 if Nkind (Main_Spec) = N_Package_Body then
1258 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
1259 end if;
1261 U := Parent_Spec (Main_Spec);
1262 while Present (U) loop
1263 if U = Parent (Decl) then
1264 Generate_Reference (Id, Scope (Id), 'k', False);
1265 exit;
1267 elsif Nkind (Unit (U)) = N_Package_Body then
1268 exit;
1270 else
1271 U := Parent_Spec (Unit (U));
1272 end if;
1273 end loop;
1274 end;
1275 end if;
1276 end Generate_Parent_References;
1278 ---------------------
1279 -- Is_Public_Child --
1280 ---------------------
1282 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
1283 begin
1284 if not Is_Private_Descendant (Child) then
1285 return True;
1286 else
1287 if Child = Unit then
1288 return not Private_Present (
1289 Parent (Unit_Declaration_Node (Child)));
1290 else
1291 return Is_Public_Child (Scope (Child), Unit);
1292 end if;
1293 end if;
1294 end Is_Public_Child;
1296 ----------------------------------------
1297 -- Inspect_Unchecked_Union_Completion --
1298 ----------------------------------------
1300 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
1301 Decl : Node_Id;
1303 begin
1304 Decl := First (Decls);
1305 while Present (Decl) loop
1307 -- We are looking at an incomplete or private type declaration
1308 -- with a known_discriminant_part whose full view is an
1309 -- Unchecked_Union.
1311 if Nkind_In (Decl, N_Incomplete_Type_Declaration,
1312 N_Private_Type_Declaration)
1313 and then Has_Discriminants (Defining_Identifier (Decl))
1314 and then Present (Full_View (Defining_Identifier (Decl)))
1315 and then
1316 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
1317 then
1318 Error_Msg_N
1319 ("completion of discriminated partial view "
1320 & "cannot be an unchecked union",
1321 Full_View (Defining_Identifier (Decl)));
1322 end if;
1324 Next (Decl);
1325 end loop;
1326 end Inspect_Unchecked_Union_Completion;
1328 -----------------------------------------
1329 -- Install_Parent_Private_Declarations --
1330 -----------------------------------------
1332 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
1333 Inst_Par : Entity_Id;
1334 Gen_Par : Entity_Id;
1335 Inst_Node : Node_Id;
1337 begin
1338 Inst_Par := Inst_Id;
1340 Gen_Par :=
1341 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
1342 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
1343 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
1345 if Nkind_In (Inst_Node, N_Package_Instantiation,
1346 N_Formal_Package_Declaration)
1347 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
1348 then
1349 Inst_Par := Entity (Prefix (Name (Inst_Node)));
1351 if Present (Renamed_Entity (Inst_Par)) then
1352 Inst_Par := Renamed_Entity (Inst_Par);
1353 end if;
1355 Gen_Par :=
1356 Generic_Parent
1357 (Specification (Unit_Declaration_Node (Inst_Par)));
1359 -- Install the private declarations and private use clauses
1360 -- of a parent instance of the child instance, unless the
1361 -- parent instance private declarations have already been
1362 -- installed earlier in Analyze_Package_Specification, which
1363 -- happens when a generic child is instantiated, and the
1364 -- instance is a child of the parent instance.
1366 -- Installing the use clauses of the parent instance twice
1367 -- is both unnecessary and wrong, because it would cause the
1368 -- clauses to be chained to themselves in the use clauses
1369 -- list of the scope stack entry. That in turn would cause
1370 -- an endless loop from End_Use_Clauses upon scope exit.
1372 -- The parent is now fully visible. It may be a hidden open
1373 -- scope if we are currently compiling some child instance
1374 -- declared within it, but while the current instance is being
1375 -- compiled the parent is immediately visible. In particular
1376 -- its entities must remain visible if a stack save/restore
1377 -- takes place through a call to Rtsfind.
1379 if Present (Gen_Par) then
1380 if not In_Private_Part (Inst_Par) then
1381 Install_Private_Declarations (Inst_Par);
1382 Set_Use (Private_Declarations
1383 (Specification
1384 (Unit_Declaration_Node (Inst_Par))));
1385 Set_Is_Hidden_Open_Scope (Inst_Par, False);
1386 end if;
1388 -- If we've reached the end of the generic instance parents,
1389 -- then finish off by looping through the nongeneric parents
1390 -- and installing their private declarations.
1392 -- If one of the non-generic parents is itself on the scope
1393 -- stack, do not install its private declarations: they are
1394 -- installed in due time when the private part of that parent
1395 -- is analyzed.
1397 else
1398 while Present (Inst_Par)
1399 and then Inst_Par /= Standard_Standard
1400 and then (not In_Open_Scopes (Inst_Par)
1401 or else not In_Private_Part (Inst_Par))
1402 loop
1403 if Nkind (Inst_Node) = N_Formal_Package_Declaration
1404 or else
1405 not Is_Ancestor_Package
1406 (Inst_Par, Cunit_Entity (Current_Sem_Unit))
1407 then
1408 Install_Private_Declarations (Inst_Par);
1409 Set_Use
1410 (Private_Declarations
1411 (Specification
1412 (Unit_Declaration_Node (Inst_Par))));
1413 Inst_Par := Scope (Inst_Par);
1414 else
1415 exit;
1416 end if;
1417 end loop;
1419 exit;
1420 end if;
1422 else
1423 exit;
1424 end if;
1425 end loop;
1426 end Install_Parent_Private_Declarations;
1428 -- Start of processing for Analyze_Package_Specification
1430 begin
1431 if Present (Vis_Decls) then
1432 Analyze_Declarations (Vis_Decls);
1433 end if;
1435 -- Inspect the entities defined in the package and ensure that all
1436 -- incomplete types have received full declarations. Build default
1437 -- initial condition and invariant procedures for all qualifying types.
1439 E := First_Entity (Id);
1440 while Present (E) loop
1442 -- Check on incomplete types
1444 -- AI05-0213: A formal incomplete type has no completion
1446 if Ekind (E) = E_Incomplete_Type
1447 and then No (Full_View (E))
1448 and then not Is_Generic_Type (E)
1449 then
1450 Error_Msg_N ("no declaration in visible part for incomplete}", E);
1451 end if;
1453 Next_Entity (E);
1454 end loop;
1456 if Is_Remote_Call_Interface (Id)
1457 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1458 then
1459 Validate_RCI_Declarations (Id);
1460 end if;
1462 -- Save global references in the visible declarations, before installing
1463 -- private declarations of parent unit if there is one, because the
1464 -- privacy status of types defined in the parent will change. This is
1465 -- only relevant for generic child units, but is done in all cases for
1466 -- uniformity.
1468 if Ekind (Id) = E_Generic_Package
1469 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1470 then
1471 declare
1472 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1473 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1475 begin
1476 -- Insert the freezing nodes after the visible declarations to
1477 -- ensure that we analyze its aspects; needed to ensure that
1478 -- global entities referenced in the aspects are properly handled.
1480 if Ada_Version >= Ada_2012
1481 and then Is_Non_Empty_List (Vis_Decls)
1482 and then Is_Empty_List (Priv_Decls)
1483 then
1484 Insert_List_After_And_Analyze
1485 (Last (Vis_Decls), Freeze_Entity (Id, Last (Vis_Decls)));
1486 end if;
1488 Set_Private_Declarations (Orig_Spec, Empty_List);
1489 Save_Global_References (Orig_Decl);
1490 Set_Private_Declarations (Orig_Spec, Save_Priv);
1491 end;
1492 end if;
1494 -- If package is a public child unit, then make the private declarations
1495 -- of the parent visible.
1497 Public_Child := False;
1499 declare
1500 Par : Entity_Id;
1501 Pack_Decl : Node_Id;
1502 Par_Spec : Node_Id;
1504 begin
1505 Par := Id;
1506 Par_Spec := Parent_Spec (Parent (N));
1508 -- If the package is formal package of an enclosing generic, it is
1509 -- transformed into a local generic declaration, and compiled to make
1510 -- its spec available. We need to retrieve the original generic to
1511 -- determine whether it is a child unit, and install its parents.
1513 if No (Par_Spec)
1514 and then
1515 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1516 then
1517 Par := Entity (Name (Original_Node (Parent (N))));
1518 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1519 end if;
1521 if Present (Par_Spec) then
1522 Generate_Parent_References;
1524 while Scope (Par) /= Standard_Standard
1525 and then Is_Public_Child (Id, Par)
1526 and then In_Open_Scopes (Par)
1527 loop
1528 Public_Child := True;
1529 Par := Scope (Par);
1530 Install_Private_Declarations (Par);
1531 Install_Private_With_Clauses (Par);
1532 Pack_Decl := Unit_Declaration_Node (Par);
1533 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1534 end loop;
1535 end if;
1536 end;
1538 if Is_Compilation_Unit (Id) then
1539 Install_Private_With_Clauses (Id);
1540 else
1541 -- The current compilation unit may include private with_clauses,
1542 -- which are visible in the private part of the current nested
1543 -- package, and have to be installed now. This is not done for
1544 -- nested instantiations, where the private with_clauses of the
1545 -- enclosing unit have no effect once the instantiation info is
1546 -- established and we start analyzing the package declaration.
1548 declare
1549 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1550 begin
1551 if Is_Package_Or_Generic_Package (Comp_Unit)
1552 and then not In_Private_Part (Comp_Unit)
1553 and then not In_Instance
1554 then
1555 Install_Private_With_Clauses (Comp_Unit);
1556 Private_With_Clauses_Installed := True;
1557 end if;
1558 end;
1559 end if;
1561 -- If this is a package associated with a generic instance or formal
1562 -- package, then the private declarations of each of the generic's
1563 -- parents must be installed at this point.
1565 if Is_Generic_Instance (Id) then
1566 Install_Parent_Private_Declarations (Id);
1567 end if;
1569 -- Analyze private part if present. The flag In_Private_Part is reset
1570 -- in End_Package_Scope.
1572 L := Last_Entity (Id);
1574 if Present (Priv_Decls) then
1575 Set_In_Private_Part (Id);
1577 -- Upon entering a public child's private part, it may be necessary
1578 -- to declare subprograms that were derived in the package's visible
1579 -- part but not yet made visible.
1581 if Public_Child then
1582 Declare_Inherited_Private_Subprograms (Id);
1583 end if;
1585 Analyze_Declarations (Priv_Decls);
1587 -- Check the private declarations for incomplete deferred constants
1589 Inspect_Deferred_Constant_Completion (Priv_Decls);
1591 -- The first private entity is the immediate follower of the last
1592 -- visible entity, if there was one.
1594 if Present (L) then
1595 Set_First_Private_Entity (Id, Next_Entity (L));
1596 else
1597 Set_First_Private_Entity (Id, First_Entity (Id));
1598 end if;
1600 -- There may be inherited private subprograms that need to be declared,
1601 -- even in the absence of an explicit private part. If there are any
1602 -- public declarations in the package and the package is a public child
1603 -- unit, then an implicit private part is assumed.
1605 elsif Present (L) and then Public_Child then
1606 Set_In_Private_Part (Id);
1607 Declare_Inherited_Private_Subprograms (Id);
1608 Set_First_Private_Entity (Id, Next_Entity (L));
1609 end if;
1611 E := First_Entity (Id);
1612 while Present (E) loop
1614 -- Check rule of 3.6(11), which in general requires waiting till all
1615 -- full types have been seen.
1617 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1618 Check_Aliased_Component_Types (E);
1619 end if;
1621 -- Check preelaborable initialization for full type completing a
1622 -- private type for which pragma Preelaborable_Initialization given.
1624 if Is_Type (E)
1625 and then Must_Have_Preelab_Init (E)
1626 and then not Has_Preelaborable_Initialization (E)
1627 then
1628 Error_Msg_N
1629 ("full view of & does not have preelaborable initialization", E);
1630 end if;
1632 Next_Entity (E);
1633 end loop;
1635 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1636 -- declaration having a known_discriminant_part shall not be an
1637 -- unchecked union type.
1639 if Present (Vis_Decls) then
1640 Inspect_Unchecked_Union_Completion (Vis_Decls);
1641 end if;
1643 if Present (Priv_Decls) then
1644 Inspect_Unchecked_Union_Completion (Priv_Decls);
1645 end if;
1647 if Ekind (Id) = E_Generic_Package
1648 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1649 and then Present (Priv_Decls)
1650 then
1651 -- Save global references in private declarations, ignoring the
1652 -- visible declarations that were processed earlier.
1654 declare
1655 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1656 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1657 Save_Form : constant List_Id :=
1658 Generic_Formal_Declarations (Orig_Decl);
1660 begin
1661 -- Insert the freezing nodes after the private declarations to
1662 -- ensure that we analyze its aspects; needed to ensure that
1663 -- global entities referenced in the aspects are properly handled.
1665 if Ada_Version >= Ada_2012
1666 and then Is_Non_Empty_List (Priv_Decls)
1667 then
1668 Insert_List_After_And_Analyze
1669 (Last (Priv_Decls), Freeze_Entity (Id, Last (Priv_Decls)));
1670 end if;
1672 Set_Visible_Declarations (Orig_Spec, Empty_List);
1673 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1674 Save_Global_References (Orig_Decl);
1675 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1676 Set_Visible_Declarations (Orig_Spec, Save_Vis);
1677 end;
1678 end if;
1680 Process_End_Label (N, 'e', Id);
1682 -- Remove private_with_clauses of enclosing compilation unit, if they
1683 -- were installed.
1685 if Private_With_Clauses_Installed then
1686 Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
1687 end if;
1689 -- For the case of a library level package, we must go through all the
1690 -- entities clearing the indications that the value may be constant and
1691 -- not modified. Why? Because any client of this package may modify
1692 -- these values freely from anywhere. This also applies to any nested
1693 -- packages or generic packages.
1695 -- For now we unconditionally clear constants for packages that are
1696 -- instances of generic packages. The reason is that we do not have the
1697 -- body yet, and we otherwise think things are unreferenced when they
1698 -- are not. This should be fixed sometime (the effect is not terrible,
1699 -- we just lose some warnings, and also some cases of value propagation)
1700 -- ???
1702 if Is_Library_Level_Entity (Id)
1703 or else Is_Generic_Instance (Id)
1704 then
1705 Clear_Constants (Id, First_Entity (Id));
1706 Clear_Constants (Id, First_Private_Entity (Id));
1707 end if;
1709 -- Issue an error in SPARK mode if a package specification contains
1710 -- more than one tagged type or type extension.
1712 Check_One_Tagged_Type_Or_Extension_At_Most;
1714 -- Output relevant information as to why the package requires a body.
1715 -- Do not consider generated packages as this exposes internal symbols
1716 -- and leads to confusing messages.
1718 if List_Body_Required_Info
1719 and then In_Extended_Main_Source_Unit (Id)
1720 and then Unit_Requires_Body (Id)
1721 and then Comes_From_Source (Id)
1722 then
1723 Unit_Requires_Body_Info (Id);
1724 end if;
1725 end Analyze_Package_Specification;
1727 --------------------------------------
1728 -- Analyze_Private_Type_Declaration --
1729 --------------------------------------
1731 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1732 Id : constant Entity_Id := Defining_Identifier (N);
1733 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
1735 begin
1736 Generate_Definition (Id);
1737 Set_Is_Pure (Id, PF);
1738 Init_Size_Align (Id);
1740 if not Is_Package_Or_Generic_Package (Current_Scope)
1741 or else In_Private_Part (Current_Scope)
1742 then
1743 Error_Msg_N ("invalid context for private declaration", N);
1744 end if;
1746 New_Private_Type (N, Id, N);
1747 Set_Depends_On_Private (Id);
1749 if Has_Aspects (N) then
1750 Analyze_Aspect_Specifications (N, Id);
1751 end if;
1752 end Analyze_Private_Type_Declaration;
1754 ----------------------------------
1755 -- Check_Anonymous_Access_Types --
1756 ----------------------------------
1758 procedure Check_Anonymous_Access_Types
1759 (Spec_Id : Entity_Id;
1760 P_Body : Node_Id)
1762 E : Entity_Id;
1763 IR : Node_Id;
1765 begin
1766 -- Itype references are only needed by gigi, to force elaboration of
1767 -- itypes. In the absence of code generation, they are not needed.
1769 if not Expander_Active then
1770 return;
1771 end if;
1773 E := First_Entity (Spec_Id);
1774 while Present (E) loop
1775 if Ekind (E) = E_Anonymous_Access_Type
1776 and then From_Limited_With (E)
1777 then
1778 IR := Make_Itype_Reference (Sloc (P_Body));
1779 Set_Itype (IR, E);
1781 if No (Declarations (P_Body)) then
1782 Set_Declarations (P_Body, New_List (IR));
1783 else
1784 Prepend (IR, Declarations (P_Body));
1785 end if;
1786 end if;
1788 Next_Entity (E);
1789 end loop;
1790 end Check_Anonymous_Access_Types;
1792 -------------------------------------------
1793 -- Declare_Inherited_Private_Subprograms --
1794 -------------------------------------------
1796 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1798 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1799 -- Check whether an inherited subprogram S is an operation of an
1800 -- untagged derived type T.
1802 ---------------------
1803 -- Is_Primitive_Of --
1804 ---------------------
1806 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1807 Formal : Entity_Id;
1809 begin
1810 -- If the full view is a scalar type, the type is the anonymous base
1811 -- type, but the operation mentions the first subtype, so check the
1812 -- signature against the base type.
1814 if Base_Type (Etype (S)) = Base_Type (T) then
1815 return True;
1817 else
1818 Formal := First_Formal (S);
1819 while Present (Formal) loop
1820 if Base_Type (Etype (Formal)) = Base_Type (T) then
1821 return True;
1822 end if;
1824 Next_Formal (Formal);
1825 end loop;
1827 return False;
1828 end if;
1829 end Is_Primitive_Of;
1831 -- Local variables
1833 E : Entity_Id;
1834 Op_List : Elist_Id;
1835 Op_Elmt : Elmt_Id;
1836 Op_Elmt_2 : Elmt_Id;
1837 Prim_Op : Entity_Id;
1838 New_Op : Entity_Id := Empty;
1839 Parent_Subp : Entity_Id;
1840 Tag : Entity_Id;
1842 -- Start of processing for Declare_Inherited_Private_Subprograms
1844 begin
1845 E := First_Entity (Id);
1846 while Present (E) loop
1848 -- If the entity is a nonprivate type extension whose parent type
1849 -- is declared in an open scope, then the type may have inherited
1850 -- operations that now need to be made visible. Ditto if the entity
1851 -- is a formal derived type in a child unit.
1853 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
1854 or else
1855 (Nkind (Parent (E)) = N_Private_Extension_Declaration
1856 and then Is_Generic_Type (E)))
1857 and then In_Open_Scopes (Scope (Etype (E)))
1858 and then Is_Base_Type (E)
1859 then
1860 if Is_Tagged_Type (E) then
1861 Op_List := Primitive_Operations (E);
1862 New_Op := Empty;
1863 Tag := First_Tag_Component (E);
1865 Op_Elmt := First_Elmt (Op_List);
1866 while Present (Op_Elmt) loop
1867 Prim_Op := Node (Op_Elmt);
1869 -- Search primitives that are implicit operations with an
1870 -- internal name whose parent operation has a normal name.
1872 if Present (Alias (Prim_Op))
1873 and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
1874 and then not Comes_From_Source (Prim_Op)
1875 and then Is_Internal_Name (Chars (Prim_Op))
1876 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1877 then
1878 Parent_Subp := Alias (Prim_Op);
1880 -- Case 1: Check if the type has also an explicit
1881 -- overriding for this primitive.
1883 Op_Elmt_2 := Next_Elmt (Op_Elmt);
1884 while Present (Op_Elmt_2) loop
1886 -- Skip entities with attribute Interface_Alias since
1887 -- they are not overriding primitives (these entities
1888 -- link an interface primitive with their covering
1889 -- primitive)
1891 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
1892 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
1893 and then No (Interface_Alias (Node (Op_Elmt_2)))
1894 then
1895 -- The private inherited operation has been
1896 -- overridden by an explicit subprogram:
1897 -- replace the former by the latter.
1899 New_Op := Node (Op_Elmt_2);
1900 Replace_Elmt (Op_Elmt, New_Op);
1901 Remove_Elmt (Op_List, Op_Elmt_2);
1902 Set_Overridden_Operation (New_Op, Parent_Subp);
1904 -- We don't need to inherit its dispatching slot.
1905 -- Set_All_DT_Position has previously ensured that
1906 -- the same slot was assigned to the two primitives
1908 if Present (Tag)
1909 and then Present (DTC_Entity (New_Op))
1910 and then Present (DTC_Entity (Prim_Op))
1911 then
1912 pragma Assert
1913 (DT_Position (New_Op) = DT_Position (Prim_Op));
1914 null;
1915 end if;
1917 goto Next_Primitive;
1918 end if;
1920 Next_Elmt (Op_Elmt_2);
1921 end loop;
1923 -- Case 2: We have not found any explicit overriding and
1924 -- hence we need to declare the operation (i.e., make it
1925 -- visible).
1927 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1929 -- Inherit the dispatching slot if E is already frozen
1931 if Is_Frozen (E)
1932 and then Present (DTC_Entity (Alias (Prim_Op)))
1933 then
1934 Set_DTC_Entity_Value (E, New_Op);
1935 Set_DT_Position_Value (New_Op,
1936 DT_Position (Alias (Prim_Op)));
1937 end if;
1939 pragma Assert
1940 (Is_Dispatching_Operation (New_Op)
1941 and then Node (Last_Elmt (Op_List)) = New_Op);
1943 -- Substitute the new operation for the old one in the
1944 -- type's primitive operations list. Since the new
1945 -- operation was also just added to the end of list,
1946 -- the last element must be removed.
1948 -- (Question: is there a simpler way of declaring the
1949 -- operation, say by just replacing the name of the
1950 -- earlier operation, reentering it in the in the symbol
1951 -- table (how?), and marking it as private???)
1953 Replace_Elmt (Op_Elmt, New_Op);
1954 Remove_Last_Elmt (Op_List);
1955 end if;
1957 <<Next_Primitive>>
1958 Next_Elmt (Op_Elmt);
1959 end loop;
1961 -- Generate listing showing the contents of the dispatch table
1963 if Debug_Flag_ZZ then
1964 Write_DT (E);
1965 end if;
1967 else
1968 -- For untagged type, scan forward to locate inherited hidden
1969 -- operations.
1971 Prim_Op := Next_Entity (E);
1972 while Present (Prim_Op) loop
1973 if Is_Subprogram (Prim_Op)
1974 and then Present (Alias (Prim_Op))
1975 and then not Comes_From_Source (Prim_Op)
1976 and then Is_Internal_Name (Chars (Prim_Op))
1977 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1978 and then Is_Primitive_Of (E, Prim_Op)
1979 then
1980 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1981 end if;
1983 Next_Entity (Prim_Op);
1985 -- Derived operations appear immediately after the type
1986 -- declaration (or the following subtype indication for
1987 -- a derived scalar type). Further declarations cannot
1988 -- include inherited operations of the type.
1990 if Present (Prim_Op) then
1991 exit when Ekind (Prim_Op) not in Overloadable_Kind;
1992 end if;
1993 end loop;
1994 end if;
1995 end if;
1997 Next_Entity (E);
1998 end loop;
1999 end Declare_Inherited_Private_Subprograms;
2001 -----------------------
2002 -- End_Package_Scope --
2003 -----------------------
2005 procedure End_Package_Scope (P : Entity_Id) is
2006 begin
2007 Uninstall_Declarations (P);
2008 Pop_Scope;
2009 end End_Package_Scope;
2011 ---------------------------
2012 -- Exchange_Declarations --
2013 ---------------------------
2015 procedure Exchange_Declarations (Id : Entity_Id) is
2016 Full_Id : constant Entity_Id := Full_View (Id);
2017 H1 : constant Entity_Id := Homonym (Id);
2018 Next1 : constant Entity_Id := Next_Entity (Id);
2019 H2 : Entity_Id;
2020 Next2 : Entity_Id;
2022 begin
2023 -- If missing full declaration for type, nothing to exchange
2025 if No (Full_Id) then
2026 return;
2027 end if;
2029 -- Otherwise complete the exchange, and preserve semantic links
2031 Next2 := Next_Entity (Full_Id);
2032 H2 := Homonym (Full_Id);
2034 -- Reset full declaration pointer to reflect the switched entities and
2035 -- readjust the next entity chains.
2037 Exchange_Entities (Id, Full_Id);
2039 Set_Next_Entity (Id, Next1);
2040 Set_Homonym (Id, H1);
2042 Set_Full_View (Full_Id, Id);
2043 Set_Next_Entity (Full_Id, Next2);
2044 Set_Homonym (Full_Id, H2);
2045 end Exchange_Declarations;
2047 ----------------------------
2048 -- Install_Package_Entity --
2049 ----------------------------
2051 procedure Install_Package_Entity (Id : Entity_Id) is
2052 begin
2053 if not Is_Internal (Id) then
2054 if Debug_Flag_E then
2055 Write_Str ("Install: ");
2056 Write_Name (Chars (Id));
2057 Write_Eol;
2058 end if;
2060 if Is_Child_Unit (Id) then
2061 null;
2063 -- Do not enter implicitly inherited non-overridden subprograms of
2064 -- a tagged type back into visibility if they have non-conformant
2065 -- homographs (Ada RM 8.3 12.3/2).
2067 elsif Is_Hidden_Non_Overridden_Subpgm (Id) then
2068 null;
2070 else
2071 Set_Is_Immediately_Visible (Id);
2072 end if;
2073 end if;
2074 end Install_Package_Entity;
2076 ----------------------------------
2077 -- Install_Private_Declarations --
2078 ----------------------------------
2080 procedure Install_Private_Declarations (P : Entity_Id) is
2081 Id : Entity_Id;
2082 Full : Entity_Id;
2083 Priv_Deps : Elist_Id;
2085 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
2086 -- When the full view of a private type is made available, we do the
2087 -- same for its private dependents under proper visibility conditions.
2088 -- When compiling a grand-chid unit this needs to be done recursively.
2090 -----------------------------
2091 -- Swap_Private_Dependents --
2092 -----------------------------
2094 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
2095 Deps : Elist_Id;
2096 Priv : Entity_Id;
2097 Priv_Elmt : Elmt_Id;
2098 Is_Priv : Boolean;
2100 begin
2101 Priv_Elmt := First_Elmt (Priv_Deps);
2102 while Present (Priv_Elmt) loop
2103 Priv := Node (Priv_Elmt);
2105 -- Before the exchange, verify that the presence of the Full_View
2106 -- field. This field will be empty if the entity has already been
2107 -- installed due to a previous call.
2109 if Present (Full_View (Priv)) and then Is_Visible_Dependent (Priv)
2110 then
2111 if Is_Private_Type (Priv) then
2112 Deps := Private_Dependents (Priv);
2113 Is_Priv := True;
2114 else
2115 Is_Priv := False;
2116 end if;
2118 -- For each subtype that is swapped, we also swap the reference
2119 -- to it in Private_Dependents, to allow access to it when we
2120 -- swap them out in End_Package_Scope.
2122 Replace_Elmt (Priv_Elmt, Full_View (Priv));
2124 -- Ensure that both views of the dependent private subtype are
2125 -- immediately visible if within some open scope. Check full
2126 -- view before exchanging views.
2128 if In_Open_Scopes (Scope (Full_View (Priv))) then
2129 Set_Is_Immediately_Visible (Priv);
2130 end if;
2132 Exchange_Declarations (Priv);
2133 Set_Is_Immediately_Visible
2134 (Priv, In_Open_Scopes (Scope (Priv)));
2136 Set_Is_Potentially_Use_Visible
2137 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
2139 -- Within a child unit, recurse, except in generic child unit,
2140 -- which (unfortunately) handle private_dependents separately.
2142 if Is_Priv
2143 and then Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
2144 and then not Is_Empty_Elmt_List (Deps)
2145 and then not Inside_A_Generic
2146 then
2147 Swap_Private_Dependents (Deps);
2148 end if;
2149 end if;
2151 Next_Elmt (Priv_Elmt);
2152 end loop;
2153 end Swap_Private_Dependents;
2155 -- Start of processing for Install_Private_Declarations
2157 begin
2158 -- First exchange declarations for private types, so that the full
2159 -- declaration is visible. For each private type, we check its
2160 -- Private_Dependents list and also exchange any subtypes of or derived
2161 -- types from it. Finally, if this is a Taft amendment type, the
2162 -- incomplete declaration is irrelevant, and we want to link the
2163 -- eventual full declaration with the original private one so we
2164 -- also skip the exchange.
2166 Id := First_Entity (P);
2167 while Present (Id) and then Id /= First_Private_Entity (P) loop
2168 if Is_Private_Base_Type (Id)
2169 and then Present (Full_View (Id))
2170 and then Comes_From_Source (Full_View (Id))
2171 and then Scope (Full_View (Id)) = Scope (Id)
2172 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
2173 then
2174 -- If there is a use-type clause on the private type, set the full
2175 -- view accordingly.
2177 Set_In_Use (Full_View (Id), In_Use (Id));
2178 Full := Full_View (Id);
2180 if Is_Private_Base_Type (Full)
2181 and then Has_Private_Declaration (Full)
2182 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
2183 and then In_Open_Scopes (Scope (Etype (Full)))
2184 and then In_Package_Body (Current_Scope)
2185 and then not Is_Private_Type (Etype (Full))
2186 then
2187 -- This is the completion of a private type by a derivation
2188 -- from another private type which is not private anymore. This
2189 -- can only happen in a package nested within a child package,
2190 -- when the parent type is defined in the parent unit. At this
2191 -- point the current type is not private either, and we have
2192 -- to install the underlying full view, which is now visible.
2193 -- Save the current full view as well, so that all views can be
2194 -- restored on exit. It may seem that after compiling the child
2195 -- body there are not environments to restore, but the back-end
2196 -- expects those links to be valid, and freeze nodes depend on
2197 -- them.
2199 if No (Full_View (Full))
2200 and then Present (Underlying_Full_View (Full))
2201 then
2202 Set_Full_View (Id, Underlying_Full_View (Full));
2203 Set_Underlying_Full_View (Id, Full);
2204 Set_Is_Underlying_Full_View (Full);
2206 Set_Underlying_Full_View (Full, Empty);
2207 Set_Is_Frozen (Full_View (Id));
2208 end if;
2209 end if;
2211 Priv_Deps := Private_Dependents (Id);
2212 Exchange_Declarations (Id);
2213 Set_Is_Immediately_Visible (Id);
2214 Swap_Private_Dependents (Priv_Deps);
2215 end if;
2217 Next_Entity (Id);
2218 end loop;
2220 -- Next make other declarations in the private part visible as well
2222 Id := First_Private_Entity (P);
2223 while Present (Id) loop
2224 Install_Package_Entity (Id);
2225 Set_Is_Hidden (Id, False);
2226 Next_Entity (Id);
2227 end loop;
2229 -- An abstract state is partially refined when it has at least one
2230 -- Part_Of constituent. Since these constituents are being installed
2231 -- into visibility, update the partial refinement status of any state
2232 -- defined in the associated package, subject to at least one Part_Of
2233 -- constituent.
2235 if Ekind_In (P, E_Generic_Package, E_Package) then
2236 declare
2237 States : constant Elist_Id := Abstract_States (P);
2238 State_Elmt : Elmt_Id;
2239 State_Id : Entity_Id;
2241 begin
2242 if Present (States) then
2243 State_Elmt := First_Elmt (States);
2244 while Present (State_Elmt) loop
2245 State_Id := Node (State_Elmt);
2247 if Present (Part_Of_Constituents (State_Id)) then
2248 Set_Has_Partial_Visible_Refinement (State_Id);
2249 end if;
2251 Next_Elmt (State_Elmt);
2252 end loop;
2253 end if;
2254 end;
2255 end if;
2257 -- Indicate that the private part is currently visible, so it can be
2258 -- properly reset on exit.
2260 Set_In_Private_Part (P);
2261 end Install_Private_Declarations;
2263 ----------------------------------
2264 -- Install_Visible_Declarations --
2265 ----------------------------------
2267 procedure Install_Visible_Declarations (P : Entity_Id) is
2268 Id : Entity_Id;
2269 Last_Entity : Entity_Id;
2271 begin
2272 pragma Assert
2273 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
2275 if Is_Package_Or_Generic_Package (P) then
2276 Last_Entity := First_Private_Entity (P);
2277 else
2278 Last_Entity := Empty;
2279 end if;
2281 Id := First_Entity (P);
2282 while Present (Id) and then Id /= Last_Entity loop
2283 Install_Package_Entity (Id);
2284 Next_Entity (Id);
2285 end loop;
2286 end Install_Visible_Declarations;
2288 --------------------------
2289 -- Is_Private_Base_Type --
2290 --------------------------
2292 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
2293 begin
2294 return Ekind (E) = E_Private_Type
2295 or else Ekind (E) = E_Limited_Private_Type
2296 or else Ekind (E) = E_Record_Type_With_Private;
2297 end Is_Private_Base_Type;
2299 --------------------------
2300 -- Is_Visible_Dependent --
2301 --------------------------
2303 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
2305 S : constant Entity_Id := Scope (Dep);
2307 begin
2308 -- Renamings created for actual types have the visibility of the actual
2310 if Ekind (S) = E_Package
2311 and then Is_Generic_Instance (S)
2312 and then (Is_Generic_Actual_Type (Dep)
2313 or else Is_Generic_Actual_Type (Full_View (Dep)))
2314 then
2315 return True;
2317 elsif not (Is_Derived_Type (Dep))
2318 and then Is_Derived_Type (Full_View (Dep))
2319 then
2320 -- When instantiating a package body, the scope stack is empty, so
2321 -- check instead whether the dependent type is defined in the same
2322 -- scope as the instance itself.
2324 return In_Open_Scopes (S)
2325 or else (Is_Generic_Instance (Current_Scope)
2326 and then Scope (Dep) = Scope (Current_Scope));
2327 else
2328 return True;
2329 end if;
2330 end Is_Visible_Dependent;
2332 ----------------------------
2333 -- May_Need_Implicit_Body --
2334 ----------------------------
2336 procedure May_Need_Implicit_Body (E : Entity_Id) is
2337 P : constant Node_Id := Unit_Declaration_Node (E);
2338 S : constant Node_Id := Parent (P);
2339 B : Node_Id;
2340 Decls : List_Id;
2342 begin
2343 if not Has_Completion (E)
2344 and then Nkind (P) = N_Package_Declaration
2345 and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
2346 then
2347 B :=
2348 Make_Package_Body (Sloc (E),
2349 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
2350 Chars => Chars (E)),
2351 Declarations => New_List);
2353 if Nkind (S) = N_Package_Specification then
2354 if Present (Private_Declarations (S)) then
2355 Decls := Private_Declarations (S);
2356 else
2357 Decls := Visible_Declarations (S);
2358 end if;
2359 else
2360 Decls := Declarations (S);
2361 end if;
2363 Append (B, Decls);
2364 Analyze (B);
2365 end if;
2366 end May_Need_Implicit_Body;
2368 ----------------------
2369 -- New_Private_Type --
2370 ----------------------
2372 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
2373 begin
2374 -- For other than Ada 2012, enter the name in the current scope
2376 if Ada_Version < Ada_2012 then
2377 Enter_Name (Id);
2379 -- Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2380 -- there may be an incomplete previous view.
2382 else
2383 declare
2384 Prev : Entity_Id;
2385 begin
2386 Prev := Find_Type_Name (N);
2387 pragma Assert (Prev = Id
2388 or else (Ekind (Prev) = E_Incomplete_Type
2389 and then Present (Full_View (Prev))
2390 and then Full_View (Prev) = Id));
2391 end;
2392 end if;
2394 if Limited_Present (Def) then
2395 Set_Ekind (Id, E_Limited_Private_Type);
2396 else
2397 Set_Ekind (Id, E_Private_Type);
2398 end if;
2400 Set_Etype (Id, Id);
2401 Set_Has_Delayed_Freeze (Id);
2402 Set_Is_First_Subtype (Id);
2403 Init_Size_Align (Id);
2405 Set_Is_Constrained (Id,
2406 No (Discriminant_Specifications (N))
2407 and then not Unknown_Discriminants_Present (N));
2409 -- Set tagged flag before processing discriminants, to catch illegal
2410 -- usage.
2412 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
2414 Set_Discriminant_Constraint (Id, No_Elist);
2415 Set_Stored_Constraint (Id, No_Elist);
2417 if Present (Discriminant_Specifications (N)) then
2418 Push_Scope (Id);
2419 Process_Discriminants (N);
2420 End_Scope;
2422 elsif Unknown_Discriminants_Present (N) then
2423 Set_Has_Unknown_Discriminants (Id);
2424 end if;
2426 Set_Private_Dependents (Id, New_Elmt_List);
2428 if Tagged_Present (Def) then
2429 Set_Ekind (Id, E_Record_Type_With_Private);
2430 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2431 Set_Is_Abstract_Type (Id, Abstract_Present (Def));
2432 Set_Is_Limited_Record (Id, Limited_Present (Def));
2433 Set_Has_Delayed_Freeze (Id, True);
2435 -- Recognize Ada.Real_Time.Timing_Events.Timing_Events here
2437 if Is_RTE (Id, RE_Timing_Event) then
2438 Set_Has_Timing_Event (Id);
2439 end if;
2441 -- Create a class-wide type with the same attributes
2443 Make_Class_Wide_Type (Id);
2445 elsif Abstract_Present (Def) then
2446 Error_Msg_N ("only a tagged type can be abstract", N);
2447 end if;
2448 end New_Private_Type;
2450 ---------------------------------
2451 -- Requires_Completion_In_Body --
2452 ---------------------------------
2454 function Requires_Completion_In_Body
2455 (Id : Entity_Id;
2456 Pack_Id : Entity_Id;
2457 Do_Abstract_States : Boolean := False) return Boolean
2459 begin
2460 -- Always ignore child units. Child units get added to the entity list
2461 -- of a parent unit, but are not original entities of the parent, and
2462 -- so do not affect whether the parent needs a body.
2464 if Is_Child_Unit (Id) then
2465 return False;
2467 -- Ignore formal packages and their renamings
2469 elsif Ekind (Id) = E_Package
2470 and then Nkind (Original_Node (Unit_Declaration_Node (Id))) =
2471 N_Formal_Package_Declaration
2472 then
2473 return False;
2475 -- Otherwise test to see if entity requires a completion. Note that
2476 -- subprogram entities whose declaration does not come from source are
2477 -- ignored here on the basis that we assume the expander will provide an
2478 -- implicit completion at some point.
2480 elsif (Is_Overloadable (Id)
2481 and then not Ekind_In (Id, E_Enumeration_Literal, E_Operator)
2482 and then not Is_Abstract_Subprogram (Id)
2483 and then not Has_Completion (Id)
2484 and then Comes_From_Source (Parent (Id)))
2486 or else
2487 (Ekind (Id) = E_Package
2488 and then Id /= Pack_Id
2489 and then not Has_Completion (Id)
2490 and then Unit_Requires_Body (Id, Do_Abstract_States))
2492 or else
2493 (Ekind (Id) = E_Incomplete_Type
2494 and then No (Full_View (Id))
2495 and then not Is_Generic_Type (Id))
2497 or else
2498 (Ekind_In (Id, E_Task_Type, E_Protected_Type)
2499 and then not Has_Completion (Id))
2501 or else
2502 (Ekind (Id) = E_Generic_Package
2503 and then Id /= Pack_Id
2504 and then not Has_Completion (Id)
2505 and then Unit_Requires_Body (Id, Do_Abstract_States))
2507 or else
2508 (Is_Generic_Subprogram (Id)
2509 and then not Has_Completion (Id))
2510 then
2511 return True;
2513 -- Otherwise the entity does not require completion in a package body
2515 else
2516 return False;
2517 end if;
2518 end Requires_Completion_In_Body;
2520 ----------------------------
2521 -- Uninstall_Declarations --
2522 ----------------------------
2524 procedure Uninstall_Declarations (P : Entity_Id) is
2525 Decl : constant Node_Id := Unit_Declaration_Node (P);
2526 Id : Entity_Id;
2527 Full : Entity_Id;
2528 Priv_Elmt : Elmt_Id;
2529 Priv_Sub : Entity_Id;
2531 procedure Preserve_Full_Attributes (Priv : Entity_Id; Full : Entity_Id);
2532 -- Copy to the private declaration the attributes of the full view that
2533 -- need to be available for the partial view also.
2535 function Type_In_Use (T : Entity_Id) return Boolean;
2536 -- Check whether type or base type appear in an active use_type clause
2538 ------------------------------
2539 -- Preserve_Full_Attributes --
2540 ------------------------------
2542 procedure Preserve_Full_Attributes
2543 (Priv : Entity_Id;
2544 Full : Entity_Id)
2546 Full_Base : constant Entity_Id := Base_Type (Full);
2547 Priv_Is_Base_Type : constant Boolean := Is_Base_Type (Priv);
2549 begin
2550 Set_Size_Info (Priv, Full);
2551 Set_RM_Size (Priv, RM_Size (Full));
2552 Set_Size_Known_At_Compile_Time
2553 (Priv, Size_Known_At_Compile_Time (Full));
2554 Set_Is_Volatile (Priv, Is_Volatile (Full));
2555 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
2556 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full));
2557 Set_Is_Ada_2012_Only (Priv, Is_Ada_2012_Only (Full));
2558 Set_Has_Pragma_Unmodified (Priv, Has_Pragma_Unmodified (Full));
2559 Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced (Full));
2560 Set_Has_Pragma_Unreferenced_Objects
2561 (Priv, Has_Pragma_Unreferenced_Objects
2562 (Full));
2563 if Is_Unchecked_Union (Full) then
2564 Set_Is_Unchecked_Union (Base_Type (Priv));
2565 end if;
2566 -- Why is atomic not copied here ???
2568 if Referenced (Full) then
2569 Set_Referenced (Priv);
2570 end if;
2572 if Priv_Is_Base_Type then
2573 Set_Is_Controlled (Priv, Is_Controlled (Full_Base));
2574 Set_Finalize_Storage_Only
2575 (Priv, Finalize_Storage_Only (Full_Base));
2576 Set_Has_Controlled_Component
2577 (Priv, Has_Controlled_Component (Full_Base));
2579 Propagate_Concurrent_Flags (Priv, Base_Type (Full));
2580 end if;
2582 Set_Freeze_Node (Priv, Freeze_Node (Full));
2584 -- Propagate Default_Initial_Condition-related attributes from the
2585 -- base type of the full view to the full view and vice versa. This
2586 -- may seem strange, but is necessary depending on which type
2587 -- triggered the generation of the DIC procedure body. As a result,
2588 -- both the full view and its base type carry the same DIC-related
2589 -- information.
2591 Propagate_DIC_Attributes (Full, From_Typ => Full_Base);
2592 Propagate_DIC_Attributes (Full_Base, From_Typ => Full);
2594 -- Propagate Default_Initial_Condition-related attributes from the
2595 -- full view to the private view.
2597 Propagate_DIC_Attributes (Priv, From_Typ => Full);
2599 -- Propagate invariant-related attributes from the base type of the
2600 -- full view to the full view and vice versa. This may seem strange,
2601 -- but is necessary depending on which type triggered the generation
2602 -- of the invariant procedure body. As a result, both the full view
2603 -- and its base type carry the same invariant-related information.
2605 Propagate_Invariant_Attributes (Full, From_Typ => Full_Base);
2606 Propagate_Invariant_Attributes (Full_Base, From_Typ => Full);
2608 -- Propagate invariant-related attributes from the full view to the
2609 -- private view.
2611 Propagate_Invariant_Attributes (Priv, From_Typ => Full);
2613 if Is_Tagged_Type (Priv)
2614 and then Is_Tagged_Type (Full)
2615 and then not Error_Posted (Full)
2616 then
2617 if Is_Tagged_Type (Priv) then
2619 -- If the type is tagged, the tag itself must be available on
2620 -- the partial view, for expansion purposes.
2622 Set_First_Entity (Priv, First_Entity (Full));
2624 -- If there are discriminants in the partial view, these remain
2625 -- visible. Otherwise only the tag itself is visible, and there
2626 -- are no nameable components in the partial view.
2628 if No (Last_Entity (Priv)) then
2629 Set_Last_Entity (Priv, First_Entity (Priv));
2630 end if;
2631 end if;
2633 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2635 if Has_Discriminants (Full) then
2636 Set_Discriminant_Constraint (Priv,
2637 Discriminant_Constraint (Full));
2638 end if;
2639 end if;
2640 end Preserve_Full_Attributes;
2642 -----------------
2643 -- Type_In_Use --
2644 -----------------
2646 function Type_In_Use (T : Entity_Id) return Boolean is
2647 begin
2648 return Scope (Base_Type (T)) = P
2649 and then (In_Use (T) or else In_Use (Base_Type (T)));
2650 end Type_In_Use;
2652 -- Start of processing for Uninstall_Declarations
2654 begin
2655 Id := First_Entity (P);
2656 while Present (Id) and then Id /= First_Private_Entity (P) loop
2657 if Debug_Flag_E then
2658 Write_Str ("unlinking visible entity ");
2659 Write_Int (Int (Id));
2660 Write_Eol;
2661 end if;
2663 -- On exit from the package scope, we must preserve the visibility
2664 -- established by use clauses in the current scope. Two cases:
2666 -- a) If the entity is an operator, it may be a primitive operator of
2667 -- a type for which there is a visible use-type clause.
2669 -- b) for other entities, their use-visibility is determined by a
2670 -- visible use clause for the package itself. For a generic instance,
2671 -- the instantiation of the formals appears in the visible part,
2672 -- but the formals are private and remain so.
2674 if Ekind (Id) = E_Function
2675 and then Is_Operator_Symbol_Name (Chars (Id))
2676 and then not Is_Hidden (Id)
2677 and then not Error_Posted (Id)
2678 then
2679 Set_Is_Potentially_Use_Visible (Id,
2680 In_Use (P)
2681 or else Type_In_Use (Etype (Id))
2682 or else Type_In_Use (Etype (First_Formal (Id)))
2683 or else (Present (Next_Formal (First_Formal (Id)))
2684 and then
2685 Type_In_Use
2686 (Etype (Next_Formal (First_Formal (Id))))));
2687 else
2688 if In_Use (P) and then not Is_Hidden (Id) then
2690 -- A child unit of a use-visible package remains use-visible
2691 -- only if it is itself a visible child unit. Otherwise it
2692 -- would remain visible in other contexts where P is use-
2693 -- visible, because once compiled it stays in the entity list
2694 -- of its parent unit.
2696 if Is_Child_Unit (Id) then
2697 Set_Is_Potentially_Use_Visible
2698 (Id, Is_Visible_Lib_Unit (Id));
2699 else
2700 Set_Is_Potentially_Use_Visible (Id);
2701 end if;
2703 else
2704 Set_Is_Potentially_Use_Visible (Id, False);
2705 end if;
2706 end if;
2708 -- Local entities are not immediately visible outside of the package
2710 Set_Is_Immediately_Visible (Id, False);
2712 -- If this is a private type with a full view (for example a local
2713 -- subtype of a private type declared elsewhere), ensure that the
2714 -- full view is also removed from visibility: it may be exposed when
2715 -- swapping views in an instantiation. Similarly, ensure that the
2716 -- use-visibility is properly set on both views.
2718 if Is_Type (Id) and then Present (Full_View (Id)) then
2719 Set_Is_Immediately_Visible (Full_View (Id), False);
2720 Set_Is_Potentially_Use_Visible (Full_View (Id),
2721 Is_Potentially_Use_Visible (Id));
2722 end if;
2724 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2725 Check_Abstract_Overriding (Id);
2726 Check_Conventions (Id);
2727 end if;
2729 if Ekind_In (Id, E_Private_Type, E_Limited_Private_Type)
2730 and then No (Full_View (Id))
2731 and then not Is_Generic_Type (Id)
2732 and then not Is_Derived_Type (Id)
2733 then
2734 Error_Msg_N ("missing full declaration for private type&", Id);
2736 elsif Ekind (Id) = E_Record_Type_With_Private
2737 and then not Is_Generic_Type (Id)
2738 and then No (Full_View (Id))
2739 then
2740 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
2741 Error_Msg_N ("missing full declaration for private type&", Id);
2742 else
2743 Error_Msg_N
2744 ("missing full declaration for private extension", Id);
2745 end if;
2747 -- Case of constant, check for deferred constant declaration with
2748 -- no full view. Likely just a matter of a missing expression, or
2749 -- accidental use of the keyword constant.
2751 elsif Ekind (Id) = E_Constant
2753 -- OK if constant value present
2755 and then No (Constant_Value (Id))
2757 -- OK if full view present
2759 and then No (Full_View (Id))
2761 -- OK if imported, since that provides the completion
2763 and then not Is_Imported (Id)
2765 -- OK if object declaration replaced by renaming declaration as
2766 -- a result of OK_To_Rename processing (e.g. for concatenation)
2768 and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration
2770 -- OK if object declaration with the No_Initialization flag set
2772 and then not (Nkind (Parent (Id)) = N_Object_Declaration
2773 and then No_Initialization (Parent (Id)))
2774 then
2775 -- If no private declaration is present, we assume the user did
2776 -- not intend a deferred constant declaration and the problem
2777 -- is simply that the initializing expression is missing.
2779 if not Has_Private_Declaration (Etype (Id)) then
2781 -- We assume that the user did not intend a deferred constant
2782 -- declaration, and the expression is just missing.
2784 Error_Msg_N
2785 ("constant declaration requires initialization expression",
2786 Parent (Id));
2788 if Is_Limited_Type (Etype (Id)) then
2789 Error_Msg_N
2790 ("\if variable intended, remove CONSTANT from declaration",
2791 Parent (Id));
2792 end if;
2794 -- Otherwise if a private declaration is present, then we are
2795 -- missing the full declaration for the deferred constant.
2797 else
2798 Error_Msg_N
2799 ("missing full declaration for deferred constant (RM 7.4)",
2800 Id);
2802 if Is_Limited_Type (Etype (Id)) then
2803 Error_Msg_N
2804 ("\if variable intended, remove CONSTANT from declaration",
2805 Parent (Id));
2806 end if;
2807 end if;
2808 end if;
2810 Next_Entity (Id);
2811 end loop;
2813 -- If the specification was installed as the parent of a public child
2814 -- unit, the private declarations were not installed, and there is
2815 -- nothing to do.
2817 if not In_Private_Part (P) then
2818 return;
2819 else
2820 Set_In_Private_Part (P, False);
2821 end if;
2823 -- Make private entities invisible and exchange full and private
2824 -- declarations for private types. Id is now the first private entity
2825 -- in the package.
2827 while Present (Id) loop
2828 if Debug_Flag_E then
2829 Write_Str ("unlinking private entity ");
2830 Write_Int (Int (Id));
2831 Write_Eol;
2832 end if;
2834 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2835 Check_Abstract_Overriding (Id);
2836 Check_Conventions (Id);
2837 end if;
2839 Set_Is_Immediately_Visible (Id, False);
2841 if Is_Private_Base_Type (Id) and then Present (Full_View (Id)) then
2842 Full := Full_View (Id);
2844 -- If the partial view is not declared in the visible part of the
2845 -- package (as is the case when it is a type derived from some
2846 -- other private type in the private part of the current package),
2847 -- no exchange takes place.
2849 if No (Parent (Id))
2850 or else List_Containing (Parent (Id)) /=
2851 Visible_Declarations (Specification (Decl))
2852 then
2853 goto Next_Id;
2854 end if;
2856 -- The entry in the private part points to the full declaration,
2857 -- which is currently visible. Exchange them so only the private
2858 -- type declaration remains accessible, and link private and full
2859 -- declaration in the opposite direction. Before the actual
2860 -- exchange, we copy back attributes of the full view that must
2861 -- be available to the partial view too.
2863 Preserve_Full_Attributes (Id, Full);
2865 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
2867 -- The following test may be redundant, as this is already
2868 -- diagnosed in sem_ch3. ???
2870 if not Is_Definite_Subtype (Full)
2871 and then Is_Definite_Subtype (Id)
2872 then
2873 Error_Msg_Sloc := Sloc (Parent (Id));
2874 Error_Msg_NE
2875 ("full view of& not compatible with declaration#", Full, Id);
2876 end if;
2878 -- Swap out the subtypes and derived types of Id that
2879 -- were compiled in this scope, or installed previously
2880 -- by Install_Private_Declarations.
2882 -- Before we do the swap, we verify the presence of the Full_View
2883 -- field which may be empty due to a swap by a previous call to
2884 -- End_Package_Scope (e.g. from the freezing mechanism).
2886 Priv_Elmt := First_Elmt (Private_Dependents (Id));
2887 while Present (Priv_Elmt) loop
2888 Priv_Sub := Node (Priv_Elmt);
2890 if Present (Full_View (Priv_Sub)) then
2891 if Scope (Priv_Sub) = P
2892 or else not In_Open_Scopes (Scope (Priv_Sub))
2893 then
2894 Set_Is_Immediately_Visible (Priv_Sub, False);
2895 end if;
2897 if Is_Visible_Dependent (Priv_Sub) then
2898 Preserve_Full_Attributes
2899 (Priv_Sub, Full_View (Priv_Sub));
2900 Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub));
2901 Exchange_Declarations (Priv_Sub);
2902 end if;
2903 end if;
2905 Next_Elmt (Priv_Elmt);
2906 end loop;
2908 -- Now restore the type itself to its private view
2910 Exchange_Declarations (Id);
2912 -- If we have installed an underlying full view for a type derived
2913 -- from a private type in a child unit, restore the proper views
2914 -- of private and full view. See corresponding code in
2915 -- Install_Private_Declarations.
2917 -- After the exchange, Full denotes the private type in the
2918 -- visible part of the package.
2920 if Is_Private_Base_Type (Full)
2921 and then Present (Full_View (Full))
2922 and then Present (Underlying_Full_View (Full))
2923 and then In_Package_Body (Current_Scope)
2924 then
2925 Set_Full_View (Full, Underlying_Full_View (Full));
2926 Set_Underlying_Full_View (Full, Empty);
2927 end if;
2929 elsif Ekind (Id) = E_Incomplete_Type
2930 and then Comes_From_Source (Id)
2931 and then No (Full_View (Id))
2932 then
2933 -- Mark Taft amendment types. Verify that there are no primitive
2934 -- operations declared for the type (3.10.1(9)).
2936 Set_Has_Completion_In_Body (Id);
2938 declare
2939 Elmt : Elmt_Id;
2940 Subp : Entity_Id;
2942 begin
2943 Elmt := First_Elmt (Private_Dependents (Id));
2944 while Present (Elmt) loop
2945 Subp := Node (Elmt);
2947 -- Is_Primitive is tested because there can be cases where
2948 -- nonprimitive subprograms (in nested packages) are added
2949 -- to the Private_Dependents list.
2951 if Is_Overloadable (Subp) and then Is_Primitive (Subp) then
2952 Error_Msg_NE
2953 ("type& must be completed in the private part",
2954 Parent (Subp), Id);
2956 -- The result type of an access-to-function type cannot be a
2957 -- Taft-amendment type, unless the version is Ada 2012 or
2958 -- later (see AI05-151).
2960 elsif Ada_Version < Ada_2012
2961 and then Ekind (Subp) = E_Subprogram_Type
2962 then
2963 if Etype (Subp) = Id
2964 or else
2965 (Is_Class_Wide_Type (Etype (Subp))
2966 and then Etype (Etype (Subp)) = Id)
2967 then
2968 Error_Msg_NE
2969 ("type& must be completed in the private part",
2970 Associated_Node_For_Itype (Subp), Id);
2971 end if;
2972 end if;
2974 Next_Elmt (Elmt);
2975 end loop;
2976 end;
2978 elsif not Is_Child_Unit (Id)
2979 and then (not Is_Private_Type (Id) or else No (Full_View (Id)))
2980 then
2981 Set_Is_Hidden (Id);
2982 Set_Is_Potentially_Use_Visible (Id, False);
2983 end if;
2985 <<Next_Id>>
2986 Next_Entity (Id);
2987 end loop;
2988 end Uninstall_Declarations;
2990 ------------------------
2991 -- Unit_Requires_Body --
2992 ------------------------
2994 function Unit_Requires_Body
2995 (Pack_Id : Entity_Id;
2996 Do_Abstract_States : Boolean := False) return Boolean
2998 E : Entity_Id;
3000 Requires_Body : Boolean := False;
3001 -- Flag set when the unit has at least one construct that requries
3002 -- completion in a body.
3004 begin
3005 -- Imported entity never requires body. Right now, only subprograms can
3006 -- be imported, but perhaps in the future we will allow import of
3007 -- packages.
3009 if Is_Imported (Pack_Id) then
3010 return False;
3012 -- Body required if library package with pragma Elaborate_Body
3014 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3015 return True;
3017 -- Body required if subprogram
3019 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3020 return True;
3022 -- Treat a block as requiring a body
3024 elsif Ekind (Pack_Id) = E_Block then
3025 return True;
3027 elsif Ekind (Pack_Id) = E_Package
3028 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3029 and then Present (Generic_Parent (Parent (Pack_Id)))
3030 then
3031 declare
3032 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3033 begin
3034 if Has_Pragma_Elaborate_Body (G_P) then
3035 return True;
3036 end if;
3037 end;
3038 end if;
3040 -- Traverse the entity chain of the package and look for constructs that
3041 -- require a completion in a body.
3043 E := First_Entity (Pack_Id);
3044 while Present (E) loop
3046 -- Skip abstract states because their completion depends on several
3047 -- criteria (see below).
3049 if Ekind (E) = E_Abstract_State then
3050 null;
3052 elsif Requires_Completion_In_Body
3053 (E, Pack_Id, Do_Abstract_States)
3054 then
3055 Requires_Body := True;
3056 exit;
3057 end if;
3059 Next_Entity (E);
3060 end loop;
3062 -- A [generic] package that defines at least one non-null abstract state
3063 -- requires a completion only when at least one other construct requires
3064 -- a completion in a body (SPARK RM 7.1.4(4) and (6)). This check is not
3065 -- performed if the caller requests this behavior.
3067 if Do_Abstract_States
3068 and then Ekind_In (Pack_Id, E_Generic_Package, E_Package)
3069 and then Has_Non_Null_Abstract_State (Pack_Id)
3070 and then Requires_Body
3071 then
3072 return True;
3073 end if;
3075 return Requires_Body;
3076 end Unit_Requires_Body;
3078 -----------------------------
3079 -- Unit_Requires_Body_Info --
3080 -----------------------------
3082 procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id) is
3083 E : Entity_Id;
3085 begin
3086 -- An imported entity never requires body. Right now, only subprograms
3087 -- can be imported, but perhaps in the future we will allow import of
3088 -- packages.
3090 if Is_Imported (Pack_Id) then
3091 return;
3093 -- Body required if library package with pragma Elaborate_Body
3095 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3096 Error_Msg_N ("info: & requires body (Elaborate_Body)?Y?", Pack_Id);
3098 -- Body required if subprogram
3100 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3101 Error_Msg_N ("info: & requires body (subprogram case)?Y?", Pack_Id);
3103 -- Body required if generic parent has Elaborate_Body
3105 elsif Ekind (Pack_Id) = E_Package
3106 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3107 and then Present (Generic_Parent (Parent (Pack_Id)))
3108 then
3109 declare
3110 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3111 begin
3112 if Has_Pragma_Elaborate_Body (G_P) then
3113 Error_Msg_N
3114 ("info: & requires body (generic parent Elaborate_Body)?Y?",
3115 Pack_Id);
3116 end if;
3117 end;
3119 -- A [generic] package that introduces at least one non-null abstract
3120 -- state requires completion. However, there is a separate rule that
3121 -- requires that such a package have a reason other than this for a
3122 -- body being required (if necessary a pragma Elaborate_Body must be
3123 -- provided). If Ignore_Abstract_State is True, we don't do this check
3124 -- (so we can use Unit_Requires_Body to check for some other reason).
3126 elsif Ekind_In (Pack_Id, E_Generic_Package, E_Package)
3127 and then Present (Abstract_States (Pack_Id))
3128 and then not Is_Null_State
3129 (Node (First_Elmt (Abstract_States (Pack_Id))))
3130 then
3131 Error_Msg_N
3132 ("info: & requires body (non-null abstract state aspect)?Y?",
3133 Pack_Id);
3134 end if;
3136 -- Otherwise search entity chain for entity requiring completion
3138 E := First_Entity (Pack_Id);
3139 while Present (E) loop
3140 if Requires_Completion_In_Body (E, Pack_Id) then
3141 Error_Msg_Node_2 := E;
3142 Error_Msg_NE
3143 ("info: & requires body (& requires completion)?Y?", E, Pack_Id);
3144 end if;
3146 Next_Entity (E);
3147 end loop;
3148 end Unit_Requires_Body_Info;
3150 end Sem_Ch7;