2005-12-29 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / ada / sem_ch7.adb
blobe538970b5a4e02eb55c8198f9b2cc63bfbc5151d
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-2005, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 -- This package contains the routines to process package specifications and
28 -- bodies. The most important semantic aspects of package processing are the
29 -- handling of private and full declarations, and the construction of
30 -- dispatch tables for tagged types.
32 with Atree; use Atree;
33 with Debug; use Debug;
34 with Einfo; use Einfo;
35 with Elists; use Elists;
36 with Errout; use Errout;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Dbug; use Exp_Dbug;
39 with Lib; use Lib;
40 with Lib.Xref; use Lib.Xref;
41 with Namet; use Namet;
42 with Nmake; use Nmake;
43 with Nlists; use Nlists;
44 with Opt; use Opt;
45 with Output; use Output;
46 with Sem; use Sem;
47 with Sem_Cat; use Sem_Cat;
48 with Sem_Ch3; use Sem_Ch3;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Ch10; use Sem_Ch10;
52 with Sem_Ch12; use Sem_Ch12;
53 with Sem_Util; use Sem_Util;
54 with Sem_Warn; use Sem_Warn;
55 with Snames; use Snames;
56 with Stand; use Stand;
57 with Sinfo; use Sinfo;
58 with Sinput; use Sinput;
59 with Style;
61 package body Sem_Ch7 is
63 -----------------------------------
64 -- Handling private declarations --
65 -----------------------------------
67 -- The principle that each entity has a single defining occurrence clashes
68 -- with the presence of two separate definitions for private types: the
69 -- first is the private type declaration, and the second is the full type
70 -- declaration. It is important that all references to the type point to
71 -- the same defining occurrence, namely the first one. To enforce the two
72 -- separate views of the entity, the corresponding information is swapped
73 -- between the two declarations. Outside of the package, the defining
74 -- occurrence only contains the private declaration information, while in
75 -- the private part and the body of the package the defining occurrence
76 -- contains the full declaration. To simplify the swap, the defining
77 -- occurrence that currently holds the private declaration points to the
78 -- full declaration. During semantic processing the defining occurrence
79 -- also points to a list of private dependents, that is to say access types
80 -- or composite types whose designated types or component types are
81 -- subtypes or derived types of the private type in question. After the
82 -- full declaration has been seen, the private dependents are updated to
83 -- indicate that they have full definitions.
85 -----------------------
86 -- Local Subprograms --
87 -----------------------
89 procedure Install_Package_Entity (Id : Entity_Id);
90 -- Basic procedure for the previous two. Places one entity on its
91 -- visibility chain, and recurses on the visible part if the entity
92 -- is an inner package.
94 function Is_Private_Base_Type (E : Entity_Id) return Boolean;
95 -- True for a private type that is not a subtype
97 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean;
98 -- If the private dependent is a private type whose full view is
99 -- derived from the parent type, its full properties are revealed
100 -- only if we are in the immediate scope of the private dependent.
101 -- Should this predicate be tightened further???
103 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id);
104 -- Called upon entering the private part of a public child package
105 -- and the body of a nested package, to potentially declare certain
106 -- inherited subprograms that were inherited by types in the visible
107 -- part, but whose declaration was deferred because the parent
108 -- operation was private and not visible at that point. These
109 -- subprograms are located by traversing the visible part declarations
110 -- looking for non-private type extensions and then examining each of
111 -- the primitive operations of such types to find those that were
112 -- inherited but declared with a special internal name. Each such
113 -- operation is now declared as an operation with a normal name (using
114 -- the name of the parent operation) and replaces the previous implicit
115 -- operation in the primitive operations list of the type. If the
116 -- inherited private operation has been overridden, then it's
117 -- replaced by the overriding operation.
119 --------------------------
120 -- Analyze_Package_Body --
121 --------------------------
123 procedure Analyze_Package_Body (N : Node_Id) is
124 Loc : constant Source_Ptr := Sloc (N);
125 HSS : Node_Id;
126 Body_Id : Entity_Id;
127 Spec_Id : Entity_Id;
128 Last_Spec_Entity : Entity_Id;
129 New_N : Node_Id;
130 Pack_Decl : Node_Id;
132 procedure Install_Composite_Operations (P : Entity_Id);
133 -- Composite types declared in the current scope may depend on
134 -- types that were private at the point of declaration, and whose
135 -- full view is now in scope. Indicate that the corresponding
136 -- operations on the composite type are available.
138 ----------------------------------
139 -- Install_Composite_Operations --
140 ----------------------------------
142 procedure Install_Composite_Operations (P : Entity_Id) is
143 Id : Entity_Id;
145 begin
146 Id := First_Entity (P);
148 while Present (Id) loop
150 if Is_Type (Id)
151 and then (Is_Limited_Composite (Id)
152 or else Is_Private_Composite (Id))
153 and then No (Private_Component (Id))
154 then
155 Set_Is_Limited_Composite (Id, False);
156 Set_Is_Private_Composite (Id, False);
157 end if;
159 Next_Entity (Id);
160 end loop;
161 end Install_Composite_Operations;
163 -- Start of processing for Analyze_Package_Body
165 begin
166 -- Find corresponding package specification, and establish the
167 -- current scope. The visible defining entity for the package is the
168 -- defining occurrence in the spec. On exit from the package body, all
169 -- body declarations are attached to the defining entity for the body,
170 -- but the later is never used for name resolution. In this fashion
171 -- there is only one visible entity that denotes the package.
173 if Debug_Flag_C then
174 Write_Str ("==== Compiling package body ");
175 Write_Name (Chars (Defining_Entity (N)));
176 Write_Str (" from ");
177 Write_Location (Loc);
178 Write_Eol;
179 end if;
181 -- Set Body_Id. Note that this Will be reset to point to the
182 -- generic copy later on in the generic case.
184 Body_Id := Defining_Entity (N);
186 if Present (Corresponding_Spec (N)) then
188 -- Body is body of package instantiation. Corresponding spec
189 -- has already been set.
191 Spec_Id := Corresponding_Spec (N);
192 Pack_Decl := Unit_Declaration_Node (Spec_Id);
194 else
195 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
197 if Present (Spec_Id)
198 and then Is_Package_Or_Generic_Package (Spec_Id)
199 then
200 Pack_Decl := Unit_Declaration_Node (Spec_Id);
202 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
203 Error_Msg_N ("cannot supply body for package renaming", N);
204 return;
206 elsif Present (Corresponding_Body (Pack_Decl)) then
207 Error_Msg_N ("redefinition of package body", N);
208 return;
209 end if;
211 else
212 Error_Msg_N ("missing specification for package body", N);
213 return;
214 end if;
216 if Is_Package_Or_Generic_Package (Spec_Id)
217 and then
218 (Scope (Spec_Id) = Standard_Standard
219 or else Is_Child_Unit (Spec_Id))
220 and then not Unit_Requires_Body (Spec_Id)
221 then
222 if Ada_Version = Ada_83 then
223 Error_Msg_N
224 ("optional package body (not allowed in Ada 95)?", N);
225 else
226 Error_Msg_N
227 ("spec of this package does not allow a body", N);
228 end if;
229 end if;
230 end if;
232 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
233 Style.Check_Identifier (Body_Id, Spec_Id);
235 if Is_Child_Unit (Spec_Id) then
236 if Nkind (Parent (N)) /= N_Compilation_Unit then
237 Error_Msg_NE
238 ("body of child unit& cannot be an inner package", N, Spec_Id);
239 end if;
241 Set_Is_Child_Unit (Body_Id);
242 end if;
244 -- Generic package case
246 if Ekind (Spec_Id) = E_Generic_Package then
248 -- Disable expansion and perform semantic analysis on copy.
249 -- The unannotated body will be used in all instantiations.
251 Body_Id := Defining_Entity (N);
252 Set_Ekind (Body_Id, E_Package_Body);
253 Set_Scope (Body_Id, Scope (Spec_Id));
254 Set_Body_Entity (Spec_Id, Body_Id);
255 Set_Spec_Entity (Body_Id, Spec_Id);
257 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
258 Rewrite (N, New_N);
260 -- Update Body_Id to point to the copied node for the remainder
261 -- of the processing.
263 Body_Id := Defining_Entity (N);
264 Start_Generic;
265 end if;
267 -- The Body_Id is that of the copied node in the generic case, the
268 -- current node otherwise. Note that N was rewritten above, so we
269 -- must be sure to get the latest Body_Id value.
271 Set_Ekind (Body_Id, E_Package_Body);
272 Set_Body_Entity (Spec_Id, Body_Id);
273 Set_Spec_Entity (Body_Id, Spec_Id);
275 -- Defining name for the package body is not a visible entity: Only
276 -- the defining name for the declaration is visible.
278 Set_Etype (Body_Id, Standard_Void_Type);
279 Set_Scope (Body_Id, Scope (Spec_Id));
280 Set_Corresponding_Spec (N, Spec_Id);
281 Set_Corresponding_Body (Pack_Decl, Body_Id);
283 -- The body entity is not used for semantics or code generation, but
284 -- it is attached to the entity list of the enclosing scope to simplify
285 -- the listing of back-annotations for the types it main contain.
287 if Scope (Spec_Id) /= Standard_Standard then
288 Append_Entity (Body_Id, Scope (Spec_Id));
289 end if;
291 -- Indicate that we are currently compiling the body of the package
293 Set_In_Package_Body (Spec_Id);
294 Set_Has_Completion (Spec_Id);
295 Last_Spec_Entity := Last_Entity (Spec_Id);
297 New_Scope (Spec_Id);
299 Set_Categorization_From_Pragmas (N);
301 Install_Visible_Declarations (Spec_Id);
302 Install_Private_Declarations (Spec_Id);
303 Install_Private_With_Clauses (Spec_Id);
304 Install_Composite_Operations (Spec_Id);
306 if Ekind (Spec_Id) = E_Generic_Package then
307 Set_Use (Generic_Formal_Declarations (Pack_Decl));
308 end if;
310 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
311 Set_Use (Private_Declarations (Specification (Pack_Decl)));
313 -- This is a nested package, so it may be necessary to declare
314 -- certain inherited subprograms that are not yet visible because
315 -- the parent type's subprograms are now visible.
317 if Ekind (Scope (Spec_Id)) = E_Package
318 and then Scope (Spec_Id) /= Standard_Standard
319 then
320 Declare_Inherited_Private_Subprograms (Spec_Id);
321 end if;
323 if Present (Declarations (N)) then
324 Analyze_Declarations (Declarations (N));
325 end if;
327 HSS := Handled_Statement_Sequence (N);
329 if Present (HSS) then
330 Process_End_Label (HSS, 't', Spec_Id);
331 Analyze (HSS);
333 -- Check that elaboration code in a preelaborable package body is
334 -- empty other than null statements and labels (RM 10.2.1(6)).
336 Validate_Null_Statement_Sequence (N);
337 end if;
339 Validate_Categorization_Dependency (N, Spec_Id);
340 Check_Completion (Body_Id);
342 -- Generate start of body reference. Note that we do this fairly late,
343 -- because the call will use In_Extended_Main_Source_Unit as a check,
344 -- and we want to make sure that Corresponding_Stub links are set
346 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
348 -- For a generic package, collect global references and mark
349 -- them on the original body so that they are not resolved
350 -- again at the point of instantiation.
352 if Ekind (Spec_Id) /= E_Package then
353 Save_Global_References (Original_Node (N));
354 End_Generic;
355 end if;
357 -- The entities of the package body have so far been chained onto
358 -- the declaration chain for the spec. That's been fine while we
359 -- were in the body, since we wanted them to be visible, but now
360 -- that we are leaving the package body, they are no longer visible,
361 -- so we remove them from the entity chain of the package spec entity,
362 -- and copy them to the entity chain of the package body entity, where
363 -- they will never again be visible.
365 if Present (Last_Spec_Entity) then
366 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
367 Set_Next_Entity (Last_Spec_Entity, Empty);
368 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
369 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
371 else
372 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
373 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
374 Set_First_Entity (Spec_Id, Empty);
375 Set_Last_Entity (Spec_Id, Empty);
376 end if;
378 End_Package_Scope (Spec_Id);
380 -- All entities declared in body are not visible
382 declare
383 E : Entity_Id;
385 begin
386 E := First_Entity (Body_Id);
388 while Present (E) loop
389 Set_Is_Immediately_Visible (E, False);
390 Set_Is_Potentially_Use_Visible (E, False);
391 Set_Is_Hidden (E);
393 -- Child units may appear on the entity list (for example if
394 -- they appear in the context of a subunit) but they are not
395 -- body entities.
397 if not Is_Child_Unit (E) then
398 Set_Is_Package_Body_Entity (E);
399 end if;
401 Next_Entity (E);
402 end loop;
403 end;
405 Check_References (Body_Id);
407 -- For a generic unit, check that the formal parameters are referenced,
408 -- and that local variables are used, as for regular packages.
410 if Ekind (Spec_Id) = E_Generic_Package then
411 Check_References (Spec_Id);
412 end if;
414 -- The processing so far has made all entities of the package body
415 -- public (i.e. externally visible to the linker). This is in general
416 -- necessary, since inlined or generic bodies, for which code is
417 -- generated in other units, may need to see these entities. The
418 -- following loop runs backwards from the end of the entities of the
419 -- package body making these entities invisible until we reach a
420 -- referencer, i.e. a declaration that could reference a previous
421 -- declaration, a generic body or an inlined body, or a stub (which
422 -- may contain either of these). This is of course an approximation,
423 -- but it is conservative and definitely correct.
425 -- We only do this at the outer (library) level non-generic packages.
426 -- The reason is simply to cut down on the number of external symbols
427 -- generated, so this is simply an optimization of the efficiency
428 -- of the compilation process. It has no other effect.
430 if (Scope (Spec_Id) = Standard_Standard or else Is_Child_Unit (Spec_Id))
431 and then not Is_Generic_Unit (Spec_Id)
432 and then Present (Declarations (N))
433 then
434 Make_Non_Public_Where_Possible : declare
436 function Has_Referencer
437 (L : List_Id;
438 Outer : Boolean)
439 return Boolean;
440 -- Traverse the given list of declarations in reverse order.
441 -- Return True as soon as a referencer is reached. Return
442 -- False if none is found. The Outer parameter is True for
443 -- the outer level call, and False for inner level calls for
444 -- nested packages. If Outer is True, then any entities up
445 -- to the point of hitting a referencer get their Is_Public
446 -- flag cleared, so that the entities will be treated as
447 -- static entities in the C sense, and need not have fully
448 -- qualified names. For inner levels, we need all names to
449 -- be fully qualified to deal with the same name appearing
450 -- in parallel packages (right now this is tied to their
451 -- being external).
453 --------------------
454 -- Has_Referencer --
455 --------------------
457 function Has_Referencer
458 (L : List_Id;
459 Outer : Boolean)
460 return Boolean
462 D : Node_Id;
463 E : Entity_Id;
464 K : Node_Kind;
465 S : Entity_Id;
467 begin
468 if No (L) then
469 return False;
470 end if;
472 D := Last (L);
474 while Present (D) loop
475 K := Nkind (D);
477 if K in N_Body_Stub then
478 return True;
480 elsif K = N_Subprogram_Body then
481 if Acts_As_Spec (D) then
482 E := Defining_Entity (D);
484 -- An inlined body acts as a referencer. Note also
485 -- that we never reset Is_Public for an inlined
486 -- subprogram. Gigi requires Is_Public to be set.
488 -- Note that we test Has_Pragma_Inline here rather
489 -- than Is_Inlined. We are compiling this for a
490 -- client, and it is the client who will decide
491 -- if actual inlining should occur, so we need to
492 -- assume that the procedure could be inlined for
493 -- the purpose of accessing global entities.
495 if Has_Pragma_Inline (E) then
496 return True;
497 else
498 Set_Is_Public (E, False);
499 end if;
501 else
502 E := Corresponding_Spec (D);
504 if Present (E)
505 and then (Is_Generic_Unit (E)
506 or else Has_Pragma_Inline (E)
507 or else Is_Inlined (E))
508 then
509 return True;
510 end if;
511 end if;
513 -- Processing for package bodies
515 elsif K = N_Package_Body
516 and then Present (Corresponding_Spec (D))
517 then
518 E := Corresponding_Spec (D);
520 -- Generic package body is a referencer. It would
521 -- seem that we only have to consider generics that
522 -- can be exported, i.e. where the corresponding spec
523 -- is the spec of the current package, but because of
524 -- nested instantiations, a fully private generic
525 -- body may export other private body entities.
527 if Is_Generic_Unit (E) then
528 return True;
530 -- For non-generic package body, recurse into body
531 -- unless this is an instance, we ignore instances
532 -- since they cannot have references that affect
533 -- outer entities.
535 elsif not Is_Generic_Instance (E) then
536 if Has_Referencer
537 (Declarations (D), Outer => False)
538 then
539 return True;
540 end if;
541 end if;
543 -- Processing for package specs, recurse into declarations.
544 -- Again we skip this for the case of generic instances.
546 elsif K = N_Package_Declaration then
547 S := Specification (D);
549 if not Is_Generic_Unit (Defining_Entity (S)) then
550 if Has_Referencer
551 (Private_Declarations (S), Outer => False)
552 then
553 return True;
554 elsif Has_Referencer
555 (Visible_Declarations (S), Outer => False)
556 then
557 return True;
558 end if;
559 end if;
561 -- Objects and exceptions need not be public if we have
562 -- not encountered a referencer so far. We only reset
563 -- the flag for outer level entities that are not
564 -- imported/exported, and which have no interface name.
566 elsif K = N_Object_Declaration
567 or else K = N_Exception_Declaration
568 or else K = N_Subprogram_Declaration
569 then
570 E := Defining_Entity (D);
572 if Outer
573 and then not Is_Imported (E)
574 and then not Is_Exported (E)
575 and then No (Interface_Name (E))
576 then
577 Set_Is_Public (E, False);
578 end if;
579 end if;
581 Prev (D);
582 end loop;
584 return False;
585 end Has_Referencer;
587 -- Start of processing for Make_Non_Public_Where_Possible
589 begin
590 declare
591 Discard : Boolean;
592 pragma Warnings (Off, Discard);
594 begin
595 Discard := Has_Referencer (Declarations (N), Outer => True);
596 end;
597 end Make_Non_Public_Where_Possible;
598 end if;
600 -- If expander is not active, then here is where we turn off the
601 -- In_Package_Body flag, otherwise it is turned off at the end of
602 -- the corresponding expansion routine. If this is an instance body,
603 -- we need to qualify names of local entities, because the body may
604 -- have been compiled as a preliminary to another instantiation.
606 if not Expander_Active then
607 Set_In_Package_Body (Spec_Id, False);
609 if Is_Generic_Instance (Spec_Id)
610 and then Operating_Mode = Generate_Code
611 then
612 Qualify_Entity_Names (N);
613 end if;
614 end if;
615 end Analyze_Package_Body;
617 ---------------------------------
618 -- Analyze_Package_Declaration --
619 ---------------------------------
621 procedure Analyze_Package_Declaration (N : Node_Id) is
622 Id : constant Node_Id := Defining_Entity (N);
623 PF : Boolean;
625 begin
626 -- Ada 2005 (AI-217): Check if the package has been erroneously named
627 -- in a limited-with clause of its own context. In this case the error
628 -- has been previously notified by Analyze_Context.
630 -- limited with Pkg; -- ERROR
631 -- package Pkg is ...
633 if From_With_Type (Id) then
634 return;
635 end if;
637 Generate_Definition (Id);
638 Enter_Name (Id);
639 Set_Ekind (Id, E_Package);
640 Set_Etype (Id, Standard_Void_Type);
642 New_Scope (Id);
644 PF := Is_Pure (Enclosing_Lib_Unit_Entity);
645 Set_Is_Pure (Id, PF);
647 Set_Categorization_From_Pragmas (N);
649 if Debug_Flag_C then
650 Write_Str ("==== Compiling package spec ");
651 Write_Name (Chars (Id));
652 Write_Str (" from ");
653 Write_Location (Sloc (N));
654 Write_Eol;
655 end if;
657 Analyze (Specification (N));
658 Validate_Categorization_Dependency (N, Id);
659 End_Package_Scope (Id);
661 -- For a compilation unit, indicate whether it needs a body, and
662 -- whether elaboration warnings may be meaningful on it.
664 if Nkind (Parent (N)) = N_Compilation_Unit then
665 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
667 if not Body_Required (Parent (N)) then
668 Set_Suppress_Elaboration_Warnings (Id);
669 end if;
671 Validate_RT_RAT_Component (N);
672 end if;
673 end Analyze_Package_Declaration;
675 -----------------------------------
676 -- Analyze_Package_Specification --
677 -----------------------------------
679 -- Note that this code is shared for the analysis of generic package
680 -- specs (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
682 procedure Analyze_Package_Specification (N : Node_Id) is
683 Id : constant Entity_Id := Defining_Entity (N);
684 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
685 Vis_Decls : constant List_Id := Visible_Declarations (N);
686 Priv_Decls : constant List_Id := Private_Declarations (N);
687 E : Entity_Id;
688 L : Entity_Id;
689 Public_Child : Boolean;
691 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
692 -- Clears constant indications (Never_Set_In_Source, Constant_Value,
693 -- and Is_True_Constant) on all variables that are entities of Id,
694 -- and on the chain whose first element is FE. A recursive call is
695 -- made for all packages and generic packages.
697 procedure Generate_Parent_References;
698 -- For a child unit, generate references to parent units, for
699 -- GPS navigation purposes.
701 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
702 -- Child and Unit are entities of compilation units. True if Child
703 -- is a public child of Parent as defined in 10.1.1
705 procedure Inspect_Deferred_Constant_Completion;
706 -- Examines the deferred constants in the private part of the package
707 -- specification. Emits the error message "constant declaration requires
708 -- initialization expression " if not completed by an Import pragma.
710 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
711 -- Detects all incomplete or private type declarations having a known
712 -- discriminant part that are completed by an Unchecked_Union. Emits
713 -- the error message "Unchecked_Union may not complete discriminated
714 -- partial view".
716 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
717 -- Given the package entity of a generic package instantiation or
718 -- formal package whose corresponding generic is a child unit, installs
719 -- the private declarations of each of the child unit's parents.
720 -- This has to be done at the point of entering the instance package's
721 -- private part rather than being done in Sem_Ch12.Install_Parent
722 -- (which is where the parents' visible declarations are installed).
724 ---------------------
725 -- Clear_Constants --
726 ---------------------
728 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
729 E : Entity_Id;
731 begin
732 -- Ignore package renamings, not interesting and they can
733 -- cause self referential loops in the code below.
735 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
736 return;
737 end if;
739 -- Note: in the loop below, the check for Next_Entity pointing
740 -- back to the package entity seems very odd, but it is needed,
741 -- because this kind of unexpected circularity does occur ???
743 E := FE;
744 while Present (E) and then E /= Id loop
745 if Ekind (E) = E_Variable then
746 Set_Never_Set_In_Source (E, False);
747 Set_Is_True_Constant (E, False);
748 Set_Current_Value (E, Empty);
749 Set_Is_Known_Non_Null (E, False);
751 elsif Ekind (E) = E_Package
752 or else
753 Ekind (E) = E_Generic_Package
754 then
755 Clear_Constants (E, First_Entity (E));
756 Clear_Constants (E, First_Private_Entity (E));
757 end if;
759 Next_Entity (E);
760 end loop;
761 end Clear_Constants;
763 --------------------------------
764 -- Generate_Parent_References --
765 --------------------------------
767 procedure Generate_Parent_References is
768 Decl : constant Node_Id := Parent (N);
770 begin
771 if Id = Cunit_Entity (Main_Unit)
772 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
773 then
774 Generate_Reference (Id, Scope (Id), 'k', False);
776 elsif Nkind (Unit (Cunit (Main_Unit))) /= N_Subprogram_Body
777 and then Nkind (Unit (Cunit (Main_Unit))) /= N_Subunit
778 then
779 -- If current unit is an ancestor of main unit, generate
780 -- a reference to its own parent.
782 declare
783 U : Node_Id;
784 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
786 begin
787 if Nkind (Main_Spec) = N_Package_Body then
788 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
789 end if;
791 U := Parent_Spec (Main_Spec);
792 while Present (U) loop
793 if U = Parent (Decl) then
794 Generate_Reference (Id, Scope (Id), 'k', False);
795 exit;
797 elsif Nkind (Unit (U)) = N_Package_Body then
798 exit;
800 else
801 U := Parent_Spec (Unit (U));
802 end if;
803 end loop;
804 end;
805 end if;
806 end Generate_Parent_References;
808 ---------------------
809 -- Is_Public_Child --
810 ---------------------
812 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
813 begin
814 if not Is_Private_Descendant (Child) then
815 return True;
816 else
817 if Child = Unit then
818 return not Private_Present (
819 Parent (Unit_Declaration_Node (Child)));
820 else
821 return Is_Public_Child (Scope (Child), Unit);
822 end if;
823 end if;
824 end Is_Public_Child;
826 ------------------------------------------
827 -- Inspect_Deferred_Constant_Completion --
828 ------------------------------------------
830 procedure Inspect_Deferred_Constant_Completion is
831 Decl : Node_Id;
833 begin
834 Decl := First (Priv_Decls);
835 while Present (Decl) loop
837 -- Deferred constant signature
839 if Nkind (Decl) = N_Object_Declaration
840 and then Constant_Present (Decl)
841 and then No (Expression (Decl))
843 -- No need to check internally generated constants
845 and then Comes_From_Source (Decl)
847 -- The constant is not completed. A full object declaration
848 -- or a pragma Import complete a deferred constant.
850 and then not Has_Completion (Defining_Identifier (Decl))
851 then
852 Error_Msg_N
853 ("constant declaration requires initialization expression",
854 Defining_Identifier (Decl));
855 end if;
857 Decl := Next (Decl);
858 end loop;
859 end Inspect_Deferred_Constant_Completion;
861 ----------------------------------------
862 -- Inspect_Unchecked_Union_Completion --
863 ----------------------------------------
865 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
866 Decl : Node_Id := First (Decls);
868 begin
869 while Present (Decl) loop
871 -- We are looking at an incomplete or private type declaration
872 -- with a known_discriminant_part whose full view is an
873 -- Unchecked_Union.
875 if (Nkind (Decl) = N_Incomplete_Type_Declaration
876 or else
877 Nkind (Decl) = N_Private_Type_Declaration)
878 and then Has_Discriminants (Defining_Identifier (Decl))
879 and then Present (Full_View (Defining_Identifier (Decl)))
880 and then Is_Unchecked_Union
881 (Full_View (Defining_Identifier (Decl)))
882 then
883 Error_Msg_N ("completion of discriminated partial view" &
884 " cannot be an Unchecked_Union",
885 Full_View (Defining_Identifier (Decl)));
886 end if;
888 Next (Decl);
889 end loop;
890 end Inspect_Unchecked_Union_Completion;
892 -----------------------------------------
893 -- Install_Parent_Private_Declarations --
894 -----------------------------------------
896 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
897 Inst_Par : Entity_Id := Inst_Id;
898 Gen_Par : Entity_Id;
899 Inst_Node : Node_Id;
901 begin
902 Gen_Par :=
903 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
904 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
905 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
907 if (Nkind (Inst_Node) = N_Package_Instantiation
908 or else Nkind (Inst_Node) = N_Formal_Package_Declaration)
909 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
910 then
911 Inst_Par := Entity (Prefix (Name (Inst_Node)));
913 if Present (Renamed_Entity (Inst_Par)) then
914 Inst_Par := Renamed_Entity (Inst_Par);
915 end if;
917 Gen_Par :=
918 Generic_Parent
919 (Specification (Unit_Declaration_Node (Inst_Par)));
921 -- Install the private declarations and private use clauses
922 -- of a parent instance of the child instance.
924 if Present (Gen_Par) then
925 Install_Private_Declarations (Inst_Par);
926 Set_Use (Private_Declarations
927 (Specification
928 (Unit_Declaration_Node (Inst_Par))));
930 -- If we've reached the end of the generic instance parents,
931 -- then finish off by looping through the nongeneric parents
932 -- and installing their private declarations.
934 else
935 while Present (Inst_Par)
936 and then Inst_Par /= Standard_Standard
937 and then (not In_Open_Scopes (Inst_Par)
938 or else not In_Private_Part (Inst_Par))
939 loop
940 Install_Private_Declarations (Inst_Par);
941 Set_Use (Private_Declarations
942 (Specification
943 (Unit_Declaration_Node (Inst_Par))));
944 Inst_Par := Scope (Inst_Par);
945 end loop;
947 exit;
948 end if;
950 else
951 exit;
952 end if;
953 end loop;
954 end Install_Parent_Private_Declarations;
956 -- Start of processing for Analyze_Package_Specification
958 begin
959 if Present (Vis_Decls) then
960 Analyze_Declarations (Vis_Decls);
961 end if;
963 -- Verify that incomplete types have received full declarations
965 E := First_Entity (Id);
966 while Present (E) loop
967 if Ekind (E) = E_Incomplete_Type
968 and then No (Full_View (E))
969 then
970 Error_Msg_N ("no declaration in visible part for incomplete}", E);
971 end if;
973 Next_Entity (E);
974 end loop;
976 if Is_Remote_Call_Interface (Id)
977 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
978 then
979 Validate_RCI_Declarations (Id);
980 end if;
982 -- Save global references in the visible declarations, before
983 -- installing private declarations of parent unit if there is one,
984 -- because the privacy status of types defined in the parent will
985 -- change. This is only relevant for generic child units, but is
986 -- done in all cases for uniformity.
988 if Ekind (Id) = E_Generic_Package
989 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
990 then
991 declare
992 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
993 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
995 begin
996 Set_Private_Declarations (Orig_Spec, Empty_List);
997 Save_Global_References (Orig_Decl);
998 Set_Private_Declarations (Orig_Spec, Save_Priv);
999 end;
1000 end if;
1002 -- If package is a public child unit, then make the private
1003 -- declarations of the parent visible.
1005 Public_Child := False;
1007 declare
1008 Par : Entity_Id;
1009 Pack_Decl : Node_Id;
1010 Par_Spec : Node_Id;
1012 begin
1013 Par := Id;
1014 Par_Spec := Parent_Spec (Parent (N));
1016 -- If the package is formal package of an enclosing generic, is is
1017 -- transformed into a local generic declaration, and compiled to make
1018 -- its spec available. We need to retrieve the original generic to
1019 -- determine whether it is a child unit, and install its parents.
1021 if No (Par_Spec)
1022 and then
1023 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1024 then
1025 Par := Entity (Name (Original_Node (Parent (N))));
1026 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1027 end if;
1029 if Present (Par_Spec) then
1030 Generate_Parent_References;
1032 while Scope (Par) /= Standard_Standard
1033 and then Is_Public_Child (Id, Par)
1034 loop
1035 Public_Child := True;
1036 Par := Scope (Par);
1037 Install_Private_Declarations (Par);
1038 Install_Private_With_Clauses (Par);
1039 Pack_Decl := Unit_Declaration_Node (Par);
1040 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1041 end loop;
1042 end if;
1043 end;
1045 if Is_Compilation_Unit (Id) then
1046 Install_Private_With_Clauses (Id);
1047 end if;
1049 -- If this is a package associated with a generic instance or formal
1050 -- package, then the private declarations of each of the generic's
1051 -- parents must be installed at this point.
1053 if Is_Generic_Instance (Id)
1054 or else
1055 (Nkind (Unit_Declaration_Node (Id)) = N_Generic_Package_Declaration
1056 and then
1057 Nkind (Original_Node (Unit_Declaration_Node (Id)))
1058 = N_Formal_Package_Declaration)
1059 then
1060 Install_Parent_Private_Declarations (Id);
1061 end if;
1063 -- Analyze private part if present. The flag In_Private_Part is
1064 -- reset in End_Package_Scope.
1066 L := Last_Entity (Id);
1068 if Present (Priv_Decls) then
1069 Set_In_Private_Part (Id);
1071 -- Upon entering a public child's private part, it may be
1072 -- necessary to declare subprograms that were derived in
1073 -- the package visible part but not yet made visible.
1075 if Public_Child then
1076 Declare_Inherited_Private_Subprograms (Id);
1077 end if;
1079 Analyze_Declarations (Priv_Decls);
1081 -- Check the private declarations for incomplete deferred constants
1083 Inspect_Deferred_Constant_Completion;
1085 -- The first private entity is the immediate follower of the last
1086 -- visible entity, if there was one.
1088 if Present (L) then
1089 Set_First_Private_Entity (Id, Next_Entity (L));
1090 else
1091 Set_First_Private_Entity (Id, First_Entity (Id));
1092 end if;
1094 -- There may be inherited private subprograms that need to be
1095 -- declared, even in the absence of an explicit private part.
1096 -- If there are any public declarations in the package and
1097 -- the package is a public child unit, then an implicit private
1098 -- part is assumed.
1100 elsif Present (L) and then Public_Child then
1101 Set_In_Private_Part (Id);
1102 Declare_Inherited_Private_Subprograms (Id);
1103 Set_First_Private_Entity (Id, Next_Entity (L));
1104 end if;
1106 -- Check rule of 3.6(11), which in general requires
1107 -- waiting till all full types have been seen.
1109 E := First_Entity (Id);
1110 while Present (E) loop
1111 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1112 Check_Aliased_Component_Types (E);
1113 end if;
1115 Next_Entity (E);
1116 end loop;
1118 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1119 -- declaration having a known_discriminant_part shall not be an
1120 -- Unchecked_Union type.
1122 if Present (Vis_Decls) then
1123 Inspect_Unchecked_Union_Completion (Vis_Decls);
1124 end if;
1126 if Present (Priv_Decls) then
1127 Inspect_Unchecked_Union_Completion (Priv_Decls);
1128 end if;
1130 if Ekind (Id) = E_Generic_Package
1131 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1132 and then Present (Priv_Decls)
1133 then
1134 -- Save global references in private declarations, ignoring the
1135 -- visible declarations that were processed earlier.
1137 declare
1138 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1139 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1140 Save_Form : constant List_Id :=
1141 Generic_Formal_Declarations (Orig_Decl);
1143 begin
1144 Set_Visible_Declarations (Orig_Spec, Empty_List);
1145 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1146 Save_Global_References (Orig_Decl);
1147 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1148 Set_Visible_Declarations (Orig_Spec, Save_Vis);
1149 end;
1150 end if;
1152 Process_End_Label (N, 'e', Id);
1154 -- For the case of a library level package, we must go through all
1155 -- the entities clearing the indications that the value may be
1156 -- constant and not modified. Why? Because any client of this
1157 -- package may modify these values freely from anywhere. This
1158 -- also applies to any nested packages or generic packages.
1160 -- For now we unconditionally clear constants for packages that
1161 -- are instances of generic packages. The reason is that we do not
1162 -- have the body yet, and we otherwise think things are unreferenced
1163 -- when they are not. This should be fixed sometime (the effect is
1164 -- not terrible, we just lose some warnings, and also some cases
1165 -- of value propagation) ???
1167 if Is_Library_Level_Entity (Id)
1168 or else Is_Generic_Instance (Id)
1169 then
1170 Clear_Constants (Id, First_Entity (Id));
1171 Clear_Constants (Id, First_Private_Entity (Id));
1172 end if;
1173 end Analyze_Package_Specification;
1175 --------------------------------------
1176 -- Analyze_Private_Type_Declaration --
1177 --------------------------------------
1179 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1180 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
1181 Id : constant Entity_Id := Defining_Identifier (N);
1183 begin
1184 Generate_Definition (Id);
1185 Set_Is_Pure (Id, PF);
1186 Init_Size_Align (Id);
1188 if (Ekind (Current_Scope) /= E_Package
1189 and then Ekind (Current_Scope) /= E_Generic_Package)
1190 or else In_Private_Part (Current_Scope)
1191 then
1192 Error_Msg_N ("invalid context for private declaration", N);
1193 end if;
1195 New_Private_Type (N, Id, N);
1196 Set_Depends_On_Private (Id);
1197 end Analyze_Private_Type_Declaration;
1199 -------------------------------------------
1200 -- Declare_Inherited_Private_Subprograms --
1201 -------------------------------------------
1203 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1204 E : Entity_Id;
1205 Op_List : Elist_Id;
1206 Op_Elmt : Elmt_Id;
1207 Op_Elmt_2 : Elmt_Id;
1208 Prim_Op : Entity_Id;
1209 New_Op : Entity_Id := Empty;
1210 Parent_Subp : Entity_Id;
1211 Found_Explicit : Boolean;
1212 Decl_Privates : Boolean;
1214 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1215 -- Check whether an inherited subprogram is an operation of an
1216 -- untagged derived type.
1218 ---------------------
1219 -- Is_Primitive_Of --
1220 ---------------------
1222 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1223 Formal : Entity_Id;
1225 begin
1226 if Etype (S) = T then
1227 return True;
1229 else
1230 Formal := First_Formal (S);
1232 while Present (Formal) loop
1233 if Etype (Formal) = T then
1234 return True;
1235 end if;
1237 Next_Formal (Formal);
1238 end loop;
1240 return False;
1241 end if;
1242 end Is_Primitive_Of;
1244 -- Start of processing for Declare_Inherited_Private_Subprograms
1246 begin
1247 E := First_Entity (Id);
1248 while Present (E) loop
1250 -- If the entity is a nonprivate type extension whose parent
1251 -- type is declared in an open scope, then the type may have
1252 -- inherited operations that now need to be made visible.
1253 -- Ditto if the entity is a formal derived type in a child unit.
1255 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
1256 or else
1257 (Nkind (Parent (E)) = N_Private_Extension_Declaration
1258 and then Is_Generic_Type (E)))
1259 and then In_Open_Scopes (Scope (Etype (E)))
1260 and then E = Base_Type (E)
1261 then
1262 if Is_Tagged_Type (E) then
1263 Op_List := Primitive_Operations (E);
1264 New_Op := Empty;
1265 Decl_Privates := False;
1267 Op_Elmt := First_Elmt (Op_List);
1268 while Present (Op_Elmt) loop
1269 Prim_Op := Node (Op_Elmt);
1271 -- If the primitive operation is an implicit operation
1272 -- with an internal name whose parent operation has
1273 -- a normal name, then we now need to either declare the
1274 -- operation (i.e., make it visible), or replace it
1275 -- by an overriding operation if one exists.
1277 if Present (Alias (Prim_Op))
1278 and then not Comes_From_Source (Prim_Op)
1279 and then Is_Internal_Name (Chars (Prim_Op))
1280 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1281 then
1282 Parent_Subp := Alias (Prim_Op);
1284 Found_Explicit := False;
1285 Op_Elmt_2 := Next_Elmt (Op_Elmt);
1286 while Present (Op_Elmt_2) loop
1287 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
1288 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
1289 then
1290 -- The private inherited operation has been
1291 -- overridden by an explicit subprogram, so
1292 -- change the private op's list element to
1293 -- designate the explicit so the explicit
1294 -- one will get the right dispatching slot.
1296 New_Op := Node (Op_Elmt_2);
1297 Replace_Elmt (Op_Elmt, New_Op);
1298 Remove_Elmt (Op_List, Op_Elmt_2);
1299 Found_Explicit := True;
1300 Set_Is_Overriding_Operation (New_Op);
1301 Decl_Privates := True;
1303 exit;
1304 end if;
1306 Next_Elmt (Op_Elmt_2);
1307 end loop;
1309 if not Found_Explicit then
1310 Derive_Subprogram
1311 (New_Op, Alias (Prim_Op), E, Etype (E));
1313 pragma Assert
1314 (Is_Dispatching_Operation (New_Op)
1315 and then Node (Last_Elmt (Op_List)) = New_Op);
1317 -- Substitute the new operation for the old one
1318 -- in the type's primitive operations list. Since
1319 -- the new operation was also just added to the end
1320 -- of list, the last element must be removed.
1322 -- (Question: is there a simpler way of declaring
1323 -- the operation, say by just replacing the name
1324 -- of the earlier operation, reentering it in the
1325 -- in the symbol table (how?), and marking it as
1326 -- private???)
1328 Replace_Elmt (Op_Elmt, New_Op);
1329 Remove_Last_Elmt (Op_List);
1330 Decl_Privates := True;
1331 end if;
1332 end if;
1334 Next_Elmt (Op_Elmt);
1335 end loop;
1337 -- The type's DT attributes need to be recalculated
1338 -- in the case where private dispatching operations
1339 -- have been added or overridden. Normally this action
1340 -- occurs during type freezing, but we force it here
1341 -- since the type may already have been frozen (e.g.,
1342 -- if the type's package has an empty private part).
1343 -- This can only be done if expansion is active, otherwise
1344 -- Tag may not be present.
1346 if Decl_Privates
1347 and then Expander_Active
1348 then
1349 Set_All_DT_Position (E);
1350 end if;
1352 else
1353 -- Non-tagged type, scan forward to locate
1354 -- inherited hidden operations.
1356 Prim_Op := Next_Entity (E);
1358 while Present (Prim_Op) loop
1359 if Is_Subprogram (Prim_Op)
1360 and then Present (Alias (Prim_Op))
1361 and then not Comes_From_Source (Prim_Op)
1362 and then Is_Internal_Name (Chars (Prim_Op))
1363 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1364 and then Is_Primitive_Of (E, Prim_Op)
1365 then
1366 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1367 end if;
1369 Next_Entity (Prim_Op);
1370 end loop;
1371 end if;
1372 end if;
1374 Next_Entity (E);
1375 end loop;
1376 end Declare_Inherited_Private_Subprograms;
1378 -----------------------
1379 -- End_Package_Scope --
1380 -----------------------
1382 procedure End_Package_Scope (P : Entity_Id) is
1383 begin
1384 Uninstall_Declarations (P);
1385 Pop_Scope;
1386 end End_Package_Scope;
1388 ---------------------------
1389 -- Exchange_Declarations --
1390 ---------------------------
1392 procedure Exchange_Declarations (Id : Entity_Id) is
1393 Full_Id : constant Entity_Id := Full_View (Id);
1394 H1 : constant Entity_Id := Homonym (Id);
1395 Next1 : constant Entity_Id := Next_Entity (Id);
1396 H2 : Entity_Id;
1397 Next2 : Entity_Id;
1399 begin
1400 -- If missing full declaration for type, nothing to exchange
1402 if No (Full_Id) then
1403 return;
1404 end if;
1406 -- Otherwise complete the exchange, and preserve semantic links
1408 Next2 := Next_Entity (Full_Id);
1409 H2 := Homonym (Full_Id);
1411 -- Reset full declaration pointer to reflect the switched entities
1412 -- and readjust the next entity chains.
1414 Exchange_Entities (Id, Full_Id);
1416 Set_Next_Entity (Id, Next1);
1417 Set_Homonym (Id, H1);
1419 Set_Full_View (Full_Id, Id);
1420 Set_Next_Entity (Full_Id, Next2);
1421 Set_Homonym (Full_Id, H2);
1422 end Exchange_Declarations;
1424 ----------------------------
1425 -- Install_Package_Entity --
1426 ----------------------------
1428 procedure Install_Package_Entity (Id : Entity_Id) is
1429 begin
1430 if not Is_Internal (Id) then
1431 if Debug_Flag_E then
1432 Write_Str ("Install: ");
1433 Write_Name (Chars (Id));
1434 Write_Eol;
1435 end if;
1437 if not Is_Child_Unit (Id) then
1438 Set_Is_Immediately_Visible (Id);
1439 end if;
1441 end if;
1442 end Install_Package_Entity;
1444 ----------------------------------
1445 -- Install_Private_Declarations --
1446 ----------------------------------
1448 procedure Install_Private_Declarations (P : Entity_Id) is
1449 Id : Entity_Id;
1450 Priv_Elmt : Elmt_Id;
1451 Priv : Entity_Id;
1452 Full : Entity_Id;
1454 begin
1455 -- First exchange declarations for private types, so that the
1456 -- full declaration is visible. For each private type, we check
1457 -- its Private_Dependents list and also exchange any subtypes of
1458 -- or derived types from it. Finally, if this is a Taft amendment
1459 -- type, the incomplete declaration is irrelevant, and we want to
1460 -- link the eventual full declaration with the original private
1461 -- one so we also skip the exchange.
1463 Id := First_Entity (P);
1464 while Present (Id) and then Id /= First_Private_Entity (P) loop
1466 if Is_Private_Base_Type (Id)
1467 and then Comes_From_Source (Full_View (Id))
1468 and then Present (Full_View (Id))
1469 and then Scope (Full_View (Id)) = Scope (Id)
1470 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
1471 then
1472 -- If there is a use-type clause on the private type, set the
1473 -- full view accordingly.
1475 Set_In_Use (Full_View (Id), In_Use (Id));
1476 Full := Full_View (Id);
1478 if Is_Private_Base_Type (Full)
1479 and then Has_Private_Declaration (Full)
1480 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
1481 and then In_Open_Scopes (Scope (Etype (Full)))
1482 and then In_Package_Body (Current_Scope)
1483 and then not Is_Private_Type (Etype (Full))
1484 then
1485 -- This is the completion of a private type by a derivation
1486 -- from another private type which is not private anymore. This
1487 -- can only happen in a package nested within a child package,
1488 -- when the parent type is defined in the parent unit. At this
1489 -- point the current type is not private either, and we have to
1490 -- install the underlying full view, which is now visible.
1492 if No (Full_View (Full))
1493 and then Present (Underlying_Full_View (Full))
1494 then
1495 Set_Full_View (Id, Underlying_Full_View (Full));
1496 Set_Underlying_Full_View (Full, Empty);
1497 Set_Is_Frozen (Full_View (Id));
1498 end if;
1499 end if;
1501 Priv_Elmt := First_Elmt (Private_Dependents (Id));
1503 Exchange_Declarations (Id);
1504 Set_Is_Immediately_Visible (Id);
1506 while Present (Priv_Elmt) loop
1507 Priv := Node (Priv_Elmt);
1509 -- Before the exchange, verify that the presence of the
1510 -- Full_View field. It will be empty if the entity
1511 -- has already been installed due to a previous call.
1513 if Present (Full_View (Priv))
1514 and then Is_Visible_Dependent (Priv)
1515 then
1517 -- For each subtype that is swapped, we also swap the
1518 -- reference to it in Private_Dependents, to allow access
1519 -- to it when we swap them out in End_Package_Scope.
1521 Replace_Elmt (Priv_Elmt, Full_View (Priv));
1522 Exchange_Declarations (Priv);
1523 Set_Is_Immediately_Visible
1524 (Priv, In_Open_Scopes (Scope (Priv)));
1525 Set_Is_Potentially_Use_Visible
1526 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
1527 end if;
1529 Next_Elmt (Priv_Elmt);
1530 end loop;
1531 end if;
1533 Next_Entity (Id);
1534 end loop;
1536 -- Next make other declarations in the private part visible as well
1538 Id := First_Private_Entity (P);
1540 while Present (Id) loop
1541 Install_Package_Entity (Id);
1542 Set_Is_Hidden (Id, False);
1543 Next_Entity (Id);
1544 end loop;
1546 -- Indicate that the private part is currently visible, so it can be
1547 -- properly reset on exit.
1549 Set_In_Private_Part (P);
1550 end Install_Private_Declarations;
1552 ----------------------------------
1553 -- Install_Visible_Declarations --
1554 ----------------------------------
1556 procedure Install_Visible_Declarations (P : Entity_Id) is
1557 Id : Entity_Id;
1558 Last_Entity : Entity_Id;
1560 begin
1561 pragma Assert
1562 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
1564 if Is_Package_Or_Generic_Package (P) then
1565 Last_Entity := First_Private_Entity (P);
1566 else
1567 Last_Entity := Empty;
1568 end if;
1570 Id := First_Entity (P);
1572 while Present (Id) and then Id /= Last_Entity loop
1573 Install_Package_Entity (Id);
1574 Next_Entity (Id);
1575 end loop;
1576 end Install_Visible_Declarations;
1578 --------------------------
1579 -- Is_Private_Base_Type --
1580 --------------------------
1582 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
1583 begin
1584 return Ekind (E) = E_Private_Type
1585 or else Ekind (E) = E_Limited_Private_Type
1586 or else Ekind (E) = E_Record_Type_With_Private;
1587 end Is_Private_Base_Type;
1589 --------------------------
1590 -- Is_Visible_Dependent --
1591 --------------------------
1593 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
1595 S : constant Entity_Id := Scope (Dep);
1597 begin
1598 -- Renamings created for actual types have the visibility of the
1599 -- actual.
1601 if Ekind (S) = E_Package
1602 and then Is_Generic_Instance (S)
1603 and then (Is_Generic_Actual_Type (Dep)
1604 or else Is_Generic_Actual_Type (Full_View (Dep)))
1605 then
1606 return True;
1608 elsif not (Is_Derived_Type (Dep))
1609 and then Is_Derived_Type (Full_View (Dep))
1610 then
1611 -- When instantiating a package body, the scope stack is empty,
1612 -- so check instead whether the dependent type is defined in
1613 -- the same scope as the instance itself.
1615 return In_Open_Scopes (S)
1616 or else (Is_Generic_Instance (Current_Scope)
1617 and then Scope (Dep) = Scope (Current_Scope));
1618 else
1619 return True;
1620 end if;
1621 end Is_Visible_Dependent;
1623 ----------------------------
1624 -- May_Need_Implicit_Body --
1625 ----------------------------
1627 procedure May_Need_Implicit_Body (E : Entity_Id) is
1628 P : constant Node_Id := Unit_Declaration_Node (E);
1629 S : constant Node_Id := Parent (P);
1630 B : Node_Id;
1631 Decls : List_Id;
1633 begin
1634 if not Has_Completion (E)
1635 and then Nkind (P) = N_Package_Declaration
1636 and then Present (Activation_Chain_Entity (P))
1637 then
1638 B :=
1639 Make_Package_Body (Sloc (E),
1640 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
1641 Chars => Chars (E)),
1642 Declarations => New_List);
1644 if Nkind (S) = N_Package_Specification then
1645 if Present (Private_Declarations (S)) then
1646 Decls := Private_Declarations (S);
1647 else
1648 Decls := Visible_Declarations (S);
1649 end if;
1650 else
1651 Decls := Declarations (S);
1652 end if;
1654 Append (B, Decls);
1655 Analyze (B);
1656 end if;
1657 end May_Need_Implicit_Body;
1659 ----------------------
1660 -- New_Private_Type --
1661 ----------------------
1663 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
1664 begin
1665 Enter_Name (Id);
1667 if Limited_Present (Def) then
1668 Set_Ekind (Id, E_Limited_Private_Type);
1669 else
1670 Set_Ekind (Id, E_Private_Type);
1671 end if;
1673 Set_Etype (Id, Id);
1674 Set_Has_Delayed_Freeze (Id);
1675 Set_Is_First_Subtype (Id);
1676 Init_Size_Align (Id);
1678 Set_Is_Constrained (Id,
1679 No (Discriminant_Specifications (N))
1680 and then not Unknown_Discriminants_Present (N));
1682 -- Set tagged flag before processing discriminants, to catch
1683 -- illegal usage.
1685 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
1687 Set_Discriminant_Constraint (Id, No_Elist);
1688 Set_Stored_Constraint (Id, No_Elist);
1690 if Present (Discriminant_Specifications (N)) then
1691 New_Scope (Id);
1692 Process_Discriminants (N);
1693 End_Scope;
1695 elsif Unknown_Discriminants_Present (N) then
1696 Set_Has_Unknown_Discriminants (Id);
1697 end if;
1699 Set_Private_Dependents (Id, New_Elmt_List);
1701 if Tagged_Present (Def) then
1702 Set_Ekind (Id, E_Record_Type_With_Private);
1703 Make_Class_Wide_Type (Id);
1704 Set_Primitive_Operations (Id, New_Elmt_List);
1705 Set_Is_Abstract (Id, Abstract_Present (Def));
1706 Set_Is_Limited_Record (Id, Limited_Present (Def));
1707 Set_Has_Delayed_Freeze (Id, True);
1709 elsif Abstract_Present (Def) then
1710 Error_Msg_N ("only a tagged type can be abstract", N);
1711 end if;
1712 end New_Private_Type;
1714 ----------------------------
1715 -- Uninstall_Declarations --
1716 ----------------------------
1718 procedure Uninstall_Declarations (P : Entity_Id) is
1719 Decl : constant Node_Id := Unit_Declaration_Node (P);
1720 Id : Entity_Id;
1721 Full : Entity_Id;
1722 Priv_Elmt : Elmt_Id;
1723 Priv_Sub : Entity_Id;
1725 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id);
1726 -- Copy to the private declaration the attributes of the full view
1727 -- that need to be available for the partial view also.
1729 function Type_In_Use (T : Entity_Id) return Boolean;
1730 -- Check whether type or base type appear in an active use_type clause
1732 ------------------------------
1733 -- Preserve_Full_Attributes --
1734 ------------------------------
1736 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id) is
1737 Priv_Is_Base_Type : constant Boolean := Priv = Base_Type (Priv);
1739 begin
1740 Set_Size_Info (Priv, (Full));
1741 Set_RM_Size (Priv, RM_Size (Full));
1742 Set_Size_Known_At_Compile_Time (Priv, Size_Known_At_Compile_Time
1743 (Full));
1744 Set_Is_Volatile (Priv, Is_Volatile (Full));
1745 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
1746 Set_Is_Ada_2005 (Priv, Is_Ada_2005 (Full));
1748 if Is_Unchecked_Union (Full) then
1749 Set_Is_Unchecked_Union (Base_Type (Priv));
1750 end if;
1751 -- Why is atomic not copied here ???
1753 if Referenced (Full) then
1754 Set_Referenced (Priv);
1755 end if;
1757 if Priv_Is_Base_Type then
1758 Set_Is_Controlled (Priv, Is_Controlled (Base_Type (Full)));
1759 Set_Finalize_Storage_Only (Priv, Finalize_Storage_Only
1760 (Base_Type (Full)));
1761 Set_Has_Task (Priv, Has_Task (Base_Type (Full)));
1762 Set_Has_Controlled_Component (Priv, Has_Controlled_Component
1763 (Base_Type (Full)));
1764 end if;
1766 Set_Freeze_Node (Priv, Freeze_Node (Full));
1768 if Is_Tagged_Type (Priv)
1769 and then Is_Tagged_Type (Full)
1770 and then not Error_Posted (Full)
1771 then
1772 if Priv_Is_Base_Type then
1774 -- Ada 2005 (AI-345): The full view of a type implementing
1775 -- an interface can be a task type.
1777 -- type T is new I with private;
1778 -- private
1779 -- task type T is new I with ...
1781 if Is_Interface (Etype (Priv))
1782 and then Is_Concurrent_Type (Base_Type (Full))
1783 then
1784 -- Protect the frontend against previous errors
1786 if Present (Corresponding_Record_Type
1787 (Base_Type (Full)))
1788 then
1789 Set_Access_Disp_Table
1790 (Priv, Access_Disp_Table
1791 (Corresponding_Record_Type (Base_Type (Full))));
1793 -- Generic context, or previous errors
1795 else
1796 null;
1797 end if;
1799 else
1800 Set_Access_Disp_Table
1801 (Priv, Access_Disp_Table (Base_Type (Full)));
1802 end if;
1803 end if;
1805 Set_First_Entity (Priv, First_Entity (Full));
1806 Set_Last_Entity (Priv, Last_Entity (Full));
1807 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
1808 end if;
1809 end Preserve_Full_Attributes;
1811 -----------------
1812 -- Type_In_Use --
1813 -----------------
1815 function Type_In_Use (T : Entity_Id) return Boolean is
1816 begin
1817 return Scope (Base_Type (T)) = P
1818 and then (In_Use (T) or else In_Use (Base_Type (T)));
1819 end Type_In_Use;
1821 -- Start of processing for Uninstall_Declarations
1823 begin
1824 Id := First_Entity (P);
1826 while Present (Id) and then Id /= First_Private_Entity (P) loop
1827 if Debug_Flag_E then
1828 Write_Str ("unlinking visible entity ");
1829 Write_Int (Int (Id));
1830 Write_Eol;
1831 end if;
1833 -- On exit from the package scope, we must preserve the visibility
1834 -- established by use clauses in the current scope. Two cases:
1836 -- a) If the entity is an operator, it may be a primitive operator of
1837 -- a type for which there is a visible use-type clause.
1839 -- b) for other entities, their use-visibility is determined by a
1840 -- visible use clause for the package itself. For a generic instance,
1841 -- the instantiation of the formals appears in the visible part,
1842 -- but the formals are private and remain so.
1844 if Ekind (Id) = E_Function
1845 and then Is_Operator_Symbol_Name (Chars (Id))
1846 and then not Is_Hidden (Id)
1847 and then not Error_Posted (Id)
1848 then
1849 Set_Is_Potentially_Use_Visible (Id,
1850 In_Use (P)
1851 or else Type_In_Use (Etype (Id))
1852 or else Type_In_Use (Etype (First_Formal (Id)))
1853 or else (Present (Next_Formal (First_Formal (Id)))
1854 and then
1855 Type_In_Use
1856 (Etype (Next_Formal (First_Formal (Id))))));
1857 else
1858 Set_Is_Potentially_Use_Visible (Id,
1859 In_Use (P) and not Is_Hidden (Id));
1860 end if;
1862 -- Local entities are not immediately visible outside of the package
1864 Set_Is_Immediately_Visible (Id, False);
1866 -- If this is a private type with a full view (for example a local
1867 -- subtype of a private type declared elsewhere), ensure that the
1868 -- full view is also removed from visibility: it may be exposed when
1869 -- swapping views in an instantiation.
1871 if Is_Type (Id)
1872 and then Present (Full_View (Id))
1873 then
1874 Set_Is_Immediately_Visible (Full_View (Id), False);
1875 end if;
1877 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
1878 Check_Abstract_Overriding (Id);
1879 end if;
1881 if (Ekind (Id) = E_Private_Type
1882 or else Ekind (Id) = E_Limited_Private_Type)
1883 and then No (Full_View (Id))
1884 and then not Is_Generic_Type (Id)
1885 and then not Is_Derived_Type (Id)
1886 then
1887 Error_Msg_N ("missing full declaration for private type&", Id);
1889 elsif Ekind (Id) = E_Record_Type_With_Private
1890 and then not Is_Generic_Type (Id)
1891 and then No (Full_View (Id))
1892 then
1893 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
1894 Error_Msg_N ("missing full declaration for private type&", Id);
1895 else
1896 Error_Msg_N
1897 ("missing full declaration for private extension", Id);
1898 end if;
1900 elsif Ekind (Id) = E_Constant
1901 and then No (Constant_Value (Id))
1902 and then No (Full_View (Id))
1903 and then not Is_Imported (Id)
1904 and then (Nkind (Parent (Id)) /= N_Object_Declaration
1905 or else not No_Initialization (Parent (Id)))
1906 then
1907 if not Has_Private_Declaration (Etype (Id)) then
1909 -- We assume that the user did not not intend a deferred
1910 -- constant declaration, and the expression is just missing.
1912 Error_Msg_N
1913 ("constant declaration requires initialization expression",
1914 Parent (Id));
1916 if Is_Limited_Type (Etype (Id)) then
1917 Error_Msg_N
1918 ("\else remove keyword CONSTANT from declaration",
1919 Parent (Id));
1920 end if;
1922 else
1923 Error_Msg_N
1924 ("missing full declaration for deferred constant ('R'M 7.4)",
1925 Id);
1927 if Is_Limited_Type (Etype (Id)) then
1928 Error_Msg_N
1929 ("\else remove keyword CONSTANT from declaration",
1930 Parent (Id));
1931 end if;
1932 end if;
1933 end if;
1935 Next_Entity (Id);
1936 end loop;
1938 -- If the specification was installed as the parent of a public child
1939 -- unit, the private declarations were not installed, and there is
1940 -- nothing to do.
1942 if not In_Private_Part (P) then
1943 return;
1944 else
1945 Set_In_Private_Part (P, False);
1946 end if;
1948 -- Make private entities invisible and exchange full and private
1949 -- declarations for private types.
1951 while Present (Id) loop
1952 if Debug_Flag_E then
1953 Write_Str ("unlinking private entity ");
1954 Write_Int (Int (Id));
1955 Write_Eol;
1956 end if;
1958 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
1959 Check_Abstract_Overriding (Id);
1960 end if;
1962 Set_Is_Immediately_Visible (Id, False);
1964 if Is_Private_Base_Type (Id)
1965 and then Present (Full_View (Id))
1966 then
1967 Full := Full_View (Id);
1969 -- If the partial view is not declared in the visible part
1970 -- of the package (as is the case when it is a type derived
1971 -- from some other private type in the private part of the
1972 -- current package), no exchange takes place.
1974 if No (Parent (Id))
1975 or else List_Containing (Parent (Id))
1976 /= Visible_Declarations (Specification (Decl))
1977 then
1978 goto Next_Id;
1979 end if;
1981 -- The entry in the private part points to the full declaration,
1982 -- which is currently visible. Exchange them so only the private
1983 -- type declaration remains accessible, and link private and
1984 -- full declaration in the opposite direction. Before the actual
1985 -- exchange, we copy back attributes of the full view that
1986 -- must be available to the partial view too.
1988 Preserve_Full_Attributes (Id, Full);
1990 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
1992 if Is_Indefinite_Subtype (Full)
1993 and then not Is_Indefinite_Subtype (Id)
1994 then
1995 Error_Msg_N
1996 ("full view of type must be definite subtype", Full);
1997 end if;
1999 Priv_Elmt := First_Elmt (Private_Dependents (Id));
2001 -- Swap out the subtypes and derived types of Id that were
2002 -- compiled in this scope, or installed previously by
2003 -- Install_Private_Declarations.
2004 -- Before we do the swap, we verify the presence of the
2005 -- Full_View field which may be empty due to a swap by
2006 -- a previous call to End_Package_Scope (e.g. from the
2007 -- freezing mechanism).
2009 while Present (Priv_Elmt) loop
2010 Priv_Sub := Node (Priv_Elmt);
2012 if Present (Full_View (Priv_Sub)) then
2014 if Scope (Priv_Sub) = P
2015 or else not In_Open_Scopes (Scope (Priv_Sub))
2016 then
2017 Set_Is_Immediately_Visible (Priv_Sub, False);
2018 end if;
2020 if Is_Visible_Dependent (Priv_Sub) then
2021 Preserve_Full_Attributes
2022 (Priv_Sub, Full_View (Priv_Sub));
2023 Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub));
2024 Exchange_Declarations (Priv_Sub);
2025 end if;
2026 end if;
2028 Next_Elmt (Priv_Elmt);
2029 end loop;
2031 -- Now restore the type itself to its private view
2033 Exchange_Declarations (Id);
2035 elsif Ekind (Id) = E_Incomplete_Type
2036 and then No (Full_View (Id))
2037 then
2038 -- Mark Taft amendment types
2040 Set_Has_Completion_In_Body (Id);
2042 elsif not Is_Child_Unit (Id)
2043 and then (not Is_Private_Type (Id)
2044 or else No (Full_View (Id)))
2045 then
2046 Set_Is_Hidden (Id);
2047 Set_Is_Potentially_Use_Visible (Id, False);
2048 end if;
2050 <<Next_Id>>
2051 Next_Entity (Id);
2052 end loop;
2053 end Uninstall_Declarations;
2055 ------------------------
2056 -- Unit_Requires_Body --
2057 ------------------------
2059 function Unit_Requires_Body (P : Entity_Id) return Boolean is
2060 E : Entity_Id;
2062 begin
2063 -- Imported entity never requires body. Right now, only
2064 -- subprograms can be imported, but perhaps in the future
2065 -- we will allow import of packages.
2067 if Is_Imported (P) then
2068 return False;
2070 -- Body required if library package with pragma Elaborate_Body
2072 elsif Has_Pragma_Elaborate_Body (P) then
2073 return True;
2075 -- Body required if subprogram
2077 elsif Is_Subprogram (P) or else Is_Generic_Subprogram (P) then
2078 return True;
2080 -- Treat a block as requiring a body
2082 elsif Ekind (P) = E_Block then
2083 return True;
2085 elsif Ekind (P) = E_Package
2086 and then Nkind (Parent (P)) = N_Package_Specification
2087 and then Present (Generic_Parent (Parent (P)))
2088 then
2089 declare
2090 G_P : constant Entity_Id := Generic_Parent (Parent (P));
2092 begin
2093 if Has_Pragma_Elaborate_Body (G_P) then
2094 return True;
2095 end if;
2096 end;
2097 end if;
2099 -- Otherwise search entity chain for entity requiring completion
2101 E := First_Entity (P);
2102 while Present (E) loop
2104 -- Always ignore child units. Child units get added to the entity
2105 -- list of a parent unit, but are not original entities of the
2106 -- parent, and so do not affect whether the parent needs a body.
2108 if Is_Child_Unit (E) then
2109 null;
2111 -- Ignore formal packages and their renamings
2113 elsif Ekind (E) = E_Package
2114 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
2115 N_Formal_Package_Declaration
2116 then
2117 null;
2119 -- Otherwise test to see if entity requires a completion
2121 elsif (Is_Overloadable (E)
2122 and then Ekind (E) /= E_Enumeration_Literal
2123 and then Ekind (E) /= E_Operator
2124 and then not Is_Abstract (E)
2125 and then not Has_Completion (E))
2127 or else
2128 (Ekind (E) = E_Package
2129 and then E /= P
2130 and then not Has_Completion (E)
2131 and then Unit_Requires_Body (E))
2133 or else
2134 (Ekind (E) = E_Incomplete_Type and then No (Full_View (E)))
2136 or else
2137 ((Ekind (E) = E_Task_Type or else
2138 Ekind (E) = E_Protected_Type)
2139 and then not Has_Completion (E))
2141 or else
2142 (Ekind (E) = E_Generic_Package and then E /= P
2143 and then not Has_Completion (E)
2144 and then Unit_Requires_Body (E))
2146 or else
2147 (Is_Generic_Subprogram (E)
2148 and then not Has_Completion (E))
2150 then
2151 return True;
2153 -- Entity that does not require completion
2155 else
2156 null;
2157 end if;
2159 Next_Entity (E);
2160 end loop;
2162 return False;
2163 end Unit_Requires_Body;
2165 end Sem_Ch7;