[gcc/]
[official-gcc.git] / gcc / ada / sem_ch7.adb
blob418e2166b4f16e06b4bc3c5d61499623b8439a59
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-2014, 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 Mode : SPARK_Mode_Type;
184 Prag : Node_Id;
186 begin
187 -- Due to the timing of contract analysis, delayed pragmas may be
188 -- subject to the wrong SPARK_Mode, usually that of the enclosing
189 -- context. To remedy this, restore the original SPARK_Mode of the
190 -- related package body.
192 Save_SPARK_Mode_And_Set (Body_Id, Mode);
194 Prag := Get_Pragma (Body_Id, Pragma_Refined_State);
196 -- The analysis of pragma Refined_State detects whether the spec has
197 -- abstract states available for refinement.
199 if Present (Prag) then
200 Analyze_Refined_State_In_Decl_Part (Prag);
202 -- State refinement is required when the package declaration defines at
203 -- least one abstract state. Null states are not considered. Refinement
204 -- is not envorced when SPARK checks are turned off.
206 elsif SPARK_Mode /= Off
207 and then Requires_State_Refinement (Spec_Id, Body_Id)
208 then
209 Error_Msg_N ("package & requires state refinement", Spec_Id);
210 end if;
212 -- Restore the SPARK_Mode of the enclosing context after all delayed
213 -- pragmas have been analyzed.
215 Restore_SPARK_Mode (Mode);
216 end Analyze_Package_Body_Contract;
218 ---------------------------------
219 -- Analyze_Package_Body_Helper --
220 ---------------------------------
222 procedure Analyze_Package_Body_Helper (N : Node_Id) is
223 HSS : Node_Id;
224 Body_Id : Entity_Id;
225 Spec_Id : Entity_Id;
226 Last_Spec_Entity : Entity_Id;
227 New_N : Node_Id;
228 Pack_Decl : Node_Id;
230 procedure Install_Composite_Operations (P : Entity_Id);
231 -- Composite types declared in the current scope may depend on types
232 -- that were private at the point of declaration, and whose full view
233 -- is now in scope. Indicate that the corresponding operations on the
234 -- composite type are available.
236 ----------------------------------
237 -- Install_Composite_Operations --
238 ----------------------------------
240 procedure Install_Composite_Operations (P : Entity_Id) is
241 Id : Entity_Id;
243 begin
244 Id := First_Entity (P);
245 while Present (Id) loop
246 if Is_Type (Id)
247 and then (Is_Limited_Composite (Id)
248 or else Is_Private_Composite (Id))
249 and then No (Private_Component (Id))
250 then
251 Set_Is_Limited_Composite (Id, False);
252 Set_Is_Private_Composite (Id, False);
253 end if;
255 Next_Entity (Id);
256 end loop;
257 end Install_Composite_Operations;
259 -- Start of processing for Analyze_Package_Body_Helper
261 begin
262 -- Find corresponding package specification, and establish the current
263 -- scope. The visible defining entity for the package is the defining
264 -- occurrence in the spec. On exit from the package body, all body
265 -- declarations are attached to the defining entity for the body, but
266 -- the later is never used for name resolution. In this fashion there
267 -- is only one visible entity that denotes the package.
269 -- Set Body_Id. Note that this will be reset to point to the generic
270 -- copy later on in the generic case.
272 Body_Id := Defining_Entity (N);
274 -- Body is body of package instantiation. Corresponding spec has already
275 -- been set.
277 if Present (Corresponding_Spec (N)) then
278 Spec_Id := Corresponding_Spec (N);
279 Pack_Decl := Unit_Declaration_Node (Spec_Id);
281 else
282 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
284 if Present (Spec_Id)
285 and then Is_Package_Or_Generic_Package (Spec_Id)
286 then
287 Pack_Decl := Unit_Declaration_Node (Spec_Id);
289 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
290 Error_Msg_N ("cannot supply body for package renaming", N);
291 return;
293 elsif Present (Corresponding_Body (Pack_Decl)) then
294 Error_Msg_N ("redefinition of package body", N);
295 return;
296 end if;
298 else
299 Error_Msg_N ("missing specification for package body", N);
300 return;
301 end if;
303 if Is_Package_Or_Generic_Package (Spec_Id)
304 and then (Scope (Spec_Id) = Standard_Standard
305 or else Is_Child_Unit (Spec_Id))
306 and then not Unit_Requires_Body (Spec_Id)
307 then
308 if Ada_Version = Ada_83 then
309 Error_Msg_N
310 ("optional package body (not allowed in Ada 95)??", N);
311 else
312 Error_Msg_N ("spec of this package does not allow a body", N);
313 end if;
314 end if;
315 end if;
317 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
318 Style.Check_Identifier (Body_Id, Spec_Id);
320 if Is_Child_Unit (Spec_Id) then
321 if Nkind (Parent (N)) /= N_Compilation_Unit then
322 Error_Msg_NE
323 ("body of child unit& cannot be an inner package", N, Spec_Id);
324 end if;
326 Set_Is_Child_Unit (Body_Id);
327 end if;
329 -- Generic package case
331 if Ekind (Spec_Id) = E_Generic_Package then
333 -- Disable expansion and perform semantic analysis on copy. The
334 -- unannotated body will be used in all instantiations.
336 Body_Id := Defining_Entity (N);
337 Set_Ekind (Body_Id, E_Package_Body);
338 Set_Scope (Body_Id, Scope (Spec_Id));
339 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
340 Set_Body_Entity (Spec_Id, Body_Id);
341 Set_Spec_Entity (Body_Id, Spec_Id);
343 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
344 Rewrite (N, New_N);
346 -- Once the contents of the generic copy and the template are
347 -- swapped, do the same for their respective aspect specifications.
349 Exchange_Aspects (N, New_N);
351 -- Update Body_Id to point to the copied node for the remainder of
352 -- the processing.
354 Body_Id := Defining_Entity (N);
355 Start_Generic;
356 end if;
358 -- The Body_Id is that of the copied node in the generic case, the
359 -- current node otherwise. Note that N was rewritten above, so we must
360 -- be sure to get the latest Body_Id value.
362 Set_Ekind (Body_Id, E_Package_Body);
363 Set_Body_Entity (Spec_Id, Body_Id);
364 Set_Spec_Entity (Body_Id, Spec_Id);
365 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
367 -- Defining name for the package body is not a visible entity: Only the
368 -- defining name for the declaration is visible.
370 Set_Etype (Body_Id, Standard_Void_Type);
371 Set_Scope (Body_Id, Scope (Spec_Id));
372 Set_Corresponding_Spec (N, Spec_Id);
373 Set_Corresponding_Body (Pack_Decl, Body_Id);
375 -- The body entity is not used for semantics or code generation, but
376 -- it is attached to the entity list of the enclosing scope to simplify
377 -- the listing of back-annotations for the types it main contain.
379 if Scope (Spec_Id) /= Standard_Standard then
380 Append_Entity (Body_Id, Scope (Spec_Id));
381 end if;
383 -- Indicate that we are currently compiling the body of the package
385 Set_In_Package_Body (Spec_Id);
386 Set_Has_Completion (Spec_Id);
387 Last_Spec_Entity := Last_Entity (Spec_Id);
389 if Has_Aspects (N) then
390 Analyze_Aspect_Specifications (N, Body_Id);
391 end if;
393 Push_Scope (Spec_Id);
395 -- Set SPARK_Mode only for non-generic package
397 if Ekind (Spec_Id) = E_Package then
399 -- Set SPARK_Mode from context
401 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
402 Set_SPARK_Pragma_Inherited (Body_Id, True);
404 -- Set elaboration code SPARK mode the same for now
406 Set_SPARK_Aux_Pragma (Body_Id, SPARK_Pragma (Body_Id));
407 Set_SPARK_Aux_Pragma_Inherited (Body_Id, True);
408 end if;
410 Set_Categorization_From_Pragmas (N);
412 Install_Visible_Declarations (Spec_Id);
413 Install_Private_Declarations (Spec_Id);
414 Install_Private_With_Clauses (Spec_Id);
415 Install_Composite_Operations (Spec_Id);
417 Check_Anonymous_Access_Types (Spec_Id, N);
419 if Ekind (Spec_Id) = E_Generic_Package then
420 Set_Use (Generic_Formal_Declarations (Pack_Decl));
421 end if;
423 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
424 Set_Use (Private_Declarations (Specification (Pack_Decl)));
426 -- This is a nested package, so it may be necessary to declare certain
427 -- inherited subprograms that are not yet visible because the parent
428 -- type's subprograms are now visible.
430 if Ekind (Scope (Spec_Id)) = E_Package
431 and then Scope (Spec_Id) /= Standard_Standard
432 then
433 Declare_Inherited_Private_Subprograms (Spec_Id);
434 end if;
436 if Present (Declarations (N)) then
437 Analyze_Declarations (Declarations (N));
438 Inspect_Deferred_Constant_Completion (Declarations (N));
439 end if;
441 -- After declarations have been analyzed, the body has been set to have
442 -- the final value of SPARK_Mode. Check that the SPARK_Mode for the body
443 -- is consistent with the SPARK_Mode for the spec.
445 if Present (SPARK_Pragma (Body_Id)) then
446 if Present (SPARK_Aux_Pragma (Spec_Id)) then
447 if Get_SPARK_Mode_From_Pragma (SPARK_Aux_Pragma (Spec_Id)) = Off
448 and then
449 Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Body_Id)) = On
450 then
451 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
452 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
453 Error_Msg_Sloc := Sloc (SPARK_Aux_Pragma (Spec_Id));
454 Error_Msg_NE
455 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
456 end if;
458 else
459 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
460 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
461 Error_Msg_Sloc := Sloc (Spec_Id);
462 Error_Msg_NE
463 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
464 end if;
465 end if;
467 -- Analyze_Declarations has caused freezing of all types. Now generate
468 -- bodies for RACW primitives and stream attributes, if any.
470 if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then
472 -- Attach subprogram bodies to support RACWs declared in spec
474 Append_RACW_Bodies (Declarations (N), Spec_Id);
475 Analyze_List (Declarations (N));
476 end if;
478 HSS := Handled_Statement_Sequence (N);
480 if Present (HSS) then
481 Process_End_Label (HSS, 't', Spec_Id);
482 Analyze (HSS);
484 -- Check that elaboration code in a preelaborable package body is
485 -- empty other than null statements and labels (RM 10.2.1(6)).
487 Validate_Null_Statement_Sequence (N);
488 end if;
490 Validate_Categorization_Dependency (N, Spec_Id);
491 Check_Completion (Body_Id);
493 -- Generate start of body reference. Note that we do this fairly late,
494 -- because the call will use In_Extended_Main_Source_Unit as a check,
495 -- and we want to make sure that Corresponding_Stub links are set
497 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
499 -- For a generic package, collect global references and mark them on
500 -- the original body so that they are not resolved again at the point
501 -- of instantiation.
503 if Ekind (Spec_Id) /= E_Package then
504 Save_Global_References (Original_Node (N));
505 End_Generic;
506 end if;
508 -- The entities of the package body have so far been chained onto the
509 -- declaration chain for the spec. That's been fine while we were in the
510 -- body, since we wanted them to be visible, but now that we are leaving
511 -- the package body, they are no longer visible, so we remove them from
512 -- the entity chain of the package spec entity, and copy them to the
513 -- entity chain of the package body entity, where they will never again
514 -- be visible.
516 if Present (Last_Spec_Entity) then
517 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
518 Set_Next_Entity (Last_Spec_Entity, Empty);
519 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
520 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
522 else
523 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
524 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
525 Set_First_Entity (Spec_Id, Empty);
526 Set_Last_Entity (Spec_Id, Empty);
527 end if;
529 End_Package_Scope (Spec_Id);
531 -- All entities declared in body are not visible
533 declare
534 E : Entity_Id;
536 begin
537 E := First_Entity (Body_Id);
538 while Present (E) loop
539 Set_Is_Immediately_Visible (E, False);
540 Set_Is_Potentially_Use_Visible (E, False);
541 Set_Is_Hidden (E);
543 -- Child units may appear on the entity list (e.g. if they appear
544 -- in the context of a subunit) but they are not body entities.
546 if not Is_Child_Unit (E) then
547 Set_Is_Package_Body_Entity (E);
548 end if;
550 Next_Entity (E);
551 end loop;
552 end;
554 Check_References (Body_Id);
556 -- For a generic unit, check that the formal parameters are referenced,
557 -- and that local variables are used, as for regular packages.
559 if Ekind (Spec_Id) = E_Generic_Package then
560 Check_References (Spec_Id);
561 end if;
563 -- The processing so far has made all entities of the package body
564 -- public (i.e. externally visible to the linker). This is in general
565 -- necessary, since inlined or generic bodies, for which code is
566 -- generated in other units, may need to see these entities. The
567 -- following loop runs backwards from the end of the entities of the
568 -- package body making these entities invisible until we reach a
569 -- referencer, i.e. a declaration that could reference a previous
570 -- declaration, a generic body or an inlined body, or a stub (which may
571 -- contain either of these). This is of course an approximation, but it
572 -- is conservative and definitely correct.
574 -- We only do this at the outer (library) level non-generic packages.
575 -- The reason is simply to cut down on the number of global symbols
576 -- generated, which has a double effect: (1) to make the compilation
577 -- process more efficient and (2) to give the code generator more
578 -- freedom to optimize within each unit, especially subprograms.
580 if (Scope (Spec_Id) = Standard_Standard or else Is_Child_Unit (Spec_Id))
581 and then not Is_Generic_Unit (Spec_Id)
582 and then Present (Declarations (N))
583 then
584 Make_Non_Public_Where_Possible : declare
586 function Has_Referencer
587 (L : List_Id;
588 Outer : Boolean) return Boolean;
589 -- Traverse given list of declarations in reverse order. Return
590 -- True if a referencer is present. Return False if none is found.
592 -- The Outer parameter is True for the outer level call and False
593 -- for inner level calls for nested packages. If Outer is True,
594 -- then any entities up to the point of hitting a referencer get
595 -- their Is_Public flag cleared, so that the entities will be
596 -- treated as static entities in the C sense, and need not have
597 -- fully qualified names. Furthermore, if the referencer is an
598 -- inlined subprogram that doesn't reference other subprograms,
599 -- we keep clearing the Is_Public flag on subprograms. For inner
600 -- levels, we need all names to be fully qualified to deal with
601 -- the same name appearing in parallel packages (right now this
602 -- is tied to their being external).
604 --------------------
605 -- Has_Referencer --
606 --------------------
608 function Has_Referencer
609 (L : List_Id;
610 Outer : Boolean) return Boolean
612 Has_Referencer_Except_For_Subprograms : Boolean := False;
614 D : Node_Id;
615 E : Entity_Id;
616 K : Node_Kind;
617 S : Entity_Id;
619 function Check_Subprogram_Ref (N : Node_Id)
620 return Traverse_Result;
621 -- Look for references to subprograms
623 --------------------------
624 -- Check_Subprogram_Ref --
625 --------------------------
627 function Check_Subprogram_Ref (N : Node_Id)
628 return Traverse_Result
630 V : Node_Id;
632 begin
633 -- Check name of procedure or function calls
635 if Nkind (N) in N_Subprogram_Call
636 and then Is_Entity_Name (Name (N))
637 then
638 return Abandon;
639 end if;
641 -- Check prefix of attribute references
643 if Nkind (N) = N_Attribute_Reference
644 and then Is_Entity_Name (Prefix (N))
645 and then Present (Entity (Prefix (N)))
646 and then Ekind (Entity (Prefix (N))) in Subprogram_Kind
647 then
648 return Abandon;
649 end if;
651 -- Check value of constants
653 if Nkind (N) = N_Identifier
654 and then Present (Entity (N))
655 and then Ekind (Entity (N)) = E_Constant
656 then
657 V := Constant_Value (Entity (N));
659 if Present (V)
660 and then not Compile_Time_Known_Value_Or_Aggr (V)
661 then
662 return Abandon;
663 end if;
664 end if;
666 return OK;
667 end Check_Subprogram_Ref;
669 function Check_Subprogram_Refs is
670 new Traverse_Func (Check_Subprogram_Ref);
672 -- Start of processing for Has_Referencer
674 begin
675 if No (L) then
676 return False;
677 end if;
679 D := Last (L);
680 while Present (D) loop
681 K := Nkind (D);
683 if K in N_Body_Stub then
684 return True;
686 -- Processing for subprogram bodies
688 elsif K = N_Subprogram_Body then
689 if Acts_As_Spec (D) then
690 E := Defining_Entity (D);
692 -- An inlined body acts as a referencer. Note also
693 -- that we never reset Is_Public for an inlined
694 -- subprogram. Gigi requires Is_Public to be set.
696 -- Note that we test Has_Pragma_Inline here rather
697 -- than Is_Inlined. We are compiling this for a
698 -- client, and it is the client who will decide if
699 -- actual inlining should occur, so we need to assume
700 -- that the procedure could be inlined for the purpose
701 -- of accessing global entities.
703 if Has_Pragma_Inline (E) then
704 if Outer
705 and then Check_Subprogram_Refs (D) = OK
706 then
707 Has_Referencer_Except_For_Subprograms := True;
708 else
709 return True;
710 end if;
711 else
712 Set_Is_Public (E, False);
713 end if;
715 else
716 E := Corresponding_Spec (D);
718 if Present (E) then
720 -- A generic subprogram body acts as a referencer
722 if Is_Generic_Unit (E) then
723 return True;
724 end if;
726 if Has_Pragma_Inline (E) or else Is_Inlined (E) then
727 if Outer
728 and then Check_Subprogram_Refs (D) = OK
729 then
730 Has_Referencer_Except_For_Subprograms := True;
731 else
732 return True;
733 end if;
734 end if;
735 end if;
736 end if;
738 -- Processing for package bodies
740 elsif K = N_Package_Body
741 and then Present (Corresponding_Spec (D))
742 then
743 E := Corresponding_Spec (D);
745 -- Generic package body is a referencer. It would seem
746 -- that we only have to consider generics that can be
747 -- exported, i.e. where the corresponding spec is the
748 -- spec of the current package, but because of nested
749 -- instantiations, a fully private generic body may
750 -- export other private body entities. Furthermore,
751 -- regardless of whether there was a previous inlined
752 -- subprogram, (an instantiation of) the generic package
753 -- may reference any entity declared before it.
755 if Is_Generic_Unit (E) then
756 return True;
758 -- For non-generic package body, recurse into body unless
759 -- this is an instance, we ignore instances since they
760 -- cannot have references that affect outer entities.
762 elsif not Is_Generic_Instance (E)
763 and then not Has_Referencer_Except_For_Subprograms
764 then
765 if Has_Referencer
766 (Declarations (D), Outer => False)
767 then
768 return True;
769 end if;
770 end if;
772 -- Processing for package specs, recurse into declarations.
773 -- Again we skip this for the case of generic instances.
775 elsif K = N_Package_Declaration
776 and then not Has_Referencer_Except_For_Subprograms
777 then
778 S := Specification (D);
780 if not Is_Generic_Unit (Defining_Entity (S)) then
781 if Has_Referencer
782 (Private_Declarations (S), Outer => False)
783 then
784 return True;
785 elsif Has_Referencer
786 (Visible_Declarations (S), Outer => False)
787 then
788 return True;
789 end if;
790 end if;
792 -- Objects and exceptions need not be public if we have not
793 -- encountered a referencer so far. We only reset the flag
794 -- for outer level entities that are not imported/exported,
795 -- and which have no interface name.
797 elsif Nkind_In (K, N_Object_Declaration,
798 N_Exception_Declaration,
799 N_Subprogram_Declaration)
800 then
801 E := Defining_Entity (D);
803 if Outer
804 and then (not Has_Referencer_Except_For_Subprograms
805 or else K = N_Subprogram_Declaration)
806 and then not Is_Imported (E)
807 and then not Is_Exported (E)
808 and then No (Interface_Name (E))
809 then
810 Set_Is_Public (E, False);
811 end if;
812 end if;
814 Prev (D);
815 end loop;
817 return Has_Referencer_Except_For_Subprograms;
818 end Has_Referencer;
820 -- Start of processing for Make_Non_Public_Where_Possible
822 begin
823 declare
824 Discard : Boolean;
825 pragma Warnings (Off, Discard);
827 begin
828 Discard := Has_Referencer (Declarations (N), Outer => True);
829 end;
830 end Make_Non_Public_Where_Possible;
831 end if;
833 -- If expander is not active, then here is where we turn off the
834 -- In_Package_Body flag, otherwise it is turned off at the end of the
835 -- corresponding expansion routine. If this is an instance body, we need
836 -- to qualify names of local entities, because the body may have been
837 -- compiled as a preliminary to another instantiation.
839 if not Expander_Active then
840 Set_In_Package_Body (Spec_Id, False);
842 if Is_Generic_Instance (Spec_Id)
843 and then Operating_Mode = Generate_Code
844 then
845 Qualify_Entity_Names (N);
846 end if;
847 end if;
848 end Analyze_Package_Body_Helper;
850 ------------------------------
851 -- Analyze_Package_Contract --
852 ------------------------------
854 procedure Analyze_Package_Contract (Pack_Id : Entity_Id) is
855 Mode : SPARK_Mode_Type;
856 Prag : Node_Id;
858 begin
859 -- Due to the timing of contract analysis, delayed pragmas may be
860 -- subject to the wrong SPARK_Mode, usually that of the enclosing
861 -- context. To remedy this, restore the original SPARK_Mode of the
862 -- related package.
864 Save_SPARK_Mode_And_Set (Pack_Id, Mode);
866 -- Analyze the initialization related pragmas. Initializes must come
867 -- before Initial_Condition due to item dependencies.
869 Prag := Get_Pragma (Pack_Id, Pragma_Initializes);
871 if Present (Prag) then
872 Analyze_Initializes_In_Decl_Part (Prag);
873 end if;
875 Prag := Get_Pragma (Pack_Id, Pragma_Initial_Condition);
877 if Present (Prag) then
878 Analyze_Initial_Condition_In_Decl_Part (Prag);
879 end if;
881 -- Check whether the lack of indicator Part_Of agrees with the placement
882 -- of the package instantiation with respect to the state space.
884 if Is_Generic_Instance (Pack_Id) then
885 Prag := Get_Pragma (Pack_Id, Pragma_Part_Of);
887 if No (Prag) then
888 Check_Missing_Part_Of (Pack_Id);
889 end if;
890 end if;
892 -- Restore the SPARK_Mode of the enclosing context after all delayed
893 -- pragmas have been analyzed.
895 Restore_SPARK_Mode (Mode);
896 end Analyze_Package_Contract;
898 ---------------------------------
899 -- Analyze_Package_Declaration --
900 ---------------------------------
902 procedure Analyze_Package_Declaration (N : Node_Id) is
903 Id : constant Node_Id := Defining_Entity (N);
905 PF : Boolean;
906 -- True when in the context of a declared pure library unit
908 Body_Required : Boolean;
909 -- True when this package declaration requires a corresponding body
911 Comp_Unit : Boolean;
912 -- True when this package declaration is not a nested declaration
914 begin
915 if Debug_Flag_C then
916 Write_Str ("==> package spec ");
917 Write_Name (Chars (Id));
918 Write_Str (" from ");
919 Write_Location (Sloc (N));
920 Write_Eol;
921 Indent;
922 end if;
924 Generate_Definition (Id);
925 Enter_Name (Id);
926 Set_Ekind (Id, E_Package);
927 Set_Etype (Id, Standard_Void_Type);
928 Set_Contract (Id, Make_Contract (Sloc (Id)));
930 -- Set SPARK_Mode from context only for non-generic package
932 if Ekind (Id) = E_Package then
933 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
934 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
935 Set_SPARK_Pragma_Inherited (Id, True);
936 Set_SPARK_Aux_Pragma_Inherited (Id, True);
937 end if;
939 -- Analyze aspect specifications immediately, since we need to recognize
940 -- things like Pure early enough to diagnose violations during analysis.
942 if Has_Aspects (N) then
943 Analyze_Aspect_Specifications (N, Id);
944 end if;
946 -- Ada 2005 (AI-217): Check if the package has been illegally named
947 -- in a limited-with clause of its own context. In this case the error
948 -- has been previously notified by Analyze_Context.
950 -- limited with Pkg; -- ERROR
951 -- package Pkg is ...
953 if From_Limited_With (Id) then
954 return;
955 end if;
957 Push_Scope (Id);
959 PF := Is_Pure (Enclosing_Lib_Unit_Entity);
960 Set_Is_Pure (Id, PF);
962 Set_Categorization_From_Pragmas (N);
964 Analyze (Specification (N));
965 Validate_Categorization_Dependency (N, Id);
967 Body_Required := Unit_Requires_Body (Id);
969 -- When this spec does not require an explicit body, we know that there
970 -- are no entities requiring completion in the language sense; we call
971 -- Check_Completion here only to ensure that any nested package
972 -- declaration that requires an implicit body gets one. (In the case
973 -- where a body is required, Check_Completion is called at the end of
974 -- the body's declarative part.)
976 if not Body_Required then
977 Check_Completion;
978 end if;
980 Comp_Unit := Nkind (Parent (N)) = N_Compilation_Unit;
981 if Comp_Unit then
983 -- Set Body_Required indication on the compilation unit node, and
984 -- determine whether elaboration warnings may be meaningful on it.
986 Set_Body_Required (Parent (N), Body_Required);
988 if not Body_Required then
989 Set_Suppress_Elaboration_Warnings (Id);
990 end if;
992 end if;
994 End_Package_Scope (Id);
996 -- For the declaration of a library unit that is a remote types package,
997 -- check legality rules regarding availability of stream attributes for
998 -- types that contain non-remote access values. This subprogram performs
999 -- visibility tests that rely on the fact that we have exited the scope
1000 -- of Id.
1002 if Comp_Unit then
1003 Validate_RT_RAT_Component (N);
1004 end if;
1006 if Debug_Flag_C then
1007 Outdent;
1008 Write_Str ("<== package spec ");
1009 Write_Name (Chars (Id));
1010 Write_Str (" from ");
1011 Write_Location (Sloc (N));
1012 Write_Eol;
1013 end if;
1014 end Analyze_Package_Declaration;
1016 -----------------------------------
1017 -- Analyze_Package_Specification --
1018 -----------------------------------
1020 -- Note that this code is shared for the analysis of generic package specs
1021 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
1023 procedure Analyze_Package_Specification (N : Node_Id) is
1024 Id : constant Entity_Id := Defining_Entity (N);
1025 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
1026 Vis_Decls : constant List_Id := Visible_Declarations (N);
1027 Priv_Decls : constant List_Id := Private_Declarations (N);
1028 E : Entity_Id;
1029 L : Entity_Id;
1030 Public_Child : Boolean;
1032 Private_With_Clauses_Installed : Boolean := False;
1033 -- In Ada 2005, private with_clauses are visible in the private part
1034 -- of a nested package, even if it appears in the public part of the
1035 -- enclosing package. This requires a separate step to install these
1036 -- private_with_clauses, and remove them at the end of the nested
1037 -- package.
1039 procedure Check_One_Tagged_Type_Or_Extension_At_Most;
1040 -- Issue an error in SPARK mode if a package specification contains
1041 -- more than one tagged type or type extension.
1043 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
1044 -- Clears constant indications (Never_Set_In_Source, Constant_Value, and
1045 -- Is_True_Constant) on all variables that are entities of Id, and on
1046 -- the chain whose first element is FE. A recursive call is made for all
1047 -- packages and generic packages.
1049 procedure Generate_Parent_References;
1050 -- For a child unit, generate references to parent units, for
1051 -- GPS navigation purposes.
1053 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
1054 -- Child and Unit are entities of compilation units. True if Child
1055 -- is a public child of Parent as defined in 10.1.1
1057 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
1058 -- Reject completion of an incomplete or private type declarations
1059 -- having a known discriminant part by an unchecked union.
1061 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
1062 -- Given the package entity of a generic package instantiation or
1063 -- formal package whose corresponding generic is a child unit, installs
1064 -- the private declarations of each of the child unit's parents.
1065 -- This has to be done at the point of entering the instance package's
1066 -- private part rather than being done in Sem_Ch12.Install_Parent
1067 -- (which is where the parents' visible declarations are installed).
1069 ------------------------------------------------
1070 -- Check_One_Tagged_Type_Or_Extension_At_Most --
1071 ------------------------------------------------
1073 procedure Check_One_Tagged_Type_Or_Extension_At_Most is
1074 Previous : Node_Id;
1076 procedure Check_Decls (Decls : List_Id);
1077 -- Check that either Previous is Empty and Decls does not contain
1078 -- more than one tagged type or type extension, or Previous is
1079 -- already set and Decls contains no tagged type or type extension.
1081 -----------------
1082 -- Check_Decls --
1083 -----------------
1085 procedure Check_Decls (Decls : List_Id) is
1086 Decl : Node_Id;
1088 begin
1089 Decl := First (Decls);
1090 while Present (Decl) loop
1091 if Nkind (Decl) = N_Full_Type_Declaration
1092 and then Is_Tagged_Type (Defining_Identifier (Decl))
1093 then
1094 if No (Previous) then
1095 Previous := Decl;
1097 else
1098 Error_Msg_Sloc := Sloc (Previous);
1099 Check_SPARK_Restriction
1100 ("at most one tagged type or type extension allowed",
1101 "\\ previous declaration#",
1102 Decl);
1103 end if;
1104 end if;
1106 Next (Decl);
1107 end loop;
1108 end Check_Decls;
1110 -- Start of processing for Check_One_Tagged_Type_Or_Extension_At_Most
1112 begin
1113 Previous := Empty;
1114 Check_Decls (Vis_Decls);
1116 if Present (Priv_Decls) then
1117 Check_Decls (Priv_Decls);
1118 end if;
1119 end Check_One_Tagged_Type_Or_Extension_At_Most;
1121 ---------------------
1122 -- Clear_Constants --
1123 ---------------------
1125 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
1126 E : Entity_Id;
1128 begin
1129 -- Ignore package renamings, not interesting and they can cause self
1130 -- referential loops in the code below.
1132 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
1133 return;
1134 end if;
1136 -- Note: in the loop below, the check for Next_Entity pointing back
1137 -- to the package entity may seem odd, but it is needed, because a
1138 -- package can contain a renaming declaration to itself, and such
1139 -- renamings are generated automatically within package instances.
1141 E := FE;
1142 while Present (E) and then E /= Id loop
1143 if Is_Assignable (E) then
1144 Set_Never_Set_In_Source (E, False);
1145 Set_Is_True_Constant (E, False);
1146 Set_Current_Value (E, Empty);
1147 Set_Is_Known_Null (E, False);
1148 Set_Last_Assignment (E, Empty);
1150 if not Can_Never_Be_Null (E) then
1151 Set_Is_Known_Non_Null (E, False);
1152 end if;
1154 elsif Is_Package_Or_Generic_Package (E) then
1155 Clear_Constants (E, First_Entity (E));
1156 Clear_Constants (E, First_Private_Entity (E));
1157 end if;
1159 Next_Entity (E);
1160 end loop;
1161 end Clear_Constants;
1163 --------------------------------
1164 -- Generate_Parent_References --
1165 --------------------------------
1167 procedure Generate_Parent_References is
1168 Decl : constant Node_Id := Parent (N);
1170 begin
1171 if Id = Cunit_Entity (Main_Unit)
1172 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
1173 then
1174 Generate_Reference (Id, Scope (Id), 'k', False);
1176 elsif not Nkind_In (Unit (Cunit (Main_Unit)), N_Subprogram_Body,
1177 N_Subunit)
1178 then
1179 -- If current unit is an ancestor of main unit, generate a
1180 -- reference to its own parent.
1182 declare
1183 U : Node_Id;
1184 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
1186 begin
1187 if Nkind (Main_Spec) = N_Package_Body then
1188 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
1189 end if;
1191 U := Parent_Spec (Main_Spec);
1192 while Present (U) loop
1193 if U = Parent (Decl) then
1194 Generate_Reference (Id, Scope (Id), 'k', False);
1195 exit;
1197 elsif Nkind (Unit (U)) = N_Package_Body then
1198 exit;
1200 else
1201 U := Parent_Spec (Unit (U));
1202 end if;
1203 end loop;
1204 end;
1205 end if;
1206 end Generate_Parent_References;
1208 ---------------------
1209 -- Is_Public_Child --
1210 ---------------------
1212 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
1213 begin
1214 if not Is_Private_Descendant (Child) then
1215 return True;
1216 else
1217 if Child = Unit then
1218 return not Private_Present (
1219 Parent (Unit_Declaration_Node (Child)));
1220 else
1221 return Is_Public_Child (Scope (Child), Unit);
1222 end if;
1223 end if;
1224 end Is_Public_Child;
1226 ----------------------------------------
1227 -- Inspect_Unchecked_Union_Completion --
1228 ----------------------------------------
1230 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
1231 Decl : Node_Id;
1233 begin
1234 Decl := First (Decls);
1235 while Present (Decl) loop
1237 -- We are looking at an incomplete or private type declaration
1238 -- with a known_discriminant_part whose full view is an
1239 -- Unchecked_Union.
1241 if Nkind_In (Decl, N_Incomplete_Type_Declaration,
1242 N_Private_Type_Declaration)
1243 and then Has_Discriminants (Defining_Identifier (Decl))
1244 and then Present (Full_View (Defining_Identifier (Decl)))
1245 and then
1246 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
1247 then
1248 Error_Msg_N
1249 ("completion of discriminated partial view "
1250 & "cannot be an unchecked union",
1251 Full_View (Defining_Identifier (Decl)));
1252 end if;
1254 Next (Decl);
1255 end loop;
1256 end Inspect_Unchecked_Union_Completion;
1258 -----------------------------------------
1259 -- Install_Parent_Private_Declarations --
1260 -----------------------------------------
1262 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
1263 Inst_Par : Entity_Id;
1264 Gen_Par : Entity_Id;
1265 Inst_Node : Node_Id;
1267 begin
1268 Inst_Par := Inst_Id;
1270 Gen_Par :=
1271 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
1272 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
1273 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
1275 if Nkind_In (Inst_Node, N_Package_Instantiation,
1276 N_Formal_Package_Declaration)
1277 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
1278 then
1279 Inst_Par := Entity (Prefix (Name (Inst_Node)));
1281 if Present (Renamed_Entity (Inst_Par)) then
1282 Inst_Par := Renamed_Entity (Inst_Par);
1283 end if;
1285 Gen_Par :=
1286 Generic_Parent
1287 (Specification (Unit_Declaration_Node (Inst_Par)));
1289 -- Install the private declarations and private use clauses
1290 -- of a parent instance of the child instance, unless the
1291 -- parent instance private declarations have already been
1292 -- installed earlier in Analyze_Package_Specification, which
1293 -- happens when a generic child is instantiated, and the
1294 -- instance is a child of the parent instance.
1296 -- Installing the use clauses of the parent instance twice
1297 -- is both unnecessary and wrong, because it would cause the
1298 -- clauses to be chained to themselves in the use clauses
1299 -- list of the scope stack entry. That in turn would cause
1300 -- an endless loop from End_Use_Clauses upon scope exit.
1302 -- The parent is now fully visible. It may be a hidden open
1303 -- scope if we are currently compiling some child instance
1304 -- declared within it, but while the current instance is being
1305 -- compiled the parent is immediately visible. In particular
1306 -- its entities must remain visible if a stack save/restore
1307 -- takes place through a call to Rtsfind.
1309 if Present (Gen_Par) then
1310 if not In_Private_Part (Inst_Par) then
1311 Install_Private_Declarations (Inst_Par);
1312 Set_Use (Private_Declarations
1313 (Specification
1314 (Unit_Declaration_Node (Inst_Par))));
1315 Set_Is_Hidden_Open_Scope (Inst_Par, False);
1316 end if;
1318 -- If we've reached the end of the generic instance parents,
1319 -- then finish off by looping through the nongeneric parents
1320 -- and installing their private declarations.
1322 -- If one of the non-generic parents is itself on the scope
1323 -- stack, do not install its private declarations: they are
1324 -- installed in due time when the private part of that parent
1325 -- is analyzed. This is delicate ???
1327 else
1328 while Present (Inst_Par)
1329 and then Inst_Par /= Standard_Standard
1330 and then (not In_Open_Scopes (Inst_Par)
1331 or else not In_Private_Part (Inst_Par))
1332 loop
1333 Install_Private_Declarations (Inst_Par);
1334 Set_Use (Private_Declarations
1335 (Specification
1336 (Unit_Declaration_Node (Inst_Par))));
1337 Inst_Par := Scope (Inst_Par);
1338 end loop;
1340 exit;
1341 end if;
1343 else
1344 exit;
1345 end if;
1346 end loop;
1347 end Install_Parent_Private_Declarations;
1349 -- Start of processing for Analyze_Package_Specification
1351 begin
1352 if Present (Vis_Decls) then
1353 Analyze_Declarations (Vis_Decls);
1354 end if;
1356 -- Verify that incomplete types have received full declarations and
1357 -- also build invariant procedures for any types with invariants.
1359 E := First_Entity (Id);
1360 while Present (E) loop
1362 -- Check on incomplete types
1364 -- AI05-0213: A formal incomplete type has no completion
1366 if Ekind (E) = E_Incomplete_Type
1367 and then No (Full_View (E))
1368 and then not Is_Generic_Type (E)
1369 then
1370 Error_Msg_N ("no declaration in visible part for incomplete}", E);
1371 end if;
1373 -- Build invariant procedures
1375 if Is_Type (E) and then Has_Invariants (E) then
1376 Build_Invariant_Procedure (E, N);
1377 end if;
1379 Next_Entity (E);
1380 end loop;
1382 if Is_Remote_Call_Interface (Id)
1383 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1384 then
1385 Validate_RCI_Declarations (Id);
1386 end if;
1388 -- Save global references in the visible declarations, before installing
1389 -- private declarations of parent unit if there is one, because the
1390 -- privacy status of types defined in the parent will change. This is
1391 -- only relevant for generic child units, but is done in all cases for
1392 -- uniformity.
1394 if Ekind (Id) = E_Generic_Package
1395 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1396 then
1397 declare
1398 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1399 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1400 begin
1401 Set_Private_Declarations (Orig_Spec, Empty_List);
1402 Save_Global_References (Orig_Decl);
1403 Set_Private_Declarations (Orig_Spec, Save_Priv);
1404 end;
1405 end if;
1407 -- If package is a public child unit, then make the private declarations
1408 -- of the parent visible.
1410 Public_Child := False;
1412 declare
1413 Par : Entity_Id;
1414 Pack_Decl : Node_Id;
1415 Par_Spec : Node_Id;
1417 begin
1418 Par := Id;
1419 Par_Spec := Parent_Spec (Parent (N));
1421 -- If the package is formal package of an enclosing generic, it is
1422 -- transformed into a local generic declaration, and compiled to make
1423 -- its spec available. We need to retrieve the original generic to
1424 -- determine whether it is a child unit, and install its parents.
1426 if No (Par_Spec)
1427 and then
1428 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1429 then
1430 Par := Entity (Name (Original_Node (Parent (N))));
1431 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1432 end if;
1434 if Present (Par_Spec) then
1435 Generate_Parent_References;
1437 while Scope (Par) /= Standard_Standard
1438 and then Is_Public_Child (Id, Par)
1439 and then In_Open_Scopes (Par)
1440 loop
1441 Public_Child := True;
1442 Par := Scope (Par);
1443 Install_Private_Declarations (Par);
1444 Install_Private_With_Clauses (Par);
1445 Pack_Decl := Unit_Declaration_Node (Par);
1446 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1447 end loop;
1448 end if;
1449 end;
1451 if Is_Compilation_Unit (Id) then
1452 Install_Private_With_Clauses (Id);
1453 else
1455 -- The current compilation unit may include private with_clauses,
1456 -- which are visible in the private part of the current nested
1457 -- package, and have to be installed now. This is not done for
1458 -- nested instantiations, where the private with_clauses of the
1459 -- enclosing unit have no effect once the instantiation info is
1460 -- established and we start analyzing the package declaration.
1462 declare
1463 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1464 begin
1465 if Is_Package_Or_Generic_Package (Comp_Unit)
1466 and then not In_Private_Part (Comp_Unit)
1467 and then not In_Instance
1468 then
1469 Install_Private_With_Clauses (Comp_Unit);
1470 Private_With_Clauses_Installed := True;
1471 end if;
1472 end;
1473 end if;
1475 -- If this is a package associated with a generic instance or formal
1476 -- package, then the private declarations of each of the generic's
1477 -- parents must be installed at this point.
1479 if Is_Generic_Instance (Id) then
1480 Install_Parent_Private_Declarations (Id);
1481 end if;
1483 -- Analyze private part if present. The flag In_Private_Part is reset
1484 -- in End_Package_Scope.
1486 L := Last_Entity (Id);
1488 if Present (Priv_Decls) then
1489 Set_In_Private_Part (Id);
1491 -- Upon entering a public child's private part, it may be necessary
1492 -- to declare subprograms that were derived in the package's visible
1493 -- part but not yet made visible.
1495 if Public_Child then
1496 Declare_Inherited_Private_Subprograms (Id);
1497 end if;
1499 Analyze_Declarations (Priv_Decls);
1501 -- Check the private declarations for incomplete deferred constants
1503 Inspect_Deferred_Constant_Completion (Priv_Decls);
1505 -- The first private entity is the immediate follower of the last
1506 -- visible entity, if there was one.
1508 if Present (L) then
1509 Set_First_Private_Entity (Id, Next_Entity (L));
1510 else
1511 Set_First_Private_Entity (Id, First_Entity (Id));
1512 end if;
1514 -- There may be inherited private subprograms that need to be declared,
1515 -- even in the absence of an explicit private part. If there are any
1516 -- public declarations in the package and the package is a public child
1517 -- unit, then an implicit private part is assumed.
1519 elsif Present (L) and then Public_Child then
1520 Set_In_Private_Part (Id);
1521 Declare_Inherited_Private_Subprograms (Id);
1522 Set_First_Private_Entity (Id, Next_Entity (L));
1523 end if;
1525 E := First_Entity (Id);
1526 while Present (E) loop
1528 -- Check rule of 3.6(11), which in general requires waiting till all
1529 -- full types have been seen.
1531 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1532 Check_Aliased_Component_Types (E);
1533 end if;
1535 -- Check preelaborable initialization for full type completing a
1536 -- private type for which pragma Preelaborable_Initialization given.
1538 if Is_Type (E)
1539 and then Must_Have_Preelab_Init (E)
1540 and then not Has_Preelaborable_Initialization (E)
1541 then
1542 Error_Msg_N
1543 ("full view of & does not have preelaborable initialization", E);
1544 end if;
1546 -- An invariant may appear on a full view of a type
1548 if Is_Type (E)
1549 and then Has_Private_Declaration (E)
1550 and then Nkind (Parent (E)) = N_Full_Type_Declaration
1551 and then Has_Aspects (Parent (E))
1552 then
1553 declare
1554 ASN : Node_Id;
1556 begin
1557 ASN := First (Aspect_Specifications (Parent (E)));
1558 while Present (ASN) loop
1559 if Nam_In (Chars (Identifier (ASN)), Name_Invariant,
1560 Name_Type_Invariant)
1561 then
1562 Build_Invariant_Procedure (E, N);
1563 exit;
1564 end if;
1566 Next (ASN);
1567 end loop;
1568 end;
1569 end if;
1571 Next_Entity (E);
1572 end loop;
1574 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1575 -- declaration having a known_discriminant_part shall not be an
1576 -- unchecked union type.
1578 if Present (Vis_Decls) then
1579 Inspect_Unchecked_Union_Completion (Vis_Decls);
1580 end if;
1582 if Present (Priv_Decls) then
1583 Inspect_Unchecked_Union_Completion (Priv_Decls);
1584 end if;
1586 if Ekind (Id) = E_Generic_Package
1587 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1588 and then Present (Priv_Decls)
1589 then
1590 -- Save global references in private declarations, ignoring the
1591 -- visible declarations that were processed earlier.
1593 declare
1594 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1595 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1596 Save_Form : constant List_Id :=
1597 Generic_Formal_Declarations (Orig_Decl);
1599 begin
1600 Set_Visible_Declarations (Orig_Spec, Empty_List);
1601 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1602 Save_Global_References (Orig_Decl);
1603 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1604 Set_Visible_Declarations (Orig_Spec, Save_Vis);
1605 end;
1606 end if;
1608 Process_End_Label (N, 'e', Id);
1610 -- Remove private_with_clauses of enclosing compilation unit, if they
1611 -- were installed.
1613 if Private_With_Clauses_Installed then
1614 Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
1615 end if;
1617 -- For the case of a library level package, we must go through all the
1618 -- entities clearing the indications that the value may be constant and
1619 -- not modified. Why? Because any client of this package may modify
1620 -- these values freely from anywhere. This also applies to any nested
1621 -- packages or generic packages.
1623 -- For now we unconditionally clear constants for packages that are
1624 -- instances of generic packages. The reason is that we do not have the
1625 -- body yet, and we otherwise think things are unreferenced when they
1626 -- are not. This should be fixed sometime (the effect is not terrible,
1627 -- we just lose some warnings, and also some cases of value propagation)
1628 -- ???
1630 if Is_Library_Level_Entity (Id)
1631 or else Is_Generic_Instance (Id)
1632 then
1633 Clear_Constants (Id, First_Entity (Id));
1634 Clear_Constants (Id, First_Private_Entity (Id));
1635 end if;
1637 -- Issue an error in SPARK mode if a package specification contains
1638 -- more than one tagged type or type extension.
1640 Check_One_Tagged_Type_Or_Extension_At_Most;
1642 -- If switch set, output information on why body required
1644 if List_Body_Required_Info
1645 and then In_Extended_Main_Source_Unit (Id)
1646 and then Unit_Requires_Body (Id)
1647 then
1648 Unit_Requires_Body_Info (Id);
1649 end if;
1650 end Analyze_Package_Specification;
1652 --------------------------------------
1653 -- Analyze_Private_Type_Declaration --
1654 --------------------------------------
1656 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1657 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
1658 Id : constant Entity_Id := Defining_Identifier (N);
1660 begin
1661 Generate_Definition (Id);
1662 Set_Is_Pure (Id, PF);
1663 Init_Size_Align (Id);
1665 if not Is_Package_Or_Generic_Package (Current_Scope)
1666 or else In_Private_Part (Current_Scope)
1667 then
1668 Error_Msg_N ("invalid context for private declaration", N);
1669 end if;
1671 New_Private_Type (N, Id, N);
1672 Set_Depends_On_Private (Id);
1674 if Has_Aspects (N) then
1675 Analyze_Aspect_Specifications (N, Id);
1676 end if;
1677 end Analyze_Private_Type_Declaration;
1679 ----------------------------------
1680 -- Check_Anonymous_Access_Types --
1681 ----------------------------------
1683 procedure Check_Anonymous_Access_Types
1684 (Spec_Id : Entity_Id;
1685 P_Body : Node_Id)
1687 E : Entity_Id;
1688 IR : Node_Id;
1690 begin
1691 -- Itype references are only needed by gigi, to force elaboration of
1692 -- itypes. In the absence of code generation, they are not needed.
1694 if not Expander_Active then
1695 return;
1696 end if;
1698 E := First_Entity (Spec_Id);
1699 while Present (E) loop
1700 if Ekind (E) = E_Anonymous_Access_Type
1701 and then From_Limited_With (E)
1702 then
1703 IR := Make_Itype_Reference (Sloc (P_Body));
1704 Set_Itype (IR, E);
1706 if No (Declarations (P_Body)) then
1707 Set_Declarations (P_Body, New_List (IR));
1708 else
1709 Prepend (IR, Declarations (P_Body));
1710 end if;
1711 end if;
1713 Next_Entity (E);
1714 end loop;
1715 end Check_Anonymous_Access_Types;
1717 -------------------------------------------
1718 -- Declare_Inherited_Private_Subprograms --
1719 -------------------------------------------
1721 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1723 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1724 -- Check whether an inherited subprogram S is an operation of an
1725 -- untagged derived type T.
1727 ---------------------
1728 -- Is_Primitive_Of --
1729 ---------------------
1731 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1732 Formal : Entity_Id;
1734 begin
1735 -- If the full view is a scalar type, the type is the anonymous base
1736 -- type, but the operation mentions the first subtype, so check the
1737 -- signature against the base type.
1739 if Base_Type (Etype (S)) = Base_Type (T) then
1740 return True;
1742 else
1743 Formal := First_Formal (S);
1744 while Present (Formal) loop
1745 if Base_Type (Etype (Formal)) = Base_Type (T) then
1746 return True;
1747 end if;
1749 Next_Formal (Formal);
1750 end loop;
1752 return False;
1753 end if;
1754 end Is_Primitive_Of;
1756 -- Local variables
1758 E : Entity_Id;
1759 Op_List : Elist_Id;
1760 Op_Elmt : Elmt_Id;
1761 Op_Elmt_2 : Elmt_Id;
1762 Prim_Op : Entity_Id;
1763 New_Op : Entity_Id := Empty;
1764 Parent_Subp : Entity_Id;
1765 Tag : Entity_Id;
1767 -- Start of processing for Declare_Inherited_Private_Subprograms
1769 begin
1770 E := First_Entity (Id);
1771 while Present (E) loop
1773 -- If the entity is a nonprivate type extension whose parent type
1774 -- is declared in an open scope, then the type may have inherited
1775 -- operations that now need to be made visible. Ditto if the entity
1776 -- is a formal derived type in a child unit.
1778 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
1779 or else
1780 (Nkind (Parent (E)) = N_Private_Extension_Declaration
1781 and then Is_Generic_Type (E)))
1782 and then In_Open_Scopes (Scope (Etype (E)))
1783 and then Is_Base_Type (E)
1784 then
1785 if Is_Tagged_Type (E) then
1786 Op_List := Primitive_Operations (E);
1787 New_Op := Empty;
1788 Tag := First_Tag_Component (E);
1790 Op_Elmt := First_Elmt (Op_List);
1791 while Present (Op_Elmt) loop
1792 Prim_Op := Node (Op_Elmt);
1794 -- Search primitives that are implicit operations with an
1795 -- internal name whose parent operation has a normal name.
1797 if Present (Alias (Prim_Op))
1798 and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
1799 and then not Comes_From_Source (Prim_Op)
1800 and then Is_Internal_Name (Chars (Prim_Op))
1801 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1802 then
1803 Parent_Subp := Alias (Prim_Op);
1805 -- Case 1: Check if the type has also an explicit
1806 -- overriding for this primitive.
1808 Op_Elmt_2 := Next_Elmt (Op_Elmt);
1809 while Present (Op_Elmt_2) loop
1811 -- Skip entities with attribute Interface_Alias since
1812 -- they are not overriding primitives (these entities
1813 -- link an interface primitive with their covering
1814 -- primitive)
1816 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
1817 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
1818 and then No (Interface_Alias (Node (Op_Elmt_2)))
1819 then
1820 -- The private inherited operation has been
1821 -- overridden by an explicit subprogram:
1822 -- replace the former by the latter.
1824 New_Op := Node (Op_Elmt_2);
1825 Replace_Elmt (Op_Elmt, New_Op);
1826 Remove_Elmt (Op_List, Op_Elmt_2);
1827 Set_Overridden_Operation (New_Op, Parent_Subp);
1829 -- We don't need to inherit its dispatching slot.
1830 -- Set_All_DT_Position has previously ensured that
1831 -- the same slot was assigned to the two primitives
1833 if Present (Tag)
1834 and then Present (DTC_Entity (New_Op))
1835 and then Present (DTC_Entity (Prim_Op))
1836 then
1837 pragma Assert
1838 (DT_Position (New_Op) = DT_Position (Prim_Op));
1839 null;
1840 end if;
1842 goto Next_Primitive;
1843 end if;
1845 Next_Elmt (Op_Elmt_2);
1846 end loop;
1848 -- Case 2: We have not found any explicit overriding and
1849 -- hence we need to declare the operation (i.e., make it
1850 -- visible).
1852 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1854 -- Inherit the dispatching slot if E is already frozen
1856 if Is_Frozen (E)
1857 and then Present (DTC_Entity (Alias (Prim_Op)))
1858 then
1859 Set_DTC_Entity_Value (E, New_Op);
1860 Set_DT_Position (New_Op,
1861 DT_Position (Alias (Prim_Op)));
1862 end if;
1864 pragma Assert
1865 (Is_Dispatching_Operation (New_Op)
1866 and then Node (Last_Elmt (Op_List)) = New_Op);
1868 -- Substitute the new operation for the old one in the
1869 -- type's primitive operations list. Since the new
1870 -- operation was also just added to the end of list,
1871 -- the last element must be removed.
1873 -- (Question: is there a simpler way of declaring the
1874 -- operation, say by just replacing the name of the
1875 -- earlier operation, reentering it in the in the symbol
1876 -- table (how?), and marking it as private???)
1878 Replace_Elmt (Op_Elmt, New_Op);
1879 Remove_Last_Elmt (Op_List);
1880 end if;
1882 <<Next_Primitive>>
1883 Next_Elmt (Op_Elmt);
1884 end loop;
1886 -- Generate listing showing the contents of the dispatch table
1888 if Debug_Flag_ZZ then
1889 Write_DT (E);
1890 end if;
1892 else
1893 -- Non-tagged type, scan forward to locate inherited hidden
1894 -- operations.
1896 Prim_Op := Next_Entity (E);
1897 while Present (Prim_Op) loop
1898 if Is_Subprogram (Prim_Op)
1899 and then Present (Alias (Prim_Op))
1900 and then not Comes_From_Source (Prim_Op)
1901 and then Is_Internal_Name (Chars (Prim_Op))
1902 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1903 and then Is_Primitive_Of (E, Prim_Op)
1904 then
1905 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1906 end if;
1908 Next_Entity (Prim_Op);
1910 -- Derived operations appear immediately after the type
1911 -- declaration (or the following subtype indication for
1912 -- a derived scalar type). Further declarations cannot
1913 -- include inherited operations of the type.
1915 if Present (Prim_Op) then
1916 exit when Ekind (Prim_Op) not in Overloadable_Kind;
1917 end if;
1918 end loop;
1919 end if;
1920 end if;
1922 Next_Entity (E);
1923 end loop;
1924 end Declare_Inherited_Private_Subprograms;
1926 -----------------------
1927 -- End_Package_Scope --
1928 -----------------------
1930 procedure End_Package_Scope (P : Entity_Id) is
1931 begin
1932 Uninstall_Declarations (P);
1933 Pop_Scope;
1934 end End_Package_Scope;
1936 ---------------------------
1937 -- Exchange_Declarations --
1938 ---------------------------
1940 procedure Exchange_Declarations (Id : Entity_Id) is
1941 Full_Id : constant Entity_Id := Full_View (Id);
1942 H1 : constant Entity_Id := Homonym (Id);
1943 Next1 : constant Entity_Id := Next_Entity (Id);
1944 H2 : Entity_Id;
1945 Next2 : Entity_Id;
1947 begin
1948 -- If missing full declaration for type, nothing to exchange
1950 if No (Full_Id) then
1951 return;
1952 end if;
1954 -- Otherwise complete the exchange, and preserve semantic links
1956 Next2 := Next_Entity (Full_Id);
1957 H2 := Homonym (Full_Id);
1959 -- Reset full declaration pointer to reflect the switched entities and
1960 -- readjust the next entity chains.
1962 Exchange_Entities (Id, Full_Id);
1964 Set_Next_Entity (Id, Next1);
1965 Set_Homonym (Id, H1);
1967 Set_Full_View (Full_Id, Id);
1968 Set_Next_Entity (Full_Id, Next2);
1969 Set_Homonym (Full_Id, H2);
1970 end Exchange_Declarations;
1972 ----------------------------
1973 -- Install_Package_Entity --
1974 ----------------------------
1976 procedure Install_Package_Entity (Id : Entity_Id) is
1977 begin
1978 if not Is_Internal (Id) then
1979 if Debug_Flag_E then
1980 Write_Str ("Install: ");
1981 Write_Name (Chars (Id));
1982 Write_Eol;
1983 end if;
1985 if not Is_Child_Unit (Id) then
1986 Set_Is_Immediately_Visible (Id);
1987 end if;
1989 end if;
1990 end Install_Package_Entity;
1992 ----------------------------------
1993 -- Install_Private_Declarations --
1994 ----------------------------------
1996 procedure Install_Private_Declarations (P : Entity_Id) is
1997 Id : Entity_Id;
1998 Full : Entity_Id;
1999 Priv_Deps : Elist_Id;
2001 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
2002 -- When the full view of a private type is made available, we do the
2003 -- same for its private dependents under proper visibility conditions.
2004 -- When compiling a grand-chid unit this needs to be done recursively.
2006 -----------------------------
2007 -- Swap_Private_Dependents --
2008 -----------------------------
2010 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
2011 Deps : Elist_Id;
2012 Priv : Entity_Id;
2013 Priv_Elmt : Elmt_Id;
2014 Is_Priv : Boolean;
2016 begin
2017 Priv_Elmt := First_Elmt (Priv_Deps);
2018 while Present (Priv_Elmt) loop
2019 Priv := Node (Priv_Elmt);
2021 -- Before the exchange, verify that the presence of the Full_View
2022 -- field. This field will be empty if the entity has already been
2023 -- installed due to a previous call.
2025 if Present (Full_View (Priv))
2026 and then Is_Visible_Dependent (Priv)
2027 then
2028 if Is_Private_Type (Priv) then
2029 Deps := Private_Dependents (Priv);
2030 Is_Priv := True;
2031 else
2032 Is_Priv := False;
2033 end if;
2035 -- For each subtype that is swapped, we also swap the reference
2036 -- to it in Private_Dependents, to allow access to it when we
2037 -- swap them out in End_Package_Scope.
2039 Replace_Elmt (Priv_Elmt, Full_View (Priv));
2040 Exchange_Declarations (Priv);
2041 Set_Is_Immediately_Visible
2042 (Priv, In_Open_Scopes (Scope (Priv)));
2043 Set_Is_Potentially_Use_Visible
2044 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
2046 -- Within a child unit, recurse, except in generic child unit,
2047 -- which (unfortunately) handle private_dependents separately.
2049 if Is_Priv
2050 and then Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
2051 and then not Is_Empty_Elmt_List (Deps)
2052 and then not Inside_A_Generic
2053 then
2054 Swap_Private_Dependents (Deps);
2055 end if;
2056 end if;
2058 Next_Elmt (Priv_Elmt);
2059 end loop;
2060 end Swap_Private_Dependents;
2062 -- Start of processing for Install_Private_Declarations
2064 begin
2065 -- First exchange declarations for private types, so that the full
2066 -- declaration is visible. For each private type, we check its
2067 -- Private_Dependents list and also exchange any subtypes of or derived
2068 -- types from it. Finally, if this is a Taft amendment type, the
2069 -- incomplete declaration is irrelevant, and we want to link the
2070 -- eventual full declaration with the original private one so we
2071 -- also skip the exchange.
2073 Id := First_Entity (P);
2074 while Present (Id) and then Id /= First_Private_Entity (P) loop
2075 if Is_Private_Base_Type (Id)
2076 and then Comes_From_Source (Full_View (Id))
2077 and then Present (Full_View (Id))
2078 and then Scope (Full_View (Id)) = Scope (Id)
2079 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
2080 then
2081 -- If there is a use-type clause on the private type, set the full
2082 -- view accordingly.
2084 Set_In_Use (Full_View (Id), In_Use (Id));
2085 Full := Full_View (Id);
2087 if Is_Private_Base_Type (Full)
2088 and then Has_Private_Declaration (Full)
2089 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
2090 and then In_Open_Scopes (Scope (Etype (Full)))
2091 and then In_Package_Body (Current_Scope)
2092 and then not Is_Private_Type (Etype (Full))
2093 then
2094 -- This is the completion of a private type by a derivation
2095 -- from another private type which is not private anymore. This
2096 -- can only happen in a package nested within a child package,
2097 -- when the parent type is defined in the parent unit. At this
2098 -- point the current type is not private either, and we have
2099 -- to install the underlying full view, which is now visible.
2100 -- Save the current full view as well, so that all views can be
2101 -- restored on exit. It may seem that after compiling the child
2102 -- body there are not environments to restore, but the back-end
2103 -- expects those links to be valid, and freeze nodes depend on
2104 -- them.
2106 if No (Full_View (Full))
2107 and then Present (Underlying_Full_View (Full))
2108 then
2109 Set_Full_View (Id, Underlying_Full_View (Full));
2110 Set_Underlying_Full_View (Id, Full);
2112 Set_Underlying_Full_View (Full, Empty);
2113 Set_Is_Frozen (Full_View (Id));
2114 end if;
2115 end if;
2117 Priv_Deps := Private_Dependents (Id);
2118 Exchange_Declarations (Id);
2119 Set_Is_Immediately_Visible (Id);
2120 Swap_Private_Dependents (Priv_Deps);
2121 end if;
2123 Next_Entity (Id);
2124 end loop;
2126 -- Next make other declarations in the private part visible as well
2128 Id := First_Private_Entity (P);
2129 while Present (Id) loop
2130 Install_Package_Entity (Id);
2131 Set_Is_Hidden (Id, False);
2132 Next_Entity (Id);
2133 end loop;
2135 -- Indicate that the private part is currently visible, so it can be
2136 -- properly reset on exit.
2138 Set_In_Private_Part (P);
2139 end Install_Private_Declarations;
2141 ----------------------------------
2142 -- Install_Visible_Declarations --
2143 ----------------------------------
2145 procedure Install_Visible_Declarations (P : Entity_Id) is
2146 Id : Entity_Id;
2147 Last_Entity : Entity_Id;
2149 begin
2150 pragma Assert
2151 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
2153 if Is_Package_Or_Generic_Package (P) then
2154 Last_Entity := First_Private_Entity (P);
2155 else
2156 Last_Entity := Empty;
2157 end if;
2159 Id := First_Entity (P);
2160 while Present (Id) and then Id /= Last_Entity loop
2161 Install_Package_Entity (Id);
2162 Next_Entity (Id);
2163 end loop;
2164 end Install_Visible_Declarations;
2166 --------------------------
2167 -- Is_Private_Base_Type --
2168 --------------------------
2170 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
2171 begin
2172 return Ekind (E) = E_Private_Type
2173 or else Ekind (E) = E_Limited_Private_Type
2174 or else Ekind (E) = E_Record_Type_With_Private;
2175 end Is_Private_Base_Type;
2177 --------------------------
2178 -- Is_Visible_Dependent --
2179 --------------------------
2181 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
2183 S : constant Entity_Id := Scope (Dep);
2185 begin
2186 -- Renamings created for actual types have the visibility of the actual
2188 if Ekind (S) = E_Package
2189 and then Is_Generic_Instance (S)
2190 and then (Is_Generic_Actual_Type (Dep)
2191 or else Is_Generic_Actual_Type (Full_View (Dep)))
2192 then
2193 return True;
2195 elsif not (Is_Derived_Type (Dep))
2196 and then Is_Derived_Type (Full_View (Dep))
2197 then
2198 -- When instantiating a package body, the scope stack is empty, so
2199 -- check instead whether the dependent type is defined in the same
2200 -- scope as the instance itself.
2202 return In_Open_Scopes (S)
2203 or else (Is_Generic_Instance (Current_Scope)
2204 and then Scope (Dep) = Scope (Current_Scope));
2205 else
2206 return True;
2207 end if;
2208 end Is_Visible_Dependent;
2210 ----------------------------
2211 -- May_Need_Implicit_Body --
2212 ----------------------------
2214 procedure May_Need_Implicit_Body (E : Entity_Id) is
2215 P : constant Node_Id := Unit_Declaration_Node (E);
2216 S : constant Node_Id := Parent (P);
2217 B : Node_Id;
2218 Decls : List_Id;
2220 begin
2221 if not Has_Completion (E)
2222 and then Nkind (P) = N_Package_Declaration
2223 and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
2224 then
2225 B :=
2226 Make_Package_Body (Sloc (E),
2227 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
2228 Chars => Chars (E)),
2229 Declarations => New_List);
2231 if Nkind (S) = N_Package_Specification then
2232 if Present (Private_Declarations (S)) then
2233 Decls := Private_Declarations (S);
2234 else
2235 Decls := Visible_Declarations (S);
2236 end if;
2237 else
2238 Decls := Declarations (S);
2239 end if;
2241 Append (B, Decls);
2242 Analyze (B);
2243 end if;
2244 end May_Need_Implicit_Body;
2246 ----------------------
2247 -- New_Private_Type --
2248 ----------------------
2250 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
2251 begin
2252 -- For other than Ada 2012, enter the name in the current scope
2254 if Ada_Version < Ada_2012 then
2255 Enter_Name (Id);
2257 -- Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2258 -- there may be an incomplete previous view.
2260 else
2261 declare
2262 Prev : Entity_Id;
2263 begin
2264 Prev := Find_Type_Name (N);
2265 pragma Assert (Prev = Id
2266 or else (Ekind (Prev) = E_Incomplete_Type
2267 and then Present (Full_View (Prev))
2268 and then Full_View (Prev) = Id));
2269 end;
2270 end if;
2272 if Limited_Present (Def) then
2273 Set_Ekind (Id, E_Limited_Private_Type);
2274 else
2275 Set_Ekind (Id, E_Private_Type);
2276 end if;
2278 Set_Etype (Id, Id);
2279 Set_Has_Delayed_Freeze (Id);
2280 Set_Is_First_Subtype (Id);
2281 Init_Size_Align (Id);
2283 Set_Is_Constrained (Id,
2284 No (Discriminant_Specifications (N))
2285 and then not Unknown_Discriminants_Present (N));
2287 -- Set tagged flag before processing discriminants, to catch illegal
2288 -- usage.
2290 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
2292 Set_Discriminant_Constraint (Id, No_Elist);
2293 Set_Stored_Constraint (Id, No_Elist);
2295 if Present (Discriminant_Specifications (N)) then
2296 Push_Scope (Id);
2297 Process_Discriminants (N);
2298 End_Scope;
2300 elsif Unknown_Discriminants_Present (N) then
2301 Set_Has_Unknown_Discriminants (Id);
2302 end if;
2304 Set_Private_Dependents (Id, New_Elmt_List);
2306 if Tagged_Present (Def) then
2307 Set_Ekind (Id, E_Record_Type_With_Private);
2308 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2309 Set_Is_Abstract_Type (Id, Abstract_Present (Def));
2310 Set_Is_Limited_Record (Id, Limited_Present (Def));
2311 Set_Has_Delayed_Freeze (Id, True);
2313 -- Create a class-wide type with the same attributes
2315 Make_Class_Wide_Type (Id);
2317 elsif Abstract_Present (Def) then
2318 Error_Msg_N ("only a tagged type can be abstract", N);
2319 end if;
2320 end New_Private_Type;
2322 ----------------------------
2323 -- Uninstall_Declarations --
2324 ----------------------------
2326 procedure Uninstall_Declarations (P : Entity_Id) is
2327 Decl : constant Node_Id := Unit_Declaration_Node (P);
2328 Id : Entity_Id;
2329 Full : Entity_Id;
2330 Priv_Elmt : Elmt_Id;
2331 Priv_Sub : Entity_Id;
2333 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id);
2334 -- Copy to the private declaration the attributes of the full view that
2335 -- need to be available for the partial view also.
2337 function Type_In_Use (T : Entity_Id) return Boolean;
2338 -- Check whether type or base type appear in an active use_type clause
2340 ------------------------------
2341 -- Preserve_Full_Attributes --
2342 ------------------------------
2344 procedure Preserve_Full_Attributes (Priv, Full : Entity_Id) is
2345 Priv_Is_Base_Type : constant Boolean := Is_Base_Type (Priv);
2347 begin
2348 Set_Size_Info (Priv, (Full));
2349 Set_RM_Size (Priv, RM_Size (Full));
2350 Set_Size_Known_At_Compile_Time
2351 (Priv, Size_Known_At_Compile_Time (Full));
2352 Set_Is_Volatile (Priv, Is_Volatile (Full));
2353 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
2354 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full));
2355 Set_Is_Ada_2012_Only (Priv, Is_Ada_2012_Only (Full));
2356 Set_Has_Pragma_Unmodified (Priv, Has_Pragma_Unmodified (Full));
2357 Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced (Full));
2358 Set_Has_Pragma_Unreferenced_Objects
2359 (Priv, Has_Pragma_Unreferenced_Objects
2360 (Full));
2361 if Is_Unchecked_Union (Full) then
2362 Set_Is_Unchecked_Union (Base_Type (Priv));
2363 end if;
2364 -- Why is atomic not copied here ???
2366 if Referenced (Full) then
2367 Set_Referenced (Priv);
2368 end if;
2370 if Priv_Is_Base_Type then
2371 Set_Is_Controlled (Priv, Is_Controlled (Base_Type (Full)));
2372 Set_Finalize_Storage_Only
2373 (Priv, Finalize_Storage_Only
2374 (Base_Type (Full)));
2375 Set_Has_Task (Priv, Has_Task (Base_Type (Full)));
2376 Set_Has_Protected (Priv, Has_Protected (Base_Type (Full)));
2377 Set_Has_Controlled_Component
2378 (Priv, Has_Controlled_Component
2379 (Base_Type (Full)));
2380 end if;
2382 Set_Freeze_Node (Priv, Freeze_Node (Full));
2384 -- Propagate information of type invariants, which may be specified
2385 -- for the full view.
2387 if Has_Invariants (Full) and not Has_Invariants (Priv) then
2388 Set_Has_Invariants (Priv);
2389 Set_Subprograms_For_Type (Priv, Subprograms_For_Type (Full));
2390 end if;
2392 if Is_Tagged_Type (Priv)
2393 and then Is_Tagged_Type (Full)
2394 and then not Error_Posted (Full)
2395 then
2396 if Is_Tagged_Type (Priv) then
2398 -- If the type is tagged, the tag itself must be available on
2399 -- the partial view, for expansion purposes.
2401 Set_First_Entity (Priv, First_Entity (Full));
2403 -- If there are discriminants in the partial view, these remain
2404 -- visible. Otherwise only the tag itself is visible, and there
2405 -- are no nameable components in the partial view.
2407 if No (Last_Entity (Priv)) then
2408 Set_Last_Entity (Priv, First_Entity (Priv));
2409 end if;
2410 end if;
2412 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2414 if Has_Discriminants (Full) then
2415 Set_Discriminant_Constraint (Priv,
2416 Discriminant_Constraint (Full));
2417 end if;
2418 end if;
2419 end Preserve_Full_Attributes;
2421 -----------------
2422 -- Type_In_Use --
2423 -----------------
2425 function Type_In_Use (T : Entity_Id) return Boolean is
2426 begin
2427 return Scope (Base_Type (T)) = P
2428 and then (In_Use (T) or else In_Use (Base_Type (T)));
2429 end Type_In_Use;
2431 -- Start of processing for Uninstall_Declarations
2433 begin
2434 Id := First_Entity (P);
2435 while Present (Id) and then Id /= First_Private_Entity (P) loop
2436 if Debug_Flag_E then
2437 Write_Str ("unlinking visible entity ");
2438 Write_Int (Int (Id));
2439 Write_Eol;
2440 end if;
2442 -- On exit from the package scope, we must preserve the visibility
2443 -- established by use clauses in the current scope. Two cases:
2445 -- a) If the entity is an operator, it may be a primitive operator of
2446 -- a type for which there is a visible use-type clause.
2448 -- b) for other entities, their use-visibility is determined by a
2449 -- visible use clause for the package itself. For a generic instance,
2450 -- the instantiation of the formals appears in the visible part,
2451 -- but the formals are private and remain so.
2453 if Ekind (Id) = E_Function
2454 and then Is_Operator_Symbol_Name (Chars (Id))
2455 and then not Is_Hidden (Id)
2456 and then not Error_Posted (Id)
2457 then
2458 Set_Is_Potentially_Use_Visible (Id,
2459 In_Use (P)
2460 or else Type_In_Use (Etype (Id))
2461 or else Type_In_Use (Etype (First_Formal (Id)))
2462 or else (Present (Next_Formal (First_Formal (Id)))
2463 and then
2464 Type_In_Use
2465 (Etype (Next_Formal (First_Formal (Id))))));
2466 else
2467 if In_Use (P) and then not Is_Hidden (Id) then
2469 -- A child unit of a use-visible package remains use-visible
2470 -- only if it is itself a visible child unit. Otherwise it
2471 -- would remain visible in other contexts where P is use-
2472 -- visible, because once compiled it stays in the entity list
2473 -- of its parent unit.
2475 if Is_Child_Unit (Id) then
2476 Set_Is_Potentially_Use_Visible
2477 (Id, Is_Visible_Lib_Unit (Id));
2478 else
2479 Set_Is_Potentially_Use_Visible (Id);
2480 end if;
2482 else
2483 Set_Is_Potentially_Use_Visible (Id, False);
2484 end if;
2485 end if;
2487 -- Local entities are not immediately visible outside of the package
2489 Set_Is_Immediately_Visible (Id, False);
2491 -- If this is a private type with a full view (for example a local
2492 -- subtype of a private type declared elsewhere), ensure that the
2493 -- full view is also removed from visibility: it may be exposed when
2494 -- swapping views in an instantiation.
2496 if Is_Type (Id) and then Present (Full_View (Id)) then
2497 Set_Is_Immediately_Visible (Full_View (Id), False);
2498 end if;
2500 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2501 Check_Abstract_Overriding (Id);
2502 Check_Conventions (Id);
2503 end if;
2505 if Ekind_In (Id, E_Private_Type, E_Limited_Private_Type)
2506 and then No (Full_View (Id))
2507 and then not Is_Generic_Type (Id)
2508 and then not Is_Derived_Type (Id)
2509 then
2510 Error_Msg_N ("missing full declaration for private type&", Id);
2512 elsif Ekind (Id) = E_Record_Type_With_Private
2513 and then not Is_Generic_Type (Id)
2514 and then No (Full_View (Id))
2515 then
2516 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
2517 Error_Msg_N ("missing full declaration for private type&", Id);
2518 else
2519 Error_Msg_N
2520 ("missing full declaration for private extension", Id);
2521 end if;
2523 -- Case of constant, check for deferred constant declaration with
2524 -- no full view. Likely just a matter of a missing expression, or
2525 -- accidental use of the keyword constant.
2527 elsif Ekind (Id) = E_Constant
2529 -- OK if constant value present
2531 and then No (Constant_Value (Id))
2533 -- OK if full view present
2535 and then No (Full_View (Id))
2537 -- OK if imported, since that provides the completion
2539 and then not Is_Imported (Id)
2541 -- OK if object declaration replaced by renaming declaration as
2542 -- a result of OK_To_Rename processing (e.g. for concatenation)
2544 and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration
2546 -- OK if object declaration with the No_Initialization flag set
2548 and then not (Nkind (Parent (Id)) = N_Object_Declaration
2549 and then No_Initialization (Parent (Id)))
2550 then
2551 -- If no private declaration is present, we assume the user did
2552 -- not intend a deferred constant declaration and the problem
2553 -- is simply that the initializing expression is missing.
2555 if not Has_Private_Declaration (Etype (Id)) then
2557 -- We assume that the user did not intend a deferred constant
2558 -- declaration, and the expression is just missing.
2560 Error_Msg_N
2561 ("constant declaration requires initialization expression",
2562 Parent (Id));
2564 if Is_Limited_Type (Etype (Id)) then
2565 Error_Msg_N
2566 ("\if variable intended, remove CONSTANT from declaration",
2567 Parent (Id));
2568 end if;
2570 -- Otherwise if a private declaration is present, then we are
2571 -- missing the full declaration for the deferred constant.
2573 else
2574 Error_Msg_N
2575 ("missing full declaration for deferred constant (RM 7.4)",
2576 Id);
2578 if Is_Limited_Type (Etype (Id)) then
2579 Error_Msg_N
2580 ("\if variable intended, remove CONSTANT from declaration",
2581 Parent (Id));
2582 end if;
2583 end if;
2584 end if;
2586 Next_Entity (Id);
2587 end loop;
2589 -- If the specification was installed as the parent of a public child
2590 -- unit, the private declarations were not installed, and there is
2591 -- nothing to do.
2593 if not In_Private_Part (P) then
2594 return;
2595 else
2596 Set_In_Private_Part (P, False);
2597 end if;
2599 -- Make private entities invisible and exchange full and private
2600 -- declarations for private types. Id is now the first private entity
2601 -- in the package.
2603 while Present (Id) loop
2604 if Debug_Flag_E then
2605 Write_Str ("unlinking private entity ");
2606 Write_Int (Int (Id));
2607 Write_Eol;
2608 end if;
2610 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2611 Check_Abstract_Overriding (Id);
2612 Check_Conventions (Id);
2613 end if;
2615 Set_Is_Immediately_Visible (Id, False);
2617 if Is_Private_Base_Type (Id) and then Present (Full_View (Id)) then
2618 Full := Full_View (Id);
2620 -- If the partial view is not declared in the visible part of the
2621 -- package (as is the case when it is a type derived from some
2622 -- other private type in the private part of the current package),
2623 -- no exchange takes place.
2625 if No (Parent (Id))
2626 or else List_Containing (Parent (Id)) /=
2627 Visible_Declarations (Specification (Decl))
2628 then
2629 goto Next_Id;
2630 end if;
2632 -- The entry in the private part points to the full declaration,
2633 -- which is currently visible. Exchange them so only the private
2634 -- type declaration remains accessible, and link private and full
2635 -- declaration in the opposite direction. Before the actual
2636 -- exchange, we copy back attributes of the full view that must
2637 -- be available to the partial view too.
2639 Preserve_Full_Attributes (Id, Full);
2641 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
2643 -- The following test may be redundant, as this is already
2644 -- diagnosed in sem_ch3. ???
2646 if Is_Indefinite_Subtype (Full)
2647 and then not Is_Indefinite_Subtype (Id)
2648 then
2649 Error_Msg_Sloc := Sloc (Parent (Id));
2650 Error_Msg_NE
2651 ("full view of& not compatible with declaration#", Full, Id);
2652 end if;
2654 -- Swap out the subtypes and derived types of Id that
2655 -- were compiled in this scope, or installed previously
2656 -- by Install_Private_Declarations.
2658 -- Before we do the swap, we verify the presence of the Full_View
2659 -- field which may be empty due to a swap by a previous call to
2660 -- End_Package_Scope (e.g. from the freezing mechanism).
2662 Priv_Elmt := First_Elmt (Private_Dependents (Id));
2663 while Present (Priv_Elmt) loop
2664 Priv_Sub := Node (Priv_Elmt);
2666 if Present (Full_View (Priv_Sub)) then
2667 if Scope (Priv_Sub) = P
2668 or else not In_Open_Scopes (Scope (Priv_Sub))
2669 then
2670 Set_Is_Immediately_Visible (Priv_Sub, False);
2671 end if;
2673 if Is_Visible_Dependent (Priv_Sub) then
2674 Preserve_Full_Attributes
2675 (Priv_Sub, Full_View (Priv_Sub));
2676 Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub));
2677 Exchange_Declarations (Priv_Sub);
2678 end if;
2679 end if;
2681 Next_Elmt (Priv_Elmt);
2682 end loop;
2684 -- Now restore the type itself to its private view
2686 Exchange_Declarations (Id);
2688 -- If we have installed an underlying full view for a type derived
2689 -- from a private type in a child unit, restore the proper views
2690 -- of private and full view. See corresponding code in
2691 -- Install_Private_Declarations.
2693 -- After the exchange, Full denotes the private type in the
2694 -- visible part of the package.
2696 if Is_Private_Base_Type (Full)
2697 and then Present (Full_View (Full))
2698 and then Present (Underlying_Full_View (Full))
2699 and then In_Package_Body (Current_Scope)
2700 then
2701 Set_Full_View (Full, Underlying_Full_View (Full));
2702 Set_Underlying_Full_View (Full, Empty);
2703 end if;
2705 elsif Ekind (Id) = E_Incomplete_Type
2706 and then Comes_From_Source (Id)
2707 and then No (Full_View (Id))
2708 then
2709 -- Mark Taft amendment types. Verify that there are no primitive
2710 -- operations declared for the type (3.10.1(9)).
2712 Set_Has_Completion_In_Body (Id);
2714 declare
2715 Elmt : Elmt_Id;
2716 Subp : Entity_Id;
2718 begin
2719 Elmt := First_Elmt (Private_Dependents (Id));
2720 while Present (Elmt) loop
2721 Subp := Node (Elmt);
2723 -- Is_Primitive is tested because there can be cases where
2724 -- nonprimitive subprograms (in nested packages) are added
2725 -- to the Private_Dependents list.
2727 if Is_Overloadable (Subp) and then Is_Primitive (Subp) then
2728 Error_Msg_NE
2729 ("type& must be completed in the private part",
2730 Parent (Subp), Id);
2732 -- The result type of an access-to-function type cannot be a
2733 -- Taft-amendment type, unless the version is Ada 2012 or
2734 -- later (see AI05-151).
2736 elsif Ada_Version < Ada_2012
2737 and then Ekind (Subp) = E_Subprogram_Type
2738 then
2739 if Etype (Subp) = Id
2740 or else
2741 (Is_Class_Wide_Type (Etype (Subp))
2742 and then Etype (Etype (Subp)) = Id)
2743 then
2744 Error_Msg_NE
2745 ("type& must be completed in the private part",
2746 Associated_Node_For_Itype (Subp), Id);
2747 end if;
2748 end if;
2750 Next_Elmt (Elmt);
2751 end loop;
2752 end;
2754 elsif not Is_Child_Unit (Id)
2755 and then (not Is_Private_Type (Id) or else No (Full_View (Id)))
2756 then
2757 Set_Is_Hidden (Id);
2758 Set_Is_Potentially_Use_Visible (Id, False);
2759 end if;
2761 <<Next_Id>>
2762 Next_Entity (Id);
2763 end loop;
2764 end Uninstall_Declarations;
2766 ------------------------
2767 -- Unit_Requires_Body --
2768 ------------------------
2770 function Unit_Requires_Body
2771 (P : Entity_Id;
2772 Ignore_Abstract_State : Boolean := False) return Boolean
2774 E : Entity_Id;
2776 begin
2777 -- Imported entity never requires body. Right now, only subprograms can
2778 -- be imported, but perhaps in the future we will allow import of
2779 -- packages.
2781 if Is_Imported (P) then
2782 return False;
2784 -- Body required if library package with pragma Elaborate_Body
2786 elsif Has_Pragma_Elaborate_Body (P) then
2787 return True;
2789 -- Body required if subprogram
2791 elsif Is_Subprogram (P) or else Is_Generic_Subprogram (P) then
2792 return True;
2794 -- Treat a block as requiring a body
2796 elsif Ekind (P) = E_Block then
2797 return True;
2799 elsif Ekind (P) = E_Package
2800 and then Nkind (Parent (P)) = N_Package_Specification
2801 and then Present (Generic_Parent (Parent (P)))
2802 then
2803 declare
2804 G_P : constant Entity_Id := Generic_Parent (Parent (P));
2805 begin
2806 if Has_Pragma_Elaborate_Body (G_P) then
2807 return True;
2808 end if;
2809 end;
2811 -- A [generic] package that introduces at least one non-null abstract
2812 -- state requires completion. However, there is a separate rule that
2813 -- requires that such a package have a reason other than this for a
2814 -- body being required (if necessary a pragma Elaborate_Body must be
2815 -- provided). If Ignore_Abstract_State is True, we don't do this check
2816 -- (so we can use Unit_Requires_Body to check for some other reason).
2818 elsif Ekind_In (P, E_Generic_Package, E_Package)
2819 and then not Ignore_Abstract_State
2820 and then Present (Abstract_States (P))
2821 and then
2822 not Is_Null_State (Node (First_Elmt (Abstract_States (P))))
2823 then
2824 return True;
2825 end if;
2827 -- Otherwise search entity chain for entity requiring completion
2829 E := First_Entity (P);
2830 while Present (E) loop
2832 -- Always ignore child units. Child units get added to the entity
2833 -- list of a parent unit, but are not original entities of the
2834 -- parent, and so do not affect whether the parent needs a body.
2836 if Is_Child_Unit (E) then
2837 null;
2839 -- Ignore formal packages and their renamings
2841 elsif Ekind (E) = E_Package
2842 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
2843 N_Formal_Package_Declaration
2844 then
2845 null;
2847 -- Otherwise test to see if entity requires a completion.
2848 -- Note that subprogram entities whose declaration does not come
2849 -- from source are ignored here on the basis that we assume the
2850 -- expander will provide an implicit completion at some point.
2852 elsif (Is_Overloadable (E)
2853 and then Ekind (E) /= E_Enumeration_Literal
2854 and then Ekind (E) /= E_Operator
2855 and then not Is_Abstract_Subprogram (E)
2856 and then not Has_Completion (E)
2857 and then Comes_From_Source (Parent (E)))
2859 or else
2860 (Ekind (E) = E_Package
2861 and then E /= P
2862 and then not Has_Completion (E)
2863 and then Unit_Requires_Body (E))
2865 or else
2866 (Ekind (E) = E_Incomplete_Type
2867 and then No (Full_View (E))
2868 and then not Is_Generic_Type (E))
2870 or else
2871 (Ekind_In (E, E_Task_Type, E_Protected_Type)
2872 and then not Has_Completion (E))
2874 or else
2875 (Ekind (E) = E_Generic_Package
2876 and then E /= P
2877 and then not Has_Completion (E)
2878 and then Unit_Requires_Body (E))
2880 or else
2881 (Is_Generic_Subprogram (E)
2882 and then not Has_Completion (E))
2884 then
2885 return True;
2887 -- Entity that does not require completion
2889 else
2890 null;
2891 end if;
2893 Next_Entity (E);
2894 end loop;
2896 return False;
2897 end Unit_Requires_Body;
2899 -----------------------------
2900 -- Unit_Requires_Body_Info --
2901 -----------------------------
2903 procedure Unit_Requires_Body_Info (P : Entity_Id) is
2904 E : Entity_Id;
2906 begin
2907 -- Imported entity never requires body. Right now, only subprograms can
2908 -- be imported, but perhaps in the future we will allow import of
2909 -- packages.
2911 if Is_Imported (P) then
2912 return;
2914 -- Body required if library package with pragma Elaborate_Body
2916 elsif Has_Pragma_Elaborate_Body (P) then
2917 Error_Msg_N ("info: & requires body (Elaborate_Body)?Y?", P);
2919 -- Body required if subprogram
2921 elsif Is_Subprogram (P) or else Is_Generic_Subprogram (P) then
2922 Error_Msg_N ("info: & requires body (subprogram case)?Y?", P);
2924 -- Body required if generic parent has Elaborate_Body
2926 elsif Ekind (P) = E_Package
2927 and then Nkind (Parent (P)) = N_Package_Specification
2928 and then Present (Generic_Parent (Parent (P)))
2929 then
2930 declare
2931 G_P : constant Entity_Id := Generic_Parent (Parent (P));
2932 begin
2933 if Has_Pragma_Elaborate_Body (G_P) then
2934 Error_Msg_N
2935 ("info: & requires body (generic parent Elaborate_Body)?Y?",
2937 end if;
2938 end;
2940 -- A [generic] package that introduces at least one non-null abstract
2941 -- state requires completion. However, there is a separate rule that
2942 -- requires that such a package have a reason other than this for a
2943 -- body being required (if necessary a pragma Elaborate_Body must be
2944 -- provided). If Ignore_Abstract_State is True, we don't do this check
2945 -- (so we can use Unit_Requires_Body to check for some other reason).
2947 elsif Ekind_In (P, E_Generic_Package, E_Package)
2948 and then Present (Abstract_States (P))
2949 and then
2950 not Is_Null_State (Node (First_Elmt (Abstract_States (P))))
2951 then
2952 Error_Msg_N
2953 ("info: & requires body (non-null abstract state aspect)?Y?", P);
2954 end if;
2956 -- Otherwise search entity chain for entity requiring completion
2958 E := First_Entity (P);
2959 while Present (E) loop
2961 -- Always ignore child units. Child units get added to the entity
2962 -- list of a parent unit, but are not original entities of the
2963 -- parent, and so do not affect whether the parent needs a body.
2965 if Is_Child_Unit (E) then
2966 null;
2968 -- Ignore formal packages and their renamings
2970 elsif Ekind (E) = E_Package
2971 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
2972 N_Formal_Package_Declaration
2973 then
2974 null;
2976 -- Otherwise test to see if entity requires a completion.
2977 -- Note that subprogram entities whose declaration does not come
2978 -- from source are ignored here on the basis that we assume the
2979 -- expander will provide an implicit completion at some point.
2981 elsif (Is_Overloadable (E)
2982 and then Ekind (E) /= E_Enumeration_Literal
2983 and then Ekind (E) /= E_Operator
2984 and then not Is_Abstract_Subprogram (E)
2985 and then not Has_Completion (E)
2986 and then Comes_From_Source (Parent (E)))
2988 or else
2989 (Ekind (E) = E_Package
2990 and then E /= P
2991 and then not Has_Completion (E)
2992 and then Unit_Requires_Body (E))
2994 or else
2995 (Ekind (E) = E_Incomplete_Type
2996 and then No (Full_View (E))
2997 and then not Is_Generic_Type (E))
2999 or else
3000 (Ekind_In (E, E_Task_Type, E_Protected_Type)
3001 and then not Has_Completion (E))
3003 or else
3004 (Ekind (E) = E_Generic_Package
3005 and then E /= P
3006 and then not Has_Completion (E)
3007 and then Unit_Requires_Body (E))
3009 or else
3010 (Is_Generic_Subprogram (E)
3011 and then not Has_Completion (E))
3013 then
3014 Error_Msg_Node_2 := E;
3015 Error_Msg_NE
3016 ("info: & requires body (& requires completion)?Y?",
3017 E, P);
3019 -- Entity that does not require completion
3021 else
3022 null;
3023 end if;
3025 Next_Entity (E);
3026 end loop;
3027 end Unit_Requires_Body_Info;
3028 end Sem_Ch7;