OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [gcc-4.5.1/] [gcc-4.5.1-or32-1.0rc4/] [gcc/] [ada/] [sem_ch7.adb] - Blame information for rev 855

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 281 jeremybenn
------------------------------------------------------------------------------
2
--                                                                          --
3
--                         GNAT COMPILER COMPONENTS                         --
4
--                                                                          --
5
--                              S E M . C H 7                               --
6
--                                                                          --
7
--                                 B o d y                                  --
8
--                                                                          --
9
--          Copyright (C) 1992-2009, 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 Atree;    use Atree;
32
with Debug;    use Debug;
33
with Einfo;    use Einfo;
34
with Elists;   use Elists;
35
with Errout;   use Errout;
36
with Exp_Disp; use Exp_Disp;
37
with Exp_Dist; use Exp_Dist;
38
with Exp_Dbug; use Exp_Dbug;
39
with Lib;      use Lib;
40
with Lib.Xref; use Lib.Xref;
41
with Namet;    use Namet;
42
with Nmake;    use Nmake;
43
with Nlists;   use Nlists;
44
with Opt;      use Opt;
45
with Output;   use Output;
46
with Sem;      use Sem;
47
with Sem_Aux;  use Sem_Aux;
48
with Sem_Cat;  use Sem_Cat;
49
with Sem_Ch3;  use Sem_Ch3;
50
with Sem_Ch6;  use Sem_Ch6;
51
with Sem_Ch8;  use Sem_Ch8;
52
with Sem_Ch10; use Sem_Ch10;
53
with Sem_Ch12; use Sem_Ch12;
54
with Sem_Disp; use Sem_Disp;
55
with Sem_Prag; use Sem_Prag;
56
with Sem_Util; use Sem_Util;
57
with Sem_Warn; use Sem_Warn;
58
with Snames;   use Snames;
59
with Stand;    use Stand;
60
with Sinfo;    use Sinfo;
61
with Sinput;   use Sinput;
62
with Style;
63
with Uintp;    use Uintp;
64
 
65
package body Sem_Ch7 is
66
 
67
   -----------------------------------
68
   -- Handling private declarations --
69
   -----------------------------------
70
 
71
   --  The principle that each entity has a single defining occurrence clashes
72
   --  with the presence of two separate definitions for private types: the
73
   --  first is the private type declaration, and the second is the full type
74
   --  declaration. It is important that all references to the type point to
75
   --  the same defining occurrence, namely the first one. To enforce the two
76
   --  separate views of the entity, the corresponding information is swapped
77
   --  between the two declarations. Outside of the package, the defining
78
   --  occurrence only contains the private declaration information, while in
79
   --  the private part and the body of the package the defining occurrence
80
   --  contains the full declaration. To simplify the swap, the defining
81
   --  occurrence that currently holds the private declaration points to the
82
   --  full declaration. During semantic processing the defining occurrence
83
   --  also points to a list of private dependents, that is to say access types
84
   --  or composite types whose designated types or component types are
85
   --  subtypes or derived types of the private type in question. After the
86
   --  full declaration has been seen, the private dependents are updated to
87
   --  indicate that they have full definitions.
88
 
89
   -----------------------
90
   -- Local Subprograms --
91
   -----------------------
92
 
93
   procedure Analyze_Package_Body_Helper (N : Node_Id);
94
   --  Does all the real work of Analyze_Package_Body
95
 
96
   procedure Check_Anonymous_Access_Types
97
     (Spec_Id : Entity_Id;
98
      P_Body  : Node_Id);
99
   --  If the spec of a package has a limited_with_clause, it may declare
100
   --  anonymous access types whose designated type is a limited view, such an
101
   --  anonymous access return type for a function. This access type cannot be
102
   --  elaborated in the spec itself, but it may need an itype reference if it
103
   --  is used within a nested scope. In that case the itype reference is
104
   --  created at the beginning of the corresponding package body and inserted
105
   --  before other body declarations.
106
 
107
   procedure Install_Package_Entity (Id : Entity_Id);
108
   --  Supporting procedure for Install_{Visible,Private}_Declarations. Places
109
   --  one entity on its visibility chain, and recurses on the visible part if
110
   --  the entity is an inner package.
111
 
112
   function Is_Private_Base_Type (E : Entity_Id) return Boolean;
113
   --  True for a private type that is not a subtype
114
 
115
   function Is_Visible_Dependent (Dep : Entity_Id) return Boolean;
116
   --  If the private dependent is a private type whose full view is derived
117
   --  from the parent type, its full properties are revealed only if we are in
118
   --  the immediate scope of the private dependent. Should this predicate be
119
   --  tightened further???
120
 
121
   procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id);
122
   --  Called upon entering the private part of a public child package and the
123
   --  body of a nested package, to potentially declare certain inherited
124
   --  subprograms that were inherited by types in the visible part, but whose
125
   --  declaration was deferred because the parent operation was private and
126
   --  not visible at that point. These subprograms are located by traversing
127
   --  the visible part declarations looking for non-private type extensions
128
   --  and then examining each of the primitive operations of such types to
129
   --  find those that were inherited but declared with a special internal
130
   --  name. Each such operation is now declared as an operation with a normal
131
   --  name (using the name of the parent operation) and replaces the previous
132
   --  implicit operation in the primitive operations list of the type. If the
133
   --  inherited private operation has been overridden, then it's replaced by
134
   --  the overriding operation.
135
 
136
   --------------------------
137
   -- Analyze_Package_Body --
138
   --------------------------
139
 
140
   procedure Analyze_Package_Body (N : Node_Id) is
141
      Loc : constant Source_Ptr := Sloc (N);
142
 
143
   begin
144
      if Debug_Flag_C then
145
         Write_Str ("==> package body ");
146
         Write_Name (Chars (Defining_Entity (N)));
147
         Write_Str (" from ");
148
         Write_Location (Loc);
149
         Write_Eol;
150
         Indent;
151
      end if;
152
 
153
      --  The real work is split out into the helper, so it can do "return;"
154
      --  without skipping the debug output.
155
 
156
      Analyze_Package_Body_Helper (N);
157
 
158
      if Debug_Flag_C then
159
         Outdent;
160
         Write_Str ("<== package body ");
161
         Write_Name (Chars (Defining_Entity (N)));
162
         Write_Str (" from ");
163
         Write_Location (Loc);
164
         Write_Eol;
165
      end if;
166
   end Analyze_Package_Body;
167
 
168
   ---------------------------------
169
   -- Analyze_Package_Body_Helper --
170
   ---------------------------------
171
 
172
   procedure Analyze_Package_Body_Helper (N : Node_Id) is
173
      HSS              : Node_Id;
174
      Body_Id          : Entity_Id;
175
      Spec_Id          : Entity_Id;
176
      Last_Spec_Entity : Entity_Id;
177
      New_N            : Node_Id;
178
      Pack_Decl        : Node_Id;
179
 
180
      procedure Install_Composite_Operations (P : Entity_Id);
181
      --  Composite types declared in the current scope may depend on types
182
      --  that were private at the point of declaration, and whose full view
183
      --  is now in scope. Indicate that the corresponding operations on the
184
      --  composite type are available.
185
 
186
      ----------------------------------
187
      -- Install_Composite_Operations --
188
      ----------------------------------
189
 
190
      procedure Install_Composite_Operations (P : Entity_Id) is
191
         Id : Entity_Id;
192
 
193
      begin
194
         Id := First_Entity (P);
195
         while Present (Id) loop
196
            if Is_Type (Id)
197
              and then (Is_Limited_Composite (Id)
198
                         or else Is_Private_Composite (Id))
199
              and then No (Private_Component (Id))
200
            then
201
               Set_Is_Limited_Composite (Id, False);
202
               Set_Is_Private_Composite (Id, False);
203
            end if;
204
 
205
            Next_Entity (Id);
206
         end loop;
207
      end Install_Composite_Operations;
208
 
209
   --  Start of processing for Analyze_Package_Body_Helper
210
 
211
   begin
212
      --  Find corresponding package specification, and establish the current
213
      --  scope. The visible defining entity for the package is the defining
214
      --  occurrence in the spec. On exit from the package body, all body
215
      --  declarations are attached to the defining entity for the body, but
216
      --  the later is never used for name resolution. In this fashion there
217
      --  is only one visible entity that denotes the package.
218
 
219
      --  Set Body_Id. Note that this Will be reset to point to the generic
220
      --  copy later on in the generic case.
221
 
222
      Body_Id := Defining_Entity (N);
223
 
224
      if Present (Corresponding_Spec (N)) then
225
 
226
         --  Body is body of package instantiation. Corresponding spec has
227
         --  already been set.
228
 
229
         Spec_Id := Corresponding_Spec (N);
230
         Pack_Decl := Unit_Declaration_Node (Spec_Id);
231
 
232
      else
233
         Spec_Id := Current_Entity_In_Scope (Defining_Entity (N));
234
 
235
         if Present (Spec_Id)
236
           and then Is_Package_Or_Generic_Package (Spec_Id)
237
         then
238
            Pack_Decl := Unit_Declaration_Node (Spec_Id);
239
 
240
            if Nkind (Pack_Decl) = N_Package_Renaming_Declaration then
241
               Error_Msg_N ("cannot supply body for package renaming", N);
242
               return;
243
 
244
            elsif Present (Corresponding_Body (Pack_Decl)) then
245
               Error_Msg_N ("redefinition of package body", N);
246
               return;
247
            end if;
248
 
249
         else
250
            Error_Msg_N ("missing specification for package body", N);
251
            return;
252
         end if;
253
 
254
         if Is_Package_Or_Generic_Package (Spec_Id)
255
           and then
256
             (Scope (Spec_Id) = Standard_Standard
257
               or else Is_Child_Unit (Spec_Id))
258
           and then not Unit_Requires_Body (Spec_Id)
259
         then
260
            if Ada_Version = Ada_83 then
261
               Error_Msg_N
262
                 ("optional package body (not allowed in Ada 95)?", N);
263
            else
264
               Error_Msg_N
265
                 ("spec of this package does not allow a body", N);
266
            end if;
267
         end if;
268
      end if;
269
 
270
      Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
271
      Style.Check_Identifier (Body_Id, Spec_Id);
272
 
273
      if Is_Child_Unit (Spec_Id) then
274
         if Nkind (Parent (N)) /= N_Compilation_Unit then
275
            Error_Msg_NE
276
              ("body of child unit& cannot be an inner package", N, Spec_Id);
277
         end if;
278
 
279
         Set_Is_Child_Unit (Body_Id);
280
      end if;
281
 
282
      --  Generic package case
283
 
284
      if Ekind (Spec_Id) = E_Generic_Package then
285
 
286
         --  Disable expansion and perform semantic analysis on copy. The
287
         --  unannotated body will be used in all instantiations.
288
 
289
         Body_Id := Defining_Entity (N);
290
         Set_Ekind (Body_Id, E_Package_Body);
291
         Set_Scope (Body_Id, Scope (Spec_Id));
292
         Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
293
         Set_Body_Entity (Spec_Id, Body_Id);
294
         Set_Spec_Entity (Body_Id, Spec_Id);
295
 
296
         New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
297
         Rewrite (N, New_N);
298
 
299
         --  Update Body_Id to point to the copied node for the remainder of
300
         --  the processing.
301
 
302
         Body_Id := Defining_Entity (N);
303
         Start_Generic;
304
      end if;
305
 
306
      --  The Body_Id is that of the copied node in the generic case, the
307
      --  current node otherwise. Note that N was rewritten above, so we must
308
      --  be sure to get the latest Body_Id value.
309
 
310
      Set_Ekind (Body_Id, E_Package_Body);
311
      Set_Body_Entity (Spec_Id, Body_Id);
312
      Set_Spec_Entity (Body_Id, Spec_Id);
313
 
314
      --  Defining name for the package body is not a visible entity: Only the
315
      --  defining name for the declaration is visible.
316
 
317
      Set_Etype (Body_Id, Standard_Void_Type);
318
      Set_Scope (Body_Id, Scope (Spec_Id));
319
      Set_Corresponding_Spec (N, Spec_Id);
320
      Set_Corresponding_Body (Pack_Decl, Body_Id);
321
 
322
      --  The body entity is not used for semantics or code generation, but
323
      --  it is attached to the entity list of the enclosing scope to simplify
324
      --  the listing of back-annotations for the types it main contain.
325
 
326
      if Scope (Spec_Id) /= Standard_Standard then
327
         Append_Entity (Body_Id, Scope (Spec_Id));
328
      end if;
329
 
330
      --  Indicate that we are currently compiling the body of the package
331
 
332
      Set_In_Package_Body (Spec_Id);
333
      Set_Has_Completion (Spec_Id);
334
      Last_Spec_Entity := Last_Entity (Spec_Id);
335
 
336
      Push_Scope (Spec_Id);
337
 
338
      Set_Categorization_From_Pragmas (N);
339
 
340
      Install_Visible_Declarations (Spec_Id);
341
      Install_Private_Declarations (Spec_Id);
342
      Install_Private_With_Clauses (Spec_Id);
343
      Install_Composite_Operations (Spec_Id);
344
 
345
      Check_Anonymous_Access_Types (Spec_Id, N);
346
 
347
      if Ekind (Spec_Id) = E_Generic_Package then
348
         Set_Use (Generic_Formal_Declarations (Pack_Decl));
349
      end if;
350
 
351
      Set_Use (Visible_Declarations (Specification (Pack_Decl)));
352
      Set_Use (Private_Declarations (Specification (Pack_Decl)));
353
 
354
      --  This is a nested package, so it may be necessary to declare certain
355
      --  inherited subprograms that are not yet visible because the parent
356
      --  type's subprograms are now visible.
357
 
358
      if Ekind (Scope (Spec_Id)) = E_Package
359
        and then Scope (Spec_Id) /= Standard_Standard
360
      then
361
         Declare_Inherited_Private_Subprograms (Spec_Id);
362
      end if;
363
 
364
      if Present (Declarations (N)) then
365
         Analyze_Declarations (Declarations (N));
366
         Inspect_Deferred_Constant_Completion (Declarations (N));
367
      end if;
368
 
369
      --  Analyze_Declarations has caused freezing of all types. Now generate
370
      --  bodies for RACW primitives and stream attributes, if any.
371
 
372
      if Ekind (Spec_Id) = E_Package and then Has_RACW (Spec_Id) then
373
 
374
         --  Attach subprogram bodies to support RACWs declared in spec
375
 
376
         Append_RACW_Bodies (Declarations (N), Spec_Id);
377
         Analyze_List (Declarations (N));
378
      end if;
379
 
380
      HSS := Handled_Statement_Sequence (N);
381
 
382
      if Present (HSS) then
383
         Process_End_Label (HSS, 't', Spec_Id);
384
         Analyze (HSS);
385
 
386
         --  Check that elaboration code in a preelaborable package body is
387
         --  empty other than null statements and labels (RM 10.2.1(6)).
388
 
389
         Validate_Null_Statement_Sequence (N);
390
      end if;
391
 
392
      Validate_Categorization_Dependency (N, Spec_Id);
393
      Check_Completion (Body_Id);
394
 
395
      --  Generate start of body reference. Note that we do this fairly late,
396
      --  because the call will use In_Extended_Main_Source_Unit as a check,
397
      --  and we want to make sure that Corresponding_Stub links are set
398
 
399
      Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
400
 
401
      --  For a generic package, collect global references and mark them on
402
      --  the original body so that they are not resolved again at the point
403
      --  of instantiation.
404
 
405
      if Ekind (Spec_Id) /= E_Package then
406
         Save_Global_References (Original_Node (N));
407
         End_Generic;
408
      end if;
409
 
410
      --  The entities of the package body have so far been chained onto the
411
      --  declaration chain for the spec. That's been fine while we were in the
412
      --  body, since we wanted them to be visible, but now that we are leaving
413
      --  the package body, they are no longer visible, so we remove them from
414
      --  the entity chain of the package spec entity, and copy them to the
415
      --  entity chain of the package body entity, where they will never again
416
      --  be visible.
417
 
418
      if Present (Last_Spec_Entity) then
419
         Set_First_Entity (Body_Id, Next_Entity (Last_Spec_Entity));
420
         Set_Next_Entity (Last_Spec_Entity, Empty);
421
         Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
422
         Set_Last_Entity (Spec_Id, Last_Spec_Entity);
423
 
424
      else
425
         Set_First_Entity (Body_Id, First_Entity (Spec_Id));
426
         Set_Last_Entity  (Body_Id, Last_Entity  (Spec_Id));
427
         Set_First_Entity (Spec_Id, Empty);
428
         Set_Last_Entity  (Spec_Id, Empty);
429
      end if;
430
 
431
      End_Package_Scope (Spec_Id);
432
 
433
      --  All entities declared in body are not visible
434
 
435
      declare
436
         E : Entity_Id;
437
 
438
      begin
439
         E := First_Entity (Body_Id);
440
         while Present (E) loop
441
            Set_Is_Immediately_Visible (E, False);
442
            Set_Is_Potentially_Use_Visible (E, False);
443
            Set_Is_Hidden (E);
444
 
445
            --  Child units may appear on the entity list (e.g. if they appear
446
            --  in the context of a subunit) but they are not body entities.
447
 
448
            if not Is_Child_Unit (E) then
449
               Set_Is_Package_Body_Entity (E);
450
            end if;
451
 
452
            Next_Entity (E);
453
         end loop;
454
      end;
455
 
456
      Check_References (Body_Id);
457
 
458
      --  For a generic unit, check that the formal parameters are referenced,
459
      --  and that local variables are used, as for regular packages.
460
 
461
      if Ekind (Spec_Id) = E_Generic_Package then
462
         Check_References (Spec_Id);
463
      end if;
464
 
465
      --  The processing so far has made all entities of the package body
466
      --  public (i.e. externally visible to the linker). This is in general
467
      --  necessary, since inlined or generic bodies, for which code is
468
      --  generated in other units, may need to see these entities. The
469
      --  following loop runs backwards from the end of the entities of the
470
      --  package body making these entities invisible until we reach a
471
      --  referencer, i.e. a declaration that could reference a previous
472
      --  declaration, a generic body or an inlined body, or a stub (which may
473
      --  contain either of these). This is of course an approximation, but it
474
      --  is conservative and definitely correct.
475
 
476
      --  We only do this at the outer (library) level non-generic packages.
477
      --  The reason is simply to cut down on the number of external symbols
478
      --  generated, so this is simply an optimization of the efficiency
479
      --  of the compilation process. It has no other effect.
480
 
481
      if (Scope (Spec_Id) = Standard_Standard or else Is_Child_Unit (Spec_Id))
482
        and then not Is_Generic_Unit (Spec_Id)
483
        and then Present (Declarations (N))
484
      then
485
         Make_Non_Public_Where_Possible : declare
486
 
487
            function Has_Referencer
488
              (L     : List_Id;
489
               Outer : Boolean)
490
               return  Boolean;
491
            --  Traverse the given list of declarations in reverse order.
492
            --  Return True as soon as a referencer is reached. Return False if
493
            --  none is found. The Outer parameter is True for the outer level
494
            --  call, and False for inner level calls for nested packages. If
495
            --  Outer is True, then any entities up to the point of hitting a
496
            --  referencer get their Is_Public flag cleared, so that the
497
            --  entities will be treated as static entities in the C sense, and
498
            --  need not have fully qualified names. For inner levels, we need
499
            --  all names to be fully qualified to deal with the same name
500
            --  appearing in parallel packages (right now this is tied to their
501
            --  being external).
502
 
503
            --------------------
504
            -- Has_Referencer --
505
            --------------------
506
 
507
            function Has_Referencer
508
              (L     : List_Id;
509
               Outer : Boolean)
510
               return  Boolean
511
            is
512
               D : Node_Id;
513
               E : Entity_Id;
514
               K : Node_Kind;
515
               S : Entity_Id;
516
 
517
            begin
518
               if No (L) then
519
                  return False;
520
               end if;
521
 
522
               D := Last (L);
523
               while Present (D) loop
524
                  K := Nkind (D);
525
 
526
                  if K in N_Body_Stub then
527
                     return True;
528
 
529
                  elsif K = N_Subprogram_Body then
530
                     if Acts_As_Spec (D) then
531
                        E := Defining_Entity (D);
532
 
533
                        --  An inlined body acts as a referencer. Note also
534
                        --  that we never reset Is_Public for an inlined
535
                        --  subprogram. Gigi requires Is_Public to be set.
536
 
537
                        --  Note that we test Has_Pragma_Inline here rather
538
                        --  than Is_Inlined. We are compiling this for a
539
                        --  client, and it is the client who will decide if
540
                        --  actual inlining should occur, so we need to assume
541
                        --  that the procedure could be inlined for the purpose
542
                        --  of accessing global entities.
543
 
544
                        if Has_Pragma_Inline (E) then
545
                           return True;
546
                        else
547
                           Set_Is_Public (E, False);
548
                        end if;
549
 
550
                     else
551
                        E := Corresponding_Spec (D);
552
 
553
                        if Present (E)
554
                          and then (Is_Generic_Unit (E)
555
                                     or else Has_Pragma_Inline (E)
556
                                     or else Is_Inlined (E))
557
                        then
558
                           return True;
559
                        end if;
560
                     end if;
561
 
562
                  --  Processing for package bodies
563
 
564
                  elsif K = N_Package_Body
565
                    and then Present (Corresponding_Spec (D))
566
                  then
567
                     E := Corresponding_Spec (D);
568
 
569
                     --  Generic package body is a referencer. It would seem
570
                     --  that we only have to consider generics that can be
571
                     --  exported, i.e. where the corresponding spec is the
572
                     --  spec of the current package, but because of nested
573
                     --  instantiations, a fully private generic body may
574
                     --  export other private body entities.
575
 
576
                     if Is_Generic_Unit (E) then
577
                        return True;
578
 
579
                     --  For non-generic package body, recurse into body unless
580
                     --  this is an instance, we ignore instances since they
581
                     --  cannot have references that affect outer entities.
582
 
583
                     elsif not Is_Generic_Instance (E) then
584
                        if Has_Referencer
585
                             (Declarations (D), Outer => False)
586
                        then
587
                           return True;
588
                        end if;
589
                     end if;
590
 
591
                  --  Processing for package specs, recurse into declarations.
592
                  --  Again we skip this for the case of generic instances.
593
 
594
                  elsif K = N_Package_Declaration then
595
                     S := Specification (D);
596
 
597
                     if not Is_Generic_Unit (Defining_Entity (S)) then
598
                        if Has_Referencer
599
                             (Private_Declarations (S), Outer => False)
600
                        then
601
                           return True;
602
                        elsif Has_Referencer
603
                               (Visible_Declarations (S), Outer => False)
604
                        then
605
                           return True;
606
                        end if;
607
                     end if;
608
 
609
                  --  Objects and exceptions need not be public if we have not
610
                  --  encountered a referencer so far. We only reset the flag
611
                  --  for outer level entities that are not imported/exported,
612
                  --  and which have no interface name.
613
 
614
                  elsif Nkind_In (K, N_Object_Declaration,
615
                                     N_Exception_Declaration,
616
                                     N_Subprogram_Declaration)
617
                  then
618
                     E := Defining_Entity (D);
619
 
620
                     if Outer
621
                       and then not Is_Imported (E)
622
                       and then not Is_Exported (E)
623
                       and then No (Interface_Name (E))
624
                     then
625
                        Set_Is_Public (E, False);
626
                     end if;
627
                  end if;
628
 
629
                  Prev (D);
630
               end loop;
631
 
632
               return False;
633
            end Has_Referencer;
634
 
635
         --  Start of processing for Make_Non_Public_Where_Possible
636
 
637
         begin
638
            declare
639
               Discard : Boolean;
640
               pragma Warnings (Off, Discard);
641
 
642
            begin
643
               Discard := Has_Referencer (Declarations (N), Outer => True);
644
            end;
645
         end Make_Non_Public_Where_Possible;
646
      end if;
647
 
648
      --  If expander is not active, then here is where we turn off the
649
      --  In_Package_Body flag, otherwise it is turned off at the end of the
650
      --  corresponding expansion routine. If this is an instance body, we need
651
      --  to qualify names of local entities, because the body may have been
652
      --  compiled as a preliminary to another instantiation.
653
 
654
      if not Expander_Active then
655
         Set_In_Package_Body (Spec_Id, False);
656
 
657
         if Is_Generic_Instance (Spec_Id)
658
           and then Operating_Mode = Generate_Code
659
         then
660
            Qualify_Entity_Names (N);
661
         end if;
662
      end if;
663
   end Analyze_Package_Body_Helper;
664
 
665
   ---------------------------------
666
   -- Analyze_Package_Declaration --
667
   ---------------------------------
668
 
669
   procedure Analyze_Package_Declaration (N : Node_Id) is
670
      Id : constant Node_Id := Defining_Entity (N);
671
 
672
      PF : Boolean;
673
      --  True when in the context of a declared pure library unit
674
 
675
      Body_Required : Boolean;
676
      --  True when this package declaration requires a corresponding body
677
 
678
      Comp_Unit : Boolean;
679
      --  True when this package declaration is not a nested declaration
680
 
681
   begin
682
      --  Ada 2005 (AI-217): Check if the package has been erroneously named
683
      --  in a limited-with clause of its own context. In this case the error
684
      --  has been previously notified by Analyze_Context.
685
 
686
      --     limited with Pkg; -- ERROR
687
      --     package Pkg is ...
688
 
689
      if From_With_Type (Id) then
690
         return;
691
      end if;
692
 
693
      if Debug_Flag_C then
694
         Write_Str ("==> package spec ");
695
         Write_Name (Chars (Id));
696
         Write_Str (" from ");
697
         Write_Location (Sloc (N));
698
         Write_Eol;
699
         Indent;
700
      end if;
701
 
702
      Generate_Definition (Id);
703
      Enter_Name (Id);
704
      Set_Ekind (Id, E_Package);
705
      Set_Etype (Id, Standard_Void_Type);
706
 
707
      Push_Scope (Id);
708
 
709
      PF := Is_Pure (Enclosing_Lib_Unit_Entity);
710
      Set_Is_Pure (Id, PF);
711
 
712
      Set_Categorization_From_Pragmas (N);
713
 
714
      Analyze (Specification (N));
715
      Validate_Categorization_Dependency (N, Id);
716
 
717
      Body_Required := Unit_Requires_Body (Id);
718
 
719
      --  When this spec does not require an explicit body, we know that there
720
      --  are no entities requiring completion in the language sense; we call
721
      --  Check_Completion here only to ensure that any nested package
722
      --  declaration that requires an implicit body gets one. (In the case
723
      --  where a body is required, Check_Completion is called at the end of
724
      --  the body's declarative part.)
725
 
726
      if not Body_Required then
727
         Check_Completion;
728
      end if;
729
 
730
      Comp_Unit := Nkind (Parent (N)) = N_Compilation_Unit;
731
      if Comp_Unit then
732
 
733
         --  Set Body_Required indication on the compilation unit node, and
734
         --  determine whether elaboration warnings may be meaningful on it.
735
 
736
         Set_Body_Required (Parent (N), Body_Required);
737
 
738
         if not Body_Required then
739
            Set_Suppress_Elaboration_Warnings (Id);
740
         end if;
741
 
742
      end if;
743
 
744
      End_Package_Scope (Id);
745
 
746
      --  For the declaration of a library unit that is a remote types package,
747
      --  check legality rules regarding availability of stream attributes for
748
      --  types that contain non-remote access values. This subprogram performs
749
      --  visibility tests that rely on the fact that we have exited the scope
750
      --  of Id.
751
 
752
      if Comp_Unit then
753
         Validate_RT_RAT_Component (N);
754
      end if;
755
 
756
      if Debug_Flag_C then
757
         Outdent;
758
         Write_Str ("<== package spec ");
759
         Write_Name (Chars (Id));
760
         Write_Str (" from ");
761
         Write_Location (Sloc (N));
762
         Write_Eol;
763
      end if;
764
   end Analyze_Package_Declaration;
765
 
766
   -----------------------------------
767
   -- Analyze_Package_Specification --
768
   -----------------------------------
769
 
770
   --  Note that this code is shared for the analysis of generic package specs
771
   --  (see Sem_Ch12.Analyze_Generic_Package_Declaration for details).
772
 
773
   procedure Analyze_Package_Specification (N : Node_Id) is
774
      Id           : constant Entity_Id  := Defining_Entity (N);
775
      Orig_Decl    : constant Node_Id    := Original_Node (Parent (N));
776
      Vis_Decls    : constant List_Id    := Visible_Declarations (N);
777
      Priv_Decls   : constant List_Id    := Private_Declarations (N);
778
      E            : Entity_Id;
779
      L            : Entity_Id;
780
      Public_Child : Boolean;
781
 
782
      Private_With_Clauses_Installed : Boolean := False;
783
      --  In Ada 2005, private with_clauses are visible in the private part
784
      --  of a nested package, even if it appears in the public part of the
785
      --  enclosing package. This requires a separate step to install these
786
      --  private_with_clauses, and remove them at the end of the nested
787
      --  package.
788
 
789
      procedure Analyze_PPCs (Decls : List_Id);
790
      --  Given a list of declarations, go through looking for subprogram
791
      --  specs, and for each one found, analyze any pre/postconditions that
792
      --  are chained to the spec. This is the implementation of the late
793
      --  visibility analysis for preconditions and postconditions in specs.
794
 
795
      procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id);
796
      --  Clears constant indications (Never_Set_In_Source, Constant_Value, and
797
      --  Is_True_Constant) on all variables that are entities of Id, and on
798
      --  the chain whose first element is FE. A recursive call is made for all
799
      --  packages and generic packages.
800
 
801
      procedure Generate_Parent_References;
802
      --  For a child unit, generate references to parent units, for
803
      --  GPS navigation purposes.
804
 
805
      function Is_Public_Child (Child, Unit : Entity_Id) return Boolean;
806
      --  Child and Unit are entities of compilation units. True if Child
807
      --  is a public child of Parent as defined in 10.1.1
808
 
809
      procedure Inspect_Unchecked_Union_Completion (Decls : List_Id);
810
      --  Detects all incomplete or private type declarations having a known
811
      --  discriminant part that are completed by an Unchecked_Union. Emits
812
      --  the error message "Unchecked_Union may not complete discriminated
813
      --  partial view".
814
 
815
      procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id);
816
      --  Given the package entity of a generic package instantiation or
817
      --  formal package whose corresponding generic is a child unit, installs
818
      --  the private declarations of each of the child unit's parents.
819
      --  This has to be done at the point of entering the instance package's
820
      --  private part rather than being done in Sem_Ch12.Install_Parent
821
      --  (which is where the parents' visible declarations are installed).
822
 
823
      ------------------
824
      -- Analyze_PPCs --
825
      ------------------
826
 
827
      procedure Analyze_PPCs (Decls : List_Id) is
828
         Decl : Node_Id;
829
         Spec : Node_Id;
830
         Sent : Entity_Id;
831
         Prag : Node_Id;
832
 
833
      begin
834
         Decl := First (Decls);
835
         while Present (Decl) loop
836
            if Nkind (Original_Node (Decl)) = N_Subprogram_Declaration then
837
               Spec := Specification (Original_Node (Decl));
838
               Sent := Defining_Unit_Name (Spec);
839
               Prag := Spec_PPC_List (Sent);
840
               while Present (Prag) loop
841
                  Analyze_PPC_In_Decl_Part (Prag, Sent);
842
                  Prag := Next_Pragma (Prag);
843
               end loop;
844
            end if;
845
 
846
            Next (Decl);
847
         end loop;
848
      end Analyze_PPCs;
849
 
850
      ---------------------
851
      -- Clear_Constants --
852
      ---------------------
853
 
854
      procedure Clear_Constants (Id : Entity_Id; FE : Entity_Id) is
855
         E : Entity_Id;
856
 
857
      begin
858
         --  Ignore package renamings, not interesting and they can cause self
859
         --  referential loops in the code below.
860
 
861
         if Nkind (Parent (Id)) = N_Package_Renaming_Declaration then
862
            return;
863
         end if;
864
 
865
         --  Note: in the loop below, the check for Next_Entity pointing back
866
         --  to the package entity may seem odd, but it is needed, because a
867
         --  package can contain a renaming declaration to itself, and such
868
         --  renamings are generated automatically within package instances.
869
 
870
         E := FE;
871
         while Present (E) and then E /= Id loop
872
            if Is_Assignable (E) then
873
               Set_Never_Set_In_Source (E, False);
874
               Set_Is_True_Constant    (E, False);
875
               Set_Current_Value       (E, Empty);
876
               Set_Is_Known_Null       (E, False);
877
               Set_Last_Assignment     (E, Empty);
878
 
879
               if not Can_Never_Be_Null (E) then
880
                  Set_Is_Known_Non_Null (E, False);
881
               end if;
882
 
883
            elsif Is_Package_Or_Generic_Package (E) then
884
               Clear_Constants (E, First_Entity (E));
885
               Clear_Constants (E, First_Private_Entity (E));
886
            end if;
887
 
888
            Next_Entity (E);
889
         end loop;
890
      end Clear_Constants;
891
 
892
      --------------------------------
893
      -- Generate_Parent_References --
894
      --------------------------------
895
 
896
      procedure Generate_Parent_References is
897
         Decl : constant Node_Id := Parent (N);
898
 
899
      begin
900
         if Id = Cunit_Entity (Main_Unit)
901
           or else Parent (Decl) = Library_Unit (Cunit (Main_Unit))
902
         then
903
            Generate_Reference (Id, Scope (Id), 'k', False);
904
 
905
         elsif not Nkind_In (Unit (Cunit (Main_Unit)), N_Subprogram_Body,
906
                                                       N_Subunit)
907
         then
908
            --  If current unit is an ancestor of main unit, generate a
909
            --  reference to its own parent.
910
 
911
            declare
912
               U         : Node_Id;
913
               Main_Spec : Node_Id := Unit (Cunit (Main_Unit));
914
 
915
            begin
916
               if Nkind (Main_Spec) = N_Package_Body then
917
                  Main_Spec := Unit (Library_Unit (Cunit (Main_Unit)));
918
               end if;
919
 
920
               U := Parent_Spec (Main_Spec);
921
               while Present (U) loop
922
                  if U = Parent (Decl) then
923
                     Generate_Reference (Id, Scope (Id), 'k',  False);
924
                     exit;
925
 
926
                  elsif Nkind (Unit (U)) = N_Package_Body then
927
                     exit;
928
 
929
                  else
930
                     U := Parent_Spec (Unit (U));
931
                  end if;
932
               end loop;
933
            end;
934
         end if;
935
      end Generate_Parent_References;
936
 
937
      ---------------------
938
      -- Is_Public_Child --
939
      ---------------------
940
 
941
      function Is_Public_Child (Child, Unit : Entity_Id) return Boolean is
942
      begin
943
         if not Is_Private_Descendant (Child) then
944
            return True;
945
         else
946
            if Child = Unit then
947
               return not Private_Present (
948
                 Parent (Unit_Declaration_Node (Child)));
949
            else
950
               return Is_Public_Child (Scope (Child), Unit);
951
            end if;
952
         end if;
953
      end Is_Public_Child;
954
 
955
      ----------------------------------------
956
      -- Inspect_Unchecked_Union_Completion --
957
      ----------------------------------------
958
 
959
      procedure Inspect_Unchecked_Union_Completion (Decls : List_Id) is
960
         Decl : Node_Id;
961
 
962
      begin
963
         Decl := First (Decls);
964
         while Present (Decl) loop
965
 
966
            --  We are looking at an incomplete or private type declaration
967
            --  with a known_discriminant_part whose full view is an
968
            --  Unchecked_Union.
969
 
970
            if Nkind_In (Decl, N_Incomplete_Type_Declaration,
971
                               N_Private_Type_Declaration)
972
              and then Has_Discriminants (Defining_Identifier (Decl))
973
              and then Present (Full_View (Defining_Identifier (Decl)))
974
              and then
975
                Is_Unchecked_Union (Full_View (Defining_Identifier (Decl)))
976
            then
977
               Error_Msg_N
978
                 ("completion of discriminated partial view "
979
                  & "cannot be an Unchecked_Union",
980
                 Full_View (Defining_Identifier (Decl)));
981
            end if;
982
 
983
            Next (Decl);
984
         end loop;
985
      end Inspect_Unchecked_Union_Completion;
986
 
987
      -----------------------------------------
988
      -- Install_Parent_Private_Declarations --
989
      -----------------------------------------
990
 
991
      procedure Install_Parent_Private_Declarations (Inst_Id : Entity_Id) is
992
         Inst_Par  : Entity_Id;
993
         Gen_Par   : Entity_Id;
994
         Inst_Node : Node_Id;
995
 
996
      begin
997
         Inst_Par := Inst_Id;
998
 
999
         Gen_Par :=
1000
           Generic_Parent (Specification (Unit_Declaration_Node (Inst_Par)));
1001
         while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
1002
            Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
1003
 
1004
            if Nkind_In (Inst_Node, N_Package_Instantiation,
1005
                                    N_Formal_Package_Declaration)
1006
              and then Nkind (Name (Inst_Node)) = N_Expanded_Name
1007
            then
1008
               Inst_Par := Entity (Prefix (Name (Inst_Node)));
1009
 
1010
               if Present (Renamed_Entity (Inst_Par)) then
1011
                  Inst_Par := Renamed_Entity (Inst_Par);
1012
               end if;
1013
 
1014
               Gen_Par :=
1015
                 Generic_Parent
1016
                   (Specification (Unit_Declaration_Node (Inst_Par)));
1017
 
1018
               --  Install the private declarations and private use clauses
1019
               --  of a parent instance of the child instance, unless the
1020
               --  parent instance private declarations have already been
1021
               --  installed earlier in Analyze_Package_Specification, which
1022
               --  happens when a generic child is instantiated, and the
1023
               --  instance is a child of the parent instance.
1024
 
1025
               --  Installing the use clauses of the parent instance twice
1026
               --  is both unnecessary and wrong, because it would cause the
1027
               --  clauses to be chained to themselves in the use clauses
1028
               --  list of the scope stack entry. That in turn would cause
1029
               --  an endless loop from End_Use_Clauses upon scope exit.
1030
 
1031
               --  The parent is now fully visible. It may be a hidden open
1032
               --  scope if we are currently compiling some child instance
1033
               --  declared within it, but while the current instance is being
1034
               --  compiled the parent is immediately visible. In particular
1035
               --  its entities must remain visible if a stack save/restore
1036
               --  takes place through a call to Rtsfind.
1037
 
1038
               if Present (Gen_Par) then
1039
                  if not In_Private_Part (Inst_Par) then
1040
                     Install_Private_Declarations (Inst_Par);
1041
                     Set_Use (Private_Declarations
1042
                                (Specification
1043
                                   (Unit_Declaration_Node (Inst_Par))));
1044
                     Set_Is_Hidden_Open_Scope (Inst_Par, False);
1045
                  end if;
1046
 
1047
               --  If we've reached the end of the generic instance parents,
1048
               --  then finish off by looping through the nongeneric parents
1049
               --  and installing their private declarations.
1050
 
1051
               else
1052
                  while Present (Inst_Par)
1053
                    and then Inst_Par /= Standard_Standard
1054
                    and then (not In_Open_Scopes (Inst_Par)
1055
                                or else not In_Private_Part (Inst_Par))
1056
                  loop
1057
                     Install_Private_Declarations (Inst_Par);
1058
                     Set_Use (Private_Declarations
1059
                                (Specification
1060
                                   (Unit_Declaration_Node (Inst_Par))));
1061
                     Inst_Par := Scope (Inst_Par);
1062
                  end loop;
1063
 
1064
                  exit;
1065
               end if;
1066
 
1067
            else
1068
               exit;
1069
            end if;
1070
         end loop;
1071
      end Install_Parent_Private_Declarations;
1072
 
1073
   --  Start of processing for Analyze_Package_Specification
1074
 
1075
   begin
1076
      if Present (Vis_Decls) then
1077
         Analyze_Declarations (Vis_Decls);
1078
         Analyze_PPCs (Vis_Decls);
1079
      end if;
1080
 
1081
      --  Verify that incomplete types have received full declarations
1082
 
1083
      E := First_Entity (Id);
1084
      while Present (E) loop
1085
         if Ekind (E) = E_Incomplete_Type
1086
           and then No (Full_View (E))
1087
         then
1088
            Error_Msg_N ("no declaration in visible part for incomplete}", E);
1089
         end if;
1090
 
1091
         Next_Entity (E);
1092
      end loop;
1093
 
1094
      if Is_Remote_Call_Interface (Id)
1095
         and then Nkind (Parent (Parent (N))) = N_Compilation_Unit
1096
      then
1097
         Validate_RCI_Declarations (Id);
1098
      end if;
1099
 
1100
      --  Save global references in the visible declarations, before installing
1101
      --  private declarations of parent unit if there is one, because the
1102
      --  privacy status of types defined in the parent will change. This is
1103
      --  only relevant for generic child units, but is done in all cases for
1104
      --  uniformity.
1105
 
1106
      if Ekind (Id) = E_Generic_Package
1107
        and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1108
      then
1109
         declare
1110
            Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1111
            Save_Priv : constant List_Id := Private_Declarations (Orig_Spec);
1112
 
1113
         begin
1114
            Set_Private_Declarations (Orig_Spec, Empty_List);
1115
            Save_Global_References   (Orig_Decl);
1116
            Set_Private_Declarations (Orig_Spec, Save_Priv);
1117
         end;
1118
      end if;
1119
 
1120
      --  If package is a public child unit, then make the private declarations
1121
      --  of the parent visible.
1122
 
1123
      Public_Child := False;
1124
 
1125
      declare
1126
         Par       : Entity_Id;
1127
         Pack_Decl : Node_Id;
1128
         Par_Spec  : Node_Id;
1129
 
1130
      begin
1131
         Par := Id;
1132
         Par_Spec := Parent_Spec (Parent (N));
1133
 
1134
         --  If the package is formal package of an enclosing generic, it is
1135
         --  transformed into a local generic declaration, and compiled to make
1136
         --  its spec available. We need to retrieve the original generic to
1137
         --  determine whether it is a child unit, and install its parents.
1138
 
1139
         if No (Par_Spec)
1140
           and then
1141
             Nkind (Original_Node (Parent (N))) = N_Formal_Package_Declaration
1142
         then
1143
            Par := Entity (Name (Original_Node (Parent (N))));
1144
            Par_Spec := Parent_Spec (Unit_Declaration_Node (Par));
1145
         end if;
1146
 
1147
         if Present (Par_Spec) then
1148
            Generate_Parent_References;
1149
 
1150
            while Scope (Par) /= Standard_Standard
1151
              and then Is_Public_Child (Id, Par)
1152
              and then In_Open_Scopes (Par)
1153
            loop
1154
               Public_Child := True;
1155
               Par := Scope (Par);
1156
               Install_Private_Declarations (Par);
1157
               Install_Private_With_Clauses (Par);
1158
               Pack_Decl := Unit_Declaration_Node (Par);
1159
               Set_Use (Private_Declarations (Specification (Pack_Decl)));
1160
            end loop;
1161
         end if;
1162
      end;
1163
 
1164
      if Is_Compilation_Unit (Id) then
1165
         Install_Private_With_Clauses (Id);
1166
      else
1167
 
1168
         --  The current compilation unit may include private with_clauses,
1169
         --  which are visible in the private part of the current nested
1170
         --  package, and have to be installed now. This is not done for
1171
         --  nested instantiations, where the private with_clauses of the
1172
         --  enclosing unit have no effect once the instantiation info is
1173
         --  established and we start analyzing the package declaration.
1174
 
1175
         declare
1176
            Comp_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
1177
         begin
1178
            if Is_Package_Or_Generic_Package (Comp_Unit)
1179
              and then not In_Private_Part (Comp_Unit)
1180
              and then not In_Instance
1181
            then
1182
               Install_Private_With_Clauses (Comp_Unit);
1183
               Private_With_Clauses_Installed := True;
1184
            end if;
1185
         end;
1186
      end if;
1187
 
1188
      --  If this is a package associated with a generic instance or formal
1189
      --  package, then the private declarations of each of the generic's
1190
      --  parents must be installed at this point.
1191
 
1192
      if Is_Generic_Instance (Id) then
1193
         Install_Parent_Private_Declarations (Id);
1194
      end if;
1195
 
1196
      --  Analyze private part if present. The flag In_Private_Part is reset
1197
      --  in End_Package_Scope.
1198
 
1199
      L := Last_Entity (Id);
1200
 
1201
      if Present (Priv_Decls) then
1202
         Set_In_Private_Part (Id);
1203
 
1204
         --  Upon entering a public child's private part, it may be necessary
1205
         --  to declare subprograms that were derived in the package's visible
1206
         --  part but not yet made visible.
1207
 
1208
         if Public_Child then
1209
            Declare_Inherited_Private_Subprograms (Id);
1210
         end if;
1211
 
1212
         Analyze_Declarations (Priv_Decls);
1213
         Analyze_PPCs (Priv_Decls);
1214
 
1215
         --  Check the private declarations for incomplete deferred constants
1216
 
1217
         Inspect_Deferred_Constant_Completion (Priv_Decls);
1218
 
1219
         --  The first private entity is the immediate follower of the last
1220
         --  visible entity, if there was one.
1221
 
1222
         if Present (L) then
1223
            Set_First_Private_Entity (Id, Next_Entity (L));
1224
         else
1225
            Set_First_Private_Entity (Id, First_Entity (Id));
1226
         end if;
1227
 
1228
      --  There may be inherited private subprograms that need to be declared,
1229
      --  even in the absence of an explicit private part.  If there are any
1230
      --  public declarations in the package and the package is a public child
1231
      --  unit, then an implicit private part is assumed.
1232
 
1233
      elsif Present (L) and then Public_Child then
1234
         Set_In_Private_Part (Id);
1235
         Declare_Inherited_Private_Subprograms (Id);
1236
         Set_First_Private_Entity (Id, Next_Entity (L));
1237
      end if;
1238
 
1239
      E := First_Entity (Id);
1240
      while Present (E) loop
1241
 
1242
         --  Check rule of 3.6(11), which in general requires waiting till all
1243
         --  full types have been seen.
1244
 
1245
         if Ekind (E) = E_Record_Type or else Ekind (E) = E_Array_Type then
1246
            Check_Aliased_Component_Types (E);
1247
         end if;
1248
 
1249
         --  Check preelaborable initialization for full type completing a
1250
         --  private type for which pragma Preelaborable_Initialization given.
1251
 
1252
         if Is_Type (E)
1253
           and then Must_Have_Preelab_Init (E)
1254
           and then not Has_Preelaborable_Initialization (E)
1255
         then
1256
            Error_Msg_N
1257
              ("full view of & does not have preelaborable initialization", E);
1258
         end if;
1259
 
1260
         Next_Entity (E);
1261
      end loop;
1262
 
1263
      --  Ada 2005 (AI-216): The completion of an incomplete or private type
1264
      --  declaration having a known_discriminant_part shall not be an
1265
      --  Unchecked_Union type.
1266
 
1267
      if Present (Vis_Decls) then
1268
         Inspect_Unchecked_Union_Completion (Vis_Decls);
1269
      end if;
1270
 
1271
      if Present (Priv_Decls) then
1272
         Inspect_Unchecked_Union_Completion (Priv_Decls);
1273
      end if;
1274
 
1275
      if Ekind (Id) = E_Generic_Package
1276
        and then Nkind (Orig_Decl) = N_Generic_Package_Declaration
1277
        and then Present (Priv_Decls)
1278
      then
1279
         --  Save global references in private declarations, ignoring the
1280
         --  visible declarations that were processed earlier.
1281
 
1282
         declare
1283
            Orig_Spec : constant Node_Id := Specification (Orig_Decl);
1284
            Save_Vis  : constant List_Id := Visible_Declarations (Orig_Spec);
1285
            Save_Form : constant List_Id :=
1286
                          Generic_Formal_Declarations (Orig_Decl);
1287
 
1288
         begin
1289
            Set_Visible_Declarations        (Orig_Spec, Empty_List);
1290
            Set_Generic_Formal_Declarations (Orig_Decl, Empty_List);
1291
            Save_Global_References          (Orig_Decl);
1292
            Set_Generic_Formal_Declarations (Orig_Decl, Save_Form);
1293
            Set_Visible_Declarations        (Orig_Spec, Save_Vis);
1294
         end;
1295
      end if;
1296
 
1297
      Process_End_Label (N, 'e', Id);
1298
 
1299
      --  Remove private_with_clauses of enclosing compilation unit, if they
1300
      --  were installed.
1301
 
1302
      if Private_With_Clauses_Installed then
1303
         Remove_Private_With_Clauses (Cunit (Current_Sem_Unit));
1304
      end if;
1305
 
1306
      --  For the case of a library level package, we must go through all the
1307
      --  entities clearing the indications that the value may be constant and
1308
      --  not modified. Why? Because any client of this package may modify
1309
      --  these values freely from anywhere. This also applies to any nested
1310
      --  packages or generic packages.
1311
 
1312
      --  For now we unconditionally clear constants for packages that are
1313
      --  instances of generic packages. The reason is that we do not have the
1314
      --  body yet, and we otherwise think things are unreferenced when they
1315
      --  are not. This should be fixed sometime (the effect is not terrible,
1316
      --  we just lose some warnings, and also some cases of value propagation)
1317
      --  ???
1318
 
1319
      if Is_Library_Level_Entity (Id)
1320
        or else Is_Generic_Instance (Id)
1321
      then
1322
         Clear_Constants (Id, First_Entity (Id));
1323
         Clear_Constants (Id, First_Private_Entity (Id));
1324
      end if;
1325
   end Analyze_Package_Specification;
1326
 
1327
   --------------------------------------
1328
   -- Analyze_Private_Type_Declaration --
1329
   --------------------------------------
1330
 
1331
   procedure Analyze_Private_Type_Declaration (N : Node_Id) is
1332
      PF : constant Boolean   := Is_Pure (Enclosing_Lib_Unit_Entity);
1333
      Id : constant Entity_Id := Defining_Identifier (N);
1334
 
1335
   begin
1336
      Generate_Definition (Id);
1337
      Set_Is_Pure         (Id, PF);
1338
      Init_Size_Align     (Id);
1339
 
1340
      if not Is_Package_Or_Generic_Package (Current_Scope)
1341
        or else In_Private_Part (Current_Scope)
1342
      then
1343
         Error_Msg_N ("invalid context for private declaration", N);
1344
      end if;
1345
 
1346
      New_Private_Type (N, Id, N);
1347
      Set_Depends_On_Private (Id);
1348
   end Analyze_Private_Type_Declaration;
1349
 
1350
   ----------------------------------
1351
   -- Check_Anonymous_Access_Types --
1352
   ----------------------------------
1353
 
1354
   procedure Check_Anonymous_Access_Types
1355
     (Spec_Id : Entity_Id;
1356
      P_Body  : Node_Id)
1357
   is
1358
      E  : Entity_Id;
1359
      IR : Node_Id;
1360
 
1361
   begin
1362
      --  Itype references are only needed by gigi, to force elaboration of
1363
      --  itypes. In the absence of code generation, they are not needed.
1364
 
1365
      if not Expander_Active then
1366
         return;
1367
      end if;
1368
 
1369
      E := First_Entity (Spec_Id);
1370
      while Present (E) loop
1371
         if Ekind (E) = E_Anonymous_Access_Type
1372
           and then From_With_Type (E)
1373
         then
1374
            IR := Make_Itype_Reference (Sloc (P_Body));
1375
            Set_Itype (IR, E);
1376
 
1377
            if No (Declarations (P_Body)) then
1378
               Set_Declarations (P_Body, New_List (IR));
1379
            else
1380
               Prepend (IR, Declarations (P_Body));
1381
            end if;
1382
         end if;
1383
 
1384
         Next_Entity (E);
1385
      end loop;
1386
   end Check_Anonymous_Access_Types;
1387
 
1388
   -------------------------------------------
1389
   -- Declare_Inherited_Private_Subprograms --
1390
   -------------------------------------------
1391
 
1392
   procedure Declare_Inherited_Private_Subprograms (Id : Entity_Id) is
1393
 
1394
      function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean;
1395
      --  Check whether an inherited subprogram is an operation of an untagged
1396
      --  derived type.
1397
 
1398
      ---------------------
1399
      -- Is_Primitive_Of --
1400
      ---------------------
1401
 
1402
      function Is_Primitive_Of (T : Entity_Id; S : Entity_Id) return Boolean is
1403
         Formal : Entity_Id;
1404
 
1405
      begin
1406
         --  If the full view is a scalar type, the type is the anonymous base
1407
         --  type, but the operation mentions the first subtype, so check the
1408
         --  signature against the base type.
1409
 
1410
         if Base_Type (Etype (S)) = Base_Type (T) then
1411
            return True;
1412
 
1413
         else
1414
            Formal := First_Formal (S);
1415
            while Present (Formal) loop
1416
               if Base_Type (Etype (Formal)) = Base_Type (T) then
1417
                  return True;
1418
               end if;
1419
 
1420
               Next_Formal (Formal);
1421
            end loop;
1422
 
1423
            return False;
1424
         end if;
1425
      end Is_Primitive_Of;
1426
 
1427
      --  Local variables
1428
 
1429
      E           : Entity_Id;
1430
      Op_List     : Elist_Id;
1431
      Op_Elmt     : Elmt_Id;
1432
      Op_Elmt_2   : Elmt_Id;
1433
      Prim_Op     : Entity_Id;
1434
      New_Op      : Entity_Id := Empty;
1435
      Parent_Subp : Entity_Id;
1436
      Tag         : Entity_Id;
1437
 
1438
   --  Start of processing for Declare_Inherited_Private_Subprograms
1439
 
1440
   begin
1441
      E := First_Entity (Id);
1442
      while Present (E) loop
1443
 
1444
         --  If the entity is a nonprivate type extension whose parent type
1445
         --  is declared in an open scope, then the type may have inherited
1446
         --  operations that now need to be made visible. Ditto if the entity
1447
         --  is a formal derived type in a child unit.
1448
 
1449
         if ((Is_Derived_Type (E) and then not Is_Private_Type (E))
1450
               or else
1451
                 (Nkind (Parent (E)) = N_Private_Extension_Declaration
1452
                   and then Is_Generic_Type (E)))
1453
           and then In_Open_Scopes (Scope (Etype (E)))
1454
           and then E = Base_Type (E)
1455
         then
1456
            if Is_Tagged_Type (E) then
1457
               Op_List := Primitive_Operations (E);
1458
               New_Op  := Empty;
1459
               Tag     := First_Tag_Component (E);
1460
 
1461
               Op_Elmt := First_Elmt (Op_List);
1462
               while Present (Op_Elmt) loop
1463
                  Prim_Op := Node (Op_Elmt);
1464
 
1465
                  --  Search primitives that are implicit operations with an
1466
                  --  internal name whose parent operation has a normal name.
1467
 
1468
                  if Present (Alias (Prim_Op))
1469
                    and then Find_Dispatching_Type (Alias (Prim_Op)) /= E
1470
                    and then not Comes_From_Source (Prim_Op)
1471
                    and then Is_Internal_Name (Chars (Prim_Op))
1472
                    and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1473
                  then
1474
                     Parent_Subp := Alias (Prim_Op);
1475
 
1476
                     --  Case 1: Check if the type has also an explicit
1477
                     --  overriding for this primitive.
1478
 
1479
                     Op_Elmt_2 := Next_Elmt (Op_Elmt);
1480
                     while Present (Op_Elmt_2) loop
1481
                        if Chars (Node (Op_Elmt_2)) = Chars (Parent_Subp)
1482
                          and then Type_Conformant (Prim_Op, Node (Op_Elmt_2))
1483
                        then
1484
                           --  The private inherited operation has been
1485
                           --  overridden by an explicit subprogram: replace
1486
                           --  the former by the latter.
1487
 
1488
                           New_Op := Node (Op_Elmt_2);
1489
                           Replace_Elmt (Op_Elmt, New_Op);
1490
                           Remove_Elmt  (Op_List, Op_Elmt_2);
1491
                           Set_Is_Overriding_Operation (New_Op);
1492
                           Set_Overridden_Operation (New_Op, Parent_Subp);
1493
 
1494
                           --  We don't need to inherit its dispatching slot.
1495
                           --  Set_All_DT_Position has previously ensured that
1496
                           --  the same slot was assigned to the two primitives
1497
 
1498
                           if Present (Tag)
1499
                             and then Present (DTC_Entity (New_Op))
1500
                             and then Present (DTC_Entity (Prim_Op))
1501
                           then
1502
                              pragma Assert (DT_Position (New_Op)
1503
                                              = DT_Position (Prim_Op));
1504
                              null;
1505
                           end if;
1506
 
1507
                           goto Next_Primitive;
1508
                        end if;
1509
 
1510
                        Next_Elmt (Op_Elmt_2);
1511
                     end loop;
1512
 
1513
                     --  Case 2: We have not found any explicit overriding and
1514
                     --  hence we need to declare the operation (i.e., make it
1515
                     --  visible).
1516
 
1517
                     Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1518
 
1519
                     --  Inherit the dispatching slot if E is already frozen
1520
 
1521
                     if Is_Frozen (E)
1522
                       and then Present (DTC_Entity (Alias (Prim_Op)))
1523
                     then
1524
                        Set_DTC_Entity_Value (E, New_Op);
1525
                        Set_DT_Position (New_Op,
1526
                          DT_Position (Alias (Prim_Op)));
1527
                     end if;
1528
 
1529
                     pragma Assert
1530
                       (Is_Dispatching_Operation (New_Op)
1531
                         and then Node (Last_Elmt (Op_List)) = New_Op);
1532
 
1533
                     --  Substitute the new operation for the old one in the
1534
                     --  type's primitive operations list. Since the new
1535
                     --  operation was also just added to the end of list,
1536
                     --  the last element must be removed.
1537
 
1538
                     --  (Question: is there a simpler way of declaring the
1539
                     --  operation, say by just replacing the name of the
1540
                     --  earlier operation, reentering it in the in the symbol
1541
                     --  table (how?), and marking it as private???)
1542
 
1543
                     Replace_Elmt (Op_Elmt, New_Op);
1544
                     Remove_Last_Elmt (Op_List);
1545
                  end if;
1546
 
1547
                  <<Next_Primitive>>
1548
                  Next_Elmt (Op_Elmt);
1549
               end loop;
1550
 
1551
               --  Generate listing showing the contents of the dispatch table
1552
 
1553
               if Debug_Flag_ZZ then
1554
                  Write_DT (E);
1555
               end if;
1556
 
1557
            else
1558
               --  Non-tagged type, scan forward to locate inherited hidden
1559
               --  operations.
1560
 
1561
               Prim_Op := Next_Entity (E);
1562
               while Present (Prim_Op) loop
1563
                  if Is_Subprogram (Prim_Op)
1564
                    and then Present (Alias (Prim_Op))
1565
                    and then not Comes_From_Source (Prim_Op)
1566
                    and then Is_Internal_Name (Chars (Prim_Op))
1567
                    and then not Is_Internal_Name (Chars (Alias (Prim_Op)))
1568
                    and then Is_Primitive_Of (E, Prim_Op)
1569
                  then
1570
                     Derive_Subprogram (New_Op, Alias (Prim_Op), E, Etype (E));
1571
                  end if;
1572
 
1573
                  Next_Entity (Prim_Op);
1574
               end loop;
1575
            end if;
1576
         end if;
1577
 
1578
         Next_Entity (E);
1579
      end loop;
1580
   end Declare_Inherited_Private_Subprograms;
1581
 
1582
   -----------------------
1583
   -- End_Package_Scope --
1584
   -----------------------
1585
 
1586
   procedure End_Package_Scope (P : Entity_Id) is
1587
   begin
1588
      Uninstall_Declarations (P);
1589
      Pop_Scope;
1590
   end End_Package_Scope;
1591
 
1592
   ---------------------------
1593
   -- Exchange_Declarations --
1594
   ---------------------------
1595
 
1596
   procedure Exchange_Declarations (Id : Entity_Id) is
1597
      Full_Id : constant Entity_Id := Full_View (Id);
1598
      H1      : constant Entity_Id := Homonym (Id);
1599
      Next1   : constant Entity_Id := Next_Entity (Id);
1600
      H2      : Entity_Id;
1601
      Next2   : Entity_Id;
1602
 
1603
   begin
1604
      --  If missing full declaration for type, nothing to exchange
1605
 
1606
      if No (Full_Id) then
1607
         return;
1608
      end if;
1609
 
1610
      --  Otherwise complete the exchange, and preserve semantic links
1611
 
1612
      Next2 := Next_Entity (Full_Id);
1613
      H2    := Homonym (Full_Id);
1614
 
1615
      --  Reset full declaration pointer to reflect the switched entities and
1616
      --  readjust the next entity chains.
1617
 
1618
      Exchange_Entities (Id, Full_Id);
1619
 
1620
      Set_Next_Entity (Id, Next1);
1621
      Set_Homonym     (Id, H1);
1622
 
1623
      Set_Full_View   (Full_Id, Id);
1624
      Set_Next_Entity (Full_Id, Next2);
1625
      Set_Homonym     (Full_Id, H2);
1626
   end Exchange_Declarations;
1627
 
1628
   ----------------------------
1629
   -- Install_Package_Entity --
1630
   ----------------------------
1631
 
1632
   procedure Install_Package_Entity (Id : Entity_Id) is
1633
   begin
1634
      if not Is_Internal (Id) then
1635
         if Debug_Flag_E then
1636
            Write_Str ("Install: ");
1637
            Write_Name (Chars (Id));
1638
            Write_Eol;
1639
         end if;
1640
 
1641
         if not Is_Child_Unit (Id) then
1642
            Set_Is_Immediately_Visible (Id);
1643
         end if;
1644
 
1645
      end if;
1646
   end Install_Package_Entity;
1647
 
1648
   ----------------------------------
1649
   -- Install_Private_Declarations --
1650
   ----------------------------------
1651
 
1652
   procedure Install_Private_Declarations (P : Entity_Id) is
1653
      Id        : Entity_Id;
1654
      Priv_Elmt : Elmt_Id;
1655
      Priv      : Entity_Id;
1656
      Full      : Entity_Id;
1657
 
1658
   begin
1659
      --  First exchange declarations for private types, so that the full
1660
      --  declaration is visible. For each private type, we check its
1661
      --  Private_Dependents list and also exchange any subtypes of or derived
1662
      --  types from it. Finally, if this is a Taft amendment type, the
1663
      --  incomplete declaration is irrelevant, and we want to link the
1664
      --  eventual full declaration with the original private one so we also
1665
      --  skip the exchange.
1666
 
1667
      Id := First_Entity (P);
1668
      while Present (Id) and then Id /= First_Private_Entity (P) loop
1669
         if Is_Private_Base_Type (Id)
1670
           and then Comes_From_Source (Full_View (Id))
1671
           and then Present (Full_View (Id))
1672
           and then Scope (Full_View (Id)) = Scope (Id)
1673
           and then Ekind (Full_View (Id)) /= E_Incomplete_Type
1674
         then
1675
            --  If there is a use-type clause on the private type, set the
1676
            --  full view accordingly.
1677
 
1678
            Set_In_Use (Full_View (Id), In_Use (Id));
1679
            Full := Full_View (Id);
1680
 
1681
            if Is_Private_Base_Type (Full)
1682
              and then Has_Private_Declaration (Full)
1683
              and then Nkind (Parent (Full)) = N_Full_Type_Declaration
1684
              and then In_Open_Scopes (Scope (Etype (Full)))
1685
              and then In_Package_Body (Current_Scope)
1686
              and then not Is_Private_Type (Etype (Full))
1687
            then
1688
               --  This is the completion of a private type by a derivation
1689
               --  from another private type which is not private anymore. This
1690
               --  can only happen in a package nested within a child package,
1691
               --  when the parent type is defined in the parent unit. At this
1692
               --  point the current type is not private either, and we have to
1693
               --  install the underlying full view, which is now visible. Save
1694
               --  the current full view as well, so that all views can be
1695
               --  restored on exit. It may seem that after compiling the child
1696
               --  body there are not environments to restore, but the back-end
1697
               --  expects those links to be valid, and freeze nodes depend on
1698
               --  them.
1699
 
1700
               if No (Full_View (Full))
1701
                 and then Present (Underlying_Full_View (Full))
1702
               then
1703
                  Set_Full_View (Id, Underlying_Full_View (Full));
1704
                  Set_Underlying_Full_View (Id, Full);
1705
 
1706
                  Set_Underlying_Full_View (Full, Empty);
1707
                  Set_Is_Frozen (Full_View (Id));
1708
               end if;
1709
            end if;
1710
 
1711
            Priv_Elmt := First_Elmt (Private_Dependents (Id));
1712
 
1713
            Exchange_Declarations (Id);
1714
            Set_Is_Immediately_Visible (Id);
1715
 
1716
            while Present (Priv_Elmt) loop
1717
               Priv := Node (Priv_Elmt);
1718
 
1719
               --  Before the exchange, verify that the presence of the
1720
               --  Full_View field. It will be empty if the entity has already
1721
               --  been installed due to a previous call.
1722
 
1723
               if Present (Full_View (Priv))
1724
                 and then Is_Visible_Dependent (Priv)
1725
               then
1726
 
1727
                  --  For each subtype that is swapped, we also swap the
1728
                  --  reference to it in Private_Dependents, to allow access
1729
                  --  to it when we swap them out in End_Package_Scope.
1730
 
1731
                  Replace_Elmt (Priv_Elmt, Full_View (Priv));
1732
                  Exchange_Declarations (Priv);
1733
                  Set_Is_Immediately_Visible
1734
                    (Priv, In_Open_Scopes (Scope (Priv)));
1735
                  Set_Is_Potentially_Use_Visible
1736
                    (Priv, Is_Potentially_Use_Visible (Node (Priv_Elmt)));
1737
               end if;
1738
 
1739
               Next_Elmt (Priv_Elmt);
1740
            end loop;
1741
         end if;
1742
 
1743
         Next_Entity (Id);
1744
      end loop;
1745
 
1746
      --  Next make other declarations in the private part visible as well
1747
 
1748
      Id := First_Private_Entity (P);
1749
      while Present (Id) loop
1750
         Install_Package_Entity (Id);
1751
         Set_Is_Hidden (Id, False);
1752
         Next_Entity (Id);
1753
      end loop;
1754
 
1755
      --  Indicate that the private part is currently visible, so it can be
1756
      --  properly reset on exit.
1757
 
1758
      Set_In_Private_Part (P);
1759
   end Install_Private_Declarations;
1760
 
1761
   ----------------------------------
1762
   -- Install_Visible_Declarations --
1763
   ----------------------------------
1764
 
1765
   procedure Install_Visible_Declarations (P : Entity_Id) is
1766
      Id          : Entity_Id;
1767
      Last_Entity : Entity_Id;
1768
 
1769
   begin
1770
      pragma Assert
1771
        (Is_Package_Or_Generic_Package (P) or else Is_Record_Type (P));
1772
 
1773
      if Is_Package_Or_Generic_Package (P) then
1774
         Last_Entity := First_Private_Entity (P);
1775
      else
1776
         Last_Entity := Empty;
1777
      end if;
1778
 
1779
      Id := First_Entity (P);
1780
      while Present (Id) and then Id /= Last_Entity loop
1781
         Install_Package_Entity (Id);
1782
         Next_Entity (Id);
1783
      end loop;
1784
   end Install_Visible_Declarations;
1785
 
1786
   --------------------------
1787
   -- Is_Private_Base_Type --
1788
   --------------------------
1789
 
1790
   function Is_Private_Base_Type (E : Entity_Id) return Boolean is
1791
   begin
1792
      return Ekind (E) = E_Private_Type
1793
        or else Ekind (E) = E_Limited_Private_Type
1794
        or else Ekind (E) = E_Record_Type_With_Private;
1795
   end Is_Private_Base_Type;
1796
 
1797
   --------------------------
1798
   -- Is_Visible_Dependent --
1799
   --------------------------
1800
 
1801
   function Is_Visible_Dependent (Dep : Entity_Id) return Boolean
1802
   is
1803
      S : constant Entity_Id := Scope (Dep);
1804
 
1805
   begin
1806
      --  Renamings created for actual types have the visibility of the actual
1807
 
1808
      if Ekind (S) = E_Package
1809
        and then Is_Generic_Instance (S)
1810
        and then (Is_Generic_Actual_Type (Dep)
1811
                   or else Is_Generic_Actual_Type (Full_View (Dep)))
1812
      then
1813
         return True;
1814
 
1815
      elsif not (Is_Derived_Type (Dep))
1816
        and then Is_Derived_Type (Full_View (Dep))
1817
      then
1818
         --  When instantiating a package body, the scope stack is empty, so
1819
         --  check instead whether the dependent type is defined in the same
1820
         --  scope as the instance itself.
1821
 
1822
         return In_Open_Scopes (S)
1823
           or else (Is_Generic_Instance (Current_Scope)
1824
              and then Scope (Dep) = Scope (Current_Scope));
1825
      else
1826
         return True;
1827
      end if;
1828
   end Is_Visible_Dependent;
1829
 
1830
   ----------------------------
1831
   -- May_Need_Implicit_Body --
1832
   ----------------------------
1833
 
1834
   procedure May_Need_Implicit_Body (E : Entity_Id) is
1835
      P     : constant Node_Id := Unit_Declaration_Node (E);
1836
      S     : constant Node_Id := Parent (P);
1837
      B     : Node_Id;
1838
      Decls : List_Id;
1839
 
1840
   begin
1841
      if not Has_Completion (E)
1842
        and then Nkind (P) = N_Package_Declaration
1843
        and then (Present (Activation_Chain_Entity (P)) or else Has_RACW (E))
1844
      then
1845
         B :=
1846
           Make_Package_Body (Sloc (E),
1847
             Defining_Unit_Name => Make_Defining_Identifier (Sloc (E),
1848
               Chars => Chars (E)),
1849
             Declarations  => New_List);
1850
 
1851
         if Nkind (S) = N_Package_Specification then
1852
            if Present (Private_Declarations (S)) then
1853
               Decls := Private_Declarations (S);
1854
            else
1855
               Decls := Visible_Declarations (S);
1856
            end if;
1857
         else
1858
            Decls := Declarations (S);
1859
         end if;
1860
 
1861
         Append (B, Decls);
1862
         Analyze (B);
1863
      end if;
1864
   end May_Need_Implicit_Body;
1865
 
1866
   ----------------------
1867
   -- New_Private_Type --
1868
   ----------------------
1869
 
1870
   procedure New_Private_Type (N : Node_Id; Id : Entity_Id; Def : Node_Id) is
1871
   begin
1872
      Enter_Name (Id);
1873
 
1874
      if Limited_Present (Def) then
1875
         Set_Ekind (Id, E_Limited_Private_Type);
1876
      else
1877
         Set_Ekind (Id, E_Private_Type);
1878
      end if;
1879
 
1880
      Set_Etype              (Id, Id);
1881
      Set_Has_Delayed_Freeze (Id);
1882
      Set_Is_First_Subtype   (Id);
1883
      Init_Size_Align        (Id);
1884
 
1885
      Set_Is_Constrained (Id,
1886
        No (Discriminant_Specifications (N))
1887
          and then not Unknown_Discriminants_Present (N));
1888
 
1889
      --  Set tagged flag before processing discriminants, to catch illegal
1890
      --  usage.
1891
 
1892
      Set_Is_Tagged_Type (Id, Tagged_Present (Def));
1893
 
1894
      Set_Discriminant_Constraint (Id, No_Elist);
1895
      Set_Stored_Constraint (Id, No_Elist);
1896
 
1897
      if Present (Discriminant_Specifications (N)) then
1898
         Push_Scope (Id);
1899
         Process_Discriminants (N);
1900
         End_Scope;
1901
 
1902
      elsif Unknown_Discriminants_Present (N) then
1903
         Set_Has_Unknown_Discriminants (Id);
1904
      end if;
1905
 
1906
      Set_Private_Dependents (Id, New_Elmt_List);
1907
 
1908
      if Tagged_Present (Def) then
1909
         Set_Ekind                (Id, E_Record_Type_With_Private);
1910
         Set_Primitive_Operations (Id, New_Elmt_List);
1911
         Set_Is_Abstract_Type     (Id, Abstract_Present (Def));
1912
         Set_Is_Limited_Record    (Id, Limited_Present (Def));
1913
         Set_Has_Delayed_Freeze   (Id, True);
1914
 
1915
         --  Create a class-wide type with the same attributes
1916
 
1917
         Make_Class_Wide_Type     (Id);
1918
 
1919
      elsif Abstract_Present (Def) then
1920
         Error_Msg_N ("only a tagged type can be abstract", N);
1921
      end if;
1922
   end New_Private_Type;
1923
 
1924
   ----------------------------
1925
   -- Uninstall_Declarations --
1926
   ----------------------------
1927
 
1928
   procedure Uninstall_Declarations (P : Entity_Id) is
1929
      Decl      : constant Node_Id := Unit_Declaration_Node (P);
1930
      Id        : Entity_Id;
1931
      Full      : Entity_Id;
1932
      Priv_Elmt : Elmt_Id;
1933
      Priv_Sub  : Entity_Id;
1934
 
1935
      procedure Preserve_Full_Attributes (Priv, Full : Entity_Id);
1936
      --  Copy to the private declaration the attributes of the full view that
1937
      --  need to be available for the partial view also.
1938
 
1939
      function Type_In_Use (T : Entity_Id) return Boolean;
1940
      --  Check whether type or base type appear in an active use_type clause
1941
 
1942
      ------------------------------
1943
      -- Preserve_Full_Attributes --
1944
      ------------------------------
1945
 
1946
      procedure Preserve_Full_Attributes (Priv, Full : Entity_Id) is
1947
         Priv_Is_Base_Type : constant Boolean := Priv = Base_Type (Priv);
1948
 
1949
      begin
1950
         Set_Size_Info (Priv, (Full));
1951
         Set_RM_Size                 (Priv, RM_Size (Full));
1952
         Set_Size_Known_At_Compile_Time
1953
                                     (Priv, Size_Known_At_Compile_Time (Full));
1954
         Set_Is_Volatile             (Priv, Is_Volatile                (Full));
1955
         Set_Treat_As_Volatile       (Priv, Treat_As_Volatile          (Full));
1956
         Set_Is_Ada_2005_Only        (Priv, Is_Ada_2005_Only           (Full));
1957
         Set_Has_Pragma_Unreferenced (Priv, Has_Pragma_Unreferenced    (Full));
1958
         Set_Has_Pragma_Unreferenced_Objects
1959
                                     (Priv, Has_Pragma_Unreferenced_Objects
1960
                                                                       (Full));
1961
         if Is_Unchecked_Union (Full) then
1962
            Set_Is_Unchecked_Union (Base_Type (Priv));
1963
         end if;
1964
         --  Why is atomic not copied here ???
1965
 
1966
         if Referenced (Full) then
1967
            Set_Referenced (Priv);
1968
         end if;
1969
 
1970
         if Priv_Is_Base_Type then
1971
            Set_Is_Controlled (Priv, Is_Controlled (Base_Type (Full)));
1972
            Set_Finalize_Storage_Only (Priv, Finalize_Storage_Only
1973
                                                           (Base_Type (Full)));
1974
            Set_Has_Task (Priv, Has_Task (Base_Type (Full)));
1975
            Set_Has_Controlled_Component (Priv, Has_Controlled_Component
1976
                                                           (Base_Type (Full)));
1977
         end if;
1978
 
1979
         Set_Freeze_Node (Priv, Freeze_Node (Full));
1980
 
1981
         if Is_Tagged_Type (Priv)
1982
           and then Is_Tagged_Type (Full)
1983
           and then not Error_Posted (Full)
1984
         then
1985
            if Priv_Is_Base_Type then
1986
 
1987
               --  Ada 2005 (AI-345): The full view of a type implementing an
1988
               --  interface can be a task type.
1989
 
1990
               --    type T is new I with private;
1991
               --  private
1992
               --    task type T is new I with ...
1993
 
1994
               if Is_Interface (Etype (Priv))
1995
                 and then Is_Concurrent_Type (Base_Type (Full))
1996
               then
1997
                  --  Protect the frontend against previous errors
1998
 
1999
                  if Present (Corresponding_Record_Type
2000
                               (Base_Type (Full)))
2001
                  then
2002
                     Set_Access_Disp_Table
2003
                       (Priv, Access_Disp_Table
2004
                               (Corresponding_Record_Type (Base_Type (Full))));
2005
 
2006
                  --  Generic context, or previous errors
2007
 
2008
                  else
2009
                     null;
2010
                  end if;
2011
 
2012
               else
2013
                  Set_Access_Disp_Table
2014
                    (Priv, Access_Disp_Table (Base_Type (Full)));
2015
               end if;
2016
            end if;
2017
 
2018
            if Is_Tagged_Type (Priv) then
2019
 
2020
               --  If the type is tagged, the tag itself must be available on
2021
               --  the partial view, for expansion purposes.
2022
 
2023
               Set_First_Entity (Priv, First_Entity (Full));
2024
 
2025
               --  If there are discriminants in the partial view, these remain
2026
               --  visible. Otherwise only the tag itself is visible, and there
2027
               --  are no nameable components in the partial view.
2028
 
2029
               if No (Last_Entity (Priv)) then
2030
                  Set_Last_Entity (Priv, First_Entity (Priv));
2031
               end if;
2032
            end if;
2033
 
2034
            Set_Has_Discriminants (Priv, Has_Discriminants (Full));
2035
         end if;
2036
      end Preserve_Full_Attributes;
2037
 
2038
      -----------------
2039
      -- Type_In_Use --
2040
      -----------------
2041
 
2042
      function Type_In_Use (T : Entity_Id) return Boolean is
2043
      begin
2044
         return Scope (Base_Type (T)) = P
2045
           and then (In_Use (T) or else In_Use (Base_Type (T)));
2046
      end Type_In_Use;
2047
 
2048
   --  Start of processing for Uninstall_Declarations
2049
 
2050
   begin
2051
      Id := First_Entity (P);
2052
      while Present (Id) and then Id /= First_Private_Entity (P) loop
2053
         if Debug_Flag_E then
2054
            Write_Str ("unlinking visible entity ");
2055
            Write_Int (Int (Id));
2056
            Write_Eol;
2057
         end if;
2058
 
2059
         --  On  exit from the package scope, we must preserve the visibility
2060
         --  established by use clauses in the current scope. Two cases:
2061
 
2062
         --  a) If the entity is an operator, it may be a primitive operator of
2063
         --  a type for which there is a visible use-type clause.
2064
 
2065
         --  b) for other entities, their use-visibility is determined by a
2066
         --  visible use clause for the package itself. For a generic instance,
2067
         --  the instantiation of the formals appears in the visible part,
2068
         --  but the formals are private and remain so.
2069
 
2070
         if Ekind (Id) = E_Function
2071
           and then  Is_Operator_Symbol_Name (Chars (Id))
2072
           and then not Is_Hidden (Id)
2073
           and then not Error_Posted (Id)
2074
         then
2075
            Set_Is_Potentially_Use_Visible (Id,
2076
              In_Use (P)
2077
              or else Type_In_Use (Etype (Id))
2078
              or else Type_In_Use (Etype (First_Formal (Id)))
2079
              or else (Present (Next_Formal (First_Formal (Id)))
2080
                         and then
2081
                           Type_In_Use
2082
                             (Etype (Next_Formal (First_Formal (Id))))));
2083
         else
2084
            if In_Use (P) and then not Is_Hidden (Id) then
2085
 
2086
               --  A child unit of a use-visible package remains use-visible
2087
               --  only if it is itself a visible child unit. Otherwise it
2088
               --  would remain visible in other contexts where P is use-
2089
               --  visible, because once compiled it stays in the entity list
2090
               --  of its parent unit.
2091
 
2092
               if Is_Child_Unit (Id) then
2093
                  Set_Is_Potentially_Use_Visible (Id,
2094
                    Is_Visible_Child_Unit (Id));
2095
               else
2096
                  Set_Is_Potentially_Use_Visible (Id);
2097
               end if;
2098
 
2099
            else
2100
               Set_Is_Potentially_Use_Visible (Id, False);
2101
            end if;
2102
         end if;
2103
 
2104
         --  Local entities are not immediately visible outside of the package
2105
 
2106
         Set_Is_Immediately_Visible (Id, False);
2107
 
2108
         --  If this is a private type with a full view (for example a local
2109
         --  subtype of a private type declared elsewhere), ensure that the
2110
         --  full view is also removed from visibility: it may be exposed when
2111
         --  swapping views in an instantiation.
2112
 
2113
         if Is_Type (Id)
2114
           and then Present (Full_View (Id))
2115
         then
2116
            Set_Is_Immediately_Visible (Full_View (Id), False);
2117
         end if;
2118
 
2119
         if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2120
            Check_Abstract_Overriding (Id);
2121
            Check_Conventions (Id);
2122
         end if;
2123
 
2124
         if (Ekind (Id) = E_Private_Type
2125
               or else Ekind (Id) = E_Limited_Private_Type)
2126
           and then No (Full_View (Id))
2127
           and then not Is_Generic_Type (Id)
2128
           and then not Is_Derived_Type (Id)
2129
         then
2130
            Error_Msg_N ("missing full declaration for private type&", Id);
2131
 
2132
         elsif Ekind (Id) = E_Record_Type_With_Private
2133
           and then not Is_Generic_Type (Id)
2134
           and then No (Full_View (Id))
2135
         then
2136
            if Nkind (Parent (Id)) = N_Private_Type_Declaration then
2137
               Error_Msg_N ("missing full declaration for private type&", Id);
2138
            else
2139
               Error_Msg_N
2140
                 ("missing full declaration for private extension", Id);
2141
            end if;
2142
 
2143
         --  Case of constant, check for deferred constant declaration with
2144
         --  no full view. Likely just a matter of a missing expression, or
2145
         --  accidental use of the keyword constant.
2146
 
2147
         elsif Ekind (Id) = E_Constant
2148
 
2149
           --  OK if constant value present
2150
 
2151
           and then No (Constant_Value (Id))
2152
 
2153
           --  OK if full view present
2154
 
2155
           and then No (Full_View (Id))
2156
 
2157
           --  OK if imported, since that provides the completion
2158
 
2159
           and then not Is_Imported (Id)
2160
 
2161
           --  OK if object declaration replaced by renaming declaration as
2162
           --  a result of OK_To_Rename processing (e.g. for concatenation)
2163
 
2164
           and then Nkind (Parent (Id)) /= N_Object_Renaming_Declaration
2165
 
2166
           --  OK if object declaration with the No_Initialization flag set
2167
 
2168
           and then not (Nkind (Parent (Id)) = N_Object_Declaration
2169
                           and then No_Initialization (Parent (Id)))
2170
         then
2171
            --  If no private declaration is present, we assume the user did
2172
            --  not intend a deferred constant declaration and the problem
2173
            --  is simply that the initializing expression is missing.
2174
 
2175
            if not Has_Private_Declaration (Etype (Id)) then
2176
 
2177
               --  We assume that the user did not intend a deferred constant
2178
               --  declaration, and the expression is just missing.
2179
 
2180
               Error_Msg_N
2181
                 ("constant declaration requires initialization expression",
2182
                   Parent (Id));
2183
 
2184
               if Is_Limited_Type (Etype (Id)) then
2185
                  Error_Msg_N
2186
                    ("\if variable intended, remove CONSTANT from declaration",
2187
                    Parent (Id));
2188
               end if;
2189
 
2190
            --  Otherwise if a private declaration is present, then we are
2191
            --  missing the full declaration for the deferred constant.
2192
 
2193
            else
2194
               Error_Msg_N
2195
                  ("missing full declaration for deferred constant (RM 7.4)",
2196
                     Id);
2197
 
2198
               if Is_Limited_Type (Etype (Id)) then
2199
                  Error_Msg_N
2200
                    ("\if variable intended, remove CONSTANT from declaration",
2201
                    Parent (Id));
2202
               end if;
2203
            end if;
2204
         end if;
2205
 
2206
         Next_Entity (Id);
2207
      end loop;
2208
 
2209
      --  If the specification was installed as the parent of a public child
2210
      --  unit, the private declarations were not installed, and there is
2211
      --  nothing to do.
2212
 
2213
      if not In_Private_Part (P) then
2214
         return;
2215
      else
2216
         Set_In_Private_Part (P, False);
2217
      end if;
2218
 
2219
      --  Make private entities invisible and exchange full and private
2220
      --  declarations for private types. Id is now the first private entity
2221
      --  in the package.
2222
 
2223
      while Present (Id) loop
2224
         if Debug_Flag_E then
2225
            Write_Str ("unlinking private entity ");
2226
            Write_Int (Int (Id));
2227
            Write_Eol;
2228
         end if;
2229
 
2230
         if Is_Tagged_Type (Id) and then Ekind (Id) = E_Record_Type then
2231
            Check_Abstract_Overriding (Id);
2232
            Check_Conventions (Id);
2233
         end if;
2234
 
2235
         Set_Is_Immediately_Visible (Id, False);
2236
 
2237
         if Is_Private_Base_Type (Id)
2238
           and then Present (Full_View (Id))
2239
         then
2240
            Full := Full_View (Id);
2241
 
2242
            --  If the partial view is not declared in the visible part of the
2243
            --  package (as is the case when it is a type derived from some
2244
            --  other private type in the private part of the current package),
2245
            --  no exchange takes place.
2246
 
2247
            if No (Parent (Id))
2248
              or else List_Containing (Parent (Id))
2249
                /= Visible_Declarations (Specification (Decl))
2250
            then
2251
               goto Next_Id;
2252
            end if;
2253
 
2254
            --  The entry in the private part points to the full declaration,
2255
            --  which is currently visible. Exchange them so only the private
2256
            --  type declaration remains accessible, and link private and full
2257
            --  declaration in the opposite direction. Before the actual
2258
            --  exchange, we copy back attributes of the full view that must
2259
            --  be available to the partial view too.
2260
 
2261
            Preserve_Full_Attributes (Id, Full);
2262
 
2263
            Set_Is_Potentially_Use_Visible (Id, In_Use (P));
2264
 
2265
            if  Is_Indefinite_Subtype (Full)
2266
              and then not Is_Indefinite_Subtype (Id)
2267
            then
2268
               Error_Msg_N
2269
                 ("full view of type must be definite subtype", Full);
2270
            end if;
2271
 
2272
            Priv_Elmt := First_Elmt (Private_Dependents (Id));
2273
 
2274
            --  Swap out the subtypes and derived types of Id that were
2275
            --  compiled in this scope, or installed previously by
2276
            --  Install_Private_Declarations.
2277
 
2278
            --  Before we do the swap, we verify the presence of the Full_View
2279
            --  field which may be empty due to a swap by a previous call to
2280
            --  End_Package_Scope (e.g. from the freezing mechanism).
2281
 
2282
            while Present (Priv_Elmt) loop
2283
               Priv_Sub := Node (Priv_Elmt);
2284
 
2285
               if Present (Full_View (Priv_Sub)) then
2286
 
2287
                  if Scope (Priv_Sub) = P
2288
                     or else not In_Open_Scopes (Scope (Priv_Sub))
2289
                  then
2290
                     Set_Is_Immediately_Visible (Priv_Sub, False);
2291
                  end if;
2292
 
2293
                  if Is_Visible_Dependent (Priv_Sub) then
2294
                     Preserve_Full_Attributes
2295
                       (Priv_Sub, Full_View (Priv_Sub));
2296
                     Replace_Elmt (Priv_Elmt, Full_View (Priv_Sub));
2297
                     Exchange_Declarations (Priv_Sub);
2298
                  end if;
2299
               end if;
2300
 
2301
               Next_Elmt (Priv_Elmt);
2302
            end loop;
2303
 
2304
            --  Now restore the type itself to its private view
2305
 
2306
            Exchange_Declarations (Id);
2307
 
2308
            --  If we have installed an underlying full view for a type derived
2309
            --  from a private type in a child unit, restore the proper views
2310
            --  of private and full view. See corresponding code in
2311
            --  Install_Private_Declarations.
2312
 
2313
            --  After the exchange, Full denotes the private type in the
2314
            --  visible part of the package.
2315
 
2316
            if Is_Private_Base_Type (Full)
2317
              and then Present (Full_View (Full))
2318
              and then Present (Underlying_Full_View (Full))
2319
              and then In_Package_Body (Current_Scope)
2320
            then
2321
               Set_Full_View (Full, Underlying_Full_View (Full));
2322
               Set_Underlying_Full_View (Full, Empty);
2323
            end if;
2324
 
2325
         elsif Ekind (Id) = E_Incomplete_Type
2326
           and then Comes_From_Source (Id)
2327
           and then No (Full_View (Id))
2328
         then
2329
            --  Mark Taft amendment types. Verify that there are no primitive
2330
            --  operations declared for the type (3.10.1(9)).
2331
 
2332
            Set_Has_Completion_In_Body (Id);
2333
 
2334
            declare
2335
               Elmt : Elmt_Id;
2336
               Subp : Entity_Id;
2337
 
2338
            begin
2339
               Elmt := First_Elmt (Private_Dependents (Id));
2340
               while Present (Elmt) loop
2341
                  Subp := Node (Elmt);
2342
 
2343
                  if Is_Overloadable (Subp) then
2344
                     Error_Msg_NE
2345
                       ("type& must be completed in the private part",
2346
                         Parent (Subp), Id);
2347
 
2348
                  --  The return type of an access_to_function cannot be a
2349
                  --  Taft-amendment type.
2350
 
2351
                  elsif Ekind (Subp) = E_Subprogram_Type then
2352
                     if Etype (Subp) = Id
2353
                       or else
2354
                         (Is_Class_Wide_Type (Etype (Subp))
2355
                            and then Etype (Etype (Subp)) = Id)
2356
                     then
2357
                        Error_Msg_NE
2358
                          ("type& must be completed in the private part",
2359
                             Associated_Node_For_Itype (Subp), Id);
2360
                     end if;
2361
                  end if;
2362
 
2363
                  Next_Elmt (Elmt);
2364
               end loop;
2365
            end;
2366
 
2367
         elsif not Is_Child_Unit (Id)
2368
           and then (not Is_Private_Type (Id)
2369
                      or else No (Full_View (Id)))
2370
         then
2371
            Set_Is_Hidden (Id);
2372
            Set_Is_Potentially_Use_Visible (Id, False);
2373
         end if;
2374
 
2375
         <<Next_Id>>
2376
            Next_Entity (Id);
2377
      end loop;
2378
   end Uninstall_Declarations;
2379
 
2380
   ------------------------
2381
   -- Unit_Requires_Body --
2382
   ------------------------
2383
 
2384
   function Unit_Requires_Body (P : Entity_Id) return Boolean is
2385
      E : Entity_Id;
2386
 
2387
   begin
2388
      --  Imported entity never requires body. Right now, only subprograms can
2389
      --  be imported, but perhaps in the future we will allow import of
2390
      --  packages.
2391
 
2392
      if Is_Imported (P) then
2393
         return False;
2394
 
2395
      --  Body required if library package with pragma Elaborate_Body
2396
 
2397
      elsif Has_Pragma_Elaborate_Body (P) then
2398
         return True;
2399
 
2400
      --  Body required if subprogram
2401
 
2402
      elsif Is_Subprogram (P) or else Is_Generic_Subprogram (P) then
2403
         return True;
2404
 
2405
      --  Treat a block as requiring a body
2406
 
2407
      elsif Ekind (P) = E_Block then
2408
         return True;
2409
 
2410
      elsif Ekind (P) = E_Package
2411
        and then Nkind (Parent (P)) = N_Package_Specification
2412
        and then Present (Generic_Parent (Parent (P)))
2413
      then
2414
         declare
2415
            G_P : constant Entity_Id := Generic_Parent (Parent (P));
2416
         begin
2417
            if Has_Pragma_Elaborate_Body (G_P) then
2418
               return True;
2419
            end if;
2420
         end;
2421
      end if;
2422
 
2423
      --  Otherwise search entity chain for entity requiring completion
2424
 
2425
      E := First_Entity (P);
2426
      while Present (E) loop
2427
 
2428
         --  Always ignore child units. Child units get added to the entity
2429
         --  list of a parent unit, but are not original entities of the
2430
         --  parent, and so do not affect whether the parent needs a body.
2431
 
2432
         if Is_Child_Unit (E) then
2433
            null;
2434
 
2435
         --  Ignore formal packages and their renamings
2436
 
2437
         elsif Ekind (E) = E_Package
2438
           and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
2439
                                                N_Formal_Package_Declaration
2440
         then
2441
            null;
2442
 
2443
         --  Otherwise test to see if entity requires a completion.
2444
         --  Note that subprogram entities whose declaration does not come
2445
         --  from source are ignored here on the basis that we assume the
2446
         --  expander will provide an implicit completion at some point.
2447
 
2448
         elsif (Is_Overloadable (E)
2449
               and then Ekind (E) /= E_Enumeration_Literal
2450
               and then Ekind (E) /= E_Operator
2451
               and then not Is_Abstract_Subprogram (E)
2452
               and then not Has_Completion (E)
2453
               and then Comes_From_Source (Parent (E)))
2454
 
2455
           or else
2456
             (Ekind (E) = E_Package
2457
               and then E /= P
2458
               and then not Has_Completion (E)
2459
               and then Unit_Requires_Body (E))
2460
 
2461
           or else
2462
             (Ekind (E) = E_Incomplete_Type and then No (Full_View (E)))
2463
 
2464
           or else
2465
            ((Ekind (E) = E_Task_Type or else
2466
              Ekind (E) = E_Protected_Type)
2467
               and then not Has_Completion (E))
2468
 
2469
           or else
2470
             (Ekind (E) = E_Generic_Package and then E /= P
2471
               and then not Has_Completion (E)
2472
               and then Unit_Requires_Body (E))
2473
 
2474
           or else
2475
             (Is_Generic_Subprogram (E)
2476
               and then not Has_Completion (E))
2477
 
2478
         then
2479
            return True;
2480
 
2481
         --  Entity that does not require completion
2482
 
2483
         else
2484
            null;
2485
         end if;
2486
 
2487
         Next_Entity (E);
2488
      end loop;
2489
 
2490
      return False;
2491
   end Unit_Requires_Body;
2492
 
2493
end Sem_Ch7;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.