Daily bump.
[official-gcc.git] / gcc / ada / sem_ch7.adb
blob326219d1fc64d1dba9b2051e221f7c2a0dd4c00d
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-2012, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 -- This package contains the routines to process package specifications and
27 -- bodies. The most important semantic aspects of package processing are the
28 -- handling of private and full declarations, and the construction of dispatch
29 -- tables for tagged types.
31 with Atree; use Atree;
32 with Debug; use Debug;
33 with Einfo; use Einfo;
34 with Elists; use Elists;
35 with Errout; use Errout;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Dist; use Exp_Dist;
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 Restrict; use Restrict;
47 with Sem; use Sem;
48 with Sem_Aux; use Sem_Aux;
49 with Sem_Cat; use Sem_Cat;
50 with Sem_Ch3; use Sem_Ch3;
51 with Sem_Ch6; use Sem_Ch6;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Ch10; use Sem_Ch10;
54 with Sem_Ch12; use Sem_Ch12;
55 with Sem_Ch13; use Sem_Ch13;
56 with Sem_Disp; use Sem_Disp;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Util; use Sem_Util;
59 with Sem_Warn; use Sem_Warn;
60 with Snames; use Snames;
61 with Stand; use Stand;
62 with Sinfo; use Sinfo;
63 with Sinput; use Sinput;
64 with Style;
65 with Uintp; use Uintp;
67 package body Sem_Ch7 is
69 -----------------------------------
70 -- Handling private declarations --
71 -----------------------------------
73 -- The principle that each entity has a single defining occurrence clashes
74 -- with the presence of two separate definitions for private types: the
75 -- first is the private type declaration, and the second is the full type
76 -- declaration. It is important that all references to the type point to
77 -- the same defining occurrence, namely the first one. To enforce the two
78 -- separate views of the entity, the corresponding information is swapped
79 -- between the two declarations. Outside of the package, the defining
80 -- occurrence only contains the private declaration information, while in
81 -- the private part and the body of the package the defining occurrence
82 -- contains the full declaration. To simplify the swap, the defining
83 -- occurrence that currently holds the private declaration points to the
84 -- full declaration. During semantic processing the defining occurrence
85 -- also points to a list of private dependents, that is to say access types
86 -- or composite types whose designated types or component types are
87 -- subtypes or derived types of the private type in question. After the
88 -- full declaration has been seen, the private dependents are updated to
89 -- indicate that they have full definitions.
91 -----------------------
92 -- Local Subprograms --
93 -----------------------
95 procedure Analyze_Package_Body_Helper (N : Node_Id);
96 -- Does all the real work of Analyze_Package_Body
98 procedure Check_Anonymous_Access_Types
99 (Spec_Id : Entity_Id;
100 P_Body : Node_Id);
101 -- If the spec of a package has a limited_with_clause, it may declare
102 -- anonymous access types whose designated type is a limited view, such an
103 -- anonymous access return type for a function. This access type cannot be
104 -- elaborated in the spec itself, but it may need an itype reference if it
105 -- is used within a nested scope. In that case the itype reference is
106 -- created at the beginning of the corresponding package body and inserted
107 -- before other body declarations.
109 procedure Install_Package_Entity (Id : Entity_Id);
110 -- Supporting procedure for Install_{Visible,Private}_Declarations. Places
111 -- one entity on its visibility chain, and recurses on the visible part if
112 -- the entity is an inner package.
114 function Is_Private_Base_Type (E : Entity_Id) return Boolean;
115 -- True for a private type that is not a subtype
117 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean;
118 -- If the private dependent is a private type whose full view is derived
119 -- from the parent type, its full properties are revealed only if we are in
120 -- the immediate scope of the private dependent. Should this predicate be
121 -- tightened further???
123 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id);
124 -- Called upon entering the private part of a public child package and the
125 -- body of a nested package, to potentially declare certain inherited
126 -- subprograms that were inherited by types in the visible part, but whose
127 -- declaration was deferred because the parent operation was private and
128 -- not visible at that point. These subprograms are located by traversing
129 -- the visible part declarations looking for non-private type extensions
130 -- and then examining each of the primitive operations of such types to
131 -- find those that were inherited but declared with a special internal
132 -- name. Each such operation is now declared as an operation with a normal
133 -- name (using the name of the parent operation) and replaces the previous
134 -- implicit operation in the primitive operations list of the type. If the
135 -- inherited private operation has been overridden, then it's replaced by
136 -- the overriding operation.
138 --------------------------
139 -- Analyze_Package_Body --
140 --------------------------
142 procedure Analyze_Package_Body (N : Node_Id) is
143 Loc : constant Source_Ptr := Sloc (N);
145 begin
146 if Debug_Flag_C then
147 Write_Str ("==> package body ");
148 Write_Name (Chars (Defining_Entity (N)));
149 Write_Str (" from ");
150 Write_Location (Loc);
151 Write_Eol;
152 Indent;
153 end if;
155 -- The real work is split out into the helper, so it can do "return;"
156 -- without skipping the debug output.
158 Analyze_Package_Body_Helper (N);
160 if Debug_Flag_C then
161 Outdent;
162 Write_Str ("<== package body ");
163 Write_Name (Chars (Defining_Entity (N)));
164 Write_Str (" from ");
165 Write_Location (Loc);
166 Write_Eol;
167 end if;
168 end Analyze_Package_Body;
170 ---------------------------------
171 -- Analyze_Package_Body_Helper --
172 ---------------------------------
174 procedure Analyze_Package_Body_Helper (N : Node_Id) is
175 HSS : Node_Id;
176 Body_Id : Entity_Id;
177 Spec_Id : Entity_Id;
178 Last_Spec_Entity : Entity_Id;
179 New_N : Node_Id;
180 Pack_Decl : Node_Id;
182 procedure Install_Composite_Operations (P : Entity_Id);
183 -- Composite types declared in the current scope may depend on types
184 -- that were private at the point of declaration, and whose full view
185 -- is now in scope. Indicate that the corresponding operations on the
186 -- composite type are available.
188 ----------------------------------
189 -- Install_Composite_Operations --
190 ----------------------------------
192 procedure Install_Composite_Operations (P : Entity_Id) is
193 Id : Entity_Id;
195 begin
196 Id := First_Entity (P);
197 while Present (Id) loop
198 if Is_Type (Id)
199 and then (Is_Limited_Composite (Id)
200 or else Is_Private_Composite (Id))
201 and then No (Private_Component (Id))
202 then
203 Set_Is_Limited_Composite (Id, False);
204 Set_Is_Private_Composite (Id, False);
205 end if;
207 Next_Entity (Id);
208 end loop;
209 end Install_Composite_Operations;
211 -- Start of processing for Analyze_Package_Body_Helper
213 begin
214 -- Find corresponding package specification, and establish the current
215 -- scope. The visible defining entity for the package is the defining
216 -- occurrence in the spec. On exit from the package body, all body
217 -- declarations are attached to the defining entity for the body, but
218 -- the later is never used for name resolution. In this fashion there
219 -- is only one visible entity that denotes the package.
221 -- Set Body_Id. Note that this Will be reset to point to the generic
222 -- copy later on in the generic case.
224 Body_Id := Defining_Entity (N);
226 if Present (Corresponding_Spec (N)) then
228 -- Body is body of package instantiation. Corresponding spec has
229 -- already been set.
231 Spec_Id := Corresponding_Spec (N);
232 Pack_Decl := Unit_Declaration_Node (Spec_Id);
234 else
235 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
237 if Present (Spec_Id)
238 and then Is_Package_Or_Generic_Package (Spec_Id)
239 then
240 Pack_Decl := Unit_Declaration_Node (Spec_Id);
242 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
243 Error_Msg_N ("cannot supply body for package renaming", N);
244 return;
246 elsif Present (Corresponding_Body (Pack_Decl)) then
247 Error_Msg_N ("redefinition of package body", N);
248 return;
249 end if;
251 else
252 Error_Msg_N ("missing specification for package body", N);
253 return;
254 end if;
256 if Is_Package_Or_Generic_Package (Spec_Id)
257 and then (Scope (Spec_Id) = Standard_Standard
258 or else Is_Child_Unit (Spec_Id))
259 and then not Unit_Requires_Body (Spec_Id)
260 then
261 if Ada_Version = Ada_83 then
262 Error_Msg_N
263 ("optional package body (not allowed in Ada 95)?", N);
264 else
265 Error_Msg_N ("spec of this package does not allow a body", N);
266 end if;
267 end if;
268 end if;
270 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
271 Style.Check_Identifier (Body_Id, Spec_Id);
273 if Is_Child_Unit (Spec_Id) then
274 if Nkind (Parent (N)) /= N_Compilation_Unit then
275 Error_Msg_NE
276 ("body of child unit& cannot be an inner package", N, Spec_Id);
277 end if;
279 Set_Is_Child_Unit (Body_Id);
280 end if;
282 -- Generic package case
284 if Ekind (Spec_Id) = E_Generic_Package then
286 -- Disable expansion and perform semantic analysis on copy. The
287 -- unannotated body will be used in all instantiations.
289 Body_Id := Defining_Entity (N);
290 Set_Ekind (Body_Id, E_Package_Body);
291 Set_Scope (Body_Id, Scope (Spec_Id));
292 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
293 Set_Body_Entity (Spec_Id, Body_Id);
294 Set_Spec_Entity (Body_Id, Spec_Id);
296 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
297 Rewrite (N, New_N);
299 -- Update Body_Id to point to the copied node for the remainder of
300 -- the processing.
302 Body_Id := Defining_Entity (N);
303 Start_Generic;
304 end if;
306 -- The Body_Id is that of the copied node in the generic case, the
307 -- current node otherwise. Note that N was rewritten above, so we must
308 -- be sure to get the latest Body_Id value.
310 Set_Ekind (Body_Id, E_Package_Body);
311 Set_Body_Entity (Spec_Id, Body_Id);
312 Set_Spec_Entity (Body_Id, Spec_Id);
314 -- Defining name for the package body is not a visible entity: Only the
315 -- defining name for the declaration is visible.
317 Set_Etype (Body_Id, Standard_Void_Type);
318 Set_Scope (Body_Id, Scope (Spec_Id));
319 Set_Corresponding_Spec (N, Spec_Id);
320 Set_Corresponding_Body (Pack_Decl, Body_Id);
322 -- The body entity is not used for semantics or code generation, but
323 -- it is attached to the entity list of the enclosing scope to simplify
324 -- the listing of back-annotations for the types it main contain.
326 if Scope (Spec_Id) /= Standard_Standard then
327 Append_Entity (Body_Id, Scope (Spec_Id));
328 end if;
330 -- Indicate that we are currently compiling the body of the package
332 Set_In_Package_Body (Spec_Id);
333 Set_Has_Completion (Spec_Id);
334 Last_Spec_Entity := Last_Entity (Spec_Id);
336 Push_Scope (Spec_Id);
338 Set_Categorization_From_Pragmas (N);
340 Install_Visible_Declarations (Spec_Id);
341 Install_Private_Declarations (Spec_Id);
342 Install_Private_With_Clauses (Spec_Id);
343 Install_Composite_Operations (Spec_Id);
345 Check_Anonymous_Access_Types (Spec_Id, N);
347 if Ekind (Spec_Id) = E_Generic_Package then
348 Set_Use (Generic_Formal_Declarations (Pack_Decl));
349 end if;
351 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
352 Set_Use (Private_Declarations (Specification (Pack_Decl)));
354 -- This is a nested package, so it may be necessary to declare certain
355 -- inherited subprograms that are not yet visible because the parent
356 -- type's subprograms are now visible.
358 if Ekind (Scope (Spec_Id)) = E_Package
359 and then Scope (Spec_Id) /= Standard_Standard
360 then
361 Declare_Inherited_Private_Subprograms (Spec_Id);
362 end if;
364 if Present (Declarations (N)) then
365 Analyze_Declarations (Declarations (N));
366 Inspect_Deferred_Constant_Completion (Declarations (N));
367 end if;
369 -- Analyze_Declarations has caused freezing of all types. Now generate
370 -- bodies for RACW primitives and stream attributes, if any.
372 if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then
374 -- Attach subprogram bodies to support RACWs declared in spec
376 Append_RACW_Bodies (Declarations (N), Spec_Id);
377 Analyze_List (Declarations (N));
378 end if;
380 HSS := Handled_Statement_Sequence (N);
382 if Present (HSS) then
383 Process_End_Label (HSS, 't', Spec_Id);
384 Analyze (HSS);
386 -- Check that elaboration code in a preelaborable package body is
387 -- empty other than null statements and labels (RM 10.2.1(6)).
389 Validate_Null_Statement_Sequence (N);
390 end if;
392 Validate_Categorization_Dependency (N, Spec_Id);
393 Check_Completion (Body_Id);
395 -- Generate start of body reference. Note that we do this fairly late,
396 -- because the call will use In_Extended_Main_Source_Unit as a check,
397 -- and we want to make sure that Corresponding_Stub links are set
399 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
401 -- For a generic package, collect global references and mark them on
402 -- the original body so that they are not resolved again at the point
403 -- of instantiation.
405 if Ekind (Spec_Id) /= E_Package then
406 Save_Global_References (Original_Node (N));
407 End_Generic;
408 end if;
410 -- The entities of the package body have so far been chained onto the
411 -- declaration chain for the spec. That's been fine while we were in the
412 -- body, since we wanted them to be visible, but now that we are leaving
413 -- the package body, they are no longer visible, so we remove them from
414 -- the entity chain of the package spec entity, and copy them to the
415 -- entity chain of the package body entity, where they will never again
416 -- be visible.
418 if Present (Last_Spec_Entity) then
419 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
420 Set_Next_Entity (Last_Spec_Entity, Empty);
421 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
422 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
424 else
425 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
426 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
427 Set_First_Entity (Spec_Id, Empty);
428 Set_Last_Entity (Spec_Id, Empty);
429 end if;
431 End_Package_Scope (Spec_Id);
433 -- All entities declared in body are not visible
435 declare
436 E : Entity_Id;
438 begin
439 E := First_Entity (Body_Id);
440 while Present (E) loop
441 Set_Is_Immediately_Visible (E, False);
442 Set_Is_Potentially_Use_Visible (E, False);
443 Set_Is_Hidden (E);
445 -- Child units may appear on the entity list (e.g. if they appear
446 -- in the context of a subunit) but they are not body entities.
448 if not Is_Child_Unit (E) then
449 Set_Is_Package_Body_Entity (E);
450 end if;
452 Next_Entity (E);
453 end loop;
454 end;
456 Check_References (Body_Id);
458 -- For a generic unit, check that the formal parameters are referenced,
459 -- and that local variables are used, as for regular packages.
461 if Ekind (Spec_Id) = E_Generic_Package then
462 Check_References (Spec_Id);
463 end if;
465 -- The processing so far has made all entities of the package body
466 -- public (i.e. externally visible to the linker). This is in general
467 -- necessary, since inlined or generic bodies, for which code is
468 -- generated in other units, may need to see these entities. The
469 -- following loop runs backwards from the end of the entities of the
470 -- package body making these entities invisible until we reach a
471 -- referencer, i.e. a declaration that could reference a previous
472 -- declaration, a generic body or an inlined body, or a stub (which may
473 -- contain either of these). This is of course an approximation, but it
474 -- is conservative and definitely correct.
476 -- We only do this at the outer (library) level non-generic packages.
477 -- The reason is simply to cut down on the number of global symbols
478 -- generated, which has a double effect: (1) to make the compilation
479 -- process more efficient and (2) to give the code generator more
480 -- freedom to optimize within each unit, especially subprograms.
482 if (Scope (Spec_Id) = Standard_Standard or else Is_Child_Unit (Spec_Id))
483 and then not Is_Generic_Unit (Spec_Id)
484 and then Present (Declarations (N))
485 then
486 Make_Non_Public_Where_Possible : declare
488 function Has_Referencer
489 (L : List_Id;
490 Outer : Boolean) return Boolean;
491 -- Traverse the given list of declarations in reverse order.
492 -- Return True if a referencer is present. Return False if none is
493 -- found. The Outer parameter is True for the outer level call and
494 -- False for inner level calls for nested packages. If Outer is
495 -- True, then any entities up to the point of hitting a referencer
496 -- get their Is_Public flag cleared, so that the entities will be
497 -- treated as static entities in the C sense, and need not have
498 -- fully qualified names. Furthermore, if the referencer is an
499 -- inlined subprogram that doesn't reference other subprograms,
500 -- we keep clearing the Is_Public flag on subprograms. For inner
501 -- levels, we need all names to be fully qualified to deal with
502 -- the same name appearing in parallel packages (right now this
503 -- is tied to their being external).
505 --------------------
506 -- Has_Referencer --
507 --------------------
509 function Has_Referencer
510 (L : List_Id;
511 Outer : Boolean) return Boolean
513 Has_Referencer_Except_For_Subprograms : Boolean := False;
515 D : Node_Id;
516 E : Entity_Id;
517 K : Node_Kind;
518 S : Entity_Id;
520 function Check_Subprogram_Ref (N : Node_Id)
521 return Traverse_Result;
522 -- Look for references to subprograms
524 --------------------------
525 -- Check_Subprogram_Ref --
526 --------------------------
528 function Check_Subprogram_Ref (N : Node_Id)
529 return Traverse_Result
531 V : Node_Id;
533 begin
534 -- Check name of procedure or function calls
536 if Nkind (N) in N_Subprogram_Call
537 and then Is_Entity_Name (Name (N))
538 then
539 return Abandon;
540 end if;
542 -- Check prefix of attribute references
544 if Nkind (N) = N_Attribute_Reference
545 and then Is_Entity_Name (Prefix (N))
546 and then Present (Entity (Prefix (N)))
547 and then Ekind (Entity (Prefix (N))) in Subprogram_Kind
548 then
549 return Abandon;
550 end if;
552 -- Check value of constants
554 if Nkind (N) = N_Identifier
555 and then Present (Entity (N))
556 and then Ekind (Entity (N)) = E_Constant
557 then
558 V := Constant_Value (Entity (N));
559 if Present (V)
560 and then not Compile_Time_Known_Value_Or_Aggr (V)
561 then
562 return Abandon;
563 end if;
564 end if;
566 return OK;
567 end Check_Subprogram_Ref;
569 function Check_Subprogram_Refs is
570 new Traverse_Func (Check_Subprogram_Ref);
572 -- Start of processing for Has_Referencer
574 begin
575 if No (L) then
576 return False;
577 end if;
579 D := Last (L);
580 while Present (D) loop
581 K := Nkind (D);
583 if K in N_Body_Stub then
584 return True;
586 -- Processing for subprogram bodies
588 elsif K = N_Subprogram_Body then
589 if Acts_As_Spec (D) then
590 E := Defining_Entity (D);
592 -- An inlined body acts as a referencer. Note also
593 -- that we never reset Is_Public for an inlined
594 -- subprogram. Gigi requires Is_Public to be set.
596 -- Note that we test Has_Pragma_Inline here rather
597 -- than Is_Inlined. We are compiling this for a
598 -- client, and it is the client who will decide if
599 -- actual inlining should occur, so we need to assume
600 -- that the procedure could be inlined for the purpose
601 -- of accessing global entities.
603 if Has_Pragma_Inline (E) then
604 if Outer
605 and then Check_Subprogram_Refs (D) = OK
606 then
607 Has_Referencer_Except_For_Subprograms := True;
608 else
609 return True;
610 end if;
611 else
612 Set_Is_Public (E, False);
613 end if;
615 else
616 E := Corresponding_Spec (D);
618 if Present (E) then
620 -- A generic subprogram body acts as a referencer
622 if Is_Generic_Unit (E) then
623 return True;
624 end if;
626 if Has_Pragma_Inline (E) or else Is_Inlined (E) then
627 if Outer
628 and then Check_Subprogram_Refs (D) = OK
629 then
630 Has_Referencer_Except_For_Subprograms := True;
631 else
632 return True;
633 end if;
634 end if;
635 end if;
636 end if;
638 -- Processing for package bodies
640 elsif K = N_Package_Body
641 and then Present (Corresponding_Spec (D))
642 then
643 E := Corresponding_Spec (D);
645 -- Generic package body is a referencer. It would seem
646 -- that we only have to consider generics that can be
647 -- exported, i.e. where the corresponding spec is the
648 -- spec of the current package, but because of nested
649 -- instantiations, a fully private generic body may
650 -- export other private body entities. Furthermore,
651 -- regardless of whether there was a previous inlined
652 -- subprogram, (an instantiation of) the generic package
653 -- may reference any entity declared before it.
655 if Is_Generic_Unit (E) then
656 return True;
658 -- For non-generic package body, recurse into body unless
659 -- this is an instance, we ignore instances since they
660 -- cannot have references that affect outer entities.
662 elsif not Is_Generic_Instance (E)
663 and then not Has_Referencer_Except_For_Subprograms
664 then
665 if Has_Referencer
666 (Declarations (D), Outer => False)
667 then
668 return True;
669 end if;
670 end if;
672 -- Processing for package specs, recurse into declarations.
673 -- Again we skip this for the case of generic instances.
675 elsif K = N_Package_Declaration
676 and then not Has_Referencer_Except_For_Subprograms
677 then
678 S := Specification (D);
680 if not Is_Generic_Unit (Defining_Entity (S)) then
681 if Has_Referencer
682 (Private_Declarations (S), Outer => False)
683 then
684 return True;
685 elsif Has_Referencer
686 (Visible_Declarations (S), Outer => False)
687 then
688 return True;
689 end if;
690 end if;
692 -- Objects and exceptions need not be public if we have not
693 -- encountered a referencer so far. We only reset the flag
694 -- for outer level entities that are not imported/exported,
695 -- and which have no interface name.
697 elsif Nkind_In (K, N_Object_Declaration,
698 N_Exception_Declaration,
699 N_Subprogram_Declaration)
700 then
701 E := Defining_Entity (D);
703 if Outer
704 and then (not Has_Referencer_Except_For_Subprograms
705 or else K = N_Subprogram_Declaration)
706 and then not Is_Imported (E)
707 and then not Is_Exported (E)
708 and then No (Interface_Name (E))
709 then
710 Set_Is_Public (E, False);
711 end if;
712 end if;
714 Prev (D);
715 end loop;
717 return Has_Referencer_Except_For_Subprograms;
718 end Has_Referencer;
720 -- Start of processing for Make_Non_Public_Where_Possible
722 begin
723 declare
724 Discard : Boolean;
725 pragma Warnings (Off, Discard);
727 begin
728 Discard := Has_Referencer (Declarations (N), Outer => True);
729 end;
730 end Make_Non_Public_Where_Possible;
731 end if;
733 -- If expander is not active, then here is where we turn off the
734 -- In_Package_Body flag, otherwise it is turned off at the end of the
735 -- corresponding expansion routine. If this is an instance body, we need
736 -- to qualify names of local entities, because the body may have been
737 -- compiled as a preliminary to another instantiation.
739 if not Expander_Active then
740 Set_In_Package_Body (Spec_Id, False);
742 if Is_Generic_Instance (Spec_Id)
743 and then Operating_Mode = Generate_Code
744 then
745 Qualify_Entity_Names (N);
746 end if;
747 end if;
748 end Analyze_Package_Body_Helper;
750 ---------------------------------
751 -- Analyze_Package_Declaration --
752 ---------------------------------
754 procedure Analyze_Package_Declaration (N : Node_Id) is
755 Id : constant Node_Id := Defining_Entity (N);
757 PF : Boolean;
758 -- True when in the context of a declared pure library unit
760 Body_Required : Boolean;
761 -- True when this package declaration requires a corresponding body
763 Comp_Unit : Boolean;
764 -- True when this package declaration is not a nested declaration
766 begin
767 -- Analye aspect specifications immediately, since we need to recognize
768 -- things like Pure early enough to diagnose violations during analysis.
770 if Has_Aspects (N) then
771 Analyze_Aspect_Specifications (N, Id);
772 end if;
774 -- Ada 2005 (AI-217): Check if the package has been erroneously named
775 -- in a limited-with clause of its own context. In this case the error
776 -- has been previously notified by Analyze_Context.
778 -- limited with Pkg; -- ERROR
779 -- package Pkg is ...
781 if From_With_Type (Id) then
782 return;
783 end if;
785 if Debug_Flag_C then
786 Write_Str ("==> package spec ");
787 Write_Name (Chars (Id));
788 Write_Str (" from ");
789 Write_Location (Sloc (N));
790 Write_Eol;
791 Indent;
792 end if;
794 Generate_Definition (Id);
795 Enter_Name (Id);
796 Set_Ekind (Id, E_Package);
797 Set_Etype (Id, Standard_Void_Type);
799 Push_Scope (Id);
801 PF := Is_Pure (Enclosing_Lib_Unit_Entity);
802 Set_Is_Pure (Id, PF);
804 Set_Categorization_From_Pragmas (N);
806 Analyze (Specification (N));
807 Validate_Categorization_Dependency (N, Id);
809 Body_Required := Unit_Requires_Body (Id);
811 -- When this spec does not require an explicit body, we know that there
812 -- are no entities requiring completion in the language sense; we call
813 -- Check_Completion here only to ensure that any nested package
814 -- declaration that requires an implicit body gets one. (In the case
815 -- where a body is required, Check_Completion is called at the end of
816 -- the body's declarative part.)
818 if not Body_Required then
819 Check_Completion;
820 end if;
822 Comp_Unit := Nkind (Parent (N)) = N_Compilation_Unit;
823 if Comp_Unit then
825 -- Set Body_Required indication on the compilation unit node, and
826 -- determine whether elaboration warnings may be meaningful on it.
828 Set_Body_Required (Parent (N), Body_Required);
830 if not Body_Required then
831 Set_Suppress_Elaboration_Warnings (Id);
832 end if;
834 end if;
836 End_Package_Scope (Id);
838 -- For the declaration of a library unit that is a remote types package,
839 -- check legality rules regarding availability of stream attributes for
840 -- types that contain non-remote access values. This subprogram performs
841 -- visibility tests that rely on the fact that we have exited the scope
842 -- of Id.
844 if Comp_Unit then
845 Validate_RT_RAT_Component (N);
846 end if;
848 if Debug_Flag_C then
849 Outdent;
850 Write_Str ("<== package spec ");
851 Write_Name (Chars (Id));
852 Write_Str (" from ");
853 Write_Location (Sloc (N));
854 Write_Eol;
855 end if;
856 end Analyze_Package_Declaration;
858 -----------------------------------
859 -- Analyze_Package_Specification --
860 -----------------------------------
862 -- Note that this code is shared for the analysis of generic package specs
863 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
865 procedure Analyze_Package_Specification (N : Node_Id) is
866 Id : constant Entity_Id := Defining_Entity (N);
867 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
868 Vis_Decls : constant List_Id := Visible_Declarations (N);
869 Priv_Decls : constant List_Id := Private_Declarations (N);
870 E : Entity_Id;
871 L : Entity_Id;
872 Public_Child : Boolean;
874 Private_With_Clauses_Installed : Boolean := False;
875 -- In Ada 2005, private with_clauses are visible in the private part
876 -- of a nested package, even if it appears in the public part of the
877 -- enclosing package. This requires a separate step to install these
878 -- private_with_clauses, and remove them at the end of the nested
879 -- package.
881 procedure Check_One_Tagged_Type_Or_Extension_At_Most;
882 -- Issue an error in SPARK mode if a package specification contains
883 -- more than one tagged type or type extension.
885 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
886 -- Clears constant indications (Never_Set_In_Source, Constant_Value, and
887 -- Is_True_Constant) on all variables that are entities of Id, and on
888 -- the chain whose first element is FE. A recursive call is made for all
889 -- packages and generic packages.
891 procedure Generate_Parent_References;
892 -- For a child unit, generate references to parent units, for
893 -- GPS navigation purposes.
895 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
896 -- Child and Unit are entities of compilation units. True if Child
897 -- is a public child of Parent as defined in 10.1.1
899 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
900 -- Reject completion of an incomplete or private type declarations
901 -- having a known discriminant part by an unchecked union.
903 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
904 -- Given the package entity of a generic package instantiation or
905 -- formal package whose corresponding generic is a child unit, installs
906 -- the private declarations of each of the child unit's parents.
907 -- This has to be done at the point of entering the instance package's
908 -- private part rather than being done in Sem_Ch12.Install_Parent
909 -- (which is where the parents' visible declarations are installed).
911 ------------------------------------------------
912 -- Check_One_Tagged_Type_Or_Extension_At_Most --
913 ------------------------------------------------
915 procedure Check_One_Tagged_Type_Or_Extension_At_Most is
916 Previous : Node_Id;
918 procedure Check_Decls (Decls : List_Id);
919 -- Check that either Previous is Empty and Decls does not contain
920 -- more than one tagged type or type extension, or Previous is
921 -- already set and Decls contains no tagged type or type extension.
923 -----------------
924 -- Check_Decls --
925 -----------------
927 procedure Check_Decls (Decls : List_Id) is
928 Decl : Node_Id;
930 begin
931 Decl := First (Decls);
932 while Present (Decl) loop
933 if Nkind (Decl) = N_Full_Type_Declaration
934 and then Is_Tagged_Type (Defining_Identifier (Decl))
935 then
936 if No (Previous) then
937 Previous := Decl;
939 else
940 Error_Msg_Sloc := Sloc (Previous);
941 Check_SPARK_Restriction
942 ("at most one tagged type or type extension allowed",
943 "\\ previous declaration#",
944 Decl);
945 end if;
946 end if;
948 Next (Decl);
949 end loop;
950 end Check_Decls;
952 -- Start of processing for Check_One_Tagged_Type_Or_Extension_At_Most
954 begin
955 Previous := Empty;
956 Check_Decls (Vis_Decls);
958 if Present (Priv_Decls) then
959 Check_Decls (Priv_Decls);
960 end if;
961 end Check_One_Tagged_Type_Or_Extension_At_Most;
963 ---------------------
964 -- Clear_Constants --
965 ---------------------
967 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
968 E : Entity_Id;
970 begin
971 -- Ignore package renamings, not interesting and they can cause self
972 -- referential loops in the code below.
974 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
975 return;
976 end if;
978 -- Note: in the loop below, the check for Next_Entity pointing back
979 -- to the package entity may seem odd, but it is needed, because a
980 -- package can contain a renaming declaration to itself, and such
981 -- renamings are generated automatically within package instances.
983 E := FE;
984 while Present (E) and then E /= Id loop
985 if Is_Assignable (E) then
986 Set_Never_Set_In_Source (E, False);
987 Set_Is_True_Constant (E, False);
988 Set_Current_Value (E, Empty);
989 Set_Is_Known_Null (E, False);
990 Set_Last_Assignment (E, Empty);
992 if not Can_Never_Be_Null (E) then
993 Set_Is_Known_Non_Null (E, False);
994 end if;
996 elsif Is_Package_Or_Generic_Package (E) then
997 Clear_Constants (E, First_Entity (E));
998 Clear_Constants (E, First_Private_Entity (E));
999 end if;
1001 Next_Entity (E);
1002 end loop;
1003 end Clear_Constants;
1005 --------------------------------
1006 -- Generate_Parent_References --
1007 --------------------------------
1009 procedure Generate_Parent_References is
1010 Decl : constant Node_Id := Parent (N);
1012 begin
1013 if Id = Cunit_Entity (Main_Unit)
1014 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
1015 then
1016 Generate_Reference (Id, Scope (Id), 'k', False);
1018 elsif not Nkind_In (Unit (Cunit (Main_Unit)), N_Subprogram_Body,
1019 N_Subunit)
1020 then
1021 -- If current unit is an ancestor of main unit, generate a
1022 -- reference to its own parent.
1024 declare
1025 U : Node_Id;
1026 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
1028 begin
1029 if Nkind (Main_Spec) = N_Package_Body then
1030 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
1031 end if;
1033 U := Parent_Spec (Main_Spec);
1034 while Present (U) loop
1035 if U = Parent (Decl) then
1036 Generate_Reference (Id, Scope (Id), 'k', False);
1037 exit;
1039 elsif Nkind (Unit (U)) = N_Package_Body then
1040 exit;
1042 else
1043 U := Parent_Spec (Unit (U));
1044 end if;
1045 end loop;
1046 end;
1047 end if;
1048 end Generate_Parent_References;
1050 ---------------------
1051 -- Is_Public_Child --
1052 ---------------------
1054 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
1055 begin
1056 if not Is_Private_Descendant (Child) then
1057 return True;
1058 else
1059 if Child = Unit then
1060 return not Private_Present (
1061 Parent (Unit_Declaration_Node (Child)));
1062 else
1063 return Is_Public_Child (Scope (Child), Unit);
1064 end if;
1065 end if;
1066 end Is_Public_Child;
1068 ----------------------------------------
1069 -- Inspect_Unchecked_Union_Completion --
1070 ----------------------------------------
1072 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
1073 Decl : Node_Id;
1075 begin
1076 Decl := First (Decls);
1077 while Present (Decl) loop
1079 -- We are looking at an incomplete or private type declaration
1080 -- with a known_discriminant_part whose full view is an
1081 -- Unchecked_Union.
1083 if Nkind_In (Decl, N_Incomplete_Type_Declaration,
1084 N_Private_Type_Declaration)
1085 and then Has_Discriminants (Defining_Identifier (Decl))
1086 and then Present (Full_View (Defining_Identifier (Decl)))
1087 and then
1088 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
1089 then
1090 Error_Msg_N
1091 ("completion of discriminated partial view "
1092 & "cannot be an unchecked union",
1093 Full_View (Defining_Identifier (Decl)));
1094 end if;
1096 Next (Decl);
1097 end loop;
1098 end Inspect_Unchecked_Union_Completion;
1100 -----------------------------------------
1101 -- Install_Parent_Private_Declarations --
1102 -----------------------------------------
1104 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
1105 Inst_Par : Entity_Id;
1106 Gen_Par : Entity_Id;
1107 Inst_Node : Node_Id;
1109 begin
1110 Inst_Par := Inst_Id;
1112 Gen_Par :=
1113 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
1114 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
1115 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
1117 if Nkind_In (Inst_Node, N_Package_Instantiation,
1118 N_Formal_Package_Declaration)
1119 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
1120 then
1121 Inst_Par := Entity (Prefix (Name (Inst_Node)));
1123 if Present (Renamed_Entity (Inst_Par)) then
1124 Inst_Par := Renamed_Entity (Inst_Par);
1125 end if;
1127 Gen_Par :=
1128 Generic_Parent
1129 (Specification (Unit_Declaration_Node (Inst_Par)));
1131 -- Install the private declarations and private use clauses
1132 -- of a parent instance of the child instance, unless the
1133 -- parent instance private declarations have already been
1134 -- installed earlier in Analyze_Package_Specification, which
1135 -- happens when a generic child is instantiated, and the
1136 -- instance is a child of the parent instance.
1138 -- Installing the use clauses of the parent instance twice
1139 -- is both unnecessary and wrong, because it would cause the
1140 -- clauses to be chained to themselves in the use clauses
1141 -- list of the scope stack entry. That in turn would cause
1142 -- an endless loop from End_Use_Clauses upon scope exit.
1144 -- The parent is now fully visible. It may be a hidden open
1145 -- scope if we are currently compiling some child instance
1146 -- declared within it, but while the current instance is being
1147 -- compiled the parent is immediately visible. In particular
1148 -- its entities must remain visible if a stack save/restore
1149 -- takes place through a call to Rtsfind.
1151 if Present (Gen_Par) then
1152 if not In_Private_Part (Inst_Par) then
1153 Install_Private_Declarations (Inst_Par);
1154 Set_Use (Private_Declarations
1155 (Specification
1156 (Unit_Declaration_Node (Inst_Par))));
1157 Set_Is_Hidden_Open_Scope (Inst_Par, False);
1158 end if;
1160 -- If we've reached the end of the generic instance parents,
1161 -- then finish off by looping through the nongeneric parents
1162 -- and installing their private declarations.
1164 else
1165 while Present (Inst_Par)
1166 and then Inst_Par /= Standard_Standard
1167 and then (not In_Open_Scopes (Inst_Par)
1168 or else not In_Private_Part (Inst_Par))
1169 loop
1170 Install_Private_Declarations (Inst_Par);
1171 Set_Use (Private_Declarations
1172 (Specification
1173 (Unit_Declaration_Node (Inst_Par))));
1174 Inst_Par := Scope (Inst_Par);
1175 end loop;
1177 exit;
1178 end if;
1180 else
1181 exit;
1182 end if;
1183 end loop;
1184 end Install_Parent_Private_Declarations;
1186 -- Start of processing for Analyze_Package_Specification
1188 begin
1189 if Present (Vis_Decls) then
1190 Analyze_Declarations (Vis_Decls);
1191 end if;
1193 -- Verify that incomplete types have received full declarations and
1194 -- also build invariant procedures for any types with invariants.
1196 E := First_Entity (Id);
1197 while Present (E) loop
1199 -- Check on incomplete types
1201 -- AI05-0213: A formal incomplete type has no completion
1203 if Ekind (E) = E_Incomplete_Type
1204 and then No (Full_View (E))
1205 and then not Is_Generic_Type (E)
1206 then
1207 Error_Msg_N ("no declaration in visible part for incomplete}", E);
1208 end if;
1210 -- Build invariant procedures
1212 if Is_Type (E) and then Has_Invariants (E) then
1213 Build_Invariant_Procedure (E, N);
1214 end if;
1216 Next_Entity (E);
1217 end loop;
1219 if Is_Remote_Call_Interface (Id)
1220 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1221 then
1222 Validate_RCI_Declarations (Id);
1223 end if;
1225 -- Save global references in the visible declarations, before installing
1226 -- private declarations of parent unit if there is one, because the
1227 -- privacy status of types defined in the parent will change. This is
1228 -- only relevant for generic child units, but is done in all cases for
1229 -- uniformity.
1231 if Ekind (Id) = E_Generic_Package
1232 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1233 then
1234 declare
1235 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1236 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1237 begin
1238 Set_Private_Declarations (Orig_Spec, Empty_List);
1239 Save_Global_References (Orig_Decl);
1240 Set_Private_Declarations (Orig_Spec, Save_Priv);
1241 end;
1242 end if;
1244 -- If package is a public child unit, then make the private declarations
1245 -- of the parent visible.
1247 Public_Child := False;
1249 declare
1250 Par : Entity_Id;
1251 Pack_Decl : Node_Id;
1252 Par_Spec : Node_Id;
1254 begin
1255 Par := Id;
1256 Par_Spec := Parent_Spec (Parent (N));
1258 -- If the package is formal package of an enclosing generic, it is
1259 -- transformed into a local generic declaration, and compiled to make
1260 -- its spec available. We need to retrieve the original generic to
1261 -- determine whether it is a child unit, and install its parents.
1263 if No (Par_Spec)
1264 and then
1265 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1266 then
1267 Par := Entity (Name (Original_Node (Parent (N))));
1268 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1269 end if;
1271 if Present (Par_Spec) then
1272 Generate_Parent_References;
1274 while Scope (Par) /= Standard_Standard
1275 and then Is_Public_Child (Id, Par)
1276 and then In_Open_Scopes (Par)
1277 loop
1278 Public_Child := True;
1279 Par := Scope (Par);
1280 Install_Private_Declarations (Par);
1281 Install_Private_With_Clauses (Par);
1282 Pack_Decl := Unit_Declaration_Node (Par);
1283 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1284 end loop;
1285 end if;
1286 end;
1288 if Is_Compilation_Unit (Id) then
1289 Install_Private_With_Clauses (Id);
1290 else
1292 -- The current compilation unit may include private with_clauses,
1293 -- which are visible in the private part of the current nested
1294 -- package, and have to be installed now. This is not done for
1295 -- nested instantiations, where the private with_clauses of the
1296 -- enclosing unit have no effect once the instantiation info is
1297 -- established and we start analyzing the package declaration.
1299 declare
1300 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1301 begin
1302 if Is_Package_Or_Generic_Package (Comp_Unit)
1303 and then not In_Private_Part (Comp_Unit)
1304 and then not In_Instance
1305 then
1306 Install_Private_With_Clauses (Comp_Unit);
1307 Private_With_Clauses_Installed := True;
1308 end if;
1309 end;
1310 end if;
1312 -- If this is a package associated with a generic instance or formal
1313 -- package, then the private declarations of each of the generic's
1314 -- parents must be installed at this point.
1316 if Is_Generic_Instance (Id) then
1317 Install_Parent_Private_Declarations (Id);
1318 end if;
1320 -- Analyze private part if present. The flag In_Private_Part is reset
1321 -- in End_Package_Scope.
1323 L := Last_Entity (Id);
1325 if Present (Priv_Decls) then
1326 Set_In_Private_Part (Id);
1328 -- Upon entering a public child's private part, it may be necessary
1329 -- to declare subprograms that were derived in the package's visible
1330 -- part but not yet made visible.
1332 if Public_Child then
1333 Declare_Inherited_Private_Subprograms (Id);
1334 end if;
1336 Analyze_Declarations (Priv_Decls);
1338 -- Check the private declarations for incomplete deferred constants
1340 Inspect_Deferred_Constant_Completion (Priv_Decls);
1342 -- The first private entity is the immediate follower of the last
1343 -- visible entity, if there was one.
1345 if Present (L) then
1346 Set_First_Private_Entity (Id, Next_Entity (L));
1347 else
1348 Set_First_Private_Entity (Id, First_Entity (Id));
1349 end if;
1351 -- There may be inherited private subprograms that need to be declared,
1352 -- even in the absence of an explicit private part. If there are any
1353 -- public declarations in the package and the package is a public child
1354 -- unit, then an implicit private part is assumed.
1356 elsif Present (L) and then Public_Child then
1357 Set_In_Private_Part (Id);
1358 Declare_Inherited_Private_Subprograms (Id);
1359 Set_First_Private_Entity (Id, Next_Entity (L));
1360 end if;
1362 E := First_Entity (Id);
1363 while Present (E) loop
1365 -- Check rule of 3.6(11), which in general requires waiting till all
1366 -- full types have been seen.
1368 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1369 Check_Aliased_Component_Types (E);
1370 end if;
1372 -- Check preelaborable initialization for full type completing a
1373 -- private type for which pragma Preelaborable_Initialization given.
1375 if Is_Type (E)
1376 and then Must_Have_Preelab_Init (E)
1377 and then not Has_Preelaborable_Initialization (E)
1378 then
1379 Error_Msg_N
1380 ("full view of & does not have preelaborable initialization", E);
1381 end if;
1383 -- An invariant may appear on a full view of a type
1385 if Is_Type (E)
1386 and then Has_Private_Declaration (E)
1387 and then Nkind (Parent (E)) = N_Full_Type_Declaration
1388 and then Has_Aspects (Parent (E))
1389 then
1390 Build_Invariant_Procedure (E, N);
1391 end if;
1393 Next_Entity (E);
1394 end loop;
1396 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1397 -- declaration having a known_discriminant_part shall not be an
1398 -- unchecked union type.
1400 if Present (Vis_Decls) then
1401 Inspect_Unchecked_Union_Completion (Vis_Decls);
1402 end if;
1404 if Present (Priv_Decls) then
1405 Inspect_Unchecked_Union_Completion (Priv_Decls);
1406 end if;
1408 if Ekind (Id) = E_Generic_Package
1409 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1410 and then Present (Priv_Decls)
1411 then
1412 -- Save global references in private declarations, ignoring the
1413 -- visible declarations that were processed earlier.
1415 declare
1416 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1417 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1418 Save_Form : constant List_Id :=
1419 Generic_Formal_Declarations (Orig_Decl);
1421 begin
1422 Set_Visible_Declarations (Orig_Spec, Empty_List);
1423 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1424 Save_Global_References (Orig_Decl);
1425 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1426 Set_Visible_Declarations (Orig_Spec, Save_Vis);
1427 end;
1428 end if;
1430 Process_End_Label (N, 'e', Id);
1432 -- Remove private_with_clauses of enclosing compilation unit, if they
1433 -- were installed.
1435 if Private_With_Clauses_Installed then
1436 Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
1437 end if;
1439 -- For the case of a library level package, we must go through all the
1440 -- entities clearing the indications that the value may be constant and
1441 -- not modified. Why? Because any client of this package may modify
1442 -- these values freely from anywhere. This also applies to any nested
1443 -- packages or generic packages.
1445 -- For now we unconditionally clear constants for packages that are
1446 -- instances of generic packages. The reason is that we do not have the
1447 -- body yet, and we otherwise think things are unreferenced when they
1448 -- are not. This should be fixed sometime (the effect is not terrible,
1449 -- we just lose some warnings, and also some cases of value propagation)
1450 -- ???
1452 if Is_Library_Level_Entity (Id)
1453 or else Is_Generic_Instance (Id)
1454 then
1455 Clear_Constants (Id, First_Entity (Id));
1456 Clear_Constants (Id, First_Private_Entity (Id));
1457 end if;
1459 Check_One_Tagged_Type_Or_Extension_At_Most;
1460 end Analyze_Package_Specification;
1462 --------------------------------------
1463 -- Analyze_Private_Type_Declaration --
1464 --------------------------------------
1466 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1467 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
1468 Id : constant Entity_Id := Defining_Identifier (N);
1470 begin
1471 Generate_Definition (Id);
1472 Set_Is_Pure (Id, PF);
1473 Init_Size_Align (Id);
1475 if not Is_Package_Or_Generic_Package (Current_Scope)
1476 or else In_Private_Part (Current_Scope)
1477 then
1478 Error_Msg_N ("invalid context for private declaration", N);
1479 end if;
1481 New_Private_Type (N, Id, N);
1482 Set_Depends_On_Private (Id);
1484 if Has_Aspects (N) then
1485 Analyze_Aspect_Specifications (N, Id);
1486 end if;
1487 end Analyze_Private_Type_Declaration;
1489 ----------------------------------
1490 -- Check_Anonymous_Access_Types --
1491 ----------------------------------
1493 procedure Check_Anonymous_Access_Types
1494 (Spec_Id : Entity_Id;
1495 P_Body : Node_Id)
1497 E : Entity_Id;
1498 IR : Node_Id;
1500 begin
1501 -- Itype references are only needed by gigi, to force elaboration of
1502 -- itypes. In the absence of code generation, they are not needed.
1504 if not Expander_Active then
1505 return;
1506 end if;
1508 E := First_Entity (Spec_Id);
1509 while Present (E) loop
1510 if Ekind (E) = E_Anonymous_Access_Type
1511 and then From_With_Type (E)
1512 then
1513 IR := Make_Itype_Reference (Sloc (P_Body));
1514 Set_Itype (IR, E);
1516 if No (Declarations (P_Body)) then
1517 Set_Declarations (P_Body, New_List (IR));
1518 else
1519 Prepend (IR, Declarations (P_Body));
1520 end if;
1521 end if;
1523 Next_Entity (E);
1524 end loop;
1525 end Check_Anonymous_Access_Types;
1527 -------------------------------------------
1528 -- Declare_Inherited_Private_Subprograms --
1529 -------------------------------------------
1531 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1533 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1534 -- Check whether an inherited subprogram S is an operation of an
1535 -- untagged derived type T.
1537 ---------------------
1538 -- Is_Primitive_Of --
1539 ---------------------
1541 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1542 Formal : Entity_Id;
1544 begin
1545 -- If the full view is a scalar type, the type is the anonymous base
1546 -- type, but the operation mentions the first subtype, so check the
1547 -- signature against the base type.
1549 if Base_Type (Etype (S)) = Base_Type (T) then
1550 return True;
1552 else
1553 Formal := First_Formal (S);
1554 while Present (Formal) loop
1555 if Base_Type (Etype (Formal)) = Base_Type (T) then
1556 return True;
1557 end if;
1559 Next_Formal (Formal);
1560 end loop;
1562 return False;
1563 end if;
1564 end Is_Primitive_Of;
1566 -- Local variables
1568 E : Entity_Id;
1569 Op_List : Elist_Id;
1570 Op_Elmt : Elmt_Id;
1571 Op_Elmt_2 : Elmt_Id;
1572 Prim_Op : Entity_Id;
1573 New_Op : Entity_Id := Empty;
1574 Parent_Subp : Entity_Id;
1575 Tag : Entity_Id;
1577 -- Start of processing for Declare_Inherited_Private_Subprograms
1579 begin
1580 E := First_Entity (Id);
1581 while Present (E) loop
1583 -- If the entity is a nonprivate type extension whose parent type
1584 -- is declared in an open scope, then the type may have inherited
1585 -- operations that now need to be made visible. Ditto if the entity
1586 -- is a formal derived type in a child unit.
1588 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
1589 or else
1590 (Nkind (Parent (E)) = N_Private_Extension_Declaration
1591 and then Is_Generic_Type (E)))
1592 and then In_Open_Scopes (Scope (Etype (E)))
1593 and then Is_Base_Type (E)
1594 then
1595 if Is_Tagged_Type (E) then
1596 Op_List := Primitive_Operations (E);
1597 New_Op := Empty;
1598 Tag := First_Tag_Component (E);
1600 Op_Elmt := First_Elmt (Op_List);
1601 while Present (Op_Elmt) loop
1602 Prim_Op := Node (Op_Elmt);
1604 -- Search primitives that are implicit operations with an
1605 -- internal name whose parent operation has a normal name.
1607 if Present (Alias (Prim_Op))
1608 and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
1609 and then not Comes_From_Source (Prim_Op)
1610 and then Is_Internal_Name (Chars (Prim_Op))
1611 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1612 then
1613 Parent_Subp := Alias (Prim_Op);
1615 -- Case 1: Check if the type has also an explicit
1616 -- overriding for this primitive.
1618 Op_Elmt_2 := Next_Elmt (Op_Elmt);
1619 while Present (Op_Elmt_2) loop
1621 -- Skip entities with attribute Interface_Alias since
1622 -- they are not overriding primitives (these entities
1623 -- link an interface primitive with their covering
1624 -- primitive)
1626 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
1627 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
1628 and then No (Interface_Alias (Node (Op_Elmt_2)))
1629 then
1630 -- The private inherited operation has been
1631 -- overridden by an explicit subprogram: replace
1632 -- the former by the latter.
1634 New_Op := Node (Op_Elmt_2);
1635 Replace_Elmt (Op_Elmt, New_Op);
1636 Remove_Elmt (Op_List, Op_Elmt_2);
1637 Set_Overridden_Operation (New_Op, Parent_Subp);
1639 -- We don't need to inherit its dispatching slot.
1640 -- Set_All_DT_Position has previously ensured that
1641 -- the same slot was assigned to the two primitives
1643 if Present (Tag)
1644 and then Present (DTC_Entity (New_Op))
1645 and then Present (DTC_Entity (Prim_Op))
1646 then
1647 pragma Assert (DT_Position (New_Op)
1648 = DT_Position (Prim_Op));
1649 null;
1650 end if;
1652 goto Next_Primitive;
1653 end if;
1655 Next_Elmt (Op_Elmt_2);
1656 end loop;
1658 -- Case 2: We have not found any explicit overriding and
1659 -- hence we need to declare the operation (i.e., make it
1660 -- visible).
1662 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1664 -- Inherit the dispatching slot if E is already frozen
1666 if Is_Frozen (E)
1667 and then Present (DTC_Entity (Alias (Prim_Op)))
1668 then
1669 Set_DTC_Entity_Value (E, New_Op);
1670 Set_DT_Position (New_Op,
1671 DT_Position (Alias (Prim_Op)));
1672 end if;
1674 pragma Assert
1675 (Is_Dispatching_Operation (New_Op)
1676 and then Node (Last_Elmt (Op_List)) = New_Op);
1678 -- Substitute the new operation for the old one in the
1679 -- type's primitive operations list. Since the new
1680 -- operation was also just added to the end of list,
1681 -- the last element must be removed.
1683 -- (Question: is there a simpler way of declaring the
1684 -- operation, say by just replacing the name of the
1685 -- earlier operation, reentering it in the in the symbol
1686 -- table (how?), and marking it as private???)
1688 Replace_Elmt (Op_Elmt, New_Op);
1689 Remove_Last_Elmt (Op_List);
1690 end if;
1692 <<Next_Primitive>>
1693 Next_Elmt (Op_Elmt);
1694 end loop;
1696 -- Generate listing showing the contents of the dispatch table
1698 if Debug_Flag_ZZ then
1699 Write_DT (E);
1700 end if;
1702 else
1703 -- Non-tagged type, scan forward to locate inherited hidden
1704 -- operations.
1706 Prim_Op := Next_Entity (E);
1707 while Present (Prim_Op) loop
1708 if Is_Subprogram (Prim_Op)
1709 and then Present (Alias (Prim_Op))
1710 and then not Comes_From_Source (Prim_Op)
1711 and then Is_Internal_Name (Chars (Prim_Op))
1712 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1713 and then Is_Primitive_Of (E, Prim_Op)
1714 then
1715 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1716 end if;
1718 Next_Entity (Prim_Op);
1719 end loop;
1720 end if;
1721 end if;
1723 Next_Entity (E);
1724 end loop;
1725 end Declare_Inherited_Private_Subprograms;
1727 -----------------------
1728 -- End_Package_Scope --
1729 -----------------------
1731 procedure End_Package_Scope (P : Entity_Id) is
1732 begin
1733 Uninstall_Declarations (P);
1734 Pop_Scope;
1735 end End_Package_Scope;
1737 ---------------------------
1738 -- Exchange_Declarations --
1739 ---------------------------
1741 procedure Exchange_Declarations (Id : Entity_Id) is
1742 Full_Id : constant Entity_Id := Full_View (Id);
1743 H1 : constant Entity_Id := Homonym (Id);
1744 Next1 : constant Entity_Id := Next_Entity (Id);
1745 H2 : Entity_Id;
1746 Next2 : Entity_Id;
1748 begin
1749 -- If missing full declaration for type, nothing to exchange
1751 if No (Full_Id) then
1752 return;
1753 end if;
1755 -- Otherwise complete the exchange, and preserve semantic links
1757 Next2 := Next_Entity (Full_Id);
1758 H2 := Homonym (Full_Id);
1760 -- Reset full declaration pointer to reflect the switched entities and
1761 -- readjust the next entity chains.
1763 Exchange_Entities (Id, Full_Id);
1765 Set_Next_Entity (Id, Next1);
1766 Set_Homonym (Id, H1);
1768 Set_Full_View (Full_Id, Id);
1769 Set_Next_Entity (Full_Id, Next2);
1770 Set_Homonym (Full_Id, H2);
1771 end Exchange_Declarations;
1773 ----------------------------
1774 -- Install_Package_Entity --
1775 ----------------------------
1777 procedure Install_Package_Entity (Id : Entity_Id) is
1778 begin
1779 if not Is_Internal (Id) then
1780 if Debug_Flag_E then
1781 Write_Str ("Install: ");
1782 Write_Name (Chars (Id));
1783 Write_Eol;
1784 end if;
1786 if not Is_Child_Unit (Id) then
1787 Set_Is_Immediately_Visible (Id);
1788 end if;
1790 end if;
1791 end Install_Package_Entity;
1793 ----------------------------------
1794 -- Install_Private_Declarations --
1795 ----------------------------------
1797 procedure Install_Private_Declarations (P : Entity_Id) is
1798 Id : Entity_Id;
1799 Priv_Elmt : Elmt_Id;
1800 Priv : Entity_Id;
1801 Full : Entity_Id;
1803 begin
1804 -- First exchange declarations for private types, so that the full
1805 -- declaration is visible. For each private type, we check its
1806 -- Private_Dependents list and also exchange any subtypes of or derived
1807 -- types from it. Finally, if this is a Taft amendment type, the
1808 -- incomplete declaration is irrelevant, and we want to link the
1809 -- eventual full declaration with the original private one so we also
1810 -- skip the exchange.
1812 Id := First_Entity (P);
1813 while Present (Id) and then Id /= First_Private_Entity (P) loop
1814 if Is_Private_Base_Type (Id)
1815 and then Comes_From_Source (Full_View (Id))
1816 and then Present (Full_View (Id))
1817 and then Scope (Full_View (Id)) = Scope (Id)
1818 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
1819 then
1820 -- If there is a use-type clause on the private type, set the
1821 -- full view accordingly.
1823 Set_In_Use (Full_View (Id), In_Use (Id));
1824 Full := Full_View (Id);
1826 if Is_Private_Base_Type (Full)
1827 and then Has_Private_Declaration (Full)
1828 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
1829 and then In_Open_Scopes (Scope (Etype (Full)))
1830 and then In_Package_Body (Current_Scope)
1831 and then not Is_Private_Type (Etype (Full))
1832 then
1833 -- This is the completion of a private type by a derivation
1834 -- from another private type which is not private anymore. This
1835 -- can only happen in a package nested within a child package,
1836 -- when the parent type is defined in the parent unit. At this
1837 -- point the current type is not private either, and we have to
1838 -- install the underlying full view, which is now visible. Save
1839 -- the current full view as well, so that all views can be
1840 -- restored on exit. It may seem that after compiling the child
1841 -- body there are not environments to restore, but the back-end
1842 -- expects those links to be valid, and freeze nodes depend on
1843 -- them.
1845 if No (Full_View (Full))
1846 and then Present (Underlying_Full_View (Full))
1847 then
1848 Set_Full_View (Id, Underlying_Full_View (Full));
1849 Set_Underlying_Full_View (Id, Full);
1851 Set_Underlying_Full_View (Full, Empty);
1852 Set_Is_Frozen (Full_View (Id));
1853 end if;
1854 end if;
1856 Priv_Elmt := First_Elmt (Private_Dependents (Id));
1858 Exchange_Declarations (Id);
1859 Set_Is_Immediately_Visible (Id);
1861 while Present (Priv_Elmt) loop
1862 Priv := Node (Priv_Elmt);
1864 -- Before the exchange, verify that the presence of the
1865 -- Full_View field. It will be empty if the entity has already
1866 -- been installed due to a previous call.
1868 if Present (Full_View (Priv))
1869 and then Is_Visible_Dependent (Priv)
1870 then
1872 -- For each subtype that is swapped, we also swap the
1873 -- reference to it in Private_Dependents, to allow access
1874 -- to it when we swap them out in End_Package_Scope.
1876 Replace_Elmt (Priv_Elmt, Full_View (Priv));
1877 Exchange_Declarations (Priv);
1878 Set_Is_Immediately_Visible
1879 (Priv, In_Open_Scopes (Scope (Priv)));
1880 Set_Is_Potentially_Use_Visible
1881 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
1882 end if;
1884 Next_Elmt (Priv_Elmt);
1885 end loop;
1886 end if;
1888 Next_Entity (Id);
1889 end loop;
1891 -- Next make other declarations in the private part visible as well
1893 Id := First_Private_Entity (P);
1894 while Present (Id) loop
1895 Install_Package_Entity (Id);
1896 Set_Is_Hidden (Id, False);
1897 Next_Entity (Id);
1898 end loop;
1900 -- Indicate that the private part is currently visible, so it can be
1901 -- properly reset on exit.
1903 Set_In_Private_Part (P);
1904 end Install_Private_Declarations;
1906 ----------------------------------
1907 -- Install_Visible_Declarations --
1908 ----------------------------------
1910 procedure Install_Visible_Declarations (P : Entity_Id) is
1911 Id : Entity_Id;
1912 Last_Entity : Entity_Id;
1914 begin
1915 pragma Assert
1916 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
1918 if Is_Package_Or_Generic_Package (P) then
1919 Last_Entity := First_Private_Entity (P);
1920 else
1921 Last_Entity := Empty;
1922 end if;
1924 Id := First_Entity (P);
1925 while Present (Id) and then Id /= Last_Entity loop
1926 Install_Package_Entity (Id);
1927 Next_Entity (Id);
1928 end loop;
1929 end Install_Visible_Declarations;
1931 --------------------------
1932 -- Is_Private_Base_Type --
1933 --------------------------
1935 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
1936 begin
1937 return Ekind (E) = E_Private_Type
1938 or else Ekind (E) = E_Limited_Private_Type
1939 or else Ekind (E) = E_Record_Type_With_Private;
1940 end Is_Private_Base_Type;
1942 --------------------------
1943 -- Is_Visible_Dependent --
1944 --------------------------
1946 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
1948 S : constant Entity_Id := Scope (Dep);
1950 begin
1951 -- Renamings created for actual types have the visibility of the actual
1953 if Ekind (S) = E_Package
1954 and then Is_Generic_Instance (S)
1955 and then (Is_Generic_Actual_Type (Dep)
1956 or else Is_Generic_Actual_Type (Full_View (Dep)))
1957 then
1958 return True;
1960 elsif not (Is_Derived_Type (Dep))
1961 and then Is_Derived_Type (Full_View (Dep))
1962 then
1963 -- When instantiating a package body, the scope stack is empty, so
1964 -- check instead whether the dependent type is defined in the same
1965 -- scope as the instance itself.
1967 return In_Open_Scopes (S)
1968 or else (Is_Generic_Instance (Current_Scope)
1969 and then Scope (Dep) = Scope (Current_Scope));
1970 else
1971 return True;
1972 end if;
1973 end Is_Visible_Dependent;
1975 ----------------------------
1976 -- May_Need_Implicit_Body --
1977 ----------------------------
1979 procedure May_Need_Implicit_Body (E : Entity_Id) is
1980 P : constant Node_Id := Unit_Declaration_Node (E);
1981 S : constant Node_Id := Parent (P);
1982 B : Node_Id;
1983 Decls : List_Id;
1985 begin
1986 if not Has_Completion (E)
1987 and then Nkind (P) = N_Package_Declaration
1988 and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
1989 then
1990 B :=
1991 Make_Package_Body (Sloc (E),
1992 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
1993 Chars => Chars (E)),
1994 Declarations => New_List);
1996 if Nkind (S) = N_Package_Specification then
1997 if Present (Private_Declarations (S)) then
1998 Decls := Private_Declarations (S);
1999 else
2000 Decls := Visible_Declarations (S);
2001 end if;
2002 else
2003 Decls := Declarations (S);
2004 end if;
2006 Append (B, Decls);
2007 Analyze (B);
2008 end if;
2009 end May_Need_Implicit_Body;
2011 ----------------------
2012 -- New_Private_Type --
2013 ----------------------
2015 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
2016 begin
2017 -- For other than Ada 2012, enter the name in the current scope
2019 if Ada_Version < Ada_2012 then
2020 Enter_Name (Id);
2022 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
2023 -- private type that completes an incomplete type.
2025 else
2026 declare
2027 Prev : Entity_Id;
2028 begin
2029 Prev := Find_Type_Name (N);
2030 pragma Assert (Prev = Id
2031 or else (Ekind (Prev) = E_Incomplete_Type
2032 and then Present (Full_View (Prev))
2033 and then Full_View (Prev) = Id));
2034 end;
2035 end if;
2037 if Limited_Present (Def) then
2038 Set_Ekind (Id, E_Limited_Private_Type);
2039 else
2040 Set_Ekind (Id, E_Private_Type);
2041 end if;
2043 Set_Etype (Id, Id);
2044 Set_Has_Delayed_Freeze (Id);
2045 Set_Is_First_Subtype (Id);
2046 Init_Size_Align (Id);
2048 Set_Is_Constrained (Id,
2049 No (Discriminant_Specifications (N))
2050 and then not Unknown_Discriminants_Present (N));
2052 -- Set tagged flag before processing discriminants, to catch illegal
2053 -- usage.
2055 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
2057 Set_Discriminant_Constraint (Id, No_Elist);
2058 Set_Stored_Constraint (Id, No_Elist);
2060 if Present (Discriminant_Specifications (N)) then
2061 Push_Scope (Id);
2062 Process_Discriminants (N);
2063 End_Scope;
2065 elsif Unknown_Discriminants_Present (N) then
2066 Set_Has_Unknown_Discriminants (Id);
2067 end if;
2069 Set_Private_Dependents (Id, New_Elmt_List);
2071 if Tagged_Present (Def) then
2072 Set_Ekind (Id, E_Record_Type_With_Private);
2073 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2074 Set_Is_Abstract_Type (Id, Abstract_Present (Def));
2075 Set_Is_Limited_Record (Id, Limited_Present (Def));
2076 Set_Has_Delayed_Freeze (Id, True);
2078 -- Create a class-wide type with the same attributes
2080 Make_Class_Wide_Type (Id);
2082 elsif Abstract_Present (Def) then
2083 Error_Msg_N ("only a tagged type can be abstract", N);
2084 end if;
2085 end New_Private_Type;
2087 ----------------------------
2088 -- Uninstall_Declarations --
2089 ----------------------------
2091 procedure Uninstall_Declarations (P : Entity_Id) is
2092 Decl : constant Node_Id := Unit_Declaration_Node (P);
2093 Id : Entity_Id;
2094 Full : Entity_Id;
2095 Priv_Elmt : Elmt_Id;
2096 Priv_Sub : Entity_Id;
2098 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id);
2099 -- Copy to the private declaration the attributes of the full view that
2100 -- need to be available for the partial view also.
2102 function Type_In_Use (T : Entity_Id) return Boolean;
2103 -- Check whether type or base type appear in an active use_type clause
2105 ------------------------------
2106 -- Preserve_Full_Attributes --
2107 ------------------------------
2109 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id) is
2110 Priv_Is_Base_Type : constant Boolean := Is_Base_Type (Priv);
2112 begin
2113 Set_Size_Info (Priv, (Full));
2114 Set_RM_Size (Priv, RM_Size (Full));
2115 Set_Size_Known_At_Compile_Time
2116 (Priv, Size_Known_At_Compile_Time (Full));
2117 Set_Is_Volatile (Priv, Is_Volatile (Full));
2118 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
2119 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full));
2120 Set_Is_Ada_2012_Only (Priv, Is_Ada_2012_Only (Full));
2121 Set_Has_Pragma_Unmodified (Priv, Has_Pragma_Unmodified (Full));
2122 Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced (Full));
2123 Set_Has_Pragma_Unreferenced_Objects
2124 (Priv, Has_Pragma_Unreferenced_Objects
2125 (Full));
2126 if Is_Unchecked_Union (Full) then
2127 Set_Is_Unchecked_Union (Base_Type (Priv));
2128 end if;
2129 -- Why is atomic not copied here ???
2131 if Referenced (Full) then
2132 Set_Referenced (Priv);
2133 end if;
2135 if Priv_Is_Base_Type then
2136 Set_Is_Controlled (Priv, Is_Controlled (Base_Type (Full)));
2137 Set_Finalize_Storage_Only (Priv, Finalize_Storage_Only
2138 (Base_Type (Full)));
2139 Set_Has_Task (Priv, Has_Task (Base_Type (Full)));
2140 Set_Has_Controlled_Component (Priv, Has_Controlled_Component
2141 (Base_Type (Full)));
2142 end if;
2144 Set_Freeze_Node (Priv, Freeze_Node (Full));
2146 if Is_Tagged_Type (Priv)
2147 and then Is_Tagged_Type (Full)
2148 and then not Error_Posted (Full)
2149 then
2150 if Is_Tagged_Type (Priv) then
2152 -- If the type is tagged, the tag itself must be available on
2153 -- the partial view, for expansion purposes.
2155 Set_First_Entity (Priv, First_Entity (Full));
2157 -- If there are discriminants in the partial view, these remain
2158 -- visible. Otherwise only the tag itself is visible, and there
2159 -- are no nameable components in the partial view.
2161 if No (Last_Entity (Priv)) then
2162 Set_Last_Entity (Priv, First_Entity (Priv));
2163 end if;
2164 end if;
2166 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2168 if Has_Discriminants (Full) then
2169 Set_Discriminant_Constraint (Priv,
2170 Discriminant_Constraint (Full));
2171 end if;
2172 end if;
2173 end Preserve_Full_Attributes;
2175 -----------------
2176 -- Type_In_Use --
2177 -----------------
2179 function Type_In_Use (T : Entity_Id) return Boolean is
2180 begin
2181 return Scope (Base_Type (T)) = P
2182 and then (In_Use (T) or else In_Use (Base_Type (T)));
2183 end Type_In_Use;
2185 -- Start of processing for Uninstall_Declarations
2187 begin
2188 Id := First_Entity (P);
2189 while Present (Id) and then Id /= First_Private_Entity (P) loop
2190 if Debug_Flag_E then
2191 Write_Str ("unlinking visible entity ");
2192 Write_Int (Int (Id));
2193 Write_Eol;
2194 end if;
2196 -- On exit from the package scope, we must preserve the visibility
2197 -- established by use clauses in the current scope. Two cases:
2199 -- a) If the entity is an operator, it may be a primitive operator of
2200 -- a type for which there is a visible use-type clause.
2202 -- b) for other entities, their use-visibility is determined by a
2203 -- visible use clause for the package itself. For a generic instance,
2204 -- the instantiation of the formals appears in the visible part,
2205 -- but the formals are private and remain so.
2207 if Ekind (Id) = E_Function
2208 and then Is_Operator_Symbol_Name (Chars (Id))
2209 and then not Is_Hidden (Id)
2210 and then not Error_Posted (Id)
2211 then
2212 Set_Is_Potentially_Use_Visible (Id,
2213 In_Use (P)
2214 or else Type_In_Use (Etype (Id))
2215 or else Type_In_Use (Etype (First_Formal (Id)))
2216 or else (Present (Next_Formal (First_Formal (Id)))
2217 and then
2218 Type_In_Use
2219 (Etype (Next_Formal (First_Formal (Id))))));
2220 else
2221 if In_Use (P) and then not Is_Hidden (Id) then
2223 -- A child unit of a use-visible package remains use-visible
2224 -- only if it is itself a visible child unit. Otherwise it
2225 -- would remain visible in other contexts where P is use-
2226 -- visible, because once compiled it stays in the entity list
2227 -- of its parent unit.
2229 if Is_Child_Unit (Id) then
2230 Set_Is_Potentially_Use_Visible (Id,
2231 Is_Visible_Child_Unit (Id));
2232 else
2233 Set_Is_Potentially_Use_Visible (Id);
2234 end if;
2236 else
2237 Set_Is_Potentially_Use_Visible (Id, False);
2238 end if;
2239 end if;
2241 -- Local entities are not immediately visible outside of the package
2243 Set_Is_Immediately_Visible (Id, False);
2245 -- If this is a private type with a full view (for example a local
2246 -- subtype of a private type declared elsewhere), ensure that the
2247 -- full view is also removed from visibility: it may be exposed when
2248 -- swapping views in an instantiation.
2250 if Is_Type (Id)
2251 and then Present (Full_View (Id))
2252 then
2253 Set_Is_Immediately_Visible (Full_View (Id), False);
2254 end if;
2256 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2257 Check_Abstract_Overriding (Id);
2258 Check_Conventions (Id);
2259 end if;
2261 if (Ekind (Id) = E_Private_Type
2262 or else Ekind (Id) = E_Limited_Private_Type)
2263 and then No (Full_View (Id))
2264 and then not Is_Generic_Type (Id)
2265 and then not Is_Derived_Type (Id)
2266 then
2267 Error_Msg_N ("missing full declaration for private type&", Id);
2269 elsif Ekind (Id) = E_Record_Type_With_Private
2270 and then not Is_Generic_Type (Id)
2271 and then No (Full_View (Id))
2272 then
2273 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
2274 Error_Msg_N ("missing full declaration for private type&", Id);
2275 else
2276 Error_Msg_N
2277 ("missing full declaration for private extension", Id);
2278 end if;
2280 -- Case of constant, check for deferred constant declaration with
2281 -- no full view. Likely just a matter of a missing expression, or
2282 -- accidental use of the keyword constant.
2284 elsif Ekind (Id) = E_Constant
2286 -- OK if constant value present
2288 and then No (Constant_Value (Id))
2290 -- OK if full view present
2292 and then No (Full_View (Id))
2294 -- OK if imported, since that provides the completion
2296 and then not Is_Imported (Id)
2298 -- OK if object declaration replaced by renaming declaration as
2299 -- a result of OK_To_Rename processing (e.g. for concatenation)
2301 and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration
2303 -- OK if object declaration with the No_Initialization flag set
2305 and then not (Nkind (Parent (Id)) = N_Object_Declaration
2306 and then No_Initialization (Parent (Id)))
2307 then
2308 -- If no private declaration is present, we assume the user did
2309 -- not intend a deferred constant declaration and the problem
2310 -- is simply that the initializing expression is missing.
2312 if not Has_Private_Declaration (Etype (Id)) then
2314 -- We assume that the user did not intend a deferred constant
2315 -- declaration, and the expression is just missing.
2317 Error_Msg_N
2318 ("constant declaration requires initialization expression",
2319 Parent (Id));
2321 if Is_Limited_Type (Etype (Id)) then
2322 Error_Msg_N
2323 ("\if variable intended, remove CONSTANT from declaration",
2324 Parent (Id));
2325 end if;
2327 -- Otherwise if a private declaration is present, then we are
2328 -- missing the full declaration for the deferred constant.
2330 else
2331 Error_Msg_N
2332 ("missing full declaration for deferred constant (RM 7.4)",
2333 Id);
2335 if Is_Limited_Type (Etype (Id)) then
2336 Error_Msg_N
2337 ("\if variable intended, remove CONSTANT from declaration",
2338 Parent (Id));
2339 end if;
2340 end if;
2341 end if;
2343 Next_Entity (Id);
2344 end loop;
2346 -- If the specification was installed as the parent of a public child
2347 -- unit, the private declarations were not installed, and there is
2348 -- nothing to do.
2350 if not In_Private_Part (P) then
2351 return;
2352 else
2353 Set_In_Private_Part (P, False);
2354 end if;
2356 -- Make private entities invisible and exchange full and private
2357 -- declarations for private types. Id is now the first private entity
2358 -- in the package.
2360 while Present (Id) loop
2361 if Debug_Flag_E then
2362 Write_Str ("unlinking private entity ");
2363 Write_Int (Int (Id));
2364 Write_Eol;
2365 end if;
2367 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2368 Check_Abstract_Overriding (Id);
2369 Check_Conventions (Id);
2370 end if;
2372 Set_Is_Immediately_Visible (Id, False);
2374 if Is_Private_Base_Type (Id)
2375 and then Present (Full_View (Id))
2376 then
2377 Full := Full_View (Id);
2379 -- If the partial view is not declared in the visible part of the
2380 -- package (as is the case when it is a type derived from some
2381 -- other private type in the private part of the current package),
2382 -- no exchange takes place.
2384 if No (Parent (Id))
2385 or else List_Containing (Parent (Id))
2386 /= Visible_Declarations (Specification (Decl))
2387 then
2388 goto Next_Id;
2389 end if;
2391 -- The entry in the private part points to the full declaration,
2392 -- which is currently visible. Exchange them so only the private
2393 -- type declaration remains accessible, and link private and full
2394 -- declaration in the opposite direction. Before the actual
2395 -- exchange, we copy back attributes of the full view that must
2396 -- be available to the partial view too.
2398 Preserve_Full_Attributes (Id, Full);
2400 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
2402 if Is_Indefinite_Subtype (Full)
2403 and then not Is_Indefinite_Subtype (Id)
2404 then
2405 Error_Msg_N
2406 ("full view of type must be definite subtype", Full);
2407 end if;
2409 Priv_Elmt := First_Elmt (Private_Dependents (Id));
2411 -- Swap out the subtypes and derived types of Id that were
2412 -- compiled in this scope, or installed previously by
2413 -- Install_Private_Declarations.
2415 -- Before we do the swap, we verify the presence of the Full_View
2416 -- field which may be empty due to a swap by a previous call to
2417 -- End_Package_Scope (e.g. from the freezing mechanism).
2419 while Present (Priv_Elmt) loop
2420 Priv_Sub := Node (Priv_Elmt);
2422 if Present (Full_View (Priv_Sub)) then
2424 if Scope (Priv_Sub) = P
2425 or else not In_Open_Scopes (Scope (Priv_Sub))
2426 then
2427 Set_Is_Immediately_Visible (Priv_Sub, False);
2428 end if;
2430 if Is_Visible_Dependent (Priv_Sub) then
2431 Preserve_Full_Attributes
2432 (Priv_Sub, Full_View (Priv_Sub));
2433 Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub));
2434 Exchange_Declarations (Priv_Sub);
2435 end if;
2436 end if;
2438 Next_Elmt (Priv_Elmt);
2439 end loop;
2441 -- Now restore the type itself to its private view
2443 Exchange_Declarations (Id);
2445 -- If we have installed an underlying full view for a type derived
2446 -- from a private type in a child unit, restore the proper views
2447 -- of private and full view. See corresponding code in
2448 -- Install_Private_Declarations.
2450 -- After the exchange, Full denotes the private type in the
2451 -- visible part of the package.
2453 if Is_Private_Base_Type (Full)
2454 and then Present (Full_View (Full))
2455 and then Present (Underlying_Full_View (Full))
2456 and then In_Package_Body (Current_Scope)
2457 then
2458 Set_Full_View (Full, Underlying_Full_View (Full));
2459 Set_Underlying_Full_View (Full, Empty);
2460 end if;
2462 elsif Ekind (Id) = E_Incomplete_Type
2463 and then Comes_From_Source (Id)
2464 and then No (Full_View (Id))
2465 then
2466 -- Mark Taft amendment types. Verify that there are no primitive
2467 -- operations declared for the type (3.10.1(9)).
2469 Set_Has_Completion_In_Body (Id);
2471 declare
2472 Elmt : Elmt_Id;
2473 Subp : Entity_Id;
2475 begin
2476 Elmt := First_Elmt (Private_Dependents (Id));
2477 while Present (Elmt) loop
2478 Subp := Node (Elmt);
2480 -- Is_Primitive is tested because there can be cases where
2481 -- nonprimitive subprograms (in nested packages) are added
2482 -- to the Private_Dependents list.
2484 if Is_Overloadable (Subp) and then Is_Primitive (Subp) then
2485 Error_Msg_NE
2486 ("type& must be completed in the private part",
2487 Parent (Subp), Id);
2489 -- The result type of an access-to-function type cannot be a
2490 -- Taft-amendment type, unless the version is Ada 2012 or
2491 -- later (see AI05-151).
2493 elsif Ada_Version < Ada_2012
2494 and then Ekind (Subp) = E_Subprogram_Type
2495 then
2496 if Etype (Subp) = Id
2497 or else
2498 (Is_Class_Wide_Type (Etype (Subp))
2499 and then Etype (Etype (Subp)) = Id)
2500 then
2501 Error_Msg_NE
2502 ("type& must be completed in the private part",
2503 Associated_Node_For_Itype (Subp), Id);
2504 end if;
2505 end if;
2507 Next_Elmt (Elmt);
2508 end loop;
2509 end;
2511 elsif not Is_Child_Unit (Id)
2512 and then (not Is_Private_Type (Id)
2513 or else No (Full_View (Id)))
2514 then
2515 Set_Is_Hidden (Id);
2516 Set_Is_Potentially_Use_Visible (Id, False);
2517 end if;
2519 <<Next_Id>>
2520 Next_Entity (Id);
2521 end loop;
2522 end Uninstall_Declarations;
2524 ------------------------
2525 -- Unit_Requires_Body --
2526 ------------------------
2528 function Unit_Requires_Body (P : Entity_Id) return Boolean is
2529 E : Entity_Id;
2531 begin
2532 -- Imported entity never requires body. Right now, only subprograms can
2533 -- be imported, but perhaps in the future we will allow import of
2534 -- packages.
2536 if Is_Imported (P) then
2537 return False;
2539 -- Body required if library package with pragma Elaborate_Body
2541 elsif Has_Pragma_Elaborate_Body (P) then
2542 return True;
2544 -- Body required if subprogram
2546 elsif Is_Subprogram (P) or else Is_Generic_Subprogram (P) then
2547 return True;
2549 -- Treat a block as requiring a body
2551 elsif Ekind (P) = E_Block then
2552 return True;
2554 elsif Ekind (P) = E_Package
2555 and then Nkind (Parent (P)) = N_Package_Specification
2556 and then Present (Generic_Parent (Parent (P)))
2557 then
2558 declare
2559 G_P : constant Entity_Id := Generic_Parent (Parent (P));
2560 begin
2561 if Has_Pragma_Elaborate_Body (G_P) then
2562 return True;
2563 end if;
2564 end;
2565 end if;
2567 -- Otherwise search entity chain for entity requiring completion
2569 E := First_Entity (P);
2570 while Present (E) loop
2572 -- Always ignore child units. Child units get added to the entity
2573 -- list of a parent unit, but are not original entities of the
2574 -- parent, and so do not affect whether the parent needs a body.
2576 if Is_Child_Unit (E) then
2577 null;
2579 -- Ignore formal packages and their renamings
2581 elsif Ekind (E) = E_Package
2582 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
2583 N_Formal_Package_Declaration
2584 then
2585 null;
2587 -- Otherwise test to see if entity requires a completion.
2588 -- Note that subprogram entities whose declaration does not come
2589 -- from source are ignored here on the basis that we assume the
2590 -- expander will provide an implicit completion at some point.
2592 elsif (Is_Overloadable (E)
2593 and then Ekind (E) /= E_Enumeration_Literal
2594 and then Ekind (E) /= E_Operator
2595 and then not Is_Abstract_Subprogram (E)
2596 and then not Has_Completion (E)
2597 and then Comes_From_Source (Parent (E)))
2599 or else
2600 (Ekind (E) = E_Package
2601 and then E /= P
2602 and then not Has_Completion (E)
2603 and then Unit_Requires_Body (E))
2605 or else
2606 (Ekind (E) = E_Incomplete_Type
2607 and then No (Full_View (E))
2608 and then not Is_Generic_Type (E))
2610 or else
2611 ((Ekind (E) = E_Task_Type or else
2612 Ekind (E) = E_Protected_Type)
2613 and then not Has_Completion (E))
2615 or else
2616 (Ekind (E) = E_Generic_Package and then E /= P
2617 and then not Has_Completion (E)
2618 and then Unit_Requires_Body (E))
2620 or else
2621 (Is_Generic_Subprogram (E)
2622 and then not Has_Completion (E))
2624 then
2625 return True;
2627 -- Entity that does not require completion
2629 else
2630 null;
2631 end if;
2633 Next_Entity (E);
2634 end loop;
2636 return False;
2637 end Unit_Requires_Body;
2639 end Sem_Ch7;