* gimplify.c (find_single_pointer_decl_1): New static function.
[official-gcc.git] / gcc / ada / sem_ch7.adb
blob178cfd3dd60155004ea7aa360ac3dd43e758e69a
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 (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 (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 ---------------------
717 -- Clear_Constants --
718 ---------------------
720 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
721 E : Entity_Id;
723 begin
724 -- Ignore package renamings, not interesting and they can
725 -- cause self referential loops in the code below.
727 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
728 return;
729 end if;
731 -- Note: in the loop below, the check for Next_Entity pointing
732 -- back to the package entity seems very odd, but it is needed,
733 -- because this kind of unexpected circularity does occur ???
735 E := FE;
736 while Present (E) and then E /= Id loop
737 if Ekind (E) = E_Variable then
738 Set_Never_Set_In_Source (E, False);
739 Set_Is_True_Constant (E, False);
740 Set_Current_Value (E, Empty);
741 Set_Is_Known_Non_Null (E, False);
743 elsif Ekind (E) = E_Package
744 or else
745 Ekind (E) = E_Generic_Package
746 then
747 Clear_Constants (E, First_Entity (E));
748 Clear_Constants (E, First_Private_Entity (E));
749 end if;
751 Next_Entity (E);
752 end loop;
753 end Clear_Constants;
755 --------------------------------
756 -- Generate_Parent_References --
757 --------------------------------
759 procedure Generate_Parent_References is
760 Decl : constant Node_Id := Parent (N);
762 begin
763 if Id = Cunit_Entity (Main_Unit)
764 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
765 then
766 Generate_Reference (Id, Scope (Id), 'k', False);
768 elsif Nkind (Unit (Cunit (Main_Unit))) /= N_Subprogram_Body
769 and then Nkind (Unit (Cunit (Main_Unit))) /= N_Subunit
770 then
771 -- If current unit is an ancestor of main unit, generate
772 -- a reference to its own parent.
774 declare
775 U : Node_Id;
776 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
778 begin
779 if Nkind (Main_Spec) = N_Package_Body then
780 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
781 end if;
783 U := Parent_Spec (Main_Spec);
784 while Present (U) loop
785 if U = Parent (Decl) then
786 Generate_Reference (Id, Scope (Id), 'k', False);
787 exit;
789 elsif Nkind (Unit (U)) = N_Package_Body then
790 exit;
792 else
793 U := Parent_Spec (Unit (U));
794 end if;
795 end loop;
796 end;
797 end if;
798 end Generate_Parent_References;
800 ---------------------
801 -- Is_Public_Child --
802 ---------------------
804 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
805 begin
806 if not Is_Private_Descendant (Child) then
807 return True;
808 else
809 if Child = Unit then
810 return not Private_Present (
811 Parent (Unit_Declaration_Node (Child)));
812 else
813 return Is_Public_Child (Scope (Child), Unit);
814 end if;
815 end if;
816 end Is_Public_Child;
818 ------------------------------------------
819 -- Inspect_Deferred_Constant_Completion --
820 ------------------------------------------
822 procedure Inspect_Deferred_Constant_Completion is
823 Decl : Node_Id;
825 begin
826 Decl := First (Priv_Decls);
827 while Present (Decl) loop
829 -- Deferred constant signature
831 if Nkind (Decl) = N_Object_Declaration
832 and then Constant_Present (Decl)
833 and then No (Expression (Decl))
835 -- No need to check internally generated constants
837 and then Comes_From_Source (Decl)
839 -- The constant is not completed. A full object declaration
840 -- or a pragma Import complete a deferred constant.
842 and then not Has_Completion (Defining_Identifier (Decl))
843 then
844 Error_Msg_N
845 ("constant declaration requires initialization expression",
846 Defining_Identifier (Decl));
847 end if;
849 Decl := Next (Decl);
850 end loop;
851 end Inspect_Deferred_Constant_Completion;
853 ----------------------------------------
854 -- Inspect_Unchecked_Union_Completion --
855 ----------------------------------------
857 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
858 Decl : Node_Id := First (Decls);
860 begin
861 while Present (Decl) loop
863 -- We are looking at an incomplete or private type declaration
864 -- with a known_discriminant_part whose full view is an
865 -- Unchecked_Union.
867 if (Nkind (Decl) = N_Incomplete_Type_Declaration
868 or else
869 Nkind (Decl) = N_Private_Type_Declaration)
870 and then Has_Discriminants (Defining_Identifier (Decl))
871 and then Present (Full_View (Defining_Identifier (Decl)))
872 and then Is_Unchecked_Union
873 (Full_View (Defining_Identifier (Decl)))
874 then
875 Error_Msg_N ("completion of discriminated partial view" &
876 " cannot be an Unchecked_Union",
877 Full_View (Defining_Identifier (Decl)));
878 end if;
880 Next (Decl);
881 end loop;
882 end Inspect_Unchecked_Union_Completion;
884 -- Start of processing for Analyze_Package_Specification
886 begin
887 if Present (Vis_Decls) then
888 Analyze_Declarations (Vis_Decls);
889 end if;
891 -- Verify that incomplete types have received full declarations
893 E := First_Entity (Id);
894 while Present (E) loop
895 if Ekind (E) = E_Incomplete_Type
896 and then No (Full_View (E))
897 then
898 Error_Msg_N ("no declaration in visible part for incomplete}", E);
899 end if;
901 Next_Entity (E);
902 end loop;
904 if Is_Remote_Call_Interface (Id)
905 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
906 then
907 Validate_RCI_Declarations (Id);
908 end if;
910 -- Save global references in the visible declarations, before
911 -- installing private declarations of parent unit if there is one,
912 -- because the privacy status of types defined in the parent will
913 -- change. This is only relevant for generic child units, but is
914 -- done in all cases for uniformity.
916 if Ekind (Id) = E_Generic_Package
917 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
918 then
919 declare
920 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
921 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
923 begin
924 Set_Private_Declarations (Orig_Spec, Empty_List);
925 Save_Global_References (Orig_Decl);
926 Set_Private_Declarations (Orig_Spec, Save_Priv);
927 end;
928 end if;
930 -- If package is a public child unit, then make the private
931 -- declarations of the parent visible.
933 Public_Child := False;
935 declare
936 Par : Entity_Id;
937 Pack_Decl : Node_Id;
938 Par_Spec : Node_Id;
940 begin
941 Par := Id;
942 Par_Spec := Parent_Spec (Parent (N));
944 -- If the package is formal package of an enclosing generic, is is
945 -- transformed into a local generic declaration, and compiled to make
946 -- its spec available. We need to retrieve the original generic to
947 -- determine whether it is a child unit, and install its parents.
949 if No (Par_Spec)
950 and then
951 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
952 then
953 Par := Entity (Name (Original_Node (Parent (N))));
954 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
955 end if;
957 if Present (Par_Spec) then
958 Generate_Parent_References;
960 while Scope (Par) /= Standard_Standard
961 and then Is_Public_Child (Id, Par)
962 loop
963 Public_Child := True;
964 Par := Scope (Par);
965 Install_Private_Declarations (Par);
966 Install_Private_With_Clauses (Par);
967 Pack_Decl := Unit_Declaration_Node (Par);
968 Set_Use (Private_Declarations (Specification (Pack_Decl)));
969 end loop;
970 end if;
971 end;
973 if Is_Compilation_Unit (Id) then
974 Install_Private_With_Clauses (Id);
975 end if;
977 -- Analyze private part if present. The flag In_Private_Part is
978 -- reset in End_Package_Scope.
980 L := Last_Entity (Id);
982 if Present (Priv_Decls) then
983 Set_In_Private_Part (Id);
985 -- Upon entering a public child's private part, it may be
986 -- necessary to declare subprograms that were derived in
987 -- the package visible part but not yet made visible.
989 if Public_Child then
990 Declare_Inherited_Private_Subprograms (Id);
991 end if;
993 Analyze_Declarations (Priv_Decls);
995 -- Check the private declarations for incomplete deferred constants
997 Inspect_Deferred_Constant_Completion;
999 -- The first private entity is the immediate follower of the last
1000 -- visible entity, if there was one.
1002 if Present (L) then
1003 Set_First_Private_Entity (Id, Next_Entity (L));
1004 else
1005 Set_First_Private_Entity (Id, First_Entity (Id));
1006 end if;
1008 -- There may be inherited private subprograms that need to be
1009 -- declared, even in the absence of an explicit private part.
1010 -- If there are any public declarations in the package and
1011 -- the package is a public child unit, then an implicit private
1012 -- part is assumed.
1014 elsif Present (L) and then Public_Child then
1015 Set_In_Private_Part (Id);
1016 Declare_Inherited_Private_Subprograms (Id);
1017 Set_First_Private_Entity (Id, Next_Entity (L));
1018 end if;
1020 -- Check rule of 3.6(11), which in general requires
1021 -- waiting till all full types have been seen.
1023 E := First_Entity (Id);
1024 while Present (E) loop
1025 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1026 Check_Aliased_Component_Types (E);
1027 end if;
1029 Next_Entity (E);
1030 end loop;
1032 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1033 -- declaration having a known_discriminant_part shall not be an
1034 -- Unchecked_Union type.
1036 if Present (Vis_Decls) then
1037 Inspect_Unchecked_Union_Completion (Vis_Decls);
1038 end if;
1040 if Present (Priv_Decls) then
1041 Inspect_Unchecked_Union_Completion (Priv_Decls);
1042 end if;
1044 if Ekind (Id) = E_Generic_Package
1045 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1046 and then Present (Priv_Decls)
1047 then
1048 -- Save global references in private declarations, ignoring the
1049 -- visible declarations that were processed earlier.
1051 declare
1052 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1053 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1054 Save_Form : constant List_Id :=
1055 Generic_Formal_Declarations (Orig_Decl);
1057 begin
1058 Set_Visible_Declarations (Orig_Spec, Empty_List);
1059 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1060 Save_Global_References (Orig_Decl);
1061 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1062 Set_Visible_Declarations (Orig_Spec, Save_Vis);
1063 end;
1064 end if;
1066 Process_End_Label (N, 'e', Id);
1068 -- For the case of a library level package, we must go through all
1069 -- the entities clearing the indications that the value may be
1070 -- constant and not modified. Why? Because any client of this
1071 -- package may modify these values freely from anywhere. This
1072 -- also applies to any nested packages or generic packages.
1074 -- For now we unconditionally clear constants for packages that
1075 -- are instances of generic packages. The reason is that we do not
1076 -- have the body yet, and we otherwise think things are unreferenced
1077 -- when they are not. This should be fixed sometime (the effect is
1078 -- not terrible, we just lose some warnings, and also some cases
1079 -- of value propagation) ???
1081 if Is_Library_Level_Entity (Id)
1082 or else Is_Generic_Instance (Id)
1083 then
1084 Clear_Constants (Id, First_Entity (Id));
1085 Clear_Constants (Id, First_Private_Entity (Id));
1086 end if;
1087 end Analyze_Package_Specification;
1089 --------------------------------------
1090 -- Analyze_Private_Type_Declaration --
1091 --------------------------------------
1093 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1094 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
1095 Id : constant Entity_Id := Defining_Identifier (N);
1097 begin
1098 Generate_Definition (Id);
1099 Set_Is_Pure (Id, PF);
1100 Init_Size_Align (Id);
1102 if (Ekind (Current_Scope) /= E_Package
1103 and then Ekind (Current_Scope) /= E_Generic_Package)
1104 or else In_Private_Part (Current_Scope)
1105 then
1106 Error_Msg_N ("invalid context for private declaration", N);
1107 end if;
1109 New_Private_Type (N, Id, N);
1110 Set_Depends_On_Private (Id);
1111 end Analyze_Private_Type_Declaration;
1113 -------------------------------------------
1114 -- Declare_Inherited_Private_Subprograms --
1115 -------------------------------------------
1117 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1118 E : Entity_Id;
1119 Op_List : Elist_Id;
1120 Op_Elmt : Elmt_Id;
1121 Op_Elmt_2 : Elmt_Id;
1122 Prim_Op : Entity_Id;
1123 New_Op : Entity_Id := Empty;
1124 Parent_Subp : Entity_Id;
1125 Found_Explicit : Boolean;
1126 Decl_Privates : Boolean;
1128 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1129 -- Check whether an inherited subprogram is an operation of an
1130 -- untagged derived type.
1132 ---------------------
1133 -- Is_Primitive_Of --
1134 ---------------------
1136 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1137 Formal : Entity_Id;
1139 begin
1140 if Etype (S) = T then
1141 return True;
1143 else
1144 Formal := First_Formal (S);
1146 while Present (Formal) loop
1147 if Etype (Formal) = T then
1148 return True;
1149 end if;
1151 Next_Formal (Formal);
1152 end loop;
1154 return False;
1155 end if;
1156 end Is_Primitive_Of;
1158 -- Start of processing for Declare_Inherited_Private_Subprograms
1160 begin
1161 E := First_Entity (Id);
1162 while Present (E) loop
1164 -- If the entity is a nonprivate type extension whose parent
1165 -- type is declared in an open scope, then the type may have
1166 -- inherited operations that now need to be made visible.
1167 -- Ditto if the entity is a formal derived type in a child unit.
1169 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
1170 or else
1171 (Nkind (Parent (E)) = N_Private_Extension_Declaration
1172 and then Is_Generic_Type (E)))
1173 and then In_Open_Scopes (Scope (Etype (E)))
1174 and then E = Base_Type (E)
1175 then
1176 if Is_Tagged_Type (E) then
1177 Op_List := Primitive_Operations (E);
1178 New_Op := Empty;
1179 Decl_Privates := False;
1181 Op_Elmt := First_Elmt (Op_List);
1182 while Present (Op_Elmt) loop
1183 Prim_Op := Node (Op_Elmt);
1185 -- If the primitive operation is an implicit operation
1186 -- with an internal name whose parent operation has
1187 -- a normal name, then we now need to either declare the
1188 -- operation (i.e., make it visible), or replace it
1189 -- by an overriding operation if one exists.
1191 if Present (Alias (Prim_Op))
1192 and then not Comes_From_Source (Prim_Op)
1193 and then Is_Internal_Name (Chars (Prim_Op))
1194 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1195 then
1196 Parent_Subp := Alias (Prim_Op);
1198 Found_Explicit := False;
1199 Op_Elmt_2 := Next_Elmt (Op_Elmt);
1200 while Present (Op_Elmt_2) loop
1201 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
1202 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
1203 then
1204 -- The private inherited operation has been
1205 -- overridden by an explicit subprogram, so
1206 -- change the private op's list element to
1207 -- designate the explicit so the explicit
1208 -- one will get the right dispatching slot.
1210 New_Op := Node (Op_Elmt_2);
1211 Replace_Elmt (Op_Elmt, New_Op);
1212 Remove_Elmt (Op_List, Op_Elmt_2);
1213 Found_Explicit := True;
1214 Set_Is_Overriding_Operation (New_Op);
1215 Decl_Privates := True;
1217 exit;
1218 end if;
1220 Next_Elmt (Op_Elmt_2);
1221 end loop;
1223 if not Found_Explicit then
1224 Derive_Subprogram
1225 (New_Op, Alias (Prim_Op), E, Etype (E));
1227 pragma Assert
1228 (Is_Dispatching_Operation (New_Op)
1229 and then Node (Last_Elmt (Op_List)) = New_Op);
1231 -- Substitute the new operation for the old one
1232 -- in the type's primitive operations list. Since
1233 -- the new operation was also just added to the end
1234 -- of list, the last element must be removed.
1236 -- (Question: is there a simpler way of declaring
1237 -- the operation, say by just replacing the name
1238 -- of the earlier operation, reentering it in the
1239 -- in the symbol table (how?), and marking it as
1240 -- private???)
1242 Replace_Elmt (Op_Elmt, New_Op);
1243 Remove_Last_Elmt (Op_List);
1244 Decl_Privates := True;
1245 end if;
1246 end if;
1248 Next_Elmt (Op_Elmt);
1249 end loop;
1251 -- The type's DT attributes need to be recalculated
1252 -- in the case where private dispatching operations
1253 -- have been added or overridden. Normally this action
1254 -- occurs during type freezing, but we force it here
1255 -- since the type may already have been frozen (e.g.,
1256 -- if the type's package has an empty private part).
1257 -- This can only be done if expansion is active, otherwise
1258 -- Tag may not be present.
1260 if Decl_Privates
1261 and then Expander_Active
1262 then
1263 Set_All_DT_Position (E);
1264 end if;
1266 else
1267 -- Non-tagged type, scan forward to locate
1268 -- inherited hidden operations.
1270 Prim_Op := Next_Entity (E);
1272 while Present (Prim_Op) loop
1273 if Is_Subprogram (Prim_Op)
1274 and then Present (Alias (Prim_Op))
1275 and then not Comes_From_Source (Prim_Op)
1276 and then Is_Internal_Name (Chars (Prim_Op))
1277 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1278 and then Is_Primitive_Of (E, Prim_Op)
1279 then
1280 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1281 end if;
1283 Next_Entity (Prim_Op);
1284 end loop;
1285 end if;
1286 end if;
1288 Next_Entity (E);
1289 end loop;
1290 end Declare_Inherited_Private_Subprograms;
1292 -----------------------
1293 -- End_Package_Scope --
1294 -----------------------
1296 procedure End_Package_Scope (P : Entity_Id) is
1297 begin
1298 Uninstall_Declarations (P);
1299 Pop_Scope;
1300 end End_Package_Scope;
1302 ---------------------------
1303 -- Exchange_Declarations --
1304 ---------------------------
1306 procedure Exchange_Declarations (Id : Entity_Id) is
1307 Full_Id : constant Entity_Id := Full_View (Id);
1308 H1 : constant Entity_Id := Homonym (Id);
1309 Next1 : constant Entity_Id := Next_Entity (Id);
1310 H2 : Entity_Id;
1311 Next2 : Entity_Id;
1313 begin
1314 -- If missing full declaration for type, nothing to exchange
1316 if No (Full_Id) then
1317 return;
1318 end if;
1320 -- Otherwise complete the exchange, and preserve semantic links
1322 Next2 := Next_Entity (Full_Id);
1323 H2 := Homonym (Full_Id);
1325 -- Reset full declaration pointer to reflect the switched entities
1326 -- and readjust the next entity chains.
1328 Exchange_Entities (Id, Full_Id);
1330 Set_Next_Entity (Id, Next1);
1331 Set_Homonym (Id, H1);
1333 Set_Full_View (Full_Id, Id);
1334 Set_Next_Entity (Full_Id, Next2);
1335 Set_Homonym (Full_Id, H2);
1336 end Exchange_Declarations;
1338 ----------------------------
1339 -- Install_Package_Entity --
1340 ----------------------------
1342 procedure Install_Package_Entity (Id : Entity_Id) is
1343 begin
1344 if not Is_Internal (Id) then
1345 if Debug_Flag_E then
1346 Write_Str ("Install: ");
1347 Write_Name (Chars (Id));
1348 Write_Eol;
1349 end if;
1351 if not Is_Child_Unit (Id) then
1352 Set_Is_Immediately_Visible (Id);
1353 end if;
1355 end if;
1356 end Install_Package_Entity;
1358 ----------------------------------
1359 -- Install_Private_Declarations --
1360 ----------------------------------
1362 procedure Install_Private_Declarations (P : Entity_Id) is
1363 Id : Entity_Id;
1364 Priv_Elmt : Elmt_Id;
1365 Priv : Entity_Id;
1366 Full : Entity_Id;
1368 begin
1369 -- First exchange declarations for private types, so that the
1370 -- full declaration is visible. For each private type, we check
1371 -- its Private_Dependents list and also exchange any subtypes of
1372 -- or derived types from it. Finally, if this is a Taft amendment
1373 -- type, the incomplete declaration is irrelevant, and we want to
1374 -- link the eventual full declaration with the original private
1375 -- one so we also skip the exchange.
1377 Id := First_Entity (P);
1378 while Present (Id) and then Id /= First_Private_Entity (P) loop
1380 if Is_Private_Base_Type (Id)
1381 and then Comes_From_Source (Full_View (Id))
1382 and then Present (Full_View (Id))
1383 and then Scope (Full_View (Id)) = Scope (Id)
1384 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
1385 then
1386 -- If there is a use-type clause on the private type, set the
1387 -- full view accordingly.
1389 Set_In_Use (Full_View (Id), In_Use (Id));
1390 Full := Full_View (Id);
1392 if Is_Private_Base_Type (Full)
1393 and then Has_Private_Declaration (Full)
1394 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
1395 and then In_Open_Scopes (Scope (Etype (Full)))
1396 and then In_Package_Body (Current_Scope)
1397 and then not Is_Private_Type (Etype (Full))
1398 then
1399 -- This is the completion of a private type by a derivation
1400 -- from another private type which is not private anymore. This
1401 -- can only happen in a package nested within a child package,
1402 -- when the parent type is defined in the parent unit. At this
1403 -- point the current type is not private either, and we have to
1404 -- install the underlying full view, which is now visible.
1406 if No (Full_View (Full))
1407 and then Present (Underlying_Full_View (Full))
1408 then
1409 Set_Full_View (Id, Underlying_Full_View (Full));
1410 Set_Underlying_Full_View (Full, Empty);
1411 Set_Is_Frozen (Full_View (Id));
1412 end if;
1413 end if;
1415 Priv_Elmt := First_Elmt (Private_Dependents (Id));
1417 Exchange_Declarations (Id);
1418 Set_Is_Immediately_Visible (Id);
1420 while Present (Priv_Elmt) loop
1421 Priv := Node (Priv_Elmt);
1423 -- Before the exchange, verify that the presence of the
1424 -- Full_View field. It will be empty if the entity
1425 -- has already been installed due to a previous call.
1427 if Present (Full_View (Priv))
1428 and then Is_Visible_Dependent (Priv)
1429 then
1431 -- For each subtype that is swapped, we also swap the
1432 -- reference to it in Private_Dependents, to allow access
1433 -- to it when we swap them out in End_Package_Scope.
1435 Replace_Elmt (Priv_Elmt, Full_View (Priv));
1436 Exchange_Declarations (Priv);
1437 Set_Is_Immediately_Visible
1438 (Priv, In_Open_Scopes (Scope (Priv)));
1439 Set_Is_Potentially_Use_Visible
1440 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
1441 end if;
1443 Next_Elmt (Priv_Elmt);
1444 end loop;
1445 end if;
1447 Next_Entity (Id);
1448 end loop;
1450 -- Next make other declarations in the private part visible as well
1452 Id := First_Private_Entity (P);
1454 while Present (Id) loop
1455 Install_Package_Entity (Id);
1456 Set_Is_Hidden (Id, False);
1457 Next_Entity (Id);
1458 end loop;
1460 -- Indicate that the private part is currently visible, so it can be
1461 -- properly reset on exit.
1463 Set_In_Private_Part (P);
1464 end Install_Private_Declarations;
1466 ----------------------------------
1467 -- Install_Visible_Declarations --
1468 ----------------------------------
1470 procedure Install_Visible_Declarations (P : Entity_Id) is
1471 Id : Entity_Id;
1472 Last_Entity : Entity_Id;
1474 begin
1475 pragma Assert (Is_Package (P) or else Is_Record_Type (P));
1477 if Is_Package (P) then
1478 Last_Entity := First_Private_Entity (P);
1479 else
1480 Last_Entity := Empty;
1481 end if;
1483 Id := First_Entity (P);
1485 while Present (Id) and then Id /= Last_Entity loop
1486 Install_Package_Entity (Id);
1487 Next_Entity (Id);
1488 end loop;
1489 end Install_Visible_Declarations;
1491 --------------------------
1492 -- Is_Private_Base_Type --
1493 --------------------------
1495 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
1496 begin
1497 return Ekind (E) = E_Private_Type
1498 or else Ekind (E) = E_Limited_Private_Type
1499 or else Ekind (E) = E_Record_Type_With_Private;
1500 end Is_Private_Base_Type;
1502 --------------------------
1503 -- Is_Visible_Dependent --
1504 --------------------------
1506 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
1508 S : constant Entity_Id := Scope (Dep);
1510 begin
1511 -- Renamings created for actual types have the visibility of the
1512 -- actual.
1514 if Ekind (S) = E_Package
1515 and then Is_Generic_Instance (S)
1516 and then (Is_Generic_Actual_Type (Dep)
1517 or else Is_Generic_Actual_Type (Full_View (Dep)))
1518 then
1519 return True;
1521 elsif not (Is_Derived_Type (Dep))
1522 and then Is_Derived_Type (Full_View (Dep))
1523 then
1524 -- When instantiating a package body, the scope stack is empty,
1525 -- so check instead whether the dependent type is defined in
1526 -- the same scope as the instance itself.
1528 return In_Open_Scopes (S)
1529 or else (Is_Generic_Instance (Current_Scope)
1530 and then Scope (Dep) = Scope (Current_Scope));
1531 else
1532 return True;
1533 end if;
1534 end Is_Visible_Dependent;
1536 ----------------------------
1537 -- May_Need_Implicit_Body --
1538 ----------------------------
1540 procedure May_Need_Implicit_Body (E : Entity_Id) is
1541 P : constant Node_Id := Unit_Declaration_Node (E);
1542 S : constant Node_Id := Parent (P);
1543 B : Node_Id;
1544 Decls : List_Id;
1546 begin
1547 if not Has_Completion (E)
1548 and then Nkind (P) = N_Package_Declaration
1549 and then Present (Activation_Chain_Entity (P))
1550 then
1551 B :=
1552 Make_Package_Body (Sloc (E),
1553 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
1554 Chars => Chars (E)),
1555 Declarations => New_List);
1557 if Nkind (S) = N_Package_Specification then
1558 if Present (Private_Declarations (S)) then
1559 Decls := Private_Declarations (S);
1560 else
1561 Decls := Visible_Declarations (S);
1562 end if;
1563 else
1564 Decls := Declarations (S);
1565 end if;
1567 Append (B, Decls);
1568 Analyze (B);
1569 end if;
1570 end May_Need_Implicit_Body;
1572 ----------------------
1573 -- New_Private_Type --
1574 ----------------------
1576 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
1577 begin
1578 Enter_Name (Id);
1580 if Limited_Present (Def) then
1581 Set_Ekind (Id, E_Limited_Private_Type);
1582 else
1583 Set_Ekind (Id, E_Private_Type);
1584 end if;
1586 Set_Etype (Id, Id);
1587 Set_Has_Delayed_Freeze (Id);
1588 Set_Is_First_Subtype (Id);
1589 Init_Size_Align (Id);
1591 Set_Is_Constrained (Id,
1592 No (Discriminant_Specifications (N))
1593 and then not Unknown_Discriminants_Present (N));
1595 -- Set tagged flag before processing discriminants, to catch
1596 -- illegal usage.
1598 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
1600 Set_Discriminant_Constraint (Id, No_Elist);
1601 Set_Stored_Constraint (Id, No_Elist);
1603 if Present (Discriminant_Specifications (N)) then
1604 New_Scope (Id);
1605 Process_Discriminants (N);
1606 End_Scope;
1608 elsif Unknown_Discriminants_Present (N) then
1609 Set_Has_Unknown_Discriminants (Id);
1610 end if;
1612 Set_Private_Dependents (Id, New_Elmt_List);
1614 if Tagged_Present (Def) then
1615 Set_Ekind (Id, E_Record_Type_With_Private);
1616 Make_Class_Wide_Type (Id);
1617 Set_Primitive_Operations (Id, New_Elmt_List);
1618 Set_Is_Abstract (Id, Abstract_Present (Def));
1619 Set_Is_Limited_Record (Id, Limited_Present (Def));
1620 Set_Has_Delayed_Freeze (Id, True);
1622 elsif Abstract_Present (Def) then
1623 Error_Msg_N ("only a tagged type can be abstract", N);
1624 end if;
1625 end New_Private_Type;
1627 ----------------------------
1628 -- Uninstall_Declarations --
1629 ----------------------------
1631 procedure Uninstall_Declarations (P : Entity_Id) is
1632 Decl : constant Node_Id := Unit_Declaration_Node (P);
1633 Id : Entity_Id;
1634 Full : Entity_Id;
1635 Priv_Elmt : Elmt_Id;
1636 Priv_Sub : Entity_Id;
1638 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id);
1639 -- Copy to the private declaration the attributes of the full view
1640 -- that need to be available for the partial view also.
1642 function Type_In_Use (T : Entity_Id) return Boolean;
1643 -- Check whether type or base type appear in an active use_type clause
1645 ------------------------------
1646 -- Preserve_Full_Attributes --
1647 ------------------------------
1649 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id) is
1650 Priv_Is_Base_Type : constant Boolean := Priv = Base_Type (Priv);
1652 begin
1653 Set_Size_Info (Priv, (Full));
1654 Set_RM_Size (Priv, RM_Size (Full));
1655 Set_Size_Known_At_Compile_Time (Priv, Size_Known_At_Compile_Time
1656 (Full));
1657 Set_Is_Volatile (Priv, Is_Volatile (Full));
1658 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
1659 Set_Is_Ada_2005 (Priv, Is_Ada_2005 (Full));
1661 if Is_Unchecked_Union (Full) then
1662 Set_Is_Unchecked_Union (Base_Type (Priv));
1663 end if;
1664 -- Why is atomic not copied here ???
1666 if Referenced (Full) then
1667 Set_Referenced (Priv);
1668 end if;
1670 if Priv_Is_Base_Type then
1671 Set_Is_Controlled (Priv, Is_Controlled (Base_Type (Full)));
1672 Set_Finalize_Storage_Only (Priv, Finalize_Storage_Only
1673 (Base_Type (Full)));
1674 Set_Has_Task (Priv, Has_Task (Base_Type (Full)));
1675 Set_Has_Controlled_Component (Priv, Has_Controlled_Component
1676 (Base_Type (Full)));
1677 end if;
1679 Set_Freeze_Node (Priv, Freeze_Node (Full));
1681 if Is_Tagged_Type (Priv)
1682 and then Is_Tagged_Type (Full)
1683 and then not Error_Posted (Full)
1684 then
1685 if Priv_Is_Base_Type then
1687 -- Ada 2005 (AI-345): The full view of a type implementing
1688 -- an interface can be a task type.
1690 -- type T is new I with private;
1691 -- private
1692 -- task type T is new I with ...
1694 if Is_Interface (Etype (Priv))
1695 and then Is_Concurrent_Type (Base_Type (Full))
1696 then
1697 -- Protect the frontend against previous errors
1699 if Present (Corresponding_Record_Type
1700 (Base_Type (Full)))
1701 then
1702 Set_Access_Disp_Table
1703 (Priv, Access_Disp_Table
1704 (Corresponding_Record_Type (Base_Type (Full))));
1705 else
1706 pragma Assert (Serious_Errors_Detected > 0);
1707 null;
1708 end if;
1710 else
1711 Set_Access_Disp_Table
1712 (Priv, Access_Disp_Table (Base_Type (Full)));
1713 end if;
1714 end if;
1716 Set_First_Entity (Priv, First_Entity (Full));
1717 Set_Last_Entity (Priv, Last_Entity (Full));
1718 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
1719 end if;
1720 end Preserve_Full_Attributes;
1722 -----------------
1723 -- Type_In_Use --
1724 -----------------
1726 function Type_In_Use (T : Entity_Id) return Boolean is
1727 begin
1728 return Scope (Base_Type (T)) = P
1729 and then (In_Use (T) or else In_Use (Base_Type (T)));
1730 end Type_In_Use;
1732 -- Start of processing for Uninstall_Declarations
1734 begin
1735 Id := First_Entity (P);
1737 while Present (Id) and then Id /= First_Private_Entity (P) loop
1738 if Debug_Flag_E then
1739 Write_Str ("unlinking visible entity ");
1740 Write_Int (Int (Id));
1741 Write_Eol;
1742 end if;
1744 -- On exit from the package scope, we must preserve the visibility
1745 -- established by use clauses in the current scope. Two cases:
1747 -- a) If the entity is an operator, it may be a primitive operator of
1748 -- a type for which there is a visible use-type clause.
1750 -- b) for other entities, their use-visibility is determined by a
1751 -- visible use clause for the package itself. For a generic instance,
1752 -- the instantiation of the formals appears in the visible part,
1753 -- but the formals are private and remain so.
1755 if Ekind (Id) = E_Function
1756 and then Is_Operator_Symbol_Name (Chars (Id))
1757 and then not Is_Hidden (Id)
1758 and then not Error_Posted (Id)
1759 then
1760 Set_Is_Potentially_Use_Visible (Id,
1761 In_Use (P)
1762 or else Type_In_Use (Etype (Id))
1763 or else Type_In_Use (Etype (First_Formal (Id)))
1764 or else (Present (Next_Formal (First_Formal (Id)))
1765 and then
1766 Type_In_Use
1767 (Etype (Next_Formal (First_Formal (Id))))));
1768 else
1769 Set_Is_Potentially_Use_Visible (Id,
1770 In_Use (P) and not Is_Hidden (Id));
1771 end if;
1773 -- Local entities are not immediately visible outside of the package
1775 Set_Is_Immediately_Visible (Id, False);
1777 -- If this is a private type with a full view (for example a local
1778 -- subtype of a private type declared elsewhere), ensure that the
1779 -- full view is also removed from visibility: it may be exposed when
1780 -- swapping views in an instantiation.
1782 if Is_Type (Id)
1783 and then Present (Full_View (Id))
1784 then
1785 Set_Is_Immediately_Visible (Full_View (Id), False);
1786 end if;
1788 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
1789 Check_Abstract_Overriding (Id);
1790 end if;
1792 if (Ekind (Id) = E_Private_Type
1793 or else Ekind (Id) = E_Limited_Private_Type)
1794 and then No (Full_View (Id))
1795 and then not Is_Generic_Type (Id)
1796 and then not Is_Derived_Type (Id)
1797 then
1798 Error_Msg_N ("missing full declaration for private type&", Id);
1800 elsif Ekind (Id) = E_Record_Type_With_Private
1801 and then not Is_Generic_Type (Id)
1802 and then No (Full_View (Id))
1803 then
1804 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
1805 Error_Msg_N ("missing full declaration for private type&", Id);
1806 else
1807 Error_Msg_N
1808 ("missing full declaration for private extension", Id);
1809 end if;
1811 elsif Ekind (Id) = E_Constant
1812 and then No (Constant_Value (Id))
1813 and then No (Full_View (Id))
1814 and then not Is_Imported (Id)
1815 and then (Nkind (Parent (Id)) /= N_Object_Declaration
1816 or else not No_Initialization (Parent (Id)))
1817 then
1818 if not Has_Private_Declaration (Etype (Id)) then
1820 -- We assume that the user did not not intend a deferred
1821 -- constant declaration, and the expression is just missing.
1823 Error_Msg_N
1824 ("constant declaration requires initialization expression",
1825 Parent (Id));
1827 if Is_Limited_Type (Etype (Id)) then
1828 Error_Msg_N
1829 ("\else remove keyword CONSTANT from declaration",
1830 Parent (Id));
1831 end if;
1833 else
1834 Error_Msg_N
1835 ("missing full declaration for deferred constant ('R'M 7.4)",
1836 Id);
1838 if Is_Limited_Type (Etype (Id)) then
1839 Error_Msg_N
1840 ("\else remove keyword CONSTANT from declaration",
1841 Parent (Id));
1842 end if;
1843 end if;
1844 end if;
1846 Next_Entity (Id);
1847 end loop;
1849 -- If the specification was installed as the parent of a public child
1850 -- unit, the private declarations were not installed, and there is
1851 -- nothing to do.
1853 if not In_Private_Part (P) then
1854 return;
1855 else
1856 Set_In_Private_Part (P, False);
1857 end if;
1859 -- Make private entities invisible and exchange full and private
1860 -- declarations for private types.
1862 while Present (Id) loop
1863 if Debug_Flag_E then
1864 Write_Str ("unlinking private entity ");
1865 Write_Int (Int (Id));
1866 Write_Eol;
1867 end if;
1869 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
1870 Check_Abstract_Overriding (Id);
1871 end if;
1873 Set_Is_Immediately_Visible (Id, False);
1875 if Is_Private_Base_Type (Id)
1876 and then Present (Full_View (Id))
1877 then
1878 Full := Full_View (Id);
1880 -- If the partial view is not declared in the visible part
1881 -- of the package (as is the case when it is a type derived
1882 -- from some other private type in the private part of the
1883 -- current package), no exchange takes place.
1885 if No (Parent (Id))
1886 or else List_Containing (Parent (Id))
1887 /= Visible_Declarations (Specification (Decl))
1888 then
1889 goto Next_Id;
1890 end if;
1892 -- The entry in the private part points to the full declaration,
1893 -- which is currently visible. Exchange them so only the private
1894 -- type declaration remains accessible, and link private and
1895 -- full declaration in the opposite direction. Before the actual
1896 -- exchange, we copy back attributes of the full view that
1897 -- must be available to the partial view too.
1899 Preserve_Full_Attributes (Id, Full);
1901 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
1903 if Is_Indefinite_Subtype (Full)
1904 and then not Is_Indefinite_Subtype (Id)
1905 then
1906 Error_Msg_N
1907 ("full view of type must be definite subtype", Full);
1908 end if;
1910 Priv_Elmt := First_Elmt (Private_Dependents (Id));
1912 -- Swap out the subtypes and derived types of Id that were
1913 -- compiled in this scope, or installed previously by
1914 -- Install_Private_Declarations.
1915 -- Before we do the swap, we verify the presence of the
1916 -- Full_View field which may be empty due to a swap by
1917 -- a previous call to End_Package_Scope (e.g. from the
1918 -- freezing mechanism).
1920 while Present (Priv_Elmt) loop
1921 Priv_Sub := Node (Priv_Elmt);
1923 if Present (Full_View (Priv_Sub)) then
1925 if Scope (Priv_Sub) = P
1926 or else not In_Open_Scopes (Scope (Priv_Sub))
1927 then
1928 Set_Is_Immediately_Visible (Priv_Sub, False);
1929 end if;
1931 if Is_Visible_Dependent (Priv_Sub) then
1932 Preserve_Full_Attributes
1933 (Priv_Sub, Full_View (Priv_Sub));
1934 Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub));
1935 Exchange_Declarations (Priv_Sub);
1936 end if;
1937 end if;
1939 Next_Elmt (Priv_Elmt);
1940 end loop;
1942 -- Now restore the type itself to its private view
1944 Exchange_Declarations (Id);
1946 elsif Ekind (Id) = E_Incomplete_Type
1947 and then No (Full_View (Id))
1948 then
1949 -- Mark Taft amendment types
1951 Set_Has_Completion_In_Body (Id);
1953 elsif not Is_Child_Unit (Id)
1954 and then (not Is_Private_Type (Id)
1955 or else No (Full_View (Id)))
1956 then
1957 Set_Is_Hidden (Id);
1958 Set_Is_Potentially_Use_Visible (Id, False);
1959 end if;
1961 <<Next_Id>>
1962 Next_Entity (Id);
1963 end loop;
1964 end Uninstall_Declarations;
1966 ------------------------
1967 -- Unit_Requires_Body --
1968 ------------------------
1970 function Unit_Requires_Body (P : Entity_Id) return Boolean is
1971 E : Entity_Id;
1973 begin
1974 -- Imported entity never requires body. Right now, only
1975 -- subprograms can be imported, but perhaps in the future
1976 -- we will allow import of packages.
1978 if Is_Imported (P) then
1979 return False;
1981 -- Body required if library package with pragma Elaborate_Body
1983 elsif Has_Pragma_Elaborate_Body (P) then
1984 return True;
1986 -- Body required if subprogram
1988 elsif Is_Subprogram (P) or else Is_Generic_Subprogram (P) then
1989 return True;
1991 -- Treat a block as requiring a body
1993 elsif Ekind (P) = E_Block then
1994 return True;
1996 elsif Ekind (P) = E_Package
1997 and then Nkind (Parent (P)) = N_Package_Specification
1998 and then Present (Generic_Parent (Parent (P)))
1999 then
2000 declare
2001 G_P : constant Entity_Id := Generic_Parent (Parent (P));
2003 begin
2004 if Has_Pragma_Elaborate_Body (G_P) then
2005 return True;
2006 end if;
2007 end;
2008 end if;
2010 -- Otherwise search entity chain for entity requiring completion
2012 E := First_Entity (P);
2013 while Present (E) loop
2015 -- Always ignore child units. Child units get added to the entity
2016 -- list of a parent unit, but are not original entities of the
2017 -- parent, and so do not affect whether the parent needs a body.
2019 if Is_Child_Unit (E) then
2020 null;
2022 -- Ignore formal packages and their renamings
2024 elsif Ekind (E) = E_Package
2025 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
2026 N_Formal_Package_Declaration
2027 then
2028 null;
2030 -- Otherwise test to see if entity requires a completion
2032 elsif (Is_Overloadable (E)
2033 and then Ekind (E) /= E_Enumeration_Literal
2034 and then Ekind (E) /= E_Operator
2035 and then not Is_Abstract (E)
2036 and then not Has_Completion (E))
2038 or else
2039 (Ekind (E) = E_Package
2040 and then E /= P
2041 and then not Has_Completion (E)
2042 and then Unit_Requires_Body (E))
2044 or else
2045 (Ekind (E) = E_Incomplete_Type and then No (Full_View (E)))
2047 or else
2048 ((Ekind (E) = E_Task_Type or else
2049 Ekind (E) = E_Protected_Type)
2050 and then not Has_Completion (E))
2052 or else
2053 (Ekind (E) = E_Generic_Package and then E /= P
2054 and then not Has_Completion (E)
2055 and then Unit_Requires_Body (E))
2057 or else
2058 (Is_Generic_Subprogram (E)
2059 and then not Has_Completion (E))
2061 then
2062 return True;
2064 -- Entity that does not require completion
2066 else
2067 null;
2068 end if;
2070 Next_Entity (E);
2071 end loop;
2073 return False;
2074 end Unit_Requires_Body;
2076 end Sem_Ch7;