]> gcc.gnu.org Git - gcc.git/blob - gcc/ada/sem_ch7.adb
[Ada] Fix internal error on package instantiation on private type
[gcc.git] / gcc / ada / sem_ch7.adb
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-2018, 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 ------------------------------------------------------------------------------
25
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.
30
31 with Aspects; use Aspects;
32 with Atree; use Atree;
33 with Contracts; use Contracts;
34 with Debug; use Debug;
35 with Einfo; use Einfo;
36 with Elists; use Elists;
37 with Errout; use Errout;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Dist; use Exp_Dist;
40 with Exp_Dbug; use Exp_Dbug;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Lib; use Lib;
44 with Lib.Xref; use Lib.Xref;
45 with Namet; use Namet;
46 with Nmake; use Nmake;
47 with Nlists; use Nlists;
48 with Opt; use Opt;
49 with Output; use Output;
50 with Restrict; use Restrict;
51 with Rtsfind; use Rtsfind;
52 with Sem; use Sem;
53 with Sem_Aux; use Sem_Aux;
54 with Sem_Cat; use Sem_Cat;
55 with Sem_Ch3; use Sem_Ch3;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch8; use Sem_Ch8;
58 with Sem_Ch10; use Sem_Ch10;
59 with Sem_Ch12; use Sem_Ch12;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Disp; use Sem_Disp;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Prag; use Sem_Prag;
64 with Sem_Util; use Sem_Util;
65 with Sem_Warn; use Sem_Warn;
66 with Snames; use Snames;
67 with Stand; use Stand;
68 with Sinfo; use Sinfo;
69 with Sinput; use Sinput;
70 with Style;
71 with Uintp; use Uintp;
72
73 with GNAT.HTable;
74
75 package body Sem_Ch7 is
76
77 -----------------------------------
78 -- Handling private declarations --
79 -----------------------------------
80
81 -- The principle that each entity has a single defining occurrence clashes
82 -- with the presence of two separate definitions for private types: the
83 -- first is the private type declaration, and the second is the full type
84 -- declaration. It is important that all references to the type point to
85 -- the same defining occurrence, namely the first one. To enforce the two
86 -- separate views of the entity, the corresponding information is swapped
87 -- between the two declarations. Outside of the package, the defining
88 -- occurrence only contains the private declaration information, while in
89 -- the private part and the body of the package the defining occurrence
90 -- contains the full declaration. To simplify the swap, the defining
91 -- occurrence that currently holds the private declaration points to the
92 -- full declaration. During semantic processing the defining occurrence
93 -- also points to a list of private dependents, that is to say access types
94 -- or composite types whose designated types or component types are
95 -- subtypes or derived types of the private type in question. After the
96 -- full declaration has been seen, the private dependents are updated to
97 -- indicate that they have full definitions.
98
99 -----------------------
100 -- Local Subprograms --
101 -----------------------
102
103 procedure Analyze_Package_Body_Helper (N : Node_Id);
104 -- Does all the real work of Analyze_Package_Body
105
106 procedure Check_Anonymous_Access_Types
107 (Spec_Id : Entity_Id;
108 P_Body : Node_Id);
109 -- If the spec of a package has a limited_with_clause, it may declare
110 -- anonymous access types whose designated type is a limited view, such an
111 -- anonymous access return type for a function. This access type cannot be
112 -- elaborated in the spec itself, but it may need an itype reference if it
113 -- is used within a nested scope. In that case the itype reference is
114 -- created at the beginning of the corresponding package body and inserted
115 -- before other body declarations.
116
117 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id);
118 -- Called upon entering the private part of a public child package and the
119 -- body of a nested package, to potentially declare certain inherited
120 -- subprograms that were inherited by types in the visible part, but whose
121 -- declaration was deferred because the parent operation was private and
122 -- not visible at that point. These subprograms are located by traversing
123 -- the visible part declarations looking for non-private type extensions
124 -- and then examining each of the primitive operations of such types to
125 -- find those that were inherited but declared with a special internal
126 -- name. Each such operation is now declared as an operation with a normal
127 -- name (using the name of the parent operation) and replaces the previous
128 -- implicit operation in the primitive operations list of the type. If the
129 -- inherited private operation has been overridden, then it's replaced by
130 -- the overriding operation.
131
132 procedure Install_Package_Entity (Id : Entity_Id);
133 -- Supporting procedure for Install_{Visible,Private}_Declarations. Places
134 -- one entity on its visibility chain, and recurses on the visible part if
135 -- the entity is an inner package.
136
137 function Is_Private_Base_Type (E : Entity_Id) return Boolean;
138 -- True for a private type that is not a subtype
139
140 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean;
141 -- If the private dependent is a private type whose full view is derived
142 -- from the parent type, its full properties are revealed only if we are in
143 -- the immediate scope of the private dependent. Should this predicate be
144 -- tightened further???
145
146 function Requires_Completion_In_Body
147 (Id : Entity_Id;
148 Pack_Id : Entity_Id;
149 Do_Abstract_States : Boolean := False) return Boolean;
150 -- Subsidiary to routines Unit_Requires_Body and Unit_Requires_Body_Info.
151 -- Determine whether entity Id declared in package spec Pack_Id requires
152 -- completion in a package body. Flag Do_Abstract_Stats should be set when
153 -- abstract states are to be considered in the completion test.
154
155 procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id);
156 -- Outputs info messages showing why package Pack_Id requires a body. The
157 -- caller has checked that the switch requesting this information is set,
158 -- and that the package does indeed require a body.
159
160 --------------------------
161 -- Analyze_Package_Body --
162 --------------------------
163
164 procedure Analyze_Package_Body (N : Node_Id) is
165 Loc : constant Source_Ptr := Sloc (N);
166
167 begin
168 if Debug_Flag_C then
169 Write_Str ("==> package body ");
170 Write_Name (Chars (Defining_Entity (N)));
171 Write_Str (" from ");
172 Write_Location (Loc);
173 Write_Eol;
174 Indent;
175 end if;
176
177 -- The real work is split out into the helper, so it can do "return;"
178 -- without skipping the debug output.
179
180 Analyze_Package_Body_Helper (N);
181
182 if Debug_Flag_C then
183 Outdent;
184 Write_Str ("<== package body ");
185 Write_Name (Chars (Defining_Entity (N)));
186 Write_Str (" from ");
187 Write_Location (Loc);
188 Write_Eol;
189 end if;
190 end Analyze_Package_Body;
191
192 ------------------------------------------------------
193 -- Analyze_Package_Body_Helper Data and Subprograms --
194 ------------------------------------------------------
195
196 Entity_Table_Size : constant := 4093;
197 -- Number of headers in hash table
198
199 subtype Entity_Header_Num is Integer range 0 .. Entity_Table_Size - 1;
200 -- Range of headers in hash table
201
202 function Node_Hash (Id : Entity_Id) return Entity_Header_Num;
203 -- Simple hash function for Entity_Ids
204
205 package Subprogram_Table is new GNAT.Htable.Simple_HTable
206 (Header_Num => Entity_Header_Num,
207 Element => Boolean,
208 No_Element => False,
209 Key => Entity_Id,
210 Hash => Node_Hash,
211 Equal => "=");
212 -- Hash table to record which subprograms are referenced. It is declared
213 -- at library level to avoid elaborating it for every call to Analyze.
214
215 package Traversed_Table is new GNAT.Htable.Simple_HTable
216 (Header_Num => Entity_Header_Num,
217 Element => Boolean,
218 No_Element => False,
219 Key => Node_Id,
220 Hash => Node_Hash,
221 Equal => "=");
222 -- Hash table to record which nodes we have traversed, so we can avoid
223 -- traversing the same nodes repeatedly.
224
225 -----------------
226 -- Node_Hash --
227 -----------------
228
229 function Node_Hash (Id : Entity_Id) return Entity_Header_Num is
230 begin
231 return Entity_Header_Num (Id mod Entity_Table_Size);
232 end Node_Hash;
233
234 ---------------------------------
235 -- Analyze_Package_Body_Helper --
236 ---------------------------------
237
238 -- WARNING: This routine manages Ghost regions. Return statements must be
239 -- replaced by gotos which jump to the end of the routine and restore the
240 -- Ghost mode.
241
242 procedure Analyze_Package_Body_Helper (N : Node_Id) is
243 procedure Hide_Public_Entities (Decls : List_Id);
244 -- Attempt to hide all public entities found in declarative list Decls
245 -- by resetting their Is_Public flag to False depending on whether the
246 -- entities are not referenced by inlined or generic bodies. This kind
247 -- of processing is a conservative approximation and will still leave
248 -- entities externally visible if the package is not simple enough.
249
250 procedure Install_Composite_Operations (P : Entity_Id);
251 -- Composite types declared in the current scope may depend on types
252 -- that were private at the point of declaration, and whose full view
253 -- is now in scope. Indicate that the corresponding operations on the
254 -- composite type are available.
255
256 --------------------------
257 -- Hide_Public_Entities --
258 --------------------------
259
260 procedure Hide_Public_Entities (Decls : List_Id) is
261 function Has_Referencer
262 (Decls : List_Id;
263 In_Nested_Instance : Boolean;
264 Has_Outer_Referencer_Of_Non_Subprograms : Boolean) return Boolean;
265 -- A "referencer" is a construct which may reference a previous
266 -- declaration. Examine all declarations in list Decls in reverse
267 -- and determine whether one such referencer exists. All entities
268 -- in the range Last (Decls) .. Referencer are hidden from external
269 -- visibility.
270
271 function Scan_Subprogram_Ref (N : Node_Id) return Traverse_Result;
272 -- Determine whether a node denotes a reference to a subprogram
273
274 procedure Traverse_And_Scan_Subprogram_Refs is
275 new Traverse_Proc (Scan_Subprogram_Ref);
276 -- Subsidiary to routine Has_Referencer. Determine whether a node
277 -- contains references to a subprogram and record them.
278 -- WARNING: this is a very expensive routine as it performs a full
279 -- tree traversal.
280
281 procedure Scan_Subprogram_Refs (Node : Node_Id);
282 -- If we haven't already traversed Node, then mark it and traverse
283 -- it.
284
285 --------------------
286 -- Has_Referencer --
287 --------------------
288
289 function Has_Referencer
290 (Decls : List_Id;
291 In_Nested_Instance : Boolean;
292 Has_Outer_Referencer_Of_Non_Subprograms : Boolean) return Boolean
293 is
294 Decl : Node_Id;
295 Decl_Id : Entity_Id;
296 Spec : Node_Id;
297
298 Has_Referencer_Of_Non_Subprograms : Boolean :=
299 Has_Outer_Referencer_Of_Non_Subprograms;
300 -- Set if an inlined subprogram body was detected as a referencer.
301 -- In this case, we do not return True immediately but keep hiding
302 -- subprograms from external visibility.
303
304 begin
305 if No (Decls) then
306 return False;
307 end if;
308
309 -- Examine all declarations in reverse order, hiding all entities
310 -- from external visibility until a referencer has been found. The
311 -- algorithm recurses into nested packages.
312
313 Decl := Last (Decls);
314 while Present (Decl) loop
315
316 -- A stub is always considered a referencer
317
318 if Nkind (Decl) in N_Body_Stub then
319 return True;
320
321 -- Package declaration
322
323 elsif Nkind (Decl) = N_Package_Declaration then
324 Spec := Specification (Decl);
325 Decl_Id := Defining_Entity (Spec);
326
327 -- Inspect the declarations of a non-generic package to try
328 -- and hide more entities from external visibility.
329
330 if not Is_Generic_Unit (Decl_Id) then
331 if Has_Referencer (Private_Declarations (Spec),
332 In_Nested_Instance
333 or else
334 Is_Generic_Instance (Decl_Id),
335 Has_Referencer_Of_Non_Subprograms)
336 or else
337 Has_Referencer (Visible_Declarations (Spec),
338 In_Nested_Instance
339 or else
340 Is_Generic_Instance (Decl_Id),
341 Has_Referencer_Of_Non_Subprograms)
342 then
343 return True;
344 end if;
345 end if;
346
347 -- Package body
348
349 elsif Nkind (Decl) = N_Package_Body
350 and then Present (Corresponding_Spec (Decl))
351 then
352 Decl_Id := Corresponding_Spec (Decl);
353
354 -- A generic package body is a referencer. It would seem
355 -- that we only have to consider generics that can be
356 -- exported, i.e. where the corresponding spec is the
357 -- spec of the current package, but because of nested
358 -- instantiations, a fully private generic body may export
359 -- other private body entities. Furthermore, regardless of
360 -- whether there was a previous inlined subprogram, (an
361 -- instantiation of) the generic package may reference any
362 -- entity declared before it.
363
364 if Is_Generic_Unit (Decl_Id) then
365 return True;
366
367 -- Inspect the declarations of a non-generic package body to
368 -- try and hide more entities from external visibility.
369
370 elsif Has_Referencer (Declarations (Decl),
371 In_Nested_Instance
372 or else
373 Is_Generic_Instance (Decl_Id),
374 Has_Referencer_Of_Non_Subprograms)
375 then
376 return True;
377 end if;
378
379 -- Subprogram body
380
381 elsif Nkind (Decl) = N_Subprogram_Body then
382 if Present (Corresponding_Spec (Decl)) then
383 Decl_Id := Corresponding_Spec (Decl);
384
385 -- A generic subprogram body acts as a referencer
386
387 if Is_Generic_Unit (Decl_Id) then
388 return True;
389 end if;
390
391 -- An inlined subprogram body acts as a referencer
392
393 -- Note that we test Has_Pragma_Inline here in addition
394 -- to Is_Inlined. We are doing this for a client, since
395 -- we are computing which entities should be public, and
396 -- it is the client who will decide if actual inlining
397 -- should occur, so we need to catch all cases where the
398 -- subprogram may be inlined by the client.
399
400 if Is_Inlined (Decl_Id)
401 or else Has_Pragma_Inline (Decl_Id)
402 then
403 Has_Referencer_Of_Non_Subprograms := True;
404
405 -- Inspect the statements of the subprogram body
406 -- to determine whether the body references other
407 -- subprograms.
408
409 Scan_Subprogram_Refs (Decl);
410 end if;
411
412 -- Otherwise this is a stand alone subprogram body
413
414 else
415 Decl_Id := Defining_Entity (Decl);
416
417 -- An inlined subprogram body acts as a referencer
418
419 if Is_Inlined (Decl_Id)
420 or else Has_Pragma_Inline (Decl_Id)
421 then
422 Has_Referencer_Of_Non_Subprograms := True;
423
424 -- Inspect the statements of the subprogram body
425 -- to determine whether the body references other
426 -- subprograms.
427
428 Scan_Subprogram_Refs (Decl);
429
430 -- Otherwise we can reset Is_Public right away
431
432 elsif not Subprogram_Table.Get (Decl_Id) then
433 Set_Is_Public (Decl_Id, False);
434 end if;
435 end if;
436
437 -- Freeze node
438
439 elsif Nkind (Decl) = N_Freeze_Entity then
440 declare
441 Discard : Boolean;
442 pragma Unreferenced (Discard);
443 begin
444 -- Inspect the actions to find references to subprograms.
445 -- We assume that the actions do not contain other kinds
446 -- of references and, therefore, we do not stop the scan
447 -- or set Has_Referencer_Of_Non_Subprograms here. Doing
448 -- it would pessimize common cases for which the actions
449 -- contain the declaration of an init procedure, since
450 -- such a procedure is automatically marked inline.
451
452 Discard :=
453 Has_Referencer (Actions (Decl),
454 In_Nested_Instance,
455 Has_Referencer_Of_Non_Subprograms);
456 end;
457
458 -- Exceptions, objects and renamings do not need to be public
459 -- if they are not followed by a construct which can reference
460 -- and export them. Likewise for subprograms but we work harder
461 -- for them to see whether they are referenced on an individual
462 -- basis by looking into the table of referenced subprograms.
463 -- But we cannot say anything for entities declared in nested
464 -- instances because instantiations are not done yet so the
465 -- bodies are not visible and could contain references to them.
466 elsif Nkind_In (Decl, N_Exception_Declaration,
467 N_Object_Declaration,
468 N_Object_Renaming_Declaration,
469 N_Subprogram_Declaration,
470 N_Subprogram_Renaming_Declaration)
471 then
472 Decl_Id := Defining_Entity (Decl);
473
474 if not In_Nested_Instance
475 and then not Is_Imported (Decl_Id)
476 and then not Is_Exported (Decl_Id)
477 and then No (Interface_Name (Decl_Id))
478 and then
479 ((Nkind (Decl) /= N_Subprogram_Declaration
480 and then not Has_Referencer_Of_Non_Subprograms)
481 or else (Nkind (Decl) = N_Subprogram_Declaration
482 and then not Subprogram_Table.Get (Decl_Id)))
483 then
484 Set_Is_Public (Decl_Id, False);
485 end if;
486
487 -- For a subprogram renaming, if the entity is referenced,
488 -- then so is the renamed subprogram. But there is an issue
489 -- with generic bodies because instantiations are not done
490 -- yet and, therefore, cannot be scanned for referencers.
491 -- That's why we use an approximation and test that we have
492 -- at least one subprogram referenced by an inlined body
493 -- instead of precisely the entity of this renaming.
494
495 if Nkind (Decl) = N_Subprogram_Renaming_Declaration
496 and then Subprogram_Table.Get_First
497 and then Is_Entity_Name (Name (Decl))
498 and then Present (Entity (Name (Decl)))
499 and then Is_Subprogram (Entity (Name (Decl)))
500 then
501 Subprogram_Table.Set (Entity (Name (Decl)), True);
502 end if;
503 end if;
504
505 Prev (Decl);
506 end loop;
507
508 return Has_Referencer_Of_Non_Subprograms;
509 end Has_Referencer;
510
511 -------------------------
512 -- Scan_Subprogram_Ref --
513 -------------------------
514
515 function Scan_Subprogram_Ref (N : Node_Id) return Traverse_Result is
516 begin
517 -- Detect a reference of the form
518 -- Subp_Call
519
520 if Nkind (N) in N_Subprogram_Call
521 and then Is_Entity_Name (Name (N))
522 and then Present (Entity (Name (N)))
523 and then Is_Subprogram (Entity (Name (N)))
524 then
525 Subprogram_Table.Set (Entity (Name (N)), True);
526
527 -- Detect a reference of the form
528 -- Subp'Some_Attribute
529
530 elsif Nkind (N) = N_Attribute_Reference
531 and then Is_Entity_Name (Prefix (N))
532 and then Present (Entity (Prefix (N)))
533 and then Is_Subprogram (Entity (Prefix (N)))
534 then
535 Subprogram_Table.Set (Entity (Prefix (N)), True);
536
537 -- Constants can be substituted by their value in gigi, which may
538 -- contain a reference, so scan the value recursively.
539
540 elsif Is_Entity_Name (N)
541 and then Present (Entity (N))
542 and then Ekind (Entity (N)) = E_Constant
543 then
544 declare
545 Val : constant Node_Id := Constant_Value (Entity (N));
546 begin
547 if Present (Val)
548 and then not Compile_Time_Known_Value (Val)
549 then
550 Scan_Subprogram_Refs (Val);
551 end if;
552 end;
553 end if;
554
555 return OK;
556 end Scan_Subprogram_Ref;
557
558 --------------------------
559 -- Scan_Subprogram_Refs --
560 --------------------------
561
562 procedure Scan_Subprogram_Refs (Node : Node_Id) is
563 begin
564 if not Traversed_Table.Get (Node) then
565 Traversed_Table.Set (Node, True);
566 Traverse_And_Scan_Subprogram_Refs (Node);
567 end if;
568 end Scan_Subprogram_Refs;
569
570 -- Local variables
571
572 Discard : Boolean;
573 pragma Unreferenced (Discard);
574
575 -- Start of processing for Hide_Public_Entities
576
577 begin
578 -- The algorithm examines the top level declarations of a package
579 -- body in reverse looking for a construct that may export entities
580 -- declared prior to it. If such a scenario is encountered, then all
581 -- entities in the range Last (Decls) .. construct are hidden from
582 -- external visibility. Consider:
583
584 -- package Pack is
585 -- generic
586 -- package Gen is
587 -- end Gen;
588 -- end Pack;
589
590 -- package body Pack is
591 -- External_Obj : ...; -- (1)
592
593 -- package body Gen is -- (2)
594 -- ... External_Obj ... -- (3)
595 -- end Gen;
596
597 -- Local_Obj : ...; -- (4)
598 -- end Pack;
599
600 -- In this example Local_Obj (4) must not be externally visible as
601 -- it cannot be exported by anything in Pack. The body of generic
602 -- package Gen (2) on the other hand acts as a "referencer" and may
603 -- export anything declared before it. Since the compiler does not
604 -- perform flow analysis, it is not possible to determine precisely
605 -- which entities will be exported when Gen is instantiated. In the
606 -- example above External_Obj (1) is exported at (3), but this may
607 -- not always be the case. The algorithm takes a conservative stance
608 -- and leaves entity External_Obj public.
609
610 -- This very conservative algorithm is supplemented by a more precise
611 -- processing for inlined bodies. For them, we traverse the syntactic
612 -- tree and record which subprograms are actually referenced from it.
613 -- This makes it possible to compute a much smaller set of externally
614 -- visible subprograms in the absence of generic bodies, which can
615 -- have a significant impact on the inlining decisions made in the
616 -- back end and the removal of out-of-line bodies from the object
617 -- code. We do it only for inlined bodies because they are supposed
618 -- to be reasonably small and tree traversal is very expensive.
619
620 -- Note that even this special processing is not optimal for inlined
621 -- bodies, because we treat all inlined subprograms alike. An optimal
622 -- algorithm would require computing the transitive closure of the
623 -- inlined subprograms that can really be referenced from other units
624 -- in the source code.
625
626 -- We could extend this processing for inlined bodies and record all
627 -- entities, not just subprograms, referenced from them, which would
628 -- make it possible to compute a much smaller set of all externally
629 -- visible entities in the absence of generic bodies. But this would
630 -- mean implementing a more thorough tree traversal of the bodies,
631 -- i.e. not just syntactic, and the gain would very likely be worth
632 -- neither the hassle nor the slowdown of the compiler.
633
634 -- Finally, an important thing to be aware of is that, at this point,
635 -- instantiations are not done yet so we cannot directly see inlined
636 -- bodies coming from them. That's not catastrophic because only the
637 -- actual parameters of the instantiations matter here, and they are
638 -- present in the declarations list of the instantiated packages.
639
640 Traversed_Table.Reset;
641 Subprogram_Table.Reset;
642 Discard := Has_Referencer (Decls, False, False);
643 end Hide_Public_Entities;
644
645 ----------------------------------
646 -- Install_Composite_Operations --
647 ----------------------------------
648
649 procedure Install_Composite_Operations (P : Entity_Id) is
650 Id : Entity_Id;
651
652 begin
653 Id := First_Entity (P);
654 while Present (Id) loop
655 if Is_Type (Id)
656 and then (Is_Limited_Composite (Id)
657 or else Is_Private_Composite (Id))
658 and then No (Private_Component (Id))
659 then
660 Set_Is_Limited_Composite (Id, False);
661 Set_Is_Private_Composite (Id, False);
662 end if;
663
664 Next_Entity (Id);
665 end loop;
666 end Install_Composite_Operations;
667
668 -- Local variables
669
670 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
671 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
672 Saved_ISMP : constant Boolean :=
673 Ignore_SPARK_Mode_Pragmas_In_Instance;
674 -- Save the Ghost and SPARK mode-related data to restore on exit
675
676 Body_Id : Entity_Id;
677 HSS : Node_Id;
678 Last_Spec_Entity : Entity_Id;
679 New_N : Node_Id;
680 Pack_Decl : Node_Id;
681 Spec_Id : Entity_Id;
682
683 -- Start of processing for Analyze_Package_Body_Helper
684
685 begin
686 -- Find corresponding package specification, and establish the current
687 -- scope. The visible defining entity for the package is the defining
688 -- occurrence in the spec. On exit from the package body, all body
689 -- declarations are attached to the defining entity for the body, but
690 -- the later is never used for name resolution. In this fashion there
691 -- is only one visible entity that denotes the package.
692
693 -- Set Body_Id. Note that this will be reset to point to the generic
694 -- copy later on in the generic case.
695
696 Body_Id := Defining_Entity (N);
697
698 -- Body is body of package instantiation. Corresponding spec has already
699 -- been set.
700
701 if Present (Corresponding_Spec (N)) then
702 Spec_Id := Corresponding_Spec (N);
703 Pack_Decl := Unit_Declaration_Node (Spec_Id);
704
705 else
706 Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
707
708 if Present (Spec_Id)
709 and then Is_Package_Or_Generic_Package (Spec_Id)
710 then
711 Pack_Decl := Unit_Declaration_Node (Spec_Id);
712
713 if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
714 Error_Msg_N ("cannot supply body for package renaming", N);
715 return;
716
717 elsif Present (Corresponding_Body (Pack_Decl)) then
718 Error_Msg_N ("redefinition of package body", N);
719 return;
720 end if;
721
722 else
723 Error_Msg_N ("missing specification for package body", N);
724 return;
725 end if;
726
727 if Is_Package_Or_Generic_Package (Spec_Id)
728 and then (Scope (Spec_Id) = Standard_Standard
729 or else Is_Child_Unit (Spec_Id))
730 and then not Unit_Requires_Body (Spec_Id)
731 then
732 if Ada_Version = Ada_83 then
733 Error_Msg_N
734 ("optional package body (not allowed in Ada 95)??", N);
735 else
736 Error_Msg_N ("spec of this package does not allow a body", N);
737 end if;
738 end if;
739 end if;
740
741 -- A [generic] package body freezes the contract of the nearest
742 -- enclosing package body and all other contracts encountered in
743 -- the same declarative part up to and excluding the package body:
744
745 -- package body Nearest_Enclosing_Package
746 -- with Refined_State => (State => Constit)
747 -- is
748 -- Constit : ...;
749
750 -- package body Freezes_Enclosing_Package_Body
751 -- with Refined_State => (State_2 => Constit_2)
752 -- is
753 -- Constit_2 : ...;
754
755 -- procedure Proc
756 -- with Refined_Depends => (Input => (Constit, Constit_2)) ...
757
758 -- This ensures that any annotations referenced by the contract of a
759 -- [generic] subprogram body declared within the current package body
760 -- are available. This form of freezing is decoupled from the usual
761 -- Freeze_xxx mechanism because it must also work in the context of
762 -- generics where normal freezing is disabled.
763
764 -- Only bodies coming from source should cause this type of freezing.
765 -- Instantiated generic bodies are excluded because their processing is
766 -- performed in a separate compilation pass which lacks enough semantic
767 -- information with respect to contract analysis. It is safe to suppress
768 -- the freezing of contracts in this case because this action already
769 -- took place at the end of the enclosing declarative part.
770
771 if Comes_From_Source (N)
772 and then not Is_Generic_Instance (Spec_Id)
773 then
774 Freeze_Previous_Contracts (N);
775 end if;
776
777 -- A package body is Ghost when the corresponding spec is Ghost. Set
778 -- the mode now to ensure that any nodes generated during analysis and
779 -- expansion are properly flagged as ignored Ghost.
780
781 Mark_And_Set_Ghost_Body (N, Spec_Id);
782
783 -- If the body completes the initial declaration of a compilation unit
784 -- which is subject to pragma Elaboration_Checks, set the model of the
785 -- pragma because it applies to all parts of the unit.
786
787 Install_Elaboration_Model (Spec_Id);
788
789 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
790 Style.Check_Identifier (Body_Id, Spec_Id);
791
792 if Is_Child_Unit (Spec_Id) then
793 if Nkind (Parent (N)) /= N_Compilation_Unit then
794 Error_Msg_NE
795 ("body of child unit& cannot be an inner package", N, Spec_Id);
796 end if;
797
798 Set_Is_Child_Unit (Body_Id);
799 end if;
800
801 -- Generic package case
802
803 if Ekind (Spec_Id) = E_Generic_Package then
804
805 -- Disable expansion and perform semantic analysis on copy. The
806 -- unannotated body will be used in all instantiations.
807
808 Body_Id := Defining_Entity (N);
809 Set_Ekind (Body_Id, E_Package_Body);
810 Set_Scope (Body_Id, Scope (Spec_Id));
811 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
812 Set_Body_Entity (Spec_Id, Body_Id);
813 Set_Spec_Entity (Body_Id, Spec_Id);
814
815 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
816 Rewrite (N, New_N);
817
818 -- Once the contents of the generic copy and the template are
819 -- swapped, do the same for their respective aspect specifications.
820
821 Exchange_Aspects (N, New_N);
822
823 -- Collect all contract-related source pragmas found within the
824 -- template and attach them to the contract of the package body.
825 -- This contract is used in the capture of global references within
826 -- annotations.
827
828 Create_Generic_Contract (N);
829
830 -- Update Body_Id to point to the copied node for the remainder of
831 -- the processing.
832
833 Body_Id := Defining_Entity (N);
834 Start_Generic;
835 end if;
836
837 -- The Body_Id is that of the copied node in the generic case, the
838 -- current node otherwise. Note that N was rewritten above, so we must
839 -- be sure to get the latest Body_Id value.
840
841 Set_Ekind (Body_Id, E_Package_Body);
842 Set_Body_Entity (Spec_Id, Body_Id);
843 Set_Spec_Entity (Body_Id, Spec_Id);
844
845 -- Defining name for the package body is not a visible entity: Only the
846 -- defining name for the declaration is visible.
847
848 Set_Etype (Body_Id, Standard_Void_Type);
849 Set_Scope (Body_Id, Scope (Spec_Id));
850 Set_Corresponding_Spec (N, Spec_Id);
851 Set_Corresponding_Body (Pack_Decl, Body_Id);
852
853 -- The body entity is not used for semantics or code generation, but
854 -- it is attached to the entity list of the enclosing scope to simplify
855 -- the listing of back-annotations for the types it main contain.
856
857 if Scope (Spec_Id) /= Standard_Standard then
858 Append_Entity (Body_Id, Scope (Spec_Id));
859 end if;
860
861 -- Indicate that we are currently compiling the body of the package
862
863 Set_In_Package_Body (Spec_Id);
864 Set_Has_Completion (Spec_Id);
865 Last_Spec_Entity := Last_Entity (Spec_Id);
866
867 if Has_Aspects (N) then
868 Analyze_Aspect_Specifications (N, Body_Id);
869 end if;
870
871 Push_Scope (Spec_Id);
872
873 -- Set SPARK_Mode only for non-generic package
874
875 if Ekind (Spec_Id) = E_Package then
876 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
877 Set_SPARK_Aux_Pragma (Body_Id, SPARK_Mode_Pragma);
878 Set_SPARK_Pragma_Inherited (Body_Id);
879 Set_SPARK_Aux_Pragma_Inherited (Body_Id);
880
881 -- A package body may be instantiated or inlined at a later pass.
882 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when
883 -- it applied to the package spec.
884
885 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
886 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
887 end if;
888 end if;
889
890 Set_Categorization_From_Pragmas (N);
891
892 Install_Visible_Declarations (Spec_Id);
893 Install_Private_Declarations (Spec_Id);
894 Install_Private_With_Clauses (Spec_Id);
895 Install_Composite_Operations (Spec_Id);
896
897 Check_Anonymous_Access_Types (Spec_Id, N);
898
899 if Ekind (Spec_Id) = E_Generic_Package then
900 Set_Use (Generic_Formal_Declarations (Pack_Decl));
901 end if;
902
903 Set_Use (Visible_Declarations (Specification (Pack_Decl)));
904 Set_Use (Private_Declarations (Specification (Pack_Decl)));
905
906 -- This is a nested package, so it may be necessary to declare certain
907 -- inherited subprograms that are not yet visible because the parent
908 -- type's subprograms are now visible.
909
910 if Ekind (Scope (Spec_Id)) = E_Package
911 and then Scope (Spec_Id) /= Standard_Standard
912 then
913 Declare_Inherited_Private_Subprograms (Spec_Id);
914 end if;
915
916 if Present (Declarations (N)) then
917 Analyze_Declarations (Declarations (N));
918 Inspect_Deferred_Constant_Completion (Declarations (N));
919 end if;
920
921 -- Verify that the SPARK_Mode of the body agrees with that of its spec
922
923 if Present (SPARK_Pragma (Body_Id)) then
924 if Present (SPARK_Aux_Pragma (Spec_Id)) then
925 if Get_SPARK_Mode_From_Annotation (SPARK_Aux_Pragma (Spec_Id)) =
926 Off
927 and then
928 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
929 then
930 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
931 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
932 Error_Msg_Sloc := Sloc (SPARK_Aux_Pragma (Spec_Id));
933 Error_Msg_NE
934 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
935 end if;
936
937 else
938 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
939 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
940 Error_Msg_Sloc := Sloc (Spec_Id);
941 Error_Msg_NE
942 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
943 end if;
944 end if;
945
946 -- Analyze_Declarations has caused freezing of all types. Now generate
947 -- bodies for RACW primitives and stream attributes, if any.
948
949 if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then
950
951 -- Attach subprogram bodies to support RACWs declared in spec
952
953 Append_RACW_Bodies (Declarations (N), Spec_Id);
954 Analyze_List (Declarations (N));
955 end if;
956
957 HSS := Handled_Statement_Sequence (N);
958
959 if Present (HSS) then
960 Process_End_Label (HSS, 't', Spec_Id);
961 Analyze (HSS);
962
963 -- Check that elaboration code in a preelaborable package body is
964 -- empty other than null statements and labels (RM 10.2.1(6)).
965
966 Validate_Null_Statement_Sequence (N);
967 end if;
968
969 Validate_Categorization_Dependency (N, Spec_Id);
970 Check_Completion (Body_Id);
971
972 -- Generate start of body reference. Note that we do this fairly late,
973 -- because the call will use In_Extended_Main_Source_Unit as a check,
974 -- and we want to make sure that Corresponding_Stub links are set
975
976 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
977
978 -- For a generic package, collect global references and mark them on
979 -- the original body so that they are not resolved again at the point
980 -- of instantiation.
981
982 if Ekind (Spec_Id) /= E_Package then
983 Save_Global_References (Original_Node (N));
984 End_Generic;
985 end if;
986
987 -- The entities of the package body have so far been chained onto the
988 -- declaration chain for the spec. That's been fine while we were in the
989 -- body, since we wanted them to be visible, but now that we are leaving
990 -- the package body, they are no longer visible, so we remove them from
991 -- the entity chain of the package spec entity, and copy them to the
992 -- entity chain of the package body entity, where they will never again
993 -- be visible.
994
995 if Present (Last_Spec_Entity) then
996 Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
997 Set_Next_Entity (Last_Spec_Entity, Empty);
998 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
999 Set_Last_Entity (Spec_Id, Last_Spec_Entity);
1000
1001 else
1002 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
1003 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
1004 Set_First_Entity (Spec_Id, Empty);
1005 Set_Last_Entity (Spec_Id, Empty);
1006 end if;
1007
1008 Update_Use_Clause_Chain;
1009 End_Package_Scope (Spec_Id);
1010
1011 -- All entities declared in body are not visible
1012
1013 declare
1014 E : Entity_Id;
1015
1016 begin
1017 E := First_Entity (Body_Id);
1018 while Present (E) loop
1019 Set_Is_Immediately_Visible (E, False);
1020 Set_Is_Potentially_Use_Visible (E, False);
1021 Set_Is_Hidden (E);
1022
1023 -- Child units may appear on the entity list (e.g. if they appear
1024 -- in the context of a subunit) but they are not body entities.
1025
1026 if not Is_Child_Unit (E) then
1027 Set_Is_Package_Body_Entity (E);
1028 end if;
1029
1030 Next_Entity (E);
1031 end loop;
1032 end;
1033
1034 Check_References (Body_Id);
1035
1036 -- For a generic unit, check that the formal parameters are referenced,
1037 -- and that local variables are used, as for regular packages.
1038
1039 if Ekind (Spec_Id) = E_Generic_Package then
1040 Check_References (Spec_Id);
1041 end if;
1042
1043 -- At this point all entities of the package body are externally visible
1044 -- to the linker as their Is_Public flag is set to True. This proactive
1045 -- approach is necessary because an inlined or a generic body for which
1046 -- code is generated in other units may need to see these entities. Cut
1047 -- down the number of global symbols that do not neet public visibility
1048 -- as this has two beneficial effects:
1049 -- (1) It makes the compilation process more efficient.
1050 -- (2) It gives the code generator more leeway to optimize within each
1051 -- unit, especially subprograms.
1052
1053 -- This is done only for top-level library packages or child units as
1054 -- the algorithm does a top-down traversal of the package body.
1055
1056 if (Scope (Spec_Id) = Standard_Standard or else Is_Child_Unit (Spec_Id))
1057 and then not Is_Generic_Unit (Spec_Id)
1058 then
1059 Hide_Public_Entities (Declarations (N));
1060 end if;
1061
1062 -- If expander is not active, then here is where we turn off the
1063 -- In_Package_Body flag, otherwise it is turned off at the end of the
1064 -- corresponding expansion routine. If this is an instance body, we need
1065 -- to qualify names of local entities, because the body may have been
1066 -- compiled as a preliminary to another instantiation.
1067
1068 if not Expander_Active then
1069 Set_In_Package_Body (Spec_Id, False);
1070
1071 if Is_Generic_Instance (Spec_Id)
1072 and then Operating_Mode = Generate_Code
1073 then
1074 Qualify_Entity_Names (N);
1075 end if;
1076 end if;
1077
1078 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
1079 Restore_Ghost_Region (Saved_GM, Saved_IGR);
1080 end Analyze_Package_Body_Helper;
1081
1082 ---------------------------------
1083 -- Analyze_Package_Declaration --
1084 ---------------------------------
1085
1086 procedure Analyze_Package_Declaration (N : Node_Id) is
1087 Id : constant Node_Id := Defining_Entity (N);
1088
1089 Is_Comp_Unit : constant Boolean :=
1090 Nkind (Parent (N)) = N_Compilation_Unit;
1091
1092 Body_Required : Boolean;
1093 -- True when this package declaration requires a corresponding body
1094
1095 begin
1096 if Debug_Flag_C then
1097 Write_Str ("==> package spec ");
1098 Write_Name (Chars (Id));
1099 Write_Str (" from ");
1100 Write_Location (Sloc (N));
1101 Write_Eol;
1102 Indent;
1103 end if;
1104
1105 Generate_Definition (Id);
1106 Enter_Name (Id);
1107 Set_Ekind (Id, E_Package);
1108 Set_Etype (Id, Standard_Void_Type);
1109
1110 -- Set SPARK_Mode from context
1111
1112 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
1113 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
1114 Set_SPARK_Pragma_Inherited (Id);
1115 Set_SPARK_Aux_Pragma_Inherited (Id);
1116
1117 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
1118 -- the body of this package is instantiated or inlined later and out of
1119 -- context. The body uses this attribute to restore the value of the
1120 -- global flag.
1121
1122 if Ignore_SPARK_Mode_Pragmas_In_Instance then
1123 Set_Ignore_SPARK_Mode_Pragmas (Id);
1124 end if;
1125
1126 -- Analyze aspect specifications immediately, since we need to recognize
1127 -- things like Pure early enough to diagnose violations during analysis.
1128
1129 if Has_Aspects (N) then
1130 Analyze_Aspect_Specifications (N, Id);
1131 end if;
1132
1133 -- Ada 2005 (AI-217): Check if the package has been illegally named in
1134 -- a limited-with clause of its own context. In this case the error has
1135 -- been previously notified by Analyze_Context.
1136
1137 -- limited with Pkg; -- ERROR
1138 -- package Pkg is ...
1139
1140 if From_Limited_With (Id) then
1141 return;
1142 end if;
1143
1144 Push_Scope (Id);
1145
1146 Set_Is_Pure (Id, Is_Pure (Enclosing_Lib_Unit_Entity));
1147 Set_Categorization_From_Pragmas (N);
1148
1149 Analyze (Specification (N));
1150 Validate_Categorization_Dependency (N, Id);
1151
1152 -- Determine whether the package requires a body. Abstract states are
1153 -- intentionally ignored because they do require refinement which can
1154 -- only come in a body, but at the same time they do not force the need
1155 -- for a body on their own (SPARK RM 7.1.4(4) and 7.2.2(3)).
1156
1157 Body_Required := Unit_Requires_Body (Id);
1158
1159 if not Body_Required then
1160
1161 -- If the package spec does not require an explicit body, then there
1162 -- are not entities requiring completion in the language sense. Call
1163 -- Check_Completion now to ensure that nested package declarations
1164 -- that require an implicit body get one. (In the case where a body
1165 -- is required, Check_Completion is called at the end of the body's
1166 -- declarative part.)
1167
1168 Check_Completion;
1169
1170 -- If the package spec does not require an explicit body, then all
1171 -- abstract states declared in nested packages cannot possibly get
1172 -- a proper refinement (SPARK RM 7.2.2(3)). This check is performed
1173 -- only when the compilation unit is the main unit to allow for
1174 -- modular SPARK analysis where packages do not necessarily have
1175 -- bodies.
1176
1177 if Is_Comp_Unit then
1178 Check_State_Refinements
1179 (Context => N,
1180 Is_Main_Unit => Parent (N) = Cunit (Main_Unit));
1181 end if;
1182 end if;
1183
1184 -- Set Body_Required indication on the compilation unit node
1185
1186 if Is_Comp_Unit then
1187 Set_Body_Required (Parent (N), Body_Required);
1188
1189 if Legacy_Elaboration_Checks and not Body_Required then
1190 Set_Suppress_Elaboration_Warnings (Id);
1191 end if;
1192 end if;
1193
1194 End_Package_Scope (Id);
1195
1196 -- For the declaration of a library unit that is a remote types package,
1197 -- check legality rules regarding availability of stream attributes for
1198 -- types that contain non-remote access values. This subprogram performs
1199 -- visibility tests that rely on the fact that we have exited the scope
1200 -- of Id.
1201
1202 if Is_Comp_Unit then
1203 Validate_RT_RAT_Component (N);
1204 end if;
1205
1206 if Debug_Flag_C then
1207 Outdent;
1208 Write_Str ("<== package spec ");
1209 Write_Name (Chars (Id));
1210 Write_Str (" from ");
1211 Write_Location (Sloc (N));
1212 Write_Eol;
1213 end if;
1214 end Analyze_Package_Declaration;
1215
1216 -----------------------------------
1217 -- Analyze_Package_Specification --
1218 -----------------------------------
1219
1220 -- Note that this code is shared for the analysis of generic package specs
1221 -- (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
1222
1223 procedure Analyze_Package_Specification (N : Node_Id) is
1224 Id : constant Entity_Id := Defining_Entity (N);
1225 Orig_Decl : constant Node_Id := Original_Node (Parent (N));
1226 Vis_Decls : constant List_Id := Visible_Declarations (N);
1227 Priv_Decls : constant List_Id := Private_Declarations (N);
1228 E : Entity_Id;
1229 L : Entity_Id;
1230 Public_Child : Boolean;
1231
1232 Private_With_Clauses_Installed : Boolean := False;
1233 -- In Ada 2005, private with_clauses are visible in the private part
1234 -- of a nested package, even if it appears in the public part of the
1235 -- enclosing package. This requires a separate step to install these
1236 -- private_with_clauses, and remove them at the end of the nested
1237 -- package.
1238
1239 procedure Check_One_Tagged_Type_Or_Extension_At_Most;
1240 -- Issue an error in SPARK mode if a package specification contains
1241 -- more than one tagged type or type extension.
1242
1243 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
1244 -- Clears constant indications (Never_Set_In_Source, Constant_Value, and
1245 -- Is_True_Constant) on all variables that are entities of Id, and on
1246 -- the chain whose first element is FE. A recursive call is made for all
1247 -- packages and generic packages.
1248
1249 procedure Generate_Parent_References;
1250 -- For a child unit, generate references to parent units, for
1251 -- GPS navigation purposes.
1252
1253 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
1254 -- Child and Unit are entities of compilation units. True if Child
1255 -- is a public child of Parent as defined in 10.1.1
1256
1257 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
1258 -- Reject completion of an incomplete or private type declarations
1259 -- having a known discriminant part by an unchecked union.
1260
1261 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
1262 -- Given the package entity of a generic package instantiation or
1263 -- formal package whose corresponding generic is a child unit, installs
1264 -- the private declarations of each of the child unit's parents.
1265 -- This has to be done at the point of entering the instance package's
1266 -- private part rather than being done in Sem_Ch12.Install_Parent
1267 -- (which is where the parents' visible declarations are installed).
1268
1269 ------------------------------------------------
1270 -- Check_One_Tagged_Type_Or_Extension_At_Most --
1271 ------------------------------------------------
1272
1273 procedure Check_One_Tagged_Type_Or_Extension_At_Most is
1274 Previous : Node_Id;
1275
1276 procedure Check_Decls (Decls : List_Id);
1277 -- Check that either Previous is Empty and Decls does not contain
1278 -- more than one tagged type or type extension, or Previous is
1279 -- already set and Decls contains no tagged type or type extension.
1280
1281 -----------------
1282 -- Check_Decls --
1283 -----------------
1284
1285 procedure Check_Decls (Decls : List_Id) is
1286 Decl : Node_Id;
1287
1288 begin
1289 Decl := First (Decls);
1290 while Present (Decl) loop
1291 if Nkind (Decl) = N_Full_Type_Declaration
1292 and then Is_Tagged_Type (Defining_Identifier (Decl))
1293 then
1294 if No (Previous) then
1295 Previous := Decl;
1296
1297 else
1298 Error_Msg_Sloc := Sloc (Previous);
1299 Check_SPARK_05_Restriction
1300 ("at most one tagged type or type extension allowed",
1301 "\\ previous declaration#",
1302 Decl);
1303 end if;
1304 end if;
1305
1306 Next (Decl);
1307 end loop;
1308 end Check_Decls;
1309
1310 -- Start of processing for Check_One_Tagged_Type_Or_Extension_At_Most
1311
1312 begin
1313 Previous := Empty;
1314 Check_Decls (Vis_Decls);
1315
1316 if Present (Priv_Decls) then
1317 Check_Decls (Priv_Decls);
1318 end if;
1319 end Check_One_Tagged_Type_Or_Extension_At_Most;
1320
1321 ---------------------
1322 -- Clear_Constants --
1323 ---------------------
1324
1325 procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
1326 E : Entity_Id;
1327
1328 begin
1329 -- Ignore package renamings, not interesting and they can cause self
1330 -- referential loops in the code below.
1331
1332 if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
1333 return;
1334 end if;
1335
1336 -- Note: in the loop below, the check for Next_Entity pointing back
1337 -- to the package entity may seem odd, but it is needed, because a
1338 -- package can contain a renaming declaration to itself, and such
1339 -- renamings are generated automatically within package instances.
1340
1341 E := FE;
1342 while Present (E) and then E /= Id loop
1343 if Is_Assignable (E) then
1344 Set_Never_Set_In_Source (E, False);
1345 Set_Is_True_Constant (E, False);
1346 Set_Current_Value (E, Empty);
1347 Set_Is_Known_Null (E, False);
1348 Set_Last_Assignment (E, Empty);
1349
1350 if not Can_Never_Be_Null (E) then
1351 Set_Is_Known_Non_Null (E, False);
1352 end if;
1353
1354 elsif Is_Package_Or_Generic_Package (E) then
1355 Clear_Constants (E, First_Entity (E));
1356 Clear_Constants (E, First_Private_Entity (E));
1357 end if;
1358
1359 Next_Entity (E);
1360 end loop;
1361 end Clear_Constants;
1362
1363 --------------------------------
1364 -- Generate_Parent_References --
1365 --------------------------------
1366
1367 procedure Generate_Parent_References is
1368 Decl : constant Node_Id := Parent (N);
1369
1370 begin
1371 if Id = Cunit_Entity (Main_Unit)
1372 or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
1373 then
1374 Generate_Reference (Id, Scope (Id), 'k', False);
1375
1376 elsif not Nkind_In (Unit (Cunit (Main_Unit)), N_Subprogram_Body,
1377 N_Subunit)
1378 then
1379 -- If current unit is an ancestor of main unit, generate a
1380 -- reference to its own parent.
1381
1382 declare
1383 U : Node_Id;
1384 Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
1385
1386 begin
1387 if Nkind (Main_Spec) = N_Package_Body then
1388 Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
1389 end if;
1390
1391 U := Parent_Spec (Main_Spec);
1392 while Present (U) loop
1393 if U = Parent (Decl) then
1394 Generate_Reference (Id, Scope (Id), 'k', False);
1395 exit;
1396
1397 elsif Nkind (Unit (U)) = N_Package_Body then
1398 exit;
1399
1400 else
1401 U := Parent_Spec (Unit (U));
1402 end if;
1403 end loop;
1404 end;
1405 end if;
1406 end Generate_Parent_References;
1407
1408 ---------------------
1409 -- Is_Public_Child --
1410 ---------------------
1411
1412 function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
1413 begin
1414 if not Is_Private_Descendant (Child) then
1415 return True;
1416 else
1417 if Child = Unit then
1418 return not Private_Present (
1419 Parent (Unit_Declaration_Node (Child)));
1420 else
1421 return Is_Public_Child (Scope (Child), Unit);
1422 end if;
1423 end if;
1424 end Is_Public_Child;
1425
1426 ----------------------------------------
1427 -- Inspect_Unchecked_Union_Completion --
1428 ----------------------------------------
1429
1430 procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
1431 Decl : Node_Id;
1432
1433 begin
1434 Decl := First (Decls);
1435 while Present (Decl) loop
1436
1437 -- We are looking at an incomplete or private type declaration
1438 -- with a known_discriminant_part whose full view is an
1439 -- Unchecked_Union. The seemingly useless check with Is_Type
1440 -- prevents cascaded errors when routines defined only for type
1441 -- entities are called with non-type entities.
1442
1443 if Nkind_In (Decl, N_Incomplete_Type_Declaration,
1444 N_Private_Type_Declaration)
1445 and then Is_Type (Defining_Identifier (Decl))
1446 and then Has_Discriminants (Defining_Identifier (Decl))
1447 and then Present (Full_View (Defining_Identifier (Decl)))
1448 and then
1449 Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
1450 then
1451 Error_Msg_N
1452 ("completion of discriminated partial view "
1453 & "cannot be an unchecked union",
1454 Full_View (Defining_Identifier (Decl)));
1455 end if;
1456
1457 Next (Decl);
1458 end loop;
1459 end Inspect_Unchecked_Union_Completion;
1460
1461 -----------------------------------------
1462 -- Install_Parent_Private_Declarations --
1463 -----------------------------------------
1464
1465 procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
1466 Inst_Par : Entity_Id;
1467 Gen_Par : Entity_Id;
1468 Inst_Node : Node_Id;
1469
1470 begin
1471 Inst_Par := Inst_Id;
1472
1473 Gen_Par :=
1474 Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
1475 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
1476 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
1477
1478 if Nkind_In (Inst_Node, N_Package_Instantiation,
1479 N_Formal_Package_Declaration)
1480 and then Nkind (Name (Inst_Node)) = N_Expanded_Name
1481 then
1482 Inst_Par := Entity (Prefix (Name (Inst_Node)));
1483
1484 if Present (Renamed_Entity (Inst_Par)) then
1485 Inst_Par := Renamed_Entity (Inst_Par);
1486 end if;
1487
1488 Gen_Par :=
1489 Generic_Parent
1490 (Specification (Unit_Declaration_Node (Inst_Par)));
1491
1492 -- Install the private declarations and private use clauses
1493 -- of a parent instance of the child instance, unless the
1494 -- parent instance private declarations have already been
1495 -- installed earlier in Analyze_Package_Specification, which
1496 -- happens when a generic child is instantiated, and the
1497 -- instance is a child of the parent instance.
1498
1499 -- Installing the use clauses of the parent instance twice
1500 -- is both unnecessary and wrong, because it would cause the
1501 -- clauses to be chained to themselves in the use clauses
1502 -- list of the scope stack entry. That in turn would cause
1503 -- an endless loop from End_Use_Clauses upon scope exit.
1504
1505 -- The parent is now fully visible. It may be a hidden open
1506 -- scope if we are currently compiling some child instance
1507 -- declared within it, but while the current instance is being
1508 -- compiled the parent is immediately visible. In particular
1509 -- its entities must remain visible if a stack save/restore
1510 -- takes place through a call to Rtsfind.
1511
1512 if Present (Gen_Par) then
1513 if not In_Private_Part (Inst_Par) then
1514 Install_Private_Declarations (Inst_Par);
1515 Set_Use (Private_Declarations
1516 (Specification
1517 (Unit_Declaration_Node (Inst_Par))));
1518 Set_Is_Hidden_Open_Scope (Inst_Par, False);
1519 end if;
1520
1521 -- If we've reached the end of the generic instance parents,
1522 -- then finish off by looping through the nongeneric parents
1523 -- and installing their private declarations.
1524
1525 -- If one of the non-generic parents is itself on the scope
1526 -- stack, do not install its private declarations: they are
1527 -- installed in due time when the private part of that parent
1528 -- is analyzed.
1529
1530 else
1531 while Present (Inst_Par)
1532 and then Inst_Par /= Standard_Standard
1533 and then (not In_Open_Scopes (Inst_Par)
1534 or else not In_Private_Part (Inst_Par))
1535 loop
1536 if Nkind (Inst_Node) = N_Formal_Package_Declaration
1537 or else
1538 not Is_Ancestor_Package
1539 (Inst_Par, Cunit_Entity (Current_Sem_Unit))
1540 then
1541 Install_Private_Declarations (Inst_Par);
1542 Set_Use
1543 (Private_Declarations
1544 (Specification
1545 (Unit_Declaration_Node (Inst_Par))));
1546 Inst_Par := Scope (Inst_Par);
1547 else
1548 exit;
1549 end if;
1550 end loop;
1551
1552 exit;
1553 end if;
1554
1555 else
1556 exit;
1557 end if;
1558 end loop;
1559 end Install_Parent_Private_Declarations;
1560
1561 -- Start of processing for Analyze_Package_Specification
1562
1563 begin
1564 if Present (Vis_Decls) then
1565 Analyze_Declarations (Vis_Decls);
1566 end if;
1567
1568 -- Inspect the entities defined in the package and ensure that all
1569 -- incomplete types have received full declarations. Build default
1570 -- initial condition and invariant procedures for all qualifying types.
1571
1572 E := First_Entity (Id);
1573 while Present (E) loop
1574
1575 -- Check on incomplete types
1576
1577 -- AI05-0213: A formal incomplete type has no completion, and neither
1578 -- does the corresponding subtype in an instance.
1579
1580 if Is_Incomplete_Type (E)
1581 and then No (Full_View (E))
1582 and then not Is_Generic_Type (E)
1583 and then not From_Limited_With (E)
1584 and then not Is_Generic_Actual_Type (E)
1585 then
1586 Error_Msg_N ("no declaration in visible part for incomplete}", E);
1587 end if;
1588
1589 Next_Entity (E);
1590 end loop;
1591
1592 if Is_Remote_Call_Interface (Id)
1593 and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1594 then
1595 Validate_RCI_Declarations (Id);
1596 end if;
1597
1598 -- Save global references in the visible declarations, before installing
1599 -- private declarations of parent unit if there is one, because the
1600 -- privacy status of types defined in the parent will change. This is
1601 -- only relevant for generic child units, but is done in all cases for
1602 -- uniformity.
1603
1604 if Ekind (Id) = E_Generic_Package
1605 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1606 then
1607 declare
1608 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1609 Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1610
1611 begin
1612 -- Insert the freezing nodes after the visible declarations to
1613 -- ensure that we analyze its aspects; needed to ensure that
1614 -- global entities referenced in the aspects are properly handled.
1615
1616 if Ada_Version >= Ada_2012
1617 and then Is_Non_Empty_List (Vis_Decls)
1618 and then Is_Empty_List (Priv_Decls)
1619 then
1620 Insert_List_After_And_Analyze
1621 (Last (Vis_Decls), Freeze_Entity (Id, Last (Vis_Decls)));
1622 end if;
1623
1624 Set_Private_Declarations (Orig_Spec, Empty_List);
1625 Save_Global_References (Orig_Decl);
1626 Set_Private_Declarations (Orig_Spec, Save_Priv);
1627 end;
1628 end if;
1629
1630 -- If package is a public child unit, then make the private declarations
1631 -- of the parent visible.
1632
1633 Public_Child := False;
1634
1635 declare
1636 Par : Entity_Id;
1637 Pack_Decl : Node_Id;
1638 Par_Spec : Node_Id;
1639
1640 begin
1641 Par := Id;
1642 Par_Spec := Parent_Spec (Parent (N));
1643
1644 -- If the package is formal package of an enclosing generic, it is
1645 -- transformed into a local generic declaration, and compiled to make
1646 -- its spec available. We need to retrieve the original generic to
1647 -- determine whether it is a child unit, and install its parents.
1648
1649 if No (Par_Spec)
1650 and then
1651 Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1652 then
1653 Par := Entity (Name (Original_Node (Parent (N))));
1654 Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1655 end if;
1656
1657 if Present (Par_Spec) then
1658 Generate_Parent_References;
1659
1660 while Scope (Par) /= Standard_Standard
1661 and then Is_Public_Child (Id, Par)
1662 and then In_Open_Scopes (Par)
1663 loop
1664 Public_Child := True;
1665 Par := Scope (Par);
1666 Install_Private_Declarations (Par);
1667 Install_Private_With_Clauses (Par);
1668 Pack_Decl := Unit_Declaration_Node (Par);
1669 Set_Use (Private_Declarations (Specification (Pack_Decl)));
1670 end loop;
1671 end if;
1672 end;
1673
1674 if Is_Compilation_Unit (Id) then
1675 Install_Private_With_Clauses (Id);
1676 else
1677 -- The current compilation unit may include private with_clauses,
1678 -- which are visible in the private part of the current nested
1679 -- package, and have to be installed now. This is not done for
1680 -- nested instantiations, where the private with_clauses of the
1681 -- enclosing unit have no effect once the instantiation info is
1682 -- established and we start analyzing the package declaration.
1683
1684 declare
1685 Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1686 begin
1687 if Is_Package_Or_Generic_Package (Comp_Unit)
1688 and then not In_Private_Part (Comp_Unit)
1689 and then not In_Instance
1690 then
1691 Install_Private_With_Clauses (Comp_Unit);
1692 Private_With_Clauses_Installed := True;
1693 end if;
1694 end;
1695 end if;
1696
1697 -- If this is a package associated with a generic instance or formal
1698 -- package, then the private declarations of each of the generic's
1699 -- parents must be installed at this point.
1700
1701 if Is_Generic_Instance (Id) then
1702 Install_Parent_Private_Declarations (Id);
1703 end if;
1704
1705 -- Analyze private part if present. The flag In_Private_Part is reset
1706 -- in End_Package_Scope.
1707
1708 L := Last_Entity (Id);
1709
1710 if Present (Priv_Decls) then
1711 Set_In_Private_Part (Id);
1712
1713 -- Upon entering a public child's private part, it may be necessary
1714 -- to declare subprograms that were derived in the package's visible
1715 -- part but not yet made visible.
1716
1717 if Public_Child then
1718 Declare_Inherited_Private_Subprograms (Id);
1719 end if;
1720
1721 Analyze_Declarations (Priv_Decls);
1722
1723 -- Check the private declarations for incomplete deferred constants
1724
1725 Inspect_Deferred_Constant_Completion (Priv_Decls);
1726
1727 -- The first private entity is the immediate follower of the last
1728 -- visible entity, if there was one.
1729
1730 if Present (L) then
1731 Set_First_Private_Entity (Id, Next_Entity (L));
1732 else
1733 Set_First_Private_Entity (Id, First_Entity (Id));
1734 end if;
1735
1736 -- There may be inherited private subprograms that need to be declared,
1737 -- even in the absence of an explicit private part. If there are any
1738 -- public declarations in the package and the package is a public child
1739 -- unit, then an implicit private part is assumed.
1740
1741 elsif Present (L) and then Public_Child then
1742 Set_In_Private_Part (Id);
1743 Declare_Inherited_Private_Subprograms (Id);
1744 Set_First_Private_Entity (Id, Next_Entity (L));
1745 end if;
1746
1747 E := First_Entity (Id);
1748 while Present (E) loop
1749
1750 -- Check rule of 3.6(11), which in general requires waiting till all
1751 -- full types have been seen.
1752
1753 if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1754 Check_Aliased_Component_Types (E);
1755 end if;
1756
1757 -- Check preelaborable initialization for full type completing a
1758 -- private type for which pragma Preelaborable_Initialization given.
1759
1760 if Is_Type (E)
1761 and then Must_Have_Preelab_Init (E)
1762 and then not Has_Preelaborable_Initialization (E)
1763 then
1764 Error_Msg_N
1765 ("full view of & does not have preelaborable initialization", E);
1766 end if;
1767
1768 Next_Entity (E);
1769 end loop;
1770
1771 -- Ada 2005 (AI-216): The completion of an incomplete or private type
1772 -- declaration having a known_discriminant_part shall not be an
1773 -- unchecked union type.
1774
1775 if Present (Vis_Decls) then
1776 Inspect_Unchecked_Union_Completion (Vis_Decls);
1777 end if;
1778
1779 if Present (Priv_Decls) then
1780 Inspect_Unchecked_Union_Completion (Priv_Decls);
1781 end if;
1782
1783 if Ekind (Id) = E_Generic_Package
1784 and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1785 and then Present (Priv_Decls)
1786 then
1787 -- Save global references in private declarations, ignoring the
1788 -- visible declarations that were processed earlier.
1789
1790 declare
1791 Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1792 Save_Vis : constant List_Id := Visible_Declarations (Orig_Spec);
1793 Save_Form : constant List_Id :=
1794 Generic_Formal_Declarations (Orig_Decl);
1795
1796 begin
1797 -- Insert the freezing nodes after the private declarations to
1798 -- ensure that we analyze its aspects; needed to ensure that
1799 -- global entities referenced in the aspects are properly handled.
1800
1801 if Ada_Version >= Ada_2012
1802 and then Is_Non_Empty_List (Priv_Decls)
1803 then
1804 Insert_List_After_And_Analyze
1805 (Last (Priv_Decls), Freeze_Entity (Id, Last (Priv_Decls)));
1806 end if;
1807
1808 Set_Visible_Declarations (Orig_Spec, Empty_List);
1809 Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1810 Save_Global_References (Orig_Decl);
1811 Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1812 Set_Visible_Declarations (Orig_Spec, Save_Vis);
1813 end;
1814 end if;
1815
1816 Process_End_Label (N, 'e', Id);
1817
1818 -- Remove private_with_clauses of enclosing compilation unit, if they
1819 -- were installed.
1820
1821 if Private_With_Clauses_Installed then
1822 Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
1823 end if;
1824
1825 -- For the case of a library level package, we must go through all the
1826 -- entities clearing the indications that the value may be constant and
1827 -- not modified. Why? Because any client of this package may modify
1828 -- these values freely from anywhere. This also applies to any nested
1829 -- packages or generic packages.
1830
1831 -- For now we unconditionally clear constants for packages that are
1832 -- instances of generic packages. The reason is that we do not have the
1833 -- body yet, and we otherwise think things are unreferenced when they
1834 -- are not. This should be fixed sometime (the effect is not terrible,
1835 -- we just lose some warnings, and also some cases of value propagation)
1836 -- ???
1837
1838 if Is_Library_Level_Entity (Id)
1839 or else Is_Generic_Instance (Id)
1840 then
1841 Clear_Constants (Id, First_Entity (Id));
1842 Clear_Constants (Id, First_Private_Entity (Id));
1843 end if;
1844
1845 -- Issue an error in SPARK mode if a package specification contains
1846 -- more than one tagged type or type extension.
1847
1848 Check_One_Tagged_Type_Or_Extension_At_Most;
1849
1850 -- Output relevant information as to why the package requires a body.
1851 -- Do not consider generated packages as this exposes internal symbols
1852 -- and leads to confusing messages.
1853
1854 if List_Body_Required_Info
1855 and then In_Extended_Main_Source_Unit (Id)
1856 and then Unit_Requires_Body (Id)
1857 and then Comes_From_Source (Id)
1858 then
1859 Unit_Requires_Body_Info (Id);
1860 end if;
1861
1862 -- Nested package specs that do not require bodies are not checked for
1863 -- ineffective use clauses due to the possbility of subunits. This is
1864 -- because at this stage it is impossible to tell whether there will be
1865 -- a separate body.
1866
1867 if not Unit_Requires_Body (Id)
1868 and then Is_Compilation_Unit (Id)
1869 and then not Is_Private_Descendant (Id)
1870 then
1871 Update_Use_Clause_Chain;
1872 end if;
1873 end Analyze_Package_Specification;
1874
1875 --------------------------------------
1876 -- Analyze_Private_Type_Declaration --
1877 --------------------------------------
1878
1879 procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1880 Id : constant Entity_Id := Defining_Identifier (N);
1881 PF : constant Boolean := Is_Pure (Enclosing_Lib_Unit_Entity);
1882
1883 begin
1884 Generate_Definition (Id);
1885 Set_Is_Pure (Id, PF);
1886 Init_Size_Align (Id);
1887
1888 if not Is_Package_Or_Generic_Package (Current_Scope)
1889 or else In_Private_Part (Current_Scope)
1890 then
1891 Error_Msg_N ("invalid context for private declaration", N);
1892 end if;
1893
1894 New_Private_Type (N, Id, N);
1895 Set_Depends_On_Private (Id);
1896
1897 -- Set the SPARK mode from the current context
1898
1899 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
1900 Set_SPARK_Pragma_Inherited (Id);
1901
1902 if Has_Aspects (N) then
1903 Analyze_Aspect_Specifications (N, Id);
1904 end if;
1905 end Analyze_Private_Type_Declaration;
1906
1907 ----------------------------------
1908 -- Check_Anonymous_Access_Types --
1909 ----------------------------------
1910
1911 procedure Check_Anonymous_Access_Types
1912 (Spec_Id : Entity_Id;
1913 P_Body : Node_Id)
1914 is
1915 E : Entity_Id;
1916 IR : Node_Id;
1917
1918 begin
1919 -- Itype references are only needed by gigi, to force elaboration of
1920 -- itypes. In the absence of code generation, they are not needed.
1921
1922 if not Expander_Active then
1923 return;
1924 end if;
1925
1926 E := First_Entity (Spec_Id);
1927 while Present (E) loop
1928 if Ekind (E) = E_Anonymous_Access_Type
1929 and then From_Limited_With (E)
1930 then
1931 IR := Make_Itype_Reference (Sloc (P_Body));
1932 Set_Itype (IR, E);
1933
1934 if No (Declarations (P_Body)) then
1935 Set_Declarations (P_Body, New_List (IR));
1936 else
1937 Prepend (IR, Declarations (P_Body));
1938 end if;
1939 end if;
1940
1941 Next_Entity (E);
1942 end loop;
1943 end Check_Anonymous_Access_Types;
1944
1945 -------------------------------------------
1946 -- Declare_Inherited_Private_Subprograms --
1947 -------------------------------------------
1948
1949 procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1950
1951 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1952 -- Check whether an inherited subprogram S is an operation of an
1953 -- untagged derived type T.
1954
1955 ---------------------
1956 -- Is_Primitive_Of --
1957 ---------------------
1958
1959 function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1960 Formal : Entity_Id;
1961
1962 begin
1963 -- If the full view is a scalar type, the type is the anonymous base
1964 -- type, but the operation mentions the first subtype, so check the
1965 -- signature against the base type.
1966
1967 if Base_Type (Etype (S)) = Base_Type (T) then
1968 return True;
1969
1970 else
1971 Formal := First_Formal (S);
1972 while Present (Formal) loop
1973 if Base_Type (Etype (Formal)) = Base_Type (T) then
1974 return True;
1975 end if;
1976
1977 Next_Formal (Formal);
1978 end loop;
1979
1980 return False;
1981 end if;
1982 end Is_Primitive_Of;
1983
1984 -- Local variables
1985
1986 E : Entity_Id;
1987 Op_List : Elist_Id;
1988 Op_Elmt : Elmt_Id;
1989 Op_Elmt_2 : Elmt_Id;
1990 Prim_Op : Entity_Id;
1991 New_Op : Entity_Id := Empty;
1992 Parent_Subp : Entity_Id;
1993 Tag : Entity_Id;
1994
1995 -- Start of processing for Declare_Inherited_Private_Subprograms
1996
1997 begin
1998 E := First_Entity (Id);
1999 while Present (E) loop
2000
2001 -- If the entity is a nonprivate type extension whose parent type
2002 -- is declared in an open scope, then the type may have inherited
2003 -- operations that now need to be made visible. Ditto if the entity
2004 -- is a formal derived type in a child unit.
2005
2006 if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
2007 or else
2008 (Nkind (Parent (E)) = N_Private_Extension_Declaration
2009 and then Is_Generic_Type (E)))
2010 and then In_Open_Scopes (Scope (Etype (E)))
2011 and then Is_Base_Type (E)
2012 then
2013 if Is_Tagged_Type (E) then
2014 Op_List := Primitive_Operations (E);
2015 New_Op := Empty;
2016 Tag := First_Tag_Component (E);
2017
2018 Op_Elmt := First_Elmt (Op_List);
2019 while Present (Op_Elmt) loop
2020 Prim_Op := Node (Op_Elmt);
2021
2022 -- Search primitives that are implicit operations with an
2023 -- internal name whose parent operation has a normal name.
2024
2025 if Present (Alias (Prim_Op))
2026 and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
2027 and then not Comes_From_Source (Prim_Op)
2028 and then Is_Internal_Name (Chars (Prim_Op))
2029 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
2030 then
2031 Parent_Subp := Alias (Prim_Op);
2032
2033 -- Case 1: Check if the type has also an explicit
2034 -- overriding for this primitive.
2035
2036 Op_Elmt_2 := Next_Elmt (Op_Elmt);
2037 while Present (Op_Elmt_2) loop
2038
2039 -- Skip entities with attribute Interface_Alias since
2040 -- they are not overriding primitives (these entities
2041 -- link an interface primitive with their covering
2042 -- primitive)
2043
2044 if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
2045 and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
2046 and then No (Interface_Alias (Node (Op_Elmt_2)))
2047 then
2048 -- The private inherited operation has been
2049 -- overridden by an explicit subprogram:
2050 -- replace the former by the latter.
2051
2052 New_Op := Node (Op_Elmt_2);
2053 Replace_Elmt (Op_Elmt, New_Op);
2054 Remove_Elmt (Op_List, Op_Elmt_2);
2055 Set_Overridden_Operation (New_Op, Parent_Subp);
2056
2057 -- We don't need to inherit its dispatching slot.
2058 -- Set_All_DT_Position has previously ensured that
2059 -- the same slot was assigned to the two primitives
2060
2061 if Present (Tag)
2062 and then Present (DTC_Entity (New_Op))
2063 and then Present (DTC_Entity (Prim_Op))
2064 then
2065 pragma Assert
2066 (DT_Position (New_Op) = DT_Position (Prim_Op));
2067 null;
2068 end if;
2069
2070 goto Next_Primitive;
2071 end if;
2072
2073 Next_Elmt (Op_Elmt_2);
2074 end loop;
2075
2076 -- Case 2: We have not found any explicit overriding and
2077 -- hence we need to declare the operation (i.e., make it
2078 -- visible).
2079
2080 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2081
2082 -- Inherit the dispatching slot if E is already frozen
2083
2084 if Is_Frozen (E)
2085 and then Present (DTC_Entity (Alias (Prim_Op)))
2086 then
2087 Set_DTC_Entity_Value (E, New_Op);
2088 Set_DT_Position_Value (New_Op,
2089 DT_Position (Alias (Prim_Op)));
2090 end if;
2091
2092 pragma Assert
2093 (Is_Dispatching_Operation (New_Op)
2094 and then Node (Last_Elmt (Op_List)) = New_Op);
2095
2096 -- Substitute the new operation for the old one in the
2097 -- type's primitive operations list. Since the new
2098 -- operation was also just added to the end of list,
2099 -- the last element must be removed.
2100
2101 -- (Question: is there a simpler way of declaring the
2102 -- operation, say by just replacing the name of the
2103 -- earlier operation, reentering it in the in the symbol
2104 -- table (how?), and marking it as private???)
2105
2106 Replace_Elmt (Op_Elmt, New_Op);
2107 Remove_Last_Elmt (Op_List);
2108 end if;
2109
2110 <<Next_Primitive>>
2111 Next_Elmt (Op_Elmt);
2112 end loop;
2113
2114 -- Generate listing showing the contents of the dispatch table
2115
2116 if Debug_Flag_ZZ then
2117 Write_DT (E);
2118 end if;
2119
2120 else
2121 -- For untagged type, scan forward to locate inherited hidden
2122 -- operations.
2123
2124 Prim_Op := Next_Entity (E);
2125 while Present (Prim_Op) loop
2126 if Is_Subprogram (Prim_Op)
2127 and then Present (Alias (Prim_Op))
2128 and then not Comes_From_Source (Prim_Op)
2129 and then Is_Internal_Name (Chars (Prim_Op))
2130 and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
2131 and then Is_Primitive_Of (E, Prim_Op)
2132 then
2133 Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
2134 end if;
2135
2136 Next_Entity (Prim_Op);
2137
2138 -- Derived operations appear immediately after the type
2139 -- declaration (or the following subtype indication for
2140 -- a derived scalar type). Further declarations cannot
2141 -- include inherited operations of the type.
2142
2143 if Present (Prim_Op) then
2144 exit when Ekind (Prim_Op) not in Overloadable_Kind;
2145 end if;
2146 end loop;
2147 end if;
2148 end if;
2149
2150 Next_Entity (E);
2151 end loop;
2152 end Declare_Inherited_Private_Subprograms;
2153
2154 -----------------------
2155 -- End_Package_Scope --
2156 -----------------------
2157
2158 procedure End_Package_Scope (P : Entity_Id) is
2159 begin
2160 Uninstall_Declarations (P);
2161 Pop_Scope;
2162 end End_Package_Scope;
2163
2164 ---------------------------
2165 -- Exchange_Declarations --
2166 ---------------------------
2167
2168 procedure Exchange_Declarations (Id : Entity_Id) is
2169 Full_Id : constant Entity_Id := Full_View (Id);
2170 H1 : constant Entity_Id := Homonym (Id);
2171 Next1 : constant Entity_Id := Next_Entity (Id);
2172 H2 : Entity_Id;
2173 Next2 : Entity_Id;
2174
2175 begin
2176 -- If missing full declaration for type, nothing to exchange
2177
2178 if No (Full_Id) then
2179 return;
2180 end if;
2181
2182 -- Otherwise complete the exchange, and preserve semantic links
2183
2184 Next2 := Next_Entity (Full_Id);
2185 H2 := Homonym (Full_Id);
2186
2187 -- Reset full declaration pointer to reflect the switched entities and
2188 -- readjust the next entity chains.
2189
2190 Exchange_Entities (Id, Full_Id);
2191
2192 Link_Entities (Id, Next1);
2193 Set_Homonym (Id, H1);
2194
2195 Set_Full_View (Full_Id, Id);
2196 Link_Entities (Full_Id, Next2);
2197 Set_Homonym (Full_Id, H2);
2198 end Exchange_Declarations;
2199
2200 ----------------------------
2201 -- Install_Package_Entity --
2202 ----------------------------
2203
2204 procedure Install_Package_Entity (Id : Entity_Id) is
2205 begin
2206 if not Is_Internal (Id) then
2207 if Debug_Flag_E then
2208 Write_Str ("Install: ");
2209 Write_Name (Chars (Id));
2210 Write_Eol;
2211 end if;
2212
2213 if Is_Child_Unit (Id) then
2214 null;
2215
2216 -- Do not enter implicitly inherited non-overridden subprograms of
2217 -- a tagged type back into visibility if they have non-conformant
2218 -- homographs (Ada RM 8.3 12.3/2).
2219
2220 elsif Is_Hidden_Non_Overridden_Subpgm (Id) then
2221 null;
2222
2223 else
2224 Set_Is_Immediately_Visible (Id);
2225 end if;
2226 end if;
2227 end Install_Package_Entity;
2228
2229 ----------------------------------
2230 -- Install_Private_Declarations --
2231 ----------------------------------
2232
2233 procedure Install_Private_Declarations (P : Entity_Id) is
2234 Id : Entity_Id;
2235 Full : Entity_Id;
2236 Priv_Deps : Elist_Id;
2237
2238 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id);
2239 -- When the full view of a private type is made available, we do the
2240 -- same for its private dependents under proper visibility conditions.
2241 -- When compiling a grand-chid unit this needs to be done recursively.
2242
2243 -----------------------------
2244 -- Swap_Private_Dependents --
2245 -----------------------------
2246
2247 procedure Swap_Private_Dependents (Priv_Deps : Elist_Id) is
2248 Deps : Elist_Id;
2249 Priv : Entity_Id;
2250 Priv_Elmt : Elmt_Id;
2251 Is_Priv : Boolean;
2252
2253 begin
2254 Priv_Elmt := First_Elmt (Priv_Deps);
2255 while Present (Priv_Elmt) loop
2256 Priv := Node (Priv_Elmt);
2257
2258 -- Before the exchange, verify that the presence of the Full_View
2259 -- field. This field will be empty if the entity has already been
2260 -- installed due to a previous call.
2261
2262 if Present (Full_View (Priv)) and then Is_Visible_Dependent (Priv)
2263 then
2264 if Is_Private_Type (Priv) then
2265 Deps := Private_Dependents (Priv);
2266 Is_Priv := True;
2267 else
2268 Is_Priv := False;
2269 end if;
2270
2271 -- For each subtype that is swapped, we also swap the reference
2272 -- to it in Private_Dependents, to allow access to it when we
2273 -- swap them out in End_Package_Scope.
2274
2275 Replace_Elmt (Priv_Elmt, Full_View (Priv));
2276
2277 -- Ensure that both views of the dependent private subtype are
2278 -- immediately visible if within some open scope. Check full
2279 -- view before exchanging views.
2280
2281 if In_Open_Scopes (Scope (Full_View (Priv))) then
2282 Set_Is_Immediately_Visible (Priv);
2283 end if;
2284
2285 Exchange_Declarations (Priv);
2286 Set_Is_Immediately_Visible
2287 (Priv, In_Open_Scopes (Scope (Priv)));
2288
2289 Set_Is_Potentially_Use_Visible
2290 (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
2291
2292 -- Within a child unit, recurse, except in generic child unit,
2293 -- which (unfortunately) handle private_dependents separately.
2294
2295 if Is_Priv
2296 and then Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
2297 and then not Is_Empty_Elmt_List (Deps)
2298 and then not Inside_A_Generic
2299 then
2300 Swap_Private_Dependents (Deps);
2301 end if;
2302 end if;
2303
2304 Next_Elmt (Priv_Elmt);
2305 end loop;
2306 end Swap_Private_Dependents;
2307
2308 -- Start of processing for Install_Private_Declarations
2309
2310 begin
2311 -- First exchange declarations for private types, so that the full
2312 -- declaration is visible. For each private type, we check its
2313 -- Private_Dependents list and also exchange any subtypes of or derived
2314 -- types from it. Finally, if this is a Taft amendment type, the
2315 -- incomplete declaration is irrelevant, and we want to link the
2316 -- eventual full declaration with the original private one so we
2317 -- also skip the exchange.
2318
2319 Id := First_Entity (P);
2320 while Present (Id) and then Id /= First_Private_Entity (P) loop
2321 if Is_Private_Base_Type (Id)
2322 and then Present (Full_View (Id))
2323 and then Comes_From_Source (Full_View (Id))
2324 and then Scope (Full_View (Id)) = Scope (Id)
2325 and then Ekind (Full_View (Id)) /= E_Incomplete_Type
2326 then
2327 -- If there is a use-type clause on the private type, set the full
2328 -- view accordingly.
2329
2330 Set_In_Use (Full_View (Id), In_Use (Id));
2331 Full := Full_View (Id);
2332
2333 if Is_Private_Base_Type (Full)
2334 and then Has_Private_Declaration (Full)
2335 and then Nkind (Parent (Full)) = N_Full_Type_Declaration
2336 and then In_Open_Scopes (Scope (Etype (Full)))
2337 and then In_Package_Body (Current_Scope)
2338 and then not Is_Private_Type (Etype (Full))
2339 then
2340 -- This is the completion of a private type by a derivation
2341 -- from another private type which is not private anymore. This
2342 -- can only happen in a package nested within a child package,
2343 -- when the parent type is defined in the parent unit. At this
2344 -- point the current type is not private either, and we have
2345 -- to install the underlying full view, which is now visible.
2346 -- Save the current full view as well, so that all views can be
2347 -- restored on exit. It may seem that after compiling the child
2348 -- body there are not environments to restore, but the back-end
2349 -- expects those links to be valid, and freeze nodes depend on
2350 -- them.
2351
2352 if No (Full_View (Full))
2353 and then Present (Underlying_Full_View (Full))
2354 then
2355 Set_Full_View (Id, Underlying_Full_View (Full));
2356 Set_Underlying_Full_View (Id, Full);
2357 Set_Is_Underlying_Full_View (Full);
2358
2359 Set_Underlying_Full_View (Full, Empty);
2360 Set_Is_Frozen (Full_View (Id));
2361 end if;
2362 end if;
2363
2364 Priv_Deps := Private_Dependents (Id);
2365 Exchange_Declarations (Id);
2366 Set_Is_Immediately_Visible (Id);
2367 Swap_Private_Dependents (Priv_Deps);
2368 end if;
2369
2370 Next_Entity (Id);
2371 end loop;
2372
2373 -- Next make other declarations in the private part visible as well
2374
2375 Id := First_Private_Entity (P);
2376 while Present (Id) loop
2377 Install_Package_Entity (Id);
2378 Set_Is_Hidden (Id, False);
2379 Next_Entity (Id);
2380 end loop;
2381
2382 -- An abstract state is partially refined when it has at least one
2383 -- Part_Of constituent. Since these constituents are being installed
2384 -- into visibility, update the partial refinement status of any state
2385 -- defined in the associated package, subject to at least one Part_Of
2386 -- constituent.
2387
2388 if Ekind_In (P, E_Generic_Package, E_Package) then
2389 declare
2390 States : constant Elist_Id := Abstract_States (P);
2391 State_Elmt : Elmt_Id;
2392 State_Id : Entity_Id;
2393
2394 begin
2395 if Present (States) then
2396 State_Elmt := First_Elmt (States);
2397 while Present (State_Elmt) loop
2398 State_Id := Node (State_Elmt);
2399
2400 if Present (Part_Of_Constituents (State_Id)) then
2401 Set_Has_Partial_Visible_Refinement (State_Id);
2402 end if;
2403
2404 Next_Elmt (State_Elmt);
2405 end loop;
2406 end if;
2407 end;
2408 end if;
2409
2410 -- Indicate that the private part is currently visible, so it can be
2411 -- properly reset on exit.
2412
2413 Set_In_Private_Part (P);
2414 end Install_Private_Declarations;
2415
2416 ----------------------------------
2417 -- Install_Visible_Declarations --
2418 ----------------------------------
2419
2420 procedure Install_Visible_Declarations (P : Entity_Id) is
2421 Id : Entity_Id;
2422 Last_Entity : Entity_Id;
2423
2424 begin
2425 pragma Assert
2426 (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
2427
2428 if Is_Package_Or_Generic_Package (P) then
2429 Last_Entity := First_Private_Entity (P);
2430 else
2431 Last_Entity := Empty;
2432 end if;
2433
2434 Id := First_Entity (P);
2435 while Present (Id) and then Id /= Last_Entity loop
2436 Install_Package_Entity (Id);
2437 Next_Entity (Id);
2438 end loop;
2439 end Install_Visible_Declarations;
2440
2441 --------------------------
2442 -- Is_Private_Base_Type --
2443 --------------------------
2444
2445 function Is_Private_Base_Type (E : Entity_Id) return Boolean is
2446 begin
2447 return Ekind (E) = E_Private_Type
2448 or else Ekind (E) = E_Limited_Private_Type
2449 or else Ekind (E) = E_Record_Type_With_Private;
2450 end Is_Private_Base_Type;
2451
2452 --------------------------
2453 -- Is_Visible_Dependent --
2454 --------------------------
2455
2456 function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
2457 is
2458 S : constant Entity_Id := Scope (Dep);
2459
2460 begin
2461 -- Renamings created for actual types have the visibility of the actual
2462
2463 if Ekind (S) = E_Package
2464 and then Is_Generic_Instance (S)
2465 and then (Is_Generic_Actual_Type (Dep)
2466 or else Is_Generic_Actual_Type (Full_View (Dep)))
2467 then
2468 return True;
2469
2470 elsif not (Is_Derived_Type (Dep))
2471 and then Is_Derived_Type (Full_View (Dep))
2472 then
2473 -- When instantiating a package body, the scope stack is empty, so
2474 -- check instead whether the dependent type is defined in the same
2475 -- scope as the instance itself.
2476
2477 return In_Open_Scopes (S)
2478 or else (Is_Generic_Instance (Current_Scope)
2479 and then Scope (Dep) = Scope (Current_Scope));
2480 else
2481 return True;
2482 end if;
2483 end Is_Visible_Dependent;
2484
2485 ----------------------------
2486 -- May_Need_Implicit_Body --
2487 ----------------------------
2488
2489 procedure May_Need_Implicit_Body (E : Entity_Id) is
2490 P : constant Node_Id := Unit_Declaration_Node (E);
2491 S : constant Node_Id := Parent (P);
2492 B : Node_Id;
2493 Decls : List_Id;
2494
2495 begin
2496 if not Has_Completion (E)
2497 and then Nkind (P) = N_Package_Declaration
2498 and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
2499 then
2500 B :=
2501 Make_Package_Body (Sloc (E),
2502 Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
2503 Chars => Chars (E)),
2504 Declarations => New_List);
2505
2506 if Nkind (S) = N_Package_Specification then
2507 if Present (Private_Declarations (S)) then
2508 Decls := Private_Declarations (S);
2509 else
2510 Decls := Visible_Declarations (S);
2511 end if;
2512 else
2513 Decls := Declarations (S);
2514 end if;
2515
2516 Append (B, Decls);
2517 Analyze (B);
2518 end if;
2519 end May_Need_Implicit_Body;
2520
2521 ----------------------
2522 -- New_Private_Type --
2523 ----------------------
2524
2525 procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
2526 begin
2527 -- For other than Ada 2012, enter the name in the current scope
2528
2529 if Ada_Version < Ada_2012 then
2530 Enter_Name (Id);
2531
2532 -- Ada 2012 (AI05-0162): Enter the name in the current scope. Note that
2533 -- there may be an incomplete previous view.
2534
2535 else
2536 declare
2537 Prev : Entity_Id;
2538 begin
2539 Prev := Find_Type_Name (N);
2540 pragma Assert (Prev = Id
2541 or else (Ekind (Prev) = E_Incomplete_Type
2542 and then Present (Full_View (Prev))
2543 and then Full_View (Prev) = Id));
2544 end;
2545 end if;
2546
2547 if Limited_Present (Def) then
2548 Set_Ekind (Id, E_Limited_Private_Type);
2549 else
2550 Set_Ekind (Id, E_Private_Type);
2551 end if;
2552
2553 Set_Etype (Id, Id);
2554 Set_Has_Delayed_Freeze (Id);
2555 Set_Is_First_Subtype (Id);
2556 Init_Size_Align (Id);
2557
2558 Set_Is_Constrained (Id,
2559 No (Discriminant_Specifications (N))
2560 and then not Unknown_Discriminants_Present (N));
2561
2562 -- Set tagged flag before processing discriminants, to catch illegal
2563 -- usage.
2564
2565 Set_Is_Tagged_Type (Id, Tagged_Present (Def));
2566
2567 Set_Discriminant_Constraint (Id, No_Elist);
2568 Set_Stored_Constraint (Id, No_Elist);
2569
2570 if Present (Discriminant_Specifications (N)) then
2571 Push_Scope (Id);
2572 Process_Discriminants (N);
2573 End_Scope;
2574
2575 elsif Unknown_Discriminants_Present (N) then
2576 Set_Has_Unknown_Discriminants (Id);
2577 end if;
2578
2579 Set_Private_Dependents (Id, New_Elmt_List);
2580
2581 if Tagged_Present (Def) then
2582 Set_Ekind (Id, E_Record_Type_With_Private);
2583 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
2584 Set_Is_Abstract_Type (Id, Abstract_Present (Def));
2585 Set_Is_Limited_Record (Id, Limited_Present (Def));
2586 Set_Has_Delayed_Freeze (Id, True);
2587
2588 -- Recognize Ada.Real_Time.Timing_Events.Timing_Events here
2589
2590 if Is_RTE (Id, RE_Timing_Event) then
2591 Set_Has_Timing_Event (Id);
2592 end if;
2593
2594 -- Create a class-wide type with the same attributes
2595
2596 Make_Class_Wide_Type (Id);
2597
2598 elsif Abstract_Present (Def) then
2599 Error_Msg_N ("only a tagged type can be abstract", N);
2600 end if;
2601 end New_Private_Type;
2602
2603 ---------------------------------
2604 -- Requires_Completion_In_Body --
2605 ---------------------------------
2606
2607 function Requires_Completion_In_Body
2608 (Id : Entity_Id;
2609 Pack_Id : Entity_Id;
2610 Do_Abstract_States : Boolean := False) return Boolean
2611 is
2612 begin
2613 -- Always ignore child units. Child units get added to the entity list
2614 -- of a parent unit, but are not original entities of the parent, and
2615 -- so do not affect whether the parent needs a body.
2616
2617 if Is_Child_Unit (Id) then
2618 return False;
2619
2620 -- Ignore formal packages and their renamings
2621
2622 elsif Ekind (Id) = E_Package
2623 and then Nkind (Original_Node (Unit_Declaration_Node (Id))) =
2624 N_Formal_Package_Declaration
2625 then
2626 return False;
2627
2628 -- Otherwise test to see if entity requires a completion. Note that
2629 -- subprogram entities whose declaration does not come from source are
2630 -- ignored here on the basis that we assume the expander will provide an
2631 -- implicit completion at some point.
2632
2633 elsif (Is_Overloadable (Id)
2634 and then not Ekind_In (Id, E_Enumeration_Literal, E_Operator)
2635 and then not Is_Abstract_Subprogram (Id)
2636 and then not Has_Completion (Id)
2637 and then Comes_From_Source (Parent (Id)))
2638
2639 or else
2640 (Ekind (Id) = E_Package
2641 and then Id /= Pack_Id
2642 and then not Has_Completion (Id)
2643 and then Unit_Requires_Body (Id, Do_Abstract_States))
2644
2645 or else
2646 (Ekind (Id) = E_Incomplete_Type
2647 and then No (Full_View (Id))
2648 and then not Is_Generic_Type (Id))
2649
2650 or else
2651 (Ekind_In (Id, E_Task_Type, E_Protected_Type)
2652 and then not Has_Completion (Id))
2653
2654 or else
2655 (Ekind (Id) = E_Generic_Package
2656 and then Id /= Pack_Id
2657 and then not Has_Completion (Id)
2658 and then Unit_Requires_Body (Id, Do_Abstract_States))
2659
2660 or else
2661 (Is_Generic_Subprogram (Id)
2662 and then not Has_Completion (Id))
2663 then
2664 return True;
2665
2666 -- Otherwise the entity does not require completion in a package body
2667
2668 else
2669 return False;
2670 end if;
2671 end Requires_Completion_In_Body;
2672
2673 ----------------------------
2674 -- Uninstall_Declarations --
2675 ----------------------------
2676
2677 procedure Uninstall_Declarations (P : Entity_Id) is
2678 Decl : constant Node_Id := Unit_Declaration_Node (P);
2679 Id : Entity_Id;
2680 Full : Entity_Id;
2681 Priv_Elmt : Elmt_Id;
2682 Priv_Sub : Entity_Id;
2683
2684 procedure Preserve_Full_Attributes (Priv : Entity_Id; Full : Entity_Id);
2685 -- Copy to the private declaration the attributes of the full view that
2686 -- need to be available for the partial view also.
2687
2688 function Type_In_Use (T : Entity_Id) return Boolean;
2689 -- Check whether type or base type appear in an active use_type clause
2690
2691 ------------------------------
2692 -- Preserve_Full_Attributes --
2693 ------------------------------
2694
2695 procedure Preserve_Full_Attributes
2696 (Priv : Entity_Id;
2697 Full : Entity_Id)
2698 is
2699 Full_Base : constant Entity_Id := Base_Type (Full);
2700 Priv_Is_Base_Type : constant Boolean := Is_Base_Type (Priv);
2701
2702 begin
2703 Set_Size_Info (Priv, Full);
2704 Set_RM_Size (Priv, RM_Size (Full));
2705 Set_Size_Known_At_Compile_Time
2706 (Priv, Size_Known_At_Compile_Time (Full));
2707 Set_Is_Volatile (Priv, Is_Volatile (Full));
2708 Set_Treat_As_Volatile (Priv, Treat_As_Volatile (Full));
2709 Set_Is_Ada_2005_Only (Priv, Is_Ada_2005_Only (Full));
2710 Set_Is_Ada_2012_Only (Priv, Is_Ada_2012_Only (Full));
2711 Set_Has_Pragma_Unmodified (Priv, Has_Pragma_Unmodified (Full));
2712 Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced (Full));
2713 Set_Has_Pragma_Unreferenced_Objects
2714 (Priv, Has_Pragma_Unreferenced_Objects
2715 (Full));
2716 if Is_Unchecked_Union (Full) then
2717 Set_Is_Unchecked_Union (Base_Type (Priv));
2718 end if;
2719 -- Why is atomic not copied here ???
2720
2721 if Referenced (Full) then
2722 Set_Referenced (Priv);
2723 end if;
2724
2725 if Priv_Is_Base_Type then
2726 Set_Is_Controlled_Active
2727 (Priv, Is_Controlled_Active (Full_Base));
2728 Set_Finalize_Storage_Only
2729 (Priv, Finalize_Storage_Only (Full_Base));
2730 Set_Has_Controlled_Component
2731 (Priv, Has_Controlled_Component (Full_Base));
2732
2733 Propagate_Concurrent_Flags (Priv, Base_Type (Full));
2734 end if;
2735
2736 -- As explained in Freeze_Entity, private types are required to point
2737 -- to the same freeze node as their corresponding full view, if any.
2738 -- But we ought not to overwrite a node already inserted in the tree.
2739
2740 pragma Assert (Serious_Errors_Detected /= 0
2741 or else No (Freeze_Node (Priv))
2742 or else No (Parent (Freeze_Node (Priv)))
2743 or else Freeze_Node (Priv) = Freeze_Node (Full));
2744
2745 Set_Freeze_Node (Priv, Freeze_Node (Full));
2746
2747 -- Propagate Default_Initial_Condition-related attributes from the
2748 -- base type of the full view to the full view and vice versa. This
2749 -- may seem strange, but is necessary depending on which type
2750 -- triggered the generation of the DIC procedure body. As a result,
2751 -- both the full view and its base type carry the same DIC-related
2752 -- information.
2753
2754 Propagate_DIC_Attributes (Full, From_Typ => Full_Base);
2755 Propagate_DIC_Attributes (Full_Base, From_Typ => Full);
2756
2757 -- Propagate Default_Initial_Condition-related attributes from the
2758 -- full view to the private view.
2759
2760 Propagate_DIC_Attributes (Priv, From_Typ => Full);
2761
2762 -- Propagate invariant-related attributes from the base type of the
2763 -- full view to the full view and vice versa. This may seem strange,
2764 -- but is necessary depending on which type triggered the generation
2765 -- of the invariant procedure body. As a result, both the full view
2766 -- and its base type carry the same invariant-related information.
2767
2768 Propagate_Invariant_Attributes (Full, From_Typ => Full_Base);
2769 Propagate_Invariant_Attributes (Full_Base, From_Typ => Full);
2770
2771 -- Propagate invariant-related attributes from the full view to the
2772 -- private view.
2773
2774 Propagate_Invariant_Attributes (Priv, From_Typ => Full);
2775
2776 if Is_Tagged_Type (Priv)
2777 and then Is_Tagged_Type (Full)
2778 and then not Error_Posted (Full)
2779 then
2780 if Is_Tagged_Type (Priv) then
2781
2782 -- If the type is tagged, the tag itself must be available on
2783 -- the partial view, for expansion purposes.
2784
2785 Set_First_Entity (Priv, First_Entity (Full));
2786
2787 -- If there are discriminants in the partial view, these remain
2788 -- visible. Otherwise only the tag itself is visible, and there
2789 -- are no nameable components in the partial view.
2790
2791 if No (Last_Entity (Priv)) then
2792 Set_Last_Entity (Priv, First_Entity (Priv));
2793 end if;
2794 end if;
2795
2796 Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2797
2798 if Has_Discriminants (Full) then
2799 Set_Discriminant_Constraint (Priv,
2800 Discriminant_Constraint (Full));
2801 end if;
2802 end if;
2803 end Preserve_Full_Attributes;
2804
2805 -----------------
2806 -- Type_In_Use --
2807 -----------------
2808
2809 function Type_In_Use (T : Entity_Id) return Boolean is
2810 begin
2811 return Scope (Base_Type (T)) = P
2812 and then (In_Use (T) or else In_Use (Base_Type (T)));
2813 end Type_In_Use;
2814
2815 -- Start of processing for Uninstall_Declarations
2816
2817 begin
2818 Id := First_Entity (P);
2819 while Present (Id) and then Id /= First_Private_Entity (P) loop
2820 if Debug_Flag_E then
2821 Write_Str ("unlinking visible entity ");
2822 Write_Int (Int (Id));
2823 Write_Eol;
2824 end if;
2825
2826 -- On exit from the package scope, we must preserve the visibility
2827 -- established by use clauses in the current scope. Two cases:
2828
2829 -- a) If the entity is an operator, it may be a primitive operator of
2830 -- a type for which there is a visible use-type clause.
2831
2832 -- b) For other entities, their use-visibility is determined by a
2833 -- visible use clause for the package itself or a use-all-type clause
2834 -- applied directly to the entity's type. For a generic instance,
2835 -- the instantiation of the formals appears in the visible part,
2836 -- but the formals are private and remain so.
2837
2838 if Ekind (Id) = E_Function
2839 and then Is_Operator_Symbol_Name (Chars (Id))
2840 and then not Is_Hidden (Id)
2841 and then not Error_Posted (Id)
2842 then
2843 Set_Is_Potentially_Use_Visible (Id,
2844 In_Use (P)
2845 or else Type_In_Use (Etype (Id))
2846 or else Type_In_Use (Etype (First_Formal (Id)))
2847 or else (Present (Next_Formal (First_Formal (Id)))
2848 and then
2849 Type_In_Use
2850 (Etype (Next_Formal (First_Formal (Id))))));
2851 else
2852 if In_Use (P) and then not Is_Hidden (Id) then
2853
2854 -- A child unit of a use-visible package remains use-visible
2855 -- only if it is itself a visible child unit. Otherwise it
2856 -- would remain visible in other contexts where P is use-
2857 -- visible, because once compiled it stays in the entity list
2858 -- of its parent unit.
2859
2860 if Is_Child_Unit (Id) then
2861 Set_Is_Potentially_Use_Visible
2862 (Id, Is_Visible_Lib_Unit (Id));
2863 else
2864 Set_Is_Potentially_Use_Visible (Id);
2865 end if;
2866
2867 -- We need to avoid incorrectly marking enumeration literals as
2868 -- non-visible when a visible use-all-type clause is in effect.
2869
2870 elsif Type_In_Use (Etype (Id))
2871 and then Nkind (Current_Use_Clause (Etype (Id))) =
2872 N_Use_Type_Clause
2873 and then All_Present (Current_Use_Clause (Etype (Id)))
2874 then
2875 null;
2876
2877 else
2878 Set_Is_Potentially_Use_Visible (Id, False);
2879 end if;
2880 end if;
2881
2882 -- Local entities are not immediately visible outside of the package
2883
2884 Set_Is_Immediately_Visible (Id, False);
2885
2886 -- If this is a private type with a full view (for example a local
2887 -- subtype of a private type declared elsewhere), ensure that the
2888 -- full view is also removed from visibility: it may be exposed when
2889 -- swapping views in an instantiation. Similarly, ensure that the
2890 -- use-visibility is properly set on both views.
2891
2892 if Is_Type (Id) and then Present (Full_View (Id)) then
2893 Set_Is_Immediately_Visible (Full_View (Id), False);
2894 Set_Is_Potentially_Use_Visible (Full_View (Id),
2895 Is_Potentially_Use_Visible (Id));
2896 end if;
2897
2898 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2899 Check_Abstract_Overriding (Id);
2900 Check_Conventions (Id);
2901 end if;
2902
2903 if Ekind_In (Id, E_Private_Type, E_Limited_Private_Type)
2904 and then No (Full_View (Id))
2905 and then not Is_Generic_Type (Id)
2906 and then not Is_Derived_Type (Id)
2907 then
2908 Error_Msg_N ("missing full declaration for private type&", Id);
2909
2910 elsif Ekind (Id) = E_Record_Type_With_Private
2911 and then not Is_Generic_Type (Id)
2912 and then No (Full_View (Id))
2913 then
2914 if Nkind (Parent (Id)) = N_Private_Type_Declaration then
2915 Error_Msg_N ("missing full declaration for private type&", Id);
2916 else
2917 Error_Msg_N
2918 ("missing full declaration for private extension", Id);
2919 end if;
2920
2921 -- Case of constant, check for deferred constant declaration with
2922 -- no full view. Likely just a matter of a missing expression, or
2923 -- accidental use of the keyword constant.
2924
2925 elsif Ekind (Id) = E_Constant
2926
2927 -- OK if constant value present
2928
2929 and then No (Constant_Value (Id))
2930
2931 -- OK if full view present
2932
2933 and then No (Full_View (Id))
2934
2935 -- OK if imported, since that provides the completion
2936
2937 and then not Is_Imported (Id)
2938
2939 -- OK if object declaration replaced by renaming declaration as
2940 -- a result of OK_To_Rename processing (e.g. for concatenation)
2941
2942 and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration
2943
2944 -- OK if object declaration with the No_Initialization flag set
2945
2946 and then not (Nkind (Parent (Id)) = N_Object_Declaration
2947 and then No_Initialization (Parent (Id)))
2948 then
2949 -- If no private declaration is present, we assume the user did
2950 -- not intend a deferred constant declaration and the problem
2951 -- is simply that the initializing expression is missing.
2952
2953 if not Has_Private_Declaration (Etype (Id)) then
2954
2955 -- We assume that the user did not intend a deferred constant
2956 -- declaration, and the expression is just missing.
2957
2958 Error_Msg_N
2959 ("constant declaration requires initialization expression",
2960 Parent (Id));
2961
2962 if Is_Limited_Type (Etype (Id)) then
2963 Error_Msg_N
2964 ("\if variable intended, remove CONSTANT from declaration",
2965 Parent (Id));
2966 end if;
2967
2968 -- Otherwise if a private declaration is present, then we are
2969 -- missing the full declaration for the deferred constant.
2970
2971 else
2972 Error_Msg_N
2973 ("missing full declaration for deferred constant (RM 7.4)",
2974 Id);
2975
2976 if Is_Limited_Type (Etype (Id)) then
2977 Error_Msg_N
2978 ("\if variable intended, remove CONSTANT from declaration",
2979 Parent (Id));
2980 end if;
2981 end if;
2982 end if;
2983
2984 Next_Entity (Id);
2985 end loop;
2986
2987 -- If the specification was installed as the parent of a public child
2988 -- unit, the private declarations were not installed, and there is
2989 -- nothing to do.
2990
2991 if not In_Private_Part (P) then
2992 return;
2993 else
2994 Set_In_Private_Part (P, False);
2995 end if;
2996
2997 -- Make private entities invisible and exchange full and private
2998 -- declarations for private types. Id is now the first private entity
2999 -- in the package.
3000
3001 while Present (Id) loop
3002 if Debug_Flag_E then
3003 Write_Str ("unlinking private entity ");
3004 Write_Int (Int (Id));
3005 Write_Eol;
3006 end if;
3007
3008 if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
3009 Check_Abstract_Overriding (Id);
3010 Check_Conventions (Id);
3011 end if;
3012
3013 Set_Is_Immediately_Visible (Id, False);
3014
3015 if Is_Private_Base_Type (Id) and then Present (Full_View (Id)) then
3016 Full := Full_View (Id);
3017
3018 -- If the partial view is not declared in the visible part of the
3019 -- package (as is the case when it is a type derived from some
3020 -- other private type in the private part of the current package),
3021 -- no exchange takes place.
3022
3023 if No (Parent (Id))
3024 or else List_Containing (Parent (Id)) /=
3025 Visible_Declarations (Specification (Decl))
3026 then
3027 goto Next_Id;
3028 end if;
3029
3030 -- The entry in the private part points to the full declaration,
3031 -- which is currently visible. Exchange them so only the private
3032 -- type declaration remains accessible, and link private and full
3033 -- declaration in the opposite direction. Before the actual
3034 -- exchange, we copy back attributes of the full view that must
3035 -- be available to the partial view too.
3036
3037 Preserve_Full_Attributes (Id, Full);
3038
3039 Set_Is_Potentially_Use_Visible (Id, In_Use (P));
3040
3041 -- The following test may be redundant, as this is already
3042 -- diagnosed in sem_ch3. ???
3043
3044 if not Is_Definite_Subtype (Full)
3045 and then Is_Definite_Subtype (Id)
3046 then
3047 Error_Msg_Sloc := Sloc (Parent (Id));
3048 Error_Msg_NE
3049 ("full view of& not compatible with declaration#", Full, Id);
3050 end if;
3051
3052 -- Swap out the subtypes and derived types of Id that
3053 -- were compiled in this scope, or installed previously
3054 -- by Install_Private_Declarations.
3055
3056 -- Before we do the swap, we verify the presence of the Full_View
3057 -- field which may be empty due to a swap by a previous call to
3058 -- End_Package_Scope (e.g. from the freezing mechanism).
3059
3060 Priv_Elmt := First_Elmt (Private_Dependents (Id));
3061 while Present (Priv_Elmt) loop
3062 Priv_Sub := Node (Priv_Elmt);
3063
3064 if Present (Full_View (Priv_Sub)) then
3065 if Scope (Priv_Sub) = P
3066 or else not In_Open_Scopes (Scope (Priv_Sub))
3067 then
3068 Set_Is_Immediately_Visible (Priv_Sub, False);
3069 end if;
3070
3071 if Is_Visible_Dependent (Priv_Sub) then
3072 Preserve_Full_Attributes
3073 (Priv_Sub, Full_View (Priv_Sub));
3074 Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub));
3075 Exchange_Declarations (Priv_Sub);
3076 end if;
3077 end if;
3078
3079 Next_Elmt (Priv_Elmt);
3080 end loop;
3081
3082 -- Now restore the type itself to its private view
3083
3084 Exchange_Declarations (Id);
3085
3086 -- If we have installed an underlying full view for a type derived
3087 -- from a private type in a child unit, restore the proper views
3088 -- of private and full view. See corresponding code in
3089 -- Install_Private_Declarations.
3090
3091 -- After the exchange, Full denotes the private type in the
3092 -- visible part of the package.
3093
3094 if Is_Private_Base_Type (Full)
3095 and then Present (Full_View (Full))
3096 and then Present (Underlying_Full_View (Full))
3097 and then In_Package_Body (Current_Scope)
3098 then
3099 Set_Full_View (Full, Underlying_Full_View (Full));
3100 Set_Underlying_Full_View (Full, Empty);
3101 end if;
3102
3103 elsif Ekind (Id) = E_Incomplete_Type
3104 and then Comes_From_Source (Id)
3105 and then No (Full_View (Id))
3106 then
3107 -- Mark Taft amendment types. Verify that there are no primitive
3108 -- operations declared for the type (3.10.1(9)).
3109
3110 Set_Has_Completion_In_Body (Id);
3111
3112 declare
3113 Elmt : Elmt_Id;
3114 Subp : Entity_Id;
3115
3116 begin
3117 Elmt := First_Elmt (Private_Dependents (Id));
3118 while Present (Elmt) loop
3119 Subp := Node (Elmt);
3120
3121 -- Is_Primitive is tested because there can be cases where
3122 -- nonprimitive subprograms (in nested packages) are added
3123 -- to the Private_Dependents list.
3124
3125 if Is_Overloadable (Subp) and then Is_Primitive (Subp) then
3126 Error_Msg_NE
3127 ("type& must be completed in the private part",
3128 Parent (Subp), Id);
3129
3130 -- The result type of an access-to-function type cannot be a
3131 -- Taft-amendment type, unless the version is Ada 2012 or
3132 -- later (see AI05-151).
3133
3134 elsif Ada_Version < Ada_2012
3135 and then Ekind (Subp) = E_Subprogram_Type
3136 then
3137 if Etype (Subp) = Id
3138 or else
3139 (Is_Class_Wide_Type (Etype (Subp))
3140 and then Etype (Etype (Subp)) = Id)
3141 then
3142 Error_Msg_NE
3143 ("type& must be completed in the private part",
3144 Associated_Node_For_Itype (Subp), Id);
3145 end if;
3146 end if;
3147
3148 Next_Elmt (Elmt);
3149 end loop;
3150 end;
3151
3152 elsif not Is_Child_Unit (Id)
3153 and then (not Is_Private_Type (Id) or else No (Full_View (Id)))
3154 then
3155 Set_Is_Hidden (Id);
3156 Set_Is_Potentially_Use_Visible (Id, False);
3157 end if;
3158
3159 <<Next_Id>>
3160 Next_Entity (Id);
3161 end loop;
3162 end Uninstall_Declarations;
3163
3164 ------------------------
3165 -- Unit_Requires_Body --
3166 ------------------------
3167
3168 function Unit_Requires_Body
3169 (Pack_Id : Entity_Id;
3170 Do_Abstract_States : Boolean := False) return Boolean
3171 is
3172 E : Entity_Id;
3173
3174 Requires_Body : Boolean := False;
3175 -- Flag set when the unit has at least one construct that requries
3176 -- completion in a body.
3177
3178 begin
3179 -- Imported entity never requires body. Right now, only subprograms can
3180 -- be imported, but perhaps in the future we will allow import of
3181 -- packages.
3182
3183 if Is_Imported (Pack_Id) then
3184 return False;
3185
3186 -- Body required if library package with pragma Elaborate_Body
3187
3188 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3189 return True;
3190
3191 -- Body required if subprogram
3192
3193 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3194 return True;
3195
3196 -- Treat a block as requiring a body
3197
3198 elsif Ekind (Pack_Id) = E_Block then
3199 return True;
3200
3201 elsif Ekind (Pack_Id) = E_Package
3202 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3203 and then Present (Generic_Parent (Parent (Pack_Id)))
3204 then
3205 declare
3206 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3207 begin
3208 if Has_Pragma_Elaborate_Body (G_P) then
3209 return True;
3210 end if;
3211 end;
3212 end if;
3213
3214 -- Traverse the entity chain of the package and look for constructs that
3215 -- require a completion in a body.
3216
3217 E := First_Entity (Pack_Id);
3218 while Present (E) loop
3219
3220 -- Skip abstract states because their completion depends on several
3221 -- criteria (see below).
3222
3223 if Ekind (E) = E_Abstract_State then
3224 null;
3225
3226 elsif Requires_Completion_In_Body
3227 (E, Pack_Id, Do_Abstract_States)
3228 then
3229 Requires_Body := True;
3230 exit;
3231 end if;
3232
3233 Next_Entity (E);
3234 end loop;
3235
3236 -- A [generic] package that defines at least one non-null abstract state
3237 -- requires a completion only when at least one other construct requires
3238 -- a completion in a body (SPARK RM 7.1.4(4) and (6)). This check is not
3239 -- performed if the caller requests this behavior.
3240
3241 if Do_Abstract_States
3242 and then Ekind_In (Pack_Id, E_Generic_Package, E_Package)
3243 and then Has_Non_Null_Abstract_State (Pack_Id)
3244 and then Requires_Body
3245 then
3246 return True;
3247 end if;
3248
3249 return Requires_Body;
3250 end Unit_Requires_Body;
3251
3252 -----------------------------
3253 -- Unit_Requires_Body_Info --
3254 -----------------------------
3255
3256 procedure Unit_Requires_Body_Info (Pack_Id : Entity_Id) is
3257 E : Entity_Id;
3258
3259 begin
3260 -- An imported entity never requires body. Right now, only subprograms
3261 -- can be imported, but perhaps in the future we will allow import of
3262 -- packages.
3263
3264 if Is_Imported (Pack_Id) then
3265 return;
3266
3267 -- Body required if library package with pragma Elaborate_Body
3268
3269 elsif Has_Pragma_Elaborate_Body (Pack_Id) then
3270 Error_Msg_N ("info: & requires body (Elaborate_Body)?Y?", Pack_Id);
3271
3272 -- Body required if subprogram
3273
3274 elsif Is_Subprogram_Or_Generic_Subprogram (Pack_Id) then
3275 Error_Msg_N ("info: & requires body (subprogram case)?Y?", Pack_Id);
3276
3277 -- Body required if generic parent has Elaborate_Body
3278
3279 elsif Ekind (Pack_Id) = E_Package
3280 and then Nkind (Parent (Pack_Id)) = N_Package_Specification
3281 and then Present (Generic_Parent (Parent (Pack_Id)))
3282 then
3283 declare
3284 G_P : constant Entity_Id := Generic_Parent (Parent (Pack_Id));
3285 begin
3286 if Has_Pragma_Elaborate_Body (G_P) then
3287 Error_Msg_N
3288 ("info: & requires body (generic parent Elaborate_Body)?Y?",
3289 Pack_Id);
3290 end if;
3291 end;
3292
3293 -- A [generic] package that introduces at least one non-null abstract
3294 -- state requires completion. However, there is a separate rule that
3295 -- requires that such a package have a reason other than this for a
3296 -- body being required (if necessary a pragma Elaborate_Body must be
3297 -- provided). If Ignore_Abstract_State is True, we don't do this check
3298 -- (so we can use Unit_Requires_Body to check for some other reason).
3299
3300 elsif Ekind_In (Pack_Id, E_Generic_Package, E_Package)
3301 and then Present (Abstract_States (Pack_Id))
3302 and then not Is_Null_State
3303 (Node (First_Elmt (Abstract_States (Pack_Id))))
3304 then
3305 Error_Msg_N
3306 ("info: & requires body (non-null abstract state aspect)?Y?",
3307 Pack_Id);
3308 end if;
3309
3310 -- Otherwise search entity chain for entity requiring completion
3311
3312 E := First_Entity (Pack_Id);
3313 while Present (E) loop
3314 if Requires_Completion_In_Body (E, Pack_Id) then
3315 Error_Msg_Node_2 := E;
3316 Error_Msg_NE
3317 ("info: & requires body (& requires completion)?Y?", E, Pack_Id);
3318 end if;
3319
3320 Next_Entity (E);
3321 end loop;
3322 end Unit_Requires_Body_Info;
3323
3324 end Sem_Ch7;
This page took 0.163872 seconds and 6 git commands to generate.