OSDN Git Service

2008-08-04 Javier Miranda <miranda@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_ch3.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              S E M _ C H 3                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2008, 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 with Atree;    use Atree;
27 with Checks;   use Checks;
28 with Debug;    use Debug;
29 with Elists;   use Elists;
30 with Einfo;    use Einfo;
31 with Errout;   use Errout;
32 with Eval_Fat; use Eval_Fat;
33 with Exp_Ch3;  use Exp_Ch3;
34 with Exp_Ch9;  use Exp_Ch9;
35 with Exp_Disp; use Exp_Disp;
36 with Exp_Dist; use Exp_Dist;
37 with Exp_Tss;  use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Fname;    use Fname;
40 with Freeze;   use Freeze;
41 with Itypes;   use Itypes;
42 with Layout;   use Layout;
43 with Lib;      use Lib;
44 with Lib.Xref; use Lib.Xref;
45 with Namet;    use Namet;
46 with Nmake;    use Nmake;
47 with Opt;      use Opt;
48 with Restrict; use Restrict;
49 with Rident;   use Rident;
50 with Rtsfind;  use Rtsfind;
51 with Sem;      use Sem;
52 with Sem_Case; use Sem_Case;
53 with Sem_Cat;  use Sem_Cat;
54 with Sem_Ch6;  use Sem_Ch6;
55 with Sem_Ch7;  use Sem_Ch7;
56 with Sem_Ch8;  use Sem_Ch8;
57 with Sem_Ch13; use Sem_Ch13;
58 with Sem_Disp; use Sem_Disp;
59 with Sem_Dist; use Sem_Dist;
60 with Sem_Elim; use Sem_Elim;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Mech; use Sem_Mech;
63 with Sem_Res;  use Sem_Res;
64 with Sem_Smem; use Sem_Smem;
65 with Sem_Type; use Sem_Type;
66 with Sem_Util; use Sem_Util;
67 with Sem_Warn; use Sem_Warn;
68 with Stand;    use Stand;
69 with Sinfo;    use Sinfo;
70 with Snames;   use Snames;
71 with Targparm; use Targparm;
72 with Tbuild;   use Tbuild;
73 with Ttypes;   use Ttypes;
74 with Uintp;    use Uintp;
75 with Urealp;   use Urealp;
76
77 package body Sem_Ch3 is
78
79    -----------------------
80    -- Local Subprograms --
81    -----------------------
82
83    procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
84    --  Ada 2005 (AI-251): Add the tag components corresponding to all the
85    --  abstract interface types implemented by a record type or a derived
86    --  record type.
87
88    procedure Build_Derived_Type
89      (N             : Node_Id;
90       Parent_Type   : Entity_Id;
91       Derived_Type  : Entity_Id;
92       Is_Completion : Boolean;
93       Derive_Subps  : Boolean := True);
94    --  Create and decorate a Derived_Type given the Parent_Type entity. N is
95    --  the N_Full_Type_Declaration node containing the derived type definition.
96    --  Parent_Type is the entity for the parent type in the derived type
97    --  definition and Derived_Type the actual derived type. Is_Completion must
98    --  be set to False if Derived_Type is the N_Defining_Identifier node in N
99    --  (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
100    --  completion of a private type declaration. If Is_Completion is set to
101    --  True, N is the completion of a private type declaration and Derived_Type
102    --  is different from the defining identifier inside N (i.e. Derived_Type /=
103    --  Defining_Identifier (N)). Derive_Subps indicates whether the parent
104    --  subprograms should be derived. The only case where this parameter is
105    --  False is when Build_Derived_Type is recursively called to process an
106    --  implicit derived full type for a type derived from a private type (in
107    --  that case the subprograms must only be derived for the private view of
108    --  the type).
109    --
110    --  ??? These flags need a bit of re-examination and re-documentation:
111    --  ???  are they both necessary (both seem related to the recursion)?
112
113    procedure Build_Derived_Access_Type
114      (N            : Node_Id;
115       Parent_Type  : Entity_Id;
116       Derived_Type : Entity_Id);
117    --  Subsidiary procedure to Build_Derived_Type. For a derived access type,
118    --  create an implicit base if the parent type is constrained or if the
119    --  subtype indication has a constraint.
120
121    procedure Build_Derived_Array_Type
122      (N            : Node_Id;
123       Parent_Type  : Entity_Id;
124       Derived_Type : Entity_Id);
125    --  Subsidiary procedure to Build_Derived_Type. For a derived array type,
126    --  create an implicit base if the parent type is constrained or if the
127    --  subtype indication has a constraint.
128
129    procedure Build_Derived_Concurrent_Type
130      (N            : Node_Id;
131       Parent_Type  : Entity_Id;
132       Derived_Type : Entity_Id);
133    --  Subsidiary procedure to Build_Derived_Type. For a derived task or
134    --  protected type, inherit entries and protected subprograms, check
135    --  legality of discriminant constraints if any.
136
137    procedure Build_Derived_Enumeration_Type
138      (N            : Node_Id;
139       Parent_Type  : Entity_Id;
140       Derived_Type : Entity_Id);
141    --  Subsidiary procedure to Build_Derived_Type. For a derived enumeration
142    --  type, we must create a new list of literals. Types derived from
143    --  Character and Wide_Character are special-cased.
144
145    procedure Build_Derived_Numeric_Type
146      (N            : Node_Id;
147       Parent_Type  : Entity_Id;
148       Derived_Type : Entity_Id);
149    --  Subsidiary procedure to Build_Derived_Type. For numeric types, create
150    --  an anonymous base type, and propagate constraint to subtype if needed.
151
152    procedure Build_Derived_Private_Type
153      (N             : Node_Id;
154       Parent_Type   : Entity_Id;
155       Derived_Type  : Entity_Id;
156       Is_Completion : Boolean;
157       Derive_Subps  : Boolean := True);
158    --  Subsidiary procedure to Build_Derived_Type. This procedure is complex
159    --  because the parent may or may not have a completion, and the derivation
160    --  may itself be a completion.
161
162    procedure Build_Derived_Record_Type
163      (N            : Node_Id;
164       Parent_Type  : Entity_Id;
165       Derived_Type : Entity_Id;
166       Derive_Subps : Boolean := True);
167    --  Subsidiary procedure for Build_Derived_Type and
168    --  Analyze_Private_Extension_Declaration used for tagged and untagged
169    --  record types. All parameters are as in Build_Derived_Type except that
170    --  N, in addition to being an N_Full_Type_Declaration node, can also be an
171    --  N_Private_Extension_Declaration node. See the definition of this routine
172    --  for much more info. Derive_Subps indicates whether subprograms should
173    --  be derived from the parent type. The only case where Derive_Subps is
174    --  False is for an implicit derived full type for a type derived from a
175    --  private type (see Build_Derived_Type).
176
177    procedure Build_Discriminal (Discrim : Entity_Id);
178    --  Create the discriminal corresponding to discriminant Discrim, that is
179    --  the parameter corresponding to Discrim to be used in initialization
180    --  procedures for the type where Discrim is a discriminant. Discriminals
181    --  are not used during semantic analysis, and are not fully defined
182    --  entities until expansion. Thus they are not given a scope until
183    --  initialization procedures are built.
184
185    function Build_Discriminant_Constraints
186      (T           : Entity_Id;
187       Def         : Node_Id;
188       Derived_Def : Boolean := False) return Elist_Id;
189    --  Validate discriminant constraints and return the list of the constraints
190    --  in order of discriminant declarations, where T is the discriminated
191    --  unconstrained type. Def is the N_Subtype_Indication node where the
192    --  discriminants constraints for T are specified. Derived_Def is True
193    --  when building the discriminant constraints in a derived type definition
194    --  of the form "type D (...) is new T (xxx)". In this case T is the parent
195    --  type and Def is the constraint "(xxx)" on T and this routine sets the
196    --  Corresponding_Discriminant field of the discriminants in the derived
197    --  type D to point to the corresponding discriminants in the parent type T.
198
199    procedure Build_Discriminated_Subtype
200      (T           : Entity_Id;
201       Def_Id      : Entity_Id;
202       Elist       : Elist_Id;
203       Related_Nod : Node_Id;
204       For_Access  : Boolean := False);
205    --  Subsidiary procedure to Constrain_Discriminated_Type and to
206    --  Process_Incomplete_Dependents. Given
207    --
208    --     T (a possibly discriminated base type)
209    --     Def_Id (a very partially built subtype for T),
210    --
211    --  the call completes Def_Id to be the appropriate E_*_Subtype.
212    --
213    --  The Elist is the list of discriminant constraints if any (it is set
214    --  to No_Elist if T is not a discriminated type, and to an empty list if
215    --  T has discriminants but there are no discriminant constraints). The
216    --  Related_Nod is the same as Decl_Node in Create_Constrained_Components.
217    --  The For_Access says whether or not this subtype is really constraining
218    --  an access type. That is its sole purpose is the designated type of an
219    --  access type -- in which case a Private_Subtype Is_For_Access_Subtype
220    --  is built to avoid freezing T when the access subtype is frozen.
221
222    function Build_Scalar_Bound
223      (Bound : Node_Id;
224       Par_T : Entity_Id;
225       Der_T : Entity_Id) return Node_Id;
226    --  The bounds of a derived scalar type are conversions of the bounds of
227    --  the parent type. Optimize the representation if the bounds are literals.
228    --  Needs a more complete spec--what are the parameters exactly, and what
229    --  exactly is the returned value, and how is Bound affected???
230
231    procedure Build_Itype_Reference
232      (Ityp : Entity_Id;
233       Nod  : Node_Id);
234    --  Create a reference to an internal type, for use by Gigi. The back-end
235    --  elaborates itypes on demand, i.e. when their first use is seen. This
236    --  can lead to scope anomalies if the first use is within a scope that is
237    --  nested within the scope that contains  the point of definition of the
238    --  itype. The Itype_Reference node forces the elaboration of the itype
239    --  in the proper scope. The node is inserted after Nod, which is the
240    --  enclosing declaration that generated Ityp.
241    --
242    --  A related mechanism is used during expansion, for itypes created in
243    --  branches of conditionals. See Ensure_Defined in exp_util.
244    --  Could both mechanisms be merged ???
245
246    procedure Build_Underlying_Full_View
247      (N   : Node_Id;
248       Typ : Entity_Id;
249       Par : Entity_Id);
250    --  If the completion of a private type is itself derived from a private
251    --  type, or if the full view of a private subtype is itself private, the
252    --  back-end has no way to compute the actual size of this type. We build
253    --  an internal subtype declaration of the proper parent type to convey
254    --  this information. This extra mechanism is needed because a full
255    --  view cannot itself have a full view (it would get clobbered during
256    --  view exchanges).
257
258    procedure Check_Access_Discriminant_Requires_Limited
259      (D   : Node_Id;
260       Loc : Node_Id);
261    --  Check the restriction that the type to which an access discriminant
262    --  belongs must be a concurrent type or a descendant of a type with
263    --  the reserved word 'limited' in its declaration.
264
265    procedure Check_Anonymous_Access_Components
266       (Typ_Decl  : Node_Id;
267        Typ       : Entity_Id;
268        Prev      : Entity_Id;
269        Comp_List : Node_Id);
270    --  Ada 2005 AI-382: an access component in a record definition can refer to
271    --  the enclosing record, in which case it denotes the type itself, and not
272    --  the current instance of the type. We create an anonymous access type for
273    --  the component, and flag it as an access to a component, so accessibility
274    --  checks are properly performed on it. The declaration of the access type
275    --  is placed ahead of that of the record to prevent order-of-elaboration
276    --  circularity issues in Gigi. We create an incomplete type for the record
277    --  declaration, which is the designated type of the anonymous access.
278
279    procedure Check_Delta_Expression (E : Node_Id);
280    --  Check that the expression represented by E is suitable for use as a
281    --  delta expression, i.e. it is of real type and is static.
282
283    procedure Check_Digits_Expression (E : Node_Id);
284    --  Check that the expression represented by E is suitable for use as a
285    --  digits expression, i.e. it is of integer type, positive and static.
286
287    procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
288    --  Validate the initialization of an object declaration. T is the required
289    --  type, and Exp is the initialization expression.
290
291    procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
292    --  Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
293
294    procedure Check_Or_Process_Discriminants
295      (N    : Node_Id;
296       T    : Entity_Id;
297       Prev : Entity_Id := Empty);
298    --  If T is the full declaration of an incomplete or private type, check the
299    --  conformance of the discriminants, otherwise process them. Prev is the
300    --  entity of the partial declaration, if any.
301
302    procedure Check_Real_Bound (Bound : Node_Id);
303    --  Check given bound for being of real type and static. If not, post an
304    --  appropriate message, and rewrite the bound with the real literal zero.
305
306    procedure Constant_Redeclaration
307      (Id : Entity_Id;
308       N  : Node_Id;
309       T  : out Entity_Id);
310    --  Various checks on legality of full declaration of deferred constant.
311    --  Id is the entity for the redeclaration, N is the N_Object_Declaration,
312    --  node. The caller has not yet set any attributes of this entity.
313
314    function Contain_Interface
315      (Iface  : Entity_Id;
316       Ifaces : Elist_Id) return Boolean;
317    --  Ada 2005: Determine whether Iface is present in the list Ifaces
318
319    procedure Convert_Scalar_Bounds
320      (N            : Node_Id;
321       Parent_Type  : Entity_Id;
322       Derived_Type : Entity_Id;
323       Loc          : Source_Ptr);
324    --  For derived scalar types, convert the bounds in the type definition to
325    --  the derived type, and complete their analysis. Given a constraint of the
326    --  form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
327    --  T'Base, the parent_type. The bounds of the derived type (the anonymous
328    --  base) are copies of Lo and Hi. Finally, the bounds of the derived
329    --  subtype are conversions of those bounds to the derived_type, so that
330    --  their typing is consistent.
331
332    procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
333    --  Copies attributes from array base type T2 to array base type T1. Copies
334    --  only attributes that apply to base types, but not subtypes.
335
336    procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
337    --  Copies attributes from array subtype T2 to array subtype T1. Copies
338    --  attributes that apply to both subtypes and base types.
339
340    procedure Create_Constrained_Components
341      (Subt        : Entity_Id;
342       Decl_Node   : Node_Id;
343       Typ         : Entity_Id;
344       Constraints : Elist_Id);
345    --  Build the list of entities for a constrained discriminated record
346    --  subtype. If a component depends on a discriminant, replace its subtype
347    --  using the discriminant values in the discriminant constraint. Subt
348    --  is the defining identifier for the subtype whose list of constrained
349    --  entities we will create. Decl_Node is the type declaration node where
350    --  we will attach all the itypes created. Typ is the base discriminated
351    --  type for the subtype Subt. Constraints is the list of discriminant
352    --  constraints for Typ.
353
354    function Constrain_Component_Type
355      (Comp            : Entity_Id;
356       Constrained_Typ : Entity_Id;
357       Related_Node    : Node_Id;
358       Typ             : Entity_Id;
359       Constraints     : Elist_Id) return Entity_Id;
360    --  Given a discriminated base type Typ, a list of discriminant constraint
361    --  Constraints for Typ and a component of Typ, with type Compon_Type,
362    --  create and return the type corresponding to Compon_type where all
363    --  discriminant references are replaced with the corresponding constraint.
364    --  If no discriminant references occur in Compon_Typ then return it as is.
365    --  Constrained_Typ is the final constrained subtype to which the
366    --  constrained Compon_Type belongs. Related_Node is the node where we will
367    --  attach all the itypes created.
368    --
369    --  Above description is confused, what is Compon_Type???
370
371    procedure Constrain_Access
372      (Def_Id      : in out Entity_Id;
373       S           : Node_Id;
374       Related_Nod : Node_Id);
375    --  Apply a list of constraints to an access type. If Def_Id is empty, it is
376    --  an anonymous type created for a subtype indication. In that case it is
377    --  created in the procedure and attached to Related_Nod.
378
379    procedure Constrain_Array
380      (Def_Id      : in out Entity_Id;
381       SI          : Node_Id;
382       Related_Nod : Node_Id;
383       Related_Id  : Entity_Id;
384       Suffix      : Character);
385    --  Apply a list of index constraints to an unconstrained array type. The
386    --  first parameter is the entity for the resulting subtype. A value of
387    --  Empty for Def_Id indicates that an implicit type must be created, but
388    --  creation is delayed (and must be done by this procedure) because other
389    --  subsidiary implicit types must be created first (which is why Def_Id
390    --  is an in/out parameter). The second parameter is a subtype indication
391    --  node for the constrained array to be created (e.g. something of the
392    --  form string (1 .. 10)). Related_Nod gives the place where this type
393    --  has to be inserted in the tree. The Related_Id and Suffix parameters
394    --  are used to build the associated Implicit type name.
395
396    procedure Constrain_Concurrent
397      (Def_Id      : in out Entity_Id;
398       SI          : Node_Id;
399       Related_Nod : Node_Id;
400       Related_Id  : Entity_Id;
401       Suffix      : Character);
402    --  Apply list of discriminant constraints to an unconstrained concurrent
403    --  type.
404    --
405    --    SI is the N_Subtype_Indication node containing the constraint and
406    --    the unconstrained type to constrain.
407    --
408    --    Def_Id is the entity for the resulting constrained subtype. A value
409    --    of Empty for Def_Id indicates that an implicit type must be created,
410    --    but creation is delayed (and must be done by this procedure) because
411    --    other subsidiary implicit types must be created first (which is why
412    --    Def_Id is an in/out parameter).
413    --
414    --    Related_Nod gives the place where this type has to be inserted
415    --    in the tree
416    --
417    --  The last two arguments are used to create its external name if needed.
418
419    function Constrain_Corresponding_Record
420      (Prot_Subt   : Entity_Id;
421       Corr_Rec    : Entity_Id;
422       Related_Nod : Node_Id;
423       Related_Id  : Entity_Id) return Entity_Id;
424    --  When constraining a protected type or task type with discriminants,
425    --  constrain the corresponding record with the same discriminant values.
426
427    procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
428    --  Constrain a decimal fixed point type with a digits constraint and/or a
429    --  range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
430
431    procedure Constrain_Discriminated_Type
432      (Def_Id      : Entity_Id;
433       S           : Node_Id;
434       Related_Nod : Node_Id;
435       For_Access  : Boolean := False);
436    --  Process discriminant constraints of composite type. Verify that values
437    --  have been provided for all discriminants, that the original type is
438    --  unconstrained, and that the types of the supplied expressions match
439    --  the discriminant types. The first three parameters are like in routine
440    --  Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
441    --  of For_Access.
442
443    procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
444    --  Constrain an enumeration type with a range constraint. This is identical
445    --  to Constrain_Integer, but for the Ekind of the resulting subtype.
446
447    procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
448    --  Constrain a floating point type with either a digits constraint
449    --  and/or a range constraint, building a E_Floating_Point_Subtype.
450
451    procedure Constrain_Index
452      (Index        : Node_Id;
453       S            : Node_Id;
454       Related_Nod  : Node_Id;
455       Related_Id   : Entity_Id;
456       Suffix       : Character;
457       Suffix_Index : Nat);
458    --  Process an index constraint in a constrained array declaration. The
459    --  constraint can be a subtype name, or a range with or without an explicit
460    --  subtype mark. The index is the corresponding index of the unconstrained
461    --  array. The Related_Id and Suffix parameters are used to build the
462    --  associated Implicit type name.
463
464    procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
465    --  Build subtype of a signed or modular integer type
466
467    procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
468    --  Constrain an ordinary fixed point type with a range constraint, and
469    --  build an E_Ordinary_Fixed_Point_Subtype entity.
470
471    procedure Copy_And_Swap (Priv, Full : Entity_Id);
472    --  Copy the Priv entity into the entity of its full declaration then swap
473    --  the two entities in such a manner that the former private type is now
474    --  seen as a full type.
475
476    procedure Decimal_Fixed_Point_Type_Declaration
477      (T   : Entity_Id;
478       Def : Node_Id);
479    --  Create a new decimal fixed point type, and apply the constraint to
480    --  obtain a subtype of this new type.
481
482    procedure Complete_Private_Subtype
483      (Priv        : Entity_Id;
484       Full        : Entity_Id;
485       Full_Base   : Entity_Id;
486       Related_Nod : Node_Id);
487    --  Complete the implicit full view of a private subtype by setting the
488    --  appropriate semantic fields. If the full view of the parent is a record
489    --  type, build constrained components of subtype.
490
491    procedure Derive_Progenitor_Subprograms
492      (Parent_Type : Entity_Id;
493       Tagged_Type : Entity_Id);
494    --  Ada 2005 (AI-251): To complete type derivation, collect the primitive
495    --  operations of progenitors of Tagged_Type, and replace the subsidiary
496    --  subtypes with Tagged_Type, to build the specs of the inherited interface
497    --  primitives. The derived primitives are aliased to those of the
498    --  interface. This routine takes care also of transferring to the full-view
499    --  subprograms associated with the partial-view of Tagged_Type that cover
500    --  interface primitives.
501
502    procedure Derived_Standard_Character
503      (N             : Node_Id;
504       Parent_Type   : Entity_Id;
505       Derived_Type  : Entity_Id);
506    --  Subsidiary procedure to Build_Derived_Enumeration_Type which handles
507    --  derivations from types Standard.Character and Standard.Wide_Character.
508
509    procedure Derived_Type_Declaration
510      (T             : Entity_Id;
511       N             : Node_Id;
512       Is_Completion : Boolean);
513    --  Process a derived type declaration. Build_Derived_Type is invoked
514    --  to process the actual derived type definition. Parameters N and
515    --  Is_Completion have the same meaning as in Build_Derived_Type.
516    --  T is the N_Defining_Identifier for the entity defined in the
517    --  N_Full_Type_Declaration node N, that is T is the derived type.
518
519    procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
520    --  Insert each literal in symbol table, as an overloadable identifier. Each
521    --  enumeration type is mapped into a sequence of integers, and each literal
522    --  is defined as a constant with integer value. If any of the literals are
523    --  character literals, the type is a character type, which means that
524    --  strings are legal aggregates for arrays of components of the type.
525
526    function Expand_To_Stored_Constraint
527      (Typ        : Entity_Id;
528       Constraint : Elist_Id) return Elist_Id;
529    --  Given a constraint (i.e. a list of expressions) on the discriminants of
530    --  Typ, expand it into a constraint on the stored discriminants and return
531    --  the new list of expressions constraining the stored discriminants.
532
533    function Find_Type_Of_Object
534      (Obj_Def     : Node_Id;
535       Related_Nod : Node_Id) return Entity_Id;
536    --  Get type entity for object referenced by Obj_Def, attaching the
537    --  implicit types generated to Related_Nod
538
539    procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
540    --  Create a new float and apply the constraint to obtain subtype of it
541
542    function Has_Range_Constraint (N : Node_Id) return Boolean;
543    --  Given an N_Subtype_Indication node N, return True if a range constraint
544    --  is present, either directly, or as part of a digits or delta constraint.
545    --  In addition, a digits constraint in the decimal case returns True, since
546    --  it establishes a default range if no explicit range is present.
547
548    function Inherit_Components
549      (N             : Node_Id;
550       Parent_Base   : Entity_Id;
551       Derived_Base  : Entity_Id;
552       Is_Tagged     : Boolean;
553       Inherit_Discr : Boolean;
554       Discs         : Elist_Id) return Elist_Id;
555    --  Called from Build_Derived_Record_Type to inherit the components of
556    --  Parent_Base (a base type) into the Derived_Base (the derived base type).
557    --  For more information on derived types and component inheritance please
558    --  consult the comment above the body of Build_Derived_Record_Type.
559    --
560    --    N is the original derived type declaration
561    --
562    --    Is_Tagged is set if we are dealing with tagged types
563    --
564    --    If Inherit_Discr is set, Derived_Base inherits its discriminants from
565    --    Parent_Base, otherwise no discriminants are inherited.
566    --
567    --    Discs gives the list of constraints that apply to Parent_Base in the
568    --    derived type declaration. If Discs is set to No_Elist, then we have
569    --    the following situation:
570    --
571    --      type Parent (D1..Dn : ..) is [tagged] record ...;
572    --      type Derived is new Parent [with ...];
573    --
574    --    which gets treated as
575    --
576    --      type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
577    --
578    --  For untagged types the returned value is an association list. The list
579    --  starts from the association (Parent_Base => Derived_Base), and then it
580    --  contains a sequence of the associations of the form
581    --
582    --    (Old_Component => New_Component),
583    --
584    --  where Old_Component is the Entity_Id of a component in Parent_Base and
585    --  New_Component is the Entity_Id of the corresponding component in
586    --  Derived_Base. For untagged records, this association list is needed when
587    --  copying the record declaration for the derived base. In the tagged case
588    --  the value returned is irrelevant.
589
590    function Is_Progenitor
591      (Iface : Entity_Id;
592       Typ   :  Entity_Id) return Boolean;
593    --  Determine whether type Typ implements interface Iface. This requires
594    --  traversing the list of abstract interfaces of the type, as well as that
595    --  of the ancestor types. The predicate is used to determine when a formal
596    --  in the signature of an inherited operation must carry the derived type.
597
598    function Is_Valid_Constraint_Kind
599      (T_Kind          : Type_Kind;
600       Constraint_Kind : Node_Kind) return Boolean;
601    --  Returns True if it is legal to apply the given kind of constraint to the
602    --  given kind of type (index constraint to an array type, for example).
603
604    procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
605    --  Create new modular type. Verify that modulus is in  bounds and is
606    --  a power of two (implementation restriction).
607
608    procedure New_Concatenation_Op (Typ : Entity_Id);
609    --  Create an abbreviated declaration for an operator in order to
610    --  materialize concatenation on array types.
611
612    procedure Ordinary_Fixed_Point_Type_Declaration
613      (T   : Entity_Id;
614       Def : Node_Id);
615    --  Create a new ordinary fixed point type, and apply the constraint to
616    --  obtain subtype of it.
617
618    procedure Prepare_Private_Subtype_Completion
619      (Id          : Entity_Id;
620       Related_Nod : Node_Id);
621    --  Id is a subtype of some private type. Creates the full declaration
622    --  associated with Id whenever possible, i.e. when the full declaration
623    --  of the base type is already known. Records each subtype into
624    --  Private_Dependents of the base type.
625
626    procedure Process_Incomplete_Dependents
627      (N      : Node_Id;
628       Full_T : Entity_Id;
629       Inc_T  : Entity_Id);
630    --  Process all entities that depend on an incomplete type. There include
631    --  subtypes, subprogram types that mention the incomplete type in their
632    --  profiles, and subprogram with access parameters that designate the
633    --  incomplete type.
634
635    --  Inc_T is the defining identifier of an incomplete type declaration, its
636    --  Ekind is E_Incomplete_Type.
637    --
638    --    N is the corresponding N_Full_Type_Declaration for Inc_T.
639    --
640    --    Full_T is N's defining identifier.
641    --
642    --  Subtypes of incomplete types with discriminants are completed when the
643    --  parent type is. This is simpler than private subtypes, because they can
644    --  only appear in the same scope, and there is no need to exchange views.
645    --  Similarly, access_to_subprogram types may have a parameter or a return
646    --  type that is an incomplete type, and that must be replaced with the
647    --  full type.
648    --
649    --  If the full type is tagged, subprogram with access parameters that
650    --  designated the incomplete may be primitive operations of the full type,
651    --  and have to be processed accordingly.
652
653    procedure Process_Real_Range_Specification (Def : Node_Id);
654    --  Given the type definition for a real type, this procedure processes and
655    --  checks the real range specification of this type definition if one is
656    --  present. If errors are found, error messages are posted, and the
657    --  Real_Range_Specification of Def is reset to Empty.
658
659    procedure Record_Type_Declaration
660      (T    : Entity_Id;
661       N    : Node_Id;
662       Prev : Entity_Id);
663    --  Process a record type declaration (for both untagged and tagged
664    --  records). Parameters T and N are exactly like in procedure
665    --  Derived_Type_Declaration, except that no flag Is_Completion is needed
666    --  for this routine. If this is the completion of an incomplete type
667    --  declaration, Prev is the entity of the incomplete declaration, used for
668    --  cross-referencing. Otherwise Prev = T.
669
670    procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
671    --  This routine is used to process the actual record type definition (both
672    --  for untagged and tagged records). Def is a record type definition node.
673    --  This procedure analyzes the components in this record type definition.
674    --  Prev_T is the entity for the enclosing record type. It is provided so
675    --  that its Has_Task flag can be set if any of the component have Has_Task
676    --  set. If the declaration is the completion of an incomplete type
677    --  declaration, Prev_T is the original incomplete type, whose full view is
678    --  the record type.
679
680    procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
681    --  Subsidiary to Build_Derived_Record_Type. For untagged records, we
682    --  build a copy of the declaration tree of the parent, and we create
683    --  independently the list of components for the derived type. Semantic
684    --  information uses the component entities, but record representation
685    --  clauses are validated on the declaration tree. This procedure replaces
686    --  discriminants and components in the declaration with those that have
687    --  been created by Inherit_Components.
688
689    procedure Set_Fixed_Range
690      (E   : Entity_Id;
691       Loc : Source_Ptr;
692       Lo  : Ureal;
693       Hi  : Ureal);
694    --  Build a range node with the given bounds and set it as the Scalar_Range
695    --  of the given fixed-point type entity. Loc is the source location used
696    --  for the constructed range. See body for further details.
697
698    procedure Set_Scalar_Range_For_Subtype
699      (Def_Id : Entity_Id;
700       R      : Node_Id;
701       Subt   : Entity_Id);
702    --  This routine is used to set the scalar range field for a subtype given
703    --  Def_Id, the entity for the subtype, and R, the range expression for the
704    --  scalar range. Subt provides the parent subtype to be used to analyze,
705    --  resolve, and check the given range.
706
707    procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
708    --  Create a new signed integer entity, and apply the constraint to obtain
709    --  the required first named subtype of this type.
710
711    procedure Set_Stored_Constraint_From_Discriminant_Constraint
712      (E : Entity_Id);
713    --  E is some record type. This routine computes E's Stored_Constraint
714    --  from its Discriminant_Constraint.
715
716    procedure Diagnose_Interface (N : Node_Id;  E : Entity_Id);
717    --  Check that an entity in a list of progenitors is an interface,
718    --  emit error otherwise.
719
720    -----------------------
721    -- Access_Definition --
722    -----------------------
723
724    function Access_Definition
725      (Related_Nod : Node_Id;
726       N           : Node_Id) return Entity_Id
727    is
728       Loc        : constant Source_Ptr := Sloc (Related_Nod);
729       Anon_Type  : Entity_Id;
730       Anon_Scope : Entity_Id;
731       Desig_Type : Entity_Id;
732       Decl       : Entity_Id;
733
734    begin
735       if Is_Entry (Current_Scope)
736         and then Is_Task_Type (Etype (Scope (Current_Scope)))
737       then
738          Error_Msg_N ("task entries cannot have access parameters", N);
739          return Empty;
740       end if;
741
742       --  Ada 2005: for an object declaration the corresponding anonymous
743       --  type is declared in the current scope.
744
745       --  If the access definition is the return type of another access to
746       --  function, scope is the current one, because it is the one of the
747       --  current type declaration.
748
749       if Nkind_In (Related_Nod, N_Object_Declaration,
750                                 N_Access_Function_Definition)
751       then
752          Anon_Scope := Current_Scope;
753
754       --  For the anonymous function result case, retrieve the scope of the
755       --  function specification's associated entity rather than using the
756       --  current scope. The current scope will be the function itself if the
757       --  formal part is currently being analyzed, but will be the parent scope
758       --  in the case of a parameterless function, and we always want to use
759       --  the function's parent scope. Finally, if the function is a child
760       --  unit, we must traverse the tree to retrieve the proper entity.
761
762       elsif Nkind (Related_Nod) = N_Function_Specification
763         and then Nkind (Parent (N)) /= N_Parameter_Specification
764       then
765          --  If the current scope is a protected type, the anonymous access
766          --  is associated with one of the protected operations, and must
767          --  be available in the scope that encloses the protected declaration.
768          --  Otherwise the type is is in the scope enclosing the subprogram.
769
770          if Ekind (Current_Scope) = E_Protected_Type then
771             Anon_Scope := Scope (Scope (Defining_Entity (Related_Nod)));
772          else
773             Anon_Scope := Scope (Defining_Entity (Related_Nod));
774          end if;
775
776       else
777          --  For access formals, access components, and access discriminants,
778          --  the scope is that of the enclosing declaration,
779
780          Anon_Scope := Scope (Current_Scope);
781       end if;
782
783       Anon_Type :=
784         Create_Itype
785          (E_Anonymous_Access_Type, Related_Nod, Scope_Id =>  Anon_Scope);
786
787       if All_Present (N)
788         and then Ada_Version >= Ada_05
789       then
790          Error_Msg_N ("ALL is not permitted for anonymous access types", N);
791       end if;
792
793       --  Ada 2005 (AI-254): In case of anonymous access to subprograms call
794       --  the corresponding semantic routine
795
796       if Present (Access_To_Subprogram_Definition (N)) then
797          Access_Subprogram_Declaration
798            (T_Name => Anon_Type,
799             T_Def  => Access_To_Subprogram_Definition (N));
800
801          if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
802             Set_Ekind
803               (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
804          else
805             Set_Ekind
806               (Anon_Type, E_Anonymous_Access_Subprogram_Type);
807          end if;
808
809          Set_Can_Use_Internal_Rep
810            (Anon_Type, not Always_Compatible_Rep_On_Target);
811
812          --  If the anonymous access is associated with a protected operation
813          --  create a reference to it after the enclosing protected definition
814          --  because the itype will be used in the subsequent bodies.
815
816          if Ekind (Current_Scope) = E_Protected_Type then
817             Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
818          end if;
819
820          return Anon_Type;
821       end if;
822
823       Find_Type (Subtype_Mark (N));
824       Desig_Type := Entity (Subtype_Mark (N));
825
826       Set_Directly_Designated_Type
827                              (Anon_Type, Desig_Type);
828       Set_Etype              (Anon_Type, Anon_Type);
829
830       --  Make sure the anonymous access type has size and alignment fields
831       --  set, as required by gigi. This is necessary in the case of the
832       --  Task_Body_Procedure.
833
834       if not Has_Private_Component (Desig_Type) then
835          Layout_Type (Anon_Type);
836       end if;
837
838       --  ???The following makes no sense, because Anon_Type is an access type
839       --  and therefore cannot have components, private or otherwise. Hence
840       --  the assertion. Not sure what was meant, here.
841       Set_Depends_On_Private (Anon_Type, Has_Private_Component (Anon_Type));
842       pragma Assert (not Depends_On_Private (Anon_Type));
843
844       --  Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
845       --  from Ada 95 semantics. In Ada 2005, anonymous access must specify if
846       --  the null value is allowed. In Ada 95 the null value is never allowed.
847
848       if Ada_Version >= Ada_05 then
849          Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
850       else
851          Set_Can_Never_Be_Null (Anon_Type, True);
852       end if;
853
854       --  The anonymous access type is as public as the discriminated type or
855       --  subprogram that defines it. It is imported (for back-end purposes)
856       --  if the designated type is.
857
858       Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
859
860       --  Ada 2005 (AI-50217): Propagate the attribute that indicates that the
861       --  designated type comes from the limited view.
862
863       Set_From_With_Type (Anon_Type, From_With_Type (Desig_Type));
864
865       --  Ada 2005 (AI-231): Propagate the access-constant attribute
866
867       Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
868
869       --  The context is either a subprogram declaration, object declaration,
870       --  or an access discriminant, in a private or a full type declaration.
871       --  In the case of a subprogram, if the designated type is incomplete,
872       --  the operation will be a primitive operation of the full type, to be
873       --  updated subsequently. If the type is imported through a limited_with
874       --  clause, the subprogram is not a primitive operation of the type
875       --  (which is declared elsewhere in some other scope).
876
877       if Ekind (Desig_Type) = E_Incomplete_Type
878         and then not From_With_Type (Desig_Type)
879         and then Is_Overloadable (Current_Scope)
880       then
881          Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
882          Set_Has_Delayed_Freeze (Current_Scope);
883       end if;
884
885       --  Ada 2005: if the designated type is an interface that may contain
886       --  tasks, create a Master entity for the declaration. This must be done
887       --  before expansion of the full declaration, because the declaration may
888       --  include an expression that is an allocator, whose expansion needs the
889       --  proper Master for the created tasks.
890
891       if Nkind (Related_Nod) = N_Object_Declaration
892          and then Expander_Active
893       then
894          if Is_Interface (Desig_Type)
895            and then Is_Limited_Record (Desig_Type)
896          then
897             Build_Class_Wide_Master (Anon_Type);
898
899          --  Similarly, if the type is an anonymous access that designates
900          --  tasks, create a master entity for it in the current context.
901
902          elsif Has_Task (Desig_Type)
903            and then Comes_From_Source (Related_Nod)
904          then
905             if not Has_Master_Entity (Current_Scope) then
906                Decl :=
907                  Make_Object_Declaration (Loc,
908                    Defining_Identifier =>
909                      Make_Defining_Identifier (Loc, Name_uMaster),
910                    Constant_Present => True,
911                    Object_Definition =>
912                      New_Reference_To (RTE (RE_Master_Id), Loc),
913                    Expression =>
914                      Make_Explicit_Dereference (Loc,
915                        New_Reference_To (RTE (RE_Current_Master), Loc)));
916
917                Insert_Before (Related_Nod, Decl);
918                Analyze (Decl);
919
920                Set_Master_Id (Anon_Type, Defining_Identifier (Decl));
921                Set_Has_Master_Entity (Current_Scope);
922             else
923                Build_Master_Renaming (Related_Nod, Anon_Type);
924             end if;
925          end if;
926       end if;
927
928       --  For a private component of a protected type, it is imperative that
929       --  the back-end elaborate the type immediately after the protected
930       --  declaration, because this type will be used in the declarations
931       --  created for the component within each protected body, so we must
932       --  create an itype reference for it now.
933
934       if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
935          Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
936
937       --  Similarly, if the access definition is the return result of a
938       --  function, create an itype reference for it because it
939       --  will be used within the function body. For a regular function that
940       --  is not a compilation unit, insert reference after the declaration.
941       --  For a protected operation, insert it after the enclosing protected
942       --  type declaration. In either case, do not create a reference for a
943       --  type obtained through a limited_with clause, because this would
944       --  introduce semantic dependencies.
945
946       elsif Nkind (Related_Nod) = N_Function_Specification
947         and then not From_With_Type (Anon_Type)
948       then
949          if Ekind (Current_Scope) = E_Protected_Type then
950             Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
951
952          elsif Is_List_Member (Parent (Related_Nod))
953            and then Nkind (Parent (N)) /= N_Parameter_Specification
954          then
955             Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
956          end if;
957
958       --  Finally, create an itype reference for an object declaration of
959       --  an anonymous access type. This is strictly necessary only for
960       --  deferred constants, but in any case will avoid out-of-scope
961       --  problems in the back-end.
962
963       elsif Nkind (Related_Nod) = N_Object_Declaration then
964          Build_Itype_Reference (Anon_Type, Related_Nod);
965       end if;
966
967       return Anon_Type;
968    end Access_Definition;
969
970    -----------------------------------
971    -- Access_Subprogram_Declaration --
972    -----------------------------------
973
974    procedure Access_Subprogram_Declaration
975      (T_Name : Entity_Id;
976       T_Def  : Node_Id)
977    is
978
979       procedure Check_For_Premature_Usage (Def : Node_Id);
980       --  Check that type T_Name is not used, directly or recursively,
981       --  as a parameter or a return type in Def. Def is either a subtype,
982       --  an access_definition, or an access_to_subprogram_definition.
983
984       -------------------------------
985       -- Check_For_Premature_Usage --
986       -------------------------------
987
988       procedure Check_For_Premature_Usage (Def : Node_Id) is
989          Param : Node_Id;
990
991       begin
992          --  Check for a subtype mark
993
994          if Nkind (Def) in N_Has_Etype then
995             if Etype (Def) = T_Name then
996                Error_Msg_N
997                  ("type& cannot be used before end of its declaration", Def);
998             end if;
999
1000          --  If this is not a subtype, then this is an access_definition
1001
1002          elsif Nkind (Def) = N_Access_Definition then
1003             if Present (Access_To_Subprogram_Definition (Def)) then
1004                Check_For_Premature_Usage
1005                  (Access_To_Subprogram_Definition (Def));
1006             else
1007                Check_For_Premature_Usage (Subtype_Mark (Def));
1008             end if;
1009
1010          --  The only cases left are N_Access_Function_Definition and
1011          --  N_Access_Procedure_Definition.
1012
1013          else
1014             if Present (Parameter_Specifications (Def)) then
1015                Param := First (Parameter_Specifications (Def));
1016                while Present (Param) loop
1017                   Check_For_Premature_Usage (Parameter_Type (Param));
1018                   Param := Next (Param);
1019                end loop;
1020             end if;
1021
1022             if Nkind (Def) = N_Access_Function_Definition then
1023                Check_For_Premature_Usage (Result_Definition (Def));
1024             end if;
1025          end if;
1026       end Check_For_Premature_Usage;
1027
1028       --  Local variables
1029
1030       Formals    : constant List_Id := Parameter_Specifications (T_Def);
1031       Formal     : Entity_Id;
1032       D_Ityp     : Node_Id;
1033       Desig_Type : constant Entity_Id :=
1034                      Create_Itype (E_Subprogram_Type, Parent (T_Def));
1035
1036    --  Start of processing for Access_Subprogram_Declaration
1037
1038    begin
1039       --  Associate the Itype node with the inner full-type declaration or
1040       --  subprogram spec. This is required to handle nested anonymous
1041       --  declarations. For example:
1042
1043       --      procedure P
1044       --       (X : access procedure
1045       --                     (Y : access procedure
1046       --                                   (Z : access T)))
1047
1048       D_Ityp := Associated_Node_For_Itype (Desig_Type);
1049       while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1050                                    N_Private_Type_Declaration,
1051                                    N_Private_Extension_Declaration,
1052                                    N_Procedure_Specification,
1053                                    N_Function_Specification)
1054                    or else
1055                  Nkind_In (D_Ityp, N_Object_Declaration,
1056                                    N_Object_Renaming_Declaration,
1057                                    N_Formal_Object_Declaration,
1058                                    N_Formal_Type_Declaration,
1059                                    N_Formal_Object_Declaration,
1060                                    N_Task_Type_Declaration,
1061                                    N_Protected_Type_Declaration))
1062       loop
1063          D_Ityp := Parent (D_Ityp);
1064          pragma Assert (D_Ityp /= Empty);
1065       end loop;
1066
1067       Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1068
1069       if Nkind_In (D_Ityp, N_Procedure_Specification,
1070                            N_Function_Specification)
1071       then
1072          Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1073
1074       elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1075                               N_Object_Declaration,
1076                               N_Object_Renaming_Declaration,
1077                               N_Formal_Type_Declaration)
1078       then
1079          Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1080       end if;
1081
1082       if Nkind (T_Def) = N_Access_Function_Definition then
1083          if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1084             declare
1085                Acc : constant Node_Id := Result_Definition (T_Def);
1086
1087             begin
1088                if Present (Access_To_Subprogram_Definition (Acc))
1089                  and then
1090                    Protected_Present (Access_To_Subprogram_Definition (Acc))
1091                then
1092                   Set_Etype
1093                     (Desig_Type,
1094                        Replace_Anonymous_Access_To_Protected_Subprogram
1095                          (T_Def));
1096
1097                else
1098                   Set_Etype
1099                     (Desig_Type,
1100                        Access_Definition (T_Def, Result_Definition (T_Def)));
1101                end if;
1102             end;
1103
1104          else
1105             Analyze (Result_Definition (T_Def));
1106             Set_Etype (Desig_Type, Entity (Result_Definition (T_Def)));
1107          end if;
1108
1109          if not (Is_Type (Etype (Desig_Type))) then
1110             Error_Msg_N
1111               ("expect type in function specification",
1112                Result_Definition (T_Def));
1113          end if;
1114
1115       else
1116          Set_Etype (Desig_Type, Standard_Void_Type);
1117       end if;
1118
1119       if Present (Formals) then
1120          Push_Scope (Desig_Type);
1121
1122          --  A bit of a kludge here. These kludges will be removed when Itypes
1123          --  have proper parent pointers to their declarations???
1124
1125          --  Kludge 1) Link definining_identifier of formals. Required by
1126          --  First_Formal to provide its functionality.
1127
1128          declare
1129             F : Node_Id;
1130
1131          begin
1132             F := First (Formals);
1133             while Present (F) loop
1134                if No (Parent (Defining_Identifier (F))) then
1135                   Set_Parent (Defining_Identifier (F), F);
1136                end if;
1137
1138                Next (F);
1139             end loop;
1140          end;
1141
1142          Process_Formals (Formals, Parent (T_Def));
1143
1144          --  Kludge 2) End_Scope requires that the parent pointer be set to
1145          --  something reasonable, but Itypes don't have parent pointers. So
1146          --  we set it and then unset it ???
1147
1148          Set_Parent (Desig_Type, T_Name);
1149          End_Scope;
1150          Set_Parent (Desig_Type, Empty);
1151       end if;
1152
1153       --  Check for premature usage of the type being defined
1154
1155       Check_For_Premature_Usage (T_Def);
1156
1157       --  The return type and/or any parameter type may be incomplete. Mark
1158       --  the subprogram_type as depending on the incomplete type, so that
1159       --  it can be updated when the full type declaration is seen. This
1160       --  only applies to incomplete types declared in some enclosing scope,
1161       --  not to limited views from other packages.
1162
1163       if Present (Formals) then
1164          Formal := First_Formal (Desig_Type);
1165          while Present (Formal) loop
1166             if Ekind (Formal) /= E_In_Parameter
1167               and then Nkind (T_Def) = N_Access_Function_Definition
1168             then
1169                Error_Msg_N ("functions can only have IN parameters", Formal);
1170             end if;
1171
1172             if Ekind (Etype (Formal)) = E_Incomplete_Type
1173               and then In_Open_Scopes (Scope (Etype (Formal)))
1174             then
1175                Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1176                Set_Has_Delayed_Freeze (Desig_Type);
1177             end if;
1178
1179             Next_Formal (Formal);
1180          end loop;
1181       end if;
1182
1183       if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1184         and then not Has_Delayed_Freeze (Desig_Type)
1185       then
1186          Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1187          Set_Has_Delayed_Freeze (Desig_Type);
1188       end if;
1189
1190       Check_Delayed_Subprogram (Desig_Type);
1191
1192       if Protected_Present (T_Def) then
1193          Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1194          Set_Convention (Desig_Type, Convention_Protected);
1195       else
1196          Set_Ekind (T_Name, E_Access_Subprogram_Type);
1197       end if;
1198
1199       Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1200
1201       Set_Etype                    (T_Name, T_Name);
1202       Init_Size_Align              (T_Name);
1203       Set_Directly_Designated_Type (T_Name, Desig_Type);
1204
1205       --  Ada 2005 (AI-231): Propagate the null-excluding attribute
1206
1207       Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1208
1209       Check_Restriction (No_Access_Subprograms, T_Def);
1210    end Access_Subprogram_Declaration;
1211
1212    ----------------------------
1213    -- Access_Type_Declaration --
1214    ----------------------------
1215
1216    procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1217       S : constant Node_Id := Subtype_Indication (Def);
1218       P : constant Node_Id := Parent (Def);
1219
1220       Desig : Entity_Id;
1221       --  Designated type
1222
1223    begin
1224       --  Check for permissible use of incomplete type
1225
1226       if Nkind (S) /= N_Subtype_Indication then
1227          Analyze (S);
1228
1229          if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1230             Set_Directly_Designated_Type (T, Entity (S));
1231          else
1232             Set_Directly_Designated_Type (T,
1233               Process_Subtype (S, P, T, 'P'));
1234          end if;
1235
1236       else
1237          Set_Directly_Designated_Type (T,
1238            Process_Subtype (S, P, T, 'P'));
1239       end if;
1240
1241       if All_Present (Def) or Constant_Present (Def) then
1242          Set_Ekind (T, E_General_Access_Type);
1243       else
1244          Set_Ekind (T, E_Access_Type);
1245       end if;
1246
1247       if Base_Type (Designated_Type (T)) = T then
1248          Error_Msg_N ("access type cannot designate itself", S);
1249
1250       --  In Ada 2005, the type may have a limited view through some unit
1251       --  in its own context, allowing the following circularity that cannot
1252       --  be detected earlier
1253
1254       elsif Is_Class_Wide_Type (Designated_Type (T))
1255         and then Etype (Designated_Type (T)) = T
1256       then
1257          Error_Msg_N
1258            ("access type cannot designate its own classwide type", S);
1259
1260          --  Clean up indication of tagged status to prevent cascaded errors
1261
1262          Set_Is_Tagged_Type (T, False);
1263       end if;
1264
1265       Set_Etype (T, T);
1266
1267       --  If the type has appeared already in a with_type clause, it is
1268       --  frozen and the pointer size is already set. Else, initialize.
1269
1270       if not From_With_Type (T) then
1271          Init_Size_Align (T);
1272       end if;
1273
1274       Desig := Designated_Type (T);
1275
1276       --  If designated type is an imported tagged type, indicate that the
1277       --  access type is also imported, and therefore restricted in its use.
1278       --  The access type may already be imported, so keep setting otherwise.
1279
1280       --  Ada 2005 (AI-50217): If the non-limited view of the designated type
1281       --  is available, use it as the designated type of the access type, so
1282       --  that the back-end gets a usable entity.
1283
1284       if From_With_Type (Desig)
1285         and then Ekind (Desig) /= E_Access_Type
1286       then
1287          Set_From_With_Type (T);
1288       end if;
1289
1290       --  Note that Has_Task is always false, since the access type itself
1291       --  is not a task type. See Einfo for more description on this point.
1292       --  Exactly the same consideration applies to Has_Controlled_Component.
1293
1294       Set_Has_Task (T, False);
1295       Set_Has_Controlled_Component (T, False);
1296
1297       --  Initialize Associated_Final_Chain explicitly to Empty, to avoid
1298       --  problems where an incomplete view of this entity has been previously
1299       --  established by a limited with and an overlaid version of this field
1300       --  (Stored_Constraint) was initialized for the incomplete view.
1301
1302       Set_Associated_Final_Chain (T, Empty);
1303
1304       --  Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1305       --  attributes
1306
1307       Set_Can_Never_Be_Null  (T, Null_Exclusion_Present (Def));
1308       Set_Is_Access_Constant (T, Constant_Present (Def));
1309    end Access_Type_Declaration;
1310
1311    ----------------------------------
1312    -- Add_Interface_Tag_Components --
1313    ----------------------------------
1314
1315    procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1316       Loc      : constant Source_Ptr := Sloc (N);
1317       L        : List_Id;
1318       Last_Tag : Node_Id;
1319
1320       procedure Add_Tag (Iface : Entity_Id);
1321       --  Add tag for one of the progenitor interfaces
1322
1323       -------------
1324       -- Add_Tag --
1325       -------------
1326
1327       procedure Add_Tag (Iface : Entity_Id) is
1328          Decl   : Node_Id;
1329          Def    : Node_Id;
1330          Tag    : Entity_Id;
1331          Offset : Entity_Id;
1332
1333       begin
1334          pragma Assert (Is_Tagged_Type (Iface)
1335            and then Is_Interface (Iface));
1336
1337          Def :=
1338            Make_Component_Definition (Loc,
1339              Aliased_Present    => True,
1340              Subtype_Indication =>
1341                New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1342
1343          Tag := Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1344
1345          Decl :=
1346            Make_Component_Declaration (Loc,
1347              Defining_Identifier  => Tag,
1348              Component_Definition => Def);
1349
1350          Analyze_Component_Declaration (Decl);
1351
1352          Set_Analyzed (Decl);
1353          Set_Ekind               (Tag, E_Component);
1354          Set_Is_Tag              (Tag);
1355          Set_Is_Aliased          (Tag);
1356          Set_Related_Type        (Tag, Iface);
1357          Init_Component_Location (Tag);
1358
1359          pragma Assert (Is_Frozen (Iface));
1360
1361          Set_DT_Entry_Count    (Tag,
1362            DT_Entry_Count (First_Entity (Iface)));
1363
1364          if No (Last_Tag) then
1365             Prepend (Decl, L);
1366          else
1367             Insert_After (Last_Tag, Decl);
1368          end if;
1369
1370          Last_Tag := Decl;
1371
1372          --  If the ancestor has discriminants we need to give special support
1373          --  to store the offset_to_top value of the secondary dispatch tables.
1374          --  For this purpose we add a supplementary component just after the
1375          --  field that contains the tag associated with each secondary DT.
1376
1377          if Typ /= Etype (Typ)
1378            and then Has_Discriminants (Etype (Typ))
1379          then
1380             Def :=
1381               Make_Component_Definition (Loc,
1382                 Subtype_Indication =>
1383                   New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1384
1385             Offset :=
1386               Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1387
1388             Decl :=
1389               Make_Component_Declaration (Loc,
1390                 Defining_Identifier  => Offset,
1391                 Component_Definition => Def);
1392
1393             Analyze_Component_Declaration (Decl);
1394
1395             Set_Analyzed (Decl);
1396             Set_Ekind               (Offset, E_Component);
1397             Set_Is_Aliased          (Offset);
1398             Set_Related_Type        (Offset, Iface);
1399             Init_Component_Location (Offset);
1400             Insert_After (Last_Tag, Decl);
1401             Last_Tag := Decl;
1402          end if;
1403       end Add_Tag;
1404
1405       --  Local variables
1406
1407       Elmt : Elmt_Id;
1408       Ext  : Node_Id;
1409       Comp : Node_Id;
1410
1411    --  Start of processing for Add_Interface_Tag_Components
1412
1413    begin
1414       if not RTE_Available (RE_Interface_Tag) then
1415          Error_Msg
1416            ("(Ada 2005) interface types not supported by this run-time!",
1417             Sloc (N));
1418          return;
1419       end if;
1420
1421       if Ekind (Typ) /= E_Record_Type
1422         or else (Is_Concurrent_Record_Type (Typ)
1423                   and then Is_Empty_List (Abstract_Interface_List (Typ)))
1424         or else (not Is_Concurrent_Record_Type (Typ)
1425                   and then No (Interfaces (Typ))
1426                   and then Is_Empty_Elmt_List (Interfaces (Typ)))
1427       then
1428          return;
1429       end if;
1430
1431       --  Find the current last tag
1432
1433       if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1434          Ext := Record_Extension_Part (Type_Definition (N));
1435       else
1436          pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1437          Ext := Type_Definition (N);
1438       end if;
1439
1440       Last_Tag := Empty;
1441
1442       if not (Present (Component_List (Ext))) then
1443          Set_Null_Present (Ext, False);
1444          L := New_List;
1445          Set_Component_List (Ext,
1446            Make_Component_List (Loc,
1447              Component_Items => L,
1448              Null_Present => False));
1449       else
1450          if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1451             L := Component_Items
1452                    (Component_List
1453                      (Record_Extension_Part
1454                        (Type_Definition (N))));
1455          else
1456             L := Component_Items
1457                    (Component_List
1458                      (Type_Definition (N)));
1459          end if;
1460
1461          --  Find the last tag component
1462
1463          Comp := First (L);
1464          while Present (Comp) loop
1465             if Nkind (Comp) = N_Component_Declaration
1466               and then Is_Tag (Defining_Identifier (Comp))
1467             then
1468                Last_Tag := Comp;
1469             end if;
1470
1471             Next (Comp);
1472          end loop;
1473       end if;
1474
1475       --  At this point L references the list of components and Last_Tag
1476       --  references the current last tag (if any). Now we add the tag
1477       --  corresponding with all the interfaces that are not implemented
1478       --  by the parent.
1479
1480       if Present (Interfaces (Typ)) then
1481          Elmt := First_Elmt (Interfaces (Typ));
1482          while Present (Elmt) loop
1483             Add_Tag (Node (Elmt));
1484             Next_Elmt (Elmt);
1485          end loop;
1486       end if;
1487    end Add_Interface_Tag_Components;
1488
1489    -----------------------------------
1490    -- Analyze_Component_Declaration --
1491    -----------------------------------
1492
1493    procedure Analyze_Component_Declaration (N : Node_Id) is
1494       Id : constant Entity_Id := Defining_Identifier (N);
1495       E  : constant Node_Id   := Expression (N);
1496       T  : Entity_Id;
1497       P  : Entity_Id;
1498
1499       function Contains_POC (Constr : Node_Id) return Boolean;
1500       --  Determines whether a constraint uses the discriminant of a record
1501       --  type thus becoming a per-object constraint (POC).
1502
1503       function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1504       --  Typ is the type of the current component, check whether this type is
1505       --  a limited type. Used to validate declaration against that of
1506       --  enclosing record.
1507
1508       ------------------
1509       -- Contains_POC --
1510       ------------------
1511
1512       function Contains_POC (Constr : Node_Id) return Boolean is
1513       begin
1514          --  Prevent cascaded errors
1515
1516          if Error_Posted (Constr) then
1517             return False;
1518          end if;
1519
1520          case Nkind (Constr) is
1521             when N_Attribute_Reference =>
1522                return
1523                  Attribute_Name (Constr) = Name_Access
1524                    and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1525
1526             when N_Discriminant_Association =>
1527                return Denotes_Discriminant (Expression (Constr));
1528
1529             when N_Identifier =>
1530                return Denotes_Discriminant (Constr);
1531
1532             when N_Index_Or_Discriminant_Constraint =>
1533                declare
1534                   IDC : Node_Id;
1535
1536                begin
1537                   IDC := First (Constraints (Constr));
1538                   while Present (IDC) loop
1539
1540                      --  One per-object constraint is sufficient
1541
1542                      if Contains_POC (IDC) then
1543                         return True;
1544                      end if;
1545
1546                      Next (IDC);
1547                   end loop;
1548
1549                   return False;
1550                end;
1551
1552             when N_Range =>
1553                return Denotes_Discriminant (Low_Bound (Constr))
1554                         or else
1555                       Denotes_Discriminant (High_Bound (Constr));
1556
1557             when N_Range_Constraint =>
1558                return Denotes_Discriminant (Range_Expression (Constr));
1559
1560             when others =>
1561                return False;
1562
1563          end case;
1564       end Contains_POC;
1565
1566       ----------------------
1567       -- Is_Known_Limited --
1568       ----------------------
1569
1570       function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1571          P : constant Entity_Id := Etype (Typ);
1572          R : constant Entity_Id := Root_Type (Typ);
1573
1574       begin
1575          if Is_Limited_Record (Typ) then
1576             return True;
1577
1578          --  If the root type is limited (and not a limited interface)
1579          --  so is the current type
1580
1581          elsif Is_Limited_Record (R)
1582            and then
1583              (not Is_Interface (R)
1584                or else not Is_Limited_Interface (R))
1585          then
1586             return True;
1587
1588          --  Else the type may have a limited interface progenitor, but a
1589          --  limited record parent.
1590
1591          elsif R /= P
1592            and then Is_Limited_Record (P)
1593          then
1594             return True;
1595
1596          else
1597             return False;
1598          end if;
1599       end Is_Known_Limited;
1600
1601    --  Start of processing for Analyze_Component_Declaration
1602
1603    begin
1604       Generate_Definition (Id);
1605       Enter_Name (Id);
1606
1607       if Present (Subtype_Indication (Component_Definition (N))) then
1608          T := Find_Type_Of_Object
1609                 (Subtype_Indication (Component_Definition (N)), N);
1610
1611       --  Ada 2005 (AI-230): Access Definition case
1612
1613       else
1614          pragma Assert (Present
1615                           (Access_Definition (Component_Definition (N))));
1616
1617          T := Access_Definition
1618                 (Related_Nod => N,
1619                  N => Access_Definition (Component_Definition (N)));
1620          Set_Is_Local_Anonymous_Access (T);
1621
1622          --  Ada 2005 (AI-254)
1623
1624          if Present (Access_To_Subprogram_Definition
1625                       (Access_Definition (Component_Definition (N))))
1626            and then Protected_Present (Access_To_Subprogram_Definition
1627                                         (Access_Definition
1628                                           (Component_Definition (N))))
1629          then
1630             T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1631          end if;
1632       end if;
1633
1634       --  If the subtype is a constrained subtype of the enclosing record,
1635       --  (which must have a partial view) the back-end does not properly
1636       --  handle the recursion. Rewrite the component declaration with an
1637       --  explicit subtype indication, which is acceptable to Gigi. We can copy
1638       --  the tree directly because side effects have already been removed from
1639       --  discriminant constraints.
1640
1641       if Ekind (T) = E_Access_Subtype
1642         and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1643         and then Comes_From_Source (T)
1644         and then Nkind (Parent (T)) = N_Subtype_Declaration
1645         and then Etype (Directly_Designated_Type (T)) = Current_Scope
1646       then
1647          Rewrite
1648            (Subtype_Indication (Component_Definition (N)),
1649              New_Copy_Tree (Subtype_Indication (Parent (T))));
1650          T := Find_Type_Of_Object
1651                  (Subtype_Indication (Component_Definition (N)), N);
1652       end if;
1653
1654       --  If the component declaration includes a default expression, then we
1655       --  check that the component is not of a limited type (RM 3.7(5)),
1656       --  and do the special preanalysis of the expression (see section on
1657       --  "Handling of Default and Per-Object Expressions" in the spec of
1658       --  package Sem).
1659
1660       if Present (E) then
1661          Preanalyze_Spec_Expression (E, T);
1662          Check_Initialization (T, E);
1663
1664          if Ada_Version >= Ada_05
1665            and then Ekind (T) = E_Anonymous_Access_Type
1666          then
1667             --  Check RM 3.9.2(9): "if the expected type for an expression is
1668             --  an anonymous access-to-specific tagged type, then the object
1669             --  designated by the expression shall not be dynamically tagged
1670             --  unless it is a controlling operand in a call on a dispatching
1671             --  operation"
1672
1673             if Is_Tagged_Type (Directly_Designated_Type (T))
1674               and then
1675                 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1676               and then
1677                 Ekind (Directly_Designated_Type (Etype (E))) =
1678                   E_Class_Wide_Type
1679             then
1680                Error_Msg_N
1681                  ("access to specific tagged type required (RM 3.9.2(9))", E);
1682             end if;
1683
1684             --  (Ada 2005: AI-230): Accessibility check for anonymous
1685             --  components
1686
1687             if Type_Access_Level (Etype (E)) > Type_Access_Level (T) then
1688                Error_Msg_N
1689                  ("expression has deeper access level than component " &
1690                   "(RM 3.10.2 (12.2))", E);
1691             end if;
1692
1693             --  The initialization expression is a reference to an access
1694             --  discriminant. The type of the discriminant is always deeper
1695             --  than any access type.
1696
1697             if Ekind (Etype (E)) = E_Anonymous_Access_Type
1698               and then Is_Entity_Name (E)
1699               and then Ekind (Entity (E)) = E_In_Parameter
1700               and then Present (Discriminal_Link (Entity (E)))
1701             then
1702                Error_Msg_N
1703                  ("discriminant has deeper accessibility level than target",
1704                   E);
1705             end if;
1706          end if;
1707       end if;
1708
1709       --  The parent type may be a private view with unknown discriminants,
1710       --  and thus unconstrained. Regular components must be constrained.
1711
1712       if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1713          if Is_Class_Wide_Type (T) then
1714             Error_Msg_N
1715                ("class-wide subtype with unknown discriminants" &
1716                  " in component declaration",
1717                  Subtype_Indication (Component_Definition (N)));
1718          else
1719             Error_Msg_N
1720               ("unconstrained subtype in component declaration",
1721                Subtype_Indication (Component_Definition (N)));
1722          end if;
1723
1724       --  Components cannot be abstract, except for the special case of
1725       --  the _Parent field (case of extending an abstract tagged type)
1726
1727       elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
1728          Error_Msg_N ("type of a component cannot be abstract", N);
1729       end if;
1730
1731       Set_Etype (Id, T);
1732       Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1733
1734       --  The component declaration may have a per-object constraint, set
1735       --  the appropriate flag in the defining identifier of the subtype.
1736
1737       if Present (Subtype_Indication (Component_Definition (N))) then
1738          declare
1739             Sindic : constant Node_Id :=
1740                        Subtype_Indication (Component_Definition (N));
1741          begin
1742             if Nkind (Sindic) = N_Subtype_Indication
1743               and then Present (Constraint (Sindic))
1744               and then Contains_POC (Constraint (Sindic))
1745             then
1746                Set_Has_Per_Object_Constraint (Id);
1747             end if;
1748          end;
1749       end if;
1750
1751       --  Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1752       --  out some static checks.
1753
1754       if Ada_Version >= Ada_05
1755         and then Can_Never_Be_Null (T)
1756       then
1757          Null_Exclusion_Static_Checks (N);
1758       end if;
1759
1760       --  If this component is private (or depends on a private type), flag the
1761       --  record type to indicate that some operations are not available.
1762
1763       P := Private_Component (T);
1764
1765       if Present (P) then
1766
1767          --  Check for circular definitions
1768
1769          if P = Any_Type then
1770             Set_Etype (Id, Any_Type);
1771
1772          --  There is a gap in the visibility of operations only if the
1773          --  component type is not defined in the scope of the record type.
1774
1775          elsif Scope (P) = Scope (Current_Scope) then
1776             null;
1777
1778          elsif Is_Limited_Type (P) then
1779             Set_Is_Limited_Composite (Current_Scope);
1780
1781          else
1782             Set_Is_Private_Composite (Current_Scope);
1783          end if;
1784       end if;
1785
1786       if P /= Any_Type
1787         and then Is_Limited_Type (T)
1788         and then Chars (Id) /= Name_uParent
1789         and then Is_Tagged_Type (Current_Scope)
1790       then
1791          if Is_Derived_Type (Current_Scope)
1792            and then not Is_Known_Limited (Current_Scope)
1793          then
1794             Error_Msg_N
1795               ("extension of nonlimited type cannot have limited components",
1796                N);
1797
1798             if Is_Interface (Root_Type (Current_Scope)) then
1799                Error_Msg_N
1800                  ("\limitedness is not inherited from limited interface", N);
1801                Error_Msg_N
1802                  ("\add LIMITED to type indication", N);
1803             end if;
1804
1805             Explain_Limited_Type (T, N);
1806             Set_Etype (Id, Any_Type);
1807             Set_Is_Limited_Composite (Current_Scope, False);
1808
1809          elsif not Is_Derived_Type (Current_Scope)
1810            and then not Is_Limited_Record (Current_Scope)
1811            and then not Is_Concurrent_Type (Current_Scope)
1812          then
1813             Error_Msg_N
1814               ("nonlimited tagged type cannot have limited components", N);
1815             Explain_Limited_Type (T, N);
1816             Set_Etype (Id, Any_Type);
1817             Set_Is_Limited_Composite (Current_Scope, False);
1818          end if;
1819       end if;
1820
1821       Set_Original_Record_Component (Id, Id);
1822    end Analyze_Component_Declaration;
1823
1824    --------------------------
1825    -- Analyze_Declarations --
1826    --------------------------
1827
1828    procedure Analyze_Declarations (L : List_Id) is
1829       D           : Node_Id;
1830       Freeze_From : Entity_Id := Empty;
1831       Next_Node   : Node_Id;
1832
1833       procedure Adjust_D;
1834       --  Adjust D not to include implicit label declarations, since these
1835       --  have strange Sloc values that result in elaboration check problems.
1836       --  (They have the sloc of the label as found in the source, and that
1837       --  is ahead of the current declarative part).
1838
1839       --------------
1840       -- Adjust_D --
1841       --------------
1842
1843       procedure Adjust_D is
1844       begin
1845          while Present (Prev (D))
1846            and then Nkind (D) = N_Implicit_Label_Declaration
1847          loop
1848             Prev (D);
1849          end loop;
1850       end Adjust_D;
1851
1852    --  Start of processing for Analyze_Declarations
1853
1854    begin
1855       D := First (L);
1856       while Present (D) loop
1857
1858          --  Complete analysis of declaration
1859
1860          Analyze (D);
1861          Next_Node := Next (D);
1862
1863          if No (Freeze_From) then
1864             Freeze_From := First_Entity (Current_Scope);
1865          end if;
1866
1867          --  At the end of a declarative part, freeze remaining entities
1868          --  declared in it. The end of the visible declarations of package
1869          --  specification is not the end of a declarative part if private
1870          --  declarations are present. The end of a package declaration is a
1871          --  freezing point only if it a library package. A task definition or
1872          --  protected type definition is not a freeze point either. Finally,
1873          --  we do not freeze entities in generic scopes, because there is no
1874          --  code generated for them and freeze nodes will be generated for
1875          --  the instance.
1876
1877          --  The end of a package instantiation is not a freeze point, but
1878          --  for now we make it one, because the generic body is inserted
1879          --  (currently) immediately after. Generic instantiations will not
1880          --  be a freeze point once delayed freezing of bodies is implemented.
1881          --  (This is needed in any case for early instantiations ???).
1882
1883          if No (Next_Node) then
1884             if Nkind_In (Parent (L), N_Component_List,
1885                                      N_Task_Definition,
1886                                      N_Protected_Definition)
1887             then
1888                null;
1889
1890             elsif Nkind (Parent (L)) /= N_Package_Specification then
1891                if Nkind (Parent (L)) = N_Package_Body then
1892                   Freeze_From := First_Entity (Current_Scope);
1893                end if;
1894
1895                Adjust_D;
1896                Freeze_All (Freeze_From, D);
1897                Freeze_From := Last_Entity (Current_Scope);
1898
1899             elsif Scope (Current_Scope) /= Standard_Standard
1900               and then not Is_Child_Unit (Current_Scope)
1901               and then No (Generic_Parent (Parent (L)))
1902             then
1903                null;
1904
1905             elsif L /= Visible_Declarations (Parent (L))
1906                or else No (Private_Declarations (Parent (L)))
1907                or else Is_Empty_List (Private_Declarations (Parent (L)))
1908             then
1909                Adjust_D;
1910                Freeze_All (Freeze_From, D);
1911                Freeze_From := Last_Entity (Current_Scope);
1912             end if;
1913
1914          --  If next node is a body then freeze all types before the body.
1915          --  An exception occurs for some expander-generated bodies. If these
1916          --  are generated at places where in general language rules would not
1917          --  allow a freeze point, then we assume that the expander has
1918          --  explicitly checked that all required types are properly frozen,
1919          --  and we do not cause general freezing here. This special circuit
1920          --  is used when the encountered body is marked as having already
1921          --  been analyzed.
1922
1923          --  In all other cases (bodies that come from source, and expander
1924          --  generated bodies that have not been analyzed yet), freeze all
1925          --  types now. Note that in the latter case, the expander must take
1926          --  care to attach the bodies at a proper place in the tree so as to
1927          --  not cause unwanted freezing at that point.
1928
1929          elsif not Analyzed (Next_Node)
1930            and then (Nkind_In (Next_Node, N_Subprogram_Body,
1931                                           N_Entry_Body,
1932                                           N_Package_Body,
1933                                           N_Protected_Body,
1934                                           N_Task_Body)
1935                        or else
1936                      Nkind (Next_Node) in N_Body_Stub)
1937          then
1938             Adjust_D;
1939             Freeze_All (Freeze_From, D);
1940             Freeze_From := Last_Entity (Current_Scope);
1941          end if;
1942
1943          D := Next_Node;
1944       end loop;
1945    end Analyze_Declarations;
1946
1947    ----------------------------------
1948    -- Analyze_Incomplete_Type_Decl --
1949    ----------------------------------
1950
1951    procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
1952       F : constant Boolean := Is_Pure (Current_Scope);
1953       T : Entity_Id;
1954
1955    begin
1956       Generate_Definition (Defining_Identifier (N));
1957
1958       --  Process an incomplete declaration. The identifier must not have been
1959       --  declared already in the scope. However, an incomplete declaration may
1960       --  appear in the private part of a package, for a private type that has
1961       --  already been declared.
1962
1963       --  In this case, the discriminants (if any) must match
1964
1965       T := Find_Type_Name (N);
1966
1967       Set_Ekind (T, E_Incomplete_Type);
1968       Init_Size_Align (T);
1969       Set_Is_First_Subtype (T, True);
1970       Set_Etype (T, T);
1971
1972       --  Ada 2005 (AI-326): Minimum decoration to give support to tagged
1973       --  incomplete types.
1974
1975       if Tagged_Present (N) then
1976          Set_Is_Tagged_Type (T);
1977          Make_Class_Wide_Type (T);
1978          Set_Primitive_Operations (T, New_Elmt_List);
1979       end if;
1980
1981       Push_Scope (T);
1982
1983       Set_Stored_Constraint (T, No_Elist);
1984
1985       if Present (Discriminant_Specifications (N)) then
1986          Process_Discriminants (N);
1987       end if;
1988
1989       End_Scope;
1990
1991       --  If the type has discriminants, non-trivial subtypes may be be
1992       --  declared before the full view of the type. The full views of those
1993       --  subtypes will be built after the full view of the type.
1994
1995       Set_Private_Dependents (T, New_Elmt_List);
1996       Set_Is_Pure (T, F);
1997    end Analyze_Incomplete_Type_Decl;
1998
1999    -----------------------------------
2000    -- Analyze_Interface_Declaration --
2001    -----------------------------------
2002
2003    procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2004       CW : constant Entity_Id := Class_Wide_Type (T);
2005
2006    begin
2007       Set_Is_Tagged_Type (T);
2008
2009       Set_Is_Limited_Record (T, Limited_Present (Def)
2010                                   or else Task_Present (Def)
2011                                   or else Protected_Present (Def)
2012                                   or else Synchronized_Present (Def));
2013
2014       --  Type is abstract if full declaration carries keyword, or if previous
2015       --  partial view did.
2016
2017       Set_Is_Abstract_Type (T);
2018       Set_Is_Interface (T);
2019
2020       --  Type is a limited interface if it includes the keyword limited, task,
2021       --  protected, or synchronized.
2022
2023       Set_Is_Limited_Interface
2024         (T, Limited_Present (Def)
2025               or else Protected_Present (Def)
2026               or else Synchronized_Present (Def)
2027               or else Task_Present (Def));
2028
2029       Set_Is_Protected_Interface (T, Protected_Present (Def));
2030       Set_Is_Task_Interface (T, Task_Present (Def));
2031
2032       --  Type is a synchronized interface if it includes the keyword task,
2033       --  protected, or synchronized.
2034
2035       Set_Is_Synchronized_Interface
2036         (T, Synchronized_Present (Def)
2037               or else Protected_Present (Def)
2038               or else Task_Present (Def));
2039
2040       Set_Interfaces (T, New_Elmt_List);
2041       Set_Primitive_Operations (T, New_Elmt_List);
2042
2043       --  Complete the decoration of the class-wide entity if it was already
2044       --  built (i.e. during the creation of the limited view)
2045
2046       if Present (CW) then
2047          Set_Is_Interface (CW);
2048          Set_Is_Limited_Interface      (CW, Is_Limited_Interface (T));
2049          Set_Is_Protected_Interface    (CW, Is_Protected_Interface (T));
2050          Set_Is_Synchronized_Interface (CW, Is_Synchronized_Interface (T));
2051          Set_Is_Task_Interface         (CW, Is_Task_Interface (T));
2052       end if;
2053
2054       --  Check runtime support for synchronized interfaces
2055
2056       if VM_Target = No_VM
2057         and then (Is_Task_Interface (T)
2058                     or else Is_Protected_Interface (T)
2059                     or else Is_Synchronized_Interface (T))
2060         and then not RTE_Available (RE_Select_Specific_Data)
2061       then
2062          Error_Msg_CRT ("synchronized interfaces", T);
2063       end if;
2064    end Analyze_Interface_Declaration;
2065
2066    -----------------------------
2067    -- Analyze_Itype_Reference --
2068    -----------------------------
2069
2070    --  Nothing to do. This node is placed in the tree only for the benefit of
2071    --  back end processing, and has no effect on the semantic processing.
2072
2073    procedure Analyze_Itype_Reference (N : Node_Id) is
2074    begin
2075       pragma Assert (Is_Itype (Itype (N)));
2076       null;
2077    end Analyze_Itype_Reference;
2078
2079    --------------------------------
2080    -- Analyze_Number_Declaration --
2081    --------------------------------
2082
2083    procedure Analyze_Number_Declaration (N : Node_Id) is
2084       Id    : constant Entity_Id := Defining_Identifier (N);
2085       E     : constant Node_Id   := Expression (N);
2086       T     : Entity_Id;
2087       Index : Interp_Index;
2088       It    : Interp;
2089
2090    begin
2091       Generate_Definition (Id);
2092       Enter_Name (Id);
2093
2094       --  This is an optimization of a common case of an integer literal
2095
2096       if Nkind (E) = N_Integer_Literal then
2097          Set_Is_Static_Expression (E, True);
2098          Set_Etype                (E, Universal_Integer);
2099
2100          Set_Etype     (Id, Universal_Integer);
2101          Set_Ekind     (Id, E_Named_Integer);
2102          Set_Is_Frozen (Id, True);
2103          return;
2104       end if;
2105
2106       Set_Is_Pure (Id, Is_Pure (Current_Scope));
2107
2108       --  Process expression, replacing error by integer zero, to avoid
2109       --  cascaded errors or aborts further along in the processing
2110
2111       --  Replace Error by integer zero, which seems least likely to
2112       --  cause cascaded errors.
2113
2114       if E = Error then
2115          Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2116          Set_Error_Posted (E);
2117       end if;
2118
2119       Analyze (E);
2120
2121       --  Verify that the expression is static and numeric. If
2122       --  the expression is overloaded, we apply the preference
2123       --  rule that favors root numeric types.
2124
2125       if not Is_Overloaded (E) then
2126          T := Etype (E);
2127
2128       else
2129          T := Any_Type;
2130
2131          Get_First_Interp (E, Index, It);
2132          while Present (It.Typ) loop
2133             if (Is_Integer_Type (It.Typ)
2134                  or else Is_Real_Type (It.Typ))
2135               and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2136             then
2137                if T = Any_Type then
2138                   T := It.Typ;
2139
2140                elsif It.Typ = Universal_Real
2141                  or else It.Typ = Universal_Integer
2142                then
2143                   --  Choose universal interpretation over any other
2144
2145                   T := It.Typ;
2146                   exit;
2147                end if;
2148             end if;
2149
2150             Get_Next_Interp (Index, It);
2151          end loop;
2152       end if;
2153
2154       if Is_Integer_Type (T)  then
2155          Resolve (E, T);
2156          Set_Etype (Id, Universal_Integer);
2157          Set_Ekind (Id, E_Named_Integer);
2158
2159       elsif Is_Real_Type (T) then
2160
2161          --  Because the real value is converted to universal_real, this is a
2162          --  legal context for a universal fixed expression.
2163
2164          if T = Universal_Fixed then
2165             declare
2166                Loc  : constant Source_Ptr := Sloc (N);
2167                Conv : constant Node_Id := Make_Type_Conversion (Loc,
2168                         Subtype_Mark =>
2169                           New_Occurrence_Of (Universal_Real, Loc),
2170                         Expression => Relocate_Node (E));
2171
2172             begin
2173                Rewrite (E, Conv);
2174                Analyze (E);
2175             end;
2176
2177          elsif T = Any_Fixed then
2178             Error_Msg_N ("illegal context for mixed mode operation", E);
2179
2180             --  Expression is of the form : universal_fixed * integer. Try to
2181             --  resolve as universal_real.
2182
2183             T := Universal_Real;
2184             Set_Etype (E, T);
2185          end if;
2186
2187          Resolve (E, T);
2188          Set_Etype (Id, Universal_Real);
2189          Set_Ekind (Id, E_Named_Real);
2190
2191       else
2192          Wrong_Type (E, Any_Numeric);
2193          Resolve (E, T);
2194
2195          Set_Etype               (Id, T);
2196          Set_Ekind               (Id, E_Constant);
2197          Set_Never_Set_In_Source (Id, True);
2198          Set_Is_True_Constant    (Id, True);
2199          return;
2200       end if;
2201
2202       if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
2203          Set_Etype (E, Etype (Id));
2204       end if;
2205
2206       if not Is_OK_Static_Expression (E) then
2207          Flag_Non_Static_Expr
2208            ("non-static expression used in number declaration!", E);
2209          Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2210          Set_Etype (E, Any_Type);
2211       end if;
2212    end Analyze_Number_Declaration;
2213
2214    --------------------------------
2215    -- Analyze_Object_Declaration --
2216    --------------------------------
2217
2218    procedure Analyze_Object_Declaration (N : Node_Id) is
2219       Loc   : constant Source_Ptr := Sloc (N);
2220       Id    : constant Entity_Id  := Defining_Identifier (N);
2221       T     : Entity_Id;
2222       Act_T : Entity_Id;
2223
2224       E : Node_Id := Expression (N);
2225       --  E is set to Expression (N) throughout this routine. When
2226       --  Expression (N) is modified, E is changed accordingly.
2227
2228       Prev_Entity : Entity_Id := Empty;
2229
2230       function Count_Tasks (T : Entity_Id) return Uint;
2231       --  This function is called when a non-generic library level object of a
2232       --  task type is declared. Its function is to count the static number of
2233       --  tasks declared within the type (it is only called if Has_Tasks is set
2234       --  for T). As a side effect, if an array of tasks with non-static bounds
2235       --  or a variant record type is encountered, Check_Restrictions is called
2236       --  indicating the count is unknown.
2237
2238       -----------------
2239       -- Count_Tasks --
2240       -----------------
2241
2242       function Count_Tasks (T : Entity_Id) return Uint is
2243          C : Entity_Id;
2244          X : Node_Id;
2245          V : Uint;
2246
2247       begin
2248          if Is_Task_Type (T) then
2249             return Uint_1;
2250
2251          elsif Is_Record_Type (T) then
2252             if Has_Discriminants (T) then
2253                Check_Restriction (Max_Tasks, N);
2254                return Uint_0;
2255
2256             else
2257                V := Uint_0;
2258                C := First_Component (T);
2259                while Present (C) loop
2260                   V := V + Count_Tasks (Etype (C));
2261                   Next_Component (C);
2262                end loop;
2263
2264                return V;
2265             end if;
2266
2267          elsif Is_Array_Type (T) then
2268             X := First_Index (T);
2269             V := Count_Tasks (Component_Type (T));
2270             while Present (X) loop
2271                C := Etype (X);
2272
2273                if not Is_Static_Subtype (C) then
2274                   Check_Restriction (Max_Tasks, N);
2275                   return Uint_0;
2276                else
2277                   V := V * (UI_Max (Uint_0,
2278                                     Expr_Value (Type_High_Bound (C)) -
2279                                     Expr_Value (Type_Low_Bound (C)) + Uint_1));
2280                end if;
2281
2282                Next_Index (X);
2283             end loop;
2284
2285             return V;
2286
2287          else
2288             return Uint_0;
2289          end if;
2290       end Count_Tasks;
2291
2292    --  Start of processing for Analyze_Object_Declaration
2293
2294    begin
2295       --  There are three kinds of implicit types generated by an
2296       --  object declaration:
2297
2298       --   1. Those for generated by the original Object Definition
2299
2300       --   2. Those generated by the Expression
2301
2302       --   3. Those used to constrained the Object Definition with the
2303       --       expression constraints when it is unconstrained
2304
2305       --  They must be generated in this order to avoid order of elaboration
2306       --  issues. Thus the first step (after entering the name) is to analyze
2307       --  the object definition.
2308
2309       if Constant_Present (N) then
2310          Prev_Entity := Current_Entity_In_Scope (Id);
2311
2312          --  If the homograph is an implicit subprogram, it is overridden by
2313          --  the current declaration.
2314
2315          if Present (Prev_Entity)
2316            and then
2317              ((Is_Overloadable (Prev_Entity)
2318                  and then Is_Inherited_Operation (Prev_Entity))
2319
2320                --  The current object is a discriminal generated for an entry
2321                --  family index. Even though the index is a constant, in this
2322                --  particular context there is no true constant redeclaration.
2323                --  Enter_Name will handle the visibility.
2324
2325                or else
2326                 (Is_Discriminal (Id)
2327                    and then Ekind (Discriminal_Link (Id)) =
2328                               E_Entry_Index_Parameter))
2329          then
2330             Prev_Entity := Empty;
2331          end if;
2332       end if;
2333
2334       if Present (Prev_Entity) then
2335          Constant_Redeclaration (Id, N, T);
2336
2337          Generate_Reference (Prev_Entity, Id, 'c');
2338          Set_Completion_Referenced (Id);
2339
2340          if Error_Posted (N) then
2341
2342             --  Type mismatch or illegal redeclaration, Do not analyze
2343             --  expression to avoid cascaded errors.
2344
2345             T := Find_Type_Of_Object (Object_Definition (N), N);
2346             Set_Etype (Id, T);
2347             Set_Ekind (Id, E_Variable);
2348             return;
2349          end if;
2350
2351       --  In the normal case, enter identifier at the start to catch premature
2352       --  usage in the initialization expression.
2353
2354       else
2355          Generate_Definition (Id);
2356          Enter_Name (Id);
2357
2358          Mark_Coextensions (N, Object_Definition (N));
2359
2360          T := Find_Type_Of_Object (Object_Definition (N), N);
2361
2362          if Nkind (Object_Definition (N)) = N_Access_Definition
2363            and then Present
2364              (Access_To_Subprogram_Definition (Object_Definition (N)))
2365            and then Protected_Present
2366              (Access_To_Subprogram_Definition (Object_Definition (N)))
2367          then
2368             T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2369          end if;
2370
2371          if Error_Posted (Id) then
2372             Set_Etype (Id, T);
2373             Set_Ekind (Id, E_Variable);
2374             return;
2375          end if;
2376       end if;
2377
2378       --  Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2379       --  out some static checks
2380
2381       if Ada_Version >= Ada_05
2382         and then Can_Never_Be_Null (T)
2383       then
2384          --  In case of aggregates we must also take care of the correct
2385          --  initialization of nested aggregates bug this is done at the
2386          --  point of the analysis of the aggregate (see sem_aggr.adb)
2387
2388          if Present (Expression (N))
2389            and then Nkind (Expression (N)) = N_Aggregate
2390          then
2391             null;
2392
2393          else
2394             declare
2395                Save_Typ : constant Entity_Id := Etype (Id);
2396             begin
2397                Set_Etype (Id, T); --  Temp. decoration for static checks
2398                Null_Exclusion_Static_Checks (N);
2399                Set_Etype (Id, Save_Typ);
2400             end;
2401          end if;
2402       end if;
2403
2404       Set_Is_Pure (Id, Is_Pure (Current_Scope));
2405
2406       --  If deferred constant, make sure context is appropriate. We detect
2407       --  a deferred constant as a constant declaration with no expression.
2408       --  A deferred constant can appear in a package body if its completion
2409       --  is by means of an interface pragma.
2410
2411       if Constant_Present (N)
2412         and then No (E)
2413       then
2414          --  We exclude forward references to tags
2415
2416          if Is_Imported (Defining_Identifier (N))
2417            and then
2418              (T = RTE (RE_Tag)
2419                or else
2420                  (Present (Full_View (T))
2421                    and then Full_View (T) = RTE (RE_Tag)))
2422          then
2423             null;
2424
2425          --  A deferred constant may appear in the declarative part of the
2426          --  following constructs:
2427
2428          --     blocks
2429          --     entry bodies
2430          --     extended return statements
2431          --     package specs
2432          --     package bodies
2433          --     subprogram bodies
2434          --     task bodies
2435
2436          --  When declared inside a package spec, a deferred constant must be
2437          --  completed by a full constant declaration or pragma Import. In all
2438          --  other cases, the only proper completion is pragma Import. Extended
2439          --  return statements are flagged as invalid contexts because they do
2440          --  not have a declarative part and so cannot accommodate the pragma.
2441
2442          elsif Ekind (Current_Scope) = E_Return_Statement then
2443             Error_Msg_N
2444               ("invalid context for deferred constant declaration (RM 7.4)",
2445                N);
2446             Error_Msg_N
2447               ("\declaration requires an initialization expression",
2448                 N);
2449             Set_Constant_Present (N, False);
2450
2451          --  In Ada 83, deferred constant must be of private type
2452
2453          elsif not Is_Private_Type (T) then
2454             if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2455                Error_Msg_N
2456                  ("(Ada 83) deferred constant must be private type", N);
2457             end if;
2458          end if;
2459
2460       --  If not a deferred constant, then object declaration freezes its type
2461
2462       else
2463          Check_Fully_Declared (T, N);
2464          Freeze_Before (N, T);
2465       end if;
2466
2467       --  If the object was created by a constrained array definition, then
2468       --  set the link in both the anonymous base type and anonymous subtype
2469       --  that are built to represent the array type to point to the object.
2470
2471       if Nkind (Object_Definition (Declaration_Node (Id))) =
2472                         N_Constrained_Array_Definition
2473       then
2474          Set_Related_Array_Object (T, Id);
2475          Set_Related_Array_Object (Base_Type (T), Id);
2476       end if;
2477
2478       --  Special checks for protected objects not at library level
2479
2480       if Is_Protected_Type (T)
2481         and then not Is_Library_Level_Entity (Id)
2482       then
2483          Check_Restriction (No_Local_Protected_Objects, Id);
2484
2485          --  Protected objects with interrupt handlers must be at library level
2486
2487          --  Ada 2005: this test is not needed (and the corresponding clause
2488          --  in the RM is removed) because accessibility checks are sufficient
2489          --  to make handlers not at the library level illegal.
2490
2491          if Has_Interrupt_Handler (T)
2492            and then Ada_Version < Ada_05
2493          then
2494             Error_Msg_N
2495               ("interrupt object can only be declared at library level", Id);
2496          end if;
2497       end if;
2498
2499       --  The actual subtype of the object is the nominal subtype, unless
2500       --  the nominal one is unconstrained and obtained from the expression.
2501
2502       Act_T := T;
2503
2504       --  Process initialization expression if present and not in error
2505
2506       if Present (E) and then E /= Error then
2507
2508          --  Generate an error in case of CPP class-wide object initialization.
2509          --  Required because otherwise the expansion of the class-wide
2510          --  assignment would try to use 'size to initialize the object
2511          --  (primitive that is not available in CPP tagged types).
2512
2513          if Is_Class_Wide_Type (Act_T)
2514            and then
2515              (Is_CPP_Class (Root_Type (Etype (Act_T)))
2516                or else
2517                  (Present (Full_View (Root_Type (Etype (Act_T))))
2518                     and then
2519                       Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
2520          then
2521             Error_Msg_N
2522               ("predefined assignment not available for 'C'P'P tagged types",
2523                E);
2524          end if;
2525
2526          Mark_Coextensions (N, E);
2527          Analyze (E);
2528
2529          --  In case of errors detected in the analysis of the expression,
2530          --  decorate it with the expected type to avoid cascaded errors
2531
2532          if No (Etype (E)) then
2533             Set_Etype (E, T);
2534          end if;
2535
2536          --  If an initialization expression is present, then we set the
2537          --  Is_True_Constant flag. It will be reset if this is a variable
2538          --  and it is indeed modified.
2539
2540          Set_Is_True_Constant (Id, True);
2541
2542          --  If we are analyzing a constant declaration, set its completion
2543          --  flag after analyzing and resolving the expression.
2544
2545          if Constant_Present (N) then
2546             Set_Has_Completion (Id);
2547          end if;
2548
2549          --  Set type and resolve (type may be overridden later on)
2550
2551          Set_Etype (Id, T);
2552          Resolve (E, T);
2553
2554          --  If the object is an access to variable, the initialization
2555          --  expression cannot be an access to constant.
2556
2557          if Is_Access_Type (T)
2558            and then not Is_Access_Constant (T)
2559            and then Is_Access_Type (Etype (E))
2560            and then Is_Access_Constant (Etype (E))
2561          then
2562             Error_Msg_N
2563               ("object that is an access to variable cannot be initialized " &
2564                 "with an access-to-constant expression", E);
2565          end if;
2566
2567          if not Assignment_OK (N) then
2568             Check_Initialization (T, E);
2569          end if;
2570
2571          Check_Unset_Reference (E);
2572
2573          --  If this is a variable, then set current value
2574
2575          if not Constant_Present (N) then
2576             if Compile_Time_Known_Value (E) then
2577                Set_Current_Value (Id, E);
2578             end if;
2579          end if;
2580
2581          --  Deal with setting of null flags
2582
2583          if Is_Access_Type (T) then
2584             if Known_Non_Null (E) then
2585                Set_Is_Known_Non_Null (Id, True);
2586             elsif Known_Null (E)
2587               and then not Can_Never_Be_Null (Id)
2588             then
2589                Set_Is_Known_Null (Id, True);
2590             end if;
2591          end if;
2592
2593          --  Check incorrect use of dynamically tagged expressions. Note
2594          --  the use of Is_Tagged_Type (T) which seems redundant but is in
2595          --  fact important to avoid spurious errors due to expanded code
2596          --  for dispatching functions over an anonymous access type
2597
2598          if (Is_Class_Wide_Type (Etype (E)) or else Is_Dynamically_Tagged (E))
2599            and then Is_Tagged_Type (T)
2600            and then not Is_Class_Wide_Type (T)
2601          then
2602             Error_Msg_N ("dynamically tagged expression not allowed!", E);
2603          end if;
2604
2605          Apply_Scalar_Range_Check (E, T);
2606          Apply_Static_Length_Check (E, T);
2607       end if;
2608
2609       --  If the No_Streams restriction is set, check that the type of the
2610       --  object is not, and does not contain, any subtype derived from
2611       --  Ada.Streams.Root_Stream_Type. Note that we guard the call to
2612       --  Has_Stream just for efficiency reasons. There is no point in
2613       --  spending time on a Has_Stream check if the restriction is not set.
2614
2615       if Restrictions.Set (No_Streams) then
2616          if Has_Stream (T) then
2617             Check_Restriction (No_Streams, N);
2618          end if;
2619       end if;
2620
2621       --  Abstract type is never permitted for a variable or constant.
2622       --  Note: we inhibit this check for objects that do not come from
2623       --  source because there is at least one case (the expansion of
2624       --  x'class'input where x is abstract) where we legitimately
2625       --  generate an abstract object.
2626
2627       if Is_Abstract_Type (T) and then Comes_From_Source (N) then
2628          Error_Msg_N ("type of object cannot be abstract",
2629                       Object_Definition (N));
2630
2631          if Is_CPP_Class (T) then
2632             Error_Msg_NE ("\} may need a cpp_constructor",
2633               Object_Definition (N), T);
2634          end if;
2635
2636       --  Case of unconstrained type
2637
2638       elsif Is_Indefinite_Subtype (T) then
2639
2640          --  Nothing to do in deferred constant case
2641
2642          if Constant_Present (N) and then No (E) then
2643             null;
2644
2645          --  Case of no initialization present
2646
2647          elsif No (E) then
2648             if No_Initialization (N) then
2649                null;
2650
2651             elsif Is_Class_Wide_Type (T) then
2652                Error_Msg_N
2653                  ("initialization required in class-wide declaration ", N);
2654
2655             else
2656                Error_Msg_N
2657                  ("unconstrained subtype not allowed (need initialization)",
2658                   Object_Definition (N));
2659
2660                if Is_Record_Type (T) and then Has_Discriminants (T) then
2661                   Error_Msg_N
2662                     ("\provide initial value or explicit discriminant values",
2663                      Object_Definition (N));
2664
2665                   Error_Msg_NE
2666                     ("\or give default discriminant values for type&",
2667                      Object_Definition (N), T);
2668
2669                elsif Is_Array_Type (T) then
2670                   Error_Msg_N
2671                     ("\provide initial value or explicit array bounds",
2672                      Object_Definition (N));
2673                end if;
2674             end if;
2675
2676          --  Case of initialization present but in error. Set initial
2677          --  expression as absent (but do not make above complaints)
2678
2679          elsif E = Error then
2680             Set_Expression (N, Empty);
2681             E := Empty;
2682
2683          --  Case of initialization present
2684
2685          else
2686             --  Not allowed in Ada 83
2687
2688             if not Constant_Present (N) then
2689                if Ada_Version = Ada_83
2690                  and then Comes_From_Source (Object_Definition (N))
2691                then
2692                   Error_Msg_N
2693                     ("(Ada 83) unconstrained variable not allowed",
2694                      Object_Definition (N));
2695                end if;
2696             end if;
2697
2698             --  Now we constrain the variable from the initializing expression
2699
2700             --  If the expression is an aggregate, it has been expanded into
2701             --  individual assignments. Retrieve the actual type from the
2702             --  expanded construct.
2703
2704             if Is_Array_Type (T)
2705               and then No_Initialization (N)
2706               and then Nkind (Original_Node (E)) = N_Aggregate
2707             then
2708                Act_T := Etype (E);
2709
2710             else
2711                Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
2712                Act_T := Find_Type_Of_Object (Object_Definition (N), N);
2713             end if;
2714
2715             Set_Is_Constr_Subt_For_U_Nominal (Act_T);
2716
2717             if Aliased_Present (N) then
2718                Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2719             end if;
2720
2721             Freeze_Before (N, Act_T);
2722             Freeze_Before (N, T);
2723          end if;
2724
2725       elsif Is_Array_Type (T)
2726         and then No_Initialization (N)
2727         and then Nkind (Original_Node (E)) = N_Aggregate
2728       then
2729          if not Is_Entity_Name (Object_Definition (N)) then
2730             Act_T := Etype (E);
2731             Check_Compile_Time_Size (Act_T);
2732
2733             if Aliased_Present (N) then
2734                Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2735             end if;
2736          end if;
2737
2738          --  When the given object definition and the aggregate are specified
2739          --  independently, and their lengths might differ do a length check.
2740          --  This cannot happen if the aggregate is of the form (others =>...)
2741
2742          if not Is_Constrained (T) then
2743             null;
2744
2745          elsif Nkind (E) = N_Raise_Constraint_Error then
2746
2747             --  Aggregate is statically illegal. Place back in declaration
2748
2749             Set_Expression (N, E);
2750             Set_No_Initialization (N, False);
2751
2752          elsif T = Etype (E) then
2753             null;
2754
2755          elsif Nkind (E) = N_Aggregate
2756            and then Present (Component_Associations (E))
2757            and then Present (Choices (First (Component_Associations (E))))
2758            and then Nkind (First
2759             (Choices (First (Component_Associations (E))))) = N_Others_Choice
2760          then
2761             null;
2762
2763          else
2764             Apply_Length_Check (E, T);
2765          end if;
2766
2767       --  If the type is limited unconstrained with defaulted discriminants
2768       --  and there is no expression, then the object is constrained by the
2769       --  defaults, so it is worthwhile building the corresponding subtype.
2770
2771       elsif (Is_Limited_Record (T)
2772                or else Is_Concurrent_Type (T))
2773         and then not Is_Constrained (T)
2774         and then Has_Discriminants (T)
2775       then
2776          if No (E) then
2777             Act_T := Build_Default_Subtype (T, N);
2778          else
2779             --  Ada 2005:  a limited object may be initialized by means of an
2780             --  aggregate. If the type has default discriminants it has an
2781             --  unconstrained nominal type, Its actual subtype will be obtained
2782             --  from the aggregate, and not from the default discriminants.
2783
2784             Act_T := Etype (E);
2785          end if;
2786
2787          Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
2788
2789       elsif Present (Underlying_Type (T))
2790         and then not Is_Constrained (Underlying_Type (T))
2791         and then Has_Discriminants (Underlying_Type (T))
2792         and then Nkind (E) = N_Function_Call
2793         and then Constant_Present (N)
2794       then
2795          --  The back-end has problems with constants of a discriminated type
2796          --  with defaults, if the initial value is a function call. We
2797          --  generate an intermediate temporary for the result of the call.
2798          --  It is unclear why this should make it acceptable to gcc. ???
2799
2800          Remove_Side_Effects (E);
2801       end if;
2802
2803       --  Check No_Wide_Characters restriction
2804
2805       if T = Standard_Wide_Character
2806         or else T = Standard_Wide_Wide_Character
2807         or else Root_Type (T) = Standard_Wide_String
2808         or else Root_Type (T) = Standard_Wide_Wide_String
2809       then
2810          Check_Restriction (No_Wide_Characters, Object_Definition (N));
2811       end if;
2812
2813       --  Indicate this is not set in source. Certainly true for constants,
2814       --  and true for variables so far (will be reset for a variable if and
2815       --  when we encounter a modification in the source).
2816
2817       Set_Never_Set_In_Source (Id, True);
2818
2819       --  Now establish the proper kind and type of the object
2820
2821       if Constant_Present (N) then
2822          Set_Ekind            (Id, E_Constant);
2823          Set_Is_True_Constant (Id, True);
2824
2825       else
2826          Set_Ekind (Id, E_Variable);
2827
2828          --  A variable is set as shared passive if it appears in a shared
2829          --  passive package, and is at the outer level. This is not done
2830          --  for entities generated during expansion, because those are
2831          --  always manipulated locally.
2832
2833          if Is_Shared_Passive (Current_Scope)
2834            and then Is_Library_Level_Entity (Id)
2835            and then Comes_From_Source (Id)
2836          then
2837             Set_Is_Shared_Passive (Id);
2838             Check_Shared_Var (Id, T, N);
2839          end if;
2840
2841          --  Set Has_Initial_Value if initializing expression present. Note
2842          --  that if there is no initializing expression, we leave the state
2843          --  of this flag unchanged (usually it will be False, but notably in
2844          --  the case of exception choice variables, it will already be true).
2845
2846          if Present (E) then
2847             Set_Has_Initial_Value (Id, True);
2848          end if;
2849       end if;
2850
2851       --  Initialize alignment and size and capture alignment setting
2852
2853       Init_Alignment               (Id);
2854       Init_Esize                   (Id);
2855       Set_Optimize_Alignment_Flags (Id);
2856
2857       --  Deal with aliased case
2858
2859       if Aliased_Present (N) then
2860          Set_Is_Aliased (Id);
2861
2862          --  If the object is aliased and the type is unconstrained with
2863          --  defaulted discriminants and there is no expression, then the
2864          --  object is constrained by the defaults, so it is worthwhile
2865          --  building the corresponding subtype.
2866
2867          --  Ada 2005 (AI-363): If the aliased object is discriminated and
2868          --  unconstrained, then only establish an actual subtype if the
2869          --  nominal subtype is indefinite. In definite cases the object is
2870          --  unconstrained in Ada 2005.
2871
2872          if No (E)
2873            and then Is_Record_Type (T)
2874            and then not Is_Constrained (T)
2875            and then Has_Discriminants (T)
2876            and then (Ada_Version < Ada_05 or else Is_Indefinite_Subtype (T))
2877          then
2878             Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
2879          end if;
2880       end if;
2881
2882       --  Now we can set the type of the object
2883
2884       Set_Etype (Id, Act_T);
2885
2886       --  Deal with controlled types
2887
2888       if Has_Controlled_Component (Etype (Id))
2889         or else Is_Controlled (Etype (Id))
2890       then
2891          if not Is_Library_Level_Entity (Id) then
2892             Check_Restriction (No_Nested_Finalization, N);
2893          else
2894             Validate_Controlled_Object (Id);
2895          end if;
2896
2897          --  Generate a warning when an initialization causes an obvious ABE
2898          --  violation. If the init expression is a simple aggregate there
2899          --  shouldn't be any initialize/adjust call generated. This will be
2900          --  true as soon as aggregates are built in place when possible.
2901
2902          --  ??? at the moment we do not generate warnings for temporaries
2903          --  created for those aggregates although Program_Error might be
2904          --  generated if compiled with -gnato.
2905
2906          if Is_Controlled (Etype (Id))
2907             and then Comes_From_Source (Id)
2908          then
2909             declare
2910                BT : constant Entity_Id := Base_Type (Etype (Id));
2911
2912                Implicit_Call : Entity_Id;
2913                pragma Warnings (Off, Implicit_Call);
2914                --  ??? what is this for (never referenced!)
2915
2916                function Is_Aggr (N : Node_Id) return Boolean;
2917                --  Check that N is an aggregate
2918
2919                -------------
2920                -- Is_Aggr --
2921                -------------
2922
2923                function Is_Aggr (N : Node_Id) return Boolean is
2924                begin
2925                   case Nkind (Original_Node (N)) is
2926                      when N_Aggregate | N_Extension_Aggregate =>
2927                         return True;
2928
2929                      when N_Qualified_Expression |
2930                           N_Type_Conversion      |
2931                           N_Unchecked_Type_Conversion =>
2932                         return Is_Aggr (Expression (Original_Node (N)));
2933
2934                      when others =>
2935                         return False;
2936                   end case;
2937                end Is_Aggr;
2938
2939             begin
2940                --  If no underlying type, we already are in an error situation.
2941                --  Do not try to add a warning since we do not have access to
2942                --  prim-op list.
2943
2944                if No (Underlying_Type (BT)) then
2945                   Implicit_Call := Empty;
2946
2947                --  A generic type does not have usable primitive operators.
2948                --  Initialization calls are built for instances.
2949
2950                elsif Is_Generic_Type (BT) then
2951                   Implicit_Call := Empty;
2952
2953                --  If the init expression is not an aggregate, an adjust call
2954                --  will be generated
2955
2956                elsif Present (E) and then not Is_Aggr (E) then
2957                   Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
2958
2959                --  If no init expression and we are not in the deferred
2960                --  constant case, an Initialize call will be generated
2961
2962                elsif No (E) and then not Constant_Present (N) then
2963                   Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
2964
2965                else
2966                   Implicit_Call := Empty;
2967                end if;
2968             end;
2969          end if;
2970       end if;
2971
2972       if Has_Task (Etype (Id)) then
2973          Check_Restriction (No_Tasking, N);
2974
2975          --  Deal with counting max tasks
2976
2977          --  Nothing to do if inside a generic
2978
2979          if Inside_A_Generic then
2980             null;
2981
2982          --  If library level entity, then count tasks
2983
2984          elsif Is_Library_Level_Entity (Id) then
2985             Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
2986
2987          --  If not library level entity, then indicate we don't know max
2988          --  tasks and also check task hierarchy restriction and blocking
2989          --  operation (since starting a task is definitely blocking!)
2990
2991          else
2992             Check_Restriction (Max_Tasks, N);
2993             Check_Restriction (No_Task_Hierarchy, N);
2994             Check_Potentially_Blocking_Operation (N);
2995          end if;
2996
2997          --  A rather specialized test. If we see two tasks being declared
2998          --  of the same type in the same object declaration, and the task
2999          --  has an entry with an address clause, we know that program error
3000          --  will be raised at run-time since we can't have two tasks with
3001          --  entries at the same address.
3002
3003          if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
3004             declare
3005                E : Entity_Id;
3006
3007             begin
3008                E := First_Entity (Etype (Id));
3009                while Present (E) loop
3010                   if Ekind (E) = E_Entry
3011                     and then Present (Get_Attribute_Definition_Clause
3012                                         (E, Attribute_Address))
3013                   then
3014                      Error_Msg_N
3015                        ("?more than one task with same entry address", N);
3016                      Error_Msg_N
3017                        ("\?Program_Error will be raised at run time", N);
3018                      Insert_Action (N,
3019                        Make_Raise_Program_Error (Loc,
3020                          Reason => PE_Duplicated_Entry_Address));
3021                      exit;
3022                   end if;
3023
3024                   Next_Entity (E);
3025                end loop;
3026             end;
3027          end if;
3028       end if;
3029
3030       --  Some simple constant-propagation: if the expression is a constant
3031       --  string initialized with a literal, share the literal. This avoids
3032       --  a run-time copy.
3033
3034       if Present (E)
3035         and then Is_Entity_Name (E)
3036         and then Ekind (Entity (E)) = E_Constant
3037         and then Base_Type (Etype (E)) = Standard_String
3038       then
3039          declare
3040             Val : constant Node_Id := Constant_Value (Entity (E));
3041          begin
3042             if Present (Val)
3043               and then Nkind (Val) = N_String_Literal
3044             then
3045                Rewrite (E, New_Copy (Val));
3046             end if;
3047          end;
3048       end if;
3049
3050       --  Another optimization: if the nominal subtype is unconstrained and
3051       --  the expression is a function call that returns an unconstrained
3052       --  type, rewrite the declaration as a renaming of the result of the
3053       --  call. The exceptions below are cases where the copy is expected,
3054       --  either by the back end (Aliased case) or by the semantics, as for
3055       --  initializing controlled types or copying tags for classwide types.
3056
3057       if Present (E)
3058         and then Nkind (E) = N_Explicit_Dereference
3059         and then Nkind (Original_Node (E)) = N_Function_Call
3060         and then not Is_Library_Level_Entity (Id)
3061         and then not Is_Constrained (Underlying_Type (T))
3062         and then not Is_Aliased (Id)
3063         and then not Is_Class_Wide_Type (T)
3064         and then not Is_Controlled (T)
3065         and then not Has_Controlled_Component (Base_Type (T))
3066         and then Expander_Active
3067       then
3068          Rewrite (N,
3069            Make_Object_Renaming_Declaration (Loc,
3070              Defining_Identifier => Id,
3071              Access_Definition   => Empty,
3072              Subtype_Mark        => New_Occurrence_Of
3073                                       (Base_Type (Etype (Id)), Loc),
3074              Name                => E));
3075
3076          Set_Renamed_Object (Id, E);
3077
3078          --  Force generation of debugging information for the constant and for
3079          --  the renamed function call.
3080
3081          Set_Debug_Info_Needed (Id);
3082          Set_Debug_Info_Needed (Entity (Prefix (E)));
3083       end if;
3084
3085       if Present (Prev_Entity)
3086         and then Is_Frozen (Prev_Entity)
3087         and then not Error_Posted (Id)
3088       then
3089          Error_Msg_N ("full constant declaration appears too late", N);
3090       end if;
3091
3092       Check_Eliminated (Id);
3093
3094       --  Deal with setting In_Private_Part flag if in private part
3095
3096       if Ekind (Scope (Id)) = E_Package
3097         and then In_Private_Part (Scope (Id))
3098       then
3099          Set_In_Private_Part (Id);
3100       end if;
3101
3102       --  Check for violation of No_Local_Timing_Events
3103
3104       if Is_RTE (Etype (Id), RE_Timing_Event)
3105         and then not Is_Library_Level_Entity (Id)
3106       then
3107          Check_Restriction (No_Local_Timing_Events, N);
3108       end if;
3109    end Analyze_Object_Declaration;
3110
3111    ---------------------------
3112    -- Analyze_Others_Choice --
3113    ---------------------------
3114
3115    --  Nothing to do for the others choice node itself, the semantic analysis
3116    --  of the others choice will occur as part of the processing of the parent
3117
3118    procedure Analyze_Others_Choice (N : Node_Id) is
3119       pragma Warnings (Off, N);
3120    begin
3121       null;
3122    end Analyze_Others_Choice;
3123
3124    -------------------------------------------
3125    -- Analyze_Private_Extension_Declaration --
3126    -------------------------------------------
3127
3128    procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
3129       T           : constant Entity_Id := Defining_Identifier (N);
3130       Indic       : constant Node_Id   := Subtype_Indication (N);
3131       Parent_Type : Entity_Id;
3132       Parent_Base : Entity_Id;
3133
3134    begin
3135       --  Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
3136
3137       if Is_Non_Empty_List (Interface_List (N)) then
3138          declare
3139             Intf : Node_Id;
3140             T    : Entity_Id;
3141
3142          begin
3143             Intf := First (Interface_List (N));
3144             while Present (Intf) loop
3145                T := Find_Type_Of_Subtype_Indic (Intf);
3146
3147                Diagnose_Interface (Intf, T);
3148                Next (Intf);
3149             end loop;
3150          end;
3151       end if;
3152
3153       Generate_Definition (T);
3154       Enter_Name (T);
3155
3156       Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
3157       Parent_Base := Base_Type (Parent_Type);
3158
3159       if Parent_Type = Any_Type
3160         or else Etype (Parent_Type) = Any_Type
3161       then
3162          Set_Ekind (T, Ekind (Parent_Type));
3163          Set_Etype (T, Any_Type);
3164          return;
3165
3166       elsif not Is_Tagged_Type (Parent_Type) then
3167          Error_Msg_N
3168            ("parent of type extension must be a tagged type ", Indic);
3169          return;
3170
3171       elsif Ekind (Parent_Type) = E_Void
3172         or else Ekind (Parent_Type) = E_Incomplete_Type
3173       then
3174          Error_Msg_N ("premature derivation of incomplete type", Indic);
3175          return;
3176
3177       elsif Is_Concurrent_Type (Parent_Type) then
3178          Error_Msg_N
3179            ("parent type of a private extension cannot be "
3180             & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
3181
3182          Set_Etype              (T, Any_Type);
3183          Set_Ekind              (T, E_Limited_Private_Type);
3184          Set_Private_Dependents (T, New_Elmt_List);
3185          Set_Error_Posted       (T);
3186          return;
3187       end if;
3188
3189       --  Perhaps the parent type should be changed to the class-wide type's
3190       --  specific type in this case to prevent cascading errors ???
3191
3192       if Is_Class_Wide_Type (Parent_Type) then
3193          Error_Msg_N
3194            ("parent of type extension must not be a class-wide type", Indic);
3195          return;
3196       end if;
3197
3198       if (not Is_Package_Or_Generic_Package (Current_Scope)
3199            and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
3200         or else In_Private_Part (Current_Scope)
3201
3202       then
3203          Error_Msg_N ("invalid context for private extension", N);
3204       end if;
3205
3206       --  Set common attributes
3207
3208       Set_Is_Pure          (T, Is_Pure (Current_Scope));
3209       Set_Scope            (T, Current_Scope);
3210       Set_Ekind            (T, E_Record_Type_With_Private);
3211       Init_Size_Align      (T);
3212
3213       Set_Etype            (T,            Parent_Base);
3214       Set_Has_Task         (T, Has_Task  (Parent_Base));
3215
3216       Set_Convention       (T, Convention     (Parent_Type));
3217       Set_First_Rep_Item   (T, First_Rep_Item (Parent_Type));
3218       Set_Is_First_Subtype (T);
3219       Make_Class_Wide_Type (T);
3220
3221       if Unknown_Discriminants_Present (N) then
3222          Set_Discriminant_Constraint (T, No_Elist);
3223       end if;
3224
3225       Build_Derived_Record_Type (N, Parent_Type, T);
3226
3227       --  Ada 2005 (AI-443): Synchronized private extension or a rewritten
3228       --  synchronized formal derived type.
3229
3230       if Ada_Version >= Ada_05
3231         and then Synchronized_Present (N)
3232       then
3233          Set_Is_Limited_Record (T);
3234
3235          --  Formal derived type case
3236
3237          if Is_Generic_Type (T) then
3238
3239             --  The parent must be a tagged limited type or a synchronized
3240             --  interface.
3241
3242             if (not Is_Tagged_Type (Parent_Type)
3243                   or else not Is_Limited_Type (Parent_Type))
3244               and then
3245                (not Is_Interface (Parent_Type)
3246                   or else not Is_Synchronized_Interface (Parent_Type))
3247             then
3248                Error_Msg_NE ("parent type of & must be tagged limited " &
3249                              "or synchronized", N, T);
3250             end if;
3251
3252             --  The progenitors (if any) must be limited or synchronized
3253             --  interfaces.
3254
3255             if Present (Interfaces (T)) then
3256                declare
3257                   Iface      : Entity_Id;
3258                   Iface_Elmt : Elmt_Id;
3259
3260                begin
3261                   Iface_Elmt := First_Elmt (Interfaces (T));
3262                   while Present (Iface_Elmt) loop
3263                      Iface := Node (Iface_Elmt);
3264
3265                      if not Is_Limited_Interface (Iface)
3266                        and then not Is_Synchronized_Interface (Iface)
3267                      then
3268                         Error_Msg_NE ("progenitor & must be limited " &
3269                                       "or synchronized", N, Iface);
3270                      end if;
3271
3272                      Next_Elmt (Iface_Elmt);
3273                   end loop;
3274                end;
3275             end if;
3276
3277          --  Regular derived extension, the parent must be a limited or
3278          --  synchronized interface.
3279
3280          else
3281             if not Is_Interface (Parent_Type)
3282               or else (not Is_Limited_Interface (Parent_Type)
3283                          and then
3284                        not Is_Synchronized_Interface (Parent_Type))
3285             then
3286                Error_Msg_NE
3287                  ("parent type of & must be limited interface", N, T);
3288             end if;
3289          end if;
3290
3291       elsif Limited_Present (N) then
3292          Set_Is_Limited_Record (T);
3293
3294          if not Is_Limited_Type (Parent_Type)
3295            and then
3296              (not Is_Interface (Parent_Type)
3297                or else not Is_Limited_Interface (Parent_Type))
3298          then
3299             Error_Msg_NE ("parent type& of limited extension must be limited",
3300               N, Parent_Type);
3301          end if;
3302       end if;
3303    end Analyze_Private_Extension_Declaration;
3304
3305    ---------------------------------
3306    -- Analyze_Subtype_Declaration --
3307    ---------------------------------
3308
3309    procedure Analyze_Subtype_Declaration
3310      (N    : Node_Id;
3311       Skip : Boolean := False)
3312    is
3313       Id       : constant Entity_Id := Defining_Identifier (N);
3314       T        : Entity_Id;
3315       R_Checks : Check_Result;
3316
3317    begin
3318       Generate_Definition (Id);
3319       Set_Is_Pure (Id, Is_Pure (Current_Scope));
3320       Init_Size_Align (Id);
3321
3322       --  The following guard condition on Enter_Name is to handle cases where
3323       --  the defining identifier has already been entered into the scope but
3324       --  the declaration as a whole needs to be analyzed.
3325
3326       --  This case in particular happens for derived enumeration types. The
3327       --  derived enumeration type is processed as an inserted enumeration type
3328       --  declaration followed by a rewritten subtype declaration. The defining
3329       --  identifier, however, is entered into the name scope very early in the
3330       --  processing of the original type declaration and therefore needs to be
3331       --  avoided here, when the created subtype declaration is analyzed. (See
3332       --  Build_Derived_Types)
3333
3334       --  This also happens when the full view of a private type is derived
3335       --  type with constraints. In this case the entity has been introduced
3336       --  in the private declaration.
3337
3338       if Skip
3339         or else (Present (Etype (Id))
3340                    and then (Is_Private_Type (Etype (Id))
3341                                or else Is_Task_Type (Etype (Id))
3342                                or else Is_Rewrite_Substitution (N)))
3343       then
3344          null;
3345
3346       else
3347          Enter_Name (Id);
3348       end if;
3349
3350       T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
3351
3352       --  Inherit common attributes
3353
3354       Set_Is_Generic_Type   (Id, Is_Generic_Type   (Base_Type (T)));
3355       Set_Is_Volatile       (Id, Is_Volatile       (T));
3356       Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
3357       Set_Is_Atomic         (Id, Is_Atomic         (T));
3358       Set_Is_Ada_2005_Only  (Id, Is_Ada_2005_Only  (T));
3359       Set_Convention        (Id, Convention        (T));
3360
3361       --  In the case where there is no constraint given in the subtype
3362       --  indication, Process_Subtype just returns the Subtype_Mark, so its
3363       --  semantic attributes must be established here.
3364
3365       if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
3366          Set_Etype (Id, Base_Type (T));
3367
3368          case Ekind (T) is
3369             when Array_Kind =>
3370                Set_Ekind                       (Id, E_Array_Subtype);
3371                Copy_Array_Subtype_Attributes   (Id, T);
3372
3373             when Decimal_Fixed_Point_Kind =>
3374                Set_Ekind                (Id, E_Decimal_Fixed_Point_Subtype);
3375                Set_Digits_Value         (Id, Digits_Value       (T));
3376                Set_Delta_Value          (Id, Delta_Value        (T));
3377                Set_Scale_Value          (Id, Scale_Value        (T));
3378                Set_Small_Value          (Id, Small_Value        (T));
3379                Set_Scalar_Range         (Id, Scalar_Range       (T));
3380                Set_Machine_Radix_10     (Id, Machine_Radix_10   (T));
3381                Set_Is_Constrained       (Id, Is_Constrained     (T));
3382                Set_RM_Size              (Id, RM_Size            (T));
3383
3384             when Enumeration_Kind =>
3385                Set_Ekind                (Id, E_Enumeration_Subtype);
3386                Set_First_Literal        (Id, First_Literal (Base_Type (T)));
3387                Set_Scalar_Range         (Id, Scalar_Range       (T));
3388                Set_Is_Character_Type    (Id, Is_Character_Type  (T));
3389                Set_Is_Constrained       (Id, Is_Constrained     (T));
3390                Set_RM_Size              (Id, RM_Size            (T));
3391
3392             when Ordinary_Fixed_Point_Kind =>
3393                Set_Ekind                (Id, E_Ordinary_Fixed_Point_Subtype);
3394                Set_Scalar_Range         (Id, Scalar_Range       (T));
3395                Set_Small_Value          (Id, Small_Value        (T));
3396                Set_Delta_Value          (Id, Delta_Value        (T));
3397                Set_Is_Constrained       (Id, Is_Constrained     (T));
3398                Set_RM_Size              (Id, RM_Size            (T));
3399
3400             when Float_Kind =>
3401                Set_Ekind                (Id, E_Floating_Point_Subtype);
3402                Set_Scalar_Range         (Id, Scalar_Range       (T));
3403                Set_Digits_Value         (Id, Digits_Value       (T));
3404                Set_Is_Constrained       (Id, Is_Constrained     (T));
3405
3406             when Signed_Integer_Kind =>
3407                Set_Ekind                (Id, E_Signed_Integer_Subtype);
3408                Set_Scalar_Range         (Id, Scalar_Range       (T));
3409                Set_Is_Constrained       (Id, Is_Constrained     (T));
3410                Set_RM_Size              (Id, RM_Size            (T));
3411
3412             when Modular_Integer_Kind =>
3413                Set_Ekind                (Id, E_Modular_Integer_Subtype);
3414                Set_Scalar_Range         (Id, Scalar_Range       (T));
3415                Set_Is_Constrained       (Id, Is_Constrained     (T));
3416                Set_RM_Size              (Id, RM_Size            (T));
3417
3418             when Class_Wide_Kind =>
3419                Set_Ekind                (Id, E_Class_Wide_Subtype);
3420                Set_First_Entity         (Id, First_Entity       (T));
3421                Set_Last_Entity          (Id, Last_Entity        (T));
3422                Set_Class_Wide_Type      (Id, Class_Wide_Type    (T));
3423                Set_Cloned_Subtype       (Id, T);
3424                Set_Is_Tagged_Type       (Id, True);
3425                Set_Has_Unknown_Discriminants
3426                                         (Id, True);
3427
3428                if Ekind (T) = E_Class_Wide_Subtype then
3429                   Set_Equivalent_Type   (Id, Equivalent_Type    (T));
3430                end if;
3431
3432             when E_Record_Type | E_Record_Subtype =>
3433                Set_Ekind                (Id, E_Record_Subtype);
3434
3435                if Ekind (T) = E_Record_Subtype
3436                  and then Present (Cloned_Subtype (T))
3437                then
3438                   Set_Cloned_Subtype    (Id, Cloned_Subtype (T));
3439                else
3440                   Set_Cloned_Subtype    (Id, T);
3441                end if;
3442
3443                Set_First_Entity         (Id, First_Entity       (T));
3444                Set_Last_Entity          (Id, Last_Entity        (T));
3445                Set_Has_Discriminants    (Id, Has_Discriminants  (T));
3446                Set_Is_Constrained       (Id, Is_Constrained     (T));
3447                Set_Is_Limited_Record    (Id, Is_Limited_Record  (T));
3448                Set_Has_Unknown_Discriminants
3449                                         (Id, Has_Unknown_Discriminants (T));
3450
3451                if Has_Discriminants (T) then
3452                   Set_Discriminant_Constraint
3453                                         (Id, Discriminant_Constraint (T));
3454                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3455
3456                elsif Has_Unknown_Discriminants (Id) then
3457                   Set_Discriminant_Constraint (Id, No_Elist);
3458                end if;
3459
3460                if Is_Tagged_Type (T) then
3461                   Set_Is_Tagged_Type    (Id);
3462                   Set_Is_Abstract_Type  (Id, Is_Abstract_Type (T));
3463                   Set_Primitive_Operations
3464                                         (Id, Primitive_Operations (T));
3465                   Set_Class_Wide_Type   (Id, Class_Wide_Type (T));
3466
3467                   if Is_Interface (T) then
3468                      Set_Is_Interface (Id);
3469                      Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
3470                   end if;
3471                end if;
3472
3473             when Private_Kind =>
3474                Set_Ekind              (Id, Subtype_Kind (Ekind   (T)));
3475                Set_Has_Discriminants  (Id, Has_Discriminants     (T));
3476                Set_Is_Constrained     (Id, Is_Constrained        (T));
3477                Set_First_Entity       (Id, First_Entity          (T));
3478                Set_Last_Entity        (Id, Last_Entity           (T));
3479                Set_Private_Dependents (Id, New_Elmt_List);
3480                Set_Is_Limited_Record  (Id, Is_Limited_Record     (T));
3481                Set_Has_Unknown_Discriminants
3482                                       (Id, Has_Unknown_Discriminants (T));
3483                Set_Known_To_Have_Preelab_Init
3484                                       (Id, Known_To_Have_Preelab_Init (T));
3485
3486                if Is_Tagged_Type (T) then
3487                   Set_Is_Tagged_Type       (Id);
3488                   Set_Is_Abstract_Type     (Id, Is_Abstract_Type (T));
3489                   Set_Primitive_Operations (Id, Primitive_Operations (T));
3490                   Set_Class_Wide_Type      (Id, Class_Wide_Type (T));
3491                end if;
3492
3493                --  In general the attributes of the subtype of a private type
3494                --  are the attributes of the partial view of parent. However,
3495                --  the full view may be a discriminated type, and the subtype
3496                --  must share the discriminant constraint to generate correct
3497                --  calls to initialization procedures.
3498
3499                if Has_Discriminants (T) then
3500                   Set_Discriminant_Constraint
3501                                      (Id, Discriminant_Constraint (T));
3502                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3503
3504                elsif Present (Full_View (T))
3505                  and then Has_Discriminants (Full_View (T))
3506                then
3507                   Set_Discriminant_Constraint
3508                                (Id, Discriminant_Constraint (Full_View (T)));
3509                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3510
3511                   --  This would seem semantically correct, but apparently
3512                   --  confuses the back-end. To be explained and checked with
3513                   --  current version ???
3514
3515                   --  Set_Has_Discriminants (Id);
3516                end if;
3517
3518                Prepare_Private_Subtype_Completion (Id, N);
3519
3520             when Access_Kind =>
3521                Set_Ekind             (Id, E_Access_Subtype);
3522                Set_Is_Constrained    (Id, Is_Constrained        (T));
3523                Set_Is_Access_Constant
3524                                      (Id, Is_Access_Constant    (T));
3525                Set_Directly_Designated_Type
3526                                      (Id, Designated_Type       (T));
3527                Set_Can_Never_Be_Null (Id, Can_Never_Be_Null     (T));
3528
3529                --  A Pure library_item must not contain the declaration of a
3530                --  named access type, except within a subprogram, generic
3531                --  subprogram, task unit, or protected unit (RM 10.2.1(16)).
3532
3533                if Comes_From_Source (Id)
3534                  and then In_Pure_Unit
3535                  and then not In_Subprogram_Task_Protected_Unit
3536                then
3537                   Error_Msg_N
3538                     ("named access types not allowed in pure unit", N);
3539                end if;
3540
3541             when Concurrent_Kind =>
3542                Set_Ekind                (Id, Subtype_Kind (Ekind   (T)));
3543                Set_Corresponding_Record_Type (Id,
3544                                          Corresponding_Record_Type (T));
3545                Set_First_Entity         (Id, First_Entity          (T));
3546                Set_First_Private_Entity (Id, First_Private_Entity  (T));
3547                Set_Has_Discriminants    (Id, Has_Discriminants     (T));
3548                Set_Is_Constrained       (Id, Is_Constrained        (T));
3549                Set_Is_Tagged_Type       (Id, Is_Tagged_Type        (T));
3550                Set_Last_Entity          (Id, Last_Entity           (T));
3551
3552                if Has_Discriminants (T) then
3553                   Set_Discriminant_Constraint (Id,
3554                                            Discriminant_Constraint (T));
3555                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3556                end if;
3557
3558             when E_Incomplete_Type =>
3559                if Ada_Version >= Ada_05 then
3560                   Set_Ekind (Id, E_Incomplete_Subtype);
3561
3562                   --  Ada 2005 (AI-412): Decorate an incomplete subtype
3563                   --  of an incomplete type visible through a limited
3564                   --  with clause.
3565
3566                   if From_With_Type (T)
3567                     and then Present (Non_Limited_View (T))
3568                   then
3569                      Set_From_With_Type   (Id);
3570                      Set_Non_Limited_View (Id, Non_Limited_View (T));
3571
3572                   --  Ada 2005 (AI-412): Add the regular incomplete subtype
3573                   --  to the private dependents of the original incomplete
3574                   --  type for future transformation.
3575
3576                   else
3577                      Append_Elmt (Id, Private_Dependents (T));
3578                   end if;
3579
3580                --  If the subtype name denotes an incomplete type an error
3581                --  was already reported by Process_Subtype.
3582
3583                else
3584                   Set_Etype (Id, Any_Type);
3585                end if;
3586
3587             when others =>
3588                raise Program_Error;
3589          end case;
3590       end if;
3591
3592       if Etype (Id) = Any_Type then
3593          return;
3594       end if;
3595
3596       --  Some common processing on all types
3597
3598       Set_Size_Info      (Id,                 T);
3599       Set_First_Rep_Item (Id, First_Rep_Item (T));
3600
3601       T := Etype (Id);
3602
3603       Set_Is_Immediately_Visible   (Id, True);
3604       Set_Depends_On_Private       (Id, Has_Private_Component (T));
3605       Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
3606
3607       if Is_Interface (T) then
3608          Set_Is_Interface (Id);
3609       end if;
3610
3611       if Present (Generic_Parent_Type (N))
3612         and then
3613           (Nkind
3614              (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
3615             or else Nkind
3616               (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
3617                 /=  N_Formal_Private_Type_Definition)
3618       then
3619          if Is_Tagged_Type (Id) then
3620
3621             --  If this is a generic actual subtype for a synchronized type,
3622             --  the primitive operations are those of the corresponding record
3623             --  for which there is a separate subtype declaration.
3624
3625             if Is_Concurrent_Type (Id) then
3626                null;
3627             elsif Is_Class_Wide_Type (Id) then
3628                Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
3629             else
3630                Derive_Subprograms (Generic_Parent_Type (N), Id, T);
3631             end if;
3632
3633          elsif Scope (Etype (Id)) /= Standard_Standard then
3634             Derive_Subprograms (Generic_Parent_Type (N), Id);
3635          end if;
3636       end if;
3637
3638       if Is_Private_Type (T)
3639         and then Present (Full_View (T))
3640       then
3641          Conditional_Delay (Id, Full_View (T));
3642
3643       --  The subtypes of components or subcomponents of protected types
3644       --  do not need freeze nodes, which would otherwise appear in the
3645       --  wrong scope (before the freeze node for the protected type). The
3646       --  proper subtypes are those of the subcomponents of the corresponding
3647       --  record.
3648
3649       elsif Ekind (Scope (Id)) /= E_Protected_Type
3650         and then Present (Scope (Scope (Id))) -- error defense!
3651         and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
3652       then
3653          Conditional_Delay (Id, T);
3654       end if;
3655
3656       --  Check that constraint_error is raised for a scalar subtype
3657       --  indication when the lower or upper bound of a non-null range
3658       --  lies outside the range of the type mark.
3659
3660       if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
3661          if Is_Scalar_Type (Etype (Id))
3662             and then Scalar_Range (Id) /=
3663                      Scalar_Range (Etype (Subtype_Mark
3664                                            (Subtype_Indication (N))))
3665          then
3666             Apply_Range_Check
3667               (Scalar_Range (Id),
3668                Etype (Subtype_Mark (Subtype_Indication (N))));
3669
3670          elsif Is_Array_Type (Etype (Id))
3671            and then Present (First_Index (Id))
3672          then
3673             --  This really should be a subprogram that finds the indications
3674             --  to check???
3675
3676             if ((Nkind (First_Index (Id)) = N_Identifier
3677                    and then Ekind (Entity (First_Index (Id))) in Scalar_Kind)
3678                  or else Nkind (First_Index (Id)) = N_Subtype_Indication)
3679               and then
3680                 Nkind (Scalar_Range (Etype (First_Index (Id)))) = N_Range
3681             then
3682                declare
3683                   Target_Typ : constant Entity_Id :=
3684                                  Etype
3685                                    (First_Index (Etype
3686                                      (Subtype_Mark (Subtype_Indication (N)))));
3687                begin
3688                   R_Checks :=
3689                     Get_Range_Checks
3690                       (Scalar_Range (Etype (First_Index (Id))),
3691                        Target_Typ,
3692                        Etype (First_Index (Id)),
3693                        Defining_Identifier (N));
3694
3695                   Insert_Range_Checks
3696                     (R_Checks,
3697                      N,
3698                      Target_Typ,
3699                      Sloc (Defining_Identifier (N)));
3700                end;
3701             end if;
3702          end if;
3703       end if;
3704
3705       Set_Optimize_Alignment_Flags (Id);
3706       Check_Eliminated (Id);
3707    end Analyze_Subtype_Declaration;
3708
3709    --------------------------------
3710    -- Analyze_Subtype_Indication --
3711    --------------------------------
3712
3713    procedure Analyze_Subtype_Indication (N : Node_Id) is
3714       T : constant Entity_Id := Subtype_Mark (N);
3715       R : constant Node_Id   := Range_Expression (Constraint (N));
3716
3717    begin
3718       Analyze (T);
3719
3720       if R /= Error then
3721          Analyze (R);
3722          Set_Etype (N, Etype (R));
3723          Resolve (R, Entity (T));
3724       else
3725          Set_Error_Posted (R);
3726          Set_Error_Posted (T);
3727       end if;
3728    end Analyze_Subtype_Indication;
3729
3730    ------------------------------
3731    -- Analyze_Type_Declaration --
3732    ------------------------------
3733
3734    procedure Analyze_Type_Declaration (N : Node_Id) is
3735       Def    : constant Node_Id   := Type_Definition (N);
3736       Def_Id : constant Entity_Id := Defining_Identifier (N);
3737       T      : Entity_Id;
3738       Prev   : Entity_Id;
3739
3740       Is_Remote : constant Boolean :=
3741                     (Is_Remote_Types (Current_Scope)
3742                        or else Is_Remote_Call_Interface (Current_Scope))
3743                     and then not (In_Private_Part (Current_Scope)
3744                                     or else In_Package_Body (Current_Scope));
3745
3746       procedure Check_Ops_From_Incomplete_Type;
3747       --  If there is a tagged incomplete partial view of the type, transfer
3748       --  its operations to the full view, and indicate that the type of the
3749       --  controlling parameter (s) is this full view.
3750
3751       ------------------------------------
3752       -- Check_Ops_From_Incomplete_Type --
3753       ------------------------------------
3754
3755       procedure Check_Ops_From_Incomplete_Type is
3756          Elmt   : Elmt_Id;
3757          Formal : Entity_Id;
3758          Op     : Entity_Id;
3759
3760       begin
3761          if Prev /= T
3762            and then Ekind (Prev) = E_Incomplete_Type
3763            and then Is_Tagged_Type (Prev)
3764            and then Is_Tagged_Type (T)
3765          then
3766             Elmt := First_Elmt (Primitive_Operations (Prev));
3767             while Present (Elmt) loop
3768                Op := Node (Elmt);
3769                Prepend_Elmt (Op, Primitive_Operations (T));
3770
3771                Formal := First_Formal (Op);
3772                while Present (Formal) loop
3773                   if Etype (Formal) = Prev then
3774                      Set_Etype (Formal, T);
3775                   end if;
3776
3777                   Next_Formal (Formal);
3778                end loop;
3779
3780                if Etype (Op) = Prev then
3781                   Set_Etype (Op, T);
3782                end if;
3783
3784                Next_Elmt (Elmt);
3785             end loop;
3786          end if;
3787       end Check_Ops_From_Incomplete_Type;
3788
3789    --  Start of processing for Analyze_Type_Declaration
3790
3791    begin
3792       Prev := Find_Type_Name (N);
3793
3794       --  The full view, if present, now points to the current type
3795
3796       --  Ada 2005 (AI-50217): If the type was previously decorated when
3797       --  imported through a LIMITED WITH clause, it appears as incomplete
3798       --  but has no full view.
3799       --  If the incomplete view is tagged, a class_wide type has been
3800       --  created already. Use it for the full view as well, to prevent
3801       --  multiple incompatible class-wide types that may be  created for
3802       --  self-referential anonymous access components.
3803
3804       if Ekind (Prev) = E_Incomplete_Type
3805         and then Present (Full_View (Prev))
3806       then
3807          T := Full_View (Prev);
3808
3809          if Is_Tagged_Type (Prev)
3810            and then Present (Class_Wide_Type (Prev))
3811          then
3812             Set_Ekind (T, Ekind (Prev));         --  will be reset later
3813             Set_Class_Wide_Type (T, Class_Wide_Type (Prev));
3814             Set_Etype (Class_Wide_Type (T), T);
3815          end if;
3816
3817       else
3818          T := Prev;
3819       end if;
3820
3821       Set_Is_Pure (T, Is_Pure (Current_Scope));
3822
3823       --  We set the flag Is_First_Subtype here. It is needed to set the
3824       --  corresponding flag for the Implicit class-wide-type created
3825       --  during tagged types processing.
3826
3827       Set_Is_First_Subtype (T, True);
3828
3829       --  Only composite types other than array types are allowed to have
3830       --  discriminants.
3831
3832       case Nkind (Def) is
3833
3834          --  For derived types, the rule will be checked once we've figured
3835          --  out the parent type.
3836
3837          when N_Derived_Type_Definition =>
3838             null;
3839
3840          --  For record types, discriminants are allowed
3841
3842          when N_Record_Definition =>
3843             null;
3844
3845          when others =>
3846             if Present (Discriminant_Specifications (N)) then
3847                Error_Msg_N
3848                  ("elementary or array type cannot have discriminants",
3849                   Defining_Identifier
3850                   (First (Discriminant_Specifications (N))));
3851             end if;
3852       end case;
3853
3854       --  Elaborate the type definition according to kind, and generate
3855       --  subsidiary (implicit) subtypes where needed. We skip this if it was
3856       --  already done (this happens during the reanalysis that follows a call
3857       --  to the high level optimizer).
3858
3859       if not Analyzed (T) then
3860          Set_Analyzed (T);
3861
3862          case Nkind (Def) is
3863
3864             when N_Access_To_Subprogram_Definition =>
3865                Access_Subprogram_Declaration (T, Def);
3866
3867                --  If this is a remote access to subprogram, we must create the
3868                --  equivalent fat pointer type, and related subprograms.
3869
3870                if Is_Remote then
3871                   Process_Remote_AST_Declaration (N);
3872                end if;
3873
3874                --  Validate categorization rule against access type declaration
3875                --  usually a violation in Pure unit, Shared_Passive unit.
3876
3877                Validate_Access_Type_Declaration (T, N);
3878
3879             when N_Access_To_Object_Definition =>
3880                Access_Type_Declaration (T, Def);
3881
3882                --  Validate categorization rule against access type declaration
3883                --  usually a violation in Pure unit, Shared_Passive unit.
3884
3885                Validate_Access_Type_Declaration (T, N);
3886
3887                --  If we are in a Remote_Call_Interface package and define a
3888                --  RACW, then calling stubs and specific stream attributes
3889                --  must be added.
3890
3891                if Is_Remote
3892                  and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3893                then
3894                   Add_RACW_Features (Def_Id);
3895                end if;
3896
3897                --  Set no strict aliasing flag if config pragma seen
3898
3899                if Opt.No_Strict_Aliasing then
3900                   Set_No_Strict_Aliasing (Base_Type (Def_Id));
3901                end if;
3902
3903             when N_Array_Type_Definition =>
3904                Array_Type_Declaration (T, Def);
3905
3906             when N_Derived_Type_Definition =>
3907                Derived_Type_Declaration (T, N, T /= Def_Id);
3908
3909             when N_Enumeration_Type_Definition =>
3910                Enumeration_Type_Declaration (T, Def);
3911
3912             when N_Floating_Point_Definition =>
3913                Floating_Point_Type_Declaration (T, Def);
3914
3915             when N_Decimal_Fixed_Point_Definition =>
3916                Decimal_Fixed_Point_Type_Declaration (T, Def);
3917
3918             when N_Ordinary_Fixed_Point_Definition =>
3919                Ordinary_Fixed_Point_Type_Declaration (T, Def);
3920
3921             when N_Signed_Integer_Type_Definition =>
3922                Signed_Integer_Type_Declaration (T, Def);
3923
3924             when N_Modular_Type_Definition =>
3925                Modular_Type_Declaration (T, Def);
3926
3927             when N_Record_Definition =>
3928                Record_Type_Declaration (T, N, Prev);
3929
3930             when others =>
3931                raise Program_Error;
3932
3933          end case;
3934       end if;
3935
3936       if Etype (T) = Any_Type then
3937          return;
3938       end if;
3939
3940       --  Some common processing for all types
3941
3942       Set_Depends_On_Private (T, Has_Private_Component (T));
3943       Check_Ops_From_Incomplete_Type;
3944
3945       --  Both the declared entity, and its anonymous base type if one
3946       --  was created, need freeze nodes allocated.
3947
3948       declare
3949          B : constant Entity_Id := Base_Type (T);
3950
3951       begin
3952          --  In the case where the base type differs from the first subtype, we
3953          --  pre-allocate a freeze node, and set the proper link to the first
3954          --  subtype. Freeze_Entity will use this preallocated freeze node when
3955          --  it freezes the entity.
3956
3957          if B /= T then
3958             Ensure_Freeze_Node (B);
3959             Set_First_Subtype_Link (Freeze_Node (B), T);
3960          end if;
3961
3962          if not From_With_Type (T) then
3963             Set_Has_Delayed_Freeze (T);
3964          end if;
3965       end;
3966
3967       --  Case of T is the full declaration of some private type which has
3968       --  been swapped in Defining_Identifier (N).
3969
3970       if T /= Def_Id and then Is_Private_Type (Def_Id) then
3971          Process_Full_View (N, T, Def_Id);
3972
3973          --  Record the reference. The form of this is a little strange, since
3974          --  the full declaration has been swapped in. So the first parameter
3975          --  here represents the entity to which a reference is made which is
3976          --  the "real" entity, i.e. the one swapped in, and the second
3977          --  parameter provides the reference location.
3978
3979          --  Also, we want to kill Has_Pragma_Unreferenced temporarily here
3980          --  since we don't want a complaint about the full type being an
3981          --  unwanted reference to the private type
3982
3983          declare
3984             B : constant Boolean := Has_Pragma_Unreferenced (T);
3985          begin
3986             Set_Has_Pragma_Unreferenced (T, False);
3987             Generate_Reference (T, T, 'c');
3988             Set_Has_Pragma_Unreferenced (T, B);
3989          end;
3990
3991          Set_Completion_Referenced (Def_Id);
3992
3993       --  For completion of incomplete type, process incomplete dependents
3994       --  and always mark the full type as referenced (it is the incomplete
3995       --  type that we get for any real reference).
3996
3997       elsif Ekind (Prev) = E_Incomplete_Type then
3998          Process_Incomplete_Dependents (N, T, Prev);
3999          Generate_Reference (Prev, Def_Id, 'c');
4000          Set_Completion_Referenced (Def_Id);
4001
4002       --  If not private type or incomplete type completion, this is a real
4003       --  definition of a new entity, so record it.
4004
4005       else
4006          Generate_Definition (Def_Id);
4007       end if;
4008
4009       if Chars (Scope (Def_Id)) =  Name_System
4010         and then Chars (Def_Id) = Name_Address
4011         and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
4012       then
4013          Set_Is_Descendent_Of_Address (Def_Id);
4014          Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
4015          Set_Is_Descendent_Of_Address (Prev);
4016       end if;
4017
4018       Set_Optimize_Alignment_Flags (Def_Id);
4019       Check_Eliminated (Def_Id);
4020    end Analyze_Type_Declaration;
4021
4022    --------------------------
4023    -- Analyze_Variant_Part --
4024    --------------------------
4025
4026    procedure Analyze_Variant_Part (N : Node_Id) is
4027
4028       procedure Non_Static_Choice_Error (Choice : Node_Id);
4029       --  Error routine invoked by the generic instantiation below when the
4030       --  variant part has a non static choice.
4031
4032       procedure Process_Declarations (Variant : Node_Id);
4033       --  Analyzes all the declarations associated with a Variant. Needed by
4034       --  the generic instantiation below.
4035
4036       package Variant_Choices_Processing is new
4037         Generic_Choices_Processing
4038           (Get_Alternatives          => Variants,
4039            Get_Choices               => Discrete_Choices,
4040            Process_Empty_Choice      => No_OP,
4041            Process_Non_Static_Choice => Non_Static_Choice_Error,
4042            Process_Associated_Node   => Process_Declarations);
4043       use Variant_Choices_Processing;
4044       --  Instantiation of the generic choice processing package
4045
4046       -----------------------------
4047       -- Non_Static_Choice_Error --
4048       -----------------------------
4049
4050       procedure Non_Static_Choice_Error (Choice : Node_Id) is
4051       begin
4052          Flag_Non_Static_Expr
4053            ("choice given in variant part is not static!", Choice);
4054       end Non_Static_Choice_Error;
4055
4056       --------------------------
4057       -- Process_Declarations --
4058       --------------------------
4059
4060       procedure Process_Declarations (Variant : Node_Id) is
4061       begin
4062          if not Null_Present (Component_List (Variant)) then
4063             Analyze_Declarations (Component_Items (Component_List (Variant)));
4064
4065             if Present (Variant_Part (Component_List (Variant))) then
4066                Analyze (Variant_Part (Component_List (Variant)));
4067             end if;
4068          end if;
4069       end Process_Declarations;
4070
4071       --  Local Variables
4072
4073       Discr_Name : Node_Id;
4074       Discr_Type : Entity_Id;
4075
4076       Case_Table     : Choice_Table_Type (1 .. Number_Of_Choices (N));
4077       Last_Choice    : Nat;
4078       Dont_Care      : Boolean;
4079       Others_Present : Boolean := False;
4080
4081       pragma Warnings (Off, Case_Table);
4082       pragma Warnings (Off, Last_Choice);
4083       pragma Warnings (Off, Dont_Care);
4084       pragma Warnings (Off, Others_Present);
4085       --  We don't care about the assigned values of any of these
4086
4087    --  Start of processing for Analyze_Variant_Part
4088
4089    begin
4090       Discr_Name := Name (N);
4091       Analyze (Discr_Name);
4092
4093       --  If Discr_Name bad, get out (prevent cascaded errors)
4094
4095       if Etype (Discr_Name) = Any_Type then
4096          return;
4097       end if;
4098
4099       --  Check invalid discriminant in variant part
4100
4101       if Ekind (Entity (Discr_Name)) /= E_Discriminant then
4102          Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
4103       end if;
4104
4105       Discr_Type := Etype (Entity (Discr_Name));
4106
4107       if not Is_Discrete_Type (Discr_Type) then
4108          Error_Msg_N
4109            ("discriminant in a variant part must be of a discrete type",
4110              Name (N));
4111          return;
4112       end if;
4113
4114       --  Call the instantiated Analyze_Choices which does the rest of the work
4115
4116       Analyze_Choices
4117         (N, Discr_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
4118    end Analyze_Variant_Part;
4119
4120    ----------------------------
4121    -- Array_Type_Declaration --
4122    ----------------------------
4123
4124    procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
4125       Component_Def : constant Node_Id := Component_Definition (Def);
4126       Element_Type  : Entity_Id;
4127       Implicit_Base : Entity_Id;
4128       Index         : Node_Id;
4129       Related_Id    : Entity_Id := Empty;
4130       Nb_Index      : Nat;
4131       P             : constant Node_Id := Parent (Def);
4132       Priv          : Entity_Id;
4133
4134    begin
4135       if Nkind (Def) = N_Constrained_Array_Definition then
4136          Index := First (Discrete_Subtype_Definitions (Def));
4137       else
4138          Index := First (Subtype_Marks (Def));
4139       end if;
4140
4141       --  Find proper names for the implicit types which may be public. In case
4142       --  of anonymous arrays we use the name of the first object of that type
4143       --  as prefix.
4144
4145       if No (T) then
4146          Related_Id :=  Defining_Identifier (P);
4147       else
4148          Related_Id := T;
4149       end if;
4150
4151       Nb_Index := 1;
4152       while Present (Index) loop
4153          Analyze (Index);
4154
4155          --  Add a subtype declaration for each index of private array type
4156          --  declaration whose etype is also private. For example:
4157
4158          --     package Pkg is
4159          --        type Index is private;
4160          --     private
4161          --        type Table is array (Index) of ...
4162          --     end;
4163
4164          --  This is currently required by the expander for the internally
4165          --  generated equality subprogram of records with variant parts in
4166          --  which the etype of some component is such private type.
4167
4168          if Ekind (Current_Scope) = E_Package
4169            and then In_Private_Part (Current_Scope)
4170            and then Has_Private_Declaration (Etype (Index))
4171          then
4172             declare
4173                Loc   : constant Source_Ptr := Sloc (Def);
4174                New_E : Entity_Id;
4175                Decl  : Entity_Id;
4176
4177             begin
4178                New_E :=
4179                  Make_Defining_Identifier (Loc,
4180                    Chars => New_Internal_Name ('T'));
4181                Set_Is_Internal (New_E);
4182
4183                Decl :=
4184                  Make_Subtype_Declaration (Loc,
4185                    Defining_Identifier => New_E,
4186                    Subtype_Indication  =>
4187                      New_Occurrence_Of (Etype (Index), Loc));
4188
4189                Insert_Before (Parent (Def), Decl);
4190                Analyze (Decl);
4191                Set_Etype (Index, New_E);
4192
4193                --  If the index is a range the Entity attribute is not
4194                --  available. Example:
4195
4196                --     package Pkg is
4197                --        type T is private;
4198                --     private
4199                --        type T is new Natural;
4200                --        Table : array (T(1) .. T(10)) of Boolean;
4201                --     end Pkg;
4202
4203                if Nkind (Index) /= N_Range then
4204                   Set_Entity (Index, New_E);
4205                end if;
4206             end;
4207          end if;
4208
4209          Make_Index (Index, P, Related_Id, Nb_Index);
4210          Next_Index (Index);
4211          Nb_Index := Nb_Index + 1;
4212       end loop;
4213
4214       --  Process subtype indication if one is present
4215
4216       if Present (Subtype_Indication (Component_Def)) then
4217          Element_Type :=
4218            Process_Subtype
4219              (Subtype_Indication (Component_Def), P, Related_Id, 'C');
4220
4221       --  Ada 2005 (AI-230): Access Definition case
4222
4223       else pragma Assert (Present (Access_Definition (Component_Def)));
4224
4225          --  Indicate that the anonymous access type is created by the
4226          --  array type declaration.
4227
4228          Element_Type := Access_Definition
4229                            (Related_Nod => P,
4230                             N           => Access_Definition (Component_Def));
4231          Set_Is_Local_Anonymous_Access (Element_Type);
4232
4233          --  Propagate the parent. This field is needed if we have to generate
4234          --  the master_id associated with an anonymous access to task type
4235          --  component (see Expand_N_Full_Type_Declaration.Build_Master)
4236
4237          Set_Parent (Element_Type, Parent (T));
4238
4239          --  Ada 2005 (AI-230): In case of components that are anonymous access
4240          --  types the level of accessibility depends on the enclosing type
4241          --  declaration
4242
4243          Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
4244
4245          --  Ada 2005 (AI-254)
4246
4247          declare
4248             CD : constant Node_Id :=
4249                    Access_To_Subprogram_Definition
4250                      (Access_Definition (Component_Def));
4251          begin
4252             if Present (CD) and then Protected_Present (CD) then
4253                Element_Type :=
4254                  Replace_Anonymous_Access_To_Protected_Subprogram (Def);
4255             end if;
4256          end;
4257       end if;
4258
4259       --  Constrained array case
4260
4261       if No (T) then
4262          T := Create_Itype (E_Void, P, Related_Id, 'T');
4263       end if;
4264
4265       if Nkind (Def) = N_Constrained_Array_Definition then
4266
4267          --  Establish Implicit_Base as unconstrained base type
4268
4269          Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
4270
4271          Set_Etype              (Implicit_Base, Implicit_Base);
4272          Set_Scope              (Implicit_Base, Current_Scope);
4273          Set_Has_Delayed_Freeze (Implicit_Base);
4274
4275          --  The constrained array type is a subtype of the unconstrained one
4276
4277          Set_Ekind          (T, E_Array_Subtype);
4278          Init_Size_Align    (T);
4279          Set_Etype          (T, Implicit_Base);
4280          Set_Scope          (T, Current_Scope);
4281          Set_Is_Constrained (T, True);
4282          Set_First_Index    (T, First (Discrete_Subtype_Definitions (Def)));
4283          Set_Has_Delayed_Freeze (T);
4284
4285          --  Complete setup of implicit base type
4286
4287          Set_First_Index       (Implicit_Base, First_Index (T));
4288          Set_Component_Type    (Implicit_Base, Element_Type);
4289          Set_Has_Task          (Implicit_Base, Has_Task (Element_Type));
4290          Set_Component_Size    (Implicit_Base, Uint_0);
4291          Set_Packed_Array_Type (Implicit_Base, Empty);
4292          Set_Has_Controlled_Component
4293                                (Implicit_Base, Has_Controlled_Component
4294                                                         (Element_Type)
4295                                                  or else Is_Controlled
4296                                                         (Element_Type));
4297          Set_Finalize_Storage_Only
4298                                (Implicit_Base, Finalize_Storage_Only
4299                                                         (Element_Type));
4300
4301       --  Unconstrained array case
4302
4303       else
4304          Set_Ekind                    (T, E_Array_Type);
4305          Init_Size_Align              (T);
4306          Set_Etype                    (T, T);
4307          Set_Scope                    (T, Current_Scope);
4308          Set_Component_Size           (T, Uint_0);
4309          Set_Is_Constrained           (T, False);
4310          Set_First_Index              (T, First (Subtype_Marks (Def)));
4311          Set_Has_Delayed_Freeze       (T, True);
4312          Set_Has_Task                 (T, Has_Task      (Element_Type));
4313          Set_Has_Controlled_Component (T, Has_Controlled_Component
4314                                                         (Element_Type)
4315                                             or else
4316                                           Is_Controlled (Element_Type));
4317          Set_Finalize_Storage_Only    (T, Finalize_Storage_Only
4318                                                         (Element_Type));
4319       end if;
4320
4321       --  Common attributes for both cases
4322
4323       Set_Component_Type (Base_Type (T), Element_Type);
4324       Set_Packed_Array_Type (T, Empty);
4325
4326       if Aliased_Present (Component_Definition (Def)) then
4327          Set_Has_Aliased_Components (Etype (T));
4328       end if;
4329
4330       --  Ada 2005 (AI-231): Propagate the null-excluding attribute to the
4331       --  array type to ensure that objects of this type are initialized.
4332
4333       if Ada_Version >= Ada_05
4334         and then Can_Never_Be_Null (Element_Type)
4335       then
4336          Set_Can_Never_Be_Null (T);
4337
4338          if Null_Exclusion_Present (Component_Definition (Def))
4339
4340             --  No need to check itypes because in their case this check was
4341             --  done at their point of creation
4342
4343            and then not Is_Itype (Element_Type)
4344          then
4345             Error_Msg_N
4346               ("`NOT NULL` not allowed (null already excluded)",
4347                Subtype_Indication (Component_Definition (Def)));
4348          end if;
4349       end if;
4350
4351       Priv := Private_Component (Element_Type);
4352
4353       if Present (Priv) then
4354
4355          --  Check for circular definitions
4356
4357          if Priv = Any_Type then
4358             Set_Component_Type (Etype (T), Any_Type);
4359
4360          --  There is a gap in the visibility of operations on the composite
4361          --  type only if the component type is defined in a different scope.
4362
4363          elsif Scope (Priv) = Current_Scope then
4364             null;
4365
4366          elsif Is_Limited_Type (Priv) then
4367             Set_Is_Limited_Composite (Etype (T));
4368             Set_Is_Limited_Composite (T);
4369          else
4370             Set_Is_Private_Composite (Etype (T));
4371             Set_Is_Private_Composite (T);
4372          end if;
4373       end if;
4374
4375       --  A syntax error in the declaration itself may lead to an empty index
4376       --  list, in which case do a minimal patch.
4377
4378       if No (First_Index (T)) then
4379          Error_Msg_N ("missing index definition in array type declaration", T);
4380
4381          declare
4382             Indices : constant List_Id :=
4383                         New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
4384          begin
4385             Set_Discrete_Subtype_Definitions (Def, Indices);
4386             Set_First_Index (T, First (Indices));
4387             return;
4388          end;
4389       end if;
4390
4391       --  Create a concatenation operator for the new type. Internal array
4392       --  types created for packed entities do not need such, they are
4393       --  compatible with the user-defined type.
4394
4395       if Number_Dimensions (T) = 1
4396          and then not Is_Packed_Array_Type (T)
4397       then
4398          New_Concatenation_Op (T);
4399       end if;
4400
4401       --  In the case of an unconstrained array the parser has already verified
4402       --  that all the indices are unconstrained but we still need to make sure
4403       --  that the element type is constrained.
4404
4405       if Is_Indefinite_Subtype (Element_Type) then
4406          Error_Msg_N
4407            ("unconstrained element type in array declaration",
4408             Subtype_Indication (Component_Def));
4409
4410       elsif Is_Abstract_Type (Element_Type) then
4411          Error_Msg_N
4412            ("the type of a component cannot be abstract",
4413             Subtype_Indication (Component_Def));
4414       end if;
4415    end Array_Type_Declaration;
4416
4417    ------------------------------------------------------
4418    -- Replace_Anonymous_Access_To_Protected_Subprogram --
4419    ------------------------------------------------------
4420
4421    function Replace_Anonymous_Access_To_Protected_Subprogram
4422      (N : Node_Id) return Entity_Id
4423    is
4424       Loc : constant Source_Ptr := Sloc (N);
4425
4426       Curr_Scope : constant Scope_Stack_Entry :=
4427                      Scope_Stack.Table (Scope_Stack.Last);
4428
4429       Anon : constant Entity_Id :=
4430                Make_Defining_Identifier (Loc,
4431                  Chars => New_Internal_Name ('S'));
4432
4433       Acc  : Node_Id;
4434       Comp : Node_Id;
4435       Decl : Node_Id;
4436       P    : Node_Id;
4437
4438    begin
4439       Set_Is_Internal (Anon);
4440
4441       case Nkind (N) is
4442          when N_Component_Declaration       |
4443            N_Unconstrained_Array_Definition |
4444            N_Constrained_Array_Definition   =>
4445             Comp := Component_Definition (N);
4446             Acc  := Access_Definition (Comp);
4447
4448          when N_Discriminant_Specification =>
4449             Comp := Discriminant_Type (N);
4450             Acc  := Comp;
4451
4452          when N_Parameter_Specification =>
4453             Comp := Parameter_Type (N);
4454             Acc  := Comp;
4455
4456          when N_Access_Function_Definition  =>
4457             Comp := Result_Definition (N);
4458             Acc  := Comp;
4459
4460          when N_Object_Declaration  =>
4461             Comp := Object_Definition (N);
4462             Acc  := Comp;
4463
4464          when N_Function_Specification =>
4465             Comp := Result_Definition (N);
4466             Acc  := Comp;
4467
4468          when others =>
4469             raise Program_Error;
4470       end case;
4471
4472       Decl := Make_Full_Type_Declaration (Loc,
4473                 Defining_Identifier => Anon,
4474                 Type_Definition   =>
4475                   Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
4476
4477       Mark_Rewrite_Insertion (Decl);
4478
4479       --  Insert the new declaration in the nearest enclosing scope
4480
4481       P := Parent (N);
4482       while Present (P) and then not Has_Declarations (P) loop
4483          P := Parent (P);
4484       end loop;
4485
4486       pragma Assert (Present (P));
4487
4488       if Nkind (P) = N_Package_Specification then
4489          Prepend (Decl, Visible_Declarations (P));
4490       else
4491          Prepend (Decl, Declarations (P));
4492       end if;
4493
4494       --  Replace the anonymous type with an occurrence of the new declaration.
4495       --  In all cases the rewritten node does not have the null-exclusion
4496       --  attribute because (if present) it was already inherited by the
4497       --  anonymous entity (Anon). Thus, in case of components we do not
4498       --  inherit this attribute.
4499
4500       if Nkind (N) = N_Parameter_Specification then
4501          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4502          Set_Etype (Defining_Identifier (N), Anon);
4503          Set_Null_Exclusion_Present (N, False);
4504
4505       elsif Nkind (N) = N_Object_Declaration then
4506          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4507          Set_Etype (Defining_Identifier (N), Anon);
4508
4509       elsif Nkind (N) = N_Access_Function_Definition then
4510          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4511
4512       elsif Nkind (N) = N_Function_Specification then
4513          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4514          Set_Etype (Defining_Unit_Name (N), Anon);
4515
4516       else
4517          Rewrite (Comp,
4518            Make_Component_Definition (Loc,
4519              Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
4520       end if;
4521
4522       Mark_Rewrite_Insertion (Comp);
4523
4524       --  Temporarily remove the current scope from the stack to add the new
4525       --  declarations to the enclosing scope
4526
4527       if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
4528          Analyze (Decl);
4529
4530       else
4531          Scope_Stack.Decrement_Last;
4532          Analyze (Decl);
4533          Set_Is_Itype (Anon);
4534          Scope_Stack.Append (Curr_Scope);
4535       end if;
4536
4537       Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
4538       Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
4539       return Anon;
4540    end Replace_Anonymous_Access_To_Protected_Subprogram;
4541
4542    -------------------------------
4543    -- Build_Derived_Access_Type --
4544    -------------------------------
4545
4546    procedure Build_Derived_Access_Type
4547      (N            : Node_Id;
4548       Parent_Type  : Entity_Id;
4549       Derived_Type : Entity_Id)
4550    is
4551       S : constant Node_Id := Subtype_Indication (Type_Definition (N));
4552
4553       Desig_Type      : Entity_Id;
4554       Discr           : Entity_Id;
4555       Discr_Con_Elist : Elist_Id;
4556       Discr_Con_El    : Elmt_Id;
4557       Subt            : Entity_Id;
4558
4559    begin
4560       --  Set the designated type so it is available in case this is an access
4561       --  to a self-referential type, e.g. a standard list type with a next
4562       --  pointer. Will be reset after subtype is built.
4563
4564       Set_Directly_Designated_Type
4565         (Derived_Type, Designated_Type (Parent_Type));
4566
4567       Subt := Process_Subtype (S, N);
4568
4569       if Nkind (S) /= N_Subtype_Indication
4570         and then Subt /= Base_Type (Subt)
4571       then
4572          Set_Ekind (Derived_Type, E_Access_Subtype);
4573       end if;
4574
4575       if Ekind (Derived_Type) = E_Access_Subtype then
4576          declare
4577             Pbase      : constant Entity_Id := Base_Type (Parent_Type);
4578             Ibase      : constant Entity_Id :=
4579                            Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
4580             Svg_Chars  : constant Name_Id   := Chars (Ibase);
4581             Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
4582
4583          begin
4584             Copy_Node (Pbase, Ibase);
4585
4586             Set_Chars             (Ibase, Svg_Chars);
4587             Set_Next_Entity       (Ibase, Svg_Next_E);
4588             Set_Sloc              (Ibase, Sloc (Derived_Type));
4589             Set_Scope             (Ibase, Scope (Derived_Type));
4590             Set_Freeze_Node       (Ibase, Empty);
4591             Set_Is_Frozen         (Ibase, False);
4592             Set_Comes_From_Source (Ibase, False);
4593             Set_Is_First_Subtype  (Ibase, False);
4594
4595             Set_Etype (Ibase, Pbase);
4596             Set_Etype (Derived_Type, Ibase);
4597          end;
4598       end if;
4599
4600       Set_Directly_Designated_Type
4601         (Derived_Type, Designated_Type (Subt));
4602
4603       Set_Is_Constrained     (Derived_Type, Is_Constrained (Subt));
4604       Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
4605       Set_Size_Info          (Derived_Type,                     Parent_Type);
4606       Set_RM_Size            (Derived_Type, RM_Size            (Parent_Type));
4607       Set_Depends_On_Private (Derived_Type,
4608                               Has_Private_Component (Derived_Type));
4609       Conditional_Delay      (Derived_Type, Subt);
4610
4611       --  Ada 2005 (AI-231). Set the null-exclusion attribute
4612
4613       if Null_Exclusion_Present (Type_Definition (N))
4614         or else Can_Never_Be_Null (Parent_Type)
4615       then
4616          Set_Can_Never_Be_Null (Derived_Type);
4617       end if;
4618
4619       --  Note: we do not copy the Storage_Size_Variable, since we always go to
4620       --  the root type for this information.
4621
4622       --  Apply range checks to discriminants for derived record case
4623       --  ??? THIS CODE SHOULD NOT BE HERE REALLY.
4624
4625       Desig_Type := Designated_Type (Derived_Type);
4626       if Is_Composite_Type (Desig_Type)
4627         and then (not Is_Array_Type (Desig_Type))
4628         and then Has_Discriminants (Desig_Type)
4629         and then Base_Type (Desig_Type) /= Desig_Type
4630       then
4631          Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
4632          Discr_Con_El := First_Elmt (Discr_Con_Elist);
4633
4634          Discr := First_Discriminant (Base_Type (Desig_Type));
4635          while Present (Discr_Con_El) loop
4636             Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
4637             Next_Elmt (Discr_Con_El);
4638             Next_Discriminant (Discr);
4639          end loop;
4640       end if;
4641    end Build_Derived_Access_Type;
4642
4643    ------------------------------
4644    -- Build_Derived_Array_Type --
4645    ------------------------------
4646
4647    procedure Build_Derived_Array_Type
4648      (N            : Node_Id;
4649       Parent_Type  : Entity_Id;
4650       Derived_Type : Entity_Id)
4651    is
4652       Loc           : constant Source_Ptr := Sloc (N);
4653       Tdef          : constant Node_Id    := Type_Definition (N);
4654       Indic         : constant Node_Id    := Subtype_Indication (Tdef);
4655       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
4656       Implicit_Base : Entity_Id;
4657       New_Indic     : Node_Id;
4658
4659       procedure Make_Implicit_Base;
4660       --  If the parent subtype is constrained, the derived type is a subtype
4661       --  of an implicit base type derived from the parent base.
4662
4663       ------------------------
4664       -- Make_Implicit_Base --
4665       ------------------------
4666
4667       procedure Make_Implicit_Base is
4668       begin
4669          Implicit_Base :=
4670            Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
4671
4672          Set_Ekind (Implicit_Base, Ekind (Parent_Base));
4673          Set_Etype (Implicit_Base, Parent_Base);
4674
4675          Copy_Array_Subtype_Attributes   (Implicit_Base, Parent_Base);
4676          Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
4677
4678          Set_Has_Delayed_Freeze (Implicit_Base, True);
4679       end Make_Implicit_Base;
4680
4681    --  Start of processing for Build_Derived_Array_Type
4682
4683    begin
4684       if not Is_Constrained (Parent_Type) then
4685          if Nkind (Indic) /= N_Subtype_Indication then
4686             Set_Ekind (Derived_Type, E_Array_Type);
4687
4688             Copy_Array_Subtype_Attributes   (Derived_Type, Parent_Type);
4689             Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
4690
4691             Set_Has_Delayed_Freeze (Derived_Type, True);
4692
4693          else
4694             Make_Implicit_Base;
4695             Set_Etype (Derived_Type, Implicit_Base);
4696
4697             New_Indic :=
4698               Make_Subtype_Declaration (Loc,
4699                 Defining_Identifier => Derived_Type,
4700                 Subtype_Indication  =>
4701                   Make_Subtype_Indication (Loc,
4702                     Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
4703                     Constraint => Constraint (Indic)));
4704
4705             Rewrite (N, New_Indic);
4706             Analyze (N);
4707          end if;
4708
4709       else
4710          if Nkind (Indic) /= N_Subtype_Indication then
4711             Make_Implicit_Base;
4712
4713             Set_Ekind             (Derived_Type, Ekind (Parent_Type));
4714             Set_Etype             (Derived_Type, Implicit_Base);
4715             Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
4716
4717          else
4718             Error_Msg_N ("illegal constraint on constrained type", Indic);
4719          end if;
4720       end if;
4721
4722       --  If parent type is not a derived type itself, and is declared in
4723       --  closed scope (e.g. a subprogram), then we must explicitly introduce
4724       --  the new type's concatenation operator since Derive_Subprograms
4725       --  will not inherit the parent's operator. If the parent type is
4726       --  unconstrained, the operator is of the unconstrained base type.
4727
4728       if Number_Dimensions (Parent_Type) = 1
4729         and then not Is_Limited_Type (Parent_Type)
4730         and then not Is_Derived_Type (Parent_Type)
4731         and then not Is_Package_Or_Generic_Package
4732                        (Scope (Base_Type (Parent_Type)))
4733       then
4734          if not Is_Constrained (Parent_Type)
4735            and then Is_Constrained (Derived_Type)
4736          then
4737             New_Concatenation_Op (Implicit_Base);
4738          else
4739             New_Concatenation_Op (Derived_Type);
4740          end if;
4741       end if;
4742    end Build_Derived_Array_Type;
4743
4744    -----------------------------------
4745    -- Build_Derived_Concurrent_Type --
4746    -----------------------------------
4747
4748    procedure Build_Derived_Concurrent_Type
4749      (N            : Node_Id;
4750       Parent_Type  : Entity_Id;
4751       Derived_Type : Entity_Id)
4752    is
4753       D_Constraint : Node_Id;
4754       Disc_Spec    : Node_Id;
4755       Old_Disc     : Entity_Id;
4756       New_Disc     : Entity_Id;
4757
4758       Constraint_Present : constant Boolean :=
4759                              Nkind (Subtype_Indication (Type_Definition (N)))
4760                                                      = N_Subtype_Indication;
4761
4762    begin
4763       Set_Stored_Constraint (Derived_Type, No_Elist);
4764
4765       --  Copy Storage_Size and Relative_Deadline variables if task case
4766
4767       if Is_Task_Type (Parent_Type) then
4768          Set_Storage_Size_Variable (Derived_Type,
4769            Storage_Size_Variable (Parent_Type));
4770          Set_Relative_Deadline_Variable (Derived_Type,
4771            Relative_Deadline_Variable (Parent_Type));
4772       end if;
4773
4774       if Present (Discriminant_Specifications (N)) then
4775          Push_Scope (Derived_Type);
4776          Check_Or_Process_Discriminants (N, Derived_Type);
4777          End_Scope;
4778
4779       elsif Constraint_Present then
4780
4781          --  Build constrained subtype and derive from it
4782
4783          declare
4784             Loc  : constant Source_Ptr := Sloc (N);
4785             Anon : constant Entity_Id :=
4786                      Make_Defining_Identifier (Loc,
4787                        New_External_Name (Chars (Derived_Type), 'T'));
4788             Decl : Node_Id;
4789
4790          begin
4791             Decl :=
4792               Make_Subtype_Declaration (Loc,
4793                 Defining_Identifier => Anon,
4794                 Subtype_Indication =>
4795                   Subtype_Indication (Type_Definition (N)));
4796             Insert_Before (N, Decl);
4797             Analyze (Decl);
4798
4799             Rewrite (Subtype_Indication (Type_Definition (N)),
4800               New_Occurrence_Of (Anon, Loc));
4801             Set_Analyzed (Derived_Type, False);
4802             Analyze (N);
4803             return;
4804          end;
4805       end if;
4806
4807       --  All attributes are inherited from parent. In particular,
4808       --  entries and the corresponding record type are the same.
4809       --  Discriminants may be renamed, and must be treated separately.
4810
4811       Set_Has_Discriminants
4812         (Derived_Type, Has_Discriminants         (Parent_Type));
4813       Set_Corresponding_Record_Type
4814         (Derived_Type, Corresponding_Record_Type (Parent_Type));
4815
4816       --  Is_Constrained is set according the parent subtype, but is set to
4817       --  False if the derived type is declared with new discriminants.
4818
4819       Set_Is_Constrained
4820         (Derived_Type,
4821          (Is_Constrained (Parent_Type) or else Constraint_Present)
4822            and then not Present (Discriminant_Specifications (N)));
4823
4824       if Constraint_Present then
4825          if not Has_Discriminants (Parent_Type) then
4826             Error_Msg_N ("untagged parent must have discriminants", N);
4827
4828          elsif Present (Discriminant_Specifications (N)) then
4829
4830             --  Verify that new discriminants are used to constrain old ones
4831
4832             D_Constraint :=
4833               First
4834                 (Constraints
4835                   (Constraint (Subtype_Indication (Type_Definition (N)))));
4836
4837             Old_Disc  := First_Discriminant (Parent_Type);
4838             New_Disc  := First_Discriminant (Derived_Type);
4839             Disc_Spec := First (Discriminant_Specifications (N));
4840             while Present (Old_Disc) and then Present (Disc_Spec) loop
4841                if Nkind (Discriminant_Type (Disc_Spec)) /=
4842                                               N_Access_Definition
4843                then
4844                   Analyze (Discriminant_Type (Disc_Spec));
4845
4846                   if not Subtypes_Statically_Compatible (
4847                              Etype (Discriminant_Type (Disc_Spec)),
4848                                Etype (Old_Disc))
4849                   then
4850                      Error_Msg_N
4851                        ("not statically compatible with parent discriminant",
4852                         Discriminant_Type (Disc_Spec));
4853                   end if;
4854                end if;
4855
4856                if Nkind (D_Constraint) = N_Identifier
4857                  and then Chars (D_Constraint) /=
4858                           Chars (Defining_Identifier (Disc_Spec))
4859                then
4860                   Error_Msg_N ("new discriminants must constrain old ones",
4861                     D_Constraint);
4862                else
4863                   Set_Corresponding_Discriminant (New_Disc, Old_Disc);
4864                end if;
4865
4866                Next_Discriminant (Old_Disc);
4867                Next_Discriminant (New_Disc);
4868                Next (Disc_Spec);
4869             end loop;
4870
4871             if Present (Old_Disc) or else Present (Disc_Spec) then
4872                Error_Msg_N ("discriminant mismatch in derivation", N);
4873             end if;
4874
4875          end if;
4876
4877       elsif Present (Discriminant_Specifications (N)) then
4878          Error_Msg_N
4879            ("missing discriminant constraint in untagged derivation",
4880             N);
4881       end if;
4882
4883       if Present (Discriminant_Specifications (N)) then
4884          Old_Disc := First_Discriminant (Parent_Type);
4885          while Present (Old_Disc) loop
4886
4887             if No (Next_Entity (Old_Disc))
4888               or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
4889             then
4890                Set_Next_Entity (Last_Entity (Derived_Type),
4891                                          Next_Entity (Old_Disc));
4892                exit;
4893             end if;
4894
4895             Next_Discriminant (Old_Disc);
4896          end loop;
4897
4898       else
4899          Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
4900          if Has_Discriminants (Parent_Type) then
4901             Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
4902             Set_Discriminant_Constraint (
4903               Derived_Type, Discriminant_Constraint (Parent_Type));
4904          end if;
4905       end if;
4906
4907       Set_Last_Entity  (Derived_Type, Last_Entity  (Parent_Type));
4908
4909       Set_Has_Completion (Derived_Type);
4910    end Build_Derived_Concurrent_Type;
4911
4912    ------------------------------------
4913    -- Build_Derived_Enumeration_Type --
4914    ------------------------------------
4915
4916    procedure Build_Derived_Enumeration_Type
4917      (N            : Node_Id;
4918       Parent_Type  : Entity_Id;
4919       Derived_Type : Entity_Id)
4920    is
4921       Loc           : constant Source_Ptr := Sloc (N);
4922       Def           : constant Node_Id    := Type_Definition (N);
4923       Indic         : constant Node_Id    := Subtype_Indication (Def);
4924       Implicit_Base : Entity_Id;
4925       Literal       : Entity_Id;
4926       New_Lit       : Entity_Id;
4927       Literals_List : List_Id;
4928       Type_Decl     : Node_Id;
4929       Hi, Lo        : Node_Id;
4930       Rang_Expr     : Node_Id;
4931
4932    begin
4933       --  Since types Standard.Character and Standard.Wide_Character do
4934       --  not have explicit literals lists we need to process types derived
4935       --  from them specially. This is handled by Derived_Standard_Character.
4936       --  If the parent type is a generic type, there are no literals either,
4937       --  and we construct the same skeletal representation as for the generic
4938       --  parent type.
4939
4940       if Is_Standard_Character_Type (Parent_Type) then
4941          Derived_Standard_Character (N, Parent_Type, Derived_Type);
4942
4943       elsif Is_Generic_Type (Root_Type (Parent_Type)) then
4944          declare
4945             Lo : Node_Id;
4946             Hi : Node_Id;
4947
4948          begin
4949             Lo :=
4950                Make_Attribute_Reference (Loc,
4951                  Attribute_Name => Name_First,
4952                  Prefix => New_Reference_To (Derived_Type, Loc));
4953             Set_Etype (Lo, Derived_Type);
4954
4955             Hi :=
4956                Make_Attribute_Reference (Loc,
4957                  Attribute_Name => Name_Last,
4958                  Prefix => New_Reference_To (Derived_Type, Loc));
4959             Set_Etype (Hi, Derived_Type);
4960
4961             Set_Scalar_Range (Derived_Type,
4962                Make_Range (Loc,
4963                  Low_Bound => Lo,
4964                  High_Bound => Hi));
4965          end;
4966
4967       else
4968          --  If a constraint is present, analyze the bounds to catch
4969          --  premature usage of the derived literals.
4970
4971          if Nkind (Indic) = N_Subtype_Indication
4972            and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
4973          then
4974             Analyze (Low_Bound  (Range_Expression (Constraint (Indic))));
4975             Analyze (High_Bound (Range_Expression (Constraint (Indic))));
4976          end if;
4977
4978          --  Introduce an implicit base type for the derived type even if there
4979          --  is no constraint attached to it, since this seems closer to the
4980          --  Ada semantics. Build a full type declaration tree for the derived
4981          --  type using the implicit base type as the defining identifier. The
4982          --  build a subtype declaration tree which applies the constraint (if
4983          --  any) have it replace the derived type declaration.
4984
4985          Literal := First_Literal (Parent_Type);
4986          Literals_List := New_List;
4987          while Present (Literal)
4988            and then Ekind (Literal) = E_Enumeration_Literal
4989          loop
4990             --  Literals of the derived type have the same representation as
4991             --  those of the parent type, but this representation can be
4992             --  overridden by an explicit representation clause. Indicate
4993             --  that there is no explicit representation given yet. These
4994             --  derived literals are implicit operations of the new type,
4995             --  and can be overridden by explicit ones.
4996
4997             if Nkind (Literal) = N_Defining_Character_Literal then
4998                New_Lit :=
4999                  Make_Defining_Character_Literal (Loc, Chars (Literal));
5000             else
5001                New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
5002             end if;
5003
5004             Set_Ekind                (New_Lit, E_Enumeration_Literal);
5005             Set_Enumeration_Pos      (New_Lit, Enumeration_Pos (Literal));
5006             Set_Enumeration_Rep      (New_Lit, Enumeration_Rep (Literal));
5007             Set_Enumeration_Rep_Expr (New_Lit, Empty);
5008             Set_Alias                (New_Lit, Literal);
5009             Set_Is_Known_Valid       (New_Lit, True);
5010
5011             Append (New_Lit, Literals_List);
5012             Next_Literal (Literal);
5013          end loop;
5014
5015          Implicit_Base :=
5016            Make_Defining_Identifier (Sloc (Derived_Type),
5017              New_External_Name (Chars (Derived_Type), 'B'));
5018
5019          --  Indicate the proper nature of the derived type. This must be done
5020          --  before analysis of the literals, to recognize cases when a literal
5021          --  may be hidden by a previous explicit function definition (cf.
5022          --  c83031a).
5023
5024          Set_Ekind (Derived_Type, E_Enumeration_Subtype);
5025          Set_Etype (Derived_Type, Implicit_Base);
5026
5027          Type_Decl :=
5028            Make_Full_Type_Declaration (Loc,
5029              Defining_Identifier => Implicit_Base,
5030              Discriminant_Specifications => No_List,
5031              Type_Definition =>
5032                Make_Enumeration_Type_Definition (Loc, Literals_List));
5033
5034          Mark_Rewrite_Insertion (Type_Decl);
5035          Insert_Before (N, Type_Decl);
5036          Analyze (Type_Decl);
5037
5038          --  After the implicit base is analyzed its Etype needs to be changed
5039          --  to reflect the fact that it is derived from the parent type which
5040          --  was ignored during analysis. We also set the size at this point.
5041
5042          Set_Etype (Implicit_Base, Parent_Type);
5043
5044          Set_Size_Info      (Implicit_Base,                 Parent_Type);
5045          Set_RM_Size        (Implicit_Base, RM_Size        (Parent_Type));
5046          Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
5047
5048          Set_Has_Non_Standard_Rep
5049                             (Implicit_Base, Has_Non_Standard_Rep
5050                                                            (Parent_Type));
5051          Set_Has_Delayed_Freeze (Implicit_Base);
5052
5053          --  Process the subtype indication including a validation check on the
5054          --  constraint, if any. If a constraint is given, its bounds must be
5055          --  implicitly converted to the new type.
5056
5057          if Nkind (Indic) = N_Subtype_Indication then
5058             declare
5059                R : constant Node_Id :=
5060                      Range_Expression (Constraint (Indic));
5061
5062             begin
5063                if Nkind (R) = N_Range then
5064                   Hi := Build_Scalar_Bound
5065                           (High_Bound (R), Parent_Type, Implicit_Base);
5066                   Lo := Build_Scalar_Bound
5067                           (Low_Bound  (R), Parent_Type, Implicit_Base);
5068
5069                else
5070                   --  Constraint is a Range attribute. Replace with explicit
5071                   --  mention of the bounds of the prefix, which must be a
5072                   --  subtype.
5073
5074                   Analyze (Prefix (R));
5075                   Hi :=
5076                     Convert_To (Implicit_Base,
5077                       Make_Attribute_Reference (Loc,
5078                         Attribute_Name => Name_Last,
5079                         Prefix =>
5080                           New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5081
5082                   Lo :=
5083                     Convert_To (Implicit_Base,
5084                       Make_Attribute_Reference (Loc,
5085                         Attribute_Name => Name_First,
5086                         Prefix =>
5087                           New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5088                end if;
5089             end;
5090
5091          else
5092             Hi :=
5093               Build_Scalar_Bound
5094                 (Type_High_Bound (Parent_Type),
5095                  Parent_Type, Implicit_Base);
5096             Lo :=
5097                Build_Scalar_Bound
5098                  (Type_Low_Bound (Parent_Type),
5099                   Parent_Type, Implicit_Base);
5100          end if;
5101
5102          Rang_Expr :=
5103            Make_Range (Loc,
5104              Low_Bound  => Lo,
5105              High_Bound => Hi);
5106
5107          --  If we constructed a default range for the case where no range
5108          --  was given, then the expressions in the range must not freeze
5109          --  since they do not correspond to expressions in the source.
5110
5111          if Nkind (Indic) /= N_Subtype_Indication then
5112             Set_Must_Not_Freeze (Lo);
5113             Set_Must_Not_Freeze (Hi);
5114             Set_Must_Not_Freeze (Rang_Expr);
5115          end if;
5116
5117          Rewrite (N,
5118            Make_Subtype_Declaration (Loc,
5119              Defining_Identifier => Derived_Type,
5120              Subtype_Indication =>
5121                Make_Subtype_Indication (Loc,
5122                  Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
5123                  Constraint =>
5124                    Make_Range_Constraint (Loc,
5125                      Range_Expression => Rang_Expr))));
5126
5127          Analyze (N);
5128
5129          --  If pragma Discard_Names applies on the first subtype of the parent
5130          --  type, then it must be applied on this subtype as well.
5131
5132          if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
5133             Set_Discard_Names (Derived_Type);
5134          end if;
5135
5136          --  Apply a range check. Since this range expression doesn't have an
5137          --  Etype, we have to specifically pass the Source_Typ parameter. Is
5138          --  this right???
5139
5140          if Nkind (Indic) = N_Subtype_Indication then
5141             Apply_Range_Check (Range_Expression (Constraint (Indic)),
5142                                Parent_Type,
5143                                Source_Typ => Entity (Subtype_Mark (Indic)));
5144          end if;
5145       end if;
5146    end Build_Derived_Enumeration_Type;
5147
5148    --------------------------------
5149    -- Build_Derived_Numeric_Type --
5150    --------------------------------
5151
5152    procedure Build_Derived_Numeric_Type
5153      (N            : Node_Id;
5154       Parent_Type  : Entity_Id;
5155       Derived_Type : Entity_Id)
5156    is
5157       Loc           : constant Source_Ptr := Sloc (N);
5158       Tdef          : constant Node_Id    := Type_Definition (N);
5159       Indic         : constant Node_Id    := Subtype_Indication (Tdef);
5160       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
5161       No_Constraint : constant Boolean    := Nkind (Indic) /=
5162                                                   N_Subtype_Indication;
5163       Implicit_Base : Entity_Id;
5164
5165       Lo : Node_Id;
5166       Hi : Node_Id;
5167
5168    begin
5169       --  Process the subtype indication including a validation check on
5170       --  the constraint if any.
5171
5172       Discard_Node (Process_Subtype (Indic, N));
5173
5174       --  Introduce an implicit base type for the derived type even if there
5175       --  is no constraint attached to it, since this seems closer to the Ada
5176       --  semantics.
5177
5178       Implicit_Base :=
5179         Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5180
5181       Set_Etype          (Implicit_Base, Parent_Base);
5182       Set_Ekind          (Implicit_Base, Ekind          (Parent_Base));
5183       Set_Size_Info      (Implicit_Base,                 Parent_Base);
5184       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
5185       Set_Parent         (Implicit_Base, Parent (Derived_Type));
5186
5187       --  Set RM Size for discrete type or decimal fixed-point type
5188       --  Ordinary fixed-point is excluded, why???
5189
5190       if Is_Discrete_Type (Parent_Base)
5191         or else Is_Decimal_Fixed_Point_Type (Parent_Base)
5192       then
5193          Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
5194       end if;
5195
5196       Set_Has_Delayed_Freeze (Implicit_Base);
5197
5198       Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Base));
5199       Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
5200
5201       Set_Scalar_Range (Implicit_Base,
5202         Make_Range (Loc,
5203           Low_Bound  => Lo,
5204           High_Bound => Hi));
5205
5206       if Has_Infinities (Parent_Base) then
5207          Set_Includes_Infinities (Scalar_Range (Implicit_Base));
5208       end if;
5209
5210       --  The Derived_Type, which is the entity of the declaration, is a
5211       --  subtype of the implicit base. Its Ekind is a subtype, even in the
5212       --  absence of an explicit constraint.
5213
5214       Set_Etype (Derived_Type, Implicit_Base);
5215
5216       --  If we did not have a constraint, then the Ekind is set from the
5217       --  parent type (otherwise Process_Subtype has set the bounds)
5218
5219       if No_Constraint then
5220          Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
5221       end if;
5222
5223       --  If we did not have a range constraint, then set the range from the
5224       --  parent type. Otherwise, the call to Process_Subtype has set the
5225       --  bounds.
5226
5227       if No_Constraint
5228         or else not Has_Range_Constraint (Indic)
5229       then
5230          Set_Scalar_Range (Derived_Type,
5231            Make_Range (Loc,
5232              Low_Bound  => New_Copy_Tree (Type_Low_Bound  (Parent_Type)),
5233              High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
5234          Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5235
5236          if Has_Infinities (Parent_Type) then
5237             Set_Includes_Infinities (Scalar_Range (Derived_Type));
5238          end if;
5239       end if;
5240
5241       Set_Is_Descendent_Of_Address (Derived_Type,
5242         Is_Descendent_Of_Address (Parent_Type));
5243       Set_Is_Descendent_Of_Address (Implicit_Base,
5244         Is_Descendent_Of_Address (Parent_Type));
5245
5246       --  Set remaining type-specific fields, depending on numeric type
5247
5248       if Is_Modular_Integer_Type (Parent_Type) then
5249          Set_Modulus (Implicit_Base, Modulus (Parent_Base));
5250
5251          Set_Non_Binary_Modulus
5252            (Implicit_Base, Non_Binary_Modulus (Parent_Base));
5253
5254       elsif Is_Floating_Point_Type (Parent_Type) then
5255
5256          --  Digits of base type is always copied from the digits value of
5257          --  the parent base type, but the digits of the derived type will
5258          --  already have been set if there was a constraint present.
5259
5260          Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5261          Set_Vax_Float    (Implicit_Base, Vax_Float    (Parent_Base));
5262
5263          if No_Constraint then
5264             Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
5265          end if;
5266
5267       elsif Is_Fixed_Point_Type (Parent_Type) then
5268
5269          --  Small of base type and derived type are always copied from the
5270          --  parent base type, since smalls never change. The delta of the
5271          --  base type is also copied from the parent base type. However the
5272          --  delta of the derived type will have been set already if a
5273          --  constraint was present.
5274
5275          Set_Small_Value (Derived_Type,  Small_Value (Parent_Base));
5276          Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
5277          Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
5278
5279          if No_Constraint then
5280             Set_Delta_Value (Derived_Type,  Delta_Value (Parent_Type));
5281          end if;
5282
5283          --  The scale and machine radix in the decimal case are always
5284          --  copied from the parent base type.
5285
5286          if Is_Decimal_Fixed_Point_Type (Parent_Type) then
5287             Set_Scale_Value (Derived_Type,  Scale_Value (Parent_Base));
5288             Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
5289
5290             Set_Machine_Radix_10
5291               (Derived_Type,  Machine_Radix_10 (Parent_Base));
5292             Set_Machine_Radix_10
5293               (Implicit_Base, Machine_Radix_10 (Parent_Base));
5294
5295             Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5296
5297             if No_Constraint then
5298                Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
5299
5300             else
5301                --  the analysis of the subtype_indication sets the
5302                --  digits value of the derived type.
5303
5304                null;
5305             end if;
5306          end if;
5307       end if;
5308
5309       --  The type of the bounds is that of the parent type, and they
5310       --  must be converted to the derived type.
5311
5312       Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
5313
5314       --  The implicit_base should be frozen when the derived type is frozen,
5315       --  but note that it is used in the conversions of the bounds. For fixed
5316       --  types we delay the determination of the bounds until the proper
5317       --  freezing point. For other numeric types this is rejected by GCC, for
5318       --  reasons that are currently unclear (???), so we choose to freeze the
5319       --  implicit base now. In the case of integers and floating point types
5320       --  this is harmless because subsequent representation clauses cannot
5321       --  affect anything, but it is still baffling that we cannot use the
5322       --  same mechanism for all derived numeric types.
5323
5324       --  There is a further complication: actually *some* representation
5325       --  clauses can affect the implicit base type. Namely, attribute
5326       --  definition clauses for stream-oriented attributes need to set the
5327       --  corresponding TSS entries on the base type, and this normally cannot
5328       --  be done after the base type is frozen, so the circuitry in
5329       --  Sem_Ch13.New_Stream_Subprogram must account for this possibility and
5330       --  not use Set_TSS in this case.
5331
5332       if Is_Fixed_Point_Type (Parent_Type) then
5333          Conditional_Delay (Implicit_Base, Parent_Type);
5334       else
5335          Freeze_Before (N, Implicit_Base);
5336       end if;
5337    end Build_Derived_Numeric_Type;
5338
5339    --------------------------------
5340    -- Build_Derived_Private_Type --
5341    --------------------------------
5342
5343    procedure Build_Derived_Private_Type
5344      (N             : Node_Id;
5345       Parent_Type   : Entity_Id;
5346       Derived_Type  : Entity_Id;
5347       Is_Completion : Boolean;
5348       Derive_Subps  : Boolean := True)
5349    is
5350       Der_Base    : Entity_Id;
5351       Discr       : Entity_Id;
5352       Full_Decl   : Node_Id := Empty;
5353       Full_Der    : Entity_Id;
5354       Full_P      : Entity_Id;
5355       Last_Discr  : Entity_Id;
5356       Par_Scope   : constant Entity_Id := Scope (Base_Type (Parent_Type));
5357       Swapped     : Boolean := False;
5358
5359       procedure Copy_And_Build;
5360       --  Copy derived type declaration, replace parent with its full view,
5361       --  and analyze new declaration.
5362
5363       --------------------
5364       -- Copy_And_Build --
5365       --------------------
5366
5367       procedure Copy_And_Build is
5368          Full_N : Node_Id;
5369
5370       begin
5371          if Ekind (Parent_Type) in Record_Kind
5372            or else
5373              (Ekind (Parent_Type) in Enumeration_Kind
5374                and then not Is_Standard_Character_Type (Parent_Type)
5375                and then not Is_Generic_Type (Root_Type (Parent_Type)))
5376          then
5377             Full_N := New_Copy_Tree (N);
5378             Insert_After (N, Full_N);
5379             Build_Derived_Type (
5380               Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
5381
5382          else
5383             Build_Derived_Type (
5384               N, Parent_Type, Full_Der, True, Derive_Subps => False);
5385          end if;
5386       end Copy_And_Build;
5387
5388    --  Start of processing for Build_Derived_Private_Type
5389
5390    begin
5391       if Is_Tagged_Type (Parent_Type) then
5392          Build_Derived_Record_Type
5393            (N, Parent_Type, Derived_Type, Derive_Subps);
5394          return;
5395
5396       elsif Has_Discriminants (Parent_Type) then
5397          if Present (Full_View (Parent_Type)) then
5398             if not Is_Completion then
5399
5400                --  Copy declaration for subsequent analysis, to provide a
5401                --  completion for what is a private declaration. Indicate that
5402                --  the full type is internally generated.
5403
5404                Full_Decl := New_Copy_Tree (N);
5405                Full_Der  := New_Copy (Derived_Type);
5406                Set_Comes_From_Source (Full_Decl, False);
5407                Set_Comes_From_Source (Full_Der, False);
5408
5409                Insert_After (N, Full_Decl);
5410
5411             else
5412                --  If this is a completion, the full view being built is
5413                --  itself private. We build a subtype of the parent with
5414                --  the same constraints as this full view, to convey to the
5415                --  back end the constrained components and the size of this
5416                --  subtype. If the parent is constrained, its full view can
5417                --  serve as the underlying full view of the derived type.
5418
5419                if No (Discriminant_Specifications (N)) then
5420                   if Nkind (Subtype_Indication (Type_Definition (N))) =
5421                                                         N_Subtype_Indication
5422                   then
5423                      Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
5424
5425                   elsif Is_Constrained (Full_View (Parent_Type)) then
5426                      Set_Underlying_Full_View (Derived_Type,
5427                        Full_View (Parent_Type));
5428                   end if;
5429
5430                else
5431                   --  If there are new discriminants, the parent subtype is
5432                   --  constrained by them, but it is not clear how to build
5433                   --  the underlying_full_view in this case ???
5434
5435                   null;
5436                end if;
5437             end if;
5438          end if;
5439
5440          --  Build partial view of derived type from partial view of parent
5441
5442          Build_Derived_Record_Type
5443            (N, Parent_Type, Derived_Type, Derive_Subps);
5444
5445          if Present (Full_View (Parent_Type))
5446            and then not Is_Completion
5447          then
5448             if not In_Open_Scopes (Par_Scope)
5449               or else not In_Same_Source_Unit (N, Parent_Type)
5450             then
5451                --  Swap partial and full views temporarily
5452
5453                Install_Private_Declarations (Par_Scope);
5454                Install_Visible_Declarations (Par_Scope);
5455                Swapped := True;
5456             end if;
5457
5458             --  Build full view of derived type from full view of parent which
5459             --  is now installed. Subprograms have been derived on the partial
5460             --  view, the completion does not derive them anew.
5461
5462             if not Is_Tagged_Type (Parent_Type) then
5463
5464                --  If the parent is itself derived from another private type,
5465                --  installing the private declarations has not affected its
5466                --  privacy status, so use its own full view explicitly.
5467
5468                if Is_Private_Type (Parent_Type) then
5469                   Build_Derived_Record_Type
5470                     (Full_Decl, Full_View (Parent_Type), Full_Der, False);
5471                else
5472                   Build_Derived_Record_Type
5473                     (Full_Decl, Parent_Type, Full_Der, False);
5474                end if;
5475
5476             else
5477                --  If full view of parent is tagged, the completion
5478                --  inherits the proper primitive operations.
5479
5480                Set_Defining_Identifier (Full_Decl, Full_Der);
5481                Build_Derived_Record_Type
5482                  (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
5483                Set_Analyzed (Full_Decl);
5484             end if;
5485
5486             if Swapped then
5487                Uninstall_Declarations (Par_Scope);
5488
5489                if In_Open_Scopes (Par_Scope) then
5490                   Install_Visible_Declarations (Par_Scope);
5491                end if;
5492             end if;
5493
5494             Der_Base := Base_Type (Derived_Type);
5495             Set_Full_View (Derived_Type, Full_Der);
5496             Set_Full_View (Der_Base, Base_Type (Full_Der));
5497
5498             --  Copy the discriminant list from full view to the partial views
5499             --  (base type and its subtype). Gigi requires that the partial
5500             --  and full views have the same discriminants.
5501
5502             --  Note that since the partial view is pointing to discriminants
5503             --  in the full view, their scope will be that of the full view.
5504             --  This might cause some front end problems and need
5505             --  adjustment???
5506
5507             Discr := First_Discriminant (Base_Type (Full_Der));
5508             Set_First_Entity (Der_Base, Discr);
5509
5510             loop
5511                Last_Discr := Discr;
5512                Next_Discriminant (Discr);
5513                exit when No (Discr);
5514             end loop;
5515
5516             Set_Last_Entity (Der_Base, Last_Discr);
5517
5518             Set_First_Entity (Derived_Type, First_Entity (Der_Base));
5519             Set_Last_Entity  (Derived_Type, Last_Entity  (Der_Base));
5520             Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
5521
5522          else
5523             --  If this is a completion, the derived type stays private
5524             --  and there is no need to create a further full view, except
5525             --  in the unusual case when the derivation is nested within a
5526             --  child unit, see below.
5527
5528             null;
5529          end if;
5530
5531       elsif Present (Full_View (Parent_Type))
5532         and then  Has_Discriminants (Full_View (Parent_Type))
5533       then
5534          if Has_Unknown_Discriminants (Parent_Type)
5535            and then Nkind (Subtype_Indication (Type_Definition (N))) =
5536                                                          N_Subtype_Indication
5537          then
5538             Error_Msg_N
5539               ("cannot constrain type with unknown discriminants",
5540                Subtype_Indication (Type_Definition (N)));
5541             return;
5542          end if;
5543
5544          --  If full view of parent is a record type, Build full view as
5545          --  a derivation from the parent's full view. Partial view remains
5546          --  private. For code generation and linking, the full view must
5547          --  have the same public status as the partial one. This full view
5548          --  is only needed if the parent type is in an enclosing scope, so
5549          --  that the full view may actually become visible, e.g. in a child
5550          --  unit. This is both more efficient, and avoids order of freezing
5551          --  problems with the added entities.
5552
5553          if not Is_Private_Type (Full_View (Parent_Type))
5554            and then (In_Open_Scopes (Scope (Parent_Type)))
5555          then
5556             Full_Der := Make_Defining_Identifier (Sloc (Derived_Type),
5557                                               Chars (Derived_Type));
5558             Set_Is_Itype (Full_Der);
5559             Set_Has_Private_Declaration (Full_Der);
5560             Set_Has_Private_Declaration (Derived_Type);
5561             Set_Associated_Node_For_Itype (Full_Der, N);
5562             Set_Parent (Full_Der, Parent (Derived_Type));
5563             Set_Full_View (Derived_Type, Full_Der);
5564             Set_Is_Public (Full_Der, Is_Public (Derived_Type));
5565             Full_P := Full_View (Parent_Type);
5566             Exchange_Declarations (Parent_Type);
5567             Copy_And_Build;
5568             Exchange_Declarations (Full_P);
5569
5570          else
5571             Build_Derived_Record_Type
5572               (N, Full_View (Parent_Type), Derived_Type,
5573                 Derive_Subps => False);
5574          end if;
5575
5576          --  In any case, the primitive operations are inherited from
5577          --  the parent type, not from the internal full view.
5578
5579          Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
5580
5581          if Derive_Subps then
5582             Derive_Subprograms (Parent_Type, Derived_Type);
5583          end if;
5584
5585       else
5586          --  Untagged type, No discriminants on either view
5587
5588          if Nkind (Subtype_Indication (Type_Definition (N))) =
5589                                                    N_Subtype_Indication
5590          then
5591             Error_Msg_N
5592               ("illegal constraint on type without discriminants", N);
5593          end if;
5594
5595          if Present (Discriminant_Specifications (N))
5596            and then Present (Full_View (Parent_Type))
5597            and then not Is_Tagged_Type (Full_View (Parent_Type))
5598          then
5599             Error_Msg_N
5600               ("cannot add discriminants to untagged type", N);
5601          end if;
5602
5603          Set_Stored_Constraint (Derived_Type, No_Elist);
5604          Set_Is_Constrained    (Derived_Type, Is_Constrained (Parent_Type));
5605          Set_Is_Controlled     (Derived_Type, Is_Controlled  (Parent_Type));
5606          Set_Has_Controlled_Component
5607                                (Derived_Type, Has_Controlled_Component
5608                                                              (Parent_Type));
5609
5610          --  Direct controlled types do not inherit Finalize_Storage_Only flag
5611
5612          if not Is_Controlled  (Parent_Type) then
5613             Set_Finalize_Storage_Only
5614               (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
5615          end if;
5616
5617          --  Construct the implicit full view by deriving from full view of
5618          --  the parent type. In order to get proper visibility, we install
5619          --  the parent scope and its declarations.
5620
5621          --  ??? if the parent is untagged private and its completion is
5622          --  tagged, this mechanism will not work because we cannot derive
5623          --  from the tagged full view unless we have an extension
5624
5625          if Present (Full_View (Parent_Type))
5626            and then not Is_Tagged_Type (Full_View (Parent_Type))
5627            and then not Is_Completion
5628          then
5629             Full_Der :=
5630               Make_Defining_Identifier (Sloc (Derived_Type),
5631                 Chars => Chars (Derived_Type));
5632             Set_Is_Itype (Full_Der);
5633             Set_Has_Private_Declaration (Full_Der);
5634             Set_Has_Private_Declaration (Derived_Type);
5635             Set_Associated_Node_For_Itype (Full_Der, N);
5636             Set_Parent (Full_Der, Parent (Derived_Type));
5637             Set_Full_View (Derived_Type, Full_Der);
5638
5639             if not In_Open_Scopes (Par_Scope) then
5640                Install_Private_Declarations (Par_Scope);
5641                Install_Visible_Declarations (Par_Scope);
5642                Copy_And_Build;
5643                Uninstall_Declarations (Par_Scope);
5644
5645             --  If parent scope is open and in another unit, and parent has a
5646             --  completion, then the derivation is taking place in the visible
5647             --  part of a child unit. In that case retrieve the full view of
5648             --  the parent momentarily.
5649
5650             elsif not In_Same_Source_Unit (N, Parent_Type) then
5651                Full_P := Full_View (Parent_Type);
5652                Exchange_Declarations (Parent_Type);
5653                Copy_And_Build;
5654                Exchange_Declarations (Full_P);
5655
5656             --  Otherwise it is a local derivation
5657
5658             else
5659                Copy_And_Build;
5660             end if;
5661
5662             Set_Scope                (Full_Der, Current_Scope);
5663             Set_Is_First_Subtype     (Full_Der,
5664                                        Is_First_Subtype (Derived_Type));
5665             Set_Has_Size_Clause      (Full_Der, False);
5666             Set_Has_Alignment_Clause (Full_Der, False);
5667             Set_Next_Entity          (Full_Der, Empty);
5668             Set_Has_Delayed_Freeze   (Full_Der);
5669             Set_Is_Frozen            (Full_Der, False);
5670             Set_Freeze_Node          (Full_Der, Empty);
5671             Set_Depends_On_Private   (Full_Der,
5672                                         Has_Private_Component    (Full_Der));
5673             Set_Public_Status        (Full_Der);
5674          end if;
5675       end if;
5676
5677       Set_Has_Unknown_Discriminants (Derived_Type,
5678         Has_Unknown_Discriminants (Parent_Type));
5679
5680       if Is_Private_Type (Derived_Type) then
5681          Set_Private_Dependents (Derived_Type, New_Elmt_List);
5682       end if;
5683
5684       if Is_Private_Type (Parent_Type)
5685         and then Base_Type (Parent_Type) = Parent_Type
5686         and then In_Open_Scopes (Scope (Parent_Type))
5687       then
5688          Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
5689
5690          if Is_Child_Unit (Scope (Current_Scope))
5691            and then Is_Completion
5692            and then In_Private_Part (Current_Scope)
5693            and then Scope (Parent_Type) /= Current_Scope
5694          then
5695             --  This is the unusual case where a type completed by a private
5696             --  derivation occurs within a package nested in a child unit,
5697             --  and the parent is declared in an ancestor. In this case, the
5698             --  full view of the parent type will become visible in the body
5699             --  of the enclosing child, and only then will the current type
5700             --  be possibly non-private. We build a underlying full view that
5701             --  will be installed when the enclosing child body is compiled.
5702
5703             Full_Der :=
5704               Make_Defining_Identifier (Sloc (Derived_Type),
5705                 Chars => Chars (Derived_Type));
5706             Set_Is_Itype (Full_Der);
5707             Build_Itype_Reference (Full_Der, N);
5708
5709             --  The full view will be used to swap entities on entry/exit to
5710             --  the body, and must appear in the entity list for the package.
5711
5712             Append_Entity (Full_Der, Scope (Derived_Type));
5713             Set_Has_Private_Declaration (Full_Der);
5714             Set_Has_Private_Declaration (Derived_Type);
5715             Set_Associated_Node_For_Itype (Full_Der, N);
5716             Set_Parent (Full_Der, Parent (Derived_Type));
5717             Full_P := Full_View (Parent_Type);
5718             Exchange_Declarations (Parent_Type);
5719             Copy_And_Build;
5720             Exchange_Declarations (Full_P);
5721             Set_Underlying_Full_View (Derived_Type, Full_Der);
5722          end if;
5723       end if;
5724    end Build_Derived_Private_Type;
5725
5726    -------------------------------
5727    -- Build_Derived_Record_Type --
5728    -------------------------------
5729
5730    --  1. INTRODUCTION
5731
5732    --  Ideally we would like to use the same model of type derivation for
5733    --  tagged and untagged record types. Unfortunately this is not quite
5734    --  possible because the semantics of representation clauses is different
5735    --  for tagged and untagged records under inheritance. Consider the
5736    --  following:
5737
5738    --     type R (...) is [tagged] record ... end record;
5739    --     type T (...) is new R (...) [with ...];
5740
5741    --  The representation clauses for T can specify a completely different
5742    --  record layout from R's. Hence the same component can be placed in two
5743    --  very different positions in objects of type T and R. If R and are tagged
5744    --  types, representation clauses for T can only specify the layout of non
5745    --  inherited components, thus components that are common in R and T have
5746    --  the same position in objects of type R and T.
5747
5748    --  This has two implications. The first is that the entire tree for R's
5749    --  declaration needs to be copied for T in the untagged case, so that T
5750    --  can be viewed as a record type of its own with its own representation
5751    --  clauses. The second implication is the way we handle discriminants.
5752    --  Specifically, in the untagged case we need a way to communicate to Gigi
5753    --  what are the real discriminants in the record, while for the semantics
5754    --  we need to consider those introduced by the user to rename the
5755    --  discriminants in the parent type. This is handled by introducing the
5756    --  notion of stored discriminants. See below for more.
5757
5758    --  Fortunately the way regular components are inherited can be handled in
5759    --  the same way in tagged and untagged types.
5760
5761    --  To complicate things a bit more the private view of a private extension
5762    --  cannot be handled in the same way as the full view (for one thing the
5763    --  semantic rules are somewhat different). We will explain what differs
5764    --  below.
5765
5766    --  2. DISCRIMINANTS UNDER INHERITANCE
5767
5768    --  The semantic rules governing the discriminants of derived types are
5769    --  quite subtle.
5770
5771    --   type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
5772    --      [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
5773
5774    --  If parent type has discriminants, then the discriminants that are
5775    --  declared in the derived type are [3.4 (11)]:
5776
5777    --  o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
5778    --    there is one;
5779
5780    --  o Otherwise, each discriminant of the parent type (implicitly declared
5781    --    in the same order with the same specifications). In this case, the
5782    --    discriminants are said to be "inherited", or if unknown in the parent
5783    --    are also unknown in the derived type.
5784
5785    --  Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
5786
5787    --  o The parent subtype shall be constrained;
5788
5789    --  o If the parent type is not a tagged type, then each discriminant of
5790    --    the derived type shall be used in the constraint defining a parent
5791    --    subtype. [Implementation note: This ensures that the new discriminant
5792    --    can share storage with an existing discriminant.]
5793
5794    --  For the derived type each discriminant of the parent type is either
5795    --  inherited, constrained to equal some new discriminant of the derived
5796    --  type, or constrained to the value of an expression.
5797
5798    --  When inherited or constrained to equal some new discriminant, the
5799    --  parent discriminant and the discriminant of the derived type are said
5800    --  to "correspond".
5801
5802    --  If a discriminant of the parent type is constrained to a specific value
5803    --  in the derived type definition, then the discriminant is said to be
5804    --  "specified" by that derived type definition.
5805
5806    --  3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
5807
5808    --  We have spoken about stored discriminants in point 1 (introduction)
5809    --  above. There are two sort of stored discriminants: implicit and
5810    --  explicit. As long as the derived type inherits the same discriminants as
5811    --  the root record type, stored discriminants are the same as regular
5812    --  discriminants, and are said to be implicit. However, if any discriminant
5813    --  in the root type was renamed in the derived type, then the derived
5814    --  type will contain explicit stored discriminants. Explicit stored
5815    --  discriminants are discriminants in addition to the semantically visible
5816    --  discriminants defined for the derived type. Stored discriminants are
5817    --  used by Gigi to figure out what are the physical discriminants in
5818    --  objects of the derived type (see precise definition in einfo.ads).
5819    --  As an example, consider the following:
5820
5821    --           type R  (D1, D2, D3 : Int) is record ... end record;
5822    --           type T1 is new R;
5823    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
5824    --           type T3 is new T2;
5825    --           type T4 (Y : Int) is new T3 (Y, 99);
5826
5827    --  The following table summarizes the discriminants and stored
5828    --  discriminants in R and T1 through T4.
5829
5830    --   Type      Discrim     Stored Discrim  Comment
5831    --    R      (D1, D2, D3)   (D1, D2, D3)   Girder discrims implicit in R
5832    --    T1     (D1, D2, D3)   (D1, D2, D3)   Girder discrims implicit in T1
5833    --    T2     (X1, X2)       (D1, D2, D3)   Girder discrims EXPLICIT in T2
5834    --    T3     (X1, X2)       (D1, D2, D3)   Girder discrims EXPLICIT in T3
5835    --    T4     (Y)            (D1, D2, D3)   Girder discrims EXPLICIT in T4
5836
5837    --  Field Corresponding_Discriminant (abbreviated CD below) allows us to
5838    --  find the corresponding discriminant in the parent type, while
5839    --  Original_Record_Component (abbreviated ORC below), the actual physical
5840    --  component that is renamed. Finally the field Is_Completely_Hidden
5841    --  (abbreviated ICH below) is set for all explicit stored discriminants
5842    --  (see einfo.ads for more info). For the above example this gives:
5843
5844    --                 Discrim     CD        ORC     ICH
5845    --                 ^^^^^^^     ^^        ^^^     ^^^
5846    --                 D1 in R    empty     itself    no
5847    --                 D2 in R    empty     itself    no
5848    --                 D3 in R    empty     itself    no
5849
5850    --                 D1 in T1  D1 in R    itself    no
5851    --                 D2 in T1  D2 in R    itself    no
5852    --                 D3 in T1  D3 in R    itself    no
5853
5854    --                 X1 in T2  D3 in T1  D3 in T2   no
5855    --                 X2 in T2  D1 in T1  D1 in T2   no
5856    --                 D1 in T2   empty    itself    yes
5857    --                 D2 in T2   empty    itself    yes
5858    --                 D3 in T2   empty    itself    yes
5859
5860    --                 X1 in T3  X1 in T2  D3 in T3   no
5861    --                 X2 in T3  X2 in T2  D1 in T3   no
5862    --                 D1 in T3   empty    itself    yes
5863    --                 D2 in T3   empty    itself    yes
5864    --                 D3 in T3   empty    itself    yes
5865
5866    --                 Y  in T4  X1 in T3  D3 in T3   no
5867    --                 D1 in T3   empty    itself    yes
5868    --                 D2 in T3   empty    itself    yes
5869    --                 D3 in T3   empty    itself    yes
5870
5871    --  4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
5872
5873    --  Type derivation for tagged types is fairly straightforward. If no
5874    --  discriminants are specified by the derived type, these are inherited
5875    --  from the parent. No explicit stored discriminants are ever necessary.
5876    --  The only manipulation that is done to the tree is that of adding a
5877    --  _parent field with parent type and constrained to the same constraint
5878    --  specified for the parent in the derived type definition. For instance:
5879
5880    --           type R  (D1, D2, D3 : Int) is tagged record ... end record;
5881    --           type T1 is new R with null record;
5882    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
5883
5884    --  are changed into:
5885
5886    --           type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
5887    --              _parent : R (D1, D2, D3);
5888    --           end record;
5889
5890    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
5891    --              _parent : T1 (X2, 88, X1);
5892    --           end record;
5893
5894    --  The discriminants actually present in R, T1 and T2 as well as their CD,
5895    --  ORC and ICH fields are:
5896
5897    --                 Discrim     CD        ORC     ICH
5898    --                 ^^^^^^^     ^^        ^^^     ^^^
5899    --                 D1 in R    empty     itself    no
5900    --                 D2 in R    empty     itself    no
5901    --                 D3 in R    empty     itself    no
5902
5903    --                 D1 in T1  D1 in R    D1 in R   no
5904    --                 D2 in T1  D2 in R    D2 in R   no
5905    --                 D3 in T1  D3 in R    D3 in R   no
5906
5907    --                 X1 in T2  D3 in T1   D3 in R   no
5908    --                 X2 in T2  D1 in T1   D1 in R   no
5909
5910    --  5. FIRST TRANSFORMATION FOR DERIVED RECORDS
5911    --
5912    --  Regardless of whether we dealing with a tagged or untagged type
5913    --  we will transform all derived type declarations of the form
5914    --
5915    --               type T is new R (...) [with ...];
5916    --  or
5917    --               subtype S is R (...);
5918    --               type T is new S [with ...];
5919    --  into
5920    --               type BT is new R [with ...];
5921    --               subtype T is BT (...);
5922    --
5923    --  That is, the base derived type is constrained only if it has no
5924    --  discriminants. The reason for doing this is that GNAT's semantic model
5925    --  assumes that a base type with discriminants is unconstrained.
5926    --
5927    --  Note that, strictly speaking, the above transformation is not always
5928    --  correct. Consider for instance the following excerpt from ACVC b34011a:
5929    --
5930    --       procedure B34011A is
5931    --          type REC (D : integer := 0) is record
5932    --             I : Integer;
5933    --          end record;
5934
5935    --          package P is
5936    --             type T6 is new Rec;
5937    --             function F return T6;
5938    --          end P;
5939
5940    --          use P;
5941    --          package Q6 is
5942    --             type U is new T6 (Q6.F.I);                   -- ERROR: Q6.F.
5943    --          end Q6;
5944    --
5945    --  The definition of Q6.U is illegal. However transforming Q6.U into
5946
5947    --             type BaseU is new T6;
5948    --             subtype U is BaseU (Q6.F.I)
5949
5950    --  turns U into a legal subtype, which is incorrect. To avoid this problem
5951    --  we always analyze the constraint (in this case (Q6.F.I)) before applying
5952    --  the transformation described above.
5953
5954    --  There is another instance where the above transformation is incorrect.
5955    --  Consider:
5956
5957    --          package Pack is
5958    --             type Base (D : Integer) is tagged null record;
5959    --             procedure P (X : Base);
5960
5961    --             type Der is new Base (2) with null record;
5962    --             procedure P (X : Der);
5963    --          end Pack;
5964
5965    --  Then the above transformation turns this into
5966
5967    --             type Der_Base is new Base with null record;
5968    --             --  procedure P (X : Base) is implicitly inherited here
5969    --             --  as procedure P (X : Der_Base).
5970
5971    --             subtype Der is Der_Base (2);
5972    --             procedure P (X : Der);
5973    --             --  The overriding of P (X : Der_Base) is illegal since we
5974    --             --  have a parameter conformance problem.
5975
5976    --  To get around this problem, after having semantically processed Der_Base
5977    --  and the rewritten subtype declaration for Der, we copy Der_Base field
5978    --  Discriminant_Constraint from Der so that when parameter conformance is
5979    --  checked when P is overridden, no semantic errors are flagged.
5980
5981    --  6. SECOND TRANSFORMATION FOR DERIVED RECORDS
5982
5983    --  Regardless of whether we are dealing with a tagged or untagged type
5984    --  we will transform all derived type declarations of the form
5985
5986    --               type R (D1, .., Dn : ...) is [tagged] record ...;
5987    --               type T is new R [with ...];
5988    --  into
5989    --               type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
5990
5991    --  The reason for such transformation is that it allows us to implement a
5992    --  very clean form of component inheritance as explained below.
5993
5994    --  Note that this transformation is not achieved by direct tree rewriting
5995    --  and manipulation, but rather by redoing the semantic actions that the
5996    --  above transformation will entail. This is done directly in routine
5997    --  Inherit_Components.
5998
5999    --  7. TYPE DERIVATION AND COMPONENT INHERITANCE
6000
6001    --  In both tagged and untagged derived types, regular non discriminant
6002    --  components are inherited in the derived type from the parent type. In
6003    --  the absence of discriminants component, inheritance is straightforward
6004    --  as components can simply be copied from the parent.
6005
6006    --  If the parent has discriminants, inheriting components constrained with
6007    --  these discriminants requires caution. Consider the following example:
6008
6009    --      type R  (D1, D2 : Positive) is [tagged] record
6010    --         S : String (D1 .. D2);
6011    --      end record;
6012
6013    --      type T1                is new R        [with null record];
6014    --      type T2 (X : positive) is new R (1, X) [with null record];
6015
6016    --  As explained in 6. above, T1 is rewritten as
6017    --      type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
6018    --  which makes the treatment for T1 and T2 identical.
6019
6020    --  What we want when inheriting S, is that references to D1 and D2 in R are
6021    --  replaced with references to their correct constraints, i.e. D1 and D2 in
6022    --  T1 and 1 and X in T2. So all R's discriminant references are replaced
6023    --  with either discriminant references in the derived type or expressions.
6024    --  This replacement is achieved as follows: before inheriting R's
6025    --  components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
6026    --  created in the scope of T1 (resp. scope of T2) so that discriminants D1
6027    --  and D2 of T1 are visible (resp. discriminant X of T2 is visible).
6028    --  For T2, for instance, this has the effect of replacing String (D1 .. D2)
6029    --  by String (1 .. X).
6030
6031    --  8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
6032
6033    --  We explain here the rules governing private type extensions relevant to
6034    --  type derivation. These rules are explained on the following example:
6035
6036    --      type D [(...)] is new A [(...)] with private;      <-- partial view
6037    --      type D [(...)] is new P [(...)] with null record;  <-- full view
6038
6039    --  Type A is called the ancestor subtype of the private extension.
6040    --  Type P is the parent type of the full view of the private extension. It
6041    --  must be A or a type derived from A.
6042
6043    --  The rules concerning the discriminants of private type extensions are
6044    --  [7.3(10-13)]:
6045
6046    --  o If a private extension inherits known discriminants from the ancestor
6047    --    subtype, then the full view shall also inherit its discriminants from
6048    --    the ancestor subtype and the parent subtype of the full view shall be
6049    --    constrained if and only if the ancestor subtype is constrained.
6050
6051    --  o If a partial view has unknown discriminants, then the full view may
6052    --    define a definite or an indefinite subtype, with or without
6053    --    discriminants.
6054
6055    --  o If a partial view has neither known nor unknown discriminants, then
6056    --    the full view shall define a definite subtype.
6057
6058    --  o If the ancestor subtype of a private extension has constrained
6059    --    discriminants, then the parent subtype of the full view shall impose a
6060    --    statically matching constraint on those discriminants.
6061
6062    --  This means that only the following forms of private extensions are
6063    --  allowed:
6064
6065    --      type D is new A with private;      <-- partial view
6066    --      type D is new P with null record;  <-- full view
6067
6068    --  If A has no discriminants than P has no discriminants, otherwise P must
6069    --  inherit A's discriminants.
6070
6071    --      type D is new A (...) with private;      <-- partial view
6072    --      type D is new P (:::) with null record;  <-- full view
6073
6074    --  P must inherit A's discriminants and (...) and (:::) must statically
6075    --  match.
6076
6077    --      subtype A is R (...);
6078    --      type D is new A with private;      <-- partial view
6079    --      type D is new P with null record;  <-- full view
6080
6081    --  P must have inherited R's discriminants and must be derived from A or
6082    --  any of its subtypes.
6083
6084    --      type D (..) is new A with private;              <-- partial view
6085    --      type D (..) is new P [(:::)] with null record;  <-- full view
6086
6087    --  No specific constraints on P's discriminants or constraint (:::).
6088    --  Note that A can be unconstrained, but the parent subtype P must either
6089    --  be constrained or (:::) must be present.
6090
6091    --      type D (..) is new A [(...)] with private;      <-- partial view
6092    --      type D (..) is new P [(:::)] with null record;  <-- full view
6093
6094    --  P's constraints on A's discriminants must statically match those
6095    --  imposed by (...).
6096
6097    --  9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
6098
6099    --  The full view of a private extension is handled exactly as described
6100    --  above. The model chose for the private view of a private extension is
6101    --  the same for what concerns discriminants (i.e. they receive the same
6102    --  treatment as in the tagged case). However, the private view of the
6103    --  private extension always inherits the components of the parent base,
6104    --  without replacing any discriminant reference. Strictly speaking this is
6105    --  incorrect. However, Gigi never uses this view to generate code so this
6106    --  is a purely semantic issue. In theory, a set of transformations similar
6107    --  to those given in 5. and 6. above could be applied to private views of
6108    --  private extensions to have the same model of component inheritance as
6109    --  for non private extensions. However, this is not done because it would
6110    --  further complicate private type processing. Semantically speaking, this
6111    --  leaves us in an uncomfortable situation. As an example consider:
6112
6113    --          package Pack is
6114    --             type R (D : integer) is tagged record
6115    --                S : String (1 .. D);
6116    --             end record;
6117    --             procedure P (X : R);
6118    --             type T is new R (1) with private;
6119    --          private
6120    --             type T is new R (1) with null record;
6121    --          end;
6122
6123    --  This is transformed into:
6124
6125    --          package Pack is
6126    --             type R (D : integer) is tagged record
6127    --                S : String (1 .. D);
6128    --             end record;
6129    --             procedure P (X : R);
6130    --             type T is new R (1) with private;
6131    --          private
6132    --             type BaseT is new R with null record;
6133    --             subtype  T is BaseT (1);
6134    --          end;
6135
6136    --  (strictly speaking the above is incorrect Ada)
6137
6138    --  From the semantic standpoint the private view of private extension T
6139    --  should be flagged as constrained since one can clearly have
6140    --
6141    --             Obj : T;
6142    --
6143    --  in a unit withing Pack. However, when deriving subprograms for the
6144    --  private view of private extension T, T must be seen as unconstrained
6145    --  since T has discriminants (this is a constraint of the current
6146    --  subprogram derivation model). Thus, when processing the private view of
6147    --  a private extension such as T, we first mark T as unconstrained, we
6148    --  process it, we perform program derivation and just before returning from
6149    --  Build_Derived_Record_Type we mark T as constrained.
6150
6151    --  ??? Are there are other uncomfortable cases that we will have to
6152    --      deal with.
6153
6154    --  10. RECORD_TYPE_WITH_PRIVATE complications
6155
6156    --  Types that are derived from a visible record type and have a private
6157    --  extension present other peculiarities. They behave mostly like private
6158    --  types, but if they have primitive operations defined, these will not
6159    --  have the proper signatures for further inheritance, because other
6160    --  primitive operations will use the implicit base that we define for
6161    --  private derivations below. This affect subprogram inheritance (see
6162    --  Derive_Subprograms for details). We also derive the implicit base from
6163    --  the base type of the full view, so that the implicit base is a record
6164    --  type and not another private type, This avoids infinite loops.
6165
6166    procedure Build_Derived_Record_Type
6167      (N            : Node_Id;
6168       Parent_Type  : Entity_Id;
6169       Derived_Type : Entity_Id;
6170       Derive_Subps : Boolean := True)
6171    is
6172       Loc          : constant Source_Ptr := Sloc (N);
6173       Parent_Base  : Entity_Id;
6174       Type_Def     : Node_Id;
6175       Indic        : Node_Id;
6176       Discrim      : Entity_Id;
6177       Last_Discrim : Entity_Id;
6178       Constrs      : Elist_Id;
6179
6180       Discs : Elist_Id := New_Elmt_List;
6181       --  An empty Discs list means that there were no constraints in the
6182       --  subtype indication or that there was an error processing it.
6183
6184       Assoc_List : Elist_Id;
6185       New_Discrs : Elist_Id;
6186       New_Base   : Entity_Id;
6187       New_Decl   : Node_Id;
6188       New_Indic  : Node_Id;
6189
6190       Is_Tagged          : constant Boolean := Is_Tagged_Type (Parent_Type);
6191       Discriminant_Specs : constant Boolean :=
6192                              Present (Discriminant_Specifications (N));
6193       Private_Extension  : constant Boolean :=
6194                              Nkind (N) = N_Private_Extension_Declaration;
6195
6196       Constraint_Present : Boolean;
6197       Inherit_Discrims   : Boolean := False;
6198       Save_Etype         : Entity_Id;
6199       Save_Discr_Constr  : Elist_Id;
6200       Save_Next_Entity   : Entity_Id;
6201
6202    begin
6203       if Ekind (Parent_Type) = E_Record_Type_With_Private
6204         and then Present (Full_View (Parent_Type))
6205         and then Has_Discriminants (Parent_Type)
6206       then
6207          Parent_Base := Base_Type (Full_View (Parent_Type));
6208       else
6209          Parent_Base := Base_Type (Parent_Type);
6210       end if;
6211
6212       --  Before we start the previously documented transformations, here is
6213       --  little fix for size and alignment of tagged types. Normally when we
6214       --  derive type D from type P, we copy the size and alignment of P as the
6215       --  default for D, and in the absence of explicit representation clauses
6216       --  for D, the size and alignment are indeed the same as the parent.
6217
6218       --  But this is wrong for tagged types, since fields may be added, and
6219       --  the default size may need to be larger, and the default alignment may
6220       --  need to be larger.
6221
6222       --  We therefore reset the size and alignment fields in the tagged case.
6223       --  Note that the size and alignment will in any case be at least as
6224       --  large as the parent type (since the derived type has a copy of the
6225       --  parent type in the _parent field)
6226
6227       --  The type is also marked as being tagged here, which is needed when
6228       --  processing components with a self-referential anonymous access type
6229       --  in the call to Check_Anonymous_Access_Components below. Note that
6230       --  this flag is also set later on for completeness.
6231
6232       if Is_Tagged then
6233          Set_Is_Tagged_Type (Derived_Type);
6234          Init_Size_Align    (Derived_Type);
6235       end if;
6236
6237       --  STEP 0a: figure out what kind of derived type declaration we have
6238
6239       if Private_Extension then
6240          Type_Def := N;
6241          Set_Ekind (Derived_Type, E_Record_Type_With_Private);
6242
6243       else
6244          Type_Def := Type_Definition (N);
6245
6246          --  Ekind (Parent_Base) is not necessarily E_Record_Type since
6247          --  Parent_Base can be a private type or private extension. However,
6248          --  for tagged types with an extension the newly added fields are
6249          --  visible and hence the Derived_Type is always an E_Record_Type.
6250          --  (except that the parent may have its own private fields).
6251          --  For untagged types we preserve the Ekind of the Parent_Base.
6252
6253          if Present (Record_Extension_Part (Type_Def)) then
6254             Set_Ekind (Derived_Type, E_Record_Type);
6255
6256             --  Create internal access types for components with anonymous
6257             --  access types.
6258
6259             if Ada_Version >= Ada_05 then
6260                Check_Anonymous_Access_Components
6261                  (N, Derived_Type, Derived_Type,
6262                    Component_List (Record_Extension_Part (Type_Def)));
6263             end if;
6264
6265          else
6266             Set_Ekind (Derived_Type, Ekind (Parent_Base));
6267          end if;
6268       end if;
6269
6270       --  Indic can either be an N_Identifier if the subtype indication
6271       --  contains no constraint or an N_Subtype_Indication if the subtype
6272       --  indication has a constraint.
6273
6274       Indic := Subtype_Indication (Type_Def);
6275       Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
6276
6277       --  Check that the type has visible discriminants. The type may be
6278       --  a private type with unknown discriminants whose full view has
6279       --  discriminants which are invisible.
6280
6281       if Constraint_Present then
6282          if not Has_Discriminants (Parent_Base)
6283            or else
6284              (Has_Unknown_Discriminants (Parent_Base)
6285                 and then Is_Private_Type (Parent_Base))
6286          then
6287             Error_Msg_N
6288               ("invalid constraint: type has no discriminant",
6289                  Constraint (Indic));
6290
6291             Constraint_Present := False;
6292             Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6293
6294          elsif Is_Constrained (Parent_Type) then
6295             Error_Msg_N
6296                ("invalid constraint: parent type is already constrained",
6297                   Constraint (Indic));
6298
6299             Constraint_Present := False;
6300             Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6301          end if;
6302       end if;
6303
6304       --  STEP 0b: If needed, apply transformation given in point 5. above
6305
6306       if not Private_Extension
6307         and then Has_Discriminants (Parent_Type)
6308         and then not Discriminant_Specs
6309         and then (Is_Constrained (Parent_Type) or else Constraint_Present)
6310       then
6311          --  First, we must analyze the constraint (see comment in point 5.)
6312
6313          if Constraint_Present then
6314             New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
6315
6316             if Has_Discriminants (Derived_Type)
6317               and then Has_Private_Declaration (Derived_Type)
6318               and then Present (Discriminant_Constraint (Derived_Type))
6319             then
6320                --  Verify that constraints of the full view statically match
6321                --  those given in the partial view.
6322
6323                declare
6324                   C1, C2 : Elmt_Id;
6325
6326                begin
6327                   C1 := First_Elmt (New_Discrs);
6328                   C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
6329                   while Present (C1) and then Present (C2) loop
6330                      if Fully_Conformant_Expressions (Node (C1), Node (C2))
6331                        or else
6332                          (Is_OK_Static_Expression (Node (C1))
6333                             and then
6334                           Is_OK_Static_Expression (Node (C2))
6335                             and then
6336                           Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
6337                      then
6338                         null;
6339
6340                      else
6341                         Error_Msg_N (
6342                           "constraint not conformant to previous declaration",
6343                              Node (C1));
6344                      end if;
6345
6346                      Next_Elmt (C1);
6347                      Next_Elmt (C2);
6348                   end loop;
6349                end;
6350             end if;
6351          end if;
6352
6353          --  Insert and analyze the declaration for the unconstrained base type
6354
6355          New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
6356
6357          New_Decl :=
6358            Make_Full_Type_Declaration (Loc,
6359               Defining_Identifier => New_Base,
6360               Type_Definition     =>
6361                 Make_Derived_Type_Definition (Loc,
6362                   Abstract_Present      => Abstract_Present (Type_Def),
6363                   Subtype_Indication    =>
6364                     New_Occurrence_Of (Parent_Base, Loc),
6365                   Record_Extension_Part =>
6366                     Relocate_Node (Record_Extension_Part (Type_Def))));
6367
6368          Set_Parent (New_Decl, Parent (N));
6369          Mark_Rewrite_Insertion (New_Decl);
6370          Insert_Before (N, New_Decl);
6371
6372          --  Note that this call passes False for the Derive_Subps parameter
6373          --  because subprogram derivation is deferred until after creating
6374          --  the subtype (see below).
6375
6376          Build_Derived_Type
6377            (New_Decl, Parent_Base, New_Base,
6378             Is_Completion => True, Derive_Subps => False);
6379
6380          --  ??? This needs re-examination to determine whether the
6381          --  above call can simply be replaced by a call to Analyze.
6382
6383          Set_Analyzed (New_Decl);
6384
6385          --  Insert and analyze the declaration for the constrained subtype
6386
6387          if Constraint_Present then
6388             New_Indic :=
6389               Make_Subtype_Indication (Loc,
6390                 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6391                 Constraint   => Relocate_Node (Constraint (Indic)));
6392
6393          else
6394             declare
6395                Constr_List : constant List_Id := New_List;
6396                C           : Elmt_Id;
6397                Expr        : Node_Id;
6398
6399             begin
6400                C := First_Elmt (Discriminant_Constraint (Parent_Type));
6401                while Present (C) loop
6402                   Expr := Node (C);
6403
6404                   --  It is safe here to call New_Copy_Tree since
6405                   --  Force_Evaluation was called on each constraint in
6406                   --  Build_Discriminant_Constraints.
6407
6408                   Append (New_Copy_Tree (Expr), To => Constr_List);
6409
6410                   Next_Elmt (C);
6411                end loop;
6412
6413                New_Indic :=
6414                  Make_Subtype_Indication (Loc,
6415                    Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6416                    Constraint   =>
6417                      Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
6418             end;
6419          end if;
6420
6421          Rewrite (N,
6422            Make_Subtype_Declaration (Loc,
6423              Defining_Identifier => Derived_Type,
6424              Subtype_Indication  => New_Indic));
6425
6426          Analyze (N);
6427
6428          --  Derivation of subprograms must be delayed until the full subtype
6429          --  has been established to ensure proper overriding of subprograms
6430          --  inherited by full types. If the derivations occurred as part of
6431          --  the call to Build_Derived_Type above, then the check for type
6432          --  conformance would fail because earlier primitive subprograms
6433          --  could still refer to the full type prior the change to the new
6434          --  subtype and hence would not match the new base type created here.
6435
6436          Derive_Subprograms (Parent_Type, Derived_Type);
6437
6438          --  For tagged types the Discriminant_Constraint of the new base itype
6439          --  is inherited from the first subtype so that no subtype conformance
6440          --  problem arise when the first subtype overrides primitive
6441          --  operations inherited by the implicit base type.
6442
6443          if Is_Tagged then
6444             Set_Discriminant_Constraint
6445               (New_Base, Discriminant_Constraint (Derived_Type));
6446          end if;
6447
6448          return;
6449       end if;
6450
6451       --  If we get here Derived_Type will have no discriminants or it will be
6452       --  a discriminated unconstrained base type.
6453
6454       --  STEP 1a: perform preliminary actions/checks for derived tagged types
6455
6456       if Is_Tagged then
6457
6458          --  The parent type is frozen for non-private extensions (RM 13.14(7))
6459          --  The declaration of a specific descendant of an interface type
6460          --  freezes the interface type (RM 13.14).
6461
6462          if not Private_Extension
6463            or else Is_Interface (Parent_Base)
6464          then
6465             Freeze_Before (N, Parent_Type);
6466          end if;
6467
6468          --  In Ada 2005 (AI-344), the restriction that a derived tagged type
6469          --  cannot be declared at a deeper level than its parent type is
6470          --  removed. The check on derivation within a generic body is also
6471          --  relaxed, but there's a restriction that a derived tagged type
6472          --  cannot be declared in a generic body if it's derived directly
6473          --  or indirectly from a formal type of that generic.
6474
6475          if Ada_Version >= Ada_05 then
6476             if Present (Enclosing_Generic_Body (Derived_Type)) then
6477                declare
6478                   Ancestor_Type : Entity_Id;
6479
6480                begin
6481                   --  Check to see if any ancestor of the derived type is a
6482                   --  formal type.
6483
6484                   Ancestor_Type := Parent_Type;
6485                   while not Is_Generic_Type (Ancestor_Type)
6486                     and then Etype (Ancestor_Type) /= Ancestor_Type
6487                   loop
6488                      Ancestor_Type := Etype (Ancestor_Type);
6489                   end loop;
6490
6491                   --  If the derived type does have a formal type as an
6492                   --  ancestor, then it's an error if the derived type is
6493                   --  declared within the body of the generic unit that
6494                   --  declares the formal type in its generic formal part. It's
6495                   --  sufficient to check whether the ancestor type is declared
6496                   --  inside the same generic body as the derived type (such as
6497                   --  within a nested generic spec), in which case the
6498                   --  derivation is legal. If the formal type is declared
6499                   --  outside of that generic body, then it's guaranteed that
6500                   --  the derived type is declared within the generic body of
6501                   --  the generic unit declaring the formal type.
6502
6503                   if Is_Generic_Type (Ancestor_Type)
6504                     and then Enclosing_Generic_Body (Ancestor_Type) /=
6505                                Enclosing_Generic_Body (Derived_Type)
6506                   then
6507                      Error_Msg_NE
6508                        ("parent type of& must not be descendant of formal type"
6509                           & " of an enclosing generic body",
6510                             Indic, Derived_Type);
6511                   end if;
6512                end;
6513             end if;
6514
6515          elsif Type_Access_Level (Derived_Type) /=
6516                  Type_Access_Level (Parent_Type)
6517            and then not Is_Generic_Type (Derived_Type)
6518          then
6519             if Is_Controlled (Parent_Type) then
6520                Error_Msg_N
6521                  ("controlled type must be declared at the library level",
6522                   Indic);
6523             else
6524                Error_Msg_N
6525                  ("type extension at deeper accessibility level than parent",
6526                   Indic);
6527             end if;
6528
6529          else
6530             declare
6531                GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
6532
6533             begin
6534                if Present (GB)
6535                  and then GB /= Enclosing_Generic_Body (Parent_Base)
6536                then
6537                   Error_Msg_NE
6538                     ("parent type of& must not be outside generic body"
6539                        & " (RM 3.9.1(4))",
6540                          Indic, Derived_Type);
6541                end if;
6542             end;
6543          end if;
6544       end if;
6545
6546       --  Ada 2005 (AI-251)
6547
6548       if Ada_Version = Ada_05
6549         and then Is_Tagged
6550       then
6551          --  "The declaration of a specific descendant of an interface type
6552          --  freezes the interface type" (RM 13.14).
6553
6554          declare
6555             Iface : Node_Id;
6556          begin
6557             if Is_Non_Empty_List (Interface_List (Type_Def)) then
6558                Iface := First (Interface_List (Type_Def));
6559                while Present (Iface) loop
6560                   Freeze_Before (N, Etype (Iface));
6561                   Next (Iface);
6562                end loop;
6563             end if;
6564          end;
6565       end if;
6566
6567       --  STEP 1b : preliminary cleanup of the full view of private types
6568
6569       --  If the type is already marked as having discriminants, then it's the
6570       --  completion of a private type or private extension and we need to
6571       --  retain the discriminants from the partial view if the current
6572       --  declaration has Discriminant_Specifications so that we can verify
6573       --  conformance. However, we must remove any existing components that
6574       --  were inherited from the parent (and attached in Copy_And_Swap)
6575       --  because the full type inherits all appropriate components anyway, and
6576       --  we do not want the partial view's components interfering.
6577
6578       if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
6579          Discrim := First_Discriminant (Derived_Type);
6580          loop
6581             Last_Discrim := Discrim;
6582             Next_Discriminant (Discrim);
6583             exit when No (Discrim);
6584          end loop;
6585
6586          Set_Last_Entity (Derived_Type, Last_Discrim);
6587
6588       --  In all other cases wipe out the list of inherited components (even
6589       --  inherited discriminants), it will be properly rebuilt here.
6590
6591       else
6592          Set_First_Entity (Derived_Type, Empty);
6593          Set_Last_Entity  (Derived_Type, Empty);
6594       end if;
6595
6596       --  STEP 1c: Initialize some flags for the Derived_Type
6597
6598       --  The following flags must be initialized here so that
6599       --  Process_Discriminants can check that discriminants of tagged types do
6600       --  not have a default initial value and that access discriminants are
6601       --  only specified for limited records. For completeness, these flags are
6602       --  also initialized along with all the other flags below.
6603
6604       --  AI-419: Limitedness is not inherited from an interface parent, so to
6605       --  be limited in that case the type must be explicitly declared as
6606       --  limited. However, task and protected interfaces are always limited.
6607
6608       if Limited_Present (Type_Def) then
6609          Set_Is_Limited_Record (Derived_Type);
6610
6611       elsif Is_Limited_Record (Parent_Type)
6612         or else (Present (Full_View (Parent_Type))
6613                    and then Is_Limited_Record (Full_View (Parent_Type)))
6614       then
6615          if not Is_Interface (Parent_Type)
6616            or else Is_Synchronized_Interface (Parent_Type)
6617            or else Is_Protected_Interface (Parent_Type)
6618            or else Is_Task_Interface (Parent_Type)
6619          then
6620             Set_Is_Limited_Record (Derived_Type);
6621          end if;
6622       end if;
6623
6624       --  STEP 2a: process discriminants of derived type if any
6625
6626       Push_Scope (Derived_Type);
6627
6628       if Discriminant_Specs then
6629          Set_Has_Unknown_Discriminants (Derived_Type, False);
6630
6631          --  The following call initializes fields Has_Discriminants and
6632          --  Discriminant_Constraint, unless we are processing the completion
6633          --  of a private type declaration.
6634
6635          Check_Or_Process_Discriminants (N, Derived_Type);
6636
6637          --  For non-tagged types the constraint on the Parent_Type must be
6638          --  present and is used to rename the discriminants.
6639
6640          if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
6641             Error_Msg_N ("untagged parent must have discriminants", Indic);
6642
6643          elsif not Is_Tagged and then not Constraint_Present then
6644             Error_Msg_N
6645               ("discriminant constraint needed for derived untagged records",
6646                Indic);
6647
6648          --  Otherwise the parent subtype must be constrained unless we have a
6649          --  private extension.
6650
6651          elsif not Constraint_Present
6652            and then not Private_Extension
6653            and then not Is_Constrained (Parent_Type)
6654          then
6655             Error_Msg_N
6656               ("unconstrained type not allowed in this context", Indic);
6657
6658          elsif Constraint_Present then
6659             --  The following call sets the field Corresponding_Discriminant
6660             --  for the discriminants in the Derived_Type.
6661
6662             Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
6663
6664             --  For untagged types all new discriminants must rename
6665             --  discriminants in the parent. For private extensions new
6666             --  discriminants cannot rename old ones (implied by [7.3(13)]).
6667
6668             Discrim := First_Discriminant (Derived_Type);
6669             while Present (Discrim) loop
6670                if not Is_Tagged
6671                  and then No (Corresponding_Discriminant (Discrim))
6672                then
6673                   Error_Msg_N
6674                     ("new discriminants must constrain old ones", Discrim);
6675
6676                elsif Private_Extension
6677                  and then Present (Corresponding_Discriminant (Discrim))
6678                then
6679                   Error_Msg_N
6680                     ("only static constraints allowed for parent"
6681                      & " discriminants in the partial view", Indic);
6682                   exit;
6683                end if;
6684
6685                --  If a new discriminant is used in the constraint, then its
6686                --  subtype must be statically compatible with the parent
6687                --  discriminant's subtype (3.7(15)).
6688
6689                if Present (Corresponding_Discriminant (Discrim))
6690                  and then
6691                    not Subtypes_Statically_Compatible
6692                          (Etype (Discrim),
6693                           Etype (Corresponding_Discriminant (Discrim)))
6694                then
6695                   Error_Msg_N
6696                     ("subtype must be compatible with parent discriminant",
6697                      Discrim);
6698                end if;
6699
6700                Next_Discriminant (Discrim);
6701             end loop;
6702
6703             --  Check whether the constraints of the full view statically
6704             --  match those imposed by the parent subtype [7.3(13)].
6705
6706             if Present (Stored_Constraint (Derived_Type)) then
6707                declare
6708                   C1, C2 : Elmt_Id;
6709
6710                begin
6711                   C1 := First_Elmt (Discs);
6712                   C2 := First_Elmt (Stored_Constraint (Derived_Type));
6713                   while Present (C1) and then Present (C2) loop
6714                      if not
6715                        Fully_Conformant_Expressions (Node (C1), Node (C2))
6716                      then
6717                         Error_Msg_N
6718                           ("not conformant with previous declaration",
6719                            Node (C1));
6720                      end if;
6721
6722                      Next_Elmt (C1);
6723                      Next_Elmt (C2);
6724                   end loop;
6725                end;
6726             end if;
6727          end if;
6728
6729       --  STEP 2b: No new discriminants, inherit discriminants if any
6730
6731       else
6732          if Private_Extension then
6733             Set_Has_Unknown_Discriminants
6734               (Derived_Type,
6735                Has_Unknown_Discriminants (Parent_Type)
6736                  or else Unknown_Discriminants_Present (N));
6737
6738          --  The partial view of the parent may have unknown discriminants,
6739          --  but if the full view has discriminants and the parent type is
6740          --  in scope they must be inherited.
6741
6742          elsif Has_Unknown_Discriminants (Parent_Type)
6743            and then
6744             (not Has_Discriminants (Parent_Type)
6745               or else not In_Open_Scopes (Scope (Parent_Type)))
6746          then
6747             Set_Has_Unknown_Discriminants (Derived_Type);
6748          end if;
6749
6750          if not Has_Unknown_Discriminants (Derived_Type)
6751            and then not Has_Unknown_Discriminants (Parent_Base)
6752            and then Has_Discriminants (Parent_Type)
6753          then
6754             Inherit_Discrims := True;
6755             Set_Has_Discriminants
6756               (Derived_Type, True);
6757             Set_Discriminant_Constraint
6758               (Derived_Type, Discriminant_Constraint (Parent_Base));
6759          end if;
6760
6761          --  The following test is true for private types (remember
6762          --  transformation 5. is not applied to those) and in an error
6763          --  situation.
6764
6765          if Constraint_Present then
6766             Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
6767          end if;
6768
6769          --  For now mark a new derived type as constrained only if it has no
6770          --  discriminants. At the end of Build_Derived_Record_Type we properly
6771          --  set this flag in the case of private extensions. See comments in
6772          --  point 9. just before body of Build_Derived_Record_Type.
6773
6774          Set_Is_Constrained
6775            (Derived_Type,
6776             not (Inherit_Discrims
6777                    or else Has_Unknown_Discriminants (Derived_Type)));
6778       end if;
6779
6780       --  STEP 3: initialize fields of derived type
6781
6782       Set_Is_Tagged_Type    (Derived_Type, Is_Tagged);
6783       Set_Stored_Constraint (Derived_Type, No_Elist);
6784
6785       --  Ada 2005 (AI-251): Private type-declarations can implement interfaces
6786       --  but cannot be interfaces
6787
6788       if not Private_Extension
6789          and then Ekind (Derived_Type) /= E_Private_Type
6790          and then Ekind (Derived_Type) /= E_Limited_Private_Type
6791       then
6792          if Interface_Present (Type_Def) then
6793             Analyze_Interface_Declaration (Derived_Type, Type_Def);
6794          end if;
6795
6796          Set_Interfaces (Derived_Type, No_Elist);
6797       end if;
6798
6799       --  Fields inherited from the Parent_Type
6800
6801       Set_Discard_Names
6802         (Derived_Type, Einfo.Discard_Names      (Parent_Type));
6803       Set_Has_Specified_Layout
6804         (Derived_Type, Has_Specified_Layout     (Parent_Type));
6805       Set_Is_Limited_Composite
6806         (Derived_Type, Is_Limited_Composite     (Parent_Type));
6807       Set_Is_Private_Composite
6808         (Derived_Type, Is_Private_Composite     (Parent_Type));
6809
6810       --  Fields inherited from the Parent_Base
6811
6812       Set_Has_Controlled_Component
6813         (Derived_Type, Has_Controlled_Component (Parent_Base));
6814       Set_Has_Non_Standard_Rep
6815         (Derived_Type, Has_Non_Standard_Rep     (Parent_Base));
6816       Set_Has_Primitive_Operations
6817         (Derived_Type, Has_Primitive_Operations (Parent_Base));
6818
6819       --  Fields inherited from the Parent_Base in the non-private case
6820
6821       if Ekind (Derived_Type) = E_Record_Type then
6822          Set_Has_Complex_Representation
6823            (Derived_Type, Has_Complex_Representation (Parent_Base));
6824       end if;
6825
6826       --  Fields inherited from the Parent_Base for record types
6827
6828       if Is_Record_Type (Derived_Type) then
6829          Set_OK_To_Reorder_Components
6830            (Derived_Type, OK_To_Reorder_Components (Parent_Base));
6831          Set_Reverse_Bit_Order
6832            (Derived_Type, Reverse_Bit_Order (Parent_Base));
6833       end if;
6834
6835       --  Direct controlled types do not inherit Finalize_Storage_Only flag
6836
6837       if not Is_Controlled (Parent_Type) then
6838          Set_Finalize_Storage_Only
6839            (Derived_Type, Finalize_Storage_Only (Parent_Type));
6840       end if;
6841
6842       --  Set fields for private derived types
6843
6844       if Is_Private_Type (Derived_Type) then
6845          Set_Depends_On_Private (Derived_Type, True);
6846          Set_Private_Dependents (Derived_Type, New_Elmt_List);
6847
6848       --  Inherit fields from non private record types. If this is the
6849       --  completion of a derivation from a private type, the parent itself
6850       --  is private, and the attributes come from its full view, which must
6851       --  be present.
6852
6853       else
6854          if Is_Private_Type (Parent_Base)
6855            and then not Is_Record_Type (Parent_Base)
6856          then
6857             Set_Component_Alignment
6858               (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
6859             Set_C_Pass_By_Copy
6860               (Derived_Type, C_Pass_By_Copy      (Full_View (Parent_Base)));
6861          else
6862             Set_Component_Alignment
6863               (Derived_Type, Component_Alignment (Parent_Base));
6864
6865             Set_C_Pass_By_Copy
6866               (Derived_Type, C_Pass_By_Copy      (Parent_Base));
6867          end if;
6868       end if;
6869
6870       --  Set fields for tagged types
6871
6872       if Is_Tagged then
6873          Set_Primitive_Operations (Derived_Type, New_Elmt_List);
6874
6875          --  All tagged types defined in Ada.Finalization are controlled
6876
6877          if Chars (Scope (Derived_Type)) = Name_Finalization
6878            and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
6879            and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
6880          then
6881             Set_Is_Controlled (Derived_Type);
6882          else
6883             Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
6884          end if;
6885
6886          Make_Class_Wide_Type (Derived_Type);
6887          Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
6888
6889          if Has_Discriminants (Derived_Type)
6890            and then Constraint_Present
6891          then
6892             Set_Stored_Constraint
6893               (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
6894          end if;
6895
6896          if Ada_Version >= Ada_05 then
6897             declare
6898                Ifaces_List : Elist_Id;
6899
6900             begin
6901                --  Checks rules 3.9.4 (13/2 and 14/2)
6902
6903                if Comes_From_Source (Derived_Type)
6904                  and then not Is_Private_Type (Derived_Type)
6905                  and then Is_Interface (Parent_Type)
6906                  and then not Is_Interface (Derived_Type)
6907                then
6908                   if Is_Task_Interface (Parent_Type) then
6909                      Error_Msg_N
6910                        ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
6911                         Derived_Type);
6912
6913                   elsif Is_Protected_Interface (Parent_Type) then
6914                      Error_Msg_N
6915                        ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
6916                         Derived_Type);
6917                   end if;
6918                end if;
6919
6920                --  Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
6921
6922                Check_Interfaces (N, Type_Def);
6923
6924                --  Ada 2005 (AI-251): Collect the list of progenitors that are
6925                --  not already in the parents.
6926
6927                Collect_Interfaces
6928                  (T               => Derived_Type,
6929                   Ifaces_List     => Ifaces_List,
6930                   Exclude_Parents => True);
6931
6932                Set_Interfaces (Derived_Type, Ifaces_List);
6933             end;
6934          end if;
6935
6936       else
6937          Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
6938          Set_Has_Non_Standard_Rep
6939                        (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
6940       end if;
6941
6942       --  STEP 4: Inherit components from the parent base and constrain them.
6943       --          Apply the second transformation described in point 6. above.
6944
6945       if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
6946         or else not Has_Discriminants (Parent_Type)
6947         or else not Is_Constrained (Parent_Type)
6948       then
6949          Constrs := Discs;
6950       else
6951          Constrs := Discriminant_Constraint (Parent_Type);
6952       end if;
6953
6954       Assoc_List :=
6955         Inherit_Components
6956           (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
6957
6958       --  STEP 5a: Copy the parent record declaration for untagged types
6959
6960       if not Is_Tagged then
6961
6962          --  Discriminant_Constraint (Derived_Type) has been properly
6963          --  constructed. Save it and temporarily set it to Empty because we
6964          --  do not want the call to New_Copy_Tree below to mess this list.
6965
6966          if Has_Discriminants (Derived_Type) then
6967             Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
6968             Set_Discriminant_Constraint (Derived_Type, No_Elist);
6969          else
6970             Save_Discr_Constr := No_Elist;
6971          end if;
6972
6973          --  Save the Etype field of Derived_Type. It is correctly set now,
6974          --  but the call to New_Copy tree may remap it to point to itself,
6975          --  which is not what we want. Ditto for the Next_Entity field.
6976
6977          Save_Etype       := Etype (Derived_Type);
6978          Save_Next_Entity := Next_Entity (Derived_Type);
6979
6980          --  Assoc_List maps all stored discriminants in the Parent_Base to
6981          --  stored discriminants in the Derived_Type. It is fundamental that
6982          --  no types or itypes with discriminants other than the stored
6983          --  discriminants appear in the entities declared inside
6984          --  Derived_Type, since the back end cannot deal with it.
6985
6986          New_Decl :=
6987            New_Copy_Tree
6988              (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
6989
6990          --  Restore the fields saved prior to the New_Copy_Tree call
6991          --  and compute the stored constraint.
6992
6993          Set_Etype       (Derived_Type, Save_Etype);
6994          Set_Next_Entity (Derived_Type, Save_Next_Entity);
6995
6996          if Has_Discriminants (Derived_Type) then
6997             Set_Discriminant_Constraint
6998               (Derived_Type, Save_Discr_Constr);
6999             Set_Stored_Constraint
7000               (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
7001             Replace_Components (Derived_Type, New_Decl);
7002          end if;
7003
7004          --  Insert the new derived type declaration
7005
7006          Rewrite (N, New_Decl);
7007
7008       --  STEP 5b: Complete the processing for record extensions in generics
7009
7010       --  There is no completion for record extensions declared in the
7011       --  parameter part of a generic, so we need to complete processing for
7012       --  these generic record extensions here. The Record_Type_Definition call
7013       --  will change the Ekind of the components from E_Void to E_Component.
7014
7015       elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
7016          Record_Type_Definition (Empty, Derived_Type);
7017
7018       --  STEP 5c: Process the record extension for non private tagged types
7019
7020       elsif not Private_Extension then
7021
7022          --  Add the _parent field in the derived type
7023
7024          Expand_Record_Extension (Derived_Type, Type_Def);
7025
7026          --  Ada 2005 (AI-251): Addition of the Tag corresponding to all the
7027          --  implemented interfaces if we are in expansion mode
7028
7029          if Expander_Active
7030            and then Has_Interfaces (Derived_Type)
7031          then
7032             Add_Interface_Tag_Components (N, Derived_Type);
7033          end if;
7034
7035          --  Analyze the record extension
7036
7037          Record_Type_Definition
7038            (Record_Extension_Part (Type_Def), Derived_Type);
7039       end if;
7040
7041       End_Scope;
7042
7043       --  Nothing else to do if there is an error in the derivation.
7044       --  An unusual case: the full view may be derived from a type in an
7045       --  instance, when the partial view was used illegally as an actual
7046       --  in that instance, leading to a circular definition.
7047
7048       if Etype (Derived_Type) = Any_Type
7049         or else Etype (Parent_Type) = Derived_Type
7050       then
7051          return;
7052       end if;
7053
7054       --  Set delayed freeze and then derive subprograms, we need to do
7055       --  this in this order so that derived subprograms inherit the
7056       --  derived freeze if necessary.
7057
7058       Set_Has_Delayed_Freeze (Derived_Type);
7059
7060       if Derive_Subps then
7061          Derive_Subprograms (Parent_Type, Derived_Type);
7062       end if;
7063
7064       --  If we have a private extension which defines a constrained derived
7065       --  type mark as constrained here after we have derived subprograms. See
7066       --  comment on point 9. just above the body of Build_Derived_Record_Type.
7067
7068       if Private_Extension and then Inherit_Discrims then
7069          if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
7070             Set_Is_Constrained          (Derived_Type, True);
7071             Set_Discriminant_Constraint (Derived_Type, Discs);
7072
7073          elsif Is_Constrained (Parent_Type) then
7074             Set_Is_Constrained
7075               (Derived_Type, True);
7076             Set_Discriminant_Constraint
7077               (Derived_Type, Discriminant_Constraint (Parent_Type));
7078          end if;
7079       end if;
7080
7081       --  Update the class_wide type, which shares the now-completed
7082       --  entity list with its specific type.
7083
7084       if Is_Tagged then
7085          Set_First_Entity
7086            (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
7087          Set_Last_Entity
7088            (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
7089       end if;
7090
7091       --  Update the scope of anonymous access types of discriminants and other
7092       --  components, to prevent scope anomalies in gigi, when the derivation
7093       --  appears in a scope nested within that of the parent.
7094
7095       declare
7096          D : Entity_Id;
7097
7098       begin
7099          D := First_Entity (Derived_Type);
7100          while Present (D) loop
7101             if Ekind (D) = E_Discriminant
7102               or else Ekind (D) = E_Component
7103             then
7104                if Is_Itype (Etype (D))
7105                   and then Ekind (Etype (D)) = E_Anonymous_Access_Type
7106                then
7107                   Set_Scope (Etype (D), Current_Scope);
7108                end if;
7109             end if;
7110
7111             Next_Entity (D);
7112          end loop;
7113       end;
7114    end Build_Derived_Record_Type;
7115
7116    ------------------------
7117    -- Build_Derived_Type --
7118    ------------------------
7119
7120    procedure Build_Derived_Type
7121      (N             : Node_Id;
7122       Parent_Type   : Entity_Id;
7123       Derived_Type  : Entity_Id;
7124       Is_Completion : Boolean;
7125       Derive_Subps  : Boolean := True)
7126    is
7127       Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7128
7129    begin
7130       --  Set common attributes
7131
7132       Set_Scope         (Derived_Type, Current_Scope);
7133
7134       Set_Ekind         (Derived_Type, Ekind    (Parent_Base));
7135       Set_Etype         (Derived_Type,           Parent_Base);
7136       Set_Has_Task      (Derived_Type, Has_Task (Parent_Base));
7137
7138       Set_Size_Info     (Derived_Type,                Parent_Type);
7139       Set_RM_Size       (Derived_Type, RM_Size       (Parent_Type));
7140       Set_Convention    (Derived_Type, Convention    (Parent_Type));
7141       Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7142
7143       --  The derived type inherits the representation clauses of the parent.
7144       --  However, for a private type that is completed by a derivation, there
7145       --  may be operation attributes that have been specified already (stream
7146       --  attributes and External_Tag) and those must be provided. Finally,
7147       --  if the partial view is a private extension, the representation items
7148       --  of the parent have been inherited already, and should not be chained
7149       --  twice to the derived type.
7150
7151       if Is_Tagged_Type (Parent_Type)
7152         and then Present (First_Rep_Item (Derived_Type))
7153       then
7154          --  The existing items are either operational items or items inherited
7155          --  from a private extension declaration.
7156
7157          declare
7158             Rep : Node_Id;
7159             --  Used to iterate over representation items of the derived type
7160
7161             Last_Rep : Node_Id;
7162             --  Last representation item of the (non-empty) representation
7163             --  item list of the derived type.
7164
7165             Found : Boolean := False;
7166
7167          begin
7168             Rep      := First_Rep_Item (Derived_Type);
7169             Last_Rep := Rep;
7170             while Present (Rep) loop
7171                if Rep = First_Rep_Item (Parent_Type) then
7172                   Found := True;
7173                   exit;
7174
7175                else
7176                   Rep := Next_Rep_Item (Rep);
7177
7178                   if Present (Rep) then
7179                      Last_Rep := Rep;
7180                   end if;
7181                end if;
7182             end loop;
7183
7184             --  Here if we either encountered the parent type's first rep
7185             --  item on the derived type's rep item list (in which case
7186             --  Found is True, and we have nothing else to do), or if we
7187             --  reached the last rep item of the derived type, which is
7188             --  Last_Rep, in which case we further chain the parent type's
7189             --  rep items to those of the derived type.
7190
7191             if not Found then
7192                Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
7193             end if;
7194          end;
7195
7196       else
7197          Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
7198       end if;
7199
7200       case Ekind (Parent_Type) is
7201          when Numeric_Kind =>
7202             Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
7203
7204          when Array_Kind =>
7205             Build_Derived_Array_Type (N, Parent_Type,  Derived_Type);
7206
7207          when E_Record_Type
7208             | E_Record_Subtype
7209             | Class_Wide_Kind  =>
7210             Build_Derived_Record_Type
7211               (N, Parent_Type, Derived_Type, Derive_Subps);
7212             return;
7213
7214          when Enumeration_Kind =>
7215             Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
7216
7217          when Access_Kind =>
7218             Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
7219
7220          when Incomplete_Or_Private_Kind =>
7221             Build_Derived_Private_Type
7222               (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
7223
7224             --  For discriminated types, the derivation includes deriving
7225             --  primitive operations. For others it is done below.
7226
7227             if Is_Tagged_Type (Parent_Type)
7228               or else Has_Discriminants (Parent_Type)
7229               or else (Present (Full_View (Parent_Type))
7230                         and then Has_Discriminants (Full_View (Parent_Type)))
7231             then
7232                return;
7233             end if;
7234
7235          when Concurrent_Kind =>
7236             Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
7237
7238          when others =>
7239             raise Program_Error;
7240       end case;
7241
7242       if Etype (Derived_Type) = Any_Type then
7243          return;
7244       end if;
7245
7246       --  Set delayed freeze and then derive subprograms, we need to do this
7247       --  in this order so that derived subprograms inherit the derived freeze
7248       --  if necessary.
7249
7250       Set_Has_Delayed_Freeze (Derived_Type);
7251       if Derive_Subps then
7252          Derive_Subprograms (Parent_Type, Derived_Type);
7253       end if;
7254
7255       Set_Has_Primitive_Operations
7256         (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
7257    end Build_Derived_Type;
7258
7259    -----------------------
7260    -- Build_Discriminal --
7261    -----------------------
7262
7263    procedure Build_Discriminal (Discrim : Entity_Id) is
7264       D_Minal : Entity_Id;
7265       CR_Disc : Entity_Id;
7266
7267    begin
7268       --  A discriminal has the same name as the discriminant
7269
7270       D_Minal :=
7271         Make_Defining_Identifier (Sloc (Discrim),
7272           Chars => Chars (Discrim));
7273
7274       Set_Ekind     (D_Minal, E_In_Parameter);
7275       Set_Mechanism (D_Minal, Default_Mechanism);
7276       Set_Etype     (D_Minal, Etype (Discrim));
7277
7278       Set_Discriminal (Discrim, D_Minal);
7279       Set_Discriminal_Link (D_Minal, Discrim);
7280
7281       --  For task types, build at once the discriminants of the corresponding
7282       --  record, which are needed if discriminants are used in entry defaults
7283       --  and in family bounds.
7284
7285       if Is_Concurrent_Type (Current_Scope)
7286         or else Is_Limited_Type (Current_Scope)
7287       then
7288          CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
7289
7290          Set_Ekind            (CR_Disc, E_In_Parameter);
7291          Set_Mechanism        (CR_Disc, Default_Mechanism);
7292          Set_Etype            (CR_Disc, Etype (Discrim));
7293          Set_Discriminal_Link (CR_Disc, Discrim);
7294          Set_CR_Discriminant  (Discrim, CR_Disc);
7295       end if;
7296    end Build_Discriminal;
7297
7298    ------------------------------------
7299    -- Build_Discriminant_Constraints --
7300    ------------------------------------
7301
7302    function Build_Discriminant_Constraints
7303      (T           : Entity_Id;
7304       Def         : Node_Id;
7305       Derived_Def : Boolean := False) return Elist_Id
7306    is
7307       C        : constant Node_Id := Constraint (Def);
7308       Nb_Discr : constant Nat     := Number_Discriminants (T);
7309
7310       Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
7311       --  Saves the expression corresponding to a given discriminant in T
7312
7313       function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
7314       --  Return the Position number within array Discr_Expr of a discriminant
7315       --  D within the discriminant list of the discriminated type T.
7316
7317       ------------------
7318       -- Pos_Of_Discr --
7319       ------------------
7320
7321       function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
7322          Disc : Entity_Id;
7323
7324       begin
7325          Disc := First_Discriminant (T);
7326          for J in Discr_Expr'Range loop
7327             if Disc = D then
7328                return J;
7329             end if;
7330
7331             Next_Discriminant (Disc);
7332          end loop;
7333
7334          --  Note: Since this function is called on discriminants that are
7335          --  known to belong to the discriminated type, falling through the
7336          --  loop with no match signals an internal compiler error.
7337
7338          raise Program_Error;
7339       end Pos_Of_Discr;
7340
7341       --  Declarations local to Build_Discriminant_Constraints
7342
7343       Discr : Entity_Id;
7344       E     : Entity_Id;
7345       Elist : constant Elist_Id := New_Elmt_List;
7346
7347       Constr   : Node_Id;
7348       Expr     : Node_Id;
7349       Id       : Node_Id;
7350       Position : Nat;
7351       Found    : Boolean;
7352
7353       Discrim_Present : Boolean := False;
7354
7355    --  Start of processing for Build_Discriminant_Constraints
7356
7357    begin
7358       --  The following loop will process positional associations only.
7359       --  For a positional association, the (single) discriminant is
7360       --  implicitly specified by position, in textual order (RM 3.7.2).
7361
7362       Discr  := First_Discriminant (T);
7363       Constr := First (Constraints (C));
7364       for D in Discr_Expr'Range loop
7365          exit when Nkind (Constr) = N_Discriminant_Association;
7366
7367          if No (Constr) then
7368             Error_Msg_N ("too few discriminants given in constraint", C);
7369             return New_Elmt_List;
7370
7371          elsif Nkind (Constr) = N_Range
7372            or else (Nkind (Constr) = N_Attribute_Reference
7373                      and then
7374                     Attribute_Name (Constr) = Name_Range)
7375          then
7376             Error_Msg_N
7377               ("a range is not a valid discriminant constraint", Constr);
7378             Discr_Expr (D) := Error;
7379
7380          else
7381             Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
7382             Discr_Expr (D) := Constr;
7383          end if;
7384
7385          Next_Discriminant (Discr);
7386          Next (Constr);
7387       end loop;
7388
7389       if No (Discr) and then Present (Constr) then
7390          Error_Msg_N ("too many discriminants given in constraint", Constr);
7391          return New_Elmt_List;
7392       end if;
7393
7394       --  Named associations can be given in any order, but if both positional
7395       --  and named associations are used in the same discriminant constraint,
7396       --  then positional associations must occur first, at their normal
7397       --  position. Hence once a named association is used, the rest of the
7398       --  discriminant constraint must use only named associations.
7399
7400       while Present (Constr) loop
7401
7402          --  Positional association forbidden after a named association
7403
7404          if Nkind (Constr) /= N_Discriminant_Association then
7405             Error_Msg_N ("positional association follows named one", Constr);
7406             return New_Elmt_List;
7407
7408          --  Otherwise it is a named association
7409
7410          else
7411             --  E records the type of the discriminants in the named
7412             --  association. All the discriminants specified in the same name
7413             --  association must have the same type.
7414
7415             E := Empty;
7416
7417             --  Search the list of discriminants in T to see if the simple name
7418             --  given in the constraint matches any of them.
7419
7420             Id := First (Selector_Names (Constr));
7421             while Present (Id) loop
7422                Found := False;
7423
7424                --  If Original_Discriminant is present, we are processing a
7425                --  generic instantiation and this is an instance node. We need
7426                --  to find the name of the corresponding discriminant in the
7427                --  actual record type T and not the name of the discriminant in
7428                --  the generic formal. Example:
7429
7430                --    generic
7431                --       type G (D : int) is private;
7432                --    package P is
7433                --       subtype W is G (D => 1);
7434                --    end package;
7435                --    type Rec (X : int) is record ... end record;
7436                --    package Q is new P (G => Rec);
7437
7438                --  At the point of the instantiation, formal type G is Rec
7439                --  and therefore when reanalyzing "subtype W is G (D => 1);"
7440                --  which really looks like "subtype W is Rec (D => 1);" at
7441                --  the point of instantiation, we want to find the discriminant
7442                --  that corresponds to D in Rec, i.e. X.
7443
7444                if Present (Original_Discriminant (Id)) then
7445                   Discr := Find_Corresponding_Discriminant (Id, T);
7446                   Found := True;
7447
7448                else
7449                   Discr := First_Discriminant (T);
7450                   while Present (Discr) loop
7451                      if Chars (Discr) = Chars (Id) then
7452                         Found := True;
7453                         exit;
7454                      end if;
7455
7456                      Next_Discriminant (Discr);
7457                   end loop;
7458
7459                   if not Found then
7460                      Error_Msg_N ("& does not match any discriminant", Id);
7461                      return New_Elmt_List;
7462
7463                   --  The following is only useful for the benefit of generic
7464                   --  instances but it does not interfere with other
7465                   --  processing for the non-generic case so we do it in all
7466                   --  cases (for generics this statement is executed when
7467                   --  processing the generic definition, see comment at the
7468                   --  beginning of this if statement).
7469
7470                   else
7471                      Set_Original_Discriminant (Id, Discr);
7472                   end if;
7473                end if;
7474
7475                Position := Pos_Of_Discr (T, Discr);
7476
7477                if Present (Discr_Expr (Position)) then
7478                   Error_Msg_N ("duplicate constraint for discriminant&", Id);
7479
7480                else
7481                   --  Each discriminant specified in the same named association
7482                   --  must be associated with a separate copy of the
7483                   --  corresponding expression.
7484
7485                   if Present (Next (Id)) then
7486                      Expr := New_Copy_Tree (Expression (Constr));
7487                      Set_Parent (Expr, Parent (Expression (Constr)));
7488                   else
7489                      Expr := Expression (Constr);
7490                   end if;
7491
7492                   Discr_Expr (Position) := Expr;
7493                   Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
7494                end if;
7495
7496                --  A discriminant association with more than one discriminant
7497                --  name is only allowed if the named discriminants are all of
7498                --  the same type (RM 3.7.1(8)).
7499
7500                if E = Empty then
7501                   E := Base_Type (Etype (Discr));
7502
7503                elsif Base_Type (Etype (Discr)) /= E then
7504                   Error_Msg_N
7505                     ("all discriminants in an association " &
7506                      "must have the same type", Id);
7507                end if;
7508
7509                Next (Id);
7510             end loop;
7511          end if;
7512
7513          Next (Constr);
7514       end loop;
7515
7516       --  A discriminant constraint must provide exactly one value for each
7517       --  discriminant of the type (RM 3.7.1(8)).
7518
7519       for J in Discr_Expr'Range loop
7520          if No (Discr_Expr (J)) then
7521             Error_Msg_N ("too few discriminants given in constraint", C);
7522             return New_Elmt_List;
7523          end if;
7524       end loop;
7525
7526       --  Determine if there are discriminant expressions in the constraint
7527
7528       for J in Discr_Expr'Range loop
7529          if Denotes_Discriminant
7530               (Discr_Expr (J), Check_Concurrent => True)
7531          then
7532             Discrim_Present := True;
7533          end if;
7534       end loop;
7535
7536       --  Build an element list consisting of the expressions given in the
7537       --  discriminant constraint and apply the appropriate checks. The list
7538       --  is constructed after resolving any named discriminant associations
7539       --  and therefore the expressions appear in the textual order of the
7540       --  discriminants.
7541
7542       Discr := First_Discriminant (T);
7543       for J in Discr_Expr'Range loop
7544          if Discr_Expr (J) /= Error then
7545             Append_Elmt (Discr_Expr (J), Elist);
7546
7547             --  If any of the discriminant constraints is given by a
7548             --  discriminant and we are in a derived type declaration we
7549             --  have a discriminant renaming. Establish link between new
7550             --  and old discriminant.
7551
7552             if Denotes_Discriminant (Discr_Expr (J)) then
7553                if Derived_Def then
7554                   Set_Corresponding_Discriminant
7555                     (Entity (Discr_Expr (J)), Discr);
7556                end if;
7557
7558             --  Force the evaluation of non-discriminant expressions.
7559             --  If we have found a discriminant in the constraint 3.4(26)
7560             --  and 3.8(18) demand that no range checks are performed are
7561             --  after evaluation. If the constraint is for a component
7562             --  definition that has a per-object constraint, expressions are
7563             --  evaluated but not checked either. In all other cases perform
7564             --  a range check.
7565
7566             else
7567                if Discrim_Present then
7568                   null;
7569
7570                elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
7571                  and then
7572                    Has_Per_Object_Constraint
7573                      (Defining_Identifier (Parent (Parent (Def))))
7574                then
7575                   null;
7576
7577                elsif Is_Access_Type (Etype (Discr)) then
7578                   Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
7579
7580                else
7581                   Apply_Range_Check (Discr_Expr (J), Etype (Discr));
7582                end if;
7583
7584                Force_Evaluation (Discr_Expr (J));
7585             end if;
7586
7587             --  Check that the designated type of an access discriminant's
7588             --  expression is not a class-wide type unless the discriminant's
7589             --  designated type is also class-wide.
7590
7591             if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
7592               and then not Is_Class_Wide_Type
7593                          (Designated_Type (Etype (Discr)))
7594               and then Etype (Discr_Expr (J)) /= Any_Type
7595               and then Is_Class_Wide_Type
7596                          (Designated_Type (Etype (Discr_Expr (J))))
7597             then
7598                Wrong_Type (Discr_Expr (J), Etype (Discr));
7599             end if;
7600          end if;
7601
7602          Next_Discriminant (Discr);
7603       end loop;
7604
7605       return Elist;
7606    end Build_Discriminant_Constraints;
7607
7608    ---------------------------------
7609    -- Build_Discriminated_Subtype --
7610    ---------------------------------
7611
7612    procedure Build_Discriminated_Subtype
7613      (T           : Entity_Id;
7614       Def_Id      : Entity_Id;
7615       Elist       : Elist_Id;
7616       Related_Nod : Node_Id;
7617       For_Access  : Boolean := False)
7618    is
7619       Has_Discrs  : constant Boolean := Has_Discriminants (T);
7620       Constrained : constant Boolean :=
7621                       (Has_Discrs
7622                          and then not Is_Empty_Elmt_List (Elist)
7623                          and then not Is_Class_Wide_Type (T))
7624                         or else Is_Constrained (T);
7625
7626    begin
7627       if Ekind (T) = E_Record_Type then
7628          if For_Access then
7629             Set_Ekind (Def_Id, E_Private_Subtype);
7630             Set_Is_For_Access_Subtype (Def_Id, True);
7631          else
7632             Set_Ekind (Def_Id, E_Record_Subtype);
7633          end if;
7634
7635          --  Inherit preelaboration flag from base, for types for which it
7636          --  may have been set: records, private types, protected types.
7637
7638          Set_Known_To_Have_Preelab_Init
7639            (Def_Id, Known_To_Have_Preelab_Init (T));
7640
7641       elsif Ekind (T) = E_Task_Type then
7642          Set_Ekind (Def_Id, E_Task_Subtype);
7643
7644       elsif Ekind (T) = E_Protected_Type then
7645          Set_Ekind (Def_Id, E_Protected_Subtype);
7646          Set_Known_To_Have_Preelab_Init
7647            (Def_Id, Known_To_Have_Preelab_Init (T));
7648
7649       elsif Is_Private_Type (T) then
7650          Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
7651          Set_Known_To_Have_Preelab_Init
7652            (Def_Id, Known_To_Have_Preelab_Init (T));
7653
7654       elsif Is_Class_Wide_Type (T) then
7655          Set_Ekind (Def_Id, E_Class_Wide_Subtype);
7656
7657       else
7658          --  Incomplete type. Attach subtype to list of dependents, to be
7659          --  completed with full view of parent type,  unless is it the
7660          --  designated subtype of a record component within an init_proc.
7661          --  This last case arises for a component of an access type whose
7662          --  designated type is incomplete (e.g. a Taft Amendment type).
7663          --  The designated subtype is within an inner scope, and needs no
7664          --  elaboration, because only the access type is needed in the
7665          --  initialization procedure.
7666
7667          Set_Ekind (Def_Id, Ekind (T));
7668
7669          if For_Access and then Within_Init_Proc then
7670             null;
7671          else
7672             Append_Elmt (Def_Id, Private_Dependents (T));
7673          end if;
7674       end if;
7675
7676       Set_Etype             (Def_Id, T);
7677       Init_Size_Align       (Def_Id);
7678       Set_Has_Discriminants (Def_Id, Has_Discrs);
7679       Set_Is_Constrained    (Def_Id, Constrained);
7680
7681       Set_First_Entity      (Def_Id, First_Entity   (T));
7682       Set_Last_Entity       (Def_Id, Last_Entity    (T));
7683
7684       --  If the subtype is the completion of a private declaration, there may
7685       --  have been representation clauses for the partial view, and they must
7686       --  be preserved. Build_Derived_Type chains the inherited clauses with
7687       --  the ones appearing on the extension. If this comes from a subtype
7688       --  declaration, all clauses are inherited.
7689
7690       if No (First_Rep_Item (Def_Id)) then
7691          Set_First_Rep_Item    (Def_Id, First_Rep_Item (T));
7692       end if;
7693
7694       if Is_Tagged_Type (T) then
7695          Set_Is_Tagged_Type  (Def_Id);
7696          Make_Class_Wide_Type (Def_Id);
7697       end if;
7698
7699       Set_Stored_Constraint (Def_Id, No_Elist);
7700
7701       if Has_Discrs then
7702          Set_Discriminant_Constraint (Def_Id, Elist);
7703          Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
7704       end if;
7705
7706       if Is_Tagged_Type (T) then
7707
7708          --  Ada 2005 (AI-251): In case of concurrent types we inherit the
7709          --  concurrent record type (which has the list of primitive
7710          --  operations).
7711
7712          if Ada_Version >= Ada_05
7713            and then Is_Concurrent_Type (T)
7714          then
7715             Set_Corresponding_Record_Type (Def_Id,
7716                Corresponding_Record_Type (T));
7717          else
7718             Set_Primitive_Operations (Def_Id, Primitive_Operations (T));
7719          end if;
7720
7721          Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
7722       end if;
7723
7724       --  Subtypes introduced by component declarations do not need to be
7725       --  marked as delayed, and do not get freeze nodes, because the semantics
7726       --  verifies that the parents of the subtypes are frozen before the
7727       --  enclosing record is frozen.
7728
7729       if not Is_Type (Scope (Def_Id)) then
7730          Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
7731
7732          if Is_Private_Type (T)
7733            and then Present (Full_View (T))
7734          then
7735             Conditional_Delay (Def_Id, Full_View (T));
7736          else
7737             Conditional_Delay (Def_Id, T);
7738          end if;
7739       end if;
7740
7741       if Is_Record_Type (T) then
7742          Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
7743
7744          if Has_Discrs
7745             and then not Is_Empty_Elmt_List (Elist)
7746             and then not For_Access
7747          then
7748             Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
7749          elsif not For_Access then
7750             Set_Cloned_Subtype (Def_Id, T);
7751          end if;
7752       end if;
7753    end Build_Discriminated_Subtype;
7754
7755    ---------------------------
7756    -- Build_Itype_Reference --
7757    ---------------------------
7758
7759    procedure Build_Itype_Reference
7760      (Ityp : Entity_Id;
7761       Nod  : Node_Id)
7762    is
7763       IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
7764    begin
7765       Set_Itype (IR, Ityp);
7766       Insert_After (Nod, IR);
7767    end Build_Itype_Reference;
7768
7769    ------------------------
7770    -- Build_Scalar_Bound --
7771    ------------------------
7772
7773    function Build_Scalar_Bound
7774      (Bound : Node_Id;
7775       Par_T : Entity_Id;
7776       Der_T : Entity_Id) return Node_Id
7777    is
7778       New_Bound : Entity_Id;
7779
7780    begin
7781       --  Note: not clear why this is needed, how can the original bound
7782       --  be unanalyzed at this point? and if it is, what business do we
7783       --  have messing around with it? and why is the base type of the
7784       --  parent type the right type for the resolution. It probably is
7785       --  not! It is OK for the new bound we are creating, but not for
7786       --  the old one??? Still if it never happens, no problem!
7787
7788       Analyze_And_Resolve (Bound, Base_Type (Par_T));
7789
7790       if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
7791          New_Bound := New_Copy (Bound);
7792          Set_Etype (New_Bound, Der_T);
7793          Set_Analyzed (New_Bound);
7794
7795       elsif Is_Entity_Name (Bound) then
7796          New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
7797
7798       --  The following is almost certainly wrong. What business do we have
7799       --  relocating a node (Bound) that is presumably still attached to
7800       --  the tree elsewhere???
7801
7802       else
7803          New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
7804       end if;
7805
7806       Set_Etype (New_Bound, Der_T);
7807       return New_Bound;
7808    end Build_Scalar_Bound;
7809
7810    --------------------------------
7811    -- Build_Underlying_Full_View --
7812    --------------------------------
7813
7814    procedure Build_Underlying_Full_View
7815      (N   : Node_Id;
7816       Typ : Entity_Id;
7817       Par : Entity_Id)
7818    is
7819       Loc  : constant Source_Ptr := Sloc (N);
7820       Subt : constant Entity_Id :=
7821                Make_Defining_Identifier
7822                  (Loc, New_External_Name (Chars (Typ), 'S'));
7823
7824       Constr : Node_Id;
7825       Indic  : Node_Id;
7826       C      : Node_Id;
7827       Id     : Node_Id;
7828
7829       procedure Set_Discriminant_Name (Id : Node_Id);
7830       --  If the derived type has discriminants, they may rename discriminants
7831       --  of the parent. When building the full view of the parent, we need to
7832       --  recover the names of the original discriminants if the constraint is
7833       --  given by named associations.
7834
7835       ---------------------------
7836       -- Set_Discriminant_Name --
7837       ---------------------------
7838
7839       procedure Set_Discriminant_Name (Id : Node_Id) is
7840          Disc : Entity_Id;
7841
7842       begin
7843          Set_Original_Discriminant (Id, Empty);
7844
7845          if Has_Discriminants (Typ) then
7846             Disc := First_Discriminant (Typ);
7847             while Present (Disc) loop
7848                if Chars (Disc) = Chars (Id)
7849                  and then Present (Corresponding_Discriminant (Disc))
7850                then
7851                   Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
7852                end if;
7853                Next_Discriminant (Disc);
7854             end loop;
7855          end if;
7856       end Set_Discriminant_Name;
7857
7858    --  Start of processing for Build_Underlying_Full_View
7859
7860    begin
7861       if Nkind (N) = N_Full_Type_Declaration then
7862          Constr := Constraint (Subtype_Indication (Type_Definition (N)));
7863
7864       elsif Nkind (N) = N_Subtype_Declaration then
7865          Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
7866
7867       elsif Nkind (N) = N_Component_Declaration then
7868          Constr :=
7869            New_Copy_Tree
7870              (Constraint (Subtype_Indication (Component_Definition (N))));
7871
7872       else
7873          raise Program_Error;
7874       end if;
7875
7876       C := First (Constraints (Constr));
7877       while Present (C) loop
7878          if Nkind (C) = N_Discriminant_Association then
7879             Id := First (Selector_Names (C));
7880             while Present (Id) loop
7881                Set_Discriminant_Name (Id);
7882                Next (Id);
7883             end loop;
7884          end if;
7885
7886          Next (C);
7887       end loop;
7888
7889       Indic :=
7890         Make_Subtype_Declaration (Loc,
7891           Defining_Identifier => Subt,
7892           Subtype_Indication  =>
7893             Make_Subtype_Indication (Loc,
7894               Subtype_Mark => New_Reference_To (Par, Loc),
7895               Constraint   => New_Copy_Tree (Constr)));
7896
7897       --  If this is a component subtype for an outer itype, it is not
7898       --  a list member, so simply set the parent link for analysis: if
7899       --  the enclosing type does not need to be in a declarative list,
7900       --  neither do the components.
7901
7902       if Is_List_Member (N)
7903         and then Nkind (N) /= N_Component_Declaration
7904       then
7905          Insert_Before (N, Indic);
7906       else
7907          Set_Parent (Indic, Parent (N));
7908       end if;
7909
7910       Analyze (Indic);
7911       Set_Underlying_Full_View (Typ, Full_View (Subt));
7912    end Build_Underlying_Full_View;
7913
7914    -------------------------------
7915    -- Check_Abstract_Overriding --
7916    -------------------------------
7917
7918    procedure Check_Abstract_Overriding (T : Entity_Id) is
7919       Alias_Subp : Entity_Id;
7920       Elmt       : Elmt_Id;
7921       Op_List    : Elist_Id;
7922       Subp       : Entity_Id;
7923       Type_Def   : Node_Id;
7924
7925    begin
7926       Op_List := Primitive_Operations (T);
7927
7928       --  Loop to check primitive operations
7929
7930       Elmt := First_Elmt (Op_List);
7931       while Present (Elmt) loop
7932          Subp := Node (Elmt);
7933          Alias_Subp := Alias (Subp);
7934
7935          --  Inherited subprograms are identified by the fact that they do not
7936          --  come from source, and the associated source location is the
7937          --  location of the first subtype of the derived type.
7938
7939          --  Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
7940          --  subprograms that "require overriding".
7941
7942          --  Special exception, do not complain about failure to override the
7943          --  stream routines _Input and _Output, as well as the primitive
7944          --  operations used in dispatching selects since we always provide
7945          --  automatic overridings for these subprograms.
7946
7947          --  Also ignore this rule for convention CIL since .NET libraries
7948          --  do bizarre things with interfaces???
7949
7950          --  The partial view of T may have been a private extension, for
7951          --  which inherited functions dispatching on result are abstract.
7952          --  If the full view is a null extension, there is no need for
7953          --  overriding in Ada2005, but wrappers need to be built for them
7954          --  (see exp_ch3, Build_Controlling_Function_Wrappers).
7955
7956          if Is_Null_Extension (T)
7957            and then Has_Controlling_Result (Subp)
7958            and then Ada_Version >= Ada_05
7959            and then Present (Alias_Subp)
7960            and then not Comes_From_Source (Subp)
7961            and then not Is_Abstract_Subprogram (Alias_Subp)
7962            and then not Is_Access_Type (Etype (Subp))
7963          then
7964             null;
7965
7966          --  Ada 2005 (AI-251): Internal entities of interfaces need no
7967          --  processing because this check is done with the aliased
7968          --  entity
7969
7970          elsif Present (Interface_Alias (Subp)) then
7971             null;
7972
7973          elsif (Is_Abstract_Subprogram (Subp)
7974                  or else Requires_Overriding (Subp)
7975                  or else
7976                    (Has_Controlling_Result (Subp)
7977                      and then Present (Alias_Subp)
7978                      and then not Comes_From_Source (Subp)
7979                      and then Sloc (Subp) = Sloc (First_Subtype (T))))
7980            and then not Is_TSS (Subp, TSS_Stream_Input)
7981            and then not Is_TSS (Subp, TSS_Stream_Output)
7982            and then not Is_Abstract_Type (T)
7983            and then Convention (T) /= Convention_CIL
7984            and then not Is_Predefined_Interface_Primitive (Subp)
7985
7986             --  Ada 2005 (AI-251): Do not consider hidden entities associated
7987             --  with abstract interface types because the check will be done
7988             --  with the aliased entity (otherwise we generate a duplicated
7989             --  error message).
7990
7991            and then not Present (Interface_Alias (Subp))
7992          then
7993             if Present (Alias_Subp) then
7994
7995                --  Only perform the check for a derived subprogram when the
7996                --  type has an explicit record extension. This avoids incorrect
7997                --  flagging of abstract subprograms for the case of a type
7998                --  without an extension that is derived from a formal type
7999                --  with a tagged actual (can occur within a private part).
8000
8001                --  Ada 2005 (AI-391): In the case of an inherited function with
8002                --  a controlling result of the type, the rule does not apply if
8003                --  the type is a null extension (unless the parent function
8004                --  itself is abstract, in which case the function must still be
8005                --  be overridden). The expander will generate an overriding
8006                --  wrapper function calling the parent subprogram (see
8007                --  Exp_Ch3.Make_Controlling_Wrapper_Functions).
8008
8009                Type_Def := Type_Definition (Parent (T));
8010
8011                if Nkind (Type_Def) = N_Derived_Type_Definition
8012                  and then Present (Record_Extension_Part (Type_Def))
8013                  and then
8014                    (Ada_Version < Ada_05
8015                       or else not Is_Null_Extension (T)
8016                       or else Ekind (Subp) = E_Procedure
8017                       or else not Has_Controlling_Result (Subp)
8018                       or else Is_Abstract_Subprogram (Alias_Subp)
8019                       or else Requires_Overriding (Subp)
8020                       or else Is_Access_Type (Etype (Subp)))
8021                then
8022                   --  Avoid reporting error in case of abstract predefined
8023                   --  primitive inherited from interface type because the
8024                   --  body of internally generated predefined primitives
8025                   --  of tagged types are generated later by Freeze_Type
8026
8027                   if Is_Interface (Root_Type (T))
8028                     and then Is_Abstract_Subprogram (Subp)
8029                     and then Is_Predefined_Dispatching_Operation (Subp)
8030                     and then not Comes_From_Source (Ultimate_Alias (Subp))
8031                   then
8032                      null;
8033
8034                   else
8035                      Error_Msg_NE
8036                        ("type must be declared abstract or & overridden",
8037                         T, Subp);
8038
8039                      --  Traverse the whole chain of aliased subprograms to
8040                      --  complete the error notification. This is especially
8041                      --  useful for traceability of the chain of entities when
8042                      --  the subprogram corresponds with an interface
8043                      --  subprogram (which may be defined in another package).
8044
8045                      if Present (Alias_Subp) then
8046                         declare
8047                            E : Entity_Id;
8048
8049                         begin
8050                            E := Subp;
8051                            while Present (Alias (E)) loop
8052                               Error_Msg_Sloc := Sloc (E);
8053                               Error_Msg_NE
8054                                 ("\& has been inherited #", T, Subp);
8055                               E := Alias (E);
8056                            end loop;
8057
8058                            Error_Msg_Sloc := Sloc (E);
8059                            Error_Msg_NE
8060                              ("\& has been inherited from subprogram #",
8061                               T, Subp);
8062                         end;
8063                      end if;
8064                   end if;
8065
8066                --  Ada 2005 (AI-345): Protected or task type implementing
8067                --  abstract interfaces.
8068
8069                elsif Is_Concurrent_Record_Type (T)
8070                  and then Present (Interfaces (T))
8071                then
8072                   --  The controlling formal of Subp must be of mode "out",
8073                   --  "in out" or an access-to-variable to be overridden.
8074
8075                   --  Error message below needs rewording (remember comma
8076                   --  in -gnatj mode) ???
8077
8078                   if Ekind (First_Formal (Subp)) = E_In_Parameter then
8079                      if not Is_Predefined_Dispatching_Operation (Subp) then
8080                         Error_Msg_NE
8081                           ("first formal of & must be of mode `OUT`, " &
8082                            "`IN OUT` or access-to-variable", T, Subp);
8083                         Error_Msg_N
8084                           ("\to be overridden by protected procedure or " &
8085                            "entry (RM 9.4(11.9/2))", T);
8086                      end if;
8087
8088                   --  Some other kind of overriding failure
8089
8090                   else
8091                      Error_Msg_NE
8092                        ("interface subprogram & must be overridden",
8093                         T, Subp);
8094                   end if;
8095                end if;
8096
8097             else
8098                Error_Msg_Node_2 := T;
8099                Error_Msg_N
8100                  ("abstract subprogram& not allowed for type&", Subp);
8101
8102                --  Also post unconditional warning on the type (unconditional
8103                --  so that if there are more than one of these cases, we get
8104                --  them all, and not just the first one).
8105
8106                Error_Msg_Node_2 := Subp;
8107                Error_Msg_N
8108                  ("nonabstract type& has abstract subprogram&!", T);
8109             end if;
8110          end if;
8111
8112          --  Ada 2005 (AI05-0030): Inspect hidden subprograms which provide
8113          --  the mapping between interface and implementing type primitives.
8114          --  If the interface alias is marked as Implemented_By_Entry, the
8115          --  alias must be an entry wrapper.
8116
8117          if Ada_Version >= Ada_05
8118            and then Is_Hidden (Subp)
8119            and then Present (Interface_Alias (Subp))
8120            and then Implemented_By_Entry (Interface_Alias (Subp))
8121            and then Present (Alias_Subp)
8122            and then
8123              (not Is_Primitive_Wrapper (Alias_Subp)
8124                 or else Ekind (Wrapped_Entity (Alias_Subp)) /= E_Entry)
8125          then
8126             declare
8127                Error_Ent : Entity_Id := T;
8128
8129             begin
8130                if Is_Concurrent_Record_Type (Error_Ent) then
8131                   Error_Ent := Corresponding_Concurrent_Type (Error_Ent);
8132                end if;
8133
8134                Error_Msg_Node_2 := Interface_Alias (Subp);
8135                Error_Msg_NE
8136                  ("type & must implement abstract subprogram & with an entry",
8137                   Error_Ent, Error_Ent);
8138             end;
8139          end if;
8140
8141          Next_Elmt (Elmt);
8142       end loop;
8143    end Check_Abstract_Overriding;
8144
8145    ------------------------------------------------
8146    -- Check_Access_Discriminant_Requires_Limited --
8147    ------------------------------------------------
8148
8149    procedure Check_Access_Discriminant_Requires_Limited
8150      (D   : Node_Id;
8151       Loc : Node_Id)
8152    is
8153    begin
8154       --  A discriminant_specification for an access discriminant shall appear
8155       --  only in the declaration for a task or protected type, or for a type
8156       --  with the reserved word 'limited' in its definition or in one of its
8157       --  ancestors. (RM 3.7(10))
8158
8159       if Nkind (Discriminant_Type (D)) = N_Access_Definition
8160         and then not Is_Concurrent_Type (Current_Scope)
8161         and then not Is_Concurrent_Record_Type (Current_Scope)
8162         and then not Is_Limited_Record (Current_Scope)
8163         and then Ekind (Current_Scope) /= E_Limited_Private_Type
8164       then
8165          Error_Msg_N
8166            ("access discriminants allowed only for limited types", Loc);
8167       end if;
8168    end Check_Access_Discriminant_Requires_Limited;
8169
8170    -----------------------------------
8171    -- Check_Aliased_Component_Types --
8172    -----------------------------------
8173
8174    procedure Check_Aliased_Component_Types (T : Entity_Id) is
8175       C : Entity_Id;
8176
8177    begin
8178       --  ??? Also need to check components of record extensions, but not
8179       --  components of protected types (which are always limited).
8180
8181       --  Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
8182       --  types to be unconstrained. This is safe because it is illegal to
8183       --  create access subtypes to such types with explicit discriminant
8184       --  constraints.
8185
8186       if not Is_Limited_Type (T) then
8187          if Ekind (T) = E_Record_Type then
8188             C := First_Component (T);
8189             while Present (C) loop
8190                if Is_Aliased (C)
8191                  and then Has_Discriminants (Etype (C))
8192                  and then not Is_Constrained (Etype (C))
8193                  and then not In_Instance_Body
8194                  and then Ada_Version < Ada_05
8195                then
8196                   Error_Msg_N
8197                     ("aliased component must be constrained (RM 3.6(11))",
8198                       C);
8199                end if;
8200
8201                Next_Component (C);
8202             end loop;
8203
8204          elsif Ekind (T) = E_Array_Type then
8205             if Has_Aliased_Components (T)
8206               and then Has_Discriminants (Component_Type (T))
8207               and then not Is_Constrained (Component_Type (T))
8208               and then not In_Instance_Body
8209               and then Ada_Version < Ada_05
8210             then
8211                Error_Msg_N
8212                  ("aliased component type must be constrained (RM 3.6(11))",
8213                     T);
8214             end if;
8215          end if;
8216       end if;
8217    end Check_Aliased_Component_Types;
8218
8219    ----------------------
8220    -- Check_Completion --
8221    ----------------------
8222
8223    procedure Check_Completion (Body_Id : Node_Id := Empty) is
8224       E : Entity_Id;
8225
8226       procedure Post_Error;
8227       --  Post error message for lack of completion for entity E
8228
8229       ----------------
8230       -- Post_Error --
8231       ----------------
8232
8233       procedure Post_Error is
8234       begin
8235          if not Comes_From_Source (E) then
8236
8237             if Ekind (E) = E_Task_Type
8238               or else Ekind (E) = E_Protected_Type
8239             then
8240                --  It may be an anonymous protected type created for a
8241                --  single variable. Post error on variable, if present.
8242
8243                declare
8244                   Var : Entity_Id;
8245
8246                begin
8247                   Var := First_Entity (Current_Scope);
8248                   while Present (Var) loop
8249                      exit when Etype (Var) = E
8250                        and then Comes_From_Source (Var);
8251
8252                      Next_Entity (Var);
8253                   end loop;
8254
8255                   if Present (Var) then
8256                      E := Var;
8257                   end if;
8258                end;
8259             end if;
8260          end if;
8261
8262          --  If a generated entity has no completion, then either previous
8263          --  semantic errors have disabled the expansion phase, or else we had
8264          --  missing subunits, or else we are compiling without expansion,
8265          --  or else something is very wrong.
8266
8267          if not Comes_From_Source (E) then
8268             pragma Assert
8269               (Serious_Errors_Detected > 0
8270                 or else Configurable_Run_Time_Violations > 0
8271                 or else Subunits_Missing
8272                 or else not Expander_Active);
8273             return;
8274
8275          --  Here for source entity
8276
8277          else
8278             --  Here if no body to post the error message, so we post the error
8279             --  on the declaration that has no completion. This is not really
8280             --  the right place to post it, think about this later ???
8281
8282             if No (Body_Id) then
8283                if Is_Type (E) then
8284                   Error_Msg_NE
8285                     ("missing full declaration for }", Parent (E), E);
8286                else
8287                   Error_Msg_NE
8288                     ("missing body for &", Parent (E), E);
8289                end if;
8290
8291             --  Package body has no completion for a declaration that appears
8292             --  in the corresponding spec. Post error on the body, with a
8293             --  reference to the non-completed declaration.
8294
8295             else
8296                Error_Msg_Sloc := Sloc (E);
8297
8298                if Is_Type (E) then
8299                   Error_Msg_NE
8300                     ("missing full declaration for }!", Body_Id, E);
8301
8302                elsif Is_Overloadable (E)
8303                  and then Current_Entity_In_Scope (E) /= E
8304                then
8305                   --  It may be that the completion is mistyped and appears as
8306                   --  a distinct overloading of the entity.
8307
8308                   declare
8309                      Candidate : constant Entity_Id :=
8310                                    Current_Entity_In_Scope (E);
8311                      Decl      : constant Node_Id :=
8312                                    Unit_Declaration_Node (Candidate);
8313
8314                   begin
8315                      if Is_Overloadable (Candidate)
8316                        and then Ekind (Candidate) = Ekind (E)
8317                        and then Nkind (Decl) = N_Subprogram_Body
8318                        and then Acts_As_Spec (Decl)
8319                      then
8320                         Check_Type_Conformant (Candidate, E);
8321
8322                      else
8323                         Error_Msg_NE ("missing body for & declared#!",
8324                            Body_Id, E);
8325                      end if;
8326                   end;
8327                else
8328                   Error_Msg_NE ("missing body for & declared#!",
8329                      Body_Id, E);
8330                end if;
8331             end if;
8332          end if;
8333       end Post_Error;
8334
8335    --  Start processing for Check_Completion
8336
8337    begin
8338       E := First_Entity (Current_Scope);
8339       while Present (E) loop
8340          if Is_Intrinsic_Subprogram (E) then
8341             null;
8342
8343          --  The following situation requires special handling: a child unit
8344          --  that appears in the context clause of the body of its parent:
8345
8346          --    procedure Parent.Child (...);
8347
8348          --    with Parent.Child;
8349          --    package body Parent is
8350
8351          --  Here Parent.Child appears as a local entity, but should not be
8352          --  flagged as requiring completion, because it is a compilation
8353          --  unit.
8354
8355          --  Ignore missing completion for a subprogram that does not come from
8356          --  source (including the _Call primitive operation of RAS types,
8357          --  which has to have the flag Comes_From_Source for other purposes):
8358          --  we assume that the expander will provide the missing completion.
8359
8360          elsif     Ekind (E) = E_Function
8361            or else Ekind (E) = E_Procedure
8362            or else Ekind (E) = E_Generic_Function
8363            or else Ekind (E) = E_Generic_Procedure
8364          then
8365             if not Has_Completion (E)
8366               and then not (Is_Subprogram (E)
8367                             and then Is_Abstract_Subprogram (E))
8368               and then not (Is_Subprogram (E)
8369                               and then
8370                             (not Comes_From_Source (E)
8371                               or else Chars (E) = Name_uCall))
8372               and then Nkind (Parent (Unit_Declaration_Node (E))) /=
8373                                                        N_Compilation_Unit
8374               and then Chars (E) /= Name_uSize
8375             then
8376                Post_Error;
8377             end if;
8378
8379          elsif Is_Entry (E) then
8380             if not Has_Completion (E) and then
8381               (Ekind (Scope (E)) = E_Protected_Object
8382                 or else Ekind (Scope (E)) = E_Protected_Type)
8383             then
8384                Post_Error;
8385             end if;
8386
8387          elsif Is_Package_Or_Generic_Package (E) then
8388             if Unit_Requires_Body (E) then
8389                if not Has_Completion (E)
8390                  and then Nkind (Parent (Unit_Declaration_Node (E))) /=
8391                                                        N_Compilation_Unit
8392                then
8393                   Post_Error;
8394                end if;
8395
8396             elsif not Is_Child_Unit (E) then
8397                May_Need_Implicit_Body (E);
8398             end if;
8399
8400          elsif Ekind (E) = E_Incomplete_Type
8401            and then No (Underlying_Type (E))
8402          then
8403             Post_Error;
8404
8405          elsif (Ekind (E) = E_Task_Type or else
8406                 Ekind (E) = E_Protected_Type)
8407            and then not Has_Completion (E)
8408          then
8409             Post_Error;
8410
8411          --  A single task declared in the current scope is a constant, verify
8412          --  that the body of its anonymous type is in the same scope. If the
8413          --  task is defined elsewhere, this may be a renaming declaration for
8414          --  which no completion is needed.
8415
8416          elsif Ekind (E) = E_Constant
8417            and then Ekind (Etype (E)) = E_Task_Type
8418            and then not Has_Completion (Etype (E))
8419            and then Scope (Etype (E)) = Current_Scope
8420          then
8421             Post_Error;
8422
8423          elsif Ekind (E) = E_Protected_Object
8424            and then not Has_Completion (Etype (E))
8425          then
8426             Post_Error;
8427
8428          elsif Ekind (E) = E_Record_Type then
8429             if Is_Tagged_Type (E) then
8430                Check_Abstract_Overriding (E);
8431                Check_Conventions (E);
8432             end if;
8433
8434             Check_Aliased_Component_Types (E);
8435
8436          elsif Ekind (E) = E_Array_Type then
8437             Check_Aliased_Component_Types (E);
8438
8439          end if;
8440
8441          Next_Entity (E);
8442       end loop;
8443    end Check_Completion;
8444
8445    ----------------------------
8446    -- Check_Delta_Expression --
8447    ----------------------------
8448
8449    procedure Check_Delta_Expression (E : Node_Id) is
8450    begin
8451       if not (Is_Real_Type (Etype (E))) then
8452          Wrong_Type (E, Any_Real);
8453
8454       elsif not Is_OK_Static_Expression (E) then
8455          Flag_Non_Static_Expr
8456            ("non-static expression used for delta value!", E);
8457
8458       elsif not UR_Is_Positive (Expr_Value_R (E)) then
8459          Error_Msg_N ("delta expression must be positive", E);
8460
8461       else
8462          return;
8463       end if;
8464
8465       --  If any of above errors occurred, then replace the incorrect
8466       --  expression by the real 0.1, which should prevent further errors.
8467
8468       Rewrite (E,
8469         Make_Real_Literal (Sloc (E), Ureal_Tenth));
8470       Analyze_And_Resolve (E, Standard_Float);
8471    end Check_Delta_Expression;
8472
8473    -----------------------------
8474    -- Check_Digits_Expression --
8475    -----------------------------
8476
8477    procedure Check_Digits_Expression (E : Node_Id) is
8478    begin
8479       if not (Is_Integer_Type (Etype (E))) then
8480          Wrong_Type (E, Any_Integer);
8481
8482       elsif not Is_OK_Static_Expression (E) then
8483          Flag_Non_Static_Expr
8484            ("non-static expression used for digits value!", E);
8485
8486       elsif Expr_Value (E) <= 0 then
8487          Error_Msg_N ("digits value must be greater than zero", E);
8488
8489       else
8490          return;
8491       end if;
8492
8493       --  If any of above errors occurred, then replace the incorrect
8494       --  expression by the integer 1, which should prevent further errors.
8495
8496       Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
8497       Analyze_And_Resolve (E, Standard_Integer);
8498
8499    end Check_Digits_Expression;
8500
8501    --------------------------
8502    -- Check_Initialization --
8503    --------------------------
8504
8505    procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
8506    begin
8507       if Is_Limited_Type (T)
8508         and then not In_Instance
8509         and then not In_Inlined_Body
8510       then
8511          if not OK_For_Limited_Init (Exp) then
8512
8513             --  In GNAT mode, this is just a warning, to allow it to be evilly
8514             --  turned off. Otherwise it is a real error.
8515
8516             if GNAT_Mode then
8517                Error_Msg_N
8518                  ("?cannot initialize entities of limited type!", Exp);
8519
8520             elsif Ada_Version < Ada_05 then
8521                Error_Msg_N
8522                  ("cannot initialize entities of limited type", Exp);
8523                Explain_Limited_Type (T, Exp);
8524
8525             else
8526                --  Specialize error message according to kind of illegal
8527                --  initial expression.
8528
8529                if Nkind (Exp) = N_Type_Conversion
8530                  and then Nkind (Expression (Exp)) = N_Function_Call
8531                then
8532                   Error_Msg_N
8533                     ("illegal context for call"
8534                       & " to function with limited result", Exp);
8535
8536                else
8537                   Error_Msg_N
8538                     ("initialization of limited object requires aggregate "
8539                       & "or function call",  Exp);
8540                end if;
8541             end if;
8542          end if;
8543       end if;
8544    end Check_Initialization;
8545
8546    ----------------------
8547    -- Check_Interfaces --
8548    ----------------------
8549
8550    procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
8551       Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
8552
8553       Iface       : Node_Id;
8554       Iface_Def   : Node_Id;
8555       Iface_Typ   : Entity_Id;
8556       Parent_Node : Node_Id;
8557
8558       Is_Task : Boolean := False;
8559       --  Set True if parent type or any progenitor is a task interface
8560
8561       Is_Protected : Boolean := False;
8562       --  Set True if parent type or any progenitor is a protected interface
8563
8564       procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
8565       --  Check that a progenitor is compatible with declaration.
8566       --  Error is posted on Error_Node.
8567
8568       ------------------
8569       -- Check_Ifaces --
8570       ------------------
8571
8572       procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
8573          Iface_Id : constant Entity_Id :=
8574                       Defining_Identifier (Parent (Iface_Def));
8575          Type_Def : Node_Id;
8576
8577       begin
8578          if Nkind (N) = N_Private_Extension_Declaration then
8579             Type_Def := N;
8580          else
8581             Type_Def := Type_Definition (N);
8582          end if;
8583
8584          if Is_Task_Interface (Iface_Id) then
8585             Is_Task := True;
8586
8587          elsif Is_Protected_Interface (Iface_Id) then
8588             Is_Protected := True;
8589          end if;
8590
8591          --  Check that the characteristics of the progenitor are compatible
8592          --  with the explicit qualifier in the declaration.
8593          --  The check only applies to qualifiers that come from source.
8594          --  Limited_Present also appears in the declaration of corresponding
8595          --  records, and the check does not apply to them.
8596
8597          if Limited_Present (Type_Def)
8598            and then not
8599              Is_Concurrent_Record_Type (Defining_Identifier (N))
8600          then
8601             if Is_Limited_Interface (Parent_Type)
8602               and then not Is_Limited_Interface (Iface_Id)
8603             then
8604                Error_Msg_NE
8605                  ("progenitor& must be limited interface",
8606                    Error_Node, Iface_Id);
8607
8608             elsif
8609               (Task_Present (Iface_Def)
8610                 or else Protected_Present (Iface_Def)
8611                 or else Synchronized_Present (Iface_Def))
8612               and then Nkind (N) /= N_Private_Extension_Declaration
8613             then
8614                Error_Msg_NE
8615                  ("progenitor& must be limited interface",
8616                    Error_Node, Iface_Id);
8617             end if;
8618
8619          --  Protected interfaces can only inherit from limited, synchronized
8620          --  or protected interfaces.
8621
8622          elsif Nkind (N) = N_Full_Type_Declaration
8623            and then  Protected_Present (Type_Def)
8624          then
8625             if Limited_Present (Iface_Def)
8626               or else Synchronized_Present (Iface_Def)
8627               or else Protected_Present (Iface_Def)
8628             then
8629                null;
8630
8631             elsif Task_Present (Iface_Def) then
8632                Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
8633                             & " from task interface", Error_Node);
8634
8635             else
8636                Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
8637                             & " from non-limited interface", Error_Node);
8638             end if;
8639
8640          --  Ada 2005 (AI-345): Synchronized interfaces can only inherit from
8641          --  limited and synchronized.
8642
8643          elsif Synchronized_Present (Type_Def) then
8644             if Limited_Present (Iface_Def)
8645               or else Synchronized_Present (Iface_Def)
8646             then
8647                null;
8648
8649             elsif Protected_Present (Iface_Def)
8650               and then Nkind (N) /= N_Private_Extension_Declaration
8651             then
8652                Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8653                             & " from protected interface", Error_Node);
8654
8655             elsif Task_Present (Iface_Def)
8656               and then Nkind (N) /= N_Private_Extension_Declaration
8657             then
8658                Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8659                             & " from task interface", Error_Node);
8660
8661             elsif not Is_Limited_Interface (Iface_Id) then
8662                Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8663                             & " from non-limited interface", Error_Node);
8664             end if;
8665
8666          --  Ada 2005 (AI-345): Task interfaces can only inherit from limited,
8667          --  synchronized or task interfaces.
8668
8669          elsif Nkind (N) = N_Full_Type_Declaration
8670            and then Task_Present (Type_Def)
8671          then
8672             if Limited_Present (Iface_Def)
8673               or else Synchronized_Present (Iface_Def)
8674               or else Task_Present (Iface_Def)
8675             then
8676                null;
8677
8678             elsif Protected_Present (Iface_Def) then
8679                Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
8680                             & " protected interface", Error_Node);
8681
8682             else
8683                Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
8684                             & " non-limited interface", Error_Node);
8685             end if;
8686          end if;
8687       end Check_Ifaces;
8688
8689    --  Start of processing for Check_Interfaces
8690
8691    begin
8692       if Is_Interface (Parent_Type) then
8693          if Is_Task_Interface (Parent_Type) then
8694             Is_Task := True;
8695
8696          elsif Is_Protected_Interface (Parent_Type) then
8697             Is_Protected := True;
8698          end if;
8699       end if;
8700
8701       if Nkind (N) = N_Private_Extension_Declaration then
8702
8703          --  Check that progenitors are compatible with declaration
8704
8705          Iface := First (Interface_List (Def));
8706          while Present (Iface) loop
8707             Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
8708
8709             Parent_Node := Parent (Base_Type (Iface_Typ));
8710             Iface_Def   := Type_Definition (Parent_Node);
8711
8712             if not Is_Interface (Iface_Typ) then
8713                Diagnose_Interface (Iface, Iface_Typ);
8714
8715             else
8716                Check_Ifaces (Iface_Def, Iface);
8717             end if;
8718
8719             Next (Iface);
8720          end loop;
8721
8722          if Is_Task and Is_Protected then
8723             Error_Msg_N
8724               ("type cannot derive from task and protected interface", N);
8725          end if;
8726
8727          return;
8728       end if;
8729
8730       --  Full type declaration of derived type.
8731       --  Check compatibility with parent if it is interface type
8732
8733       if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
8734         and then Is_Interface (Parent_Type)
8735       then
8736          Parent_Node := Parent (Parent_Type);
8737
8738          --  More detailed checks for interface varieties
8739
8740          Check_Ifaces
8741            (Iface_Def  => Type_Definition (Parent_Node),
8742             Error_Node => Subtype_Indication (Type_Definition (N)));
8743       end if;
8744
8745       Iface := First (Interface_List (Def));
8746       while Present (Iface) loop
8747          Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
8748
8749          Parent_Node := Parent (Base_Type (Iface_Typ));
8750          Iface_Def   := Type_Definition (Parent_Node);
8751
8752          if not Is_Interface (Iface_Typ) then
8753             Diagnose_Interface (Iface, Iface_Typ);
8754
8755          else
8756             --  "The declaration of a specific descendant of an interface
8757             --   type freezes the interface type" RM 13.14
8758
8759             Freeze_Before (N, Iface_Typ);
8760             Check_Ifaces (Iface_Def, Error_Node => Iface);
8761          end if;
8762
8763          Next (Iface);
8764       end loop;
8765
8766       if Is_Task and Is_Protected then
8767          Error_Msg_N
8768            ("type cannot derive from task and protected interface", N);
8769       end if;
8770    end Check_Interfaces;
8771
8772    ------------------------------------
8773    -- Check_Or_Process_Discriminants --
8774    ------------------------------------
8775
8776    --  If an incomplete or private type declaration was already given for the
8777    --  type, the discriminants may have already been processed if they were
8778    --  present on the incomplete declaration. In this case a full conformance
8779    --  check is performed otherwise just process them.
8780
8781    procedure Check_Or_Process_Discriminants
8782      (N    : Node_Id;
8783       T    : Entity_Id;
8784       Prev : Entity_Id := Empty)
8785    is
8786    begin
8787       if Has_Discriminants (T) then
8788
8789          --  Make the discriminants visible to component declarations
8790
8791          declare
8792             D    : Entity_Id;
8793             Prev : Entity_Id;
8794
8795          begin
8796             D := First_Discriminant (T);
8797             while Present (D) loop
8798                Prev := Current_Entity (D);
8799                Set_Current_Entity (D);
8800                Set_Is_Immediately_Visible (D);
8801                Set_Homonym (D, Prev);
8802
8803                --  Ada 2005 (AI-230): Access discriminant allowed in
8804                --  non-limited record types.
8805
8806                if Ada_Version < Ada_05 then
8807
8808                   --  This restriction gets applied to the full type here. It
8809                   --  has already been applied earlier to the partial view.
8810
8811                   Check_Access_Discriminant_Requires_Limited (Parent (D), N);
8812                end if;
8813
8814                Next_Discriminant (D);
8815             end loop;
8816          end;
8817
8818       elsif Present (Discriminant_Specifications (N)) then
8819          Process_Discriminants (N, Prev);
8820       end if;
8821    end Check_Or_Process_Discriminants;
8822
8823    ----------------------
8824    -- Check_Real_Bound --
8825    ----------------------
8826
8827    procedure Check_Real_Bound (Bound : Node_Id) is
8828    begin
8829       if not Is_Real_Type (Etype (Bound)) then
8830          Error_Msg_N
8831            ("bound in real type definition must be of real type", Bound);
8832
8833       elsif not Is_OK_Static_Expression (Bound) then
8834          Flag_Non_Static_Expr
8835            ("non-static expression used for real type bound!", Bound);
8836
8837       else
8838          return;
8839       end if;
8840
8841       Rewrite
8842         (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
8843       Analyze (Bound);
8844       Resolve (Bound, Standard_Float);
8845    end Check_Real_Bound;
8846
8847    ------------------------------
8848    -- Complete_Private_Subtype --
8849    ------------------------------
8850
8851    procedure Complete_Private_Subtype
8852      (Priv        : Entity_Id;
8853       Full        : Entity_Id;
8854       Full_Base   : Entity_Id;
8855       Related_Nod : Node_Id)
8856    is
8857       Save_Next_Entity : Entity_Id;
8858       Save_Homonym     : Entity_Id;
8859
8860    begin
8861       --  Set semantic attributes for (implicit) private subtype completion.
8862       --  If the full type has no discriminants, then it is a copy of the full
8863       --  view of the base. Otherwise, it is a subtype of the base with a
8864       --  possible discriminant constraint. Save and restore the original
8865       --  Next_Entity field of full to ensure that the calls to Copy_Node
8866       --  do not corrupt the entity chain.
8867
8868       --  Note that the type of the full view is the same entity as the type of
8869       --  the partial view. In this fashion, the subtype has access to the
8870       --  correct view of the parent.
8871
8872       Save_Next_Entity := Next_Entity (Full);
8873       Save_Homonym     := Homonym (Priv);
8874
8875       case Ekind (Full_Base) is
8876          when E_Record_Type    |
8877               E_Record_Subtype |
8878               Class_Wide_Kind  |
8879               Private_Kind     |
8880               Task_Kind        |
8881               Protected_Kind   =>
8882             Copy_Node (Priv, Full);
8883
8884             Set_Has_Discriminants  (Full, Has_Discriminants (Full_Base));
8885             Set_First_Entity       (Full, First_Entity (Full_Base));
8886             Set_Last_Entity        (Full, Last_Entity (Full_Base));
8887
8888          when others =>
8889             Copy_Node (Full_Base, Full);
8890             Set_Chars          (Full, Chars (Priv));
8891             Conditional_Delay  (Full, Priv);
8892             Set_Sloc           (Full, Sloc (Priv));
8893       end case;
8894
8895       Set_Next_Entity (Full, Save_Next_Entity);
8896       Set_Homonym     (Full, Save_Homonym);
8897       Set_Associated_Node_For_Itype (Full, Related_Nod);
8898
8899       --  Set common attributes for all subtypes
8900
8901       Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
8902
8903       --  The Etype of the full view is inconsistent. Gigi needs to see the
8904       --  structural full view,  which is what the current scheme gives:
8905       --  the Etype of the full view is the etype of the full base. However,
8906       --  if the full base is a derived type, the full view then looks like
8907       --  a subtype of the parent, not a subtype of the full base. If instead
8908       --  we write:
8909
8910       --       Set_Etype (Full, Full_Base);
8911
8912       --  then we get inconsistencies in the front-end (confusion between
8913       --  views). Several outstanding bugs are related to this ???
8914
8915       Set_Is_First_Subtype (Full, False);
8916       Set_Scope            (Full, Scope (Priv));
8917       Set_Size_Info        (Full, Full_Base);
8918       Set_RM_Size          (Full, RM_Size (Full_Base));
8919       Set_Is_Itype         (Full);
8920
8921       --  A subtype of a private-type-without-discriminants, whose full-view
8922       --  has discriminants with default expressions, is not constrained!
8923
8924       if not Has_Discriminants (Priv) then
8925          Set_Is_Constrained (Full, Is_Constrained (Full_Base));
8926
8927          if Has_Discriminants (Full_Base) then
8928             Set_Discriminant_Constraint
8929               (Full, Discriminant_Constraint (Full_Base));
8930
8931             --  The partial view may have been indefinite, the full view
8932             --  might not be.
8933
8934             Set_Has_Unknown_Discriminants
8935               (Full, Has_Unknown_Discriminants (Full_Base));
8936          end if;
8937       end if;
8938
8939       Set_First_Rep_Item     (Full, First_Rep_Item (Full_Base));
8940       Set_Depends_On_Private (Full, Has_Private_Component (Full));
8941
8942       --  Freeze the private subtype entity if its parent is delayed, and not
8943       --  already frozen. We skip this processing if the type is an anonymous
8944       --  subtype of a record component, or is the corresponding record of a
8945       --  protected type, since ???
8946
8947       if not Is_Type (Scope (Full)) then
8948          Set_Has_Delayed_Freeze (Full,
8949            Has_Delayed_Freeze (Full_Base)
8950              and then (not Is_Frozen (Full_Base)));
8951       end if;
8952
8953       Set_Freeze_Node (Full, Empty);
8954       Set_Is_Frozen (Full, False);
8955       Set_Full_View (Priv, Full);
8956
8957       if Has_Discriminants (Full) then
8958          Set_Stored_Constraint_From_Discriminant_Constraint (Full);
8959          Set_Stored_Constraint (Priv, Stored_Constraint (Full));
8960
8961          if Has_Unknown_Discriminants (Full) then
8962             Set_Discriminant_Constraint (Full, No_Elist);
8963          end if;
8964       end if;
8965
8966       if Ekind (Full_Base) = E_Record_Type
8967         and then Has_Discriminants (Full_Base)
8968         and then Has_Discriminants (Priv) -- might not, if errors
8969         and then not Has_Unknown_Discriminants (Priv)
8970         and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
8971       then
8972          Create_Constrained_Components
8973            (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
8974
8975       --  If the full base is itself derived from private, build a congruent
8976       --  subtype of its underlying type, for use by the back end. For a
8977       --  constrained record component, the declaration cannot be placed on
8978       --  the component list, but it must nevertheless be built an analyzed, to
8979       --  supply enough information for Gigi to compute the size of component.
8980
8981       elsif Ekind (Full_Base) in Private_Kind
8982         and then Is_Derived_Type (Full_Base)
8983         and then Has_Discriminants (Full_Base)
8984         and then (Ekind (Current_Scope) /= E_Record_Subtype)
8985       then
8986          if not Is_Itype (Priv)
8987            and then
8988              Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
8989          then
8990             Build_Underlying_Full_View
8991               (Parent (Priv), Full, Etype (Full_Base));
8992
8993          elsif Nkind (Related_Nod) = N_Component_Declaration then
8994             Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
8995          end if;
8996
8997       elsif Is_Record_Type (Full_Base) then
8998
8999          --  Show Full is simply a renaming of Full_Base
9000
9001          Set_Cloned_Subtype (Full, Full_Base);
9002       end if;
9003
9004       --  It is unsafe to share to bounds of a scalar type, because the Itype
9005       --  is elaborated on demand, and if a bound is non-static then different
9006       --  orders of elaboration in different units will lead to different
9007       --  external symbols.
9008
9009       if Is_Scalar_Type (Full_Base) then
9010          Set_Scalar_Range (Full,
9011            Make_Range (Sloc (Related_Nod),
9012              Low_Bound  =>
9013                Duplicate_Subexpr_No_Checks (Type_Low_Bound  (Full_Base)),
9014              High_Bound =>
9015                Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
9016
9017          --  This completion inherits the bounds of the full parent, but if
9018          --  the parent is an unconstrained floating point type, so is the
9019          --  completion.
9020
9021          if Is_Floating_Point_Type (Full_Base) then
9022             Set_Includes_Infinities
9023              (Scalar_Range (Full), Has_Infinities (Full_Base));
9024          end if;
9025       end if;
9026
9027       --  ??? It seems that a lot of fields are missing that should be copied
9028       --  from Full_Base to Full. Here are some that are introduced in a
9029       --  non-disruptive way but a cleanup is necessary.
9030
9031       if Is_Tagged_Type (Full_Base) then
9032          Set_Is_Tagged_Type (Full);
9033          Set_Primitive_Operations (Full, Primitive_Operations (Full_Base));
9034          Set_Class_Wide_Type      (Full, Class_Wide_Type (Full_Base));
9035
9036       --  If this is a subtype of a protected or task type, constrain its
9037       --  corresponding record, unless this is a subtype without constraints,
9038       --  i.e. a simple renaming as with an actual subtype in an instance.
9039
9040       elsif Is_Concurrent_Type (Full_Base) then
9041          if Has_Discriminants (Full)
9042            and then Present (Corresponding_Record_Type (Full_Base))
9043            and then
9044              not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
9045          then
9046             Set_Corresponding_Record_Type (Full,
9047               Constrain_Corresponding_Record
9048                 (Full, Corresponding_Record_Type (Full_Base),
9049                   Related_Nod, Full_Base));
9050
9051          else
9052             Set_Corresponding_Record_Type (Full,
9053               Corresponding_Record_Type (Full_Base));
9054          end if;
9055       end if;
9056    end Complete_Private_Subtype;
9057
9058    ----------------------------
9059    -- Constant_Redeclaration --
9060    ----------------------------
9061
9062    procedure Constant_Redeclaration
9063      (Id : Entity_Id;
9064       N  : Node_Id;
9065       T  : out Entity_Id)
9066    is
9067       Prev    : constant Entity_Id := Current_Entity_In_Scope (Id);
9068       Obj_Def : constant Node_Id := Object_Definition (N);
9069       New_T   : Entity_Id;
9070
9071       procedure Check_Possible_Deferred_Completion
9072         (Prev_Id      : Entity_Id;
9073          Prev_Obj_Def : Node_Id;
9074          Curr_Obj_Def : Node_Id);
9075       --  Determine whether the two object definitions describe the partial
9076       --  and the full view of a constrained deferred constant. Generate
9077       --  a subtype for the full view and verify that it statically matches
9078       --  the subtype of the partial view.
9079
9080       procedure Check_Recursive_Declaration (Typ : Entity_Id);
9081       --  If deferred constant is an access type initialized with an allocator,
9082       --  check whether there is an illegal recursion in the definition,
9083       --  through a default value of some record subcomponent. This is normally
9084       --  detected when generating init procs, but requires this additional
9085       --  mechanism when expansion is disabled.
9086
9087       ----------------------------------------
9088       -- Check_Possible_Deferred_Completion --
9089       ----------------------------------------
9090
9091       procedure Check_Possible_Deferred_Completion
9092         (Prev_Id      : Entity_Id;
9093          Prev_Obj_Def : Node_Id;
9094          Curr_Obj_Def : Node_Id)
9095       is
9096       begin
9097          if Nkind (Prev_Obj_Def) = N_Subtype_Indication
9098            and then Present (Constraint (Prev_Obj_Def))
9099            and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
9100            and then Present (Constraint (Curr_Obj_Def))
9101          then
9102             declare
9103                Loc    : constant Source_Ptr := Sloc (N);
9104                Def_Id : constant Entity_Id :=
9105                           Make_Defining_Identifier (Loc,
9106                             New_Internal_Name ('S'));
9107                Decl   : constant Node_Id :=
9108                           Make_Subtype_Declaration (Loc,
9109                             Defining_Identifier =>
9110                               Def_Id,
9111                             Subtype_Indication =>
9112                               Relocate_Node (Curr_Obj_Def));
9113
9114             begin
9115                Insert_Before_And_Analyze (N, Decl);
9116                Set_Etype (Id, Def_Id);
9117
9118                if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
9119                   Error_Msg_Sloc := Sloc (Prev_Id);
9120                   Error_Msg_N ("subtype does not statically match deferred " &
9121                                "declaration#", N);
9122                end if;
9123             end;
9124          end if;
9125       end Check_Possible_Deferred_Completion;
9126
9127       ---------------------------------
9128       -- Check_Recursive_Declaration --
9129       ---------------------------------
9130
9131       procedure Check_Recursive_Declaration (Typ : Entity_Id) is
9132          Comp : Entity_Id;
9133
9134       begin
9135          if Is_Record_Type (Typ) then
9136             Comp := First_Component (Typ);
9137             while Present (Comp) loop
9138                if Comes_From_Source (Comp) then
9139                   if Present (Expression (Parent (Comp)))
9140                     and then Is_Entity_Name (Expression (Parent (Comp)))
9141                     and then Entity (Expression (Parent (Comp))) = Prev
9142                   then
9143                      Error_Msg_Sloc := Sloc (Parent (Comp));
9144                      Error_Msg_NE
9145                        ("illegal circularity with declaration for&#",
9146                          N, Comp);
9147                      return;
9148
9149                   elsif Is_Record_Type (Etype (Comp)) then
9150                      Check_Recursive_Declaration (Etype (Comp));
9151                   end if;
9152                end if;
9153
9154                Next_Component (Comp);
9155             end loop;
9156          end if;
9157       end Check_Recursive_Declaration;
9158
9159    --  Start of processing for Constant_Redeclaration
9160
9161    begin
9162       if Nkind (Parent (Prev)) = N_Object_Declaration then
9163          if Nkind (Object_Definition
9164                      (Parent (Prev))) = N_Subtype_Indication
9165          then
9166             --  Find type of new declaration. The constraints of the two
9167             --  views must match statically, but there is no point in
9168             --  creating an itype for the full view.
9169
9170             if Nkind (Obj_Def) = N_Subtype_Indication then
9171                Find_Type (Subtype_Mark (Obj_Def));
9172                New_T := Entity (Subtype_Mark (Obj_Def));
9173
9174             else
9175                Find_Type (Obj_Def);
9176                New_T := Entity (Obj_Def);
9177             end if;
9178
9179             T := Etype (Prev);
9180
9181          else
9182             --  The full view may impose a constraint, even if the partial
9183             --  view does not, so construct the subtype.
9184
9185             New_T := Find_Type_Of_Object (Obj_Def, N);
9186             T     := New_T;
9187          end if;
9188
9189       else
9190          --  Current declaration is illegal, diagnosed below in Enter_Name
9191
9192          T := Empty;
9193          New_T := Any_Type;
9194       end if;
9195
9196       --  If previous full declaration exists, or if a homograph is present,
9197       --  let Enter_Name handle it, either with an error, or with the removal
9198       --  of an overridden implicit subprogram.
9199
9200       if Ekind (Prev) /= E_Constant
9201         or else Present (Expression (Parent (Prev)))
9202         or else Present (Full_View (Prev))
9203       then
9204          Enter_Name (Id);
9205
9206       --  Verify that types of both declarations match, or else that both types
9207       --  are anonymous access types whose designated subtypes statically match
9208       --  (as allowed in Ada 2005 by AI-385).
9209
9210       elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
9211         and then
9212           (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
9213              or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
9214              or else Is_Access_Constant (Etype (New_T)) /=
9215                      Is_Access_Constant (Etype (Prev))
9216              or else Can_Never_Be_Null (Etype (New_T)) /=
9217                      Can_Never_Be_Null (Etype (Prev))
9218              or else Null_Exclusion_Present (Parent (Prev)) /=
9219                      Null_Exclusion_Present (Parent (Id))
9220              or else not Subtypes_Statically_Match
9221                            (Designated_Type (Etype (Prev)),
9222                             Designated_Type (Etype (New_T))))
9223       then
9224          Error_Msg_Sloc := Sloc (Prev);
9225          Error_Msg_N ("type does not match declaration#", N);
9226          Set_Full_View (Prev, Id);
9227          Set_Etype (Id, Any_Type);
9228
9229       elsif
9230         Null_Exclusion_Present (Parent (Prev))
9231           and then not Null_Exclusion_Present (N)
9232       then
9233          Error_Msg_Sloc := Sloc (Prev);
9234          Error_Msg_N ("null-exclusion does not match declaration#", N);
9235          Set_Full_View (Prev, Id);
9236          Set_Etype (Id, Any_Type);
9237
9238       --  If so, process the full constant declaration
9239
9240       else
9241          --  RM 7.4 (6): If the subtype defined by the subtype_indication in
9242          --  the deferred declaration is constrained, then the subtype defined
9243          --  by the subtype_indication in the full declaration shall match it
9244          --  statically.
9245
9246          Check_Possible_Deferred_Completion
9247            (Prev_Id      => Prev,
9248             Prev_Obj_Def => Object_Definition (Parent (Prev)),
9249             Curr_Obj_Def => Obj_Def);
9250
9251          Set_Full_View (Prev, Id);
9252          Set_Is_Public (Id, Is_Public (Prev));
9253          Set_Is_Internal (Id);
9254          Append_Entity (Id, Current_Scope);
9255
9256          --  Check ALIASED present if present before (RM 7.4(7))
9257
9258          if Is_Aliased (Prev)
9259            and then not Aliased_Present (N)
9260          then
9261             Error_Msg_Sloc := Sloc (Prev);
9262             Error_Msg_N ("ALIASED required (see declaration#)", N);
9263          end if;
9264
9265          --  Allow incomplete declaration of tags (used to handle forward
9266          --  references to tags). The check on Ada_Tags avoids circularities
9267          --  when rebuilding the compiler.
9268
9269          if RTU_Loaded (Ada_Tags)
9270            and then T = RTE (RE_Tag)
9271          then
9272             null;
9273
9274          --  Check that placement is in private part and that the incomplete
9275          --  declaration appeared in the visible part.
9276
9277          elsif Ekind (Current_Scope) = E_Package
9278            and then not In_Private_Part (Current_Scope)
9279          then
9280             Error_Msg_Sloc := Sloc (Prev);
9281             Error_Msg_N ("full constant for declaration#"
9282                          & " must be in private part", N);
9283
9284          elsif Ekind (Current_Scope) = E_Package
9285            and then List_Containing (Parent (Prev))
9286            /= Visible_Declarations
9287              (Specification (Unit_Declaration_Node (Current_Scope)))
9288          then
9289             Error_Msg_N
9290               ("deferred constant must be declared in visible part",
9291                  Parent (Prev));
9292          end if;
9293
9294          if Is_Access_Type (T)
9295            and then Nkind (Expression (N)) = N_Allocator
9296          then
9297             Check_Recursive_Declaration (Designated_Type (T));
9298          end if;
9299       end if;
9300    end Constant_Redeclaration;
9301
9302    ----------------------
9303    -- Constrain_Access --
9304    ----------------------
9305
9306    procedure Constrain_Access
9307      (Def_Id      : in out Entity_Id;
9308       S           : Node_Id;
9309       Related_Nod : Node_Id)
9310    is
9311       T             : constant Entity_Id := Entity (Subtype_Mark (S));
9312       Desig_Type    : constant Entity_Id := Designated_Type (T);
9313       Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
9314       Constraint_OK : Boolean := True;
9315
9316       function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
9317       --  Simple predicate to test for defaulted discriminants
9318       --  Shouldn't this be in sem_util???
9319
9320       ---------------------------------
9321       -- Has_Defaulted_Discriminants --
9322       ---------------------------------
9323
9324       function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
9325       begin
9326          return Has_Discriminants (Typ)
9327           and then Present (First_Discriminant (Typ))
9328           and then Present
9329             (Discriminant_Default_Value (First_Discriminant (Typ)));
9330       end Has_Defaulted_Discriminants;
9331
9332    --  Start of processing for Constrain_Access
9333
9334    begin
9335       if Is_Array_Type (Desig_Type) then
9336          Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
9337
9338       elsif (Is_Record_Type (Desig_Type)
9339               or else Is_Incomplete_Or_Private_Type (Desig_Type))
9340         and then not Is_Constrained (Desig_Type)
9341       then
9342          --  ??? The following code is a temporary kludge to ignore a
9343          --  discriminant constraint on access type if it is constraining
9344          --  the current record. Avoid creating the implicit subtype of the
9345          --  record we are currently compiling since right now, we cannot
9346          --  handle these. For now, just return the access type itself.
9347
9348          if Desig_Type = Current_Scope
9349            and then No (Def_Id)
9350          then
9351             Set_Ekind (Desig_Subtype, E_Record_Subtype);
9352             Def_Id := Entity (Subtype_Mark (S));
9353
9354             --  This call added to ensure that the constraint is analyzed
9355             --  (needed for a B test). Note that we still return early from
9356             --  this procedure to avoid recursive processing. ???
9357
9358             Constrain_Discriminated_Type
9359               (Desig_Subtype, S, Related_Nod, For_Access => True);
9360             return;
9361          end if;
9362
9363          if (Ekind (T) = E_General_Access_Type
9364               or else Ada_Version >= Ada_05)
9365            and then Has_Private_Declaration (Desig_Type)
9366            and then In_Open_Scopes (Scope (Desig_Type))
9367            and then Has_Discriminants (Desig_Type)
9368          then
9369             --  Enforce rule that the constraint is illegal if there is
9370             --  an unconstrained view of the designated type. This means
9371             --  that the partial view (either a private type declaration or
9372             --  a derivation from a private type) has no discriminants.
9373             --  (Defect Report 8652/0008, Technical Corrigendum 1, checked
9374             --  by ACATS B371001).
9375
9376             --  Rule updated for Ada 2005: the private type is said to have
9377             --  a constrained partial view, given that objects of the type
9378             --  can be declared. Furthermore, the rule applies to all access
9379             --  types, unlike the rule concerning default discriminants.
9380
9381             declare
9382                Pack  : constant Node_Id :=
9383                          Unit_Declaration_Node (Scope (Desig_Type));
9384                Decls : List_Id;
9385                Decl  : Node_Id;
9386
9387             begin
9388                if Nkind (Pack) = N_Package_Declaration then
9389                   Decls := Visible_Declarations (Specification (Pack));
9390                   Decl := First (Decls);
9391                   while Present (Decl) loop
9392                      if (Nkind (Decl) = N_Private_Type_Declaration
9393                           and then
9394                             Chars (Defining_Identifier (Decl)) =
9395                                                      Chars (Desig_Type))
9396
9397                        or else
9398                         (Nkind (Decl) = N_Full_Type_Declaration
9399                           and then
9400                             Chars (Defining_Identifier (Decl)) =
9401                                                      Chars (Desig_Type)
9402                           and then Is_Derived_Type (Desig_Type)
9403                           and then
9404                             Has_Private_Declaration (Etype (Desig_Type)))
9405                      then
9406                         if No (Discriminant_Specifications (Decl)) then
9407                            Error_Msg_N
9408                             ("cannot constrain general access type if " &
9409                                "designated type has constrained partial view",
9410                                 S);
9411                         end if;
9412
9413                         exit;
9414                      end if;
9415
9416                      Next (Decl);
9417                   end loop;
9418                end if;
9419             end;
9420          end if;
9421
9422          Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
9423            For_Access => True);
9424
9425       elsif (Is_Task_Type (Desig_Type)
9426               or else Is_Protected_Type (Desig_Type))
9427         and then not Is_Constrained (Desig_Type)
9428       then
9429          Constrain_Concurrent
9430            (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
9431
9432       else
9433          Error_Msg_N ("invalid constraint on access type", S);
9434          Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
9435          Constraint_OK := False;
9436       end if;
9437
9438       if No (Def_Id) then
9439          Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
9440       else
9441          Set_Ekind (Def_Id, E_Access_Subtype);
9442       end if;
9443
9444       if Constraint_OK then
9445          Set_Etype (Def_Id, Base_Type (T));
9446
9447          if Is_Private_Type (Desig_Type) then
9448             Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
9449          end if;
9450       else
9451          Set_Etype (Def_Id, Any_Type);
9452       end if;
9453
9454       Set_Size_Info                (Def_Id, T);
9455       Set_Is_Constrained           (Def_Id, Constraint_OK);
9456       Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
9457       Set_Depends_On_Private       (Def_Id, Has_Private_Component (Def_Id));
9458       Set_Is_Access_Constant       (Def_Id, Is_Access_Constant (T));
9459
9460       Conditional_Delay (Def_Id, T);
9461
9462       --  AI-363 : Subtypes of general access types whose designated types have
9463       --  default discriminants are disallowed. In instances, the rule has to
9464       --  be checked against the actual, of which T is the subtype. In a
9465       --  generic body, the rule is checked assuming that the actual type has
9466       --  defaulted discriminants.
9467
9468       if Ada_Version >= Ada_05 or else Warn_On_Ada_2005_Compatibility then
9469          if Ekind (Base_Type (T)) = E_General_Access_Type
9470            and then Has_Defaulted_Discriminants (Desig_Type)
9471          then
9472             if Ada_Version < Ada_05 then
9473                Error_Msg_N
9474                  ("access subtype of general access type would not " &
9475                   "be allowed in Ada 2005?", S);
9476             else
9477                Error_Msg_N
9478                  ("access subype of general access type not allowed", S);
9479             end if;
9480
9481             Error_Msg_N ("\discriminants have defaults", S);
9482
9483          elsif Is_Access_Type (T)
9484            and then Is_Generic_Type (Desig_Type)
9485            and then Has_Discriminants (Desig_Type)
9486            and then In_Package_Body (Current_Scope)
9487          then
9488             if Ada_Version < Ada_05 then
9489                Error_Msg_N
9490                  ("access subtype would not be allowed in generic body " &
9491                   "in Ada 2005?", S);
9492             else
9493                Error_Msg_N
9494                  ("access subtype not allowed in generic body", S);
9495             end if;
9496
9497             Error_Msg_N
9498               ("\designated type is a discriminated formal", S);
9499          end if;
9500       end if;
9501    end Constrain_Access;
9502
9503    ---------------------
9504    -- Constrain_Array --
9505    ---------------------
9506
9507    procedure Constrain_Array
9508      (Def_Id      : in out Entity_Id;
9509       SI          : Node_Id;
9510       Related_Nod : Node_Id;
9511       Related_Id  : Entity_Id;
9512       Suffix      : Character)
9513    is
9514       C                     : constant Node_Id := Constraint (SI);
9515       Number_Of_Constraints : Nat := 0;
9516       Index                 : Node_Id;
9517       S, T                  : Entity_Id;
9518       Constraint_OK         : Boolean := True;
9519
9520    begin
9521       T := Entity (Subtype_Mark (SI));
9522
9523       if Ekind (T) in Access_Kind then
9524          T := Designated_Type (T);
9525       end if;
9526
9527       --  If an index constraint follows a subtype mark in a subtype indication
9528       --  then the type or subtype denoted by the subtype mark must not already
9529       --  impose an index constraint. The subtype mark must denote either an
9530       --  unconstrained array type or an access type whose designated type
9531       --  is such an array type... (RM 3.6.1)
9532
9533       if Is_Constrained (T) then
9534          Error_Msg_N
9535            ("array type is already constrained", Subtype_Mark (SI));
9536          Constraint_OK := False;
9537
9538       else
9539          S := First (Constraints (C));
9540          while Present (S) loop
9541             Number_Of_Constraints := Number_Of_Constraints + 1;
9542             Next (S);
9543          end loop;
9544
9545          --  In either case, the index constraint must provide a discrete
9546          --  range for each index of the array type and the type of each
9547          --  discrete range must be the same as that of the corresponding
9548          --  index. (RM 3.6.1)
9549
9550          if Number_Of_Constraints /= Number_Dimensions (T) then
9551             Error_Msg_NE ("incorrect number of index constraints for }", C, T);
9552             Constraint_OK := False;
9553
9554          else
9555             S := First (Constraints (C));
9556             Index := First_Index (T);
9557             Analyze (Index);
9558
9559             --  Apply constraints to each index type
9560
9561             for J in 1 .. Number_Of_Constraints loop
9562                Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
9563                Next (Index);
9564                Next (S);
9565             end loop;
9566
9567          end if;
9568       end if;
9569
9570       if No (Def_Id) then
9571          Def_Id :=
9572            Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
9573          Set_Parent (Def_Id, Related_Nod);
9574
9575       else
9576          Set_Ekind (Def_Id, E_Array_Subtype);
9577       end if;
9578
9579       Set_Size_Info      (Def_Id,                (T));
9580       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9581       Set_Etype          (Def_Id, Base_Type      (T));
9582
9583       if Constraint_OK then
9584          Set_First_Index (Def_Id, First (Constraints (C)));
9585       else
9586          Set_First_Index (Def_Id, First_Index (T));
9587       end if;
9588
9589       Set_Is_Constrained     (Def_Id, True);
9590       Set_Is_Aliased         (Def_Id, Is_Aliased (T));
9591       Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
9592
9593       Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
9594       Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
9595
9596       --  A subtype does not inherit the packed_array_type of is parent. We
9597       --  need to initialize the attribute because if Def_Id is previously
9598       --  analyzed through a limited_with clause, it will have the attributes
9599       --  of an incomplete type, one of which is an Elist that overlaps the
9600       --  Packed_Array_Type field.
9601
9602       Set_Packed_Array_Type (Def_Id, Empty);
9603
9604       --  Build a freeze node if parent still needs one. Also make sure that
9605       --  the Depends_On_Private status is set because the subtype will need
9606       --  reprocessing at the time the base type does, and also we must set a
9607       --  conditional delay.
9608
9609       Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9610       Conditional_Delay (Def_Id, T);
9611    end Constrain_Array;
9612
9613    ------------------------------
9614    -- Constrain_Component_Type --
9615    ------------------------------
9616
9617    function Constrain_Component_Type
9618      (Comp            : Entity_Id;
9619       Constrained_Typ : Entity_Id;
9620       Related_Node    : Node_Id;
9621       Typ             : Entity_Id;
9622       Constraints     : Elist_Id) return Entity_Id
9623    is
9624       Loc         : constant Source_Ptr := Sloc (Constrained_Typ);
9625       Compon_Type : constant Entity_Id := Etype (Comp);
9626
9627       function Build_Constrained_Array_Type
9628         (Old_Type : Entity_Id) return Entity_Id;
9629       --  If Old_Type is an array type, one of whose indices is constrained
9630       --  by a discriminant, build an Itype whose constraint replaces the
9631       --  discriminant with its value in the constraint.
9632
9633       function Build_Constrained_Discriminated_Type
9634         (Old_Type : Entity_Id) return Entity_Id;
9635       --  Ditto for record components
9636
9637       function Build_Constrained_Access_Type
9638         (Old_Type : Entity_Id) return Entity_Id;
9639       --  Ditto for access types. Makes use of previous two functions, to
9640       --  constrain designated type.
9641
9642       function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
9643       --  T is an array or discriminated type, C is a list of constraints
9644       --  that apply to T. This routine builds the constrained subtype.
9645
9646       function Is_Discriminant (Expr : Node_Id) return Boolean;
9647       --  Returns True if Expr is a discriminant
9648
9649       function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
9650       --  Find the value of discriminant Discrim in Constraint
9651
9652       -----------------------------------
9653       -- Build_Constrained_Access_Type --
9654       -----------------------------------
9655
9656       function Build_Constrained_Access_Type
9657         (Old_Type : Entity_Id) return Entity_Id
9658       is
9659          Desig_Type    : constant Entity_Id := Designated_Type (Old_Type);
9660          Itype         : Entity_Id;
9661          Desig_Subtype : Entity_Id;
9662          Scop          : Entity_Id;
9663
9664       begin
9665          --  if the original access type was not embedded in the enclosing
9666          --  type definition, there is no need to produce a new access
9667          --  subtype. In fact every access type with an explicit constraint
9668          --  generates an itype whose scope is the enclosing record.
9669
9670          if not Is_Type (Scope (Old_Type)) then
9671             return Old_Type;
9672
9673          elsif Is_Array_Type (Desig_Type) then
9674             Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
9675
9676          elsif Has_Discriminants (Desig_Type) then
9677
9678             --  This may be an access type to an enclosing record type for
9679             --  which we are constructing the constrained components. Return
9680             --  the enclosing record subtype. This is not always correct,
9681             --  but avoids infinite recursion. ???
9682
9683             Desig_Subtype := Any_Type;
9684
9685             for J in reverse 0 .. Scope_Stack.Last loop
9686                Scop := Scope_Stack.Table (J).Entity;
9687
9688                if Is_Type (Scop)
9689                  and then Base_Type (Scop) = Base_Type (Desig_Type)
9690                then
9691                   Desig_Subtype := Scop;
9692                end if;
9693
9694                exit when not Is_Type (Scop);
9695             end loop;
9696
9697             if Desig_Subtype = Any_Type then
9698                Desig_Subtype :=
9699                  Build_Constrained_Discriminated_Type (Desig_Type);
9700             end if;
9701
9702          else
9703             return Old_Type;
9704          end if;
9705
9706          if Desig_Subtype /= Desig_Type then
9707
9708             --  The Related_Node better be here or else we won't be able
9709             --  to attach new itypes to a node in the tree.
9710
9711             pragma Assert (Present (Related_Node));
9712
9713             Itype := Create_Itype (E_Access_Subtype, Related_Node);
9714
9715             Set_Etype                    (Itype, Base_Type      (Old_Type));
9716             Set_Size_Info                (Itype,                (Old_Type));
9717             Set_Directly_Designated_Type (Itype, Desig_Subtype);
9718             Set_Depends_On_Private       (Itype, Has_Private_Component
9719                                                                 (Old_Type));
9720             Set_Is_Access_Constant       (Itype, Is_Access_Constant
9721                                                                 (Old_Type));
9722
9723             --  The new itype needs freezing when it depends on a not frozen
9724             --  type and the enclosing subtype needs freezing.
9725
9726             if Has_Delayed_Freeze (Constrained_Typ)
9727               and then not Is_Frozen (Constrained_Typ)
9728             then
9729                Conditional_Delay (Itype, Base_Type (Old_Type));
9730             end if;
9731
9732             return Itype;
9733
9734          else
9735             return Old_Type;
9736          end if;
9737       end Build_Constrained_Access_Type;
9738
9739       ----------------------------------
9740       -- Build_Constrained_Array_Type --
9741       ----------------------------------
9742
9743       function Build_Constrained_Array_Type
9744         (Old_Type : Entity_Id) return Entity_Id
9745       is
9746          Lo_Expr     : Node_Id;
9747          Hi_Expr     : Node_Id;
9748          Old_Index   : Node_Id;
9749          Range_Node  : Node_Id;
9750          Constr_List : List_Id;
9751
9752          Need_To_Create_Itype : Boolean := False;
9753
9754       begin
9755          Old_Index := First_Index (Old_Type);
9756          while Present (Old_Index) loop
9757             Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
9758
9759             if Is_Discriminant (Lo_Expr)
9760               or else Is_Discriminant (Hi_Expr)
9761             then
9762                Need_To_Create_Itype := True;
9763             end if;
9764
9765             Next_Index (Old_Index);
9766          end loop;
9767
9768          if Need_To_Create_Itype then
9769             Constr_List := New_List;
9770
9771             Old_Index := First_Index (Old_Type);
9772             while Present (Old_Index) loop
9773                Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
9774
9775                if Is_Discriminant (Lo_Expr) then
9776                   Lo_Expr := Get_Discr_Value (Lo_Expr);
9777                end if;
9778
9779                if Is_Discriminant (Hi_Expr) then
9780                   Hi_Expr := Get_Discr_Value (Hi_Expr);
9781                end if;
9782
9783                Range_Node :=
9784                  Make_Range
9785                    (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
9786
9787                Append (Range_Node, To => Constr_List);
9788
9789                Next_Index (Old_Index);
9790             end loop;
9791
9792             return Build_Subtype (Old_Type, Constr_List);
9793
9794          else
9795             return Old_Type;
9796          end if;
9797       end Build_Constrained_Array_Type;
9798
9799       ------------------------------------------
9800       -- Build_Constrained_Discriminated_Type --
9801       ------------------------------------------
9802
9803       function Build_Constrained_Discriminated_Type
9804         (Old_Type : Entity_Id) return Entity_Id
9805       is
9806          Expr           : Node_Id;
9807          Constr_List    : List_Id;
9808          Old_Constraint : Elmt_Id;
9809
9810          Need_To_Create_Itype : Boolean := False;
9811
9812       begin
9813          Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
9814          while Present (Old_Constraint) loop
9815             Expr := Node (Old_Constraint);
9816
9817             if Is_Discriminant (Expr) then
9818                Need_To_Create_Itype := True;
9819             end if;
9820
9821             Next_Elmt (Old_Constraint);
9822          end loop;
9823
9824          if Need_To_Create_Itype then
9825             Constr_List := New_List;
9826
9827             Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
9828             while Present (Old_Constraint) loop
9829                Expr := Node (Old_Constraint);
9830
9831                if Is_Discriminant (Expr) then
9832                   Expr := Get_Discr_Value (Expr);
9833                end if;
9834
9835                Append (New_Copy_Tree (Expr), To => Constr_List);
9836
9837                Next_Elmt (Old_Constraint);
9838             end loop;
9839
9840             return Build_Subtype (Old_Type, Constr_List);
9841
9842          else
9843             return Old_Type;
9844          end if;
9845       end Build_Constrained_Discriminated_Type;
9846
9847       -------------------
9848       -- Build_Subtype --
9849       -------------------
9850
9851       function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
9852          Indic       : Node_Id;
9853          Subtyp_Decl : Node_Id;
9854          Def_Id      : Entity_Id;
9855          Btyp        : Entity_Id := Base_Type (T);
9856
9857       begin
9858          --  The Related_Node better be here or else we won't be able to
9859          --  attach new itypes to a node in the tree.
9860
9861          pragma Assert (Present (Related_Node));
9862
9863          --  If the view of the component's type is incomplete or private
9864          --  with unknown discriminants, then the constraint must be applied
9865          --  to the full type.
9866
9867          if Has_Unknown_Discriminants (Btyp)
9868            and then Present (Underlying_Type (Btyp))
9869          then
9870             Btyp := Underlying_Type (Btyp);
9871          end if;
9872
9873          Indic :=
9874            Make_Subtype_Indication (Loc,
9875              Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
9876              Constraint   => Make_Index_Or_Discriminant_Constraint (Loc, C));
9877
9878          Def_Id := Create_Itype (Ekind (T), Related_Node);
9879
9880          Subtyp_Decl :=
9881            Make_Subtype_Declaration (Loc,
9882              Defining_Identifier => Def_Id,
9883              Subtype_Indication  => Indic);
9884
9885          Set_Parent (Subtyp_Decl, Parent (Related_Node));
9886
9887          --  Itypes must be analyzed with checks off (see package Itypes)
9888
9889          Analyze (Subtyp_Decl, Suppress => All_Checks);
9890
9891          return Def_Id;
9892       end Build_Subtype;
9893
9894       ---------------------
9895       -- Get_Discr_Value --
9896       ---------------------
9897
9898       function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
9899          D : Entity_Id;
9900          E : Elmt_Id;
9901
9902       begin
9903          --  The discriminant may be declared for the type, in which case we
9904          --  find it by iterating over the list of discriminants. If the
9905          --  discriminant is inherited from a parent type, it appears as the
9906          --  corresponding discriminant of the current type. This will be the
9907          --  case when constraining an inherited component whose constraint is
9908          --  given by a discriminant of the parent.
9909
9910          D := First_Discriminant (Typ);
9911          E := First_Elmt (Constraints);
9912
9913          while Present (D) loop
9914             if D = Entity (Discrim)
9915               or else D = CR_Discriminant (Entity (Discrim))
9916               or else Corresponding_Discriminant (D) = Entity (Discrim)
9917             then
9918                return Node (E);
9919             end if;
9920
9921             Next_Discriminant (D);
9922             Next_Elmt (E);
9923          end loop;
9924
9925          --  The corresponding_Discriminant mechanism is incomplete, because
9926          --  the correspondence between new and old discriminants is not one
9927          --  to one: one new discriminant can constrain several old ones. In
9928          --  that case, scan sequentially the stored_constraint, the list of
9929          --  discriminants of the parents, and the constraints.
9930          --  Previous code checked for the present of the Stored_Constraint
9931          --  list for the derived type, but did not use it at all. Should it
9932          --  be present when the component is a discriminated task type?
9933
9934          if Is_Derived_Type (Typ)
9935            and then Scope (Entity (Discrim)) = Etype (Typ)
9936          then
9937             D := First_Discriminant (Etype (Typ));
9938             E := First_Elmt (Constraints);
9939             while Present (D) loop
9940                if D = Entity (Discrim) then
9941                   return Node (E);
9942                end if;
9943
9944                Next_Discriminant (D);
9945                Next_Elmt (E);
9946             end loop;
9947          end if;
9948
9949          --  Something is wrong if we did not find the value
9950
9951          raise Program_Error;
9952       end Get_Discr_Value;
9953
9954       ---------------------
9955       -- Is_Discriminant --
9956       ---------------------
9957
9958       function Is_Discriminant (Expr : Node_Id) return Boolean is
9959          Discrim_Scope : Entity_Id;
9960
9961       begin
9962          if Denotes_Discriminant (Expr) then
9963             Discrim_Scope := Scope (Entity (Expr));
9964
9965             --  Either we have a reference to one of Typ's discriminants,
9966
9967             pragma Assert (Discrim_Scope = Typ
9968
9969                --  or to the discriminants of the parent type, in the case
9970                --  of a derivation of a tagged type with variants.
9971
9972                or else Discrim_Scope = Etype (Typ)
9973                or else Full_View (Discrim_Scope) = Etype (Typ)
9974
9975                --  or same as above for the case where the discriminants
9976                --  were declared in Typ's private view.
9977
9978                or else (Is_Private_Type (Discrim_Scope)
9979                         and then Chars (Discrim_Scope) = Chars (Typ))
9980
9981                --  or else we are deriving from the full view and the
9982                --  discriminant is declared in the private entity.
9983
9984                or else (Is_Private_Type (Typ)
9985                          and then Chars (Discrim_Scope) = Chars (Typ))
9986
9987                --  Or we are constrained the corresponding record of a
9988                --  synchronized type that completes a private declaration.
9989
9990                or else (Is_Concurrent_Record_Type (Typ)
9991                          and then
9992                            Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
9993
9994                --  or we have a class-wide type, in which case make sure the
9995                --  discriminant found belongs to the root type.
9996
9997                or else (Is_Class_Wide_Type (Typ)
9998                          and then Etype (Typ) = Discrim_Scope));
9999
10000             return True;
10001          end if;
10002
10003          --  In all other cases we have something wrong
10004
10005          return False;
10006       end Is_Discriminant;
10007
10008    --  Start of processing for Constrain_Component_Type
10009
10010    begin
10011       if Nkind (Parent (Comp)) = N_Component_Declaration
10012         and then Comes_From_Source (Parent (Comp))
10013         and then Comes_From_Source
10014           (Subtype_Indication (Component_Definition (Parent (Comp))))
10015         and then
10016           Is_Entity_Name
10017             (Subtype_Indication (Component_Definition (Parent (Comp))))
10018       then
10019          return Compon_Type;
10020
10021       elsif Is_Array_Type (Compon_Type) then
10022          return Build_Constrained_Array_Type (Compon_Type);
10023
10024       elsif Has_Discriminants (Compon_Type) then
10025          return Build_Constrained_Discriminated_Type (Compon_Type);
10026
10027       elsif Is_Access_Type (Compon_Type) then
10028          return Build_Constrained_Access_Type (Compon_Type);
10029
10030       else
10031          return Compon_Type;
10032       end if;
10033    end Constrain_Component_Type;
10034
10035    --------------------------
10036    -- Constrain_Concurrent --
10037    --------------------------
10038
10039    --  For concurrent types, the associated record value type carries the same
10040    --  discriminants, so when we constrain a concurrent type, we must constrain
10041    --  the corresponding record type as well.
10042
10043    procedure Constrain_Concurrent
10044      (Def_Id      : in out Entity_Id;
10045       SI          : Node_Id;
10046       Related_Nod : Node_Id;
10047       Related_Id  : Entity_Id;
10048       Suffix      : Character)
10049    is
10050       T_Ent : Entity_Id := Entity (Subtype_Mark (SI));
10051       T_Val : Entity_Id;
10052
10053    begin
10054       if Ekind (T_Ent) in Access_Kind then
10055          T_Ent := Designated_Type (T_Ent);
10056       end if;
10057
10058       T_Val := Corresponding_Record_Type (T_Ent);
10059
10060       if Present (T_Val) then
10061
10062          if No (Def_Id) then
10063             Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
10064          end if;
10065
10066          Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
10067
10068          Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10069          Set_Corresponding_Record_Type (Def_Id,
10070            Constrain_Corresponding_Record
10071              (Def_Id, T_Val, Related_Nod, Related_Id));
10072
10073       else
10074          --  If there is no associated record, expansion is disabled and this
10075          --  is a generic context. Create a subtype in any case, so that
10076          --  semantic analysis can proceed.
10077
10078          if No (Def_Id) then
10079             Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
10080          end if;
10081
10082          Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
10083       end if;
10084    end Constrain_Concurrent;
10085
10086    ------------------------------------
10087    -- Constrain_Corresponding_Record --
10088    ------------------------------------
10089
10090    function Constrain_Corresponding_Record
10091      (Prot_Subt   : Entity_Id;
10092       Corr_Rec    : Entity_Id;
10093       Related_Nod : Node_Id;
10094       Related_Id  : Entity_Id) return Entity_Id
10095    is
10096       T_Sub : constant Entity_Id :=
10097                 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
10098
10099    begin
10100       Set_Etype             (T_Sub, Corr_Rec);
10101       Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
10102       Set_Is_Constrained    (T_Sub, True);
10103       Set_First_Entity      (T_Sub, First_Entity (Corr_Rec));
10104       Set_Last_Entity       (T_Sub, Last_Entity  (Corr_Rec));
10105
10106       --  As elsewhere, we do not want to create a freeze node for this itype
10107       --  if it is created for a constrained component of an enclosing record
10108       --  because references to outer discriminants will appear out of scope.
10109
10110       if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
10111          Conditional_Delay (T_Sub, Corr_Rec);
10112       else
10113          Set_Is_Frozen (T_Sub);
10114       end if;
10115
10116       if Has_Discriminants (Prot_Subt) then -- False only if errors.
10117          Set_Discriminant_Constraint
10118            (T_Sub, Discriminant_Constraint (Prot_Subt));
10119          Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
10120          Create_Constrained_Components
10121            (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
10122       end if;
10123
10124       Set_Depends_On_Private      (T_Sub, Has_Private_Component (T_Sub));
10125
10126       return T_Sub;
10127    end Constrain_Corresponding_Record;
10128
10129    -----------------------
10130    -- Constrain_Decimal --
10131    -----------------------
10132
10133    procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
10134       T           : constant Entity_Id  := Entity (Subtype_Mark (S));
10135       C           : constant Node_Id    := Constraint (S);
10136       Loc         : constant Source_Ptr := Sloc (C);
10137       Range_Expr  : Node_Id;
10138       Digits_Expr : Node_Id;
10139       Digits_Val  : Uint;
10140       Bound_Val   : Ureal;
10141
10142    begin
10143       Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
10144
10145       if Nkind (C) = N_Range_Constraint then
10146          Range_Expr := Range_Expression (C);
10147          Digits_Val := Digits_Value (T);
10148
10149       else
10150          pragma Assert (Nkind (C) = N_Digits_Constraint);
10151          Digits_Expr := Digits_Expression (C);
10152          Analyze_And_Resolve (Digits_Expr, Any_Integer);
10153
10154          Check_Digits_Expression (Digits_Expr);
10155          Digits_Val := Expr_Value (Digits_Expr);
10156
10157          if Digits_Val > Digits_Value (T) then
10158             Error_Msg_N
10159                ("digits expression is incompatible with subtype", C);
10160             Digits_Val := Digits_Value (T);
10161          end if;
10162
10163          if Present (Range_Constraint (C)) then
10164             Range_Expr := Range_Expression (Range_Constraint (C));
10165          else
10166             Range_Expr := Empty;
10167          end if;
10168       end if;
10169
10170       Set_Etype            (Def_Id, Base_Type        (T));
10171       Set_Size_Info        (Def_Id,                  (T));
10172       Set_First_Rep_Item   (Def_Id, First_Rep_Item   (T));
10173       Set_Delta_Value      (Def_Id, Delta_Value      (T));
10174       Set_Scale_Value      (Def_Id, Scale_Value      (T));
10175       Set_Small_Value      (Def_Id, Small_Value      (T));
10176       Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
10177       Set_Digits_Value     (Def_Id, Digits_Val);
10178
10179       --  Manufacture range from given digits value if no range present
10180
10181       if No (Range_Expr) then
10182          Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
10183          Range_Expr :=
10184            Make_Range (Loc,
10185              Low_Bound =>
10186                Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
10187              High_Bound =>
10188                Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
10189       end if;
10190
10191       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
10192       Set_Discrete_RM_Size (Def_Id);
10193
10194       --  Unconditionally delay the freeze, since we cannot set size
10195       --  information in all cases correctly until the freeze point.
10196
10197       Set_Has_Delayed_Freeze (Def_Id);
10198    end Constrain_Decimal;
10199
10200    ----------------------------------
10201    -- Constrain_Discriminated_Type --
10202    ----------------------------------
10203
10204    procedure Constrain_Discriminated_Type
10205      (Def_Id      : Entity_Id;
10206       S           : Node_Id;
10207       Related_Nod : Node_Id;
10208       For_Access  : Boolean := False)
10209    is
10210       E     : constant Entity_Id := Entity (Subtype_Mark (S));
10211       T     : Entity_Id;
10212       C     : Node_Id;
10213       Elist : Elist_Id := New_Elmt_List;
10214
10215       procedure Fixup_Bad_Constraint;
10216       --  This is called after finding a bad constraint, and after having
10217       --  posted an appropriate error message. The mission is to leave the
10218       --  entity T in as reasonable state as possible!
10219
10220       --------------------------
10221       -- Fixup_Bad_Constraint --
10222       --------------------------
10223
10224       procedure Fixup_Bad_Constraint is
10225       begin
10226          --  Set a reasonable Ekind for the entity. For an incomplete type,
10227          --  we can't do much, but for other types, we can set the proper
10228          --  corresponding subtype kind.
10229
10230          if Ekind (T) = E_Incomplete_Type then
10231             Set_Ekind (Def_Id, Ekind (T));
10232          else
10233             Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10234          end if;
10235
10236          --  Set Etype to the known type, to reduce chances of cascaded errors
10237
10238          Set_Etype (Def_Id, E);
10239          Set_Error_Posted (Def_Id);
10240       end Fixup_Bad_Constraint;
10241
10242    --  Start of processing for Constrain_Discriminated_Type
10243
10244    begin
10245       C := Constraint (S);
10246
10247       --  A discriminant constraint is only allowed in a subtype indication,
10248       --  after a subtype mark. This subtype mark must denote either a type
10249       --  with discriminants, or an access type whose designated type is a
10250       --  type with discriminants. A discriminant constraint specifies the
10251       --  values of these discriminants (RM 3.7.2(5)).
10252
10253       T := Base_Type (Entity (Subtype_Mark (S)));
10254
10255       if Ekind (T) in Access_Kind then
10256          T := Designated_Type (T);
10257       end if;
10258
10259       --  Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
10260       --  Avoid generating an error for access-to-incomplete subtypes.
10261
10262       if Ada_Version >= Ada_05
10263         and then Ekind (T) = E_Incomplete_Type
10264         and then Nkind (Parent (S)) = N_Subtype_Declaration
10265         and then not Is_Itype (Def_Id)
10266       then
10267          --  A little sanity check, emit an error message if the type
10268          --  has discriminants to begin with. Type T may be a regular
10269          --  incomplete type or imported via a limited with clause.
10270
10271          if Has_Discriminants (T)
10272            or else
10273              (From_With_Type (T)
10274                 and then Present (Non_Limited_View (T))
10275                 and then Nkind (Parent (Non_Limited_View (T))) =
10276                            N_Full_Type_Declaration
10277                 and then Present (Discriminant_Specifications
10278                           (Parent (Non_Limited_View (T)))))
10279          then
10280             Error_Msg_N
10281               ("(Ada 2005) incomplete subtype may not be constrained", C);
10282          else
10283             Error_Msg_N
10284               ("invalid constraint: type has no discriminant", C);
10285          end if;
10286
10287          Fixup_Bad_Constraint;
10288          return;
10289
10290       --  Check that the type has visible discriminants. The type may be
10291       --  a private type with unknown discriminants whose full view has
10292       --  discriminants which are invisible.
10293
10294       elsif not Has_Discriminants (T)
10295         or else
10296           (Has_Unknown_Discriminants (T)
10297              and then Is_Private_Type (T))
10298       then
10299          Error_Msg_N ("invalid constraint: type has no discriminant", C);
10300          Fixup_Bad_Constraint;
10301          return;
10302
10303       elsif Is_Constrained (E)
10304         or else (Ekind (E) = E_Class_Wide_Subtype
10305                   and then Present (Discriminant_Constraint (E)))
10306       then
10307          Error_Msg_N ("type is already constrained", Subtype_Mark (S));
10308          Fixup_Bad_Constraint;
10309          return;
10310       end if;
10311
10312       --  T may be an unconstrained subtype (e.g. a generic actual).
10313       --  Constraint applies to the base type.
10314
10315       T := Base_Type (T);
10316
10317       Elist := Build_Discriminant_Constraints (T, S);
10318
10319       --  If the list returned was empty we had an error in building the
10320       --  discriminant constraint. We have also already signalled an error
10321       --  in the incomplete type case
10322
10323       if Is_Empty_Elmt_List (Elist) then
10324          Fixup_Bad_Constraint;
10325          return;
10326       end if;
10327
10328       Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
10329    end Constrain_Discriminated_Type;
10330
10331    ---------------------------
10332    -- Constrain_Enumeration --
10333    ---------------------------
10334
10335    procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
10336       T : constant Entity_Id := Entity (Subtype_Mark (S));
10337       C : constant Node_Id   := Constraint (S);
10338
10339    begin
10340       Set_Ekind (Def_Id, E_Enumeration_Subtype);
10341
10342       Set_First_Literal     (Def_Id, First_Literal (Base_Type (T)));
10343
10344       Set_Etype             (Def_Id, Base_Type         (T));
10345       Set_Size_Info         (Def_Id,                   (T));
10346       Set_First_Rep_Item    (Def_Id, First_Rep_Item    (T));
10347       Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
10348
10349       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10350
10351       Set_Discrete_RM_Size (Def_Id);
10352    end Constrain_Enumeration;
10353
10354    ----------------------
10355    -- Constrain_Float --
10356    ----------------------
10357
10358    procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
10359       T    : constant Entity_Id := Entity (Subtype_Mark (S));
10360       C    : Node_Id;
10361       D    : Node_Id;
10362       Rais : Node_Id;
10363
10364    begin
10365       Set_Ekind (Def_Id, E_Floating_Point_Subtype);
10366
10367       Set_Etype          (Def_Id, Base_Type      (T));
10368       Set_Size_Info      (Def_Id,                (T));
10369       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10370
10371       --  Process the constraint
10372
10373       C := Constraint (S);
10374
10375       --  Digits constraint present
10376
10377       if Nkind (C) = N_Digits_Constraint then
10378          Check_Restriction (No_Obsolescent_Features, C);
10379
10380          if Warn_On_Obsolescent_Feature then
10381             Error_Msg_N
10382               ("subtype digits constraint is an " &
10383                "obsolescent feature (RM J.3(8))?", C);
10384          end if;
10385
10386          D := Digits_Expression (C);
10387          Analyze_And_Resolve (D, Any_Integer);
10388          Check_Digits_Expression (D);
10389          Set_Digits_Value (Def_Id, Expr_Value (D));
10390
10391          --  Check that digits value is in range. Obviously we can do this
10392          --  at compile time, but it is strictly a runtime check, and of
10393          --  course there is an ACVC test that checks this!
10394
10395          if Digits_Value (Def_Id) > Digits_Value (T) then
10396             Error_Msg_Uint_1 := Digits_Value (T);
10397             Error_Msg_N ("?digits value is too large, maximum is ^", D);
10398             Rais :=
10399               Make_Raise_Constraint_Error (Sloc (D),
10400                 Reason => CE_Range_Check_Failed);
10401             Insert_Action (Declaration_Node (Def_Id), Rais);
10402          end if;
10403
10404          C := Range_Constraint (C);
10405
10406       --  No digits constraint present
10407
10408       else
10409          Set_Digits_Value (Def_Id, Digits_Value (T));
10410       end if;
10411
10412       --  Range constraint present
10413
10414       if Nkind (C) = N_Range_Constraint then
10415          Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10416
10417       --  No range constraint present
10418
10419       else
10420          pragma Assert (No (C));
10421          Set_Scalar_Range (Def_Id, Scalar_Range (T));
10422       end if;
10423
10424       Set_Is_Constrained (Def_Id);
10425    end Constrain_Float;
10426
10427    ---------------------
10428    -- Constrain_Index --
10429    ---------------------
10430
10431    procedure Constrain_Index
10432      (Index        : Node_Id;
10433       S            : Node_Id;
10434       Related_Nod  : Node_Id;
10435       Related_Id   : Entity_Id;
10436       Suffix       : Character;
10437       Suffix_Index : Nat)
10438    is
10439       Def_Id : Entity_Id;
10440       R      : Node_Id := Empty;
10441       T      : constant Entity_Id := Etype (Index);
10442
10443    begin
10444       if Nkind (S) = N_Range
10445         or else
10446           (Nkind (S) = N_Attribute_Reference
10447             and then Attribute_Name (S) = Name_Range)
10448       then
10449          --  A Range attribute will transformed into N_Range by Resolve
10450
10451          Analyze (S);
10452          Set_Etype (S, T);
10453          R := S;
10454
10455          Process_Range_Expr_In_Decl (R, T, Empty_List);
10456
10457          if not Error_Posted (S)
10458            and then
10459              (Nkind (S) /= N_Range
10460                or else not Covers (T, (Etype (Low_Bound (S))))
10461                or else not Covers (T, (Etype (High_Bound (S)))))
10462          then
10463             if Base_Type (T) /= Any_Type
10464               and then Etype (Low_Bound (S)) /= Any_Type
10465               and then Etype (High_Bound (S)) /= Any_Type
10466             then
10467                Error_Msg_N ("range expected", S);
10468             end if;
10469          end if;
10470
10471       elsif Nkind (S) = N_Subtype_Indication then
10472
10473          --  The parser has verified that this is a discrete indication
10474
10475          Resolve_Discrete_Subtype_Indication (S, T);
10476          R := Range_Expression (Constraint (S));
10477
10478       elsif Nkind (S) = N_Discriminant_Association then
10479
10480          --  Syntactically valid in subtype indication
10481
10482          Error_Msg_N ("invalid index constraint", S);
10483          Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
10484          return;
10485
10486       --  Subtype_Mark case, no anonymous subtypes to construct
10487
10488       else
10489          Analyze (S);
10490
10491          if Is_Entity_Name (S) then
10492             if not Is_Type (Entity (S)) then
10493                Error_Msg_N ("expect subtype mark for index constraint", S);
10494
10495             elsif Base_Type (Entity (S)) /= Base_Type (T) then
10496                Wrong_Type (S, Base_Type (T));
10497             end if;
10498
10499             return;
10500
10501          else
10502             Error_Msg_N ("invalid index constraint", S);
10503             Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
10504             return;
10505          end if;
10506       end if;
10507
10508       Def_Id :=
10509         Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
10510
10511       Set_Etype (Def_Id, Base_Type (T));
10512
10513       if Is_Modular_Integer_Type (T) then
10514          Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
10515
10516       elsif Is_Integer_Type (T) then
10517          Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
10518
10519       else
10520          Set_Ekind (Def_Id, E_Enumeration_Subtype);
10521          Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
10522       end if;
10523
10524       Set_Size_Info      (Def_Id,                (T));
10525       Set_RM_Size        (Def_Id, RM_Size        (T));
10526       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10527
10528       Set_Scalar_Range   (Def_Id, R);
10529
10530       Set_Etype (S, Def_Id);
10531       Set_Discrete_RM_Size (Def_Id);
10532    end Constrain_Index;
10533
10534    -----------------------
10535    -- Constrain_Integer --
10536    -----------------------
10537
10538    procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
10539       T : constant Entity_Id := Entity (Subtype_Mark (S));
10540       C : constant Node_Id   := Constraint (S);
10541
10542    begin
10543       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10544
10545       if Is_Modular_Integer_Type (T) then
10546          Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
10547       else
10548          Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
10549       end if;
10550
10551       Set_Etype            (Def_Id, Base_Type        (T));
10552       Set_Size_Info        (Def_Id,                  (T));
10553       Set_First_Rep_Item   (Def_Id, First_Rep_Item   (T));
10554       Set_Discrete_RM_Size (Def_Id);
10555    end Constrain_Integer;
10556
10557    ------------------------------
10558    -- Constrain_Ordinary_Fixed --
10559    ------------------------------
10560
10561    procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
10562       T    : constant Entity_Id := Entity (Subtype_Mark (S));
10563       C    : Node_Id;
10564       D    : Node_Id;
10565       Rais : Node_Id;
10566
10567    begin
10568       Set_Ekind          (Def_Id, E_Ordinary_Fixed_Point_Subtype);
10569       Set_Etype          (Def_Id, Base_Type        (T));
10570       Set_Size_Info      (Def_Id,                  (T));
10571       Set_First_Rep_Item (Def_Id, First_Rep_Item   (T));
10572       Set_Small_Value    (Def_Id, Small_Value      (T));
10573
10574       --  Process the constraint
10575
10576       C := Constraint (S);
10577
10578       --  Delta constraint present
10579
10580       if Nkind (C) = N_Delta_Constraint then
10581          Check_Restriction (No_Obsolescent_Features, C);
10582
10583          if Warn_On_Obsolescent_Feature then
10584             Error_Msg_S
10585               ("subtype delta constraint is an " &
10586                "obsolescent feature (RM J.3(7))?");
10587          end if;
10588
10589          D := Delta_Expression (C);
10590          Analyze_And_Resolve (D, Any_Real);
10591          Check_Delta_Expression (D);
10592          Set_Delta_Value (Def_Id, Expr_Value_R (D));
10593
10594          --  Check that delta value is in range. Obviously we can do this
10595          --  at compile time, but it is strictly a runtime check, and of
10596          --  course there is an ACVC test that checks this!
10597
10598          if Delta_Value (Def_Id) < Delta_Value (T) then
10599             Error_Msg_N ("?delta value is too small", D);
10600             Rais :=
10601               Make_Raise_Constraint_Error (Sloc (D),
10602                 Reason => CE_Range_Check_Failed);
10603             Insert_Action (Declaration_Node (Def_Id), Rais);
10604          end if;
10605
10606          C := Range_Constraint (C);
10607
10608       --  No delta constraint present
10609
10610       else
10611          Set_Delta_Value (Def_Id, Delta_Value (T));
10612       end if;
10613
10614       --  Range constraint present
10615
10616       if Nkind (C) = N_Range_Constraint then
10617          Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10618
10619       --  No range constraint present
10620
10621       else
10622          pragma Assert (No (C));
10623          Set_Scalar_Range (Def_Id, Scalar_Range (T));
10624
10625       end if;
10626
10627       Set_Discrete_RM_Size (Def_Id);
10628
10629       --  Unconditionally delay the freeze, since we cannot set size
10630       --  information in all cases correctly until the freeze point.
10631
10632       Set_Has_Delayed_Freeze (Def_Id);
10633    end Constrain_Ordinary_Fixed;
10634
10635    -----------------------
10636    -- Contain_Interface --
10637    -----------------------
10638
10639    function Contain_Interface
10640      (Iface  : Entity_Id;
10641       Ifaces : Elist_Id) return Boolean
10642    is
10643       Iface_Elmt : Elmt_Id;
10644
10645    begin
10646       if Present (Ifaces) then
10647          Iface_Elmt := First_Elmt (Ifaces);
10648          while Present (Iface_Elmt) loop
10649             if Node (Iface_Elmt) = Iface then
10650                return True;
10651             end if;
10652
10653             Next_Elmt (Iface_Elmt);
10654          end loop;
10655       end if;
10656
10657       return False;
10658    end Contain_Interface;
10659
10660    ---------------------------
10661    -- Convert_Scalar_Bounds --
10662    ---------------------------
10663
10664    procedure Convert_Scalar_Bounds
10665      (N            : Node_Id;
10666       Parent_Type  : Entity_Id;
10667       Derived_Type : Entity_Id;
10668       Loc          : Source_Ptr)
10669    is
10670       Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
10671
10672       Lo  : Node_Id;
10673       Hi  : Node_Id;
10674       Rng : Node_Id;
10675
10676    begin
10677       Lo := Build_Scalar_Bound
10678               (Type_Low_Bound (Derived_Type),
10679                Parent_Type, Implicit_Base);
10680
10681       Hi := Build_Scalar_Bound
10682               (Type_High_Bound (Derived_Type),
10683                Parent_Type, Implicit_Base);
10684
10685       Rng :=
10686         Make_Range (Loc,
10687           Low_Bound  => Lo,
10688           High_Bound => Hi);
10689
10690       Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
10691
10692       Set_Parent (Rng, N);
10693       Set_Scalar_Range (Derived_Type, Rng);
10694
10695       --  Analyze the bounds
10696
10697       Analyze_And_Resolve (Lo, Implicit_Base);
10698       Analyze_And_Resolve (Hi, Implicit_Base);
10699
10700       --  Analyze the range itself, except that we do not analyze it if
10701       --  the bounds are real literals, and we have a fixed-point type.
10702       --  The reason for this is that we delay setting the bounds in this
10703       --  case till we know the final Small and Size values (see circuit
10704       --  in Freeze.Freeze_Fixed_Point_Type for further details).
10705
10706       if Is_Fixed_Point_Type (Parent_Type)
10707         and then Nkind (Lo) = N_Real_Literal
10708         and then Nkind (Hi) = N_Real_Literal
10709       then
10710          return;
10711
10712       --  Here we do the analysis of the range
10713
10714       --  Note: we do this manually, since if we do a normal Analyze and
10715       --  Resolve call, there are problems with the conversions used for
10716       --  the derived type range.
10717
10718       else
10719          Set_Etype    (Rng, Implicit_Base);
10720          Set_Analyzed (Rng, True);
10721       end if;
10722    end Convert_Scalar_Bounds;
10723
10724    -------------------
10725    -- Copy_And_Swap --
10726    -------------------
10727
10728    procedure Copy_And_Swap (Priv, Full : Entity_Id) is
10729    begin
10730       --  Initialize new full declaration entity by copying the pertinent
10731       --  fields of the corresponding private declaration entity.
10732
10733       --  We temporarily set Ekind to a value appropriate for a type to
10734       --  avoid assert failures in Einfo from checking for setting type
10735       --  attributes on something that is not a type. Ekind (Priv) is an
10736       --  appropriate choice, since it allowed the attributes to be set
10737       --  in the first place. This Ekind value will be modified later.
10738
10739       Set_Ekind (Full, Ekind (Priv));
10740
10741       --  Also set Etype temporarily to Any_Type, again, in the absence
10742       --  of errors, it will be properly reset, and if there are errors,
10743       --  then we want a value of Any_Type to remain.
10744
10745       Set_Etype (Full, Any_Type);
10746
10747       --  Now start copying attributes
10748
10749       Set_Has_Discriminants          (Full, Has_Discriminants       (Priv));
10750
10751       if Has_Discriminants (Full) then
10752          Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
10753          Set_Stored_Constraint       (Full, Stored_Constraint       (Priv));
10754       end if;
10755
10756       Set_First_Rep_Item             (Full, First_Rep_Item          (Priv));
10757       Set_Homonym                    (Full, Homonym                 (Priv));
10758       Set_Is_Immediately_Visible     (Full, Is_Immediately_Visible  (Priv));
10759       Set_Is_Public                  (Full, Is_Public               (Priv));
10760       Set_Is_Pure                    (Full, Is_Pure                 (Priv));
10761       Set_Is_Tagged_Type             (Full, Is_Tagged_Type          (Priv));
10762       Set_Has_Pragma_Unreferenced    (Full, Has_Pragma_Unreferenced (Priv));
10763       Set_Has_Pragma_Unreferenced_Objects
10764                                      (Full, Has_Pragma_Unreferenced_Objects
10765                                                                     (Priv));
10766
10767       Conditional_Delay              (Full,                          Priv);
10768
10769       if Is_Tagged_Type (Full) then
10770          Set_Primitive_Operations    (Full, Primitive_Operations    (Priv));
10771
10772          if Priv = Base_Type (Priv) then
10773             Set_Class_Wide_Type      (Full, Class_Wide_Type         (Priv));
10774          end if;
10775       end if;
10776
10777       Set_Is_Volatile                (Full, Is_Volatile             (Priv));
10778       Set_Treat_As_Volatile          (Full, Treat_As_Volatile       (Priv));
10779       Set_Scope                      (Full, Scope                   (Priv));
10780       Set_Next_Entity                (Full, Next_Entity             (Priv));
10781       Set_First_Entity               (Full, First_Entity            (Priv));
10782       Set_Last_Entity                (Full, Last_Entity             (Priv));
10783
10784       --  If access types have been recorded for later handling, keep them in
10785       --  the full view so that they get handled when the full view freeze
10786       --  node is expanded.
10787
10788       if Present (Freeze_Node (Priv))
10789         and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
10790       then
10791          Ensure_Freeze_Node (Full);
10792          Set_Access_Types_To_Process
10793            (Freeze_Node (Full),
10794             Access_Types_To_Process (Freeze_Node (Priv)));
10795       end if;
10796
10797       --  Swap the two entities. Now Privat is the full type entity and
10798       --  Full is the private one. They will be swapped back at the end
10799       --  of the private part. This swapping ensures that the entity that
10800       --  is visible in the private part is the full declaration.
10801
10802       Exchange_Entities (Priv, Full);
10803       Append_Entity (Full, Scope (Full));
10804    end Copy_And_Swap;
10805
10806    -------------------------------------
10807    -- Copy_Array_Base_Type_Attributes --
10808    -------------------------------------
10809
10810    procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
10811    begin
10812       Set_Component_Alignment      (T1, Component_Alignment      (T2));
10813       Set_Component_Type           (T1, Component_Type           (T2));
10814       Set_Component_Size           (T1, Component_Size           (T2));
10815       Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
10816       Set_Finalize_Storage_Only    (T1, Finalize_Storage_Only    (T2));
10817       Set_Has_Non_Standard_Rep     (T1, Has_Non_Standard_Rep     (T2));
10818       Set_Has_Task                 (T1, Has_Task                 (T2));
10819       Set_Is_Packed                (T1, Is_Packed                (T2));
10820       Set_Has_Aliased_Components   (T1, Has_Aliased_Components   (T2));
10821       Set_Has_Atomic_Components    (T1, Has_Atomic_Components    (T2));
10822       Set_Has_Volatile_Components  (T1, Has_Volatile_Components  (T2));
10823    end Copy_Array_Base_Type_Attributes;
10824
10825    -----------------------------------
10826    -- Copy_Array_Subtype_Attributes --
10827    -----------------------------------
10828
10829    procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
10830    begin
10831       Set_Size_Info (T1, T2);
10832
10833       Set_First_Index          (T1, First_Index           (T2));
10834       Set_Is_Aliased           (T1, Is_Aliased            (T2));
10835       Set_Is_Atomic            (T1, Is_Atomic             (T2));
10836       Set_Is_Volatile          (T1, Is_Volatile           (T2));
10837       Set_Treat_As_Volatile    (T1, Treat_As_Volatile     (T2));
10838       Set_Is_Constrained       (T1, Is_Constrained        (T2));
10839       Set_Depends_On_Private   (T1, Has_Private_Component (T2));
10840       Set_First_Rep_Item       (T1, First_Rep_Item        (T2));
10841       Set_Convention           (T1, Convention            (T2));
10842       Set_Is_Limited_Composite (T1, Is_Limited_Composite  (T2));
10843       Set_Is_Private_Composite (T1, Is_Private_Composite  (T2));
10844    end Copy_Array_Subtype_Attributes;
10845
10846    -----------------------------------
10847    -- Create_Constrained_Components --
10848    -----------------------------------
10849
10850    procedure Create_Constrained_Components
10851      (Subt        : Entity_Id;
10852       Decl_Node   : Node_Id;
10853       Typ         : Entity_Id;
10854       Constraints : Elist_Id)
10855    is
10856       Loc         : constant Source_Ptr := Sloc (Subt);
10857       Comp_List   : constant Elist_Id   := New_Elmt_List;
10858       Parent_Type : constant Entity_Id  := Etype (Typ);
10859       Assoc_List  : constant List_Id    := New_List;
10860       Discr_Val   : Elmt_Id;
10861       Errors      : Boolean;
10862       New_C       : Entity_Id;
10863       Old_C       : Entity_Id;
10864       Is_Static   : Boolean := True;
10865
10866       procedure Collect_Fixed_Components (Typ : Entity_Id);
10867       --  Collect parent type components that do not appear in a variant part
10868
10869       procedure Create_All_Components;
10870       --  Iterate over Comp_List to create the components of the subtype
10871
10872       function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
10873       --  Creates a new component from Old_Compon, copying all the fields from
10874       --  it, including its Etype, inserts the new component in the Subt entity
10875       --  chain and returns the new component.
10876
10877       function Is_Variant_Record (T : Entity_Id) return Boolean;
10878       --  If true, and discriminants are static, collect only components from
10879       --  variants selected by discriminant values.
10880
10881       ------------------------------
10882       -- Collect_Fixed_Components --
10883       ------------------------------
10884
10885       procedure Collect_Fixed_Components (Typ : Entity_Id) is
10886       begin
10887       --  Build association list for discriminants, and find components of the
10888       --  variant part selected by the values of the discriminants.
10889
10890          Old_C := First_Discriminant (Typ);
10891          Discr_Val := First_Elmt (Constraints);
10892          while Present (Old_C) loop
10893             Append_To (Assoc_List,
10894               Make_Component_Association (Loc,
10895                  Choices    => New_List (New_Occurrence_Of (Old_C, Loc)),
10896                  Expression => New_Copy (Node (Discr_Val))));
10897
10898             Next_Elmt (Discr_Val);
10899             Next_Discriminant (Old_C);
10900          end loop;
10901
10902          --  The tag, and the possible parent and controller components
10903          --  are unconditionally in the subtype.
10904
10905          if Is_Tagged_Type (Typ)
10906            or else Has_Controlled_Component (Typ)
10907          then
10908             Old_C := First_Component (Typ);
10909             while Present (Old_C) loop
10910                if Chars ((Old_C)) = Name_uTag
10911                  or else Chars ((Old_C)) = Name_uParent
10912                  or else Chars ((Old_C)) = Name_uController
10913                then
10914                   Append_Elmt (Old_C, Comp_List);
10915                end if;
10916
10917                Next_Component (Old_C);
10918             end loop;
10919          end if;
10920       end Collect_Fixed_Components;
10921
10922       ---------------------------
10923       -- Create_All_Components --
10924       ---------------------------
10925
10926       procedure Create_All_Components is
10927          Comp : Elmt_Id;
10928
10929       begin
10930          Comp := First_Elmt (Comp_List);
10931          while Present (Comp) loop
10932             Old_C := Node (Comp);
10933             New_C := Create_Component (Old_C);
10934
10935             Set_Etype
10936               (New_C,
10937                Constrain_Component_Type
10938                  (Old_C, Subt, Decl_Node, Typ, Constraints));
10939             Set_Is_Public (New_C, Is_Public (Subt));
10940
10941             Next_Elmt (Comp);
10942          end loop;
10943       end Create_All_Components;
10944
10945       ----------------------
10946       -- Create_Component --
10947       ----------------------
10948
10949       function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
10950          New_Compon : constant Entity_Id := New_Copy (Old_Compon);
10951
10952       begin
10953          if Ekind (Old_Compon) = E_Discriminant
10954            and then Is_Completely_Hidden (Old_Compon)
10955          then
10956             --  This is a shadow discriminant created for a discriminant of
10957             --  the parent type that is one of several renamed by the same
10958             --  new discriminant. Give the shadow discriminant an internal
10959             --  name that cannot conflict with that of visible components.
10960
10961             Set_Chars (New_Compon, New_Internal_Name ('C'));
10962          end if;
10963
10964          --  Set the parent so we have a proper link for freezing etc. This is
10965          --  not a real parent pointer, since of course our parent does not own
10966          --  up to us and reference us, we are an illegitimate child of the
10967          --  original parent!
10968
10969          Set_Parent (New_Compon, Parent (Old_Compon));
10970
10971          --  If the old component's Esize was already determined and is a
10972          --  static value, then the new component simply inherits it. Otherwise
10973          --  the old component's size may require run-time determination, but
10974          --  the new component's size still might be statically determinable
10975          --  (if, for example it has a static constraint). In that case we want
10976          --  Layout_Type to recompute the component's size, so we reset its
10977          --  size and positional fields.
10978
10979          if Frontend_Layout_On_Target
10980            and then not Known_Static_Esize (Old_Compon)
10981          then
10982             Set_Esize (New_Compon, Uint_0);
10983             Init_Normalized_First_Bit    (New_Compon);
10984             Init_Normalized_Position     (New_Compon);
10985             Init_Normalized_Position_Max (New_Compon);
10986          end if;
10987
10988          --  We do not want this node marked as Comes_From_Source, since
10989          --  otherwise it would get first class status and a separate cross-
10990          --  reference line would be generated. Illegitimate children do not
10991          --  rate such recognition.
10992
10993          Set_Comes_From_Source (New_Compon, False);
10994
10995          --  But it is a real entity, and a birth certificate must be properly
10996          --  registered by entering it into the entity list.
10997
10998          Enter_Name (New_Compon);
10999
11000          return New_Compon;
11001       end Create_Component;
11002
11003       -----------------------
11004       -- Is_Variant_Record --
11005       -----------------------
11006
11007       function Is_Variant_Record (T : Entity_Id) return Boolean is
11008       begin
11009          return Nkind (Parent (T)) = N_Full_Type_Declaration
11010            and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
11011            and then Present (Component_List (Type_Definition (Parent (T))))
11012            and then
11013              Present
11014                (Variant_Part (Component_List (Type_Definition (Parent (T)))));
11015       end Is_Variant_Record;
11016
11017    --  Start of processing for Create_Constrained_Components
11018
11019    begin
11020       pragma Assert (Subt /= Base_Type (Subt));
11021       pragma Assert (Typ = Base_Type (Typ));
11022
11023       Set_First_Entity (Subt, Empty);
11024       Set_Last_Entity  (Subt, Empty);
11025
11026       --  Check whether constraint is fully static, in which case we can
11027       --  optimize the list of components.
11028
11029       Discr_Val := First_Elmt (Constraints);
11030       while Present (Discr_Val) loop
11031          if not Is_OK_Static_Expression (Node (Discr_Val)) then
11032             Is_Static := False;
11033             exit;
11034          end if;
11035
11036          Next_Elmt (Discr_Val);
11037       end loop;
11038
11039       Set_Has_Static_Discriminants (Subt, Is_Static);
11040
11041       Push_Scope (Subt);
11042
11043       --  Inherit the discriminants of the parent type
11044
11045       Add_Discriminants : declare
11046          Num_Disc : Int;
11047          Num_Gird : Int;
11048
11049       begin
11050          Num_Disc := 0;
11051          Old_C := First_Discriminant (Typ);
11052
11053          while Present (Old_C) loop
11054             Num_Disc := Num_Disc + 1;
11055             New_C := Create_Component (Old_C);
11056             Set_Is_Public (New_C, Is_Public (Subt));
11057             Next_Discriminant (Old_C);
11058          end loop;
11059
11060          --  For an untagged derived subtype, the number of discriminants may
11061          --  be smaller than the number of inherited discriminants, because
11062          --  several of them may be renamed by a single new discriminant.
11063          --  In this case, add the hidden discriminants back into the subtype,
11064          --  because otherwise the size of the subtype is computed incorrectly
11065          --  in GCC 4.1.
11066
11067          Num_Gird := 0;
11068
11069          if Is_Derived_Type (Typ)
11070            and then not Is_Tagged_Type (Typ)
11071          then
11072             Old_C := First_Stored_Discriminant (Typ);
11073
11074             while Present (Old_C) loop
11075                Num_Gird := Num_Gird + 1;
11076                Next_Stored_Discriminant (Old_C);
11077             end loop;
11078          end if;
11079
11080          if Num_Gird > Num_Disc then
11081
11082             --  Find out multiple uses of new discriminants, and add hidden
11083             --  components for the extra renamed discriminants. We recognize
11084             --  multiple uses through the Corresponding_Discriminant of a
11085             --  new discriminant: if it constrains several old discriminants,
11086             --  this field points to the last one in the parent type. The
11087             --  stored discriminants of the derived type have the same name
11088             --  as those of the parent.
11089
11090             declare
11091                Constr    : Elmt_Id;
11092                New_Discr : Entity_Id;
11093                Old_Discr : Entity_Id;
11094
11095             begin
11096                Constr    := First_Elmt (Stored_Constraint (Typ));
11097                Old_Discr := First_Stored_Discriminant (Typ);
11098                while Present (Constr) loop
11099                   if Is_Entity_Name (Node (Constr))
11100                     and then Ekind (Entity (Node (Constr))) = E_Discriminant
11101                   then
11102                      New_Discr := Entity (Node (Constr));
11103
11104                      if Chars (Corresponding_Discriminant (New_Discr)) /=
11105                         Chars (Old_Discr)
11106                      then
11107                         --  The new discriminant has been used to rename a
11108                         --  subsequent old discriminant. Introduce a shadow
11109                         --  component for the current old discriminant.
11110
11111                         New_C := Create_Component (Old_Discr);
11112                         Set_Original_Record_Component  (New_C, Old_Discr);
11113                      end if;
11114                   end if;
11115
11116                   Next_Elmt (Constr);
11117                   Next_Stored_Discriminant (Old_Discr);
11118                end loop;
11119             end;
11120          end if;
11121       end Add_Discriminants;
11122
11123       if Is_Static
11124         and then Is_Variant_Record (Typ)
11125       then
11126          Collect_Fixed_Components (Typ);
11127
11128          Gather_Components (
11129            Typ,
11130            Component_List (Type_Definition (Parent (Typ))),
11131            Governed_By   => Assoc_List,
11132            Into          => Comp_List,
11133            Report_Errors => Errors);
11134          pragma Assert (not Errors);
11135
11136          Create_All_Components;
11137
11138       --  If the subtype declaration is created for a tagged type derivation
11139       --  with constraints, we retrieve the record definition of the parent
11140       --  type to select the components of the proper variant.
11141
11142       elsif Is_Static
11143         and then Is_Tagged_Type (Typ)
11144         and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
11145         and then
11146           Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
11147         and then Is_Variant_Record (Parent_Type)
11148       then
11149          Collect_Fixed_Components (Typ);
11150
11151          Gather_Components (
11152            Typ,
11153            Component_List (Type_Definition (Parent (Parent_Type))),
11154            Governed_By   => Assoc_List,
11155            Into          => Comp_List,
11156            Report_Errors => Errors);
11157          pragma Assert (not Errors);
11158
11159          --  If the tagged derivation has a type extension, collect all the
11160          --  new components therein.
11161
11162          if Present
11163               (Record_Extension_Part (Type_Definition (Parent (Typ))))
11164          then
11165             Old_C := First_Component (Typ);
11166             while Present (Old_C) loop
11167                if Original_Record_Component (Old_C) = Old_C
11168                 and then Chars (Old_C) /= Name_uTag
11169                 and then Chars (Old_C) /= Name_uParent
11170                 and then Chars (Old_C) /= Name_uController
11171                then
11172                   Append_Elmt (Old_C, Comp_List);
11173                end if;
11174
11175                Next_Component (Old_C);
11176             end loop;
11177          end if;
11178
11179          Create_All_Components;
11180
11181       else
11182          --  If discriminants are not static, or if this is a multi-level type
11183          --  extension, we have to include all components of the parent type.
11184
11185          Old_C := First_Component (Typ);
11186          while Present (Old_C) loop
11187             New_C := Create_Component (Old_C);
11188
11189             Set_Etype
11190               (New_C,
11191                Constrain_Component_Type
11192                  (Old_C, Subt, Decl_Node, Typ, Constraints));
11193             Set_Is_Public (New_C, Is_Public (Subt));
11194
11195             Next_Component (Old_C);
11196          end loop;
11197       end if;
11198
11199       End_Scope;
11200    end Create_Constrained_Components;
11201
11202    ------------------------------------------
11203    -- Decimal_Fixed_Point_Type_Declaration --
11204    ------------------------------------------
11205
11206    procedure Decimal_Fixed_Point_Type_Declaration
11207      (T   : Entity_Id;
11208       Def : Node_Id)
11209    is
11210       Loc           : constant Source_Ptr := Sloc (Def);
11211       Digs_Expr     : constant Node_Id    := Digits_Expression (Def);
11212       Delta_Expr    : constant Node_Id    := Delta_Expression (Def);
11213       Implicit_Base : Entity_Id;
11214       Digs_Val      : Uint;
11215       Delta_Val     : Ureal;
11216       Scale_Val     : Uint;
11217       Bound_Val     : Ureal;
11218
11219    begin
11220       Check_Restriction (No_Fixed_Point, Def);
11221
11222       --  Create implicit base type
11223
11224       Implicit_Base :=
11225         Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
11226       Set_Etype (Implicit_Base, Implicit_Base);
11227
11228       --  Analyze and process delta expression
11229
11230       Analyze_And_Resolve (Delta_Expr, Universal_Real);
11231
11232       Check_Delta_Expression (Delta_Expr);
11233       Delta_Val := Expr_Value_R (Delta_Expr);
11234
11235       --  Check delta is power of 10, and determine scale value from it
11236
11237       declare
11238          Val : Ureal;
11239
11240       begin
11241          Scale_Val := Uint_0;
11242          Val := Delta_Val;
11243
11244          if Val < Ureal_1 then
11245             while Val < Ureal_1 loop
11246                Val := Val * Ureal_10;
11247                Scale_Val := Scale_Val + 1;
11248             end loop;
11249
11250             if Scale_Val > 18 then
11251                Error_Msg_N ("scale exceeds maximum value of 18", Def);
11252                Scale_Val := UI_From_Int (+18);
11253             end if;
11254
11255          else
11256             while Val > Ureal_1 loop
11257                Val := Val / Ureal_10;
11258                Scale_Val := Scale_Val - 1;
11259             end loop;
11260
11261             if Scale_Val < -18 then
11262                Error_Msg_N ("scale is less than minimum value of -18", Def);
11263                Scale_Val := UI_From_Int (-18);
11264             end if;
11265          end if;
11266
11267          if Val /= Ureal_1 then
11268             Error_Msg_N ("delta expression must be a power of 10", Def);
11269             Delta_Val := Ureal_10 ** (-Scale_Val);
11270          end if;
11271       end;
11272
11273       --  Set delta, scale and small (small = delta for decimal type)
11274
11275       Set_Delta_Value (Implicit_Base, Delta_Val);
11276       Set_Scale_Value (Implicit_Base, Scale_Val);
11277       Set_Small_Value (Implicit_Base, Delta_Val);
11278
11279       --  Analyze and process digits expression
11280
11281       Analyze_And_Resolve (Digs_Expr, Any_Integer);
11282       Check_Digits_Expression (Digs_Expr);
11283       Digs_Val := Expr_Value (Digs_Expr);
11284
11285       if Digs_Val > 18 then
11286          Digs_Val := UI_From_Int (+18);
11287          Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
11288       end if;
11289
11290       Set_Digits_Value (Implicit_Base, Digs_Val);
11291       Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
11292
11293       --  Set range of base type from digits value for now. This will be
11294       --  expanded to represent the true underlying base range by Freeze.
11295
11296       Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
11297
11298       --  Note: We leave size as zero for now, size will be set at freeze
11299       --  time. We have to do this for ordinary fixed-point, because the size
11300       --  depends on the specified small, and we might as well do the same for
11301       --  decimal fixed-point.
11302
11303       pragma Assert (Esize (Implicit_Base) = Uint_0);
11304
11305       --  If there are bounds given in the declaration use them as the
11306       --  bounds of the first named subtype.
11307
11308       if Present (Real_Range_Specification (Def)) then
11309          declare
11310             RRS      : constant Node_Id := Real_Range_Specification (Def);
11311             Low      : constant Node_Id := Low_Bound (RRS);
11312             High     : constant Node_Id := High_Bound (RRS);
11313             Low_Val  : Ureal;
11314             High_Val : Ureal;
11315
11316          begin
11317             Analyze_And_Resolve (Low, Any_Real);
11318             Analyze_And_Resolve (High, Any_Real);
11319             Check_Real_Bound (Low);
11320             Check_Real_Bound (High);
11321             Low_Val := Expr_Value_R (Low);
11322             High_Val := Expr_Value_R (High);
11323
11324             if Low_Val < (-Bound_Val) then
11325                Error_Msg_N
11326                  ("range low bound too small for digits value", Low);
11327                Low_Val := -Bound_Val;
11328             end if;
11329
11330             if High_Val > Bound_Val then
11331                Error_Msg_N
11332                  ("range high bound too large for digits value", High);
11333                High_Val := Bound_Val;
11334             end if;
11335
11336             Set_Fixed_Range (T, Loc, Low_Val, High_Val);
11337          end;
11338
11339       --  If no explicit range, use range that corresponds to given
11340       --  digits value. This will end up as the final range for the
11341       --  first subtype.
11342
11343       else
11344          Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
11345       end if;
11346
11347       --  Complete entity for first subtype
11348
11349       Set_Ekind          (T, E_Decimal_Fixed_Point_Subtype);
11350       Set_Etype          (T, Implicit_Base);
11351       Set_Size_Info      (T, Implicit_Base);
11352       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
11353       Set_Digits_Value   (T, Digs_Val);
11354       Set_Delta_Value    (T, Delta_Val);
11355       Set_Small_Value    (T, Delta_Val);
11356       Set_Scale_Value    (T, Scale_Val);
11357       Set_Is_Constrained (T);
11358    end Decimal_Fixed_Point_Type_Declaration;
11359
11360    -----------------------------------
11361    -- Derive_Progenitor_Subprograms --
11362    -----------------------------------
11363
11364    procedure Derive_Progenitor_Subprograms
11365      (Parent_Type : Entity_Id;
11366       Tagged_Type : Entity_Id)
11367    is
11368       E          : Entity_Id;
11369       Elmt       : Elmt_Id;
11370       Iface      : Entity_Id;
11371       Iface_Elmt : Elmt_Id;
11372       Iface_Subp : Entity_Id;
11373       New_Subp   : Entity_Id := Empty;
11374       Prim_Elmt  : Elmt_Id;
11375       Subp       : Entity_Id;
11376       Typ        : Entity_Id;
11377
11378    begin
11379       pragma Assert (Ada_Version >= Ada_05
11380         and then Is_Record_Type (Tagged_Type)
11381         and then Is_Tagged_Type (Tagged_Type)
11382         and then Has_Interfaces (Tagged_Type));
11383
11384       --  Step 1: Transfer to the full-view primitives associated with the
11385       --  partial-view that cover interface primitives. Conceptually this
11386       --  work should be done later by Process_Full_View; done here to
11387       --  simplify its implementation at later stages. It can be safely
11388       --  done here because interfaces must be visible in the partial and
11389       --  private view (RM 7.3(7.3/2)).
11390
11391       --  Small optimization: This work is only required if the parent is
11392       --  abstract. If the tagged type is not abstract, it cannot have
11393       --  abstract primitives (the only entities in the list of primitives of
11394       --  non-abstract tagged types that can reference abstract primitives
11395       --  through its Alias attribute are the internal entities that have
11396       --  attribute Interface_Alias, and these entities are generated later
11397       --  by Freeze_Record_Type).
11398
11399       if In_Private_Part (Current_Scope)
11400         and then Is_Abstract_Type (Parent_Type)
11401       then
11402          Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
11403          while Present (Elmt) loop
11404             Subp := Node (Elmt);
11405
11406             --  At this stage it is not possible to have entities in the list
11407             --  of primitives that have attribute Interface_Alias
11408
11409             pragma Assert (No (Interface_Alias (Subp)));
11410
11411             Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
11412
11413             if Is_Interface (Typ) then
11414                E := Find_Primitive_Covering_Interface
11415                       (Tagged_Type => Tagged_Type,
11416                        Iface_Prim  => Subp);
11417
11418                if Present (E)
11419                  and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
11420                then
11421                   Replace_Elmt (Elmt, E);
11422                   Remove_Homonym (Subp);
11423                end if;
11424             end if;
11425
11426             Next_Elmt (Elmt);
11427          end loop;
11428       end if;
11429
11430       --  Step 2: Add primitives of progenitors that are not implemented by
11431       --  parents of Tagged_Type
11432
11433       if Present (Interfaces (Tagged_Type)) then
11434          Iface_Elmt := First_Elmt (Interfaces (Tagged_Type));
11435          while Present (Iface_Elmt) loop
11436             Iface := Node (Iface_Elmt);
11437
11438             Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
11439             while Present (Prim_Elmt) loop
11440                Iface_Subp := Node (Prim_Elmt);
11441
11442                --  Exclude derivation of predefined primitives except those
11443                --  that come from source. Required to catch declarations of
11444                --  equality operators of interfaces. For example:
11445
11446                --     type Iface is interface;
11447                --     function "=" (Left, Right : Iface) return Boolean;
11448
11449                if not Is_Predefined_Dispatching_Operation (Iface_Subp)
11450                  or else Comes_From_Source (Iface_Subp)
11451                then
11452                   E := Find_Primitive_Covering_Interface
11453                          (Tagged_Type => Tagged_Type,
11454                           Iface_Prim  => Iface_Subp);
11455
11456                   --  If not found we derive a new primitive leaving its alias
11457                   --  attribute referencing the interface primitive
11458
11459                   if No (E) then
11460                      Derive_Subprogram
11461                        (New_Subp, Iface_Subp, Tagged_Type, Iface);
11462
11463                   --  Propagate to the full view interface entities associated
11464                   --  with the partial view
11465
11466                   elsif In_Private_Part (Current_Scope)
11467                     and then Present (Alias (E))
11468                     and then Alias (E) = Iface_Subp
11469                     and then
11470                       List_Containing (Parent (E)) /=
11471                         Private_Declarations
11472                           (Specification
11473                             (Unit_Declaration_Node (Current_Scope)))
11474                   then
11475                      Append_Elmt (E, Primitive_Operations (Tagged_Type));
11476                   end if;
11477                end if;
11478
11479                Next_Elmt (Prim_Elmt);
11480             end loop;
11481
11482             Next_Elmt (Iface_Elmt);
11483          end loop;
11484       end if;
11485    end Derive_Progenitor_Subprograms;
11486
11487    -----------------------
11488    -- Derive_Subprogram --
11489    -----------------------
11490
11491    procedure Derive_Subprogram
11492      (New_Subp     : in out Entity_Id;
11493       Parent_Subp  : Entity_Id;
11494       Derived_Type : Entity_Id;
11495       Parent_Type  : Entity_Id;
11496       Actual_Subp  : Entity_Id := Empty)
11497    is
11498       Formal : Entity_Id;
11499       --  Formal parameter of parent primitive operation
11500
11501       Formal_Of_Actual : Entity_Id;
11502       --  Formal parameter of actual operation, when the derivation is to
11503       --  create a renaming for a primitive operation of an actual in an
11504       --  instantiation.
11505
11506       New_Formal : Entity_Id;
11507       --  Formal of inherited operation
11508
11509       Visible_Subp : Entity_Id := Parent_Subp;
11510
11511       function Is_Private_Overriding return Boolean;
11512       --  If Subp is a private overriding of a visible operation, the inherited
11513       --  operation derives from the overridden op (even though its body is the
11514       --  overriding one) and the inherited operation is visible now. See
11515       --  sem_disp to see the full details of the handling of the overridden
11516       --  subprogram, which is removed from the list of primitive operations of
11517       --  the type. The overridden subprogram is saved locally in Visible_Subp,
11518       --  and used to diagnose abstract operations that need overriding in the
11519       --  derived type.
11520
11521       procedure Replace_Type (Id, New_Id : Entity_Id);
11522       --  When the type is an anonymous access type, create a new access type
11523       --  designating the derived type.
11524
11525       procedure Set_Derived_Name;
11526       --  This procedure sets the appropriate Chars name for New_Subp. This
11527       --  is normally just a copy of the parent name. An exception arises for
11528       --  type support subprograms, where the name is changed to reflect the
11529       --  name of the derived type, e.g. if type foo is derived from type bar,
11530       --  then a procedure barDA is derived with a name fooDA.
11531
11532       ---------------------------
11533       -- Is_Private_Overriding --
11534       ---------------------------
11535
11536       function Is_Private_Overriding return Boolean is
11537          Prev : Entity_Id;
11538
11539       begin
11540          --  If the parent is not a dispatching operation there is no
11541          --  need to investigate overridings
11542
11543          if not Is_Dispatching_Operation (Parent_Subp) then
11544             return False;
11545          end if;
11546
11547          --  The visible operation that is overridden is a homonym of the
11548          --  parent subprogram. We scan the homonym chain to find the one
11549          --  whose alias is the subprogram we are deriving.
11550
11551          Prev := Current_Entity (Parent_Subp);
11552          while Present (Prev) loop
11553             if Ekind (Prev) = Ekind (Parent_Subp)
11554               and then Alias (Prev) = Parent_Subp
11555               and then Scope (Parent_Subp) = Scope (Prev)
11556               and then not Is_Hidden (Prev)
11557             then
11558                Visible_Subp := Prev;
11559                return True;
11560             end if;
11561
11562             Prev := Homonym (Prev);
11563          end loop;
11564
11565          return False;
11566       end Is_Private_Overriding;
11567
11568       ------------------
11569       -- Replace_Type --
11570       ------------------
11571
11572       procedure Replace_Type (Id, New_Id : Entity_Id) is
11573          Acc_Type : Entity_Id;
11574          Par      : constant Node_Id := Parent (Derived_Type);
11575
11576       begin
11577          --  When the type is an anonymous access type, create a new access
11578          --  type designating the derived type. This itype must be elaborated
11579          --  at the point of the derivation, not on subsequent calls that may
11580          --  be out of the proper scope for Gigi, so we insert a reference to
11581          --  it after the derivation.
11582
11583          if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
11584             declare
11585                Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
11586
11587             begin
11588                if Ekind (Desig_Typ) = E_Record_Type_With_Private
11589                  and then Present (Full_View (Desig_Typ))
11590                  and then not Is_Private_Type (Parent_Type)
11591                then
11592                   Desig_Typ := Full_View (Desig_Typ);
11593                end if;
11594
11595                if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
11596
11597                   --  Ada 2005 (AI-251): Handle also derivations of abstract
11598                   --  interface primitives.
11599
11600                  or else (Is_Interface (Desig_Typ)
11601                           and then not Is_Class_Wide_Type (Desig_Typ))
11602                then
11603                   Acc_Type := New_Copy (Etype (Id));
11604                   Set_Etype (Acc_Type, Acc_Type);
11605                   Set_Scope (Acc_Type, New_Subp);
11606
11607                   --  Compute size of anonymous access type
11608
11609                   if Is_Array_Type (Desig_Typ)
11610                     and then not Is_Constrained (Desig_Typ)
11611                   then
11612                      Init_Size (Acc_Type, 2 * System_Address_Size);
11613                   else
11614                      Init_Size (Acc_Type, System_Address_Size);
11615                   end if;
11616
11617                   Init_Alignment (Acc_Type);
11618                   Set_Directly_Designated_Type (Acc_Type, Derived_Type);
11619
11620                   Set_Etype (New_Id, Acc_Type);
11621                   Set_Scope (New_Id, New_Subp);
11622
11623                   --  Create a reference to it
11624                   Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
11625
11626                else
11627                   Set_Etype (New_Id, Etype (Id));
11628                end if;
11629             end;
11630
11631          elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
11632            or else
11633              (Ekind (Etype (Id)) = E_Record_Type_With_Private
11634                and then Present (Full_View (Etype (Id)))
11635                and then
11636                  Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
11637          then
11638             --  Constraint checks on formals are generated during expansion,
11639             --  based on the signature of the original subprogram. The bounds
11640             --  of the derived type are not relevant, and thus we can use
11641             --  the base type for the formals. However, the return type may be
11642             --  used in a context that requires that the proper static bounds
11643             --  be used (a case statement, for example)  and for those cases
11644             --  we must use the derived type (first subtype), not its base.
11645
11646             --  If the derived_type_definition has no constraints, we know that
11647             --  the derived type has the same constraints as the first subtype
11648             --  of the parent, and we can also use it rather than its base,
11649             --  which can lead to more efficient code.
11650
11651             if Etype (Id) = Parent_Type then
11652                if Is_Scalar_Type (Parent_Type)
11653                  and then
11654                    Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
11655                then
11656                   Set_Etype (New_Id, Derived_Type);
11657
11658                elsif Nkind (Par) = N_Full_Type_Declaration
11659                  and then
11660                    Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
11661                  and then
11662                    Is_Entity_Name
11663                      (Subtype_Indication (Type_Definition (Par)))
11664                then
11665                   Set_Etype (New_Id, Derived_Type);
11666
11667                else
11668                   Set_Etype (New_Id, Base_Type (Derived_Type));
11669                end if;
11670
11671             else
11672                Set_Etype (New_Id, Base_Type (Derived_Type));
11673             end if;
11674
11675          --  Ada 2005 (AI-251): Handle derivations of abstract interface
11676          --  primitives.
11677
11678          elsif Is_Interface (Etype (Id))
11679            and then not Is_Class_Wide_Type (Etype (Id))
11680            and then Is_Progenitor (Etype (Id), Derived_Type)
11681          then
11682             Set_Etype (New_Id, Derived_Type);
11683
11684          else
11685             Set_Etype (New_Id, Etype (Id));
11686          end if;
11687       end Replace_Type;
11688
11689       ----------------------
11690       -- Set_Derived_Name --
11691       ----------------------
11692
11693       procedure Set_Derived_Name is
11694          Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
11695       begin
11696          if Nm = TSS_Null then
11697             Set_Chars (New_Subp, Chars (Parent_Subp));
11698          else
11699             Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
11700          end if;
11701       end Set_Derived_Name;
11702
11703       --  Local variables
11704
11705       Parent_Overrides_Interface_Primitive : Boolean := False;
11706
11707    --  Start of processing for Derive_Subprogram
11708
11709    begin
11710       New_Subp :=
11711          New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
11712       Set_Ekind (New_Subp, Ekind (Parent_Subp));
11713
11714       --  Check whether the parent overrides an interface primitive
11715
11716       if Is_Overriding_Operation (Parent_Subp) then
11717          declare
11718             E : Entity_Id := Parent_Subp;
11719          begin
11720             while Present (Overridden_Operation (E)) loop
11721                E := Ultimate_Alias (Overridden_Operation (E));
11722             end loop;
11723
11724             Parent_Overrides_Interface_Primitive :=
11725               Is_Dispatching_Operation (E)
11726                 and then Present (Find_Dispatching_Type (E))
11727                 and then Is_Interface (Find_Dispatching_Type (E));
11728          end;
11729       end if;
11730
11731       --  Check whether the inherited subprogram is a private operation that
11732       --  should be inherited but not yet made visible. Such subprograms can
11733       --  become visible at a later point (e.g., the private part of a public
11734       --  child unit) via Declare_Inherited_Private_Subprograms. If the
11735       --  following predicate is true, then this is not such a private
11736       --  operation and the subprogram simply inherits the name of the parent
11737       --  subprogram. Note the special check for the names of controlled
11738       --  operations, which are currently exempted from being inherited with
11739       --  a hidden name because they must be findable for generation of
11740       --  implicit run-time calls.
11741
11742       if not Is_Hidden (Parent_Subp)
11743         or else Is_Internal (Parent_Subp)
11744         or else Is_Private_Overriding
11745         or else Is_Internal_Name (Chars (Parent_Subp))
11746         or else Chars (Parent_Subp) = Name_Initialize
11747         or else Chars (Parent_Subp) = Name_Adjust
11748         or else Chars (Parent_Subp) = Name_Finalize
11749       then
11750          Set_Derived_Name;
11751
11752       --  If parent is hidden, this can be a regular derivation if the
11753       --  parent is immediately visible in a non-instantiating context,
11754       --  or if we are in the private part of an instance. This test
11755       --  should still be refined ???
11756
11757       --  The test for In_Instance_Not_Visible avoids inheriting the derived
11758       --  operation as a non-visible operation in cases where the parent
11759       --  subprogram might not be visible now, but was visible within the
11760       --  original generic, so it would be wrong to make the inherited
11761       --  subprogram non-visible now. (Not clear if this test is fully
11762       --  correct; are there any cases where we should declare the inherited
11763       --  operation as not visible to avoid it being overridden, e.g., when
11764       --  the parent type is a generic actual with private primitives ???)
11765
11766       --  (they should be treated the same as other private inherited
11767       --  subprograms, but it's not clear how to do this cleanly). ???
11768
11769       elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
11770               and then Is_Immediately_Visible (Parent_Subp)
11771               and then not In_Instance)
11772         or else In_Instance_Not_Visible
11773       then
11774          Set_Derived_Name;
11775
11776       --  Ada 2005 (AI-251): Regular derivation if the parent subprogram
11777       --  overrides an interface primitive because interface primitives
11778       --  must be visible in the partial view of the parent (RM 7.3 (7.3/2))
11779
11780       elsif Parent_Overrides_Interface_Primitive then
11781          Set_Derived_Name;
11782
11783       --  The type is inheriting a private operation, so enter
11784       --  it with a special name so it can't be overridden.
11785
11786       else
11787          Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
11788       end if;
11789
11790       Set_Parent (New_Subp, Parent (Derived_Type));
11791
11792       if Present (Actual_Subp) then
11793          Replace_Type (Actual_Subp, New_Subp);
11794       else
11795          Replace_Type (Parent_Subp, New_Subp);
11796       end if;
11797
11798       Conditional_Delay (New_Subp, Parent_Subp);
11799
11800       --  If we are creating a renaming for a primitive operation of an
11801       --  actual of a generic derived type, we must examine the signature
11802       --  of the actual primitive, not that of the generic formal, which for
11803       --  example may be an interface. However the name and initial value
11804       --  of the inherited operation are those of the formal primitive.
11805
11806       Formal := First_Formal (Parent_Subp);
11807
11808       if Present (Actual_Subp) then
11809          Formal_Of_Actual := First_Formal (Actual_Subp);
11810       else
11811          Formal_Of_Actual := Empty;
11812       end if;
11813
11814       while Present (Formal) loop
11815          New_Formal := New_Copy (Formal);
11816
11817          --  Normally we do not go copying parents, but in the case of
11818          --  formals, we need to link up to the declaration (which is the
11819          --  parameter specification), and it is fine to link up to the
11820          --  original formal's parameter specification in this case.
11821
11822          Set_Parent (New_Formal, Parent (Formal));
11823          Append_Entity (New_Formal, New_Subp);
11824
11825          if Present (Formal_Of_Actual) then
11826             Replace_Type (Formal_Of_Actual, New_Formal);
11827             Next_Formal (Formal_Of_Actual);
11828          else
11829             Replace_Type (Formal, New_Formal);
11830          end if;
11831
11832          Next_Formal (Formal);
11833       end loop;
11834
11835       --  If this derivation corresponds to a tagged generic actual, then
11836       --  primitive operations rename those of the actual. Otherwise the
11837       --  primitive operations rename those of the parent type, If the parent
11838       --  renames an intrinsic operator, so does the new subprogram. We except
11839       --  concatenation, which is always properly typed, and does not get
11840       --  expanded as other intrinsic operations.
11841
11842       if No (Actual_Subp) then
11843          if Is_Intrinsic_Subprogram (Parent_Subp) then
11844             Set_Is_Intrinsic_Subprogram (New_Subp);
11845
11846             if Present (Alias (Parent_Subp))
11847               and then Chars (Parent_Subp) /= Name_Op_Concat
11848             then
11849                Set_Alias (New_Subp, Alias (Parent_Subp));
11850             else
11851                Set_Alias (New_Subp, Parent_Subp);
11852             end if;
11853
11854          else
11855             Set_Alias (New_Subp, Parent_Subp);
11856          end if;
11857
11858       else
11859          Set_Alias (New_Subp, Actual_Subp);
11860       end if;
11861
11862       --  Derived subprograms of a tagged type must inherit the convention
11863       --  of the parent subprogram (a requirement of AI-117). Derived
11864       --  subprograms of untagged types simply get convention Ada by default.
11865
11866       if Is_Tagged_Type (Derived_Type) then
11867          Set_Convention (New_Subp, Convention (Parent_Subp));
11868       end if;
11869
11870       Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
11871       Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
11872
11873       if Ekind (Parent_Subp) = E_Procedure then
11874          Set_Is_Valued_Procedure
11875            (New_Subp, Is_Valued_Procedure (Parent_Subp));
11876       end if;
11877
11878       --  No_Return must be inherited properly. If this is overridden in the
11879       --  case of a dispatching operation, then a check is made in Sem_Disp
11880       --  that the overriding operation is also No_Return (no such check is
11881       --  required for the case of non-dispatching operation.
11882
11883       Set_No_Return (New_Subp, No_Return (Parent_Subp));
11884
11885       --  A derived function with a controlling result is abstract. If the
11886       --  Derived_Type is a nonabstract formal generic derived type, then
11887       --  inherited operations are not abstract: the required check is done at
11888       --  instantiation time. If the derivation is for a generic actual, the
11889       --  function is not abstract unless the actual is.
11890
11891       if Is_Generic_Type (Derived_Type)
11892         and then not Is_Abstract_Type (Derived_Type)
11893       then
11894          null;
11895
11896       --  Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
11897       --  properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
11898
11899       elsif Ada_Version >= Ada_05
11900         and then (Is_Abstract_Subprogram (Alias (New_Subp))
11901                    or else (Is_Tagged_Type (Derived_Type)
11902                             and then Etype (New_Subp) = Derived_Type
11903                             and then not Is_Null_Extension (Derived_Type))
11904                    or else (Is_Tagged_Type (Derived_Type)
11905                             and then Ekind (Etype (New_Subp)) =
11906                                                        E_Anonymous_Access_Type
11907                             and then Designated_Type (Etype (New_Subp)) =
11908                                                        Derived_Type
11909                             and then not Is_Null_Extension (Derived_Type)))
11910         and then No (Actual_Subp)
11911       then
11912          if not Is_Tagged_Type (Derived_Type)
11913            or else Is_Abstract_Type (Derived_Type)
11914            or else Is_Abstract_Subprogram (Alias (New_Subp))
11915          then
11916             Set_Is_Abstract_Subprogram (New_Subp);
11917          else
11918             Set_Requires_Overriding (New_Subp);
11919          end if;
11920
11921       elsif Ada_Version < Ada_05
11922         and then (Is_Abstract_Subprogram (Alias (New_Subp))
11923                    or else (Is_Tagged_Type (Derived_Type)
11924                              and then Etype (New_Subp) = Derived_Type
11925                              and then No (Actual_Subp)))
11926       then
11927          Set_Is_Abstract_Subprogram (New_Subp);
11928
11929       --  Finally, if the parent type is abstract we must verify that all
11930       --  inherited operations are either non-abstract or overridden, or that
11931       --  the derived type itself is abstract (this check is performed at the
11932       --  end of a package declaration, in Check_Abstract_Overriding). A
11933       --  private overriding in the parent type will not be visible in the
11934       --  derivation if we are not in an inner package or in a child unit of
11935       --  the parent type, in which case the abstractness of the inherited
11936       --  operation is carried to the new subprogram.
11937
11938       elsif Is_Abstract_Type (Parent_Type)
11939         and then not In_Open_Scopes (Scope (Parent_Type))
11940         and then Is_Private_Overriding
11941         and then Is_Abstract_Subprogram (Visible_Subp)
11942       then
11943          if No (Actual_Subp) then
11944             Set_Alias (New_Subp, Visible_Subp);
11945             Set_Is_Abstract_Subprogram
11946               (New_Subp, True);
11947          else
11948             --  If this is a derivation for an instance of a formal derived
11949             --  type, abstractness comes from the primitive operation of the
11950             --  actual, not from the operation inherited from the ancestor.
11951
11952             Set_Is_Abstract_Subprogram
11953               (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
11954          end if;
11955       end if;
11956
11957       New_Overloaded_Entity (New_Subp, Derived_Type);
11958
11959       --  Check for case of a derived subprogram for the instantiation of a
11960       --  formal derived tagged type, if so mark the subprogram as dispatching
11961       --  and inherit the dispatching attributes of the parent subprogram. The
11962       --  derived subprogram is effectively renaming of the actual subprogram,
11963       --  so it needs to have the same attributes as the actual.
11964
11965       if Present (Actual_Subp)
11966         and then Is_Dispatching_Operation (Parent_Subp)
11967       then
11968          Set_Is_Dispatching_Operation (New_Subp);
11969
11970          if Present (DTC_Entity (Parent_Subp)) then
11971             Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
11972             Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
11973          end if;
11974       end if;
11975
11976       --  Indicate that a derived subprogram does not require a body and that
11977       --  it does not require processing of default expressions.
11978
11979       Set_Has_Completion (New_Subp);
11980       Set_Default_Expressions_Processed (New_Subp);
11981
11982       if Ekind (New_Subp) = E_Function then
11983          Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
11984       end if;
11985    end Derive_Subprogram;
11986
11987    ------------------------
11988    -- Derive_Subprograms --
11989    ------------------------
11990
11991    procedure Derive_Subprograms
11992      (Parent_Type    : Entity_Id;
11993       Derived_Type   : Entity_Id;
11994       Generic_Actual : Entity_Id := Empty)
11995    is
11996       Op_List : constant Elist_Id :=
11997                   Collect_Primitive_Operations (Parent_Type);
11998
11999       function Check_Derived_Type return Boolean;
12000       --  Check that all primitive inherited from Parent_Type are found in
12001       --  the list of primitives of Derived_Type exactly in the same order.
12002
12003       function Check_Derived_Type return Boolean is
12004          E        : Entity_Id;
12005          Elmt     : Elmt_Id;
12006          List     : Elist_Id;
12007          New_Subp : Entity_Id;
12008          Op_Elmt  : Elmt_Id;
12009          Subp     : Entity_Id;
12010
12011       begin
12012          --  Traverse list of entities in the current scope searching for
12013          --  an incomplete type whose full-view is derived type
12014
12015          E := First_Entity (Scope (Derived_Type));
12016          while Present (E)
12017            and then E /= Derived_Type
12018          loop
12019             if Ekind (E) = E_Incomplete_Type
12020               and then Present (Full_View (E))
12021               and then Full_View (E) = Derived_Type
12022             then
12023                --  Disable this test if Derived_Type completes an incomplete
12024                --  type because in such case more primitives can be added
12025                --  later to the list of primitives of Derived_Type by routine
12026                --  Process_Incomplete_Dependents
12027
12028                return True;
12029             end if;
12030
12031             E := Next_Entity (E);
12032          end loop;
12033
12034          List := Collect_Primitive_Operations (Derived_Type);
12035          Elmt := First_Elmt (List);
12036
12037          Op_Elmt := First_Elmt (Op_List);
12038          while Present (Op_Elmt) loop
12039             Subp     := Node (Op_Elmt);
12040             New_Subp := Node (Elmt);
12041
12042             --  At this early stage Derived_Type has no entities with attribute
12043             --  Interface_Alias. In addition, such primitives are always
12044             --  located at the end of the list of primitives of Parent_Type.
12045             --  Therefore, if found we can safely stop processing pending
12046             --  entities.
12047
12048             exit when Present (Interface_Alias (Subp));
12049
12050             --  Handle hidden entities
12051
12052             if not Is_Predefined_Dispatching_Operation (Subp)
12053               and then Is_Hidden (Subp)
12054             then
12055                if Present (New_Subp)
12056                  and then Primitive_Names_Match (Subp, New_Subp)
12057                then
12058                   Next_Elmt (Elmt);
12059                end if;
12060
12061             else
12062                if not Present (New_Subp)
12063                  or else Ekind (Subp) /= Ekind (New_Subp)
12064                  or else not Primitive_Names_Match (Subp, New_Subp)
12065                then
12066                   return False;
12067                end if;
12068
12069                Next_Elmt (Elmt);
12070             end if;
12071
12072             Next_Elmt (Op_Elmt);
12073          end loop;
12074
12075          return True;
12076       end Check_Derived_Type;
12077
12078       --  Local variables
12079
12080       Alias_Subp   : Entity_Id;
12081       Act_List     : Elist_Id;
12082       Act_Elmt     : Elmt_Id   := No_Elmt;
12083       Act_Subp     : Entity_Id := Empty;
12084       Elmt         : Elmt_Id;
12085       Need_Search  : Boolean   := False;
12086       New_Subp     : Entity_Id := Empty;
12087       Parent_Base  : Entity_Id;
12088       Subp         : Entity_Id;
12089
12090    --  Start of processing for Derive_Subprograms
12091
12092    begin
12093       if Ekind (Parent_Type) = E_Record_Type_With_Private
12094         and then Has_Discriminants (Parent_Type)
12095         and then Present (Full_View (Parent_Type))
12096       then
12097          Parent_Base := Full_View (Parent_Type);
12098       else
12099          Parent_Base := Parent_Type;
12100       end if;
12101
12102       if Present (Generic_Actual) then
12103          Act_List := Collect_Primitive_Operations (Generic_Actual);
12104          Act_Elmt := First_Elmt (Act_List);
12105       end if;
12106
12107       --  Derive primitives inherited from the parent. Note that if the generic
12108       --  actual is present, this is not really a type derivation, it is a
12109       --  completion within an instance.
12110
12111       --  Case 1: Derived_Type does not implement interfaces
12112
12113       if not Is_Tagged_Type (Derived_Type)
12114         or else (not Has_Interfaces (Derived_Type)
12115                   and then not (Present (Generic_Actual)
12116                                   and then
12117                                 Has_Interfaces (Generic_Actual)))
12118       then
12119          Elmt := First_Elmt (Op_List);
12120          while Present (Elmt) loop
12121             Subp := Node (Elmt);
12122
12123             --  Literals are derived earlier in the process of building the
12124             --  derived type, and are skipped here.
12125
12126             if Ekind (Subp) = E_Enumeration_Literal then
12127                null;
12128
12129             --  The actual is a direct descendant and the common primitive
12130             --  operations appear in the same order.
12131
12132             --  If the generic parent type is present, the derived type is an
12133             --  instance of a formal derived type, and within the instance its
12134             --  operations are those of the actual. We derive from the formal
12135             --  type but make the inherited operations aliases of the
12136             --  corresponding operations of the actual.
12137
12138             else
12139                Derive_Subprogram
12140                  (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
12141
12142                if Present (Act_Elmt) then
12143                   Next_Elmt (Act_Elmt);
12144                end if;
12145             end if;
12146
12147             Next_Elmt (Elmt);
12148          end loop;
12149
12150       --  Case 2: Derived_Type implements interfaces
12151
12152       else
12153          --  If the parent type has no predefined primitives we remove
12154          --  predefined primitives from the list of primitives of generic
12155          --  actual to simplify the complexity of this algorithm.
12156
12157          if Present (Generic_Actual) then
12158             declare
12159                Has_Predefined_Primitives : Boolean := False;
12160
12161             begin
12162                --  Check if the parent type has predefined primitives
12163
12164                Elmt := First_Elmt (Op_List);
12165                while Present (Elmt) loop
12166                   Subp := Node (Elmt);
12167
12168                   if Is_Predefined_Dispatching_Operation (Subp)
12169                     and then not Comes_From_Source (Ultimate_Alias (Subp))
12170                   then
12171                      Has_Predefined_Primitives := True;
12172                      exit;
12173                   end if;
12174
12175                   Next_Elmt (Elmt);
12176                end loop;
12177
12178                --  Remove predefined primitives of Generic_Actual. We must use
12179                --  an auxiliary list because in case of tagged types the value
12180                --  returned by Collect_Primitive_Operations is the value stored
12181                --  in its Primitive_Operations attribute (and we don't want to
12182                --  modify its current contents).
12183
12184                if not Has_Predefined_Primitives then
12185                   declare
12186                      Aux_List : constant Elist_Id := New_Elmt_List;
12187
12188                   begin
12189                      Elmt := First_Elmt (Act_List);
12190                      while Present (Elmt) loop
12191                         Subp := Node (Elmt);
12192
12193                         if not Is_Predefined_Dispatching_Operation (Subp)
12194                           or else Comes_From_Source (Subp)
12195                         then
12196                            Append_Elmt (Subp, Aux_List);
12197                         end if;
12198
12199                         Next_Elmt (Elmt);
12200                      end loop;
12201
12202                      Act_List := Aux_List;
12203                   end;
12204                end if;
12205
12206                Act_Elmt := First_Elmt (Act_List);
12207                Act_Subp := Node (Act_Elmt);
12208             end;
12209          end if;
12210
12211          --  Stage 1: If the generic actual is not present we derive the
12212          --  primitives inherited from the parent type. If the generic parent
12213          --  type is present, the derived type is an instance of a formal
12214          --  derived type, and within the instance its operations are those of
12215          --  the actual. We derive from the formal type but make the inherited
12216          --  operations aliases of the corresponding operations of the actual.
12217
12218          Elmt := First_Elmt (Op_List);
12219          while Present (Elmt) loop
12220             Subp       := Node (Elmt);
12221             Alias_Subp := Ultimate_Alias (Subp);
12222
12223             --  At this early stage Derived_Type has no entities with attribute
12224             --  Interface_Alias. In addition, such primitives are always
12225             --  located at the end of the list of primitives of Parent_Type.
12226             --  Therefore, if found we can safely stop processing pending
12227             --  entities.
12228
12229             exit when Present (Interface_Alias (Subp));
12230
12231             --  If the generic actual is present find the corresponding
12232             --  operation in the generic actual. If the parent type is a
12233             --  direct ancestor of the derived type then, even if it is an
12234             --  interface, the operations are inherited from the primary
12235             --  dispatch table and are in the proper order. If we detect here
12236             --  that primitives are not in the same order we traverse the list
12237             --  of primitive operations of the actual to find the one that
12238             --  implements the interface primitive.
12239
12240             if Need_Search
12241               or else
12242                 (Present (Generic_Actual)
12243                    and then Present (Act_Subp)
12244                    and then not Primitive_Names_Match (Subp, Act_Subp))
12245             then
12246                pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual));
12247                pragma Assert (Is_Interface (Parent_Base));
12248
12249                --  Remember that we need searching for all the pending
12250                --  primitives
12251
12252                Need_Search := True;
12253
12254                --  Handle entities associated with interface primitives
12255
12256                if Present (Alias (Subp))
12257                  and then Is_Interface (Find_Dispatching_Type (Alias (Subp)))
12258                  and then not Is_Predefined_Dispatching_Operation (Subp)
12259                then
12260                   Act_Subp :=
12261                     Find_Primitive_Covering_Interface
12262                       (Tagged_Type => Generic_Actual,
12263                        Iface_Prim  => Subp);
12264
12265                --  Handle predefined primitives plus the rest of user-defined
12266                --  primitives
12267
12268                else
12269                   Act_Elmt := First_Elmt (Act_List);
12270                   while Present (Act_Elmt) loop
12271                      Act_Subp := Node (Act_Elmt);
12272
12273                      exit when Primitive_Names_Match (Subp, Act_Subp)
12274                        and then Type_Conformant (Subp, Act_Subp,
12275                                   Skip_Controlling_Formals => True)
12276                        and then No (Interface_Alias (Act_Subp));
12277
12278                      Next_Elmt (Act_Elmt);
12279                   end loop;
12280                end if;
12281             end if;
12282
12283             --   Case 1: If the parent is a limited interface then it has the
12284             --   predefined primitives of synchronized interfaces. However, the
12285             --   actual type may be a non-limited type and hence it does not
12286             --   have such primitives.
12287
12288             if Present (Generic_Actual)
12289               and then not Present (Act_Subp)
12290               and then Is_Limited_Interface (Parent_Base)
12291               and then Is_Predefined_Interface_Primitive (Subp)
12292             then
12293                null;
12294
12295             --  Case 2: Inherit entities associated with interfaces that
12296             --  were not covered by the parent type. We exclude here null
12297             --  interface primitives because they do not need special
12298             --  management.
12299
12300             elsif Present (Alias (Subp))
12301               and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
12302               and then not
12303                 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
12304                    and then Null_Present (Parent (Alias_Subp)))
12305             then
12306                Derive_Subprogram
12307                  (New_Subp     => New_Subp,
12308                   Parent_Subp  => Alias_Subp,
12309                   Derived_Type => Derived_Type,
12310                   Parent_Type  => Find_Dispatching_Type (Alias_Subp),
12311                   Actual_Subp  => Act_Subp);
12312
12313                if No (Generic_Actual) then
12314                   Set_Alias (New_Subp, Subp);
12315                end if;
12316
12317             --  Case 3: Common derivation
12318
12319             else
12320                Derive_Subprogram
12321                  (New_Subp     => New_Subp,
12322                   Parent_Subp  => Subp,
12323                   Derived_Type => Derived_Type,
12324                   Parent_Type  => Parent_Base,
12325                   Actual_Subp  => Act_Subp);
12326             end if;
12327
12328             --  No need to update Act_Elm if we must search for the
12329             --  corresponding operation in the generic actual
12330
12331             if not Need_Search
12332               and then Present (Act_Elmt)
12333             then
12334                Next_Elmt (Act_Elmt);
12335                Act_Subp := Node (Act_Elmt);
12336             end if;
12337
12338             Next_Elmt (Elmt);
12339          end loop;
12340
12341          --  Inherit additional operations from progenitors. If the derived
12342          --  type is a generic actual, there are not new primitive operations
12343          --  for the type because it has those of the actual, and therefore
12344          --  nothing needs to be done. The renamings generated above are not
12345          --  primitive operations, and their purpose is simply to make the
12346          --  proper operations visible within an instantiation.
12347
12348          if No (Generic_Actual) then
12349             Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
12350          end if;
12351       end if;
12352
12353       --  Final check: Direct descendants must have their primitives in the
12354       --  same order. We exclude from this test non-tagged types and instances
12355       --  of formal derived types. We skip this test if we have already
12356       --  reported serious errors in the sources.
12357
12358       pragma Assert (not Is_Tagged_Type (Derived_Type)
12359         or else Present (Generic_Actual)
12360         or else Serious_Errors_Detected > 0
12361         or else Check_Derived_Type);
12362    end Derive_Subprograms;
12363
12364    --------------------------------
12365    -- Derived_Standard_Character --
12366    --------------------------------
12367
12368    procedure Derived_Standard_Character
12369      (N            : Node_Id;
12370       Parent_Type  : Entity_Id;
12371       Derived_Type : Entity_Id)
12372    is
12373       Loc           : constant Source_Ptr := Sloc (N);
12374       Def           : constant Node_Id    := Type_Definition (N);
12375       Indic         : constant Node_Id    := Subtype_Indication (Def);
12376       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
12377       Implicit_Base : constant Entity_Id  :=
12378                         Create_Itype
12379                           (E_Enumeration_Type, N, Derived_Type, 'B');
12380
12381       Lo : Node_Id;
12382       Hi : Node_Id;
12383
12384    begin
12385       Discard_Node (Process_Subtype (Indic, N));
12386
12387       Set_Etype     (Implicit_Base, Parent_Base);
12388       Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
12389       Set_RM_Size   (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
12390
12391       Set_Is_Character_Type  (Implicit_Base, True);
12392       Set_Has_Delayed_Freeze (Implicit_Base);
12393
12394       --  The bounds of the implicit base are the bounds of the parent base.
12395       --  Note that their type is the parent base.
12396
12397       Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Base));
12398       Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
12399
12400       Set_Scalar_Range (Implicit_Base,
12401         Make_Range (Loc,
12402           Low_Bound  => Lo,
12403           High_Bound => Hi));
12404
12405       Conditional_Delay (Derived_Type, Parent_Type);
12406
12407       Set_Ekind (Derived_Type, E_Enumeration_Subtype);
12408       Set_Etype (Derived_Type, Implicit_Base);
12409       Set_Size_Info         (Derived_Type, Parent_Type);
12410
12411       if Unknown_RM_Size (Derived_Type) then
12412          Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
12413       end if;
12414
12415       Set_Is_Character_Type (Derived_Type, True);
12416
12417       if Nkind (Indic) /= N_Subtype_Indication then
12418
12419          --  If no explicit constraint, the bounds are those
12420          --  of the parent type.
12421
12422          Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Type));
12423          Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
12424          Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
12425       end if;
12426
12427       Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
12428
12429       --  Because the implicit base is used in the conversion of the bounds, we
12430       --  have to freeze it now. This is similar to what is done for numeric
12431       --  types, and it equally suspicious, but otherwise a non-static bound
12432       --  will have a reference to an unfrozen type, which is rejected by Gigi
12433       --  (???). This requires specific care for definition of stream
12434       --  attributes. For details, see comments at the end of
12435       --  Build_Derived_Numeric_Type.
12436
12437       Freeze_Before (N, Implicit_Base);
12438    end Derived_Standard_Character;
12439
12440    ------------------------------
12441    -- Derived_Type_Declaration --
12442    ------------------------------
12443
12444    procedure Derived_Type_Declaration
12445      (T             : Entity_Id;
12446       N             : Node_Id;
12447       Is_Completion : Boolean)
12448    is
12449       Parent_Type  : Entity_Id;
12450
12451       function Comes_From_Generic (Typ : Entity_Id) return Boolean;
12452       --  Check whether the parent type is a generic formal, or derives
12453       --  directly or indirectly from one.
12454
12455       ------------------------
12456       -- Comes_From_Generic --
12457       ------------------------
12458
12459       function Comes_From_Generic (Typ : Entity_Id) return Boolean is
12460       begin
12461          if Is_Generic_Type (Typ) then
12462             return True;
12463
12464          elsif Is_Generic_Type (Root_Type (Parent_Type)) then
12465             return True;
12466
12467          elsif Is_Private_Type (Typ)
12468            and then Present (Full_View (Typ))
12469            and then Is_Generic_Type (Root_Type (Full_View (Typ)))
12470          then
12471             return True;
12472
12473          elsif Is_Generic_Actual_Type (Typ) then
12474             return True;
12475
12476          else
12477             return False;
12478          end if;
12479       end Comes_From_Generic;
12480
12481       --  Local variables
12482
12483       Def          : constant Node_Id := Type_Definition (N);
12484       Iface_Def    : Node_Id;
12485       Indic        : constant Node_Id := Subtype_Indication (Def);
12486       Extension    : constant Node_Id := Record_Extension_Part (Def);
12487       Parent_Node  : Node_Id;
12488       Parent_Scope : Entity_Id;
12489       Taggd        : Boolean;
12490
12491    --  Start of processing for Derived_Type_Declaration
12492
12493    begin
12494       Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
12495
12496       --  Ada 2005 (AI-251): In case of interface derivation check that the
12497       --  parent is also an interface.
12498
12499       if Interface_Present (Def) then
12500          if not Is_Interface (Parent_Type) then
12501             Diagnose_Interface (Indic, Parent_Type);
12502
12503          else
12504             Parent_Node := Parent (Base_Type (Parent_Type));
12505             Iface_Def   := Type_Definition (Parent_Node);
12506
12507             --  Ada 2005 (AI-251): Limited interfaces can only inherit from
12508             --  other limited interfaces.
12509
12510             if Limited_Present (Def) then
12511                if Limited_Present (Iface_Def) then
12512                   null;
12513
12514                elsif Protected_Present (Iface_Def) then
12515                   Error_Msg_N
12516                     ("(Ada 2005) limited interface cannot "
12517                      & "inherit from protected interface", Indic);
12518
12519                elsif Synchronized_Present (Iface_Def) then
12520                   Error_Msg_N
12521                     ("(Ada 2005) limited interface cannot "
12522                      & "inherit from synchronized interface", Indic);
12523
12524                elsif Task_Present (Iface_Def) then
12525                   Error_Msg_N
12526                     ("(Ada 2005) limited interface cannot "
12527                      & "inherit from task interface", Indic);
12528
12529                else
12530                   Error_Msg_N
12531                     ("(Ada 2005) limited interface cannot "
12532                      & "inherit from non-limited interface", Indic);
12533                end if;
12534
12535             --  Ada 2005 (AI-345): Non-limited interfaces can only inherit
12536             --  from non-limited or limited interfaces.
12537
12538             elsif not Protected_Present (Def)
12539               and then not Synchronized_Present (Def)
12540               and then not Task_Present (Def)
12541             then
12542                if Limited_Present (Iface_Def) then
12543                   null;
12544
12545                elsif Protected_Present (Iface_Def) then
12546                   Error_Msg_N
12547                     ("(Ada 2005) non-limited interface cannot "
12548                      & "inherit from protected interface", Indic);
12549
12550                elsif Synchronized_Present (Iface_Def) then
12551                   Error_Msg_N
12552                     ("(Ada 2005) non-limited interface cannot "
12553                      & "inherit from synchronized interface", Indic);
12554
12555                elsif Task_Present (Iface_Def) then
12556                   Error_Msg_N
12557                     ("(Ada 2005) non-limited interface cannot "
12558                      & "inherit from task interface", Indic);
12559
12560                else
12561                   null;
12562                end if;
12563             end if;
12564          end if;
12565       end if;
12566
12567       if Is_Tagged_Type (Parent_Type)
12568         and then Is_Concurrent_Type (Parent_Type)
12569         and then not Is_Interface (Parent_Type)
12570       then
12571          Error_Msg_N
12572            ("parent type of a record extension cannot be "
12573             & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
12574          Set_Etype (T, Any_Type);
12575          return;
12576       end if;
12577
12578       --  Ada 2005 (AI-251): Decorate all the names in the list of ancestor
12579       --  interfaces
12580
12581       if Is_Tagged_Type (Parent_Type)
12582         and then Is_Non_Empty_List (Interface_List (Def))
12583       then
12584          declare
12585             Intf : Node_Id;
12586             T    : Entity_Id;
12587
12588          begin
12589             Intf := First (Interface_List (Def));
12590             while Present (Intf) loop
12591                T := Find_Type_Of_Subtype_Indic (Intf);
12592
12593                if not Is_Interface (T) then
12594                   Diagnose_Interface (Intf, T);
12595
12596                --  Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
12597                --  a limited type from having a nonlimited progenitor.
12598
12599                elsif (Limited_Present (Def)
12600                        or else (not Is_Interface (Parent_Type)
12601                                  and then Is_Limited_Type (Parent_Type)))
12602                  and then not Is_Limited_Interface (T)
12603                then
12604                   Error_Msg_NE
12605                    ("progenitor interface& of limited type must be limited",
12606                      N, T);
12607                end if;
12608
12609                Next (Intf);
12610             end loop;
12611          end;
12612       end if;
12613
12614       if Parent_Type = Any_Type
12615         or else Etype (Parent_Type) = Any_Type
12616         or else (Is_Class_Wide_Type (Parent_Type)
12617                    and then Etype (Parent_Type) = T)
12618       then
12619          --  If Parent_Type is undefined or illegal, make new type into a
12620          --  subtype of Any_Type, and set a few attributes to prevent cascaded
12621          --  errors. If this is a self-definition, emit error now.
12622
12623          if T = Parent_Type
12624            or else T = Etype (Parent_Type)
12625          then
12626             Error_Msg_N ("type cannot be used in its own definition", Indic);
12627          end if;
12628
12629          Set_Ekind        (T, Ekind (Parent_Type));
12630          Set_Etype        (T, Any_Type);
12631          Set_Scalar_Range (T, Scalar_Range (Any_Type));
12632
12633          if Is_Tagged_Type (T) then
12634             Set_Primitive_Operations (T, New_Elmt_List);
12635          end if;
12636
12637          return;
12638       end if;
12639
12640       --  Ada 2005 (AI-251): The case in which the parent of the full-view is
12641       --  an interface is special because the list of interfaces in the full
12642       --  view can be given in any order. For example:
12643
12644       --     type A is interface;
12645       --     type B is interface and A;
12646       --     type D is new B with private;
12647       --   private
12648       --     type D is new A and B with null record; -- 1 --
12649
12650       --  In this case we perform the following transformation of -1-:
12651
12652       --     type D is new B and A with null record;
12653
12654       --  If the parent of the full-view covers the parent of the partial-view
12655       --  we have two possible cases:
12656
12657       --     1) They have the same parent
12658       --     2) The parent of the full-view implements some further interfaces
12659
12660       --  In both cases we do not need to perform the transformation. In the
12661       --  first case the source program is correct and the transformation is
12662       --  not needed; in the second case the source program does not fulfill
12663       --  the no-hidden interfaces rule (AI-396) and the error will be reported
12664       --  later.
12665
12666       --  This transformation not only simplifies the rest of the analysis of
12667       --  this type declaration but also simplifies the correct generation of
12668       --  the object layout to the expander.
12669
12670       if In_Private_Part (Current_Scope)
12671         and then Is_Interface (Parent_Type)
12672       then
12673          declare
12674             Iface               : Node_Id;
12675             Partial_View        : Entity_Id;
12676             Partial_View_Parent : Entity_Id;
12677             New_Iface           : Node_Id;
12678
12679          begin
12680             --  Look for the associated private type declaration
12681
12682             Partial_View := First_Entity (Current_Scope);
12683             loop
12684                exit when No (Partial_View)
12685                  or else (Has_Private_Declaration (Partial_View)
12686                            and then Full_View (Partial_View) = T);
12687
12688                Next_Entity (Partial_View);
12689             end loop;
12690
12691             --  If the partial view was not found then the source code has
12692             --  errors and the transformation is not needed.
12693
12694             if Present (Partial_View) then
12695                Partial_View_Parent := Etype (Partial_View);
12696
12697                --  If the parent of the full-view covers the parent of the
12698                --  partial-view we have nothing else to do.
12699
12700                if Interface_Present_In_Ancestor
12701                     (Parent_Type, Partial_View_Parent)
12702                then
12703                   null;
12704
12705                --  Traverse the list of interfaces of the full-view to look
12706                --  for the parent of the partial-view and perform the tree
12707                --  transformation.
12708
12709                else
12710                   Iface := First (Interface_List (Def));
12711                   while Present (Iface) loop
12712                      if Etype (Iface) = Etype (Partial_View) then
12713                         Rewrite (Subtype_Indication (Def),
12714                           New_Copy (Subtype_Indication
12715                                      (Parent (Partial_View))));
12716
12717                         New_Iface := Make_Identifier (Sloc (N),
12718                                        Chars (Parent_Type));
12719                         Append (New_Iface, Interface_List (Def));
12720
12721                         --  Analyze the transformed code
12722
12723                         Derived_Type_Declaration (T, N, Is_Completion);
12724                         return;
12725                      end if;
12726
12727                      Next (Iface);
12728                   end loop;
12729                end if;
12730             end if;
12731          end;
12732       end if;
12733
12734       --  Only composite types other than array types are allowed to have
12735       --  discriminants.
12736
12737       if Present (Discriminant_Specifications (N))
12738         and then (Is_Elementary_Type (Parent_Type)
12739                   or else Is_Array_Type (Parent_Type))
12740         and then not Error_Posted (N)
12741       then
12742          Error_Msg_N
12743            ("elementary or array type cannot have discriminants",
12744             Defining_Identifier (First (Discriminant_Specifications (N))));
12745          Set_Has_Discriminants (T, False);
12746       end if;
12747
12748       --  In Ada 83, a derived type defined in a package specification cannot
12749       --  be used for further derivation until the end of its visible part.
12750       --  Note that derivation in the private part of the package is allowed.
12751
12752       if Ada_Version = Ada_83
12753         and then Is_Derived_Type (Parent_Type)
12754         and then In_Visible_Part (Scope (Parent_Type))
12755       then
12756          if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
12757             Error_Msg_N
12758               ("(Ada 83): premature use of type for derivation", Indic);
12759          end if;
12760       end if;
12761
12762       --  Check for early use of incomplete or private type
12763
12764       if Ekind (Parent_Type) = E_Void
12765         or else Ekind (Parent_Type) = E_Incomplete_Type
12766       then
12767          Error_Msg_N ("premature derivation of incomplete type", Indic);
12768          return;
12769
12770       elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
12771               and then not Comes_From_Generic (Parent_Type))
12772         or else Has_Private_Component (Parent_Type)
12773       then
12774          --  The ancestor type of a formal type can be incomplete, in which
12775          --  case only the operations of the partial view are available in
12776          --  the generic. Subsequent checks may be required when the full
12777          --  view is analyzed, to verify that derivation from a tagged type
12778          --  has an extension.
12779
12780          if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
12781             null;
12782
12783          elsif No (Underlying_Type (Parent_Type))
12784            or else Has_Private_Component (Parent_Type)
12785          then
12786             Error_Msg_N
12787               ("premature derivation of derived or private type", Indic);
12788
12789             --  Flag the type itself as being in error, this prevents some
12790             --  nasty problems with subsequent uses of the malformed type.
12791
12792             Set_Error_Posted (T);
12793
12794          --  Check that within the immediate scope of an untagged partial
12795          --  view it's illegal to derive from the partial view if the
12796          --  full view is tagged. (7.3(7))
12797
12798          --  We verify that the Parent_Type is a partial view by checking
12799          --  that it is not a Full_Type_Declaration (i.e. a private type or
12800          --  private extension declaration), to distinguish a partial view
12801          --  from  a derivation from a private type which also appears as
12802          --  E_Private_Type.
12803
12804          elsif Present (Full_View (Parent_Type))
12805            and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
12806            and then not Is_Tagged_Type (Parent_Type)
12807            and then Is_Tagged_Type (Full_View (Parent_Type))
12808          then
12809             Parent_Scope := Scope (T);
12810             while Present (Parent_Scope)
12811               and then Parent_Scope /= Standard_Standard
12812             loop
12813                if Parent_Scope = Scope (Parent_Type) then
12814                   Error_Msg_N
12815                     ("premature derivation from type with tagged full view",
12816                      Indic);
12817                end if;
12818
12819                Parent_Scope := Scope (Parent_Scope);
12820             end loop;
12821          end if;
12822       end if;
12823
12824       --  Check that form of derivation is appropriate
12825
12826       Taggd := Is_Tagged_Type (Parent_Type);
12827
12828       --  Perhaps the parent type should be changed to the class-wide type's
12829       --  specific type in this case to prevent cascading errors ???
12830
12831       if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
12832          Error_Msg_N ("parent type must not be a class-wide type", Indic);
12833          return;
12834       end if;
12835
12836       if Present (Extension) and then not Taggd then
12837          Error_Msg_N
12838            ("type derived from untagged type cannot have extension", Indic);
12839
12840       elsif No (Extension) and then Taggd then
12841
12842          --  If this declaration is within a private part (or body) of a
12843          --  generic instantiation then the derivation is allowed (the parent
12844          --  type can only appear tagged in this case if it's a generic actual
12845          --  type, since it would otherwise have been rejected in the analysis
12846          --  of the generic template).
12847
12848          if not Is_Generic_Actual_Type (Parent_Type)
12849            or else In_Visible_Part (Scope (Parent_Type))
12850          then
12851             Error_Msg_N
12852               ("type derived from tagged type must have extension", Indic);
12853          end if;
12854       end if;
12855
12856       --  AI-443: Synchronized formal derived types require a private
12857       --  extension. There is no point in checking the ancestor type or
12858       --  the progenitors since the construct is wrong to begin with.
12859
12860       if Ada_Version >= Ada_05
12861         and then Is_Generic_Type (T)
12862         and then Present (Original_Node (N))
12863       then
12864          declare
12865             Decl : constant Node_Id := Original_Node (N);
12866
12867          begin
12868             if Nkind (Decl) = N_Formal_Type_Declaration
12869               and then Nkind (Formal_Type_Definition (Decl)) =
12870                          N_Formal_Derived_Type_Definition
12871               and then Synchronized_Present (Formal_Type_Definition (Decl))
12872               and then No (Extension)
12873
12874                --  Avoid emitting a duplicate error message
12875
12876               and then not Error_Posted (Indic)
12877             then
12878                Error_Msg_N
12879                  ("synchronized derived type must have extension", N);
12880             end if;
12881          end;
12882       end if;
12883
12884       Build_Derived_Type (N, Parent_Type, T, Is_Completion);
12885
12886       --  AI-419: The parent type of an explicitly limited derived type must
12887       --  be a limited type or a limited interface.
12888
12889       if Limited_Present (Def) then
12890          Set_Is_Limited_Record (T);
12891
12892          if Is_Interface (T) then
12893             Set_Is_Limited_Interface (T);
12894          end if;
12895
12896          if not Is_Limited_Type (Parent_Type)
12897            and then
12898              (not Is_Interface (Parent_Type)
12899                or else not Is_Limited_Interface (Parent_Type))
12900          then
12901             Error_Msg_NE ("parent type& of limited type must be limited",
12902               N, Parent_Type);
12903          end if;
12904       end if;
12905    end Derived_Type_Declaration;
12906
12907    ------------------------
12908    -- Diagnose_Interface --
12909    ------------------------
12910
12911    procedure Diagnose_Interface (N : Node_Id;  E : Entity_Id) is
12912    begin
12913       if not Is_Interface (E)
12914         and then  E /= Any_Type
12915       then
12916          Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
12917       end if;
12918    end Diagnose_Interface;
12919
12920    ----------------------------------
12921    -- Enumeration_Type_Declaration --
12922    ----------------------------------
12923
12924    procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
12925       Ev     : Uint;
12926       L      : Node_Id;
12927       R_Node : Node_Id;
12928       B_Node : Node_Id;
12929
12930    begin
12931       --  Create identifier node representing lower bound
12932
12933       B_Node := New_Node (N_Identifier, Sloc (Def));
12934       L := First (Literals (Def));
12935       Set_Chars (B_Node, Chars (L));
12936       Set_Entity (B_Node,  L);
12937       Set_Etype (B_Node, T);
12938       Set_Is_Static_Expression (B_Node, True);
12939
12940       R_Node := New_Node (N_Range, Sloc (Def));
12941       Set_Low_Bound  (R_Node, B_Node);
12942
12943       Set_Ekind (T, E_Enumeration_Type);
12944       Set_First_Literal (T, L);
12945       Set_Etype (T, T);
12946       Set_Is_Constrained (T);
12947
12948       Ev := Uint_0;
12949
12950       --  Loop through literals of enumeration type setting pos and rep values
12951       --  except that if the Ekind is already set, then it means that the
12952       --  literal was already constructed (case of a derived type declaration
12953       --  and we should not disturb the Pos and Rep values.
12954
12955       while Present (L) loop
12956          if Ekind (L) /= E_Enumeration_Literal then
12957             Set_Ekind (L, E_Enumeration_Literal);
12958             Set_Enumeration_Pos (L, Ev);
12959             Set_Enumeration_Rep (L, Ev);
12960             Set_Is_Known_Valid  (L, True);
12961          end if;
12962
12963          Set_Etype (L, T);
12964          New_Overloaded_Entity (L);
12965          Generate_Definition (L);
12966          Set_Convention (L, Convention_Intrinsic);
12967
12968          if Nkind (L) = N_Defining_Character_Literal then
12969             Set_Is_Character_Type (T, True);
12970          end if;
12971
12972          Ev := Ev + 1;
12973          Next (L);
12974       end loop;
12975
12976       --  Now create a node representing upper bound
12977
12978       B_Node := New_Node (N_Identifier, Sloc (Def));
12979       Set_Chars (B_Node, Chars (Last (Literals (Def))));
12980       Set_Entity (B_Node,  Last (Literals (Def)));
12981       Set_Etype (B_Node, T);
12982       Set_Is_Static_Expression (B_Node, True);
12983
12984       Set_High_Bound (R_Node, B_Node);
12985
12986       --  Initialize various fields of the type. Some of this information
12987       --  may be overwritten later through rep.clauses.
12988
12989       Set_Scalar_Range    (T, R_Node);
12990       Set_RM_Size         (T, UI_From_Int (Minimum_Size (T)));
12991       Set_Enum_Esize      (T);
12992       Set_Enum_Pos_To_Rep (T, Empty);
12993
12994       --  Set Discard_Names if configuration pragma set, or if there is
12995       --  a parameterless pragma in the current declarative region
12996
12997       if Global_Discard_Names
12998         or else Discard_Names (Scope (T))
12999       then
13000          Set_Discard_Names (T);
13001       end if;
13002
13003       --  Process end label if there is one
13004
13005       if Present (Def) then
13006          Process_End_Label (Def, 'e', T);
13007       end if;
13008    end Enumeration_Type_Declaration;
13009
13010    ---------------------------------
13011    -- Expand_To_Stored_Constraint --
13012    ---------------------------------
13013
13014    function Expand_To_Stored_Constraint
13015      (Typ        : Entity_Id;
13016       Constraint : Elist_Id) return Elist_Id
13017    is
13018       Explicitly_Discriminated_Type : Entity_Id;
13019       Expansion    : Elist_Id;
13020       Discriminant : Entity_Id;
13021
13022       function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
13023       --  Find the nearest type that actually specifies discriminants
13024
13025       ---------------------------------
13026       -- Type_With_Explicit_Discrims --
13027       ---------------------------------
13028
13029       function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
13030          Typ : constant E := Base_Type (Id);
13031
13032       begin
13033          if Ekind (Typ) in Incomplete_Or_Private_Kind then
13034             if Present (Full_View (Typ)) then
13035                return Type_With_Explicit_Discrims (Full_View (Typ));
13036             end if;
13037
13038          else
13039             if Has_Discriminants (Typ) then
13040                return Typ;
13041             end if;
13042          end if;
13043
13044          if Etype (Typ) = Typ then
13045             return Empty;
13046          elsif Has_Discriminants (Typ) then
13047             return Typ;
13048          else
13049             return Type_With_Explicit_Discrims (Etype (Typ));
13050          end if;
13051
13052       end Type_With_Explicit_Discrims;
13053
13054    --  Start of processing for Expand_To_Stored_Constraint
13055
13056    begin
13057       if No (Constraint)
13058         or else Is_Empty_Elmt_List (Constraint)
13059       then
13060          return No_Elist;
13061       end if;
13062
13063       Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
13064
13065       if No (Explicitly_Discriminated_Type) then
13066          return No_Elist;
13067       end if;
13068
13069       Expansion := New_Elmt_List;
13070
13071       Discriminant :=
13072          First_Stored_Discriminant (Explicitly_Discriminated_Type);
13073       while Present (Discriminant) loop
13074          Append_Elmt (
13075            Get_Discriminant_Value (
13076              Discriminant, Explicitly_Discriminated_Type, Constraint),
13077            Expansion);
13078          Next_Stored_Discriminant (Discriminant);
13079       end loop;
13080
13081       return Expansion;
13082    end Expand_To_Stored_Constraint;
13083
13084    ---------------------------
13085    -- Find_Hidden_Interface --
13086    ---------------------------
13087
13088    function Find_Hidden_Interface
13089      (Src  : Elist_Id;
13090       Dest : Elist_Id) return Entity_Id
13091    is
13092       Iface      : Entity_Id;
13093       Iface_Elmt : Elmt_Id;
13094
13095    begin
13096       if Present (Src) and then Present (Dest) then
13097          Iface_Elmt := First_Elmt (Src);
13098          while Present (Iface_Elmt) loop
13099             Iface := Node (Iface_Elmt);
13100
13101             if Is_Interface (Iface)
13102               and then not Contain_Interface (Iface, Dest)
13103             then
13104                return Iface;
13105             end if;
13106
13107             Next_Elmt (Iface_Elmt);
13108          end loop;
13109       end if;
13110
13111       return Empty;
13112    end Find_Hidden_Interface;
13113
13114    --------------------
13115    -- Find_Type_Name --
13116    --------------------
13117
13118    function Find_Type_Name (N : Node_Id) return Entity_Id is
13119       Id       : constant Entity_Id := Defining_Identifier (N);
13120       Prev     : Entity_Id;
13121       New_Id   : Entity_Id;
13122       Prev_Par : Node_Id;
13123
13124       procedure Tag_Mismatch;
13125       --  Diagnose a tagged partial view whose full view is untagged.
13126       --  We post the message on the full view, with a reference to
13127       --  the previous partial view. The partial view can be private
13128       --  or incomplete, and these are handled in a different manner,
13129       --  so we determine the position of the error message from the
13130       --  respective slocs of both.
13131
13132       ------------------
13133       -- Tag_Mismatch --
13134       ------------------
13135
13136       procedure Tag_Mismatch is
13137       begin
13138          if Sloc (Prev) < Sloc (Id) then
13139             Error_Msg_NE
13140               ("full declaration of } must be a tagged type ", Id, Prev);
13141          else
13142             Error_Msg_NE
13143               ("full declaration of } must be a tagged type ", Prev, Id);
13144          end if;
13145       end Tag_Mismatch;
13146
13147    --  Start processing for Find_Type_Name
13148
13149    begin
13150       --  Find incomplete declaration, if one was given
13151
13152       Prev := Current_Entity_In_Scope (Id);
13153
13154       if Present (Prev) then
13155
13156          --  Previous declaration exists. Error if not incomplete/private case
13157          --  except if previous declaration is implicit, etc. Enter_Name will
13158          --  emit error if appropriate.
13159
13160          Prev_Par := Parent (Prev);
13161
13162          if not Is_Incomplete_Or_Private_Type (Prev) then
13163             Enter_Name (Id);
13164             New_Id := Id;
13165
13166          elsif not Nkind_In (N, N_Full_Type_Declaration,
13167                                 N_Task_Type_Declaration,
13168                                 N_Protected_Type_Declaration)
13169          then
13170             --  Completion must be a full type declarations (RM 7.3(4))
13171
13172             Error_Msg_Sloc := Sloc (Prev);
13173             Error_Msg_NE ("invalid completion of }", Id, Prev);
13174
13175             --  Set scope of Id to avoid cascaded errors. Entity is never
13176             --  examined again, except when saving globals in generics.
13177
13178             Set_Scope (Id, Current_Scope);
13179             New_Id := Id;
13180
13181          --  Case of full declaration of incomplete type
13182
13183          elsif Ekind (Prev) = E_Incomplete_Type then
13184
13185             --  Indicate that the incomplete declaration has a matching full
13186             --  declaration. The defining occurrence of the incomplete
13187             --  declaration remains the visible one, and the procedure
13188             --  Get_Full_View dereferences it whenever the type is used.
13189
13190             if Present (Full_View (Prev)) then
13191                Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
13192             end if;
13193
13194             Set_Full_View (Prev,  Id);
13195             Append_Entity (Id, Current_Scope);
13196             Set_Is_Public (Id, Is_Public (Prev));
13197             Set_Is_Internal (Id);
13198             New_Id := Prev;
13199
13200          --  Case of full declaration of private type
13201
13202          else
13203             if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
13204                if Etype (Prev) /= Prev then
13205
13206                   --  Prev is a private subtype or a derived type, and needs
13207                   --  no completion.
13208
13209                   Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
13210                   New_Id := Id;
13211
13212                elsif Ekind (Prev) = E_Private_Type
13213                  and then Nkind_In (N, N_Task_Type_Declaration,
13214                                        N_Protected_Type_Declaration)
13215                then
13216                   Error_Msg_N
13217                    ("completion of nonlimited type cannot be limited", N);
13218
13219                elsif Ekind (Prev) = E_Record_Type_With_Private
13220                  and then Nkind_In (N, N_Task_Type_Declaration,
13221                                        N_Protected_Type_Declaration)
13222                then
13223                   if not Is_Limited_Record (Prev) then
13224                      Error_Msg_N
13225                         ("completion of nonlimited type cannot be limited", N);
13226
13227                   elsif No (Interface_List (N)) then
13228                      Error_Msg_N
13229                         ("completion of tagged private type must be tagged",
13230                            N);
13231                   end if;
13232                end if;
13233
13234             --  Ada 2005 (AI-251): Private extension declaration of a task
13235             --  type or a protected type. This case arises when covering
13236             --  interface types.
13237
13238             elsif Nkind_In (N, N_Task_Type_Declaration,
13239                                N_Protected_Type_Declaration)
13240             then
13241                null;
13242
13243             elsif Nkind (N) /= N_Full_Type_Declaration
13244               or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
13245             then
13246                Error_Msg_N
13247                  ("full view of private extension must be an extension", N);
13248
13249             elsif not (Abstract_Present (Parent (Prev)))
13250               and then Abstract_Present (Type_Definition (N))
13251             then
13252                Error_Msg_N
13253                  ("full view of non-abstract extension cannot be abstract", N);
13254             end if;
13255
13256             if not In_Private_Part (Current_Scope) then
13257                Error_Msg_N
13258                  ("declaration of full view must appear in private part", N);
13259             end if;
13260
13261             Copy_And_Swap (Prev, Id);
13262             Set_Has_Private_Declaration (Prev);
13263             Set_Has_Private_Declaration (Id);
13264
13265             --  If no error, propagate freeze_node from private to full view.
13266             --  It may have been generated for an early operational item.
13267
13268             if Present (Freeze_Node (Id))
13269               and then Serious_Errors_Detected = 0
13270               and then No (Full_View (Id))
13271             then
13272                Set_Freeze_Node (Prev, Freeze_Node (Id));
13273                Set_Freeze_Node (Id, Empty);
13274                Set_First_Rep_Item (Prev, First_Rep_Item (Id));
13275             end if;
13276
13277             Set_Full_View (Id, Prev);
13278             New_Id := Prev;
13279          end if;
13280
13281          --  Verify that full declaration conforms to partial one
13282
13283          if Is_Incomplete_Or_Private_Type (Prev)
13284            and then Present (Discriminant_Specifications (Prev_Par))
13285          then
13286             if Present (Discriminant_Specifications (N)) then
13287                if Ekind (Prev) = E_Incomplete_Type then
13288                   Check_Discriminant_Conformance (N, Prev, Prev);
13289                else
13290                   Check_Discriminant_Conformance (N, Prev, Id);
13291                end if;
13292
13293             else
13294                Error_Msg_N
13295                  ("missing discriminants in full type declaration", N);
13296
13297                --  To avoid cascaded errors on subsequent use, share the
13298                --  discriminants of the partial view.
13299
13300                Set_Discriminant_Specifications (N,
13301                  Discriminant_Specifications (Prev_Par));
13302             end if;
13303          end if;
13304
13305          --  A prior untagged partial view can have an associated class-wide
13306          --  type due to use of the class attribute, and in this case the full
13307          --  type must also be tagged. This Ada 95 usage is deprecated in favor
13308          --  of incomplete tagged declarations, but we check for it.
13309
13310          if Is_Type (Prev)
13311            and then (Is_Tagged_Type (Prev)
13312                       or else Present (Class_Wide_Type (Prev)))
13313          then
13314             --  The full declaration is either a tagged type (including
13315             --  a synchronized type that implements interfaces) or a
13316             --  type extension, otherwise this is an error.
13317
13318             if Nkind_In (N, N_Task_Type_Declaration,
13319                             N_Protected_Type_Declaration)
13320             then
13321                if No (Interface_List (N))
13322                  and then not Error_Posted (N)
13323                then
13324                   Tag_Mismatch;
13325                end if;
13326
13327             elsif Nkind (Type_Definition (N)) = N_Record_Definition then
13328
13329                --  Indicate that the previous declaration (tagged incomplete
13330                --  or private declaration) requires the same on the full one.
13331
13332                if not Tagged_Present (Type_Definition (N)) then
13333                   Tag_Mismatch;
13334                   Set_Is_Tagged_Type (Id);
13335                   Set_Primitive_Operations (Id, New_Elmt_List);
13336                end if;
13337
13338             elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
13339                if No (Record_Extension_Part (Type_Definition (N))) then
13340                   Error_Msg_NE (
13341                     "full declaration of } must be a record extension",
13342                     Prev, Id);
13343                   Set_Is_Tagged_Type (Id);
13344                   Set_Primitive_Operations (Id, New_Elmt_List);
13345                end if;
13346
13347             else
13348                Tag_Mismatch;
13349             end if;
13350          end if;
13351
13352          return New_Id;
13353
13354       else
13355          --  New type declaration
13356
13357          Enter_Name (Id);
13358          return Id;
13359       end if;
13360    end Find_Type_Name;
13361
13362    -------------------------
13363    -- Find_Type_Of_Object --
13364    -------------------------
13365
13366    function Find_Type_Of_Object
13367      (Obj_Def     : Node_Id;
13368       Related_Nod : Node_Id) return Entity_Id
13369    is
13370       Def_Kind : constant Node_Kind := Nkind (Obj_Def);
13371       P        : Node_Id := Parent (Obj_Def);
13372       T        : Entity_Id;
13373       Nam      : Name_Id;
13374
13375    begin
13376       --  If the parent is a component_definition node we climb to the
13377       --  component_declaration node
13378
13379       if Nkind (P) = N_Component_Definition then
13380          P := Parent (P);
13381       end if;
13382
13383       --  Case of an anonymous array subtype
13384
13385       if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
13386                              N_Unconstrained_Array_Definition)
13387       then
13388          T := Empty;
13389          Array_Type_Declaration (T, Obj_Def);
13390
13391       --  Create an explicit subtype whenever possible
13392
13393       elsif Nkind (P) /= N_Component_Declaration
13394         and then Def_Kind = N_Subtype_Indication
13395       then
13396          --  Base name of subtype on object name, which will be unique in
13397          --  the current scope.
13398
13399          --  If this is a duplicate declaration, return base type, to avoid
13400          --  generating duplicate anonymous types.
13401
13402          if Error_Posted (P) then
13403             Analyze (Subtype_Mark (Obj_Def));
13404             return Entity (Subtype_Mark (Obj_Def));
13405          end if;
13406
13407          Nam :=
13408             New_External_Name
13409              (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
13410
13411          T := Make_Defining_Identifier (Sloc (P), Nam);
13412
13413          Insert_Action (Obj_Def,
13414            Make_Subtype_Declaration (Sloc (P),
13415              Defining_Identifier => T,
13416              Subtype_Indication  => Relocate_Node (Obj_Def)));
13417
13418          --  This subtype may need freezing, and this will not be done
13419          --  automatically if the object declaration is not in declarative
13420          --  part. Since this is an object declaration, the type cannot always
13421          --  be frozen here. Deferred constants do not freeze their type
13422          --  (which often enough will be private).
13423
13424          if Nkind (P) = N_Object_Declaration
13425            and then Constant_Present (P)
13426            and then No (Expression (P))
13427          then
13428             null;
13429          else
13430             Insert_Actions (Obj_Def, Freeze_Entity (T, Sloc (P)));
13431          end if;
13432
13433       --  Ada 2005 AI-406: the object definition in an object declaration
13434       --  can be an access definition.
13435
13436       elsif Def_Kind = N_Access_Definition then
13437          T := Access_Definition (Related_Nod, Obj_Def);
13438          Set_Is_Local_Anonymous_Access (T);
13439
13440       --  Otherwise, the object definition is just a subtype_mark
13441
13442       else
13443          T := Process_Subtype (Obj_Def, Related_Nod);
13444       end if;
13445
13446       return T;
13447    end Find_Type_Of_Object;
13448
13449    --------------------------------
13450    -- Find_Type_Of_Subtype_Indic --
13451    --------------------------------
13452
13453    function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
13454       Typ : Entity_Id;
13455
13456    begin
13457       --  Case of subtype mark with a constraint
13458
13459       if Nkind (S) = N_Subtype_Indication then
13460          Find_Type (Subtype_Mark (S));
13461          Typ := Entity (Subtype_Mark (S));
13462
13463          if not
13464            Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
13465          then
13466             Error_Msg_N
13467               ("incorrect constraint for this kind of type", Constraint (S));
13468             Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
13469          end if;
13470
13471       --  Otherwise we have a subtype mark without a constraint
13472
13473       elsif Error_Posted (S) then
13474          Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
13475          return Any_Type;
13476
13477       else
13478          Find_Type (S);
13479          Typ := Entity (S);
13480       end if;
13481
13482       --  Check No_Wide_Characters restriction
13483
13484       if Typ = Standard_Wide_Character
13485         or else Typ = Standard_Wide_Wide_Character
13486         or else Typ = Standard_Wide_String
13487         or else Typ = Standard_Wide_Wide_String
13488       then
13489          Check_Restriction (No_Wide_Characters, S);
13490       end if;
13491
13492       return Typ;
13493    end Find_Type_Of_Subtype_Indic;
13494
13495    -------------------------------------
13496    -- Floating_Point_Type_Declaration --
13497    -------------------------------------
13498
13499    procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
13500       Digs          : constant Node_Id := Digits_Expression (Def);
13501       Digs_Val      : Uint;
13502       Base_Typ      : Entity_Id;
13503       Implicit_Base : Entity_Id;
13504       Bound         : Node_Id;
13505
13506       function Can_Derive_From (E : Entity_Id) return Boolean;
13507       --  Find if given digits value allows derivation from specified type
13508
13509       ---------------------
13510       -- Can_Derive_From --
13511       ---------------------
13512
13513       function Can_Derive_From (E : Entity_Id) return Boolean is
13514          Spec : constant Entity_Id := Real_Range_Specification (Def);
13515
13516       begin
13517          if Digs_Val > Digits_Value (E) then
13518             return False;
13519          end if;
13520
13521          if Present (Spec) then
13522             if Expr_Value_R (Type_Low_Bound (E)) >
13523                Expr_Value_R (Low_Bound (Spec))
13524             then
13525                return False;
13526             end if;
13527
13528             if Expr_Value_R (Type_High_Bound (E)) <
13529                Expr_Value_R (High_Bound (Spec))
13530             then
13531                return False;
13532             end if;
13533          end if;
13534
13535          return True;
13536       end Can_Derive_From;
13537
13538    --  Start of processing for Floating_Point_Type_Declaration
13539
13540    begin
13541       Check_Restriction (No_Floating_Point, Def);
13542
13543       --  Create an implicit base type
13544
13545       Implicit_Base :=
13546         Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
13547
13548       --  Analyze and verify digits value
13549
13550       Analyze_And_Resolve (Digs, Any_Integer);
13551       Check_Digits_Expression (Digs);
13552       Digs_Val := Expr_Value (Digs);
13553
13554       --  Process possible range spec and find correct type to derive from
13555
13556       Process_Real_Range_Specification (Def);
13557
13558       if Can_Derive_From (Standard_Short_Float) then
13559          Base_Typ := Standard_Short_Float;
13560       elsif Can_Derive_From (Standard_Float) then
13561          Base_Typ := Standard_Float;
13562       elsif Can_Derive_From (Standard_Long_Float) then
13563          Base_Typ := Standard_Long_Float;
13564       elsif Can_Derive_From (Standard_Long_Long_Float) then
13565          Base_Typ := Standard_Long_Long_Float;
13566
13567       --  If we can't derive from any existing type, use long_long_float
13568       --  and give appropriate message explaining the problem.
13569
13570       else
13571          Base_Typ := Standard_Long_Long_Float;
13572
13573          if Digs_Val >= Digits_Value (Standard_Long_Long_Float) then
13574             Error_Msg_Uint_1 := Digits_Value (Standard_Long_Long_Float);
13575             Error_Msg_N ("digits value out of range, maximum is ^", Digs);
13576
13577          else
13578             Error_Msg_N
13579               ("range too large for any predefined type",
13580                Real_Range_Specification (Def));
13581          end if;
13582       end if;
13583
13584       --  If there are bounds given in the declaration use them as the bounds
13585       --  of the type, otherwise use the bounds of the predefined base type
13586       --  that was chosen based on the Digits value.
13587
13588       if Present (Real_Range_Specification (Def)) then
13589          Set_Scalar_Range (T, Real_Range_Specification (Def));
13590          Set_Is_Constrained (T);
13591
13592          --  The bounds of this range must be converted to machine numbers
13593          --  in accordance with RM 4.9(38).
13594
13595          Bound := Type_Low_Bound (T);
13596
13597          if Nkind (Bound) = N_Real_Literal then
13598             Set_Realval
13599               (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
13600             Set_Is_Machine_Number (Bound);
13601          end if;
13602
13603          Bound := Type_High_Bound (T);
13604
13605          if Nkind (Bound) = N_Real_Literal then
13606             Set_Realval
13607               (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
13608             Set_Is_Machine_Number (Bound);
13609          end if;
13610
13611       else
13612          Set_Scalar_Range (T, Scalar_Range (Base_Typ));
13613       end if;
13614
13615       --  Complete definition of implicit base and declared first subtype
13616
13617       Set_Etype          (Implicit_Base, Base_Typ);
13618
13619       Set_Scalar_Range   (Implicit_Base, Scalar_Range   (Base_Typ));
13620       Set_Size_Info      (Implicit_Base,                (Base_Typ));
13621       Set_RM_Size        (Implicit_Base, RM_Size        (Base_Typ));
13622       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
13623       Set_Digits_Value   (Implicit_Base, Digits_Value   (Base_Typ));
13624       Set_Vax_Float      (Implicit_Base, Vax_Float      (Base_Typ));
13625
13626       Set_Ekind          (T, E_Floating_Point_Subtype);
13627       Set_Etype          (T, Implicit_Base);
13628
13629       Set_Size_Info      (T,                (Implicit_Base));
13630       Set_RM_Size        (T, RM_Size        (Implicit_Base));
13631       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
13632       Set_Digits_Value   (T, Digs_Val);
13633    end Floating_Point_Type_Declaration;
13634
13635    ----------------------------
13636    -- Get_Discriminant_Value --
13637    ----------------------------
13638
13639    --  This is the situation:
13640
13641    --  There is a non-derived type
13642
13643    --       type T0 (Dx, Dy, Dz...)
13644
13645    --  There are zero or more levels of derivation, with each derivation
13646    --  either purely inheriting the discriminants, or defining its own.
13647
13648    --       type Ti      is new Ti-1
13649    --  or
13650    --       type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
13651    --  or
13652    --       subtype Ti is ...
13653
13654    --  The subtype issue is avoided by the use of Original_Record_Component,
13655    --  and the fact that derived subtypes also derive the constraints.
13656
13657    --  This chain leads back from
13658
13659    --       Typ_For_Constraint
13660
13661    --  Typ_For_Constraint has discriminants, and the value for each
13662    --  discriminant is given by its corresponding Elmt of Constraints.
13663
13664    --  Discriminant is some discriminant in this hierarchy
13665
13666    --  We need to return its value
13667
13668    --  We do this by recursively searching each level, and looking for
13669    --  Discriminant. Once we get to the bottom, we start backing up
13670    --  returning the value for it which may in turn be a discriminant
13671    --  further up, so on the backup we continue the substitution.
13672
13673    function Get_Discriminant_Value
13674      (Discriminant       : Entity_Id;
13675       Typ_For_Constraint : Entity_Id;
13676       Constraint         : Elist_Id) return Node_Id
13677    is
13678       function Search_Derivation_Levels
13679         (Ti                    : Entity_Id;
13680          Discrim_Values        : Elist_Id;
13681          Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
13682       --  This is the routine that performs the recursive search of levels
13683       --  as described above.
13684
13685       ------------------------------
13686       -- Search_Derivation_Levels --
13687       ------------------------------
13688
13689       function Search_Derivation_Levels
13690         (Ti                    : Entity_Id;
13691          Discrim_Values        : Elist_Id;
13692          Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
13693       is
13694          Assoc          : Elmt_Id;
13695          Disc           : Entity_Id;
13696          Result         : Node_Or_Entity_Id;
13697          Result_Entity  : Node_Id;
13698
13699       begin
13700          --  If inappropriate type, return Error, this happens only in
13701          --  cascaded error situations, and we want to avoid a blow up.
13702
13703          if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
13704             return Error;
13705          end if;
13706
13707          --  Look deeper if possible. Use Stored_Constraints only for
13708          --  untagged types. For tagged types use the given constraint.
13709          --  This asymmetry needs explanation???
13710
13711          if not Stored_Discrim_Values
13712            and then Present (Stored_Constraint (Ti))
13713            and then not Is_Tagged_Type (Ti)
13714          then
13715             Result :=
13716               Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
13717          else
13718             declare
13719                Td : constant Entity_Id := Etype (Ti);
13720
13721             begin
13722                if Td = Ti then
13723                   Result := Discriminant;
13724
13725                else
13726                   if Present (Stored_Constraint (Ti)) then
13727                      Result :=
13728                         Search_Derivation_Levels
13729                           (Td, Stored_Constraint (Ti), True);
13730                   else
13731                      Result :=
13732                         Search_Derivation_Levels
13733                           (Td, Discrim_Values, Stored_Discrim_Values);
13734                   end if;
13735                end if;
13736             end;
13737          end if;
13738
13739          --  Extra underlying places to search, if not found above. For
13740          --  concurrent types, the relevant discriminant appears in the
13741          --  corresponding record. For a type derived from a private type
13742          --  without discriminant, the full view inherits the discriminants
13743          --  of the full view of the parent.
13744
13745          if Result = Discriminant then
13746             if Is_Concurrent_Type (Ti)
13747               and then Present (Corresponding_Record_Type (Ti))
13748             then
13749                Result :=
13750                  Search_Derivation_Levels (
13751                    Corresponding_Record_Type (Ti),
13752                    Discrim_Values,
13753                    Stored_Discrim_Values);
13754
13755             elsif Is_Private_Type (Ti)
13756               and then not Has_Discriminants (Ti)
13757               and then Present (Full_View (Ti))
13758               and then Etype (Full_View (Ti)) /= Ti
13759             then
13760                Result :=
13761                  Search_Derivation_Levels (
13762                    Full_View (Ti),
13763                    Discrim_Values,
13764                    Stored_Discrim_Values);
13765             end if;
13766          end if;
13767
13768          --  If Result is not a (reference to a) discriminant, return it,
13769          --  otherwise set Result_Entity to the discriminant.
13770
13771          if Nkind (Result) = N_Defining_Identifier then
13772             pragma Assert (Result = Discriminant);
13773             Result_Entity := Result;
13774
13775          else
13776             if not Denotes_Discriminant (Result) then
13777                return Result;
13778             end if;
13779
13780             Result_Entity := Entity (Result);
13781          end if;
13782
13783          --  See if this level of derivation actually has discriminants
13784          --  because tagged derivations can add them, hence the lower
13785          --  levels need not have any.
13786
13787          if not Has_Discriminants (Ti) then
13788             return Result;
13789          end if;
13790
13791          --  Scan Ti's discriminants for Result_Entity,
13792          --  and return its corresponding value, if any.
13793
13794          Result_Entity := Original_Record_Component (Result_Entity);
13795
13796          Assoc := First_Elmt (Discrim_Values);
13797
13798          if Stored_Discrim_Values then
13799             Disc := First_Stored_Discriminant (Ti);
13800          else
13801             Disc := First_Discriminant (Ti);
13802          end if;
13803
13804          while Present (Disc) loop
13805             pragma Assert (Present (Assoc));
13806
13807             if Original_Record_Component (Disc) = Result_Entity then
13808                return Node (Assoc);
13809             end if;
13810
13811             Next_Elmt (Assoc);
13812
13813             if Stored_Discrim_Values then
13814                Next_Stored_Discriminant (Disc);
13815             else
13816                Next_Discriminant (Disc);
13817             end if;
13818          end loop;
13819
13820          --  Could not find it
13821          --
13822          return Result;
13823       end Search_Derivation_Levels;
13824
13825       --  Local Variables
13826
13827       Result : Node_Or_Entity_Id;
13828
13829    --  Start of processing for Get_Discriminant_Value
13830
13831    begin
13832       --  ??? This routine is a gigantic mess and will be deleted. For the
13833       --  time being just test for the trivial case before calling recurse.
13834
13835       if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
13836          declare
13837             D : Entity_Id;
13838             E : Elmt_Id;
13839
13840          begin
13841             D := First_Discriminant (Typ_For_Constraint);
13842             E := First_Elmt (Constraint);
13843             while Present (D) loop
13844                if Chars (D) = Chars (Discriminant) then
13845                   return Node (E);
13846                end if;
13847
13848                Next_Discriminant (D);
13849                Next_Elmt (E);
13850             end loop;
13851          end;
13852       end if;
13853
13854       Result := Search_Derivation_Levels
13855         (Typ_For_Constraint, Constraint, False);
13856
13857       --  ??? hack to disappear when this routine is gone
13858
13859       if  Nkind (Result) = N_Defining_Identifier then
13860          declare
13861             D : Entity_Id;
13862             E : Elmt_Id;
13863
13864          begin
13865             D := First_Discriminant (Typ_For_Constraint);
13866             E := First_Elmt (Constraint);
13867             while Present (D) loop
13868                if Corresponding_Discriminant (D) = Discriminant then
13869                   return Node (E);
13870                end if;
13871
13872                Next_Discriminant (D);
13873                Next_Elmt (E);
13874             end loop;
13875          end;
13876       end if;
13877
13878       pragma Assert (Nkind (Result) /= N_Defining_Identifier);
13879       return Result;
13880    end Get_Discriminant_Value;
13881
13882    --------------------------
13883    -- Has_Range_Constraint --
13884    --------------------------
13885
13886    function Has_Range_Constraint (N : Node_Id) return Boolean is
13887       C : constant Node_Id := Constraint (N);
13888
13889    begin
13890       if Nkind (C) = N_Range_Constraint then
13891          return True;
13892
13893       elsif Nkind (C) = N_Digits_Constraint then
13894          return
13895             Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
13896               or else
13897             Present (Range_Constraint (C));
13898
13899       elsif Nkind (C) = N_Delta_Constraint then
13900          return Present (Range_Constraint (C));
13901
13902       else
13903          return False;
13904       end if;
13905    end Has_Range_Constraint;
13906
13907    ------------------------
13908    -- Inherit_Components --
13909    ------------------------
13910
13911    function Inherit_Components
13912      (N             : Node_Id;
13913       Parent_Base   : Entity_Id;
13914       Derived_Base  : Entity_Id;
13915       Is_Tagged     : Boolean;
13916       Inherit_Discr : Boolean;
13917       Discs         : Elist_Id) return Elist_Id
13918    is
13919       Assoc_List : constant Elist_Id := New_Elmt_List;
13920
13921       procedure Inherit_Component
13922         (Old_C          : Entity_Id;
13923          Plain_Discrim  : Boolean := False;
13924          Stored_Discrim : Boolean := False);
13925       --  Inherits component Old_C from Parent_Base to the Derived_Base. If
13926       --  Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
13927       --  True, Old_C is a stored discriminant. If they are both false then
13928       --  Old_C is a regular component.
13929
13930       -----------------------
13931       -- Inherit_Component --
13932       -----------------------
13933
13934       procedure Inherit_Component
13935         (Old_C          : Entity_Id;
13936          Plain_Discrim  : Boolean := False;
13937          Stored_Discrim : Boolean := False)
13938       is
13939          New_C : constant Entity_Id := New_Copy (Old_C);
13940
13941          Discrim      : Entity_Id;
13942          Corr_Discrim : Entity_Id;
13943
13944       begin
13945          pragma Assert (not Is_Tagged or else not Stored_Discrim);
13946
13947          Set_Parent (New_C, Parent (Old_C));
13948
13949          --  Regular discriminants and components must be inserted in the scope
13950          --  of the Derived_Base. Do it here.
13951
13952          if not Stored_Discrim then
13953             Enter_Name (New_C);
13954          end if;
13955
13956          --  For tagged types the Original_Record_Component must point to
13957          --  whatever this field was pointing to in the parent type. This has
13958          --  already been achieved by the call to New_Copy above.
13959
13960          if not Is_Tagged then
13961             Set_Original_Record_Component (New_C, New_C);
13962          end if;
13963
13964          --  If we have inherited a component then see if its Etype contains
13965          --  references to Parent_Base discriminants. In this case, replace
13966          --  these references with the constraints given in Discs. We do not
13967          --  do this for the partial view of private types because this is
13968          --  not needed (only the components of the full view will be used
13969          --  for code generation) and cause problem. We also avoid this
13970          --  transformation in some error situations.
13971
13972          if Ekind (New_C) = E_Component then
13973             if (Is_Private_Type (Derived_Base)
13974                  and then not Is_Generic_Type (Derived_Base))
13975               or else (Is_Empty_Elmt_List (Discs)
13976                         and then  not Expander_Active)
13977             then
13978                Set_Etype (New_C, Etype (Old_C));
13979
13980             else
13981                --  The current component introduces a circularity of the
13982                --  following kind:
13983
13984                --     limited with Pack_2;
13985                --     package Pack_1 is
13986                --        type T_1 is tagged record
13987                --           Comp : access Pack_2.T_2;
13988                --           ...
13989                --        end record;
13990                --     end Pack_1;
13991
13992                --     with Pack_1;
13993                --     package Pack_2 is
13994                --        type T_2 is new Pack_1.T_1 with ...;
13995                --     end Pack_2;
13996
13997                Set_Etype
13998                  (New_C,
13999                   Constrain_Component_Type
14000                   (Old_C, Derived_Base, N, Parent_Base, Discs));
14001             end if;
14002          end if;
14003
14004          --  In derived tagged types it is illegal to reference a non
14005          --  discriminant component in the parent type. To catch this, mark
14006          --  these components with an Ekind of E_Void. This will be reset in
14007          --  Record_Type_Definition after processing the record extension of
14008          --  the derived type.
14009
14010          --  If the declaration is a private extension, there is no further
14011          --  record extension to process, and the components retain their
14012          --  current kind, because they are visible at this point.
14013
14014          if Is_Tagged and then Ekind (New_C) = E_Component
14015            and then Nkind (N) /= N_Private_Extension_Declaration
14016          then
14017             Set_Ekind (New_C, E_Void);
14018          end if;
14019
14020          if Plain_Discrim then
14021             Set_Corresponding_Discriminant (New_C, Old_C);
14022             Build_Discriminal (New_C);
14023
14024          --  If we are explicitly inheriting a stored discriminant it will be
14025          --  completely hidden.
14026
14027          elsif Stored_Discrim then
14028             Set_Corresponding_Discriminant (New_C, Empty);
14029             Set_Discriminal (New_C, Empty);
14030             Set_Is_Completely_Hidden (New_C);
14031
14032             --  Set the Original_Record_Component of each discriminant in the
14033             --  derived base to point to the corresponding stored that we just
14034             --  created.
14035
14036             Discrim := First_Discriminant (Derived_Base);
14037             while Present (Discrim) loop
14038                Corr_Discrim := Corresponding_Discriminant (Discrim);
14039
14040                --  Corr_Discrim could be missing in an error situation
14041
14042                if Present (Corr_Discrim)
14043                  and then Original_Record_Component (Corr_Discrim) = Old_C
14044                then
14045                   Set_Original_Record_Component (Discrim, New_C);
14046                end if;
14047
14048                Next_Discriminant (Discrim);
14049             end loop;
14050
14051             Append_Entity (New_C, Derived_Base);
14052          end if;
14053
14054          if not Is_Tagged then
14055             Append_Elmt (Old_C, Assoc_List);
14056             Append_Elmt (New_C, Assoc_List);
14057          end if;
14058       end Inherit_Component;
14059
14060       --  Variables local to Inherit_Component
14061
14062       Loc : constant Source_Ptr := Sloc (N);
14063
14064       Parent_Discrim : Entity_Id;
14065       Stored_Discrim : Entity_Id;
14066       D              : Entity_Id;
14067       Component      : Entity_Id;
14068
14069    --  Start of processing for Inherit_Components
14070
14071    begin
14072       if not Is_Tagged then
14073          Append_Elmt (Parent_Base,  Assoc_List);
14074          Append_Elmt (Derived_Base, Assoc_List);
14075       end if;
14076
14077       --  Inherit parent discriminants if needed
14078
14079       if Inherit_Discr then
14080          Parent_Discrim := First_Discriminant (Parent_Base);
14081          while Present (Parent_Discrim) loop
14082             Inherit_Component (Parent_Discrim, Plain_Discrim => True);
14083             Next_Discriminant (Parent_Discrim);
14084          end loop;
14085       end if;
14086
14087       --  Create explicit stored discrims for untagged types when necessary
14088
14089       if not Has_Unknown_Discriminants (Derived_Base)
14090         and then Has_Discriminants (Parent_Base)
14091         and then not Is_Tagged
14092         and then
14093           (not Inherit_Discr
14094              or else First_Discriminant (Parent_Base) /=
14095                      First_Stored_Discriminant (Parent_Base))
14096       then
14097          Stored_Discrim := First_Stored_Discriminant (Parent_Base);
14098          while Present (Stored_Discrim) loop
14099             Inherit_Component (Stored_Discrim, Stored_Discrim => True);
14100             Next_Stored_Discriminant (Stored_Discrim);
14101          end loop;
14102       end if;
14103
14104       --  See if we can apply the second transformation for derived types, as
14105       --  explained in point 6. in the comments above Build_Derived_Record_Type
14106       --  This is achieved by appending Derived_Base discriminants into Discs,
14107       --  which has the side effect of returning a non empty Discs list to the
14108       --  caller of Inherit_Components, which is what we want. This must be
14109       --  done for private derived types if there are explicit stored
14110       --  discriminants, to ensure that we can retrieve the values of the
14111       --  constraints provided in the ancestors.
14112
14113       if Inherit_Discr
14114         and then Is_Empty_Elmt_List (Discs)
14115         and then Present (First_Discriminant (Derived_Base))
14116         and then
14117           (not Is_Private_Type (Derived_Base)
14118              or else Is_Completely_Hidden
14119                (First_Stored_Discriminant (Derived_Base))
14120              or else Is_Generic_Type (Derived_Base))
14121       then
14122          D := First_Discriminant (Derived_Base);
14123          while Present (D) loop
14124             Append_Elmt (New_Reference_To (D, Loc), Discs);
14125             Next_Discriminant (D);
14126          end loop;
14127       end if;
14128
14129       --  Finally, inherit non-discriminant components unless they are not
14130       --  visible because defined or inherited from the full view of the
14131       --  parent. Don't inherit the _parent field of the parent type.
14132
14133       Component := First_Entity (Parent_Base);
14134       while Present (Component) loop
14135
14136          --  Ada 2005 (AI-251): Do not inherit components associated with
14137          --  secondary tags of the parent.
14138
14139          if Ekind (Component) = E_Component
14140            and then Present (Related_Type (Component))
14141          then
14142             null;
14143
14144          elsif Ekind (Component) /= E_Component
14145            or else Chars (Component) = Name_uParent
14146          then
14147             null;
14148
14149          --  If the derived type is within the parent type's declarative
14150          --  region, then the components can still be inherited even though
14151          --  they aren't visible at this point. This can occur for cases
14152          --  such as within public child units where the components must
14153          --  become visible upon entering the child unit's private part.
14154
14155          elsif not Is_Visible_Component (Component)
14156            and then not In_Open_Scopes (Scope (Parent_Base))
14157          then
14158             null;
14159
14160          elsif Ekind (Derived_Base) = E_Private_Type
14161            or else Ekind (Derived_Base) = E_Limited_Private_Type
14162          then
14163             null;
14164
14165          else
14166             Inherit_Component (Component);
14167          end if;
14168
14169          Next_Entity (Component);
14170       end loop;
14171
14172       --  For tagged derived types, inherited discriminants cannot be used in
14173       --  component declarations of the record extension part. To achieve this
14174       --  we mark the inherited discriminants as not visible.
14175
14176       if Is_Tagged and then Inherit_Discr then
14177          D := First_Discriminant (Derived_Base);
14178          while Present (D) loop
14179             Set_Is_Immediately_Visible (D, False);
14180             Next_Discriminant (D);
14181          end loop;
14182       end if;
14183
14184       return Assoc_List;
14185    end Inherit_Components;
14186
14187    -----------------------
14188    -- Is_Null_Extension --
14189    -----------------------
14190
14191    function Is_Null_Extension (T : Entity_Id) return Boolean is
14192       Type_Decl : constant Node_Id := Parent (T);
14193       Comp_List : Node_Id;
14194       Comp      : Node_Id;
14195
14196    begin
14197       if Nkind (Type_Decl) /= N_Full_Type_Declaration
14198         or else not Is_Tagged_Type (T)
14199         or else Nkind (Type_Definition (Type_Decl)) /=
14200                                               N_Derived_Type_Definition
14201         or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
14202       then
14203          return False;
14204       end if;
14205
14206       Comp_List :=
14207         Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
14208
14209       if Present (Discriminant_Specifications (Type_Decl)) then
14210          return False;
14211
14212       elsif Present (Comp_List)
14213         and then Is_Non_Empty_List (Component_Items (Comp_List))
14214       then
14215          Comp := First (Component_Items (Comp_List));
14216
14217          --  Only user-defined components are relevant. The component list
14218          --  may also contain a parent component and internal components
14219          --  corresponding to secondary tags, but these do not determine
14220          --  whether this is a null extension.
14221
14222          while Present (Comp) loop
14223             if Comes_From_Source (Comp) then
14224                return False;
14225             end if;
14226
14227             Next (Comp);
14228          end loop;
14229
14230          return True;
14231       else
14232          return True;
14233       end if;
14234    end Is_Null_Extension;
14235
14236    --------------------
14237    --  Is_Progenitor --
14238    --------------------
14239
14240    function Is_Progenitor
14241      (Iface : Entity_Id;
14242       Typ   : Entity_Id) return Boolean
14243    is
14244    begin
14245       return Implements_Interface (Typ, Iface,
14246                Exclude_Parents => True);
14247    end Is_Progenitor;
14248
14249    ------------------------------
14250    -- Is_Valid_Constraint_Kind --
14251    ------------------------------
14252
14253    function Is_Valid_Constraint_Kind
14254      (T_Kind          : Type_Kind;
14255       Constraint_Kind : Node_Kind) return Boolean
14256    is
14257    begin
14258       case T_Kind is
14259          when Enumeration_Kind |
14260               Integer_Kind =>
14261             return Constraint_Kind = N_Range_Constraint;
14262
14263          when Decimal_Fixed_Point_Kind =>
14264             return Nkind_In (Constraint_Kind, N_Digits_Constraint,
14265                                               N_Range_Constraint);
14266
14267          when Ordinary_Fixed_Point_Kind =>
14268             return Nkind_In (Constraint_Kind, N_Delta_Constraint,
14269                                               N_Range_Constraint);
14270
14271          when Float_Kind =>
14272             return Nkind_In (Constraint_Kind, N_Digits_Constraint,
14273                                               N_Range_Constraint);
14274
14275          when Access_Kind       |
14276               Array_Kind        |
14277               E_Record_Type     |
14278               E_Record_Subtype  |
14279               Class_Wide_Kind   |
14280               E_Incomplete_Type |
14281               Private_Kind      |
14282               Concurrent_Kind  =>
14283             return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
14284
14285          when others =>
14286             return True; -- Error will be detected later
14287       end case;
14288    end Is_Valid_Constraint_Kind;
14289
14290    --------------------------
14291    -- Is_Visible_Component --
14292    --------------------------
14293
14294    function Is_Visible_Component (C : Entity_Id) return Boolean is
14295       Original_Comp  : Entity_Id := Empty;
14296       Original_Scope : Entity_Id;
14297       Type_Scope     : Entity_Id;
14298
14299       function Is_Local_Type (Typ : Entity_Id) return Boolean;
14300       --  Check whether parent type of inherited component is declared locally,
14301       --  possibly within a nested package or instance. The current scope is
14302       --  the derived record itself.
14303
14304       -------------------
14305       -- Is_Local_Type --
14306       -------------------
14307
14308       function Is_Local_Type (Typ : Entity_Id) return Boolean is
14309          Scop : Entity_Id;
14310
14311       begin
14312          Scop := Scope (Typ);
14313          while Present (Scop)
14314            and then Scop /= Standard_Standard
14315          loop
14316             if Scop = Scope (Current_Scope) then
14317                return True;
14318             end if;
14319
14320             Scop := Scope (Scop);
14321          end loop;
14322
14323          return False;
14324       end Is_Local_Type;
14325
14326    --  Start of processing for Is_Visible_Component
14327
14328    begin
14329       if Ekind (C) = E_Component
14330         or else Ekind (C) = E_Discriminant
14331       then
14332          Original_Comp := Original_Record_Component (C);
14333       end if;
14334
14335       if No (Original_Comp) then
14336
14337          --  Premature usage, or previous error
14338
14339          return False;
14340
14341       else
14342          Original_Scope := Scope (Original_Comp);
14343          Type_Scope     := Scope (Base_Type (Scope (C)));
14344       end if;
14345
14346       --  This test only concerns tagged types
14347
14348       if not Is_Tagged_Type (Original_Scope) then
14349          return True;
14350
14351       --  If it is _Parent or _Tag, there is no visibility issue
14352
14353       elsif not Comes_From_Source (Original_Comp) then
14354          return True;
14355
14356       --  If we are in the body of an instantiation, the component is visible
14357       --  even when the parent type (possibly defined in an enclosing unit or
14358       --  in a parent unit) might not.
14359
14360       elsif In_Instance_Body then
14361          return True;
14362
14363       --  Discriminants are always visible
14364
14365       elsif Ekind (Original_Comp) = E_Discriminant
14366         and then not Has_Unknown_Discriminants (Original_Scope)
14367       then
14368          return True;
14369
14370       --  If the component has been declared in an ancestor which is currently
14371       --  a private type, then it is not visible. The same applies if the
14372       --  component's containing type is not in an open scope and the original
14373       --  component's enclosing type is a visible full view of a private type
14374       --  (which can occur in cases where an attempt is being made to reference
14375       --  a component in a sibling package that is inherited from a visible
14376       --  component of a type in an ancestor package; the component in the
14377       --  sibling package should not be visible even though the component it
14378       --  inherited from is visible). This does not apply however in the case
14379       --  where the scope of the type is a private child unit, or when the
14380       --  parent comes from a local package in which the ancestor is currently
14381       --  visible. The latter suppression of visibility is needed for cases
14382       --  that are tested in B730006.
14383
14384       elsif Is_Private_Type (Original_Scope)
14385         or else
14386           (not Is_Private_Descendant (Type_Scope)
14387             and then not In_Open_Scopes (Type_Scope)
14388             and then Has_Private_Declaration (Original_Scope))
14389       then
14390          --  If the type derives from an entity in a formal package, there
14391          --  are no additional visible components.
14392
14393          if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
14394             N_Formal_Package_Declaration
14395          then
14396             return False;
14397
14398          --  if we are not in the private part of the current package, there
14399          --  are no additional visible components.
14400
14401          elsif Ekind (Scope (Current_Scope)) = E_Package
14402            and then not In_Private_Part (Scope (Current_Scope))
14403          then
14404             return False;
14405          else
14406             return
14407               Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
14408                 and then In_Open_Scopes (Scope (Original_Scope))
14409                 and then Is_Local_Type (Type_Scope);
14410          end if;
14411
14412       --  There is another weird way in which a component may be invisible
14413       --  when the private and the full view are not derived from the same
14414       --  ancestor. Here is an example :
14415
14416       --       type A1 is tagged      record F1 : integer; end record;
14417       --       type A2 is new A1 with record F2 : integer; end record;
14418       --       type T is new A1 with private;
14419       --     private
14420       --       type T is new A2 with null record;
14421
14422       --  In this case, the full view of T inherits F1 and F2 but the private
14423       --  view inherits only F1
14424
14425       else
14426          declare
14427             Ancestor : Entity_Id := Scope (C);
14428
14429          begin
14430             loop
14431                if Ancestor = Original_Scope then
14432                   return True;
14433                elsif Ancestor = Etype (Ancestor) then
14434                   return False;
14435                end if;
14436
14437                Ancestor := Etype (Ancestor);
14438             end loop;
14439          end;
14440       end if;
14441    end Is_Visible_Component;
14442
14443    --------------------------
14444    -- Make_Class_Wide_Type --
14445    --------------------------
14446
14447    procedure Make_Class_Wide_Type (T : Entity_Id) is
14448       CW_Type : Entity_Id;
14449       CW_Name : Name_Id;
14450       Next_E  : Entity_Id;
14451
14452    begin
14453       --  The class wide type can have been defined by the partial view, in
14454       --  which case everything is already done.
14455
14456       if Present (Class_Wide_Type (T)) then
14457          return;
14458       end if;
14459
14460       CW_Type :=
14461         New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
14462
14463       --  Inherit root type characteristics
14464
14465       CW_Name := Chars (CW_Type);
14466       Next_E  := Next_Entity (CW_Type);
14467       Copy_Node (T, CW_Type);
14468       Set_Comes_From_Source (CW_Type, False);
14469       Set_Chars (CW_Type, CW_Name);
14470       Set_Parent (CW_Type, Parent (T));
14471       Set_Next_Entity (CW_Type, Next_E);
14472
14473       --  Ensure we have a new freeze node for the class-wide type. The partial
14474       --  view may have freeze action of its own, requiring a proper freeze
14475       --  node, and the same freeze node cannot be shared between the two
14476       --  types.
14477
14478       Set_Has_Delayed_Freeze (CW_Type);
14479       Set_Freeze_Node (CW_Type, Empty);
14480
14481       --  Customize the class-wide type: It has no prim. op., it cannot be
14482       --  abstract and its Etype points back to the specific root type.
14483
14484       Set_Ekind                (CW_Type, E_Class_Wide_Type);
14485       Set_Is_Tagged_Type       (CW_Type, True);
14486       Set_Primitive_Operations (CW_Type, New_Elmt_List);
14487       Set_Is_Abstract_Type     (CW_Type, False);
14488       Set_Is_Constrained       (CW_Type, False);
14489       Set_Is_First_Subtype     (CW_Type, Is_First_Subtype (T));
14490
14491       if Ekind (T) = E_Class_Wide_Subtype then
14492          Set_Etype             (CW_Type, Etype (Base_Type (T)));
14493       else
14494          Set_Etype             (CW_Type, T);
14495       end if;
14496
14497       --  If this is the class_wide type of a constrained subtype, it does
14498       --  not have discriminants.
14499
14500       Set_Has_Discriminants (CW_Type,
14501         Has_Discriminants (T) and then not Is_Constrained (T));
14502
14503       Set_Has_Unknown_Discriminants (CW_Type, True);
14504       Set_Class_Wide_Type (T, CW_Type);
14505       Set_Equivalent_Type (CW_Type, Empty);
14506
14507       --  The class-wide type of a class-wide type is itself (RM 3.9(14))
14508
14509       Set_Class_Wide_Type (CW_Type, CW_Type);
14510    end Make_Class_Wide_Type;
14511
14512    ----------------
14513    -- Make_Index --
14514    ----------------
14515
14516    procedure Make_Index
14517      (I            : Node_Id;
14518       Related_Nod  : Node_Id;
14519       Related_Id   : Entity_Id := Empty;
14520       Suffix_Index : Nat := 1)
14521    is
14522       R      : Node_Id;
14523       T      : Entity_Id;
14524       Def_Id : Entity_Id := Empty;
14525       Found  : Boolean := False;
14526
14527    begin
14528       --  For a discrete range used in a constrained array definition and
14529       --  defined by a range, an implicit conversion to the predefined type
14530       --  INTEGER is assumed if each bound is either a numeric literal, a named
14531       --  number, or an attribute, and the type of both bounds (prior to the
14532       --  implicit conversion) is the type universal_integer. Otherwise, both
14533       --  bounds must be of the same discrete type, other than universal
14534       --  integer; this type must be determinable independently of the
14535       --  context, but using the fact that the type must be discrete and that
14536       --  both bounds must have the same type.
14537
14538       --  Character literals also have a universal type in the absence of
14539       --  of additional context,  and are resolved to Standard_Character.
14540
14541       if Nkind (I) = N_Range then
14542
14543          --  The index is given by a range constraint. The bounds are known
14544          --  to be of a consistent type.
14545
14546          if not Is_Overloaded (I) then
14547             T := Etype (I);
14548
14549             --  For universal bounds, choose the specific predefined type
14550
14551             if T = Universal_Integer then
14552                T := Standard_Integer;
14553
14554             elsif T = Any_Character then
14555                Ambiguous_Character (Low_Bound (I));
14556
14557                T := Standard_Character;
14558             end if;
14559
14560          --  The node may be overloaded because some user-defined operators
14561          --  are available, but if a universal interpretation exists it is
14562          --  also the selected one.
14563
14564          elsif Universal_Interpretation (I) = Universal_Integer then
14565             T := Standard_Integer;
14566
14567          else
14568             T := Any_Type;
14569
14570             declare
14571                Ind : Interp_Index;
14572                It  : Interp;
14573
14574             begin
14575                Get_First_Interp (I, Ind, It);
14576                while Present (It.Typ) loop
14577                   if Is_Discrete_Type (It.Typ) then
14578
14579                      if Found
14580                        and then not Covers (It.Typ, T)
14581                        and then not Covers (T, It.Typ)
14582                      then
14583                         Error_Msg_N ("ambiguous bounds in discrete range", I);
14584                         exit;
14585                      else
14586                         T := It.Typ;
14587                         Found := True;
14588                      end if;
14589                   end if;
14590
14591                   Get_Next_Interp (Ind, It);
14592                end loop;
14593
14594                if T = Any_Type then
14595                   Error_Msg_N ("discrete type required for range", I);
14596                   Set_Etype (I, Any_Type);
14597                   return;
14598
14599                elsif T = Universal_Integer then
14600                   T := Standard_Integer;
14601                end if;
14602             end;
14603          end if;
14604
14605          if not Is_Discrete_Type (T) then
14606             Error_Msg_N ("discrete type required for range", I);
14607             Set_Etype (I, Any_Type);
14608             return;
14609          end if;
14610
14611          if Nkind (Low_Bound (I)) = N_Attribute_Reference
14612            and then Attribute_Name (Low_Bound (I)) = Name_First
14613            and then Is_Entity_Name (Prefix (Low_Bound (I)))
14614            and then Is_Type (Entity (Prefix (Low_Bound (I))))
14615            and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
14616          then
14617             --  The type of the index will be the type of the prefix, as long
14618             --  as the upper bound is 'Last of the same type.
14619
14620             Def_Id := Entity (Prefix (Low_Bound (I)));
14621
14622             if Nkind (High_Bound (I)) /= N_Attribute_Reference
14623               or else Attribute_Name (High_Bound (I)) /= Name_Last
14624               or else not Is_Entity_Name (Prefix (High_Bound (I)))
14625               or else Entity (Prefix (High_Bound (I))) /= Def_Id
14626             then
14627                Def_Id := Empty;
14628             end if;
14629          end if;
14630
14631          R := I;
14632          Process_Range_Expr_In_Decl (R, T);
14633
14634       elsif Nkind (I) = N_Subtype_Indication then
14635
14636          --  The index is given by a subtype with a range constraint
14637
14638          T :=  Base_Type (Entity (Subtype_Mark (I)));
14639
14640          if not Is_Discrete_Type (T) then
14641             Error_Msg_N ("discrete type required for range", I);
14642             Set_Etype (I, Any_Type);
14643             return;
14644          end if;
14645
14646          R := Range_Expression (Constraint (I));
14647
14648          Resolve (R, T);
14649          Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (I)));
14650
14651       elsif Nkind (I) = N_Attribute_Reference then
14652
14653          --  The parser guarantees that the attribute is a RANGE attribute
14654
14655          --  If the node denotes the range of a type mark, that is also the
14656          --  resulting type, and we do no need to create an Itype for it.
14657
14658          if Is_Entity_Name (Prefix (I))
14659            and then Comes_From_Source (I)
14660            and then Is_Type (Entity (Prefix (I)))
14661            and then Is_Discrete_Type (Entity (Prefix (I)))
14662          then
14663             Def_Id := Entity (Prefix (I));
14664          end if;
14665
14666          Analyze_And_Resolve (I);
14667          T := Etype (I);
14668          R := I;
14669
14670       --  If none of the above, must be a subtype. We convert this to a
14671       --  range attribute reference because in the case of declared first
14672       --  named subtypes, the types in the range reference can be different
14673       --  from the type of the entity. A range attribute normalizes the
14674       --  reference and obtains the correct types for the bounds.
14675
14676       --  This transformation is in the nature of an expansion, is only
14677       --  done if expansion is active. In particular, it is not done on
14678       --  formal generic types,  because we need to retain the name of the
14679       --  original index for instantiation purposes.
14680
14681       else
14682          if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
14683             Error_Msg_N ("invalid subtype mark in discrete range ", I);
14684             Set_Etype (I, Any_Integer);
14685             return;
14686
14687          else
14688             --  The type mark may be that of an incomplete type. It is only
14689             --  now that we can get the full view, previous analysis does
14690             --  not look specifically for a type mark.
14691
14692             Set_Entity (I, Get_Full_View (Entity (I)));
14693             Set_Etype  (I, Entity (I));
14694             Def_Id := Entity (I);
14695
14696             if not Is_Discrete_Type (Def_Id) then
14697                Error_Msg_N ("discrete type required for index", I);
14698                Set_Etype (I, Any_Type);
14699                return;
14700             end if;
14701          end if;
14702
14703          if Expander_Active then
14704             Rewrite (I,
14705               Make_Attribute_Reference (Sloc (I),
14706                 Attribute_Name => Name_Range,
14707                 Prefix         => Relocate_Node (I)));
14708
14709             --  The original was a subtype mark that does not freeze. This
14710             --  means that the rewritten version must not freeze either.
14711
14712             Set_Must_Not_Freeze (I);
14713             Set_Must_Not_Freeze (Prefix (I));
14714
14715             --  Is order critical??? if so, document why, if not
14716             --  use Analyze_And_Resolve
14717
14718             Analyze_And_Resolve (I);
14719             T := Etype (I);
14720             R := I;
14721
14722          --  If expander is inactive, type is legal, nothing else to construct
14723
14724          else
14725             return;
14726          end if;
14727       end if;
14728
14729       if not Is_Discrete_Type (T) then
14730          Error_Msg_N ("discrete type required for range", I);
14731          Set_Etype (I, Any_Type);
14732          return;
14733
14734       elsif T = Any_Type then
14735          Set_Etype (I, Any_Type);
14736          return;
14737       end if;
14738
14739       --  We will now create the appropriate Itype to describe the range, but
14740       --  first a check. If we originally had a subtype, then we just label
14741       --  the range with this subtype. Not only is there no need to construct
14742       --  a new subtype, but it is wrong to do so for two reasons:
14743
14744       --    1. A legality concern, if we have a subtype, it must not freeze,
14745       --       and the Itype would cause freezing incorrectly
14746
14747       --    2. An efficiency concern, if we created an Itype, it would not be
14748       --       recognized as the same type for the purposes of eliminating
14749       --       checks in some circumstances.
14750
14751       --  We signal this case by setting the subtype entity in Def_Id
14752
14753       if No (Def_Id) then
14754          Def_Id :=
14755            Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
14756          Set_Etype (Def_Id, Base_Type (T));
14757
14758          if Is_Signed_Integer_Type (T) then
14759             Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14760
14761          elsif Is_Modular_Integer_Type (T) then
14762             Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14763
14764          else
14765             Set_Ekind             (Def_Id, E_Enumeration_Subtype);
14766             Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14767             Set_First_Literal     (Def_Id, First_Literal (T));
14768          end if;
14769
14770          Set_Size_Info      (Def_Id,                  (T));
14771          Set_RM_Size        (Def_Id, RM_Size          (T));
14772          Set_First_Rep_Item (Def_Id, First_Rep_Item   (T));
14773
14774          Set_Scalar_Range   (Def_Id, R);
14775          Conditional_Delay  (Def_Id, T);
14776
14777          --  In the subtype indication case, if the immediate parent of the
14778          --  new subtype is non-static, then the subtype we create is non-
14779          --  static, even if its bounds are static.
14780
14781          if Nkind (I) = N_Subtype_Indication
14782            and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
14783          then
14784             Set_Is_Non_Static_Subtype (Def_Id);
14785          end if;
14786       end if;
14787
14788       --  Final step is to label the index with this constructed type
14789
14790       Set_Etype (I, Def_Id);
14791    end Make_Index;
14792
14793    ------------------------------
14794    -- Modular_Type_Declaration --
14795    ------------------------------
14796
14797    procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
14798       Mod_Expr : constant Node_Id := Expression (Def);
14799       M_Val    : Uint;
14800
14801       procedure Set_Modular_Size (Bits : Int);
14802       --  Sets RM_Size to Bits, and Esize to normal word size above this
14803
14804       ----------------------
14805       -- Set_Modular_Size --
14806       ----------------------
14807
14808       procedure Set_Modular_Size (Bits : Int) is
14809       begin
14810          Set_RM_Size (T, UI_From_Int (Bits));
14811
14812          if Bits <= 8 then
14813             Init_Esize (T, 8);
14814
14815          elsif Bits <= 16 then
14816             Init_Esize (T, 16);
14817
14818          elsif Bits <= 32 then
14819             Init_Esize (T, 32);
14820
14821          else
14822             Init_Esize (T, System_Max_Binary_Modulus_Power);
14823          end if;
14824       end Set_Modular_Size;
14825
14826    --  Start of processing for Modular_Type_Declaration
14827
14828    begin
14829       Analyze_And_Resolve (Mod_Expr, Any_Integer);
14830       Set_Etype (T, T);
14831       Set_Ekind (T, E_Modular_Integer_Type);
14832       Init_Alignment (T);
14833       Set_Is_Constrained (T);
14834
14835       if not Is_OK_Static_Expression (Mod_Expr) then
14836          Flag_Non_Static_Expr
14837            ("non-static expression used for modular type bound!", Mod_Expr);
14838          M_Val := 2 ** System_Max_Binary_Modulus_Power;
14839       else
14840          M_Val := Expr_Value (Mod_Expr);
14841       end if;
14842
14843       if M_Val < 1 then
14844          Error_Msg_N ("modulus value must be positive", Mod_Expr);
14845          M_Val := 2 ** System_Max_Binary_Modulus_Power;
14846       end if;
14847
14848       Set_Modulus (T, M_Val);
14849
14850       --   Create bounds for the modular type based on the modulus given in
14851       --   the type declaration and then analyze and resolve those bounds.
14852
14853       Set_Scalar_Range (T,
14854         Make_Range (Sloc (Mod_Expr),
14855           Low_Bound  =>
14856             Make_Integer_Literal (Sloc (Mod_Expr), 0),
14857           High_Bound =>
14858             Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
14859
14860       --  Properly analyze the literals for the range. We do this manually
14861       --  because we can't go calling Resolve, since we are resolving these
14862       --  bounds with the type, and this type is certainly not complete yet!
14863
14864       Set_Etype (Low_Bound  (Scalar_Range (T)), T);
14865       Set_Etype (High_Bound (Scalar_Range (T)), T);
14866       Set_Is_Static_Expression (Low_Bound  (Scalar_Range (T)));
14867       Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
14868
14869       --  Loop through powers of two to find number of bits required
14870
14871       for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
14872
14873          --  Binary case
14874
14875          if M_Val = 2 ** Bits then
14876             Set_Modular_Size (Bits);
14877             return;
14878
14879          --  Non-binary case
14880
14881          elsif M_Val < 2 ** Bits then
14882             Set_Non_Binary_Modulus (T);
14883
14884             if Bits > System_Max_Nonbinary_Modulus_Power then
14885                Error_Msg_Uint_1 :=
14886                  UI_From_Int (System_Max_Nonbinary_Modulus_Power);
14887                Error_Msg_F
14888                  ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
14889                Set_Modular_Size (System_Max_Binary_Modulus_Power);
14890                return;
14891
14892             else
14893                --  In the non-binary case, set size as per RM 13.3(55)
14894
14895                Set_Modular_Size (Bits);
14896                return;
14897             end if;
14898          end if;
14899
14900       end loop;
14901
14902       --  If we fall through, then the size exceed System.Max_Binary_Modulus
14903       --  so we just signal an error and set the maximum size.
14904
14905       Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
14906       Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
14907
14908       Set_Modular_Size (System_Max_Binary_Modulus_Power);
14909       Init_Alignment (T);
14910
14911    end Modular_Type_Declaration;
14912
14913    --------------------------
14914    -- New_Concatenation_Op --
14915    --------------------------
14916
14917    procedure New_Concatenation_Op (Typ : Entity_Id) is
14918       Loc : constant Source_Ptr := Sloc (Typ);
14919       Op  : Entity_Id;
14920
14921       function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
14922       --  Create abbreviated declaration for the formal of a predefined
14923       --  Operator 'Op' of type 'Typ'
14924
14925       --------------------
14926       -- Make_Op_Formal --
14927       --------------------
14928
14929       function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
14930          Formal : Entity_Id;
14931       begin
14932          Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
14933          Set_Etype (Formal, Typ);
14934          Set_Mechanism (Formal, Default_Mechanism);
14935          return Formal;
14936       end Make_Op_Formal;
14937
14938    --  Start of processing for New_Concatenation_Op
14939
14940    begin
14941       Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
14942
14943       Set_Ekind                   (Op, E_Operator);
14944       Set_Scope                   (Op, Current_Scope);
14945       Set_Etype                   (Op, Typ);
14946       Set_Homonym                 (Op, Get_Name_Entity_Id (Name_Op_Concat));
14947       Set_Is_Immediately_Visible  (Op);
14948       Set_Is_Intrinsic_Subprogram (Op);
14949       Set_Has_Completion          (Op);
14950       Append_Entity               (Op, Current_Scope);
14951
14952       Set_Name_Entity_Id (Name_Op_Concat, Op);
14953
14954       Append_Entity (Make_Op_Formal (Typ, Op), Op);
14955       Append_Entity (Make_Op_Formal (Typ, Op), Op);
14956    end New_Concatenation_Op;
14957
14958    -------------------------
14959    -- OK_For_Limited_Init --
14960    -------------------------
14961
14962    --  ???Check all calls of this, and compare the conditions under which it's
14963    --  called.
14964
14965    function OK_For_Limited_Init (Exp : Node_Id) return Boolean is
14966    begin
14967       return Ada_Version >= Ada_05
14968         and then not Debug_Flag_Dot_L
14969         and then OK_For_Limited_Init_In_05 (Exp);
14970    end OK_For_Limited_Init;
14971
14972    -------------------------------
14973    -- OK_For_Limited_Init_In_05 --
14974    -------------------------------
14975
14976    function OK_For_Limited_Init_In_05 (Exp : Node_Id) return Boolean is
14977    begin
14978       --  Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
14979       --  case of limited aggregates (including extension aggregates), and
14980       --  function calls. The function call may have been give in prefixed
14981       --  notation, in which case the original node is an indexed component.
14982
14983       case Nkind (Original_Node (Exp)) is
14984          when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
14985             return True;
14986
14987          when N_Qualified_Expression =>
14988             return
14989               OK_For_Limited_Init_In_05 (Expression (Original_Node (Exp)));
14990
14991          --  Ada 2005 (AI-251): If a class-wide interface object is initialized
14992          --  with a function call, the expander has rewritten the call into an
14993          --  N_Type_Conversion node to force displacement of the pointer to
14994          --  reference the component containing the secondary dispatch table.
14995          --  Otherwise a type conversion is not a legal context.
14996
14997          when N_Type_Conversion =>
14998             return not Comes_From_Source (Exp)
14999               and then
15000                 OK_For_Limited_Init_In_05 (Expression (Original_Node (Exp)));
15001
15002          when N_Indexed_Component | N_Selected_Component  =>
15003             return Nkind (Exp) = N_Function_Call;
15004
15005          --  A use of 'Input is a function call, hence allowed. Normally the
15006          --  attribute will be changed to a call, but the attribute by itself
15007          --  can occur with -gnatc.
15008
15009          when N_Attribute_Reference =>
15010             return Attribute_Name (Original_Node (Exp)) = Name_Input;
15011
15012          when others =>
15013             return False;
15014       end case;
15015    end OK_For_Limited_Init_In_05;
15016
15017    -------------------------------------------
15018    -- Ordinary_Fixed_Point_Type_Declaration --
15019    -------------------------------------------
15020
15021    procedure Ordinary_Fixed_Point_Type_Declaration
15022      (T   : Entity_Id;
15023       Def : Node_Id)
15024    is
15025       Loc           : constant Source_Ptr := Sloc (Def);
15026       Delta_Expr    : constant Node_Id    := Delta_Expression (Def);
15027       RRS           : constant Node_Id    := Real_Range_Specification (Def);
15028       Implicit_Base : Entity_Id;
15029       Delta_Val     : Ureal;
15030       Small_Val     : Ureal;
15031       Low_Val       : Ureal;
15032       High_Val      : Ureal;
15033
15034    begin
15035       Check_Restriction (No_Fixed_Point, Def);
15036
15037       --  Create implicit base type
15038
15039       Implicit_Base :=
15040         Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
15041       Set_Etype (Implicit_Base, Implicit_Base);
15042
15043       --  Analyze and process delta expression
15044
15045       Analyze_And_Resolve (Delta_Expr, Any_Real);
15046
15047       Check_Delta_Expression (Delta_Expr);
15048       Delta_Val := Expr_Value_R (Delta_Expr);
15049
15050       Set_Delta_Value (Implicit_Base, Delta_Val);
15051
15052       --  Compute default small from given delta, which is the largest power
15053       --  of two that does not exceed the given delta value.
15054
15055       declare
15056          Tmp   : Ureal;
15057          Scale : Int;
15058
15059       begin
15060          Tmp := Ureal_1;
15061          Scale := 0;
15062
15063          if Delta_Val < Ureal_1 then
15064             while Delta_Val < Tmp loop
15065                Tmp := Tmp / Ureal_2;
15066                Scale := Scale + 1;
15067             end loop;
15068
15069          else
15070             loop
15071                Tmp := Tmp * Ureal_2;
15072                exit when Tmp > Delta_Val;
15073                Scale := Scale - 1;
15074             end loop;
15075          end if;
15076
15077          Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
15078       end;
15079
15080       Set_Small_Value (Implicit_Base, Small_Val);
15081
15082       --  If no range was given, set a dummy range
15083
15084       if RRS <= Empty_Or_Error then
15085          Low_Val  := -Small_Val;
15086          High_Val := Small_Val;
15087
15088       --  Otherwise analyze and process given range
15089
15090       else
15091          declare
15092             Low  : constant Node_Id := Low_Bound  (RRS);
15093             High : constant Node_Id := High_Bound (RRS);
15094
15095          begin
15096             Analyze_And_Resolve (Low, Any_Real);
15097             Analyze_And_Resolve (High, Any_Real);
15098             Check_Real_Bound (Low);
15099             Check_Real_Bound (High);
15100
15101             --  Obtain and set the range
15102
15103             Low_Val  := Expr_Value_R (Low);
15104             High_Val := Expr_Value_R (High);
15105
15106             if Low_Val > High_Val then
15107                Error_Msg_NE ("?fixed point type& has null range", Def, T);
15108             end if;
15109          end;
15110       end if;
15111
15112       --  The range for both the implicit base and the declared first subtype
15113       --  cannot be set yet, so we use the special routine Set_Fixed_Range to
15114       --  set a temporary range in place. Note that the bounds of the base
15115       --  type will be widened to be symmetrical and to fill the available
15116       --  bits when the type is frozen.
15117
15118       --  We could do this with all discrete types, and probably should, but
15119       --  we absolutely have to do it for fixed-point, since the end-points
15120       --  of the range and the size are determined by the small value, which
15121       --  could be reset before the freeze point.
15122
15123       Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
15124       Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15125
15126       --  Complete definition of first subtype
15127
15128       Set_Ekind          (T, E_Ordinary_Fixed_Point_Subtype);
15129       Set_Etype          (T, Implicit_Base);
15130       Init_Size_Align    (T);
15131       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
15132       Set_Small_Value    (T, Small_Val);
15133       Set_Delta_Value    (T, Delta_Val);
15134       Set_Is_Constrained (T);
15135
15136    end Ordinary_Fixed_Point_Type_Declaration;
15137
15138    ----------------------------------------
15139    -- Prepare_Private_Subtype_Completion --
15140    ----------------------------------------
15141
15142    procedure Prepare_Private_Subtype_Completion
15143      (Id          : Entity_Id;
15144       Related_Nod : Node_Id)
15145    is
15146       Id_B   : constant Entity_Id := Base_Type (Id);
15147       Full_B : constant Entity_Id := Full_View (Id_B);
15148       Full   : Entity_Id;
15149
15150    begin
15151       if Present (Full_B) then
15152
15153          --  The Base_Type is already completed, we can complete the subtype
15154          --  now. We have to create a new entity with the same name, Thus we
15155          --  can't use Create_Itype.
15156
15157          --  This is messy, should be fixed ???
15158
15159          Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
15160          Set_Is_Itype (Full);
15161          Set_Associated_Node_For_Itype (Full, Related_Nod);
15162          Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
15163       end if;
15164
15165       --  The parent subtype may be private, but the base might not, in some
15166       --  nested instances. In that case, the subtype does not need to be
15167       --  exchanged. It would still be nice to make private subtypes and their
15168       --  bases consistent at all times ???
15169
15170       if Is_Private_Type (Id_B) then
15171          Append_Elmt (Id, Private_Dependents (Id_B));
15172       end if;
15173
15174    end Prepare_Private_Subtype_Completion;
15175
15176    ---------------------------
15177    -- Process_Discriminants --
15178    ---------------------------
15179
15180    procedure Process_Discriminants
15181      (N    : Node_Id;
15182       Prev : Entity_Id := Empty)
15183    is
15184       Elist               : constant Elist_Id := New_Elmt_List;
15185       Id                  : Node_Id;
15186       Discr               : Node_Id;
15187       Discr_Number        : Uint;
15188       Discr_Type          : Entity_Id;
15189       Default_Present     : Boolean := False;
15190       Default_Not_Present : Boolean := False;
15191
15192    begin
15193       --  A composite type other than an array type can have discriminants.
15194       --  On entry, the current scope is the composite type.
15195
15196       --  The discriminants are initially entered into the scope of the type
15197       --  via Enter_Name with the default Ekind of E_Void to prevent premature
15198       --  use, as explained at the end of this procedure.
15199
15200       Discr := First (Discriminant_Specifications (N));
15201       while Present (Discr) loop
15202          Enter_Name (Defining_Identifier (Discr));
15203
15204          --  For navigation purposes we add a reference to the discriminant
15205          --  in the entity for the type. If the current declaration is a
15206          --  completion, place references on the partial view. Otherwise the
15207          --  type is the current scope.
15208
15209          if Present (Prev) then
15210
15211             --  The references go on the partial view, if present. If the
15212             --  partial view has discriminants, the references have been
15213             --  generated already.
15214
15215             if not Has_Discriminants (Prev) then
15216                Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
15217             end if;
15218          else
15219             Generate_Reference
15220               (Current_Scope, Defining_Identifier (Discr), 'd');
15221          end if;
15222
15223          if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
15224             Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
15225
15226             --  Ada 2005 (AI-254)
15227
15228             if Present (Access_To_Subprogram_Definition
15229                          (Discriminant_Type (Discr)))
15230               and then Protected_Present (Access_To_Subprogram_Definition
15231                                            (Discriminant_Type (Discr)))
15232             then
15233                Discr_Type :=
15234                  Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
15235             end if;
15236
15237          else
15238             Find_Type (Discriminant_Type (Discr));
15239             Discr_Type := Etype (Discriminant_Type (Discr));
15240
15241             if Error_Posted (Discriminant_Type (Discr)) then
15242                Discr_Type := Any_Type;
15243             end if;
15244          end if;
15245
15246          if Is_Access_Type (Discr_Type) then
15247
15248             --  Ada 2005 (AI-230): Access discriminant allowed in non-limited
15249             --  record types
15250
15251             if Ada_Version < Ada_05 then
15252                Check_Access_Discriminant_Requires_Limited
15253                  (Discr, Discriminant_Type (Discr));
15254             end if;
15255
15256             if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
15257                Error_Msg_N
15258                  ("(Ada 83) access discriminant not allowed", Discr);
15259             end if;
15260
15261          elsif not Is_Discrete_Type (Discr_Type) then
15262             Error_Msg_N ("discriminants must have a discrete or access type",
15263               Discriminant_Type (Discr));
15264          end if;
15265
15266          Set_Etype (Defining_Identifier (Discr), Discr_Type);
15267
15268          --  If a discriminant specification includes the assignment compound
15269          --  delimiter followed by an expression, the expression is the default
15270          --  expression of the discriminant; the default expression must be of
15271          --  the type of the discriminant. (RM 3.7.1) Since this expression is
15272          --  a default expression, we do the special preanalysis, since this
15273          --  expression does not freeze (see "Handling of Default and Per-
15274          --  Object Expressions" in spec of package Sem).
15275
15276          if Present (Expression (Discr)) then
15277             Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
15278
15279             if Nkind (N) = N_Formal_Type_Declaration then
15280                Error_Msg_N
15281                  ("discriminant defaults not allowed for formal type",
15282                   Expression (Discr));
15283
15284             --  Tagged types cannot have defaulted discriminants, but a
15285             --  non-tagged private type with defaulted discriminants
15286             --   can have a tagged completion.
15287
15288             elsif Is_Tagged_Type (Current_Scope)
15289               and then Comes_From_Source (N)
15290             then
15291                Error_Msg_N
15292                  ("discriminants of tagged type cannot have defaults",
15293                   Expression (Discr));
15294
15295             else
15296                Default_Present := True;
15297                Append_Elmt (Expression (Discr), Elist);
15298
15299                --  Tag the defining identifiers for the discriminants with
15300                --  their corresponding default expressions from the tree.
15301
15302                Set_Discriminant_Default_Value
15303                  (Defining_Identifier (Discr), Expression (Discr));
15304             end if;
15305
15306          else
15307             Default_Not_Present := True;
15308          end if;
15309
15310          --  Ada 2005 (AI-231): Create an Itype that is a duplicate of
15311          --  Discr_Type but with the null-exclusion attribute
15312
15313          if Ada_Version >= Ada_05 then
15314
15315             --  Ada 2005 (AI-231): Static checks
15316
15317             if Can_Never_Be_Null (Discr_Type) then
15318                Null_Exclusion_Static_Checks (Discr);
15319
15320             elsif Is_Access_Type (Discr_Type)
15321               and then Null_Exclusion_Present (Discr)
15322
15323                --  No need to check itypes because in their case this check
15324                --  was done at their point of creation
15325
15326               and then not Is_Itype (Discr_Type)
15327             then
15328                if Can_Never_Be_Null (Discr_Type) then
15329                   Error_Msg_NE
15330                     ("`NOT NULL` not allowed (& already excludes null)",
15331                      Discr,
15332                      Discr_Type);
15333                end if;
15334
15335                Set_Etype (Defining_Identifier (Discr),
15336                  Create_Null_Excluding_Itype
15337                    (T           => Discr_Type,
15338                     Related_Nod => Discr));
15339             end if;
15340
15341             --  Ada 2005 (AI-402): access discriminants of nonlimited types
15342             --  can't have defaults. Synchronized types, or types that are
15343             --  explicitly limited are fine, but special tests apply to derived
15344             --  types in generics: in a generic body we have to assume the
15345             --  worst, and therefore defaults are not allowed if the parent is
15346             --  a generic formal private type (see ACATS B370001).
15347
15348             if Is_Access_Type (Discr_Type) then
15349                if Ekind (Discr_Type) /= E_Anonymous_Access_Type
15350                  or else not Default_Present
15351                  or else Is_Limited_Record (Current_Scope)
15352                  or else Is_Concurrent_Type (Current_Scope)
15353                  or else Is_Concurrent_Record_Type (Current_Scope)
15354                  or else Ekind (Current_Scope) = E_Limited_Private_Type
15355                then
15356                   if not Is_Derived_Type (Current_Scope)
15357                     or else not Is_Generic_Type (Etype (Current_Scope))
15358                     or else not In_Package_Body (Scope (Etype (Current_Scope)))
15359                     or else Limited_Present
15360                               (Type_Definition (Parent (Current_Scope)))
15361                   then
15362                      null;
15363
15364                   else
15365                      Error_Msg_N ("access discriminants of nonlimited types",
15366                          Expression (Discr));
15367                      Error_Msg_N ("\cannot have defaults", Expression (Discr));
15368                   end if;
15369
15370                elsif Present (Expression (Discr)) then
15371                   Error_Msg_N
15372                     ("(Ada 2005) access discriminants of nonlimited types",
15373                      Expression (Discr));
15374                   Error_Msg_N ("\cannot have defaults", Expression (Discr));
15375                end if;
15376             end if;
15377          end if;
15378
15379          Next (Discr);
15380       end loop;
15381
15382       --  An element list consisting of the default expressions of the
15383       --  discriminants is constructed in the above loop and used to set
15384       --  the Discriminant_Constraint attribute for the type. If an object
15385       --  is declared of this (record or task) type without any explicit
15386       --  discriminant constraint given, this element list will form the
15387       --  actual parameters for the corresponding initialization procedure
15388       --  for the type.
15389
15390       Set_Discriminant_Constraint (Current_Scope, Elist);
15391       Set_Stored_Constraint (Current_Scope, No_Elist);
15392
15393       --  Default expressions must be provided either for all or for none
15394       --  of the discriminants of a discriminant part. (RM 3.7.1)
15395
15396       if Default_Present and then Default_Not_Present then
15397          Error_Msg_N
15398            ("incomplete specification of defaults for discriminants", N);
15399       end if;
15400
15401       --  The use of the name of a discriminant is not allowed in default
15402       --  expressions of a discriminant part if the specification of the
15403       --  discriminant is itself given in the discriminant part. (RM 3.7.1)
15404
15405       --  To detect this, the discriminant names are entered initially with an
15406       --  Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
15407       --  attempt to use a void entity (for example in an expression that is
15408       --  type-checked) produces the error message: premature usage. Now after
15409       --  completing the semantic analysis of the discriminant part, we can set
15410       --  the Ekind of all the discriminants appropriately.
15411
15412       Discr := First (Discriminant_Specifications (N));
15413       Discr_Number := Uint_1;
15414       while Present (Discr) loop
15415          Id := Defining_Identifier (Discr);
15416          Set_Ekind (Id, E_Discriminant);
15417          Init_Component_Location (Id);
15418          Init_Esize (Id);
15419          Set_Discriminant_Number (Id, Discr_Number);
15420
15421          --  Make sure this is always set, even in illegal programs
15422
15423          Set_Corresponding_Discriminant (Id, Empty);
15424
15425          --  Initialize the Original_Record_Component to the entity itself.
15426          --  Inherit_Components will propagate the right value to
15427          --  discriminants in derived record types.
15428
15429          Set_Original_Record_Component (Id, Id);
15430
15431          --  Create the discriminal for the discriminant
15432
15433          Build_Discriminal (Id);
15434
15435          Next (Discr);
15436          Discr_Number := Discr_Number + 1;
15437       end loop;
15438
15439       Set_Has_Discriminants (Current_Scope);
15440    end Process_Discriminants;
15441
15442    -----------------------
15443    -- Process_Full_View --
15444    -----------------------
15445
15446    procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
15447       Priv_Parent : Entity_Id;
15448       Full_Parent : Entity_Id;
15449       Full_Indic  : Node_Id;
15450
15451       procedure Collect_Implemented_Interfaces
15452         (Typ    : Entity_Id;
15453          Ifaces : Elist_Id);
15454       --  Ada 2005: Gather all the interfaces that Typ directly or
15455       --  inherently implements. Duplicate entries are not added to
15456       --  the list Ifaces.
15457
15458       ------------------------------------
15459       -- Collect_Implemented_Interfaces --
15460       ------------------------------------
15461
15462       procedure Collect_Implemented_Interfaces
15463         (Typ    : Entity_Id;
15464          Ifaces : Elist_Id)
15465       is
15466          Iface      : Entity_Id;
15467          Iface_Elmt : Elmt_Id;
15468
15469       begin
15470          --  Abstract interfaces are only associated with tagged record types
15471
15472          if not Is_Tagged_Type (Typ)
15473            or else not Is_Record_Type (Typ)
15474          then
15475             return;
15476          end if;
15477
15478          --  Recursively climb to the ancestors
15479
15480          if Etype (Typ) /= Typ
15481
15482             --  Protect the frontend against wrong cyclic declarations like:
15483
15484             --     type B is new A with private;
15485             --     type C is new A with private;
15486             --  private
15487             --     type B is new C with null record;
15488             --     type C is new B with null record;
15489
15490            and then Etype (Typ) /= Priv_T
15491            and then Etype (Typ) /= Full_T
15492          then
15493             --  Keep separate the management of private type declarations
15494
15495             if Ekind (Typ) = E_Record_Type_With_Private then
15496
15497                --  Handle the following erronous case:
15498                --      type Private_Type is tagged private;
15499                --   private
15500                --      type Private_Type is new Type_Implementing_Iface;
15501
15502                if Present (Full_View (Typ))
15503                  and then Etype (Typ) /= Full_View (Typ)
15504                then
15505                   if Is_Interface (Etype (Typ)) then
15506                      Append_Unique_Elmt (Etype (Typ), Ifaces);
15507                   end if;
15508
15509                   Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
15510                end if;
15511
15512             --  Non-private types
15513
15514             else
15515                if Is_Interface (Etype (Typ)) then
15516                   Append_Unique_Elmt (Etype (Typ), Ifaces);
15517                end if;
15518
15519                Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
15520             end if;
15521          end if;
15522
15523          --  Handle entities in the list of abstract interfaces
15524
15525          if Present (Interfaces (Typ)) then
15526             Iface_Elmt := First_Elmt (Interfaces (Typ));
15527             while Present (Iface_Elmt) loop
15528                Iface := Node (Iface_Elmt);
15529
15530                pragma Assert (Is_Interface (Iface));
15531
15532                if not Contain_Interface (Iface, Ifaces) then
15533                   Append_Elmt (Iface, Ifaces);
15534                   Collect_Implemented_Interfaces (Iface, Ifaces);
15535                end if;
15536
15537                Next_Elmt (Iface_Elmt);
15538             end loop;
15539          end if;
15540       end Collect_Implemented_Interfaces;
15541
15542    --  Start of processing for Process_Full_View
15543
15544    begin
15545       --  First some sanity checks that must be done after semantic
15546       --  decoration of the full view and thus cannot be placed with other
15547       --  similar checks in Find_Type_Name
15548
15549       if not Is_Limited_Type (Priv_T)
15550         and then (Is_Limited_Type (Full_T)
15551                    or else Is_Limited_Composite (Full_T))
15552       then
15553          Error_Msg_N
15554            ("completion of nonlimited type cannot be limited", Full_T);
15555          Explain_Limited_Type (Full_T, Full_T);
15556
15557       elsif Is_Abstract_Type (Full_T)
15558         and then not Is_Abstract_Type (Priv_T)
15559       then
15560          Error_Msg_N
15561            ("completion of nonabstract type cannot be abstract", Full_T);
15562
15563       elsif Is_Tagged_Type (Priv_T)
15564         and then Is_Limited_Type (Priv_T)
15565         and then not Is_Limited_Type (Full_T)
15566       then
15567          --  If pragma CPP_Class was applied to the private declaration
15568          --  propagate the limitedness to the full-view
15569
15570          if Is_CPP_Class (Priv_T) then
15571             Set_Is_Limited_Record (Full_T);
15572
15573          --  GNAT allow its own definition of Limited_Controlled to disobey
15574          --  this rule in order in ease the implementation. The next test is
15575          --  safe because Root_Controlled is defined in a private system child
15576
15577          elsif Etype (Full_T) = Full_View (RTE (RE_Root_Controlled)) then
15578             Set_Is_Limited_Composite (Full_T);
15579          else
15580             Error_Msg_N
15581               ("completion of limited tagged type must be limited", Full_T);
15582          end if;
15583
15584       elsif Is_Generic_Type (Priv_T) then
15585          Error_Msg_N ("generic type cannot have a completion", Full_T);
15586       end if;
15587
15588       --  Check that ancestor interfaces of private and full views are
15589       --  consistent. We omit this check for synchronized types because
15590       --  they are performed on the corresponding record type when frozen.
15591
15592       if Ada_Version >= Ada_05
15593         and then Is_Tagged_Type (Priv_T)
15594         and then Is_Tagged_Type (Full_T)
15595         and then not Is_Concurrent_Type (Full_T)
15596       then
15597          declare
15598             Iface         : Entity_Id;
15599             Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
15600             Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
15601
15602          begin
15603             Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
15604             Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
15605
15606             --  Ada 2005 (AI-251): The partial view shall be a descendant of
15607             --  an interface type if and only if the full type is descendant
15608             --  of the interface type (AARM 7.3 (7.3/2).
15609
15610             Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
15611
15612             if Present (Iface) then
15613                Error_Msg_NE ("interface & not implemented by full type " &
15614                              "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
15615             end if;
15616
15617             Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
15618
15619             if Present (Iface) then
15620                Error_Msg_NE ("interface & not implemented by partial view " &
15621                              "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
15622             end if;
15623          end;
15624       end if;
15625
15626       if Is_Tagged_Type (Priv_T)
15627         and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15628         and then Is_Derived_Type (Full_T)
15629       then
15630          Priv_Parent := Etype (Priv_T);
15631
15632          --  The full view of a private extension may have been transformed
15633          --  into an unconstrained derived type declaration and a subtype
15634          --  declaration (see build_derived_record_type for details).
15635
15636          if Nkind (N) = N_Subtype_Declaration then
15637             Full_Indic  := Subtype_Indication (N);
15638             Full_Parent := Etype (Base_Type (Full_T));
15639          else
15640             Full_Indic  := Subtype_Indication (Type_Definition (N));
15641             Full_Parent := Etype (Full_T);
15642          end if;
15643
15644          --  Check that the parent type of the full type is a descendant of
15645          --  the ancestor subtype given in the private extension. If either
15646          --  entity has an Etype equal to Any_Type then we had some previous
15647          --  error situation [7.3(8)].
15648
15649          if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
15650             return;
15651
15652          --  Ada 2005 (AI-251): Interfaces in the full-typ can be given in
15653          --  any order. Therefore we don't have to check that its parent must
15654          --  be a descendant of the parent of the private type declaration.
15655
15656          elsif Is_Interface (Priv_Parent)
15657            and then Is_Interface (Full_Parent)
15658          then
15659             null;
15660
15661          --  Ada 2005 (AI-251): If the parent of the private type declaration
15662          --  is an interface there is no need to check that it is an ancestor
15663          --  of the associated full type declaration. The required tests for
15664          --  this case case are performed by Build_Derived_Record_Type.
15665
15666          elsif not Is_Interface (Base_Type (Priv_Parent))
15667            and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
15668          then
15669             Error_Msg_N
15670               ("parent of full type must descend from parent"
15671                   & " of private extension", Full_Indic);
15672
15673          --  Check the rules of 7.3(10): if the private extension inherits
15674          --  known discriminants, then the full type must also inherit those
15675          --  discriminants from the same (ancestor) type, and the parent
15676          --  subtype of the full type must be constrained if and only if
15677          --  the ancestor subtype of the private extension is constrained.
15678
15679          elsif No (Discriminant_Specifications (Parent (Priv_T)))
15680            and then not Has_Unknown_Discriminants (Priv_T)
15681            and then Has_Discriminants (Base_Type (Priv_Parent))
15682          then
15683             declare
15684                Priv_Indic  : constant Node_Id :=
15685                                Subtype_Indication (Parent (Priv_T));
15686
15687                Priv_Constr : constant Boolean :=
15688                                Is_Constrained (Priv_Parent)
15689                                  or else
15690                                    Nkind (Priv_Indic) = N_Subtype_Indication
15691                                  or else Is_Constrained (Entity (Priv_Indic));
15692
15693                Full_Constr : constant Boolean :=
15694                                Is_Constrained (Full_Parent)
15695                                  or else
15696                                    Nkind (Full_Indic) = N_Subtype_Indication
15697                                  or else Is_Constrained (Entity (Full_Indic));
15698
15699                Priv_Discr : Entity_Id;
15700                Full_Discr : Entity_Id;
15701
15702             begin
15703                Priv_Discr := First_Discriminant (Priv_Parent);
15704                Full_Discr := First_Discriminant (Full_Parent);
15705                while Present (Priv_Discr) and then Present (Full_Discr) loop
15706                   if Original_Record_Component (Priv_Discr) =
15707                      Original_Record_Component (Full_Discr)
15708                     or else
15709                      Corresponding_Discriminant (Priv_Discr) =
15710                      Corresponding_Discriminant (Full_Discr)
15711                   then
15712                      null;
15713                   else
15714                      exit;
15715                   end if;
15716
15717                   Next_Discriminant (Priv_Discr);
15718                   Next_Discriminant (Full_Discr);
15719                end loop;
15720
15721                if Present (Priv_Discr) or else Present (Full_Discr) then
15722                   Error_Msg_N
15723                     ("full view must inherit discriminants of the parent type"
15724                      & " used in the private extension", Full_Indic);
15725
15726                elsif Priv_Constr and then not Full_Constr then
15727                   Error_Msg_N
15728                     ("parent subtype of full type must be constrained",
15729                      Full_Indic);
15730
15731                elsif Full_Constr and then not Priv_Constr then
15732                   Error_Msg_N
15733                     ("parent subtype of full type must be unconstrained",
15734                      Full_Indic);
15735                end if;
15736             end;
15737
15738          --  Check the rules of 7.3(12): if a partial view has neither known
15739          --  or unknown discriminants, then the full type declaration shall
15740          --  define a definite subtype.
15741
15742          elsif      not Has_Unknown_Discriminants (Priv_T)
15743            and then not Has_Discriminants (Priv_T)
15744            and then not Is_Constrained (Full_T)
15745          then
15746             Error_Msg_N
15747               ("full view must define a constrained type if partial view"
15748                 & " has no discriminants", Full_T);
15749          end if;
15750
15751          --  ??????? Do we implement the following properly ?????
15752          --  If the ancestor subtype of a private extension has constrained
15753          --  discriminants, then the parent subtype of the full view shall
15754          --  impose a statically matching constraint on those discriminants
15755          --  [7.3(13)].
15756
15757       else
15758          --  For untagged types, verify that a type without discriminants
15759          --  is not completed with an unconstrained type.
15760
15761          if not Is_Indefinite_Subtype (Priv_T)
15762            and then Is_Indefinite_Subtype (Full_T)
15763          then
15764             Error_Msg_N ("full view of type must be definite subtype", Full_T);
15765          end if;
15766       end if;
15767
15768       --  AI-419: verify that the use of "limited" is consistent
15769
15770       declare
15771          Orig_Decl : constant Node_Id := Original_Node (N);
15772
15773       begin
15774          if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15775            and then not Limited_Present (Parent (Priv_T))
15776            and then not Synchronized_Present (Parent (Priv_T))
15777            and then Nkind (Orig_Decl) = N_Full_Type_Declaration
15778            and then Nkind
15779              (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
15780            and then Limited_Present (Type_Definition (Orig_Decl))
15781          then
15782             Error_Msg_N
15783               ("full view of non-limited extension cannot be limited", N);
15784          end if;
15785       end;
15786
15787       --  Ada 2005 (AI-443): A synchronized private extension must be
15788       --  completed by a task or protected type.
15789
15790       if Ada_Version >= Ada_05
15791         and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15792         and then Synchronized_Present (Parent (Priv_T))
15793         and then not Is_Concurrent_Type (Full_T)
15794       then
15795          Error_Msg_N ("full view of synchronized extension must " &
15796                       "be synchronized type", N);
15797       end if;
15798
15799       --  Ada 2005 AI-363: if the full view has discriminants with
15800       --  defaults, it is illegal to declare constrained access subtypes
15801       --  whose designated type is the current type. This allows objects
15802       --  of the type that are declared in the heap to be unconstrained.
15803
15804       if not Has_Unknown_Discriminants (Priv_T)
15805         and then not Has_Discriminants (Priv_T)
15806         and then Has_Discriminants (Full_T)
15807         and then
15808           Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
15809       then
15810          Set_Has_Constrained_Partial_View (Full_T);
15811          Set_Has_Constrained_Partial_View (Priv_T);
15812       end if;
15813
15814       --  Create a full declaration for all its subtypes recorded in
15815       --  Private_Dependents and swap them similarly to the base type. These
15816       --  are subtypes that have been define before the full declaration of
15817       --  the private type. We also swap the entry in Private_Dependents list
15818       --  so we can properly restore the private view on exit from the scope.
15819
15820       declare
15821          Priv_Elmt : Elmt_Id;
15822          Priv      : Entity_Id;
15823          Full      : Entity_Id;
15824
15825       begin
15826          Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
15827          while Present (Priv_Elmt) loop
15828             Priv := Node (Priv_Elmt);
15829
15830             if Ekind (Priv) = E_Private_Subtype
15831               or else Ekind (Priv) = E_Limited_Private_Subtype
15832               or else Ekind (Priv) = E_Record_Subtype_With_Private
15833             then
15834                Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
15835                Set_Is_Itype (Full);
15836                Set_Parent (Full, Parent (Priv));
15837                Set_Associated_Node_For_Itype (Full, N);
15838
15839                --  Now we need to complete the private subtype, but since the
15840                --  base type has already been swapped, we must also swap the
15841                --  subtypes (and thus, reverse the arguments in the call to
15842                --  Complete_Private_Subtype).
15843
15844                Copy_And_Swap (Priv, Full);
15845                Complete_Private_Subtype (Full, Priv, Full_T, N);
15846                Replace_Elmt (Priv_Elmt, Full);
15847             end if;
15848
15849             Next_Elmt (Priv_Elmt);
15850          end loop;
15851       end;
15852
15853       --  If the private view was tagged, copy the new primitive operations
15854       --  from the private view to the full view.
15855
15856       if Is_Tagged_Type (Full_T) then
15857          declare
15858             Disp_Typ  : Entity_Id;
15859             Full_List : Elist_Id;
15860             Prim      : Entity_Id;
15861             Prim_Elmt : Elmt_Id;
15862             Priv_List : Elist_Id;
15863
15864             function Contains
15865               (E : Entity_Id;
15866                L : Elist_Id) return Boolean;
15867             --  Determine whether list L contains element E
15868
15869             --------------
15870             -- Contains --
15871             --------------
15872
15873             function Contains
15874               (E : Entity_Id;
15875                L : Elist_Id) return Boolean
15876             is
15877                List_Elmt : Elmt_Id;
15878
15879             begin
15880                List_Elmt := First_Elmt (L);
15881                while Present (List_Elmt) loop
15882                   if Node (List_Elmt) = E then
15883                      return True;
15884                   end if;
15885
15886                   Next_Elmt (List_Elmt);
15887                end loop;
15888
15889                return False;
15890             end Contains;
15891
15892          --  Start of processing
15893
15894          begin
15895             if Is_Tagged_Type (Priv_T) then
15896                Priv_List := Primitive_Operations (Priv_T);
15897                Prim_Elmt := First_Elmt (Priv_List);
15898
15899                --  In the case of a concurrent type completing a private tagged
15900                --  type, primivies may have been declared in between the two
15901                --  views. These subprograms need to be wrapped the same way
15902                --  entries and protected procedures are handled because they
15903                --  cannot be directly shared by the two views.
15904
15905                if Is_Concurrent_Type (Full_T) then
15906                   declare
15907                      Conc_Typ  : constant Entity_Id :=
15908                                    Corresponding_Record_Type (Full_T);
15909                      Loc       : constant Source_Ptr := Sloc (Conc_Typ);
15910                      Curr_Nod  : Node_Id := Parent (Conc_Typ);
15911                      Wrap_Spec : Node_Id;
15912
15913                   begin
15914                      while Present (Prim_Elmt) loop
15915                         Prim := Node (Prim_Elmt);
15916
15917                         if Comes_From_Source (Prim)
15918                           and then not Is_Abstract_Subprogram (Prim)
15919                         then
15920                            Wrap_Spec :=
15921                              Make_Subprogram_Declaration (Loc,
15922                                Specification =>
15923                                  Build_Wrapper_Spec (Loc,
15924                                    Subp_Id => Prim,
15925                                    Obj_Typ => Conc_Typ,
15926                                    Formals =>
15927                                      Parameter_Specifications (
15928                                        Parent (Prim))));
15929
15930                            Insert_After (Curr_Nod, Wrap_Spec);
15931                            Curr_Nod := Wrap_Spec;
15932
15933                            Analyze (Wrap_Spec);
15934                         end if;
15935
15936                         Next_Elmt (Prim_Elmt);
15937                      end loop;
15938
15939                      return;
15940                   end;
15941
15942                --  For non-concurrent types, transfer explicit primitives, but
15943                --  omit those inherited from the parent of the private view
15944                --  since they will be re-inherited later on.
15945
15946                else
15947                   Full_List := Primitive_Operations (Full_T);
15948
15949                   while Present (Prim_Elmt) loop
15950                      Prim := Node (Prim_Elmt);
15951
15952                      if Comes_From_Source (Prim)
15953                        and then not Contains (Prim, Full_List)
15954                      then
15955                         Append_Elmt (Prim, Full_List);
15956                      end if;
15957
15958                      Next_Elmt (Prim_Elmt);
15959                   end loop;
15960                end if;
15961
15962             --  Untagged private view
15963
15964             else
15965                Full_List := Primitive_Operations (Full_T);
15966
15967                --  In this case the partial view is untagged, so here we locate
15968                --  all of the earlier primitives that need to be treated as
15969                --  dispatching (those that appear between the two views). Note
15970                --  that these additional operations must all be new operations
15971                --  (any earlier operations that override inherited operations
15972                --  of the full view will already have been inserted in the
15973                --  primitives list, marked by Check_Operation_From_Private_View
15974                --  as dispatching. Note that implicit "/=" operators are
15975                --  excluded from being added to the primitives list since they
15976                --  shouldn't be treated as dispatching (tagged "/=" is handled
15977                --  specially).
15978
15979                Prim := Next_Entity (Full_T);
15980                while Present (Prim) and then Prim /= Priv_T loop
15981                   if Ekind (Prim) = E_Procedure
15982                        or else
15983                      Ekind (Prim) = E_Function
15984                   then
15985                      Disp_Typ := Find_Dispatching_Type (Prim);
15986
15987                      if Disp_Typ = Full_T
15988                        and then (Chars (Prim) /= Name_Op_Ne
15989                                   or else Comes_From_Source (Prim))
15990                      then
15991                         Check_Controlling_Formals (Full_T, Prim);
15992
15993                         if not Is_Dispatching_Operation (Prim) then
15994                            Append_Elmt (Prim, Full_List);
15995                            Set_Is_Dispatching_Operation (Prim, True);
15996                            Set_DT_Position (Prim, No_Uint);
15997                         end if;
15998
15999                      elsif Is_Dispatching_Operation (Prim)
16000                        and then Disp_Typ  /= Full_T
16001                      then
16002
16003                         --  Verify that it is not otherwise controlled by a
16004                         --  formal or a return value of type T.
16005
16006                         Check_Controlling_Formals (Disp_Typ, Prim);
16007                      end if;
16008                   end if;
16009
16010                   Next_Entity (Prim);
16011                end loop;
16012             end if;
16013
16014             --  For the tagged case, the two views can share the same
16015             --  Primitive Operation list and the same class wide type.
16016             --  Update attributes of the class-wide type which depend on
16017             --  the full declaration.
16018
16019             if Is_Tagged_Type (Priv_T) then
16020                Set_Primitive_Operations (Priv_T, Full_List);
16021                Set_Class_Wide_Type
16022                  (Base_Type (Full_T), Class_Wide_Type (Priv_T));
16023
16024                Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
16025             end if;
16026          end;
16027       end if;
16028
16029       --  Ada 2005 AI 161: Check preelaboratable initialization consistency
16030
16031       if Known_To_Have_Preelab_Init (Priv_T) then
16032
16033          --  Case where there is a pragma Preelaborable_Initialization. We
16034          --  always allow this in predefined units, which is a bit of a kludge,
16035          --  but it means we don't have to struggle to meet the requirements in
16036          --  the RM for having Preelaborable Initialization. Otherwise we
16037          --  require that the type meets the RM rules. But we can't check that
16038          --  yet, because of the rule about overriding Ininitialize, so we
16039          --  simply set a flag that will be checked at freeze time.
16040
16041          if not In_Predefined_Unit (Full_T) then
16042             Set_Must_Have_Preelab_Init (Full_T);
16043          end if;
16044       end if;
16045
16046       --  If pragma CPP_Class was applied to the private type declaration,
16047       --  propagate it now to the full type declaration.
16048
16049       if Is_CPP_Class (Priv_T) then
16050          Set_Is_CPP_Class (Full_T);
16051          Set_Convention   (Full_T, Convention_CPP);
16052       end if;
16053    end Process_Full_View;
16054
16055    -----------------------------------
16056    -- Process_Incomplete_Dependents --
16057    -----------------------------------
16058
16059    procedure Process_Incomplete_Dependents
16060      (N      : Node_Id;
16061       Full_T : Entity_Id;
16062       Inc_T  : Entity_Id)
16063    is
16064       Inc_Elmt : Elmt_Id;
16065       Priv_Dep : Entity_Id;
16066       New_Subt : Entity_Id;
16067
16068       Disc_Constraint : Elist_Id;
16069
16070    begin
16071       if No (Private_Dependents (Inc_T)) then
16072          return;
16073       end if;
16074
16075       --  Itypes that may be generated by the completion of an incomplete
16076       --  subtype are not used by the back-end and not attached to the tree.
16077       --  They are created only for constraint-checking purposes.
16078
16079       Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
16080       while Present (Inc_Elmt) loop
16081          Priv_Dep := Node (Inc_Elmt);
16082
16083          if Ekind (Priv_Dep) = E_Subprogram_Type then
16084
16085             --  An Access_To_Subprogram type may have a return type or a
16086             --  parameter type that is incomplete. Replace with the full view.
16087
16088             if Etype (Priv_Dep) = Inc_T then
16089                Set_Etype (Priv_Dep, Full_T);
16090             end if;
16091
16092             declare
16093                Formal : Entity_Id;
16094
16095             begin
16096                Formal := First_Formal (Priv_Dep);
16097                while Present (Formal) loop
16098                   if Etype (Formal) = Inc_T then
16099                      Set_Etype (Formal, Full_T);
16100                   end if;
16101
16102                   Next_Formal (Formal);
16103                end loop;
16104             end;
16105
16106          elsif Is_Overloadable (Priv_Dep) then
16107
16108             --  A protected operation is never dispatching: only its
16109             --  wrapper operation (which has convention Ada) is.
16110
16111             if Is_Tagged_Type (Full_T)
16112               and then Convention (Priv_Dep) /= Convention_Protected
16113             then
16114
16115                --  Subprogram has an access parameter whose designated type
16116                --  was incomplete. Reexamine declaration now, because it may
16117                --  be a primitive operation of the full type.
16118
16119                Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
16120                Set_Is_Dispatching_Operation (Priv_Dep);
16121                Check_Controlling_Formals (Full_T, Priv_Dep);
16122             end if;
16123
16124          elsif Ekind (Priv_Dep) = E_Subprogram_Body then
16125
16126             --  Can happen during processing of a body before the completion
16127             --  of a TA type. Ignore, because spec is also on dependent list.
16128
16129             return;
16130
16131          --  Ada 2005 (AI-412): Transform a regular incomplete subtype into a
16132          --  corresponding subtype of the full view.
16133
16134          elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
16135             Set_Subtype_Indication
16136               (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
16137             Set_Etype (Priv_Dep, Full_T);
16138             Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
16139             Set_Analyzed (Parent (Priv_Dep), False);
16140
16141             --  Reanalyze the declaration, suppressing the call to
16142             --  Enter_Name to avoid duplicate names.
16143
16144             Analyze_Subtype_Declaration
16145               (N    => Parent (Priv_Dep),
16146                Skip => True);
16147
16148          --  Dependent is a subtype
16149
16150          else
16151             --  We build a new subtype indication using the full view of the
16152             --  incomplete parent. The discriminant constraints have been
16153             --  elaborated already at the point of the subtype declaration.
16154
16155             New_Subt := Create_Itype (E_Void, N);
16156
16157             if Has_Discriminants (Full_T) then
16158                Disc_Constraint := Discriminant_Constraint (Priv_Dep);
16159             else
16160                Disc_Constraint := No_Elist;
16161             end if;
16162
16163             Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
16164             Set_Full_View (Priv_Dep, New_Subt);
16165          end if;
16166
16167          Next_Elmt (Inc_Elmt);
16168       end loop;
16169    end Process_Incomplete_Dependents;
16170
16171    --------------------------------
16172    -- Process_Range_Expr_In_Decl --
16173    --------------------------------
16174
16175    procedure Process_Range_Expr_In_Decl
16176      (R           : Node_Id;
16177       T           : Entity_Id;
16178       Check_List  : List_Id := Empty_List;
16179       R_Check_Off : Boolean := False)
16180    is
16181       Lo, Hi    : Node_Id;
16182       R_Checks  : Check_Result;
16183       Type_Decl : Node_Id;
16184       Def_Id    : Entity_Id;
16185
16186    begin
16187       Analyze_And_Resolve (R, Base_Type (T));
16188
16189       if Nkind (R) = N_Range then
16190          Lo := Low_Bound (R);
16191          Hi := High_Bound (R);
16192
16193          --  We need to ensure validity of the bounds here, because if we
16194          --  go ahead and do the expansion, then the expanded code will get
16195          --  analyzed with range checks suppressed and we miss the check.
16196
16197          Validity_Check_Range (R);
16198
16199          --  If there were errors in the declaration, try and patch up some
16200          --  common mistakes in the bounds. The cases handled are literals
16201          --  which are Integer where the expected type is Real and vice versa.
16202          --  These corrections allow the compilation process to proceed further
16203          --  along since some basic assumptions of the format of the bounds
16204          --  are guaranteed.
16205
16206          if Etype (R) = Any_Type then
16207
16208             if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
16209                Rewrite (Lo,
16210                  Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
16211
16212             elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
16213                Rewrite (Hi,
16214                  Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
16215
16216             elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
16217                Rewrite (Lo,
16218                  Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
16219
16220             elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
16221                Rewrite (Hi,
16222                  Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
16223             end if;
16224
16225             Set_Etype (Lo, T);
16226             Set_Etype (Hi, T);
16227          end if;
16228
16229          --  If the bounds of the range have been mistakenly given as string
16230          --  literals (perhaps in place of character literals), then an error
16231          --  has already been reported, but we rewrite the string literal as a
16232          --  bound of the range's type to avoid blowups in later processing
16233          --  that looks at static values.
16234
16235          if Nkind (Lo) = N_String_Literal then
16236             Rewrite (Lo,
16237               Make_Attribute_Reference (Sloc (Lo),
16238                 Attribute_Name => Name_First,
16239                 Prefix => New_Reference_To (T, Sloc (Lo))));
16240             Analyze_And_Resolve (Lo);
16241          end if;
16242
16243          if Nkind (Hi) = N_String_Literal then
16244             Rewrite (Hi,
16245               Make_Attribute_Reference (Sloc (Hi),
16246                 Attribute_Name => Name_First,
16247                 Prefix => New_Reference_To (T, Sloc (Hi))));
16248             Analyze_And_Resolve (Hi);
16249          end if;
16250
16251          --  If bounds aren't scalar at this point then exit, avoiding
16252          --  problems with further processing of the range in this procedure.
16253
16254          if not Is_Scalar_Type (Etype (Lo)) then
16255             return;
16256          end if;
16257
16258          --  Resolve (actually Sem_Eval) has checked that the bounds are in
16259          --  then range of the base type. Here we check whether the bounds
16260          --  are in the range of the subtype itself. Note that if the bounds
16261          --  represent the null range the Constraint_Error exception should
16262          --  not be raised.
16263
16264          --  ??? The following code should be cleaned up as follows
16265
16266          --  1. The Is_Null_Range (Lo, Hi) test should disappear since it
16267          --     is done in the call to Range_Check (R, T); below
16268
16269          --  2. The use of R_Check_Off should be investigated and possibly
16270          --     removed, this would clean up things a bit.
16271
16272          if Is_Null_Range (Lo, Hi) then
16273             null;
16274
16275          else
16276             --  Capture values of bounds and generate temporaries for them
16277             --  if needed, before applying checks, since checks may cause
16278             --  duplication of the expression without forcing evaluation.
16279
16280             if Expander_Active then
16281                Force_Evaluation (Lo);
16282                Force_Evaluation (Hi);
16283             end if;
16284
16285             --  We use a flag here instead of suppressing checks on the
16286             --  type because the type we check against isn't necessarily
16287             --  the place where we put the check.
16288
16289             if not R_Check_Off then
16290                R_Checks := Get_Range_Checks (R, T);
16291
16292                --  Look up tree to find an appropriate insertion point.
16293                --  This seems really junk code, and very brittle, couldn't
16294                --  we just use an insert actions call of some kind ???
16295
16296                Type_Decl := Parent (R);
16297                while Present (Type_Decl) and then not
16298                  (Nkind_In (Type_Decl, N_Full_Type_Declaration,
16299                                        N_Subtype_Declaration,
16300                                        N_Loop_Statement,
16301                                        N_Task_Type_Declaration)
16302                     or else
16303                   Nkind_In (Type_Decl, N_Single_Task_Declaration,
16304                                        N_Protected_Type_Declaration,
16305                                        N_Single_Protected_Declaration))
16306                loop
16307                   Type_Decl := Parent (Type_Decl);
16308                end loop;
16309
16310                --  Why would Type_Decl not be present???  Without this test,
16311                --  short regression tests fail.
16312
16313                if Present (Type_Decl) then
16314
16315                   --  Case of loop statement (more comments ???)
16316
16317                   if Nkind (Type_Decl) = N_Loop_Statement then
16318                      declare
16319                         Indic : Node_Id;
16320
16321                      begin
16322                         Indic := Parent (R);
16323                         while Present (Indic)
16324                           and then Nkind (Indic) /= N_Subtype_Indication
16325                         loop
16326                            Indic := Parent (Indic);
16327                         end loop;
16328
16329                         if Present (Indic) then
16330                            Def_Id := Etype (Subtype_Mark (Indic));
16331
16332                            Insert_Range_Checks
16333                              (R_Checks,
16334                               Type_Decl,
16335                               Def_Id,
16336                               Sloc (Type_Decl),
16337                               R,
16338                               Do_Before => True);
16339                         end if;
16340                      end;
16341
16342                   --  All other cases (more comments ???)
16343
16344                   else
16345                      Def_Id := Defining_Identifier (Type_Decl);
16346
16347                      if (Ekind (Def_Id) = E_Record_Type
16348                           and then Depends_On_Discriminant (R))
16349                        or else
16350                         (Ekind (Def_Id) = E_Protected_Type
16351                           and then Has_Discriminants (Def_Id))
16352                      then
16353                         Append_Range_Checks
16354                           (R_Checks, Check_List, Def_Id, Sloc (Type_Decl), R);
16355
16356                      else
16357                         Insert_Range_Checks
16358                           (R_Checks, Type_Decl, Def_Id, Sloc (Type_Decl), R);
16359
16360                      end if;
16361                   end if;
16362                end if;
16363             end if;
16364          end if;
16365
16366       elsif Expander_Active then
16367          Get_Index_Bounds (R, Lo, Hi);
16368          Force_Evaluation (Lo);
16369          Force_Evaluation (Hi);
16370       end if;
16371    end Process_Range_Expr_In_Decl;
16372
16373    --------------------------------------
16374    -- Process_Real_Range_Specification --
16375    --------------------------------------
16376
16377    procedure Process_Real_Range_Specification (Def : Node_Id) is
16378       Spec : constant Node_Id := Real_Range_Specification (Def);
16379       Lo   : Node_Id;
16380       Hi   : Node_Id;
16381       Err  : Boolean := False;
16382
16383       procedure Analyze_Bound (N : Node_Id);
16384       --  Analyze and check one bound
16385
16386       -------------------
16387       -- Analyze_Bound --
16388       -------------------
16389
16390       procedure Analyze_Bound (N : Node_Id) is
16391       begin
16392          Analyze_And_Resolve (N, Any_Real);
16393
16394          if not Is_OK_Static_Expression (N) then
16395             Flag_Non_Static_Expr
16396               ("bound in real type definition is not static!", N);
16397             Err := True;
16398          end if;
16399       end Analyze_Bound;
16400
16401    --  Start of processing for Process_Real_Range_Specification
16402
16403    begin
16404       if Present (Spec) then
16405          Lo := Low_Bound (Spec);
16406          Hi := High_Bound (Spec);
16407          Analyze_Bound (Lo);
16408          Analyze_Bound (Hi);
16409
16410          --  If error, clear away junk range specification
16411
16412          if Err then
16413             Set_Real_Range_Specification (Def, Empty);
16414          end if;
16415       end if;
16416    end Process_Real_Range_Specification;
16417
16418    ---------------------
16419    -- Process_Subtype --
16420    ---------------------
16421
16422    function Process_Subtype
16423      (S           : Node_Id;
16424       Related_Nod : Node_Id;
16425       Related_Id  : Entity_Id := Empty;
16426       Suffix      : Character := ' ') return Entity_Id
16427    is
16428       P               : Node_Id;
16429       Def_Id          : Entity_Id;
16430       Error_Node      : Node_Id;
16431       Full_View_Id    : Entity_Id;
16432       Subtype_Mark_Id : Entity_Id;
16433
16434       May_Have_Null_Exclusion : Boolean;
16435
16436       procedure Check_Incomplete (T : Entity_Id);
16437       --  Called to verify that an incomplete type is not used prematurely
16438
16439       ----------------------
16440       -- Check_Incomplete --
16441       ----------------------
16442
16443       procedure Check_Incomplete (T : Entity_Id) is
16444       begin
16445          --  Ada 2005 (AI-412): Incomplete subtypes are legal
16446
16447          if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
16448            and then
16449              not (Ada_Version >= Ada_05
16450                     and then
16451                        (Nkind (Parent (T)) = N_Subtype_Declaration
16452                           or else
16453                             (Nkind (Parent (T)) = N_Subtype_Indication
16454                                and then Nkind (Parent (Parent (T))) =
16455                                           N_Subtype_Declaration)))
16456          then
16457             Error_Msg_N ("invalid use of type before its full declaration", T);
16458          end if;
16459       end Check_Incomplete;
16460
16461    --  Start of processing for Process_Subtype
16462
16463    begin
16464       --  Case of no constraints present
16465
16466       if Nkind (S) /= N_Subtype_Indication then
16467          Find_Type (S);
16468          Check_Incomplete (S);
16469          P := Parent (S);
16470
16471          --  Ada 2005 (AI-231): Static check
16472
16473          if Ada_Version >= Ada_05
16474            and then Present (P)
16475            and then Null_Exclusion_Present (P)
16476            and then Nkind (P) /= N_Access_To_Object_Definition
16477            and then not Is_Access_Type (Entity (S))
16478          then
16479             Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
16480          end if;
16481
16482          --  The following is ugly, can't we have a range or even a flag???
16483
16484          May_Have_Null_Exclusion :=
16485            Nkind_In (P, N_Access_Definition,
16486                         N_Access_Function_Definition,
16487                         N_Access_Procedure_Definition,
16488                         N_Access_To_Object_Definition,
16489                         N_Allocator,
16490                         N_Component_Definition)
16491              or else
16492            Nkind_In (P, N_Derived_Type_Definition,
16493                         N_Discriminant_Specification,
16494                         N_Object_Declaration,
16495                         N_Parameter_Specification,
16496                         N_Subtype_Declaration);
16497
16498          --  Create an Itype that is a duplicate of Entity (S) but with the
16499          --  null-exclusion attribute
16500
16501          if May_Have_Null_Exclusion
16502            and then Is_Access_Type (Entity (S))
16503            and then Null_Exclusion_Present (P)
16504
16505             --  No need to check the case of an access to object definition.
16506             --  It is correct to define double not-null pointers.
16507
16508             --  Example:
16509             --     type Not_Null_Int_Ptr is not null access Integer;
16510             --     type Acc is not null access Not_Null_Int_Ptr;
16511
16512            and then Nkind (P) /= N_Access_To_Object_Definition
16513          then
16514             if Can_Never_Be_Null (Entity (S)) then
16515                case Nkind (Related_Nod) is
16516                   when N_Full_Type_Declaration =>
16517                      if Nkind (Type_Definition (Related_Nod))
16518                        in N_Array_Type_Definition
16519                      then
16520                         Error_Node :=
16521                           Subtype_Indication
16522                             (Component_Definition
16523                              (Type_Definition (Related_Nod)));
16524                      else
16525                         Error_Node :=
16526                           Subtype_Indication (Type_Definition (Related_Nod));
16527                      end if;
16528
16529                   when N_Subtype_Declaration =>
16530                      Error_Node := Subtype_Indication (Related_Nod);
16531
16532                   when N_Object_Declaration =>
16533                      Error_Node := Object_Definition (Related_Nod);
16534
16535                   when N_Component_Declaration =>
16536                      Error_Node :=
16537                        Subtype_Indication (Component_Definition (Related_Nod));
16538
16539                   when others =>
16540                      pragma Assert (False);
16541                      Error_Node := Related_Nod;
16542                end case;
16543
16544                Error_Msg_NE
16545                  ("`NOT NULL` not allowed (& already excludes null)",
16546                   Error_Node,
16547                   Entity (S));
16548             end if;
16549
16550             Set_Etype  (S,
16551               Create_Null_Excluding_Itype
16552                 (T           => Entity (S),
16553                  Related_Nod => P));
16554             Set_Entity (S, Etype (S));
16555          end if;
16556
16557          return Entity (S);
16558
16559       --  Case of constraint present, so that we have an N_Subtype_Indication
16560       --  node (this node is created only if constraints are present).
16561
16562       else
16563          Find_Type (Subtype_Mark (S));
16564
16565          if Nkind (Parent (S)) /= N_Access_To_Object_Definition
16566            and then not
16567             (Nkind (Parent (S)) = N_Subtype_Declaration
16568               and then Is_Itype (Defining_Identifier (Parent (S))))
16569          then
16570             Check_Incomplete (Subtype_Mark (S));
16571          end if;
16572
16573          P := Parent (S);
16574          Subtype_Mark_Id := Entity (Subtype_Mark (S));
16575
16576          --  Explicit subtype declaration case
16577
16578          if Nkind (P) = N_Subtype_Declaration then
16579             Def_Id := Defining_Identifier (P);
16580
16581          --  Explicit derived type definition case
16582
16583          elsif Nkind (P) = N_Derived_Type_Definition then
16584             Def_Id := Defining_Identifier (Parent (P));
16585
16586          --  Implicit case, the Def_Id must be created as an implicit type.
16587          --  The one exception arises in the case of concurrent types, array
16588          --  and access types, where other subsidiary implicit types may be
16589          --  created and must appear before the main implicit type. In these
16590          --  cases we leave Def_Id set to Empty as a signal that Create_Itype
16591          --  has not yet been called to create Def_Id.
16592
16593          else
16594             if Is_Array_Type (Subtype_Mark_Id)
16595               or else Is_Concurrent_Type (Subtype_Mark_Id)
16596               or else Is_Access_Type (Subtype_Mark_Id)
16597             then
16598                Def_Id := Empty;
16599
16600             --  For the other cases, we create a new unattached Itype,
16601             --  and set the indication to ensure it gets attached later.
16602
16603             else
16604                Def_Id :=
16605                  Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
16606             end if;
16607          end if;
16608
16609          --  If the kind of constraint is invalid for this kind of type,
16610          --  then give an error, and then pretend no constraint was given.
16611
16612          if not Is_Valid_Constraint_Kind
16613                    (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
16614          then
16615             Error_Msg_N
16616               ("incorrect constraint for this kind of type", Constraint (S));
16617
16618             Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16619
16620             --  Set Ekind of orphan itype, to prevent cascaded errors
16621
16622             if Present (Def_Id) then
16623                Set_Ekind (Def_Id, Ekind (Any_Type));
16624             end if;
16625
16626             --  Make recursive call, having got rid of the bogus constraint
16627
16628             return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
16629          end if;
16630
16631          --  Remaining processing depends on type
16632
16633          case Ekind (Subtype_Mark_Id) is
16634             when Access_Kind =>
16635                Constrain_Access (Def_Id, S, Related_Nod);
16636
16637                if Expander_Active
16638                  and then  Is_Itype (Designated_Type (Def_Id))
16639                  and then Nkind (Related_Nod) = N_Subtype_Declaration
16640                  and then not Is_Incomplete_Type (Designated_Type (Def_Id))
16641                then
16642                   Build_Itype_Reference
16643                     (Designated_Type (Def_Id), Related_Nod);
16644                end if;
16645
16646             when Array_Kind =>
16647                Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
16648
16649             when Decimal_Fixed_Point_Kind =>
16650                Constrain_Decimal (Def_Id, S);
16651
16652             when Enumeration_Kind =>
16653                Constrain_Enumeration (Def_Id, S);
16654
16655             when Ordinary_Fixed_Point_Kind =>
16656                Constrain_Ordinary_Fixed (Def_Id, S);
16657
16658             when Float_Kind =>
16659                Constrain_Float (Def_Id, S);
16660
16661             when Integer_Kind =>
16662                Constrain_Integer (Def_Id, S);
16663
16664             when E_Record_Type     |
16665                  E_Record_Subtype  |
16666                  Class_Wide_Kind   |
16667                  E_Incomplete_Type =>
16668                Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
16669
16670             when Private_Kind =>
16671                Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
16672                Set_Private_Dependents (Def_Id, New_Elmt_List);
16673
16674                --  In case of an invalid constraint prevent further processing
16675                --  since the type constructed is missing expected fields.
16676
16677                if Etype (Def_Id) = Any_Type then
16678                   return Def_Id;
16679                end if;
16680
16681                --  If the full view is that of a task with discriminants,
16682                --  we must constrain both the concurrent type and its
16683                --  corresponding record type. Otherwise we will just propagate
16684                --  the constraint to the full view, if available.
16685
16686                if Present (Full_View (Subtype_Mark_Id))
16687                  and then Has_Discriminants (Subtype_Mark_Id)
16688                  and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
16689                then
16690                   Full_View_Id :=
16691                     Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
16692
16693                   Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
16694                   Constrain_Concurrent (Full_View_Id, S,
16695                     Related_Nod, Related_Id, Suffix);
16696                   Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
16697                   Set_Full_View (Def_Id, Full_View_Id);
16698
16699                   --  Introduce an explicit reference to the private subtype,
16700                   --  to prevent scope anomalies in gigi if first use appears
16701                   --  in a nested context, e.g. a later function body.
16702                   --  Should this be generated in other contexts than a full
16703                   --  type declaration?
16704
16705                   if Is_Itype (Def_Id)
16706                     and then
16707                       Nkind (Parent (P)) = N_Full_Type_Declaration
16708                   then
16709                      Build_Itype_Reference (Def_Id, Parent (P));
16710                   end if;
16711
16712                else
16713                   Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
16714                end if;
16715
16716             when Concurrent_Kind  =>
16717                Constrain_Concurrent (Def_Id, S,
16718                  Related_Nod, Related_Id, Suffix);
16719
16720             when others =>
16721                Error_Msg_N ("invalid subtype mark in subtype indication", S);
16722          end case;
16723
16724          --  Size and Convention are always inherited from the base type
16725
16726          Set_Size_Info  (Def_Id,            (Subtype_Mark_Id));
16727          Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
16728
16729          return Def_Id;
16730       end if;
16731    end Process_Subtype;
16732
16733    ---------------------------------------
16734    -- Check_Anonymous_Access_Components --
16735    ---------------------------------------
16736
16737    procedure Check_Anonymous_Access_Components
16738       (Typ_Decl  : Node_Id;
16739        Typ       : Entity_Id;
16740        Prev      : Entity_Id;
16741        Comp_List : Node_Id)
16742    is
16743       Loc         : constant Source_Ptr := Sloc (Typ_Decl);
16744       Anon_Access : Entity_Id;
16745       Acc_Def     : Node_Id;
16746       Comp        : Node_Id;
16747       Comp_Def    : Node_Id;
16748       Decl        : Node_Id;
16749       Type_Def    : Node_Id;
16750
16751       procedure Build_Incomplete_Type_Declaration;
16752       --  If the record type contains components that include an access to the
16753       --  current record, then create an incomplete type declaration for the
16754       --  record, to be used as the designated type of the anonymous access.
16755       --  This is done only once, and only if there is no previous partial
16756       --  view of the type.
16757
16758       function Designates_T (Subt : Node_Id) return Boolean;
16759       --  Check whether a node designates the enclosing record type, or 'Class
16760       --  of that type
16761
16762       function Mentions_T (Acc_Def : Node_Id) return Boolean;
16763       --  Check whether an access definition includes a reference to
16764       --  the enclosing record type. The reference can be a subtype mark
16765       --  in the access definition itself, a 'Class attribute reference, or
16766       --  recursively a reference appearing in a parameter specification
16767       --  or result definition of an access_to_subprogram definition.
16768
16769       --------------------------------------
16770       -- Build_Incomplete_Type_Declaration --
16771       --------------------------------------
16772
16773       procedure Build_Incomplete_Type_Declaration is
16774          Decl  : Node_Id;
16775          Inc_T : Entity_Id;
16776          H     : Entity_Id;
16777
16778          --  Is_Tagged indicates whether the type is tagged. It is tagged if
16779          --  it's "is new ... with record" or else "is tagged record ...".
16780
16781          Is_Tagged : constant Boolean :=
16782              (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
16783                  and then
16784                    Present
16785                      (Record_Extension_Part (Type_Definition (Typ_Decl))))
16786            or else
16787              (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
16788                  and then Tagged_Present (Type_Definition (Typ_Decl)));
16789
16790       begin
16791          --  If there is a previous partial view, no need to create a new one
16792          --  If the partial view, given by Prev, is incomplete,  If Prev is
16793          --  a private declaration, full declaration is flagged accordingly.
16794
16795          if Prev /= Typ then
16796             if Is_Tagged then
16797                Make_Class_Wide_Type (Prev);
16798                Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
16799                Set_Etype (Class_Wide_Type (Typ), Typ);
16800             end if;
16801
16802             return;
16803
16804          elsif Has_Private_Declaration (Typ) then
16805
16806             --  If we refer to T'Class inside T, and T is the completion of a
16807             --  private type, then we need to make sure the class-wide type
16808             --  exists.
16809
16810             if Is_Tagged then
16811                Make_Class_Wide_Type (Typ);
16812             end if;
16813
16814             return;
16815
16816          --  If there was a previous anonymous access type, the incomplete
16817          --  type declaration will have been created already.
16818
16819          elsif Present (Current_Entity (Typ))
16820            and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
16821            and then Full_View (Current_Entity (Typ)) = Typ
16822          then
16823             return;
16824
16825          else
16826             Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
16827             Decl  := Make_Incomplete_Type_Declaration (Loc, Inc_T);
16828
16829             --  Type has already been inserted into the current scope.
16830             --  Remove it, and add incomplete declaration for type, so
16831             --  that subsequent anonymous access types can use it.
16832             --  The entity is unchained from the homonym list and from
16833             --  immediate visibility. After analysis, the entity in the
16834             --  incomplete declaration becomes immediately visible in the
16835             --  record declaration that follows.
16836
16837             H := Current_Entity (Typ);
16838
16839             if H = Typ then
16840                Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
16841             else
16842                while Present (H)
16843                  and then Homonym (H) /= Typ
16844                loop
16845                   H := Homonym (Typ);
16846                end loop;
16847
16848                Set_Homonym (H, Homonym (Typ));
16849             end if;
16850
16851             Insert_Before (Typ_Decl, Decl);
16852             Analyze (Decl);
16853             Set_Full_View (Inc_T, Typ);
16854
16855             if Is_Tagged then
16856                --  Create a common class-wide type for both views, and set
16857                --  the Etype of the class-wide type to the full view.
16858
16859                Make_Class_Wide_Type (Inc_T);
16860                Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
16861                Set_Etype (Class_Wide_Type (Typ), Typ);
16862             end if;
16863          end if;
16864       end Build_Incomplete_Type_Declaration;
16865
16866       ------------------
16867       -- Designates_T --
16868       ------------------
16869
16870       function Designates_T (Subt : Node_Id) return Boolean is
16871          Type_Id : constant Name_Id := Chars (Typ);
16872
16873          function Names_T (Nam : Node_Id) return Boolean;
16874          --  The record type has not been introduced in the current scope
16875          --  yet, so we must examine the name of the type itself, either
16876          --  an identifier T, or an expanded name of the form P.T, where
16877          --  P denotes the current scope.
16878
16879          -------------
16880          -- Names_T --
16881          -------------
16882
16883          function Names_T (Nam : Node_Id) return Boolean is
16884          begin
16885             if Nkind (Nam) = N_Identifier then
16886                return Chars (Nam) = Type_Id;
16887
16888             elsif Nkind (Nam) = N_Selected_Component then
16889                if Chars (Selector_Name (Nam)) = Type_Id then
16890                   if Nkind (Prefix (Nam)) = N_Identifier then
16891                      return Chars (Prefix (Nam)) = Chars (Current_Scope);
16892
16893                   elsif Nkind (Prefix (Nam)) = N_Selected_Component then
16894                      return Chars (Selector_Name (Prefix (Nam))) =
16895                             Chars (Current_Scope);
16896                   else
16897                      return False;
16898                   end if;
16899
16900                else
16901                   return False;
16902                end if;
16903
16904             else
16905                return False;
16906             end if;
16907          end Names_T;
16908
16909       --  Start of processing for Designates_T
16910
16911       begin
16912          if Nkind (Subt) = N_Identifier then
16913             return Chars (Subt) = Type_Id;
16914
16915             --  Reference can be through an expanded name which has not been
16916             --  analyzed yet, and which designates enclosing scopes.
16917
16918          elsif Nkind (Subt) = N_Selected_Component then
16919             if Names_T (Subt) then
16920                return True;
16921
16922             --  Otherwise it must denote an entity that is already visible.
16923             --  The access definition may name a subtype of the enclosing
16924             --  type, if there is a previous incomplete declaration for it.
16925
16926             else
16927                Find_Selected_Component (Subt);
16928                return
16929                  Is_Entity_Name (Subt)
16930                    and then Scope (Entity (Subt)) = Current_Scope
16931                    and then
16932                      (Chars (Base_Type (Entity (Subt))) = Type_Id
16933                        or else
16934                          (Is_Class_Wide_Type (Entity (Subt))
16935                            and then
16936                            Chars (Etype (Base_Type (Entity (Subt)))) =
16937                                                                   Type_Id));
16938             end if;
16939
16940          --  A reference to the current type may appear as the prefix of
16941          --  a 'Class attribute.
16942
16943          elsif Nkind (Subt) = N_Attribute_Reference
16944            and then Attribute_Name (Subt) = Name_Class
16945          then
16946             return Names_T (Prefix (Subt));
16947
16948          else
16949             return False;
16950          end if;
16951       end Designates_T;
16952
16953       ----------------
16954       -- Mentions_T --
16955       ----------------
16956
16957       function Mentions_T (Acc_Def : Node_Id) return Boolean is
16958          Param_Spec : Node_Id;
16959
16960          Acc_Subprg : constant Node_Id :=
16961                         Access_To_Subprogram_Definition (Acc_Def);
16962
16963       begin
16964          if No (Acc_Subprg) then
16965             return Designates_T (Subtype_Mark (Acc_Def));
16966          end if;
16967
16968          --  Component is an access_to_subprogram: examine its formals,
16969          --  and result definition in the case of an access_to_function.
16970
16971          Param_Spec := First (Parameter_Specifications (Acc_Subprg));
16972          while Present (Param_Spec) loop
16973             if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
16974               and then Mentions_T (Parameter_Type (Param_Spec))
16975             then
16976                return True;
16977
16978             elsif Designates_T (Parameter_Type (Param_Spec)) then
16979                return True;
16980             end if;
16981
16982             Next (Param_Spec);
16983          end loop;
16984
16985          if Nkind (Acc_Subprg) = N_Access_Function_Definition then
16986             if Nkind (Result_Definition (Acc_Subprg)) =
16987                  N_Access_Definition
16988             then
16989                return Mentions_T (Result_Definition (Acc_Subprg));
16990             else
16991                return Designates_T (Result_Definition (Acc_Subprg));
16992             end if;
16993          end if;
16994
16995          return False;
16996       end Mentions_T;
16997
16998    --  Start of processing for Check_Anonymous_Access_Components
16999
17000    begin
17001       if No (Comp_List) then
17002          return;
17003       end if;
17004
17005       Comp := First (Component_Items (Comp_List));
17006       while Present (Comp) loop
17007          if Nkind (Comp) = N_Component_Declaration
17008            and then Present
17009              (Access_Definition (Component_Definition (Comp)))
17010            and then
17011              Mentions_T (Access_Definition (Component_Definition (Comp)))
17012          then
17013             Comp_Def := Component_Definition (Comp);
17014             Acc_Def :=
17015               Access_To_Subprogram_Definition
17016                 (Access_Definition (Comp_Def));
17017
17018             Build_Incomplete_Type_Declaration;
17019             Anon_Access :=
17020               Make_Defining_Identifier (Loc,
17021                 Chars => New_Internal_Name ('S'));
17022
17023             --  Create a declaration for the anonymous access type: either
17024             --  an access_to_object or an access_to_subprogram.
17025
17026             if Present (Acc_Def) then
17027                if Nkind  (Acc_Def) = N_Access_Function_Definition then
17028                   Type_Def :=
17029                     Make_Access_Function_Definition (Loc,
17030                       Parameter_Specifications =>
17031                         Parameter_Specifications (Acc_Def),
17032                       Result_Definition => Result_Definition (Acc_Def));
17033                else
17034                   Type_Def :=
17035                     Make_Access_Procedure_Definition (Loc,
17036                       Parameter_Specifications =>
17037                         Parameter_Specifications (Acc_Def));
17038                end if;
17039
17040             else
17041                Type_Def :=
17042                  Make_Access_To_Object_Definition (Loc,
17043                    Subtype_Indication =>
17044                       Relocate_Node
17045                         (Subtype_Mark
17046                           (Access_Definition (Comp_Def))));
17047
17048                Set_Constant_Present
17049                  (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
17050                Set_All_Present
17051                  (Type_Def, All_Present (Access_Definition (Comp_Def)));
17052             end if;
17053
17054             Set_Null_Exclusion_Present
17055               (Type_Def,
17056                Null_Exclusion_Present (Access_Definition (Comp_Def)));
17057
17058             Decl :=
17059               Make_Full_Type_Declaration (Loc,
17060                 Defining_Identifier => Anon_Access,
17061                 Type_Definition     => Type_Def);
17062
17063             Insert_Before (Typ_Decl, Decl);
17064             Analyze (Decl);
17065
17066             --  If an access to object, Preserve entity of designated type,
17067             --  for ASIS use, before rewriting the component definition.
17068
17069             if No (Acc_Def) then
17070                declare
17071                   Desig : Entity_Id;
17072
17073                begin
17074                   Desig := Entity (Subtype_Indication (Type_Def));
17075
17076                   --  If the access definition is to the current  record,
17077                   --  the visible entity at this point is an  incomplete
17078                   --  type. Retrieve the full view to simplify  ASIS queries
17079
17080                   if Ekind (Desig) = E_Incomplete_Type then
17081                      Desig := Full_View (Desig);
17082                   end if;
17083
17084                   Set_Entity
17085                     (Subtype_Mark (Access_Definition  (Comp_Def)), Desig);
17086                end;
17087             end if;
17088
17089             Rewrite (Comp_Def,
17090               Make_Component_Definition (Loc,
17091                 Subtype_Indication =>
17092                New_Occurrence_Of (Anon_Access, Loc)));
17093
17094             if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
17095                Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
17096             else
17097                Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
17098             end if;
17099
17100             Set_Is_Local_Anonymous_Access (Anon_Access);
17101          end if;
17102
17103          Next (Comp);
17104       end loop;
17105
17106       if Present (Variant_Part (Comp_List)) then
17107          declare
17108             V : Node_Id;
17109          begin
17110             V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
17111             while Present (V) loop
17112                Check_Anonymous_Access_Components
17113                  (Typ_Decl, Typ, Prev, Component_List (V));
17114                Next_Non_Pragma (V);
17115             end loop;
17116          end;
17117       end if;
17118    end Check_Anonymous_Access_Components;
17119
17120    --------------------------------
17121    -- Preanalyze_Spec_Expression --
17122    --------------------------------
17123
17124    procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
17125       Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
17126    begin
17127       In_Spec_Expression := True;
17128       Preanalyze_And_Resolve (N, T);
17129       In_Spec_Expression := Save_In_Spec_Expression;
17130    end Preanalyze_Spec_Expression;
17131
17132    -----------------------------
17133    -- Record_Type_Declaration --
17134    -----------------------------
17135
17136    procedure Record_Type_Declaration
17137      (T    : Entity_Id;
17138       N    : Node_Id;
17139       Prev : Entity_Id)
17140    is
17141       Def       : constant Node_Id := Type_Definition (N);
17142       Is_Tagged : Boolean;
17143       Tag_Comp  : Entity_Id;
17144
17145    begin
17146       --  These flags must be initialized before calling Process_Discriminants
17147       --  because this routine makes use of them.
17148
17149       Set_Ekind             (T, E_Record_Type);
17150       Set_Etype             (T, T);
17151       Init_Size_Align       (T);
17152       Set_Interfaces        (T, No_Elist);
17153       Set_Stored_Constraint (T, No_Elist);
17154
17155       --  Normal case
17156
17157       if Ada_Version < Ada_05
17158         or else not Interface_Present (Def)
17159       then
17160          --  The flag Is_Tagged_Type might have already been set by
17161          --  Find_Type_Name if it detected an error for declaration T. This
17162          --  arises in the case of private tagged types where the full view
17163          --  omits the word tagged.
17164
17165          Is_Tagged :=
17166            Tagged_Present (Def)
17167              or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
17168
17169          Set_Is_Tagged_Type      (T, Is_Tagged);
17170          Set_Is_Limited_Record   (T, Limited_Present (Def));
17171
17172          --  Type is abstract if full declaration carries keyword, or if
17173          --  previous partial view did.
17174
17175          Set_Is_Abstract_Type    (T, Is_Abstract_Type (T)
17176                                       or else Abstract_Present (Def));
17177
17178       else
17179          Is_Tagged := True;
17180          Analyze_Interface_Declaration (T, Def);
17181
17182          if Present (Discriminant_Specifications (N)) then
17183             Error_Msg_N
17184               ("interface types cannot have discriminants",
17185                 Defining_Identifier
17186                   (First (Discriminant_Specifications (N))));
17187          end if;
17188       end if;
17189
17190       --  First pass: if there are self-referential access components,
17191       --  create the required anonymous access type declarations, and if
17192       --  need be an incomplete type declaration for T itself.
17193
17194       Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
17195
17196       if Ada_Version >= Ada_05
17197         and then Present (Interface_List (Def))
17198       then
17199          Check_Interfaces (N, Def);
17200
17201          declare
17202             Ifaces_List : Elist_Id;
17203
17204          begin
17205             --  Ada 2005 (AI-251): Collect the list of progenitors that are not
17206             --  already in the parents.
17207
17208             Collect_Interfaces
17209               (T               => T,
17210                Ifaces_List     => Ifaces_List,
17211                Exclude_Parents => True);
17212
17213             Set_Interfaces (T, Ifaces_List);
17214          end;
17215       end if;
17216
17217       --  Records constitute a scope for the component declarations within.
17218       --  The scope is created prior to the processing of these declarations.
17219       --  Discriminants are processed first, so that they are visible when
17220       --  processing the other components. The Ekind of the record type itself
17221       --  is set to E_Record_Type (subtypes appear as E_Record_Subtype).
17222
17223       --  Enter record scope
17224
17225       Push_Scope (T);
17226
17227       --  If an incomplete or private type declaration was already given for
17228       --  the type, then this scope already exists, and the discriminants have
17229       --  been declared within. We must verify that the full declaration
17230       --  matches the incomplete one.
17231
17232       Check_Or_Process_Discriminants (N, T, Prev);
17233
17234       Set_Is_Constrained     (T, not Has_Discriminants (T));
17235       Set_Has_Delayed_Freeze (T, True);
17236
17237       --  For tagged types add a manually analyzed component corresponding
17238       --  to the component _tag, the corresponding piece of tree will be
17239       --  expanded as part of the freezing actions if it is not a CPP_Class.
17240
17241       if Is_Tagged then
17242
17243          --  Do not add the tag unless we are in expansion mode
17244
17245          if Expander_Active then
17246             Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
17247             Enter_Name (Tag_Comp);
17248
17249             Set_Ekind                     (Tag_Comp, E_Component);
17250             Set_Is_Tag                    (Tag_Comp);
17251             Set_Is_Aliased                (Tag_Comp);
17252             Set_Etype                     (Tag_Comp, RTE (RE_Tag));
17253             Set_DT_Entry_Count            (Tag_Comp, No_Uint);
17254             Set_Original_Record_Component (Tag_Comp, Tag_Comp);
17255             Init_Component_Location       (Tag_Comp);
17256
17257             --  Ada 2005 (AI-251): Addition of the Tag corresponding to all the
17258             --  implemented interfaces.
17259
17260             if Has_Interfaces (T) then
17261                Add_Interface_Tag_Components (N, T);
17262             end if;
17263          end if;
17264
17265          Make_Class_Wide_Type (T);
17266          Set_Primitive_Operations (T, New_Elmt_List);
17267       end if;
17268
17269       --  We must suppress range checks when processing the components
17270       --  of a record in the presence of discriminants, since we don't
17271       --  want spurious checks to be generated during their analysis, but
17272       --  must reset the Suppress_Range_Checks flags after having processed
17273       --  the record definition.
17274
17275       --  Note: this is the only use of Kill_Range_Checks, and is a bit odd,
17276       --  couldn't we just use the normal range check suppression method here.
17277       --  That would seem cleaner ???
17278
17279       if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
17280          Set_Kill_Range_Checks (T, True);
17281          Record_Type_Definition (Def, Prev);
17282          Set_Kill_Range_Checks (T, False);
17283       else
17284          Record_Type_Definition (Def, Prev);
17285       end if;
17286
17287       --  Exit from record scope
17288
17289       End_Scope;
17290
17291       --  Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
17292       --  the implemented interfaces and associate them an aliased entity.
17293
17294       if Is_Tagged
17295         and then not Is_Empty_List (Interface_List (Def))
17296       then
17297          Derive_Progenitor_Subprograms (T, T);
17298       end if;
17299    end Record_Type_Declaration;
17300
17301    ----------------------------
17302    -- Record_Type_Definition --
17303    ----------------------------
17304
17305    procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
17306       Component          : Entity_Id;
17307       Ctrl_Components    : Boolean := False;
17308       Final_Storage_Only : Boolean;
17309       T                  : Entity_Id;
17310
17311    begin
17312       if Ekind (Prev_T) = E_Incomplete_Type then
17313          T := Full_View (Prev_T);
17314       else
17315          T := Prev_T;
17316       end if;
17317
17318       Final_Storage_Only := not Is_Controlled (T);
17319
17320       --  Ada 2005: check whether an explicit Limited is present in a derived
17321       --  type declaration.
17322
17323       if Nkind (Parent (Def)) = N_Derived_Type_Definition
17324         and then Limited_Present (Parent (Def))
17325       then
17326          Set_Is_Limited_Record (T);
17327       end if;
17328
17329       --  If the component list of a record type is defined by the reserved
17330       --  word null and there is no discriminant part, then the record type has
17331       --  no components and all records of the type are null records (RM 3.7)
17332       --  This procedure is also called to process the extension part of a
17333       --  record extension, in which case the current scope may have inherited
17334       --  components.
17335
17336       if No (Def)
17337         or else No (Component_List (Def))
17338         or else Null_Present (Component_List (Def))
17339       then
17340          null;
17341
17342       else
17343          Analyze_Declarations (Component_Items (Component_List (Def)));
17344
17345          if Present (Variant_Part (Component_List (Def))) then
17346             Analyze (Variant_Part (Component_List (Def)));
17347          end if;
17348       end if;
17349
17350       --  After completing the semantic analysis of the record definition,
17351       --  record components, both new and inherited, are accessible. Set their
17352       --  kind accordingly. Exclude malformed itypes from illegal declarations,
17353       --  whose Ekind may be void.
17354
17355       Component := First_Entity (Current_Scope);
17356       while Present (Component) loop
17357          if Ekind (Component) = E_Void
17358            and then not Is_Itype (Component)
17359          then
17360             Set_Ekind (Component, E_Component);
17361             Init_Component_Location (Component);
17362          end if;
17363
17364          if Has_Task (Etype (Component)) then
17365             Set_Has_Task (T);
17366          end if;
17367
17368          if Ekind (Component) /= E_Component then
17369             null;
17370
17371          elsif Has_Controlled_Component (Etype (Component))
17372            or else (Chars (Component) /= Name_uParent
17373                      and then Is_Controlled (Etype (Component)))
17374          then
17375             Set_Has_Controlled_Component (T, True);
17376             Final_Storage_Only :=
17377               Final_Storage_Only
17378                 and then Finalize_Storage_Only (Etype (Component));
17379             Ctrl_Components := True;
17380          end if;
17381
17382          Next_Entity (Component);
17383       end loop;
17384
17385       --  A Type is Finalize_Storage_Only only if all its controlled components
17386       --  are also.
17387
17388       if Ctrl_Components then
17389          Set_Finalize_Storage_Only (T, Final_Storage_Only);
17390       end if;
17391
17392       --  Place reference to end record on the proper entity, which may
17393       --  be a partial view.
17394
17395       if Present (Def) then
17396          Process_End_Label (Def, 'e', Prev_T);
17397       end if;
17398    end Record_Type_Definition;
17399
17400    ------------------------
17401    -- Replace_Components --
17402    ------------------------
17403
17404    procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
17405       function Process (N : Node_Id) return Traverse_Result;
17406
17407       -------------
17408       -- Process --
17409       -------------
17410
17411       function Process (N : Node_Id) return Traverse_Result is
17412          Comp : Entity_Id;
17413
17414       begin
17415          if Nkind (N) = N_Discriminant_Specification then
17416             Comp := First_Discriminant (Typ);
17417             while Present (Comp) loop
17418                if Chars (Comp) = Chars (Defining_Identifier (N)) then
17419                   Set_Defining_Identifier (N, Comp);
17420                   exit;
17421                end if;
17422
17423                Next_Discriminant (Comp);
17424             end loop;
17425
17426          elsif Nkind (N) = N_Component_Declaration then
17427             Comp := First_Component (Typ);
17428             while Present (Comp) loop
17429                if Chars (Comp) = Chars (Defining_Identifier (N)) then
17430                   Set_Defining_Identifier (N, Comp);
17431                   exit;
17432                end if;
17433
17434                Next_Component (Comp);
17435             end loop;
17436          end if;
17437
17438          return OK;
17439       end Process;
17440
17441       procedure Replace is new Traverse_Proc (Process);
17442
17443    --  Start of processing for Replace_Components
17444
17445    begin
17446       Replace (Decl);
17447    end Replace_Components;
17448
17449    -------------------------------
17450    -- Set_Completion_Referenced --
17451    -------------------------------
17452
17453    procedure Set_Completion_Referenced (E : Entity_Id) is
17454    begin
17455       --  If in main unit, mark entity that is a completion as referenced,
17456       --  warnings go on the partial view when needed.
17457
17458       if In_Extended_Main_Source_Unit (E) then
17459          Set_Referenced (E);
17460       end if;
17461    end Set_Completion_Referenced;
17462
17463    ---------------------
17464    -- Set_Fixed_Range --
17465    ---------------------
17466
17467    --  The range for fixed-point types is complicated by the fact that we
17468    --  do not know the exact end points at the time of the declaration. This
17469    --  is true for three reasons:
17470
17471    --     A size clause may affect the fudging of the end-points
17472    --     A small clause may affect the values of the end-points
17473    --     We try to include the end-points if it does not affect the size
17474
17475    --  This means that the actual end-points must be established at the point
17476    --  when the type is frozen. Meanwhile, we first narrow the range as
17477    --  permitted (so that it will fit if necessary in a small specified size),
17478    --  and then build a range subtree with these narrowed bounds.
17479
17480    --  Set_Fixed_Range constructs the range from real literal values, and sets
17481    --  the range as the Scalar_Range of the given fixed-point type entity.
17482
17483    --  The parent of this range is set to point to the entity so that it is
17484    --  properly hooked into the tree (unlike normal Scalar_Range entries for
17485    --  other scalar types, which are just pointers to the range in the
17486    --  original tree, this would otherwise be an orphan).
17487
17488    --  The tree is left unanalyzed. When the type is frozen, the processing
17489    --  in Freeze.Freeze_Fixed_Point_Type notices that the range is not
17490    --  analyzed, and uses this as an indication that it should complete
17491    --  work on the range (it will know the final small and size values).
17492
17493    procedure Set_Fixed_Range
17494      (E   : Entity_Id;
17495       Loc : Source_Ptr;
17496       Lo  : Ureal;
17497       Hi  : Ureal)
17498    is
17499       S : constant Node_Id :=
17500             Make_Range (Loc,
17501               Low_Bound  => Make_Real_Literal (Loc, Lo),
17502               High_Bound => Make_Real_Literal (Loc, Hi));
17503    begin
17504       Set_Scalar_Range (E, S);
17505       Set_Parent (S, E);
17506    end Set_Fixed_Range;
17507
17508    ----------------------------------
17509    -- Set_Scalar_Range_For_Subtype --
17510    ----------------------------------
17511
17512    procedure Set_Scalar_Range_For_Subtype
17513      (Def_Id : Entity_Id;
17514       R      : Node_Id;
17515       Subt   : Entity_Id)
17516    is
17517       Kind : constant Entity_Kind :=  Ekind (Def_Id);
17518
17519    begin
17520       Set_Scalar_Range (Def_Id, R);
17521
17522       --  We need to link the range into the tree before resolving it so
17523       --  that types that are referenced, including importantly the subtype
17524       --  itself, are properly frozen (Freeze_Expression requires that the
17525       --  expression be properly linked into the tree). Of course if it is
17526       --  already linked in, then we do not disturb the current link.
17527
17528       if No (Parent (R)) then
17529          Set_Parent (R, Def_Id);
17530       end if;
17531
17532       --  Reset the kind of the subtype during analysis of the range, to
17533       --  catch possible premature use in the bounds themselves.
17534
17535       Set_Ekind (Def_Id, E_Void);
17536       Process_Range_Expr_In_Decl (R, Subt);
17537       Set_Ekind (Def_Id, Kind);
17538    end Set_Scalar_Range_For_Subtype;
17539
17540    --------------------------------------------------------
17541    -- Set_Stored_Constraint_From_Discriminant_Constraint --
17542    --------------------------------------------------------
17543
17544    procedure Set_Stored_Constraint_From_Discriminant_Constraint
17545      (E : Entity_Id)
17546    is
17547    begin
17548       --  Make sure set if encountered during Expand_To_Stored_Constraint
17549
17550       Set_Stored_Constraint (E, No_Elist);
17551
17552       --  Give it the right value
17553
17554       if Is_Constrained (E) and then Has_Discriminants (E) then
17555          Set_Stored_Constraint (E,
17556            Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
17557       end if;
17558    end Set_Stored_Constraint_From_Discriminant_Constraint;
17559
17560    -------------------------------------
17561    -- Signed_Integer_Type_Declaration --
17562    -------------------------------------
17563
17564    procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17565       Implicit_Base : Entity_Id;
17566       Base_Typ      : Entity_Id;
17567       Lo_Val        : Uint;
17568       Hi_Val        : Uint;
17569       Errs          : Boolean := False;
17570       Lo            : Node_Id;
17571       Hi            : Node_Id;
17572
17573       function Can_Derive_From (E : Entity_Id) return Boolean;
17574       --  Determine whether given bounds allow derivation from specified type
17575
17576       procedure Check_Bound (Expr : Node_Id);
17577       --  Check bound to make sure it is integral and static. If not, post
17578       --  appropriate error message and set Errs flag
17579
17580       ---------------------
17581       -- Can_Derive_From --
17582       ---------------------
17583
17584       --  Note we check both bounds against both end values, to deal with
17585       --  strange types like ones with a range of 0 .. -12341234.
17586
17587       function Can_Derive_From (E : Entity_Id) return Boolean is
17588          Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
17589          Hi : constant Uint := Expr_Value (Type_High_Bound (E));
17590       begin
17591          return Lo <= Lo_Val and then Lo_Val <= Hi
17592                   and then
17593                 Lo <= Hi_Val and then Hi_Val <= Hi;
17594       end Can_Derive_From;
17595
17596       -----------------
17597       -- Check_Bound --
17598       -----------------
17599
17600       procedure Check_Bound (Expr : Node_Id) is
17601       begin
17602          --  If a range constraint is used as an integer type definition, each
17603          --  bound of the range must be defined by a static expression of some
17604          --  integer type, but the two bounds need not have the same integer
17605          --  type (Negative bounds are allowed.) (RM 3.5.4)
17606
17607          if not Is_Integer_Type (Etype (Expr)) then
17608             Error_Msg_N
17609               ("integer type definition bounds must be of integer type", Expr);
17610             Errs := True;
17611
17612          elsif not Is_OK_Static_Expression (Expr) then
17613             Flag_Non_Static_Expr
17614               ("non-static expression used for integer type bound!", Expr);
17615             Errs := True;
17616
17617          --  The bounds are folded into literals, and we set their type to be
17618          --  universal, to avoid typing difficulties: we cannot set the type
17619          --  of the literal to the new type, because this would be a forward
17620          --  reference for the back end,  and if the original type is user-
17621          --  defined this can lead to spurious semantic errors (e.g. 2928-003).
17622
17623          else
17624             if Is_Entity_Name (Expr) then
17625                Fold_Uint (Expr, Expr_Value (Expr), True);
17626             end if;
17627
17628             Set_Etype (Expr, Universal_Integer);
17629          end if;
17630       end Check_Bound;
17631
17632    --  Start of processing for Signed_Integer_Type_Declaration
17633
17634    begin
17635       --  Create an anonymous base type
17636
17637       Implicit_Base :=
17638         Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
17639
17640       --  Analyze and check the bounds, they can be of any integer type
17641
17642       Lo := Low_Bound (Def);
17643       Hi := High_Bound (Def);
17644
17645       --  Arbitrarily use Integer as the type if either bound had an error
17646
17647       if Hi = Error or else Lo = Error then
17648          Base_Typ := Any_Integer;
17649          Set_Error_Posted (T, True);
17650
17651       --  Here both bounds are OK expressions
17652
17653       else
17654          Analyze_And_Resolve (Lo, Any_Integer);
17655          Analyze_And_Resolve (Hi, Any_Integer);
17656
17657          Check_Bound (Lo);
17658          Check_Bound (Hi);
17659
17660          if Errs then
17661             Hi := Type_High_Bound (Standard_Long_Long_Integer);
17662             Lo := Type_Low_Bound (Standard_Long_Long_Integer);
17663          end if;
17664
17665          --  Find type to derive from
17666
17667          Lo_Val := Expr_Value (Lo);
17668          Hi_Val := Expr_Value (Hi);
17669
17670          if Can_Derive_From (Standard_Short_Short_Integer) then
17671             Base_Typ := Base_Type (Standard_Short_Short_Integer);
17672
17673          elsif Can_Derive_From (Standard_Short_Integer) then
17674             Base_Typ := Base_Type (Standard_Short_Integer);
17675
17676          elsif Can_Derive_From (Standard_Integer) then
17677             Base_Typ := Base_Type (Standard_Integer);
17678
17679          elsif Can_Derive_From (Standard_Long_Integer) then
17680             Base_Typ := Base_Type (Standard_Long_Integer);
17681
17682          elsif Can_Derive_From (Standard_Long_Long_Integer) then
17683             Base_Typ := Base_Type (Standard_Long_Long_Integer);
17684
17685          else
17686             Base_Typ := Base_Type (Standard_Long_Long_Integer);
17687             Error_Msg_N ("integer type definition bounds out of range", Def);
17688             Hi := Type_High_Bound (Standard_Long_Long_Integer);
17689             Lo := Type_Low_Bound (Standard_Long_Long_Integer);
17690          end if;
17691       end if;
17692
17693       --  Complete both implicit base and declared first subtype entities
17694
17695       Set_Etype          (Implicit_Base, Base_Typ);
17696       Set_Scalar_Range   (Implicit_Base, Scalar_Range   (Base_Typ));
17697       Set_Size_Info      (Implicit_Base,                (Base_Typ));
17698       Set_RM_Size        (Implicit_Base, RM_Size        (Base_Typ));
17699       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17700
17701       Set_Ekind          (T, E_Signed_Integer_Subtype);
17702       Set_Etype          (T, Implicit_Base);
17703
17704       Set_Size_Info      (T,                (Implicit_Base));
17705       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
17706       Set_Scalar_Range   (T, Def);
17707       Set_RM_Size        (T, UI_From_Int (Minimum_Size (T)));
17708       Set_Is_Constrained (T);
17709    end Signed_Integer_Type_Declaration;
17710
17711 end Sem_Ch3;