* config/alpha/alpha.c, config/alpha/alpha.md,
[official-gcc.git] / gcc / ada / sem_ch7.adb
blob2e03e1f7a451ad825d9dc6d3a603584f9f0f45cb
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-2006, 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_Disp; use Sem_Disp;
54 with Sem_Util; use Sem_Util;
55 with Sem_Warn; use Sem_Warn;
56 with Snames; use Snames;
57 with Stand; use Stand;
58 with Sinfo; use Sinfo;
59 with Sinput; use Sinput;
60 with Style;
62 package body Sem_Ch7 is
64 -----------------------------------
65 -- Handling private declarations --
66 -----------------------------------
68 -- The principle that each entity has a single defining occurrence clashes
69 -- with the presence of two separate definitions for private types: the
70 -- first is the private type declaration, and the second is the full type
71 -- declaration. It is important that all references to the type point to
72 -- the same defining occurrence, namely the first one. To enforce the two
73 -- separate views of the entity, the corresponding information is swapped
74 -- between the two declarations. Outside of the package, the defining
75 -- occurrence only contains the private declaration information, while in
76 -- the private part and the body of the package the defining occurrence
77 -- contains the full declaration. To simplify the swap, the defining
78 -- occurrence that currently holds the private declaration points to the
79 -- full declaration. During semantic processing the defining occurrence
80 -- also points to a list of private dependents, that is to say access types
81 -- or composite types whose designated types or component types are
82 -- subtypes or derived types of the private type in question. After the
83 -- full declaration has been seen, the private dependents are updated to
84 -- indicate that they have full definitions.
86 -----------------------
87 -- Local Subprograms --
88 -----------------------
90 procedure Check_Anonymous_Access_Types
91 (Spec_Id : Entity_Id;
92 P_Body : Node_Id);
93 -- If the spec of a package has a limited_with_clause, it may declare
94 -- anonymous access types whose designated type is a limited view, such
95 -- an anonymous access return type for a function. This access type
96 -- cannot be elaborated in the spec itself, but it may need an itype
97 -- reference if it is used within a nested scope. In that case the itype
98 -- reference is created at the beginning of the corresponding package body
99 -- and inserted before other body declarations.
101 procedure Install_Package_Entity (Id : Entity_Id);
102 -- Basic procedure for the previous two. Places one entity on its
103 -- visibility chain, and recurses on the visible part if the entity
104 -- is an inner package.
106 function Is_Private_Base_Type (E : Entity_Id) return Boolean;
107 -- True for a private type that is not a subtype
109 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean;
110 -- If the private dependent is a private type whose full view is derived
111 -- from the parent type, its full properties are revealed only if we are in
112 -- the immediate scope of the private dependent. Should this predicate be
113 -- tightened further???
115 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id);
116 -- Called upon entering the private part of a public child package and the
117 -- body of a nested package, to potentially declare certain inherited
118 -- subprograms that were inherited by types in the visible part, but whose
119 -- declaration was deferred because the parent operation was private and
120 -- not visible at that point. These subprograms are located by traversing
121 -- the visible part declarations looking for non-private type extensions
122 -- and then examining each of the primitive operations of such types to
123 -- find those that were inherited but declared with a special internal
124 -- name. Each such operation is now declared as an operation with a normal
125 -- name (using the name of the parent operation) and replaces the previous
126 -- implicit operation in the primitive operations list of the type. If the
127 -- inherited private operation has been overridden, then it's replaced by
128 -- the overriding operation.
130 --------------------------
131 -- Analyze_Package_Body --
132 --------------------------
134 procedure Analyze_Package_Body (N : Node_Id) is
135 Loc : constant Source_Ptr := Sloc (N);
136 HSS : Node_Id;
137 Body_Id : Entity_Id;
138 Spec_Id : Entity_Id;
139 Last_Spec_Entity : Entity_Id;
140 New_N : Node_Id;
141 Pack_Decl : Node_Id;
143 procedure Install_Composite_Operations (P : Entity_Id);
144 -- Composite types declared in the current scope may depend on
145 -- types that were private at the point of declaration, and whose
146 -- full view is now in scope. Indicate that the corresponding
147 -- operations on the composite type are available.
149 ----------------------------------
150 -- Install_Composite_Operations --
151 ----------------------------------
153 procedure Install_Composite_Operations (P : Entity_Id) is
154 Id : Entity_Id;
156 begin
157 Id := First_Entity (P);
158 while Present (Id) loop
159 if Is_Type (Id)
160 and then (Is_Limited_Composite (Id)
161 or else Is_Private_Composite (Id))
162 and then No (Private_Component (Id))
163 then
164 Set_Is_Limited_Composite (Id, False);
165 Set_Is_Private_Composite (Id, False);
166 end if;
168 Next_Entity (Id);
169 end loop;
170 end Install_Composite_Operations;
172 -- Start of processing for Analyze_Package_Body
174 begin
175 -- Find corresponding package specification, and establish the
176 -- current scope. The visible defining entity for the package is the
177 -- defining occurrence in the spec. On exit from the package body, all
178 -- body declarations are attached to the defining entity for the body,
179 -- but the later is never used for name resolution. In this fashion
180 -- there is only one visible entity that denotes the package.
182 if Debug_Flag_C then
183 Write_Str ("==== Compiling package body ");
184 Write_Name (Chars (Defining_Entity (N)));
185 Write_Str (" from ");
186 Write_Location (Loc);
187 Write_Eol;
188 end if;
190 -- Set Body_Id. Note that this Will be reset to point to the
191 -- generic copy later on in the generic case.
193 Body_Id := Defining_Entity (N);
195 if Present (Corresponding_Spec (N)) then
197 -- Body is body of package instantiation. Corresponding spec
198 -- has already been set.
200 Spec_Id := Corresponding_Spec (N);
201 Pack_Decl := Unit_Declaration_Node (Spec_Id);
203 else
204 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
206 if Present (Spec_Id)
207 and then Is_Package_Or_Generic_Package (Spec_Id)
208 then
209 Pack_Decl := Unit_Declaration_Node (Spec_Id);
211 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
212 Error_Msg_N ("cannot supply body for package renaming", N);
213 return;
215 elsif Present (Corresponding_Body (Pack_Decl)) then
216 Error_Msg_N ("redefinition of package body", N);
217 return;
218 end if;
220 else
221 Error_Msg_N ("missing specification for package body", N);
222 return;
223 end if;
225 if Is_Package_Or_Generic_Package (Spec_Id)
226 and then
227 (Scope (Spec_Id) = Standard_Standard
228 or else Is_Child_Unit (Spec_Id))
229 and then not Unit_Requires_Body (Spec_Id)
230 then
231 if Ada_Version = Ada_83 then
232 Error_Msg_N
233 ("optional package body (not allowed in Ada 95)?", N);
234 else
235 Error_Msg_N
236 ("spec of this package does not allow a body", N);
237 end if;
238 end if;
239 end if;
241 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
242 Style.Check_Identifier (Body_Id, Spec_Id);
244 if Is_Child_Unit (Spec_Id) then
245 if Nkind (Parent (N)) /= N_Compilation_Unit then
246 Error_Msg_NE
247 ("body of child unit& cannot be an inner package", N, Spec_Id);
248 end if;
250 Set_Is_Child_Unit (Body_Id);
251 end if;
253 -- Generic package case
255 if Ekind (Spec_Id) = E_Generic_Package then
257 -- Disable expansion and perform semantic analysis on copy.
258 -- The unannotated body will be used in all instantiations.
260 Body_Id := Defining_Entity (N);
261 Set_Ekind (Body_Id, E_Package_Body);
262 Set_Scope (Body_Id, Scope (Spec_Id));
263 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
264 Set_Body_Entity (Spec_Id, Body_Id);
265 Set_Spec_Entity (Body_Id, Spec_Id);
267 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
268 Rewrite (N, New_N);
270 -- Update Body_Id to point to the copied node for the remainder
271 -- of the processing.
273 Body_Id := Defining_Entity (N);
274 Start_Generic;
275 end if;
277 -- The Body_Id is that of the copied node in the generic case, the
278 -- current node otherwise. Note that N was rewritten above, so we
279 -- must be sure to get the latest Body_Id value.
281 Set_Ekind (Body_Id, E_Package_Body);
282 Set_Body_Entity (Spec_Id, Body_Id);
283 Set_Spec_Entity (Body_Id, Spec_Id);
285 -- Defining name for the package body is not a visible entity: Only
286 -- the defining name for the declaration is visible.
288 Set_Etype (Body_Id, Standard_Void_Type);
289 Set_Scope (Body_Id, Scope (Spec_Id));
290 Set_Corresponding_Spec (N, Spec_Id);
291 Set_Corresponding_Body (Pack_Decl, Body_Id);
293 -- The body entity is not used for semantics or code generation, but
294 -- it is attached to the entity list of the enclosing scope to simplify
295 -- the listing of back-annotations for the types it main contain.
297 if Scope (Spec_Id) /= Standard_Standard then
298 Append_Entity (Body_Id, Scope (Spec_Id));
299 end if;
301 -- Indicate that we are currently compiling the body of the package
303 Set_In_Package_Body (Spec_Id);
304 Set_Has_Completion (Spec_Id);
305 Last_Spec_Entity := Last_Entity (Spec_Id);
307 New_Scope (Spec_Id);
309 Set_Categorization_From_Pragmas (N);
311 Install_Visible_Declarations (Spec_Id);
312 Install_Private_Declarations (Spec_Id);
313 Install_Private_With_Clauses (Spec_Id);
314 Install_Composite_Operations (Spec_Id);
316 Check_Anonymous_Access_Types (Spec_Id, N);
318 if Ekind (Spec_Id) = E_Generic_Package then
319 Set_Use (Generic_Formal_Declarations (Pack_Decl));
320 end if;
322 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
323 Set_Use (Private_Declarations (Specification (Pack_Decl)));
325 -- This is a nested package, so it may be necessary to declare
326 -- certain inherited subprograms that are not yet visible because
327 -- the parent type's subprograms are now visible.
329 if Ekind (Scope (Spec_Id)) = E_Package
330 and then Scope (Spec_Id) /= Standard_Standard
331 then
332 Declare_Inherited_Private_Subprograms (Spec_Id);
333 end if;
335 if Present (Declarations (N)) then
336 Analyze_Declarations (Declarations (N));
337 end if;
339 HSS := Handled_Statement_Sequence (N);
341 if Present (HSS) then
342 Process_End_Label (HSS, 't', Spec_Id);
343 Analyze (HSS);
345 -- Check that elaboration code in a preelaborable package body is
346 -- empty other than null statements and labels (RM 10.2.1(6)).
348 Validate_Null_Statement_Sequence (N);
349 end if;
351 Validate_Categorization_Dependency (N, Spec_Id);
352 Check_Completion (Body_Id);
354 -- Generate start of body reference. Note that we do this fairly late,
355 -- because the call will use In_Extended_Main_Source_Unit as a check,
356 -- and we want to make sure that Corresponding_Stub links are set
358 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
360 -- For a generic package, collect global references and mark them on
361 -- the original body so that they are not resolved again at the point
362 -- of instantiation.
364 if Ekind (Spec_Id) /= E_Package then
365 Save_Global_References (Original_Node (N));
366 End_Generic;
367 end if;
369 -- The entities of the package body have so far been chained onto the
370 -- declaration chain for the spec. That's been fine while we were in the
371 -- body, since we wanted them to be visible, but now that we are leaving
372 -- the package body, they are no longer visible, so we remove them from
373 -- the entity chain of the package spec entity, and copy them to the
374 -- entity chain of the package body entity, where they will never again
375 -- be visible.
377 if Present (Last_Spec_Entity) then
378 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
379 Set_Next_Entity (Last_Spec_Entity, Empty);
380 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
381 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
383 else
384 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
385 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
386 Set_First_Entity (Spec_Id, Empty);
387 Set_Last_Entity (Spec_Id, Empty);
388 end if;
390 End_Package_Scope (Spec_Id);
392 -- All entities declared in body are not visible
394 declare
395 E : Entity_Id;
397 begin
398 E := First_Entity (Body_Id);
399 while Present (E) loop
400 Set_Is_Immediately_Visible (E, False);
401 Set_Is_Potentially_Use_Visible (E, False);
402 Set_Is_Hidden (E);
404 -- Child units may appear on the entity list (for example if
405 -- they appear in the context of a subunit) but they are not
406 -- body entities.
408 if not Is_Child_Unit (E) then
409 Set_Is_Package_Body_Entity (E);
410 end if;
412 Next_Entity (E);
413 end loop;
414 end;
416 Check_References (Body_Id);
418 -- For a generic unit, check that the formal parameters are referenced,
419 -- and that local variables are used, as for regular packages.
421 if Ekind (Spec_Id) = E_Generic_Package then
422 Check_References (Spec_Id);
423 end if;
425 -- The processing so far has made all entities of the package body
426 -- public (i.e. externally visible to the linker). This is in general
427 -- necessary, since inlined or generic bodies, for which code is
428 -- generated in other units, may need to see these entities. The
429 -- following loop runs backwards from the end of the entities of the
430 -- package body making these entities invisible until we reach a
431 -- referencer, i.e. a declaration that could reference a previous
432 -- declaration, a generic body or an inlined body, or a stub (which
433 -- may contain either of these). This is of course an approximation,
434 -- but it is conservative and definitely correct.
436 -- We only do this at the outer (library) level non-generic packages.
437 -- The reason is simply to cut down on the number of external symbols
438 -- generated, so this is simply an optimization of the efficiency
439 -- of the compilation process. It has no other effect.
441 if (Scope (Spec_Id) = Standard_Standard or else Is_Child_Unit (Spec_Id))
442 and then not Is_Generic_Unit (Spec_Id)
443 and then Present (Declarations (N))
444 then
445 Make_Non_Public_Where_Possible : declare
447 function Has_Referencer
448 (L : List_Id;
449 Outer : Boolean)
450 return Boolean;
451 -- Traverse the given list of declarations in reverse order.
452 -- Return True as soon as a referencer is reached. Return
453 -- False if none is found. The Outer parameter is True for
454 -- the outer level call, and False for inner level calls for
455 -- nested packages. If Outer is True, then any entities up
456 -- to the point of hitting a referencer get their Is_Public
457 -- flag cleared, so that the entities will be treated as
458 -- static entities in the C sense, and need not have fully
459 -- qualified names. For inner levels, we need all names to
460 -- be fully qualified to deal with the same name appearing
461 -- in parallel packages (right now this is tied to their
462 -- being external).
464 --------------------
465 -- Has_Referencer --
466 --------------------
468 function Has_Referencer
469 (L : List_Id;
470 Outer : Boolean)
471 return Boolean
473 D : Node_Id;
474 E : Entity_Id;
475 K : Node_Kind;
476 S : Entity_Id;
478 begin
479 if No (L) then
480 return False;
481 end if;
483 D := Last (L);
484 while Present (D) loop
485 K := Nkind (D);
487 if K in N_Body_Stub then
488 return True;
490 elsif K = N_Subprogram_Body then
491 if Acts_As_Spec (D) then
492 E := Defining_Entity (D);
494 -- An inlined body acts as a referencer. Note also
495 -- that we never reset Is_Public for an inlined
496 -- subprogram. Gigi requires Is_Public to be set.
498 -- Note that we test Has_Pragma_Inline here rather
499 -- than Is_Inlined. We are compiling this for a
500 -- client, and it is the client who will decide
501 -- if actual inlining should occur, so we need to
502 -- assume that the procedure could be inlined for
503 -- the purpose of accessing global entities.
505 if Has_Pragma_Inline (E) then
506 return True;
507 else
508 Set_Is_Public (E, False);
509 end if;
511 else
512 E := Corresponding_Spec (D);
514 if Present (E)
515 and then (Is_Generic_Unit (E)
516 or else Has_Pragma_Inline (E)
517 or else Is_Inlined (E))
518 then
519 return True;
520 end if;
521 end if;
523 -- Processing for package bodies
525 elsif K = N_Package_Body
526 and then Present (Corresponding_Spec (D))
527 then
528 E := Corresponding_Spec (D);
530 -- Generic package body is a referencer. It would
531 -- seem that we only have to consider generics that
532 -- can be exported, i.e. where the corresponding spec
533 -- is the spec of the current package, but because of
534 -- nested instantiations, a fully private generic
535 -- body may export other private body entities.
537 if Is_Generic_Unit (E) then
538 return True;
540 -- For non-generic package body, recurse into body
541 -- unless this is an instance, we ignore instances
542 -- since they cannot have references that affect
543 -- outer entities.
545 elsif not Is_Generic_Instance (E) then
546 if Has_Referencer
547 (Declarations (D), Outer => False)
548 then
549 return True;
550 end if;
551 end if;
553 -- Processing for package specs, recurse into declarations.
554 -- Again we skip this for the case of generic instances.
556 elsif K = N_Package_Declaration then
557 S := Specification (D);
559 if not Is_Generic_Unit (Defining_Entity (S)) then
560 if Has_Referencer
561 (Private_Declarations (S), Outer => False)
562 then
563 return True;
564 elsif Has_Referencer
565 (Visible_Declarations (S), Outer => False)
566 then
567 return True;
568 end if;
569 end if;
571 -- Objects and exceptions need not be public if we have
572 -- not encountered a referencer so far. We only reset
573 -- the flag for outer level entities that are not
574 -- imported/exported, and which have no interface name.
576 elsif K = N_Object_Declaration
577 or else K = N_Exception_Declaration
578 or else K = N_Subprogram_Declaration
579 then
580 E := Defining_Entity (D);
582 if Outer
583 and then not Is_Imported (E)
584 and then not Is_Exported (E)
585 and then No (Interface_Name (E))
586 then
587 Set_Is_Public (E, False);
588 end if;
589 end if;
591 Prev (D);
592 end loop;
594 return False;
595 end Has_Referencer;
597 -- Start of processing for Make_Non_Public_Where_Possible
599 begin
600 declare
601 Discard : Boolean;
602 pragma Warnings (Off, Discard);
604 begin
605 Discard := Has_Referencer (Declarations (N), Outer => True);
606 end;
607 end Make_Non_Public_Where_Possible;
608 end if;
610 -- If expander is not active, then here is where we turn off the
611 -- In_Package_Body flag, otherwise it is turned off at the end of
612 -- the corresponding expansion routine. If this is an instance body,
613 -- we need to qualify names of local entities, because the body may
614 -- have been compiled as a preliminary to another instantiation.
616 if not Expander_Active then
617 Set_In_Package_Body (Spec_Id, False);
619 if Is_Generic_Instance (Spec_Id)
620 and then Operating_Mode = Generate_Code
621 then
622 Qualify_Entity_Names (N);
623 end if;
624 end if;
625 end Analyze_Package_Body;
627 ---------------------------------
628 -- Analyze_Package_Declaration --
629 ---------------------------------
631 procedure Analyze_Package_Declaration (N : Node_Id) is
632 Id : constant Node_Id := Defining_Entity (N);
633 PF : Boolean;
635 begin
636 -- Ada 2005 (AI-217): Check if the package has been erroneously named
637 -- in a limited-with clause of its own context. In this case the error
638 -- has been previously notified by Analyze_Context.
640 -- limited with Pkg; -- ERROR
641 -- package Pkg is ...
643 if From_With_Type (Id) then
644 return;
645 end if;
647 Generate_Definition (Id);
648 Enter_Name (Id);
649 Set_Ekind (Id, E_Package);
650 Set_Etype (Id, Standard_Void_Type);
652 New_Scope (Id);
654 PF := Is_Pure (Enclosing_Lib_Unit_Entity);
655 Set_Is_Pure (Id, PF);
657 Set_Categorization_From_Pragmas (N);
659 if Debug_Flag_C then
660 Write_Str ("==== Compiling package spec ");
661 Write_Name (Chars (Id));
662 Write_Str (" from ");
663 Write_Location (Sloc (N));
664 Write_Eol;
665 end if;
667 Analyze (Specification (N));
668 Validate_Categorization_Dependency (N, Id);
669 End_Package_Scope (Id);
671 -- For a compilation unit, indicate whether it needs a body, and
672 -- whether elaboration warnings may be meaningful on it.
674 if Nkind (Parent (N)) = N_Compilation_Unit then
675 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
677 if not Body_Required (Parent (N)) then
678 Set_Suppress_Elaboration_Warnings (Id);
679 end if;
681 Validate_RT_RAT_Component (N);
682 end if;
683 end Analyze_Package_Declaration;
685 -----------------------------------
686 -- Analyze_Package_Specification --
687 -----------------------------------
689 -- Note that this code is shared for the analysis of generic package
690 -- specs (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
692 procedure Analyze_Package_Specification (N : Node_Id) is
693 Id : constant Entity_Id := Defining_Entity (N);
694 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
695 Vis_Decls : constant List_Id := Visible_Declarations (N);
696 Priv_Decls : constant List_Id := Private_Declarations (N);
697 E : Entity_Id;
698 L : Entity_Id;
699 Public_Child : Boolean;
701 Private_With_Clauses_Installed : Boolean := False;
702 -- In Ada 2005, private with_clauses are visible in the private part
703 -- of a nested package, even if it appears in the public part of the
704 -- enclosing package. This requires a separate step to install these
705 -- private_with_clauses, and remove them at the end of the nested
706 -- package.
708 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
709 -- Clears constant indications (Never_Set_In_Source, Constant_Value,
710 -- and Is_True_Constant) on all variables that are entities of Id,
711 -- and on the chain whose first element is FE. A recursive call is
712 -- made for all packages and generic packages.
714 procedure Generate_Parent_References;
715 -- For a child unit, generate references to parent units, for
716 -- GPS navigation purposes.
718 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
719 -- Child and Unit are entities of compilation units. True if Child
720 -- is a public child of Parent as defined in 10.1.1
722 procedure Inspect_Deferred_Constant_Completion;
723 -- Examines the deferred constants in the private part of the package
724 -- specification. Emits the error message "constant declaration requires
725 -- initialization expression " if not completed by an Import pragma.
727 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
728 -- Detects all incomplete or private type declarations having a known
729 -- discriminant part that are completed by an Unchecked_Union. Emits
730 -- the error message "Unchecked_Union may not complete discriminated
731 -- partial view".
733 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
734 -- Given the package entity of a generic package instantiation or
735 -- formal package whose corresponding generic is a child unit, installs
736 -- the private declarations of each of the child unit's parents.
737 -- This has to be done at the point of entering the instance package's
738 -- private part rather than being done in Sem_Ch12.Install_Parent
739 -- (which is where the parents' visible declarations are installed).
741 ---------------------
742 -- Clear_Constants --
743 ---------------------
745 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
746 E : Entity_Id;
748 begin
749 -- Ignore package renamings, not interesting and they can
750 -- cause self referential loops in the code below.
752 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
753 return;
754 end if;
756 -- Note: in the loop below, the check for Next_Entity pointing
757 -- back to the package entity may seem odd, but it is needed,
758 -- because a package can contain a renaming declaration to itself,
759 -- and such renamings are generated automatically within package
760 -- instances.
762 E := FE;
763 while Present (E) and then E /= Id loop
764 if Ekind (E) = E_Variable then
765 Set_Never_Set_In_Source (E, False);
766 Set_Is_True_Constant (E, False);
767 Set_Current_Value (E, Empty);
768 Set_Is_Known_Null (E, False);
769 Set_Last_Assignment (E, Empty);
771 if not Can_Never_Be_Null (E) then
772 Set_Is_Known_Non_Null (E, False);
773 end if;
775 elsif Ekind (E) = E_Package
776 or else
777 Ekind (E) = E_Generic_Package
778 then
779 Clear_Constants (E, First_Entity (E));
780 Clear_Constants (E, First_Private_Entity (E));
781 end if;
783 Next_Entity (E);
784 end loop;
785 end Clear_Constants;
787 --------------------------------
788 -- Generate_Parent_References --
789 --------------------------------
791 procedure Generate_Parent_References is
792 Decl : constant Node_Id := Parent (N);
794 begin
795 if Id = Cunit_Entity (Main_Unit)
796 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
797 then
798 Generate_Reference (Id, Scope (Id), 'k', False);
800 elsif Nkind (Unit (Cunit (Main_Unit))) /= N_Subprogram_Body
801 and then Nkind (Unit (Cunit (Main_Unit))) /= N_Subunit
802 then
803 -- If current unit is an ancestor of main unit, generate
804 -- a reference to its own parent.
806 declare
807 U : Node_Id;
808 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
810 begin
811 if Nkind (Main_Spec) = N_Package_Body then
812 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
813 end if;
815 U := Parent_Spec (Main_Spec);
816 while Present (U) loop
817 if U = Parent (Decl) then
818 Generate_Reference (Id, Scope (Id), 'k', False);
819 exit;
821 elsif Nkind (Unit (U)) = N_Package_Body then
822 exit;
824 else
825 U := Parent_Spec (Unit (U));
826 end if;
827 end loop;
828 end;
829 end if;
830 end Generate_Parent_References;
832 ---------------------
833 -- Is_Public_Child --
834 ---------------------
836 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
837 begin
838 if not Is_Private_Descendant (Child) then
839 return True;
840 else
841 if Child = Unit then
842 return not Private_Present (
843 Parent (Unit_Declaration_Node (Child)));
844 else
845 return Is_Public_Child (Scope (Child), Unit);
846 end if;
847 end if;
848 end Is_Public_Child;
850 ------------------------------------------
851 -- Inspect_Deferred_Constant_Completion --
852 ------------------------------------------
854 procedure Inspect_Deferred_Constant_Completion is
855 Decl : Node_Id;
857 begin
858 Decl := First (Priv_Decls);
859 while Present (Decl) loop
861 -- Deferred constant signature
863 if Nkind (Decl) = N_Object_Declaration
864 and then Constant_Present (Decl)
865 and then No (Expression (Decl))
867 -- No need to check internally generated constants
869 and then Comes_From_Source (Decl)
871 -- The constant is not completed. A full object declaration
872 -- or a pragma Import complete a deferred constant.
874 and then not Has_Completion (Defining_Identifier (Decl))
875 then
876 Error_Msg_N
877 ("constant declaration requires initialization expression",
878 Defining_Identifier (Decl));
879 end if;
881 Decl := Next (Decl);
882 end loop;
883 end Inspect_Deferred_Constant_Completion;
885 ----------------------------------------
886 -- Inspect_Unchecked_Union_Completion --
887 ----------------------------------------
889 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
890 Decl : Node_Id;
892 begin
893 Decl := First (Decls);
894 while Present (Decl) loop
896 -- We are looking at an incomplete or private type declaration
897 -- with a known_discriminant_part whose full view is an
898 -- Unchecked_Union.
900 if (Nkind (Decl) = N_Incomplete_Type_Declaration
901 or else
902 Nkind (Decl) = N_Private_Type_Declaration)
903 and then Has_Discriminants (Defining_Identifier (Decl))
904 and then Present (Full_View (Defining_Identifier (Decl)))
905 and then Is_Unchecked_Union
906 (Full_View (Defining_Identifier (Decl)))
907 then
908 Error_Msg_N ("completion of discriminated partial view" &
909 " cannot be an Unchecked_Union",
910 Full_View (Defining_Identifier (Decl)));
911 end if;
913 Next (Decl);
914 end loop;
915 end Inspect_Unchecked_Union_Completion;
917 -----------------------------------------
918 -- Install_Parent_Private_Declarations --
919 -----------------------------------------
921 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
922 Inst_Par : Entity_Id;
923 Gen_Par : Entity_Id;
924 Inst_Node : Node_Id;
926 begin
927 Inst_Par := Inst_Id;
928 Gen_Par :=
929 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
930 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
931 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
933 if (Nkind (Inst_Node) = N_Package_Instantiation
934 or else Nkind (Inst_Node) = N_Formal_Package_Declaration)
935 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
936 then
937 Inst_Par := Entity (Prefix (Name (Inst_Node)));
939 if Present (Renamed_Entity (Inst_Par)) then
940 Inst_Par := Renamed_Entity (Inst_Par);
941 end if;
943 Gen_Par :=
944 Generic_Parent
945 (Specification (Unit_Declaration_Node (Inst_Par)));
947 -- Install the private declarations and private use clauses
948 -- of a parent instance of the child instance, unless the
949 -- parent instance private declarations have already been
950 -- installed earlier in Analyze_Package_Specification, which
951 -- happens when a generic child is instantiated, and the
952 -- instance is a child of the parent instance.
954 -- Installing the use clauses of the parent instance twice is
955 -- both unnecessary and wrong, because it would cause the
956 -- clauses to be chained to themselves in the use clauses list
957 -- of the scope stack entry. That in turn would cause
958 -- End_Use_Clauses to get into an endless look upon scope exit.
960 if Present (Gen_Par) then
961 if not In_Private_Part (Inst_Par) then
962 Install_Private_Declarations (Inst_Par);
963 Set_Use (Private_Declarations
964 (Specification
965 (Unit_Declaration_Node (Inst_Par))));
966 end if;
968 -- If we've reached the end of the generic instance parents,
969 -- then finish off by looping through the nongeneric parents
970 -- and installing their private declarations.
972 else
973 while Present (Inst_Par)
974 and then Inst_Par /= Standard_Standard
975 and then (not In_Open_Scopes (Inst_Par)
976 or else not In_Private_Part (Inst_Par))
977 loop
978 Install_Private_Declarations (Inst_Par);
979 Set_Use (Private_Declarations
980 (Specification
981 (Unit_Declaration_Node (Inst_Par))));
982 Inst_Par := Scope (Inst_Par);
983 end loop;
985 exit;
986 end if;
988 else
989 exit;
990 end if;
991 end loop;
992 end Install_Parent_Private_Declarations;
994 -- Start of processing for Analyze_Package_Specification
996 begin
997 if Present (Vis_Decls) then
998 Analyze_Declarations (Vis_Decls);
999 end if;
1001 -- Verify that incomplete types have received full declarations
1003 E := First_Entity (Id);
1004 while Present (E) loop
1005 if Ekind (E) = E_Incomplete_Type
1006 and then No (Full_View (E))
1007 then
1008 Error_Msg_N ("no declaration in visible part for incomplete}", E);
1009 end if;
1011 Next_Entity (E);
1012 end loop;
1014 if Is_Remote_Call_Interface (Id)
1015 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1016 then
1017 Validate_RCI_Declarations (Id);
1018 end if;
1020 -- Save global references in the visible declarations, before
1021 -- installing private declarations of parent unit if there is one,
1022 -- because the privacy status of types defined in the parent will
1023 -- change. This is only relevant for generic child units, but is
1024 -- done in all cases for uniformity.
1026 if Ekind (Id) = E_Generic_Package
1027 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1028 then
1029 declare
1030 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1031 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1033 begin
1034 Set_Private_Declarations (Orig_Spec, Empty_List);
1035 Save_Global_References (Orig_Decl);
1036 Set_Private_Declarations (Orig_Spec, Save_Priv);
1037 end;
1038 end if;
1040 -- If package is a public child unit, then make the private declarations
1041 -- of the parent visible.
1043 Public_Child := False;
1045 declare
1046 Par : Entity_Id;
1047 Pack_Decl : Node_Id;
1048 Par_Spec : Node_Id;
1050 begin
1051 Par := Id;
1052 Par_Spec := Parent_Spec (Parent (N));
1054 -- If the package is formal package of an enclosing generic, it is
1055 -- transformed into a local generic declaration, and compiled to make
1056 -- its spec available. We need to retrieve the original generic to
1057 -- determine whether it is a child unit, and install its parents.
1059 if No (Par_Spec)
1060 and then
1061 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1062 then
1063 Par := Entity (Name (Original_Node (Parent (N))));
1064 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1065 end if;
1067 if Present (Par_Spec) then
1068 Generate_Parent_References;
1070 while Scope (Par) /= Standard_Standard
1071 and then Is_Public_Child (Id, Par)
1072 and then In_Open_Scopes (Par)
1073 loop
1074 Public_Child := True;
1075 Par := Scope (Par);
1076 Install_Private_Declarations (Par);
1077 Install_Private_With_Clauses (Par);
1078 Pack_Decl := Unit_Declaration_Node (Par);
1079 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1080 end loop;
1081 end if;
1082 end;
1084 if Is_Compilation_Unit (Id) then
1085 Install_Private_With_Clauses (Id);
1086 else
1088 -- The current compilation unit may include private with_clauses,
1089 -- which are visible in the private part of the current nested
1090 -- package, and have to be installed now.
1092 declare
1093 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1094 begin
1095 if (Ekind (Comp_Unit) = E_Package
1096 or else Ekind (Comp_Unit) = E_Generic_Package)
1097 and then not In_Private_Part (Comp_Unit)
1098 then
1099 Install_Private_With_Clauses (Comp_Unit);
1100 Private_With_Clauses_Installed := True;
1101 end if;
1102 end;
1103 end if;
1105 -- If this is a package associated with a generic instance or formal
1106 -- package, then the private declarations of each of the generic's
1107 -- parents must be installed at this point.
1109 if Is_Generic_Instance (Id) then
1110 Install_Parent_Private_Declarations (Id);
1111 end if;
1113 -- Analyze private part if present. The flag In_Private_Part is reset
1114 -- in End_Package_Scope.
1116 L := Last_Entity (Id);
1118 if Present (Priv_Decls) then
1119 Set_In_Private_Part (Id);
1121 -- Upon entering a public child's private part, it may be necessary
1122 -- to declare subprograms that were derived in the package's visible
1123 -- part but not yet made visible.
1125 if Public_Child then
1126 Declare_Inherited_Private_Subprograms (Id);
1127 end if;
1129 Analyze_Declarations (Priv_Decls);
1131 -- Check the private declarations for incomplete deferred constants
1133 Inspect_Deferred_Constant_Completion;
1135 -- The first private entity is the immediate follower of the last
1136 -- visible entity, if there was one.
1138 if Present (L) then
1139 Set_First_Private_Entity (Id, Next_Entity (L));
1140 else
1141 Set_First_Private_Entity (Id, First_Entity (Id));
1142 end if;
1144 -- There may be inherited private subprograms that need to be declared,
1145 -- even in the absence of an explicit private part. If there are any
1146 -- public declarations in the package and the package is a public child
1147 -- unit, then an implicit private part is assumed.
1149 elsif Present (L) and then Public_Child then
1150 Set_In_Private_Part (Id);
1151 Declare_Inherited_Private_Subprograms (Id);
1152 Set_First_Private_Entity (Id, Next_Entity (L));
1153 end if;
1155 -- Check rule of 3.6(11), which in general requires waiting till all
1156 -- full types have been seen.
1158 E := First_Entity (Id);
1159 while Present (E) loop
1160 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1161 Check_Aliased_Component_Types (E);
1162 end if;
1164 Next_Entity (E);
1165 end loop;
1167 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1168 -- declaration having a known_discriminant_part shall not be an
1169 -- Unchecked_Union type.
1171 if Present (Vis_Decls) then
1172 Inspect_Unchecked_Union_Completion (Vis_Decls);
1173 end if;
1175 if Present (Priv_Decls) then
1176 Inspect_Unchecked_Union_Completion (Priv_Decls);
1177 end if;
1179 if Ekind (Id) = E_Generic_Package
1180 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1181 and then Present (Priv_Decls)
1182 then
1183 -- Save global references in private declarations, ignoring the
1184 -- visible declarations that were processed earlier.
1186 declare
1187 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1188 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1189 Save_Form : constant List_Id :=
1190 Generic_Formal_Declarations (Orig_Decl);
1192 begin
1193 Set_Visible_Declarations (Orig_Spec, Empty_List);
1194 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1195 Save_Global_References (Orig_Decl);
1196 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1197 Set_Visible_Declarations (Orig_Spec, Save_Vis);
1198 end;
1199 end if;
1201 Process_End_Label (N, 'e', Id);
1203 -- Remove private_with_clauses of enclosing compilation unit, if they
1204 -- were installed.
1206 if Private_With_Clauses_Installed then
1207 Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
1208 end if;
1210 -- For the case of a library level package, we must go through all the
1211 -- entities clearing the indications that the value may be constant and
1212 -- not modified. Why? Because any client of this package may modify
1213 -- these values freely from anywhere. This also applies to any nested
1214 -- packages or generic packages.
1216 -- For now we unconditionally clear constants for packages that are
1217 -- instances of generic packages. The reason is that we do not have the
1218 -- body yet, and we otherwise think things are unreferenced when they
1219 -- are not. This should be fixed sometime (the effect is not terrible,
1220 -- we just lose some warnings, and also some cases of value propagation)
1221 -- ???
1223 if Is_Library_Level_Entity (Id)
1224 or else Is_Generic_Instance (Id)
1225 then
1226 Clear_Constants (Id, First_Entity (Id));
1227 Clear_Constants (Id, First_Private_Entity (Id));
1228 end if;
1229 end Analyze_Package_Specification;
1231 --------------------------------------
1232 -- Analyze_Private_Type_Declaration --
1233 --------------------------------------
1235 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1236 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
1237 Id : constant Entity_Id := Defining_Identifier (N);
1239 begin
1240 Generate_Definition (Id);
1241 Set_Is_Pure (Id, PF);
1242 Init_Size_Align (Id);
1244 if (Ekind (Current_Scope) /= E_Package
1245 and then Ekind (Current_Scope) /= E_Generic_Package)
1246 or else In_Private_Part (Current_Scope)
1247 then
1248 Error_Msg_N ("invalid context for private declaration", N);
1249 end if;
1251 New_Private_Type (N, Id, N);
1252 Set_Depends_On_Private (Id);
1253 end Analyze_Private_Type_Declaration;
1255 ----------------------------------
1256 -- Check_Anonymous_Access_Types --
1257 ----------------------------------
1259 procedure Check_Anonymous_Access_Types
1260 (Spec_Id : Entity_Id;
1261 P_Body : Node_Id)
1263 E : Entity_Id;
1264 IR : Node_Id;
1266 begin
1267 -- Itype references are only needed by gigi, to force elaboration of
1268 -- itypes. In the absence of code generation, they are not needed.
1270 if not Expander_Active then
1271 return;
1272 end if;
1274 E := First_Entity (Spec_Id);
1275 while Present (E) loop
1276 if Ekind (E) = E_Anonymous_Access_Type
1277 and then From_With_Type (E)
1278 then
1279 IR := Make_Itype_Reference (Sloc (P_Body));
1280 Set_Itype (IR, E);
1282 if No (Declarations (P_Body)) then
1283 Set_Declarations (P_Body, New_List);
1284 end if;
1286 Insert_Before (First (Declarations (P_Body)), IR);
1287 end if;
1289 Next_Entity (E);
1290 end loop;
1291 end Check_Anonymous_Access_Types;
1293 -------------------------------------------
1294 -- Declare_Inherited_Private_Subprograms --
1295 -------------------------------------------
1297 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1298 E : Entity_Id;
1299 Op_List : Elist_Id;
1300 Op_Elmt : Elmt_Id;
1301 Op_Elmt_2 : Elmt_Id;
1302 Prim_Op : Entity_Id;
1303 New_Op : Entity_Id := Empty;
1304 Parent_Subp : Entity_Id;
1305 Found_Explicit : Boolean;
1306 Decl_Privates : Boolean;
1308 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1309 -- Check whether an inherited subprogram is an operation of an
1310 -- untagged derived type.
1312 ---------------------
1313 -- Is_Primitive_Of --
1314 ---------------------
1316 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1317 Formal : Entity_Id;
1319 begin
1320 if Etype (S) = T then
1321 return True;
1323 else
1324 Formal := First_Formal (S);
1325 while Present (Formal) loop
1326 if Etype (Formal) = T then
1327 return True;
1328 end if;
1330 Next_Formal (Formal);
1331 end loop;
1333 return False;
1334 end if;
1335 end Is_Primitive_Of;
1337 -- Start of processing for Declare_Inherited_Private_Subprograms
1339 begin
1340 E := First_Entity (Id);
1341 while Present (E) loop
1343 -- If the entity is a nonprivate type extension whose parent
1344 -- type is declared in an open scope, then the type may have
1345 -- inherited operations that now need to be made visible.
1346 -- Ditto if the entity is a formal derived type in a child unit.
1348 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
1349 or else
1350 (Nkind (Parent (E)) = N_Private_Extension_Declaration
1351 and then Is_Generic_Type (E)))
1352 and then In_Open_Scopes (Scope (Etype (E)))
1353 and then E = Base_Type (E)
1354 then
1355 if Is_Tagged_Type (E) then
1356 Op_List := Primitive_Operations (E);
1357 New_Op := Empty;
1358 Decl_Privates := False;
1360 Op_Elmt := First_Elmt (Op_List);
1361 while Present (Op_Elmt) loop
1362 Prim_Op := Node (Op_Elmt);
1364 -- If the primitive operation is an implicit operation
1365 -- with an internal name whose parent operation has
1366 -- a normal name, then we now need to either declare the
1367 -- operation (i.e., make it visible), or replace it
1368 -- by an overriding operation if one exists.
1370 if Present (Alias (Prim_Op))
1371 and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
1372 and then not Comes_From_Source (Prim_Op)
1373 and then Is_Internal_Name (Chars (Prim_Op))
1374 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1375 then
1376 Parent_Subp := Alias (Prim_Op);
1378 Found_Explicit := False;
1379 Op_Elmt_2 := Next_Elmt (Op_Elmt);
1380 while Present (Op_Elmt_2) loop
1381 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
1382 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
1383 then
1384 -- The private inherited operation has been
1385 -- overridden by an explicit subprogram, so
1386 -- change the private op's list element to
1387 -- designate the explicit so the explicit
1388 -- one will get the right dispatching slot.
1390 New_Op := Node (Op_Elmt_2);
1391 Replace_Elmt (Op_Elmt, New_Op);
1392 Remove_Elmt (Op_List, Op_Elmt_2);
1393 Found_Explicit := True;
1394 Set_Is_Overriding_Operation (New_Op);
1395 Decl_Privates := True;
1397 exit;
1398 end if;
1400 Next_Elmt (Op_Elmt_2);
1401 end loop;
1403 if not Found_Explicit then
1404 Derive_Subprogram
1405 (New_Op, Alias (Prim_Op), E, Etype (E));
1407 pragma Assert
1408 (Is_Dispatching_Operation (New_Op)
1409 and then Node (Last_Elmt (Op_List)) = New_Op);
1411 -- Substitute the new operation for the old one
1412 -- in the type's primitive operations list. Since
1413 -- the new operation was also just added to the end
1414 -- of list, the last element must be removed.
1416 -- (Question: is there a simpler way of declaring
1417 -- the operation, say by just replacing the name
1418 -- of the earlier operation, reentering it in the
1419 -- in the symbol table (how?), and marking it as
1420 -- private???)
1422 Replace_Elmt (Op_Elmt, New_Op);
1423 Remove_Last_Elmt (Op_List);
1424 Decl_Privates := True;
1425 end if;
1426 end if;
1428 Next_Elmt (Op_Elmt);
1429 end loop;
1431 -- The type's DT attributes need to be recalculated
1432 -- in the case where private dispatching operations
1433 -- have been added or overridden. Normally this action
1434 -- occurs during type freezing, but we force it here
1435 -- since the type may already have been frozen (e.g.,
1436 -- if the type's package has an empty private part).
1437 -- This can only be done if expansion is active, otherwise
1438 -- Tag may not be present.
1440 if Decl_Privates
1441 and then Expander_Active
1442 then
1443 Set_All_DT_Position (E);
1444 end if;
1446 else
1447 -- Non-tagged type, scan forward to locate
1448 -- inherited hidden operations.
1450 Prim_Op := Next_Entity (E);
1451 while Present (Prim_Op) loop
1452 if Is_Subprogram (Prim_Op)
1453 and then Present (Alias (Prim_Op))
1454 and then not Comes_From_Source (Prim_Op)
1455 and then Is_Internal_Name (Chars (Prim_Op))
1456 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1457 and then Is_Primitive_Of (E, Prim_Op)
1458 then
1459 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1460 end if;
1462 Next_Entity (Prim_Op);
1463 end loop;
1464 end if;
1465 end if;
1467 Next_Entity (E);
1468 end loop;
1469 end Declare_Inherited_Private_Subprograms;
1471 -----------------------
1472 -- End_Package_Scope --
1473 -----------------------
1475 procedure End_Package_Scope (P : Entity_Id) is
1476 begin
1477 Uninstall_Declarations (P);
1478 Pop_Scope;
1479 end End_Package_Scope;
1481 ---------------------------
1482 -- Exchange_Declarations --
1483 ---------------------------
1485 procedure Exchange_Declarations (Id : Entity_Id) is
1486 Full_Id : constant Entity_Id := Full_View (Id);
1487 H1 : constant Entity_Id := Homonym (Id);
1488 Next1 : constant Entity_Id := Next_Entity (Id);
1489 H2 : Entity_Id;
1490 Next2 : Entity_Id;
1492 begin
1493 -- If missing full declaration for type, nothing to exchange
1495 if No (Full_Id) then
1496 return;
1497 end if;
1499 -- Otherwise complete the exchange, and preserve semantic links
1501 Next2 := Next_Entity (Full_Id);
1502 H2 := Homonym (Full_Id);
1504 -- Reset full declaration pointer to reflect the switched entities
1505 -- and readjust the next entity chains.
1507 Exchange_Entities (Id, Full_Id);
1509 Set_Next_Entity (Id, Next1);
1510 Set_Homonym (Id, H1);
1512 Set_Full_View (Full_Id, Id);
1513 Set_Next_Entity (Full_Id, Next2);
1514 Set_Homonym (Full_Id, H2);
1515 end Exchange_Declarations;
1517 ----------------------------
1518 -- Install_Package_Entity --
1519 ----------------------------
1521 procedure Install_Package_Entity (Id : Entity_Id) is
1522 begin
1523 if not Is_Internal (Id) then
1524 if Debug_Flag_E then
1525 Write_Str ("Install: ");
1526 Write_Name (Chars (Id));
1527 Write_Eol;
1528 end if;
1530 if not Is_Child_Unit (Id) then
1531 Set_Is_Immediately_Visible (Id);
1532 end if;
1534 end if;
1535 end Install_Package_Entity;
1537 ----------------------------------
1538 -- Install_Private_Declarations --
1539 ----------------------------------
1541 procedure Install_Private_Declarations (P : Entity_Id) is
1542 Id : Entity_Id;
1543 Priv_Elmt : Elmt_Id;
1544 Priv : Entity_Id;
1545 Full : Entity_Id;
1547 begin
1548 -- First exchange declarations for private types, so that the
1549 -- full declaration is visible. For each private type, we check
1550 -- its Private_Dependents list and also exchange any subtypes of
1551 -- or derived types from it. Finally, if this is a Taft amendment
1552 -- type, the incomplete declaration is irrelevant, and we want to
1553 -- link the eventual full declaration with the original private
1554 -- one so we also skip the exchange.
1556 Id := First_Entity (P);
1557 while Present (Id) and then Id /= First_Private_Entity (P) loop
1558 if Is_Private_Base_Type (Id)
1559 and then Comes_From_Source (Full_View (Id))
1560 and then Present (Full_View (Id))
1561 and then Scope (Full_View (Id)) = Scope (Id)
1562 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
1563 then
1564 -- If there is a use-type clause on the private type, set the
1565 -- full view accordingly.
1567 Set_In_Use (Full_View (Id), In_Use (Id));
1568 Full := Full_View (Id);
1570 if Is_Private_Base_Type (Full)
1571 and then Has_Private_Declaration (Full)
1572 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
1573 and then In_Open_Scopes (Scope (Etype (Full)))
1574 and then In_Package_Body (Current_Scope)
1575 and then not Is_Private_Type (Etype (Full))
1576 then
1577 -- This is the completion of a private type by a derivation
1578 -- from another private type which is not private anymore. This
1579 -- can only happen in a package nested within a child package,
1580 -- when the parent type is defined in the parent unit. At this
1581 -- point the current type is not private either, and we have to
1582 -- install the underlying full view, which is now visible.
1584 if No (Full_View (Full))
1585 and then Present (Underlying_Full_View (Full))
1586 then
1587 Set_Full_View (Id, Underlying_Full_View (Full));
1588 Set_Underlying_Full_View (Full, Empty);
1589 Set_Is_Frozen (Full_View (Id));
1590 end if;
1591 end if;
1593 Priv_Elmt := First_Elmt (Private_Dependents (Id));
1595 Exchange_Declarations (Id);
1596 Set_Is_Immediately_Visible (Id);
1598 while Present (Priv_Elmt) loop
1599 Priv := Node (Priv_Elmt);
1601 -- Before the exchange, verify that the presence of the
1602 -- Full_View field. It will be empty if the entity
1603 -- has already been installed due to a previous call.
1605 if Present (Full_View (Priv))
1606 and then Is_Visible_Dependent (Priv)
1607 then
1609 -- For each subtype that is swapped, we also swap the
1610 -- reference to it in Private_Dependents, to allow access
1611 -- to it when we swap them out in End_Package_Scope.
1613 Replace_Elmt (Priv_Elmt, Full_View (Priv));
1614 Exchange_Declarations (Priv);
1615 Set_Is_Immediately_Visible
1616 (Priv, In_Open_Scopes (Scope (Priv)));
1617 Set_Is_Potentially_Use_Visible
1618 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
1619 end if;
1621 Next_Elmt (Priv_Elmt);
1622 end loop;
1623 end if;
1625 Next_Entity (Id);
1626 end loop;
1628 -- Next make other declarations in the private part visible as well
1630 Id := First_Private_Entity (P);
1631 while Present (Id) loop
1632 Install_Package_Entity (Id);
1633 Set_Is_Hidden (Id, False);
1634 Next_Entity (Id);
1635 end loop;
1637 -- Indicate that the private part is currently visible, so it can be
1638 -- properly reset on exit.
1640 Set_In_Private_Part (P);
1641 end Install_Private_Declarations;
1643 ----------------------------------
1644 -- Install_Visible_Declarations --
1645 ----------------------------------
1647 procedure Install_Visible_Declarations (P : Entity_Id) is
1648 Id : Entity_Id;
1649 Last_Entity : Entity_Id;
1651 begin
1652 pragma Assert
1653 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
1655 if Is_Package_Or_Generic_Package (P) then
1656 Last_Entity := First_Private_Entity (P);
1657 else
1658 Last_Entity := Empty;
1659 end if;
1661 Id := First_Entity (P);
1662 while Present (Id) and then Id /= Last_Entity loop
1663 Install_Package_Entity (Id);
1664 Next_Entity (Id);
1665 end loop;
1666 end Install_Visible_Declarations;
1668 --------------------------
1669 -- Is_Private_Base_Type --
1670 --------------------------
1672 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
1673 begin
1674 return Ekind (E) = E_Private_Type
1675 or else Ekind (E) = E_Limited_Private_Type
1676 or else Ekind (E) = E_Record_Type_With_Private;
1677 end Is_Private_Base_Type;
1679 --------------------------
1680 -- Is_Visible_Dependent --
1681 --------------------------
1683 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
1685 S : constant Entity_Id := Scope (Dep);
1687 begin
1688 -- Renamings created for actual types have the visibility of the
1689 -- actual.
1691 if Ekind (S) = E_Package
1692 and then Is_Generic_Instance (S)
1693 and then (Is_Generic_Actual_Type (Dep)
1694 or else Is_Generic_Actual_Type (Full_View (Dep)))
1695 then
1696 return True;
1698 elsif not (Is_Derived_Type (Dep))
1699 and then Is_Derived_Type (Full_View (Dep))
1700 then
1701 -- When instantiating a package body, the scope stack is empty,
1702 -- so check instead whether the dependent type is defined in
1703 -- the same scope as the instance itself.
1705 return In_Open_Scopes (S)
1706 or else (Is_Generic_Instance (Current_Scope)
1707 and then Scope (Dep) = Scope (Current_Scope));
1708 else
1709 return True;
1710 end if;
1711 end Is_Visible_Dependent;
1713 ----------------------------
1714 -- May_Need_Implicit_Body --
1715 ----------------------------
1717 procedure May_Need_Implicit_Body (E : Entity_Id) is
1718 P : constant Node_Id := Unit_Declaration_Node (E);
1719 S : constant Node_Id := Parent (P);
1720 B : Node_Id;
1721 Decls : List_Id;
1723 begin
1724 if not Has_Completion (E)
1725 and then Nkind (P) = N_Package_Declaration
1726 and then Present (Activation_Chain_Entity (P))
1727 then
1728 B :=
1729 Make_Package_Body (Sloc (E),
1730 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
1731 Chars => Chars (E)),
1732 Declarations => New_List);
1734 if Nkind (S) = N_Package_Specification then
1735 if Present (Private_Declarations (S)) then
1736 Decls := Private_Declarations (S);
1737 else
1738 Decls := Visible_Declarations (S);
1739 end if;
1740 else
1741 Decls := Declarations (S);
1742 end if;
1744 Append (B, Decls);
1745 Analyze (B);
1746 end if;
1747 end May_Need_Implicit_Body;
1749 ----------------------
1750 -- New_Private_Type --
1751 ----------------------
1753 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
1754 begin
1755 Enter_Name (Id);
1757 if Limited_Present (Def) then
1758 Set_Ekind (Id, E_Limited_Private_Type);
1759 else
1760 Set_Ekind (Id, E_Private_Type);
1761 end if;
1763 Set_Etype (Id, Id);
1764 Set_Has_Delayed_Freeze (Id);
1765 Set_Is_First_Subtype (Id);
1766 Init_Size_Align (Id);
1768 Set_Is_Constrained (Id,
1769 No (Discriminant_Specifications (N))
1770 and then not Unknown_Discriminants_Present (N));
1772 -- Set tagged flag before processing discriminants, to catch
1773 -- illegal usage.
1775 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
1777 Set_Discriminant_Constraint (Id, No_Elist);
1778 Set_Stored_Constraint (Id, No_Elist);
1780 if Present (Discriminant_Specifications (N)) then
1781 New_Scope (Id);
1782 Process_Discriminants (N);
1783 End_Scope;
1785 elsif Unknown_Discriminants_Present (N) then
1786 Set_Has_Unknown_Discriminants (Id);
1787 end if;
1789 Set_Private_Dependents (Id, New_Elmt_List);
1791 if Tagged_Present (Def) then
1792 Set_Ekind (Id, E_Record_Type_With_Private);
1793 Make_Class_Wide_Type (Id);
1794 Set_Primitive_Operations (Id, New_Elmt_List);
1795 Set_Is_Abstract (Id, Abstract_Present (Def));
1796 Set_Is_Limited_Record (Id, Limited_Present (Def));
1797 Set_Has_Delayed_Freeze (Id, True);
1799 elsif Abstract_Present (Def) then
1800 Error_Msg_N ("only a tagged type can be abstract", N);
1801 end if;
1802 end New_Private_Type;
1804 ----------------------------
1805 -- Uninstall_Declarations --
1806 ----------------------------
1808 procedure Uninstall_Declarations (P : Entity_Id) is
1809 Decl : constant Node_Id := Unit_Declaration_Node (P);
1810 Id : Entity_Id;
1811 Full : Entity_Id;
1812 Priv_Elmt : Elmt_Id;
1813 Priv_Sub : Entity_Id;
1815 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id);
1816 -- Copy to the private declaration the attributes of the full view
1817 -- that need to be available for the partial view also.
1819 function Type_In_Use (T : Entity_Id) return Boolean;
1820 -- Check whether type or base type appear in an active use_type clause
1822 ------------------------------
1823 -- Preserve_Full_Attributes --
1824 ------------------------------
1826 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id) is
1827 Priv_Is_Base_Type : constant Boolean := Priv = Base_Type (Priv);
1829 begin
1830 Set_Size_Info (Priv, (Full));
1831 Set_RM_Size (Priv, RM_Size (Full));
1832 Set_Size_Known_At_Compile_Time (Priv, Size_Known_At_Compile_Time
1833 (Full));
1834 Set_Is_Volatile (Priv, Is_Volatile (Full));
1835 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
1836 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full));
1838 if Is_Unchecked_Union (Full) then
1839 Set_Is_Unchecked_Union (Base_Type (Priv));
1840 end if;
1841 -- Why is atomic not copied here ???
1843 if Referenced (Full) then
1844 Set_Referenced (Priv);
1845 end if;
1847 if Priv_Is_Base_Type then
1848 Set_Is_Controlled (Priv, Is_Controlled (Base_Type (Full)));
1849 Set_Finalize_Storage_Only (Priv, Finalize_Storage_Only
1850 (Base_Type (Full)));
1851 Set_Has_Task (Priv, Has_Task (Base_Type (Full)));
1852 Set_Has_Controlled_Component (Priv, Has_Controlled_Component
1853 (Base_Type (Full)));
1854 end if;
1856 Set_Freeze_Node (Priv, Freeze_Node (Full));
1858 if Is_Tagged_Type (Priv)
1859 and then Is_Tagged_Type (Full)
1860 and then not Error_Posted (Full)
1861 then
1862 if Priv_Is_Base_Type then
1864 -- Ada 2005 (AI-345): The full view of a type implementing
1865 -- an interface can be a task type.
1867 -- type T is new I with private;
1868 -- private
1869 -- task type T is new I with ...
1871 if Is_Interface (Etype (Priv))
1872 and then Is_Concurrent_Type (Base_Type (Full))
1873 then
1874 -- Protect the frontend against previous errors
1876 if Present (Corresponding_Record_Type
1877 (Base_Type (Full)))
1878 then
1879 Set_Access_Disp_Table
1880 (Priv, Access_Disp_Table
1881 (Corresponding_Record_Type (Base_Type (Full))));
1883 -- Generic context, or previous errors
1885 else
1886 null;
1887 end if;
1889 else
1890 Set_Access_Disp_Table
1891 (Priv, Access_Disp_Table (Base_Type (Full)));
1892 end if;
1893 end if;
1895 Set_First_Entity (Priv, First_Entity (Full));
1896 Set_Last_Entity (Priv, Last_Entity (Full));
1897 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
1898 end if;
1899 end Preserve_Full_Attributes;
1901 -----------------
1902 -- Type_In_Use --
1903 -----------------
1905 function Type_In_Use (T : Entity_Id) return Boolean is
1906 begin
1907 return Scope (Base_Type (T)) = P
1908 and then (In_Use (T) or else In_Use (Base_Type (T)));
1909 end Type_In_Use;
1911 -- Start of processing for Uninstall_Declarations
1913 begin
1914 Id := First_Entity (P);
1915 while Present (Id) and then Id /= First_Private_Entity (P) loop
1916 if Debug_Flag_E then
1917 Write_Str ("unlinking visible entity ");
1918 Write_Int (Int (Id));
1919 Write_Eol;
1920 end if;
1922 -- On exit from the package scope, we must preserve the visibility
1923 -- established by use clauses in the current scope. Two cases:
1925 -- a) If the entity is an operator, it may be a primitive operator of
1926 -- a type for which there is a visible use-type clause.
1928 -- b) for other entities, their use-visibility is determined by a
1929 -- visible use clause for the package itself. For a generic instance,
1930 -- the instantiation of the formals appears in the visible part,
1931 -- but the formals are private and remain so.
1933 if Ekind (Id) = E_Function
1934 and then Is_Operator_Symbol_Name (Chars (Id))
1935 and then not Is_Hidden (Id)
1936 and then not Error_Posted (Id)
1937 then
1938 Set_Is_Potentially_Use_Visible (Id,
1939 In_Use (P)
1940 or else Type_In_Use (Etype (Id))
1941 or else Type_In_Use (Etype (First_Formal (Id)))
1942 or else (Present (Next_Formal (First_Formal (Id)))
1943 and then
1944 Type_In_Use
1945 (Etype (Next_Formal (First_Formal (Id))))));
1946 else
1947 Set_Is_Potentially_Use_Visible (Id,
1948 In_Use (P) and not Is_Hidden (Id));
1949 end if;
1951 -- Local entities are not immediately visible outside of the package
1953 Set_Is_Immediately_Visible (Id, False);
1955 -- If this is a private type with a full view (for example a local
1956 -- subtype of a private type declared elsewhere), ensure that the
1957 -- full view is also removed from visibility: it may be exposed when
1958 -- swapping views in an instantiation.
1960 if Is_Type (Id)
1961 and then Present (Full_View (Id))
1962 then
1963 Set_Is_Immediately_Visible (Full_View (Id), False);
1964 end if;
1966 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
1967 Check_Abstract_Overriding (Id);
1968 Check_Conventions (Id);
1969 end if;
1971 if (Ekind (Id) = E_Private_Type
1972 or else Ekind (Id) = E_Limited_Private_Type)
1973 and then No (Full_View (Id))
1974 and then not Is_Generic_Type (Id)
1975 and then not Is_Derived_Type (Id)
1976 then
1977 Error_Msg_N ("missing full declaration for private type&", Id);
1979 elsif Ekind (Id) = E_Record_Type_With_Private
1980 and then not Is_Generic_Type (Id)
1981 and then No (Full_View (Id))
1982 then
1983 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
1984 Error_Msg_N ("missing full declaration for private type&", Id);
1985 else
1986 Error_Msg_N
1987 ("missing full declaration for private extension", Id);
1988 end if;
1990 elsif Ekind (Id) = E_Constant
1991 and then No (Constant_Value (Id))
1992 and then No (Full_View (Id))
1993 and then not Is_Imported (Id)
1994 and then (Nkind (Parent (Id)) /= N_Object_Declaration
1995 or else not No_Initialization (Parent (Id)))
1996 then
1997 if not Has_Private_Declaration (Etype (Id)) then
1999 -- We assume that the user did not not intend a deferred
2000 -- constant declaration, and the expression is just missing.
2002 Error_Msg_N
2003 ("constant declaration requires initialization expression",
2004 Parent (Id));
2006 if Is_Limited_Type (Etype (Id)) then
2007 Error_Msg_N
2008 ("\if variable intended, remove CONSTANT from declaration",
2009 Parent (Id));
2010 end if;
2012 else
2013 Error_Msg_N
2014 ("missing full declaration for deferred constant ('R'M 7.4)",
2015 Id);
2017 if Is_Limited_Type (Etype (Id)) then
2018 Error_Msg_N
2019 ("\if variable intended, remove CONSTANT from declaration",
2020 Parent (Id));
2021 end if;
2022 end if;
2023 end if;
2025 Next_Entity (Id);
2026 end loop;
2028 -- If the specification was installed as the parent of a public child
2029 -- unit, the private declarations were not installed, and there is
2030 -- nothing to do.
2032 if not In_Private_Part (P) then
2033 return;
2034 else
2035 Set_In_Private_Part (P, False);
2036 end if;
2038 -- Make private entities invisible and exchange full and private
2039 -- declarations for private types.
2041 while Present (Id) loop
2042 if Debug_Flag_E then
2043 Write_Str ("unlinking private entity ");
2044 Write_Int (Int (Id));
2045 Write_Eol;
2046 end if;
2048 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2049 Check_Abstract_Overriding (Id);
2050 Check_Conventions (Id);
2051 end if;
2053 Set_Is_Immediately_Visible (Id, False);
2055 if Is_Private_Base_Type (Id)
2056 and then Present (Full_View (Id))
2057 then
2058 Full := Full_View (Id);
2060 -- If the partial view is not declared in the visible part
2061 -- of the package (as is the case when it is a type derived
2062 -- from some other private type in the private part of the
2063 -- current package), no exchange takes place.
2065 if No (Parent (Id))
2066 or else List_Containing (Parent (Id))
2067 /= Visible_Declarations (Specification (Decl))
2068 then
2069 goto Next_Id;
2070 end if;
2072 -- The entry in the private part points to the full declaration,
2073 -- which is currently visible. Exchange them so only the private
2074 -- type declaration remains accessible, and link private and
2075 -- full declaration in the opposite direction. Before the actual
2076 -- exchange, we copy back attributes of the full view that
2077 -- must be available to the partial view too.
2079 Preserve_Full_Attributes (Id, Full);
2081 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
2083 if Is_Indefinite_Subtype (Full)
2084 and then not Is_Indefinite_Subtype (Id)
2085 then
2086 Error_Msg_N
2087 ("full view of type must be definite subtype", Full);
2088 end if;
2090 Priv_Elmt := First_Elmt (Private_Dependents (Id));
2092 -- Swap out the subtypes and derived types of Id that were
2093 -- compiled in this scope, or installed previously by
2094 -- Install_Private_Declarations.
2095 -- Before we do the swap, we verify the presence of the
2096 -- Full_View field which may be empty due to a swap by
2097 -- a previous call to End_Package_Scope (e.g. from the
2098 -- freezing mechanism).
2100 while Present (Priv_Elmt) loop
2101 Priv_Sub := Node (Priv_Elmt);
2103 if Present (Full_View (Priv_Sub)) then
2105 if Scope (Priv_Sub) = P
2106 or else not In_Open_Scopes (Scope (Priv_Sub))
2107 then
2108 Set_Is_Immediately_Visible (Priv_Sub, False);
2109 end if;
2111 if Is_Visible_Dependent (Priv_Sub) then
2112 Preserve_Full_Attributes
2113 (Priv_Sub, Full_View (Priv_Sub));
2114 Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub));
2115 Exchange_Declarations (Priv_Sub);
2116 end if;
2117 end if;
2119 Next_Elmt (Priv_Elmt);
2120 end loop;
2122 -- Now restore the type itself to its private view
2124 Exchange_Declarations (Id);
2126 elsif Ekind (Id) = E_Incomplete_Type
2127 and then No (Full_View (Id))
2128 then
2129 -- Mark Taft amendment types
2131 Set_Has_Completion_In_Body (Id);
2133 elsif not Is_Child_Unit (Id)
2134 and then (not Is_Private_Type (Id)
2135 or else No (Full_View (Id)))
2136 then
2137 Set_Is_Hidden (Id);
2138 Set_Is_Potentially_Use_Visible (Id, False);
2139 end if;
2141 <<Next_Id>>
2142 Next_Entity (Id);
2143 end loop;
2144 end Uninstall_Declarations;
2146 ------------------------
2147 -- Unit_Requires_Body --
2148 ------------------------
2150 function Unit_Requires_Body (P : Entity_Id) return Boolean is
2151 E : Entity_Id;
2153 begin
2154 -- Imported entity never requires body. Right now, only
2155 -- subprograms can be imported, but perhaps in the future
2156 -- we will allow import of packages.
2158 if Is_Imported (P) then
2159 return False;
2161 -- Body required if library package with pragma Elaborate_Body
2163 elsif Has_Pragma_Elaborate_Body (P) then
2164 return True;
2166 -- Body required if subprogram
2168 elsif Is_Subprogram (P) or else Is_Generic_Subprogram (P) then
2169 return True;
2171 -- Treat a block as requiring a body
2173 elsif Ekind (P) = E_Block then
2174 return True;
2176 elsif Ekind (P) = E_Package
2177 and then Nkind (Parent (P)) = N_Package_Specification
2178 and then Present (Generic_Parent (Parent (P)))
2179 then
2180 declare
2181 G_P : constant Entity_Id := Generic_Parent (Parent (P));
2182 begin
2183 if Has_Pragma_Elaborate_Body (G_P) then
2184 return True;
2185 end if;
2186 end;
2187 end if;
2189 -- Otherwise search entity chain for entity requiring completion
2191 E := First_Entity (P);
2192 while Present (E) loop
2194 -- Always ignore child units. Child units get added to the entity
2195 -- list of a parent unit, but are not original entities of the
2196 -- parent, and so do not affect whether the parent needs a body.
2198 if Is_Child_Unit (E) then
2199 null;
2201 -- Ignore formal packages and their renamings
2203 elsif Ekind (E) = E_Package
2204 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
2205 N_Formal_Package_Declaration
2206 then
2207 null;
2209 -- Otherwise test to see if entity requires a completion
2211 elsif (Is_Overloadable (E)
2212 and then Ekind (E) /= E_Enumeration_Literal
2213 and then Ekind (E) /= E_Operator
2214 and then not Is_Abstract (E)
2215 and then not Has_Completion (E))
2217 or else
2218 (Ekind (E) = E_Package
2219 and then E /= P
2220 and then not Has_Completion (E)
2221 and then Unit_Requires_Body (E))
2223 or else
2224 (Ekind (E) = E_Incomplete_Type and then No (Full_View (E)))
2226 or else
2227 ((Ekind (E) = E_Task_Type or else
2228 Ekind (E) = E_Protected_Type)
2229 and then not Has_Completion (E))
2231 or else
2232 (Ekind (E) = E_Generic_Package and then E /= P
2233 and then not Has_Completion (E)
2234 and then Unit_Requires_Body (E))
2236 or else
2237 (Is_Generic_Subprogram (E)
2238 and then not Has_Completion (E))
2240 then
2241 return True;
2243 -- Entity that does not require completion
2245 else
2246 null;
2247 end if;
2249 Next_Entity (E);
2250 end loop;
2252 return False;
2253 end Unit_Requires_Body;
2255 end Sem_Ch7;