Daily bump.
[official-gcc.git] / gcc / ada / sem_ch7.adb
blob505fe9d9916d7cb84f834c82715a7bf63d87e8e0
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-2013, 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));
561 if Present (V)
562 and then not Compile_Time_Known_Value_Or_Aggr (V)
563 then
564 return Abandon;
565 end if;
566 end if;
568 return OK;
569 end Check_Subprogram_Ref;
571 function Check_Subprogram_Refs is
572 new Traverse_Func (Check_Subprogram_Ref);
574 -- Start of processing for Has_Referencer
576 begin
577 if No (L) then
578 return False;
579 end if;
581 D := Last (L);
582 while Present (D) loop
583 K := Nkind (D);
585 if K in N_Body_Stub then
586 return True;
588 -- Processing for subprogram bodies
590 elsif K = N_Subprogram_Body then
591 if Acts_As_Spec (D) then
592 E := Defining_Entity (D);
594 -- An inlined body acts as a referencer. Note also
595 -- that we never reset Is_Public for an inlined
596 -- subprogram. Gigi requires Is_Public to be set.
598 -- Note that we test Has_Pragma_Inline here rather
599 -- than Is_Inlined. We are compiling this for a
600 -- client, and it is the client who will decide if
601 -- actual inlining should occur, so we need to assume
602 -- that the procedure could be inlined for the purpose
603 -- of accessing global entities.
605 if Has_Pragma_Inline (E) then
606 if Outer
607 and then Check_Subprogram_Refs (D) = OK
608 then
609 Has_Referencer_Except_For_Subprograms := True;
610 else
611 return True;
612 end if;
613 else
614 Set_Is_Public (E, False);
615 end if;
617 else
618 E := Corresponding_Spec (D);
620 if Present (E) then
622 -- A generic subprogram body acts as a referencer
624 if Is_Generic_Unit (E) then
625 return True;
626 end if;
628 if Has_Pragma_Inline (E) or else Is_Inlined (E) then
629 if Outer
630 and then Check_Subprogram_Refs (D) = OK
631 then
632 Has_Referencer_Except_For_Subprograms := True;
633 else
634 return True;
635 end if;
636 end if;
637 end if;
638 end if;
640 -- Processing for package bodies
642 elsif K = N_Package_Body
643 and then Present (Corresponding_Spec (D))
644 then
645 E := Corresponding_Spec (D);
647 -- Generic package body is a referencer. It would seem
648 -- that we only have to consider generics that can be
649 -- exported, i.e. where the corresponding spec is the
650 -- spec of the current package, but because of nested
651 -- instantiations, a fully private generic body may
652 -- export other private body entities. Furthermore,
653 -- regardless of whether there was a previous inlined
654 -- subprogram, (an instantiation of) the generic package
655 -- may reference any entity declared before it.
657 if Is_Generic_Unit (E) then
658 return True;
660 -- For non-generic package body, recurse into body unless
661 -- this is an instance, we ignore instances since they
662 -- cannot have references that affect outer entities.
664 elsif not Is_Generic_Instance (E)
665 and then not Has_Referencer_Except_For_Subprograms
666 then
667 if Has_Referencer
668 (Declarations (D), Outer => False)
669 then
670 return True;
671 end if;
672 end if;
674 -- Processing for package specs, recurse into declarations.
675 -- Again we skip this for the case of generic instances.
677 elsif K = N_Package_Declaration
678 and then not Has_Referencer_Except_For_Subprograms
679 then
680 S := Specification (D);
682 if not Is_Generic_Unit (Defining_Entity (S)) then
683 if Has_Referencer
684 (Private_Declarations (S), Outer => False)
685 then
686 return True;
687 elsif Has_Referencer
688 (Visible_Declarations (S), Outer => False)
689 then
690 return True;
691 end if;
692 end if;
694 -- Objects and exceptions need not be public if we have not
695 -- encountered a referencer so far. We only reset the flag
696 -- for outer level entities that are not imported/exported,
697 -- and which have no interface name.
699 elsif Nkind_In (K, N_Object_Declaration,
700 N_Exception_Declaration,
701 N_Subprogram_Declaration)
702 then
703 E := Defining_Entity (D);
705 if Outer
706 and then (not Has_Referencer_Except_For_Subprograms
707 or else K = N_Subprogram_Declaration)
708 and then not Is_Imported (E)
709 and then not Is_Exported (E)
710 and then No (Interface_Name (E))
711 then
712 Set_Is_Public (E, False);
713 end if;
714 end if;
716 Prev (D);
717 end loop;
719 return Has_Referencer_Except_For_Subprograms;
720 end Has_Referencer;
722 -- Start of processing for Make_Non_Public_Where_Possible
724 begin
725 declare
726 Discard : Boolean;
727 pragma Warnings (Off, Discard);
729 begin
730 Discard := Has_Referencer (Declarations (N), Outer => True);
731 end;
732 end Make_Non_Public_Where_Possible;
733 end if;
735 -- If expander is not active, then here is where we turn off the
736 -- In_Package_Body flag, otherwise it is turned off at the end of the
737 -- corresponding expansion routine. If this is an instance body, we need
738 -- to qualify names of local entities, because the body may have been
739 -- compiled as a preliminary to another instantiation.
741 if not Expander_Active then
742 Set_In_Package_Body (Spec_Id, False);
744 if Is_Generic_Instance (Spec_Id)
745 and then Operating_Mode = Generate_Code
746 then
747 Qualify_Entity_Names (N);
748 end if;
749 end if;
750 end Analyze_Package_Body_Helper;
752 ---------------------------------
753 -- Analyze_Package_Declaration --
754 ---------------------------------
756 procedure Analyze_Package_Declaration (N : Node_Id) is
757 Id : constant Node_Id := Defining_Entity (N);
759 PF : Boolean;
760 -- True when in the context of a declared pure library unit
762 Body_Required : Boolean;
763 -- True when this package declaration requires a corresponding body
765 Comp_Unit : Boolean;
766 -- True when this package declaration is not a nested declaration
768 begin
769 -- Analye aspect specifications immediately, since we need to recognize
770 -- things like Pure early enough to diagnose violations during analysis.
772 if Has_Aspects (N) then
773 Analyze_Aspect_Specifications (N, Id);
774 end if;
776 -- Ada 2005 (AI-217): Check if the package has been erroneously named
777 -- in a limited-with clause of its own context. In this case the error
778 -- has been previously notified by Analyze_Context.
780 -- limited with Pkg; -- ERROR
781 -- package Pkg is ...
783 if From_With_Type (Id) then
784 return;
785 end if;
787 if Debug_Flag_C then
788 Write_Str ("==> package spec ");
789 Write_Name (Chars (Id));
790 Write_Str (" from ");
791 Write_Location (Sloc (N));
792 Write_Eol;
793 Indent;
794 end if;
796 Generate_Definition (Id);
797 Enter_Name (Id);
798 Set_Ekind (Id, E_Package);
799 Set_Etype (Id, Standard_Void_Type);
801 Push_Scope (Id);
803 PF := Is_Pure (Enclosing_Lib_Unit_Entity);
804 Set_Is_Pure (Id, PF);
806 Set_Categorization_From_Pragmas (N);
808 Analyze (Specification (N));
809 Validate_Categorization_Dependency (N, Id);
811 Body_Required := Unit_Requires_Body (Id);
813 -- When this spec does not require an explicit body, we know that there
814 -- are no entities requiring completion in the language sense; we call
815 -- Check_Completion here only to ensure that any nested package
816 -- declaration that requires an implicit body gets one. (In the case
817 -- where a body is required, Check_Completion is called at the end of
818 -- the body's declarative part.)
820 if not Body_Required then
821 Check_Completion;
822 end if;
824 Comp_Unit := Nkind (Parent (N)) = N_Compilation_Unit;
825 if Comp_Unit then
827 -- Set Body_Required indication on the compilation unit node, and
828 -- determine whether elaboration warnings may be meaningful on it.
830 Set_Body_Required (Parent (N), Body_Required);
832 if not Body_Required then
833 Set_Suppress_Elaboration_Warnings (Id);
834 end if;
836 end if;
838 End_Package_Scope (Id);
840 -- For the declaration of a library unit that is a remote types package,
841 -- check legality rules regarding availability of stream attributes for
842 -- types that contain non-remote access values. This subprogram performs
843 -- visibility tests that rely on the fact that we have exited the scope
844 -- of Id.
846 if Comp_Unit then
847 Validate_RT_RAT_Component (N);
848 end if;
850 if Debug_Flag_C then
851 Outdent;
852 Write_Str ("<== package spec ");
853 Write_Name (Chars (Id));
854 Write_Str (" from ");
855 Write_Location (Sloc (N));
856 Write_Eol;
857 end if;
858 end Analyze_Package_Declaration;
860 -----------------------------------
861 -- Analyze_Package_Specification --
862 -----------------------------------
864 -- Note that this code is shared for the analysis of generic package specs
865 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
867 procedure Analyze_Package_Specification (N : Node_Id) is
868 Id : constant Entity_Id := Defining_Entity (N);
869 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
870 Vis_Decls : constant List_Id := Visible_Declarations (N);
871 Priv_Decls : constant List_Id := Private_Declarations (N);
872 E : Entity_Id;
873 L : Entity_Id;
874 Public_Child : Boolean;
876 Private_With_Clauses_Installed : Boolean := False;
877 -- In Ada 2005, private with_clauses are visible in the private part
878 -- of a nested package, even if it appears in the public part of the
879 -- enclosing package. This requires a separate step to install these
880 -- private_with_clauses, and remove them at the end of the nested
881 -- package.
883 procedure Check_One_Tagged_Type_Or_Extension_At_Most;
884 -- Issue an error in SPARK mode if a package specification contains
885 -- more than one tagged type or type extension.
887 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
888 -- Clears constant indications (Never_Set_In_Source, Constant_Value, and
889 -- Is_True_Constant) on all variables that are entities of Id, and on
890 -- the chain whose first element is FE. A recursive call is made for all
891 -- packages and generic packages.
893 procedure Generate_Parent_References;
894 -- For a child unit, generate references to parent units, for
895 -- GPS navigation purposes.
897 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
898 -- Child and Unit are entities of compilation units. True if Child
899 -- is a public child of Parent as defined in 10.1.1
901 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
902 -- Reject completion of an incomplete or private type declarations
903 -- having a known discriminant part by an unchecked union.
905 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
906 -- Given the package entity of a generic package instantiation or
907 -- formal package whose corresponding generic is a child unit, installs
908 -- the private declarations of each of the child unit's parents.
909 -- This has to be done at the point of entering the instance package's
910 -- private part rather than being done in Sem_Ch12.Install_Parent
911 -- (which is where the parents' visible declarations are installed).
913 ------------------------------------------------
914 -- Check_One_Tagged_Type_Or_Extension_At_Most --
915 ------------------------------------------------
917 procedure Check_One_Tagged_Type_Or_Extension_At_Most is
918 Previous : Node_Id;
920 procedure Check_Decls (Decls : List_Id);
921 -- Check that either Previous is Empty and Decls does not contain
922 -- more than one tagged type or type extension, or Previous is
923 -- already set and Decls contains no tagged type or type extension.
925 -----------------
926 -- Check_Decls --
927 -----------------
929 procedure Check_Decls (Decls : List_Id) is
930 Decl : Node_Id;
932 begin
933 Decl := First (Decls);
934 while Present (Decl) loop
935 if Nkind (Decl) = N_Full_Type_Declaration
936 and then Is_Tagged_Type (Defining_Identifier (Decl))
937 then
938 if No (Previous) then
939 Previous := Decl;
941 else
942 Error_Msg_Sloc := Sloc (Previous);
943 Check_SPARK_Restriction
944 ("at most one tagged type or type extension allowed",
945 "\\ previous declaration#",
946 Decl);
947 end if;
948 end if;
950 Next (Decl);
951 end loop;
952 end Check_Decls;
954 -- Start of processing for Check_One_Tagged_Type_Or_Extension_At_Most
956 begin
957 Previous := Empty;
958 Check_Decls (Vis_Decls);
960 if Present (Priv_Decls) then
961 Check_Decls (Priv_Decls);
962 end if;
963 end Check_One_Tagged_Type_Or_Extension_At_Most;
965 ---------------------
966 -- Clear_Constants --
967 ---------------------
969 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
970 E : Entity_Id;
972 begin
973 -- Ignore package renamings, not interesting and they can cause self
974 -- referential loops in the code below.
976 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
977 return;
978 end if;
980 -- Note: in the loop below, the check for Next_Entity pointing back
981 -- to the package entity may seem odd, but it is needed, because a
982 -- package can contain a renaming declaration to itself, and such
983 -- renamings are generated automatically within package instances.
985 E := FE;
986 while Present (E) and then E /= Id loop
987 if Is_Assignable (E) then
988 Set_Never_Set_In_Source (E, False);
989 Set_Is_True_Constant (E, False);
990 Set_Current_Value (E, Empty);
991 Set_Is_Known_Null (E, False);
992 Set_Last_Assignment (E, Empty);
994 if not Can_Never_Be_Null (E) then
995 Set_Is_Known_Non_Null (E, False);
996 end if;
998 elsif Is_Package_Or_Generic_Package (E) then
999 Clear_Constants (E, First_Entity (E));
1000 Clear_Constants (E, First_Private_Entity (E));
1001 end if;
1003 Next_Entity (E);
1004 end loop;
1005 end Clear_Constants;
1007 --------------------------------
1008 -- Generate_Parent_References --
1009 --------------------------------
1011 procedure Generate_Parent_References is
1012 Decl : constant Node_Id := Parent (N);
1014 begin
1015 if Id = Cunit_Entity (Main_Unit)
1016 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
1017 then
1018 Generate_Reference (Id, Scope (Id), 'k', False);
1020 elsif not Nkind_In (Unit (Cunit (Main_Unit)), N_Subprogram_Body,
1021 N_Subunit)
1022 then
1023 -- If current unit is an ancestor of main unit, generate a
1024 -- reference to its own parent.
1026 declare
1027 U : Node_Id;
1028 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
1030 begin
1031 if Nkind (Main_Spec) = N_Package_Body then
1032 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
1033 end if;
1035 U := Parent_Spec (Main_Spec);
1036 while Present (U) loop
1037 if U = Parent (Decl) then
1038 Generate_Reference (Id, Scope (Id), 'k', False);
1039 exit;
1041 elsif Nkind (Unit (U)) = N_Package_Body then
1042 exit;
1044 else
1045 U := Parent_Spec (Unit (U));
1046 end if;
1047 end loop;
1048 end;
1049 end if;
1050 end Generate_Parent_References;
1052 ---------------------
1053 -- Is_Public_Child --
1054 ---------------------
1056 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
1057 begin
1058 if not Is_Private_Descendant (Child) then
1059 return True;
1060 else
1061 if Child = Unit then
1062 return not Private_Present (
1063 Parent (Unit_Declaration_Node (Child)));
1064 else
1065 return Is_Public_Child (Scope (Child), Unit);
1066 end if;
1067 end if;
1068 end Is_Public_Child;
1070 ----------------------------------------
1071 -- Inspect_Unchecked_Union_Completion --
1072 ----------------------------------------
1074 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
1075 Decl : Node_Id;
1077 begin
1078 Decl := First (Decls);
1079 while Present (Decl) loop
1081 -- We are looking at an incomplete or private type declaration
1082 -- with a known_discriminant_part whose full view is an
1083 -- Unchecked_Union.
1085 if Nkind_In (Decl, N_Incomplete_Type_Declaration,
1086 N_Private_Type_Declaration)
1087 and then Has_Discriminants (Defining_Identifier (Decl))
1088 and then Present (Full_View (Defining_Identifier (Decl)))
1089 and then
1090 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
1091 then
1092 Error_Msg_N
1093 ("completion of discriminated partial view "
1094 & "cannot be an unchecked union",
1095 Full_View (Defining_Identifier (Decl)));
1096 end if;
1098 Next (Decl);
1099 end loop;
1100 end Inspect_Unchecked_Union_Completion;
1102 -----------------------------------------
1103 -- Install_Parent_Private_Declarations --
1104 -----------------------------------------
1106 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
1107 Inst_Par : Entity_Id;
1108 Gen_Par : Entity_Id;
1109 Inst_Node : Node_Id;
1111 begin
1112 Inst_Par := Inst_Id;
1114 Gen_Par :=
1115 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
1116 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
1117 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
1119 if Nkind_In (Inst_Node, N_Package_Instantiation,
1120 N_Formal_Package_Declaration)
1121 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
1122 then
1123 Inst_Par := Entity (Prefix (Name (Inst_Node)));
1125 if Present (Renamed_Entity (Inst_Par)) then
1126 Inst_Par := Renamed_Entity (Inst_Par);
1127 end if;
1129 Gen_Par :=
1130 Generic_Parent
1131 (Specification (Unit_Declaration_Node (Inst_Par)));
1133 -- Install the private declarations and private use clauses
1134 -- of a parent instance of the child instance, unless the
1135 -- parent instance private declarations have already been
1136 -- installed earlier in Analyze_Package_Specification, which
1137 -- happens when a generic child is instantiated, and the
1138 -- instance is a child of the parent instance.
1140 -- Installing the use clauses of the parent instance twice
1141 -- is both unnecessary and wrong, because it would cause the
1142 -- clauses to be chained to themselves in the use clauses
1143 -- list of the scope stack entry. That in turn would cause
1144 -- an endless loop from End_Use_Clauses upon scope exit.
1146 -- The parent is now fully visible. It may be a hidden open
1147 -- scope if we are currently compiling some child instance
1148 -- declared within it, but while the current instance is being
1149 -- compiled the parent is immediately visible. In particular
1150 -- its entities must remain visible if a stack save/restore
1151 -- takes place through a call to Rtsfind.
1153 if Present (Gen_Par) then
1154 if not In_Private_Part (Inst_Par) then
1155 Install_Private_Declarations (Inst_Par);
1156 Set_Use (Private_Declarations
1157 (Specification
1158 (Unit_Declaration_Node (Inst_Par))));
1159 Set_Is_Hidden_Open_Scope (Inst_Par, False);
1160 end if;
1162 -- If we've reached the end of the generic instance parents,
1163 -- then finish off by looping through the nongeneric parents
1164 -- and installing their private declarations.
1166 else
1167 while Present (Inst_Par)
1168 and then Inst_Par /= Standard_Standard
1169 and then (not In_Open_Scopes (Inst_Par)
1170 or else not In_Private_Part (Inst_Par))
1171 loop
1172 Install_Private_Declarations (Inst_Par);
1173 Set_Use (Private_Declarations
1174 (Specification
1175 (Unit_Declaration_Node (Inst_Par))));
1176 Inst_Par := Scope (Inst_Par);
1177 end loop;
1179 exit;
1180 end if;
1182 else
1183 exit;
1184 end if;
1185 end loop;
1186 end Install_Parent_Private_Declarations;
1188 -- Start of processing for Analyze_Package_Specification
1190 begin
1191 if Present (Vis_Decls) then
1192 Analyze_Declarations (Vis_Decls);
1193 end if;
1195 -- Verify that incomplete types have received full declarations and
1196 -- also build invariant procedures for any types with invariants.
1198 E := First_Entity (Id);
1199 while Present (E) loop
1201 -- Check on incomplete types
1203 -- AI05-0213: A formal incomplete type has no completion
1205 if Ekind (E) = E_Incomplete_Type
1206 and then No (Full_View (E))
1207 and then not Is_Generic_Type (E)
1208 then
1209 Error_Msg_N ("no declaration in visible part for incomplete}", E);
1210 end if;
1212 -- Build invariant procedures
1214 if Is_Type (E) and then Has_Invariants (E) then
1215 Build_Invariant_Procedure (E, N);
1216 end if;
1218 Next_Entity (E);
1219 end loop;
1221 if Is_Remote_Call_Interface (Id)
1222 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1223 then
1224 Validate_RCI_Declarations (Id);
1225 end if;
1227 -- Save global references in the visible declarations, before installing
1228 -- private declarations of parent unit if there is one, because the
1229 -- privacy status of types defined in the parent will change. This is
1230 -- only relevant for generic child units, but is done in all cases for
1231 -- uniformity.
1233 if Ekind (Id) = E_Generic_Package
1234 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1235 then
1236 declare
1237 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1238 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1239 begin
1240 Set_Private_Declarations (Orig_Spec, Empty_List);
1241 Save_Global_References (Orig_Decl);
1242 Set_Private_Declarations (Orig_Spec, Save_Priv);
1243 end;
1244 end if;
1246 -- If package is a public child unit, then make the private declarations
1247 -- of the parent visible.
1249 Public_Child := False;
1251 declare
1252 Par : Entity_Id;
1253 Pack_Decl : Node_Id;
1254 Par_Spec : Node_Id;
1256 begin
1257 Par := Id;
1258 Par_Spec := Parent_Spec (Parent (N));
1260 -- If the package is formal package of an enclosing generic, it is
1261 -- transformed into a local generic declaration, and compiled to make
1262 -- its spec available. We need to retrieve the original generic to
1263 -- determine whether it is a child unit, and install its parents.
1265 if No (Par_Spec)
1266 and then
1267 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1268 then
1269 Par := Entity (Name (Original_Node (Parent (N))));
1270 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1271 end if;
1273 if Present (Par_Spec) then
1274 Generate_Parent_References;
1276 while Scope (Par) /= Standard_Standard
1277 and then Is_Public_Child (Id, Par)
1278 and then In_Open_Scopes (Par)
1279 loop
1280 Public_Child := True;
1281 Par := Scope (Par);
1282 Install_Private_Declarations (Par);
1283 Install_Private_With_Clauses (Par);
1284 Pack_Decl := Unit_Declaration_Node (Par);
1285 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1286 end loop;
1287 end if;
1288 end;
1290 if Is_Compilation_Unit (Id) then
1291 Install_Private_With_Clauses (Id);
1292 else
1294 -- The current compilation unit may include private with_clauses,
1295 -- which are visible in the private part of the current nested
1296 -- package, and have to be installed now. This is not done for
1297 -- nested instantiations, where the private with_clauses of the
1298 -- enclosing unit have no effect once the instantiation info is
1299 -- established and we start analyzing the package declaration.
1301 declare
1302 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1303 begin
1304 if Is_Package_Or_Generic_Package (Comp_Unit)
1305 and then not In_Private_Part (Comp_Unit)
1306 and then not In_Instance
1307 then
1308 Install_Private_With_Clauses (Comp_Unit);
1309 Private_With_Clauses_Installed := True;
1310 end if;
1311 end;
1312 end if;
1314 -- If this is a package associated with a generic instance or formal
1315 -- package, then the private declarations of each of the generic's
1316 -- parents must be installed at this point.
1318 if Is_Generic_Instance (Id) then
1319 Install_Parent_Private_Declarations (Id);
1320 end if;
1322 -- Analyze private part if present. The flag In_Private_Part is reset
1323 -- in End_Package_Scope.
1325 L := Last_Entity (Id);
1327 if Present (Priv_Decls) then
1328 Set_In_Private_Part (Id);
1330 -- Upon entering a public child's private part, it may be necessary
1331 -- to declare subprograms that were derived in the package's visible
1332 -- part but not yet made visible.
1334 if Public_Child then
1335 Declare_Inherited_Private_Subprograms (Id);
1336 end if;
1338 Analyze_Declarations (Priv_Decls);
1340 -- Check the private declarations for incomplete deferred constants
1342 Inspect_Deferred_Constant_Completion (Priv_Decls);
1344 -- The first private entity is the immediate follower of the last
1345 -- visible entity, if there was one.
1347 if Present (L) then
1348 Set_First_Private_Entity (Id, Next_Entity (L));
1349 else
1350 Set_First_Private_Entity (Id, First_Entity (Id));
1351 end if;
1353 -- There may be inherited private subprograms that need to be declared,
1354 -- even in the absence of an explicit private part. If there are any
1355 -- public declarations in the package and the package is a public child
1356 -- unit, then an implicit private part is assumed.
1358 elsif Present (L) and then Public_Child then
1359 Set_In_Private_Part (Id);
1360 Declare_Inherited_Private_Subprograms (Id);
1361 Set_First_Private_Entity (Id, Next_Entity (L));
1362 end if;
1364 E := First_Entity (Id);
1365 while Present (E) loop
1367 -- Check rule of 3.6(11), which in general requires waiting till all
1368 -- full types have been seen.
1370 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1371 Check_Aliased_Component_Types (E);
1372 end if;
1374 -- Check preelaborable initialization for full type completing a
1375 -- private type for which pragma Preelaborable_Initialization given.
1377 if Is_Type (E)
1378 and then Must_Have_Preelab_Init (E)
1379 and then not Has_Preelaborable_Initialization (E)
1380 then
1381 Error_Msg_N
1382 ("full view of & does not have preelaborable initialization", E);
1383 end if;
1385 -- An invariant may appear on a full view of a type
1387 if Is_Type (E)
1388 and then Has_Private_Declaration (E)
1389 and then Nkind (Parent (E)) = N_Full_Type_Declaration
1390 and then Has_Aspects (Parent (E))
1391 then
1392 declare
1393 ASN : Node_Id;
1395 begin
1396 ASN := First (Aspect_Specifications (Parent (E)));
1397 while Present (ASN) loop
1398 if Nam_In (Chars (Identifier (ASN)), Name_Invariant,
1399 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
1665 (DT_Position (New_Op) = 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 Full : Entity_Id;
1817 Priv_Deps : Elist_Id;
1819 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
1820 -- When the full view of a private type is made available, we do the
1821 -- same for its private dependents under proper visibility conditions.
1822 -- When compiling a grand-chid unit this needs to be done recursively.
1824 -----------------------------
1825 -- Swap_Private_Dependents --
1826 -----------------------------
1828 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
1829 Deps : Elist_Id;
1830 Priv : Entity_Id;
1831 Priv_Elmt : Elmt_Id;
1832 Is_Priv : Boolean;
1834 begin
1835 Priv_Elmt := First_Elmt (Priv_Deps);
1836 while Present (Priv_Elmt) loop
1837 Priv := Node (Priv_Elmt);
1839 -- Before the exchange, verify that the presence of the Full_View
1840 -- field. This field will be empty if the entity has already been
1841 -- installed due to a previous call.
1843 if Present (Full_View (Priv))
1844 and then Is_Visible_Dependent (Priv)
1845 then
1846 if Is_Private_Type (Priv) then
1847 Deps := Private_Dependents (Priv);
1848 Is_Priv := True;
1849 else
1850 Is_Priv := False;
1851 end if;
1853 -- For each subtype that is swapped, we also swap the reference
1854 -- to it in Private_Dependents, to allow access to it when we
1855 -- swap them out in End_Package_Scope.
1857 Replace_Elmt (Priv_Elmt, Full_View (Priv));
1858 Exchange_Declarations (Priv);
1859 Set_Is_Immediately_Visible
1860 (Priv, In_Open_Scopes (Scope (Priv)));
1861 Set_Is_Potentially_Use_Visible
1862 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
1864 -- Within a child unit, recurse, except in generic child unit,
1865 -- which (unfortunately) handle private_dependents separately.
1867 if Is_Priv
1868 and then Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
1869 and then not Is_Empty_Elmt_List (Deps)
1870 and then not Inside_A_Generic
1871 then
1872 Swap_Private_Dependents (Deps);
1873 end if;
1874 end if;
1876 Next_Elmt (Priv_Elmt);
1877 end loop;
1878 end Swap_Private_Dependents;
1880 -- Start of processing for Install_Private_Declarations
1882 begin
1883 -- First exchange declarations for private types, so that the full
1884 -- declaration is visible. For each private type, we check its
1885 -- Private_Dependents list and also exchange any subtypes of or derived
1886 -- types from it. Finally, if this is a Taft amendment type, the
1887 -- incomplete declaration is irrelevant, and we want to link the
1888 -- eventual full declaration with the original private one so we
1889 -- also skip the exchange.
1891 Id := First_Entity (P);
1892 while Present (Id) and then Id /= First_Private_Entity (P) loop
1893 if Is_Private_Base_Type (Id)
1894 and then Comes_From_Source (Full_View (Id))
1895 and then Present (Full_View (Id))
1896 and then Scope (Full_View (Id)) = Scope (Id)
1897 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
1898 then
1899 -- If there is a use-type clause on the private type, set the full
1900 -- view accordingly.
1902 Set_In_Use (Full_View (Id), In_Use (Id));
1903 Full := Full_View (Id);
1905 if Is_Private_Base_Type (Full)
1906 and then Has_Private_Declaration (Full)
1907 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
1908 and then In_Open_Scopes (Scope (Etype (Full)))
1909 and then In_Package_Body (Current_Scope)
1910 and then not Is_Private_Type (Etype (Full))
1911 then
1912 -- This is the completion of a private type by a derivation
1913 -- from another private type which is not private anymore. This
1914 -- can only happen in a package nested within a child package,
1915 -- when the parent type is defined in the parent unit. At this
1916 -- point the current type is not private either, and we have
1917 -- to install the underlying full view, which is now visible.
1918 -- Save the current full view as well, so that all views can be
1919 -- restored on exit. It may seem that after compiling the child
1920 -- body there are not environments to restore, but the back-end
1921 -- expects those links to be valid, and freeze nodes depend on
1922 -- them.
1924 if No (Full_View (Full))
1925 and then Present (Underlying_Full_View (Full))
1926 then
1927 Set_Full_View (Id, Underlying_Full_View (Full));
1928 Set_Underlying_Full_View (Id, Full);
1930 Set_Underlying_Full_View (Full, Empty);
1931 Set_Is_Frozen (Full_View (Id));
1932 end if;
1933 end if;
1935 Priv_Deps := Private_Dependents (Id);
1936 Exchange_Declarations (Id);
1937 Set_Is_Immediately_Visible (Id);
1938 Swap_Private_Dependents (Priv_Deps);
1939 end if;
1941 Next_Entity (Id);
1942 end loop;
1944 -- Next make other declarations in the private part visible as well
1946 Id := First_Private_Entity (P);
1947 while Present (Id) loop
1948 Install_Package_Entity (Id);
1949 Set_Is_Hidden (Id, False);
1950 Next_Entity (Id);
1951 end loop;
1953 -- Indicate that the private part is currently visible, so it can be
1954 -- properly reset on exit.
1956 Set_In_Private_Part (P);
1957 end Install_Private_Declarations;
1959 ----------------------------------
1960 -- Install_Visible_Declarations --
1961 ----------------------------------
1963 procedure Install_Visible_Declarations (P : Entity_Id) is
1964 Id : Entity_Id;
1965 Last_Entity : Entity_Id;
1967 begin
1968 pragma Assert
1969 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
1971 if Is_Package_Or_Generic_Package (P) then
1972 Last_Entity := First_Private_Entity (P);
1973 else
1974 Last_Entity := Empty;
1975 end if;
1977 Id := First_Entity (P);
1978 while Present (Id) and then Id /= Last_Entity loop
1979 Install_Package_Entity (Id);
1980 Next_Entity (Id);
1981 end loop;
1982 end Install_Visible_Declarations;
1984 --------------------------
1985 -- Is_Private_Base_Type --
1986 --------------------------
1988 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
1989 begin
1990 return Ekind (E) = E_Private_Type
1991 or else Ekind (E) = E_Limited_Private_Type
1992 or else Ekind (E) = E_Record_Type_With_Private;
1993 end Is_Private_Base_Type;
1995 --------------------------
1996 -- Is_Visible_Dependent --
1997 --------------------------
1999 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
2001 S : constant Entity_Id := Scope (Dep);
2003 begin
2004 -- Renamings created for actual types have the visibility of the actual
2006 if Ekind (S) = E_Package
2007 and then Is_Generic_Instance (S)
2008 and then (Is_Generic_Actual_Type (Dep)
2009 or else Is_Generic_Actual_Type (Full_View (Dep)))
2010 then
2011 return True;
2013 elsif not (Is_Derived_Type (Dep))
2014 and then Is_Derived_Type (Full_View (Dep))
2015 then
2016 -- When instantiating a package body, the scope stack is empty, so
2017 -- check instead whether the dependent type is defined in the same
2018 -- scope as the instance itself.
2020 return In_Open_Scopes (S)
2021 or else (Is_Generic_Instance (Current_Scope)
2022 and then Scope (Dep) = Scope (Current_Scope));
2023 else
2024 return True;
2025 end if;
2026 end Is_Visible_Dependent;
2028 ----------------------------
2029 -- May_Need_Implicit_Body --
2030 ----------------------------
2032 procedure May_Need_Implicit_Body (E : Entity_Id) is
2033 P : constant Node_Id := Unit_Declaration_Node (E);
2034 S : constant Node_Id := Parent (P);
2035 B : Node_Id;
2036 Decls : List_Id;
2038 begin
2039 if not Has_Completion (E)
2040 and then Nkind (P) = N_Package_Declaration
2041 and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
2042 then
2043 B :=
2044 Make_Package_Body (Sloc (E),
2045 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
2046 Chars => Chars (E)),
2047 Declarations => New_List);
2049 if Nkind (S) = N_Package_Specification then
2050 if Present (Private_Declarations (S)) then
2051 Decls := Private_Declarations (S);
2052 else
2053 Decls := Visible_Declarations (S);
2054 end if;
2055 else
2056 Decls := Declarations (S);
2057 end if;
2059 Append (B, Decls);
2060 Analyze (B);
2061 end if;
2062 end May_Need_Implicit_Body;
2064 ----------------------
2065 -- New_Private_Type --
2066 ----------------------
2068 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
2069 begin
2070 -- For other than Ada 2012, enter the name in the current scope
2072 if Ada_Version < Ada_2012 then
2073 Enter_Name (Id);
2075 -- Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2076 -- there may be an incomplete previous view.
2078 else
2079 declare
2080 Prev : Entity_Id;
2081 begin
2082 Prev := Find_Type_Name (N);
2083 pragma Assert (Prev = Id
2084 or else (Ekind (Prev) = E_Incomplete_Type
2085 and then Present (Full_View (Prev))
2086 and then Full_View (Prev) = Id));
2087 end;
2088 end if;
2090 if Limited_Present (Def) then
2091 Set_Ekind (Id, E_Limited_Private_Type);
2092 else
2093 Set_Ekind (Id, E_Private_Type);
2094 end if;
2096 Set_Etype (Id, Id);
2097 Set_Has_Delayed_Freeze (Id);
2098 Set_Is_First_Subtype (Id);
2099 Init_Size_Align (Id);
2101 Set_Is_Constrained (Id,
2102 No (Discriminant_Specifications (N))
2103 and then not Unknown_Discriminants_Present (N));
2105 -- Set tagged flag before processing discriminants, to catch illegal
2106 -- usage.
2108 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
2110 Set_Discriminant_Constraint (Id, No_Elist);
2111 Set_Stored_Constraint (Id, No_Elist);
2113 if Present (Discriminant_Specifications (N)) then
2114 Push_Scope (Id);
2115 Process_Discriminants (N);
2116 End_Scope;
2118 elsif Unknown_Discriminants_Present (N) then
2119 Set_Has_Unknown_Discriminants (Id);
2120 end if;
2122 Set_Private_Dependents (Id, New_Elmt_List);
2124 if Tagged_Present (Def) then
2125 Set_Ekind (Id, E_Record_Type_With_Private);
2126 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2127 Set_Is_Abstract_Type (Id, Abstract_Present (Def));
2128 Set_Is_Limited_Record (Id, Limited_Present (Def));
2129 Set_Has_Delayed_Freeze (Id, True);
2131 -- Create a class-wide type with the same attributes
2133 Make_Class_Wide_Type (Id);
2135 elsif Abstract_Present (Def) then
2136 Error_Msg_N ("only a tagged type can be abstract", N);
2137 end if;
2138 end New_Private_Type;
2140 ----------------------------
2141 -- Uninstall_Declarations --
2142 ----------------------------
2144 procedure Uninstall_Declarations (P : Entity_Id) is
2145 Decl : constant Node_Id := Unit_Declaration_Node (P);
2146 Id : Entity_Id;
2147 Full : Entity_Id;
2148 Priv_Elmt : Elmt_Id;
2149 Priv_Sub : Entity_Id;
2151 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id);
2152 -- Copy to the private declaration the attributes of the full view that
2153 -- need to be available for the partial view also.
2155 function Type_In_Use (T : Entity_Id) return Boolean;
2156 -- Check whether type or base type appear in an active use_type clause
2158 ------------------------------
2159 -- Preserve_Full_Attributes --
2160 ------------------------------
2162 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id) is
2163 Priv_Is_Base_Type : constant Boolean := Is_Base_Type (Priv);
2165 begin
2166 Set_Size_Info (Priv, (Full));
2167 Set_RM_Size (Priv, RM_Size (Full));
2168 Set_Size_Known_At_Compile_Time
2169 (Priv, Size_Known_At_Compile_Time (Full));
2170 Set_Is_Volatile (Priv, Is_Volatile (Full));
2171 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
2172 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full));
2173 Set_Is_Ada_2012_Only (Priv, Is_Ada_2012_Only (Full));
2174 Set_Has_Pragma_Unmodified (Priv, Has_Pragma_Unmodified (Full));
2175 Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced (Full));
2176 Set_Has_Pragma_Unreferenced_Objects
2177 (Priv, Has_Pragma_Unreferenced_Objects
2178 (Full));
2179 if Is_Unchecked_Union (Full) then
2180 Set_Is_Unchecked_Union (Base_Type (Priv));
2181 end if;
2182 -- Why is atomic not copied here ???
2184 if Referenced (Full) then
2185 Set_Referenced (Priv);
2186 end if;
2188 if Priv_Is_Base_Type then
2189 Set_Is_Controlled (Priv, Is_Controlled (Base_Type (Full)));
2190 Set_Finalize_Storage_Only (Priv, Finalize_Storage_Only
2191 (Base_Type (Full)));
2192 Set_Has_Task (Priv, Has_Task (Base_Type (Full)));
2193 Set_Has_Controlled_Component (Priv, Has_Controlled_Component
2194 (Base_Type (Full)));
2195 end if;
2197 Set_Freeze_Node (Priv, Freeze_Node (Full));
2199 -- Propagate information of type invariants, which may be specified
2200 -- for the full view.
2202 if Has_Invariants (Full) and not Has_Invariants (Priv) then
2203 Set_Has_Invariants (Priv);
2204 Set_Subprograms_For_Type (Priv, Subprograms_For_Type (Full));
2205 end if;
2207 if Is_Tagged_Type (Priv)
2208 and then Is_Tagged_Type (Full)
2209 and then not Error_Posted (Full)
2210 then
2211 if Is_Tagged_Type (Priv) then
2213 -- If the type is tagged, the tag itself must be available on
2214 -- the partial view, for expansion purposes.
2216 Set_First_Entity (Priv, First_Entity (Full));
2218 -- If there are discriminants in the partial view, these remain
2219 -- visible. Otherwise only the tag itself is visible, and there
2220 -- are no nameable components in the partial view.
2222 if No (Last_Entity (Priv)) then
2223 Set_Last_Entity (Priv, First_Entity (Priv));
2224 end if;
2225 end if;
2227 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2229 if Has_Discriminants (Full) then
2230 Set_Discriminant_Constraint (Priv,
2231 Discriminant_Constraint (Full));
2232 end if;
2233 end if;
2234 end Preserve_Full_Attributes;
2236 -----------------
2237 -- Type_In_Use --
2238 -----------------
2240 function Type_In_Use (T : Entity_Id) return Boolean is
2241 begin
2242 return Scope (Base_Type (T)) = P
2243 and then (In_Use (T) or else In_Use (Base_Type (T)));
2244 end Type_In_Use;
2246 -- Start of processing for Uninstall_Declarations
2248 begin
2249 Id := First_Entity (P);
2250 while Present (Id) and then Id /= First_Private_Entity (P) loop
2251 if Debug_Flag_E then
2252 Write_Str ("unlinking visible entity ");
2253 Write_Int (Int (Id));
2254 Write_Eol;
2255 end if;
2257 -- On exit from the package scope, we must preserve the visibility
2258 -- established by use clauses in the current scope. Two cases:
2260 -- a) If the entity is an operator, it may be a primitive operator of
2261 -- a type for which there is a visible use-type clause.
2263 -- b) for other entities, their use-visibility is determined by a
2264 -- visible use clause for the package itself. For a generic instance,
2265 -- the instantiation of the formals appears in the visible part,
2266 -- but the formals are private and remain so.
2268 if Ekind (Id) = E_Function
2269 and then Is_Operator_Symbol_Name (Chars (Id))
2270 and then not Is_Hidden (Id)
2271 and then not Error_Posted (Id)
2272 then
2273 Set_Is_Potentially_Use_Visible (Id,
2274 In_Use (P)
2275 or else Type_In_Use (Etype (Id))
2276 or else Type_In_Use (Etype (First_Formal (Id)))
2277 or else (Present (Next_Formal (First_Formal (Id)))
2278 and then
2279 Type_In_Use
2280 (Etype (Next_Formal (First_Formal (Id))))));
2281 else
2282 if In_Use (P) and then not Is_Hidden (Id) then
2284 -- A child unit of a use-visible package remains use-visible
2285 -- only if it is itself a visible child unit. Otherwise it
2286 -- would remain visible in other contexts where P is use-
2287 -- visible, because once compiled it stays in the entity list
2288 -- of its parent unit.
2290 if Is_Child_Unit (Id) then
2291 Set_Is_Potentially_Use_Visible
2292 (Id, Is_Visible_Lib_Unit (Id));
2293 else
2294 Set_Is_Potentially_Use_Visible (Id);
2295 end if;
2297 else
2298 Set_Is_Potentially_Use_Visible (Id, False);
2299 end if;
2300 end if;
2302 -- Local entities are not immediately visible outside of the package
2304 Set_Is_Immediately_Visible (Id, False);
2306 -- If this is a private type with a full view (for example a local
2307 -- subtype of a private type declared elsewhere), ensure that the
2308 -- full view is also removed from visibility: it may be exposed when
2309 -- swapping views in an instantiation.
2311 if Is_Type (Id) and then Present (Full_View (Id)) then
2312 Set_Is_Immediately_Visible (Full_View (Id), False);
2313 end if;
2315 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2316 Check_Abstract_Overriding (Id);
2317 Check_Conventions (Id);
2318 end if;
2320 if Ekind_In (Id, E_Private_Type, E_Limited_Private_Type)
2321 and then No (Full_View (Id))
2322 and then not Is_Generic_Type (Id)
2323 and then not Is_Derived_Type (Id)
2324 then
2325 Error_Msg_N ("missing full declaration for private type&", Id);
2327 elsif Ekind (Id) = E_Record_Type_With_Private
2328 and then not Is_Generic_Type (Id)
2329 and then No (Full_View (Id))
2330 then
2331 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
2332 Error_Msg_N ("missing full declaration for private type&", Id);
2333 else
2334 Error_Msg_N
2335 ("missing full declaration for private extension", Id);
2336 end if;
2338 -- Case of constant, check for deferred constant declaration with
2339 -- no full view. Likely just a matter of a missing expression, or
2340 -- accidental use of the keyword constant.
2342 elsif Ekind (Id) = E_Constant
2344 -- OK if constant value present
2346 and then No (Constant_Value (Id))
2348 -- OK if full view present
2350 and then No (Full_View (Id))
2352 -- OK if imported, since that provides the completion
2354 and then not Is_Imported (Id)
2356 -- OK if object declaration replaced by renaming declaration as
2357 -- a result of OK_To_Rename processing (e.g. for concatenation)
2359 and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration
2361 -- OK if object declaration with the No_Initialization flag set
2363 and then not (Nkind (Parent (Id)) = N_Object_Declaration
2364 and then No_Initialization (Parent (Id)))
2365 then
2366 -- If no private declaration is present, we assume the user did
2367 -- not intend a deferred constant declaration and the problem
2368 -- is simply that the initializing expression is missing.
2370 if not Has_Private_Declaration (Etype (Id)) then
2372 -- We assume that the user did not intend a deferred constant
2373 -- declaration, and the expression is just missing.
2375 Error_Msg_N
2376 ("constant declaration requires initialization expression",
2377 Parent (Id));
2379 if Is_Limited_Type (Etype (Id)) then
2380 Error_Msg_N
2381 ("\if variable intended, remove CONSTANT from declaration",
2382 Parent (Id));
2383 end if;
2385 -- Otherwise if a private declaration is present, then we are
2386 -- missing the full declaration for the deferred constant.
2388 else
2389 Error_Msg_N
2390 ("missing full declaration for deferred constant (RM 7.4)",
2391 Id);
2393 if Is_Limited_Type (Etype (Id)) then
2394 Error_Msg_N
2395 ("\if variable intended, remove CONSTANT from declaration",
2396 Parent (Id));
2397 end if;
2398 end if;
2399 end if;
2401 Next_Entity (Id);
2402 end loop;
2404 -- If the specification was installed as the parent of a public child
2405 -- unit, the private declarations were not installed, and there is
2406 -- nothing to do.
2408 if not In_Private_Part (P) then
2409 return;
2410 else
2411 Set_In_Private_Part (P, False);
2412 end if;
2414 -- Make private entities invisible and exchange full and private
2415 -- declarations for private types. Id is now the first private entity
2416 -- in the package.
2418 while Present (Id) loop
2419 if Debug_Flag_E then
2420 Write_Str ("unlinking private entity ");
2421 Write_Int (Int (Id));
2422 Write_Eol;
2423 end if;
2425 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2426 Check_Abstract_Overriding (Id);
2427 Check_Conventions (Id);
2428 end if;
2430 Set_Is_Immediately_Visible (Id, False);
2432 if Is_Private_Base_Type (Id) and then Present (Full_View (Id)) then
2433 Full := Full_View (Id);
2435 -- If the partial view is not declared in the visible part of the
2436 -- package (as is the case when it is a type derived from some
2437 -- other private type in the private part of the current package),
2438 -- no exchange takes place.
2440 if No (Parent (Id))
2441 or else List_Containing (Parent (Id)) /=
2442 Visible_Declarations (Specification (Decl))
2443 then
2444 goto Next_Id;
2445 end if;
2447 -- The entry in the private part points to the full declaration,
2448 -- which is currently visible. Exchange them so only the private
2449 -- type declaration remains accessible, and link private and full
2450 -- declaration in the opposite direction. Before the actual
2451 -- exchange, we copy back attributes of the full view that must
2452 -- be available to the partial view too.
2454 Preserve_Full_Attributes (Id, Full);
2456 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
2458 if Is_Indefinite_Subtype (Full)
2459 and then not Is_Indefinite_Subtype (Id)
2460 then
2461 Error_Msg_N
2462 ("full view of type must be definite subtype", Full);
2463 end if;
2465 -- Swap out the subtypes and derived types of Id that
2466 -- were compiled in this scope, or installed previously
2467 -- by Install_Private_Declarations.
2469 -- Before we do the swap, we verify the presence of the Full_View
2470 -- field which may be empty due to a swap by a previous call to
2471 -- End_Package_Scope (e.g. from the freezing mechanism).
2473 Priv_Elmt := First_Elmt (Private_Dependents (Id));
2474 while Present (Priv_Elmt) loop
2475 Priv_Sub := Node (Priv_Elmt);
2477 if Present (Full_View (Priv_Sub)) then
2478 if Scope (Priv_Sub) = P
2479 or else not In_Open_Scopes (Scope (Priv_Sub))
2480 then
2481 Set_Is_Immediately_Visible (Priv_Sub, False);
2482 end if;
2484 if Is_Visible_Dependent (Priv_Sub) then
2485 Preserve_Full_Attributes
2486 (Priv_Sub, Full_View (Priv_Sub));
2487 Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub));
2488 Exchange_Declarations (Priv_Sub);
2489 end if;
2490 end if;
2492 Next_Elmt (Priv_Elmt);
2493 end loop;
2495 -- Now restore the type itself to its private view
2497 Exchange_Declarations (Id);
2499 -- If we have installed an underlying full view for a type derived
2500 -- from a private type in a child unit, restore the proper views
2501 -- of private and full view. See corresponding code in
2502 -- Install_Private_Declarations.
2504 -- After the exchange, Full denotes the private type in the
2505 -- visible part of the package.
2507 if Is_Private_Base_Type (Full)
2508 and then Present (Full_View (Full))
2509 and then Present (Underlying_Full_View (Full))
2510 and then In_Package_Body (Current_Scope)
2511 then
2512 Set_Full_View (Full, Underlying_Full_View (Full));
2513 Set_Underlying_Full_View (Full, Empty);
2514 end if;
2516 elsif Ekind (Id) = E_Incomplete_Type
2517 and then Comes_From_Source (Id)
2518 and then No (Full_View (Id))
2519 then
2520 -- Mark Taft amendment types. Verify that there are no primitive
2521 -- operations declared for the type (3.10.1(9)).
2523 Set_Has_Completion_In_Body (Id);
2525 declare
2526 Elmt : Elmt_Id;
2527 Subp : Entity_Id;
2529 begin
2530 Elmt := First_Elmt (Private_Dependents (Id));
2531 while Present (Elmt) loop
2532 Subp := Node (Elmt);
2534 -- Is_Primitive is tested because there can be cases where
2535 -- nonprimitive subprograms (in nested packages) are added
2536 -- to the Private_Dependents list.
2538 if Is_Overloadable (Subp) and then Is_Primitive (Subp) then
2539 Error_Msg_NE
2540 ("type& must be completed in the private part",
2541 Parent (Subp), Id);
2543 -- The result type of an access-to-function type cannot be a
2544 -- Taft-amendment type, unless the version is Ada 2012 or
2545 -- later (see AI05-151).
2547 elsif Ada_Version < Ada_2012
2548 and then Ekind (Subp) = E_Subprogram_Type
2549 then
2550 if Etype (Subp) = Id
2551 or else
2552 (Is_Class_Wide_Type (Etype (Subp))
2553 and then Etype (Etype (Subp)) = Id)
2554 then
2555 Error_Msg_NE
2556 ("type& must be completed in the private part",
2557 Associated_Node_For_Itype (Subp), Id);
2558 end if;
2559 end if;
2561 Next_Elmt (Elmt);
2562 end loop;
2563 end;
2565 elsif not Is_Child_Unit (Id)
2566 and then (not Is_Private_Type (Id) or else No (Full_View (Id)))
2567 then
2568 Set_Is_Hidden (Id);
2569 Set_Is_Potentially_Use_Visible (Id, False);
2570 end if;
2572 <<Next_Id>>
2573 Next_Entity (Id);
2574 end loop;
2575 end Uninstall_Declarations;
2577 ------------------------
2578 -- Unit_Requires_Body --
2579 ------------------------
2581 function Unit_Requires_Body (P : Entity_Id) return Boolean is
2582 E : Entity_Id;
2584 begin
2585 -- Imported entity never requires body. Right now, only subprograms can
2586 -- be imported, but perhaps in the future we will allow import of
2587 -- packages.
2589 if Is_Imported (P) then
2590 return False;
2592 -- Body required if library package with pragma Elaborate_Body
2594 elsif Has_Pragma_Elaborate_Body (P) then
2595 return True;
2597 -- Body required if subprogram
2599 elsif Is_Subprogram (P) or else Is_Generic_Subprogram (P) then
2600 return True;
2602 -- Treat a block as requiring a body
2604 elsif Ekind (P) = E_Block then
2605 return True;
2607 elsif Ekind (P) = E_Package
2608 and then Nkind (Parent (P)) = N_Package_Specification
2609 and then Present (Generic_Parent (Parent (P)))
2610 then
2611 declare
2612 G_P : constant Entity_Id := Generic_Parent (Parent (P));
2613 begin
2614 if Has_Pragma_Elaborate_Body (G_P) then
2615 return True;
2616 end if;
2617 end;
2619 -- A [generic] package that introduces at least one non-null abstract
2620 -- state requires completion. A null abstract state always appears as
2621 -- the sole element of the state list.
2623 elsif Ekind_In (P, E_Generic_Package, E_Package)
2624 and then Present (Abstract_States (P))
2625 and then not Is_Null_State (Node (First_Elmt (Abstract_States (P))))
2626 then
2627 return True;
2628 end if;
2630 -- Otherwise search entity chain for entity requiring completion
2632 E := First_Entity (P);
2633 while Present (E) loop
2635 -- Always ignore child units. Child units get added to the entity
2636 -- list of a parent unit, but are not original entities of the
2637 -- parent, and so do not affect whether the parent needs a body.
2639 if Is_Child_Unit (E) then
2640 null;
2642 -- Ignore formal packages and their renamings
2644 elsif Ekind (E) = E_Package
2645 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
2646 N_Formal_Package_Declaration
2647 then
2648 null;
2650 -- Otherwise test to see if entity requires a completion.
2651 -- Note that subprogram entities whose declaration does not come
2652 -- from source are ignored here on the basis that we assume the
2653 -- expander will provide an implicit completion at some point.
2655 elsif (Is_Overloadable (E)
2656 and then Ekind (E) /= E_Enumeration_Literal
2657 and then Ekind (E) /= E_Operator
2658 and then not Is_Abstract_Subprogram (E)
2659 and then not Has_Completion (E)
2660 and then Comes_From_Source (Parent (E)))
2662 or else
2663 (Ekind (E) = E_Package
2664 and then E /= P
2665 and then not Has_Completion (E)
2666 and then Unit_Requires_Body (E))
2668 or else
2669 (Ekind (E) = E_Incomplete_Type
2670 and then No (Full_View (E))
2671 and then not Is_Generic_Type (E))
2673 or else
2674 (Ekind_In (E, E_Task_Type, E_Protected_Type)
2675 and then not Has_Completion (E))
2677 or else
2678 (Ekind (E) = E_Generic_Package
2679 and then E /= P
2680 and then not Has_Completion (E)
2681 and then Unit_Requires_Body (E))
2683 or else
2684 (Is_Generic_Subprogram (E)
2685 and then not Has_Completion (E))
2687 then
2688 return True;
2690 -- Entity that does not require completion
2692 else
2693 null;
2694 end if;
2696 Next_Entity (E);
2697 end loop;
2699 return False;
2700 end Unit_Requires_Body;
2702 end Sem_Ch7;