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