[AArch64] Remove simd_type
[official-gcc.git] / gcc / ada / sem_ch7.adb
blob76875b27afce0033d4250670a7f70907a53104e9
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 procedure Unit_Requires_Body_Info (P : Entity_Id);
140 -- Outputs info messages showing why package specification P requires a
141 -- body. Caller has checked that the switch requesting this information
142 -- is set, and that the package does indeed require a body.
144 --------------------------
145 -- Analyze_Package_Body --
146 --------------------------
148 procedure Analyze_Package_Body (N : Node_Id) is
149 Loc : constant Source_Ptr := Sloc (N);
151 begin
152 if Debug_Flag_C then
153 Write_Str ("==> package body ");
154 Write_Name (Chars (Defining_Entity (N)));
155 Write_Str (" from ");
156 Write_Location (Loc);
157 Write_Eol;
158 Indent;
159 end if;
161 -- The real work is split out into the helper, so it can do "return;"
162 -- without skipping the debug output.
164 Analyze_Package_Body_Helper (N);
166 if Debug_Flag_C then
167 Outdent;
168 Write_Str ("<== package body ");
169 Write_Name (Chars (Defining_Entity (N)));
170 Write_Str (" from ");
171 Write_Location (Loc);
172 Write_Eol;
173 end if;
174 end Analyze_Package_Body;
176 ---------------------------------
177 -- Analyze_Package_Body_Helper --
178 ---------------------------------
180 procedure Analyze_Package_Body_Helper (N : Node_Id) is
181 HSS : Node_Id;
182 Body_Id : Entity_Id;
183 Spec_Id : Entity_Id;
184 Last_Spec_Entity : Entity_Id;
185 New_N : Node_Id;
186 Pack_Decl : Node_Id;
188 procedure Install_Composite_Operations (P : Entity_Id);
189 -- Composite types declared in the current scope may depend on types
190 -- that were private at the point of declaration, and whose full view
191 -- is now in scope. Indicate that the corresponding operations on the
192 -- composite type are available.
194 ----------------------------------
195 -- Install_Composite_Operations --
196 ----------------------------------
198 procedure Install_Composite_Operations (P : Entity_Id) is
199 Id : Entity_Id;
201 begin
202 Id := First_Entity (P);
203 while Present (Id) loop
204 if Is_Type (Id)
205 and then (Is_Limited_Composite (Id)
206 or else Is_Private_Composite (Id))
207 and then No (Private_Component (Id))
208 then
209 Set_Is_Limited_Composite (Id, False);
210 Set_Is_Private_Composite (Id, False);
211 end if;
213 Next_Entity (Id);
214 end loop;
215 end Install_Composite_Operations;
217 -- Start of processing for Analyze_Package_Body_Helper
219 begin
220 -- Find corresponding package specification, and establish the current
221 -- scope. The visible defining entity for the package is the defining
222 -- occurrence in the spec. On exit from the package body, all body
223 -- declarations are attached to the defining entity for the body, but
224 -- the later is never used for name resolution. In this fashion there
225 -- is only one visible entity that denotes the package.
227 -- Set Body_Id. Note that this will be reset to point to the generic
228 -- copy later on in the generic case.
230 Body_Id := Defining_Entity (N);
232 -- Body is body of package instantiation. Corresponding spec has already
233 -- been set.
235 if Present (Corresponding_Spec (N)) then
236 Spec_Id := Corresponding_Spec (N);
237 Pack_Decl := Unit_Declaration_Node (Spec_Id);
239 else
240 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
242 if Present (Spec_Id)
243 and then Is_Package_Or_Generic_Package (Spec_Id)
244 then
245 Pack_Decl := Unit_Declaration_Node (Spec_Id);
247 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
248 Error_Msg_N ("cannot supply body for package renaming", N);
249 return;
251 elsif Present (Corresponding_Body (Pack_Decl)) then
252 Error_Msg_N ("redefinition of package body", N);
253 return;
254 end if;
256 else
257 Error_Msg_N ("missing specification for package body", N);
258 return;
259 end if;
261 if Is_Package_Or_Generic_Package (Spec_Id)
262 and then (Scope (Spec_Id) = Standard_Standard
263 or else Is_Child_Unit (Spec_Id))
264 and then not Unit_Requires_Body (Spec_Id)
265 then
266 if Ada_Version = Ada_83 then
267 Error_Msg_N
268 ("optional package body (not allowed in Ada 95)??", N);
269 else
270 Error_Msg_N ("spec of this package does not allow a body", N);
271 end if;
272 end if;
273 end if;
275 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
276 Style.Check_Identifier (Body_Id, Spec_Id);
278 if Is_Child_Unit (Spec_Id) then
279 if Nkind (Parent (N)) /= N_Compilation_Unit then
280 Error_Msg_NE
281 ("body of child unit& cannot be an inner package", N, Spec_Id);
282 end if;
284 Set_Is_Child_Unit (Body_Id);
285 end if;
287 -- Generic package case
289 if Ekind (Spec_Id) = E_Generic_Package then
291 -- Disable expansion and perform semantic analysis on copy. The
292 -- unannotated body will be used in all instantiations.
294 Body_Id := Defining_Entity (N);
295 Set_Ekind (Body_Id, E_Package_Body);
296 Set_Scope (Body_Id, Scope (Spec_Id));
297 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
298 Set_Body_Entity (Spec_Id, Body_Id);
299 Set_Spec_Entity (Body_Id, Spec_Id);
301 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
302 Rewrite (N, New_N);
304 -- Update Body_Id to point to the copied node for the remainder of
305 -- the processing.
307 Body_Id := Defining_Entity (N);
308 Start_Generic;
309 end if;
311 -- The Body_Id is that of the copied node in the generic case, the
312 -- current node otherwise. Note that N was rewritten above, so we must
313 -- be sure to get the latest Body_Id value.
315 Set_Ekind (Body_Id, E_Package_Body);
316 Set_Body_Entity (Spec_Id, Body_Id);
317 Set_Spec_Entity (Body_Id, Spec_Id);
318 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
320 -- Defining name for the package body is not a visible entity: Only the
321 -- defining name for the declaration is visible.
323 Set_Etype (Body_Id, Standard_Void_Type);
324 Set_Scope (Body_Id, Scope (Spec_Id));
325 Set_Corresponding_Spec (N, Spec_Id);
326 Set_Corresponding_Body (Pack_Decl, Body_Id);
328 -- The body entity is not used for semantics or code generation, but
329 -- it is attached to the entity list of the enclosing scope to simplify
330 -- the listing of back-annotations for the types it main contain.
332 if Scope (Spec_Id) /= Standard_Standard then
333 Append_Entity (Body_Id, Scope (Spec_Id));
334 end if;
336 -- Indicate that we are currently compiling the body of the package
338 Set_In_Package_Body (Spec_Id);
339 Set_Has_Completion (Spec_Id);
340 Last_Spec_Entity := Last_Entity (Spec_Id);
342 if Has_Aspects (N) then
343 Analyze_Aspect_Specifications (N, Body_Id);
344 end if;
346 Push_Scope (Spec_Id);
348 Set_Categorization_From_Pragmas (N);
350 Install_Visible_Declarations (Spec_Id);
351 Install_Private_Declarations (Spec_Id);
352 Install_Private_With_Clauses (Spec_Id);
353 Install_Composite_Operations (Spec_Id);
355 Check_Anonymous_Access_Types (Spec_Id, N);
357 if Ekind (Spec_Id) = E_Generic_Package then
358 Set_Use (Generic_Formal_Declarations (Pack_Decl));
359 end if;
361 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
362 Set_Use (Private_Declarations (Specification (Pack_Decl)));
364 -- This is a nested package, so it may be necessary to declare certain
365 -- inherited subprograms that are not yet visible because the parent
366 -- type's subprograms are now visible.
368 if Ekind (Scope (Spec_Id)) = E_Package
369 and then Scope (Spec_Id) /= Standard_Standard
370 then
371 Declare_Inherited_Private_Subprograms (Spec_Id);
372 end if;
374 if Present (Declarations (N)) then
375 Analyze_Declarations (Declarations (N));
376 Inspect_Deferred_Constant_Completion (Declarations (N));
377 end if;
379 -- Analyze_Declarations has caused freezing of all types. Now generate
380 -- bodies for RACW primitives and stream attributes, if any.
382 if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then
384 -- Attach subprogram bodies to support RACWs declared in spec
386 Append_RACW_Bodies (Declarations (N), Spec_Id);
387 Analyze_List (Declarations (N));
388 end if;
390 HSS := Handled_Statement_Sequence (N);
392 if Present (HSS) then
393 Process_End_Label (HSS, 't', Spec_Id);
394 Analyze (HSS);
396 -- Check that elaboration code in a preelaborable package body is
397 -- empty other than null statements and labels (RM 10.2.1(6)).
399 Validate_Null_Statement_Sequence (N);
400 end if;
402 Validate_Categorization_Dependency (N, Spec_Id);
403 Check_Completion (Body_Id);
405 -- Generate start of body reference. Note that we do this fairly late,
406 -- because the call will use In_Extended_Main_Source_Unit as a check,
407 -- and we want to make sure that Corresponding_Stub links are set
409 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
411 -- For a generic package, collect global references and mark them on
412 -- the original body so that they are not resolved again at the point
413 -- of instantiation.
415 if Ekind (Spec_Id) /= E_Package then
416 Save_Global_References (Original_Node (N));
417 End_Generic;
418 end if;
420 -- The entities of the package body have so far been chained onto the
421 -- declaration chain for the spec. That's been fine while we were in the
422 -- body, since we wanted them to be visible, but now that we are leaving
423 -- the package body, they are no longer visible, so we remove them from
424 -- the entity chain of the package spec entity, and copy them to the
425 -- entity chain of the package body entity, where they will never again
426 -- be visible.
428 if Present (Last_Spec_Entity) then
429 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
430 Set_Next_Entity (Last_Spec_Entity, Empty);
431 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
432 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
434 else
435 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
436 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
437 Set_First_Entity (Spec_Id, Empty);
438 Set_Last_Entity (Spec_Id, Empty);
439 end if;
441 End_Package_Scope (Spec_Id);
443 -- All entities declared in body are not visible
445 declare
446 E : Entity_Id;
448 begin
449 E := First_Entity (Body_Id);
450 while Present (E) loop
451 Set_Is_Immediately_Visible (E, False);
452 Set_Is_Potentially_Use_Visible (E, False);
453 Set_Is_Hidden (E);
455 -- Child units may appear on the entity list (e.g. if they appear
456 -- in the context of a subunit) but they are not body entities.
458 if not Is_Child_Unit (E) then
459 Set_Is_Package_Body_Entity (E);
460 end if;
462 Next_Entity (E);
463 end loop;
464 end;
466 Check_References (Body_Id);
468 -- For a generic unit, check that the formal parameters are referenced,
469 -- and that local variables are used, as for regular packages.
471 if Ekind (Spec_Id) = E_Generic_Package then
472 Check_References (Spec_Id);
473 end if;
475 -- The processing so far has made all entities of the package body
476 -- public (i.e. externally visible to the linker). This is in general
477 -- necessary, since inlined or generic bodies, for which code is
478 -- generated in other units, may need to see these entities. The
479 -- following loop runs backwards from the end of the entities of the
480 -- package body making these entities invisible until we reach a
481 -- referencer, i.e. a declaration that could reference a previous
482 -- declaration, a generic body or an inlined body, or a stub (which may
483 -- contain either of these). This is of course an approximation, but it
484 -- is conservative and definitely correct.
486 -- We only do this at the outer (library) level non-generic packages.
487 -- The reason is simply to cut down on the number of global symbols
488 -- generated, which has a double effect: (1) to make the compilation
489 -- process more efficient and (2) to give the code generator more
490 -- freedom to optimize within each unit, especially subprograms.
492 if (Scope (Spec_Id) = Standard_Standard or else Is_Child_Unit (Spec_Id))
493 and then not Is_Generic_Unit (Spec_Id)
494 and then Present (Declarations (N))
495 then
496 Make_Non_Public_Where_Possible : declare
498 function Has_Referencer
499 (L : List_Id;
500 Outer : Boolean) return Boolean;
501 -- Traverse the given list of declarations in reverse order.
502 -- Return True if a referencer is present. Return False if none is
503 -- found. The Outer parameter is True for the outer level call and
504 -- False for inner level calls for nested packages. If Outer is
505 -- True, then any entities up to the point of hitting a referencer
506 -- get their Is_Public flag cleared, so that the entities will be
507 -- treated as static entities in the C sense, and need not have
508 -- fully qualified names. Furthermore, if the referencer is an
509 -- inlined subprogram that doesn't reference other subprograms,
510 -- we keep clearing the Is_Public flag on subprograms. For inner
511 -- levels, we need all names to be fully qualified to deal with
512 -- the same name appearing in parallel packages (right now this
513 -- is tied to their being external).
515 --------------------
516 -- Has_Referencer --
517 --------------------
519 function Has_Referencer
520 (L : List_Id;
521 Outer : Boolean) return Boolean
523 Has_Referencer_Except_For_Subprograms : Boolean := False;
525 D : Node_Id;
526 E : Entity_Id;
527 K : Node_Kind;
528 S : Entity_Id;
530 function Check_Subprogram_Ref (N : Node_Id)
531 return Traverse_Result;
532 -- Look for references to subprograms
534 --------------------------
535 -- Check_Subprogram_Ref --
536 --------------------------
538 function Check_Subprogram_Ref (N : Node_Id)
539 return Traverse_Result
541 V : Node_Id;
543 begin
544 -- Check name of procedure or function calls
546 if Nkind (N) in N_Subprogram_Call
547 and then Is_Entity_Name (Name (N))
548 then
549 return Abandon;
550 end if;
552 -- Check prefix of attribute references
554 if Nkind (N) = N_Attribute_Reference
555 and then Is_Entity_Name (Prefix (N))
556 and then Present (Entity (Prefix (N)))
557 and then Ekind (Entity (Prefix (N))) in Subprogram_Kind
558 then
559 return Abandon;
560 end if;
562 -- Check value of constants
564 if Nkind (N) = N_Identifier
565 and then Present (Entity (N))
566 and then Ekind (Entity (N)) = E_Constant
567 then
568 V := Constant_Value (Entity (N));
570 if Present (V)
571 and then not Compile_Time_Known_Value_Or_Aggr (V)
572 then
573 return Abandon;
574 end if;
575 end if;
577 return OK;
578 end Check_Subprogram_Ref;
580 function Check_Subprogram_Refs is
581 new Traverse_Func (Check_Subprogram_Ref);
583 -- Start of processing for Has_Referencer
585 begin
586 if No (L) then
587 return False;
588 end if;
590 D := Last (L);
591 while Present (D) loop
592 K := Nkind (D);
594 if K in N_Body_Stub then
595 return True;
597 -- Processing for subprogram bodies
599 elsif K = N_Subprogram_Body then
600 if Acts_As_Spec (D) then
601 E := Defining_Entity (D);
603 -- An inlined body acts as a referencer. Note also
604 -- that we never reset Is_Public for an inlined
605 -- subprogram. Gigi requires Is_Public to be set.
607 -- Note that we test Has_Pragma_Inline here rather
608 -- than Is_Inlined. We are compiling this for a
609 -- client, and it is the client who will decide if
610 -- actual inlining should occur, so we need to assume
611 -- that the procedure could be inlined for the purpose
612 -- of accessing global entities.
614 if Has_Pragma_Inline (E) then
615 if Outer
616 and then Check_Subprogram_Refs (D) = OK
617 then
618 Has_Referencer_Except_For_Subprograms := True;
619 else
620 return True;
621 end if;
622 else
623 Set_Is_Public (E, False);
624 end if;
626 else
627 E := Corresponding_Spec (D);
629 if Present (E) then
631 -- A generic subprogram body acts as a referencer
633 if Is_Generic_Unit (E) then
634 return True;
635 end if;
637 if Has_Pragma_Inline (E) or else Is_Inlined (E) then
638 if Outer
639 and then Check_Subprogram_Refs (D) = OK
640 then
641 Has_Referencer_Except_For_Subprograms := True;
642 else
643 return True;
644 end if;
645 end if;
646 end if;
647 end if;
649 -- Processing for package bodies
651 elsif K = N_Package_Body
652 and then Present (Corresponding_Spec (D))
653 then
654 E := Corresponding_Spec (D);
656 -- Generic package body is a referencer. It would seem
657 -- that we only have to consider generics that can be
658 -- exported, i.e. where the corresponding spec is the
659 -- spec of the current package, but because of nested
660 -- instantiations, a fully private generic body may
661 -- export other private body entities. Furthermore,
662 -- regardless of whether there was a previous inlined
663 -- subprogram, (an instantiation of) the generic package
664 -- may reference any entity declared before it.
666 if Is_Generic_Unit (E) then
667 return True;
669 -- For non-generic package body, recurse into body unless
670 -- this is an instance, we ignore instances since they
671 -- cannot have references that affect outer entities.
673 elsif not Is_Generic_Instance (E)
674 and then not Has_Referencer_Except_For_Subprograms
675 then
676 if Has_Referencer
677 (Declarations (D), Outer => False)
678 then
679 return True;
680 end if;
681 end if;
683 -- Processing for package specs, recurse into declarations.
684 -- Again we skip this for the case of generic instances.
686 elsif K = N_Package_Declaration
687 and then not Has_Referencer_Except_For_Subprograms
688 then
689 S := Specification (D);
691 if not Is_Generic_Unit (Defining_Entity (S)) then
692 if Has_Referencer
693 (Private_Declarations (S), Outer => False)
694 then
695 return True;
696 elsif Has_Referencer
697 (Visible_Declarations (S), Outer => False)
698 then
699 return True;
700 end if;
701 end if;
703 -- Objects and exceptions need not be public if we have not
704 -- encountered a referencer so far. We only reset the flag
705 -- for outer level entities that are not imported/exported,
706 -- and which have no interface name.
708 elsif Nkind_In (K, N_Object_Declaration,
709 N_Exception_Declaration,
710 N_Subprogram_Declaration)
711 then
712 E := Defining_Entity (D);
714 if Outer
715 and then (not Has_Referencer_Except_For_Subprograms
716 or else K = N_Subprogram_Declaration)
717 and then not Is_Imported (E)
718 and then not Is_Exported (E)
719 and then No (Interface_Name (E))
720 then
721 Set_Is_Public (E, False);
722 end if;
723 end if;
725 Prev (D);
726 end loop;
728 return Has_Referencer_Except_For_Subprograms;
729 end Has_Referencer;
731 -- Start of processing for Make_Non_Public_Where_Possible
733 begin
734 declare
735 Discard : Boolean;
736 pragma Warnings (Off, Discard);
738 begin
739 Discard := Has_Referencer (Declarations (N), Outer => True);
740 end;
741 end Make_Non_Public_Where_Possible;
742 end if;
744 -- If expander is not active, then here is where we turn off the
745 -- In_Package_Body flag, otherwise it is turned off at the end of the
746 -- corresponding expansion routine. If this is an instance body, we need
747 -- to qualify names of local entities, because the body may have been
748 -- compiled as a preliminary to another instantiation.
750 if not Expander_Active then
751 Set_In_Package_Body (Spec_Id, False);
753 if Is_Generic_Instance (Spec_Id)
754 and then Operating_Mode = Generate_Code
755 then
756 Qualify_Entity_Names (N);
757 end if;
758 end if;
759 end Analyze_Package_Body_Helper;
761 ---------------------------------
762 -- Analyze_Package_Declaration --
763 ---------------------------------
765 procedure Analyze_Package_Declaration (N : Node_Id) is
766 Id : constant Node_Id := Defining_Entity (N);
768 PF : Boolean;
769 -- True when in the context of a declared pure library unit
771 Body_Required : Boolean;
772 -- True when this package declaration requires a corresponding body
774 Comp_Unit : Boolean;
775 -- True when this package declaration is not a nested declaration
777 begin
778 if Debug_Flag_C then
779 Write_Str ("==> package spec ");
780 Write_Name (Chars (Id));
781 Write_Str (" from ");
782 Write_Location (Sloc (N));
783 Write_Eol;
784 Indent;
785 end if;
787 Generate_Definition (Id);
788 Enter_Name (Id);
789 Set_Ekind (Id, E_Package);
790 Set_Etype (Id, Standard_Void_Type);
791 Set_Contract (Id, Make_Contract (Sloc (Id)));
793 -- Analyze aspect specifications immediately, since we need to recognize
794 -- things like Pure early enough to diagnose violations during analysis.
796 if Has_Aspects (N) then
797 Analyze_Aspect_Specifications (N, Id);
798 end if;
800 -- Ada 2005 (AI-217): Check if the package has been erroneously named
801 -- in a limited-with clause of its own context. In this case the error
802 -- has been previously notified by Analyze_Context.
804 -- limited with Pkg; -- ERROR
805 -- package Pkg is ...
807 if From_Limited_With (Id) then
808 return;
809 end if;
811 Push_Scope (Id);
813 PF := Is_Pure (Enclosing_Lib_Unit_Entity);
814 Set_Is_Pure (Id, PF);
816 Set_Categorization_From_Pragmas (N);
818 Analyze (Specification (N));
819 Validate_Categorization_Dependency (N, Id);
821 Body_Required := Unit_Requires_Body (Id);
823 -- When this spec does not require an explicit body, we know that there
824 -- are no entities requiring completion in the language sense; we call
825 -- Check_Completion here only to ensure that any nested package
826 -- declaration that requires an implicit body gets one. (In the case
827 -- where a body is required, Check_Completion is called at the end of
828 -- the body's declarative part.)
830 if not Body_Required then
831 Check_Completion;
832 end if;
834 Comp_Unit := Nkind (Parent (N)) = N_Compilation_Unit;
835 if Comp_Unit then
837 -- Set Body_Required indication on the compilation unit node, and
838 -- determine whether elaboration warnings may be meaningful on it.
840 Set_Body_Required (Parent (N), Body_Required);
842 if not Body_Required then
843 Set_Suppress_Elaboration_Warnings (Id);
844 end if;
846 end if;
848 End_Package_Scope (Id);
850 -- For the declaration of a library unit that is a remote types package,
851 -- check legality rules regarding availability of stream attributes for
852 -- types that contain non-remote access values. This subprogram performs
853 -- visibility tests that rely on the fact that we have exited the scope
854 -- of Id.
856 if Comp_Unit then
857 Validate_RT_RAT_Component (N);
858 end if;
860 if Debug_Flag_C then
861 Outdent;
862 Write_Str ("<== package spec ");
863 Write_Name (Chars (Id));
864 Write_Str (" from ");
865 Write_Location (Sloc (N));
866 Write_Eol;
867 end if;
868 end Analyze_Package_Declaration;
870 -----------------------------------
871 -- Analyze_Package_Specification --
872 -----------------------------------
874 -- Note that this code is shared for the analysis of generic package specs
875 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
877 procedure Analyze_Package_Specification (N : Node_Id) is
878 Id : constant Entity_Id := Defining_Entity (N);
879 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
880 Vis_Decls : constant List_Id := Visible_Declarations (N);
881 Priv_Decls : constant List_Id := Private_Declarations (N);
882 E : Entity_Id;
883 L : Entity_Id;
884 Public_Child : Boolean;
886 Private_With_Clauses_Installed : Boolean := False;
887 -- In Ada 2005, private with_clauses are visible in the private part
888 -- of a nested package, even if it appears in the public part of the
889 -- enclosing package. This requires a separate step to install these
890 -- private_with_clauses, and remove them at the end of the nested
891 -- package.
893 procedure Check_One_Tagged_Type_Or_Extension_At_Most;
894 -- Issue an error in SPARK mode if a package specification contains
895 -- more than one tagged type or type extension.
897 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
898 -- Clears constant indications (Never_Set_In_Source, Constant_Value, and
899 -- Is_True_Constant) on all variables that are entities of Id, and on
900 -- the chain whose first element is FE. A recursive call is made for all
901 -- packages and generic packages.
903 procedure Generate_Parent_References;
904 -- For a child unit, generate references to parent units, for
905 -- GPS navigation purposes.
907 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
908 -- Child and Unit are entities of compilation units. True if Child
909 -- is a public child of Parent as defined in 10.1.1
911 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
912 -- Reject completion of an incomplete or private type declarations
913 -- having a known discriminant part by an unchecked union.
915 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
916 -- Given the package entity of a generic package instantiation or
917 -- formal package whose corresponding generic is a child unit, installs
918 -- the private declarations of each of the child unit's parents.
919 -- This has to be done at the point of entering the instance package's
920 -- private part rather than being done in Sem_Ch12.Install_Parent
921 -- (which is where the parents' visible declarations are installed).
923 ------------------------------------------------
924 -- Check_One_Tagged_Type_Or_Extension_At_Most --
925 ------------------------------------------------
927 procedure Check_One_Tagged_Type_Or_Extension_At_Most is
928 Previous : Node_Id;
930 procedure Check_Decls (Decls : List_Id);
931 -- Check that either Previous is Empty and Decls does not contain
932 -- more than one tagged type or type extension, or Previous is
933 -- already set and Decls contains no tagged type or type extension.
935 -----------------
936 -- Check_Decls --
937 -----------------
939 procedure Check_Decls (Decls : List_Id) is
940 Decl : Node_Id;
942 begin
943 Decl := First (Decls);
944 while Present (Decl) loop
945 if Nkind (Decl) = N_Full_Type_Declaration
946 and then Is_Tagged_Type (Defining_Identifier (Decl))
947 then
948 if No (Previous) then
949 Previous := Decl;
951 else
952 Error_Msg_Sloc := Sloc (Previous);
953 Check_SPARK_Restriction
954 ("at most one tagged type or type extension allowed",
955 "\\ previous declaration#",
956 Decl);
957 end if;
958 end if;
960 Next (Decl);
961 end loop;
962 end Check_Decls;
964 -- Start of processing for Check_One_Tagged_Type_Or_Extension_At_Most
966 begin
967 Previous := Empty;
968 Check_Decls (Vis_Decls);
970 if Present (Priv_Decls) then
971 Check_Decls (Priv_Decls);
972 end if;
973 end Check_One_Tagged_Type_Or_Extension_At_Most;
975 ---------------------
976 -- Clear_Constants --
977 ---------------------
979 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
980 E : Entity_Id;
982 begin
983 -- Ignore package renamings, not interesting and they can cause self
984 -- referential loops in the code below.
986 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
987 return;
988 end if;
990 -- Note: in the loop below, the check for Next_Entity pointing back
991 -- to the package entity may seem odd, but it is needed, because a
992 -- package can contain a renaming declaration to itself, and such
993 -- renamings are generated automatically within package instances.
995 E := FE;
996 while Present (E) and then E /= Id loop
997 if Is_Assignable (E) then
998 Set_Never_Set_In_Source (E, False);
999 Set_Is_True_Constant (E, False);
1000 Set_Current_Value (E, Empty);
1001 Set_Is_Known_Null (E, False);
1002 Set_Last_Assignment (E, Empty);
1004 if not Can_Never_Be_Null (E) then
1005 Set_Is_Known_Non_Null (E, False);
1006 end if;
1008 elsif Is_Package_Or_Generic_Package (E) then
1009 Clear_Constants (E, First_Entity (E));
1010 Clear_Constants (E, First_Private_Entity (E));
1011 end if;
1013 Next_Entity (E);
1014 end loop;
1015 end Clear_Constants;
1017 --------------------------------
1018 -- Generate_Parent_References --
1019 --------------------------------
1021 procedure Generate_Parent_References is
1022 Decl : constant Node_Id := Parent (N);
1024 begin
1025 if Id = Cunit_Entity (Main_Unit)
1026 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
1027 then
1028 Generate_Reference (Id, Scope (Id), 'k', False);
1030 elsif not Nkind_In (Unit (Cunit (Main_Unit)), N_Subprogram_Body,
1031 N_Subunit)
1032 then
1033 -- If current unit is an ancestor of main unit, generate a
1034 -- reference to its own parent.
1036 declare
1037 U : Node_Id;
1038 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
1040 begin
1041 if Nkind (Main_Spec) = N_Package_Body then
1042 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
1043 end if;
1045 U := Parent_Spec (Main_Spec);
1046 while Present (U) loop
1047 if U = Parent (Decl) then
1048 Generate_Reference (Id, Scope (Id), 'k', False);
1049 exit;
1051 elsif Nkind (Unit (U)) = N_Package_Body then
1052 exit;
1054 else
1055 U := Parent_Spec (Unit (U));
1056 end if;
1057 end loop;
1058 end;
1059 end if;
1060 end Generate_Parent_References;
1062 ---------------------
1063 -- Is_Public_Child --
1064 ---------------------
1066 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
1067 begin
1068 if not Is_Private_Descendant (Child) then
1069 return True;
1070 else
1071 if Child = Unit then
1072 return not Private_Present (
1073 Parent (Unit_Declaration_Node (Child)));
1074 else
1075 return Is_Public_Child (Scope (Child), Unit);
1076 end if;
1077 end if;
1078 end Is_Public_Child;
1080 ----------------------------------------
1081 -- Inspect_Unchecked_Union_Completion --
1082 ----------------------------------------
1084 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
1085 Decl : Node_Id;
1087 begin
1088 Decl := First (Decls);
1089 while Present (Decl) loop
1091 -- We are looking at an incomplete or private type declaration
1092 -- with a known_discriminant_part whose full view is an
1093 -- Unchecked_Union.
1095 if Nkind_In (Decl, N_Incomplete_Type_Declaration,
1096 N_Private_Type_Declaration)
1097 and then Has_Discriminants (Defining_Identifier (Decl))
1098 and then Present (Full_View (Defining_Identifier (Decl)))
1099 and then
1100 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
1101 then
1102 Error_Msg_N
1103 ("completion of discriminated partial view "
1104 & "cannot be an unchecked union",
1105 Full_View (Defining_Identifier (Decl)));
1106 end if;
1108 Next (Decl);
1109 end loop;
1110 end Inspect_Unchecked_Union_Completion;
1112 -----------------------------------------
1113 -- Install_Parent_Private_Declarations --
1114 -----------------------------------------
1116 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
1117 Inst_Par : Entity_Id;
1118 Gen_Par : Entity_Id;
1119 Inst_Node : Node_Id;
1121 begin
1122 Inst_Par := Inst_Id;
1124 Gen_Par :=
1125 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
1126 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
1127 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
1129 if Nkind_In (Inst_Node, N_Package_Instantiation,
1130 N_Formal_Package_Declaration)
1131 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
1132 then
1133 Inst_Par := Entity (Prefix (Name (Inst_Node)));
1135 if Present (Renamed_Entity (Inst_Par)) then
1136 Inst_Par := Renamed_Entity (Inst_Par);
1137 end if;
1139 Gen_Par :=
1140 Generic_Parent
1141 (Specification (Unit_Declaration_Node (Inst_Par)));
1143 -- Install the private declarations and private use clauses
1144 -- of a parent instance of the child instance, unless the
1145 -- parent instance private declarations have already been
1146 -- installed earlier in Analyze_Package_Specification, which
1147 -- happens when a generic child is instantiated, and the
1148 -- instance is a child of the parent instance.
1150 -- Installing the use clauses of the parent instance twice
1151 -- is both unnecessary and wrong, because it would cause the
1152 -- clauses to be chained to themselves in the use clauses
1153 -- list of the scope stack entry. That in turn would cause
1154 -- an endless loop from End_Use_Clauses upon scope exit.
1156 -- The parent is now fully visible. It may be a hidden open
1157 -- scope if we are currently compiling some child instance
1158 -- declared within it, but while the current instance is being
1159 -- compiled the parent is immediately visible. In particular
1160 -- its entities must remain visible if a stack save/restore
1161 -- takes place through a call to Rtsfind.
1163 if Present (Gen_Par) then
1164 if not In_Private_Part (Inst_Par) then
1165 Install_Private_Declarations (Inst_Par);
1166 Set_Use (Private_Declarations
1167 (Specification
1168 (Unit_Declaration_Node (Inst_Par))));
1169 Set_Is_Hidden_Open_Scope (Inst_Par, False);
1170 end if;
1172 -- If we've reached the end of the generic instance parents,
1173 -- then finish off by looping through the nongeneric parents
1174 -- and installing their private declarations.
1176 -- If one of the non-generic parents is itself on the scope
1177 -- stack, do not install its private declarations: they are
1178 -- installed in due time when the private part of that parent
1179 -- is analyzed. This is delicate ???
1181 else
1182 while Present (Inst_Par)
1183 and then Inst_Par /= Standard_Standard
1184 and then (not In_Open_Scopes (Inst_Par)
1185 or else not In_Private_Part (Inst_Par))
1186 loop
1187 Install_Private_Declarations (Inst_Par);
1188 Set_Use (Private_Declarations
1189 (Specification
1190 (Unit_Declaration_Node (Inst_Par))));
1191 Inst_Par := Scope (Inst_Par);
1192 end loop;
1194 exit;
1195 end if;
1197 else
1198 exit;
1199 end if;
1200 end loop;
1201 end Install_Parent_Private_Declarations;
1203 -- Start of processing for Analyze_Package_Specification
1205 begin
1206 if Present (Vis_Decls) then
1207 Analyze_Declarations (Vis_Decls);
1208 end if;
1210 -- Verify that incomplete types have received full declarations and
1211 -- also build invariant procedures for any types with invariants.
1213 E := First_Entity (Id);
1214 while Present (E) loop
1216 -- Check on incomplete types
1218 -- AI05-0213: A formal incomplete type has no completion
1220 if Ekind (E) = E_Incomplete_Type
1221 and then No (Full_View (E))
1222 and then not Is_Generic_Type (E)
1223 then
1224 Error_Msg_N ("no declaration in visible part for incomplete}", E);
1225 end if;
1227 -- Build invariant procedures
1229 if Is_Type (E) and then Has_Invariants (E) then
1230 Build_Invariant_Procedure (E, N);
1231 end if;
1233 Next_Entity (E);
1234 end loop;
1236 if Is_Remote_Call_Interface (Id)
1237 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1238 then
1239 Validate_RCI_Declarations (Id);
1240 end if;
1242 -- Save global references in the visible declarations, before installing
1243 -- private declarations of parent unit if there is one, because the
1244 -- privacy status of types defined in the parent will change. This is
1245 -- only relevant for generic child units, but is done in all cases for
1246 -- uniformity.
1248 if Ekind (Id) = E_Generic_Package
1249 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1250 then
1251 declare
1252 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1253 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1254 begin
1255 Set_Private_Declarations (Orig_Spec, Empty_List);
1256 Save_Global_References (Orig_Decl);
1257 Set_Private_Declarations (Orig_Spec, Save_Priv);
1258 end;
1259 end if;
1261 -- If package is a public child unit, then make the private declarations
1262 -- of the parent visible.
1264 Public_Child := False;
1266 declare
1267 Par : Entity_Id;
1268 Pack_Decl : Node_Id;
1269 Par_Spec : Node_Id;
1271 begin
1272 Par := Id;
1273 Par_Spec := Parent_Spec (Parent (N));
1275 -- If the package is formal package of an enclosing generic, it is
1276 -- transformed into a local generic declaration, and compiled to make
1277 -- its spec available. We need to retrieve the original generic to
1278 -- determine whether it is a child unit, and install its parents.
1280 if No (Par_Spec)
1281 and then
1282 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1283 then
1284 Par := Entity (Name (Original_Node (Parent (N))));
1285 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1286 end if;
1288 if Present (Par_Spec) then
1289 Generate_Parent_References;
1291 while Scope (Par) /= Standard_Standard
1292 and then Is_Public_Child (Id, Par)
1293 and then In_Open_Scopes (Par)
1294 loop
1295 Public_Child := True;
1296 Par := Scope (Par);
1297 Install_Private_Declarations (Par);
1298 Install_Private_With_Clauses (Par);
1299 Pack_Decl := Unit_Declaration_Node (Par);
1300 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1301 end loop;
1302 end if;
1303 end;
1305 if Is_Compilation_Unit (Id) then
1306 Install_Private_With_Clauses (Id);
1307 else
1309 -- The current compilation unit may include private with_clauses,
1310 -- which are visible in the private part of the current nested
1311 -- package, and have to be installed now. This is not done for
1312 -- nested instantiations, where the private with_clauses of the
1313 -- enclosing unit have no effect once the instantiation info is
1314 -- established and we start analyzing the package declaration.
1316 declare
1317 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1318 begin
1319 if Is_Package_Or_Generic_Package (Comp_Unit)
1320 and then not In_Private_Part (Comp_Unit)
1321 and then not In_Instance
1322 then
1323 Install_Private_With_Clauses (Comp_Unit);
1324 Private_With_Clauses_Installed := True;
1325 end if;
1326 end;
1327 end if;
1329 -- If this is a package associated with a generic instance or formal
1330 -- package, then the private declarations of each of the generic's
1331 -- parents must be installed at this point.
1333 if Is_Generic_Instance (Id) then
1334 Install_Parent_Private_Declarations (Id);
1335 end if;
1337 -- Analyze private part if present. The flag In_Private_Part is reset
1338 -- in End_Package_Scope.
1340 L := Last_Entity (Id);
1342 if Present (Priv_Decls) then
1343 Set_In_Private_Part (Id);
1345 -- Upon entering a public child's private part, it may be necessary
1346 -- to declare subprograms that were derived in the package's visible
1347 -- part but not yet made visible.
1349 if Public_Child then
1350 Declare_Inherited_Private_Subprograms (Id);
1351 end if;
1353 Analyze_Declarations (Priv_Decls);
1355 -- Check the private declarations for incomplete deferred constants
1357 Inspect_Deferred_Constant_Completion (Priv_Decls);
1359 -- The first private entity is the immediate follower of the last
1360 -- visible entity, if there was one.
1362 if Present (L) then
1363 Set_First_Private_Entity (Id, Next_Entity (L));
1364 else
1365 Set_First_Private_Entity (Id, First_Entity (Id));
1366 end if;
1368 -- There may be inherited private subprograms that need to be declared,
1369 -- even in the absence of an explicit private part. If there are any
1370 -- public declarations in the package and the package is a public child
1371 -- unit, then an implicit private part is assumed.
1373 elsif Present (L) and then Public_Child then
1374 Set_In_Private_Part (Id);
1375 Declare_Inherited_Private_Subprograms (Id);
1376 Set_First_Private_Entity (Id, Next_Entity (L));
1377 end if;
1379 E := First_Entity (Id);
1380 while Present (E) loop
1382 -- Check rule of 3.6(11), which in general requires waiting till all
1383 -- full types have been seen.
1385 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1386 Check_Aliased_Component_Types (E);
1387 end if;
1389 -- Check preelaborable initialization for full type completing a
1390 -- private type for which pragma Preelaborable_Initialization given.
1392 if Is_Type (E)
1393 and then Must_Have_Preelab_Init (E)
1394 and then not Has_Preelaborable_Initialization (E)
1395 then
1396 Error_Msg_N
1397 ("full view of & does not have preelaborable initialization", E);
1398 end if;
1400 -- An invariant may appear on a full view of a type
1402 if Is_Type (E)
1403 and then Has_Private_Declaration (E)
1404 and then Nkind (Parent (E)) = N_Full_Type_Declaration
1405 and then Has_Aspects (Parent (E))
1406 then
1407 declare
1408 ASN : Node_Id;
1410 begin
1411 ASN := First (Aspect_Specifications (Parent (E)));
1412 while Present (ASN) loop
1413 if Nam_In (Chars (Identifier (ASN)), Name_Invariant,
1414 Name_Type_Invariant)
1415 then
1416 Build_Invariant_Procedure (E, N);
1417 exit;
1418 end if;
1420 Next (ASN);
1421 end loop;
1422 end;
1423 end if;
1425 Next_Entity (E);
1426 end loop;
1428 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1429 -- declaration having a known_discriminant_part shall not be an
1430 -- unchecked union type.
1432 if Present (Vis_Decls) then
1433 Inspect_Unchecked_Union_Completion (Vis_Decls);
1434 end if;
1436 if Present (Priv_Decls) then
1437 Inspect_Unchecked_Union_Completion (Priv_Decls);
1438 end if;
1440 if Ekind (Id) = E_Generic_Package
1441 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1442 and then Present (Priv_Decls)
1443 then
1444 -- Save global references in private declarations, ignoring the
1445 -- visible declarations that were processed earlier.
1447 declare
1448 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1449 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1450 Save_Form : constant List_Id :=
1451 Generic_Formal_Declarations (Orig_Decl);
1453 begin
1454 Set_Visible_Declarations (Orig_Spec, Empty_List);
1455 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1456 Save_Global_References (Orig_Decl);
1457 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1458 Set_Visible_Declarations (Orig_Spec, Save_Vis);
1459 end;
1460 end if;
1462 Process_End_Label (N, 'e', Id);
1464 -- Remove private_with_clauses of enclosing compilation unit, if they
1465 -- were installed.
1467 if Private_With_Clauses_Installed then
1468 Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
1469 end if;
1471 -- For the case of a library level package, we must go through all the
1472 -- entities clearing the indications that the value may be constant and
1473 -- not modified. Why? Because any client of this package may modify
1474 -- these values freely from anywhere. This also applies to any nested
1475 -- packages or generic packages.
1477 -- For now we unconditionally clear constants for packages that are
1478 -- instances of generic packages. The reason is that we do not have the
1479 -- body yet, and we otherwise think things are unreferenced when they
1480 -- are not. This should be fixed sometime (the effect is not terrible,
1481 -- we just lose some warnings, and also some cases of value propagation)
1482 -- ???
1484 if Is_Library_Level_Entity (Id)
1485 or else Is_Generic_Instance (Id)
1486 then
1487 Clear_Constants (Id, First_Entity (Id));
1488 Clear_Constants (Id, First_Private_Entity (Id));
1489 end if;
1491 -- Issue an error in SPARK mode if a package specification contains
1492 -- more than one tagged type or type extension.
1494 Check_One_Tagged_Type_Or_Extension_At_Most;
1496 -- If switch set, output information on why body required
1498 if List_Body_Required_Info
1499 and then In_Extended_Main_Source_Unit (Id)
1500 and then Unit_Requires_Body (Id)
1501 then
1502 Unit_Requires_Body_Info (Id);
1503 end if;
1504 end Analyze_Package_Specification;
1506 --------------------------------------
1507 -- Analyze_Private_Type_Declaration --
1508 --------------------------------------
1510 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1511 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
1512 Id : constant Entity_Id := Defining_Identifier (N);
1514 begin
1515 Generate_Definition (Id);
1516 Set_Is_Pure (Id, PF);
1517 Init_Size_Align (Id);
1519 if not Is_Package_Or_Generic_Package (Current_Scope)
1520 or else In_Private_Part (Current_Scope)
1521 then
1522 Error_Msg_N ("invalid context for private declaration", N);
1523 end if;
1525 New_Private_Type (N, Id, N);
1526 Set_Depends_On_Private (Id);
1528 if Has_Aspects (N) then
1529 Analyze_Aspect_Specifications (N, Id);
1530 end if;
1531 end Analyze_Private_Type_Declaration;
1533 ----------------------------------
1534 -- Check_Anonymous_Access_Types --
1535 ----------------------------------
1537 procedure Check_Anonymous_Access_Types
1538 (Spec_Id : Entity_Id;
1539 P_Body : Node_Id)
1541 E : Entity_Id;
1542 IR : Node_Id;
1544 begin
1545 -- Itype references are only needed by gigi, to force elaboration of
1546 -- itypes. In the absence of code generation, they are not needed.
1548 if not Expander_Active then
1549 return;
1550 end if;
1552 E := First_Entity (Spec_Id);
1553 while Present (E) loop
1554 if Ekind (E) = E_Anonymous_Access_Type
1555 and then From_Limited_With (E)
1556 then
1557 IR := Make_Itype_Reference (Sloc (P_Body));
1558 Set_Itype (IR, E);
1560 if No (Declarations (P_Body)) then
1561 Set_Declarations (P_Body, New_List (IR));
1562 else
1563 Prepend (IR, Declarations (P_Body));
1564 end if;
1565 end if;
1567 Next_Entity (E);
1568 end loop;
1569 end Check_Anonymous_Access_Types;
1571 -------------------------------------------
1572 -- Declare_Inherited_Private_Subprograms --
1573 -------------------------------------------
1575 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1577 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1578 -- Check whether an inherited subprogram S is an operation of an
1579 -- untagged derived type T.
1581 ---------------------
1582 -- Is_Primitive_Of --
1583 ---------------------
1585 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1586 Formal : Entity_Id;
1588 begin
1589 -- If the full view is a scalar type, the type is the anonymous base
1590 -- type, but the operation mentions the first subtype, so check the
1591 -- signature against the base type.
1593 if Base_Type (Etype (S)) = Base_Type (T) then
1594 return True;
1596 else
1597 Formal := First_Formal (S);
1598 while Present (Formal) loop
1599 if Base_Type (Etype (Formal)) = Base_Type (T) then
1600 return True;
1601 end if;
1603 Next_Formal (Formal);
1604 end loop;
1606 return False;
1607 end if;
1608 end Is_Primitive_Of;
1610 -- Local variables
1612 E : Entity_Id;
1613 Op_List : Elist_Id;
1614 Op_Elmt : Elmt_Id;
1615 Op_Elmt_2 : Elmt_Id;
1616 Prim_Op : Entity_Id;
1617 New_Op : Entity_Id := Empty;
1618 Parent_Subp : Entity_Id;
1619 Tag : Entity_Id;
1621 -- Start of processing for Declare_Inherited_Private_Subprograms
1623 begin
1624 E := First_Entity (Id);
1625 while Present (E) loop
1627 -- If the entity is a nonprivate type extension whose parent type
1628 -- is declared in an open scope, then the type may have inherited
1629 -- operations that now need to be made visible. Ditto if the entity
1630 -- is a formal derived type in a child unit.
1632 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
1633 or else
1634 (Nkind (Parent (E)) = N_Private_Extension_Declaration
1635 and then Is_Generic_Type (E)))
1636 and then In_Open_Scopes (Scope (Etype (E)))
1637 and then Is_Base_Type (E)
1638 then
1639 if Is_Tagged_Type (E) then
1640 Op_List := Primitive_Operations (E);
1641 New_Op := Empty;
1642 Tag := First_Tag_Component (E);
1644 Op_Elmt := First_Elmt (Op_List);
1645 while Present (Op_Elmt) loop
1646 Prim_Op := Node (Op_Elmt);
1648 -- Search primitives that are implicit operations with an
1649 -- internal name whose parent operation has a normal name.
1651 if Present (Alias (Prim_Op))
1652 and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
1653 and then not Comes_From_Source (Prim_Op)
1654 and then Is_Internal_Name (Chars (Prim_Op))
1655 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1656 then
1657 Parent_Subp := Alias (Prim_Op);
1659 -- Case 1: Check if the type has also an explicit
1660 -- overriding for this primitive.
1662 Op_Elmt_2 := Next_Elmt (Op_Elmt);
1663 while Present (Op_Elmt_2) loop
1665 -- Skip entities with attribute Interface_Alias since
1666 -- they are not overriding primitives (these entities
1667 -- link an interface primitive with their covering
1668 -- primitive)
1670 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
1671 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
1672 and then No (Interface_Alias (Node (Op_Elmt_2)))
1673 then
1674 -- The private inherited operation has been
1675 -- overridden by an explicit subprogram:
1676 -- replace the former by the latter.
1678 New_Op := Node (Op_Elmt_2);
1679 Replace_Elmt (Op_Elmt, New_Op);
1680 Remove_Elmt (Op_List, Op_Elmt_2);
1681 Set_Overridden_Operation (New_Op, Parent_Subp);
1683 -- We don't need to inherit its dispatching slot.
1684 -- Set_All_DT_Position has previously ensured that
1685 -- the same slot was assigned to the two primitives
1687 if Present (Tag)
1688 and then Present (DTC_Entity (New_Op))
1689 and then Present (DTC_Entity (Prim_Op))
1690 then
1691 pragma Assert
1692 (DT_Position (New_Op) = DT_Position (Prim_Op));
1693 null;
1694 end if;
1696 goto Next_Primitive;
1697 end if;
1699 Next_Elmt (Op_Elmt_2);
1700 end loop;
1702 -- Case 2: We have not found any explicit overriding and
1703 -- hence we need to declare the operation (i.e., make it
1704 -- visible).
1706 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1708 -- Inherit the dispatching slot if E is already frozen
1710 if Is_Frozen (E)
1711 and then Present (DTC_Entity (Alias (Prim_Op)))
1712 then
1713 Set_DTC_Entity_Value (E, New_Op);
1714 Set_DT_Position (New_Op,
1715 DT_Position (Alias (Prim_Op)));
1716 end if;
1718 pragma Assert
1719 (Is_Dispatching_Operation (New_Op)
1720 and then Node (Last_Elmt (Op_List)) = New_Op);
1722 -- Substitute the new operation for the old one in the
1723 -- type's primitive operations list. Since the new
1724 -- operation was also just added to the end of list,
1725 -- the last element must be removed.
1727 -- (Question: is there a simpler way of declaring the
1728 -- operation, say by just replacing the name of the
1729 -- earlier operation, reentering it in the in the symbol
1730 -- table (how?), and marking it as private???)
1732 Replace_Elmt (Op_Elmt, New_Op);
1733 Remove_Last_Elmt (Op_List);
1734 end if;
1736 <<Next_Primitive>>
1737 Next_Elmt (Op_Elmt);
1738 end loop;
1740 -- Generate listing showing the contents of the dispatch table
1742 if Debug_Flag_ZZ then
1743 Write_DT (E);
1744 end if;
1746 else
1747 -- Non-tagged type, scan forward to locate inherited hidden
1748 -- operations.
1750 Prim_Op := Next_Entity (E);
1751 while Present (Prim_Op) loop
1752 if Is_Subprogram (Prim_Op)
1753 and then Present (Alias (Prim_Op))
1754 and then not Comes_From_Source (Prim_Op)
1755 and then Is_Internal_Name (Chars (Prim_Op))
1756 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1757 and then Is_Primitive_Of (E, Prim_Op)
1758 then
1759 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1760 end if;
1762 Next_Entity (Prim_Op);
1763 end loop;
1764 end if;
1765 end if;
1767 Next_Entity (E);
1768 end loop;
1769 end Declare_Inherited_Private_Subprograms;
1771 -----------------------
1772 -- End_Package_Scope --
1773 -----------------------
1775 procedure End_Package_Scope (P : Entity_Id) is
1776 begin
1777 Uninstall_Declarations (P);
1778 Pop_Scope;
1779 end End_Package_Scope;
1781 ---------------------------
1782 -- Exchange_Declarations --
1783 ---------------------------
1785 procedure Exchange_Declarations (Id : Entity_Id) is
1786 Full_Id : constant Entity_Id := Full_View (Id);
1787 H1 : constant Entity_Id := Homonym (Id);
1788 Next1 : constant Entity_Id := Next_Entity (Id);
1789 H2 : Entity_Id;
1790 Next2 : Entity_Id;
1792 begin
1793 -- If missing full declaration for type, nothing to exchange
1795 if No (Full_Id) then
1796 return;
1797 end if;
1799 -- Otherwise complete the exchange, and preserve semantic links
1801 Next2 := Next_Entity (Full_Id);
1802 H2 := Homonym (Full_Id);
1804 -- Reset full declaration pointer to reflect the switched entities and
1805 -- readjust the next entity chains.
1807 Exchange_Entities (Id, Full_Id);
1809 Set_Next_Entity (Id, Next1);
1810 Set_Homonym (Id, H1);
1812 Set_Full_View (Full_Id, Id);
1813 Set_Next_Entity (Full_Id, Next2);
1814 Set_Homonym (Full_Id, H2);
1815 end Exchange_Declarations;
1817 ----------------------------
1818 -- Install_Package_Entity --
1819 ----------------------------
1821 procedure Install_Package_Entity (Id : Entity_Id) is
1822 begin
1823 if not Is_Internal (Id) then
1824 if Debug_Flag_E then
1825 Write_Str ("Install: ");
1826 Write_Name (Chars (Id));
1827 Write_Eol;
1828 end if;
1830 if not Is_Child_Unit (Id) then
1831 Set_Is_Immediately_Visible (Id);
1832 end if;
1834 end if;
1835 end Install_Package_Entity;
1837 ----------------------------------
1838 -- Install_Private_Declarations --
1839 ----------------------------------
1841 procedure Install_Private_Declarations (P : Entity_Id) is
1842 Id : Entity_Id;
1843 Full : Entity_Id;
1844 Priv_Deps : Elist_Id;
1846 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
1847 -- When the full view of a private type is made available, we do the
1848 -- same for its private dependents under proper visibility conditions.
1849 -- When compiling a grand-chid unit this needs to be done recursively.
1851 -----------------------------
1852 -- Swap_Private_Dependents --
1853 -----------------------------
1855 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
1856 Deps : Elist_Id;
1857 Priv : Entity_Id;
1858 Priv_Elmt : Elmt_Id;
1859 Is_Priv : Boolean;
1861 begin
1862 Priv_Elmt := First_Elmt (Priv_Deps);
1863 while Present (Priv_Elmt) loop
1864 Priv := Node (Priv_Elmt);
1866 -- Before the exchange, verify that the presence of the Full_View
1867 -- field. This field will be empty if the entity has already been
1868 -- installed due to a previous call.
1870 if Present (Full_View (Priv))
1871 and then Is_Visible_Dependent (Priv)
1872 then
1873 if Is_Private_Type (Priv) then
1874 Deps := Private_Dependents (Priv);
1875 Is_Priv := True;
1876 else
1877 Is_Priv := False;
1878 end if;
1880 -- For each subtype that is swapped, we also swap the reference
1881 -- to it in Private_Dependents, to allow access to it when we
1882 -- swap them out in End_Package_Scope.
1884 Replace_Elmt (Priv_Elmt, Full_View (Priv));
1885 Exchange_Declarations (Priv);
1886 Set_Is_Immediately_Visible
1887 (Priv, In_Open_Scopes (Scope (Priv)));
1888 Set_Is_Potentially_Use_Visible
1889 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
1891 -- Within a child unit, recurse, except in generic child unit,
1892 -- which (unfortunately) handle private_dependents separately.
1894 if Is_Priv
1895 and then Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
1896 and then not Is_Empty_Elmt_List (Deps)
1897 and then not Inside_A_Generic
1898 then
1899 Swap_Private_Dependents (Deps);
1900 end if;
1901 end if;
1903 Next_Elmt (Priv_Elmt);
1904 end loop;
1905 end Swap_Private_Dependents;
1907 -- Start of processing for Install_Private_Declarations
1909 begin
1910 -- First exchange declarations for private types, so that the full
1911 -- declaration is visible. For each private type, we check its
1912 -- Private_Dependents list and also exchange any subtypes of or derived
1913 -- types from it. Finally, if this is a Taft amendment type, the
1914 -- incomplete declaration is irrelevant, and we want to link the
1915 -- eventual full declaration with the original private one so we
1916 -- also skip the exchange.
1918 Id := First_Entity (P);
1919 while Present (Id) and then Id /= First_Private_Entity (P) loop
1920 if Is_Private_Base_Type (Id)
1921 and then Comes_From_Source (Full_View (Id))
1922 and then Present (Full_View (Id))
1923 and then Scope (Full_View (Id)) = Scope (Id)
1924 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
1925 then
1926 -- If there is a use-type clause on the private type, set the full
1927 -- view accordingly.
1929 Set_In_Use (Full_View (Id), In_Use (Id));
1930 Full := Full_View (Id);
1932 if Is_Private_Base_Type (Full)
1933 and then Has_Private_Declaration (Full)
1934 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
1935 and then In_Open_Scopes (Scope (Etype (Full)))
1936 and then In_Package_Body (Current_Scope)
1937 and then not Is_Private_Type (Etype (Full))
1938 then
1939 -- This is the completion of a private type by a derivation
1940 -- from another private type which is not private anymore. This
1941 -- can only happen in a package nested within a child package,
1942 -- when the parent type is defined in the parent unit. At this
1943 -- point the current type is not private either, and we have
1944 -- to install the underlying full view, which is now visible.
1945 -- Save the current full view as well, so that all views can be
1946 -- restored on exit. It may seem that after compiling the child
1947 -- body there are not environments to restore, but the back-end
1948 -- expects those links to be valid, and freeze nodes depend on
1949 -- them.
1951 if No (Full_View (Full))
1952 and then Present (Underlying_Full_View (Full))
1953 then
1954 Set_Full_View (Id, Underlying_Full_View (Full));
1955 Set_Underlying_Full_View (Id, Full);
1957 Set_Underlying_Full_View (Full, Empty);
1958 Set_Is_Frozen (Full_View (Id));
1959 end if;
1960 end if;
1962 Priv_Deps := Private_Dependents (Id);
1963 Exchange_Declarations (Id);
1964 Set_Is_Immediately_Visible (Id);
1965 Swap_Private_Dependents (Priv_Deps);
1966 end if;
1968 Next_Entity (Id);
1969 end loop;
1971 -- Next make other declarations in the private part visible as well
1973 Id := First_Private_Entity (P);
1974 while Present (Id) loop
1975 Install_Package_Entity (Id);
1976 Set_Is_Hidden (Id, False);
1977 Next_Entity (Id);
1978 end loop;
1980 -- Indicate that the private part is currently visible, so it can be
1981 -- properly reset on exit.
1983 Set_In_Private_Part (P);
1984 end Install_Private_Declarations;
1986 ----------------------------------
1987 -- Install_Visible_Declarations --
1988 ----------------------------------
1990 procedure Install_Visible_Declarations (P : Entity_Id) is
1991 Id : Entity_Id;
1992 Last_Entity : Entity_Id;
1994 begin
1995 pragma Assert
1996 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
1998 if Is_Package_Or_Generic_Package (P) then
1999 Last_Entity := First_Private_Entity (P);
2000 else
2001 Last_Entity := Empty;
2002 end if;
2004 Id := First_Entity (P);
2005 while Present (Id) and then Id /= Last_Entity loop
2006 Install_Package_Entity (Id);
2007 Next_Entity (Id);
2008 end loop;
2009 end Install_Visible_Declarations;
2011 --------------------------
2012 -- Is_Private_Base_Type --
2013 --------------------------
2015 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
2016 begin
2017 return Ekind (E) = E_Private_Type
2018 or else Ekind (E) = E_Limited_Private_Type
2019 or else Ekind (E) = E_Record_Type_With_Private;
2020 end Is_Private_Base_Type;
2022 --------------------------
2023 -- Is_Visible_Dependent --
2024 --------------------------
2026 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
2028 S : constant Entity_Id := Scope (Dep);
2030 begin
2031 -- Renamings created for actual types have the visibility of the actual
2033 if Ekind (S) = E_Package
2034 and then Is_Generic_Instance (S)
2035 and then (Is_Generic_Actual_Type (Dep)
2036 or else Is_Generic_Actual_Type (Full_View (Dep)))
2037 then
2038 return True;
2040 elsif not (Is_Derived_Type (Dep))
2041 and then Is_Derived_Type (Full_View (Dep))
2042 then
2043 -- When instantiating a package body, the scope stack is empty, so
2044 -- check instead whether the dependent type is defined in the same
2045 -- scope as the instance itself.
2047 return In_Open_Scopes (S)
2048 or else (Is_Generic_Instance (Current_Scope)
2049 and then Scope (Dep) = Scope (Current_Scope));
2050 else
2051 return True;
2052 end if;
2053 end Is_Visible_Dependent;
2055 ----------------------------
2056 -- May_Need_Implicit_Body --
2057 ----------------------------
2059 procedure May_Need_Implicit_Body (E : Entity_Id) is
2060 P : constant Node_Id := Unit_Declaration_Node (E);
2061 S : constant Node_Id := Parent (P);
2062 B : Node_Id;
2063 Decls : List_Id;
2065 begin
2066 if not Has_Completion (E)
2067 and then Nkind (P) = N_Package_Declaration
2068 and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
2069 then
2070 B :=
2071 Make_Package_Body (Sloc (E),
2072 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
2073 Chars => Chars (E)),
2074 Declarations => New_List);
2076 if Nkind (S) = N_Package_Specification then
2077 if Present (Private_Declarations (S)) then
2078 Decls := Private_Declarations (S);
2079 else
2080 Decls := Visible_Declarations (S);
2081 end if;
2082 else
2083 Decls := Declarations (S);
2084 end if;
2086 Append (B, Decls);
2087 Analyze (B);
2088 end if;
2089 end May_Need_Implicit_Body;
2091 ----------------------
2092 -- New_Private_Type --
2093 ----------------------
2095 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
2096 begin
2097 -- For other than Ada 2012, enter the name in the current scope
2099 if Ada_Version < Ada_2012 then
2100 Enter_Name (Id);
2102 -- Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2103 -- there may be an incomplete previous view.
2105 else
2106 declare
2107 Prev : Entity_Id;
2108 begin
2109 Prev := Find_Type_Name (N);
2110 pragma Assert (Prev = Id
2111 or else (Ekind (Prev) = E_Incomplete_Type
2112 and then Present (Full_View (Prev))
2113 and then Full_View (Prev) = Id));
2114 end;
2115 end if;
2117 if Limited_Present (Def) then
2118 Set_Ekind (Id, E_Limited_Private_Type);
2119 else
2120 Set_Ekind (Id, E_Private_Type);
2121 end if;
2123 Set_Etype (Id, Id);
2124 Set_Has_Delayed_Freeze (Id);
2125 Set_Is_First_Subtype (Id);
2126 Init_Size_Align (Id);
2128 Set_Is_Constrained (Id,
2129 No (Discriminant_Specifications (N))
2130 and then not Unknown_Discriminants_Present (N));
2132 -- Set tagged flag before processing discriminants, to catch illegal
2133 -- usage.
2135 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
2137 Set_Discriminant_Constraint (Id, No_Elist);
2138 Set_Stored_Constraint (Id, No_Elist);
2140 if Present (Discriminant_Specifications (N)) then
2141 Push_Scope (Id);
2142 Process_Discriminants (N);
2143 End_Scope;
2145 elsif Unknown_Discriminants_Present (N) then
2146 Set_Has_Unknown_Discriminants (Id);
2147 end if;
2149 Set_Private_Dependents (Id, New_Elmt_List);
2151 if Tagged_Present (Def) then
2152 Set_Ekind (Id, E_Record_Type_With_Private);
2153 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2154 Set_Is_Abstract_Type (Id, Abstract_Present (Def));
2155 Set_Is_Limited_Record (Id, Limited_Present (Def));
2156 Set_Has_Delayed_Freeze (Id, True);
2158 -- Create a class-wide type with the same attributes
2160 Make_Class_Wide_Type (Id);
2162 elsif Abstract_Present (Def) then
2163 Error_Msg_N ("only a tagged type can be abstract", N);
2164 end if;
2165 end New_Private_Type;
2167 ----------------------------
2168 -- Uninstall_Declarations --
2169 ----------------------------
2171 procedure Uninstall_Declarations (P : Entity_Id) is
2172 Decl : constant Node_Id := Unit_Declaration_Node (P);
2173 Id : Entity_Id;
2174 Full : Entity_Id;
2175 Priv_Elmt : Elmt_Id;
2176 Priv_Sub : Entity_Id;
2178 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id);
2179 -- Copy to the private declaration the attributes of the full view that
2180 -- need to be available for the partial view also.
2182 function Type_In_Use (T : Entity_Id) return Boolean;
2183 -- Check whether type or base type appear in an active use_type clause
2185 ------------------------------
2186 -- Preserve_Full_Attributes --
2187 ------------------------------
2189 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id) is
2190 Priv_Is_Base_Type : constant Boolean := Is_Base_Type (Priv);
2192 begin
2193 Set_Size_Info (Priv, (Full));
2194 Set_RM_Size (Priv, RM_Size (Full));
2195 Set_Size_Known_At_Compile_Time
2196 (Priv, Size_Known_At_Compile_Time (Full));
2197 Set_Is_Volatile (Priv, Is_Volatile (Full));
2198 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
2199 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full));
2200 Set_Is_Ada_2012_Only (Priv, Is_Ada_2012_Only (Full));
2201 Set_Has_Pragma_Unmodified (Priv, Has_Pragma_Unmodified (Full));
2202 Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced (Full));
2203 Set_Has_Pragma_Unreferenced_Objects
2204 (Priv, Has_Pragma_Unreferenced_Objects
2205 (Full));
2206 if Is_Unchecked_Union (Full) then
2207 Set_Is_Unchecked_Union (Base_Type (Priv));
2208 end if;
2209 -- Why is atomic not copied here ???
2211 if Referenced (Full) then
2212 Set_Referenced (Priv);
2213 end if;
2215 if Priv_Is_Base_Type then
2216 Set_Is_Controlled (Priv, Is_Controlled (Base_Type (Full)));
2217 Set_Finalize_Storage_Only (Priv, Finalize_Storage_Only
2218 (Base_Type (Full)));
2219 Set_Has_Task (Priv, Has_Task (Base_Type (Full)));
2220 Set_Has_Controlled_Component (Priv, Has_Controlled_Component
2221 (Base_Type (Full)));
2222 end if;
2224 Set_Freeze_Node (Priv, Freeze_Node (Full));
2226 -- Propagate information of type invariants, which may be specified
2227 -- for the full view.
2229 if Has_Invariants (Full) and not Has_Invariants (Priv) then
2230 Set_Has_Invariants (Priv);
2231 Set_Subprograms_For_Type (Priv, Subprograms_For_Type (Full));
2232 end if;
2234 if Is_Tagged_Type (Priv)
2235 and then Is_Tagged_Type (Full)
2236 and then not Error_Posted (Full)
2237 then
2238 if Is_Tagged_Type (Priv) then
2240 -- If the type is tagged, the tag itself must be available on
2241 -- the partial view, for expansion purposes.
2243 Set_First_Entity (Priv, First_Entity (Full));
2245 -- If there are discriminants in the partial view, these remain
2246 -- visible. Otherwise only the tag itself is visible, and there
2247 -- are no nameable components in the partial view.
2249 if No (Last_Entity (Priv)) then
2250 Set_Last_Entity (Priv, First_Entity (Priv));
2251 end if;
2252 end if;
2254 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2256 if Has_Discriminants (Full) then
2257 Set_Discriminant_Constraint (Priv,
2258 Discriminant_Constraint (Full));
2259 end if;
2260 end if;
2261 end Preserve_Full_Attributes;
2263 -----------------
2264 -- Type_In_Use --
2265 -----------------
2267 function Type_In_Use (T : Entity_Id) return Boolean is
2268 begin
2269 return Scope (Base_Type (T)) = P
2270 and then (In_Use (T) or else In_Use (Base_Type (T)));
2271 end Type_In_Use;
2273 -- Start of processing for Uninstall_Declarations
2275 begin
2276 Id := First_Entity (P);
2277 while Present (Id) and then Id /= First_Private_Entity (P) loop
2278 if Debug_Flag_E then
2279 Write_Str ("unlinking visible entity ");
2280 Write_Int (Int (Id));
2281 Write_Eol;
2282 end if;
2284 -- On exit from the package scope, we must preserve the visibility
2285 -- established by use clauses in the current scope. Two cases:
2287 -- a) If the entity is an operator, it may be a primitive operator of
2288 -- a type for which there is a visible use-type clause.
2290 -- b) for other entities, their use-visibility is determined by a
2291 -- visible use clause for the package itself. For a generic instance,
2292 -- the instantiation of the formals appears in the visible part,
2293 -- but the formals are private and remain so.
2295 if Ekind (Id) = E_Function
2296 and then Is_Operator_Symbol_Name (Chars (Id))
2297 and then not Is_Hidden (Id)
2298 and then not Error_Posted (Id)
2299 then
2300 Set_Is_Potentially_Use_Visible (Id,
2301 In_Use (P)
2302 or else Type_In_Use (Etype (Id))
2303 or else Type_In_Use (Etype (First_Formal (Id)))
2304 or else (Present (Next_Formal (First_Formal (Id)))
2305 and then
2306 Type_In_Use
2307 (Etype (Next_Formal (First_Formal (Id))))));
2308 else
2309 if In_Use (P) and then not Is_Hidden (Id) then
2311 -- A child unit of a use-visible package remains use-visible
2312 -- only if it is itself a visible child unit. Otherwise it
2313 -- would remain visible in other contexts where P is use-
2314 -- visible, because once compiled it stays in the entity list
2315 -- of its parent unit.
2317 if Is_Child_Unit (Id) then
2318 Set_Is_Potentially_Use_Visible
2319 (Id, Is_Visible_Lib_Unit (Id));
2320 else
2321 Set_Is_Potentially_Use_Visible (Id);
2322 end if;
2324 else
2325 Set_Is_Potentially_Use_Visible (Id, False);
2326 end if;
2327 end if;
2329 -- Local entities are not immediately visible outside of the package
2331 Set_Is_Immediately_Visible (Id, False);
2333 -- If this is a private type with a full view (for example a local
2334 -- subtype of a private type declared elsewhere), ensure that the
2335 -- full view is also removed from visibility: it may be exposed when
2336 -- swapping views in an instantiation.
2338 if Is_Type (Id) and then Present (Full_View (Id)) then
2339 Set_Is_Immediately_Visible (Full_View (Id), False);
2340 end if;
2342 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2343 Check_Abstract_Overriding (Id);
2344 Check_Conventions (Id);
2345 end if;
2347 if Ekind_In (Id, E_Private_Type, E_Limited_Private_Type)
2348 and then No (Full_View (Id))
2349 and then not Is_Generic_Type (Id)
2350 and then not Is_Derived_Type (Id)
2351 then
2352 Error_Msg_N ("missing full declaration for private type&", Id);
2354 elsif Ekind (Id) = E_Record_Type_With_Private
2355 and then not Is_Generic_Type (Id)
2356 and then No (Full_View (Id))
2357 then
2358 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
2359 Error_Msg_N ("missing full declaration for private type&", Id);
2360 else
2361 Error_Msg_N
2362 ("missing full declaration for private extension", Id);
2363 end if;
2365 -- Case of constant, check for deferred constant declaration with
2366 -- no full view. Likely just a matter of a missing expression, or
2367 -- accidental use of the keyword constant.
2369 elsif Ekind (Id) = E_Constant
2371 -- OK if constant value present
2373 and then No (Constant_Value (Id))
2375 -- OK if full view present
2377 and then No (Full_View (Id))
2379 -- OK if imported, since that provides the completion
2381 and then not Is_Imported (Id)
2383 -- OK if object declaration replaced by renaming declaration as
2384 -- a result of OK_To_Rename processing (e.g. for concatenation)
2386 and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration
2388 -- OK if object declaration with the No_Initialization flag set
2390 and then not (Nkind (Parent (Id)) = N_Object_Declaration
2391 and then No_Initialization (Parent (Id)))
2392 then
2393 -- If no private declaration is present, we assume the user did
2394 -- not intend a deferred constant declaration and the problem
2395 -- is simply that the initializing expression is missing.
2397 if not Has_Private_Declaration (Etype (Id)) then
2399 -- We assume that the user did not intend a deferred constant
2400 -- declaration, and the expression is just missing.
2402 Error_Msg_N
2403 ("constant declaration requires initialization expression",
2404 Parent (Id));
2406 if Is_Limited_Type (Etype (Id)) then
2407 Error_Msg_N
2408 ("\if variable intended, remove CONSTANT from declaration",
2409 Parent (Id));
2410 end if;
2412 -- Otherwise if a private declaration is present, then we are
2413 -- missing the full declaration for the deferred constant.
2415 else
2416 Error_Msg_N
2417 ("missing full declaration for deferred constant (RM 7.4)",
2418 Id);
2420 if Is_Limited_Type (Etype (Id)) then
2421 Error_Msg_N
2422 ("\if variable intended, remove CONSTANT from declaration",
2423 Parent (Id));
2424 end if;
2425 end if;
2426 end if;
2428 Next_Entity (Id);
2429 end loop;
2431 -- If the specification was installed as the parent of a public child
2432 -- unit, the private declarations were not installed, and there is
2433 -- nothing to do.
2435 if not In_Private_Part (P) then
2436 return;
2437 else
2438 Set_In_Private_Part (P, False);
2439 end if;
2441 -- Make private entities invisible and exchange full and private
2442 -- declarations for private types. Id is now the first private entity
2443 -- in the package.
2445 while Present (Id) loop
2446 if Debug_Flag_E then
2447 Write_Str ("unlinking private entity ");
2448 Write_Int (Int (Id));
2449 Write_Eol;
2450 end if;
2452 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2453 Check_Abstract_Overriding (Id);
2454 Check_Conventions (Id);
2455 end if;
2457 Set_Is_Immediately_Visible (Id, False);
2459 if Is_Private_Base_Type (Id) and then Present (Full_View (Id)) then
2460 Full := Full_View (Id);
2462 -- If the partial view is not declared in the visible part of the
2463 -- package (as is the case when it is a type derived from some
2464 -- other private type in the private part of the current package),
2465 -- no exchange takes place.
2467 if No (Parent (Id))
2468 or else List_Containing (Parent (Id)) /=
2469 Visible_Declarations (Specification (Decl))
2470 then
2471 goto Next_Id;
2472 end if;
2474 -- The entry in the private part points to the full declaration,
2475 -- which is currently visible. Exchange them so only the private
2476 -- type declaration remains accessible, and link private and full
2477 -- declaration in the opposite direction. Before the actual
2478 -- exchange, we copy back attributes of the full view that must
2479 -- be available to the partial view too.
2481 Preserve_Full_Attributes (Id, Full);
2483 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
2485 if Is_Indefinite_Subtype (Full)
2486 and then not Is_Indefinite_Subtype (Id)
2487 then
2488 Error_Msg_N
2489 ("full view of type must be definite subtype", Full);
2490 end if;
2492 -- Swap out the subtypes and derived types of Id that
2493 -- were compiled in this scope, or installed previously
2494 -- by Install_Private_Declarations.
2496 -- Before we do the swap, we verify the presence of the Full_View
2497 -- field which may be empty due to a swap by a previous call to
2498 -- End_Package_Scope (e.g. from the freezing mechanism).
2500 Priv_Elmt := First_Elmt (Private_Dependents (Id));
2501 while Present (Priv_Elmt) loop
2502 Priv_Sub := Node (Priv_Elmt);
2504 if Present (Full_View (Priv_Sub)) then
2505 if Scope (Priv_Sub) = P
2506 or else not In_Open_Scopes (Scope (Priv_Sub))
2507 then
2508 Set_Is_Immediately_Visible (Priv_Sub, False);
2509 end if;
2511 if Is_Visible_Dependent (Priv_Sub) then
2512 Preserve_Full_Attributes
2513 (Priv_Sub, Full_View (Priv_Sub));
2514 Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub));
2515 Exchange_Declarations (Priv_Sub);
2516 end if;
2517 end if;
2519 Next_Elmt (Priv_Elmt);
2520 end loop;
2522 -- Now restore the type itself to its private view
2524 Exchange_Declarations (Id);
2526 -- If we have installed an underlying full view for a type derived
2527 -- from a private type in a child unit, restore the proper views
2528 -- of private and full view. See corresponding code in
2529 -- Install_Private_Declarations.
2531 -- After the exchange, Full denotes the private type in the
2532 -- visible part of the package.
2534 if Is_Private_Base_Type (Full)
2535 and then Present (Full_View (Full))
2536 and then Present (Underlying_Full_View (Full))
2537 and then In_Package_Body (Current_Scope)
2538 then
2539 Set_Full_View (Full, Underlying_Full_View (Full));
2540 Set_Underlying_Full_View (Full, Empty);
2541 end if;
2543 elsif Ekind (Id) = E_Incomplete_Type
2544 and then Comes_From_Source (Id)
2545 and then No (Full_View (Id))
2546 then
2547 -- Mark Taft amendment types. Verify that there are no primitive
2548 -- operations declared for the type (3.10.1(9)).
2550 Set_Has_Completion_In_Body (Id);
2552 declare
2553 Elmt : Elmt_Id;
2554 Subp : Entity_Id;
2556 begin
2557 Elmt := First_Elmt (Private_Dependents (Id));
2558 while Present (Elmt) loop
2559 Subp := Node (Elmt);
2561 -- Is_Primitive is tested because there can be cases where
2562 -- nonprimitive subprograms (in nested packages) are added
2563 -- to the Private_Dependents list.
2565 if Is_Overloadable (Subp) and then Is_Primitive (Subp) then
2566 Error_Msg_NE
2567 ("type& must be completed in the private part",
2568 Parent (Subp), Id);
2570 -- The result type of an access-to-function type cannot be a
2571 -- Taft-amendment type, unless the version is Ada 2012 or
2572 -- later (see AI05-151).
2574 elsif Ada_Version < Ada_2012
2575 and then Ekind (Subp) = E_Subprogram_Type
2576 then
2577 if Etype (Subp) = Id
2578 or else
2579 (Is_Class_Wide_Type (Etype (Subp))
2580 and then Etype (Etype (Subp)) = Id)
2581 then
2582 Error_Msg_NE
2583 ("type& must be completed in the private part",
2584 Associated_Node_For_Itype (Subp), Id);
2585 end if;
2586 end if;
2588 Next_Elmt (Elmt);
2589 end loop;
2590 end;
2592 elsif not Is_Child_Unit (Id)
2593 and then (not Is_Private_Type (Id) or else No (Full_View (Id)))
2594 then
2595 Set_Is_Hidden (Id);
2596 Set_Is_Potentially_Use_Visible (Id, False);
2597 end if;
2599 <<Next_Id>>
2600 Next_Entity (Id);
2601 end loop;
2602 end Uninstall_Declarations;
2604 ------------------------
2605 -- Unit_Requires_Body --
2606 ------------------------
2608 function Unit_Requires_Body
2609 (P : Entity_Id;
2610 Ignore_Abstract_State : Boolean := False) return Boolean
2612 E : Entity_Id;
2614 begin
2615 -- Imported entity never requires body. Right now, only subprograms can
2616 -- be imported, but perhaps in the future we will allow import of
2617 -- packages.
2619 if Is_Imported (P) then
2620 return False;
2622 -- Body required if library package with pragma Elaborate_Body
2624 elsif Has_Pragma_Elaborate_Body (P) then
2625 return True;
2627 -- Body required if subprogram
2629 elsif Is_Subprogram (P) or else Is_Generic_Subprogram (P) then
2630 return True;
2632 -- Treat a block as requiring a body
2634 elsif Ekind (P) = E_Block then
2635 return True;
2637 elsif Ekind (P) = E_Package
2638 and then Nkind (Parent (P)) = N_Package_Specification
2639 and then Present (Generic_Parent (Parent (P)))
2640 then
2641 declare
2642 G_P : constant Entity_Id := Generic_Parent (Parent (P));
2643 begin
2644 if Has_Pragma_Elaborate_Body (G_P) then
2645 return True;
2646 end if;
2647 end;
2649 -- A [generic] package that introduces at least one non-null abstract
2650 -- state requires completion. However, there is a separate rule that
2651 -- requires that such a package have a reason other than this for a
2652 -- body being required (if necessary a pragma Elaborate_Body must be
2653 -- provided). If Ignore_Abstract_State is True, we don't do this check
2654 -- (so we can use Unit_Requires_Body to check for some other reason).
2656 elsif Ekind_In (P, E_Generic_Package, E_Package)
2657 and then not Ignore_Abstract_State
2658 and then Present (Abstract_States (P))
2659 and then
2660 not Is_Null_State (Node (First_Elmt (Abstract_States (P))))
2661 then
2662 return True;
2663 end if;
2665 -- Otherwise search entity chain for entity requiring completion
2667 E := First_Entity (P);
2668 while Present (E) loop
2670 -- Always ignore child units. Child units get added to the entity
2671 -- list of a parent unit, but are not original entities of the
2672 -- parent, and so do not affect whether the parent needs a body.
2674 if Is_Child_Unit (E) then
2675 null;
2677 -- Ignore formal packages and their renamings
2679 elsif Ekind (E) = E_Package
2680 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
2681 N_Formal_Package_Declaration
2682 then
2683 null;
2685 -- Otherwise test to see if entity requires a completion.
2686 -- Note that subprogram entities whose declaration does not come
2687 -- from source are ignored here on the basis that we assume the
2688 -- expander will provide an implicit completion at some point.
2690 elsif (Is_Overloadable (E)
2691 and then Ekind (E) /= E_Enumeration_Literal
2692 and then Ekind (E) /= E_Operator
2693 and then not Is_Abstract_Subprogram (E)
2694 and then not Has_Completion (E)
2695 and then Comes_From_Source (Parent (E)))
2697 or else
2698 (Ekind (E) = E_Package
2699 and then E /= P
2700 and then not Has_Completion (E)
2701 and then Unit_Requires_Body (E))
2703 or else
2704 (Ekind (E) = E_Incomplete_Type
2705 and then No (Full_View (E))
2706 and then not Is_Generic_Type (E))
2708 or else
2709 (Ekind_In (E, E_Task_Type, E_Protected_Type)
2710 and then not Has_Completion (E))
2712 or else
2713 (Ekind (E) = E_Generic_Package
2714 and then E /= P
2715 and then not Has_Completion (E)
2716 and then Unit_Requires_Body (E))
2718 or else
2719 (Is_Generic_Subprogram (E)
2720 and then not Has_Completion (E))
2722 then
2723 return True;
2725 -- Entity that does not require completion
2727 else
2728 null;
2729 end if;
2731 Next_Entity (E);
2732 end loop;
2734 return False;
2735 end Unit_Requires_Body;
2737 -----------------------------
2738 -- Unit_Requires_Body_Info --
2739 -----------------------------
2741 procedure Unit_Requires_Body_Info (P : Entity_Id) is
2742 E : Entity_Id;
2744 begin
2745 -- Imported entity never requires body. Right now, only subprograms can
2746 -- be imported, but perhaps in the future we will allow import of
2747 -- packages.
2749 if Is_Imported (P) then
2750 return;
2752 -- Body required if library package with pragma Elaborate_Body
2754 elsif Has_Pragma_Elaborate_Body (P) then
2755 Error_Msg_N
2756 ("?Y?info: & requires body (Elaborate_Body)", P);
2758 -- Body required if subprogram
2760 elsif Is_Subprogram (P) or else Is_Generic_Subprogram (P) then
2761 Error_Msg_N ("?Y?info: & requires body (subprogram case)", P);
2763 -- Body required if generic parent has Elaborate_Body
2765 elsif Ekind (P) = E_Package
2766 and then Nkind (Parent (P)) = N_Package_Specification
2767 and then Present (Generic_Parent (Parent (P)))
2768 then
2769 declare
2770 G_P : constant Entity_Id := Generic_Parent (Parent (P));
2771 begin
2772 if Has_Pragma_Elaborate_Body (G_P) then
2773 Error_Msg_N
2774 ("?Y?info: & requires body (generic parent Elaborate_Body)",
2776 end if;
2777 end;
2779 -- A [generic] package that introduces at least one non-null abstract
2780 -- state requires completion. However, there is a separate rule that
2781 -- requires that such a package have a reason other than this for a
2782 -- body being required (if necessary a pragma Elaborate_Body must be
2783 -- provided). If Ignore_Abstract_State is True, we don't do this check
2784 -- (so we can use Unit_Requires_Body to check for some other reason).
2786 elsif Ekind_In (P, E_Generic_Package, E_Package)
2787 and then Present (Abstract_States (P))
2788 and then
2789 not Is_Null_State (Node (First_Elmt (Abstract_States (P))))
2790 then
2791 Error_Msg_N
2792 ("?Y?info: & requires body (non-null abstract state aspect)",
2794 end if;
2796 -- Otherwise search entity chain for entity requiring completion
2798 E := First_Entity (P);
2799 while Present (E) loop
2801 -- Always ignore child units. Child units get added to the entity
2802 -- list of a parent unit, but are not original entities of the
2803 -- parent, and so do not affect whether the parent needs a body.
2805 if Is_Child_Unit (E) then
2806 null;
2808 -- Ignore formal packages and their renamings
2810 elsif Ekind (E) = E_Package
2811 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
2812 N_Formal_Package_Declaration
2813 then
2814 null;
2816 -- Otherwise test to see if entity requires a completion.
2817 -- Note that subprogram entities whose declaration does not come
2818 -- from source are ignored here on the basis that we assume the
2819 -- expander will provide an implicit completion at some point.
2821 elsif (Is_Overloadable (E)
2822 and then Ekind (E) /= E_Enumeration_Literal
2823 and then Ekind (E) /= E_Operator
2824 and then not Is_Abstract_Subprogram (E)
2825 and then not Has_Completion (E)
2826 and then Comes_From_Source (Parent (E)))
2828 or else
2829 (Ekind (E) = E_Package
2830 and then E /= P
2831 and then not Has_Completion (E)
2832 and then Unit_Requires_Body (E))
2834 or else
2835 (Ekind (E) = E_Incomplete_Type
2836 and then No (Full_View (E))
2837 and then not Is_Generic_Type (E))
2839 or else
2840 (Ekind_In (E, E_Task_Type, E_Protected_Type)
2841 and then not Has_Completion (E))
2843 or else
2844 (Ekind (E) = E_Generic_Package
2845 and then E /= P
2846 and then not Has_Completion (E)
2847 and then Unit_Requires_Body (E))
2849 or else
2850 (Is_Generic_Subprogram (E)
2851 and then not Has_Completion (E))
2853 then
2854 Error_Msg_Node_2 := E;
2855 Error_Msg_NE
2856 ("?Y?info: & requires body (& requires completion)",
2857 E, P);
2859 -- Entity that does not require completion
2861 else
2862 null;
2863 end if;
2865 Next_Entity (E);
2866 end loop;
2867 end Unit_Requires_Body_Info;
2868 end Sem_Ch7;