PR c++/37276
[official-gcc.git] / gcc / ada / sem_ch7.adb
bloba25e32833386b9c3e5f373aa77271d8545f38c40
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 Aspects; use Aspects;
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_Dist; use Exp_Dist;
39 with Exp_Dbug; use Exp_Dbug;
40 with Lib; use Lib;
41 with Lib.Xref; use Lib.Xref;
42 with Namet; use Namet;
43 with Nmake; use Nmake;
44 with Nlists; use Nlists;
45 with Opt; use Opt;
46 with Output; use Output;
47 with Restrict; use Restrict;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Cat; use Sem_Cat;
51 with Sem_Ch3; use Sem_Ch3;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Ch10; use Sem_Ch10;
55 with Sem_Ch12; use Sem_Ch12;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Disp; use Sem_Disp;
58 with Sem_Eval; use Sem_Eval;
59 with Sem_Util; use Sem_Util;
60 with Sem_Warn; use Sem_Warn;
61 with Snames; use Snames;
62 with Stand; use Stand;
63 with Sinfo; use Sinfo;
64 with Sinput; use Sinput;
65 with Style;
66 with Uintp; use Uintp;
68 package body Sem_Ch7 is
70 -----------------------------------
71 -- Handling private declarations --
72 -----------------------------------
74 -- The principle that each entity has a single defining occurrence clashes
75 -- with the presence of two separate definitions for private types: the
76 -- first is the private type declaration, and the second is the full type
77 -- declaration. It is important that all references to the type point to
78 -- the same defining occurrence, namely the first one. To enforce the two
79 -- separate views of the entity, the corresponding information is swapped
80 -- between the two declarations. Outside of the package, the defining
81 -- occurrence only contains the private declaration information, while in
82 -- the private part and the body of the package the defining occurrence
83 -- contains the full declaration. To simplify the swap, the defining
84 -- occurrence that currently holds the private declaration points to the
85 -- full declaration. During semantic processing the defining occurrence
86 -- also points to a list of private dependents, that is to say access types
87 -- or composite types whose designated types or component types are
88 -- subtypes or derived types of the private type in question. After the
89 -- full declaration has been seen, the private dependents are updated to
90 -- indicate that they have full definitions.
92 -----------------------
93 -- Local Subprograms --
94 -----------------------
96 procedure Analyze_Package_Body_Helper (N : Node_Id);
97 -- Does all the real work of Analyze_Package_Body
99 procedure Check_Anonymous_Access_Types
100 (Spec_Id : Entity_Id;
101 P_Body : Node_Id);
102 -- If the spec of a package has a limited_with_clause, it may declare
103 -- anonymous access types whose designated type is a limited view, such an
104 -- anonymous access return type for a function. This access type cannot be
105 -- elaborated in the spec itself, but it may need an itype reference if it
106 -- is used within a nested scope. In that case the itype reference is
107 -- created at the beginning of the corresponding package body and inserted
108 -- before other body declarations.
110 procedure Install_Package_Entity (Id : Entity_Id);
111 -- Supporting procedure for Install_{Visible,Private}_Declarations. Places
112 -- one entity on its visibility chain, and recurses on the visible part if
113 -- the entity is an inner package.
115 function Is_Private_Base_Type (E : Entity_Id) return Boolean;
116 -- True for a private type that is not a subtype
118 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean;
119 -- If the private dependent is a private type whose full view is derived
120 -- from the parent type, its full properties are revealed only if we are in
121 -- the immediate scope of the private dependent. Should this predicate be
122 -- tightened further???
124 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id);
125 -- Called upon entering the private part of a public child package and the
126 -- body of a nested package, to potentially declare certain inherited
127 -- subprograms that were inherited by types in the visible part, but whose
128 -- declaration was deferred because the parent operation was private and
129 -- not visible at that point. These subprograms are located by traversing
130 -- the visible part declarations looking for non-private type extensions
131 -- and then examining each of the primitive operations of such types to
132 -- find those that were inherited but declared with a special internal
133 -- name. Each such operation is now declared as an operation with a normal
134 -- name (using the name of the parent operation) and replaces the previous
135 -- implicit operation in the primitive operations list of the type. If the
136 -- inherited private operation has been overridden, then it's replaced by
137 -- the overriding operation.
139 --------------------------
140 -- Analyze_Package_Body --
141 --------------------------
143 procedure Analyze_Package_Body (N : Node_Id) is
144 Loc : constant Source_Ptr := Sloc (N);
146 begin
147 if Debug_Flag_C then
148 Write_Str ("==> package body ");
149 Write_Name (Chars (Defining_Entity (N)));
150 Write_Str (" from ");
151 Write_Location (Loc);
152 Write_Eol;
153 Indent;
154 end if;
156 -- The real work is split out into the helper, so it can do "return;"
157 -- without skipping the debug output.
159 Analyze_Package_Body_Helper (N);
161 if Debug_Flag_C then
162 Outdent;
163 Write_Str ("<== package body ");
164 Write_Name (Chars (Defining_Entity (N)));
165 Write_Str (" from ");
166 Write_Location (Loc);
167 Write_Eol;
168 end if;
169 end Analyze_Package_Body;
171 ---------------------------------
172 -- Analyze_Package_Body_Helper --
173 ---------------------------------
175 procedure Analyze_Package_Body_Helper (N : Node_Id) is
176 HSS : Node_Id;
177 Body_Id : Entity_Id;
178 Spec_Id : Entity_Id;
179 Last_Spec_Entity : Entity_Id;
180 New_N : Node_Id;
181 Pack_Decl : Node_Id;
183 procedure Install_Composite_Operations (P : Entity_Id);
184 -- Composite types declared in the current scope may depend on types
185 -- that were private at the point of declaration, and whose full view
186 -- is now in scope. Indicate that the corresponding operations on the
187 -- composite type are available.
189 ----------------------------------
190 -- Install_Composite_Operations --
191 ----------------------------------
193 procedure Install_Composite_Operations (P : Entity_Id) is
194 Id : Entity_Id;
196 begin
197 Id := First_Entity (P);
198 while Present (Id) loop
199 if Is_Type (Id)
200 and then (Is_Limited_Composite (Id)
201 or else Is_Private_Composite (Id))
202 and then No (Private_Component (Id))
203 then
204 Set_Is_Limited_Composite (Id, False);
205 Set_Is_Private_Composite (Id, False);
206 end if;
208 Next_Entity (Id);
209 end loop;
210 end Install_Composite_Operations;
212 -- Start of processing for Analyze_Package_Body_Helper
214 begin
215 -- Find corresponding package specification, and establish the current
216 -- scope. The visible defining entity for the package is the defining
217 -- occurrence in the spec. On exit from the package body, all body
218 -- declarations are attached to the defining entity for the body, but
219 -- the later is never used for name resolution. In this fashion there
220 -- is only one visible entity that denotes the package.
222 -- Set Body_Id. Note that this Will be reset to point to the generic
223 -- copy later on in the generic case.
225 Body_Id := Defining_Entity (N);
227 if Present (Corresponding_Spec (N)) then
229 -- Body is body of package instantiation. Corresponding spec has
230 -- already been set.
232 Spec_Id := Corresponding_Spec (N);
233 Pack_Decl := Unit_Declaration_Node (Spec_Id);
235 else
236 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
238 if Present (Spec_Id)
239 and then Is_Package_Or_Generic_Package (Spec_Id)
240 then
241 Pack_Decl := Unit_Declaration_Node (Spec_Id);
243 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
244 Error_Msg_N ("cannot supply body for package renaming", N);
245 return;
247 elsif Present (Corresponding_Body (Pack_Decl)) then
248 Error_Msg_N ("redefinition of package body", N);
249 return;
250 end if;
252 else
253 Error_Msg_N ("missing specification for package body", N);
254 return;
255 end if;
257 if Is_Package_Or_Generic_Package (Spec_Id)
258 and then (Scope (Spec_Id) = Standard_Standard
259 or else Is_Child_Unit (Spec_Id))
260 and then not Unit_Requires_Body (Spec_Id)
261 then
262 if Ada_Version = Ada_83 then
263 Error_Msg_N
264 ("optional package body (not allowed in Ada 95)?", N);
265 else
266 Error_Msg_N ("spec of this package does not allow a body", N);
267 end if;
268 end if;
269 end if;
271 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
272 Style.Check_Identifier (Body_Id, Spec_Id);
274 if Is_Child_Unit (Spec_Id) then
275 if Nkind (Parent (N)) /= N_Compilation_Unit then
276 Error_Msg_NE
277 ("body of child unit& cannot be an inner package", N, Spec_Id);
278 end if;
280 Set_Is_Child_Unit (Body_Id);
281 end if;
283 -- Generic package case
285 if Ekind (Spec_Id) = E_Generic_Package then
287 -- Disable expansion and perform semantic analysis on copy. The
288 -- unannotated body will be used in all instantiations.
290 Body_Id := Defining_Entity (N);
291 Set_Ekind (Body_Id, E_Package_Body);
292 Set_Scope (Body_Id, Scope (Spec_Id));
293 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
294 Set_Body_Entity (Spec_Id, Body_Id);
295 Set_Spec_Entity (Body_Id, Spec_Id);
297 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
298 Rewrite (N, New_N);
300 -- Update Body_Id to point to the copied node for the remainder of
301 -- the processing.
303 Body_Id := Defining_Entity (N);
304 Start_Generic;
305 end if;
307 -- The Body_Id is that of the copied node in the generic case, the
308 -- current node otherwise. Note that N was rewritten above, so we must
309 -- be sure to get the latest Body_Id value.
311 Set_Ekind (Body_Id, E_Package_Body);
312 Set_Body_Entity (Spec_Id, Body_Id);
313 Set_Spec_Entity (Body_Id, Spec_Id);
315 -- Defining name for the package body is not a visible entity: Only the
316 -- defining name for the declaration is visible.
318 Set_Etype (Body_Id, Standard_Void_Type);
319 Set_Scope (Body_Id, Scope (Spec_Id));
320 Set_Corresponding_Spec (N, Spec_Id);
321 Set_Corresponding_Body (Pack_Decl, Body_Id);
323 -- The body entity is not used for semantics or code generation, but
324 -- it is attached to the entity list of the enclosing scope to simplify
325 -- the listing of back-annotations for the types it main contain.
327 if Scope (Spec_Id) /= Standard_Standard then
328 Append_Entity (Body_Id, Scope (Spec_Id));
329 end if;
331 -- Indicate that we are currently compiling the body of the package
333 Set_In_Package_Body (Spec_Id);
334 Set_Has_Completion (Spec_Id);
335 Last_Spec_Entity := Last_Entity (Spec_Id);
337 Push_Scope (Spec_Id);
339 Set_Categorization_From_Pragmas (N);
341 Install_Visible_Declarations (Spec_Id);
342 Install_Private_Declarations (Spec_Id);
343 Install_Private_With_Clauses (Spec_Id);
344 Install_Composite_Operations (Spec_Id);
346 Check_Anonymous_Access_Types (Spec_Id, N);
348 if Ekind (Spec_Id) = E_Generic_Package then
349 Set_Use (Generic_Formal_Declarations (Pack_Decl));
350 end if;
352 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
353 Set_Use (Private_Declarations (Specification (Pack_Decl)));
355 -- This is a nested package, so it may be necessary to declare certain
356 -- inherited subprograms that are not yet visible because the parent
357 -- type's subprograms are now visible.
359 if Ekind (Scope (Spec_Id)) = E_Package
360 and then Scope (Spec_Id) /= Standard_Standard
361 then
362 Declare_Inherited_Private_Subprograms (Spec_Id);
363 end if;
365 if Present (Declarations (N)) then
366 Analyze_Declarations (Declarations (N));
367 Inspect_Deferred_Constant_Completion (Declarations (N));
368 end if;
370 -- Analyze_Declarations has caused freezing of all types. Now generate
371 -- bodies for RACW primitives and stream attributes, if any.
373 if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then
375 -- Attach subprogram bodies to support RACWs declared in spec
377 Append_RACW_Bodies (Declarations (N), Spec_Id);
378 Analyze_List (Declarations (N));
379 end if;
381 HSS := Handled_Statement_Sequence (N);
383 if Present (HSS) then
384 Process_End_Label (HSS, 't', Spec_Id);
385 Analyze (HSS);
387 -- Check that elaboration code in a preelaborable package body is
388 -- empty other than null statements and labels (RM 10.2.1(6)).
390 Validate_Null_Statement_Sequence (N);
391 end if;
393 Validate_Categorization_Dependency (N, Spec_Id);
394 Check_Completion (Body_Id);
396 -- Generate start of body reference. Note that we do this fairly late,
397 -- because the call will use In_Extended_Main_Source_Unit as a check,
398 -- and we want to make sure that Corresponding_Stub links are set
400 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
402 -- For a generic package, collect global references and mark them on
403 -- the original body so that they are not resolved again at the point
404 -- of instantiation.
406 if Ekind (Spec_Id) /= E_Package then
407 Save_Global_References (Original_Node (N));
408 End_Generic;
409 end if;
411 -- The entities of the package body have so far been chained onto the
412 -- declaration chain for the spec. That's been fine while we were in the
413 -- body, since we wanted them to be visible, but now that we are leaving
414 -- the package body, they are no longer visible, so we remove them from
415 -- the entity chain of the package spec entity, and copy them to the
416 -- entity chain of the package body entity, where they will never again
417 -- be visible.
419 if Present (Last_Spec_Entity) then
420 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
421 Set_Next_Entity (Last_Spec_Entity, Empty);
422 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
423 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
425 else
426 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
427 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
428 Set_First_Entity (Spec_Id, Empty);
429 Set_Last_Entity (Spec_Id, Empty);
430 end if;
432 End_Package_Scope (Spec_Id);
434 -- All entities declared in body are not visible
436 declare
437 E : Entity_Id;
439 begin
440 E := First_Entity (Body_Id);
441 while Present (E) loop
442 Set_Is_Immediately_Visible (E, False);
443 Set_Is_Potentially_Use_Visible (E, False);
444 Set_Is_Hidden (E);
446 -- Child units may appear on the entity list (e.g. if they appear
447 -- in the context of a subunit) but they are not body entities.
449 if not Is_Child_Unit (E) then
450 Set_Is_Package_Body_Entity (E);
451 end if;
453 Next_Entity (E);
454 end loop;
455 end;
457 Check_References (Body_Id);
459 -- For a generic unit, check that the formal parameters are referenced,
460 -- and that local variables are used, as for regular packages.
462 if Ekind (Spec_Id) = E_Generic_Package then
463 Check_References (Spec_Id);
464 end if;
466 -- The processing so far has made all entities of the package body
467 -- public (i.e. externally visible to the linker). This is in general
468 -- necessary, since inlined or generic bodies, for which code is
469 -- generated in other units, may need to see these entities. The
470 -- following loop runs backwards from the end of the entities of the
471 -- package body making these entities invisible until we reach a
472 -- referencer, i.e. a declaration that could reference a previous
473 -- declaration, a generic body or an inlined body, or a stub (which may
474 -- contain either of these). This is of course an approximation, but it
475 -- is conservative and definitely correct.
477 -- We only do this at the outer (library) level non-generic packages.
478 -- The reason is simply to cut down on the number of global symbols
479 -- generated, which has a double effect: (1) to make the compilation
480 -- process more efficient and (2) to give the code generator more
481 -- freedom to optimize within each unit, especially subprograms.
483 if (Scope (Spec_Id) = Standard_Standard or else Is_Child_Unit (Spec_Id))
484 and then not Is_Generic_Unit (Spec_Id)
485 and then Present (Declarations (N))
486 then
487 Make_Non_Public_Where_Possible : declare
489 function Has_Referencer
490 (L : List_Id;
491 Outer : Boolean) return Boolean;
492 -- Traverse the given list of declarations in reverse order.
493 -- Return True if a referencer is present. Return False if none is
494 -- found. The Outer parameter is True for the outer level call and
495 -- False for inner level calls for nested packages. If Outer is
496 -- True, then any entities up to the point of hitting a referencer
497 -- get their Is_Public flag cleared, so that the entities will be
498 -- treated as static entities in the C sense, and need not have
499 -- fully qualified names. Furthermore, if the referencer is an
500 -- inlined subprogram that doesn't reference other subprograms,
501 -- we keep clearing the Is_Public flag on subprograms. For inner
502 -- levels, we need all names to be fully qualified to deal with
503 -- the same name appearing in parallel packages (right now this
504 -- is tied to their being external).
506 --------------------
507 -- Has_Referencer --
508 --------------------
510 function Has_Referencer
511 (L : List_Id;
512 Outer : Boolean) return Boolean
514 Has_Referencer_Except_For_Subprograms : Boolean := False;
516 D : Node_Id;
517 E : Entity_Id;
518 K : Node_Kind;
519 S : Entity_Id;
521 function Check_Subprogram_Ref (N : Node_Id)
522 return Traverse_Result;
523 -- Look for references to subprograms
525 --------------------------
526 -- Check_Subprogram_Ref --
527 --------------------------
529 function Check_Subprogram_Ref (N : Node_Id)
530 return Traverse_Result
532 V : Node_Id;
534 begin
535 -- Check name of procedure or function calls
537 if Nkind (N) in N_Subprogram_Call
538 and then Is_Entity_Name (Name (N))
539 then
540 return Abandon;
541 end if;
543 -- Check prefix of attribute references
545 if Nkind (N) = N_Attribute_Reference
546 and then Is_Entity_Name (Prefix (N))
547 and then Present (Entity (Prefix (N)))
548 and then Ekind (Entity (Prefix (N))) in Subprogram_Kind
549 then
550 return Abandon;
551 end if;
553 -- Check value of constants
555 if Nkind (N) = N_Identifier
556 and then Present (Entity (N))
557 and then Ekind (Entity (N)) = E_Constant
558 then
559 V := Constant_Value (Entity (N));
560 if Present (V)
561 and then not Compile_Time_Known_Value_Or_Aggr (V)
562 then
563 return Abandon;
564 end if;
565 end if;
567 return OK;
568 end Check_Subprogram_Ref;
570 function Check_Subprogram_Refs is
571 new Traverse_Func (Check_Subprogram_Ref);
573 -- Start of processing for Has_Referencer
575 begin
576 if No (L) then
577 return False;
578 end if;
580 D := Last (L);
581 while Present (D) loop
582 K := Nkind (D);
584 if K in N_Body_Stub then
585 return True;
587 -- Processing for subprogram bodies
589 elsif K = N_Subprogram_Body then
590 if Acts_As_Spec (D) then
591 E := Defining_Entity (D);
593 -- An inlined body acts as a referencer. Note also
594 -- that we never reset Is_Public for an inlined
595 -- subprogram. Gigi requires Is_Public to be set.
597 -- Note that we test Has_Pragma_Inline here rather
598 -- than Is_Inlined. We are compiling this for a
599 -- client, and it is the client who will decide if
600 -- actual inlining should occur, so we need to assume
601 -- that the procedure could be inlined for the purpose
602 -- of accessing global entities.
604 if Has_Pragma_Inline (E) then
605 if Outer
606 and then Check_Subprogram_Refs (D) = OK
607 then
608 Has_Referencer_Except_For_Subprograms := True;
609 else
610 return True;
611 end if;
612 else
613 Set_Is_Public (E, False);
614 end if;
616 else
617 E := Corresponding_Spec (D);
619 if Present (E) then
621 -- A generic subprogram body acts as a referencer
623 if Is_Generic_Unit (E) then
624 return True;
625 end if;
627 if Has_Pragma_Inline (E) or else Is_Inlined (E) then
628 if Outer
629 and then Check_Subprogram_Refs (D) = OK
630 then
631 Has_Referencer_Except_For_Subprograms := True;
632 else
633 return True;
634 end if;
635 end if;
636 end if;
637 end if;
639 -- Processing for package bodies
641 elsif K = N_Package_Body
642 and then Present (Corresponding_Spec (D))
643 then
644 E := Corresponding_Spec (D);
646 -- Generic package body is a referencer. It would seem
647 -- that we only have to consider generics that can be
648 -- exported, i.e. where the corresponding spec is the
649 -- spec of the current package, but because of nested
650 -- instantiations, a fully private generic body may
651 -- export other private body entities. Furthermore,
652 -- regardless of whether there was a previous inlined
653 -- subprogram, (an instantiation of) the generic package
654 -- may reference any entity declared before it.
656 if Is_Generic_Unit (E) then
657 return True;
659 -- For non-generic package body, recurse into body unless
660 -- this is an instance, we ignore instances since they
661 -- cannot have references that affect outer entities.
663 elsif not Is_Generic_Instance (E)
664 and then not Has_Referencer_Except_For_Subprograms
665 then
666 if Has_Referencer
667 (Declarations (D), Outer => False)
668 then
669 return True;
670 end if;
671 end if;
673 -- Processing for package specs, recurse into declarations.
674 -- Again we skip this for the case of generic instances.
676 elsif K = N_Package_Declaration
677 and then not Has_Referencer_Except_For_Subprograms
678 then
679 S := Specification (D);
681 if not Is_Generic_Unit (Defining_Entity (S)) then
682 if Has_Referencer
683 (Private_Declarations (S), Outer => False)
684 then
685 return True;
686 elsif Has_Referencer
687 (Visible_Declarations (S), Outer => False)
688 then
689 return True;
690 end if;
691 end if;
693 -- Objects and exceptions need not be public if we have not
694 -- encountered a referencer so far. We only reset the flag
695 -- for outer level entities that are not imported/exported,
696 -- and which have no interface name.
698 elsif Nkind_In (K, N_Object_Declaration,
699 N_Exception_Declaration,
700 N_Subprogram_Declaration)
701 then
702 E := Defining_Entity (D);
704 if Outer
705 and then (not Has_Referencer_Except_For_Subprograms
706 or else K = N_Subprogram_Declaration)
707 and then not Is_Imported (E)
708 and then not Is_Exported (E)
709 and then No (Interface_Name (E))
710 then
711 Set_Is_Public (E, False);
712 end if;
713 end if;
715 Prev (D);
716 end loop;
718 return Has_Referencer_Except_For_Subprograms;
719 end Has_Referencer;
721 -- Start of processing for Make_Non_Public_Where_Possible
723 begin
724 declare
725 Discard : Boolean;
726 pragma Warnings (Off, Discard);
728 begin
729 Discard := Has_Referencer (Declarations (N), Outer => True);
730 end;
731 end Make_Non_Public_Where_Possible;
732 end if;
734 -- If expander is not active, then here is where we turn off the
735 -- In_Package_Body flag, otherwise it is turned off at the end of the
736 -- corresponding expansion routine. If this is an instance body, we need
737 -- to qualify names of local entities, because the body may have been
738 -- compiled as a preliminary to another instantiation.
740 if not Expander_Active then
741 Set_In_Package_Body (Spec_Id, False);
743 if Is_Generic_Instance (Spec_Id)
744 and then Operating_Mode = Generate_Code
745 then
746 Qualify_Entity_Names (N);
747 end if;
748 end if;
749 end Analyze_Package_Body_Helper;
751 ---------------------------------
752 -- Analyze_Package_Declaration --
753 ---------------------------------
755 procedure Analyze_Package_Declaration (N : Node_Id) is
756 Id : constant Node_Id := Defining_Entity (N);
758 PF : Boolean;
759 -- True when in the context of a declared pure library unit
761 Body_Required : Boolean;
762 -- True when this package declaration requires a corresponding body
764 Comp_Unit : Boolean;
765 -- True when this package declaration is not a nested declaration
767 begin
768 -- Analye aspect specifications immediately, since we need to recognize
769 -- things like Pure early enough to diagnose violations during analysis.
771 if Has_Aspects (N) then
772 Analyze_Aspect_Specifications (N, Id);
773 end if;
775 -- Ada 2005 (AI-217): Check if the package has been erroneously named
776 -- in a limited-with clause of its own context. In this case the error
777 -- has been previously notified by Analyze_Context.
779 -- limited with Pkg; -- ERROR
780 -- package Pkg is ...
782 if From_With_Type (Id) then
783 return;
784 end if;
786 if Debug_Flag_C then
787 Write_Str ("==> package spec ");
788 Write_Name (Chars (Id));
789 Write_Str (" from ");
790 Write_Location (Sloc (N));
791 Write_Eol;
792 Indent;
793 end if;
795 Generate_Definition (Id);
796 Enter_Name (Id);
797 Set_Ekind (Id, E_Package);
798 Set_Etype (Id, Standard_Void_Type);
800 Push_Scope (Id);
802 PF := Is_Pure (Enclosing_Lib_Unit_Entity);
803 Set_Is_Pure (Id, PF);
805 Set_Categorization_From_Pragmas (N);
807 Analyze (Specification (N));
808 Validate_Categorization_Dependency (N, Id);
810 Body_Required := Unit_Requires_Body (Id);
812 -- When this spec does not require an explicit body, we know that there
813 -- are no entities requiring completion in the language sense; we call
814 -- Check_Completion here only to ensure that any nested package
815 -- declaration that requires an implicit body gets one. (In the case
816 -- where a body is required, Check_Completion is called at the end of
817 -- the body's declarative part.)
819 if not Body_Required then
820 Check_Completion;
821 end if;
823 Comp_Unit := Nkind (Parent (N)) = N_Compilation_Unit;
824 if Comp_Unit then
826 -- Set Body_Required indication on the compilation unit node, and
827 -- determine whether elaboration warnings may be meaningful on it.
829 Set_Body_Required (Parent (N), Body_Required);
831 if not Body_Required then
832 Set_Suppress_Elaboration_Warnings (Id);
833 end if;
835 end if;
837 End_Package_Scope (Id);
839 -- For the declaration of a library unit that is a remote types package,
840 -- check legality rules regarding availability of stream attributes for
841 -- types that contain non-remote access values. This subprogram performs
842 -- visibility tests that rely on the fact that we have exited the scope
843 -- of Id.
845 if Comp_Unit then
846 Validate_RT_RAT_Component (N);
847 end if;
849 if Debug_Flag_C then
850 Outdent;
851 Write_Str ("<== package spec ");
852 Write_Name (Chars (Id));
853 Write_Str (" from ");
854 Write_Location (Sloc (N));
855 Write_Eol;
856 end if;
857 end Analyze_Package_Declaration;
859 -----------------------------------
860 -- Analyze_Package_Specification --
861 -----------------------------------
863 -- Note that this code is shared for the analysis of generic package specs
864 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
866 procedure Analyze_Package_Specification (N : Node_Id) is
867 Id : constant Entity_Id := Defining_Entity (N);
868 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
869 Vis_Decls : constant List_Id := Visible_Declarations (N);
870 Priv_Decls : constant List_Id := Private_Declarations (N);
871 E : Entity_Id;
872 L : Entity_Id;
873 Public_Child : Boolean;
875 Private_With_Clauses_Installed : Boolean := False;
876 -- In Ada 2005, private with_clauses are visible in the private part
877 -- of a nested package, even if it appears in the public part of the
878 -- enclosing package. This requires a separate step to install these
879 -- private_with_clauses, and remove them at the end of the nested
880 -- package.
882 procedure Check_One_Tagged_Type_Or_Extension_At_Most;
883 -- Issue an error in SPARK mode if a package specification contains
884 -- more than one tagged type or type extension.
886 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
887 -- Clears constant indications (Never_Set_In_Source, Constant_Value, and
888 -- Is_True_Constant) on all variables that are entities of Id, and on
889 -- the chain whose first element is FE. A recursive call is made for all
890 -- packages and generic packages.
892 procedure Generate_Parent_References;
893 -- For a child unit, generate references to parent units, for
894 -- GPS navigation purposes.
896 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
897 -- Child and Unit are entities of compilation units. True if Child
898 -- is a public child of Parent as defined in 10.1.1
900 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
901 -- Reject completion of an incomplete or private type declarations
902 -- having a known discriminant part by an unchecked union.
904 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
905 -- Given the package entity of a generic package instantiation or
906 -- formal package whose corresponding generic is a child unit, installs
907 -- the private declarations of each of the child unit's parents.
908 -- This has to be done at the point of entering the instance package's
909 -- private part rather than being done in Sem_Ch12.Install_Parent
910 -- (which is where the parents' visible declarations are installed).
912 ------------------------------------------------
913 -- Check_One_Tagged_Type_Or_Extension_At_Most --
914 ------------------------------------------------
916 procedure Check_One_Tagged_Type_Or_Extension_At_Most is
917 Previous : Node_Id;
919 procedure Check_Decls (Decls : List_Id);
920 -- Check that either Previous is Empty and Decls does not contain
921 -- more than one tagged type or type extension, or Previous is
922 -- already set and Decls contains no tagged type or type extension.
924 -----------------
925 -- Check_Decls --
926 -----------------
928 procedure Check_Decls (Decls : List_Id) is
929 Decl : Node_Id;
931 begin
932 Decl := First (Decls);
933 while Present (Decl) loop
934 if Nkind (Decl) = N_Full_Type_Declaration
935 and then Is_Tagged_Type (Defining_Identifier (Decl))
936 then
937 if No (Previous) then
938 Previous := Decl;
940 else
941 Error_Msg_Sloc := Sloc (Previous);
942 Check_SPARK_Restriction
943 ("at most one tagged type or type extension allowed",
944 "\\ previous declaration#",
945 Decl);
946 end if;
947 end if;
949 Next (Decl);
950 end loop;
951 end Check_Decls;
953 -- Start of processing for Check_One_Tagged_Type_Or_Extension_At_Most
955 begin
956 Previous := Empty;
957 Check_Decls (Vis_Decls);
959 if Present (Priv_Decls) then
960 Check_Decls (Priv_Decls);
961 end if;
962 end Check_One_Tagged_Type_Or_Extension_At_Most;
964 ---------------------
965 -- Clear_Constants --
966 ---------------------
968 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
969 E : Entity_Id;
971 begin
972 -- Ignore package renamings, not interesting and they can cause self
973 -- referential loops in the code below.
975 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
976 return;
977 end if;
979 -- Note: in the loop below, the check for Next_Entity pointing back
980 -- to the package entity may seem odd, but it is needed, because a
981 -- package can contain a renaming declaration to itself, and such
982 -- renamings are generated automatically within package instances.
984 E := FE;
985 while Present (E) and then E /= Id loop
986 if Is_Assignable (E) then
987 Set_Never_Set_In_Source (E, False);
988 Set_Is_True_Constant (E, False);
989 Set_Current_Value (E, Empty);
990 Set_Is_Known_Null (E, False);
991 Set_Last_Assignment (E, Empty);
993 if not Can_Never_Be_Null (E) then
994 Set_Is_Known_Non_Null (E, False);
995 end if;
997 elsif Is_Package_Or_Generic_Package (E) then
998 Clear_Constants (E, First_Entity (E));
999 Clear_Constants (E, First_Private_Entity (E));
1000 end if;
1002 Next_Entity (E);
1003 end loop;
1004 end Clear_Constants;
1006 --------------------------------
1007 -- Generate_Parent_References --
1008 --------------------------------
1010 procedure Generate_Parent_References is
1011 Decl : constant Node_Id := Parent (N);
1013 begin
1014 if Id = Cunit_Entity (Main_Unit)
1015 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
1016 then
1017 Generate_Reference (Id, Scope (Id), 'k', False);
1019 elsif not Nkind_In (Unit (Cunit (Main_Unit)), N_Subprogram_Body,
1020 N_Subunit)
1021 then
1022 -- If current unit is an ancestor of main unit, generate a
1023 -- reference to its own parent.
1025 declare
1026 U : Node_Id;
1027 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
1029 begin
1030 if Nkind (Main_Spec) = N_Package_Body then
1031 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
1032 end if;
1034 U := Parent_Spec (Main_Spec);
1035 while Present (U) loop
1036 if U = Parent (Decl) then
1037 Generate_Reference (Id, Scope (Id), 'k', False);
1038 exit;
1040 elsif Nkind (Unit (U)) = N_Package_Body then
1041 exit;
1043 else
1044 U := Parent_Spec (Unit (U));
1045 end if;
1046 end loop;
1047 end;
1048 end if;
1049 end Generate_Parent_References;
1051 ---------------------
1052 -- Is_Public_Child --
1053 ---------------------
1055 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
1056 begin
1057 if not Is_Private_Descendant (Child) then
1058 return True;
1059 else
1060 if Child = Unit then
1061 return not Private_Present (
1062 Parent (Unit_Declaration_Node (Child)));
1063 else
1064 return Is_Public_Child (Scope (Child), Unit);
1065 end if;
1066 end if;
1067 end Is_Public_Child;
1069 ----------------------------------------
1070 -- Inspect_Unchecked_Union_Completion --
1071 ----------------------------------------
1073 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
1074 Decl : Node_Id;
1076 begin
1077 Decl := First (Decls);
1078 while Present (Decl) loop
1080 -- We are looking at an incomplete or private type declaration
1081 -- with a known_discriminant_part whose full view is an
1082 -- Unchecked_Union.
1084 if Nkind_In (Decl, N_Incomplete_Type_Declaration,
1085 N_Private_Type_Declaration)
1086 and then Has_Discriminants (Defining_Identifier (Decl))
1087 and then Present (Full_View (Defining_Identifier (Decl)))
1088 and then
1089 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
1090 then
1091 Error_Msg_N
1092 ("completion of discriminated partial view "
1093 & "cannot be an unchecked union",
1094 Full_View (Defining_Identifier (Decl)));
1095 end if;
1097 Next (Decl);
1098 end loop;
1099 end Inspect_Unchecked_Union_Completion;
1101 -----------------------------------------
1102 -- Install_Parent_Private_Declarations --
1103 -----------------------------------------
1105 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
1106 Inst_Par : Entity_Id;
1107 Gen_Par : Entity_Id;
1108 Inst_Node : Node_Id;
1110 begin
1111 Inst_Par := Inst_Id;
1113 Gen_Par :=
1114 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
1115 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
1116 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
1118 if Nkind_In (Inst_Node, N_Package_Instantiation,
1119 N_Formal_Package_Declaration)
1120 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
1121 then
1122 Inst_Par := Entity (Prefix (Name (Inst_Node)));
1124 if Present (Renamed_Entity (Inst_Par)) then
1125 Inst_Par := Renamed_Entity (Inst_Par);
1126 end if;
1128 Gen_Par :=
1129 Generic_Parent
1130 (Specification (Unit_Declaration_Node (Inst_Par)));
1132 -- Install the private declarations and private use clauses
1133 -- of a parent instance of the child instance, unless the
1134 -- parent instance private declarations have already been
1135 -- installed earlier in Analyze_Package_Specification, which
1136 -- happens when a generic child is instantiated, and the
1137 -- instance is a child of the parent instance.
1139 -- Installing the use clauses of the parent instance twice
1140 -- is both unnecessary and wrong, because it would cause the
1141 -- clauses to be chained to themselves in the use clauses
1142 -- list of the scope stack entry. That in turn would cause
1143 -- an endless loop from End_Use_Clauses upon scope exit.
1145 -- The parent is now fully visible. It may be a hidden open
1146 -- scope if we are currently compiling some child instance
1147 -- declared within it, but while the current instance is being
1148 -- compiled the parent is immediately visible. In particular
1149 -- its entities must remain visible if a stack save/restore
1150 -- takes place through a call to Rtsfind.
1152 if Present (Gen_Par) then
1153 if not In_Private_Part (Inst_Par) then
1154 Install_Private_Declarations (Inst_Par);
1155 Set_Use (Private_Declarations
1156 (Specification
1157 (Unit_Declaration_Node (Inst_Par))));
1158 Set_Is_Hidden_Open_Scope (Inst_Par, False);
1159 end if;
1161 -- If we've reached the end of the generic instance parents,
1162 -- then finish off by looping through the nongeneric parents
1163 -- and installing their private declarations.
1165 else
1166 while Present (Inst_Par)
1167 and then Inst_Par /= Standard_Standard
1168 and then (not In_Open_Scopes (Inst_Par)
1169 or else not In_Private_Part (Inst_Par))
1170 loop
1171 Install_Private_Declarations (Inst_Par);
1172 Set_Use (Private_Declarations
1173 (Specification
1174 (Unit_Declaration_Node (Inst_Par))));
1175 Inst_Par := Scope (Inst_Par);
1176 end loop;
1178 exit;
1179 end if;
1181 else
1182 exit;
1183 end if;
1184 end loop;
1185 end Install_Parent_Private_Declarations;
1187 -- Start of processing for Analyze_Package_Specification
1189 begin
1190 if Present (Vis_Decls) then
1191 Analyze_Declarations (Vis_Decls);
1192 end if;
1194 -- Verify that incomplete types have received full declarations and
1195 -- also build invariant procedures for any types with invariants.
1197 E := First_Entity (Id);
1198 while Present (E) loop
1200 -- Check on incomplete types
1202 -- AI05-0213: A formal incomplete type has no completion
1204 if Ekind (E) = E_Incomplete_Type
1205 and then No (Full_View (E))
1206 and then not Is_Generic_Type (E)
1207 then
1208 Error_Msg_N ("no declaration in visible part for incomplete}", E);
1209 end if;
1211 -- Build invariant procedures
1213 if Is_Type (E) and then Has_Invariants (E) then
1214 Build_Invariant_Procedure (E, N);
1215 end if;
1217 Next_Entity (E);
1218 end loop;
1220 if Is_Remote_Call_Interface (Id)
1221 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1222 then
1223 Validate_RCI_Declarations (Id);
1224 end if;
1226 -- Save global references in the visible declarations, before installing
1227 -- private declarations of parent unit if there is one, because the
1228 -- privacy status of types defined in the parent will change. This is
1229 -- only relevant for generic child units, but is done in all cases for
1230 -- uniformity.
1232 if Ekind (Id) = E_Generic_Package
1233 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1234 then
1235 declare
1236 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1237 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1238 begin
1239 Set_Private_Declarations (Orig_Spec, Empty_List);
1240 Save_Global_References (Orig_Decl);
1241 Set_Private_Declarations (Orig_Spec, Save_Priv);
1242 end;
1243 end if;
1245 -- If package is a public child unit, then make the private declarations
1246 -- of the parent visible.
1248 Public_Child := False;
1250 declare
1251 Par : Entity_Id;
1252 Pack_Decl : Node_Id;
1253 Par_Spec : Node_Id;
1255 begin
1256 Par := Id;
1257 Par_Spec := Parent_Spec (Parent (N));
1259 -- If the package is formal package of an enclosing generic, it is
1260 -- transformed into a local generic declaration, and compiled to make
1261 -- its spec available. We need to retrieve the original generic to
1262 -- determine whether it is a child unit, and install its parents.
1264 if No (Par_Spec)
1265 and then
1266 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1267 then
1268 Par := Entity (Name (Original_Node (Parent (N))));
1269 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1270 end if;
1272 if Present (Par_Spec) then
1273 Generate_Parent_References;
1275 while Scope (Par) /= Standard_Standard
1276 and then Is_Public_Child (Id, Par)
1277 and then In_Open_Scopes (Par)
1278 loop
1279 Public_Child := True;
1280 Par := Scope (Par);
1281 Install_Private_Declarations (Par);
1282 Install_Private_With_Clauses (Par);
1283 Pack_Decl := Unit_Declaration_Node (Par);
1284 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1285 end loop;
1286 end if;
1287 end;
1289 if Is_Compilation_Unit (Id) then
1290 Install_Private_With_Clauses (Id);
1291 else
1293 -- The current compilation unit may include private with_clauses,
1294 -- which are visible in the private part of the current nested
1295 -- package, and have to be installed now. This is not done for
1296 -- nested instantiations, where the private with_clauses of the
1297 -- enclosing unit have no effect once the instantiation info is
1298 -- established and we start analyzing the package declaration.
1300 declare
1301 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1302 begin
1303 if Is_Package_Or_Generic_Package (Comp_Unit)
1304 and then not In_Private_Part (Comp_Unit)
1305 and then not In_Instance
1306 then
1307 Install_Private_With_Clauses (Comp_Unit);
1308 Private_With_Clauses_Installed := True;
1309 end if;
1310 end;
1311 end if;
1313 -- If this is a package associated with a generic instance or formal
1314 -- package, then the private declarations of each of the generic's
1315 -- parents must be installed at this point.
1317 if Is_Generic_Instance (Id) then
1318 Install_Parent_Private_Declarations (Id);
1319 end if;
1321 -- Analyze private part if present. The flag In_Private_Part is reset
1322 -- in End_Package_Scope.
1324 L := Last_Entity (Id);
1326 if Present (Priv_Decls) then
1327 Set_In_Private_Part (Id);
1329 -- Upon entering a public child's private part, it may be necessary
1330 -- to declare subprograms that were derived in the package's visible
1331 -- part but not yet made visible.
1333 if Public_Child then
1334 Declare_Inherited_Private_Subprograms (Id);
1335 end if;
1337 Analyze_Declarations (Priv_Decls);
1339 -- Check the private declarations for incomplete deferred constants
1341 Inspect_Deferred_Constant_Completion (Priv_Decls);
1343 -- The first private entity is the immediate follower of the last
1344 -- visible entity, if there was one.
1346 if Present (L) then
1347 Set_First_Private_Entity (Id, Next_Entity (L));
1348 else
1349 Set_First_Private_Entity (Id, First_Entity (Id));
1350 end if;
1352 -- There may be inherited private subprograms that need to be declared,
1353 -- even in the absence of an explicit private part. If there are any
1354 -- public declarations in the package and the package is a public child
1355 -- unit, then an implicit private part is assumed.
1357 elsif Present (L) and then Public_Child then
1358 Set_In_Private_Part (Id);
1359 Declare_Inherited_Private_Subprograms (Id);
1360 Set_First_Private_Entity (Id, Next_Entity (L));
1361 end if;
1363 E := First_Entity (Id);
1364 while Present (E) loop
1366 -- Check rule of 3.6(11), which in general requires waiting till all
1367 -- full types have been seen.
1369 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1370 Check_Aliased_Component_Types (E);
1371 end if;
1373 -- Check preelaborable initialization for full type completing a
1374 -- private type for which pragma Preelaborable_Initialization given.
1376 if Is_Type (E)
1377 and then Must_Have_Preelab_Init (E)
1378 and then not Has_Preelaborable_Initialization (E)
1379 then
1380 Error_Msg_N
1381 ("full view of & does not have preelaborable initialization", E);
1382 end if;
1384 -- An invariant may appear on a full view of a type
1386 if Is_Type (E)
1387 and then Has_Private_Declaration (E)
1388 and then Nkind (Parent (E)) = N_Full_Type_Declaration
1389 and then Has_Aspects (Parent (E))
1390 then
1391 declare
1392 ASN : Node_Id;
1394 begin
1395 ASN := First (Aspect_Specifications (Parent (E)));
1396 while Present (ASN) loop
1397 if Chars (Identifier (ASN)) = Name_Invariant
1398 or else
1399 Chars (Identifier (ASN)) = Name_Type_Invariant
1400 then
1401 Build_Invariant_Procedure (E, N);
1402 exit;
1403 end if;
1405 Next (ASN);
1406 end loop;
1407 end;
1408 end if;
1410 Next_Entity (E);
1411 end loop;
1413 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1414 -- declaration having a known_discriminant_part shall not be an
1415 -- unchecked union type.
1417 if Present (Vis_Decls) then
1418 Inspect_Unchecked_Union_Completion (Vis_Decls);
1419 end if;
1421 if Present (Priv_Decls) then
1422 Inspect_Unchecked_Union_Completion (Priv_Decls);
1423 end if;
1425 if Ekind (Id) = E_Generic_Package
1426 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1427 and then Present (Priv_Decls)
1428 then
1429 -- Save global references in private declarations, ignoring the
1430 -- visible declarations that were processed earlier.
1432 declare
1433 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1434 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1435 Save_Form : constant List_Id :=
1436 Generic_Formal_Declarations (Orig_Decl);
1438 begin
1439 Set_Visible_Declarations (Orig_Spec, Empty_List);
1440 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1441 Save_Global_References (Orig_Decl);
1442 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1443 Set_Visible_Declarations (Orig_Spec, Save_Vis);
1444 end;
1445 end if;
1447 Process_End_Label (N, 'e', Id);
1449 -- Remove private_with_clauses of enclosing compilation unit, if they
1450 -- were installed.
1452 if Private_With_Clauses_Installed then
1453 Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
1454 end if;
1456 -- For the case of a library level package, we must go through all the
1457 -- entities clearing the indications that the value may be constant and
1458 -- not modified. Why? Because any client of this package may modify
1459 -- these values freely from anywhere. This also applies to any nested
1460 -- packages or generic packages.
1462 -- For now we unconditionally clear constants for packages that are
1463 -- instances of generic packages. The reason is that we do not have the
1464 -- body yet, and we otherwise think things are unreferenced when they
1465 -- are not. This should be fixed sometime (the effect is not terrible,
1466 -- we just lose some warnings, and also some cases of value propagation)
1467 -- ???
1469 if Is_Library_Level_Entity (Id)
1470 or else Is_Generic_Instance (Id)
1471 then
1472 Clear_Constants (Id, First_Entity (Id));
1473 Clear_Constants (Id, First_Private_Entity (Id));
1474 end if;
1476 Check_One_Tagged_Type_Or_Extension_At_Most;
1477 end Analyze_Package_Specification;
1479 --------------------------------------
1480 -- Analyze_Private_Type_Declaration --
1481 --------------------------------------
1483 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1484 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
1485 Id : constant Entity_Id := Defining_Identifier (N);
1487 begin
1488 Generate_Definition (Id);
1489 Set_Is_Pure (Id, PF);
1490 Init_Size_Align (Id);
1492 if not Is_Package_Or_Generic_Package (Current_Scope)
1493 or else In_Private_Part (Current_Scope)
1494 then
1495 Error_Msg_N ("invalid context for private declaration", N);
1496 end if;
1498 New_Private_Type (N, Id, N);
1499 Set_Depends_On_Private (Id);
1501 if Has_Aspects (N) then
1502 Analyze_Aspect_Specifications (N, Id);
1503 end if;
1504 end Analyze_Private_Type_Declaration;
1506 ----------------------------------
1507 -- Check_Anonymous_Access_Types --
1508 ----------------------------------
1510 procedure Check_Anonymous_Access_Types
1511 (Spec_Id : Entity_Id;
1512 P_Body : Node_Id)
1514 E : Entity_Id;
1515 IR : Node_Id;
1517 begin
1518 -- Itype references are only needed by gigi, to force elaboration of
1519 -- itypes. In the absence of code generation, they are not needed.
1521 if not Expander_Active then
1522 return;
1523 end if;
1525 E := First_Entity (Spec_Id);
1526 while Present (E) loop
1527 if Ekind (E) = E_Anonymous_Access_Type
1528 and then From_With_Type (E)
1529 then
1530 IR := Make_Itype_Reference (Sloc (P_Body));
1531 Set_Itype (IR, E);
1533 if No (Declarations (P_Body)) then
1534 Set_Declarations (P_Body, New_List (IR));
1535 else
1536 Prepend (IR, Declarations (P_Body));
1537 end if;
1538 end if;
1540 Next_Entity (E);
1541 end loop;
1542 end Check_Anonymous_Access_Types;
1544 -------------------------------------------
1545 -- Declare_Inherited_Private_Subprograms --
1546 -------------------------------------------
1548 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1550 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1551 -- Check whether an inherited subprogram S is an operation of an
1552 -- untagged derived type T.
1554 ---------------------
1555 -- Is_Primitive_Of --
1556 ---------------------
1558 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1559 Formal : Entity_Id;
1561 begin
1562 -- If the full view is a scalar type, the type is the anonymous base
1563 -- type, but the operation mentions the first subtype, so check the
1564 -- signature against the base type.
1566 if Base_Type (Etype (S)) = Base_Type (T) then
1567 return True;
1569 else
1570 Formal := First_Formal (S);
1571 while Present (Formal) loop
1572 if Base_Type (Etype (Formal)) = Base_Type (T) then
1573 return True;
1574 end if;
1576 Next_Formal (Formal);
1577 end loop;
1579 return False;
1580 end if;
1581 end Is_Primitive_Of;
1583 -- Local variables
1585 E : Entity_Id;
1586 Op_List : Elist_Id;
1587 Op_Elmt : Elmt_Id;
1588 Op_Elmt_2 : Elmt_Id;
1589 Prim_Op : Entity_Id;
1590 New_Op : Entity_Id := Empty;
1591 Parent_Subp : Entity_Id;
1592 Tag : Entity_Id;
1594 -- Start of processing for Declare_Inherited_Private_Subprograms
1596 begin
1597 E := First_Entity (Id);
1598 while Present (E) loop
1600 -- If the entity is a nonprivate type extension whose parent type
1601 -- is declared in an open scope, then the type may have inherited
1602 -- operations that now need to be made visible. Ditto if the entity
1603 -- is a formal derived type in a child unit.
1605 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
1606 or else
1607 (Nkind (Parent (E)) = N_Private_Extension_Declaration
1608 and then Is_Generic_Type (E)))
1609 and then In_Open_Scopes (Scope (Etype (E)))
1610 and then Is_Base_Type (E)
1611 then
1612 if Is_Tagged_Type (E) then
1613 Op_List := Primitive_Operations (E);
1614 New_Op := Empty;
1615 Tag := First_Tag_Component (E);
1617 Op_Elmt := First_Elmt (Op_List);
1618 while Present (Op_Elmt) loop
1619 Prim_Op := Node (Op_Elmt);
1621 -- Search primitives that are implicit operations with an
1622 -- internal name whose parent operation has a normal name.
1624 if Present (Alias (Prim_Op))
1625 and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
1626 and then not Comes_From_Source (Prim_Op)
1627 and then Is_Internal_Name (Chars (Prim_Op))
1628 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1629 then
1630 Parent_Subp := Alias (Prim_Op);
1632 -- Case 1: Check if the type has also an explicit
1633 -- overriding for this primitive.
1635 Op_Elmt_2 := Next_Elmt (Op_Elmt);
1636 while Present (Op_Elmt_2) loop
1638 -- Skip entities with attribute Interface_Alias since
1639 -- they are not overriding primitives (these entities
1640 -- link an interface primitive with their covering
1641 -- primitive)
1643 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
1644 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
1645 and then No (Interface_Alias (Node (Op_Elmt_2)))
1646 then
1647 -- The private inherited operation has been
1648 -- overridden by an explicit subprogram: replace
1649 -- the former by the latter.
1651 New_Op := Node (Op_Elmt_2);
1652 Replace_Elmt (Op_Elmt, New_Op);
1653 Remove_Elmt (Op_List, Op_Elmt_2);
1654 Set_Overridden_Operation (New_Op, Parent_Subp);
1656 -- We don't need to inherit its dispatching slot.
1657 -- Set_All_DT_Position has previously ensured that
1658 -- the same slot was assigned to the two primitives
1660 if Present (Tag)
1661 and then Present (DTC_Entity (New_Op))
1662 and then Present (DTC_Entity (Prim_Op))
1663 then
1664 pragma Assert (DT_Position (New_Op)
1665 = DT_Position (Prim_Op));
1666 null;
1667 end if;
1669 goto Next_Primitive;
1670 end if;
1672 Next_Elmt (Op_Elmt_2);
1673 end loop;
1675 -- Case 2: We have not found any explicit overriding and
1676 -- hence we need to declare the operation (i.e., make it
1677 -- visible).
1679 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1681 -- Inherit the dispatching slot if E is already frozen
1683 if Is_Frozen (E)
1684 and then Present (DTC_Entity (Alias (Prim_Op)))
1685 then
1686 Set_DTC_Entity_Value (E, New_Op);
1687 Set_DT_Position (New_Op,
1688 DT_Position (Alias (Prim_Op)));
1689 end if;
1691 pragma Assert
1692 (Is_Dispatching_Operation (New_Op)
1693 and then Node (Last_Elmt (Op_List)) = New_Op);
1695 -- Substitute the new operation for the old one in the
1696 -- type's primitive operations list. Since the new
1697 -- operation was also just added to the end of list,
1698 -- the last element must be removed.
1700 -- (Question: is there a simpler way of declaring the
1701 -- operation, say by just replacing the name of the
1702 -- earlier operation, reentering it in the in the symbol
1703 -- table (how?), and marking it as private???)
1705 Replace_Elmt (Op_Elmt, New_Op);
1706 Remove_Last_Elmt (Op_List);
1707 end if;
1709 <<Next_Primitive>>
1710 Next_Elmt (Op_Elmt);
1711 end loop;
1713 -- Generate listing showing the contents of the dispatch table
1715 if Debug_Flag_ZZ then
1716 Write_DT (E);
1717 end if;
1719 else
1720 -- Non-tagged type, scan forward to locate inherited hidden
1721 -- operations.
1723 Prim_Op := Next_Entity (E);
1724 while Present (Prim_Op) loop
1725 if Is_Subprogram (Prim_Op)
1726 and then Present (Alias (Prim_Op))
1727 and then not Comes_From_Source (Prim_Op)
1728 and then Is_Internal_Name (Chars (Prim_Op))
1729 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1730 and then Is_Primitive_Of (E, Prim_Op)
1731 then
1732 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1733 end if;
1735 Next_Entity (Prim_Op);
1736 end loop;
1737 end if;
1738 end if;
1740 Next_Entity (E);
1741 end loop;
1742 end Declare_Inherited_Private_Subprograms;
1744 -----------------------
1745 -- End_Package_Scope --
1746 -----------------------
1748 procedure End_Package_Scope (P : Entity_Id) is
1749 begin
1750 Uninstall_Declarations (P);
1751 Pop_Scope;
1752 end End_Package_Scope;
1754 ---------------------------
1755 -- Exchange_Declarations --
1756 ---------------------------
1758 procedure Exchange_Declarations (Id : Entity_Id) is
1759 Full_Id : constant Entity_Id := Full_View (Id);
1760 H1 : constant Entity_Id := Homonym (Id);
1761 Next1 : constant Entity_Id := Next_Entity (Id);
1762 H2 : Entity_Id;
1763 Next2 : Entity_Id;
1765 begin
1766 -- If missing full declaration for type, nothing to exchange
1768 if No (Full_Id) then
1769 return;
1770 end if;
1772 -- Otherwise complete the exchange, and preserve semantic links
1774 Next2 := Next_Entity (Full_Id);
1775 H2 := Homonym (Full_Id);
1777 -- Reset full declaration pointer to reflect the switched entities and
1778 -- readjust the next entity chains.
1780 Exchange_Entities (Id, Full_Id);
1782 Set_Next_Entity (Id, Next1);
1783 Set_Homonym (Id, H1);
1785 Set_Full_View (Full_Id, Id);
1786 Set_Next_Entity (Full_Id, Next2);
1787 Set_Homonym (Full_Id, H2);
1788 end Exchange_Declarations;
1790 ----------------------------
1791 -- Install_Package_Entity --
1792 ----------------------------
1794 procedure Install_Package_Entity (Id : Entity_Id) is
1795 begin
1796 if not Is_Internal (Id) then
1797 if Debug_Flag_E then
1798 Write_Str ("Install: ");
1799 Write_Name (Chars (Id));
1800 Write_Eol;
1801 end if;
1803 if not Is_Child_Unit (Id) then
1804 Set_Is_Immediately_Visible (Id);
1805 end if;
1807 end if;
1808 end Install_Package_Entity;
1810 ----------------------------------
1811 -- Install_Private_Declarations --
1812 ----------------------------------
1814 procedure Install_Private_Declarations (P : Entity_Id) is
1815 Id : Entity_Id;
1816 Priv_Elmt : Elmt_Id;
1817 Priv : Entity_Id;
1818 Full : Entity_Id;
1820 begin
1821 -- First exchange declarations for private types, so that the full
1822 -- declaration is visible. For each private type, we check its
1823 -- Private_Dependents list and also exchange any subtypes of or derived
1824 -- types from it. Finally, if this is a Taft amendment type, the
1825 -- incomplete declaration is irrelevant, and we want to link the
1826 -- eventual full declaration with the original private one so we also
1827 -- skip the exchange.
1829 Id := First_Entity (P);
1830 while Present (Id) and then Id /= First_Private_Entity (P) loop
1831 if Is_Private_Base_Type (Id)
1832 and then Comes_From_Source (Full_View (Id))
1833 and then Present (Full_View (Id))
1834 and then Scope (Full_View (Id)) = Scope (Id)
1835 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
1836 then
1837 -- If there is a use-type clause on the private type, set the
1838 -- full view accordingly.
1840 Set_In_Use (Full_View (Id), In_Use (Id));
1841 Full := Full_View (Id);
1843 if Is_Private_Base_Type (Full)
1844 and then Has_Private_Declaration (Full)
1845 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
1846 and then In_Open_Scopes (Scope (Etype (Full)))
1847 and then In_Package_Body (Current_Scope)
1848 and then not Is_Private_Type (Etype (Full))
1849 then
1850 -- This is the completion of a private type by a derivation
1851 -- from another private type which is not private anymore. This
1852 -- can only happen in a package nested within a child package,
1853 -- when the parent type is defined in the parent unit. At this
1854 -- point the current type is not private either, and we have to
1855 -- install the underlying full view, which is now visible. Save
1856 -- the current full view as well, so that all views can be
1857 -- restored on exit. It may seem that after compiling the child
1858 -- body there are not environments to restore, but the back-end
1859 -- expects those links to be valid, and freeze nodes depend on
1860 -- them.
1862 if No (Full_View (Full))
1863 and then Present (Underlying_Full_View (Full))
1864 then
1865 Set_Full_View (Id, Underlying_Full_View (Full));
1866 Set_Underlying_Full_View (Id, Full);
1868 Set_Underlying_Full_View (Full, Empty);
1869 Set_Is_Frozen (Full_View (Id));
1870 end if;
1871 end if;
1873 Priv_Elmt := First_Elmt (Private_Dependents (Id));
1875 Exchange_Declarations (Id);
1876 Set_Is_Immediately_Visible (Id);
1878 while Present (Priv_Elmt) loop
1879 Priv := Node (Priv_Elmt);
1881 -- Before the exchange, verify that the presence of the
1882 -- Full_View field. It will be empty if the entity has already
1883 -- been installed due to a previous call.
1885 if Present (Full_View (Priv))
1886 and then Is_Visible_Dependent (Priv)
1887 then
1889 -- For each subtype that is swapped, we also swap the
1890 -- reference to it in Private_Dependents, to allow access
1891 -- to it when we swap them out in End_Package_Scope.
1893 Replace_Elmt (Priv_Elmt, Full_View (Priv));
1894 Exchange_Declarations (Priv);
1895 Set_Is_Immediately_Visible
1896 (Priv, In_Open_Scopes (Scope (Priv)));
1897 Set_Is_Potentially_Use_Visible
1898 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
1899 end if;
1901 Next_Elmt (Priv_Elmt);
1902 end loop;
1903 end if;
1905 Next_Entity (Id);
1906 end loop;
1908 -- Next make other declarations in the private part visible as well
1910 Id := First_Private_Entity (P);
1911 while Present (Id) loop
1912 Install_Package_Entity (Id);
1913 Set_Is_Hidden (Id, False);
1914 Next_Entity (Id);
1915 end loop;
1917 -- Indicate that the private part is currently visible, so it can be
1918 -- properly reset on exit.
1920 Set_In_Private_Part (P);
1921 end Install_Private_Declarations;
1923 ----------------------------------
1924 -- Install_Visible_Declarations --
1925 ----------------------------------
1927 procedure Install_Visible_Declarations (P : Entity_Id) is
1928 Id : Entity_Id;
1929 Last_Entity : Entity_Id;
1931 begin
1932 pragma Assert
1933 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
1935 if Is_Package_Or_Generic_Package (P) then
1936 Last_Entity := First_Private_Entity (P);
1937 else
1938 Last_Entity := Empty;
1939 end if;
1941 Id := First_Entity (P);
1942 while Present (Id) and then Id /= Last_Entity loop
1943 Install_Package_Entity (Id);
1944 Next_Entity (Id);
1945 end loop;
1946 end Install_Visible_Declarations;
1948 --------------------------
1949 -- Is_Private_Base_Type --
1950 --------------------------
1952 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
1953 begin
1954 return Ekind (E) = E_Private_Type
1955 or else Ekind (E) = E_Limited_Private_Type
1956 or else Ekind (E) = E_Record_Type_With_Private;
1957 end Is_Private_Base_Type;
1959 --------------------------
1960 -- Is_Visible_Dependent --
1961 --------------------------
1963 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
1965 S : constant Entity_Id := Scope (Dep);
1967 begin
1968 -- Renamings created for actual types have the visibility of the actual
1970 if Ekind (S) = E_Package
1971 and then Is_Generic_Instance (S)
1972 and then (Is_Generic_Actual_Type (Dep)
1973 or else Is_Generic_Actual_Type (Full_View (Dep)))
1974 then
1975 return True;
1977 elsif not (Is_Derived_Type (Dep))
1978 and then Is_Derived_Type (Full_View (Dep))
1979 then
1980 -- When instantiating a package body, the scope stack is empty, so
1981 -- check instead whether the dependent type is defined in the same
1982 -- scope as the instance itself.
1984 return In_Open_Scopes (S)
1985 or else (Is_Generic_Instance (Current_Scope)
1986 and then Scope (Dep) = Scope (Current_Scope));
1987 else
1988 return True;
1989 end if;
1990 end Is_Visible_Dependent;
1992 ----------------------------
1993 -- May_Need_Implicit_Body --
1994 ----------------------------
1996 procedure May_Need_Implicit_Body (E : Entity_Id) is
1997 P : constant Node_Id := Unit_Declaration_Node (E);
1998 S : constant Node_Id := Parent (P);
1999 B : Node_Id;
2000 Decls : List_Id;
2002 begin
2003 if not Has_Completion (E)
2004 and then Nkind (P) = N_Package_Declaration
2005 and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
2006 then
2007 B :=
2008 Make_Package_Body (Sloc (E),
2009 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
2010 Chars => Chars (E)),
2011 Declarations => New_List);
2013 if Nkind (S) = N_Package_Specification then
2014 if Present (Private_Declarations (S)) then
2015 Decls := Private_Declarations (S);
2016 else
2017 Decls := Visible_Declarations (S);
2018 end if;
2019 else
2020 Decls := Declarations (S);
2021 end if;
2023 Append (B, Decls);
2024 Analyze (B);
2025 end if;
2026 end May_Need_Implicit_Body;
2028 ----------------------
2029 -- New_Private_Type --
2030 ----------------------
2032 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
2033 begin
2034 -- For other than Ada 2012, enter the name in the current scope
2036 if Ada_Version < Ada_2012 then
2037 Enter_Name (Id);
2039 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
2040 -- private type that completes an incomplete type.
2042 else
2043 declare
2044 Prev : Entity_Id;
2045 begin
2046 Prev := Find_Type_Name (N);
2047 pragma Assert (Prev = Id
2048 or else (Ekind (Prev) = E_Incomplete_Type
2049 and then Present (Full_View (Prev))
2050 and then Full_View (Prev) = Id));
2051 end;
2052 end if;
2054 if Limited_Present (Def) then
2055 Set_Ekind (Id, E_Limited_Private_Type);
2056 else
2057 Set_Ekind (Id, E_Private_Type);
2058 end if;
2060 Set_Etype (Id, Id);
2061 Set_Has_Delayed_Freeze (Id);
2062 Set_Is_First_Subtype (Id);
2063 Init_Size_Align (Id);
2065 Set_Is_Constrained (Id,
2066 No (Discriminant_Specifications (N))
2067 and then not Unknown_Discriminants_Present (N));
2069 -- Set tagged flag before processing discriminants, to catch illegal
2070 -- usage.
2072 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
2074 Set_Discriminant_Constraint (Id, No_Elist);
2075 Set_Stored_Constraint (Id, No_Elist);
2077 if Present (Discriminant_Specifications (N)) then
2078 Push_Scope (Id);
2079 Process_Discriminants (N);
2080 End_Scope;
2082 elsif Unknown_Discriminants_Present (N) then
2083 Set_Has_Unknown_Discriminants (Id);
2084 end if;
2086 Set_Private_Dependents (Id, New_Elmt_List);
2088 if Tagged_Present (Def) then
2089 Set_Ekind (Id, E_Record_Type_With_Private);
2090 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2091 Set_Is_Abstract_Type (Id, Abstract_Present (Def));
2092 Set_Is_Limited_Record (Id, Limited_Present (Def));
2093 Set_Has_Delayed_Freeze (Id, True);
2095 -- Create a class-wide type with the same attributes
2097 Make_Class_Wide_Type (Id);
2099 elsif Abstract_Present (Def) then
2100 Error_Msg_N ("only a tagged type can be abstract", N);
2101 end if;
2102 end New_Private_Type;
2104 ----------------------------
2105 -- Uninstall_Declarations --
2106 ----------------------------
2108 procedure Uninstall_Declarations (P : Entity_Id) is
2109 Decl : constant Node_Id := Unit_Declaration_Node (P);
2110 Id : Entity_Id;
2111 Full : Entity_Id;
2112 Priv_Elmt : Elmt_Id;
2113 Priv_Sub : Entity_Id;
2115 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id);
2116 -- Copy to the private declaration the attributes of the full view that
2117 -- need to be available for the partial view also.
2119 function Type_In_Use (T : Entity_Id) return Boolean;
2120 -- Check whether type or base type appear in an active use_type clause
2122 ------------------------------
2123 -- Preserve_Full_Attributes --
2124 ------------------------------
2126 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id) is
2127 Priv_Is_Base_Type : constant Boolean := Is_Base_Type (Priv);
2129 begin
2130 Set_Size_Info (Priv, (Full));
2131 Set_RM_Size (Priv, RM_Size (Full));
2132 Set_Size_Known_At_Compile_Time
2133 (Priv, Size_Known_At_Compile_Time (Full));
2134 Set_Is_Volatile (Priv, Is_Volatile (Full));
2135 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
2136 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full));
2137 Set_Is_Ada_2012_Only (Priv, Is_Ada_2012_Only (Full));
2138 Set_Has_Pragma_Unmodified (Priv, Has_Pragma_Unmodified (Full));
2139 Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced (Full));
2140 Set_Has_Pragma_Unreferenced_Objects
2141 (Priv, Has_Pragma_Unreferenced_Objects
2142 (Full));
2143 if Is_Unchecked_Union (Full) then
2144 Set_Is_Unchecked_Union (Base_Type (Priv));
2145 end if;
2146 -- Why is atomic not copied here ???
2148 if Referenced (Full) then
2149 Set_Referenced (Priv);
2150 end if;
2152 if Priv_Is_Base_Type then
2153 Set_Is_Controlled (Priv, Is_Controlled (Base_Type (Full)));
2154 Set_Finalize_Storage_Only (Priv, Finalize_Storage_Only
2155 (Base_Type (Full)));
2156 Set_Has_Task (Priv, Has_Task (Base_Type (Full)));
2157 Set_Has_Controlled_Component (Priv, Has_Controlled_Component
2158 (Base_Type (Full)));
2159 end if;
2161 Set_Freeze_Node (Priv, Freeze_Node (Full));
2163 -- Propagate information of type invariants, which may be specified
2164 -- for the full view.
2166 if Has_Invariants (Full) and not Has_Invariants (Priv) then
2167 Set_Has_Invariants (Priv);
2168 Set_Subprograms_For_Type (Priv, Subprograms_For_Type (Full));
2169 end if;
2171 if Is_Tagged_Type (Priv)
2172 and then Is_Tagged_Type (Full)
2173 and then not Error_Posted (Full)
2174 then
2175 if Is_Tagged_Type (Priv) then
2177 -- If the type is tagged, the tag itself must be available on
2178 -- the partial view, for expansion purposes.
2180 Set_First_Entity (Priv, First_Entity (Full));
2182 -- If there are discriminants in the partial view, these remain
2183 -- visible. Otherwise only the tag itself is visible, and there
2184 -- are no nameable components in the partial view.
2186 if No (Last_Entity (Priv)) then
2187 Set_Last_Entity (Priv, First_Entity (Priv));
2188 end if;
2189 end if;
2191 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2193 if Has_Discriminants (Full) then
2194 Set_Discriminant_Constraint (Priv,
2195 Discriminant_Constraint (Full));
2196 end if;
2197 end if;
2198 end Preserve_Full_Attributes;
2200 -----------------
2201 -- Type_In_Use --
2202 -----------------
2204 function Type_In_Use (T : Entity_Id) return Boolean is
2205 begin
2206 return Scope (Base_Type (T)) = P
2207 and then (In_Use (T) or else In_Use (Base_Type (T)));
2208 end Type_In_Use;
2210 -- Start of processing for Uninstall_Declarations
2212 begin
2213 Id := First_Entity (P);
2214 while Present (Id) and then Id /= First_Private_Entity (P) loop
2215 if Debug_Flag_E then
2216 Write_Str ("unlinking visible entity ");
2217 Write_Int (Int (Id));
2218 Write_Eol;
2219 end if;
2221 -- On exit from the package scope, we must preserve the visibility
2222 -- established by use clauses in the current scope. Two cases:
2224 -- a) If the entity is an operator, it may be a primitive operator of
2225 -- a type for which there is a visible use-type clause.
2227 -- b) for other entities, their use-visibility is determined by a
2228 -- visible use clause for the package itself. For a generic instance,
2229 -- the instantiation of the formals appears in the visible part,
2230 -- but the formals are private and remain so.
2232 if Ekind (Id) = E_Function
2233 and then Is_Operator_Symbol_Name (Chars (Id))
2234 and then not Is_Hidden (Id)
2235 and then not Error_Posted (Id)
2236 then
2237 Set_Is_Potentially_Use_Visible (Id,
2238 In_Use (P)
2239 or else Type_In_Use (Etype (Id))
2240 or else Type_In_Use (Etype (First_Formal (Id)))
2241 or else (Present (Next_Formal (First_Formal (Id)))
2242 and then
2243 Type_In_Use
2244 (Etype (Next_Formal (First_Formal (Id))))));
2245 else
2246 if In_Use (P) and then not Is_Hidden (Id) then
2248 -- A child unit of a use-visible package remains use-visible
2249 -- only if it is itself a visible child unit. Otherwise it
2250 -- would remain visible in other contexts where P is use-
2251 -- visible, because once compiled it stays in the entity list
2252 -- of its parent unit.
2254 if Is_Child_Unit (Id) then
2255 Set_Is_Potentially_Use_Visible (Id,
2256 Is_Visible_Child_Unit (Id));
2257 else
2258 Set_Is_Potentially_Use_Visible (Id);
2259 end if;
2261 else
2262 Set_Is_Potentially_Use_Visible (Id, False);
2263 end if;
2264 end if;
2266 -- Local entities are not immediately visible outside of the package
2268 Set_Is_Immediately_Visible (Id, False);
2270 -- If this is a private type with a full view (for example a local
2271 -- subtype of a private type declared elsewhere), ensure that the
2272 -- full view is also removed from visibility: it may be exposed when
2273 -- swapping views in an instantiation.
2275 if Is_Type (Id)
2276 and then Present (Full_View (Id))
2277 then
2278 Set_Is_Immediately_Visible (Full_View (Id), False);
2279 end if;
2281 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2282 Check_Abstract_Overriding (Id);
2283 Check_Conventions (Id);
2284 end if;
2286 if (Ekind (Id) = E_Private_Type
2287 or else Ekind (Id) = E_Limited_Private_Type)
2288 and then No (Full_View (Id))
2289 and then not Is_Generic_Type (Id)
2290 and then not Is_Derived_Type (Id)
2291 then
2292 Error_Msg_N ("missing full declaration for private type&", Id);
2294 elsif Ekind (Id) = E_Record_Type_With_Private
2295 and then not Is_Generic_Type (Id)
2296 and then No (Full_View (Id))
2297 then
2298 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
2299 Error_Msg_N ("missing full declaration for private type&", Id);
2300 else
2301 Error_Msg_N
2302 ("missing full declaration for private extension", Id);
2303 end if;
2305 -- Case of constant, check for deferred constant declaration with
2306 -- no full view. Likely just a matter of a missing expression, or
2307 -- accidental use of the keyword constant.
2309 elsif Ekind (Id) = E_Constant
2311 -- OK if constant value present
2313 and then No (Constant_Value (Id))
2315 -- OK if full view present
2317 and then No (Full_View (Id))
2319 -- OK if imported, since that provides the completion
2321 and then not Is_Imported (Id)
2323 -- OK if object declaration replaced by renaming declaration as
2324 -- a result of OK_To_Rename processing (e.g. for concatenation)
2326 and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration
2328 -- OK if object declaration with the No_Initialization flag set
2330 and then not (Nkind (Parent (Id)) = N_Object_Declaration
2331 and then No_Initialization (Parent (Id)))
2332 then
2333 -- If no private declaration is present, we assume the user did
2334 -- not intend a deferred constant declaration and the problem
2335 -- is simply that the initializing expression is missing.
2337 if not Has_Private_Declaration (Etype (Id)) then
2339 -- We assume that the user did not intend a deferred constant
2340 -- declaration, and the expression is just missing.
2342 Error_Msg_N
2343 ("constant declaration requires initialization expression",
2344 Parent (Id));
2346 if Is_Limited_Type (Etype (Id)) then
2347 Error_Msg_N
2348 ("\if variable intended, remove CONSTANT from declaration",
2349 Parent (Id));
2350 end if;
2352 -- Otherwise if a private declaration is present, then we are
2353 -- missing the full declaration for the deferred constant.
2355 else
2356 Error_Msg_N
2357 ("missing full declaration for deferred constant (RM 7.4)",
2358 Id);
2360 if Is_Limited_Type (Etype (Id)) then
2361 Error_Msg_N
2362 ("\if variable intended, remove CONSTANT from declaration",
2363 Parent (Id));
2364 end if;
2365 end if;
2366 end if;
2368 Next_Entity (Id);
2369 end loop;
2371 -- If the specification was installed as the parent of a public child
2372 -- unit, the private declarations were not installed, and there is
2373 -- nothing to do.
2375 if not In_Private_Part (P) then
2376 return;
2377 else
2378 Set_In_Private_Part (P, False);
2379 end if;
2381 -- Make private entities invisible and exchange full and private
2382 -- declarations for private types. Id is now the first private entity
2383 -- in the package.
2385 while Present (Id) loop
2386 if Debug_Flag_E then
2387 Write_Str ("unlinking private entity ");
2388 Write_Int (Int (Id));
2389 Write_Eol;
2390 end if;
2392 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2393 Check_Abstract_Overriding (Id);
2394 Check_Conventions (Id);
2395 end if;
2397 Set_Is_Immediately_Visible (Id, False);
2399 if Is_Private_Base_Type (Id)
2400 and then Present (Full_View (Id))
2401 then
2402 Full := Full_View (Id);
2404 -- If the partial view is not declared in the visible part of the
2405 -- package (as is the case when it is a type derived from some
2406 -- other private type in the private part of the current package),
2407 -- no exchange takes place.
2409 if No (Parent (Id))
2410 or else List_Containing (Parent (Id))
2411 /= Visible_Declarations (Specification (Decl))
2412 then
2413 goto Next_Id;
2414 end if;
2416 -- The entry in the private part points to the full declaration,
2417 -- which is currently visible. Exchange them so only the private
2418 -- type declaration remains accessible, and link private and full
2419 -- declaration in the opposite direction. Before the actual
2420 -- exchange, we copy back attributes of the full view that must
2421 -- be available to the partial view too.
2423 Preserve_Full_Attributes (Id, Full);
2425 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
2427 if Is_Indefinite_Subtype (Full)
2428 and then not Is_Indefinite_Subtype (Id)
2429 then
2430 Error_Msg_N
2431 ("full view of type must be definite subtype", Full);
2432 end if;
2434 Priv_Elmt := First_Elmt (Private_Dependents (Id));
2436 -- Swap out the subtypes and derived types of Id that were
2437 -- compiled in this scope, or installed previously by
2438 -- Install_Private_Declarations.
2440 -- Before we do the swap, we verify the presence of the Full_View
2441 -- field which may be empty due to a swap by a previous call to
2442 -- End_Package_Scope (e.g. from the freezing mechanism).
2444 while Present (Priv_Elmt) loop
2445 Priv_Sub := Node (Priv_Elmt);
2447 if Present (Full_View (Priv_Sub)) then
2449 if Scope (Priv_Sub) = P
2450 or else not In_Open_Scopes (Scope (Priv_Sub))
2451 then
2452 Set_Is_Immediately_Visible (Priv_Sub, False);
2453 end if;
2455 if Is_Visible_Dependent (Priv_Sub) then
2456 Preserve_Full_Attributes
2457 (Priv_Sub, Full_View (Priv_Sub));
2458 Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub));
2459 Exchange_Declarations (Priv_Sub);
2460 end if;
2461 end if;
2463 Next_Elmt (Priv_Elmt);
2464 end loop;
2466 -- Now restore the type itself to its private view
2468 Exchange_Declarations (Id);
2470 -- If we have installed an underlying full view for a type derived
2471 -- from a private type in a child unit, restore the proper views
2472 -- of private and full view. See corresponding code in
2473 -- Install_Private_Declarations.
2475 -- After the exchange, Full denotes the private type in the
2476 -- visible part of the package.
2478 if Is_Private_Base_Type (Full)
2479 and then Present (Full_View (Full))
2480 and then Present (Underlying_Full_View (Full))
2481 and then In_Package_Body (Current_Scope)
2482 then
2483 Set_Full_View (Full, Underlying_Full_View (Full));
2484 Set_Underlying_Full_View (Full, Empty);
2485 end if;
2487 elsif Ekind (Id) = E_Incomplete_Type
2488 and then Comes_From_Source (Id)
2489 and then No (Full_View (Id))
2490 then
2491 -- Mark Taft amendment types. Verify that there are no primitive
2492 -- operations declared for the type (3.10.1(9)).
2494 Set_Has_Completion_In_Body (Id);
2496 declare
2497 Elmt : Elmt_Id;
2498 Subp : Entity_Id;
2500 begin
2501 Elmt := First_Elmt (Private_Dependents (Id));
2502 while Present (Elmt) loop
2503 Subp := Node (Elmt);
2505 -- Is_Primitive is tested because there can be cases where
2506 -- nonprimitive subprograms (in nested packages) are added
2507 -- to the Private_Dependents list.
2509 if Is_Overloadable (Subp) and then Is_Primitive (Subp) then
2510 Error_Msg_NE
2511 ("type& must be completed in the private part",
2512 Parent (Subp), Id);
2514 -- The result type of an access-to-function type cannot be a
2515 -- Taft-amendment type, unless the version is Ada 2012 or
2516 -- later (see AI05-151).
2518 elsif Ada_Version < Ada_2012
2519 and then Ekind (Subp) = E_Subprogram_Type
2520 then
2521 if Etype (Subp) = Id
2522 or else
2523 (Is_Class_Wide_Type (Etype (Subp))
2524 and then Etype (Etype (Subp)) = Id)
2525 then
2526 Error_Msg_NE
2527 ("type& must be completed in the private part",
2528 Associated_Node_For_Itype (Subp), Id);
2529 end if;
2530 end if;
2532 Next_Elmt (Elmt);
2533 end loop;
2534 end;
2536 elsif not Is_Child_Unit (Id)
2537 and then (not Is_Private_Type (Id)
2538 or else No (Full_View (Id)))
2539 then
2540 Set_Is_Hidden (Id);
2541 Set_Is_Potentially_Use_Visible (Id, False);
2542 end if;
2544 <<Next_Id>>
2545 Next_Entity (Id);
2546 end loop;
2547 end Uninstall_Declarations;
2549 ------------------------
2550 -- Unit_Requires_Body --
2551 ------------------------
2553 function Unit_Requires_Body (P : Entity_Id) return Boolean is
2554 E : Entity_Id;
2556 begin
2557 -- Imported entity never requires body. Right now, only subprograms can
2558 -- be imported, but perhaps in the future we will allow import of
2559 -- packages.
2561 if Is_Imported (P) then
2562 return False;
2564 -- Body required if library package with pragma Elaborate_Body
2566 elsif Has_Pragma_Elaborate_Body (P) then
2567 return True;
2569 -- Body required if subprogram
2571 elsif Is_Subprogram (P) or else Is_Generic_Subprogram (P) then
2572 return True;
2574 -- Treat a block as requiring a body
2576 elsif Ekind (P) = E_Block then
2577 return True;
2579 elsif Ekind (P) = E_Package
2580 and then Nkind (Parent (P)) = N_Package_Specification
2581 and then Present (Generic_Parent (Parent (P)))
2582 then
2583 declare
2584 G_P : constant Entity_Id := Generic_Parent (Parent (P));
2585 begin
2586 if Has_Pragma_Elaborate_Body (G_P) then
2587 return True;
2588 end if;
2589 end;
2590 end if;
2592 -- Otherwise search entity chain for entity requiring completion
2594 E := First_Entity (P);
2595 while Present (E) loop
2597 -- Always ignore child units. Child units get added to the entity
2598 -- list of a parent unit, but are not original entities of the
2599 -- parent, and so do not affect whether the parent needs a body.
2601 if Is_Child_Unit (E) then
2602 null;
2604 -- Ignore formal packages and their renamings
2606 elsif Ekind (E) = E_Package
2607 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
2608 N_Formal_Package_Declaration
2609 then
2610 null;
2612 -- Otherwise test to see if entity requires a completion.
2613 -- Note that subprogram entities whose declaration does not come
2614 -- from source are ignored here on the basis that we assume the
2615 -- expander will provide an implicit completion at some point.
2617 elsif (Is_Overloadable (E)
2618 and then Ekind (E) /= E_Enumeration_Literal
2619 and then Ekind (E) /= E_Operator
2620 and then not Is_Abstract_Subprogram (E)
2621 and then not Has_Completion (E)
2622 and then Comes_From_Source (Parent (E)))
2624 or else
2625 (Ekind (E) = E_Package
2626 and then E /= P
2627 and then not Has_Completion (E)
2628 and then Unit_Requires_Body (E))
2630 or else
2631 (Ekind (E) = E_Incomplete_Type
2632 and then No (Full_View (E))
2633 and then not Is_Generic_Type (E))
2635 or else
2636 ((Ekind (E) = E_Task_Type or else
2637 Ekind (E) = E_Protected_Type)
2638 and then not Has_Completion (E))
2640 or else
2641 (Ekind (E) = E_Generic_Package and then E /= P
2642 and then not Has_Completion (E)
2643 and then Unit_Requires_Body (E))
2645 or else
2646 (Is_Generic_Subprogram (E)
2647 and then not Has_Completion (E))
2649 then
2650 return True;
2652 -- Entity that does not require completion
2654 else
2655 null;
2656 end if;
2658 Next_Entity (E);
2659 end loop;
2661 return False;
2662 end Unit_Requires_Body;
2664 end Sem_Ch7;