2014-01-30 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ada / sem_ch7.adb
blob4b3b613e8da7a6e22dcc94d0404cb93915356977
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_Prag; use Sem_Prag;
60 with Sem_Util; use Sem_Util;
61 with Sem_Warn; use Sem_Warn;
62 with Snames; use Snames;
63 with Stand; use Stand;
64 with Sinfo; use Sinfo;
65 with Sinput; use Sinput;
66 with Style;
67 with Uintp; use Uintp;
69 package body Sem_Ch7 is
71 -----------------------------------
72 -- Handling private declarations --
73 -----------------------------------
75 -- The principle that each entity has a single defining occurrence clashes
76 -- with the presence of two separate definitions for private types: the
77 -- first is the private type declaration, and the second is the full type
78 -- declaration. It is important that all references to the type point to
79 -- the same defining occurrence, namely the first one. To enforce the two
80 -- separate views of the entity, the corresponding information is swapped
81 -- between the two declarations. Outside of the package, the defining
82 -- occurrence only contains the private declaration information, while in
83 -- the private part and the body of the package the defining occurrence
84 -- contains the full declaration. To simplify the swap, the defining
85 -- occurrence that currently holds the private declaration points to the
86 -- full declaration. During semantic processing the defining occurrence
87 -- also points to a list of private dependents, that is to say access types
88 -- or composite types whose designated types or component types are
89 -- subtypes or derived types of the private type in question. After the
90 -- full declaration has been seen, the private dependents are updated to
91 -- indicate that they have full definitions.
93 -----------------------
94 -- Local Subprograms --
95 -----------------------
97 procedure Analyze_Package_Body_Helper (N : Node_Id);
98 -- Does all the real work of Analyze_Package_Body
100 procedure Check_Anonymous_Access_Types
101 (Spec_Id : Entity_Id;
102 P_Body : Node_Id);
103 -- If the spec of a package has a limited_with_clause, it may declare
104 -- anonymous access types whose designated type is a limited view, such an
105 -- anonymous access return type for a function. This access type cannot be
106 -- elaborated in the spec itself, but it may need an itype reference if it
107 -- is used within a nested scope. In that case the itype reference is
108 -- created at the beginning of the corresponding package body and inserted
109 -- before other body declarations.
111 procedure Install_Package_Entity (Id : Entity_Id);
112 -- Supporting procedure for Install_{Visible,Private}_Declarations. Places
113 -- one entity on its visibility chain, and recurses on the visible part if
114 -- the entity is an inner package.
116 function Is_Private_Base_Type (E : Entity_Id) return Boolean;
117 -- True for a private type that is not a subtype
119 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean;
120 -- If the private dependent is a private type whose full view is derived
121 -- from the parent type, its full properties are revealed only if we are in
122 -- the immediate scope of the private dependent. Should this predicate be
123 -- tightened further???
125 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id);
126 -- Called upon entering the private part of a public child package and the
127 -- body of a nested package, to potentially declare certain inherited
128 -- subprograms that were inherited by types in the visible part, but whose
129 -- declaration was deferred because the parent operation was private and
130 -- not visible at that point. These subprograms are located by traversing
131 -- the visible part declarations looking for non-private type extensions
132 -- and then examining each of the primitive operations of such types to
133 -- find those that were inherited but declared with a special internal
134 -- name. Each such operation is now declared as an operation with a normal
135 -- name (using the name of the parent operation) and replaces the previous
136 -- implicit operation in the primitive operations list of the type. If the
137 -- inherited private operation has been overridden, then it's replaced by
138 -- the overriding operation.
140 procedure Unit_Requires_Body_Info (P : Entity_Id);
141 -- Outputs info messages showing why package specification P requires a
142 -- body. Caller has checked that the switch requesting this information
143 -- is set, and that the package does indeed require a body.
145 --------------------------
146 -- Analyze_Package_Body --
147 --------------------------
149 procedure Analyze_Package_Body (N : Node_Id) is
150 Loc : constant Source_Ptr := Sloc (N);
152 begin
153 if Debug_Flag_C then
154 Write_Str ("==> package body ");
155 Write_Name (Chars (Defining_Entity (N)));
156 Write_Str (" from ");
157 Write_Location (Loc);
158 Write_Eol;
159 Indent;
160 end if;
162 -- The real work is split out into the helper, so it can do "return;"
163 -- without skipping the debug output.
165 Analyze_Package_Body_Helper (N);
167 if Debug_Flag_C then
168 Outdent;
169 Write_Str ("<== package body ");
170 Write_Name (Chars (Defining_Entity (N)));
171 Write_Str (" from ");
172 Write_Location (Loc);
173 Write_Eol;
174 end if;
175 end Analyze_Package_Body;
177 -----------------------------------
178 -- Analyze_Package_Body_Contract --
179 -----------------------------------
181 procedure Analyze_Package_Body_Contract (Body_Id : Entity_Id) is
182 Spec_Id : constant Entity_Id := Spec_Entity (Body_Id);
183 Prag : Node_Id;
185 begin
186 Prag := Get_Pragma (Body_Id, Pragma_Refined_State);
188 -- The analysis of pragma Refined_State detects whether the spec has
189 -- abstract states available for refinement.
191 if Present (Prag) then
192 Analyze_Refined_State_In_Decl_Part (Prag);
194 -- State refinement is required when the package declaration has
195 -- abstract states. Null states are not considered.
197 elsif Requires_State_Refinement (Spec_Id, Body_Id) then
198 Error_Msg_N ("package & requires state refinement", Spec_Id);
199 end if;
200 end Analyze_Package_Body_Contract;
202 ---------------------------------
203 -- Analyze_Package_Body_Helper --
204 ---------------------------------
206 procedure Analyze_Package_Body_Helper (N : Node_Id) is
207 HSS : Node_Id;
208 Body_Id : Entity_Id;
209 Spec_Id : Entity_Id;
210 Last_Spec_Entity : Entity_Id;
211 New_N : Node_Id;
212 Pack_Decl : Node_Id;
214 procedure Install_Composite_Operations (P : Entity_Id);
215 -- Composite types declared in the current scope may depend on types
216 -- that were private at the point of declaration, and whose full view
217 -- is now in scope. Indicate that the corresponding operations on the
218 -- composite type are available.
220 ----------------------------------
221 -- Install_Composite_Operations --
222 ----------------------------------
224 procedure Install_Composite_Operations (P : Entity_Id) is
225 Id : Entity_Id;
227 begin
228 Id := First_Entity (P);
229 while Present (Id) loop
230 if Is_Type (Id)
231 and then (Is_Limited_Composite (Id)
232 or else Is_Private_Composite (Id))
233 and then No (Private_Component (Id))
234 then
235 Set_Is_Limited_Composite (Id, False);
236 Set_Is_Private_Composite (Id, False);
237 end if;
239 Next_Entity (Id);
240 end loop;
241 end Install_Composite_Operations;
243 -- Start of processing for Analyze_Package_Body_Helper
245 begin
246 -- Find corresponding package specification, and establish the current
247 -- scope. The visible defining entity for the package is the defining
248 -- occurrence in the spec. On exit from the package body, all body
249 -- declarations are attached to the defining entity for the body, but
250 -- the later is never used for name resolution. In this fashion there
251 -- is only one visible entity that denotes the package.
253 -- Set Body_Id. Note that this will be reset to point to the generic
254 -- copy later on in the generic case.
256 Body_Id := Defining_Entity (N);
258 -- Body is body of package instantiation. Corresponding spec has already
259 -- been set.
261 if Present (Corresponding_Spec (N)) then
262 Spec_Id := Corresponding_Spec (N);
263 Pack_Decl := Unit_Declaration_Node (Spec_Id);
265 else
266 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
268 if Present (Spec_Id)
269 and then Is_Package_Or_Generic_Package (Spec_Id)
270 then
271 Pack_Decl := Unit_Declaration_Node (Spec_Id);
273 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
274 Error_Msg_N ("cannot supply body for package renaming", N);
275 return;
277 elsif Present (Corresponding_Body (Pack_Decl)) then
278 Error_Msg_N ("redefinition of package body", N);
279 return;
280 end if;
282 else
283 Error_Msg_N ("missing specification for package body", N);
284 return;
285 end if;
287 if Is_Package_Or_Generic_Package (Spec_Id)
288 and then (Scope (Spec_Id) = Standard_Standard
289 or else Is_Child_Unit (Spec_Id))
290 and then not Unit_Requires_Body (Spec_Id)
291 then
292 if Ada_Version = Ada_83 then
293 Error_Msg_N
294 ("optional package body (not allowed in Ada 95)??", N);
295 else
296 Error_Msg_N ("spec of this package does not allow a body", N);
297 end if;
298 end if;
299 end if;
301 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
302 Style.Check_Identifier (Body_Id, Spec_Id);
304 if Is_Child_Unit (Spec_Id) then
305 if Nkind (Parent (N)) /= N_Compilation_Unit then
306 Error_Msg_NE
307 ("body of child unit& cannot be an inner package", N, Spec_Id);
308 end if;
310 Set_Is_Child_Unit (Body_Id);
311 end if;
313 -- Generic package case
315 if Ekind (Spec_Id) = E_Generic_Package then
317 -- Disable expansion and perform semantic analysis on copy. The
318 -- unannotated body will be used in all instantiations.
320 Body_Id := Defining_Entity (N);
321 Set_Ekind (Body_Id, E_Package_Body);
322 Set_Scope (Body_Id, Scope (Spec_Id));
323 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
324 Set_Body_Entity (Spec_Id, Body_Id);
325 Set_Spec_Entity (Body_Id, Spec_Id);
327 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
328 Rewrite (N, New_N);
330 -- Update Body_Id to point to the copied node for the remainder of
331 -- the processing.
333 Body_Id := Defining_Entity (N);
334 Start_Generic;
335 end if;
337 -- The Body_Id is that of the copied node in the generic case, the
338 -- current node otherwise. Note that N was rewritten above, so we must
339 -- be sure to get the latest Body_Id value.
341 Set_Ekind (Body_Id, E_Package_Body);
342 Set_Body_Entity (Spec_Id, Body_Id);
343 Set_Spec_Entity (Body_Id, Spec_Id);
344 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
346 -- Defining name for the package body is not a visible entity: Only the
347 -- defining name for the declaration is visible.
349 Set_Etype (Body_Id, Standard_Void_Type);
350 Set_Scope (Body_Id, Scope (Spec_Id));
351 Set_Corresponding_Spec (N, Spec_Id);
352 Set_Corresponding_Body (Pack_Decl, Body_Id);
354 -- The body entity is not used for semantics or code generation, but
355 -- it is attached to the entity list of the enclosing scope to simplify
356 -- the listing of back-annotations for the types it main contain.
358 if Scope (Spec_Id) /= Standard_Standard then
359 Append_Entity (Body_Id, Scope (Spec_Id));
360 end if;
362 -- Indicate that we are currently compiling the body of the package
364 Set_In_Package_Body (Spec_Id);
365 Set_Has_Completion (Spec_Id);
366 Last_Spec_Entity := Last_Entity (Spec_Id);
368 if Has_Aspects (N) then
369 Analyze_Aspect_Specifications (N, Body_Id);
370 end if;
372 Push_Scope (Spec_Id);
374 -- Set SPARK_Mode only for non-generic package
376 if Ekind (Spec_Id) = E_Package then
378 -- Set SPARK_Mode from context
380 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
381 Set_SPARK_Pragma_Inherited (Body_Id, True);
383 -- Set elaboration code SPARK mode the same for now
385 Set_SPARK_Aux_Pragma (Body_Id, SPARK_Pragma (Body_Id));
386 Set_SPARK_Aux_Pragma_Inherited (Body_Id, True);
387 end if;
389 Set_Categorization_From_Pragmas (N);
391 Install_Visible_Declarations (Spec_Id);
392 Install_Private_Declarations (Spec_Id);
393 Install_Private_With_Clauses (Spec_Id);
394 Install_Composite_Operations (Spec_Id);
396 Check_Anonymous_Access_Types (Spec_Id, N);
398 if Ekind (Spec_Id) = E_Generic_Package then
399 Set_Use (Generic_Formal_Declarations (Pack_Decl));
400 end if;
402 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
403 Set_Use (Private_Declarations (Specification (Pack_Decl)));
405 -- This is a nested package, so it may be necessary to declare certain
406 -- inherited subprograms that are not yet visible because the parent
407 -- type's subprograms are now visible.
409 if Ekind (Scope (Spec_Id)) = E_Package
410 and then Scope (Spec_Id) /= Standard_Standard
411 then
412 Declare_Inherited_Private_Subprograms (Spec_Id);
413 end if;
415 if Present (Declarations (N)) then
416 Analyze_Declarations (Declarations (N));
417 Inspect_Deferred_Constant_Completion (Declarations (N));
418 end if;
420 -- After declarations have been analyzed, the body has been set to have
421 -- the final value of SPARK_Mode. Check that the SPARK_Mode for the body
422 -- is consistent with the SPARK_Mode for the spec.
424 if Present (SPARK_Pragma (Body_Id)) then
425 if Present (SPARK_Aux_Pragma (Spec_Id)) then
426 if Get_SPARK_Mode_From_Pragma (SPARK_Aux_Pragma (Spec_Id)) = Off
427 and then
428 Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Body_Id)) = On
429 then
430 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
431 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
432 Error_Msg_Sloc := Sloc (SPARK_Aux_Pragma (Spec_Id));
433 Error_Msg_NE
434 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
435 end if;
437 else
438 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
439 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
440 Error_Msg_Sloc := Sloc (Spec_Id);
441 Error_Msg_NE
442 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
443 end if;
444 end if;
446 -- Analyze_Declarations has caused freezing of all types. Now generate
447 -- bodies for RACW primitives and stream attributes, if any.
449 if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then
451 -- Attach subprogram bodies to support RACWs declared in spec
453 Append_RACW_Bodies (Declarations (N), Spec_Id);
454 Analyze_List (Declarations (N));
455 end if;
457 HSS := Handled_Statement_Sequence (N);
459 if Present (HSS) then
460 Process_End_Label (HSS, 't', Spec_Id);
461 Analyze (HSS);
463 -- Check that elaboration code in a preelaborable package body is
464 -- empty other than null statements and labels (RM 10.2.1(6)).
466 Validate_Null_Statement_Sequence (N);
467 end if;
469 Validate_Categorization_Dependency (N, Spec_Id);
470 Check_Completion (Body_Id);
472 -- Generate start of body reference. Note that we do this fairly late,
473 -- because the call will use In_Extended_Main_Source_Unit as a check,
474 -- and we want to make sure that Corresponding_Stub links are set
476 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
478 -- For a generic package, collect global references and mark them on
479 -- the original body so that they are not resolved again at the point
480 -- of instantiation.
482 if Ekind (Spec_Id) /= E_Package then
483 Save_Global_References (Original_Node (N));
484 End_Generic;
485 end if;
487 -- The entities of the package body have so far been chained onto the
488 -- declaration chain for the spec. That's been fine while we were in the
489 -- body, since we wanted them to be visible, but now that we are leaving
490 -- the package body, they are no longer visible, so we remove them from
491 -- the entity chain of the package spec entity, and copy them to the
492 -- entity chain of the package body entity, where they will never again
493 -- be visible.
495 if Present (Last_Spec_Entity) then
496 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
497 Set_Next_Entity (Last_Spec_Entity, Empty);
498 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
499 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
501 else
502 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
503 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
504 Set_First_Entity (Spec_Id, Empty);
505 Set_Last_Entity (Spec_Id, Empty);
506 end if;
508 End_Package_Scope (Spec_Id);
510 -- All entities declared in body are not visible
512 declare
513 E : Entity_Id;
515 begin
516 E := First_Entity (Body_Id);
517 while Present (E) loop
518 Set_Is_Immediately_Visible (E, False);
519 Set_Is_Potentially_Use_Visible (E, False);
520 Set_Is_Hidden (E);
522 -- Child units may appear on the entity list (e.g. if they appear
523 -- in the context of a subunit) but they are not body entities.
525 if not Is_Child_Unit (E) then
526 Set_Is_Package_Body_Entity (E);
527 end if;
529 Next_Entity (E);
530 end loop;
531 end;
533 Check_References (Body_Id);
535 -- For a generic unit, check that the formal parameters are referenced,
536 -- and that local variables are used, as for regular packages.
538 if Ekind (Spec_Id) = E_Generic_Package then
539 Check_References (Spec_Id);
540 end if;
542 -- The processing so far has made all entities of the package body
543 -- public (i.e. externally visible to the linker). This is in general
544 -- necessary, since inlined or generic bodies, for which code is
545 -- generated in other units, may need to see these entities. The
546 -- following loop runs backwards from the end of the entities of the
547 -- package body making these entities invisible until we reach a
548 -- referencer, i.e. a declaration that could reference a previous
549 -- declaration, a generic body or an inlined body, or a stub (which may
550 -- contain either of these). This is of course an approximation, but it
551 -- is conservative and definitely correct.
553 -- We only do this at the outer (library) level non-generic packages.
554 -- The reason is simply to cut down on the number of global symbols
555 -- generated, which has a double effect: (1) to make the compilation
556 -- process more efficient and (2) to give the code generator more
557 -- freedom to optimize within each unit, especially subprograms.
559 if (Scope (Spec_Id) = Standard_Standard or else Is_Child_Unit (Spec_Id))
560 and then not Is_Generic_Unit (Spec_Id)
561 and then Present (Declarations (N))
562 then
563 Make_Non_Public_Where_Possible : declare
565 function Has_Referencer
566 (L : List_Id;
567 Outer : Boolean) return Boolean;
568 -- Traverse given list of declarations in reverse order. Return
569 -- True if a referencer is present. Return False if none is found.
571 -- The Outer parameter is True for the outer level call and False
572 -- for inner level calls for nested packages. If Outer is True,
573 -- then any entities up to the point of hitting a referencer get
574 -- their Is_Public flag cleared, so that the entities will be
575 -- treated as static entities in the C sense, and need not have
576 -- fully qualified names. Furthermore, if the referencer is an
577 -- inlined subprogram that doesn't reference other subprograms,
578 -- we keep clearing the Is_Public flag on subprograms. For inner
579 -- levels, we need all names to be fully qualified to deal with
580 -- the same name appearing in parallel packages (right now this
581 -- is tied to their being external).
583 --------------------
584 -- Has_Referencer --
585 --------------------
587 function Has_Referencer
588 (L : List_Id;
589 Outer : Boolean) return Boolean
591 Has_Referencer_Except_For_Subprograms : Boolean := False;
593 D : Node_Id;
594 E : Entity_Id;
595 K : Node_Kind;
596 S : Entity_Id;
598 function Check_Subprogram_Ref (N : Node_Id)
599 return Traverse_Result;
600 -- Look for references to subprograms
602 --------------------------
603 -- Check_Subprogram_Ref --
604 --------------------------
606 function Check_Subprogram_Ref (N : Node_Id)
607 return Traverse_Result
609 V : Node_Id;
611 begin
612 -- Check name of procedure or function calls
614 if Nkind (N) in N_Subprogram_Call
615 and then Is_Entity_Name (Name (N))
616 then
617 return Abandon;
618 end if;
620 -- Check prefix of attribute references
622 if Nkind (N) = N_Attribute_Reference
623 and then Is_Entity_Name (Prefix (N))
624 and then Present (Entity (Prefix (N)))
625 and then Ekind (Entity (Prefix (N))) in Subprogram_Kind
626 then
627 return Abandon;
628 end if;
630 -- Check value of constants
632 if Nkind (N) = N_Identifier
633 and then Present (Entity (N))
634 and then Ekind (Entity (N)) = E_Constant
635 then
636 V := Constant_Value (Entity (N));
638 if Present (V)
639 and then not Compile_Time_Known_Value_Or_Aggr (V)
640 then
641 return Abandon;
642 end if;
643 end if;
645 return OK;
646 end Check_Subprogram_Ref;
648 function Check_Subprogram_Refs is
649 new Traverse_Func (Check_Subprogram_Ref);
651 -- Start of processing for Has_Referencer
653 begin
654 if No (L) then
655 return False;
656 end if;
658 D := Last (L);
659 while Present (D) loop
660 K := Nkind (D);
662 if K in N_Body_Stub then
663 return True;
665 -- Processing for subprogram bodies
667 elsif K = N_Subprogram_Body then
668 if Acts_As_Spec (D) then
669 E := Defining_Entity (D);
671 -- An inlined body acts as a referencer. Note also
672 -- that we never reset Is_Public for an inlined
673 -- subprogram. Gigi requires Is_Public to be set.
675 -- Note that we test Has_Pragma_Inline here rather
676 -- than Is_Inlined. We are compiling this for a
677 -- client, and it is the client who will decide if
678 -- actual inlining should occur, so we need to assume
679 -- that the procedure could be inlined for the purpose
680 -- of accessing global entities.
682 if Has_Pragma_Inline (E) then
683 if Outer
684 and then Check_Subprogram_Refs (D) = OK
685 then
686 Has_Referencer_Except_For_Subprograms := True;
687 else
688 return True;
689 end if;
690 else
691 Set_Is_Public (E, False);
692 end if;
694 else
695 E := Corresponding_Spec (D);
697 if Present (E) then
699 -- A generic subprogram body acts as a referencer
701 if Is_Generic_Unit (E) then
702 return True;
703 end if;
705 if Has_Pragma_Inline (E) or else Is_Inlined (E) then
706 if Outer
707 and then Check_Subprogram_Refs (D) = OK
708 then
709 Has_Referencer_Except_For_Subprograms := True;
710 else
711 return True;
712 end if;
713 end if;
714 end if;
715 end if;
717 -- Processing for package bodies
719 elsif K = N_Package_Body
720 and then Present (Corresponding_Spec (D))
721 then
722 E := Corresponding_Spec (D);
724 -- Generic package body is a referencer. It would seem
725 -- that we only have to consider generics that can be
726 -- exported, i.e. where the corresponding spec is the
727 -- spec of the current package, but because of nested
728 -- instantiations, a fully private generic body may
729 -- export other private body entities. Furthermore,
730 -- regardless of whether there was a previous inlined
731 -- subprogram, (an instantiation of) the generic package
732 -- may reference any entity declared before it.
734 if Is_Generic_Unit (E) then
735 return True;
737 -- For non-generic package body, recurse into body unless
738 -- this is an instance, we ignore instances since they
739 -- cannot have references that affect outer entities.
741 elsif not Is_Generic_Instance (E)
742 and then not Has_Referencer_Except_For_Subprograms
743 then
744 if Has_Referencer
745 (Declarations (D), Outer => False)
746 then
747 return True;
748 end if;
749 end if;
751 -- Processing for package specs, recurse into declarations.
752 -- Again we skip this for the case of generic instances.
754 elsif K = N_Package_Declaration
755 and then not Has_Referencer_Except_For_Subprograms
756 then
757 S := Specification (D);
759 if not Is_Generic_Unit (Defining_Entity (S)) then
760 if Has_Referencer
761 (Private_Declarations (S), Outer => False)
762 then
763 return True;
764 elsif Has_Referencer
765 (Visible_Declarations (S), Outer => False)
766 then
767 return True;
768 end if;
769 end if;
771 -- Objects and exceptions need not be public if we have not
772 -- encountered a referencer so far. We only reset the flag
773 -- for outer level entities that are not imported/exported,
774 -- and which have no interface name.
776 elsif Nkind_In (K, N_Object_Declaration,
777 N_Exception_Declaration,
778 N_Subprogram_Declaration)
779 then
780 E := Defining_Entity (D);
782 if Outer
783 and then (not Has_Referencer_Except_For_Subprograms
784 or else K = N_Subprogram_Declaration)
785 and then not Is_Imported (E)
786 and then not Is_Exported (E)
787 and then No (Interface_Name (E))
788 then
789 Set_Is_Public (E, False);
790 end if;
791 end if;
793 Prev (D);
794 end loop;
796 return Has_Referencer_Except_For_Subprograms;
797 end Has_Referencer;
799 -- Start of processing for Make_Non_Public_Where_Possible
801 begin
802 declare
803 Discard : Boolean;
804 pragma Warnings (Off, Discard);
806 begin
807 Discard := Has_Referencer (Declarations (N), Outer => True);
808 end;
809 end Make_Non_Public_Where_Possible;
810 end if;
812 -- If expander is not active, then here is where we turn off the
813 -- In_Package_Body flag, otherwise it is turned off at the end of the
814 -- corresponding expansion routine. If this is an instance body, we need
815 -- to qualify names of local entities, because the body may have been
816 -- compiled as a preliminary to another instantiation.
818 if not Expander_Active then
819 Set_In_Package_Body (Spec_Id, False);
821 if Is_Generic_Instance (Spec_Id)
822 and then Operating_Mode = Generate_Code
823 then
824 Qualify_Entity_Names (N);
825 end if;
826 end if;
827 end Analyze_Package_Body_Helper;
829 ------------------------------
830 -- Analyze_Package_Contract --
831 ------------------------------
833 procedure Analyze_Package_Contract (Pack_Id : Entity_Id) is
834 Prag : Node_Id;
836 begin
837 -- Analyze the initialization related pragmas. Initializes must come
838 -- before Initial_Condition due to item dependencies.
840 Prag := Get_Pragma (Pack_Id, Pragma_Initializes);
842 if Present (Prag) then
843 Analyze_Initializes_In_Decl_Part (Prag);
844 end if;
846 Prag := Get_Pragma (Pack_Id, Pragma_Initial_Condition);
848 if Present (Prag) then
849 Analyze_Initial_Condition_In_Decl_Part (Prag);
850 end if;
852 -- Check whether the lack of indicator Part_Of agrees with the placement
853 -- of the package instantiation with respect to the state space.
855 if Is_Generic_Instance (Pack_Id) then
856 Prag := Get_Pragma (Pack_Id, Pragma_Part_Of);
858 if No (Prag) then
859 Check_Missing_Part_Of (Pack_Id);
860 end if;
861 end if;
862 end Analyze_Package_Contract;
864 ---------------------------------
865 -- Analyze_Package_Declaration --
866 ---------------------------------
868 procedure Analyze_Package_Declaration (N : Node_Id) is
869 Id : constant Node_Id := Defining_Entity (N);
871 PF : Boolean;
872 -- True when in the context of a declared pure library unit
874 Body_Required : Boolean;
875 -- True when this package declaration requires a corresponding body
877 Comp_Unit : Boolean;
878 -- True when this package declaration is not a nested declaration
880 begin
881 if Debug_Flag_C then
882 Write_Str ("==> package spec ");
883 Write_Name (Chars (Id));
884 Write_Str (" from ");
885 Write_Location (Sloc (N));
886 Write_Eol;
887 Indent;
888 end if;
890 Generate_Definition (Id);
891 Enter_Name (Id);
892 Set_Ekind (Id, E_Package);
893 Set_Etype (Id, Standard_Void_Type);
894 Set_Contract (Id, Make_Contract (Sloc (Id)));
896 -- Set SPARK_Mode from context only for non-generic package
898 if Ekind (Id) = E_Package then
899 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
900 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
901 Set_SPARK_Pragma_Inherited (Id, True);
902 Set_SPARK_Aux_Pragma_Inherited (Id, True);
903 end if;
905 -- Analyze aspect specifications immediately, since we need to recognize
906 -- things like Pure early enough to diagnose violations during analysis.
908 if Has_Aspects (N) then
909 Analyze_Aspect_Specifications (N, Id);
910 end if;
912 -- Ada 2005 (AI-217): Check if the package has been erroneously named
913 -- in a limited-with clause of its own context. In this case the error
914 -- has been previously notified by Analyze_Context.
916 -- limited with Pkg; -- ERROR
917 -- package Pkg is ...
919 if From_Limited_With (Id) then
920 return;
921 end if;
923 Push_Scope (Id);
925 PF := Is_Pure (Enclosing_Lib_Unit_Entity);
926 Set_Is_Pure (Id, PF);
928 Set_Categorization_From_Pragmas (N);
930 Analyze (Specification (N));
931 Validate_Categorization_Dependency (N, Id);
933 Body_Required := Unit_Requires_Body (Id);
935 -- When this spec does not require an explicit body, we know that there
936 -- are no entities requiring completion in the language sense; we call
937 -- Check_Completion here only to ensure that any nested package
938 -- declaration that requires an implicit body gets one. (In the case
939 -- where a body is required, Check_Completion is called at the end of
940 -- the body's declarative part.)
942 if not Body_Required then
943 Check_Completion;
944 end if;
946 Comp_Unit := Nkind (Parent (N)) = N_Compilation_Unit;
947 if Comp_Unit then
949 -- Set Body_Required indication on the compilation unit node, and
950 -- determine whether elaboration warnings may be meaningful on it.
952 Set_Body_Required (Parent (N), Body_Required);
954 if not Body_Required then
955 Set_Suppress_Elaboration_Warnings (Id);
956 end if;
958 end if;
960 End_Package_Scope (Id);
962 -- For the declaration of a library unit that is a remote types package,
963 -- check legality rules regarding availability of stream attributes for
964 -- types that contain non-remote access values. This subprogram performs
965 -- visibility tests that rely on the fact that we have exited the scope
966 -- of Id.
968 if Comp_Unit then
969 Validate_RT_RAT_Component (N);
970 end if;
972 if Debug_Flag_C then
973 Outdent;
974 Write_Str ("<== package spec ");
975 Write_Name (Chars (Id));
976 Write_Str (" from ");
977 Write_Location (Sloc (N));
978 Write_Eol;
979 end if;
980 end Analyze_Package_Declaration;
982 -----------------------------------
983 -- Analyze_Package_Specification --
984 -----------------------------------
986 -- Note that this code is shared for the analysis of generic package specs
987 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
989 procedure Analyze_Package_Specification (N : Node_Id) is
990 Id : constant Entity_Id := Defining_Entity (N);
991 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
992 Vis_Decls : constant List_Id := Visible_Declarations (N);
993 Priv_Decls : constant List_Id := Private_Declarations (N);
994 E : Entity_Id;
995 L : Entity_Id;
996 Public_Child : Boolean;
998 Private_With_Clauses_Installed : Boolean := False;
999 -- In Ada 2005, private with_clauses are visible in the private part
1000 -- of a nested package, even if it appears in the public part of the
1001 -- enclosing package. This requires a separate step to install these
1002 -- private_with_clauses, and remove them at the end of the nested
1003 -- package.
1005 procedure Check_One_Tagged_Type_Or_Extension_At_Most;
1006 -- Issue an error in SPARK mode if a package specification contains
1007 -- more than one tagged type or type extension.
1009 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
1010 -- Clears constant indications (Never_Set_In_Source, Constant_Value, and
1011 -- Is_True_Constant) on all variables that are entities of Id, and on
1012 -- the chain whose first element is FE. A recursive call is made for all
1013 -- packages and generic packages.
1015 procedure Generate_Parent_References;
1016 -- For a child unit, generate references to parent units, for
1017 -- GPS navigation purposes.
1019 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
1020 -- Child and Unit are entities of compilation units. True if Child
1021 -- is a public child of Parent as defined in 10.1.1
1023 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
1024 -- Reject completion of an incomplete or private type declarations
1025 -- having a known discriminant part by an unchecked union.
1027 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
1028 -- Given the package entity of a generic package instantiation or
1029 -- formal package whose corresponding generic is a child unit, installs
1030 -- the private declarations of each of the child unit's parents.
1031 -- This has to be done at the point of entering the instance package's
1032 -- private part rather than being done in Sem_Ch12.Install_Parent
1033 -- (which is where the parents' visible declarations are installed).
1035 ------------------------------------------------
1036 -- Check_One_Tagged_Type_Or_Extension_At_Most --
1037 ------------------------------------------------
1039 procedure Check_One_Tagged_Type_Or_Extension_At_Most is
1040 Previous : Node_Id;
1042 procedure Check_Decls (Decls : List_Id);
1043 -- Check that either Previous is Empty and Decls does not contain
1044 -- more than one tagged type or type extension, or Previous is
1045 -- already set and Decls contains no tagged type or type extension.
1047 -----------------
1048 -- Check_Decls --
1049 -----------------
1051 procedure Check_Decls (Decls : List_Id) is
1052 Decl : Node_Id;
1054 begin
1055 Decl := First (Decls);
1056 while Present (Decl) loop
1057 if Nkind (Decl) = N_Full_Type_Declaration
1058 and then Is_Tagged_Type (Defining_Identifier (Decl))
1059 then
1060 if No (Previous) then
1061 Previous := Decl;
1063 else
1064 Error_Msg_Sloc := Sloc (Previous);
1065 Check_SPARK_Restriction
1066 ("at most one tagged type or type extension allowed",
1067 "\\ previous declaration#",
1068 Decl);
1069 end if;
1070 end if;
1072 Next (Decl);
1073 end loop;
1074 end Check_Decls;
1076 -- Start of processing for Check_One_Tagged_Type_Or_Extension_At_Most
1078 begin
1079 Previous := Empty;
1080 Check_Decls (Vis_Decls);
1082 if Present (Priv_Decls) then
1083 Check_Decls (Priv_Decls);
1084 end if;
1085 end Check_One_Tagged_Type_Or_Extension_At_Most;
1087 ---------------------
1088 -- Clear_Constants --
1089 ---------------------
1091 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
1092 E : Entity_Id;
1094 begin
1095 -- Ignore package renamings, not interesting and they can cause self
1096 -- referential loops in the code below.
1098 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
1099 return;
1100 end if;
1102 -- Note: in the loop below, the check for Next_Entity pointing back
1103 -- to the package entity may seem odd, but it is needed, because a
1104 -- package can contain a renaming declaration to itself, and such
1105 -- renamings are generated automatically within package instances.
1107 E := FE;
1108 while Present (E) and then E /= Id loop
1109 if Is_Assignable (E) then
1110 Set_Never_Set_In_Source (E, False);
1111 Set_Is_True_Constant (E, False);
1112 Set_Current_Value (E, Empty);
1113 Set_Is_Known_Null (E, False);
1114 Set_Last_Assignment (E, Empty);
1116 if not Can_Never_Be_Null (E) then
1117 Set_Is_Known_Non_Null (E, False);
1118 end if;
1120 elsif Is_Package_Or_Generic_Package (E) then
1121 Clear_Constants (E, First_Entity (E));
1122 Clear_Constants (E, First_Private_Entity (E));
1123 end if;
1125 Next_Entity (E);
1126 end loop;
1127 end Clear_Constants;
1129 --------------------------------
1130 -- Generate_Parent_References --
1131 --------------------------------
1133 procedure Generate_Parent_References is
1134 Decl : constant Node_Id := Parent (N);
1136 begin
1137 if Id = Cunit_Entity (Main_Unit)
1138 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
1139 then
1140 Generate_Reference (Id, Scope (Id), 'k', False);
1142 elsif not Nkind_In (Unit (Cunit (Main_Unit)), N_Subprogram_Body,
1143 N_Subunit)
1144 then
1145 -- If current unit is an ancestor of main unit, generate a
1146 -- reference to its own parent.
1148 declare
1149 U : Node_Id;
1150 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
1152 begin
1153 if Nkind (Main_Spec) = N_Package_Body then
1154 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
1155 end if;
1157 U := Parent_Spec (Main_Spec);
1158 while Present (U) loop
1159 if U = Parent (Decl) then
1160 Generate_Reference (Id, Scope (Id), 'k', False);
1161 exit;
1163 elsif Nkind (Unit (U)) = N_Package_Body then
1164 exit;
1166 else
1167 U := Parent_Spec (Unit (U));
1168 end if;
1169 end loop;
1170 end;
1171 end if;
1172 end Generate_Parent_References;
1174 ---------------------
1175 -- Is_Public_Child --
1176 ---------------------
1178 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
1179 begin
1180 if not Is_Private_Descendant (Child) then
1181 return True;
1182 else
1183 if Child = Unit then
1184 return not Private_Present (
1185 Parent (Unit_Declaration_Node (Child)));
1186 else
1187 return Is_Public_Child (Scope (Child), Unit);
1188 end if;
1189 end if;
1190 end Is_Public_Child;
1192 ----------------------------------------
1193 -- Inspect_Unchecked_Union_Completion --
1194 ----------------------------------------
1196 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
1197 Decl : Node_Id;
1199 begin
1200 Decl := First (Decls);
1201 while Present (Decl) loop
1203 -- We are looking at an incomplete or private type declaration
1204 -- with a known_discriminant_part whose full view is an
1205 -- Unchecked_Union.
1207 if Nkind_In (Decl, N_Incomplete_Type_Declaration,
1208 N_Private_Type_Declaration)
1209 and then Has_Discriminants (Defining_Identifier (Decl))
1210 and then Present (Full_View (Defining_Identifier (Decl)))
1211 and then
1212 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
1213 then
1214 Error_Msg_N
1215 ("completion of discriminated partial view "
1216 & "cannot be an unchecked union",
1217 Full_View (Defining_Identifier (Decl)));
1218 end if;
1220 Next (Decl);
1221 end loop;
1222 end Inspect_Unchecked_Union_Completion;
1224 -----------------------------------------
1225 -- Install_Parent_Private_Declarations --
1226 -----------------------------------------
1228 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
1229 Inst_Par : Entity_Id;
1230 Gen_Par : Entity_Id;
1231 Inst_Node : Node_Id;
1233 begin
1234 Inst_Par := Inst_Id;
1236 Gen_Par :=
1237 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
1238 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
1239 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
1241 if Nkind_In (Inst_Node, N_Package_Instantiation,
1242 N_Formal_Package_Declaration)
1243 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
1244 then
1245 Inst_Par := Entity (Prefix (Name (Inst_Node)));
1247 if Present (Renamed_Entity (Inst_Par)) then
1248 Inst_Par := Renamed_Entity (Inst_Par);
1249 end if;
1251 Gen_Par :=
1252 Generic_Parent
1253 (Specification (Unit_Declaration_Node (Inst_Par)));
1255 -- Install the private declarations and private use clauses
1256 -- of a parent instance of the child instance, unless the
1257 -- parent instance private declarations have already been
1258 -- installed earlier in Analyze_Package_Specification, which
1259 -- happens when a generic child is instantiated, and the
1260 -- instance is a child of the parent instance.
1262 -- Installing the use clauses of the parent instance twice
1263 -- is both unnecessary and wrong, because it would cause the
1264 -- clauses to be chained to themselves in the use clauses
1265 -- list of the scope stack entry. That in turn would cause
1266 -- an endless loop from End_Use_Clauses upon scope exit.
1268 -- The parent is now fully visible. It may be a hidden open
1269 -- scope if we are currently compiling some child instance
1270 -- declared within it, but while the current instance is being
1271 -- compiled the parent is immediately visible. In particular
1272 -- its entities must remain visible if a stack save/restore
1273 -- takes place through a call to Rtsfind.
1275 if Present (Gen_Par) then
1276 if not In_Private_Part (Inst_Par) then
1277 Install_Private_Declarations (Inst_Par);
1278 Set_Use (Private_Declarations
1279 (Specification
1280 (Unit_Declaration_Node (Inst_Par))));
1281 Set_Is_Hidden_Open_Scope (Inst_Par, False);
1282 end if;
1284 -- If we've reached the end of the generic instance parents,
1285 -- then finish off by looping through the nongeneric parents
1286 -- and installing their private declarations.
1288 -- If one of the non-generic parents is itself on the scope
1289 -- stack, do not install its private declarations: they are
1290 -- installed in due time when the private part of that parent
1291 -- is analyzed. This is delicate ???
1293 else
1294 while Present (Inst_Par)
1295 and then Inst_Par /= Standard_Standard
1296 and then (not In_Open_Scopes (Inst_Par)
1297 or else not In_Private_Part (Inst_Par))
1298 loop
1299 Install_Private_Declarations (Inst_Par);
1300 Set_Use (Private_Declarations
1301 (Specification
1302 (Unit_Declaration_Node (Inst_Par))));
1303 Inst_Par := Scope (Inst_Par);
1304 end loop;
1306 exit;
1307 end if;
1309 else
1310 exit;
1311 end if;
1312 end loop;
1313 end Install_Parent_Private_Declarations;
1315 -- Start of processing for Analyze_Package_Specification
1317 begin
1318 if Present (Vis_Decls) then
1319 Analyze_Declarations (Vis_Decls);
1320 end if;
1322 -- Verify that incomplete types have received full declarations and
1323 -- also build invariant procedures for any types with invariants.
1325 E := First_Entity (Id);
1326 while Present (E) loop
1328 -- Check on incomplete types
1330 -- AI05-0213: A formal incomplete type has no completion
1332 if Ekind (E) = E_Incomplete_Type
1333 and then No (Full_View (E))
1334 and then not Is_Generic_Type (E)
1335 then
1336 Error_Msg_N ("no declaration in visible part for incomplete}", E);
1337 end if;
1339 -- Build invariant procedures
1341 if Is_Type (E) and then Has_Invariants (E) then
1342 Build_Invariant_Procedure (E, N);
1343 end if;
1345 Next_Entity (E);
1346 end loop;
1348 if Is_Remote_Call_Interface (Id)
1349 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1350 then
1351 Validate_RCI_Declarations (Id);
1352 end if;
1354 -- Save global references in the visible declarations, before installing
1355 -- private declarations of parent unit if there is one, because the
1356 -- privacy status of types defined in the parent will change. This is
1357 -- only relevant for generic child units, but is done in all cases for
1358 -- uniformity.
1360 if Ekind (Id) = E_Generic_Package
1361 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1362 then
1363 declare
1364 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1365 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1366 begin
1367 Set_Private_Declarations (Orig_Spec, Empty_List);
1368 Save_Global_References (Orig_Decl);
1369 Set_Private_Declarations (Orig_Spec, Save_Priv);
1370 end;
1371 end if;
1373 -- If package is a public child unit, then make the private declarations
1374 -- of the parent visible.
1376 Public_Child := False;
1378 declare
1379 Par : Entity_Id;
1380 Pack_Decl : Node_Id;
1381 Par_Spec : Node_Id;
1383 begin
1384 Par := Id;
1385 Par_Spec := Parent_Spec (Parent (N));
1387 -- If the package is formal package of an enclosing generic, it is
1388 -- transformed into a local generic declaration, and compiled to make
1389 -- its spec available. We need to retrieve the original generic to
1390 -- determine whether it is a child unit, and install its parents.
1392 if No (Par_Spec)
1393 and then
1394 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1395 then
1396 Par := Entity (Name (Original_Node (Parent (N))));
1397 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1398 end if;
1400 if Present (Par_Spec) then
1401 Generate_Parent_References;
1403 while Scope (Par) /= Standard_Standard
1404 and then Is_Public_Child (Id, Par)
1405 and then In_Open_Scopes (Par)
1406 loop
1407 Public_Child := True;
1408 Par := Scope (Par);
1409 Install_Private_Declarations (Par);
1410 Install_Private_With_Clauses (Par);
1411 Pack_Decl := Unit_Declaration_Node (Par);
1412 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1413 end loop;
1414 end if;
1415 end;
1417 if Is_Compilation_Unit (Id) then
1418 Install_Private_With_Clauses (Id);
1419 else
1421 -- The current compilation unit may include private with_clauses,
1422 -- which are visible in the private part of the current nested
1423 -- package, and have to be installed now. This is not done for
1424 -- nested instantiations, where the private with_clauses of the
1425 -- enclosing unit have no effect once the instantiation info is
1426 -- established and we start analyzing the package declaration.
1428 declare
1429 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1430 begin
1431 if Is_Package_Or_Generic_Package (Comp_Unit)
1432 and then not In_Private_Part (Comp_Unit)
1433 and then not In_Instance
1434 then
1435 Install_Private_With_Clauses (Comp_Unit);
1436 Private_With_Clauses_Installed := True;
1437 end if;
1438 end;
1439 end if;
1441 -- If this is a package associated with a generic instance or formal
1442 -- package, then the private declarations of each of the generic's
1443 -- parents must be installed at this point.
1445 if Is_Generic_Instance (Id) then
1446 Install_Parent_Private_Declarations (Id);
1447 end if;
1449 -- Analyze private part if present. The flag In_Private_Part is reset
1450 -- in End_Package_Scope.
1452 L := Last_Entity (Id);
1454 if Present (Priv_Decls) then
1455 Set_In_Private_Part (Id);
1457 -- Upon entering a public child's private part, it may be necessary
1458 -- to declare subprograms that were derived in the package's visible
1459 -- part but not yet made visible.
1461 if Public_Child then
1462 Declare_Inherited_Private_Subprograms (Id);
1463 end if;
1465 Analyze_Declarations (Priv_Decls);
1467 -- Check the private declarations for incomplete deferred constants
1469 Inspect_Deferred_Constant_Completion (Priv_Decls);
1471 -- The first private entity is the immediate follower of the last
1472 -- visible entity, if there was one.
1474 if Present (L) then
1475 Set_First_Private_Entity (Id, Next_Entity (L));
1476 else
1477 Set_First_Private_Entity (Id, First_Entity (Id));
1478 end if;
1480 -- There may be inherited private subprograms that need to be declared,
1481 -- even in the absence of an explicit private part. If there are any
1482 -- public declarations in the package and the package is a public child
1483 -- unit, then an implicit private part is assumed.
1485 elsif Present (L) and then Public_Child then
1486 Set_In_Private_Part (Id);
1487 Declare_Inherited_Private_Subprograms (Id);
1488 Set_First_Private_Entity (Id, Next_Entity (L));
1489 end if;
1491 E := First_Entity (Id);
1492 while Present (E) loop
1494 -- Check rule of 3.6(11), which in general requires waiting till all
1495 -- full types have been seen.
1497 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1498 Check_Aliased_Component_Types (E);
1499 end if;
1501 -- Check preelaborable initialization for full type completing a
1502 -- private type for which pragma Preelaborable_Initialization given.
1504 if Is_Type (E)
1505 and then Must_Have_Preelab_Init (E)
1506 and then not Has_Preelaborable_Initialization (E)
1507 then
1508 Error_Msg_N
1509 ("full view of & does not have preelaborable initialization", E);
1510 end if;
1512 -- An invariant may appear on a full view of a type
1514 if Is_Type (E)
1515 and then Has_Private_Declaration (E)
1516 and then Nkind (Parent (E)) = N_Full_Type_Declaration
1517 and then Has_Aspects (Parent (E))
1518 then
1519 declare
1520 ASN : Node_Id;
1522 begin
1523 ASN := First (Aspect_Specifications (Parent (E)));
1524 while Present (ASN) loop
1525 if Nam_In (Chars (Identifier (ASN)), Name_Invariant,
1526 Name_Type_Invariant)
1527 then
1528 Build_Invariant_Procedure (E, N);
1529 exit;
1530 end if;
1532 Next (ASN);
1533 end loop;
1534 end;
1535 end if;
1537 Next_Entity (E);
1538 end loop;
1540 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1541 -- declaration having a known_discriminant_part shall not be an
1542 -- unchecked union type.
1544 if Present (Vis_Decls) then
1545 Inspect_Unchecked_Union_Completion (Vis_Decls);
1546 end if;
1548 if Present (Priv_Decls) then
1549 Inspect_Unchecked_Union_Completion (Priv_Decls);
1550 end if;
1552 if Ekind (Id) = E_Generic_Package
1553 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1554 and then Present (Priv_Decls)
1555 then
1556 -- Save global references in private declarations, ignoring the
1557 -- visible declarations that were processed earlier.
1559 declare
1560 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1561 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1562 Save_Form : constant List_Id :=
1563 Generic_Formal_Declarations (Orig_Decl);
1565 begin
1566 Set_Visible_Declarations (Orig_Spec, Empty_List);
1567 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1568 Save_Global_References (Orig_Decl);
1569 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1570 Set_Visible_Declarations (Orig_Spec, Save_Vis);
1571 end;
1572 end if;
1574 Process_End_Label (N, 'e', Id);
1576 -- Remove private_with_clauses of enclosing compilation unit, if they
1577 -- were installed.
1579 if Private_With_Clauses_Installed then
1580 Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
1581 end if;
1583 -- For the case of a library level package, we must go through all the
1584 -- entities clearing the indications that the value may be constant and
1585 -- not modified. Why? Because any client of this package may modify
1586 -- these values freely from anywhere. This also applies to any nested
1587 -- packages or generic packages.
1589 -- For now we unconditionally clear constants for packages that are
1590 -- instances of generic packages. The reason is that we do not have the
1591 -- body yet, and we otherwise think things are unreferenced when they
1592 -- are not. This should be fixed sometime (the effect is not terrible,
1593 -- we just lose some warnings, and also some cases of value propagation)
1594 -- ???
1596 if Is_Library_Level_Entity (Id)
1597 or else Is_Generic_Instance (Id)
1598 then
1599 Clear_Constants (Id, First_Entity (Id));
1600 Clear_Constants (Id, First_Private_Entity (Id));
1601 end if;
1603 -- Issue an error in SPARK mode if a package specification contains
1604 -- more than one tagged type or type extension.
1606 Check_One_Tagged_Type_Or_Extension_At_Most;
1608 -- If switch set, output information on why body required
1610 if List_Body_Required_Info
1611 and then In_Extended_Main_Source_Unit (Id)
1612 and then Unit_Requires_Body (Id)
1613 then
1614 Unit_Requires_Body_Info (Id);
1615 end if;
1616 end Analyze_Package_Specification;
1618 --------------------------------------
1619 -- Analyze_Private_Type_Declaration --
1620 --------------------------------------
1622 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1623 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
1624 Id : constant Entity_Id := Defining_Identifier (N);
1626 begin
1627 Generate_Definition (Id);
1628 Set_Is_Pure (Id, PF);
1629 Init_Size_Align (Id);
1631 if not Is_Package_Or_Generic_Package (Current_Scope)
1632 or else In_Private_Part (Current_Scope)
1633 then
1634 Error_Msg_N ("invalid context for private declaration", N);
1635 end if;
1637 New_Private_Type (N, Id, N);
1638 Set_Depends_On_Private (Id);
1640 if Has_Aspects (N) then
1641 Analyze_Aspect_Specifications (N, Id);
1642 end if;
1643 end Analyze_Private_Type_Declaration;
1645 ----------------------------------
1646 -- Check_Anonymous_Access_Types --
1647 ----------------------------------
1649 procedure Check_Anonymous_Access_Types
1650 (Spec_Id : Entity_Id;
1651 P_Body : Node_Id)
1653 E : Entity_Id;
1654 IR : Node_Id;
1656 begin
1657 -- Itype references are only needed by gigi, to force elaboration of
1658 -- itypes. In the absence of code generation, they are not needed.
1660 if not Expander_Active then
1661 return;
1662 end if;
1664 E := First_Entity (Spec_Id);
1665 while Present (E) loop
1666 if Ekind (E) = E_Anonymous_Access_Type
1667 and then From_Limited_With (E)
1668 then
1669 IR := Make_Itype_Reference (Sloc (P_Body));
1670 Set_Itype (IR, E);
1672 if No (Declarations (P_Body)) then
1673 Set_Declarations (P_Body, New_List (IR));
1674 else
1675 Prepend (IR, Declarations (P_Body));
1676 end if;
1677 end if;
1679 Next_Entity (E);
1680 end loop;
1681 end Check_Anonymous_Access_Types;
1683 -------------------------------------------
1684 -- Declare_Inherited_Private_Subprograms --
1685 -------------------------------------------
1687 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1689 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1690 -- Check whether an inherited subprogram S is an operation of an
1691 -- untagged derived type T.
1693 ---------------------
1694 -- Is_Primitive_Of --
1695 ---------------------
1697 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1698 Formal : Entity_Id;
1700 begin
1701 -- If the full view is a scalar type, the type is the anonymous base
1702 -- type, but the operation mentions the first subtype, so check the
1703 -- signature against the base type.
1705 if Base_Type (Etype (S)) = Base_Type (T) then
1706 return True;
1708 else
1709 Formal := First_Formal (S);
1710 while Present (Formal) loop
1711 if Base_Type (Etype (Formal)) = Base_Type (T) then
1712 return True;
1713 end if;
1715 Next_Formal (Formal);
1716 end loop;
1718 return False;
1719 end if;
1720 end Is_Primitive_Of;
1722 -- Local variables
1724 E : Entity_Id;
1725 Op_List : Elist_Id;
1726 Op_Elmt : Elmt_Id;
1727 Op_Elmt_2 : Elmt_Id;
1728 Prim_Op : Entity_Id;
1729 New_Op : Entity_Id := Empty;
1730 Parent_Subp : Entity_Id;
1731 Tag : Entity_Id;
1733 -- Start of processing for Declare_Inherited_Private_Subprograms
1735 begin
1736 E := First_Entity (Id);
1737 while Present (E) loop
1739 -- If the entity is a nonprivate type extension whose parent type
1740 -- is declared in an open scope, then the type may have inherited
1741 -- operations that now need to be made visible. Ditto if the entity
1742 -- is a formal derived type in a child unit.
1744 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
1745 or else
1746 (Nkind (Parent (E)) = N_Private_Extension_Declaration
1747 and then Is_Generic_Type (E)))
1748 and then In_Open_Scopes (Scope (Etype (E)))
1749 and then Is_Base_Type (E)
1750 then
1751 if Is_Tagged_Type (E) then
1752 Op_List := Primitive_Operations (E);
1753 New_Op := Empty;
1754 Tag := First_Tag_Component (E);
1756 Op_Elmt := First_Elmt (Op_List);
1757 while Present (Op_Elmt) loop
1758 Prim_Op := Node (Op_Elmt);
1760 -- Search primitives that are implicit operations with an
1761 -- internal name whose parent operation has a normal name.
1763 if Present (Alias (Prim_Op))
1764 and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
1765 and then not Comes_From_Source (Prim_Op)
1766 and then Is_Internal_Name (Chars (Prim_Op))
1767 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1768 then
1769 Parent_Subp := Alias (Prim_Op);
1771 -- Case 1: Check if the type has also an explicit
1772 -- overriding for this primitive.
1774 Op_Elmt_2 := Next_Elmt (Op_Elmt);
1775 while Present (Op_Elmt_2) loop
1777 -- Skip entities with attribute Interface_Alias since
1778 -- they are not overriding primitives (these entities
1779 -- link an interface primitive with their covering
1780 -- primitive)
1782 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
1783 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
1784 and then No (Interface_Alias (Node (Op_Elmt_2)))
1785 then
1786 -- The private inherited operation has been
1787 -- overridden by an explicit subprogram:
1788 -- replace the former by the latter.
1790 New_Op := Node (Op_Elmt_2);
1791 Replace_Elmt (Op_Elmt, New_Op);
1792 Remove_Elmt (Op_List, Op_Elmt_2);
1793 Set_Overridden_Operation (New_Op, Parent_Subp);
1795 -- We don't need to inherit its dispatching slot.
1796 -- Set_All_DT_Position has previously ensured that
1797 -- the same slot was assigned to the two primitives
1799 if Present (Tag)
1800 and then Present (DTC_Entity (New_Op))
1801 and then Present (DTC_Entity (Prim_Op))
1802 then
1803 pragma Assert
1804 (DT_Position (New_Op) = DT_Position (Prim_Op));
1805 null;
1806 end if;
1808 goto Next_Primitive;
1809 end if;
1811 Next_Elmt (Op_Elmt_2);
1812 end loop;
1814 -- Case 2: We have not found any explicit overriding and
1815 -- hence we need to declare the operation (i.e., make it
1816 -- visible).
1818 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1820 -- Inherit the dispatching slot if E is already frozen
1822 if Is_Frozen (E)
1823 and then Present (DTC_Entity (Alias (Prim_Op)))
1824 then
1825 Set_DTC_Entity_Value (E, New_Op);
1826 Set_DT_Position (New_Op,
1827 DT_Position (Alias (Prim_Op)));
1828 end if;
1830 pragma Assert
1831 (Is_Dispatching_Operation (New_Op)
1832 and then Node (Last_Elmt (Op_List)) = New_Op);
1834 -- Substitute the new operation for the old one in the
1835 -- type's primitive operations list. Since the new
1836 -- operation was also just added to the end of list,
1837 -- the last element must be removed.
1839 -- (Question: is there a simpler way of declaring the
1840 -- operation, say by just replacing the name of the
1841 -- earlier operation, reentering it in the in the symbol
1842 -- table (how?), and marking it as private???)
1844 Replace_Elmt (Op_Elmt, New_Op);
1845 Remove_Last_Elmt (Op_List);
1846 end if;
1848 <<Next_Primitive>>
1849 Next_Elmt (Op_Elmt);
1850 end loop;
1852 -- Generate listing showing the contents of the dispatch table
1854 if Debug_Flag_ZZ then
1855 Write_DT (E);
1856 end if;
1858 else
1859 -- Non-tagged type, scan forward to locate inherited hidden
1860 -- operations.
1862 Prim_Op := Next_Entity (E);
1863 while Present (Prim_Op) loop
1864 if Is_Subprogram (Prim_Op)
1865 and then Present (Alias (Prim_Op))
1866 and then not Comes_From_Source (Prim_Op)
1867 and then Is_Internal_Name (Chars (Prim_Op))
1868 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1869 and then Is_Primitive_Of (E, Prim_Op)
1870 then
1871 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1872 end if;
1874 Next_Entity (Prim_Op);
1876 -- Derived operations appear immediately after the type
1877 -- declaration (or the following subtype indication for
1878 -- a derived scalar type). Further declarations cannot
1879 -- include inherited operations of the type.
1881 if Present (Prim_Op) then
1882 exit when Ekind (Prim_Op) not in Overloadable_Kind;
1883 end if;
1884 end loop;
1885 end if;
1886 end if;
1888 Next_Entity (E);
1889 end loop;
1890 end Declare_Inherited_Private_Subprograms;
1892 -----------------------
1893 -- End_Package_Scope --
1894 -----------------------
1896 procedure End_Package_Scope (P : Entity_Id) is
1897 begin
1898 Uninstall_Declarations (P);
1899 Pop_Scope;
1900 end End_Package_Scope;
1902 ---------------------------
1903 -- Exchange_Declarations --
1904 ---------------------------
1906 procedure Exchange_Declarations (Id : Entity_Id) is
1907 Full_Id : constant Entity_Id := Full_View (Id);
1908 H1 : constant Entity_Id := Homonym (Id);
1909 Next1 : constant Entity_Id := Next_Entity (Id);
1910 H2 : Entity_Id;
1911 Next2 : Entity_Id;
1913 begin
1914 -- If missing full declaration for type, nothing to exchange
1916 if No (Full_Id) then
1917 return;
1918 end if;
1920 -- Otherwise complete the exchange, and preserve semantic links
1922 Next2 := Next_Entity (Full_Id);
1923 H2 := Homonym (Full_Id);
1925 -- Reset full declaration pointer to reflect the switched entities and
1926 -- readjust the next entity chains.
1928 Exchange_Entities (Id, Full_Id);
1930 Set_Next_Entity (Id, Next1);
1931 Set_Homonym (Id, H1);
1933 Set_Full_View (Full_Id, Id);
1934 Set_Next_Entity (Full_Id, Next2);
1935 Set_Homonym (Full_Id, H2);
1936 end Exchange_Declarations;
1938 ----------------------------
1939 -- Install_Package_Entity --
1940 ----------------------------
1942 procedure Install_Package_Entity (Id : Entity_Id) is
1943 begin
1944 if not Is_Internal (Id) then
1945 if Debug_Flag_E then
1946 Write_Str ("Install: ");
1947 Write_Name (Chars (Id));
1948 Write_Eol;
1949 end if;
1951 if not Is_Child_Unit (Id) then
1952 Set_Is_Immediately_Visible (Id);
1953 end if;
1955 end if;
1956 end Install_Package_Entity;
1958 ----------------------------------
1959 -- Install_Private_Declarations --
1960 ----------------------------------
1962 procedure Install_Private_Declarations (P : Entity_Id) is
1963 Id : Entity_Id;
1964 Full : Entity_Id;
1965 Priv_Deps : Elist_Id;
1967 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
1968 -- When the full view of a private type is made available, we do the
1969 -- same for its private dependents under proper visibility conditions.
1970 -- When compiling a grand-chid unit this needs to be done recursively.
1972 -----------------------------
1973 -- Swap_Private_Dependents --
1974 -----------------------------
1976 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
1977 Deps : Elist_Id;
1978 Priv : Entity_Id;
1979 Priv_Elmt : Elmt_Id;
1980 Is_Priv : Boolean;
1982 begin
1983 Priv_Elmt := First_Elmt (Priv_Deps);
1984 while Present (Priv_Elmt) loop
1985 Priv := Node (Priv_Elmt);
1987 -- Before the exchange, verify that the presence of the Full_View
1988 -- field. This field will be empty if the entity has already been
1989 -- installed due to a previous call.
1991 if Present (Full_View (Priv))
1992 and then Is_Visible_Dependent (Priv)
1993 then
1994 if Is_Private_Type (Priv) then
1995 Deps := Private_Dependents (Priv);
1996 Is_Priv := True;
1997 else
1998 Is_Priv := False;
1999 end if;
2001 -- For each subtype that is swapped, we also swap the reference
2002 -- to it in Private_Dependents, to allow access to it when we
2003 -- swap them out in End_Package_Scope.
2005 Replace_Elmt (Priv_Elmt, Full_View (Priv));
2006 Exchange_Declarations (Priv);
2007 Set_Is_Immediately_Visible
2008 (Priv, In_Open_Scopes (Scope (Priv)));
2009 Set_Is_Potentially_Use_Visible
2010 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
2012 -- Within a child unit, recurse, except in generic child unit,
2013 -- which (unfortunately) handle private_dependents separately.
2015 if Is_Priv
2016 and then Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
2017 and then not Is_Empty_Elmt_List (Deps)
2018 and then not Inside_A_Generic
2019 then
2020 Swap_Private_Dependents (Deps);
2021 end if;
2022 end if;
2024 Next_Elmt (Priv_Elmt);
2025 end loop;
2026 end Swap_Private_Dependents;
2028 -- Start of processing for Install_Private_Declarations
2030 begin
2031 -- First exchange declarations for private types, so that the full
2032 -- declaration is visible. For each private type, we check its
2033 -- Private_Dependents list and also exchange any subtypes of or derived
2034 -- types from it. Finally, if this is a Taft amendment type, the
2035 -- incomplete declaration is irrelevant, and we want to link the
2036 -- eventual full declaration with the original private one so we
2037 -- also skip the exchange.
2039 Id := First_Entity (P);
2040 while Present (Id) and then Id /= First_Private_Entity (P) loop
2041 if Is_Private_Base_Type (Id)
2042 and then Comes_From_Source (Full_View (Id))
2043 and then Present (Full_View (Id))
2044 and then Scope (Full_View (Id)) = Scope (Id)
2045 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
2046 then
2047 -- If there is a use-type clause on the private type, set the full
2048 -- view accordingly.
2050 Set_In_Use (Full_View (Id), In_Use (Id));
2051 Full := Full_View (Id);
2053 if Is_Private_Base_Type (Full)
2054 and then Has_Private_Declaration (Full)
2055 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
2056 and then In_Open_Scopes (Scope (Etype (Full)))
2057 and then In_Package_Body (Current_Scope)
2058 and then not Is_Private_Type (Etype (Full))
2059 then
2060 -- This is the completion of a private type by a derivation
2061 -- from another private type which is not private anymore. This
2062 -- can only happen in a package nested within a child package,
2063 -- when the parent type is defined in the parent unit. At this
2064 -- point the current type is not private either, and we have
2065 -- to install the underlying full view, which is now visible.
2066 -- Save the current full view as well, so that all views can be
2067 -- restored on exit. It may seem that after compiling the child
2068 -- body there are not environments to restore, but the back-end
2069 -- expects those links to be valid, and freeze nodes depend on
2070 -- them.
2072 if No (Full_View (Full))
2073 and then Present (Underlying_Full_View (Full))
2074 then
2075 Set_Full_View (Id, Underlying_Full_View (Full));
2076 Set_Underlying_Full_View (Id, Full);
2078 Set_Underlying_Full_View (Full, Empty);
2079 Set_Is_Frozen (Full_View (Id));
2080 end if;
2081 end if;
2083 Priv_Deps := Private_Dependents (Id);
2084 Exchange_Declarations (Id);
2085 Set_Is_Immediately_Visible (Id);
2086 Swap_Private_Dependents (Priv_Deps);
2087 end if;
2089 Next_Entity (Id);
2090 end loop;
2092 -- Next make other declarations in the private part visible as well
2094 Id := First_Private_Entity (P);
2095 while Present (Id) loop
2096 Install_Package_Entity (Id);
2097 Set_Is_Hidden (Id, False);
2098 Next_Entity (Id);
2099 end loop;
2101 -- Indicate that the private part is currently visible, so it can be
2102 -- properly reset on exit.
2104 Set_In_Private_Part (P);
2105 end Install_Private_Declarations;
2107 ----------------------------------
2108 -- Install_Visible_Declarations --
2109 ----------------------------------
2111 procedure Install_Visible_Declarations (P : Entity_Id) is
2112 Id : Entity_Id;
2113 Last_Entity : Entity_Id;
2115 begin
2116 pragma Assert
2117 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
2119 if Is_Package_Or_Generic_Package (P) then
2120 Last_Entity := First_Private_Entity (P);
2121 else
2122 Last_Entity := Empty;
2123 end if;
2125 Id := First_Entity (P);
2126 while Present (Id) and then Id /= Last_Entity loop
2127 Install_Package_Entity (Id);
2128 Next_Entity (Id);
2129 end loop;
2130 end Install_Visible_Declarations;
2132 --------------------------
2133 -- Is_Private_Base_Type --
2134 --------------------------
2136 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
2137 begin
2138 return Ekind (E) = E_Private_Type
2139 or else Ekind (E) = E_Limited_Private_Type
2140 or else Ekind (E) = E_Record_Type_With_Private;
2141 end Is_Private_Base_Type;
2143 --------------------------
2144 -- Is_Visible_Dependent --
2145 --------------------------
2147 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
2149 S : constant Entity_Id := Scope (Dep);
2151 begin
2152 -- Renamings created for actual types have the visibility of the actual
2154 if Ekind (S) = E_Package
2155 and then Is_Generic_Instance (S)
2156 and then (Is_Generic_Actual_Type (Dep)
2157 or else Is_Generic_Actual_Type (Full_View (Dep)))
2158 then
2159 return True;
2161 elsif not (Is_Derived_Type (Dep))
2162 and then Is_Derived_Type (Full_View (Dep))
2163 then
2164 -- When instantiating a package body, the scope stack is empty, so
2165 -- check instead whether the dependent type is defined in the same
2166 -- scope as the instance itself.
2168 return In_Open_Scopes (S)
2169 or else (Is_Generic_Instance (Current_Scope)
2170 and then Scope (Dep) = Scope (Current_Scope));
2171 else
2172 return True;
2173 end if;
2174 end Is_Visible_Dependent;
2176 ----------------------------
2177 -- May_Need_Implicit_Body --
2178 ----------------------------
2180 procedure May_Need_Implicit_Body (E : Entity_Id) is
2181 P : constant Node_Id := Unit_Declaration_Node (E);
2182 S : constant Node_Id := Parent (P);
2183 B : Node_Id;
2184 Decls : List_Id;
2186 begin
2187 if not Has_Completion (E)
2188 and then Nkind (P) = N_Package_Declaration
2189 and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
2190 then
2191 B :=
2192 Make_Package_Body (Sloc (E),
2193 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
2194 Chars => Chars (E)),
2195 Declarations => New_List);
2197 if Nkind (S) = N_Package_Specification then
2198 if Present (Private_Declarations (S)) then
2199 Decls := Private_Declarations (S);
2200 else
2201 Decls := Visible_Declarations (S);
2202 end if;
2203 else
2204 Decls := Declarations (S);
2205 end if;
2207 Append (B, Decls);
2208 Analyze (B);
2209 end if;
2210 end May_Need_Implicit_Body;
2212 ----------------------
2213 -- New_Private_Type --
2214 ----------------------
2216 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
2217 begin
2218 -- For other than Ada 2012, enter the name in the current scope
2220 if Ada_Version < Ada_2012 then
2221 Enter_Name (Id);
2223 -- Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2224 -- there may be an incomplete previous view.
2226 else
2227 declare
2228 Prev : Entity_Id;
2229 begin
2230 Prev := Find_Type_Name (N);
2231 pragma Assert (Prev = Id
2232 or else (Ekind (Prev) = E_Incomplete_Type
2233 and then Present (Full_View (Prev))
2234 and then Full_View (Prev) = Id));
2235 end;
2236 end if;
2238 if Limited_Present (Def) then
2239 Set_Ekind (Id, E_Limited_Private_Type);
2240 else
2241 Set_Ekind (Id, E_Private_Type);
2242 end if;
2244 Set_Etype (Id, Id);
2245 Set_Has_Delayed_Freeze (Id);
2246 Set_Is_First_Subtype (Id);
2247 Init_Size_Align (Id);
2249 Set_Is_Constrained (Id,
2250 No (Discriminant_Specifications (N))
2251 and then not Unknown_Discriminants_Present (N));
2253 -- Set tagged flag before processing discriminants, to catch illegal
2254 -- usage.
2256 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
2258 Set_Discriminant_Constraint (Id, No_Elist);
2259 Set_Stored_Constraint (Id, No_Elist);
2261 if Present (Discriminant_Specifications (N)) then
2262 Push_Scope (Id);
2263 Process_Discriminants (N);
2264 End_Scope;
2266 elsif Unknown_Discriminants_Present (N) then
2267 Set_Has_Unknown_Discriminants (Id);
2268 end if;
2270 Set_Private_Dependents (Id, New_Elmt_List);
2272 if Tagged_Present (Def) then
2273 Set_Ekind (Id, E_Record_Type_With_Private);
2274 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2275 Set_Is_Abstract_Type (Id, Abstract_Present (Def));
2276 Set_Is_Limited_Record (Id, Limited_Present (Def));
2277 Set_Has_Delayed_Freeze (Id, True);
2279 -- Create a class-wide type with the same attributes
2281 Make_Class_Wide_Type (Id);
2283 elsif Abstract_Present (Def) then
2284 Error_Msg_N ("only a tagged type can be abstract", N);
2285 end if;
2286 end New_Private_Type;
2288 ----------------------------
2289 -- Uninstall_Declarations --
2290 ----------------------------
2292 procedure Uninstall_Declarations (P : Entity_Id) is
2293 Decl : constant Node_Id := Unit_Declaration_Node (P);
2294 Id : Entity_Id;
2295 Full : Entity_Id;
2296 Priv_Elmt : Elmt_Id;
2297 Priv_Sub : Entity_Id;
2299 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id);
2300 -- Copy to the private declaration the attributes of the full view that
2301 -- need to be available for the partial view also.
2303 function Type_In_Use (T : Entity_Id) return Boolean;
2304 -- Check whether type or base type appear in an active use_type clause
2306 ------------------------------
2307 -- Preserve_Full_Attributes --
2308 ------------------------------
2310 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id) is
2311 Priv_Is_Base_Type : constant Boolean := Is_Base_Type (Priv);
2313 begin
2314 Set_Size_Info (Priv, (Full));
2315 Set_RM_Size (Priv, RM_Size (Full));
2316 Set_Size_Known_At_Compile_Time
2317 (Priv, Size_Known_At_Compile_Time (Full));
2318 Set_Is_Volatile (Priv, Is_Volatile (Full));
2319 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
2320 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full));
2321 Set_Is_Ada_2012_Only (Priv, Is_Ada_2012_Only (Full));
2322 Set_Has_Pragma_Unmodified (Priv, Has_Pragma_Unmodified (Full));
2323 Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced (Full));
2324 Set_Has_Pragma_Unreferenced_Objects
2325 (Priv, Has_Pragma_Unreferenced_Objects
2326 (Full));
2327 if Is_Unchecked_Union (Full) then
2328 Set_Is_Unchecked_Union (Base_Type (Priv));
2329 end if;
2330 -- Why is atomic not copied here ???
2332 if Referenced (Full) then
2333 Set_Referenced (Priv);
2334 end if;
2336 if Priv_Is_Base_Type then
2337 Set_Is_Controlled (Priv, Is_Controlled (Base_Type (Full)));
2338 Set_Finalize_Storage_Only (Priv, Finalize_Storage_Only
2339 (Base_Type (Full)));
2340 Set_Has_Task (Priv, Has_Task (Base_Type (Full)));
2341 Set_Has_Controlled_Component (Priv, Has_Controlled_Component
2342 (Base_Type (Full)));
2343 end if;
2345 Set_Freeze_Node (Priv, Freeze_Node (Full));
2347 -- Propagate information of type invariants, which may be specified
2348 -- for the full view.
2350 if Has_Invariants (Full) and not Has_Invariants (Priv) then
2351 Set_Has_Invariants (Priv);
2352 Set_Subprograms_For_Type (Priv, Subprograms_For_Type (Full));
2353 end if;
2355 if Is_Tagged_Type (Priv)
2356 and then Is_Tagged_Type (Full)
2357 and then not Error_Posted (Full)
2358 then
2359 if Is_Tagged_Type (Priv) then
2361 -- If the type is tagged, the tag itself must be available on
2362 -- the partial view, for expansion purposes.
2364 Set_First_Entity (Priv, First_Entity (Full));
2366 -- If there are discriminants in the partial view, these remain
2367 -- visible. Otherwise only the tag itself is visible, and there
2368 -- are no nameable components in the partial view.
2370 if No (Last_Entity (Priv)) then
2371 Set_Last_Entity (Priv, First_Entity (Priv));
2372 end if;
2373 end if;
2375 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2377 if Has_Discriminants (Full) then
2378 Set_Discriminant_Constraint (Priv,
2379 Discriminant_Constraint (Full));
2380 end if;
2381 end if;
2382 end Preserve_Full_Attributes;
2384 -----------------
2385 -- Type_In_Use --
2386 -----------------
2388 function Type_In_Use (T : Entity_Id) return Boolean is
2389 begin
2390 return Scope (Base_Type (T)) = P
2391 and then (In_Use (T) or else In_Use (Base_Type (T)));
2392 end Type_In_Use;
2394 -- Start of processing for Uninstall_Declarations
2396 begin
2397 Id := First_Entity (P);
2398 while Present (Id) and then Id /= First_Private_Entity (P) loop
2399 if Debug_Flag_E then
2400 Write_Str ("unlinking visible entity ");
2401 Write_Int (Int (Id));
2402 Write_Eol;
2403 end if;
2405 -- On exit from the package scope, we must preserve the visibility
2406 -- established by use clauses in the current scope. Two cases:
2408 -- a) If the entity is an operator, it may be a primitive operator of
2409 -- a type for which there is a visible use-type clause.
2411 -- b) for other entities, their use-visibility is determined by a
2412 -- visible use clause for the package itself. For a generic instance,
2413 -- the instantiation of the formals appears in the visible part,
2414 -- but the formals are private and remain so.
2416 if Ekind (Id) = E_Function
2417 and then Is_Operator_Symbol_Name (Chars (Id))
2418 and then not Is_Hidden (Id)
2419 and then not Error_Posted (Id)
2420 then
2421 Set_Is_Potentially_Use_Visible (Id,
2422 In_Use (P)
2423 or else Type_In_Use (Etype (Id))
2424 or else Type_In_Use (Etype (First_Formal (Id)))
2425 or else (Present (Next_Formal (First_Formal (Id)))
2426 and then
2427 Type_In_Use
2428 (Etype (Next_Formal (First_Formal (Id))))));
2429 else
2430 if In_Use (P) and then not Is_Hidden (Id) then
2432 -- A child unit of a use-visible package remains use-visible
2433 -- only if it is itself a visible child unit. Otherwise it
2434 -- would remain visible in other contexts where P is use-
2435 -- visible, because once compiled it stays in the entity list
2436 -- of its parent unit.
2438 if Is_Child_Unit (Id) then
2439 Set_Is_Potentially_Use_Visible
2440 (Id, Is_Visible_Lib_Unit (Id));
2441 else
2442 Set_Is_Potentially_Use_Visible (Id);
2443 end if;
2445 else
2446 Set_Is_Potentially_Use_Visible (Id, False);
2447 end if;
2448 end if;
2450 -- Local entities are not immediately visible outside of the package
2452 Set_Is_Immediately_Visible (Id, False);
2454 -- If this is a private type with a full view (for example a local
2455 -- subtype of a private type declared elsewhere), ensure that the
2456 -- full view is also removed from visibility: it may be exposed when
2457 -- swapping views in an instantiation.
2459 if Is_Type (Id) and then Present (Full_View (Id)) then
2460 Set_Is_Immediately_Visible (Full_View (Id), False);
2461 end if;
2463 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2464 Check_Abstract_Overriding (Id);
2465 Check_Conventions (Id);
2466 end if;
2468 if Ekind_In (Id, E_Private_Type, E_Limited_Private_Type)
2469 and then No (Full_View (Id))
2470 and then not Is_Generic_Type (Id)
2471 and then not Is_Derived_Type (Id)
2472 then
2473 Error_Msg_N ("missing full declaration for private type&", Id);
2475 elsif Ekind (Id) = E_Record_Type_With_Private
2476 and then not Is_Generic_Type (Id)
2477 and then No (Full_View (Id))
2478 then
2479 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
2480 Error_Msg_N ("missing full declaration for private type&", Id);
2481 else
2482 Error_Msg_N
2483 ("missing full declaration for private extension", Id);
2484 end if;
2486 -- Case of constant, check for deferred constant declaration with
2487 -- no full view. Likely just a matter of a missing expression, or
2488 -- accidental use of the keyword constant.
2490 elsif Ekind (Id) = E_Constant
2492 -- OK if constant value present
2494 and then No (Constant_Value (Id))
2496 -- OK if full view present
2498 and then No (Full_View (Id))
2500 -- OK if imported, since that provides the completion
2502 and then not Is_Imported (Id)
2504 -- OK if object declaration replaced by renaming declaration as
2505 -- a result of OK_To_Rename processing (e.g. for concatenation)
2507 and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration
2509 -- OK if object declaration with the No_Initialization flag set
2511 and then not (Nkind (Parent (Id)) = N_Object_Declaration
2512 and then No_Initialization (Parent (Id)))
2513 then
2514 -- If no private declaration is present, we assume the user did
2515 -- not intend a deferred constant declaration and the problem
2516 -- is simply that the initializing expression is missing.
2518 if not Has_Private_Declaration (Etype (Id)) then
2520 -- We assume that the user did not intend a deferred constant
2521 -- declaration, and the expression is just missing.
2523 Error_Msg_N
2524 ("constant declaration requires initialization expression",
2525 Parent (Id));
2527 if Is_Limited_Type (Etype (Id)) then
2528 Error_Msg_N
2529 ("\if variable intended, remove CONSTANT from declaration",
2530 Parent (Id));
2531 end if;
2533 -- Otherwise if a private declaration is present, then we are
2534 -- missing the full declaration for the deferred constant.
2536 else
2537 Error_Msg_N
2538 ("missing full declaration for deferred constant (RM 7.4)",
2539 Id);
2541 if Is_Limited_Type (Etype (Id)) then
2542 Error_Msg_N
2543 ("\if variable intended, remove CONSTANT from declaration",
2544 Parent (Id));
2545 end if;
2546 end if;
2547 end if;
2549 Next_Entity (Id);
2550 end loop;
2552 -- If the specification was installed as the parent of a public child
2553 -- unit, the private declarations were not installed, and there is
2554 -- nothing to do.
2556 if not In_Private_Part (P) then
2557 return;
2558 else
2559 Set_In_Private_Part (P, False);
2560 end if;
2562 -- Make private entities invisible and exchange full and private
2563 -- declarations for private types. Id is now the first private entity
2564 -- in the package.
2566 while Present (Id) loop
2567 if Debug_Flag_E then
2568 Write_Str ("unlinking private entity ");
2569 Write_Int (Int (Id));
2570 Write_Eol;
2571 end if;
2573 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2574 Check_Abstract_Overriding (Id);
2575 Check_Conventions (Id);
2576 end if;
2578 Set_Is_Immediately_Visible (Id, False);
2580 if Is_Private_Base_Type (Id) and then Present (Full_View (Id)) then
2581 Full := Full_View (Id);
2583 -- If the partial view is not declared in the visible part of the
2584 -- package (as is the case when it is a type derived from some
2585 -- other private type in the private part of the current package),
2586 -- no exchange takes place.
2588 if No (Parent (Id))
2589 or else List_Containing (Parent (Id)) /=
2590 Visible_Declarations (Specification (Decl))
2591 then
2592 goto Next_Id;
2593 end if;
2595 -- The entry in the private part points to the full declaration,
2596 -- which is currently visible. Exchange them so only the private
2597 -- type declaration remains accessible, and link private and full
2598 -- declaration in the opposite direction. Before the actual
2599 -- exchange, we copy back attributes of the full view that must
2600 -- be available to the partial view too.
2602 Preserve_Full_Attributes (Id, Full);
2604 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
2606 if Is_Indefinite_Subtype (Full)
2607 and then not Is_Indefinite_Subtype (Id)
2608 then
2609 Error_Msg_N
2610 ("full view of type must be definite subtype", Full);
2611 end if;
2613 -- Swap out the subtypes and derived types of Id that
2614 -- were compiled in this scope, or installed previously
2615 -- by Install_Private_Declarations.
2617 -- Before we do the swap, we verify the presence of the Full_View
2618 -- field which may be empty due to a swap by a previous call to
2619 -- End_Package_Scope (e.g. from the freezing mechanism).
2621 Priv_Elmt := First_Elmt (Private_Dependents (Id));
2622 while Present (Priv_Elmt) loop
2623 Priv_Sub := Node (Priv_Elmt);
2625 if Present (Full_View (Priv_Sub)) then
2626 if Scope (Priv_Sub) = P
2627 or else not In_Open_Scopes (Scope (Priv_Sub))
2628 then
2629 Set_Is_Immediately_Visible (Priv_Sub, False);
2630 end if;
2632 if Is_Visible_Dependent (Priv_Sub) then
2633 Preserve_Full_Attributes
2634 (Priv_Sub, Full_View (Priv_Sub));
2635 Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub));
2636 Exchange_Declarations (Priv_Sub);
2637 end if;
2638 end if;
2640 Next_Elmt (Priv_Elmt);
2641 end loop;
2643 -- Now restore the type itself to its private view
2645 Exchange_Declarations (Id);
2647 -- If we have installed an underlying full view for a type derived
2648 -- from a private type in a child unit, restore the proper views
2649 -- of private and full view. See corresponding code in
2650 -- Install_Private_Declarations.
2652 -- After the exchange, Full denotes the private type in the
2653 -- visible part of the package.
2655 if Is_Private_Base_Type (Full)
2656 and then Present (Full_View (Full))
2657 and then Present (Underlying_Full_View (Full))
2658 and then In_Package_Body (Current_Scope)
2659 then
2660 Set_Full_View (Full, Underlying_Full_View (Full));
2661 Set_Underlying_Full_View (Full, Empty);
2662 end if;
2664 elsif Ekind (Id) = E_Incomplete_Type
2665 and then Comes_From_Source (Id)
2666 and then No (Full_View (Id))
2667 then
2668 -- Mark Taft amendment types. Verify that there are no primitive
2669 -- operations declared for the type (3.10.1(9)).
2671 Set_Has_Completion_In_Body (Id);
2673 declare
2674 Elmt : Elmt_Id;
2675 Subp : Entity_Id;
2677 begin
2678 Elmt := First_Elmt (Private_Dependents (Id));
2679 while Present (Elmt) loop
2680 Subp := Node (Elmt);
2682 -- Is_Primitive is tested because there can be cases where
2683 -- nonprimitive subprograms (in nested packages) are added
2684 -- to the Private_Dependents list.
2686 if Is_Overloadable (Subp) and then Is_Primitive (Subp) then
2687 Error_Msg_NE
2688 ("type& must be completed in the private part",
2689 Parent (Subp), Id);
2691 -- The result type of an access-to-function type cannot be a
2692 -- Taft-amendment type, unless the version is Ada 2012 or
2693 -- later (see AI05-151).
2695 elsif Ada_Version < Ada_2012
2696 and then Ekind (Subp) = E_Subprogram_Type
2697 then
2698 if Etype (Subp) = Id
2699 or else
2700 (Is_Class_Wide_Type (Etype (Subp))
2701 and then Etype (Etype (Subp)) = Id)
2702 then
2703 Error_Msg_NE
2704 ("type& must be completed in the private part",
2705 Associated_Node_For_Itype (Subp), Id);
2706 end if;
2707 end if;
2709 Next_Elmt (Elmt);
2710 end loop;
2711 end;
2713 elsif not Is_Child_Unit (Id)
2714 and then (not Is_Private_Type (Id) or else No (Full_View (Id)))
2715 then
2716 Set_Is_Hidden (Id);
2717 Set_Is_Potentially_Use_Visible (Id, False);
2718 end if;
2720 <<Next_Id>>
2721 Next_Entity (Id);
2722 end loop;
2723 end Uninstall_Declarations;
2725 ------------------------
2726 -- Unit_Requires_Body --
2727 ------------------------
2729 function Unit_Requires_Body
2730 (P : Entity_Id;
2731 Ignore_Abstract_State : Boolean := False) return Boolean
2733 E : Entity_Id;
2735 begin
2736 -- Imported entity never requires body. Right now, only subprograms can
2737 -- be imported, but perhaps in the future we will allow import of
2738 -- packages.
2740 if Is_Imported (P) then
2741 return False;
2743 -- Body required if library package with pragma Elaborate_Body
2745 elsif Has_Pragma_Elaborate_Body (P) then
2746 return True;
2748 -- Body required if subprogram
2750 elsif Is_Subprogram (P) or else Is_Generic_Subprogram (P) then
2751 return True;
2753 -- Treat a block as requiring a body
2755 elsif Ekind (P) = E_Block then
2756 return True;
2758 elsif Ekind (P) = E_Package
2759 and then Nkind (Parent (P)) = N_Package_Specification
2760 and then Present (Generic_Parent (Parent (P)))
2761 then
2762 declare
2763 G_P : constant Entity_Id := Generic_Parent (Parent (P));
2764 begin
2765 if Has_Pragma_Elaborate_Body (G_P) then
2766 return True;
2767 end if;
2768 end;
2770 -- A [generic] package that introduces at least one non-null abstract
2771 -- state requires completion. However, there is a separate rule that
2772 -- requires that such a package have a reason other than this for a
2773 -- body being required (if necessary a pragma Elaborate_Body must be
2774 -- provided). If Ignore_Abstract_State is True, we don't do this check
2775 -- (so we can use Unit_Requires_Body to check for some other reason).
2777 elsif Ekind_In (P, E_Generic_Package, E_Package)
2778 and then not Ignore_Abstract_State
2779 and then Present (Abstract_States (P))
2780 and then
2781 not Is_Null_State (Node (First_Elmt (Abstract_States (P))))
2782 then
2783 return True;
2784 end if;
2786 -- Otherwise search entity chain for entity requiring completion
2788 E := First_Entity (P);
2789 while Present (E) loop
2791 -- Always ignore child units. Child units get added to the entity
2792 -- list of a parent unit, but are not original entities of the
2793 -- parent, and so do not affect whether the parent needs a body.
2795 if Is_Child_Unit (E) then
2796 null;
2798 -- Ignore formal packages and their renamings
2800 elsif Ekind (E) = E_Package
2801 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
2802 N_Formal_Package_Declaration
2803 then
2804 null;
2806 -- Otherwise test to see if entity requires a completion.
2807 -- Note that subprogram entities whose declaration does not come
2808 -- from source are ignored here on the basis that we assume the
2809 -- expander will provide an implicit completion at some point.
2811 elsif (Is_Overloadable (E)
2812 and then Ekind (E) /= E_Enumeration_Literal
2813 and then Ekind (E) /= E_Operator
2814 and then not Is_Abstract_Subprogram (E)
2815 and then not Has_Completion (E)
2816 and then Comes_From_Source (Parent (E)))
2818 or else
2819 (Ekind (E) = E_Package
2820 and then E /= P
2821 and then not Has_Completion (E)
2822 and then Unit_Requires_Body (E))
2824 or else
2825 (Ekind (E) = E_Incomplete_Type
2826 and then No (Full_View (E))
2827 and then not Is_Generic_Type (E))
2829 or else
2830 (Ekind_In (E, E_Task_Type, E_Protected_Type)
2831 and then not Has_Completion (E))
2833 or else
2834 (Ekind (E) = E_Generic_Package
2835 and then E /= P
2836 and then not Has_Completion (E)
2837 and then Unit_Requires_Body (E))
2839 or else
2840 (Is_Generic_Subprogram (E)
2841 and then not Has_Completion (E))
2843 then
2844 return True;
2846 -- Entity that does not require completion
2848 else
2849 null;
2850 end if;
2852 Next_Entity (E);
2853 end loop;
2855 return False;
2856 end Unit_Requires_Body;
2858 -----------------------------
2859 -- Unit_Requires_Body_Info --
2860 -----------------------------
2862 procedure Unit_Requires_Body_Info (P : Entity_Id) is
2863 E : Entity_Id;
2865 begin
2866 -- Imported entity never requires body. Right now, only subprograms can
2867 -- be imported, but perhaps in the future we will allow import of
2868 -- packages.
2870 if Is_Imported (P) then
2871 return;
2873 -- Body required if library package with pragma Elaborate_Body
2875 elsif Has_Pragma_Elaborate_Body (P) then
2876 Error_Msg_N
2877 ("?Y?info: & requires body (Elaborate_Body)", P);
2879 -- Body required if subprogram
2881 elsif Is_Subprogram (P) or else Is_Generic_Subprogram (P) then
2882 Error_Msg_N ("?Y?info: & requires body (subprogram case)", P);
2884 -- Body required if generic parent has Elaborate_Body
2886 elsif Ekind (P) = E_Package
2887 and then Nkind (Parent (P)) = N_Package_Specification
2888 and then Present (Generic_Parent (Parent (P)))
2889 then
2890 declare
2891 G_P : constant Entity_Id := Generic_Parent (Parent (P));
2892 begin
2893 if Has_Pragma_Elaborate_Body (G_P) then
2894 Error_Msg_N
2895 ("?Y?info: & requires body (generic parent Elaborate_Body)",
2897 end if;
2898 end;
2900 -- A [generic] package that introduces at least one non-null abstract
2901 -- state requires completion. However, there is a separate rule that
2902 -- requires that such a package have a reason other than this for a
2903 -- body being required (if necessary a pragma Elaborate_Body must be
2904 -- provided). If Ignore_Abstract_State is True, we don't do this check
2905 -- (so we can use Unit_Requires_Body to check for some other reason).
2907 elsif Ekind_In (P, E_Generic_Package, E_Package)
2908 and then Present (Abstract_States (P))
2909 and then
2910 not Is_Null_State (Node (First_Elmt (Abstract_States (P))))
2911 then
2912 Error_Msg_N
2913 ("?Y?info: & requires body (non-null abstract state aspect)", P);
2914 end if;
2916 -- Otherwise search entity chain for entity requiring completion
2918 E := First_Entity (P);
2919 while Present (E) loop
2921 -- Always ignore child units. Child units get added to the entity
2922 -- list of a parent unit, but are not original entities of the
2923 -- parent, and so do not affect whether the parent needs a body.
2925 if Is_Child_Unit (E) then
2926 null;
2928 -- Ignore formal packages and their renamings
2930 elsif Ekind (E) = E_Package
2931 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
2932 N_Formal_Package_Declaration
2933 then
2934 null;
2936 -- Otherwise test to see if entity requires a completion.
2937 -- Note that subprogram entities whose declaration does not come
2938 -- from source are ignored here on the basis that we assume the
2939 -- expander will provide an implicit completion at some point.
2941 elsif (Is_Overloadable (E)
2942 and then Ekind (E) /= E_Enumeration_Literal
2943 and then Ekind (E) /= E_Operator
2944 and then not Is_Abstract_Subprogram (E)
2945 and then not Has_Completion (E)
2946 and then Comes_From_Source (Parent (E)))
2948 or else
2949 (Ekind (E) = E_Package
2950 and then E /= P
2951 and then not Has_Completion (E)
2952 and then Unit_Requires_Body (E))
2954 or else
2955 (Ekind (E) = E_Incomplete_Type
2956 and then No (Full_View (E))
2957 and then not Is_Generic_Type (E))
2959 or else
2960 (Ekind_In (E, E_Task_Type, E_Protected_Type)
2961 and then not Has_Completion (E))
2963 or else
2964 (Ekind (E) = E_Generic_Package
2965 and then E /= P
2966 and then not Has_Completion (E)
2967 and then Unit_Requires_Body (E))
2969 or else
2970 (Is_Generic_Subprogram (E)
2971 and then not Has_Completion (E))
2973 then
2974 Error_Msg_Node_2 := E;
2975 Error_Msg_NE
2976 ("?Y?info: & requires body (& requires completion)",
2977 E, P);
2979 -- Entity that does not require completion
2981 else
2982 null;
2983 end if;
2985 Next_Entity (E);
2986 end loop;
2987 end Unit_Requires_Body_Info;
2988 end Sem_Ch7;