* dwarf2out.c (loc_descriptor_from_tree, case CONSTRUCTOR): New case.
[official-gcc.git] / gcc / ada / sem_ch7.adb
blob2aa339ee8263ad694a8ef8dde5ec60d6a38914ff
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-2002, 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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_Ch12; use Sem_Ch12;
52 with Sem_Util; use Sem_Util;
53 with Sem_Warn; use Sem_Warn;
54 with Snames; use Snames;
55 with Stand; use Stand;
56 with Sinfo; use Sinfo;
57 with Sinput; use Sinput;
58 with Style;
60 package body Sem_Ch7 is
62 -----------------------------------
63 -- Handling private declarations --
64 -----------------------------------
66 -- The principle that each entity has a single defining occurrence clashes
67 -- with the presence of two separate definitions for private types: the
68 -- first is the private type declaration, and the second is the full type
69 -- declaration. It is important that all references to the type point to
70 -- the same defining occurrence, namely the first one. To enforce the two
71 -- separate views of the entity, the corresponding information is swapped
72 -- between the two declarations. Outside of the package, the defining
73 -- occurrence only contains the private declaration information, while in
74 -- the private part and the body of the package the defining occurrence
75 -- contains the full declaration. To simplify the swap, the defining
76 -- occurrence that currently holds the private declaration points to the
77 -- full declaration. During semantic processing the defining occurrence
78 -- also points to a list of private dependents, that is to say access types
79 -- or composite types whose designated types or component types are
80 -- subtypes or derived types of the private type in question. After the
81 -- full declaration has been seen, the private dependents are updated to
82 -- indicate that they have full definitions.
84 -----------------------
85 -- Local Subprograms --
86 -----------------------
88 procedure Install_Composite_Operations (P : Entity_Id);
89 -- Composite types declared in the current scope may depend on
90 -- types that were private at the point of declaration, and whose
91 -- full view is now in scope. Indicate that the corresponding
92 -- operations on the composite type are available.
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 Preserve_Full_Attributes (Priv, Full : Entity_Id);
104 -- Copy to the private declaration the attributes of the full view
105 -- that need to be available for the partial view also.
107 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id);
108 -- Called upon entering the private part of a public child package
109 -- and the body of a nested package, to potentially declare certain
110 -- inherited subprograms that were inherited by types in the visible
111 -- part, but whose declaration was deferred because the parent
112 -- operation was private and not visible at that point. These
113 -- subprograms are located by traversing the visible part declarations
114 -- looking for nonprivate type extensions and then examining each of
115 -- the primitive operations of such types to find those that were
116 -- inherited but declared with a special internal name. Each such
117 -- operation is now declared as an operation with a normal name (using
118 -- the name of the parent operation) and replaces the previous implicit
119 -- operation in the primitive operations list of the type. If the
120 -- inherited private operation has been overridden, then it's
121 -- replaced by the overriding operation.
123 --------------------------
124 -- Analyze_Package_Body --
125 --------------------------
127 procedure Analyze_Package_Body (N : Node_Id) is
128 Loc : constant Source_Ptr := Sloc (N);
129 HSS : Node_Id;
130 Body_Id : Entity_Id;
131 Spec_Id : Entity_Id;
132 Last_Spec_Entity : Entity_Id;
133 New_N : Node_Id;
134 Pack_Decl : Node_Id;
136 begin
137 -- Find corresponding package specification, and establish the
138 -- current scope. The visible defining entity for the package is the
139 -- defining occurrence in the spec. On exit from the package body, all
140 -- body declarations are attached to the defining entity for the body,
141 -- but the later is never used for name resolution. In this fashion
142 -- there is only one visible entity that denotes the package.
144 if Debug_Flag_C then
145 Write_Str ("==== Compiling package body ");
146 Write_Name (Chars (Defining_Entity (N)));
147 Write_Str (" from ");
148 Write_Location (Loc);
149 Write_Eol;
150 end if;
152 -- Set Body_Id. Note that this will be reset to point to the
153 -- generic copy later on in the generic case.
155 Body_Id := Defining_Entity (N);
157 if Present (Corresponding_Spec (N)) then
159 -- Body is body of package instantiation. Corresponding spec
160 -- has already been set.
162 Spec_Id := Corresponding_Spec (N);
163 Pack_Decl := Unit_Declaration_Node (Spec_Id);
165 else
166 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
168 if Present (Spec_Id)
169 and then Is_Package (Spec_Id)
170 then
171 Pack_Decl := Unit_Declaration_Node (Spec_Id);
173 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
174 Error_Msg_N ("cannot supply body for package renaming", N);
175 return;
177 elsif Present (Corresponding_Body (Pack_Decl)) then
178 Error_Msg_N ("redefinition of package body", N);
179 return;
180 end if;
182 else
183 Error_Msg_N ("missing specification for package body", N);
184 return;
185 end if;
187 if Is_Package (Spec_Id)
188 and then
189 (Scope (Spec_Id) = Standard_Standard
190 or else Is_Child_Unit (Spec_Id))
191 and then not Unit_Requires_Body (Spec_Id)
192 then
193 if Ada_83 then
194 Error_Msg_N
195 ("optional package body (not allowed in Ada 95)?", N);
196 else
197 Error_Msg_N
198 ("spec of this package does not allow a body", N);
199 end if;
200 end if;
201 end if;
203 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
204 Style.Check_Identifier (Body_Id, Spec_Id);
206 if Is_Child_Unit (Spec_Id) then
208 if Nkind (Parent (N)) /= N_Compilation_Unit then
209 Error_Msg_NE
210 ("body of child unit& cannot be an inner package", N, Spec_Id);
211 end if;
213 Set_Is_Child_Unit (Body_Id);
214 end if;
216 -- Generic package case
218 if Ekind (Spec_Id) = E_Generic_Package then
220 -- Disable expansion and perform semantic analysis on copy.
221 -- The unannotated body will be used in all instantiations.
223 Body_Id := Defining_Entity (N);
224 Set_Ekind (Body_Id, E_Package_Body);
225 Set_Scope (Body_Id, Scope (Spec_Id));
226 Set_Body_Entity (Spec_Id, Body_Id);
227 Set_Spec_Entity (Body_Id, Spec_Id);
229 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
230 Rewrite (N, New_N);
232 -- Update Body_Id to point to the copied node for the remainder
233 -- of the processing.
235 Body_Id := Defining_Entity (N);
236 Start_Generic;
237 end if;
239 -- The Body_Id is that of the copied node in the generic case, the
240 -- current node otherwise. Note that N was rewritten above, so we
241 -- must be sure to get the latest Body_Id value.
243 Set_Ekind (Body_Id, E_Package_Body);
244 Set_Body_Entity (Spec_Id, Body_Id);
245 Set_Spec_Entity (Body_Id, Spec_Id);
247 -- Defining name for the package body is not a visible entity: Only
248 -- the defining name for the declaration is visible.
250 Set_Etype (Body_Id, Standard_Void_Type);
251 Set_Scope (Body_Id, Scope (Spec_Id));
252 Set_Corresponding_Spec (N, Spec_Id);
253 Set_Corresponding_Body (Pack_Decl, Body_Id);
255 -- The body entity is not used for semantics or code generation, but
256 -- it is attached to the entity list of the enclosing scope to simplify
257 -- the listing of back-annotations for the types it main contain.
259 if Scope (Spec_Id) /= Standard_Standard then
260 Append_Entity (Body_Id, Scope (Spec_Id));
261 end if;
263 -- Indicate that we are currently compiling the body of the package.
265 Set_In_Package_Body (Spec_Id);
266 Set_Has_Completion (Spec_Id);
267 Last_Spec_Entity := Last_Entity (Spec_Id);
269 New_Scope (Spec_Id);
271 Set_Categorization_From_Pragmas (N);
273 Install_Visible_Declarations (Spec_Id);
274 Install_Private_Declarations (Spec_Id);
275 Install_Composite_Operations (Spec_Id);
277 if Ekind (Spec_Id) = E_Generic_Package then
278 Set_Use (Generic_Formal_Declarations (Pack_Decl));
279 end if;
281 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
282 Set_Use (Private_Declarations (Specification (Pack_Decl)));
284 -- This is a nested package, so it may be necessary to declare
285 -- certain inherited subprograms that are not yet visible because
286 -- the parent type's subprograms are now visible.
288 if Ekind (Scope (Spec_Id)) = E_Package
289 and then Scope (Spec_Id) /= Standard_Standard
290 then
291 Declare_Inherited_Private_Subprograms (Spec_Id);
292 end if;
294 if Present (Declarations (N)) then
295 Analyze_Declarations (Declarations (N));
296 end if;
298 HSS := Handled_Statement_Sequence (N);
300 if Present (HSS) then
301 Process_End_Label (HSS, 't', Spec_Id);
302 Analyze (HSS);
304 -- Check that elaboration code in a preelaborable package body is
305 -- empty other than null statements and labels (RM 10.2.1(6)).
307 Validate_Null_Statement_Sequence (N);
308 end if;
310 Validate_Categorization_Dependency (N, Spec_Id);
311 Check_Completion (Body_Id);
313 -- Generate start of body reference. Note that we do this fairly late,
314 -- because the call will use In_Extended_Main_Source_Unit as a check,
315 -- and we want to make sure that Corresponding_Stub links are set
317 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
319 -- For a generic package, collect global references and mark
320 -- them on the original body so that they are not resolved
321 -- again at the point of instantiation.
323 if Ekind (Spec_Id) /= E_Package then
324 Save_Global_References (Original_Node (N));
325 End_Generic;
326 end if;
328 -- The entities of the package body have so far been chained onto
329 -- the declaration chain for the spec. That's been fine while we
330 -- were in the body, since we wanted them to be visible, but now
331 -- that we are leaving the package body, they are no longer visible,
332 -- so we remove them from the entity chain of the package spec entity,
333 -- and copy them to the entity chain of the package body entity, where
334 -- they will never again be visible.
336 if Present (Last_Spec_Entity) then
337 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
338 Set_Next_Entity (Last_Spec_Entity, Empty);
339 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
340 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
342 else
343 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
344 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
345 Set_First_Entity (Spec_Id, Empty);
346 Set_Last_Entity (Spec_Id, Empty);
347 end if;
349 End_Package_Scope (Spec_Id);
351 -- All entities declared in body are not visible.
353 declare
354 E : Entity_Id;
356 begin
357 E := First_Entity (Body_Id);
359 while Present (E) loop
360 Set_Is_Immediately_Visible (E, False);
361 Set_Is_Potentially_Use_Visible (E, False);
362 Set_Is_Hidden (E);
364 -- Child units may appear on the entity list (for example if
365 -- they appear in the context of a subunit) but they are not
366 -- body entities.
368 if not Is_Child_Unit (E) then
369 Set_Is_Package_Body_Entity (E);
370 end if;
372 Next_Entity (E);
373 end loop;
374 end;
376 Check_References (Body_Id);
378 -- The processing so far has made all entities of the package body
379 -- public (i.e. externally visible to the linker). This is in general
380 -- necessary, since inlined or generic bodies, for which code is
381 -- generated in other units, may need to see these entities. The
382 -- following loop runs backwards from the end of the entities of the
383 -- package body making these entities invisible until we reach a
384 -- referencer, i.e. a declaration that could reference a previous
385 -- declaration, a generic body or an inlined body, or a stub (which
386 -- may contain either of these). This is of course an approximation,
387 -- but it is conservative and definitely correct.
389 -- We only do this at the outer (library) level non-generic packages.
390 -- The reason is simply to cut down on the number of external symbols
391 -- generated, so this is simply an optimization of the efficiency
392 -- of the compilation process. It has no other effect.
394 if (Scope (Spec_Id) = Standard_Standard or else Is_Child_Unit (Spec_Id))
395 and then not Is_Generic_Unit (Spec_Id)
396 and then Present (Declarations (N))
397 then
398 Make_Non_Public_Where_Possible : declare
399 Discard : Boolean;
401 function Has_Referencer
402 (L : List_Id;
403 Outer : Boolean)
404 return Boolean;
405 -- Traverse the given list of declarations in reverse order.
406 -- Return True as soon as a referencer is reached. Return
407 -- False if none is found. The Outer parameter is True for
408 -- the outer level call, and False for inner level calls for
409 -- nested packages. If Outer is True, then any entities up
410 -- to the point of hitting a referencer get their Is_Public
411 -- flag cleared, so that the entities will be treated as
412 -- static entities in the C sense, and need not have fully
413 -- qualified names. For inner levels, we need all names to
414 -- be fully qualified to deal with the same name appearing
415 -- in parallel packages (right now this is tied to their
416 -- being external).
418 --------------------
419 -- Has_Referencer --
420 --------------------
422 function Has_Referencer
423 (L : List_Id;
424 Outer : Boolean)
425 return Boolean
427 D : Node_Id;
428 E : Entity_Id;
429 K : Node_Kind;
430 S : Entity_Id;
432 begin
433 if No (L) then
434 return False;
435 end if;
437 D := Last (L);
439 while Present (D) loop
440 K := Nkind (D);
442 if K in N_Body_Stub then
443 return True;
445 elsif K = N_Subprogram_Body then
446 if Acts_As_Spec (D) then
447 E := Defining_Entity (D);
449 -- An inlined body acts as a referencer. Note also
450 -- that we never reset Is_Public for an inlined
451 -- subprogram. Gigi requires Is_Public to be set.
453 -- Note that we test Has_Pragma_Inline here rather
454 -- than Is_Inlined. We are compiling this for a
455 -- client, and it is the client who will decide
456 -- if actual inlining should occur, so we need to
457 -- assume that the procedure could be inlined for
458 -- the purpose of accessing global entities.
460 if Has_Pragma_Inline (E) then
461 return True;
462 else
463 Set_Is_Public (E, False);
464 end if;
466 else
467 E := Corresponding_Spec (D);
469 if Present (E)
470 and then (Is_Generic_Unit (E)
471 or else Has_Pragma_Inline (E)
472 or else Is_Inlined (E))
473 then
474 return True;
475 end if;
476 end if;
478 -- Processing for package bodies
480 elsif K = N_Package_Body
481 and then Present (Corresponding_Spec (D))
482 then
483 E := Corresponding_Spec (D);
485 -- Generic package body is a referencer. It would
486 -- seem that we only have to consider generics that
487 -- can be exported, i.e. where the corresponding spec
488 -- is the spec of the current package, but because of
489 -- nested instantiations, a fully private generic
490 -- body may export other private body entities.
492 if Is_Generic_Unit (E) then
493 return True;
495 -- For non-generic package body, recurse into body
496 -- unless this is an instance, we ignore instances
497 -- since they cannot have references that affect
498 -- outer entities.
500 elsif not Is_Generic_Instance (E) then
501 if Has_Referencer
502 (Declarations (D), Outer => False)
503 then
504 return True;
505 end if;
506 end if;
508 -- Processing for package specs, recurse into declarations.
509 -- Again we skip this for the case of generic instances.
511 elsif K = N_Package_Declaration then
512 S := Specification (D);
514 if not Is_Generic_Unit (Defining_Entity (S)) then
515 if Has_Referencer
516 (Private_Declarations (S), Outer => False)
517 then
518 return True;
519 elsif Has_Referencer
520 (Visible_Declarations (S), Outer => False)
521 then
522 return True;
523 end if;
524 end if;
526 -- Objects and exceptions need not be public if we have
527 -- not encountered a referencer so far. We only reset
528 -- the flag for outer level entities that are not
529 -- imported/exported, and which have no interface name.
531 elsif K = N_Object_Declaration
532 or else K = N_Exception_Declaration
533 or else K = N_Subprogram_Declaration
534 then
535 E := Defining_Entity (D);
537 if Outer
538 and then not Is_Imported (E)
539 and then not Is_Exported (E)
540 and then No (Interface_Name (E))
541 then
542 Set_Is_Public (E, False);
543 end if;
544 end if;
546 Prev (D);
547 end loop;
549 return False;
550 end Has_Referencer;
552 -- Start of processing for Make_Non_Public_Where_Possible
554 begin
555 Discard := Has_Referencer (Declarations (N), Outer => True);
556 end Make_Non_Public_Where_Possible;
557 end if;
559 -- If expander is not active, then here is where we turn off the
560 -- In_Package_Body flag, otherwise it is turned off at the end of
561 -- the corresponding expansion routine. If this is an instance body,
562 -- we need to qualify names of local entities, because the body may
563 -- have been compiled as a preliminary to another instantiation.
565 if not Expander_Active then
566 Set_In_Package_Body (Spec_Id, False);
568 if Is_Generic_Instance (Spec_Id)
569 and then Operating_Mode = Generate_Code
570 then
571 Qualify_Entity_Names (N);
572 end if;
573 end if;
574 end Analyze_Package_Body;
576 ---------------------------------
577 -- Analyze_Package_Declaration --
578 ---------------------------------
580 procedure Analyze_Package_Declaration (N : Node_Id) is
581 Id : constant Node_Id := Defining_Entity (N);
582 PF : Boolean;
584 begin
585 Generate_Definition (Id);
586 Enter_Name (Id);
587 Set_Ekind (Id, E_Package);
588 Set_Etype (Id, Standard_Void_Type);
589 New_Scope (Id);
591 PF := Is_Pure (Enclosing_Lib_Unit_Entity);
592 Set_Is_Pure (Id, PF);
594 Set_Categorization_From_Pragmas (N);
596 if Debug_Flag_C then
597 Write_Str ("==== Compiling package spec ");
598 Write_Name (Chars (Id));
599 Write_Str (" from ");
600 Write_Location (Sloc (N));
601 Write_Eol;
602 end if;
604 Analyze (Specification (N));
605 Validate_Categorization_Dependency (N, Id);
606 End_Package_Scope (Id);
608 -- For a compilation unit, indicate whether it needs a body, and
609 -- whether elaboration warnings may be meaningful on it.
611 if Nkind (Parent (N)) = N_Compilation_Unit then
612 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
614 if not Body_Required (Parent (N)) then
615 Set_Suppress_Elaboration_Warnings (Id);
616 end if;
618 Validate_RT_RAT_Component (N);
619 end if;
621 -- Clear Not_Source_Assigned on all variables in the package spec,
622 -- because at this stage some client, or the body, or a child package,
623 -- may modify variables in the declaration. Note that we wait till now
624 -- to reset these flags, because during analysis of the declaration,
625 -- the flags correctly indicated the status up to that point. We
626 -- similarly clear any Is_True_Constant indications.
628 declare
629 E : Entity_Id;
631 begin
632 E := First_Entity (Id);
633 while Present (E) loop
634 if Ekind (E) = E_Variable then
635 Set_Not_Source_Assigned (E, False);
636 Set_Is_True_Constant (E, False);
637 end if;
639 Next_Entity (E);
640 end loop;
641 end;
642 end Analyze_Package_Declaration;
644 -----------------------------------
645 -- Analyze_Package_Specification --
646 -----------------------------------
648 procedure Analyze_Package_Specification (N : Node_Id) is
649 Id : constant Entity_Id := Defining_Entity (N);
650 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
651 Vis_Decls : constant List_Id := Visible_Declarations (N);
652 Priv_Decls : constant List_Id := Private_Declarations (N);
653 E : Entity_Id;
654 L : Entity_Id;
655 Public_Child : Boolean := False;
657 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
658 -- Child and Unit are entities of compilation units. True if Child
659 -- is a public child of Parent as defined in 10.1.1
661 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
662 begin
663 if not Is_Private_Descendant (Child) then
664 return True;
665 else
666 if Child = Unit then
667 return not Private_Present (
668 Parent (Unit_Declaration_Node (Child)));
669 else
670 return Is_Public_Child (Scope (Child), Unit);
671 end if;
672 end if;
673 end Is_Public_Child;
675 -- Start of processing for Analyze_Package_Specification
677 begin
678 if Present (Vis_Decls) then
679 Analyze_Declarations (Vis_Decls);
680 end if;
682 -- Verify that incomplete types have received full declarations.
684 E := First_Entity (Id);
686 while Present (E) loop
687 if Ekind (E) = E_Incomplete_Type
688 and then No (Full_View (E))
689 then
690 Error_Msg_N ("no declaration in visible part for incomplete}", E);
691 end if;
693 Next_Entity (E);
694 end loop;
696 if Is_Remote_Call_Interface (Id)
697 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
698 then
699 Validate_RCI_Declarations (Id);
700 end if;
702 -- Save global references in the visible declarations, before
703 -- installing private declarations of parent unit if there is one,
704 -- because the privacy status of types defined in the parent will
705 -- change. This is only relevant for generic child units, but is
706 -- done in all cases for uniformity.
708 if Ekind (Id) = E_Generic_Package
709 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
710 then
711 declare
712 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
713 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
715 begin
716 Set_Private_Declarations (Orig_Spec, Empty_List);
717 Save_Global_References (Orig_Decl);
718 Set_Private_Declarations (Orig_Spec, Save_Priv);
719 end;
720 end if;
722 -- If package is a public child unit, then make the private
723 -- declarations of the parent visible.
725 if Present (Parent_Spec (Parent (N))) then
726 declare
727 Par : Entity_Id := Id;
728 Pack_Decl : Node_Id;
730 begin
731 while Scope (Par) /= Standard_Standard
732 and then Is_Public_Child (Id, Par)
733 loop
734 Public_Child := True;
735 Par := Scope (Par);
736 Install_Private_Declarations (Par);
737 Pack_Decl := Unit_Declaration_Node (Par);
738 Set_Use (Private_Declarations (Specification (Pack_Decl)));
739 end loop;
740 end;
741 end if;
743 -- Analyze private part if present. The flag In_Private_Part is
744 -- reset in End_Package_Scope.
746 L := Last_Entity (Id);
748 if Present (Priv_Decls) then
749 L := Last_Entity (Id);
750 Set_In_Private_Part (Id);
752 -- Upon entering a public child's private part, it may be
753 -- necessary to declare subprograms that were derived in
754 -- the package visible part but not yet made visible.
756 if Public_Child then
757 Declare_Inherited_Private_Subprograms (Id);
758 end if;
760 Analyze_Declarations (Priv_Decls);
762 -- The first private entity is the immediate follower of the last
763 -- visible entity, if there was one.
765 if Present (L) then
766 Set_First_Private_Entity (Id, Next_Entity (L));
767 else
768 Set_First_Private_Entity (Id, First_Entity (Id));
769 end if;
771 -- There may be inherited private subprograms that need to be
772 -- declared, even in the absence of an explicit private part.
773 -- If there are any public declarations in the package and
774 -- the package is a public child unit, then an implicit private
775 -- part is assumed.
777 elsif Present (L) and then Public_Child then
778 Set_In_Private_Part (Id);
779 Declare_Inherited_Private_Subprograms (Id);
780 Set_First_Private_Entity (Id, Next_Entity (L));
781 end if;
783 -- Check rule of 3.6(11), which in general requires
784 -- waiting till all full types have been seen.
786 E := First_Entity (Id);
787 while Present (E) loop
788 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
789 Check_Aliased_Component_Types (E);
790 end if;
792 Next_Entity (E);
793 end loop;
795 if Ekind (Id) = E_Generic_Package
796 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
797 and then Present (Priv_Decls)
798 then
799 -- Save global references in private declarations, ignoring the
800 -- visible declarations that were processed earlier.
802 declare
803 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
804 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
805 Save_Form : constant List_Id :=
806 Generic_Formal_Declarations (Orig_Decl);
808 begin
809 Set_Visible_Declarations (Orig_Spec, Empty_List);
810 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
811 Save_Global_References (Orig_Decl);
812 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
813 Set_Visible_Declarations (Orig_Spec, Save_Vis);
814 end;
815 end if;
817 Process_End_Label (N, 'e', Id);
818 end Analyze_Package_Specification;
820 --------------------------------------
821 -- Analyze_Private_Type_Declaration --
822 --------------------------------------
824 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
825 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
826 Id : Entity_Id := Defining_Identifier (N);
828 begin
829 Generate_Definition (Id);
830 Set_Is_Pure (Id, PF);
831 Init_Size_Align (Id);
833 if (Ekind (Current_Scope) /= E_Package
834 and then Ekind (Current_Scope) /= E_Generic_Package)
835 or else In_Private_Part (Current_Scope)
836 then
837 Error_Msg_N ("invalid context for private declaration", N);
838 end if;
840 New_Private_Type (N, Id, N);
841 Set_Depends_On_Private (Id);
842 Set_Has_Delayed_Freeze (Id);
844 end Analyze_Private_Type_Declaration;
846 -------------------------------------------
847 -- Declare_Inherited_Private_Subprograms --
848 -------------------------------------------
850 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
851 E : Entity_Id;
852 Op_List : Elist_Id;
853 Op_Elmt : Elmt_Id;
854 Op_Elmt_2 : Elmt_Id;
855 Prim_Op : Entity_Id;
856 New_Op : Entity_Id;
857 Parent_Subp : Entity_Id;
858 Found_Explicit : Boolean;
859 Decl_Privates : Boolean;
861 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
862 -- Check whether an inherited subprogram is an operation of an
863 -- untagged derived type.
865 ---------------------
866 -- Is_Primitive_Of --
867 ---------------------
869 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
870 Formal : Entity_Id;
872 begin
873 if Etype (S) = T then
874 return True;
876 else
877 Formal := First_Formal (S);
879 while Present (Formal) loop
880 if Etype (Formal) = T then
881 return True;
882 end if;
884 Next_Formal (Formal);
885 end loop;
887 return False;
888 end if;
889 end Is_Primitive_Of;
891 -- Start of processing for Declare_Inherited_Private_Subprograms
893 begin
894 E := First_Entity (Id);
896 while Present (E) loop
898 -- If the entity is a nonprivate type extension whose parent
899 -- type is declared in an open scope, then the type may have
900 -- inherited operations that now need to be made visible.
901 -- Ditto if the entity is a formal derived type in a child unit.
903 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
904 or else
905 (Nkind (Parent (E)) = N_Private_Extension_Declaration
906 and then Is_Generic_Type (E)))
907 and then In_Open_Scopes (Scope (Etype (E)))
908 and then E = Base_Type (E)
909 then
910 if Is_Tagged_Type (E) then
911 Op_List := Primitive_Operations (E);
912 Op_Elmt := First_Elmt (Op_List);
913 New_Op := Empty;
914 Decl_Privates := False;
916 while Present (Op_Elmt) loop
917 Prim_Op := Node (Op_Elmt);
919 -- If the primitive operation is an implicit operation
920 -- with an internal name whose parent operation has
921 -- a normal name, then we now need to either declare the
922 -- operation (i.e., make it visible), or replace it
923 -- by an overriding operation if one exists.
925 if Present (Alias (Prim_Op))
926 and then not Comes_From_Source (Prim_Op)
927 and then Is_Internal_Name (Chars (Prim_Op))
928 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
929 then
930 Parent_Subp := Alias (Prim_Op);
932 Found_Explicit := False;
933 Op_Elmt_2 := Next_Elmt (Op_Elmt);
934 while Present (Op_Elmt_2) loop
935 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
936 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
937 then
938 -- The private inherited operation has been
939 -- overridden by an explicit subprogram, so
940 -- change the private op's list element to
941 -- designate the explicit so the explicit
942 -- one will get the right dispatching slot.
944 New_Op := Node (Op_Elmt_2);
945 Replace_Elmt (Op_Elmt, New_Op);
946 Remove_Elmt (Op_List, Op_Elmt_2);
947 Found_Explicit := True;
948 Decl_Privates := True;
949 exit;
950 end if;
952 Next_Elmt (Op_Elmt_2);
953 end loop;
955 if not Found_Explicit then
956 Derive_Subprogram
957 (New_Op, Alias (Prim_Op), E, Etype (E));
959 pragma Assert
960 (Is_Dispatching_Operation (New_Op)
961 and then Node (Last_Elmt (Op_List)) = New_Op);
963 -- Substitute the new operation for the old one
964 -- in the type's primitive operations list. Since
965 -- the new operation was also just added to the end
966 -- of list, the last element must be removed.
968 -- (Question: is there a simpler way of declaring
969 -- the operation, say by just replacing the name
970 -- of the earlier operation, reentering it in the
971 -- in the symbol table (how?), and marking it as
972 -- private???)
974 Replace_Elmt (Op_Elmt, New_Op);
975 Remove_Last_Elmt (Op_List);
976 Decl_Privates := True;
977 end if;
978 end if;
980 Next_Elmt (Op_Elmt);
981 end loop;
983 -- The type's DT attributes need to be recalculated
984 -- in the case where private dispatching operations
985 -- have been added or overridden. Normally this action
986 -- occurs during type freezing, but we force it here
987 -- since the type may already have been frozen (e.g.,
988 -- if the type's package has an empty private part).
989 -- This can only be done if expansion is active, otherwise
990 -- Tag may not be present.
992 if Decl_Privates
993 and then Expander_Active
994 then
995 Set_All_DT_Position (E);
996 end if;
998 else
999 -- Non-tagged type, scan forward to locate
1000 -- inherited hidden operations.
1002 Prim_Op := Next_Entity (E);
1004 while Present (Prim_Op) loop
1005 if Is_Subprogram (Prim_Op)
1006 and then Present (Alias (Prim_Op))
1007 and then not Comes_From_Source (Prim_Op)
1008 and then Is_Internal_Name (Chars (Prim_Op))
1009 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1010 and then Is_Primitive_Of (E, Prim_Op)
1011 then
1012 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1013 end if;
1015 Next_Entity (Prim_Op);
1016 end loop;
1017 end if;
1018 end if;
1020 Next_Entity (E);
1021 end loop;
1022 end Declare_Inherited_Private_Subprograms;
1024 -----------------------
1025 -- End_Package_Scope --
1026 -----------------------
1028 procedure End_Package_Scope (P : Entity_Id) is
1029 begin
1030 Uninstall_Declarations (P);
1031 Pop_Scope;
1032 end End_Package_Scope;
1034 ---------------------------
1035 -- Exchange_Declarations --
1036 ---------------------------
1038 procedure Exchange_Declarations (Id : Entity_Id) is
1039 Full_Id : constant Entity_Id := Full_View (Id);
1040 H1 : constant Entity_Id := Homonym (Id);
1041 Next1 : constant Entity_Id := Next_Entity (Id);
1042 H2 : Entity_Id;
1043 Next2 : Entity_Id;
1045 begin
1046 -- If missing full declaration for type, nothing to exchange
1048 if No (Full_Id) then
1049 return;
1050 end if;
1052 -- Otherwise complete the exchange, and preserve semantic links
1054 Next2 := Next_Entity (Full_Id);
1055 H2 := Homonym (Full_Id);
1057 -- Reset full declaration pointer to reflect the switched entities
1058 -- and readjust the next entity chains.
1060 Exchange_Entities (Id, Full_Id);
1062 Set_Next_Entity (Id, Next1);
1063 Set_Homonym (Id, H1);
1065 Set_Full_View (Full_Id, Id);
1066 Set_Next_Entity (Full_Id, Next2);
1067 Set_Homonym (Full_Id, H2);
1068 end Exchange_Declarations;
1070 ----------------------------------
1071 -- Install_Composite_Operations --
1072 ----------------------------------
1074 procedure Install_Composite_Operations (P : Entity_Id) is
1075 Id : Entity_Id;
1077 begin
1078 Id := First_Entity (P);
1080 while Present (Id) loop
1082 if Is_Type (Id)
1083 and then (Is_Limited_Composite (Id)
1084 or else Is_Private_Composite (Id))
1085 and then No (Private_Component (Id))
1086 then
1087 Set_Is_Limited_Composite (Id, False);
1088 Set_Is_Private_Composite (Id, False);
1089 end if;
1091 Next_Entity (Id);
1092 end loop;
1093 end Install_Composite_Operations;
1095 ----------------------------
1096 -- Install_Package_Entity --
1097 ----------------------------
1099 procedure Install_Package_Entity (Id : Entity_Id) is
1100 begin
1101 if not Is_Internal (Id) then
1102 if Debug_Flag_E then
1103 Write_Str ("Install: ");
1104 Write_Name (Chars (Id));
1105 Write_Eol;
1106 end if;
1108 if not Is_Child_Unit (Id) then
1109 Set_Is_Immediately_Visible (Id);
1110 end if;
1112 end if;
1113 end Install_Package_Entity;
1115 ----------------------------------
1116 -- Install_Private_Declarations --
1117 ----------------------------------
1119 procedure Install_Private_Declarations (P : Entity_Id) is
1120 Id : Entity_Id;
1121 Priv_Elmt : Elmt_Id;
1122 Priv : Entity_Id;
1123 Full : Entity_Id;
1125 begin
1126 -- First exchange declarations for private types, so that the
1127 -- full declaration is visible. For each private type, we check
1128 -- its Private_Dependents list and also exchange any subtypes of
1129 -- or derived types from it. Finally, if this is a Taft amendment
1130 -- type, the incomplete declaration is irrelevant, and we want to
1131 -- link the eventual full declaration with the original private
1132 -- one so we also skip the exchange.
1134 Id := First_Entity (P);
1136 while Present (Id) and then Id /= First_Private_Entity (P) loop
1138 if Is_Private_Base_Type (Id)
1139 and then Comes_From_Source (Full_View (Id))
1140 and then Present (Full_View (Id))
1141 and then Scope (Full_View (Id)) = Scope (Id)
1142 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
1143 then
1144 Priv_Elmt := First_Elmt (Private_Dependents (Id));
1146 -- If there is a use-type clause on the private type, set the
1147 -- full view accordingly.
1149 Set_In_Use (Full_View (Id), In_Use (Id));
1150 Full := Full_View (Id);
1152 if Is_Private_Base_Type (Full)
1153 and then Has_Private_Declaration (Full)
1154 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
1155 and then In_Open_Scopes (Scope (Etype (Full)))
1156 and then In_Package_Body (Current_Scope)
1157 and then not Is_Private_Type (Etype (Full))
1158 then
1159 -- This is the completion of a private type by a derivation
1160 -- from another private type which is not private anymore. This
1161 -- can only happen in a package nested within a child package,
1162 -- when the parent type is defined in the parent unit. At this
1163 -- point the current type is not private either, and we have to
1164 -- install the underlying full view, which is now visible.
1166 if No (Full_View (Full))
1167 and then Present (Underlying_Full_View (Full))
1168 then
1169 Set_Full_View (Id, Underlying_Full_View (Full));
1170 Set_Underlying_Full_View (Full, Empty);
1171 Set_Is_Frozen (Full_View (Id));
1172 end if;
1173 end if;
1175 Exchange_Declarations (Id);
1176 Set_Is_Immediately_Visible (Id);
1178 while Present (Priv_Elmt) loop
1179 Priv := Node (Priv_Elmt);
1181 -- Before the exchange, verify that the presence of the
1182 -- Full_View field. It will be empty if the entity
1183 -- has already been installed due to a previous call.
1185 if Present (Full_View (Priv))
1186 and then Is_Visible_Dependent (Priv)
1187 then
1189 -- For each subtype that is swapped, we also swap the
1190 -- reference to it in Private_Dependents, to allow access
1191 -- to it when we swap them out in End_Package_Scope.
1193 Replace_Elmt (Priv_Elmt, Full_View (Priv));
1194 Exchange_Declarations (Priv);
1195 Set_Is_Immediately_Visible
1196 (Priv, In_Open_Scopes (Scope (Priv)));
1197 Set_Is_Potentially_Use_Visible
1198 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
1199 end if;
1201 Next_Elmt (Priv_Elmt);
1202 end loop;
1204 null;
1205 end if;
1207 Next_Entity (Id);
1208 end loop;
1210 -- Next make other declarations in the private part visible as well.
1212 Id := First_Private_Entity (P);
1214 while Present (Id) loop
1215 Install_Package_Entity (Id);
1216 Next_Entity (Id);
1217 end loop;
1219 -- Indicate that the private part is currently visible, so it can be
1220 -- properly reset on exit.
1222 Set_In_Private_Part (P);
1223 end Install_Private_Declarations;
1225 ----------------------------------
1226 -- Install_Visible_Declarations --
1227 ----------------------------------
1229 procedure Install_Visible_Declarations (P : Entity_Id) is
1230 Id : Entity_Id;
1232 begin
1233 Id := First_Entity (P);
1235 while Present (Id) and then Id /= First_Private_Entity (P) loop
1236 Install_Package_Entity (Id);
1237 Next_Entity (Id);
1238 end loop;
1239 end Install_Visible_Declarations;
1241 ----------------------
1242 -- Is_Fully_Visible --
1243 ----------------------
1245 -- The full declaration of a private type is visible in the private
1246 -- part of the package declaration, and in the package body, at which
1247 -- point the full declaration must have been given.
1249 function Is_Fully_Visible (Type_Id : Entity_Id) return Boolean is
1250 S : constant Entity_Id := Scope (Type_Id);
1252 begin
1253 if Is_Generic_Type (Type_Id) then
1254 return False;
1256 elsif In_Private_Part (S) then
1257 return Present (Full_View (Type_Id));
1259 else
1260 return In_Package_Body (S);
1261 end if;
1262 end Is_Fully_Visible;
1264 --------------------------
1265 -- Is_Private_Base_Type --
1266 --------------------------
1268 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
1269 begin
1270 return Ekind (E) = E_Private_Type
1271 or else Ekind (E) = E_Limited_Private_Type
1272 or else Ekind (E) = E_Record_Type_With_Private;
1273 end Is_Private_Base_Type;
1275 --------------------------
1276 -- Is_Visible_Dependent --
1277 --------------------------
1279 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
1281 S : constant Entity_Id := Scope (Dep);
1283 begin
1284 -- Renamings created for actual types have the visibility of the
1285 -- actual.
1287 if Ekind (S) = E_Package
1288 and then Is_Generic_Instance (S)
1289 and then (Is_Generic_Actual_Type (Dep)
1290 or else Is_Generic_Actual_Type (Full_View (Dep)))
1291 then
1292 return True;
1294 elsif not (Is_Derived_Type (Dep))
1295 and then Is_Derived_Type (Full_View (Dep))
1296 then
1297 return In_Open_Scopes (S);
1298 else
1299 return True;
1300 end if;
1301 end Is_Visible_Dependent;
1303 ----------------------------
1304 -- May_Need_Implicit_Body --
1305 ----------------------------
1307 procedure May_Need_Implicit_Body (E : Entity_Id) is
1308 P : constant Node_Id := Unit_Declaration_Node (E);
1309 S : constant Node_Id := Parent (P);
1310 B : Node_Id;
1311 Decls : List_Id;
1313 begin
1314 if not Has_Completion (E)
1315 and then Nkind (P) = N_Package_Declaration
1316 and then Present (Activation_Chain_Entity (P))
1317 then
1318 B :=
1319 Make_Package_Body (Sloc (E),
1320 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
1321 Chars => Chars (E)),
1322 Declarations => New_List);
1324 if Nkind (S) = N_Package_Specification then
1325 if Present (Private_Declarations (S)) then
1326 Decls := Private_Declarations (S);
1327 else
1328 Decls := Visible_Declarations (S);
1329 end if;
1330 else
1331 Decls := Declarations (S);
1332 end if;
1334 Append (B, Decls);
1335 Analyze (B);
1336 end if;
1337 end May_Need_Implicit_Body;
1339 ----------------------
1340 -- New_Private_Type --
1341 ----------------------
1343 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
1344 begin
1345 Enter_Name (Id);
1347 if Limited_Present (Def) then
1348 Set_Ekind (Id, E_Limited_Private_Type);
1349 else
1350 Set_Ekind (Id, E_Private_Type);
1351 end if;
1353 Set_Etype (Id, Id);
1354 Set_Has_Delayed_Freeze (Id);
1355 Set_Is_First_Subtype (Id);
1356 Init_Size_Align (Id);
1358 Set_Is_Constrained (Id,
1359 No (Discriminant_Specifications (N))
1360 and then not Unknown_Discriminants_Present (N));
1362 -- Set tagged flag before processing discriminants, to catch
1363 -- illegal usage.
1365 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
1367 Set_Discriminant_Constraint (Id, No_Elist);
1368 Set_Girder_Constraint (Id, No_Elist);
1370 if Present (Discriminant_Specifications (N)) then
1371 New_Scope (Id);
1372 Process_Discriminants (N);
1373 End_Scope;
1375 elsif Unknown_Discriminants_Present (N) then
1376 Set_Has_Unknown_Discriminants (Id);
1377 end if;
1379 Set_Private_Dependents (Id, New_Elmt_List);
1381 if Tagged_Present (Def) then
1382 Set_Ekind (Id, E_Record_Type_With_Private);
1383 Make_Class_Wide_Type (Id);
1384 Set_Primitive_Operations (Id, New_Elmt_List);
1385 Set_Is_Abstract (Id, Abstract_Present (Def));
1386 Set_Is_Limited_Record (Id, Limited_Present (Def));
1387 Set_Has_Delayed_Freeze (Id, True);
1389 elsif Abstract_Present (Def) then
1390 Error_Msg_N ("only a tagged type can be abstract", N);
1391 end if;
1392 end New_Private_Type;
1394 ------------------------------
1395 -- Preserve_Full_Attributes --
1396 ------------------------------
1398 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id) is
1399 Priv_Is_Base_Type : constant Boolean := Priv = Base_Type (Priv);
1401 begin
1402 Set_Size_Info (Priv, (Full));
1403 Set_RM_Size (Priv, RM_Size (Full));
1404 Set_Size_Known_At_Compile_Time (Priv, Size_Known_At_Compile_Time
1405 (Full));
1407 if Priv_Is_Base_Type then
1408 Set_Is_Controlled (Priv, Is_Controlled (Base_Type (Full)));
1409 Set_Finalize_Storage_Only (Priv, Finalize_Storage_Only
1410 (Base_Type (Full)));
1411 Set_Has_Task (Priv, Has_Task (Base_Type (Full)));
1412 Set_Has_Controlled_Component (Priv, Has_Controlled_Component
1413 (Base_Type (Full)));
1414 end if;
1416 Set_Freeze_Node (Priv, Freeze_Node (Full));
1418 if Is_Tagged_Type (Priv)
1419 and then Is_Tagged_Type (Full)
1420 and then not Error_Posted (Full)
1421 then
1422 if Priv_Is_Base_Type then
1423 Set_Access_Disp_Table (Priv, Access_Disp_Table
1424 (Base_Type (Full)));
1425 end if;
1427 Set_First_Entity (Priv, First_Entity (Full));
1428 Set_Last_Entity (Priv, Last_Entity (Full));
1429 end if;
1430 end Preserve_Full_Attributes;
1432 ----------------------------
1433 -- Uninstall_Declarations --
1434 ----------------------------
1436 procedure Uninstall_Declarations (P : Entity_Id) is
1437 Id : Entity_Id;
1438 Decl : Node_Id := Unit_Declaration_Node (P);
1439 Full : Entity_Id;
1440 Priv_Elmt : Elmt_Id;
1441 Priv_Sub : Entity_Id;
1443 function Type_In_Use (T : Entity_Id) return Boolean;
1444 -- Check whether type or base type appear in an active use_type clause.
1446 function Type_In_Use (T : Entity_Id) return Boolean is
1447 begin
1448 return Scope (Base_Type (T)) = P
1449 and then (In_Use (T) or else In_Use (Base_Type (T)));
1450 end Type_In_Use;
1452 -- Start of processing for Uninstall_Declarations
1454 begin
1455 Id := First_Entity (P);
1457 while Present (Id) and then Id /= First_Private_Entity (P) loop
1458 if Debug_Flag_E then
1459 Write_Str ("unlinking visible entity ");
1460 Write_Int (Int (Id));
1461 Write_Eol;
1462 end if;
1464 -- On exit from the package scope, we must preserve the visibility
1465 -- established by use clauses in the current scope. Two cases:
1467 -- a) If the entity is an operator, it may be a primitive operator of
1468 -- a type for which there is a visible use-type clause.
1470 -- b) for other entities, their use-visibility is determined by a
1471 -- visible use clause for the package itself. For a generic instance,
1472 -- the instantiation of the formals appears in the visible part,
1473 -- but the formals are private and remain so.
1475 if Ekind (Id) = E_Function
1476 and then Is_Operator_Symbol_Name (Chars (Id))
1477 and then not Is_Hidden (Id)
1478 then
1479 Set_Is_Potentially_Use_Visible (Id,
1480 In_Use (P)
1481 or else Type_In_Use (Etype (Id))
1482 or else Type_In_Use (Etype (First_Formal (Id)))
1483 or else (Present (Next_Formal (First_Formal (Id)))
1484 and then
1485 Type_In_Use
1486 (Etype (Next_Formal (First_Formal (Id))))));
1487 else
1488 Set_Is_Potentially_Use_Visible (Id,
1489 In_Use (P) and not Is_Hidden (Id));
1490 end if;
1492 -- Local entities are not immediately visible outside of the package.
1494 Set_Is_Immediately_Visible (Id, False);
1496 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
1497 Check_Abstract_Overriding (Id);
1498 end if;
1500 if (Ekind (Id) = E_Private_Type
1501 or else Ekind (Id) = E_Limited_Private_Type)
1502 and then No (Full_View (Id))
1503 and then not Is_Generic_Type (Id)
1504 and then not Is_Derived_Type (Id)
1505 then
1506 Error_Msg_N ("missing full declaration for private type&", Id);
1508 elsif Ekind (Id) = E_Record_Type_With_Private
1509 and then not Is_Generic_Type (Id)
1510 and then No (Full_View (Id))
1511 then
1512 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
1513 Error_Msg_N ("missing full declaration for private type&", Id);
1514 else
1515 Error_Msg_N
1516 ("missing full declaration for private extension", Id);
1517 end if;
1519 elsif Ekind (Id) = E_Constant
1520 and then No (Constant_Value (Id))
1521 and then No (Full_View (Id))
1522 and then not Is_Imported (Id)
1523 and then (Nkind (Parent (Id)) /= N_Object_Declaration
1524 or else not No_Initialization (Parent (Id)))
1525 then
1526 Error_Msg_N ("missing full declaration for deferred constant", Id);
1527 end if;
1529 Next_Entity (Id);
1530 end loop;
1532 -- If the specification was installed as the parent of a public child
1533 -- unit, the private declarations were not installed, and there is
1534 -- nothing to do.
1536 if not In_Private_Part (P) then
1537 return;
1538 else
1539 Set_In_Private_Part (P, False);
1540 end if;
1542 -- Make private entities invisible and exchange full and private
1543 -- declarations for private types.
1545 while Present (Id) loop
1546 if Debug_Flag_E then
1547 Write_Str ("unlinking private entity ");
1548 Write_Int (Int (Id));
1549 Write_Eol;
1550 end if;
1552 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
1553 Check_Abstract_Overriding (Id);
1554 end if;
1556 Set_Is_Immediately_Visible (Id, False);
1558 if Is_Private_Base_Type (Id)
1559 and then Present (Full_View (Id))
1560 then
1561 Full := Full_View (Id);
1563 -- If the partial view is not declared in the visible part
1564 -- of the package (as is the case when it is a type derived
1565 -- from some other private type in the private part if the
1566 -- current package), no exchange takes place.
1568 if No (Parent (Id))
1569 or else List_Containing (Parent (Id))
1570 /= Visible_Declarations (Specification (Decl))
1571 then
1572 goto Next_Id;
1573 end if;
1575 -- The entry in the private part points to the full declaration,
1576 -- which is currently visible. Exchange them so only the private
1577 -- type declaration remains accessible, and link private and
1578 -- full declaration in the opposite direction. Before the actual
1579 -- exchange, we copy back attributes of the full view that
1580 -- must be available to the partial view too.
1582 Preserve_Full_Attributes (Id, Full);
1584 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
1586 if Is_Indefinite_Subtype (Full)
1587 and then not Is_Indefinite_Subtype (Id)
1588 then
1589 Error_Msg_N
1590 ("full view of type must be definite subtype", Full);
1591 end if;
1593 Priv_Elmt := First_Elmt (Private_Dependents (Id));
1594 Exchange_Declarations (Id);
1596 -- Swap out the subtypes and derived types of Id that were
1597 -- compiled in this scope, or installed previously by
1598 -- Install_Private_Declarations.
1599 -- Before we do the swap, we verify the presence of the
1600 -- Full_View field which may be empty due to a swap by
1601 -- a previous call to End_Package_Scope (e.g. from the
1602 -- freezing mechanism).
1604 while Present (Priv_Elmt) loop
1605 Priv_Sub := Node (Priv_Elmt);
1607 if Present (Full_View (Priv_Sub)) then
1609 if Scope (Priv_Sub) = P
1610 or else not In_Open_Scopes (Scope (Priv_Sub))
1611 then
1612 Set_Is_Immediately_Visible (Priv_Sub, False);
1613 end if;
1615 if Is_Visible_Dependent (Priv_Sub) then
1616 Preserve_Full_Attributes
1617 (Priv_Sub, Full_View (Priv_Sub));
1618 Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub));
1619 Exchange_Declarations (Priv_Sub);
1620 end if;
1621 end if;
1623 Next_Elmt (Priv_Elmt);
1624 end loop;
1626 elsif Ekind (Id) = E_Incomplete_Type
1627 and then No (Full_View (Id))
1628 then
1629 -- Mark Taft amendment types
1631 Set_Has_Completion_In_Body (Id);
1633 elsif not Is_Child_Unit (Id)
1634 and then (not Is_Private_Type (Id)
1635 or else No (Full_View (Id)))
1636 then
1637 Set_Is_Hidden (Id);
1638 Set_Is_Potentially_Use_Visible (Id, False);
1639 end if;
1641 <<Next_Id>>
1642 Next_Entity (Id);
1643 end loop;
1645 end Uninstall_Declarations;
1647 ------------------------
1648 -- Unit_Requires_Body --
1649 ------------------------
1651 function Unit_Requires_Body (P : Entity_Id) return Boolean is
1652 E : Entity_Id;
1654 begin
1655 -- Imported entity never requires body. Right now, only
1656 -- subprograms can be imported, but perhaps in the future
1657 -- we will allow import of packages.
1659 if Is_Imported (P) then
1660 return False;
1662 -- Body required if library package with pragma Elaborate_Body
1664 elsif Has_Pragma_Elaborate_Body (P) then
1665 return True;
1667 -- Body required if subprogram
1669 elsif (Is_Subprogram (P)
1670 or else
1671 Ekind (P) = E_Generic_Function
1672 or else
1673 Ekind (P) = E_Generic_Procedure)
1674 then
1675 return True;
1677 -- Treat a block as requiring a body
1679 elsif Ekind (P) = E_Block then
1680 return True;
1682 elsif Ekind (P) = E_Package
1683 and then Nkind (Parent (P)) = N_Package_Specification
1684 and then Present (Generic_Parent (Parent (P)))
1685 then
1686 declare
1687 G_P : Entity_Id := Generic_Parent (Parent (P));
1689 begin
1690 if Has_Pragma_Elaborate_Body (G_P) then
1691 return True;
1692 end if;
1693 end;
1694 end if;
1696 -- Otherwise search entity chain for entity requiring completion.
1698 E := First_Entity (P);
1699 while Present (E) loop
1701 -- Always ignore child units. Child units get added to the entity
1702 -- list of a parent unit, but are not original entities of the
1703 -- parent, and so do not affect whether the parent needs a body.
1705 if Is_Child_Unit (E) then
1706 null;
1708 -- Otherwise test to see if entity requires a completion
1710 elsif (Is_Overloadable (E)
1711 and then Ekind (E) /= E_Enumeration_Literal
1712 and then Ekind (E) /= E_Operator
1713 and then not Is_Abstract (E)
1714 and then not Has_Completion (E))
1716 or else
1717 (Ekind (E) = E_Package
1718 and then E /= P
1719 and then not Has_Completion (E)
1720 and then Unit_Requires_Body (E))
1722 or else
1723 (Ekind (E) = E_Incomplete_Type and then No (Full_View (E)))
1725 or else
1726 ((Ekind (E) = E_Task_Type or else
1727 Ekind (E) = E_Protected_Type)
1728 and then not Has_Completion (E))
1730 or else
1731 (Ekind (E) = E_Generic_Package and then E /= P
1732 and then not Has_Completion (E)
1733 and then Unit_Requires_Body (E))
1735 or else
1736 (Ekind (E) = E_Generic_Function
1737 and then not Has_Completion (E))
1739 or else
1740 (Ekind (E) = E_Generic_Procedure
1741 and then not Has_Completion (E))
1743 then
1744 return True;
1746 -- Entity that does not require completion
1748 else
1749 null;
1750 end if;
1752 Next_Entity (E);
1753 end loop;
1755 return False;
1756 end Unit_Requires_Body;
1758 end Sem_Ch7;