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