OSDN Git Service

2008-05-28 Robert Dewar <dewar@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_Disp; use Exp_Disp;
35 with Exp_Dist; use Exp_Dist;
36 with Exp_Tss;  use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Fname;    use Fname;
39 with Freeze;   use Freeze;
40 with Itypes;   use Itypes;
41 with Layout;   use Layout;
42 with Lib;      use Lib;
43 with Lib.Xref; use Lib.Xref;
44 with Namet;    use Namet;
45 with Nmake;    use Nmake;
46 with Opt;      use Opt;
47 with Restrict; use Restrict;
48 with Rident;   use Rident;
49 with Rtsfind;  use Rtsfind;
50 with Sem;      use Sem;
51 with Sem_Case; use Sem_Case;
52 with Sem_Cat;  use Sem_Cat;
53 with Sem_Ch6;  use Sem_Ch6;
54 with Sem_Ch7;  use Sem_Ch7;
55 with Sem_Ch8;  use Sem_Ch8;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Disp; use Sem_Disp;
58 with Sem_Dist; use Sem_Dist;
59 with Sem_Elim; use Sem_Elim;
60 with Sem_Eval; use Sem_Eval;
61 with Sem_Mech; use Sem_Mech;
62 with Sem_Res;  use Sem_Res;
63 with Sem_Smem; use Sem_Smem;
64 with Sem_Type; use Sem_Type;
65 with Sem_Util; use Sem_Util;
66 with Sem_Warn; use Sem_Warn;
67 with Stand;    use Stand;
68 with Sinfo;    use Sinfo;
69 with Snames;   use Snames;
70 with Targparm; use Targparm;
71 with Tbuild;   use Tbuild;
72 with Ttypes;   use Ttypes;
73 with Uintp;    use Uintp;
74 with Urealp;   use Urealp;
75
76 package body Sem_Ch3 is
77
78    -----------------------
79    -- Local Subprograms --
80    -----------------------
81
82    procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
83    --  Ada 2005 (AI-251): Add the tag components corresponding to all the
84    --  abstract interface types implemented by a record type or a derived
85    --  record type.
86
87    procedure Build_Derived_Type
88      (N             : Node_Id;
89       Parent_Type   : Entity_Id;
90       Derived_Type  : Entity_Id;
91       Is_Completion : Boolean;
92       Derive_Subps  : Boolean := True);
93    --  Create and decorate a Derived_Type given the Parent_Type entity. N is
94    --  the N_Full_Type_Declaration node containing the derived type definition.
95    --  Parent_Type is the entity for the parent type in the derived type
96    --  definition and Derived_Type the actual derived type. Is_Completion must
97    --  be set to False if Derived_Type is the N_Defining_Identifier node in N
98    --  (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
99    --  completion of a private type declaration. If Is_Completion is set to
100    --  True, N is the completion of a private type declaration and Derived_Type
101    --  is different from the defining identifier inside N (i.e. Derived_Type /=
102    --  Defining_Identifier (N)). Derive_Subps indicates whether the parent
103    --  subprograms should be derived. The only case where this parameter is
104    --  False is when Build_Derived_Type is recursively called to process an
105    --  implicit derived full type for a type derived from a private type (in
106    --  that case the subprograms must only be derived for the private view of
107    --  the type).
108    --
109    --  ??? These flags need a bit of re-examination and re-documentation:
110    --  ???  are they both necessary (both seem related to the recursion)?
111
112    procedure Build_Derived_Access_Type
113      (N            : Node_Id;
114       Parent_Type  : Entity_Id;
115       Derived_Type : Entity_Id);
116    --  Subsidiary procedure to Build_Derived_Type. For a derived access type,
117    --  create an implicit base if the parent type is constrained or if the
118    --  subtype indication has a constraint.
119
120    procedure Build_Derived_Array_Type
121      (N            : Node_Id;
122       Parent_Type  : Entity_Id;
123       Derived_Type : Entity_Id);
124    --  Subsidiary procedure to Build_Derived_Type. For a derived array type,
125    --  create an implicit base if the parent type is constrained or if the
126    --  subtype indication has a constraint.
127
128    procedure Build_Derived_Concurrent_Type
129      (N            : Node_Id;
130       Parent_Type  : Entity_Id;
131       Derived_Type : Entity_Id);
132    --  Subsidiary procedure to Build_Derived_Type. For a derived task or
133    --  protected type, inherit entries and protected subprograms, check
134    --  legality of discriminant constraints if any.
135
136    procedure Build_Derived_Enumeration_Type
137      (N            : Node_Id;
138       Parent_Type  : Entity_Id;
139       Derived_Type : Entity_Id);
140    --  Subsidiary procedure to Build_Derived_Type. For a derived enumeration
141    --  type, we must create a new list of literals. Types derived from
142    --  Character and Wide_Character are special-cased.
143
144    procedure Build_Derived_Numeric_Type
145      (N            : Node_Id;
146       Parent_Type  : Entity_Id;
147       Derived_Type : Entity_Id);
148    --  Subsidiary procedure to Build_Derived_Type. For numeric types, create
149    --  an anonymous base type, and propagate constraint to subtype if needed.
150
151    procedure Build_Derived_Private_Type
152      (N             : Node_Id;
153       Parent_Type   : Entity_Id;
154       Derived_Type  : Entity_Id;
155       Is_Completion : Boolean;
156       Derive_Subps  : Boolean := True);
157    --  Subsidiary procedure to Build_Derived_Type. This procedure is complex
158    --  because the parent may or may not have a completion, and the derivation
159    --  may itself be a completion.
160
161    procedure Build_Derived_Record_Type
162      (N            : Node_Id;
163       Parent_Type  : Entity_Id;
164       Derived_Type : Entity_Id;
165       Derive_Subps : Boolean := True);
166    --  Subsidiary procedure for Build_Derived_Type and
167    --  Analyze_Private_Extension_Declaration used for tagged and untagged
168    --  record types. All parameters are as in Build_Derived_Type except that
169    --  N, in addition to being an N_Full_Type_Declaration node, can also be an
170    --  N_Private_Extension_Declaration node. See the definition of this routine
171    --  for much more info. Derive_Subps indicates whether subprograms should
172    --  be derived from the parent type. The only case where Derive_Subps is
173    --  False is for an implicit derived full type for a type derived from a
174    --  private type (see Build_Derived_Type).
175
176    procedure Build_Discriminal (Discrim : Entity_Id);
177    --  Create the discriminal corresponding to discriminant Discrim, that is
178    --  the parameter corresponding to Discrim to be used in initialization
179    --  procedures for the type where Discrim is a discriminant. Discriminals
180    --  are not used during semantic analysis, and are not fully defined
181    --  entities until expansion. Thus they are not given a scope until
182    --  initialization procedures are built.
183
184    function Build_Discriminant_Constraints
185      (T           : Entity_Id;
186       Def         : Node_Id;
187       Derived_Def : Boolean := False) return Elist_Id;
188    --  Validate discriminant constraints and return the list of the constraints
189    --  in order of discriminant declarations, where T is the discriminated
190    --  unconstrained type. Def is the N_Subtype_Indication node where the
191    --  discriminants constraints for T are specified. Derived_Def is True
192    --  when building the discriminant constraints in a derived type definition
193    --  of the form "type D (...) is new T (xxx)". In this case T is the parent
194    --  type and Def is the constraint "(xxx)" on T and this routine sets the
195    --  Corresponding_Discriminant field of the discriminants in the derived
196    --  type D to point to the corresponding discriminants in the parent type T.
197
198    procedure Build_Discriminated_Subtype
199      (T           : Entity_Id;
200       Def_Id      : Entity_Id;
201       Elist       : Elist_Id;
202       Related_Nod : Node_Id;
203       For_Access  : Boolean := False);
204    --  Subsidiary procedure to Constrain_Discriminated_Type and to
205    --  Process_Incomplete_Dependents. Given
206    --
207    --     T (a possibly discriminated base type)
208    --     Def_Id (a very partially built subtype for T),
209    --
210    --  the call completes Def_Id to be the appropriate E_*_Subtype.
211    --
212    --  The Elist is the list of discriminant constraints if any (it is set
213    --  to No_Elist if T is not a discriminated type, and to an empty list if
214    --  T has discriminants but there are no discriminant constraints). The
215    --  Related_Nod is the same as Decl_Node in Create_Constrained_Components.
216    --  The For_Access says whether or not this subtype is really constraining
217    --  an access type. That is its sole purpose is the designated type of an
218    --  access type -- in which case a Private_Subtype Is_For_Access_Subtype
219    --  is built to avoid freezing T when the access subtype is frozen.
220
221    function Build_Scalar_Bound
222      (Bound : Node_Id;
223       Par_T : Entity_Id;
224       Der_T : Entity_Id) return Node_Id;
225    --  The bounds of a derived scalar type are conversions of the bounds of
226    --  the parent type. Optimize the representation if the bounds are literals.
227    --  Needs a more complete spec--what are the parameters exactly, and what
228    --  exactly is the returned value, and how is Bound affected???
229
230    procedure Build_Itype_Reference
231      (Ityp : Entity_Id;
232       Nod  : Node_Id);
233    --  Create a reference to an internal type, for use by Gigi. The back-end
234    --  elaborates itypes on demand, i.e. when their first use is seen. This
235    --  can lead to scope anomalies if the first use is within a scope that is
236    --  nested within the scope that contains  the point of definition of the
237    --  itype. The Itype_Reference node forces the elaboration of the itype
238    --  in the proper scope. The node is inserted after Nod, which is the
239    --  enclosing declaration that generated Ityp.
240    --
241    --  A related mechanism is used during expansion, for itypes created in
242    --  branches of conditionals. See Ensure_Defined in exp_util.
243    --  Could both mechanisms be merged ???
244
245    procedure Build_Underlying_Full_View
246      (N   : Node_Id;
247       Typ : Entity_Id;
248       Par : Entity_Id);
249    --  If the completion of a private type is itself derived from a private
250    --  type, or if the full view of a private subtype is itself private, the
251    --  back-end has no way to compute the actual size of this type. We build
252    --  an internal subtype declaration of the proper parent type to convey
253    --  this information. This extra mechanism is needed because a full
254    --  view cannot itself have a full view (it would get clobbered during
255    --  view exchanges).
256
257    procedure Check_Access_Discriminant_Requires_Limited
258      (D   : Node_Id;
259       Loc : Node_Id);
260    --  Check the restriction that the type to which an access discriminant
261    --  belongs must be a concurrent type or a descendant of a type with
262    --  the reserved word 'limited' in its declaration.
263
264    procedure Check_Anonymous_Access_Components
265       (Typ_Decl  : Node_Id;
266        Typ       : Entity_Id;
267        Prev      : Entity_Id;
268        Comp_List : Node_Id);
269    --  Ada 2005 AI-382: an access component in a record definition can refer to
270    --  the enclosing record, in which case it denotes the type itself, and not
271    --  the current instance of the type. We create an anonymous access type for
272    --  the component, and flag it as an access to a component, so accessibility
273    --  checks are properly performed on it. The declaration of the access type
274    --  is placed ahead of that of the record to prevent order-of-elaboration
275    --  circularity issues in Gigi. We create an incomplete type for the record
276    --  declaration, which is the designated type of the anonymous access.
277
278    procedure Check_Delta_Expression (E : Node_Id);
279    --  Check that the expression represented by E is suitable for use as a
280    --  delta expression, i.e. it is of real type and is static.
281
282    procedure Check_Digits_Expression (E : Node_Id);
283    --  Check that the expression represented by E is suitable for use as a
284    --  digits expression, i.e. it is of integer type, positive and static.
285
286    procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
287    --  Validate the initialization of an object declaration. T is the required
288    --  type, and Exp is the initialization expression.
289
290    procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
291    --  Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
292
293    procedure Check_Or_Process_Discriminants
294      (N    : Node_Id;
295       T    : Entity_Id;
296       Prev : Entity_Id := Empty);
297    --  If T is the full declaration of an incomplete or private type, check the
298    --  conformance of the discriminants, otherwise process them. Prev is the
299    --  entity of the partial declaration, if any.
300
301    procedure Check_Real_Bound (Bound : Node_Id);
302    --  Check given bound for being of real type and static. If not, post an
303    --  appropriate message, and rewrite the bound with the real literal zero.
304
305    procedure Constant_Redeclaration
306      (Id : Entity_Id;
307       N  : Node_Id;
308       T  : out Entity_Id);
309    --  Various checks on legality of full declaration of deferred constant.
310    --  Id is the entity for the redeclaration, N is the N_Object_Declaration,
311    --  node. The caller has not yet set any attributes of this entity.
312
313    function Contain_Interface
314      (Iface  : Entity_Id;
315       Ifaces : Elist_Id) return Boolean;
316    --  Ada 2005: Determine whether Iface is present in the list Ifaces
317
318    procedure Convert_Scalar_Bounds
319      (N            : Node_Id;
320       Parent_Type  : Entity_Id;
321       Derived_Type : Entity_Id;
322       Loc          : Source_Ptr);
323    --  For derived scalar types, convert the bounds in the type definition to
324    --  the derived type, and complete their analysis. Given a constraint of the
325    --  form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
326    --  T'Base, the parent_type. The bounds of the derived type (the anonymous
327    --  base) are copies of Lo and Hi. Finally, the bounds of the derived
328    --  subtype are conversions of those bounds to the derived_type, so that
329    --  their typing is consistent.
330
331    procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
332    --  Copies attributes from array base type T2 to array base type T1. Copies
333    --  only attributes that apply to base types, but not subtypes.
334
335    procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
336    --  Copies attributes from array subtype T2 to array subtype T1. Copies
337    --  attributes that apply to both subtypes and base types.
338
339    procedure Create_Constrained_Components
340      (Subt        : Entity_Id;
341       Decl_Node   : Node_Id;
342       Typ         : Entity_Id;
343       Constraints : Elist_Id);
344    --  Build the list of entities for a constrained discriminated record
345    --  subtype. If a component depends on a discriminant, replace its subtype
346    --  using the discriminant values in the discriminant constraint. Subt
347    --  is the defining identifier for the subtype whose list of constrained
348    --  entities we will create. Decl_Node is the type declaration node where
349    --  we will attach all the itypes created. Typ is the base discriminated
350    --  type for the subtype Subt. Constraints is the list of discriminant
351    --  constraints for Typ.
352
353    function Constrain_Component_Type
354      (Comp            : Entity_Id;
355       Constrained_Typ : Entity_Id;
356       Related_Node    : Node_Id;
357       Typ             : Entity_Id;
358       Constraints     : Elist_Id) return Entity_Id;
359    --  Given a discriminated base type Typ, a list of discriminant constraint
360    --  Constraints for Typ and a component of Typ, with type Compon_Type,
361    --  create and return the type corresponding to Compon_type where all
362    --  discriminant references are replaced with the corresponding constraint.
363    --  If no discriminant references occur in Compon_Typ then return it as is.
364    --  Constrained_Typ is the final constrained subtype to which the
365    --  constrained Compon_Type belongs. Related_Node is the node where we will
366    --  attach all the itypes created.
367    --
368    --  Above description is confused, what is Compon_Type???
369
370    procedure Constrain_Access
371      (Def_Id      : in out Entity_Id;
372       S           : Node_Id;
373       Related_Nod : Node_Id);
374    --  Apply a list of constraints to an access type. If Def_Id is empty, it is
375    --  an anonymous type created for a subtype indication. In that case it is
376    --  created in the procedure and attached to Related_Nod.
377
378    procedure Constrain_Array
379      (Def_Id      : in out Entity_Id;
380       SI          : Node_Id;
381       Related_Nod : Node_Id;
382       Related_Id  : Entity_Id;
383       Suffix      : Character);
384    --  Apply a list of index constraints to an unconstrained array type. The
385    --  first parameter is the entity for the resulting subtype. A value of
386    --  Empty for Def_Id indicates that an implicit type must be created, but
387    --  creation is delayed (and must be done by this procedure) because other
388    --  subsidiary implicit types must be created first (which is why Def_Id
389    --  is an in/out parameter). The second parameter is a subtype indication
390    --  node for the constrained array to be created (e.g. something of the
391    --  form string (1 .. 10)). Related_Nod gives the place where this type
392    --  has to be inserted in the tree. The Related_Id and Suffix parameters
393    --  are used to build the associated Implicit type name.
394
395    procedure Constrain_Concurrent
396      (Def_Id      : in out Entity_Id;
397       SI          : Node_Id;
398       Related_Nod : Node_Id;
399       Related_Id  : Entity_Id;
400       Suffix      : Character);
401    --  Apply list of discriminant constraints to an unconstrained concurrent
402    --  type.
403    --
404    --    SI is the N_Subtype_Indication node containing the constraint and
405    --    the unconstrained type to constrain.
406    --
407    --    Def_Id is the entity for the resulting constrained subtype. A value
408    --    of Empty for Def_Id indicates that an implicit type must be created,
409    --    but creation is delayed (and must be done by this procedure) because
410    --    other subsidiary implicit types must be created first (which is why
411    --    Def_Id is an in/out parameter).
412    --
413    --    Related_Nod gives the place where this type has to be inserted
414    --    in the tree
415    --
416    --  The last two arguments are used to create its external name if needed.
417
418    function Constrain_Corresponding_Record
419      (Prot_Subt   : Entity_Id;
420       Corr_Rec    : Entity_Id;
421       Related_Nod : Node_Id;
422       Related_Id  : Entity_Id) return Entity_Id;
423    --  When constraining a protected type or task type with discriminants,
424    --  constrain the corresponding record with the same discriminant values.
425
426    procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
427    --  Constrain a decimal fixed point type with a digits constraint and/or a
428    --  range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
429
430    procedure Constrain_Discriminated_Type
431      (Def_Id      : Entity_Id;
432       S           : Node_Id;
433       Related_Nod : Node_Id;
434       For_Access  : Boolean := False);
435    --  Process discriminant constraints of composite type. Verify that values
436    --  have been provided for all discriminants, that the original type is
437    --  unconstrained, and that the types of the supplied expressions match
438    --  the discriminant types. The first three parameters are like in routine
439    --  Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
440    --  of For_Access.
441
442    procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
443    --  Constrain an enumeration type with a range constraint. This is identical
444    --  to Constrain_Integer, but for the Ekind of the resulting subtype.
445
446    procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
447    --  Constrain a floating point type with either a digits constraint
448    --  and/or a range constraint, building a E_Floating_Point_Subtype.
449
450    procedure Constrain_Index
451      (Index        : Node_Id;
452       S            : Node_Id;
453       Related_Nod  : Node_Id;
454       Related_Id   : Entity_Id;
455       Suffix       : Character;
456       Suffix_Index : Nat);
457    --  Process an index constraint in a constrained array declaration. The
458    --  constraint can be a subtype name, or a range with or without an explicit
459    --  subtype mark. The index is the corresponding index of the unconstrained
460    --  array. The Related_Id and Suffix parameters are used to build the
461    --  associated Implicit type name.
462
463    procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
464    --  Build subtype of a signed or modular integer type
465
466    procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
467    --  Constrain an ordinary fixed point type with a range constraint, and
468    --  build an E_Ordinary_Fixed_Point_Subtype entity.
469
470    procedure Copy_And_Swap (Priv, Full : Entity_Id);
471    --  Copy the Priv entity into the entity of its full declaration then swap
472    --  the two entities in such a manner that the former private type is now
473    --  seen as a full type.
474
475    procedure Decimal_Fixed_Point_Type_Declaration
476      (T   : Entity_Id;
477       Def : Node_Id);
478    --  Create a new decimal fixed point type, and apply the constraint to
479    --  obtain a subtype of this new type.
480
481    procedure Complete_Private_Subtype
482      (Priv        : Entity_Id;
483       Full        : Entity_Id;
484       Full_Base   : Entity_Id;
485       Related_Nod : Node_Id);
486    --  Complete the implicit full view of a private subtype by setting the
487    --  appropriate semantic fields. If the full view of the parent is a record
488    --  type, build constrained components of subtype.
489
490    procedure Derive_Progenitor_Subprograms
491      (Parent_Type : Entity_Id;
492       Tagged_Type : Entity_Id);
493    --  Ada 2005 (AI-251): To complete type derivation, collect the primitive
494    --  operations of progenitors of Tagged_Type, and replace the subsidiary
495    --  subtypes with Tagged_Type, to build the specs of the inherited interface
496    --  primitives. The derived primitives are aliased to those of the
497    --  interface. This routine takes care also of transferring to the full-view
498    --  subprograms associated with the partial-view of Tagged_Type that cover
499    --  interface primitives.
500
501    procedure Derived_Standard_Character
502      (N             : Node_Id;
503       Parent_Type   : Entity_Id;
504       Derived_Type  : Entity_Id);
505    --  Subsidiary procedure to Build_Derived_Enumeration_Type which handles
506    --  derivations from types Standard.Character and Standard.Wide_Character.
507
508    procedure Derived_Type_Declaration
509      (T             : Entity_Id;
510       N             : Node_Id;
511       Is_Completion : Boolean);
512    --  Process a derived type declaration. Build_Derived_Type is invoked
513    --  to process the actual derived type definition. Parameters N and
514    --  Is_Completion have the same meaning as in Build_Derived_Type.
515    --  T is the N_Defining_Identifier for the entity defined in the
516    --  N_Full_Type_Declaration node N, that is T is the derived type.
517
518    procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
519    --  Insert each literal in symbol table, as an overloadable identifier. Each
520    --  enumeration type is mapped into a sequence of integers, and each literal
521    --  is defined as a constant with integer value. If any of the literals are
522    --  character literals, the type is a character type, which means that
523    --  strings are legal aggregates for arrays of components of the type.
524
525    function Expand_To_Stored_Constraint
526      (Typ        : Entity_Id;
527       Constraint : Elist_Id) return Elist_Id;
528    --  Given a constraint (i.e. a list of expressions) on the discriminants of
529    --  Typ, expand it into a constraint on the stored discriminants and return
530    --  the new list of expressions constraining the stored discriminants.
531
532    function Find_Type_Of_Object
533      (Obj_Def     : Node_Id;
534       Related_Nod : Node_Id) return Entity_Id;
535    --  Get type entity for object referenced by Obj_Def, attaching the
536    --  implicit types generated to Related_Nod
537
538    procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
539    --  Create a new float and apply the constraint to obtain subtype of it
540
541    function Has_Range_Constraint (N : Node_Id) return Boolean;
542    --  Given an N_Subtype_Indication node N, return True if a range constraint
543    --  is present, either directly, or as part of a digits or delta constraint.
544    --  In addition, a digits constraint in the decimal case returns True, since
545    --  it establishes a default range if no explicit range is present.
546
547    function Inherit_Components
548      (N             : Node_Id;
549       Parent_Base   : Entity_Id;
550       Derived_Base  : Entity_Id;
551       Is_Tagged     : Boolean;
552       Inherit_Discr : Boolean;
553       Discs         : Elist_Id) return Elist_Id;
554    --  Called from Build_Derived_Record_Type to inherit the components of
555    --  Parent_Base (a base type) into the Derived_Base (the derived base type).
556    --  For more information on derived types and component inheritance please
557    --  consult the comment above the body of Build_Derived_Record_Type.
558    --
559    --    N is the original derived type declaration
560    --
561    --    Is_Tagged is set if we are dealing with tagged types
562    --
563    --    If Inherit_Discr is set, Derived_Base inherits its discriminants from
564    --    Parent_Base, otherwise no discriminants are inherited.
565    --
566    --    Discs gives the list of constraints that apply to Parent_Base in the
567    --    derived type declaration. If Discs is set to No_Elist, then we have
568    --    the following situation:
569    --
570    --      type Parent (D1..Dn : ..) is [tagged] record ...;
571    --      type Derived is new Parent [with ...];
572    --
573    --    which gets treated as
574    --
575    --      type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
576    --
577    --  For untagged types the returned value is an association list. The list
578    --  starts from the association (Parent_Base => Derived_Base), and then it
579    --  contains a sequence of the associations of the form
580    --
581    --    (Old_Component => New_Component),
582    --
583    --  where Old_Component is the Entity_Id of a component in Parent_Base and
584    --  New_Component is the Entity_Id of the corresponding component in
585    --  Derived_Base. For untagged records, this association list is needed when
586    --  copying the record declaration for the derived base. In the tagged case
587    --  the value returned is irrelevant.
588
589    function Is_Progenitor
590      (Iface : Entity_Id;
591       Typ   :  Entity_Id) return Boolean;
592    --  Determine whether type Typ implements interface Iface. This requires
593    --  traversing the list of abstract interfaces of the type, as well as that
594    --  of the ancestor types. The predicate is used to determine when a formal
595    --  in the signature of an inherited operation must carry the derived type.
596
597    function Is_Valid_Constraint_Kind
598      (T_Kind          : Type_Kind;
599       Constraint_Kind : Node_Kind) return Boolean;
600    --  Returns True if it is legal to apply the given kind of constraint to the
601    --  given kind of type (index constraint to an array type, for example).
602
603    procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
604    --  Create new modular type. Verify that modulus is in  bounds and is
605    --  a power of two (implementation restriction).
606
607    procedure New_Concatenation_Op (Typ : Entity_Id);
608    --  Create an abbreviated declaration for an operator in order to
609    --  materialize concatenation on array types.
610
611    procedure Ordinary_Fixed_Point_Type_Declaration
612      (T   : Entity_Id;
613       Def : Node_Id);
614    --  Create a new ordinary fixed point type, and apply the constraint to
615    --  obtain subtype of it.
616
617    procedure Prepare_Private_Subtype_Completion
618      (Id          : Entity_Id;
619       Related_Nod : Node_Id);
620    --  Id is a subtype of some private type. Creates the full declaration
621    --  associated with Id whenever possible, i.e. when the full declaration
622    --  of the base type is already known. Records each subtype into
623    --  Private_Dependents of the base type.
624
625    procedure Process_Incomplete_Dependents
626      (N      : Node_Id;
627       Full_T : Entity_Id;
628       Inc_T  : Entity_Id);
629    --  Process all entities that depend on an incomplete type. There include
630    --  subtypes, subprogram types that mention the incomplete type in their
631    --  profiles, and subprogram with access parameters that designate the
632    --  incomplete type.
633
634    --  Inc_T is the defining identifier of an incomplete type declaration, its
635    --  Ekind is E_Incomplete_Type.
636    --
637    --    N is the corresponding N_Full_Type_Declaration for Inc_T.
638    --
639    --    Full_T is N's defining identifier.
640    --
641    --  Subtypes of incomplete types with discriminants are completed when the
642    --  parent type is. This is simpler than private subtypes, because they can
643    --  only appear in the same scope, and there is no need to exchange views.
644    --  Similarly, access_to_subprogram types may have a parameter or a return
645    --  type that is an incomplete type, and that must be replaced with the
646    --  full type.
647    --
648    --  If the full type is tagged, subprogram with access parameters that
649    --  designated the incomplete may be primitive operations of the full type,
650    --  and have to be processed accordingly.
651
652    procedure Process_Real_Range_Specification (Def : Node_Id);
653    --  Given the type definition for a real type, this procedure processes and
654    --  checks the real range specification of this type definition if one is
655    --  present. If errors are found, error messages are posted, and the
656    --  Real_Range_Specification of Def is reset to Empty.
657
658    procedure Record_Type_Declaration
659      (T    : Entity_Id;
660       N    : Node_Id;
661       Prev : Entity_Id);
662    --  Process a record type declaration (for both untagged and tagged
663    --  records). Parameters T and N are exactly like in procedure
664    --  Derived_Type_Declaration, except that no flag Is_Completion is needed
665    --  for this routine. If this is the completion of an incomplete type
666    --  declaration, Prev is the entity of the incomplete declaration, used for
667    --  cross-referencing. Otherwise Prev = T.
668
669    procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
670    --  This routine is used to process the actual record type definition (both
671    --  for untagged and tagged records). Def is a record type definition node.
672    --  This procedure analyzes the components in this record type definition.
673    --  Prev_T is the entity for the enclosing record type. It is provided so
674    --  that its Has_Task flag can be set if any of the component have Has_Task
675    --  set. If the declaration is the completion of an incomplete type
676    --  declaration, Prev_T is the original incomplete type, whose full view is
677    --  the record type.
678
679    procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
680    --  Subsidiary to Build_Derived_Record_Type. For untagged records, we
681    --  build a copy of the declaration tree of the parent, and we create
682    --  independently the list of components for the derived type. Semantic
683    --  information uses the component entities, but record representation
684    --  clauses are validated on the declaration tree. This procedure replaces
685    --  discriminants and components in the declaration with those that have
686    --  been created by Inherit_Components.
687
688    procedure Set_Fixed_Range
689      (E   : Entity_Id;
690       Loc : Source_Ptr;
691       Lo  : Ureal;
692       Hi  : Ureal);
693    --  Build a range node with the given bounds and set it as the Scalar_Range
694    --  of the given fixed-point type entity. Loc is the source location used
695    --  for the constructed range. See body for further details.
696
697    procedure Set_Scalar_Range_For_Subtype
698      (Def_Id : Entity_Id;
699       R      : Node_Id;
700       Subt   : Entity_Id);
701    --  This routine is used to set the scalar range field for a subtype given
702    --  Def_Id, the entity for the subtype, and R, the range expression for the
703    --  scalar range. Subt provides the parent subtype to be used to analyze,
704    --  resolve, and check the given range.
705
706    procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
707    --  Create a new signed integer entity, and apply the constraint to obtain
708    --  the required first named subtype of this type.
709
710    procedure Set_Stored_Constraint_From_Discriminant_Constraint
711      (E : Entity_Id);
712    --  E is some record type. This routine computes E's Stored_Constraint
713    --  from its Discriminant_Constraint.
714
715    procedure Diagnose_Interface (N : Node_Id;  E : Entity_Id);
716    --  Check that an entity in a list of progenitors is an interface,
717    --  emit error otherwise.
718
719    -----------------------
720    -- Access_Definition --
721    -----------------------
722
723    function Access_Definition
724      (Related_Nod : Node_Id;
725       N           : Node_Id) return Entity_Id
726    is
727       Loc        : constant Source_Ptr := Sloc (Related_Nod);
728       Anon_Type  : Entity_Id;
729       Anon_Scope : Entity_Id;
730       Desig_Type : Entity_Id;
731       Decl       : Entity_Id;
732
733    begin
734       if Is_Entry (Current_Scope)
735         and then Is_Task_Type (Etype (Scope (Current_Scope)))
736       then
737          Error_Msg_N ("task entries cannot have access parameters", N);
738          return Empty;
739       end if;
740
741       --  Ada 2005: for an object declaration the corresponding anonymous
742       --  type is declared in the current scope.
743
744       --  If the access definition is the return type of another access to
745       --  function, scope is the current one, because it is the one of the
746       --  current type declaration.
747
748       if Nkind_In (Related_Nod, N_Object_Declaration,
749                                 N_Access_Function_Definition)
750       then
751          Anon_Scope := Current_Scope;
752
753       --  For the anonymous function result case, retrieve the scope of the
754       --  function specification's associated entity rather than using the
755       --  current scope. The current scope will be the function itself if the
756       --  formal part is currently being analyzed, but will be the parent scope
757       --  in the case of a parameterless function, and we always want to use
758       --  the function's parent scope. Finally, if the function is a child
759       --  unit, we must traverse the tree to retrieve the proper entity.
760
761       elsif Nkind (Related_Nod) = N_Function_Specification
762         and then Nkind (Parent (N)) /= N_Parameter_Specification
763       then
764          --  If the current scope is a protected type, the anonymous access
765          --  is associated with one of the protected operations, and must
766          --  be available in the scope that encloses the protected declaration.
767          --  Otherwise the type is is in the scope enclosing the subprogram.
768
769          if Ekind (Current_Scope) = E_Protected_Type then
770             Anon_Scope := Scope (Scope (Defining_Entity (Related_Nod)));
771          else
772             Anon_Scope := Scope (Defining_Entity (Related_Nod));
773          end if;
774
775       else
776          --  For access formals, access components, and access discriminants,
777          --  the scope is that of the enclosing declaration,
778
779          Anon_Scope := Scope (Current_Scope);
780       end if;
781
782       Anon_Type :=
783         Create_Itype
784          (E_Anonymous_Access_Type, Related_Nod, Scope_Id =>  Anon_Scope);
785
786       if All_Present (N)
787         and then Ada_Version >= Ada_05
788       then
789          Error_Msg_N ("ALL is not permitted for anonymous access types", N);
790       end if;
791
792       --  Ada 2005 (AI-254): In case of anonymous access to subprograms call
793       --  the corresponding semantic routine
794
795       if Present (Access_To_Subprogram_Definition (N)) then
796          Access_Subprogram_Declaration
797            (T_Name => Anon_Type,
798             T_Def  => Access_To_Subprogram_Definition (N));
799
800          if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
801             Set_Ekind
802               (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
803          else
804             Set_Ekind
805               (Anon_Type, E_Anonymous_Access_Subprogram_Type);
806          end if;
807
808          Set_Can_Use_Internal_Rep
809            (Anon_Type, not Always_Compatible_Rep_On_Target);
810
811          --  If the anonymous access is associated with a protected operation
812          --  create a reference to it after the enclosing protected definition
813          --  because the itype will be used in the subsequent bodies.
814
815          if Ekind (Current_Scope) = E_Protected_Type then
816             Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
817          end if;
818
819          return Anon_Type;
820       end if;
821
822       Find_Type (Subtype_Mark (N));
823       Desig_Type := Entity (Subtype_Mark (N));
824
825       Set_Directly_Designated_Type
826                              (Anon_Type, Desig_Type);
827       Set_Etype              (Anon_Type, Anon_Type);
828
829       --  Make sure the anonymous access type has size and alignment fields
830       --  set, as required by gigi. This is necessary in the case of the
831       --  Task_Body_Procedure.
832
833       if not Has_Private_Component (Desig_Type) then
834          Layout_Type (Anon_Type);
835       end if;
836
837       --  ???The following makes no sense, because Anon_Type is an access type
838       --  and therefore cannot have components, private or otherwise. Hence
839       --  the assertion. Not sure what was meant, here.
840       Set_Depends_On_Private (Anon_Type, Has_Private_Component (Anon_Type));
841       pragma Assert (not Depends_On_Private (Anon_Type));
842
843       --  Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
844       --  from Ada 95 semantics. In Ada 2005, anonymous access must specify if
845       --  the null value is allowed. In Ada 95 the null value is never allowed.
846
847       if Ada_Version >= Ada_05 then
848          Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
849       else
850          Set_Can_Never_Be_Null (Anon_Type, True);
851       end if;
852
853       --  The anonymous access type is as public as the discriminated type or
854       --  subprogram that defines it. It is imported (for back-end purposes)
855       --  if the designated type is.
856
857       Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
858
859       --  Ada 2005 (AI-50217): Propagate the attribute that indicates that the
860       --  designated type comes from the limited view.
861
862       Set_From_With_Type (Anon_Type, From_With_Type (Desig_Type));
863
864       --  Ada 2005 (AI-231): Propagate the access-constant attribute
865
866       Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
867
868       --  The context is either a subprogram declaration, object declaration,
869       --  or an access discriminant, in a private or a full type declaration.
870       --  In the case of a subprogram, if the designated type is incomplete,
871       --  the operation will be a primitive operation of the full type, to be
872       --  updated subsequently. If the type is imported through a limited_with
873       --  clause, the subprogram is not a primitive operation of the type
874       --  (which is declared elsewhere in some other scope).
875
876       if Ekind (Desig_Type) = E_Incomplete_Type
877         and then not From_With_Type (Desig_Type)
878         and then Is_Overloadable (Current_Scope)
879       then
880          Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
881          Set_Has_Delayed_Freeze (Current_Scope);
882       end if;
883
884       --  Ada 2005: if the designated type is an interface that may contain
885       --  tasks, create a Master entity for the declaration. This must be done
886       --  before expansion of the full declaration, because the declaration may
887       --  include an expression that is an allocator, whose expansion needs the
888       --  proper Master for the created tasks.
889
890       if Nkind (Related_Nod) = N_Object_Declaration
891          and then Expander_Active
892       then
893          if Is_Interface (Desig_Type)
894            and then Is_Limited_Record (Desig_Type)
895          then
896             Build_Class_Wide_Master (Anon_Type);
897
898          --  Similarly, if the type is an anonymous access that designates
899          --  tasks, create a master entity for it in the current context.
900
901          elsif Has_Task (Desig_Type)
902            and then Comes_From_Source (Related_Nod)
903          then
904             if not Has_Master_Entity (Current_Scope) then
905                Decl :=
906                  Make_Object_Declaration (Loc,
907                    Defining_Identifier =>
908                      Make_Defining_Identifier (Loc, Name_uMaster),
909                    Constant_Present => True,
910                    Object_Definition =>
911                      New_Reference_To (RTE (RE_Master_Id), Loc),
912                    Expression =>
913                      Make_Explicit_Dereference (Loc,
914                        New_Reference_To (RTE (RE_Current_Master), Loc)));
915
916                Insert_Before (Related_Nod, Decl);
917                Analyze (Decl);
918
919                Set_Master_Id (Anon_Type, Defining_Identifier (Decl));
920                Set_Has_Master_Entity (Current_Scope);
921             else
922                Build_Master_Renaming (Related_Nod, Anon_Type);
923             end if;
924          end if;
925       end if;
926
927       --  For a private component of a protected type, it is imperative that
928       --  the back-end elaborate the type immediately after the protected
929       --  declaration, because this type will be used in the declarations
930       --  created for the component within each protected body, so we must
931       --  create an itype reference for it now.
932
933       if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
934          Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
935
936       --  Similarly, if the access definition is the return result of a
937       --  protected function, create an itype reference for it because it
938       --  will be used within the function body.
939
940       elsif Nkind (Related_Nod) = N_Function_Specification
941         and then  Ekind (Current_Scope) = E_Protected_Type
942       then
943          Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
944
945       --  Finally, create an itype reference for an object declaration of
946       --  an anonymous access type. This is strictly necessary only for
947       --  deferred constants, but in any case will avoid out-of-scope
948       --  problems in the back-end.
949
950       elsif Nkind (Related_Nod) = N_Object_Declaration then
951          Build_Itype_Reference (Anon_Type, Related_Nod);
952       end if;
953
954       return Anon_Type;
955    end Access_Definition;
956
957    -----------------------------------
958    -- Access_Subprogram_Declaration --
959    -----------------------------------
960
961    procedure Access_Subprogram_Declaration
962      (T_Name : Entity_Id;
963       T_Def  : Node_Id)
964    is
965
966       procedure Check_For_Premature_Usage (Def : Node_Id);
967       --  Check that type T_Name is not used, directly or recursively,
968       --  as a parameter or a return type in Def. Def is either a subtype,
969       --  an access_definition, or an access_to_subprogram_definition.
970
971       -------------------------------
972       -- Check_For_Premature_Usage --
973       -------------------------------
974
975       procedure Check_For_Premature_Usage (Def : Node_Id) is
976          Param : Node_Id;
977
978       begin
979          --  Check for a subtype mark
980
981          if Nkind (Def) in N_Has_Etype then
982             if Etype (Def) = T_Name then
983                Error_Msg_N
984                  ("type& cannot be used before end of its declaration", Def);
985             end if;
986
987          --  If this is not a subtype, then this is an access_definition
988
989          elsif Nkind (Def) = N_Access_Definition then
990             if Present (Access_To_Subprogram_Definition (Def)) then
991                Check_For_Premature_Usage
992                  (Access_To_Subprogram_Definition (Def));
993             else
994                Check_For_Premature_Usage (Subtype_Mark (Def));
995             end if;
996
997          --  The only cases left are N_Access_Function_Definition and
998          --  N_Access_Procedure_Definition.
999
1000          else
1001             if Present (Parameter_Specifications (Def)) then
1002                Param := First (Parameter_Specifications (Def));
1003                while Present (Param) loop
1004                   Check_For_Premature_Usage (Parameter_Type (Param));
1005                   Param := Next (Param);
1006                end loop;
1007             end if;
1008
1009             if Nkind (Def) = N_Access_Function_Definition then
1010                Check_For_Premature_Usage (Result_Definition (Def));
1011             end if;
1012          end if;
1013       end Check_For_Premature_Usage;
1014
1015       --  Local variables
1016
1017       Formals    : constant List_Id := Parameter_Specifications (T_Def);
1018       Formal     : Entity_Id;
1019       D_Ityp     : Node_Id;
1020       Desig_Type : constant Entity_Id :=
1021                      Create_Itype (E_Subprogram_Type, Parent (T_Def));
1022
1023    --  Start of processing for Access_Subprogram_Declaration
1024
1025    begin
1026       --  Associate the Itype node with the inner full-type declaration or
1027       --  subprogram spec. This is required to handle nested anonymous
1028       --  declarations. For example:
1029
1030       --      procedure P
1031       --       (X : access procedure
1032       --                     (Y : access procedure
1033       --                                   (Z : access T)))
1034
1035       D_Ityp := Associated_Node_For_Itype (Desig_Type);
1036       while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1037                                    N_Private_Type_Declaration,
1038                                    N_Private_Extension_Declaration,
1039                                    N_Procedure_Specification,
1040                                    N_Function_Specification)
1041                    or else
1042                  Nkind_In (D_Ityp, N_Object_Declaration,
1043                                    N_Object_Renaming_Declaration,
1044                                    N_Formal_Type_Declaration,
1045                                    N_Task_Type_Declaration,
1046                                    N_Protected_Type_Declaration))
1047       loop
1048          D_Ityp := Parent (D_Ityp);
1049          pragma Assert (D_Ityp /= Empty);
1050       end loop;
1051
1052       Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1053
1054       if Nkind_In (D_Ityp, N_Procedure_Specification,
1055                            N_Function_Specification)
1056       then
1057          Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1058
1059       elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1060                               N_Object_Declaration,
1061                               N_Object_Renaming_Declaration,
1062                               N_Formal_Type_Declaration)
1063       then
1064          Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1065       end if;
1066
1067       if Nkind (T_Def) = N_Access_Function_Definition then
1068          if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1069             declare
1070                Acc : constant Node_Id := Result_Definition (T_Def);
1071
1072             begin
1073                if Present (Access_To_Subprogram_Definition (Acc))
1074                  and then
1075                    Protected_Present (Access_To_Subprogram_Definition (Acc))
1076                then
1077                   Set_Etype
1078                     (Desig_Type,
1079                        Replace_Anonymous_Access_To_Protected_Subprogram
1080                          (T_Def));
1081
1082                else
1083                   Set_Etype
1084                     (Desig_Type,
1085                        Access_Definition (T_Def, Result_Definition (T_Def)));
1086                end if;
1087             end;
1088
1089          else
1090             Analyze (Result_Definition (T_Def));
1091             Set_Etype (Desig_Type, Entity (Result_Definition (T_Def)));
1092          end if;
1093
1094          if not (Is_Type (Etype (Desig_Type))) then
1095             Error_Msg_N
1096               ("expect type in function specification",
1097                Result_Definition (T_Def));
1098          end if;
1099
1100       else
1101          Set_Etype (Desig_Type, Standard_Void_Type);
1102       end if;
1103
1104       if Present (Formals) then
1105          Push_Scope (Desig_Type);
1106          Process_Formals (Formals, Parent (T_Def));
1107
1108          --  A bit of a kludge here, End_Scope requires that the parent
1109          --  pointer be set to something reasonable, but Itypes don't have
1110          --  parent pointers. So we set it and then unset it ??? If and when
1111          --  Itypes have proper parent pointers to their declarations, this
1112          --  kludge can be removed.
1113
1114          Set_Parent (Desig_Type, T_Name);
1115          End_Scope;
1116          Set_Parent (Desig_Type, Empty);
1117       end if;
1118
1119       --  Check for premature usage of the type being defined
1120
1121       Check_For_Premature_Usage (T_Def);
1122
1123       --  The return type and/or any parameter type may be incomplete. Mark
1124       --  the subprogram_type as depending on the incomplete type, so that
1125       --  it can be updated when the full type declaration is seen. This
1126       --  only applies to incomplete types declared in some enclosing scope,
1127       --  not to limited views from other packages.
1128
1129       if Present (Formals) then
1130          Formal := First_Formal (Desig_Type);
1131          while Present (Formal) loop
1132             if Ekind (Formal) /= E_In_Parameter
1133               and then Nkind (T_Def) = N_Access_Function_Definition
1134             then
1135                Error_Msg_N ("functions can only have IN parameters", Formal);
1136             end if;
1137
1138             if Ekind (Etype (Formal)) = E_Incomplete_Type
1139               and then In_Open_Scopes (Scope (Etype (Formal)))
1140             then
1141                Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1142                Set_Has_Delayed_Freeze (Desig_Type);
1143             end if;
1144
1145             Next_Formal (Formal);
1146          end loop;
1147       end if;
1148
1149       if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1150         and then not Has_Delayed_Freeze (Desig_Type)
1151       then
1152          Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1153          Set_Has_Delayed_Freeze (Desig_Type);
1154       end if;
1155
1156       Check_Delayed_Subprogram (Desig_Type);
1157
1158       if Protected_Present (T_Def) then
1159          Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1160          Set_Convention (Desig_Type, Convention_Protected);
1161       else
1162          Set_Ekind (T_Name, E_Access_Subprogram_Type);
1163       end if;
1164
1165       Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1166
1167       Set_Etype                    (T_Name, T_Name);
1168       Init_Size_Align              (T_Name);
1169       Set_Directly_Designated_Type (T_Name, Desig_Type);
1170
1171       --  Ada 2005 (AI-231): Propagate the null-excluding attribute
1172
1173       Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1174
1175       Check_Restriction (No_Access_Subprograms, T_Def);
1176    end Access_Subprogram_Declaration;
1177
1178    ----------------------------
1179    -- Access_Type_Declaration --
1180    ----------------------------
1181
1182    procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1183       S : constant Node_Id := Subtype_Indication (Def);
1184       P : constant Node_Id := Parent (Def);
1185
1186       Desig : Entity_Id;
1187       --  Designated type
1188
1189    begin
1190       --  Check for permissible use of incomplete type
1191
1192       if Nkind (S) /= N_Subtype_Indication then
1193          Analyze (S);
1194
1195          if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1196             Set_Directly_Designated_Type (T, Entity (S));
1197          else
1198             Set_Directly_Designated_Type (T,
1199               Process_Subtype (S, P, T, 'P'));
1200          end if;
1201
1202       else
1203          Set_Directly_Designated_Type (T,
1204            Process_Subtype (S, P, T, 'P'));
1205       end if;
1206
1207       if All_Present (Def) or Constant_Present (Def) then
1208          Set_Ekind (T, E_General_Access_Type);
1209       else
1210          Set_Ekind (T, E_Access_Type);
1211       end if;
1212
1213       if Base_Type (Designated_Type (T)) = T then
1214          Error_Msg_N ("access type cannot designate itself", S);
1215
1216       --  In Ada 2005, the type may have a limited view through some unit
1217       --  in its own context, allowing the following circularity that cannot
1218       --  be detected earlier
1219
1220       elsif Is_Class_Wide_Type (Designated_Type (T))
1221         and then Etype (Designated_Type (T)) = T
1222       then
1223          Error_Msg_N
1224            ("access type cannot designate its own classwide type", S);
1225
1226          --  Clean up indication of tagged status to prevent cascaded errors
1227
1228          Set_Is_Tagged_Type (T, False);
1229       end if;
1230
1231       Set_Etype (T, T);
1232
1233       --  If the type has appeared already in a with_type clause, it is
1234       --  frozen and the pointer size is already set. Else, initialize.
1235
1236       if not From_With_Type (T) then
1237          Init_Size_Align (T);
1238       end if;
1239
1240       Desig := Designated_Type (T);
1241
1242       --  If designated type is an imported tagged type, indicate that the
1243       --  access type is also imported, and therefore restricted in its use.
1244       --  The access type may already be imported, so keep setting otherwise.
1245
1246       --  Ada 2005 (AI-50217): If the non-limited view of the designated type
1247       --  is available, use it as the designated type of the access type, so
1248       --  that the back-end gets a usable entity.
1249
1250       if From_With_Type (Desig)
1251         and then Ekind (Desig) /= E_Access_Type
1252       then
1253          Set_From_With_Type (T);
1254       end if;
1255
1256       --  Note that Has_Task is always false, since the access type itself
1257       --  is not a task type. See Einfo for more description on this point.
1258       --  Exactly the same consideration applies to Has_Controlled_Component.
1259
1260       Set_Has_Task (T, False);
1261       Set_Has_Controlled_Component (T, False);
1262
1263       --  Initialize Associated_Final_Chain explicitly to Empty, to avoid
1264       --  problems where an incomplete view of this entity has been previously
1265       --  established by a limited with and an overlaid version of this field
1266       --  (Stored_Constraint) was initialized for the incomplete view.
1267
1268       Set_Associated_Final_Chain (T, Empty);
1269
1270       --  Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1271       --  attributes
1272
1273       Set_Can_Never_Be_Null  (T, Null_Exclusion_Present (Def));
1274       Set_Is_Access_Constant (T, Constant_Present (Def));
1275    end Access_Type_Declaration;
1276
1277    ----------------------------------
1278    -- Add_Interface_Tag_Components --
1279    ----------------------------------
1280
1281    procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1282       Loc      : constant Source_Ptr := Sloc (N);
1283       L        : List_Id;
1284       Last_Tag : Node_Id;
1285
1286       procedure Add_Tag (Iface : Entity_Id);
1287       --  Add tag for one of the progenitor interfaces
1288
1289       -------------
1290       -- Add_Tag --
1291       -------------
1292
1293       procedure Add_Tag (Iface : Entity_Id) is
1294          Decl   : Node_Id;
1295          Def    : Node_Id;
1296          Tag    : Entity_Id;
1297          Offset : Entity_Id;
1298
1299       begin
1300          pragma Assert (Is_Tagged_Type (Iface)
1301            and then Is_Interface (Iface));
1302
1303          Def :=
1304            Make_Component_Definition (Loc,
1305              Aliased_Present    => True,
1306              Subtype_Indication =>
1307                New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1308
1309          Tag := Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1310
1311          Decl :=
1312            Make_Component_Declaration (Loc,
1313              Defining_Identifier  => Tag,
1314              Component_Definition => Def);
1315
1316          Analyze_Component_Declaration (Decl);
1317
1318          Set_Analyzed (Decl);
1319          Set_Ekind               (Tag, E_Component);
1320          Set_Is_Tag              (Tag);
1321          Set_Is_Aliased          (Tag);
1322          Set_Related_Type        (Tag, Iface);
1323          Init_Component_Location (Tag);
1324
1325          pragma Assert (Is_Frozen (Iface));
1326
1327          Set_DT_Entry_Count    (Tag,
1328            DT_Entry_Count (First_Entity (Iface)));
1329
1330          if No (Last_Tag) then
1331             Prepend (Decl, L);
1332          else
1333             Insert_After (Last_Tag, Decl);
1334          end if;
1335
1336          Last_Tag := Decl;
1337
1338          --  If the ancestor has discriminants we need to give special support
1339          --  to store the offset_to_top value of the secondary dispatch tables.
1340          --  For this purpose we add a supplementary component just after the
1341          --  field that contains the tag associated with each secondary DT.
1342
1343          if Typ /= Etype (Typ)
1344            and then Has_Discriminants (Etype (Typ))
1345          then
1346             Def :=
1347               Make_Component_Definition (Loc,
1348                 Subtype_Indication =>
1349                   New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1350
1351             Offset :=
1352               Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1353
1354             Decl :=
1355               Make_Component_Declaration (Loc,
1356                 Defining_Identifier  => Offset,
1357                 Component_Definition => Def);
1358
1359             Analyze_Component_Declaration (Decl);
1360
1361             Set_Analyzed (Decl);
1362             Set_Ekind               (Offset, E_Component);
1363             Set_Is_Aliased          (Offset);
1364             Set_Related_Type        (Offset, Iface);
1365             Init_Component_Location (Offset);
1366             Insert_After (Last_Tag, Decl);
1367             Last_Tag := Decl;
1368          end if;
1369       end Add_Tag;
1370
1371       --  Local variables
1372
1373       Elmt : Elmt_Id;
1374       Ext  : Node_Id;
1375       Comp : Node_Id;
1376
1377    --  Start of processing for Add_Interface_Tag_Components
1378
1379    begin
1380       if not RTE_Available (RE_Interface_Tag) then
1381          Error_Msg
1382            ("(Ada 2005) interface types not supported by this run-time!",
1383             Sloc (N));
1384          return;
1385       end if;
1386
1387       if Ekind (Typ) /= E_Record_Type
1388         or else (Is_Concurrent_Record_Type (Typ)
1389                   and then Is_Empty_List (Abstract_Interface_List (Typ)))
1390         or else (not Is_Concurrent_Record_Type (Typ)
1391                   and then No (Interfaces (Typ))
1392                   and then Is_Empty_Elmt_List (Interfaces (Typ)))
1393       then
1394          return;
1395       end if;
1396
1397       --  Find the current last tag
1398
1399       if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1400          Ext := Record_Extension_Part (Type_Definition (N));
1401       else
1402          pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1403          Ext := Type_Definition (N);
1404       end if;
1405
1406       Last_Tag := Empty;
1407
1408       if not (Present (Component_List (Ext))) then
1409          Set_Null_Present (Ext, False);
1410          L := New_List;
1411          Set_Component_List (Ext,
1412            Make_Component_List (Loc,
1413              Component_Items => L,
1414              Null_Present => False));
1415       else
1416          if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1417             L := Component_Items
1418                    (Component_List
1419                      (Record_Extension_Part
1420                        (Type_Definition (N))));
1421          else
1422             L := Component_Items
1423                    (Component_List
1424                      (Type_Definition (N)));
1425          end if;
1426
1427          --  Find the last tag component
1428
1429          Comp := First (L);
1430          while Present (Comp) loop
1431             if Nkind (Comp) = N_Component_Declaration
1432               and then Is_Tag (Defining_Identifier (Comp))
1433             then
1434                Last_Tag := Comp;
1435             end if;
1436
1437             Next (Comp);
1438          end loop;
1439       end if;
1440
1441       --  At this point L references the list of components and Last_Tag
1442       --  references the current last tag (if any). Now we add the tag
1443       --  corresponding with all the interfaces that are not implemented
1444       --  by the parent.
1445
1446       if Present (Interfaces (Typ)) then
1447          Elmt := First_Elmt (Interfaces (Typ));
1448          while Present (Elmt) loop
1449             Add_Tag (Node (Elmt));
1450             Next_Elmt (Elmt);
1451          end loop;
1452       end if;
1453    end Add_Interface_Tag_Components;
1454
1455    -----------------------------------
1456    -- Analyze_Component_Declaration --
1457    -----------------------------------
1458
1459    procedure Analyze_Component_Declaration (N : Node_Id) is
1460       Id : constant Entity_Id := Defining_Identifier (N);
1461       E  : constant Node_Id   := Expression (N);
1462       T  : Entity_Id;
1463       P  : Entity_Id;
1464
1465       function Contains_POC (Constr : Node_Id) return Boolean;
1466       --  Determines whether a constraint uses the discriminant of a record
1467       --  type thus becoming a per-object constraint (POC).
1468
1469       function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1470       --  Typ is the type of the current component, check whether this type is
1471       --  a limited type. Used to validate declaration against that of
1472       --  enclosing record.
1473
1474       ------------------
1475       -- Contains_POC --
1476       ------------------
1477
1478       function Contains_POC (Constr : Node_Id) return Boolean is
1479       begin
1480          --  Prevent cascaded errors
1481
1482          if Error_Posted (Constr) then
1483             return False;
1484          end if;
1485
1486          case Nkind (Constr) is
1487             when N_Attribute_Reference =>
1488                return
1489                  Attribute_Name (Constr) = Name_Access
1490                    and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1491
1492             when N_Discriminant_Association =>
1493                return Denotes_Discriminant (Expression (Constr));
1494
1495             when N_Identifier =>
1496                return Denotes_Discriminant (Constr);
1497
1498             when N_Index_Or_Discriminant_Constraint =>
1499                declare
1500                   IDC : Node_Id;
1501
1502                begin
1503                   IDC := First (Constraints (Constr));
1504                   while Present (IDC) loop
1505
1506                      --  One per-object constraint is sufficient
1507
1508                      if Contains_POC (IDC) then
1509                         return True;
1510                      end if;
1511
1512                      Next (IDC);
1513                   end loop;
1514
1515                   return False;
1516                end;
1517
1518             when N_Range =>
1519                return Denotes_Discriminant (Low_Bound (Constr))
1520                         or else
1521                       Denotes_Discriminant (High_Bound (Constr));
1522
1523             when N_Range_Constraint =>
1524                return Denotes_Discriminant (Range_Expression (Constr));
1525
1526             when others =>
1527                return False;
1528
1529          end case;
1530       end Contains_POC;
1531
1532       ----------------------
1533       -- Is_Known_Limited --
1534       ----------------------
1535
1536       function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1537          P : constant Entity_Id := Etype (Typ);
1538          R : constant Entity_Id := Root_Type (Typ);
1539
1540       begin
1541          if Is_Limited_Record (Typ) then
1542             return True;
1543
1544          --  If the root type is limited (and not a limited interface)
1545          --  so is the current type
1546
1547          elsif Is_Limited_Record (R)
1548            and then
1549              (not Is_Interface (R)
1550                or else not Is_Limited_Interface (R))
1551          then
1552             return True;
1553
1554          --  Else the type may have a limited interface progenitor, but a
1555          --  limited record parent.
1556
1557          elsif R /= P
1558            and then Is_Limited_Record (P)
1559          then
1560             return True;
1561
1562          else
1563             return False;
1564          end if;
1565       end Is_Known_Limited;
1566
1567    --  Start of processing for Analyze_Component_Declaration
1568
1569    begin
1570       Generate_Definition (Id);
1571       Enter_Name (Id);
1572
1573       if Present (Subtype_Indication (Component_Definition (N))) then
1574          T := Find_Type_Of_Object
1575                 (Subtype_Indication (Component_Definition (N)), N);
1576
1577       --  Ada 2005 (AI-230): Access Definition case
1578
1579       else
1580          pragma Assert (Present
1581                           (Access_Definition (Component_Definition (N))));
1582
1583          T := Access_Definition
1584                 (Related_Nod => N,
1585                  N => Access_Definition (Component_Definition (N)));
1586          Set_Is_Local_Anonymous_Access (T);
1587
1588          --  Ada 2005 (AI-254)
1589
1590          if Present (Access_To_Subprogram_Definition
1591                       (Access_Definition (Component_Definition (N))))
1592            and then Protected_Present (Access_To_Subprogram_Definition
1593                                         (Access_Definition
1594                                           (Component_Definition (N))))
1595          then
1596             T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1597          end if;
1598       end if;
1599
1600       --  If the subtype is a constrained subtype of the enclosing record,
1601       --  (which must have a partial view) the back-end does not properly
1602       --  handle the recursion. Rewrite the component declaration with an
1603       --  explicit subtype indication, which is acceptable to Gigi. We can copy
1604       --  the tree directly because side effects have already been removed from
1605       --  discriminant constraints.
1606
1607       if Ekind (T) = E_Access_Subtype
1608         and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1609         and then Comes_From_Source (T)
1610         and then Nkind (Parent (T)) = N_Subtype_Declaration
1611         and then Etype (Directly_Designated_Type (T)) = Current_Scope
1612       then
1613          Rewrite
1614            (Subtype_Indication (Component_Definition (N)),
1615              New_Copy_Tree (Subtype_Indication (Parent (T))));
1616          T := Find_Type_Of_Object
1617                  (Subtype_Indication (Component_Definition (N)), N);
1618       end if;
1619
1620       --  If the component declaration includes a default expression, then we
1621       --  check that the component is not of a limited type (RM 3.7(5)),
1622       --  and do the special preanalysis of the expression (see section on
1623       --  "Handling of Default and Per-Object Expressions" in the spec of
1624       --  package Sem).
1625
1626       if Present (E) then
1627          Preanalyze_Spec_Expression (E, T);
1628          Check_Initialization (T, E);
1629
1630          if Ada_Version >= Ada_05
1631            and then Ekind (T) = E_Anonymous_Access_Type
1632          then
1633             --  Check RM 3.9.2(9): "if the expected type for an expression is
1634             --  an anonymous access-to-specific tagged type, then the object
1635             --  designated by the expression shall not be dynamically tagged
1636             --  unless it is a controlling operand in a call on a dispatching
1637             --  operation"
1638
1639             if Is_Tagged_Type (Directly_Designated_Type (T))
1640               and then
1641                 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1642               and then
1643                 Ekind (Directly_Designated_Type (Etype (E))) =
1644                   E_Class_Wide_Type
1645             then
1646                Error_Msg_N
1647                  ("access to specific tagged type required (RM 3.9.2(9))", E);
1648             end if;
1649
1650             --  (Ada 2005: AI-230): Accessibility check for anonymous
1651             --  components
1652
1653             if Type_Access_Level (Etype (E)) > Type_Access_Level (T) then
1654                Error_Msg_N
1655                  ("expression has deeper access level than component " &
1656                   "(RM 3.10.2 (12.2))", E);
1657             end if;
1658
1659             --  The initialization expression is a reference to an access
1660             --  discriminant. The type of the discriminant is always deeper
1661             --  than any access type.
1662
1663             if Ekind (Etype (E)) = E_Anonymous_Access_Type
1664               and then Is_Entity_Name (E)
1665               and then Ekind (Entity (E)) = E_In_Parameter
1666               and then Present (Discriminal_Link (Entity (E)))
1667             then
1668                Error_Msg_N
1669                  ("discriminant has deeper accessibility level than target",
1670                   E);
1671             end if;
1672          end if;
1673       end if;
1674
1675       --  The parent type may be a private view with unknown discriminants,
1676       --  and thus unconstrained. Regular components must be constrained.
1677
1678       if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1679          if Is_Class_Wide_Type (T) then
1680             Error_Msg_N
1681                ("class-wide subtype with unknown discriminants" &
1682                  " in component declaration",
1683                  Subtype_Indication (Component_Definition (N)));
1684          else
1685             Error_Msg_N
1686               ("unconstrained subtype in component declaration",
1687                Subtype_Indication (Component_Definition (N)));
1688          end if;
1689
1690       --  Components cannot be abstract, except for the special case of
1691       --  the _Parent field (case of extending an abstract tagged type)
1692
1693       elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
1694          Error_Msg_N ("type of a component cannot be abstract", N);
1695       end if;
1696
1697       Set_Etype (Id, T);
1698       Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1699
1700       --  The component declaration may have a per-object constraint, set
1701       --  the appropriate flag in the defining identifier of the subtype.
1702
1703       if Present (Subtype_Indication (Component_Definition (N))) then
1704          declare
1705             Sindic : constant Node_Id :=
1706                        Subtype_Indication (Component_Definition (N));
1707          begin
1708             if Nkind (Sindic) = N_Subtype_Indication
1709               and then Present (Constraint (Sindic))
1710               and then Contains_POC (Constraint (Sindic))
1711             then
1712                Set_Has_Per_Object_Constraint (Id);
1713             end if;
1714          end;
1715       end if;
1716
1717       --  Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1718       --  out some static checks.
1719
1720       if Ada_Version >= Ada_05
1721         and then Can_Never_Be_Null (T)
1722       then
1723          Null_Exclusion_Static_Checks (N);
1724       end if;
1725
1726       --  If this component is private (or depends on a private type), flag the
1727       --  record type to indicate that some operations are not available.
1728
1729       P := Private_Component (T);
1730
1731       if Present (P) then
1732
1733          --  Check for circular definitions
1734
1735          if P = Any_Type then
1736             Set_Etype (Id, Any_Type);
1737
1738          --  There is a gap in the visibility of operations only if the
1739          --  component type is not defined in the scope of the record type.
1740
1741          elsif Scope (P) = Scope (Current_Scope) then
1742             null;
1743
1744          elsif Is_Limited_Type (P) then
1745             Set_Is_Limited_Composite (Current_Scope);
1746
1747          else
1748             Set_Is_Private_Composite (Current_Scope);
1749          end if;
1750       end if;
1751
1752       if P /= Any_Type
1753         and then Is_Limited_Type (T)
1754         and then Chars (Id) /= Name_uParent
1755         and then Is_Tagged_Type (Current_Scope)
1756       then
1757          if Is_Derived_Type (Current_Scope)
1758            and then not Is_Known_Limited (Current_Scope)
1759          then
1760             Error_Msg_N
1761               ("extension of nonlimited type cannot have limited components",
1762                N);
1763
1764             if Is_Interface (Root_Type (Current_Scope)) then
1765                Error_Msg_N
1766                  ("\limitedness is not inherited from limited interface", N);
1767                Error_Msg_N
1768                  ("\add LIMITED to type indication", N);
1769             end if;
1770
1771             Explain_Limited_Type (T, N);
1772             Set_Etype (Id, Any_Type);
1773             Set_Is_Limited_Composite (Current_Scope, False);
1774
1775          elsif not Is_Derived_Type (Current_Scope)
1776            and then not Is_Limited_Record (Current_Scope)
1777            and then not Is_Concurrent_Type (Current_Scope)
1778          then
1779             Error_Msg_N
1780               ("nonlimited tagged type cannot have limited components", N);
1781             Explain_Limited_Type (T, N);
1782             Set_Etype (Id, Any_Type);
1783             Set_Is_Limited_Composite (Current_Scope, False);
1784          end if;
1785       end if;
1786
1787       Set_Original_Record_Component (Id, Id);
1788    end Analyze_Component_Declaration;
1789
1790    --------------------------
1791    -- Analyze_Declarations --
1792    --------------------------
1793
1794    procedure Analyze_Declarations (L : List_Id) is
1795       D           : Node_Id;
1796       Freeze_From : Entity_Id := Empty;
1797       Next_Node   : Node_Id;
1798
1799       procedure Adjust_D;
1800       --  Adjust D not to include implicit label declarations, since these
1801       --  have strange Sloc values that result in elaboration check problems.
1802       --  (They have the sloc of the label as found in the source, and that
1803       --  is ahead of the current declarative part).
1804
1805       --------------
1806       -- Adjust_D --
1807       --------------
1808
1809       procedure Adjust_D is
1810       begin
1811          while Present (Prev (D))
1812            and then Nkind (D) = N_Implicit_Label_Declaration
1813          loop
1814             Prev (D);
1815          end loop;
1816       end Adjust_D;
1817
1818    --  Start of processing for Analyze_Declarations
1819
1820    begin
1821       D := First (L);
1822       while Present (D) loop
1823
1824          --  Complete analysis of declaration
1825
1826          Analyze (D);
1827          Next_Node := Next (D);
1828
1829          if No (Freeze_From) then
1830             Freeze_From := First_Entity (Current_Scope);
1831          end if;
1832
1833          --  At the end of a declarative part, freeze remaining entities
1834          --  declared in it. The end of the visible declarations of package
1835          --  specification is not the end of a declarative part if private
1836          --  declarations are present. The end of a package declaration is a
1837          --  freezing point only if it a library package. A task definition or
1838          --  protected type definition is not a freeze point either. Finally,
1839          --  we do not freeze entities in generic scopes, because there is no
1840          --  code generated for them and freeze nodes will be generated for
1841          --  the instance.
1842
1843          --  The end of a package instantiation is not a freeze point, but
1844          --  for now we make it one, because the generic body is inserted
1845          --  (currently) immediately after. Generic instantiations will not
1846          --  be a freeze point once delayed freezing of bodies is implemented.
1847          --  (This is needed in any case for early instantiations ???).
1848
1849          if No (Next_Node) then
1850             if Nkind_In (Parent (L), N_Component_List,
1851                                      N_Task_Definition,
1852                                      N_Protected_Definition)
1853             then
1854                null;
1855
1856             elsif Nkind (Parent (L)) /= N_Package_Specification then
1857                if Nkind (Parent (L)) = N_Package_Body then
1858                   Freeze_From := First_Entity (Current_Scope);
1859                end if;
1860
1861                Adjust_D;
1862                Freeze_All (Freeze_From, D);
1863                Freeze_From := Last_Entity (Current_Scope);
1864
1865             elsif Scope (Current_Scope) /= Standard_Standard
1866               and then not Is_Child_Unit (Current_Scope)
1867               and then No (Generic_Parent (Parent (L)))
1868             then
1869                null;
1870
1871             elsif L /= Visible_Declarations (Parent (L))
1872                or else No (Private_Declarations (Parent (L)))
1873                or else Is_Empty_List (Private_Declarations (Parent (L)))
1874             then
1875                Adjust_D;
1876                Freeze_All (Freeze_From, D);
1877                Freeze_From := Last_Entity (Current_Scope);
1878             end if;
1879
1880          --  If next node is a body then freeze all types before the body.
1881          --  An exception occurs for some expander-generated bodies. If these
1882          --  are generated at places where in general language rules would not
1883          --  allow a freeze point, then we assume that the expander has
1884          --  explicitly checked that all required types are properly frozen,
1885          --  and we do not cause general freezing here. This special circuit
1886          --  is used when the encountered body is marked as having already
1887          --  been analyzed.
1888
1889          --  In all other cases (bodies that come from source, and expander
1890          --  generated bodies that have not been analyzed yet), freeze all
1891          --  types now. Note that in the latter case, the expander must take
1892          --  care to attach the bodies at a proper place in the tree so as to
1893          --  not cause unwanted freezing at that point.
1894
1895          elsif not Analyzed (Next_Node)
1896            and then (Nkind_In (Next_Node, N_Subprogram_Body,
1897                                           N_Entry_Body,
1898                                           N_Package_Body,
1899                                           N_Protected_Body,
1900                                           N_Task_Body)
1901                        or else
1902                      Nkind (Next_Node) in N_Body_Stub)
1903          then
1904             Adjust_D;
1905             Freeze_All (Freeze_From, D);
1906             Freeze_From := Last_Entity (Current_Scope);
1907          end if;
1908
1909          D := Next_Node;
1910       end loop;
1911    end Analyze_Declarations;
1912
1913    ----------------------------------
1914    -- Analyze_Incomplete_Type_Decl --
1915    ----------------------------------
1916
1917    procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
1918       F : constant Boolean := Is_Pure (Current_Scope);
1919       T : Entity_Id;
1920
1921    begin
1922       Generate_Definition (Defining_Identifier (N));
1923
1924       --  Process an incomplete declaration. The identifier must not have been
1925       --  declared already in the scope. However, an incomplete declaration may
1926       --  appear in the private part of a package, for a private type that has
1927       --  already been declared.
1928
1929       --  In this case, the discriminants (if any) must match
1930
1931       T := Find_Type_Name (N);
1932
1933       Set_Ekind (T, E_Incomplete_Type);
1934       Init_Size_Align (T);
1935       Set_Is_First_Subtype (T, True);
1936       Set_Etype (T, T);
1937
1938       --  Ada 2005 (AI-326): Minimum decoration to give support to tagged
1939       --  incomplete types.
1940
1941       if Tagged_Present (N) then
1942          Set_Is_Tagged_Type (T);
1943          Make_Class_Wide_Type (T);
1944          Set_Primitive_Operations (T, New_Elmt_List);
1945       end if;
1946
1947       Push_Scope (T);
1948
1949       Set_Stored_Constraint (T, No_Elist);
1950
1951       if Present (Discriminant_Specifications (N)) then
1952          Process_Discriminants (N);
1953       end if;
1954
1955       End_Scope;
1956
1957       --  If the type has discriminants, non-trivial subtypes may be be
1958       --  declared before the full view of the type. The full views of those
1959       --  subtypes will be built after the full view of the type.
1960
1961       Set_Private_Dependents (T, New_Elmt_List);
1962       Set_Is_Pure (T, F);
1963    end Analyze_Incomplete_Type_Decl;
1964
1965    -----------------------------------
1966    -- Analyze_Interface_Declaration --
1967    -----------------------------------
1968
1969    procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
1970       CW : constant Entity_Id := Class_Wide_Type (T);
1971
1972    begin
1973       Set_Is_Tagged_Type (T);
1974
1975       Set_Is_Limited_Record (T, Limited_Present (Def)
1976                                   or else Task_Present (Def)
1977                                   or else Protected_Present (Def)
1978                                   or else Synchronized_Present (Def));
1979
1980       --  Type is abstract if full declaration carries keyword, or if previous
1981       --  partial view did.
1982
1983       Set_Is_Abstract_Type (T);
1984       Set_Is_Interface (T);
1985
1986       --  Type is a limited interface if it includes the keyword limited, task,
1987       --  protected, or synchronized.
1988
1989       Set_Is_Limited_Interface
1990         (T, Limited_Present (Def)
1991               or else Protected_Present (Def)
1992               or else Synchronized_Present (Def)
1993               or else Task_Present (Def));
1994
1995       Set_Is_Protected_Interface (T, Protected_Present (Def));
1996       Set_Is_Task_Interface (T, Task_Present (Def));
1997
1998       --  Type is a synchronized interface if it includes the keyword task,
1999       --  protected, or synchronized.
2000
2001       Set_Is_Synchronized_Interface
2002         (T, Synchronized_Present (Def)
2003               or else Protected_Present (Def)
2004               or else Task_Present (Def));
2005
2006       Set_Interfaces (T, New_Elmt_List);
2007       Set_Primitive_Operations (T, New_Elmt_List);
2008
2009       --  Complete the decoration of the class-wide entity if it was already
2010       --  built (i.e. during the creation of the limited view)
2011
2012       if Present (CW) then
2013          Set_Is_Interface (CW);
2014          Set_Is_Limited_Interface      (CW, Is_Limited_Interface (T));
2015          Set_Is_Protected_Interface    (CW, Is_Protected_Interface (T));
2016          Set_Is_Synchronized_Interface (CW, Is_Synchronized_Interface (T));
2017          Set_Is_Task_Interface         (CW, Is_Task_Interface (T));
2018       end if;
2019
2020       --  Check runtime support for synchronized interfaces
2021
2022       if VM_Target = No_VM
2023         and then (Is_Task_Interface (T)
2024                     or else Is_Protected_Interface (T)
2025                     or else Is_Synchronized_Interface (T))
2026         and then not RTE_Available (RE_Select_Specific_Data)
2027       then
2028          Error_Msg_CRT ("synchronized interfaces", T);
2029       end if;
2030    end Analyze_Interface_Declaration;
2031
2032    -----------------------------
2033    -- Analyze_Itype_Reference --
2034    -----------------------------
2035
2036    --  Nothing to do. This node is placed in the tree only for the benefit of
2037    --  back end processing, and has no effect on the semantic processing.
2038
2039    procedure Analyze_Itype_Reference (N : Node_Id) is
2040    begin
2041       pragma Assert (Is_Itype (Itype (N)));
2042       null;
2043    end Analyze_Itype_Reference;
2044
2045    --------------------------------
2046    -- Analyze_Number_Declaration --
2047    --------------------------------
2048
2049    procedure Analyze_Number_Declaration (N : Node_Id) is
2050       Id    : constant Entity_Id := Defining_Identifier (N);
2051       E     : constant Node_Id   := Expression (N);
2052       T     : Entity_Id;
2053       Index : Interp_Index;
2054       It    : Interp;
2055
2056    begin
2057       Generate_Definition (Id);
2058       Enter_Name (Id);
2059
2060       --  This is an optimization of a common case of an integer literal
2061
2062       if Nkind (E) = N_Integer_Literal then
2063          Set_Is_Static_Expression (E, True);
2064          Set_Etype                (E, Universal_Integer);
2065
2066          Set_Etype     (Id, Universal_Integer);
2067          Set_Ekind     (Id, E_Named_Integer);
2068          Set_Is_Frozen (Id, True);
2069          return;
2070       end if;
2071
2072       Set_Is_Pure (Id, Is_Pure (Current_Scope));
2073
2074       --  Process expression, replacing error by integer zero, to avoid
2075       --  cascaded errors or aborts further along in the processing
2076
2077       --  Replace Error by integer zero, which seems least likely to
2078       --  cause cascaded errors.
2079
2080       if E = Error then
2081          Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2082          Set_Error_Posted (E);
2083       end if;
2084
2085       Analyze (E);
2086
2087       --  Verify that the expression is static and numeric. If
2088       --  the expression is overloaded, we apply the preference
2089       --  rule that favors root numeric types.
2090
2091       if not Is_Overloaded (E) then
2092          T := Etype (E);
2093
2094       else
2095          T := Any_Type;
2096
2097          Get_First_Interp (E, Index, It);
2098          while Present (It.Typ) loop
2099             if (Is_Integer_Type (It.Typ)
2100                  or else Is_Real_Type (It.Typ))
2101               and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2102             then
2103                if T = Any_Type then
2104                   T := It.Typ;
2105
2106                elsif It.Typ = Universal_Real
2107                  or else It.Typ = Universal_Integer
2108                then
2109                   --  Choose universal interpretation over any other
2110
2111                   T := It.Typ;
2112                   exit;
2113                end if;
2114             end if;
2115
2116             Get_Next_Interp (Index, It);
2117          end loop;
2118       end if;
2119
2120       if Is_Integer_Type (T)  then
2121          Resolve (E, T);
2122          Set_Etype (Id, Universal_Integer);
2123          Set_Ekind (Id, E_Named_Integer);
2124
2125       elsif Is_Real_Type (T) then
2126
2127          --  Because the real value is converted to universal_real, this is a
2128          --  legal context for a universal fixed expression.
2129
2130          if T = Universal_Fixed then
2131             declare
2132                Loc  : constant Source_Ptr := Sloc (N);
2133                Conv : constant Node_Id := Make_Type_Conversion (Loc,
2134                         Subtype_Mark =>
2135                           New_Occurrence_Of (Universal_Real, Loc),
2136                         Expression => Relocate_Node (E));
2137
2138             begin
2139                Rewrite (E, Conv);
2140                Analyze (E);
2141             end;
2142
2143          elsif T = Any_Fixed then
2144             Error_Msg_N ("illegal context for mixed mode operation", E);
2145
2146             --  Expression is of the form : universal_fixed * integer. Try to
2147             --  resolve as universal_real.
2148
2149             T := Universal_Real;
2150             Set_Etype (E, T);
2151          end if;
2152
2153          Resolve (E, T);
2154          Set_Etype (Id, Universal_Real);
2155          Set_Ekind (Id, E_Named_Real);
2156
2157       else
2158          Wrong_Type (E, Any_Numeric);
2159          Resolve (E, T);
2160
2161          Set_Etype               (Id, T);
2162          Set_Ekind               (Id, E_Constant);
2163          Set_Never_Set_In_Source (Id, True);
2164          Set_Is_True_Constant    (Id, True);
2165          return;
2166       end if;
2167
2168       if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
2169          Set_Etype (E, Etype (Id));
2170       end if;
2171
2172       if not Is_OK_Static_Expression (E) then
2173          Flag_Non_Static_Expr
2174            ("non-static expression used in number declaration!", E);
2175          Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2176          Set_Etype (E, Any_Type);
2177       end if;
2178    end Analyze_Number_Declaration;
2179
2180    --------------------------------
2181    -- Analyze_Object_Declaration --
2182    --------------------------------
2183
2184    procedure Analyze_Object_Declaration (N : Node_Id) is
2185       Loc   : constant Source_Ptr := Sloc (N);
2186       Id    : constant Entity_Id  := Defining_Identifier (N);
2187       T     : Entity_Id;
2188       Act_T : Entity_Id;
2189
2190       E : Node_Id := Expression (N);
2191       --  E is set to Expression (N) throughout this routine. When
2192       --  Expression (N) is modified, E is changed accordingly.
2193
2194       Prev_Entity : Entity_Id := Empty;
2195
2196       function Count_Tasks (T : Entity_Id) return Uint;
2197       --  This function is called when a non-generic library level object of a
2198       --  task type is declared. Its function is to count the static number of
2199       --  tasks declared within the type (it is only called if Has_Tasks is set
2200       --  for T). As a side effect, if an array of tasks with non-static bounds
2201       --  or a variant record type is encountered, Check_Restrictions is called
2202       --  indicating the count is unknown.
2203
2204       -----------------
2205       -- Count_Tasks --
2206       -----------------
2207
2208       function Count_Tasks (T : Entity_Id) return Uint is
2209          C : Entity_Id;
2210          X : Node_Id;
2211          V : Uint;
2212
2213       begin
2214          if Is_Task_Type (T) then
2215             return Uint_1;
2216
2217          elsif Is_Record_Type (T) then
2218             if Has_Discriminants (T) then
2219                Check_Restriction (Max_Tasks, N);
2220                return Uint_0;
2221
2222             else
2223                V := Uint_0;
2224                C := First_Component (T);
2225                while Present (C) loop
2226                   V := V + Count_Tasks (Etype (C));
2227                   Next_Component (C);
2228                end loop;
2229
2230                return V;
2231             end if;
2232
2233          elsif Is_Array_Type (T) then
2234             X := First_Index (T);
2235             V := Count_Tasks (Component_Type (T));
2236             while Present (X) loop
2237                C := Etype (X);
2238
2239                if not Is_Static_Subtype (C) then
2240                   Check_Restriction (Max_Tasks, N);
2241                   return Uint_0;
2242                else
2243                   V := V * (UI_Max (Uint_0,
2244                                     Expr_Value (Type_High_Bound (C)) -
2245                                     Expr_Value (Type_Low_Bound (C)) + Uint_1));
2246                end if;
2247
2248                Next_Index (X);
2249             end loop;
2250
2251             return V;
2252
2253          else
2254             return Uint_0;
2255          end if;
2256       end Count_Tasks;
2257
2258    --  Start of processing for Analyze_Object_Declaration
2259
2260    begin
2261       --  There are three kinds of implicit types generated by an
2262       --  object declaration:
2263
2264       --   1. Those for generated by the original Object Definition
2265
2266       --   2. Those generated by the Expression
2267
2268       --   3. Those used to constrained the Object Definition with the
2269       --       expression constraints when it is unconstrained
2270
2271       --  They must be generated in this order to avoid order of elaboration
2272       --  issues. Thus the first step (after entering the name) is to analyze
2273       --  the object definition.
2274
2275       if Constant_Present (N) then
2276          Prev_Entity := Current_Entity_In_Scope (Id);
2277
2278          --  If the homograph is an implicit subprogram, it is overridden by
2279          --  the current declaration.
2280
2281          if Present (Prev_Entity)
2282            and then
2283              ((Is_Overloadable (Prev_Entity)
2284                  and then Is_Inherited_Operation (Prev_Entity))
2285
2286                --  The current object is a discriminal generated for an entry
2287                --  family index. Even though the index is a constant, in this
2288                --  particular context there is no true constant redeclaration.
2289                --  Enter_Name will handle the visibility.
2290
2291                or else
2292                 (Is_Discriminal (Id)
2293                    and then Ekind (Discriminal_Link (Id)) =
2294                               E_Entry_Index_Parameter))
2295          then
2296             Prev_Entity := Empty;
2297          end if;
2298       end if;
2299
2300       if Present (Prev_Entity) then
2301          Constant_Redeclaration (Id, N, T);
2302
2303          Generate_Reference (Prev_Entity, Id, 'c');
2304          Set_Completion_Referenced (Id);
2305
2306          if Error_Posted (N) then
2307
2308             --  Type mismatch or illegal redeclaration, Do not analyze
2309             --  expression to avoid cascaded errors.
2310
2311             T := Find_Type_Of_Object (Object_Definition (N), N);
2312             Set_Etype (Id, T);
2313             Set_Ekind (Id, E_Variable);
2314             return;
2315          end if;
2316
2317       --  In the normal case, enter identifier at the start to catch premature
2318       --  usage in the initialization expression.
2319
2320       else
2321          Generate_Definition (Id);
2322          Enter_Name (Id);
2323
2324          Mark_Coextensions (N, Object_Definition (N));
2325
2326          T := Find_Type_Of_Object (Object_Definition (N), N);
2327
2328          if Nkind (Object_Definition (N)) = N_Access_Definition
2329            and then Present
2330              (Access_To_Subprogram_Definition (Object_Definition (N)))
2331            and then Protected_Present
2332              (Access_To_Subprogram_Definition (Object_Definition (N)))
2333          then
2334             T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2335          end if;
2336
2337          if Error_Posted (Id) then
2338             Set_Etype (Id, T);
2339             Set_Ekind (Id, E_Variable);
2340             return;
2341          end if;
2342       end if;
2343
2344       --  Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2345       --  out some static checks
2346
2347       if Ada_Version >= Ada_05
2348         and then Can_Never_Be_Null (T)
2349       then
2350          --  In case of aggregates we must also take care of the correct
2351          --  initialization of nested aggregates bug this is done at the
2352          --  point of the analysis of the aggregate (see sem_aggr.adb)
2353
2354          if Present (Expression (N))
2355            and then Nkind (Expression (N)) = N_Aggregate
2356          then
2357             null;
2358
2359          else
2360             declare
2361                Save_Typ : constant Entity_Id := Etype (Id);
2362             begin
2363                Set_Etype (Id, T); --  Temp. decoration for static checks
2364                Null_Exclusion_Static_Checks (N);
2365                Set_Etype (Id, Save_Typ);
2366             end;
2367          end if;
2368       end if;
2369
2370       Set_Is_Pure (Id, Is_Pure (Current_Scope));
2371
2372       --  If deferred constant, make sure context is appropriate. We detect
2373       --  a deferred constant as a constant declaration with no expression.
2374       --  A deferred constant can appear in a package body if its completion
2375       --  is by means of an interface pragma.
2376
2377       if Constant_Present (N)
2378         and then No (E)
2379       then
2380          --  We exclude forward references to tags
2381
2382          if Is_Imported (Defining_Identifier (N))
2383            and then
2384              (T = RTE (RE_Tag)
2385                or else
2386                  (Present (Full_View (T))
2387                    and then Full_View (T) = RTE (RE_Tag)))
2388          then
2389             null;
2390
2391          --  A deferred constant may appear in the declarative part of the
2392          --  following constructs:
2393
2394          --     blocks
2395          --     entry bodies
2396          --     extended return statements
2397          --     package specs
2398          --     package bodies
2399          --     subprogram bodies
2400          --     task bodies
2401
2402          --  When declared inside a package spec, a deferred constant must be
2403          --  completed by a full constant declaration or pragma Import. In all
2404          --  other cases, the only proper completion is pragma Import. Extended
2405          --  return statements are flagged as invalid contexts because they do
2406          --  not have a declarative part and so cannot accommodate the pragma.
2407
2408          elsif Ekind (Current_Scope) = E_Return_Statement then
2409             Error_Msg_N
2410               ("invalid context for deferred constant declaration (RM 7.4)",
2411                N);
2412             Error_Msg_N
2413               ("\declaration requires an initialization expression",
2414                 N);
2415             Set_Constant_Present (N, False);
2416
2417          --  In Ada 83, deferred constant must be of private type
2418
2419          elsif not Is_Private_Type (T) then
2420             if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2421                Error_Msg_N
2422                  ("(Ada 83) deferred constant must be private type", N);
2423             end if;
2424          end if;
2425
2426       --  If not a deferred constant, then object declaration freezes its type
2427
2428       else
2429          Check_Fully_Declared (T, N);
2430          Freeze_Before (N, T);
2431       end if;
2432
2433       --  If the object was created by a constrained array definition, then
2434       --  set the link in both the anonymous base type and anonymous subtype
2435       --  that are built to represent the array type to point to the object.
2436
2437       if Nkind (Object_Definition (Declaration_Node (Id))) =
2438                         N_Constrained_Array_Definition
2439       then
2440          Set_Related_Array_Object (T, Id);
2441          Set_Related_Array_Object (Base_Type (T), Id);
2442       end if;
2443
2444       --  Special checks for protected objects not at library level
2445
2446       if Is_Protected_Type (T)
2447         and then not Is_Library_Level_Entity (Id)
2448       then
2449          Check_Restriction (No_Local_Protected_Objects, Id);
2450
2451          --  Protected objects with interrupt handlers must be at library level
2452
2453          --  Ada 2005: this test is not needed (and the corresponding clause
2454          --  in the RM is removed) because accessibility checks are sufficient
2455          --  to make handlers not at the library level illegal.
2456
2457          if Has_Interrupt_Handler (T)
2458            and then Ada_Version < Ada_05
2459          then
2460             Error_Msg_N
2461               ("interrupt object can only be declared at library level", Id);
2462          end if;
2463       end if;
2464
2465       --  The actual subtype of the object is the nominal subtype, unless
2466       --  the nominal one is unconstrained and obtained from the expression.
2467
2468       Act_T := T;
2469
2470       --  Process initialization expression if present and not in error
2471
2472       if Present (E) and then E /= Error then
2473
2474          --  Generate an error in case of CPP class-wide object initialization.
2475          --  Required because otherwise the expansion of the class-wide
2476          --  assignment would try to use 'size to initialize the object
2477          --  (primitive that is not available in CPP tagged types).
2478
2479          if Is_Class_Wide_Type (Act_T)
2480            and then
2481              (Is_CPP_Class (Root_Type (Etype (Act_T)))
2482                or else
2483                  (Present (Full_View (Root_Type (Etype (Act_T))))
2484                     and then
2485                       Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
2486          then
2487             Error_Msg_N
2488               ("predefined assignment not available for 'C'P'P tagged types",
2489                E);
2490          end if;
2491
2492          Mark_Coextensions (N, E);
2493          Analyze (E);
2494
2495          --  In case of errors detected in the analysis of the expression,
2496          --  decorate it with the expected type to avoid cascaded errors
2497
2498          if No (Etype (E)) then
2499             Set_Etype (E, T);
2500          end if;
2501
2502          --  If an initialization expression is present, then we set the
2503          --  Is_True_Constant flag. It will be reset if this is a variable
2504          --  and it is indeed modified.
2505
2506          Set_Is_True_Constant (Id, True);
2507
2508          --  If we are analyzing a constant declaration, set its completion
2509          --  flag after analyzing and resolving the expression.
2510
2511          if Constant_Present (N) then
2512             Set_Has_Completion (Id);
2513          end if;
2514
2515          --  Set type and resolve (type may be overridden later on)
2516
2517          Set_Etype (Id, T);
2518          Resolve (E, T);
2519
2520          --  If the object is an access to variable, the initialization
2521          --  expression cannot be an access to constant.
2522
2523          if Is_Access_Type (T)
2524            and then not Is_Access_Constant (T)
2525            and then Is_Access_Type (Etype (E))
2526            and then Is_Access_Constant (Etype (E))
2527          then
2528             Error_Msg_N
2529               ("object that is an access to variable cannot be initialized " &
2530                 "with an access-to-constant expression", E);
2531          end if;
2532
2533          if not Assignment_OK (N) then
2534             Check_Initialization (T, E);
2535          end if;
2536
2537          Check_Unset_Reference (E);
2538
2539          --  If this is a variable, then set current value
2540
2541          if not Constant_Present (N) then
2542             if Compile_Time_Known_Value (E) then
2543                Set_Current_Value (Id, E);
2544             end if;
2545          end if;
2546
2547          --  Deal with setting of null flags
2548
2549          if Is_Access_Type (T) then
2550             if Known_Non_Null (E) then
2551                Set_Is_Known_Non_Null (Id, True);
2552             elsif Known_Null (E)
2553               and then not Can_Never_Be_Null (Id)
2554             then
2555                Set_Is_Known_Null (Id, True);
2556             end if;
2557          end if;
2558
2559          --  Check incorrect use of dynamically tagged expressions. Note
2560          --  the use of Is_Tagged_Type (T) which seems redundant but is in
2561          --  fact important to avoid spurious errors due to expanded code
2562          --  for dispatching functions over an anonymous access type
2563
2564          if (Is_Class_Wide_Type (Etype (E)) or else Is_Dynamically_Tagged (E))
2565            and then Is_Tagged_Type (T)
2566            and then not Is_Class_Wide_Type (T)
2567          then
2568             Error_Msg_N ("dynamically tagged expression not allowed!", E);
2569          end if;
2570
2571          Apply_Scalar_Range_Check (E, T);
2572          Apply_Static_Length_Check (E, T);
2573       end if;
2574
2575       --  If the No_Streams restriction is set, check that the type of the
2576       --  object is not, and does not contain, any subtype derived from
2577       --  Ada.Streams.Root_Stream_Type. Note that we guard the call to
2578       --  Has_Stream just for efficiency reasons. There is no point in
2579       --  spending time on a Has_Stream check if the restriction is not set.
2580
2581       if Restrictions.Set (No_Streams) then
2582          if Has_Stream (T) then
2583             Check_Restriction (No_Streams, N);
2584          end if;
2585       end if;
2586
2587       --  Abstract type is never permitted for a variable or constant.
2588       --  Note: we inhibit this check for objects that do not come from
2589       --  source because there is at least one case (the expansion of
2590       --  x'class'input where x is abstract) where we legitimately
2591       --  generate an abstract object.
2592
2593       if Is_Abstract_Type (T) and then Comes_From_Source (N) then
2594          Error_Msg_N ("type of object cannot be abstract",
2595                       Object_Definition (N));
2596
2597          if Is_CPP_Class (T) then
2598             Error_Msg_NE ("\} may need a cpp_constructor",
2599               Object_Definition (N), T);
2600          end if;
2601
2602       --  Case of unconstrained type
2603
2604       elsif Is_Indefinite_Subtype (T) then
2605
2606          --  Nothing to do in deferred constant case
2607
2608          if Constant_Present (N) and then No (E) then
2609             null;
2610
2611          --  Case of no initialization present
2612
2613          elsif No (E) then
2614             if No_Initialization (N) then
2615                null;
2616
2617             elsif Is_Class_Wide_Type (T) then
2618                Error_Msg_N
2619                  ("initialization required in class-wide declaration ", N);
2620
2621             else
2622                Error_Msg_N
2623                  ("unconstrained subtype not allowed (need initialization)",
2624                   Object_Definition (N));
2625
2626                if Is_Record_Type (T) and then Has_Discriminants (T) then
2627                   Error_Msg_N
2628                     ("\provide initial value or explicit discriminant values",
2629                      Object_Definition (N));
2630
2631                   Error_Msg_NE
2632                     ("\or give default discriminant values for type&",
2633                      Object_Definition (N), T);
2634
2635                elsif Is_Array_Type (T) then
2636                   Error_Msg_N
2637                     ("\provide initial value or explicit array bounds",
2638                      Object_Definition (N));
2639                end if;
2640             end if;
2641
2642          --  Case of initialization present but in error. Set initial
2643          --  expression as absent (but do not make above complaints)
2644
2645          elsif E = Error then
2646             Set_Expression (N, Empty);
2647             E := Empty;
2648
2649          --  Case of initialization present
2650
2651          else
2652             --  Not allowed in Ada 83
2653
2654             if not Constant_Present (N) then
2655                if Ada_Version = Ada_83
2656                  and then Comes_From_Source (Object_Definition (N))
2657                then
2658                   Error_Msg_N
2659                     ("(Ada 83) unconstrained variable not allowed",
2660                      Object_Definition (N));
2661                end if;
2662             end if;
2663
2664             --  Now we constrain the variable from the initializing expression
2665
2666             --  If the expression is an aggregate, it has been expanded into
2667             --  individual assignments. Retrieve the actual type from the
2668             --  expanded construct.
2669
2670             if Is_Array_Type (T)
2671               and then No_Initialization (N)
2672               and then Nkind (Original_Node (E)) = N_Aggregate
2673             then
2674                Act_T := Etype (E);
2675
2676             else
2677                Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
2678                Act_T := Find_Type_Of_Object (Object_Definition (N), N);
2679             end if;
2680
2681             Set_Is_Constr_Subt_For_U_Nominal (Act_T);
2682
2683             if Aliased_Present (N) then
2684                Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2685             end if;
2686
2687             Freeze_Before (N, Act_T);
2688             Freeze_Before (N, T);
2689          end if;
2690
2691       elsif Is_Array_Type (T)
2692         and then No_Initialization (N)
2693         and then Nkind (Original_Node (E)) = N_Aggregate
2694       then
2695          if not Is_Entity_Name (Object_Definition (N)) then
2696             Act_T := Etype (E);
2697             Check_Compile_Time_Size (Act_T);
2698
2699             if Aliased_Present (N) then
2700                Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2701             end if;
2702          end if;
2703
2704          --  When the given object definition and the aggregate are specified
2705          --  independently, and their lengths might differ do a length check.
2706          --  This cannot happen if the aggregate is of the form (others =>...)
2707
2708          if not Is_Constrained (T) then
2709             null;
2710
2711          elsif Nkind (E) = N_Raise_Constraint_Error then
2712
2713             --  Aggregate is statically illegal. Place back in declaration
2714
2715             Set_Expression (N, E);
2716             Set_No_Initialization (N, False);
2717
2718          elsif T = Etype (E) then
2719             null;
2720
2721          elsif Nkind (E) = N_Aggregate
2722            and then Present (Component_Associations (E))
2723            and then Present (Choices (First (Component_Associations (E))))
2724            and then Nkind (First
2725             (Choices (First (Component_Associations (E))))) = N_Others_Choice
2726          then
2727             null;
2728
2729          else
2730             Apply_Length_Check (E, T);
2731          end if;
2732
2733       --  If the type is limited unconstrained with defaulted discriminants
2734       --  and there is no expression, then the object is constrained by the
2735       --  defaults, so it is worthwhile building the corresponding subtype.
2736
2737       elsif (Is_Limited_Record (T)
2738                or else Is_Concurrent_Type (T))
2739         and then not Is_Constrained (T)
2740         and then Has_Discriminants (T)
2741       then
2742          if No (E) then
2743             Act_T := Build_Default_Subtype (T, N);
2744          else
2745             --  Ada 2005:  a limited object may be initialized by means of an
2746             --  aggregate. If the type has default discriminants it has an
2747             --  unconstrained nominal type, Its actual subtype will be obtained
2748             --  from the aggregate, and not from the default discriminants.
2749
2750             Act_T := Etype (E);
2751          end if;
2752
2753          Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
2754
2755       elsif Present (Underlying_Type (T))
2756         and then not Is_Constrained (Underlying_Type (T))
2757         and then Has_Discriminants (Underlying_Type (T))
2758         and then Nkind (E) = N_Function_Call
2759         and then Constant_Present (N)
2760       then
2761          --  The back-end has problems with constants of a discriminated type
2762          --  with defaults, if the initial value is a function call. We
2763          --  generate an intermediate temporary for the result of the call.
2764          --  It is unclear why this should make it acceptable to gcc. ???
2765
2766          Remove_Side_Effects (E);
2767       end if;
2768
2769       --  Check No_Wide_Characters restriction
2770
2771       if T = Standard_Wide_Character
2772         or else T = Standard_Wide_Wide_Character
2773         or else Root_Type (T) = Standard_Wide_String
2774         or else Root_Type (T) = Standard_Wide_Wide_String
2775       then
2776          Check_Restriction (No_Wide_Characters, Object_Definition (N));
2777       end if;
2778
2779       --  Indicate this is not set in source. Certainly true for constants,
2780       --  and true for variables so far (will be reset for a variable if and
2781       --  when we encounter a modification in the source).
2782
2783       Set_Never_Set_In_Source (Id, True);
2784
2785       --  Now establish the proper kind and type of the object
2786
2787       if Constant_Present (N) then
2788          Set_Ekind            (Id, E_Constant);
2789          Set_Is_True_Constant (Id, True);
2790
2791       else
2792          Set_Ekind (Id, E_Variable);
2793
2794          --  A variable is set as shared passive if it appears in a shared
2795          --  passive package, and is at the outer level. This is not done
2796          --  for entities generated during expansion, because those are
2797          --  always manipulated locally.
2798
2799          if Is_Shared_Passive (Current_Scope)
2800            and then Is_Library_Level_Entity (Id)
2801            and then Comes_From_Source (Id)
2802          then
2803             Set_Is_Shared_Passive (Id);
2804             Check_Shared_Var (Id, T, N);
2805          end if;
2806
2807          --  Set Has_Initial_Value if initializing expression present. Note
2808          --  that if there is no initializing expression, we leave the state
2809          --  of this flag unchanged (usually it will be False, but notably in
2810          --  the case of exception choice variables, it will already be true).
2811
2812          if Present (E) then
2813             Set_Has_Initial_Value (Id, True);
2814          end if;
2815       end if;
2816
2817       --  Initialize alignment and size and capture alignment setting
2818
2819       Init_Alignment               (Id);
2820       Init_Esize                   (Id);
2821       Set_Optimize_Alignment_Flags (Id);
2822
2823       --  Deal with aliased case
2824
2825       if Aliased_Present (N) then
2826          Set_Is_Aliased (Id);
2827
2828          --  If the object is aliased and the type is unconstrained with
2829          --  defaulted discriminants and there is no expression, then the
2830          --  object is constrained by the defaults, so it is worthwhile
2831          --  building the corresponding subtype.
2832
2833          --  Ada 2005 (AI-363): If the aliased object is discriminated and
2834          --  unconstrained, then only establish an actual subtype if the
2835          --  nominal subtype is indefinite. In definite cases the object is
2836          --  unconstrained in Ada 2005.
2837
2838          if No (E)
2839            and then Is_Record_Type (T)
2840            and then not Is_Constrained (T)
2841            and then Has_Discriminants (T)
2842            and then (Ada_Version < Ada_05 or else Is_Indefinite_Subtype (T))
2843          then
2844             Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
2845          end if;
2846       end if;
2847
2848       --  Now we can set the type of the object
2849
2850       Set_Etype (Id, Act_T);
2851
2852       --  Deal with controlled types
2853
2854       if Has_Controlled_Component (Etype (Id))
2855         or else Is_Controlled (Etype (Id))
2856       then
2857          if not Is_Library_Level_Entity (Id) then
2858             Check_Restriction (No_Nested_Finalization, N);
2859          else
2860             Validate_Controlled_Object (Id);
2861          end if;
2862
2863          --  Generate a warning when an initialization causes an obvious ABE
2864          --  violation. If the init expression is a simple aggregate there
2865          --  shouldn't be any initialize/adjust call generated. This will be
2866          --  true as soon as aggregates are built in place when possible.
2867
2868          --  ??? at the moment we do not generate warnings for temporaries
2869          --  created for those aggregates although Program_Error might be
2870          --  generated if compiled with -gnato.
2871
2872          if Is_Controlled (Etype (Id))
2873             and then Comes_From_Source (Id)
2874          then
2875             declare
2876                BT : constant Entity_Id := Base_Type (Etype (Id));
2877
2878                Implicit_Call : Entity_Id;
2879                pragma Warnings (Off, Implicit_Call);
2880                --  ??? what is this for (never referenced!)
2881
2882                function Is_Aggr (N : Node_Id) return Boolean;
2883                --  Check that N is an aggregate
2884
2885                -------------
2886                -- Is_Aggr --
2887                -------------
2888
2889                function Is_Aggr (N : Node_Id) return Boolean is
2890                begin
2891                   case Nkind (Original_Node (N)) is
2892                      when N_Aggregate | N_Extension_Aggregate =>
2893                         return True;
2894
2895                      when N_Qualified_Expression |
2896                           N_Type_Conversion      |
2897                           N_Unchecked_Type_Conversion =>
2898                         return Is_Aggr (Expression (Original_Node (N)));
2899
2900                      when others =>
2901                         return False;
2902                   end case;
2903                end Is_Aggr;
2904
2905             begin
2906                --  If no underlying type, we already are in an error situation.
2907                --  Do not try to add a warning since we do not have access to
2908                --  prim-op list.
2909
2910                if No (Underlying_Type (BT)) then
2911                   Implicit_Call := Empty;
2912
2913                --  A generic type does not have usable primitive operators.
2914                --  Initialization calls are built for instances.
2915
2916                elsif Is_Generic_Type (BT) then
2917                   Implicit_Call := Empty;
2918
2919                --  If the init expression is not an aggregate, an adjust call
2920                --  will be generated
2921
2922                elsif Present (E) and then not Is_Aggr (E) then
2923                   Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
2924
2925                --  If no init expression and we are not in the deferred
2926                --  constant case, an Initialize call will be generated
2927
2928                elsif No (E) and then not Constant_Present (N) then
2929                   Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
2930
2931                else
2932                   Implicit_Call := Empty;
2933                end if;
2934             end;
2935          end if;
2936       end if;
2937
2938       if Has_Task (Etype (Id)) then
2939          Check_Restriction (No_Tasking, N);
2940
2941          --  Deal with counting max tasks
2942
2943          --  Nothing to do if inside a generic
2944
2945          if Inside_A_Generic then
2946             null;
2947
2948          --  If library level entity, then count tasks
2949
2950          elsif Is_Library_Level_Entity (Id) then
2951             Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
2952
2953          --  If not library level entity, then indicate we don't know max
2954          --  tasks and also check task hierarchy restriction and blocking
2955          --  operation (since starting a task is definitely blocking!)
2956
2957          else
2958             Check_Restriction (Max_Tasks, N);
2959             Check_Restriction (No_Task_Hierarchy, N);
2960             Check_Potentially_Blocking_Operation (N);
2961          end if;
2962
2963          --  A rather specialized test. If we see two tasks being declared
2964          --  of the same type in the same object declaration, and the task
2965          --  has an entry with an address clause, we know that program error
2966          --  will be raised at run-time since we can't have two tasks with
2967          --  entries at the same address.
2968
2969          if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
2970             declare
2971                E : Entity_Id;
2972
2973             begin
2974                E := First_Entity (Etype (Id));
2975                while Present (E) loop
2976                   if Ekind (E) = E_Entry
2977                     and then Present (Get_Attribute_Definition_Clause
2978                                         (E, Attribute_Address))
2979                   then
2980                      Error_Msg_N
2981                        ("?more than one task with same entry address", N);
2982                      Error_Msg_N
2983                        ("\?Program_Error will be raised at run time", N);
2984                      Insert_Action (N,
2985                        Make_Raise_Program_Error (Loc,
2986                          Reason => PE_Duplicated_Entry_Address));
2987                      exit;
2988                   end if;
2989
2990                   Next_Entity (E);
2991                end loop;
2992             end;
2993          end if;
2994       end if;
2995
2996       --  Some simple constant-propagation: if the expression is a constant
2997       --  string initialized with a literal, share the literal. This avoids
2998       --  a run-time copy.
2999
3000       if Present (E)
3001         and then Is_Entity_Name (E)
3002         and then Ekind (Entity (E)) = E_Constant
3003         and then Base_Type (Etype (E)) = Standard_String
3004       then
3005          declare
3006             Val : constant Node_Id := Constant_Value (Entity (E));
3007          begin
3008             if Present (Val)
3009               and then Nkind (Val) = N_String_Literal
3010             then
3011                Rewrite (E, New_Copy (Val));
3012             end if;
3013          end;
3014       end if;
3015
3016       --  Another optimization: if the nominal subtype is unconstrained and
3017       --  the expression is a function call that returns an unconstrained
3018       --  type, rewrite the declaration as a renaming of the result of the
3019       --  call. The exceptions below are cases where the copy is expected,
3020       --  either by the back end (Aliased case) or by the semantics, as for
3021       --  initializing controlled types or copying tags for classwide types.
3022
3023       if Present (E)
3024         and then Nkind (E) = N_Explicit_Dereference
3025         and then Nkind (Original_Node (E)) = N_Function_Call
3026         and then not Is_Library_Level_Entity (Id)
3027         and then not Is_Constrained (Underlying_Type (T))
3028         and then not Is_Aliased (Id)
3029         and then not Is_Class_Wide_Type (T)
3030         and then not Is_Controlled (T)
3031         and then not Has_Controlled_Component (Base_Type (T))
3032         and then Expander_Active
3033       then
3034          Rewrite (N,
3035            Make_Object_Renaming_Declaration (Loc,
3036              Defining_Identifier => Id,
3037              Access_Definition   => Empty,
3038              Subtype_Mark        => New_Occurrence_Of
3039                                       (Base_Type (Etype (Id)), Loc),
3040              Name                => E));
3041
3042          Set_Renamed_Object (Id, E);
3043
3044          --  Force generation of debugging information for the constant and for
3045          --  the renamed function call.
3046
3047          Set_Debug_Info_Needed (Id);
3048          Set_Debug_Info_Needed (Entity (Prefix (E)));
3049       end if;
3050
3051       if Present (Prev_Entity)
3052         and then Is_Frozen (Prev_Entity)
3053         and then not Error_Posted (Id)
3054       then
3055          Error_Msg_N ("full constant declaration appears too late", N);
3056       end if;
3057
3058       Check_Eliminated (Id);
3059
3060       --  Deal with setting In_Private_Part flag if in private part
3061
3062       if Ekind (Scope (Id)) = E_Package
3063         and then In_Private_Part (Scope (Id))
3064       then
3065          Set_In_Private_Part (Id);
3066       end if;
3067
3068       --  Check for violation of No_Local_Timing_Events
3069
3070       if Is_RTE (Etype (Id), RE_Timing_Event)
3071         and then not Is_Library_Level_Entity (Id)
3072       then
3073          Check_Restriction (No_Local_Timing_Events, N);
3074       end if;
3075    end Analyze_Object_Declaration;
3076
3077    ---------------------------
3078    -- Analyze_Others_Choice --
3079    ---------------------------
3080
3081    --  Nothing to do for the others choice node itself, the semantic analysis
3082    --  of the others choice will occur as part of the processing of the parent
3083
3084    procedure Analyze_Others_Choice (N : Node_Id) is
3085       pragma Warnings (Off, N);
3086    begin
3087       null;
3088    end Analyze_Others_Choice;
3089
3090    -------------------------------------------
3091    -- Analyze_Private_Extension_Declaration --
3092    -------------------------------------------
3093
3094    procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
3095       T           : constant Entity_Id := Defining_Identifier (N);
3096       Indic       : constant Node_Id   := Subtype_Indication (N);
3097       Parent_Type : Entity_Id;
3098       Parent_Base : Entity_Id;
3099
3100    begin
3101       --  Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
3102
3103       if Is_Non_Empty_List (Interface_List (N)) then
3104          declare
3105             Intf : Node_Id;
3106             T    : Entity_Id;
3107
3108          begin
3109             Intf := First (Interface_List (N));
3110             while Present (Intf) loop
3111                T := Find_Type_Of_Subtype_Indic (Intf);
3112
3113                Diagnose_Interface (Intf, T);
3114                Next (Intf);
3115             end loop;
3116          end;
3117       end if;
3118
3119       Generate_Definition (T);
3120       Enter_Name (T);
3121
3122       Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
3123       Parent_Base := Base_Type (Parent_Type);
3124
3125       if Parent_Type = Any_Type
3126         or else Etype (Parent_Type) = Any_Type
3127       then
3128          Set_Ekind (T, Ekind (Parent_Type));
3129          Set_Etype (T, Any_Type);
3130          return;
3131
3132       elsif not Is_Tagged_Type (Parent_Type) then
3133          Error_Msg_N
3134            ("parent of type extension must be a tagged type ", Indic);
3135          return;
3136
3137       elsif Ekind (Parent_Type) = E_Void
3138         or else Ekind (Parent_Type) = E_Incomplete_Type
3139       then
3140          Error_Msg_N ("premature derivation of incomplete type", Indic);
3141          return;
3142
3143       elsif Is_Concurrent_Type (Parent_Type) then
3144          Error_Msg_N
3145            ("parent type of a private extension cannot be "
3146             & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
3147
3148          Set_Etype              (T, Any_Type);
3149          Set_Ekind              (T, E_Limited_Private_Type);
3150          Set_Private_Dependents (T, New_Elmt_List);
3151          Set_Error_Posted       (T);
3152          return;
3153       end if;
3154
3155       --  Perhaps the parent type should be changed to the class-wide type's
3156       --  specific type in this case to prevent cascading errors ???
3157
3158       if Is_Class_Wide_Type (Parent_Type) then
3159          Error_Msg_N
3160            ("parent of type extension must not be a class-wide type", Indic);
3161          return;
3162       end if;
3163
3164       if (not Is_Package_Or_Generic_Package (Current_Scope)
3165            and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
3166         or else In_Private_Part (Current_Scope)
3167
3168       then
3169          Error_Msg_N ("invalid context for private extension", N);
3170       end if;
3171
3172       --  Set common attributes
3173
3174       Set_Is_Pure          (T, Is_Pure (Current_Scope));
3175       Set_Scope            (T, Current_Scope);
3176       Set_Ekind            (T, E_Record_Type_With_Private);
3177       Init_Size_Align      (T);
3178
3179       Set_Etype            (T,            Parent_Base);
3180       Set_Has_Task         (T, Has_Task  (Parent_Base));
3181
3182       Set_Convention       (T, Convention     (Parent_Type));
3183       Set_First_Rep_Item   (T, First_Rep_Item (Parent_Type));
3184       Set_Is_First_Subtype (T);
3185       Make_Class_Wide_Type (T);
3186
3187       if Unknown_Discriminants_Present (N) then
3188          Set_Discriminant_Constraint (T, No_Elist);
3189       end if;
3190
3191       Build_Derived_Record_Type (N, Parent_Type, T);
3192
3193       --  Ada 2005 (AI-443): Synchronized private extension or a rewritten
3194       --  synchronized formal derived type.
3195
3196       if Ada_Version >= Ada_05
3197         and then Synchronized_Present (N)
3198       then
3199          Set_Is_Limited_Record (T);
3200
3201          --  Formal derived type case
3202
3203          if Is_Generic_Type (T) then
3204
3205             --  The parent must be a tagged limited type or a synchronized
3206             --  interface.
3207
3208             if (not Is_Tagged_Type (Parent_Type)
3209                   or else not Is_Limited_Type (Parent_Type))
3210               and then
3211                (not Is_Interface (Parent_Type)
3212                   or else not Is_Synchronized_Interface (Parent_Type))
3213             then
3214                Error_Msg_NE ("parent type of & must be tagged limited " &
3215                              "or synchronized", N, T);
3216             end if;
3217
3218             --  The progenitors (if any) must be limited or synchronized
3219             --  interfaces.
3220
3221             if Present (Interfaces (T)) then
3222                declare
3223                   Iface      : Entity_Id;
3224                   Iface_Elmt : Elmt_Id;
3225
3226                begin
3227                   Iface_Elmt := First_Elmt (Interfaces (T));
3228                   while Present (Iface_Elmt) loop
3229                      Iface := Node (Iface_Elmt);
3230
3231                      if not Is_Limited_Interface (Iface)
3232                        and then not Is_Synchronized_Interface (Iface)
3233                      then
3234                         Error_Msg_NE ("progenitor & must be limited " &
3235                                       "or synchronized", N, Iface);
3236                      end if;
3237
3238                      Next_Elmt (Iface_Elmt);
3239                   end loop;
3240                end;
3241             end if;
3242
3243          --  Regular derived extension, the parent must be a limited or
3244          --  synchronized interface.
3245
3246          else
3247             if not Is_Interface (Parent_Type)
3248               or else (not Is_Limited_Interface (Parent_Type)
3249                          and then
3250                        not Is_Synchronized_Interface (Parent_Type))
3251             then
3252                Error_Msg_NE
3253                  ("parent type of & must be limited interface", N, T);
3254             end if;
3255          end if;
3256
3257       elsif Limited_Present (N) then
3258          Set_Is_Limited_Record (T);
3259
3260          if not Is_Limited_Type (Parent_Type)
3261            and then
3262              (not Is_Interface (Parent_Type)
3263                or else not Is_Limited_Interface (Parent_Type))
3264          then
3265             Error_Msg_NE ("parent type& of limited extension must be limited",
3266               N, Parent_Type);
3267          end if;
3268       end if;
3269    end Analyze_Private_Extension_Declaration;
3270
3271    ---------------------------------
3272    -- Analyze_Subtype_Declaration --
3273    ---------------------------------
3274
3275    procedure Analyze_Subtype_Declaration
3276      (N    : Node_Id;
3277       Skip : Boolean := False)
3278    is
3279       Id       : constant Entity_Id := Defining_Identifier (N);
3280       T        : Entity_Id;
3281       R_Checks : Check_Result;
3282
3283    begin
3284       Generate_Definition (Id);
3285       Set_Is_Pure (Id, Is_Pure (Current_Scope));
3286       Init_Size_Align (Id);
3287
3288       --  The following guard condition on Enter_Name is to handle cases where
3289       --  the defining identifier has already been entered into the scope but
3290       --  the declaration as a whole needs to be analyzed.
3291
3292       --  This case in particular happens for derived enumeration types. The
3293       --  derived enumeration type is processed as an inserted enumeration type
3294       --  declaration followed by a rewritten subtype declaration. The defining
3295       --  identifier, however, is entered into the name scope very early in the
3296       --  processing of the original type declaration and therefore needs to be
3297       --  avoided here, when the created subtype declaration is analyzed. (See
3298       --  Build_Derived_Types)
3299
3300       --  This also happens when the full view of a private type is derived
3301       --  type with constraints. In this case the entity has been introduced
3302       --  in the private declaration.
3303
3304       if Skip
3305         or else (Present (Etype (Id))
3306                    and then (Is_Private_Type (Etype (Id))
3307                                or else Is_Task_Type (Etype (Id))
3308                                or else Is_Rewrite_Substitution (N)))
3309       then
3310          null;
3311
3312       else
3313          Enter_Name (Id);
3314       end if;
3315
3316       T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
3317
3318       --  Inherit common attributes
3319
3320       Set_Is_Generic_Type   (Id, Is_Generic_Type   (Base_Type (T)));
3321       Set_Is_Volatile       (Id, Is_Volatile       (T));
3322       Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
3323       Set_Is_Atomic         (Id, Is_Atomic         (T));
3324       Set_Is_Ada_2005_Only  (Id, Is_Ada_2005_Only  (T));
3325       Set_Convention        (Id, Convention        (T));
3326
3327       --  In the case where there is no constraint given in the subtype
3328       --  indication, Process_Subtype just returns the Subtype_Mark, so its
3329       --  semantic attributes must be established here.
3330
3331       if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
3332          Set_Etype (Id, Base_Type (T));
3333
3334          case Ekind (T) is
3335             when Array_Kind =>
3336                Set_Ekind                       (Id, E_Array_Subtype);
3337                Copy_Array_Subtype_Attributes   (Id, T);
3338
3339             when Decimal_Fixed_Point_Kind =>
3340                Set_Ekind                (Id, E_Decimal_Fixed_Point_Subtype);
3341                Set_Digits_Value         (Id, Digits_Value       (T));
3342                Set_Delta_Value          (Id, Delta_Value        (T));
3343                Set_Scale_Value          (Id, Scale_Value        (T));
3344                Set_Small_Value          (Id, Small_Value        (T));
3345                Set_Scalar_Range         (Id, Scalar_Range       (T));
3346                Set_Machine_Radix_10     (Id, Machine_Radix_10   (T));
3347                Set_Is_Constrained       (Id, Is_Constrained     (T));
3348                Set_RM_Size              (Id, RM_Size            (T));
3349
3350             when Enumeration_Kind =>
3351                Set_Ekind                (Id, E_Enumeration_Subtype);
3352                Set_First_Literal        (Id, First_Literal (Base_Type (T)));
3353                Set_Scalar_Range         (Id, Scalar_Range       (T));
3354                Set_Is_Character_Type    (Id, Is_Character_Type  (T));
3355                Set_Is_Constrained       (Id, Is_Constrained     (T));
3356                Set_RM_Size              (Id, RM_Size            (T));
3357
3358             when Ordinary_Fixed_Point_Kind =>
3359                Set_Ekind                (Id, E_Ordinary_Fixed_Point_Subtype);
3360                Set_Scalar_Range         (Id, Scalar_Range       (T));
3361                Set_Small_Value          (Id, Small_Value        (T));
3362                Set_Delta_Value          (Id, Delta_Value        (T));
3363                Set_Is_Constrained       (Id, Is_Constrained     (T));
3364                Set_RM_Size              (Id, RM_Size            (T));
3365
3366             when Float_Kind =>
3367                Set_Ekind                (Id, E_Floating_Point_Subtype);
3368                Set_Scalar_Range         (Id, Scalar_Range       (T));
3369                Set_Digits_Value         (Id, Digits_Value       (T));
3370                Set_Is_Constrained       (Id, Is_Constrained     (T));
3371
3372             when Signed_Integer_Kind =>
3373                Set_Ekind                (Id, E_Signed_Integer_Subtype);
3374                Set_Scalar_Range         (Id, Scalar_Range       (T));
3375                Set_Is_Constrained       (Id, Is_Constrained     (T));
3376                Set_RM_Size              (Id, RM_Size            (T));
3377
3378             when Modular_Integer_Kind =>
3379                Set_Ekind                (Id, E_Modular_Integer_Subtype);
3380                Set_Scalar_Range         (Id, Scalar_Range       (T));
3381                Set_Is_Constrained       (Id, Is_Constrained     (T));
3382                Set_RM_Size              (Id, RM_Size            (T));
3383
3384             when Class_Wide_Kind =>
3385                Set_Ekind                (Id, E_Class_Wide_Subtype);
3386                Set_First_Entity         (Id, First_Entity       (T));
3387                Set_Last_Entity          (Id, Last_Entity        (T));
3388                Set_Class_Wide_Type      (Id, Class_Wide_Type    (T));
3389                Set_Cloned_Subtype       (Id, T);
3390                Set_Is_Tagged_Type       (Id, True);
3391                Set_Has_Unknown_Discriminants
3392                                         (Id, True);
3393
3394                if Ekind (T) = E_Class_Wide_Subtype then
3395                   Set_Equivalent_Type   (Id, Equivalent_Type    (T));
3396                end if;
3397
3398             when E_Record_Type | E_Record_Subtype =>
3399                Set_Ekind                (Id, E_Record_Subtype);
3400
3401                if Ekind (T) = E_Record_Subtype
3402                  and then Present (Cloned_Subtype (T))
3403                then
3404                   Set_Cloned_Subtype    (Id, Cloned_Subtype (T));
3405                else
3406                   Set_Cloned_Subtype    (Id, T);
3407                end if;
3408
3409                Set_First_Entity         (Id, First_Entity       (T));
3410                Set_Last_Entity          (Id, Last_Entity        (T));
3411                Set_Has_Discriminants    (Id, Has_Discriminants  (T));
3412                Set_Is_Constrained       (Id, Is_Constrained     (T));
3413                Set_Is_Limited_Record    (Id, Is_Limited_Record  (T));
3414                Set_Has_Unknown_Discriminants
3415                                         (Id, Has_Unknown_Discriminants (T));
3416
3417                if Has_Discriminants (T) then
3418                   Set_Discriminant_Constraint
3419                                         (Id, Discriminant_Constraint (T));
3420                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3421
3422                elsif Has_Unknown_Discriminants (Id) then
3423                   Set_Discriminant_Constraint (Id, No_Elist);
3424                end if;
3425
3426                if Is_Tagged_Type (T) then
3427                   Set_Is_Tagged_Type    (Id);
3428                   Set_Is_Abstract_Type  (Id, Is_Abstract_Type (T));
3429                   Set_Primitive_Operations
3430                                         (Id, Primitive_Operations (T));
3431                   Set_Class_Wide_Type   (Id, Class_Wide_Type (T));
3432
3433                   if Is_Interface (T) then
3434                      Set_Is_Interface (Id);
3435                      Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
3436                   end if;
3437                end if;
3438
3439             when Private_Kind =>
3440                Set_Ekind              (Id, Subtype_Kind (Ekind   (T)));
3441                Set_Has_Discriminants  (Id, Has_Discriminants     (T));
3442                Set_Is_Constrained     (Id, Is_Constrained        (T));
3443                Set_First_Entity       (Id, First_Entity          (T));
3444                Set_Last_Entity        (Id, Last_Entity           (T));
3445                Set_Private_Dependents (Id, New_Elmt_List);
3446                Set_Is_Limited_Record  (Id, Is_Limited_Record     (T));
3447                Set_Has_Unknown_Discriminants
3448                                       (Id, Has_Unknown_Discriminants (T));
3449                Set_Known_To_Have_Preelab_Init
3450                                       (Id, Known_To_Have_Preelab_Init (T));
3451
3452                if Is_Tagged_Type (T) then
3453                   Set_Is_Tagged_Type       (Id);
3454                   Set_Is_Abstract_Type     (Id, Is_Abstract_Type (T));
3455                   Set_Primitive_Operations (Id, Primitive_Operations (T));
3456                   Set_Class_Wide_Type      (Id, Class_Wide_Type (T));
3457                end if;
3458
3459                --  In general the attributes of the subtype of a private type
3460                --  are the attributes of the partial view of parent. However,
3461                --  the full view may be a discriminated type, and the subtype
3462                --  must share the discriminant constraint to generate correct
3463                --  calls to initialization procedures.
3464
3465                if Has_Discriminants (T) then
3466                   Set_Discriminant_Constraint
3467                                      (Id, Discriminant_Constraint (T));
3468                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3469
3470                elsif Present (Full_View (T))
3471                  and then Has_Discriminants (Full_View (T))
3472                then
3473                   Set_Discriminant_Constraint
3474                                (Id, Discriminant_Constraint (Full_View (T)));
3475                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3476
3477                   --  This would seem semantically correct, but apparently
3478                   --  confuses the back-end. To be explained and checked with
3479                   --  current version ???
3480
3481                   --  Set_Has_Discriminants (Id);
3482                end if;
3483
3484                Prepare_Private_Subtype_Completion (Id, N);
3485
3486             when Access_Kind =>
3487                Set_Ekind             (Id, E_Access_Subtype);
3488                Set_Is_Constrained    (Id, Is_Constrained        (T));
3489                Set_Is_Access_Constant
3490                                      (Id, Is_Access_Constant    (T));
3491                Set_Directly_Designated_Type
3492                                      (Id, Designated_Type       (T));
3493                Set_Can_Never_Be_Null (Id, Can_Never_Be_Null     (T));
3494
3495                --  A Pure library_item must not contain the declaration of a
3496                --  named access type, except within a subprogram, generic
3497                --  subprogram, task unit, or protected unit (RM 10.2.1(16)).
3498
3499                if Comes_From_Source (Id)
3500                  and then In_Pure_Unit
3501                  and then not In_Subprogram_Task_Protected_Unit
3502                then
3503                   Error_Msg_N
3504                     ("named access types not allowed in pure unit", N);
3505                end if;
3506
3507             when Concurrent_Kind =>
3508                Set_Ekind                (Id, Subtype_Kind (Ekind   (T)));
3509                Set_Corresponding_Record_Type (Id,
3510                                          Corresponding_Record_Type (T));
3511                Set_First_Entity         (Id, First_Entity          (T));
3512                Set_First_Private_Entity (Id, First_Private_Entity  (T));
3513                Set_Has_Discriminants    (Id, Has_Discriminants     (T));
3514                Set_Is_Constrained       (Id, Is_Constrained        (T));
3515                Set_Is_Tagged_Type       (Id, Is_Tagged_Type        (T));
3516                Set_Last_Entity          (Id, Last_Entity           (T));
3517
3518                if Has_Discriminants (T) then
3519                   Set_Discriminant_Constraint (Id,
3520                                            Discriminant_Constraint (T));
3521                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3522                end if;
3523
3524             when E_Incomplete_Type =>
3525                if Ada_Version >= Ada_05 then
3526                   Set_Ekind (Id, E_Incomplete_Subtype);
3527
3528                   --  Ada 2005 (AI-412): Decorate an incomplete subtype
3529                   --  of an incomplete type visible through a limited
3530                   --  with clause.
3531
3532                   if From_With_Type (T)
3533                     and then Present (Non_Limited_View (T))
3534                   then
3535                      Set_From_With_Type   (Id);
3536                      Set_Non_Limited_View (Id, Non_Limited_View (T));
3537
3538                   --  Ada 2005 (AI-412): Add the regular incomplete subtype
3539                   --  to the private dependents of the original incomplete
3540                   --  type for future transformation.
3541
3542                   else
3543                      Append_Elmt (Id, Private_Dependents (T));
3544                   end if;
3545
3546                --  If the subtype name denotes an incomplete type an error
3547                --  was already reported by Process_Subtype.
3548
3549                else
3550                   Set_Etype (Id, Any_Type);
3551                end if;
3552
3553             when others =>
3554                raise Program_Error;
3555          end case;
3556       end if;
3557
3558       if Etype (Id) = Any_Type then
3559          return;
3560       end if;
3561
3562       --  Some common processing on all types
3563
3564       Set_Size_Info      (Id,                 T);
3565       Set_First_Rep_Item (Id, First_Rep_Item (T));
3566
3567       T := Etype (Id);
3568
3569       Set_Is_Immediately_Visible   (Id, True);
3570       Set_Depends_On_Private       (Id, Has_Private_Component (T));
3571       Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
3572
3573       if Is_Interface (T) then
3574          Set_Is_Interface (Id);
3575       end if;
3576
3577       if Present (Generic_Parent_Type (N))
3578         and then
3579           (Nkind
3580              (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
3581             or else Nkind
3582               (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
3583                 /=  N_Formal_Private_Type_Definition)
3584       then
3585          if Is_Tagged_Type (Id) then
3586
3587             --  If this is a generic actual subtype for a synchronized type,
3588             --  the primitive operations are those of the corresponding record
3589             --  for which there is a separate subtype declaration.
3590
3591             if Is_Concurrent_Type (Id) then
3592                null;
3593             elsif Is_Class_Wide_Type (Id) then
3594                Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
3595             else
3596                Derive_Subprograms (Generic_Parent_Type (N), Id, T);
3597             end if;
3598
3599          elsif Scope (Etype (Id)) /= Standard_Standard then
3600             Derive_Subprograms (Generic_Parent_Type (N), Id);
3601          end if;
3602       end if;
3603
3604       if Is_Private_Type (T)
3605         and then Present (Full_View (T))
3606       then
3607          Conditional_Delay (Id, Full_View (T));
3608
3609       --  The subtypes of components or subcomponents of protected types
3610       --  do not need freeze nodes, which would otherwise appear in the
3611       --  wrong scope (before the freeze node for the protected type). The
3612       --  proper subtypes are those of the subcomponents of the corresponding
3613       --  record.
3614
3615       elsif Ekind (Scope (Id)) /= E_Protected_Type
3616         and then Present (Scope (Scope (Id))) -- error defense!
3617         and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
3618       then
3619          Conditional_Delay (Id, T);
3620       end if;
3621
3622       --  Check that constraint_error is raised for a scalar subtype
3623       --  indication when the lower or upper bound of a non-null range
3624       --  lies outside the range of the type mark.
3625
3626       if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
3627          if Is_Scalar_Type (Etype (Id))
3628             and then Scalar_Range (Id) /=
3629                      Scalar_Range (Etype (Subtype_Mark
3630                                            (Subtype_Indication (N))))
3631          then
3632             Apply_Range_Check
3633               (Scalar_Range (Id),
3634                Etype (Subtype_Mark (Subtype_Indication (N))));
3635
3636          elsif Is_Array_Type (Etype (Id))
3637            and then Present (First_Index (Id))
3638          then
3639             --  This really should be a subprogram that finds the indications
3640             --  to check???
3641
3642             if ((Nkind (First_Index (Id)) = N_Identifier
3643                    and then Ekind (Entity (First_Index (Id))) in Scalar_Kind)
3644                  or else Nkind (First_Index (Id)) = N_Subtype_Indication)
3645               and then
3646                 Nkind (Scalar_Range (Etype (First_Index (Id)))) = N_Range
3647             then
3648                declare
3649                   Target_Typ : constant Entity_Id :=
3650                                  Etype
3651                                    (First_Index (Etype
3652                                      (Subtype_Mark (Subtype_Indication (N)))));
3653                begin
3654                   R_Checks :=
3655                     Get_Range_Checks
3656                       (Scalar_Range (Etype (First_Index (Id))),
3657                        Target_Typ,
3658                        Etype (First_Index (Id)),
3659                        Defining_Identifier (N));
3660
3661                   Insert_Range_Checks
3662                     (R_Checks,
3663                      N,
3664                      Target_Typ,
3665                      Sloc (Defining_Identifier (N)));
3666                end;
3667             end if;
3668          end if;
3669       end if;
3670
3671       Set_Optimize_Alignment_Flags (Id);
3672       Check_Eliminated (Id);
3673    end Analyze_Subtype_Declaration;
3674
3675    --------------------------------
3676    -- Analyze_Subtype_Indication --
3677    --------------------------------
3678
3679    procedure Analyze_Subtype_Indication (N : Node_Id) is
3680       T : constant Entity_Id := Subtype_Mark (N);
3681       R : constant Node_Id   := Range_Expression (Constraint (N));
3682
3683    begin
3684       Analyze (T);
3685
3686       if R /= Error then
3687          Analyze (R);
3688          Set_Etype (N, Etype (R));
3689          Resolve (R, Entity (T));
3690       else
3691          Set_Error_Posted (R);
3692          Set_Error_Posted (T);
3693       end if;
3694    end Analyze_Subtype_Indication;
3695
3696    ------------------------------
3697    -- Analyze_Type_Declaration --
3698    ------------------------------
3699
3700    procedure Analyze_Type_Declaration (N : Node_Id) is
3701       Def    : constant Node_Id   := Type_Definition (N);
3702       Def_Id : constant Entity_Id := Defining_Identifier (N);
3703       T      : Entity_Id;
3704       Prev   : Entity_Id;
3705
3706       Is_Remote : constant Boolean :=
3707                     (Is_Remote_Types (Current_Scope)
3708                        or else Is_Remote_Call_Interface (Current_Scope))
3709                     and then not (In_Private_Part (Current_Scope)
3710                                     or else In_Package_Body (Current_Scope));
3711
3712       procedure Check_Ops_From_Incomplete_Type;
3713       --  If there is a tagged incomplete partial view of the type, transfer
3714       --  its operations to the full view, and indicate that the type of the
3715       --  controlling parameter (s) is this full view.
3716
3717       ------------------------------------
3718       -- Check_Ops_From_Incomplete_Type --
3719       ------------------------------------
3720
3721       procedure Check_Ops_From_Incomplete_Type is
3722          Elmt   : Elmt_Id;
3723          Formal : Entity_Id;
3724          Op     : Entity_Id;
3725
3726       begin
3727          if Prev /= T
3728            and then Ekind (Prev) = E_Incomplete_Type
3729            and then Is_Tagged_Type (Prev)
3730            and then Is_Tagged_Type (T)
3731          then
3732             Elmt := First_Elmt (Primitive_Operations (Prev));
3733             while Present (Elmt) loop
3734                Op := Node (Elmt);
3735                Prepend_Elmt (Op, Primitive_Operations (T));
3736
3737                Formal := First_Formal (Op);
3738                while Present (Formal) loop
3739                   if Etype (Formal) = Prev then
3740                      Set_Etype (Formal, T);
3741                   end if;
3742
3743                   Next_Formal (Formal);
3744                end loop;
3745
3746                if Etype (Op) = Prev then
3747                   Set_Etype (Op, T);
3748                end if;
3749
3750                Next_Elmt (Elmt);
3751             end loop;
3752          end if;
3753       end Check_Ops_From_Incomplete_Type;
3754
3755    --  Start of processing for Analyze_Type_Declaration
3756
3757    begin
3758       Prev := Find_Type_Name (N);
3759
3760       --  The full view, if present, now points to the current type
3761
3762       --  Ada 2005 (AI-50217): If the type was previously decorated when
3763       --  imported through a LIMITED WITH clause, it appears as incomplete
3764       --  but has no full view.
3765       --  If the incomplete view is tagged, a class_wide type has been
3766       --  created already. Use it for the full view as well, to prevent
3767       --  multiple incompatible class-wide types that may be  created for
3768       --  self-referential anonymous access components.
3769
3770       if Ekind (Prev) = E_Incomplete_Type
3771         and then Present (Full_View (Prev))
3772       then
3773          T := Full_View (Prev);
3774
3775          if Is_Tagged_Type (Prev)
3776            and then Present (Class_Wide_Type (Prev))
3777          then
3778             Set_Ekind (T, Ekind (Prev));         --  will be reset later
3779             Set_Class_Wide_Type (T, Class_Wide_Type (Prev));
3780             Set_Etype (Class_Wide_Type (T), T);
3781          end if;
3782
3783       else
3784          T := Prev;
3785       end if;
3786
3787       Set_Is_Pure (T, Is_Pure (Current_Scope));
3788
3789       --  We set the flag Is_First_Subtype here. It is needed to set the
3790       --  corresponding flag for the Implicit class-wide-type created
3791       --  during tagged types processing.
3792
3793       Set_Is_First_Subtype (T, True);
3794
3795       --  Only composite types other than array types are allowed to have
3796       --  discriminants.
3797
3798       case Nkind (Def) is
3799
3800          --  For derived types, the rule will be checked once we've figured
3801          --  out the parent type.
3802
3803          when N_Derived_Type_Definition =>
3804             null;
3805
3806          --  For record types, discriminants are allowed
3807
3808          when N_Record_Definition =>
3809             null;
3810
3811          when others =>
3812             if Present (Discriminant_Specifications (N)) then
3813                Error_Msg_N
3814                  ("elementary or array type cannot have discriminants",
3815                   Defining_Identifier
3816                   (First (Discriminant_Specifications (N))));
3817             end if;
3818       end case;
3819
3820       --  Elaborate the type definition according to kind, and generate
3821       --  subsidiary (implicit) subtypes where needed. We skip this if it was
3822       --  already done (this happens during the reanalysis that follows a call
3823       --  to the high level optimizer).
3824
3825       if not Analyzed (T) then
3826          Set_Analyzed (T);
3827
3828          case Nkind (Def) is
3829
3830             when N_Access_To_Subprogram_Definition =>
3831                Access_Subprogram_Declaration (T, Def);
3832
3833                --  If this is a remote access to subprogram, we must create the
3834                --  equivalent fat pointer type, and related subprograms.
3835
3836                if Is_Remote then
3837                   Process_Remote_AST_Declaration (N);
3838                end if;
3839
3840                --  Validate categorization rule against access type declaration
3841                --  usually a violation in Pure unit, Shared_Passive unit.
3842
3843                Validate_Access_Type_Declaration (T, N);
3844
3845             when N_Access_To_Object_Definition =>
3846                Access_Type_Declaration (T, Def);
3847
3848                --  Validate categorization rule against access type declaration
3849                --  usually a violation in Pure unit, Shared_Passive unit.
3850
3851                Validate_Access_Type_Declaration (T, N);
3852
3853                --  If we are in a Remote_Call_Interface package and define a
3854                --  RACW, then calling stubs and specific stream attributes
3855                --  must be added.
3856
3857                if Is_Remote
3858                  and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3859                then
3860                   Add_RACW_Features (Def_Id);
3861                end if;
3862
3863                --  Set no strict aliasing flag if config pragma seen
3864
3865                if Opt.No_Strict_Aliasing then
3866                   Set_No_Strict_Aliasing (Base_Type (Def_Id));
3867                end if;
3868
3869             when N_Array_Type_Definition =>
3870                Array_Type_Declaration (T, Def);
3871
3872             when N_Derived_Type_Definition =>
3873                Derived_Type_Declaration (T, N, T /= Def_Id);
3874
3875             when N_Enumeration_Type_Definition =>
3876                Enumeration_Type_Declaration (T, Def);
3877
3878             when N_Floating_Point_Definition =>
3879                Floating_Point_Type_Declaration (T, Def);
3880
3881             when N_Decimal_Fixed_Point_Definition =>
3882                Decimal_Fixed_Point_Type_Declaration (T, Def);
3883
3884             when N_Ordinary_Fixed_Point_Definition =>
3885                Ordinary_Fixed_Point_Type_Declaration (T, Def);
3886
3887             when N_Signed_Integer_Type_Definition =>
3888                Signed_Integer_Type_Declaration (T, Def);
3889
3890             when N_Modular_Type_Definition =>
3891                Modular_Type_Declaration (T, Def);
3892
3893             when N_Record_Definition =>
3894                Record_Type_Declaration (T, N, Prev);
3895
3896             when others =>
3897                raise Program_Error;
3898
3899          end case;
3900       end if;
3901
3902       if Etype (T) = Any_Type then
3903          return;
3904       end if;
3905
3906       --  Some common processing for all types
3907
3908       Set_Depends_On_Private (T, Has_Private_Component (T));
3909       Check_Ops_From_Incomplete_Type;
3910
3911       --  Both the declared entity, and its anonymous base type if one
3912       --  was created, need freeze nodes allocated.
3913
3914       declare
3915          B : constant Entity_Id := Base_Type (T);
3916
3917       begin
3918          --  In the case where the base type differs from the first subtype, we
3919          --  pre-allocate a freeze node, and set the proper link to the first
3920          --  subtype. Freeze_Entity will use this preallocated freeze node when
3921          --  it freezes the entity.
3922
3923          if B /= T then
3924             Ensure_Freeze_Node (B);
3925             Set_First_Subtype_Link (Freeze_Node (B), T);
3926          end if;
3927
3928          if not From_With_Type (T) then
3929             Set_Has_Delayed_Freeze (T);
3930          end if;
3931       end;
3932
3933       --  Case of T is the full declaration of some private type which has
3934       --  been swapped in Defining_Identifier (N).
3935
3936       if T /= Def_Id and then Is_Private_Type (Def_Id) then
3937          Process_Full_View (N, T, Def_Id);
3938
3939          --  Record the reference. The form of this is a little strange, since
3940          --  the full declaration has been swapped in. So the first parameter
3941          --  here represents the entity to which a reference is made which is
3942          --  the "real" entity, i.e. the one swapped in, and the second
3943          --  parameter provides the reference location.
3944
3945          --  Also, we want to kill Has_Pragma_Unreferenced temporarily here
3946          --  since we don't want a complaint about the full type being an
3947          --  unwanted reference to the private type
3948
3949          declare
3950             B : constant Boolean := Has_Pragma_Unreferenced (T);
3951          begin
3952             Set_Has_Pragma_Unreferenced (T, False);
3953             Generate_Reference (T, T, 'c');
3954             Set_Has_Pragma_Unreferenced (T, B);
3955          end;
3956
3957          Set_Completion_Referenced (Def_Id);
3958
3959       --  For completion of incomplete type, process incomplete dependents
3960       --  and always mark the full type as referenced (it is the incomplete
3961       --  type that we get for any real reference).
3962
3963       elsif Ekind (Prev) = E_Incomplete_Type then
3964          Process_Incomplete_Dependents (N, T, Prev);
3965          Generate_Reference (Prev, Def_Id, 'c');
3966          Set_Completion_Referenced (Def_Id);
3967
3968       --  If not private type or incomplete type completion, this is a real
3969       --  definition of a new entity, so record it.
3970
3971       else
3972          Generate_Definition (Def_Id);
3973       end if;
3974
3975       if Chars (Scope (Def_Id)) =  Name_System
3976         and then Chars (Def_Id) = Name_Address
3977         and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
3978       then
3979          Set_Is_Descendent_Of_Address (Def_Id);
3980          Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
3981          Set_Is_Descendent_Of_Address (Prev);
3982       end if;
3983
3984       Set_Optimize_Alignment_Flags (Def_Id);
3985       Check_Eliminated (Def_Id);
3986    end Analyze_Type_Declaration;
3987
3988    --------------------------
3989    -- Analyze_Variant_Part --
3990    --------------------------
3991
3992    procedure Analyze_Variant_Part (N : Node_Id) is
3993
3994       procedure Non_Static_Choice_Error (Choice : Node_Id);
3995       --  Error routine invoked by the generic instantiation below when the
3996       --  variant part has a non static choice.
3997
3998       procedure Process_Declarations (Variant : Node_Id);
3999       --  Analyzes all the declarations associated with a Variant. Needed by
4000       --  the generic instantiation below.
4001
4002       package Variant_Choices_Processing is new
4003         Generic_Choices_Processing
4004           (Get_Alternatives          => Variants,
4005            Get_Choices               => Discrete_Choices,
4006            Process_Empty_Choice      => No_OP,
4007            Process_Non_Static_Choice => Non_Static_Choice_Error,
4008            Process_Associated_Node   => Process_Declarations);
4009       use Variant_Choices_Processing;
4010       --  Instantiation of the generic choice processing package
4011
4012       -----------------------------
4013       -- Non_Static_Choice_Error --
4014       -----------------------------
4015
4016       procedure Non_Static_Choice_Error (Choice : Node_Id) is
4017       begin
4018          Flag_Non_Static_Expr
4019            ("choice given in variant part is not static!", Choice);
4020       end Non_Static_Choice_Error;
4021
4022       --------------------------
4023       -- Process_Declarations --
4024       --------------------------
4025
4026       procedure Process_Declarations (Variant : Node_Id) is
4027       begin
4028          if not Null_Present (Component_List (Variant)) then
4029             Analyze_Declarations (Component_Items (Component_List (Variant)));
4030
4031             if Present (Variant_Part (Component_List (Variant))) then
4032                Analyze (Variant_Part (Component_List (Variant)));
4033             end if;
4034          end if;
4035       end Process_Declarations;
4036
4037       --  Local Variables
4038
4039       Discr_Name : Node_Id;
4040       Discr_Type : Entity_Id;
4041
4042       Case_Table     : Choice_Table_Type (1 .. Number_Of_Choices (N));
4043       Last_Choice    : Nat;
4044       Dont_Care      : Boolean;
4045       Others_Present : Boolean := False;
4046
4047       pragma Warnings (Off, Case_Table);
4048       pragma Warnings (Off, Last_Choice);
4049       pragma Warnings (Off, Dont_Care);
4050       pragma Warnings (Off, Others_Present);
4051       --  We don't care about the assigned values of any of these
4052
4053    --  Start of processing for Analyze_Variant_Part
4054
4055    begin
4056       Discr_Name := Name (N);
4057       Analyze (Discr_Name);
4058
4059       --  If Discr_Name bad, get out (prevent cascaded errors)
4060
4061       if Etype (Discr_Name) = Any_Type then
4062          return;
4063       end if;
4064
4065       --  Check invalid discriminant in variant part
4066
4067       if Ekind (Entity (Discr_Name)) /= E_Discriminant then
4068          Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
4069       end if;
4070
4071       Discr_Type := Etype (Entity (Discr_Name));
4072
4073       if not Is_Discrete_Type (Discr_Type) then
4074          Error_Msg_N
4075            ("discriminant in a variant part must be of a discrete type",
4076              Name (N));
4077          return;
4078       end if;
4079
4080       --  Call the instantiated Analyze_Choices which does the rest of the work
4081
4082       Analyze_Choices
4083         (N, Discr_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
4084    end Analyze_Variant_Part;
4085
4086    ----------------------------
4087    -- Array_Type_Declaration --
4088    ----------------------------
4089
4090    procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
4091       Component_Def : constant Node_Id := Component_Definition (Def);
4092       Element_Type  : Entity_Id;
4093       Implicit_Base : Entity_Id;
4094       Index         : Node_Id;
4095       Related_Id    : Entity_Id := Empty;
4096       Nb_Index      : Nat;
4097       P             : constant Node_Id := Parent (Def);
4098       Priv          : Entity_Id;
4099
4100    begin
4101       if Nkind (Def) = N_Constrained_Array_Definition then
4102          Index := First (Discrete_Subtype_Definitions (Def));
4103       else
4104          Index := First (Subtype_Marks (Def));
4105       end if;
4106
4107       --  Find proper names for the implicit types which may be public. In case
4108       --  of anonymous arrays we use the name of the first object of that type
4109       --  as prefix.
4110
4111       if No (T) then
4112          Related_Id :=  Defining_Identifier (P);
4113       else
4114          Related_Id := T;
4115       end if;
4116
4117       Nb_Index := 1;
4118       while Present (Index) loop
4119          Analyze (Index);
4120
4121          --  Add a subtype declaration for each index of private array type
4122          --  declaration whose etype is also private. For example:
4123
4124          --     package Pkg is
4125          --        type Index is private;
4126          --     private
4127          --        type Table is array (Index) of ...
4128          --     end;
4129
4130          --  This is currently required by the expander for the internally
4131          --  generated equality subprogram of records with variant parts in
4132          --  which the etype of some component is such private type.
4133
4134          if Ekind (Current_Scope) = E_Package
4135            and then In_Private_Part (Current_Scope)
4136            and then Has_Private_Declaration (Etype (Index))
4137          then
4138             declare
4139                Loc   : constant Source_Ptr := Sloc (Def);
4140                New_E : Entity_Id;
4141                Decl  : Entity_Id;
4142
4143             begin
4144                New_E :=
4145                  Make_Defining_Identifier (Loc,
4146                    Chars => New_Internal_Name ('T'));
4147                Set_Is_Internal (New_E);
4148
4149                Decl :=
4150                  Make_Subtype_Declaration (Loc,
4151                    Defining_Identifier => New_E,
4152                    Subtype_Indication  =>
4153                      New_Occurrence_Of (Etype (Index), Loc));
4154
4155                Insert_Before (Parent (Def), Decl);
4156                Analyze (Decl);
4157                Set_Etype (Index, New_E);
4158
4159                --  If the index is a range the Entity attribute is not
4160                --  available. Example:
4161
4162                --     package Pkg is
4163                --        type T is private;
4164                --     private
4165                --        type T is new Natural;
4166                --        Table : array (T(1) .. T(10)) of Boolean;
4167                --     end Pkg;
4168
4169                if Nkind (Index) /= N_Range then
4170                   Set_Entity (Index, New_E);
4171                end if;
4172             end;
4173          end if;
4174
4175          Make_Index (Index, P, Related_Id, Nb_Index);
4176          Next_Index (Index);
4177          Nb_Index := Nb_Index + 1;
4178       end loop;
4179
4180       --  Process subtype indication if one is present
4181
4182       if Present (Subtype_Indication (Component_Def)) then
4183          Element_Type :=
4184            Process_Subtype
4185              (Subtype_Indication (Component_Def), P, Related_Id, 'C');
4186
4187       --  Ada 2005 (AI-230): Access Definition case
4188
4189       else pragma Assert (Present (Access_Definition (Component_Def)));
4190
4191          --  Indicate that the anonymous access type is created by the
4192          --  array type declaration.
4193
4194          Element_Type := Access_Definition
4195                            (Related_Nod => P,
4196                             N           => Access_Definition (Component_Def));
4197          Set_Is_Local_Anonymous_Access (Element_Type);
4198
4199          --  Propagate the parent. This field is needed if we have to generate
4200          --  the master_id associated with an anonymous access to task type
4201          --  component (see Expand_N_Full_Type_Declaration.Build_Master)
4202
4203          Set_Parent (Element_Type, Parent (T));
4204
4205          --  Ada 2005 (AI-230): In case of components that are anonymous access
4206          --  types the level of accessibility depends on the enclosing type
4207          --  declaration
4208
4209          Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
4210
4211          --  Ada 2005 (AI-254)
4212
4213          declare
4214             CD : constant Node_Id :=
4215                    Access_To_Subprogram_Definition
4216                      (Access_Definition (Component_Def));
4217          begin
4218             if Present (CD) and then Protected_Present (CD) then
4219                Element_Type :=
4220                  Replace_Anonymous_Access_To_Protected_Subprogram (Def);
4221             end if;
4222          end;
4223       end if;
4224
4225       --  Constrained array case
4226
4227       if No (T) then
4228          T := Create_Itype (E_Void, P, Related_Id, 'T');
4229       end if;
4230
4231       if Nkind (Def) = N_Constrained_Array_Definition then
4232
4233          --  Establish Implicit_Base as unconstrained base type
4234
4235          Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
4236
4237          Set_Etype              (Implicit_Base, Implicit_Base);
4238          Set_Scope              (Implicit_Base, Current_Scope);
4239          Set_Has_Delayed_Freeze (Implicit_Base);
4240
4241          --  The constrained array type is a subtype of the unconstrained one
4242
4243          Set_Ekind          (T, E_Array_Subtype);
4244          Init_Size_Align    (T);
4245          Set_Etype          (T, Implicit_Base);
4246          Set_Scope          (T, Current_Scope);
4247          Set_Is_Constrained (T, True);
4248          Set_First_Index    (T, First (Discrete_Subtype_Definitions (Def)));
4249          Set_Has_Delayed_Freeze (T);
4250
4251          --  Complete setup of implicit base type
4252
4253          Set_First_Index       (Implicit_Base, First_Index (T));
4254          Set_Component_Type    (Implicit_Base, Element_Type);
4255          Set_Has_Task          (Implicit_Base, Has_Task (Element_Type));
4256          Set_Component_Size    (Implicit_Base, Uint_0);
4257          Set_Packed_Array_Type (Implicit_Base, Empty);
4258          Set_Has_Controlled_Component
4259                                (Implicit_Base, Has_Controlled_Component
4260                                                         (Element_Type)
4261                                                  or else Is_Controlled
4262                                                         (Element_Type));
4263          Set_Finalize_Storage_Only
4264                                (Implicit_Base, Finalize_Storage_Only
4265                                                         (Element_Type));
4266
4267       --  Unconstrained array case
4268
4269       else
4270          Set_Ekind                    (T, E_Array_Type);
4271          Init_Size_Align              (T);
4272          Set_Etype                    (T, T);
4273          Set_Scope                    (T, Current_Scope);
4274          Set_Component_Size           (T, Uint_0);
4275          Set_Is_Constrained           (T, False);
4276          Set_First_Index              (T, First (Subtype_Marks (Def)));
4277          Set_Has_Delayed_Freeze       (T, True);
4278          Set_Has_Task                 (T, Has_Task      (Element_Type));
4279          Set_Has_Controlled_Component (T, Has_Controlled_Component
4280                                                         (Element_Type)
4281                                             or else
4282                                           Is_Controlled (Element_Type));
4283          Set_Finalize_Storage_Only    (T, Finalize_Storage_Only
4284                                                         (Element_Type));
4285       end if;
4286
4287       --  Common attributes for both cases
4288
4289       Set_Component_Type (Base_Type (T), Element_Type);
4290       Set_Packed_Array_Type (T, Empty);
4291
4292       if Aliased_Present (Component_Definition (Def)) then
4293          Set_Has_Aliased_Components (Etype (T));
4294       end if;
4295
4296       --  Ada 2005 (AI-231): Propagate the null-excluding attribute to the
4297       --  array type to ensure that objects of this type are initialized.
4298
4299       if Ada_Version >= Ada_05
4300         and then Can_Never_Be_Null (Element_Type)
4301       then
4302          Set_Can_Never_Be_Null (T);
4303
4304          if Null_Exclusion_Present (Component_Definition (Def))
4305
4306             --  No need to check itypes because in their case this check was
4307             --  done at their point of creation
4308
4309            and then not Is_Itype (Element_Type)
4310          then
4311             Error_Msg_N
4312               ("`NOT NULL` not allowed (null already excluded)",
4313                Subtype_Indication (Component_Definition (Def)));
4314          end if;
4315       end if;
4316
4317       Priv := Private_Component (Element_Type);
4318
4319       if Present (Priv) then
4320
4321          --  Check for circular definitions
4322
4323          if Priv = Any_Type then
4324             Set_Component_Type (Etype (T), Any_Type);
4325
4326          --  There is a gap in the visibility of operations on the composite
4327          --  type only if the component type is defined in a different scope.
4328
4329          elsif Scope (Priv) = Current_Scope then
4330             null;
4331
4332          elsif Is_Limited_Type (Priv) then
4333             Set_Is_Limited_Composite (Etype (T));
4334             Set_Is_Limited_Composite (T);
4335          else
4336             Set_Is_Private_Composite (Etype (T));
4337             Set_Is_Private_Composite (T);
4338          end if;
4339       end if;
4340
4341       --  A syntax error in the declaration itself may lead to an empty index
4342       --  list, in which case do a minimal patch.
4343
4344       if No (First_Index (T)) then
4345          Error_Msg_N ("missing index definition in array type declaration", T);
4346
4347          declare
4348             Indices : constant List_Id :=
4349                         New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
4350          begin
4351             Set_Discrete_Subtype_Definitions (Def, Indices);
4352             Set_First_Index (T, First (Indices));
4353             return;
4354          end;
4355       end if;
4356
4357       --  Create a concatenation operator for the new type. Internal array
4358       --  types created for packed entities do not need such, they are
4359       --  compatible with the user-defined type.
4360
4361       if Number_Dimensions (T) = 1
4362          and then not Is_Packed_Array_Type (T)
4363       then
4364          New_Concatenation_Op (T);
4365       end if;
4366
4367       --  In the case of an unconstrained array the parser has already verified
4368       --  that all the indices are unconstrained but we still need to make sure
4369       --  that the element type is constrained.
4370
4371       if Is_Indefinite_Subtype (Element_Type) then
4372          Error_Msg_N
4373            ("unconstrained element type in array declaration",
4374             Subtype_Indication (Component_Def));
4375
4376       elsif Is_Abstract_Type (Element_Type) then
4377          Error_Msg_N
4378            ("the type of a component cannot be abstract",
4379             Subtype_Indication (Component_Def));
4380       end if;
4381    end Array_Type_Declaration;
4382
4383    ------------------------------------------------------
4384    -- Replace_Anonymous_Access_To_Protected_Subprogram --
4385    ------------------------------------------------------
4386
4387    function Replace_Anonymous_Access_To_Protected_Subprogram
4388      (N : Node_Id) return Entity_Id
4389    is
4390       Loc : constant Source_Ptr := Sloc (N);
4391
4392       Curr_Scope : constant Scope_Stack_Entry :=
4393                      Scope_Stack.Table (Scope_Stack.Last);
4394
4395       Anon : constant Entity_Id :=
4396                Make_Defining_Identifier (Loc,
4397                  Chars => New_Internal_Name ('S'));
4398
4399       Acc  : Node_Id;
4400       Comp : Node_Id;
4401       Decl : Node_Id;
4402       P    : Node_Id;
4403
4404    begin
4405       Set_Is_Internal (Anon);
4406
4407       case Nkind (N) is
4408          when N_Component_Declaration       |
4409            N_Unconstrained_Array_Definition |
4410            N_Constrained_Array_Definition   =>
4411             Comp := Component_Definition (N);
4412             Acc  := Access_Definition (Comp);
4413
4414          when N_Discriminant_Specification =>
4415             Comp := Discriminant_Type (N);
4416             Acc  := Comp;
4417
4418          when N_Parameter_Specification =>
4419             Comp := Parameter_Type (N);
4420             Acc  := Comp;
4421
4422          when N_Access_Function_Definition  =>
4423             Comp := Result_Definition (N);
4424             Acc  := Comp;
4425
4426          when N_Object_Declaration  =>
4427             Comp := Object_Definition (N);
4428             Acc  := Comp;
4429
4430          when others =>
4431             raise Program_Error;
4432       end case;
4433
4434       Decl := Make_Full_Type_Declaration (Loc,
4435                 Defining_Identifier => Anon,
4436                 Type_Definition   =>
4437                   Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
4438
4439       Mark_Rewrite_Insertion (Decl);
4440
4441       --  Insert the new declaration in the nearest enclosing scope
4442
4443       P := Parent (N);
4444       while Present (P) and then not Has_Declarations (P) loop
4445          P := Parent (P);
4446       end loop;
4447
4448       pragma Assert (Present (P));
4449
4450       if Nkind (P) = N_Package_Specification then
4451          Prepend (Decl, Visible_Declarations (P));
4452       else
4453          Prepend (Decl, Declarations (P));
4454       end if;
4455
4456       --  Replace the anonymous type with an occurrence of the new declaration.
4457       --  In all cases the rewritten node does not have the null-exclusion
4458       --  attribute because (if present) it was already inherited by the
4459       --  anonymous entity (Anon). Thus, in case of components we do not
4460       --  inherit this attribute.
4461
4462       if Nkind (N) = N_Parameter_Specification then
4463          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4464          Set_Etype (Defining_Identifier (N), Anon);
4465          Set_Null_Exclusion_Present (N, False);
4466
4467       elsif Nkind (N) = N_Object_Declaration then
4468          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4469          Set_Etype (Defining_Identifier (N), Anon);
4470
4471       elsif Nkind (N) = N_Access_Function_Definition then
4472          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4473
4474       else
4475          Rewrite (Comp,
4476            Make_Component_Definition (Loc,
4477              Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
4478       end if;
4479
4480       Mark_Rewrite_Insertion (Comp);
4481
4482       --  Temporarily remove the current scope from the stack to add the new
4483       --  declarations to the enclosing scope
4484
4485       if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
4486          Analyze (Decl);
4487
4488       else
4489          Scope_Stack.Decrement_Last;
4490          Analyze (Decl);
4491          Set_Is_Itype (Anon);
4492          Scope_Stack.Append (Curr_Scope);
4493       end if;
4494
4495       Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
4496       Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
4497       return Anon;
4498    end Replace_Anonymous_Access_To_Protected_Subprogram;
4499
4500    -------------------------------
4501    -- Build_Derived_Access_Type --
4502    -------------------------------
4503
4504    procedure Build_Derived_Access_Type
4505      (N            : Node_Id;
4506       Parent_Type  : Entity_Id;
4507       Derived_Type : Entity_Id)
4508    is
4509       S : constant Node_Id := Subtype_Indication (Type_Definition (N));
4510
4511       Desig_Type      : Entity_Id;
4512       Discr           : Entity_Id;
4513       Discr_Con_Elist : Elist_Id;
4514       Discr_Con_El    : Elmt_Id;
4515       Subt            : Entity_Id;
4516
4517    begin
4518       --  Set the designated type so it is available in case this is an access
4519       --  to a self-referential type, e.g. a standard list type with a next
4520       --  pointer. Will be reset after subtype is built.
4521
4522       Set_Directly_Designated_Type
4523         (Derived_Type, Designated_Type (Parent_Type));
4524
4525       Subt := Process_Subtype (S, N);
4526
4527       if Nkind (S) /= N_Subtype_Indication
4528         and then Subt /= Base_Type (Subt)
4529       then
4530          Set_Ekind (Derived_Type, E_Access_Subtype);
4531       end if;
4532
4533       if Ekind (Derived_Type) = E_Access_Subtype then
4534          declare
4535             Pbase      : constant Entity_Id := Base_Type (Parent_Type);
4536             Ibase      : constant Entity_Id :=
4537                            Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
4538             Svg_Chars  : constant Name_Id   := Chars (Ibase);
4539             Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
4540
4541          begin
4542             Copy_Node (Pbase, Ibase);
4543
4544             Set_Chars             (Ibase, Svg_Chars);
4545             Set_Next_Entity       (Ibase, Svg_Next_E);
4546             Set_Sloc              (Ibase, Sloc (Derived_Type));
4547             Set_Scope             (Ibase, Scope (Derived_Type));
4548             Set_Freeze_Node       (Ibase, Empty);
4549             Set_Is_Frozen         (Ibase, False);
4550             Set_Comes_From_Source (Ibase, False);
4551             Set_Is_First_Subtype  (Ibase, False);
4552
4553             Set_Etype (Ibase, Pbase);
4554             Set_Etype (Derived_Type, Ibase);
4555          end;
4556       end if;
4557
4558       Set_Directly_Designated_Type
4559         (Derived_Type, Designated_Type (Subt));
4560
4561       Set_Is_Constrained     (Derived_Type, Is_Constrained (Subt));
4562       Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
4563       Set_Size_Info          (Derived_Type,                     Parent_Type);
4564       Set_RM_Size            (Derived_Type, RM_Size            (Parent_Type));
4565       Set_Depends_On_Private (Derived_Type,
4566                               Has_Private_Component (Derived_Type));
4567       Conditional_Delay      (Derived_Type, Subt);
4568
4569       --  Ada 2005 (AI-231). Set the null-exclusion attribute
4570
4571       if Null_Exclusion_Present (Type_Definition (N))
4572         or else Can_Never_Be_Null (Parent_Type)
4573       then
4574          Set_Can_Never_Be_Null (Derived_Type);
4575       end if;
4576
4577       --  Note: we do not copy the Storage_Size_Variable, since we always go to
4578       --  the root type for this information.
4579
4580       --  Apply range checks to discriminants for derived record case
4581       --  ??? THIS CODE SHOULD NOT BE HERE REALLY.
4582
4583       Desig_Type := Designated_Type (Derived_Type);
4584       if Is_Composite_Type (Desig_Type)
4585         and then (not Is_Array_Type (Desig_Type))
4586         and then Has_Discriminants (Desig_Type)
4587         and then Base_Type (Desig_Type) /= Desig_Type
4588       then
4589          Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
4590          Discr_Con_El := First_Elmt (Discr_Con_Elist);
4591
4592          Discr := First_Discriminant (Base_Type (Desig_Type));
4593          while Present (Discr_Con_El) loop
4594             Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
4595             Next_Elmt (Discr_Con_El);
4596             Next_Discriminant (Discr);
4597          end loop;
4598       end if;
4599    end Build_Derived_Access_Type;
4600
4601    ------------------------------
4602    -- Build_Derived_Array_Type --
4603    ------------------------------
4604
4605    procedure Build_Derived_Array_Type
4606      (N            : Node_Id;
4607       Parent_Type  : Entity_Id;
4608       Derived_Type : Entity_Id)
4609    is
4610       Loc           : constant Source_Ptr := Sloc (N);
4611       Tdef          : constant Node_Id    := Type_Definition (N);
4612       Indic         : constant Node_Id    := Subtype_Indication (Tdef);
4613       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
4614       Implicit_Base : Entity_Id;
4615       New_Indic     : Node_Id;
4616
4617       procedure Make_Implicit_Base;
4618       --  If the parent subtype is constrained, the derived type is a subtype
4619       --  of an implicit base type derived from the parent base.
4620
4621       ------------------------
4622       -- Make_Implicit_Base --
4623       ------------------------
4624
4625       procedure Make_Implicit_Base is
4626       begin
4627          Implicit_Base :=
4628            Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
4629
4630          Set_Ekind (Implicit_Base, Ekind (Parent_Base));
4631          Set_Etype (Implicit_Base, Parent_Base);
4632
4633          Copy_Array_Subtype_Attributes   (Implicit_Base, Parent_Base);
4634          Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
4635
4636          Set_Has_Delayed_Freeze (Implicit_Base, True);
4637       end Make_Implicit_Base;
4638
4639    --  Start of processing for Build_Derived_Array_Type
4640
4641    begin
4642       if not Is_Constrained (Parent_Type) then
4643          if Nkind (Indic) /= N_Subtype_Indication then
4644             Set_Ekind (Derived_Type, E_Array_Type);
4645
4646             Copy_Array_Subtype_Attributes   (Derived_Type, Parent_Type);
4647             Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
4648
4649             Set_Has_Delayed_Freeze (Derived_Type, True);
4650
4651          else
4652             Make_Implicit_Base;
4653             Set_Etype (Derived_Type, Implicit_Base);
4654
4655             New_Indic :=
4656               Make_Subtype_Declaration (Loc,
4657                 Defining_Identifier => Derived_Type,
4658                 Subtype_Indication  =>
4659                   Make_Subtype_Indication (Loc,
4660                     Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
4661                     Constraint => Constraint (Indic)));
4662
4663             Rewrite (N, New_Indic);
4664             Analyze (N);
4665          end if;
4666
4667       else
4668          if Nkind (Indic) /= N_Subtype_Indication then
4669             Make_Implicit_Base;
4670
4671             Set_Ekind             (Derived_Type, Ekind (Parent_Type));
4672             Set_Etype             (Derived_Type, Implicit_Base);
4673             Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
4674
4675          else
4676             Error_Msg_N ("illegal constraint on constrained type", Indic);
4677          end if;
4678       end if;
4679
4680       --  If parent type is not a derived type itself, and is declared in
4681       --  closed scope (e.g. a subprogram), then we must explicitly introduce
4682       --  the new type's concatenation operator since Derive_Subprograms
4683       --  will not inherit the parent's operator. If the parent type is
4684       --  unconstrained, the operator is of the unconstrained base type.
4685
4686       if Number_Dimensions (Parent_Type) = 1
4687         and then not Is_Limited_Type (Parent_Type)
4688         and then not Is_Derived_Type (Parent_Type)
4689         and then not Is_Package_Or_Generic_Package
4690                        (Scope (Base_Type (Parent_Type)))
4691       then
4692          if not Is_Constrained (Parent_Type)
4693            and then Is_Constrained (Derived_Type)
4694          then
4695             New_Concatenation_Op (Implicit_Base);
4696          else
4697             New_Concatenation_Op (Derived_Type);
4698          end if;
4699       end if;
4700    end Build_Derived_Array_Type;
4701
4702    -----------------------------------
4703    -- Build_Derived_Concurrent_Type --
4704    -----------------------------------
4705
4706    procedure Build_Derived_Concurrent_Type
4707      (N            : Node_Id;
4708       Parent_Type  : Entity_Id;
4709       Derived_Type : Entity_Id)
4710    is
4711       D_Constraint : Node_Id;
4712       Disc_Spec    : Node_Id;
4713       Old_Disc     : Entity_Id;
4714       New_Disc     : Entity_Id;
4715
4716       Constraint_Present : constant Boolean :=
4717                              Nkind (Subtype_Indication (Type_Definition (N)))
4718                                                      = N_Subtype_Indication;
4719
4720    begin
4721       Set_Stored_Constraint (Derived_Type, No_Elist);
4722
4723       --  Copy Storage_Size and Relative_Deadline variables if task case
4724
4725       if Is_Task_Type (Parent_Type) then
4726          Set_Storage_Size_Variable (Derived_Type,
4727            Storage_Size_Variable (Parent_Type));
4728          Set_Relative_Deadline_Variable (Derived_Type,
4729            Relative_Deadline_Variable (Parent_Type));
4730       end if;
4731
4732       if Present (Discriminant_Specifications (N)) then
4733          Push_Scope (Derived_Type);
4734          Check_Or_Process_Discriminants (N, Derived_Type);
4735          End_Scope;
4736
4737       elsif Constraint_Present then
4738
4739          --  Build constrained subtype and derive from it
4740
4741          declare
4742             Loc  : constant Source_Ptr := Sloc (N);
4743             Anon : constant Entity_Id :=
4744                      Make_Defining_Identifier (Loc,
4745                        New_External_Name (Chars (Derived_Type), 'T'));
4746             Decl : Node_Id;
4747
4748          begin
4749             Decl :=
4750               Make_Subtype_Declaration (Loc,
4751                 Defining_Identifier => Anon,
4752                 Subtype_Indication =>
4753                   Subtype_Indication (Type_Definition (N)));
4754             Insert_Before (N, Decl);
4755             Analyze (Decl);
4756
4757             Rewrite (Subtype_Indication (Type_Definition (N)),
4758               New_Occurrence_Of (Anon, Loc));
4759             Set_Analyzed (Derived_Type, False);
4760             Analyze (N);
4761             return;
4762          end;
4763       end if;
4764
4765       --  All attributes are inherited from parent. In particular,
4766       --  entries and the corresponding record type are the same.
4767       --  Discriminants may be renamed, and must be treated separately.
4768
4769       Set_Has_Discriminants
4770         (Derived_Type, Has_Discriminants         (Parent_Type));
4771       Set_Corresponding_Record_Type
4772         (Derived_Type, Corresponding_Record_Type (Parent_Type));
4773
4774       --  Is_Constrained is set according the parent subtype, but is set to
4775       --  False if the derived type is declared with new discriminants.
4776
4777       Set_Is_Constrained
4778         (Derived_Type,
4779          (Is_Constrained (Parent_Type) or else Constraint_Present)
4780            and then not Present (Discriminant_Specifications (N)));
4781
4782       if Constraint_Present then
4783          if not Has_Discriminants (Parent_Type) then
4784             Error_Msg_N ("untagged parent must have discriminants", N);
4785
4786          elsif Present (Discriminant_Specifications (N)) then
4787
4788             --  Verify that new discriminants are used to constrain old ones
4789
4790             D_Constraint :=
4791               First
4792                 (Constraints
4793                   (Constraint (Subtype_Indication (Type_Definition (N)))));
4794
4795             Old_Disc  := First_Discriminant (Parent_Type);
4796             New_Disc  := First_Discriminant (Derived_Type);
4797             Disc_Spec := First (Discriminant_Specifications (N));
4798             while Present (Old_Disc) and then Present (Disc_Spec) loop
4799                if Nkind (Discriminant_Type (Disc_Spec)) /=
4800                                               N_Access_Definition
4801                then
4802                   Analyze (Discriminant_Type (Disc_Spec));
4803
4804                   if not Subtypes_Statically_Compatible (
4805                              Etype (Discriminant_Type (Disc_Spec)),
4806                                Etype (Old_Disc))
4807                   then
4808                      Error_Msg_N
4809                        ("not statically compatible with parent discriminant",
4810                         Discriminant_Type (Disc_Spec));
4811                   end if;
4812                end if;
4813
4814                if Nkind (D_Constraint) = N_Identifier
4815                  and then Chars (D_Constraint) /=
4816                           Chars (Defining_Identifier (Disc_Spec))
4817                then
4818                   Error_Msg_N ("new discriminants must constrain old ones",
4819                     D_Constraint);
4820                else
4821                   Set_Corresponding_Discriminant (New_Disc, Old_Disc);
4822                end if;
4823
4824                Next_Discriminant (Old_Disc);
4825                Next_Discriminant (New_Disc);
4826                Next (Disc_Spec);
4827             end loop;
4828
4829             if Present (Old_Disc) or else Present (Disc_Spec) then
4830                Error_Msg_N ("discriminant mismatch in derivation", N);
4831             end if;
4832
4833          end if;
4834
4835       elsif Present (Discriminant_Specifications (N)) then
4836          Error_Msg_N
4837            ("missing discriminant constraint in untagged derivation",
4838             N);
4839       end if;
4840
4841       if Present (Discriminant_Specifications (N)) then
4842          Old_Disc := First_Discriminant (Parent_Type);
4843          while Present (Old_Disc) loop
4844
4845             if No (Next_Entity (Old_Disc))
4846               or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
4847             then
4848                Set_Next_Entity (Last_Entity (Derived_Type),
4849                                          Next_Entity (Old_Disc));
4850                exit;
4851             end if;
4852
4853             Next_Discriminant (Old_Disc);
4854          end loop;
4855
4856       else
4857          Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
4858          if Has_Discriminants (Parent_Type) then
4859             Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
4860             Set_Discriminant_Constraint (
4861               Derived_Type, Discriminant_Constraint (Parent_Type));
4862          end if;
4863       end if;
4864
4865       Set_Last_Entity  (Derived_Type, Last_Entity  (Parent_Type));
4866
4867       Set_Has_Completion (Derived_Type);
4868    end Build_Derived_Concurrent_Type;
4869
4870    ------------------------------------
4871    -- Build_Derived_Enumeration_Type --
4872    ------------------------------------
4873
4874    procedure Build_Derived_Enumeration_Type
4875      (N            : Node_Id;
4876       Parent_Type  : Entity_Id;
4877       Derived_Type : Entity_Id)
4878    is
4879       Loc           : constant Source_Ptr := Sloc (N);
4880       Def           : constant Node_Id    := Type_Definition (N);
4881       Indic         : constant Node_Id    := Subtype_Indication (Def);
4882       Implicit_Base : Entity_Id;
4883       Literal       : Entity_Id;
4884       New_Lit       : Entity_Id;
4885       Literals_List : List_Id;
4886       Type_Decl     : Node_Id;
4887       Hi, Lo        : Node_Id;
4888       Rang_Expr     : Node_Id;
4889
4890    begin
4891       --  Since types Standard.Character and Standard.Wide_Character do
4892       --  not have explicit literals lists we need to process types derived
4893       --  from them specially. This is handled by Derived_Standard_Character.
4894       --  If the parent type is a generic type, there are no literals either,
4895       --  and we construct the same skeletal representation as for the generic
4896       --  parent type.
4897
4898       if Is_Standard_Character_Type (Parent_Type) then
4899          Derived_Standard_Character (N, Parent_Type, Derived_Type);
4900
4901       elsif Is_Generic_Type (Root_Type (Parent_Type)) then
4902          declare
4903             Lo : Node_Id;
4904             Hi : Node_Id;
4905
4906          begin
4907             Lo :=
4908                Make_Attribute_Reference (Loc,
4909                  Attribute_Name => Name_First,
4910                  Prefix => New_Reference_To (Derived_Type, Loc));
4911             Set_Etype (Lo, Derived_Type);
4912
4913             Hi :=
4914                Make_Attribute_Reference (Loc,
4915                  Attribute_Name => Name_Last,
4916                  Prefix => New_Reference_To (Derived_Type, Loc));
4917             Set_Etype (Hi, Derived_Type);
4918
4919             Set_Scalar_Range (Derived_Type,
4920                Make_Range (Loc,
4921                  Low_Bound => Lo,
4922                  High_Bound => Hi));
4923          end;
4924
4925       else
4926          --  If a constraint is present, analyze the bounds to catch
4927          --  premature usage of the derived literals.
4928
4929          if Nkind (Indic) = N_Subtype_Indication
4930            and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
4931          then
4932             Analyze (Low_Bound  (Range_Expression (Constraint (Indic))));
4933             Analyze (High_Bound (Range_Expression (Constraint (Indic))));
4934          end if;
4935
4936          --  Introduce an implicit base type for the derived type even if there
4937          --  is no constraint attached to it, since this seems closer to the
4938          --  Ada semantics. Build a full type declaration tree for the derived
4939          --  type using the implicit base type as the defining identifier. The
4940          --  build a subtype declaration tree which applies the constraint (if
4941          --  any) have it replace the derived type declaration.
4942
4943          Literal := First_Literal (Parent_Type);
4944          Literals_List := New_List;
4945          while Present (Literal)
4946            and then Ekind (Literal) = E_Enumeration_Literal
4947          loop
4948             --  Literals of the derived type have the same representation as
4949             --  those of the parent type, but this representation can be
4950             --  overridden by an explicit representation clause. Indicate
4951             --  that there is no explicit representation given yet. These
4952             --  derived literals are implicit operations of the new type,
4953             --  and can be overridden by explicit ones.
4954
4955             if Nkind (Literal) = N_Defining_Character_Literal then
4956                New_Lit :=
4957                  Make_Defining_Character_Literal (Loc, Chars (Literal));
4958             else
4959                New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
4960             end if;
4961
4962             Set_Ekind                (New_Lit, E_Enumeration_Literal);
4963             Set_Enumeration_Pos      (New_Lit, Enumeration_Pos (Literal));
4964             Set_Enumeration_Rep      (New_Lit, Enumeration_Rep (Literal));
4965             Set_Enumeration_Rep_Expr (New_Lit, Empty);
4966             Set_Alias                (New_Lit, Literal);
4967             Set_Is_Known_Valid       (New_Lit, True);
4968
4969             Append (New_Lit, Literals_List);
4970             Next_Literal (Literal);
4971          end loop;
4972
4973          Implicit_Base :=
4974            Make_Defining_Identifier (Sloc (Derived_Type),
4975              New_External_Name (Chars (Derived_Type), 'B'));
4976
4977          --  Indicate the proper nature of the derived type. This must be done
4978          --  before analysis of the literals, to recognize cases when a literal
4979          --  may be hidden by a previous explicit function definition (cf.
4980          --  c83031a).
4981
4982          Set_Ekind (Derived_Type, E_Enumeration_Subtype);
4983          Set_Etype (Derived_Type, Implicit_Base);
4984
4985          Type_Decl :=
4986            Make_Full_Type_Declaration (Loc,
4987              Defining_Identifier => Implicit_Base,
4988              Discriminant_Specifications => No_List,
4989              Type_Definition =>
4990                Make_Enumeration_Type_Definition (Loc, Literals_List));
4991
4992          Mark_Rewrite_Insertion (Type_Decl);
4993          Insert_Before (N, Type_Decl);
4994          Analyze (Type_Decl);
4995
4996          --  After the implicit base is analyzed its Etype needs to be changed
4997          --  to reflect the fact that it is derived from the parent type which
4998          --  was ignored during analysis. We also set the size at this point.
4999
5000          Set_Etype (Implicit_Base, Parent_Type);
5001
5002          Set_Size_Info      (Implicit_Base,                 Parent_Type);
5003          Set_RM_Size        (Implicit_Base, RM_Size        (Parent_Type));
5004          Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
5005
5006          Set_Has_Non_Standard_Rep
5007                             (Implicit_Base, Has_Non_Standard_Rep
5008                                                            (Parent_Type));
5009          Set_Has_Delayed_Freeze (Implicit_Base);
5010
5011          --  Process the subtype indication including a validation check on the
5012          --  constraint, if any. If a constraint is given, its bounds must be
5013          --  implicitly converted to the new type.
5014
5015          if Nkind (Indic) = N_Subtype_Indication then
5016             declare
5017                R : constant Node_Id :=
5018                      Range_Expression (Constraint (Indic));
5019
5020             begin
5021                if Nkind (R) = N_Range then
5022                   Hi := Build_Scalar_Bound
5023                           (High_Bound (R), Parent_Type, Implicit_Base);
5024                   Lo := Build_Scalar_Bound
5025                           (Low_Bound  (R), Parent_Type, Implicit_Base);
5026
5027                else
5028                   --  Constraint is a Range attribute. Replace with explicit
5029                   --  mention of the bounds of the prefix, which must be a
5030                   --  subtype.
5031
5032                   Analyze (Prefix (R));
5033                   Hi :=
5034                     Convert_To (Implicit_Base,
5035                       Make_Attribute_Reference (Loc,
5036                         Attribute_Name => Name_Last,
5037                         Prefix =>
5038                           New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5039
5040                   Lo :=
5041                     Convert_To (Implicit_Base,
5042                       Make_Attribute_Reference (Loc,
5043                         Attribute_Name => Name_First,
5044                         Prefix =>
5045                           New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5046                end if;
5047             end;
5048
5049          else
5050             Hi :=
5051               Build_Scalar_Bound
5052                 (Type_High_Bound (Parent_Type),
5053                  Parent_Type, Implicit_Base);
5054             Lo :=
5055                Build_Scalar_Bound
5056                  (Type_Low_Bound (Parent_Type),
5057                   Parent_Type, Implicit_Base);
5058          end if;
5059
5060          Rang_Expr :=
5061            Make_Range (Loc,
5062              Low_Bound  => Lo,
5063              High_Bound => Hi);
5064
5065          --  If we constructed a default range for the case where no range
5066          --  was given, then the expressions in the range must not freeze
5067          --  since they do not correspond to expressions in the source.
5068
5069          if Nkind (Indic) /= N_Subtype_Indication then
5070             Set_Must_Not_Freeze (Lo);
5071             Set_Must_Not_Freeze (Hi);
5072             Set_Must_Not_Freeze (Rang_Expr);
5073          end if;
5074
5075          Rewrite (N,
5076            Make_Subtype_Declaration (Loc,
5077              Defining_Identifier => Derived_Type,
5078              Subtype_Indication =>
5079                Make_Subtype_Indication (Loc,
5080                  Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
5081                  Constraint =>
5082                    Make_Range_Constraint (Loc,
5083                      Range_Expression => Rang_Expr))));
5084
5085          Analyze (N);
5086
5087          --  If pragma Discard_Names applies on the first subtype of the parent
5088          --  type, then it must be applied on this subtype as well.
5089
5090          if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
5091             Set_Discard_Names (Derived_Type);
5092          end if;
5093
5094          --  Apply a range check. Since this range expression doesn't have an
5095          --  Etype, we have to specifically pass the Source_Typ parameter. Is
5096          --  this right???
5097
5098          if Nkind (Indic) = N_Subtype_Indication then
5099             Apply_Range_Check (Range_Expression (Constraint (Indic)),
5100                                Parent_Type,
5101                                Source_Typ => Entity (Subtype_Mark (Indic)));
5102          end if;
5103       end if;
5104    end Build_Derived_Enumeration_Type;
5105
5106    --------------------------------
5107    -- Build_Derived_Numeric_Type --
5108    --------------------------------
5109
5110    procedure Build_Derived_Numeric_Type
5111      (N            : Node_Id;
5112       Parent_Type  : Entity_Id;
5113       Derived_Type : Entity_Id)
5114    is
5115       Loc           : constant Source_Ptr := Sloc (N);
5116       Tdef          : constant Node_Id    := Type_Definition (N);
5117       Indic         : constant Node_Id    := Subtype_Indication (Tdef);
5118       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
5119       No_Constraint : constant Boolean    := Nkind (Indic) /=
5120                                                   N_Subtype_Indication;
5121       Implicit_Base : Entity_Id;
5122
5123       Lo : Node_Id;
5124       Hi : Node_Id;
5125
5126    begin
5127       --  Process the subtype indication including a validation check on
5128       --  the constraint if any.
5129
5130       Discard_Node (Process_Subtype (Indic, N));
5131
5132       --  Introduce an implicit base type for the derived type even if there
5133       --  is no constraint attached to it, since this seems closer to the Ada
5134       --  semantics.
5135
5136       Implicit_Base :=
5137         Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5138
5139       Set_Etype          (Implicit_Base, Parent_Base);
5140       Set_Ekind          (Implicit_Base, Ekind          (Parent_Base));
5141       Set_Size_Info      (Implicit_Base,                 Parent_Base);
5142       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
5143       Set_Parent         (Implicit_Base, Parent (Derived_Type));
5144
5145       --  Set RM Size for discrete type or decimal fixed-point type
5146       --  Ordinary fixed-point is excluded, why???
5147
5148       if Is_Discrete_Type (Parent_Base)
5149         or else Is_Decimal_Fixed_Point_Type (Parent_Base)
5150       then
5151          Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
5152       end if;
5153
5154       Set_Has_Delayed_Freeze (Implicit_Base);
5155
5156       Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Base));
5157       Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
5158
5159       Set_Scalar_Range (Implicit_Base,
5160         Make_Range (Loc,
5161           Low_Bound  => Lo,
5162           High_Bound => Hi));
5163
5164       if Has_Infinities (Parent_Base) then
5165          Set_Includes_Infinities (Scalar_Range (Implicit_Base));
5166       end if;
5167
5168       --  The Derived_Type, which is the entity of the declaration, is a
5169       --  subtype of the implicit base. Its Ekind is a subtype, even in the
5170       --  absence of an explicit constraint.
5171
5172       Set_Etype (Derived_Type, Implicit_Base);
5173
5174       --  If we did not have a constraint, then the Ekind is set from the
5175       --  parent type (otherwise Process_Subtype has set the bounds)
5176
5177       if No_Constraint then
5178          Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
5179       end if;
5180
5181       --  If we did not have a range constraint, then set the range from the
5182       --  parent type. Otherwise, the call to Process_Subtype has set the
5183       --  bounds.
5184
5185       if No_Constraint
5186         or else not Has_Range_Constraint (Indic)
5187       then
5188          Set_Scalar_Range (Derived_Type,
5189            Make_Range (Loc,
5190              Low_Bound  => New_Copy_Tree (Type_Low_Bound  (Parent_Type)),
5191              High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
5192          Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5193
5194          if Has_Infinities (Parent_Type) then
5195             Set_Includes_Infinities (Scalar_Range (Derived_Type));
5196          end if;
5197       end if;
5198
5199       Set_Is_Descendent_Of_Address (Derived_Type,
5200         Is_Descendent_Of_Address (Parent_Type));
5201       Set_Is_Descendent_Of_Address (Implicit_Base,
5202         Is_Descendent_Of_Address (Parent_Type));
5203
5204       --  Set remaining type-specific fields, depending on numeric type
5205
5206       if Is_Modular_Integer_Type (Parent_Type) then
5207          Set_Modulus (Implicit_Base, Modulus (Parent_Base));
5208
5209          Set_Non_Binary_Modulus
5210            (Implicit_Base, Non_Binary_Modulus (Parent_Base));
5211
5212       elsif Is_Floating_Point_Type (Parent_Type) then
5213
5214          --  Digits of base type is always copied from the digits value of
5215          --  the parent base type, but the digits of the derived type will
5216          --  already have been set if there was a constraint present.
5217
5218          Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5219          Set_Vax_Float    (Implicit_Base, Vax_Float    (Parent_Base));
5220
5221          if No_Constraint then
5222             Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
5223          end if;
5224
5225       elsif Is_Fixed_Point_Type (Parent_Type) then
5226
5227          --  Small of base type and derived type are always copied from the
5228          --  parent base type, since smalls never change. The delta of the
5229          --  base type is also copied from the parent base type. However the
5230          --  delta of the derived type will have been set already if a
5231          --  constraint was present.
5232
5233          Set_Small_Value (Derived_Type,  Small_Value (Parent_Base));
5234          Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
5235          Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
5236
5237          if No_Constraint then
5238             Set_Delta_Value (Derived_Type,  Delta_Value (Parent_Type));
5239          end if;
5240
5241          --  The scale and machine radix in the decimal case are always
5242          --  copied from the parent base type.
5243
5244          if Is_Decimal_Fixed_Point_Type (Parent_Type) then
5245             Set_Scale_Value (Derived_Type,  Scale_Value (Parent_Base));
5246             Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
5247
5248             Set_Machine_Radix_10
5249               (Derived_Type,  Machine_Radix_10 (Parent_Base));
5250             Set_Machine_Radix_10
5251               (Implicit_Base, Machine_Radix_10 (Parent_Base));
5252
5253             Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5254
5255             if No_Constraint then
5256                Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
5257
5258             else
5259                --  the analysis of the subtype_indication sets the
5260                --  digits value of the derived type.
5261
5262                null;
5263             end if;
5264          end if;
5265       end if;
5266
5267       --  The type of the bounds is that of the parent type, and they
5268       --  must be converted to the derived type.
5269
5270       Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
5271
5272       --  The implicit_base should be frozen when the derived type is frozen,
5273       --  but note that it is used in the conversions of the bounds. For fixed
5274       --  types we delay the determination of the bounds until the proper
5275       --  freezing point. For other numeric types this is rejected by GCC, for
5276       --  reasons that are currently unclear (???), so we choose to freeze the
5277       --  implicit base now. In the case of integers and floating point types
5278       --  this is harmless because subsequent representation clauses cannot
5279       --  affect anything, but it is still baffling that we cannot use the
5280       --  same mechanism for all derived numeric types.
5281
5282       --  There is a further complication: actually *some* representation
5283       --  clauses can affect the implicit base type. Namely, attribute
5284       --  definition clauses for stream-oriented attributes need to set the
5285       --  corresponding TSS entries on the base type, and this normally cannot
5286       --  be done after the base type is frozen, so the circuitry in
5287       --  Sem_Ch13.New_Stream_Subprogram must account for this possibility and
5288       --  not use Set_TSS in this case.
5289
5290       if Is_Fixed_Point_Type (Parent_Type) then
5291          Conditional_Delay (Implicit_Base, Parent_Type);
5292       else
5293          Freeze_Before (N, Implicit_Base);
5294       end if;
5295    end Build_Derived_Numeric_Type;
5296
5297    --------------------------------
5298    -- Build_Derived_Private_Type --
5299    --------------------------------
5300
5301    procedure Build_Derived_Private_Type
5302      (N             : Node_Id;
5303       Parent_Type   : Entity_Id;
5304       Derived_Type  : Entity_Id;
5305       Is_Completion : Boolean;
5306       Derive_Subps  : Boolean := True)
5307    is
5308       Der_Base    : Entity_Id;
5309       Discr       : Entity_Id;
5310       Full_Decl   : Node_Id := Empty;
5311       Full_Der    : Entity_Id;
5312       Full_P      : Entity_Id;
5313       Last_Discr  : Entity_Id;
5314       Par_Scope   : constant Entity_Id := Scope (Base_Type (Parent_Type));
5315       Swapped     : Boolean := False;
5316
5317       procedure Copy_And_Build;
5318       --  Copy derived type declaration, replace parent with its full view,
5319       --  and analyze new declaration.
5320
5321       --------------------
5322       -- Copy_And_Build --
5323       --------------------
5324
5325       procedure Copy_And_Build is
5326          Full_N : Node_Id;
5327
5328       begin
5329          if Ekind (Parent_Type) in Record_Kind
5330            or else
5331              (Ekind (Parent_Type) in Enumeration_Kind
5332                and then not Is_Standard_Character_Type (Parent_Type)
5333                and then not Is_Generic_Type (Root_Type (Parent_Type)))
5334          then
5335             Full_N := New_Copy_Tree (N);
5336             Insert_After (N, Full_N);
5337             Build_Derived_Type (
5338               Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
5339
5340          else
5341             Build_Derived_Type (
5342               N, Parent_Type, Full_Der, True, Derive_Subps => False);
5343          end if;
5344       end Copy_And_Build;
5345
5346    --  Start of processing for Build_Derived_Private_Type
5347
5348    begin
5349       if Is_Tagged_Type (Parent_Type) then
5350          Build_Derived_Record_Type
5351            (N, Parent_Type, Derived_Type, Derive_Subps);
5352          return;
5353
5354       elsif Has_Discriminants (Parent_Type) then
5355          if Present (Full_View (Parent_Type)) then
5356             if not Is_Completion then
5357
5358                --  Copy declaration for subsequent analysis, to provide a
5359                --  completion for what is a private declaration. Indicate that
5360                --  the full type is internally generated.
5361
5362                Full_Decl := New_Copy_Tree (N);
5363                Full_Der  := New_Copy (Derived_Type);
5364                Set_Comes_From_Source (Full_Decl, False);
5365                Set_Comes_From_Source (Full_Der, False);
5366
5367                Insert_After (N, Full_Decl);
5368
5369             else
5370                --  If this is a completion, the full view being built is
5371                --  itself private. We build a subtype of the parent with
5372                --  the same constraints as this full view, to convey to the
5373                --  back end the constrained components and the size of this
5374                --  subtype. If the parent is constrained, its full view can
5375                --  serve as the underlying full view of the derived type.
5376
5377                if No (Discriminant_Specifications (N)) then
5378                   if Nkind (Subtype_Indication (Type_Definition (N))) =
5379                                                         N_Subtype_Indication
5380                   then
5381                      Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
5382
5383                   elsif Is_Constrained (Full_View (Parent_Type)) then
5384                      Set_Underlying_Full_View (Derived_Type,
5385                        Full_View (Parent_Type));
5386                   end if;
5387
5388                else
5389                   --  If there are new discriminants, the parent subtype is
5390                   --  constrained by them, but it is not clear how to build
5391                   --  the underlying_full_view in this case ???
5392
5393                   null;
5394                end if;
5395             end if;
5396          end if;
5397
5398          --  Build partial view of derived type from partial view of parent
5399
5400          Build_Derived_Record_Type
5401            (N, Parent_Type, Derived_Type, Derive_Subps);
5402
5403          if Present (Full_View (Parent_Type))
5404            and then not Is_Completion
5405          then
5406             if not In_Open_Scopes (Par_Scope)
5407               or else not In_Same_Source_Unit (N, Parent_Type)
5408             then
5409                --  Swap partial and full views temporarily
5410
5411                Install_Private_Declarations (Par_Scope);
5412                Install_Visible_Declarations (Par_Scope);
5413                Swapped := True;
5414             end if;
5415
5416             --  Build full view of derived type from full view of parent which
5417             --  is now installed. Subprograms have been derived on the partial
5418             --  view, the completion does not derive them anew.
5419
5420             if not Is_Tagged_Type (Parent_Type) then
5421
5422                --  If the parent is itself derived from another private type,
5423                --  installing the private declarations has not affected its
5424                --  privacy status, so use its own full view explicitly.
5425
5426                if Is_Private_Type (Parent_Type) then
5427                   Build_Derived_Record_Type
5428                     (Full_Decl, Full_View (Parent_Type), Full_Der, False);
5429                else
5430                   Build_Derived_Record_Type
5431                     (Full_Decl, Parent_Type, Full_Der, False);
5432                end if;
5433
5434             else
5435                --  If full view of parent is tagged, the completion
5436                --  inherits the proper primitive operations.
5437
5438                Set_Defining_Identifier (Full_Decl, Full_Der);
5439                Build_Derived_Record_Type
5440                  (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
5441                Set_Analyzed (Full_Decl);
5442             end if;
5443
5444             if Swapped then
5445                Uninstall_Declarations (Par_Scope);
5446
5447                if In_Open_Scopes (Par_Scope) then
5448                   Install_Visible_Declarations (Par_Scope);
5449                end if;
5450             end if;
5451
5452             Der_Base := Base_Type (Derived_Type);
5453             Set_Full_View (Derived_Type, Full_Der);
5454             Set_Full_View (Der_Base, Base_Type (Full_Der));
5455
5456             --  Copy the discriminant list from full view to the partial views
5457             --  (base type and its subtype). Gigi requires that the partial
5458             --  and full views have the same discriminants.
5459
5460             --  Note that since the partial view is pointing to discriminants
5461             --  in the full view, their scope will be that of the full view.
5462             --  This might cause some front end problems and need
5463             --  adjustment???
5464
5465             Discr := First_Discriminant (Base_Type (Full_Der));
5466             Set_First_Entity (Der_Base, Discr);
5467
5468             loop
5469                Last_Discr := Discr;
5470                Next_Discriminant (Discr);
5471                exit when No (Discr);
5472             end loop;
5473
5474             Set_Last_Entity (Der_Base, Last_Discr);
5475
5476             Set_First_Entity (Derived_Type, First_Entity (Der_Base));
5477             Set_Last_Entity  (Derived_Type, Last_Entity  (Der_Base));
5478             Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
5479
5480          else
5481             --  If this is a completion, the derived type stays private
5482             --  and there is no need to create a further full view, except
5483             --  in the unusual case when the derivation is nested within a
5484             --  child unit, see below.
5485
5486             null;
5487          end if;
5488
5489       elsif Present (Full_View (Parent_Type))
5490         and then  Has_Discriminants (Full_View (Parent_Type))
5491       then
5492          if Has_Unknown_Discriminants (Parent_Type)
5493            and then Nkind (Subtype_Indication (Type_Definition (N))) =
5494                                                          N_Subtype_Indication
5495          then
5496             Error_Msg_N
5497               ("cannot constrain type with unknown discriminants",
5498                Subtype_Indication (Type_Definition (N)));
5499             return;
5500          end if;
5501
5502          --  If full view of parent is a record type, Build full view as
5503          --  a derivation from the parent's full view. Partial view remains
5504          --  private. For code generation and linking, the full view must
5505          --  have the same public status as the partial one. This full view
5506          --  is only needed if the parent type is in an enclosing scope, so
5507          --  that the full view may actually become visible, e.g. in a child
5508          --  unit. This is both more efficient, and avoids order of freezing
5509          --  problems with the added entities.
5510
5511          if not Is_Private_Type (Full_View (Parent_Type))
5512            and then (In_Open_Scopes (Scope (Parent_Type)))
5513          then
5514             Full_Der := Make_Defining_Identifier (Sloc (Derived_Type),
5515                                               Chars (Derived_Type));
5516             Set_Is_Itype (Full_Der);
5517             Set_Has_Private_Declaration (Full_Der);
5518             Set_Has_Private_Declaration (Derived_Type);
5519             Set_Associated_Node_For_Itype (Full_Der, N);
5520             Set_Parent (Full_Der, Parent (Derived_Type));
5521             Set_Full_View (Derived_Type, Full_Der);
5522             Set_Is_Public (Full_Der, Is_Public (Derived_Type));
5523             Full_P := Full_View (Parent_Type);
5524             Exchange_Declarations (Parent_Type);
5525             Copy_And_Build;
5526             Exchange_Declarations (Full_P);
5527
5528          else
5529             Build_Derived_Record_Type
5530               (N, Full_View (Parent_Type), Derived_Type,
5531                 Derive_Subps => False);
5532          end if;
5533
5534          --  In any case, the primitive operations are inherited from
5535          --  the parent type, not from the internal full view.
5536
5537          Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
5538
5539          if Derive_Subps then
5540             Derive_Subprograms (Parent_Type, Derived_Type);
5541          end if;
5542
5543       else
5544          --  Untagged type, No discriminants on either view
5545
5546          if Nkind (Subtype_Indication (Type_Definition (N))) =
5547                                                    N_Subtype_Indication
5548          then
5549             Error_Msg_N
5550               ("illegal constraint on type without discriminants", N);
5551          end if;
5552
5553          if Present (Discriminant_Specifications (N))
5554            and then Present (Full_View (Parent_Type))
5555            and then not Is_Tagged_Type (Full_View (Parent_Type))
5556          then
5557             Error_Msg_N
5558               ("cannot add discriminants to untagged type", N);
5559          end if;
5560
5561          Set_Stored_Constraint (Derived_Type, No_Elist);
5562          Set_Is_Constrained    (Derived_Type, Is_Constrained (Parent_Type));
5563          Set_Is_Controlled     (Derived_Type, Is_Controlled  (Parent_Type));
5564          Set_Has_Controlled_Component
5565                                (Derived_Type, Has_Controlled_Component
5566                                                              (Parent_Type));
5567
5568          --  Direct controlled types do not inherit Finalize_Storage_Only flag
5569
5570          if not Is_Controlled  (Parent_Type) then
5571             Set_Finalize_Storage_Only
5572               (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
5573          end if;
5574
5575          --  Construct the implicit full view by deriving from full view of
5576          --  the parent type. In order to get proper visibility, we install
5577          --  the parent scope and its declarations.
5578
5579          --  ??? if the parent is untagged private and its completion is
5580          --  tagged, this mechanism will not work because we cannot derive
5581          --  from the tagged full view unless we have an extension
5582
5583          if Present (Full_View (Parent_Type))
5584            and then not Is_Tagged_Type (Full_View (Parent_Type))
5585            and then not Is_Completion
5586          then
5587             Full_Der :=
5588               Make_Defining_Identifier (Sloc (Derived_Type),
5589                 Chars => Chars (Derived_Type));
5590             Set_Is_Itype (Full_Der);
5591             Set_Has_Private_Declaration (Full_Der);
5592             Set_Has_Private_Declaration (Derived_Type);
5593             Set_Associated_Node_For_Itype (Full_Der, N);
5594             Set_Parent (Full_Der, Parent (Derived_Type));
5595             Set_Full_View (Derived_Type, Full_Der);
5596
5597             if not In_Open_Scopes (Par_Scope) then
5598                Install_Private_Declarations (Par_Scope);
5599                Install_Visible_Declarations (Par_Scope);
5600                Copy_And_Build;
5601                Uninstall_Declarations (Par_Scope);
5602
5603             --  If parent scope is open and in another unit, and parent has a
5604             --  completion, then the derivation is taking place in the visible
5605             --  part of a child unit. In that case retrieve the full view of
5606             --  the parent momentarily.
5607
5608             elsif not In_Same_Source_Unit (N, Parent_Type) then
5609                Full_P := Full_View (Parent_Type);
5610                Exchange_Declarations (Parent_Type);
5611                Copy_And_Build;
5612                Exchange_Declarations (Full_P);
5613
5614             --  Otherwise it is a local derivation
5615
5616             else
5617                Copy_And_Build;
5618             end if;
5619
5620             Set_Scope                (Full_Der, Current_Scope);
5621             Set_Is_First_Subtype     (Full_Der,
5622                                        Is_First_Subtype (Derived_Type));
5623             Set_Has_Size_Clause      (Full_Der, False);
5624             Set_Has_Alignment_Clause (Full_Der, False);
5625             Set_Next_Entity          (Full_Der, Empty);
5626             Set_Has_Delayed_Freeze   (Full_Der);
5627             Set_Is_Frozen            (Full_Der, False);
5628             Set_Freeze_Node          (Full_Der, Empty);
5629             Set_Depends_On_Private   (Full_Der,
5630                                         Has_Private_Component    (Full_Der));
5631             Set_Public_Status        (Full_Der);
5632          end if;
5633       end if;
5634
5635       Set_Has_Unknown_Discriminants (Derived_Type,
5636         Has_Unknown_Discriminants (Parent_Type));
5637
5638       if Is_Private_Type (Derived_Type) then
5639          Set_Private_Dependents (Derived_Type, New_Elmt_List);
5640       end if;
5641
5642       if Is_Private_Type (Parent_Type)
5643         and then Base_Type (Parent_Type) = Parent_Type
5644         and then In_Open_Scopes (Scope (Parent_Type))
5645       then
5646          Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
5647
5648          if Is_Child_Unit (Scope (Current_Scope))
5649            and then Is_Completion
5650            and then In_Private_Part (Current_Scope)
5651            and then Scope (Parent_Type) /= Current_Scope
5652          then
5653             --  This is the unusual case where a type completed by a private
5654             --  derivation occurs within a package nested in a child unit,
5655             --  and the parent is declared in an ancestor. In this case, the
5656             --  full view of the parent type will become visible in the body
5657             --  of the enclosing child, and only then will the current type
5658             --  be possibly non-private. We build a underlying full view that
5659             --  will be installed when the enclosing child body is compiled.
5660
5661             Full_Der :=
5662               Make_Defining_Identifier (Sloc (Derived_Type),
5663                 Chars => Chars (Derived_Type));
5664             Set_Is_Itype (Full_Der);
5665             Build_Itype_Reference (Full_Der, N);
5666
5667             --  The full view will be used to swap entities on entry/exit to
5668             --  the body, and must appear in the entity list for the package.
5669
5670             Append_Entity (Full_Der, Scope (Derived_Type));
5671             Set_Has_Private_Declaration (Full_Der);
5672             Set_Has_Private_Declaration (Derived_Type);
5673             Set_Associated_Node_For_Itype (Full_Der, N);
5674             Set_Parent (Full_Der, Parent (Derived_Type));
5675             Full_P := Full_View (Parent_Type);
5676             Exchange_Declarations (Parent_Type);
5677             Copy_And_Build;
5678             Exchange_Declarations (Full_P);
5679             Set_Underlying_Full_View (Derived_Type, Full_Der);
5680          end if;
5681       end if;
5682    end Build_Derived_Private_Type;
5683
5684    -------------------------------
5685    -- Build_Derived_Record_Type --
5686    -------------------------------
5687
5688    --  1. INTRODUCTION
5689
5690    --  Ideally we would like to use the same model of type derivation for
5691    --  tagged and untagged record types. Unfortunately this is not quite
5692    --  possible because the semantics of representation clauses is different
5693    --  for tagged and untagged records under inheritance. Consider the
5694    --  following:
5695
5696    --     type R (...) is [tagged] record ... end record;
5697    --     type T (...) is new R (...) [with ...];
5698
5699    --  The representation clauses for T can specify a completely different
5700    --  record layout from R's. Hence the same component can be placed in two
5701    --  very different positions in objects of type T and R. If R and are tagged
5702    --  types, representation clauses for T can only specify the layout of non
5703    --  inherited components, thus components that are common in R and T have
5704    --  the same position in objects of type R and T.
5705
5706    --  This has two implications. The first is that the entire tree for R's
5707    --  declaration needs to be copied for T in the untagged case, so that T
5708    --  can be viewed as a record type of its own with its own representation
5709    --  clauses. The second implication is the way we handle discriminants.
5710    --  Specifically, in the untagged case we need a way to communicate to Gigi
5711    --  what are the real discriminants in the record, while for the semantics
5712    --  we need to consider those introduced by the user to rename the
5713    --  discriminants in the parent type. This is handled by introducing the
5714    --  notion of stored discriminants. See below for more.
5715
5716    --  Fortunately the way regular components are inherited can be handled in
5717    --  the same way in tagged and untagged types.
5718
5719    --  To complicate things a bit more the private view of a private extension
5720    --  cannot be handled in the same way as the full view (for one thing the
5721    --  semantic rules are somewhat different). We will explain what differs
5722    --  below.
5723
5724    --  2. DISCRIMINANTS UNDER INHERITANCE
5725
5726    --  The semantic rules governing the discriminants of derived types are
5727    --  quite subtle.
5728
5729    --   type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
5730    --      [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
5731
5732    --  If parent type has discriminants, then the discriminants that are
5733    --  declared in the derived type are [3.4 (11)]:
5734
5735    --  o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
5736    --    there is one;
5737
5738    --  o Otherwise, each discriminant of the parent type (implicitly declared
5739    --    in the same order with the same specifications). In this case, the
5740    --    discriminants are said to be "inherited", or if unknown in the parent
5741    --    are also unknown in the derived type.
5742
5743    --  Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
5744
5745    --  o The parent subtype shall be constrained;
5746
5747    --  o If the parent type is not a tagged type, then each discriminant of
5748    --    the derived type shall be used in the constraint defining a parent
5749    --    subtype. [Implementation note: This ensures that the new discriminant
5750    --    can share storage with an existing discriminant.]
5751
5752    --  For the derived type each discriminant of the parent type is either
5753    --  inherited, constrained to equal some new discriminant of the derived
5754    --  type, or constrained to the value of an expression.
5755
5756    --  When inherited or constrained to equal some new discriminant, the
5757    --  parent discriminant and the discriminant of the derived type are said
5758    --  to "correspond".
5759
5760    --  If a discriminant of the parent type is constrained to a specific value
5761    --  in the derived type definition, then the discriminant is said to be
5762    --  "specified" by that derived type definition.
5763
5764    --  3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
5765
5766    --  We have spoken about stored discriminants in point 1 (introduction)
5767    --  above. There are two sort of stored discriminants: implicit and
5768    --  explicit. As long as the derived type inherits the same discriminants as
5769    --  the root record type, stored discriminants are the same as regular
5770    --  discriminants, and are said to be implicit. However, if any discriminant
5771    --  in the root type was renamed in the derived type, then the derived
5772    --  type will contain explicit stored discriminants. Explicit stored
5773    --  discriminants are discriminants in addition to the semantically visible
5774    --  discriminants defined for the derived type. Stored discriminants are
5775    --  used by Gigi to figure out what are the physical discriminants in
5776    --  objects of the derived type (see precise definition in einfo.ads).
5777    --  As an example, consider the following:
5778
5779    --           type R  (D1, D2, D3 : Int) is record ... end record;
5780    --           type T1 is new R;
5781    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
5782    --           type T3 is new T2;
5783    --           type T4 (Y : Int) is new T3 (Y, 99);
5784
5785    --  The following table summarizes the discriminants and stored
5786    --  discriminants in R and T1 through T4.
5787
5788    --   Type      Discrim     Stored Discrim  Comment
5789    --    R      (D1, D2, D3)   (D1, D2, D3)   Girder discrims implicit in R
5790    --    T1     (D1, D2, D3)   (D1, D2, D3)   Girder discrims implicit in T1
5791    --    T2     (X1, X2)       (D1, D2, D3)   Girder discrims EXPLICIT in T2
5792    --    T3     (X1, X2)       (D1, D2, D3)   Girder discrims EXPLICIT in T3
5793    --    T4     (Y)            (D1, D2, D3)   Girder discrims EXPLICIT in T4
5794
5795    --  Field Corresponding_Discriminant (abbreviated CD below) allows us to
5796    --  find the corresponding discriminant in the parent type, while
5797    --  Original_Record_Component (abbreviated ORC below), the actual physical
5798    --  component that is renamed. Finally the field Is_Completely_Hidden
5799    --  (abbreviated ICH below) is set for all explicit stored discriminants
5800    --  (see einfo.ads for more info). For the above example this gives:
5801
5802    --                 Discrim     CD        ORC     ICH
5803    --                 ^^^^^^^     ^^        ^^^     ^^^
5804    --                 D1 in R    empty     itself    no
5805    --                 D2 in R    empty     itself    no
5806    --                 D3 in R    empty     itself    no
5807
5808    --                 D1 in T1  D1 in R    itself    no
5809    --                 D2 in T1  D2 in R    itself    no
5810    --                 D3 in T1  D3 in R    itself    no
5811
5812    --                 X1 in T2  D3 in T1  D3 in T2   no
5813    --                 X2 in T2  D1 in T1  D1 in T2   no
5814    --                 D1 in T2   empty    itself    yes
5815    --                 D2 in T2   empty    itself    yes
5816    --                 D3 in T2   empty    itself    yes
5817
5818    --                 X1 in T3  X1 in T2  D3 in T3   no
5819    --                 X2 in T3  X2 in T2  D1 in T3   no
5820    --                 D1 in T3   empty    itself    yes
5821    --                 D2 in T3   empty    itself    yes
5822    --                 D3 in T3   empty    itself    yes
5823
5824    --                 Y  in T4  X1 in T3  D3 in T3   no
5825    --                 D1 in T3   empty    itself    yes
5826    --                 D2 in T3   empty    itself    yes
5827    --                 D3 in T3   empty    itself    yes
5828
5829    --  4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
5830
5831    --  Type derivation for tagged types is fairly straightforward. If no
5832    --  discriminants are specified by the derived type, these are inherited
5833    --  from the parent. No explicit stored discriminants are ever necessary.
5834    --  The only manipulation that is done to the tree is that of adding a
5835    --  _parent field with parent type and constrained to the same constraint
5836    --  specified for the parent in the derived type definition. For instance:
5837
5838    --           type R  (D1, D2, D3 : Int) is tagged record ... end record;
5839    --           type T1 is new R with null record;
5840    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
5841
5842    --  are changed into:
5843
5844    --           type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
5845    --              _parent : R (D1, D2, D3);
5846    --           end record;
5847
5848    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
5849    --              _parent : T1 (X2, 88, X1);
5850    --           end record;
5851
5852    --  The discriminants actually present in R, T1 and T2 as well as their CD,
5853    --  ORC and ICH fields are:
5854
5855    --                 Discrim     CD        ORC     ICH
5856    --                 ^^^^^^^     ^^        ^^^     ^^^
5857    --                 D1 in R    empty     itself    no
5858    --                 D2 in R    empty     itself    no
5859    --                 D3 in R    empty     itself    no
5860
5861    --                 D1 in T1  D1 in R    D1 in R   no
5862    --                 D2 in T1  D2 in R    D2 in R   no
5863    --                 D3 in T1  D3 in R    D3 in R   no
5864
5865    --                 X1 in T2  D3 in T1   D3 in R   no
5866    --                 X2 in T2  D1 in T1   D1 in R   no
5867
5868    --  5. FIRST TRANSFORMATION FOR DERIVED RECORDS
5869    --
5870    --  Regardless of whether we dealing with a tagged or untagged type
5871    --  we will transform all derived type declarations of the form
5872    --
5873    --               type T is new R (...) [with ...];
5874    --  or
5875    --               subtype S is R (...);
5876    --               type T is new S [with ...];
5877    --  into
5878    --               type BT is new R [with ...];
5879    --               subtype T is BT (...);
5880    --
5881    --  That is, the base derived type is constrained only if it has no
5882    --  discriminants. The reason for doing this is that GNAT's semantic model
5883    --  assumes that a base type with discriminants is unconstrained.
5884    --
5885    --  Note that, strictly speaking, the above transformation is not always
5886    --  correct. Consider for instance the following excerpt from ACVC b34011a:
5887    --
5888    --       procedure B34011A is
5889    --          type REC (D : integer := 0) is record
5890    --             I : Integer;
5891    --          end record;
5892
5893    --          package P is
5894    --             type T6 is new Rec;
5895    --             function F return T6;
5896    --          end P;
5897
5898    --          use P;
5899    --          package Q6 is
5900    --             type U is new T6 (Q6.F.I);                   -- ERROR: Q6.F.
5901    --          end Q6;
5902    --
5903    --  The definition of Q6.U is illegal. However transforming Q6.U into
5904
5905    --             type BaseU is new T6;
5906    --             subtype U is BaseU (Q6.F.I)
5907
5908    --  turns U into a legal subtype, which is incorrect. To avoid this problem
5909    --  we always analyze the constraint (in this case (Q6.F.I)) before applying
5910    --  the transformation described above.
5911
5912    --  There is another instance where the above transformation is incorrect.
5913    --  Consider:
5914
5915    --          package Pack is
5916    --             type Base (D : Integer) is tagged null record;
5917    --             procedure P (X : Base);
5918
5919    --             type Der is new Base (2) with null record;
5920    --             procedure P (X : Der);
5921    --          end Pack;
5922
5923    --  Then the above transformation turns this into
5924
5925    --             type Der_Base is new Base with null record;
5926    --             --  procedure P (X : Base) is implicitly inherited here
5927    --             --  as procedure P (X : Der_Base).
5928
5929    --             subtype Der is Der_Base (2);
5930    --             procedure P (X : Der);
5931    --             --  The overriding of P (X : Der_Base) is illegal since we
5932    --             --  have a parameter conformance problem.
5933
5934    --  To get around this problem, after having semantically processed Der_Base
5935    --  and the rewritten subtype declaration for Der, we copy Der_Base field
5936    --  Discriminant_Constraint from Der so that when parameter conformance is
5937    --  checked when P is overridden, no semantic errors are flagged.
5938
5939    --  6. SECOND TRANSFORMATION FOR DERIVED RECORDS
5940
5941    --  Regardless of whether we are dealing with a tagged or untagged type
5942    --  we will transform all derived type declarations of the form
5943
5944    --               type R (D1, .., Dn : ...) is [tagged] record ...;
5945    --               type T is new R [with ...];
5946    --  into
5947    --               type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
5948
5949    --  The reason for such transformation is that it allows us to implement a
5950    --  very clean form of component inheritance as explained below.
5951
5952    --  Note that this transformation is not achieved by direct tree rewriting
5953    --  and manipulation, but rather by redoing the semantic actions that the
5954    --  above transformation will entail. This is done directly in routine
5955    --  Inherit_Components.
5956
5957    --  7. TYPE DERIVATION AND COMPONENT INHERITANCE
5958
5959    --  In both tagged and untagged derived types, regular non discriminant
5960    --  components are inherited in the derived type from the parent type. In
5961    --  the absence of discriminants component, inheritance is straightforward
5962    --  as components can simply be copied from the parent.
5963
5964    --  If the parent has discriminants, inheriting components constrained with
5965    --  these discriminants requires caution. Consider the following example:
5966
5967    --      type R  (D1, D2 : Positive) is [tagged] record
5968    --         S : String (D1 .. D2);
5969    --      end record;
5970
5971    --      type T1                is new R        [with null record];
5972    --      type T2 (X : positive) is new R (1, X) [with null record];
5973
5974    --  As explained in 6. above, T1 is rewritten as
5975    --      type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
5976    --  which makes the treatment for T1 and T2 identical.
5977
5978    --  What we want when inheriting S, is that references to D1 and D2 in R are
5979    --  replaced with references to their correct constraints, i.e. D1 and D2 in
5980    --  T1 and 1 and X in T2. So all R's discriminant references are replaced
5981    --  with either discriminant references in the derived type or expressions.
5982    --  This replacement is achieved as follows: before inheriting R's
5983    --  components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
5984    --  created in the scope of T1 (resp. scope of T2) so that discriminants D1
5985    --  and D2 of T1 are visible (resp. discriminant X of T2 is visible).
5986    --  For T2, for instance, this has the effect of replacing String (D1 .. D2)
5987    --  by String (1 .. X).
5988
5989    --  8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
5990
5991    --  We explain here the rules governing private type extensions relevant to
5992    --  type derivation. These rules are explained on the following example:
5993
5994    --      type D [(...)] is new A [(...)] with private;      <-- partial view
5995    --      type D [(...)] is new P [(...)] with null record;  <-- full view
5996
5997    --  Type A is called the ancestor subtype of the private extension.
5998    --  Type P is the parent type of the full view of the private extension. It
5999    --  must be A or a type derived from A.
6000
6001    --  The rules concerning the discriminants of private type extensions are
6002    --  [7.3(10-13)]:
6003
6004    --  o If a private extension inherits known discriminants from the ancestor
6005    --    subtype, then the full view shall also inherit its discriminants from
6006    --    the ancestor subtype and the parent subtype of the full view shall be
6007    --    constrained if and only if the ancestor subtype is constrained.
6008
6009    --  o If a partial view has unknown discriminants, then the full view may
6010    --    define a definite or an indefinite subtype, with or without
6011    --    discriminants.
6012
6013    --  o If a partial view has neither known nor unknown discriminants, then
6014    --    the full view shall define a definite subtype.
6015
6016    --  o If the ancestor subtype of a private extension has constrained
6017    --    discriminants, then the parent subtype of the full view shall impose a
6018    --    statically matching constraint on those discriminants.
6019
6020    --  This means that only the following forms of private extensions are
6021    --  allowed:
6022
6023    --      type D is new A with private;      <-- partial view
6024    --      type D is new P with null record;  <-- full view
6025
6026    --  If A has no discriminants than P has no discriminants, otherwise P must
6027    --  inherit A's discriminants.
6028
6029    --      type D is new A (...) with private;      <-- partial view
6030    --      type D is new P (:::) with null record;  <-- full view
6031
6032    --  P must inherit A's discriminants and (...) and (:::) must statically
6033    --  match.
6034
6035    --      subtype A is R (...);
6036    --      type D is new A with private;      <-- partial view
6037    --      type D is new P with null record;  <-- full view
6038
6039    --  P must have inherited R's discriminants and must be derived from A or
6040    --  any of its subtypes.
6041
6042    --      type D (..) is new A with private;              <-- partial view
6043    --      type D (..) is new P [(:::)] with null record;  <-- full view
6044
6045    --  No specific constraints on P's discriminants or constraint (:::).
6046    --  Note that A can be unconstrained, but the parent subtype P must either
6047    --  be constrained or (:::) must be present.
6048
6049    --      type D (..) is new A [(...)] with private;      <-- partial view
6050    --      type D (..) is new P [(:::)] with null record;  <-- full view
6051
6052    --  P's constraints on A's discriminants must statically match those
6053    --  imposed by (...).
6054
6055    --  9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
6056
6057    --  The full view of a private extension is handled exactly as described
6058    --  above. The model chose for the private view of a private extension is
6059    --  the same for what concerns discriminants (i.e. they receive the same
6060    --  treatment as in the tagged case). However, the private view of the
6061    --  private extension always inherits the components of the parent base,
6062    --  without replacing any discriminant reference. Strictly speaking this is
6063    --  incorrect. However, Gigi never uses this view to generate code so this
6064    --  is a purely semantic issue. In theory, a set of transformations similar
6065    --  to those given in 5. and 6. above could be applied to private views of
6066    --  private extensions to have the same model of component inheritance as
6067    --  for non private extensions. However, this is not done because it would
6068    --  further complicate private type processing. Semantically speaking, this
6069    --  leaves us in an uncomfortable situation. As an example consider:
6070
6071    --          package Pack is
6072    --             type R (D : integer) is tagged record
6073    --                S : String (1 .. D);
6074    --             end record;
6075    --             procedure P (X : R);
6076    --             type T is new R (1) with private;
6077    --          private
6078    --             type T is new R (1) with null record;
6079    --          end;
6080
6081    --  This is transformed into:
6082
6083    --          package Pack is
6084    --             type R (D : integer) is tagged record
6085    --                S : String (1 .. D);
6086    --             end record;
6087    --             procedure P (X : R);
6088    --             type T is new R (1) with private;
6089    --          private
6090    --             type BaseT is new R with null record;
6091    --             subtype  T is BaseT (1);
6092    --          end;
6093
6094    --  (strictly speaking the above is incorrect Ada)
6095
6096    --  From the semantic standpoint the private view of private extension T
6097    --  should be flagged as constrained since one can clearly have
6098    --
6099    --             Obj : T;
6100    --
6101    --  in a unit withing Pack. However, when deriving subprograms for the
6102    --  private view of private extension T, T must be seen as unconstrained
6103    --  since T has discriminants (this is a constraint of the current
6104    --  subprogram derivation model). Thus, when processing the private view of
6105    --  a private extension such as T, we first mark T as unconstrained, we
6106    --  process it, we perform program derivation and just before returning from
6107    --  Build_Derived_Record_Type we mark T as constrained.
6108
6109    --  ??? Are there are other uncomfortable cases that we will have to
6110    --      deal with.
6111
6112    --  10. RECORD_TYPE_WITH_PRIVATE complications
6113
6114    --  Types that are derived from a visible record type and have a private
6115    --  extension present other peculiarities. They behave mostly like private
6116    --  types, but if they have primitive operations defined, these will not
6117    --  have the proper signatures for further inheritance, because other
6118    --  primitive operations will use the implicit base that we define for
6119    --  private derivations below. This affect subprogram inheritance (see
6120    --  Derive_Subprograms for details). We also derive the implicit base from
6121    --  the base type of the full view, so that the implicit base is a record
6122    --  type and not another private type, This avoids infinite loops.
6123
6124    procedure Build_Derived_Record_Type
6125      (N            : Node_Id;
6126       Parent_Type  : Entity_Id;
6127       Derived_Type : Entity_Id;
6128       Derive_Subps : Boolean := True)
6129    is
6130       Loc          : constant Source_Ptr := Sloc (N);
6131       Parent_Base  : Entity_Id;
6132       Type_Def     : Node_Id;
6133       Indic        : Node_Id;
6134       Discrim      : Entity_Id;
6135       Last_Discrim : Entity_Id;
6136       Constrs      : Elist_Id;
6137
6138       Discs : Elist_Id := New_Elmt_List;
6139       --  An empty Discs list means that there were no constraints in the
6140       --  subtype indication or that there was an error processing it.
6141
6142       Assoc_List : Elist_Id;
6143       New_Discrs : Elist_Id;
6144       New_Base   : Entity_Id;
6145       New_Decl   : Node_Id;
6146       New_Indic  : Node_Id;
6147
6148       Is_Tagged          : constant Boolean := Is_Tagged_Type (Parent_Type);
6149       Discriminant_Specs : constant Boolean :=
6150                              Present (Discriminant_Specifications (N));
6151       Private_Extension  : constant Boolean :=
6152                              Nkind (N) = N_Private_Extension_Declaration;
6153
6154       Constraint_Present : Boolean;
6155       Inherit_Discrims   : Boolean := False;
6156       Save_Etype         : Entity_Id;
6157       Save_Discr_Constr  : Elist_Id;
6158       Save_Next_Entity   : Entity_Id;
6159
6160    begin
6161       if Ekind (Parent_Type) = E_Record_Type_With_Private
6162         and then Present (Full_View (Parent_Type))
6163         and then Has_Discriminants (Parent_Type)
6164       then
6165          Parent_Base := Base_Type (Full_View (Parent_Type));
6166       else
6167          Parent_Base := Base_Type (Parent_Type);
6168       end if;
6169
6170       --  Before we start the previously documented transformations, here is
6171       --  little fix for size and alignment of tagged types. Normally when we
6172       --  derive type D from type P, we copy the size and alignment of P as the
6173       --  default for D, and in the absence of explicit representation clauses
6174       --  for D, the size and alignment are indeed the same as the parent.
6175
6176       --  But this is wrong for tagged types, since fields may be added, and
6177       --  the default size may need to be larger, and the default alignment may
6178       --  need to be larger.
6179
6180       --  We therefore reset the size and alignment fields in the tagged case.
6181       --  Note that the size and alignment will in any case be at least as
6182       --  large as the parent type (since the derived type has a copy of the
6183       --  parent type in the _parent field)
6184
6185       --  The type is also marked as being tagged here, which is needed when
6186       --  processing components with a self-referential anonymous access type
6187       --  in the call to Check_Anonymous_Access_Components below. Note that
6188       --  this flag is also set later on for completeness.
6189
6190       if Is_Tagged then
6191          Set_Is_Tagged_Type (Derived_Type);
6192          Init_Size_Align    (Derived_Type);
6193       end if;
6194
6195       --  STEP 0a: figure out what kind of derived type declaration we have
6196
6197       if Private_Extension then
6198          Type_Def := N;
6199          Set_Ekind (Derived_Type, E_Record_Type_With_Private);
6200
6201       else
6202          Type_Def := Type_Definition (N);
6203
6204          --  Ekind (Parent_Base) is not necessarily E_Record_Type since
6205          --  Parent_Base can be a private type or private extension. However,
6206          --  for tagged types with an extension the newly added fields are
6207          --  visible and hence the Derived_Type is always an E_Record_Type.
6208          --  (except that the parent may have its own private fields).
6209          --  For untagged types we preserve the Ekind of the Parent_Base.
6210
6211          if Present (Record_Extension_Part (Type_Def)) then
6212             Set_Ekind (Derived_Type, E_Record_Type);
6213
6214             --  Create internal access types for components with anonymous
6215             --  access types.
6216
6217             if Ada_Version >= Ada_05 then
6218                Check_Anonymous_Access_Components
6219                  (N, Derived_Type, Derived_Type,
6220                    Component_List (Record_Extension_Part (Type_Def)));
6221             end if;
6222
6223          else
6224             Set_Ekind (Derived_Type, Ekind (Parent_Base));
6225          end if;
6226       end if;
6227
6228       --  Indic can either be an N_Identifier if the subtype indication
6229       --  contains no constraint or an N_Subtype_Indication if the subtype
6230       --  indication has a constraint.
6231
6232       Indic := Subtype_Indication (Type_Def);
6233       Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
6234
6235       --  Check that the type has visible discriminants. The type may be
6236       --  a private type with unknown discriminants whose full view has
6237       --  discriminants which are invisible.
6238
6239       if Constraint_Present then
6240          if not Has_Discriminants (Parent_Base)
6241            or else
6242              (Has_Unknown_Discriminants (Parent_Base)
6243                 and then Is_Private_Type (Parent_Base))
6244          then
6245             Error_Msg_N
6246               ("invalid constraint: type has no discriminant",
6247                  Constraint (Indic));
6248
6249             Constraint_Present := False;
6250             Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6251
6252          elsif Is_Constrained (Parent_Type) then
6253             Error_Msg_N
6254                ("invalid constraint: parent type is already constrained",
6255                   Constraint (Indic));
6256
6257             Constraint_Present := False;
6258             Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6259          end if;
6260       end if;
6261
6262       --  STEP 0b: If needed, apply transformation given in point 5. above
6263
6264       if not Private_Extension
6265         and then Has_Discriminants (Parent_Type)
6266         and then not Discriminant_Specs
6267         and then (Is_Constrained (Parent_Type) or else Constraint_Present)
6268       then
6269          --  First, we must analyze the constraint (see comment in point 5.)
6270
6271          if Constraint_Present then
6272             New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
6273
6274             if Has_Discriminants (Derived_Type)
6275               and then Has_Private_Declaration (Derived_Type)
6276               and then Present (Discriminant_Constraint (Derived_Type))
6277             then
6278                --  Verify that constraints of the full view statically match
6279                --  those given in the partial view.
6280
6281                declare
6282                   C1, C2 : Elmt_Id;
6283
6284                begin
6285                   C1 := First_Elmt (New_Discrs);
6286                   C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
6287                   while Present (C1) and then Present (C2) loop
6288
6289                      if Fully_Conformant_Expressions (Node (C1), Node (C2))
6290                        or else
6291                      (Is_OK_Static_Expression (Node (C1))
6292                         and then
6293                       Is_OK_Static_Expression (Node (C2))
6294                         and then
6295                       Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
6296                      then
6297                         null;
6298
6299                      else
6300                         Error_Msg_N (
6301                           "constraint not conformant to previous declaration",
6302                              Node (C1));
6303                      end if;
6304
6305                      Next_Elmt (C1);
6306                      Next_Elmt (C2);
6307                   end loop;
6308                end;
6309             end if;
6310          end if;
6311
6312          --  Insert and analyze the declaration for the unconstrained base type
6313
6314          New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
6315
6316          New_Decl :=
6317            Make_Full_Type_Declaration (Loc,
6318               Defining_Identifier => New_Base,
6319               Type_Definition     =>
6320                 Make_Derived_Type_Definition (Loc,
6321                   Abstract_Present      => Abstract_Present (Type_Def),
6322                   Subtype_Indication    =>
6323                     New_Occurrence_Of (Parent_Base, Loc),
6324                   Record_Extension_Part =>
6325                     Relocate_Node (Record_Extension_Part (Type_Def))));
6326
6327          Set_Parent (New_Decl, Parent (N));
6328          Mark_Rewrite_Insertion (New_Decl);
6329          Insert_Before (N, New_Decl);
6330
6331          --  Note that this call passes False for the Derive_Subps parameter
6332          --  because subprogram derivation is deferred until after creating
6333          --  the subtype (see below).
6334
6335          Build_Derived_Type
6336            (New_Decl, Parent_Base, New_Base,
6337             Is_Completion => True, Derive_Subps => False);
6338
6339          --  ??? This needs re-examination to determine whether the
6340          --  above call can simply be replaced by a call to Analyze.
6341
6342          Set_Analyzed (New_Decl);
6343
6344          --  Insert and analyze the declaration for the constrained subtype
6345
6346          if Constraint_Present then
6347             New_Indic :=
6348               Make_Subtype_Indication (Loc,
6349                 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6350                 Constraint   => Relocate_Node (Constraint (Indic)));
6351
6352          else
6353             declare
6354                Constr_List : constant List_Id := New_List;
6355                C           : Elmt_Id;
6356                Expr        : Node_Id;
6357
6358             begin
6359                C := First_Elmt (Discriminant_Constraint (Parent_Type));
6360                while Present (C) loop
6361                   Expr := Node (C);
6362
6363                   --  It is safe here to call New_Copy_Tree since
6364                   --  Force_Evaluation was called on each constraint in
6365                   --  Build_Discriminant_Constraints.
6366
6367                   Append (New_Copy_Tree (Expr), To => Constr_List);
6368
6369                   Next_Elmt (C);
6370                end loop;
6371
6372                New_Indic :=
6373                  Make_Subtype_Indication (Loc,
6374                    Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6375                    Constraint   =>
6376                      Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
6377             end;
6378          end if;
6379
6380          Rewrite (N,
6381            Make_Subtype_Declaration (Loc,
6382              Defining_Identifier => Derived_Type,
6383              Subtype_Indication  => New_Indic));
6384
6385          Analyze (N);
6386
6387          --  Derivation of subprograms must be delayed until the full subtype
6388          --  has been established to ensure proper overriding of subprograms
6389          --  inherited by full types. If the derivations occurred as part of
6390          --  the call to Build_Derived_Type above, then the check for type
6391          --  conformance would fail because earlier primitive subprograms
6392          --  could still refer to the full type prior the change to the new
6393          --  subtype and hence would not match the new base type created here.
6394
6395          Derive_Subprograms (Parent_Type, Derived_Type);
6396
6397          --  For tagged types the Discriminant_Constraint of the new base itype
6398          --  is inherited from the first subtype so that no subtype conformance
6399          --  problem arise when the first subtype overrides primitive
6400          --  operations inherited by the implicit base type.
6401
6402          if Is_Tagged then
6403             Set_Discriminant_Constraint
6404               (New_Base, Discriminant_Constraint (Derived_Type));
6405          end if;
6406
6407          return;
6408       end if;
6409
6410       --  If we get here Derived_Type will have no discriminants or it will be
6411       --  a discriminated unconstrained base type.
6412
6413       --  STEP 1a: perform preliminary actions/checks for derived tagged types
6414
6415       if Is_Tagged then
6416
6417          --  The parent type is frozen for non-private extensions (RM 13.14(7))
6418          --  The declaration of a specific descendant of an interface type
6419          --  freezes the interface type (RM 13.14).
6420
6421          if not Private_Extension
6422            or else Is_Interface (Parent_Base)
6423          then
6424             Freeze_Before (N, Parent_Type);
6425          end if;
6426
6427          --  In Ada 2005 (AI-344), the restriction that a derived tagged type
6428          --  cannot be declared at a deeper level than its parent type is
6429          --  removed. The check on derivation within a generic body is also
6430          --  relaxed, but there's a restriction that a derived tagged type
6431          --  cannot be declared in a generic body if it's derived directly
6432          --  or indirectly from a formal type of that generic.
6433
6434          if Ada_Version >= Ada_05 then
6435             if Present (Enclosing_Generic_Body (Derived_Type)) then
6436                declare
6437                   Ancestor_Type : Entity_Id;
6438
6439                begin
6440                   --  Check to see if any ancestor of the derived type is a
6441                   --  formal type.
6442
6443                   Ancestor_Type := Parent_Type;
6444                   while not Is_Generic_Type (Ancestor_Type)
6445                     and then Etype (Ancestor_Type) /= Ancestor_Type
6446                   loop
6447                      Ancestor_Type := Etype (Ancestor_Type);
6448                   end loop;
6449
6450                   --  If the derived type does have a formal type as an
6451                   --  ancestor, then it's an error if the derived type is
6452                   --  declared within the body of the generic unit that
6453                   --  declares the formal type in its generic formal part. It's
6454                   --  sufficient to check whether the ancestor type is declared
6455                   --  inside the same generic body as the derived type (such as
6456                   --  within a nested generic spec), in which case the
6457                   --  derivation is legal. If the formal type is declared
6458                   --  outside of that generic body, then it's guaranteed that
6459                   --  the derived type is declared within the generic body of
6460                   --  the generic unit declaring the formal type.
6461
6462                   if Is_Generic_Type (Ancestor_Type)
6463                     and then Enclosing_Generic_Body (Ancestor_Type) /=
6464                                Enclosing_Generic_Body (Derived_Type)
6465                   then
6466                      Error_Msg_NE
6467                        ("parent type of& must not be descendant of formal type"
6468                           & " of an enclosing generic body",
6469                             Indic, Derived_Type);
6470                   end if;
6471                end;
6472             end if;
6473
6474          elsif Type_Access_Level (Derived_Type) /=
6475                  Type_Access_Level (Parent_Type)
6476            and then not Is_Generic_Type (Derived_Type)
6477          then
6478             if Is_Controlled (Parent_Type) then
6479                Error_Msg_N
6480                  ("controlled type must be declared at the library level",
6481                   Indic);
6482             else
6483                Error_Msg_N
6484                  ("type extension at deeper accessibility level than parent",
6485                   Indic);
6486             end if;
6487
6488          else
6489             declare
6490                GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
6491
6492             begin
6493                if Present (GB)
6494                  and then GB /= Enclosing_Generic_Body (Parent_Base)
6495                then
6496                   Error_Msg_NE
6497                     ("parent type of& must not be outside generic body"
6498                        & " (RM 3.9.1(4))",
6499                          Indic, Derived_Type);
6500                end if;
6501             end;
6502          end if;
6503       end if;
6504
6505       --  Ada 2005 (AI-251)
6506
6507       if Ada_Version = Ada_05
6508         and then Is_Tagged
6509       then
6510          --  "The declaration of a specific descendant of an interface type
6511          --  freezes the interface type" (RM 13.14).
6512
6513          declare
6514             Iface : Node_Id;
6515          begin
6516             if Is_Non_Empty_List (Interface_List (Type_Def)) then
6517                Iface := First (Interface_List (Type_Def));
6518                while Present (Iface) loop
6519                   Freeze_Before (N, Etype (Iface));
6520                   Next (Iface);
6521                end loop;
6522             end if;
6523          end;
6524       end if;
6525
6526       --  STEP 1b : preliminary cleanup of the full view of private types
6527
6528       --  If the type is already marked as having discriminants, then it's the
6529       --  completion of a private type or private extension and we need to
6530       --  retain the discriminants from the partial view if the current
6531       --  declaration has Discriminant_Specifications so that we can verify
6532       --  conformance. However, we must remove any existing components that
6533       --  were inherited from the parent (and attached in Copy_And_Swap)
6534       --  because the full type inherits all appropriate components anyway, and
6535       --  we do not want the partial view's components interfering.
6536
6537       if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
6538          Discrim := First_Discriminant (Derived_Type);
6539          loop
6540             Last_Discrim := Discrim;
6541             Next_Discriminant (Discrim);
6542             exit when No (Discrim);
6543          end loop;
6544
6545          Set_Last_Entity (Derived_Type, Last_Discrim);
6546
6547       --  In all other cases wipe out the list of inherited components (even
6548       --  inherited discriminants), it will be properly rebuilt here.
6549
6550       else
6551          Set_First_Entity (Derived_Type, Empty);
6552          Set_Last_Entity  (Derived_Type, Empty);
6553       end if;
6554
6555       --  STEP 1c: Initialize some flags for the Derived_Type
6556
6557       --  The following flags must be initialized here so that
6558       --  Process_Discriminants can check that discriminants of tagged types do
6559       --  not have a default initial value and that access discriminants are
6560       --  only specified for limited records. For completeness, these flags are
6561       --  also initialized along with all the other flags below.
6562
6563       --  AI-419: Limitedness is not inherited from an interface parent, so to
6564       --  be limited in that case the type must be explicitly declared as
6565       --  limited. However, task and protected interfaces are always limited.
6566
6567       if Limited_Present (Type_Def) then
6568          Set_Is_Limited_Record (Derived_Type);
6569
6570       elsif Is_Limited_Record (Parent_Type)
6571         or else (Present (Full_View (Parent_Type))
6572                    and then Is_Limited_Record (Full_View (Parent_Type)))
6573       then
6574          if not Is_Interface (Parent_Type)
6575            or else Is_Synchronized_Interface (Parent_Type)
6576            or else Is_Protected_Interface (Parent_Type)
6577            or else Is_Task_Interface (Parent_Type)
6578          then
6579             Set_Is_Limited_Record (Derived_Type);
6580          end if;
6581       end if;
6582
6583       --  STEP 2a: process discriminants of derived type if any
6584
6585       Push_Scope (Derived_Type);
6586
6587       if Discriminant_Specs then
6588          Set_Has_Unknown_Discriminants (Derived_Type, False);
6589
6590          --  The following call initializes fields Has_Discriminants and
6591          --  Discriminant_Constraint, unless we are processing the completion
6592          --  of a private type declaration.
6593
6594          Check_Or_Process_Discriminants (N, Derived_Type);
6595
6596          --  For non-tagged types the constraint on the Parent_Type must be
6597          --  present and is used to rename the discriminants.
6598
6599          if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
6600             Error_Msg_N ("untagged parent must have discriminants", Indic);
6601
6602          elsif not Is_Tagged and then not Constraint_Present then
6603             Error_Msg_N
6604               ("discriminant constraint needed for derived untagged records",
6605                Indic);
6606
6607          --  Otherwise the parent subtype must be constrained unless we have a
6608          --  private extension.
6609
6610          elsif not Constraint_Present
6611            and then not Private_Extension
6612            and then not Is_Constrained (Parent_Type)
6613          then
6614             Error_Msg_N
6615               ("unconstrained type not allowed in this context", Indic);
6616
6617          elsif Constraint_Present then
6618             --  The following call sets the field Corresponding_Discriminant
6619             --  for the discriminants in the Derived_Type.
6620
6621             Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
6622
6623             --  For untagged types all new discriminants must rename
6624             --  discriminants in the parent. For private extensions new
6625             --  discriminants cannot rename old ones (implied by [7.3(13)]).
6626
6627             Discrim := First_Discriminant (Derived_Type);
6628             while Present (Discrim) loop
6629                if not Is_Tagged
6630                  and then No (Corresponding_Discriminant (Discrim))
6631                then
6632                   Error_Msg_N
6633                     ("new discriminants must constrain old ones", Discrim);
6634
6635                elsif Private_Extension
6636                  and then Present (Corresponding_Discriminant (Discrim))
6637                then
6638                   Error_Msg_N
6639                     ("only static constraints allowed for parent"
6640                      & " discriminants in the partial view", Indic);
6641                   exit;
6642                end if;
6643
6644                --  If a new discriminant is used in the constraint, then its
6645                --  subtype must be statically compatible with the parent
6646                --  discriminant's subtype (3.7(15)).
6647
6648                if Present (Corresponding_Discriminant (Discrim))
6649                  and then
6650                    not Subtypes_Statically_Compatible
6651                          (Etype (Discrim),
6652                           Etype (Corresponding_Discriminant (Discrim)))
6653                then
6654                   Error_Msg_N
6655                     ("subtype must be compatible with parent discriminant",
6656                      Discrim);
6657                end if;
6658
6659                Next_Discriminant (Discrim);
6660             end loop;
6661
6662             --  Check whether the constraints of the full view statically
6663             --  match those imposed by the parent subtype [7.3(13)].
6664
6665             if Present (Stored_Constraint (Derived_Type)) then
6666                declare
6667                   C1, C2 : Elmt_Id;
6668
6669                begin
6670                   C1 := First_Elmt (Discs);
6671                   C2 := First_Elmt (Stored_Constraint (Derived_Type));
6672                   while Present (C1) and then Present (C2) loop
6673                      if not
6674                        Fully_Conformant_Expressions (Node (C1), Node (C2))
6675                      then
6676                         Error_Msg_N
6677                           ("not conformant with previous declaration",
6678                            Node (C1));
6679                      end if;
6680
6681                      Next_Elmt (C1);
6682                      Next_Elmt (C2);
6683                   end loop;
6684                end;
6685             end if;
6686          end if;
6687
6688       --  STEP 2b: No new discriminants, inherit discriminants if any
6689
6690       else
6691          if Private_Extension then
6692             Set_Has_Unknown_Discriminants
6693               (Derived_Type,
6694                Has_Unknown_Discriminants (Parent_Type)
6695                  or else Unknown_Discriminants_Present (N));
6696
6697          --  The partial view of the parent may have unknown discriminants,
6698          --  but if the full view has discriminants and the parent type is
6699          --  in scope they must be inherited.
6700
6701          elsif Has_Unknown_Discriminants (Parent_Type)
6702            and then
6703             (not Has_Discriminants (Parent_Type)
6704               or else not In_Open_Scopes (Scope (Parent_Type)))
6705          then
6706             Set_Has_Unknown_Discriminants (Derived_Type);
6707          end if;
6708
6709          if not Has_Unknown_Discriminants (Derived_Type)
6710            and then not Has_Unknown_Discriminants (Parent_Base)
6711            and then Has_Discriminants (Parent_Type)
6712          then
6713             Inherit_Discrims := True;
6714             Set_Has_Discriminants
6715               (Derived_Type, True);
6716             Set_Discriminant_Constraint
6717               (Derived_Type, Discriminant_Constraint (Parent_Base));
6718          end if;
6719
6720          --  The following test is true for private types (remember
6721          --  transformation 5. is not applied to those) and in an error
6722          --  situation.
6723
6724          if Constraint_Present then
6725             Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
6726          end if;
6727
6728          --  For now mark a new derived type as constrained only if it has no
6729          --  discriminants. At the end of Build_Derived_Record_Type we properly
6730          --  set this flag in the case of private extensions. See comments in
6731          --  point 9. just before body of Build_Derived_Record_Type.
6732
6733          Set_Is_Constrained
6734            (Derived_Type,
6735             not (Inherit_Discrims
6736                    or else Has_Unknown_Discriminants (Derived_Type)));
6737       end if;
6738
6739       --  STEP 3: initialize fields of derived type
6740
6741       Set_Is_Tagged_Type    (Derived_Type, Is_Tagged);
6742       Set_Stored_Constraint (Derived_Type, No_Elist);
6743
6744       --  Ada 2005 (AI-251): Private type-declarations can implement interfaces
6745       --  but cannot be interfaces
6746
6747       if not Private_Extension
6748          and then Ekind (Derived_Type) /= E_Private_Type
6749          and then Ekind (Derived_Type) /= E_Limited_Private_Type
6750       then
6751          if Interface_Present (Type_Def) then
6752             Analyze_Interface_Declaration (Derived_Type, Type_Def);
6753          end if;
6754
6755          Set_Interfaces (Derived_Type, No_Elist);
6756       end if;
6757
6758       --  Fields inherited from the Parent_Type
6759
6760       Set_Discard_Names
6761         (Derived_Type, Einfo.Discard_Names      (Parent_Type));
6762       Set_Has_Specified_Layout
6763         (Derived_Type, Has_Specified_Layout     (Parent_Type));
6764       Set_Is_Limited_Composite
6765         (Derived_Type, Is_Limited_Composite     (Parent_Type));
6766       Set_Is_Private_Composite
6767         (Derived_Type, Is_Private_Composite     (Parent_Type));
6768
6769       --  Fields inherited from the Parent_Base
6770
6771       Set_Has_Controlled_Component
6772         (Derived_Type, Has_Controlled_Component (Parent_Base));
6773       Set_Has_Non_Standard_Rep
6774         (Derived_Type, Has_Non_Standard_Rep     (Parent_Base));
6775       Set_Has_Primitive_Operations
6776         (Derived_Type, Has_Primitive_Operations (Parent_Base));
6777
6778       --  Fields inherited from the Parent_Base in the non-private case
6779
6780       if Ekind (Derived_Type) = E_Record_Type then
6781          Set_Has_Complex_Representation
6782            (Derived_Type, Has_Complex_Representation (Parent_Base));
6783       end if;
6784
6785       --  Fields inherited from the Parent_Base for record types
6786
6787       if Is_Record_Type (Derived_Type) then
6788          Set_OK_To_Reorder_Components
6789            (Derived_Type, OK_To_Reorder_Components (Parent_Base));
6790          Set_Reverse_Bit_Order
6791            (Derived_Type, Reverse_Bit_Order (Parent_Base));
6792       end if;
6793
6794       --  Direct controlled types do not inherit Finalize_Storage_Only flag
6795
6796       if not Is_Controlled (Parent_Type) then
6797          Set_Finalize_Storage_Only
6798            (Derived_Type, Finalize_Storage_Only (Parent_Type));
6799       end if;
6800
6801       --  Set fields for private derived types
6802
6803       if Is_Private_Type (Derived_Type) then
6804          Set_Depends_On_Private (Derived_Type, True);
6805          Set_Private_Dependents (Derived_Type, New_Elmt_List);
6806
6807       --  Inherit fields from non private record types. If this is the
6808       --  completion of a derivation from a private type, the parent itself
6809       --  is private, and the attributes come from its full view, which must
6810       --  be present.
6811
6812       else
6813          if Is_Private_Type (Parent_Base)
6814            and then not Is_Record_Type (Parent_Base)
6815          then
6816             Set_Component_Alignment
6817               (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
6818             Set_C_Pass_By_Copy
6819               (Derived_Type, C_Pass_By_Copy      (Full_View (Parent_Base)));
6820          else
6821             Set_Component_Alignment
6822               (Derived_Type, Component_Alignment (Parent_Base));
6823
6824             Set_C_Pass_By_Copy
6825               (Derived_Type, C_Pass_By_Copy      (Parent_Base));
6826          end if;
6827       end if;
6828
6829       --  Set fields for tagged types
6830
6831       if Is_Tagged then
6832          Set_Primitive_Operations (Derived_Type, New_Elmt_List);
6833
6834          --  All tagged types defined in Ada.Finalization are controlled
6835
6836          if Chars (Scope (Derived_Type)) = Name_Finalization
6837            and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
6838            and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
6839          then
6840             Set_Is_Controlled (Derived_Type);
6841          else
6842             Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
6843          end if;
6844
6845          Make_Class_Wide_Type (Derived_Type);
6846          Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
6847
6848          if Has_Discriminants (Derived_Type)
6849            and then Constraint_Present
6850          then
6851             Set_Stored_Constraint
6852               (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
6853          end if;
6854
6855          if Ada_Version >= Ada_05 then
6856             declare
6857                Ifaces_List : Elist_Id;
6858
6859             begin
6860                --  Checks rules 3.9.4 (13/2 and 14/2)
6861
6862                if Comes_From_Source (Derived_Type)
6863                  and then not Is_Private_Type (Derived_Type)
6864                  and then Is_Interface (Parent_Type)
6865                  and then not Is_Interface (Derived_Type)
6866                then
6867                   if Is_Task_Interface (Parent_Type) then
6868                      Error_Msg_N
6869                        ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
6870                         Derived_Type);
6871
6872                   elsif Is_Protected_Interface (Parent_Type) then
6873                      Error_Msg_N
6874                        ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
6875                         Derived_Type);
6876                   end if;
6877                end if;
6878
6879                --  Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
6880
6881                Check_Interfaces (N, Type_Def);
6882
6883                --  Ada 2005 (AI-251): Collect the list of progenitors that are
6884                --  not already in the parents.
6885
6886                Collect_Interfaces
6887                  (T               => Derived_Type,
6888                   Ifaces_List     => Ifaces_List,
6889                   Exclude_Parents => True);
6890
6891                Set_Interfaces (Derived_Type, Ifaces_List);
6892             end;
6893          end if;
6894
6895       else
6896          Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
6897          Set_Has_Non_Standard_Rep
6898                        (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
6899       end if;
6900
6901       --  STEP 4: Inherit components from the parent base and constrain them.
6902       --          Apply the second transformation described in point 6. above.
6903
6904       if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
6905         or else not Has_Discriminants (Parent_Type)
6906         or else not Is_Constrained (Parent_Type)
6907       then
6908          Constrs := Discs;
6909       else
6910          Constrs := Discriminant_Constraint (Parent_Type);
6911       end if;
6912
6913       Assoc_List :=
6914         Inherit_Components
6915           (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
6916
6917       --  STEP 5a: Copy the parent record declaration for untagged types
6918
6919       if not Is_Tagged then
6920
6921          --  Discriminant_Constraint (Derived_Type) has been properly
6922          --  constructed. Save it and temporarily set it to Empty because we
6923          --  do not want the call to New_Copy_Tree below to mess this list.
6924
6925          if Has_Discriminants (Derived_Type) then
6926             Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
6927             Set_Discriminant_Constraint (Derived_Type, No_Elist);
6928          else
6929             Save_Discr_Constr := No_Elist;
6930          end if;
6931
6932          --  Save the Etype field of Derived_Type. It is correctly set now,
6933          --  but the call to New_Copy tree may remap it to point to itself,
6934          --  which is not what we want. Ditto for the Next_Entity field.
6935
6936          Save_Etype       := Etype (Derived_Type);
6937          Save_Next_Entity := Next_Entity (Derived_Type);
6938
6939          --  Assoc_List maps all stored discriminants in the Parent_Base to
6940          --  stored discriminants in the Derived_Type. It is fundamental that
6941          --  no types or itypes with discriminants other than the stored
6942          --  discriminants appear in the entities declared inside
6943          --  Derived_Type, since the back end cannot deal with it.
6944
6945          New_Decl :=
6946            New_Copy_Tree
6947              (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
6948
6949          --  Restore the fields saved prior to the New_Copy_Tree call
6950          --  and compute the stored constraint.
6951
6952          Set_Etype       (Derived_Type, Save_Etype);
6953          Set_Next_Entity (Derived_Type, Save_Next_Entity);
6954
6955          if Has_Discriminants (Derived_Type) then
6956             Set_Discriminant_Constraint
6957               (Derived_Type, Save_Discr_Constr);
6958             Set_Stored_Constraint
6959               (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
6960             Replace_Components (Derived_Type, New_Decl);
6961          end if;
6962
6963          --  Insert the new derived type declaration
6964
6965          Rewrite (N, New_Decl);
6966
6967       --  STEP 5b: Complete the processing for record extensions in generics
6968
6969       --  There is no completion for record extensions declared in the
6970       --  parameter part of a generic, so we need to complete processing for
6971       --  these generic record extensions here. The Record_Type_Definition call
6972       --  will change the Ekind of the components from E_Void to E_Component.
6973
6974       elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
6975          Record_Type_Definition (Empty, Derived_Type);
6976
6977       --  STEP 5c: Process the record extension for non private tagged types
6978
6979       elsif not Private_Extension then
6980
6981          --  Add the _parent field in the derived type
6982
6983          Expand_Record_Extension (Derived_Type, Type_Def);
6984
6985          --  Ada 2005 (AI-251): Addition of the Tag corresponding to all the
6986          --  implemented interfaces if we are in expansion mode
6987
6988          if Expander_Active
6989            and then Has_Interfaces (Derived_Type)
6990          then
6991             Add_Interface_Tag_Components (N, Derived_Type);
6992          end if;
6993
6994          --  Analyze the record extension
6995
6996          Record_Type_Definition
6997            (Record_Extension_Part (Type_Def), Derived_Type);
6998       end if;
6999
7000       End_Scope;
7001
7002       --  Nothing else to do if there is an error in the derivation.
7003       --  An unusual case: the full view may be derived from a type in an
7004       --  instance, when the partial view was used illegally as an actual
7005       --  in that instance, leading to a circular definition.
7006
7007       if Etype (Derived_Type) = Any_Type
7008         or else Etype (Parent_Type) = Derived_Type
7009       then
7010          return;
7011       end if;
7012
7013       --  Set delayed freeze and then derive subprograms, we need to do
7014       --  this in this order so that derived subprograms inherit the
7015       --  derived freeze if necessary.
7016
7017       Set_Has_Delayed_Freeze (Derived_Type);
7018
7019       if Derive_Subps then
7020          Derive_Subprograms (Parent_Type, Derived_Type);
7021       end if;
7022
7023       --  If we have a private extension which defines a constrained derived
7024       --  type mark as constrained here after we have derived subprograms. See
7025       --  comment on point 9. just above the body of Build_Derived_Record_Type.
7026
7027       if Private_Extension and then Inherit_Discrims then
7028          if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
7029             Set_Is_Constrained          (Derived_Type, True);
7030             Set_Discriminant_Constraint (Derived_Type, Discs);
7031
7032          elsif Is_Constrained (Parent_Type) then
7033             Set_Is_Constrained
7034               (Derived_Type, True);
7035             Set_Discriminant_Constraint
7036               (Derived_Type, Discriminant_Constraint (Parent_Type));
7037          end if;
7038       end if;
7039
7040       --  Update the class_wide type, which shares the now-completed
7041       --  entity list with its specific type.
7042
7043       if Is_Tagged then
7044          Set_First_Entity
7045            (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
7046          Set_Last_Entity
7047            (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
7048       end if;
7049
7050       --  Update the scope of anonymous access types of discriminants and other
7051       --  components, to prevent scope anomalies in gigi, when the derivation
7052       --  appears in a scope nested within that of the parent.
7053
7054       declare
7055          D : Entity_Id;
7056
7057       begin
7058          D := First_Entity (Derived_Type);
7059          while Present (D) loop
7060             if Ekind (D) = E_Discriminant
7061               or else Ekind (D) = E_Component
7062             then
7063                if Is_Itype (Etype (D))
7064                   and then Ekind (Etype (D)) = E_Anonymous_Access_Type
7065                then
7066                   Set_Scope (Etype (D), Current_Scope);
7067                end if;
7068             end if;
7069
7070             Next_Entity (D);
7071          end loop;
7072       end;
7073    end Build_Derived_Record_Type;
7074
7075    ------------------------
7076    -- Build_Derived_Type --
7077    ------------------------
7078
7079    procedure Build_Derived_Type
7080      (N             : Node_Id;
7081       Parent_Type   : Entity_Id;
7082       Derived_Type  : Entity_Id;
7083       Is_Completion : Boolean;
7084       Derive_Subps  : Boolean := True)
7085    is
7086       Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7087
7088    begin
7089       --  Set common attributes
7090
7091       Set_Scope         (Derived_Type, Current_Scope);
7092
7093       Set_Ekind         (Derived_Type, Ekind    (Parent_Base));
7094       Set_Etype         (Derived_Type,           Parent_Base);
7095       Set_Has_Task      (Derived_Type, Has_Task (Parent_Base));
7096
7097       Set_Size_Info     (Derived_Type,                Parent_Type);
7098       Set_RM_Size       (Derived_Type, RM_Size       (Parent_Type));
7099       Set_Convention    (Derived_Type, Convention    (Parent_Type));
7100       Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7101
7102       --  The derived type inherits the representation clauses of the parent.
7103       --  However, for a private type that is completed by a derivation, there
7104       --  may be operation attributes that have been specified already (stream
7105       --  attributes and External_Tag) and those must be provided. Finally,
7106       --  if the partial view is a private extension, the representation items
7107       --  of the parent have been inherited already, and should not be chained
7108       --  twice to the derived type.
7109
7110       if Is_Tagged_Type (Parent_Type)
7111         and then Present (First_Rep_Item (Derived_Type))
7112       then
7113          --  The existing items are either operational items or items inherited
7114          --  from a private extension declaration.
7115
7116          declare
7117             Rep : Node_Id;
7118             --  Used to iterate over representation items of the derived type
7119
7120             Last_Rep : Node_Id;
7121             --  Last representation item of the (non-empty) representation
7122             --  item list of the derived type.
7123
7124             Found : Boolean := False;
7125
7126          begin
7127             Rep      := First_Rep_Item (Derived_Type);
7128             Last_Rep := Rep;
7129             while Present (Rep) loop
7130                if Rep = First_Rep_Item (Parent_Type) then
7131                   Found := True;
7132                   exit;
7133
7134                else
7135                   Rep := Next_Rep_Item (Rep);
7136
7137                   if Present (Rep) then
7138                      Last_Rep := Rep;
7139                   end if;
7140                end if;
7141             end loop;
7142
7143             --  Here if we either encountered the parent type's first rep
7144             --  item on the derived type's rep item list (in which case
7145             --  Found is True, and we have nothing else to do), or if we
7146             --  reached the last rep item of the derived type, which is
7147             --  Last_Rep, in which case we further chain the parent type's
7148             --  rep items to those of the derived type.
7149
7150             if not Found then
7151                Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
7152             end if;
7153          end;
7154
7155       else
7156          Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
7157       end if;
7158
7159       case Ekind (Parent_Type) is
7160          when Numeric_Kind =>
7161             Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
7162
7163          when Array_Kind =>
7164             Build_Derived_Array_Type (N, Parent_Type,  Derived_Type);
7165
7166          when E_Record_Type
7167             | E_Record_Subtype
7168             | Class_Wide_Kind  =>
7169             Build_Derived_Record_Type
7170               (N, Parent_Type, Derived_Type, Derive_Subps);
7171             return;
7172
7173          when Enumeration_Kind =>
7174             Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
7175
7176          when Access_Kind =>
7177             Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
7178
7179          when Incomplete_Or_Private_Kind =>
7180             Build_Derived_Private_Type
7181               (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
7182
7183             --  For discriminated types, the derivation includes deriving
7184             --  primitive operations. For others it is done below.
7185
7186             if Is_Tagged_Type (Parent_Type)
7187               or else Has_Discriminants (Parent_Type)
7188               or else (Present (Full_View (Parent_Type))
7189                         and then Has_Discriminants (Full_View (Parent_Type)))
7190             then
7191                return;
7192             end if;
7193
7194          when Concurrent_Kind =>
7195             Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
7196
7197          when others =>
7198             raise Program_Error;
7199       end case;
7200
7201       if Etype (Derived_Type) = Any_Type then
7202          return;
7203       end if;
7204
7205       --  Set delayed freeze and then derive subprograms, we need to do this
7206       --  in this order so that derived subprograms inherit the derived freeze
7207       --  if necessary.
7208
7209       Set_Has_Delayed_Freeze (Derived_Type);
7210       if Derive_Subps then
7211          Derive_Subprograms (Parent_Type, Derived_Type);
7212       end if;
7213
7214       Set_Has_Primitive_Operations
7215         (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
7216    end Build_Derived_Type;
7217
7218    -----------------------
7219    -- Build_Discriminal --
7220    -----------------------
7221
7222    procedure Build_Discriminal (Discrim : Entity_Id) is
7223       D_Minal : Entity_Id;
7224       CR_Disc : Entity_Id;
7225
7226    begin
7227       --  A discriminal has the same name as the discriminant
7228
7229       D_Minal :=
7230         Make_Defining_Identifier (Sloc (Discrim),
7231           Chars => Chars (Discrim));
7232
7233       Set_Ekind     (D_Minal, E_In_Parameter);
7234       Set_Mechanism (D_Minal, Default_Mechanism);
7235       Set_Etype     (D_Minal, Etype (Discrim));
7236
7237       Set_Discriminal (Discrim, D_Minal);
7238       Set_Discriminal_Link (D_Minal, Discrim);
7239
7240       --  For task types, build at once the discriminants of the corresponding
7241       --  record, which are needed if discriminants are used in entry defaults
7242       --  and in family bounds.
7243
7244       if Is_Concurrent_Type (Current_Scope)
7245         or else Is_Limited_Type (Current_Scope)
7246       then
7247          CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
7248
7249          Set_Ekind            (CR_Disc, E_In_Parameter);
7250          Set_Mechanism        (CR_Disc, Default_Mechanism);
7251          Set_Etype            (CR_Disc, Etype (Discrim));
7252          Set_Discriminal_Link (CR_Disc, Discrim);
7253          Set_CR_Discriminant  (Discrim, CR_Disc);
7254       end if;
7255    end Build_Discriminal;
7256
7257    ------------------------------------
7258    -- Build_Discriminant_Constraints --
7259    ------------------------------------
7260
7261    function Build_Discriminant_Constraints
7262      (T           : Entity_Id;
7263       Def         : Node_Id;
7264       Derived_Def : Boolean := False) return Elist_Id
7265    is
7266       C        : constant Node_Id := Constraint (Def);
7267       Nb_Discr : constant Nat     := Number_Discriminants (T);
7268
7269       Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
7270       --  Saves the expression corresponding to a given discriminant in T
7271
7272       function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
7273       --  Return the Position number within array Discr_Expr of a discriminant
7274       --  D within the discriminant list of the discriminated type T.
7275
7276       ------------------
7277       -- Pos_Of_Discr --
7278       ------------------
7279
7280       function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
7281          Disc : Entity_Id;
7282
7283       begin
7284          Disc := First_Discriminant (T);
7285          for J in Discr_Expr'Range loop
7286             if Disc = D then
7287                return J;
7288             end if;
7289
7290             Next_Discriminant (Disc);
7291          end loop;
7292
7293          --  Note: Since this function is called on discriminants that are
7294          --  known to belong to the discriminated type, falling through the
7295          --  loop with no match signals an internal compiler error.
7296
7297          raise Program_Error;
7298       end Pos_Of_Discr;
7299
7300       --  Declarations local to Build_Discriminant_Constraints
7301
7302       Discr : Entity_Id;
7303       E     : Entity_Id;
7304       Elist : constant Elist_Id := New_Elmt_List;
7305
7306       Constr   : Node_Id;
7307       Expr     : Node_Id;
7308       Id       : Node_Id;
7309       Position : Nat;
7310       Found    : Boolean;
7311
7312       Discrim_Present : Boolean := False;
7313
7314    --  Start of processing for Build_Discriminant_Constraints
7315
7316    begin
7317       --  The following loop will process positional associations only.
7318       --  For a positional association, the (single) discriminant is
7319       --  implicitly specified by position, in textual order (RM 3.7.2).
7320
7321       Discr  := First_Discriminant (T);
7322       Constr := First (Constraints (C));
7323       for D in Discr_Expr'Range loop
7324          exit when Nkind (Constr) = N_Discriminant_Association;
7325
7326          if No (Constr) then
7327             Error_Msg_N ("too few discriminants given in constraint", C);
7328             return New_Elmt_List;
7329
7330          elsif Nkind (Constr) = N_Range
7331            or else (Nkind (Constr) = N_Attribute_Reference
7332                      and then
7333                     Attribute_Name (Constr) = Name_Range)
7334          then
7335             Error_Msg_N
7336               ("a range is not a valid discriminant constraint", Constr);
7337             Discr_Expr (D) := Error;
7338
7339          else
7340             Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
7341             Discr_Expr (D) := Constr;
7342          end if;
7343
7344          Next_Discriminant (Discr);
7345          Next (Constr);
7346       end loop;
7347
7348       if No (Discr) and then Present (Constr) then
7349          Error_Msg_N ("too many discriminants given in constraint", Constr);
7350          return New_Elmt_List;
7351       end if;
7352
7353       --  Named associations can be given in any order, but if both positional
7354       --  and named associations are used in the same discriminant constraint,
7355       --  then positional associations must occur first, at their normal
7356       --  position. Hence once a named association is used, the rest of the
7357       --  discriminant constraint must use only named associations.
7358
7359       while Present (Constr) loop
7360
7361          --  Positional association forbidden after a named association
7362
7363          if Nkind (Constr) /= N_Discriminant_Association then
7364             Error_Msg_N ("positional association follows named one", Constr);
7365             return New_Elmt_List;
7366
7367          --  Otherwise it is a named association
7368
7369          else
7370             --  E records the type of the discriminants in the named
7371             --  association. All the discriminants specified in the same name
7372             --  association must have the same type.
7373
7374             E := Empty;
7375
7376             --  Search the list of discriminants in T to see if the simple name
7377             --  given in the constraint matches any of them.
7378
7379             Id := First (Selector_Names (Constr));
7380             while Present (Id) loop
7381                Found := False;
7382
7383                --  If Original_Discriminant is present, we are processing a
7384                --  generic instantiation and this is an instance node. We need
7385                --  to find the name of the corresponding discriminant in the
7386                --  actual record type T and not the name of the discriminant in
7387                --  the generic formal. Example:
7388
7389                --    generic
7390                --       type G (D : int) is private;
7391                --    package P is
7392                --       subtype W is G (D => 1);
7393                --    end package;
7394                --    type Rec (X : int) is record ... end record;
7395                --    package Q is new P (G => Rec);
7396
7397                --  At the point of the instantiation, formal type G is Rec
7398                --  and therefore when reanalyzing "subtype W is G (D => 1);"
7399                --  which really looks like "subtype W is Rec (D => 1);" at
7400                --  the point of instantiation, we want to find the discriminant
7401                --  that corresponds to D in Rec, i.e. X.
7402
7403                if Present (Original_Discriminant (Id)) then
7404                   Discr := Find_Corresponding_Discriminant (Id, T);
7405                   Found := True;
7406
7407                else
7408                   Discr := First_Discriminant (T);
7409                   while Present (Discr) loop
7410                      if Chars (Discr) = Chars (Id) then
7411                         Found := True;
7412                         exit;
7413                      end if;
7414
7415                      Next_Discriminant (Discr);
7416                   end loop;
7417
7418                   if not Found then
7419                      Error_Msg_N ("& does not match any discriminant", Id);
7420                      return New_Elmt_List;
7421
7422                   --  The following is only useful for the benefit of generic
7423                   --  instances but it does not interfere with other
7424                   --  processing for the non-generic case so we do it in all
7425                   --  cases (for generics this statement is executed when
7426                   --  processing the generic definition, see comment at the
7427                   --  beginning of this if statement).
7428
7429                   else
7430                      Set_Original_Discriminant (Id, Discr);
7431                   end if;
7432                end if;
7433
7434                Position := Pos_Of_Discr (T, Discr);
7435
7436                if Present (Discr_Expr (Position)) then
7437                   Error_Msg_N ("duplicate constraint for discriminant&", Id);
7438
7439                else
7440                   --  Each discriminant specified in the same named association
7441                   --  must be associated with a separate copy of the
7442                   --  corresponding expression.
7443
7444                   if Present (Next (Id)) then
7445                      Expr := New_Copy_Tree (Expression (Constr));
7446                      Set_Parent (Expr, Parent (Expression (Constr)));
7447                   else
7448                      Expr := Expression (Constr);
7449                   end if;
7450
7451                   Discr_Expr (Position) := Expr;
7452                   Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
7453                end if;
7454
7455                --  A discriminant association with more than one discriminant
7456                --  name is only allowed if the named discriminants are all of
7457                --  the same type (RM 3.7.1(8)).
7458
7459                if E = Empty then
7460                   E := Base_Type (Etype (Discr));
7461
7462                elsif Base_Type (Etype (Discr)) /= E then
7463                   Error_Msg_N
7464                     ("all discriminants in an association " &
7465                      "must have the same type", Id);
7466                end if;
7467
7468                Next (Id);
7469             end loop;
7470          end if;
7471
7472          Next (Constr);
7473       end loop;
7474
7475       --  A discriminant constraint must provide exactly one value for each
7476       --  discriminant of the type (RM 3.7.1(8)).
7477
7478       for J in Discr_Expr'Range loop
7479          if No (Discr_Expr (J)) then
7480             Error_Msg_N ("too few discriminants given in constraint", C);
7481             return New_Elmt_List;
7482          end if;
7483       end loop;
7484
7485       --  Determine if there are discriminant expressions in the constraint
7486
7487       for J in Discr_Expr'Range loop
7488          if Denotes_Discriminant
7489               (Discr_Expr (J), Check_Concurrent => True)
7490          then
7491             Discrim_Present := True;
7492          end if;
7493       end loop;
7494
7495       --  Build an element list consisting of the expressions given in the
7496       --  discriminant constraint and apply the appropriate checks. The list
7497       --  is constructed after resolving any named discriminant associations
7498       --  and therefore the expressions appear in the textual order of the
7499       --  discriminants.
7500
7501       Discr := First_Discriminant (T);
7502       for J in Discr_Expr'Range loop
7503          if Discr_Expr (J) /= Error then
7504             Append_Elmt (Discr_Expr (J), Elist);
7505
7506             --  If any of the discriminant constraints is given by a
7507             --  discriminant and we are in a derived type declaration we
7508             --  have a discriminant renaming. Establish link between new
7509             --  and old discriminant.
7510
7511             if Denotes_Discriminant (Discr_Expr (J)) then
7512                if Derived_Def then
7513                   Set_Corresponding_Discriminant
7514                     (Entity (Discr_Expr (J)), Discr);
7515                end if;
7516
7517             --  Force the evaluation of non-discriminant expressions.
7518             --  If we have found a discriminant in the constraint 3.4(26)
7519             --  and 3.8(18) demand that no range checks are performed are
7520             --  after evaluation. If the constraint is for a component
7521             --  definition that has a per-object constraint, expressions are
7522             --  evaluated but not checked either. In all other cases perform
7523             --  a range check.
7524
7525             else
7526                if Discrim_Present then
7527                   null;
7528
7529                elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
7530                  and then
7531                    Has_Per_Object_Constraint
7532                      (Defining_Identifier (Parent (Parent (Def))))
7533                then
7534                   null;
7535
7536                elsif Is_Access_Type (Etype (Discr)) then
7537                   Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
7538
7539                else
7540                   Apply_Range_Check (Discr_Expr (J), Etype (Discr));
7541                end if;
7542
7543                Force_Evaluation (Discr_Expr (J));
7544             end if;
7545
7546             --  Check that the designated type of an access discriminant's
7547             --  expression is not a class-wide type unless the discriminant's
7548             --  designated type is also class-wide.
7549
7550             if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
7551               and then not Is_Class_Wide_Type
7552                          (Designated_Type (Etype (Discr)))
7553               and then Etype (Discr_Expr (J)) /= Any_Type
7554               and then Is_Class_Wide_Type
7555                          (Designated_Type (Etype (Discr_Expr (J))))
7556             then
7557                Wrong_Type (Discr_Expr (J), Etype (Discr));
7558             end if;
7559          end if;
7560
7561          Next_Discriminant (Discr);
7562       end loop;
7563
7564       return Elist;
7565    end Build_Discriminant_Constraints;
7566
7567    ---------------------------------
7568    -- Build_Discriminated_Subtype --
7569    ---------------------------------
7570
7571    procedure Build_Discriminated_Subtype
7572      (T           : Entity_Id;
7573       Def_Id      : Entity_Id;
7574       Elist       : Elist_Id;
7575       Related_Nod : Node_Id;
7576       For_Access  : Boolean := False)
7577    is
7578       Has_Discrs  : constant Boolean := Has_Discriminants (T);
7579       Constrained : constant Boolean :=
7580                       (Has_Discrs
7581                          and then not Is_Empty_Elmt_List (Elist)
7582                          and then not Is_Class_Wide_Type (T))
7583                         or else Is_Constrained (T);
7584
7585    begin
7586       if Ekind (T) = E_Record_Type then
7587          if For_Access then
7588             Set_Ekind (Def_Id, E_Private_Subtype);
7589             Set_Is_For_Access_Subtype (Def_Id, True);
7590          else
7591             Set_Ekind (Def_Id, E_Record_Subtype);
7592          end if;
7593
7594          --  Inherit preelaboration flag from base, for types for which it
7595          --  may have been set: records, private types, protected types.
7596
7597          Set_Known_To_Have_Preelab_Init
7598            (Def_Id, Known_To_Have_Preelab_Init (T));
7599
7600       elsif Ekind (T) = E_Task_Type then
7601          Set_Ekind (Def_Id, E_Task_Subtype);
7602
7603       elsif Ekind (T) = E_Protected_Type then
7604          Set_Ekind (Def_Id, E_Protected_Subtype);
7605          Set_Known_To_Have_Preelab_Init
7606            (Def_Id, Known_To_Have_Preelab_Init (T));
7607
7608       elsif Is_Private_Type (T) then
7609          Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
7610          Set_Known_To_Have_Preelab_Init
7611            (Def_Id, Known_To_Have_Preelab_Init (T));
7612
7613       elsif Is_Class_Wide_Type (T) then
7614          Set_Ekind (Def_Id, E_Class_Wide_Subtype);
7615
7616       else
7617          --  Incomplete type. Attach subtype to list of dependents, to be
7618          --  completed with full view of parent type,  unless is it the
7619          --  designated subtype of a record component within an init_proc.
7620          --  This last case arises for a component of an access type whose
7621          --  designated type is incomplete (e.g. a Taft Amendment type).
7622          --  The designated subtype is within an inner scope, and needs no
7623          --  elaboration, because only the access type is needed in the
7624          --  initialization procedure.
7625
7626          Set_Ekind (Def_Id, Ekind (T));
7627
7628          if For_Access and then Within_Init_Proc then
7629             null;
7630          else
7631             Append_Elmt (Def_Id, Private_Dependents (T));
7632          end if;
7633       end if;
7634
7635       Set_Etype             (Def_Id, T);
7636       Init_Size_Align       (Def_Id);
7637       Set_Has_Discriminants (Def_Id, Has_Discrs);
7638       Set_Is_Constrained    (Def_Id, Constrained);
7639
7640       Set_First_Entity      (Def_Id, First_Entity   (T));
7641       Set_Last_Entity       (Def_Id, Last_Entity    (T));
7642
7643       --  If the subtype is the completion of a private declaration, there may
7644       --  have been representation clauses for the partial view, and they must
7645       --  be preserved. Build_Derived_Type chains the inherited clauses with
7646       --  the ones appearing on the extension. If this comes from a subtype
7647       --  declaration, all clauses are inherited.
7648
7649       if No (First_Rep_Item (Def_Id)) then
7650          Set_First_Rep_Item    (Def_Id, First_Rep_Item (T));
7651       end if;
7652
7653       if Is_Tagged_Type (T) then
7654          Set_Is_Tagged_Type  (Def_Id);
7655          Make_Class_Wide_Type (Def_Id);
7656       end if;
7657
7658       Set_Stored_Constraint (Def_Id, No_Elist);
7659
7660       if Has_Discrs then
7661          Set_Discriminant_Constraint (Def_Id, Elist);
7662          Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
7663       end if;
7664
7665       if Is_Tagged_Type (T) then
7666
7667          --  Ada 2005 (AI-251): In case of concurrent types we inherit the
7668          --  concurrent record type (which has the list of primitive
7669          --  operations).
7670
7671          if Ada_Version >= Ada_05
7672            and then Is_Concurrent_Type (T)
7673          then
7674             Set_Corresponding_Record_Type (Def_Id,
7675                Corresponding_Record_Type (T));
7676          else
7677             Set_Primitive_Operations (Def_Id, Primitive_Operations (T));
7678          end if;
7679
7680          Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
7681       end if;
7682
7683       --  Subtypes introduced by component declarations do not need to be
7684       --  marked as delayed, and do not get freeze nodes, because the semantics
7685       --  verifies that the parents of the subtypes are frozen before the
7686       --  enclosing record is frozen.
7687
7688       if not Is_Type (Scope (Def_Id)) then
7689          Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
7690
7691          if Is_Private_Type (T)
7692            and then Present (Full_View (T))
7693          then
7694             Conditional_Delay (Def_Id, Full_View (T));
7695          else
7696             Conditional_Delay (Def_Id, T);
7697          end if;
7698       end if;
7699
7700       if Is_Record_Type (T) then
7701          Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
7702
7703          if Has_Discrs
7704             and then not Is_Empty_Elmt_List (Elist)
7705             and then not For_Access
7706          then
7707             Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
7708          elsif not For_Access then
7709             Set_Cloned_Subtype (Def_Id, T);
7710          end if;
7711       end if;
7712    end Build_Discriminated_Subtype;
7713
7714    ---------------------------
7715    -- Build_Itype_Reference --
7716    ---------------------------
7717
7718    procedure Build_Itype_Reference
7719      (Ityp : Entity_Id;
7720       Nod  : Node_Id)
7721    is
7722       IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
7723    begin
7724       Set_Itype (IR, Ityp);
7725       Insert_After (Nod, IR);
7726    end Build_Itype_Reference;
7727
7728    ------------------------
7729    -- Build_Scalar_Bound --
7730    ------------------------
7731
7732    function Build_Scalar_Bound
7733      (Bound : Node_Id;
7734       Par_T : Entity_Id;
7735       Der_T : Entity_Id) return Node_Id
7736    is
7737       New_Bound : Entity_Id;
7738
7739    begin
7740       --  Note: not clear why this is needed, how can the original bound
7741       --  be unanalyzed at this point? and if it is, what business do we
7742       --  have messing around with it? and why is the base type of the
7743       --  parent type the right type for the resolution. It probably is
7744       --  not! It is OK for the new bound we are creating, but not for
7745       --  the old one??? Still if it never happens, no problem!
7746
7747       Analyze_And_Resolve (Bound, Base_Type (Par_T));
7748
7749       if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
7750          New_Bound := New_Copy (Bound);
7751          Set_Etype (New_Bound, Der_T);
7752          Set_Analyzed (New_Bound);
7753
7754       elsif Is_Entity_Name (Bound) then
7755          New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
7756
7757       --  The following is almost certainly wrong. What business do we have
7758       --  relocating a node (Bound) that is presumably still attached to
7759       --  the tree elsewhere???
7760
7761       else
7762          New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
7763       end if;
7764
7765       Set_Etype (New_Bound, Der_T);
7766       return New_Bound;
7767    end Build_Scalar_Bound;
7768
7769    --------------------------------
7770    -- Build_Underlying_Full_View --
7771    --------------------------------
7772
7773    procedure Build_Underlying_Full_View
7774      (N   : Node_Id;
7775       Typ : Entity_Id;
7776       Par : Entity_Id)
7777    is
7778       Loc  : constant Source_Ptr := Sloc (N);
7779       Subt : constant Entity_Id :=
7780                Make_Defining_Identifier
7781                  (Loc, New_External_Name (Chars (Typ), 'S'));
7782
7783       Constr : Node_Id;
7784       Indic  : Node_Id;
7785       C      : Node_Id;
7786       Id     : Node_Id;
7787
7788       procedure Set_Discriminant_Name (Id : Node_Id);
7789       --  If the derived type has discriminants, they may rename discriminants
7790       --  of the parent. When building the full view of the parent, we need to
7791       --  recover the names of the original discriminants if the constraint is
7792       --  given by named associations.
7793
7794       ---------------------------
7795       -- Set_Discriminant_Name --
7796       ---------------------------
7797
7798       procedure Set_Discriminant_Name (Id : Node_Id) is
7799          Disc : Entity_Id;
7800
7801       begin
7802          Set_Original_Discriminant (Id, Empty);
7803
7804          if Has_Discriminants (Typ) then
7805             Disc := First_Discriminant (Typ);
7806             while Present (Disc) loop
7807                if Chars (Disc) = Chars (Id)
7808                  and then Present (Corresponding_Discriminant (Disc))
7809                then
7810                   Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
7811                end if;
7812                Next_Discriminant (Disc);
7813             end loop;
7814          end if;
7815       end Set_Discriminant_Name;
7816
7817    --  Start of processing for Build_Underlying_Full_View
7818
7819    begin
7820       if Nkind (N) = N_Full_Type_Declaration then
7821          Constr := Constraint (Subtype_Indication (Type_Definition (N)));
7822
7823       elsif Nkind (N) = N_Subtype_Declaration then
7824          Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
7825
7826       elsif Nkind (N) = N_Component_Declaration then
7827          Constr :=
7828            New_Copy_Tree
7829              (Constraint (Subtype_Indication (Component_Definition (N))));
7830
7831       else
7832          raise Program_Error;
7833       end if;
7834
7835       C := First (Constraints (Constr));
7836       while Present (C) loop
7837          if Nkind (C) = N_Discriminant_Association then
7838             Id := First (Selector_Names (C));
7839             while Present (Id) loop
7840                Set_Discriminant_Name (Id);
7841                Next (Id);
7842             end loop;
7843          end if;
7844
7845          Next (C);
7846       end loop;
7847
7848       Indic :=
7849         Make_Subtype_Declaration (Loc,
7850           Defining_Identifier => Subt,
7851           Subtype_Indication  =>
7852             Make_Subtype_Indication (Loc,
7853               Subtype_Mark => New_Reference_To (Par, Loc),
7854               Constraint   => New_Copy_Tree (Constr)));
7855
7856       --  If this is a component subtype for an outer itype, it is not
7857       --  a list member, so simply set the parent link for analysis: if
7858       --  the enclosing type does not need to be in a declarative list,
7859       --  neither do the components.
7860
7861       if Is_List_Member (N)
7862         and then Nkind (N) /= N_Component_Declaration
7863       then
7864          Insert_Before (N, Indic);
7865       else
7866          Set_Parent (Indic, Parent (N));
7867       end if;
7868
7869       Analyze (Indic);
7870       Set_Underlying_Full_View (Typ, Full_View (Subt));
7871    end Build_Underlying_Full_View;
7872
7873    -------------------------------
7874    -- Check_Abstract_Overriding --
7875    -------------------------------
7876
7877    procedure Check_Abstract_Overriding (T : Entity_Id) is
7878       Alias_Subp : Entity_Id;
7879       Elmt       : Elmt_Id;
7880       Op_List    : Elist_Id;
7881       Subp       : Entity_Id;
7882       Type_Def   : Node_Id;
7883
7884    begin
7885       Op_List := Primitive_Operations (T);
7886
7887       --  Loop to check primitive operations
7888
7889       Elmt := First_Elmt (Op_List);
7890       while Present (Elmt) loop
7891          Subp := Node (Elmt);
7892          Alias_Subp := Alias (Subp);
7893
7894          --  Inherited subprograms are identified by the fact that they do not
7895          --  come from source, and the associated source location is the
7896          --  location of the first subtype of the derived type.
7897
7898          --  Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
7899          --  subprograms that "require overriding".
7900
7901          --  Special exception, do not complain about failure to override the
7902          --  stream routines _Input and _Output, as well as the primitive
7903          --  operations used in dispatching selects since we always provide
7904          --  automatic overridings for these subprograms.
7905
7906          --  Also ignore this rule for convention CIL since .NET libraries
7907          --  do bizarre things with interfaces???
7908
7909          --  The partial view of T may have been a private extension, for
7910          --  which inherited functions dispatching on result are abstract.
7911          --  If the full view is a null extension, there is no need for
7912          --  overriding in Ada2005, but wrappers need to be built for them
7913          --  (see exp_ch3, Build_Controlling_Function_Wrappers).
7914
7915          if Is_Null_Extension (T)
7916            and then Has_Controlling_Result (Subp)
7917            and then Ada_Version >= Ada_05
7918            and then Present (Alias_Subp)
7919            and then not Comes_From_Source (Subp)
7920            and then not Is_Abstract_Subprogram (Alias_Subp)
7921            and then not Is_Access_Type (Etype (Subp))
7922          then
7923             null;
7924
7925          --  Ada 2005 (AI-251): Internal entities of interfaces need no
7926          --  processing because this check is done with the aliased
7927          --  entity
7928
7929          elsif Present (Interface_Alias (Subp)) then
7930             null;
7931
7932          elsif (Is_Abstract_Subprogram (Subp)
7933                  or else Requires_Overriding (Subp)
7934                  or else
7935                    (Has_Controlling_Result (Subp)
7936                      and then Present (Alias_Subp)
7937                      and then not Comes_From_Source (Subp)
7938                      and then Sloc (Subp) = Sloc (First_Subtype (T))))
7939            and then not Is_TSS (Subp, TSS_Stream_Input)
7940            and then not Is_TSS (Subp, TSS_Stream_Output)
7941            and then not Is_Abstract_Type (T)
7942            and then Convention (T) /= Convention_CIL
7943            and then not Is_Predefined_Interface_Primitive (Subp)
7944
7945             --  Ada 2005 (AI-251): Do not consider hidden entities associated
7946             --  with abstract interface types because the check will be done
7947             --  with the aliased entity (otherwise we generate a duplicated
7948             --  error message).
7949
7950            and then not Present (Interface_Alias (Subp))
7951          then
7952             if Present (Alias_Subp) then
7953
7954                --  Only perform the check for a derived subprogram when the
7955                --  type has an explicit record extension. This avoids incorrect
7956                --  flagging of abstract subprograms for the case of a type
7957                --  without an extension that is derived from a formal type
7958                --  with a tagged actual (can occur within a private part).
7959
7960                --  Ada 2005 (AI-391): In the case of an inherited function with
7961                --  a controlling result of the type, the rule does not apply if
7962                --  the type is a null extension (unless the parent function
7963                --  itself is abstract, in which case the function must still be
7964                --  be overridden). The expander will generate an overriding
7965                --  wrapper function calling the parent subprogram (see
7966                --  Exp_Ch3.Make_Controlling_Wrapper_Functions).
7967
7968                Type_Def := Type_Definition (Parent (T));
7969
7970                if Nkind (Type_Def) = N_Derived_Type_Definition
7971                  and then Present (Record_Extension_Part (Type_Def))
7972                  and then
7973                    (Ada_Version < Ada_05
7974                       or else not Is_Null_Extension (T)
7975                       or else Ekind (Subp) = E_Procedure
7976                       or else not Has_Controlling_Result (Subp)
7977                       or else Is_Abstract_Subprogram (Alias_Subp)
7978                       or else Requires_Overriding (Subp)
7979                       or else Is_Access_Type (Etype (Subp)))
7980                then
7981                   --  Avoid reporting error in case of abstract predefined
7982                   --  primitive inherited from interface type because the
7983                   --  body of internally generated predefined primitives
7984                   --  of tagged types are generated later by Freeze_Type
7985
7986                   if Is_Interface (Root_Type (T))
7987                     and then Is_Abstract_Subprogram (Subp)
7988                     and then Is_Predefined_Dispatching_Operation (Subp)
7989                     and then not Comes_From_Source (Ultimate_Alias (Subp))
7990                   then
7991                      null;
7992
7993                   else
7994                      Error_Msg_NE
7995                        ("type must be declared abstract or & overridden",
7996                         T, Subp);
7997
7998                      --  Traverse the whole chain of aliased subprograms to
7999                      --  complete the error notification. This is especially
8000                      --  useful for traceability of the chain of entities when
8001                      --  the subprogram corresponds with an interface
8002                      --  subprogram (which may be defined in another package).
8003
8004                      if Present (Alias_Subp) then
8005                         declare
8006                            E : Entity_Id;
8007
8008                         begin
8009                            E := Subp;
8010                            while Present (Alias (E)) loop
8011                               Error_Msg_Sloc := Sloc (E);
8012                               Error_Msg_NE
8013                                 ("\& has been inherited #", T, Subp);
8014                               E := Alias (E);
8015                            end loop;
8016
8017                            Error_Msg_Sloc := Sloc (E);
8018                            Error_Msg_NE
8019                              ("\& has been inherited from subprogram #",
8020                               T, Subp);
8021                         end;
8022                      end if;
8023                   end if;
8024
8025                --  Ada 2005 (AI-345): Protected or task type implementing
8026                --  abstract interfaces.
8027
8028                elsif Is_Concurrent_Record_Type (T)
8029                  and then Present (Interfaces (T))
8030                then
8031                   --  The controlling formal of Subp must be of mode "out",
8032                   --  "in out" or an access-to-variable to be overridden.
8033
8034                   --  Error message below needs rewording (remember comma
8035                   --  in -gnatj mode) ???
8036
8037                   if Ekind (First_Formal (Subp)) = E_In_Parameter then
8038                      if not Is_Predefined_Dispatching_Operation (Subp) then
8039                         Error_Msg_NE
8040                           ("first formal of & must be of mode `OUT`, " &
8041                            "`IN OUT` or access-to-variable", T, Subp);
8042                         Error_Msg_N
8043                           ("\to be overridden by protected procedure or " &
8044                            "entry (RM 9.4(11.9/2))", T);
8045                      end if;
8046
8047                   --  Some other kind of overriding failure
8048
8049                   else
8050                      Error_Msg_NE
8051                        ("interface subprogram & must be overridden",
8052                         T, Subp);
8053                   end if;
8054                end if;
8055
8056             else
8057                Error_Msg_Node_2 := T;
8058                Error_Msg_N
8059                  ("abstract subprogram& not allowed for type&", Subp);
8060
8061                --  Also post unconditional warning on the type (unconditional
8062                --  so that if there are more than one of these cases, we get
8063                --  them all, and not just the first one).
8064
8065                Error_Msg_Node_2 := Subp;
8066                Error_Msg_N
8067                  ("nonabstract type& has abstract subprogram&!", T);
8068             end if;
8069          end if;
8070
8071          --  Ada 2005 (AI05-0030): Inspect hidden subprograms which provide
8072          --  the mapping between interface and implementing type primitives.
8073          --  If the interface alias is marked as Implemented_By_Entry, the
8074          --  alias must be an entry wrapper.
8075
8076          if Ada_Version >= Ada_05
8077            and then Is_Hidden (Subp)
8078            and then Present (Interface_Alias (Subp))
8079            and then Implemented_By_Entry (Interface_Alias (Subp))
8080            and then Present (Alias_Subp)
8081            and then
8082              (not Is_Primitive_Wrapper (Alias_Subp)
8083                 or else Ekind (Wrapped_Entity (Alias_Subp)) /= E_Entry)
8084          then
8085             declare
8086                Error_Ent : Entity_Id := T;
8087
8088             begin
8089                if Is_Concurrent_Record_Type (Error_Ent) then
8090                   Error_Ent := Corresponding_Concurrent_Type (Error_Ent);
8091                end if;
8092
8093                Error_Msg_Node_2 := Interface_Alias (Subp);
8094                Error_Msg_NE
8095                  ("type & must implement abstract subprogram & with an entry",
8096                   Error_Ent, Error_Ent);
8097             end;
8098          end if;
8099
8100          Next_Elmt (Elmt);
8101       end loop;
8102    end Check_Abstract_Overriding;
8103
8104    ------------------------------------------------
8105    -- Check_Access_Discriminant_Requires_Limited --
8106    ------------------------------------------------
8107
8108    procedure Check_Access_Discriminant_Requires_Limited
8109      (D   : Node_Id;
8110       Loc : Node_Id)
8111    is
8112    begin
8113       --  A discriminant_specification for an access discriminant shall appear
8114       --  only in the declaration for a task or protected type, or for a type
8115       --  with the reserved word 'limited' in its definition or in one of its
8116       --  ancestors. (RM 3.7(10))
8117
8118       if Nkind (Discriminant_Type (D)) = N_Access_Definition
8119         and then not Is_Concurrent_Type (Current_Scope)
8120         and then not Is_Concurrent_Record_Type (Current_Scope)
8121         and then not Is_Limited_Record (Current_Scope)
8122         and then Ekind (Current_Scope) /= E_Limited_Private_Type
8123       then
8124          Error_Msg_N
8125            ("access discriminants allowed only for limited types", Loc);
8126       end if;
8127    end Check_Access_Discriminant_Requires_Limited;
8128
8129    -----------------------------------
8130    -- Check_Aliased_Component_Types --
8131    -----------------------------------
8132
8133    procedure Check_Aliased_Component_Types (T : Entity_Id) is
8134       C : Entity_Id;
8135
8136    begin
8137       --  ??? Also need to check components of record extensions, but not
8138       --  components of protected types (which are always limited).
8139
8140       --  Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
8141       --  types to be unconstrained. This is safe because it is illegal to
8142       --  create access subtypes to such types with explicit discriminant
8143       --  constraints.
8144
8145       if not Is_Limited_Type (T) then
8146          if Ekind (T) = E_Record_Type then
8147             C := First_Component (T);
8148             while Present (C) loop
8149                if Is_Aliased (C)
8150                  and then Has_Discriminants (Etype (C))
8151                  and then not Is_Constrained (Etype (C))
8152                  and then not In_Instance_Body
8153                  and then Ada_Version < Ada_05
8154                then
8155                   Error_Msg_N
8156                     ("aliased component must be constrained (RM 3.6(11))",
8157                       C);
8158                end if;
8159
8160                Next_Component (C);
8161             end loop;
8162
8163          elsif Ekind (T) = E_Array_Type then
8164             if Has_Aliased_Components (T)
8165               and then Has_Discriminants (Component_Type (T))
8166               and then not Is_Constrained (Component_Type (T))
8167               and then not In_Instance_Body
8168               and then Ada_Version < Ada_05
8169             then
8170                Error_Msg_N
8171                  ("aliased component type must be constrained (RM 3.6(11))",
8172                     T);
8173             end if;
8174          end if;
8175       end if;
8176    end Check_Aliased_Component_Types;
8177
8178    ----------------------
8179    -- Check_Completion --
8180    ----------------------
8181
8182    procedure Check_Completion (Body_Id : Node_Id := Empty) is
8183       E : Entity_Id;
8184
8185       procedure Post_Error;
8186       --  Post error message for lack of completion for entity E
8187
8188       ----------------
8189       -- Post_Error --
8190       ----------------
8191
8192       procedure Post_Error is
8193       begin
8194          if not Comes_From_Source (E) then
8195
8196             if Ekind (E) = E_Task_Type
8197               or else Ekind (E) = E_Protected_Type
8198             then
8199                --  It may be an anonymous protected type created for a
8200                --  single variable. Post error on variable, if present.
8201
8202                declare
8203                   Var : Entity_Id;
8204
8205                begin
8206                   Var := First_Entity (Current_Scope);
8207                   while Present (Var) loop
8208                      exit when Etype (Var) = E
8209                        and then Comes_From_Source (Var);
8210
8211                      Next_Entity (Var);
8212                   end loop;
8213
8214                   if Present (Var) then
8215                      E := Var;
8216                   end if;
8217                end;
8218             end if;
8219          end if;
8220
8221          --  If a generated entity has no completion, then either previous
8222          --  semantic errors have disabled the expansion phase, or else we had
8223          --  missing subunits, or else we are compiling without expansion,
8224          --  or else something is very wrong.
8225
8226          if not Comes_From_Source (E) then
8227             pragma Assert
8228               (Serious_Errors_Detected > 0
8229                 or else Configurable_Run_Time_Violations > 0
8230                 or else Subunits_Missing
8231                 or else not Expander_Active);
8232             return;
8233
8234          --  Here for source entity
8235
8236          else
8237             --  Here if no body to post the error message, so we post the error
8238             --  on the declaration that has no completion. This is not really
8239             --  the right place to post it, think about this later ???
8240
8241             if No (Body_Id) then
8242                if Is_Type (E) then
8243                   Error_Msg_NE
8244                     ("missing full declaration for }", Parent (E), E);
8245                else
8246                   Error_Msg_NE
8247                     ("missing body for &", Parent (E), E);
8248                end if;
8249
8250             --  Package body has no completion for a declaration that appears
8251             --  in the corresponding spec. Post error on the body, with a
8252             --  reference to the non-completed declaration.
8253
8254             else
8255                Error_Msg_Sloc := Sloc (E);
8256
8257                if Is_Type (E) then
8258                   Error_Msg_NE
8259                     ("missing full declaration for }!", Body_Id, E);
8260
8261                elsif Is_Overloadable (E)
8262                  and then Current_Entity_In_Scope (E) /= E
8263                then
8264                   --  It may be that the completion is mistyped and appears as
8265                   --  a distinct overloading of the entity.
8266
8267                   declare
8268                      Candidate : constant Entity_Id :=
8269                                    Current_Entity_In_Scope (E);
8270                      Decl      : constant Node_Id :=
8271                                    Unit_Declaration_Node (Candidate);
8272
8273                   begin
8274                      if Is_Overloadable (Candidate)
8275                        and then Ekind (Candidate) = Ekind (E)
8276                        and then Nkind (Decl) = N_Subprogram_Body
8277                        and then Acts_As_Spec (Decl)
8278                      then
8279                         Check_Type_Conformant (Candidate, E);
8280
8281                      else
8282                         Error_Msg_NE ("missing body for & declared#!",
8283                            Body_Id, E);
8284                      end if;
8285                   end;
8286                else
8287                   Error_Msg_NE ("missing body for & declared#!",
8288                      Body_Id, E);
8289                end if;
8290             end if;
8291          end if;
8292       end Post_Error;
8293
8294    --  Start processing for Check_Completion
8295
8296    begin
8297       E := First_Entity (Current_Scope);
8298       while Present (E) loop
8299          if Is_Intrinsic_Subprogram (E) then
8300             null;
8301
8302          --  The following situation requires special handling: a child unit
8303          --  that appears in the context clause of the body of its parent:
8304
8305          --    procedure Parent.Child (...);
8306
8307          --    with Parent.Child;
8308          --    package body Parent is
8309
8310          --  Here Parent.Child appears as a local entity, but should not be
8311          --  flagged as requiring completion, because it is a compilation
8312          --  unit.
8313
8314          --  Ignore missing completion for a subprogram that does not come from
8315          --  source (including the _Call primitive operation of RAS types,
8316          --  which has to have the flag Comes_From_Source for other purposes):
8317          --  we assume that the expander will provide the missing completion.
8318
8319          elsif     Ekind (E) = E_Function
8320            or else Ekind (E) = E_Procedure
8321            or else Ekind (E) = E_Generic_Function
8322            or else Ekind (E) = E_Generic_Procedure
8323          then
8324             if not Has_Completion (E)
8325               and then not (Is_Subprogram (E)
8326                             and then Is_Abstract_Subprogram (E))
8327               and then not (Is_Subprogram (E)
8328                               and then
8329                             (not Comes_From_Source (E)
8330                               or else Chars (E) = Name_uCall))
8331               and then Nkind (Parent (Unit_Declaration_Node (E))) /=
8332                                                        N_Compilation_Unit
8333               and then Chars (E) /= Name_uSize
8334             then
8335                Post_Error;
8336             end if;
8337
8338          elsif Is_Entry (E) then
8339             if not Has_Completion (E) and then
8340               (Ekind (Scope (E)) = E_Protected_Object
8341                 or else Ekind (Scope (E)) = E_Protected_Type)
8342             then
8343                Post_Error;
8344             end if;
8345
8346          elsif Is_Package_Or_Generic_Package (E) then
8347             if Unit_Requires_Body (E) then
8348                if not Has_Completion (E)
8349                  and then Nkind (Parent (Unit_Declaration_Node (E))) /=
8350                                                        N_Compilation_Unit
8351                then
8352                   Post_Error;
8353                end if;
8354
8355             elsif not Is_Child_Unit (E) then
8356                May_Need_Implicit_Body (E);
8357             end if;
8358
8359          elsif Ekind (E) = E_Incomplete_Type
8360            and then No (Underlying_Type (E))
8361          then
8362             Post_Error;
8363
8364          elsif (Ekind (E) = E_Task_Type or else
8365                 Ekind (E) = E_Protected_Type)
8366            and then not Has_Completion (E)
8367          then
8368             Post_Error;
8369
8370          --  A single task declared in the current scope is a constant, verify
8371          --  that the body of its anonymous type is in the same scope. If the
8372          --  task is defined elsewhere, this may be a renaming declaration for
8373          --  which no completion is needed.
8374
8375          elsif Ekind (E) = E_Constant
8376            and then Ekind (Etype (E)) = E_Task_Type
8377            and then not Has_Completion (Etype (E))
8378            and then Scope (Etype (E)) = Current_Scope
8379          then
8380             Post_Error;
8381
8382          elsif Ekind (E) = E_Protected_Object
8383            and then not Has_Completion (Etype (E))
8384          then
8385             Post_Error;
8386
8387          elsif Ekind (E) = E_Record_Type then
8388             if Is_Tagged_Type (E) then
8389                Check_Abstract_Overriding (E);
8390                Check_Conventions (E);
8391             end if;
8392
8393             Check_Aliased_Component_Types (E);
8394
8395          elsif Ekind (E) = E_Array_Type then
8396             Check_Aliased_Component_Types (E);
8397
8398          end if;
8399
8400          Next_Entity (E);
8401       end loop;
8402    end Check_Completion;
8403
8404    ----------------------------
8405    -- Check_Delta_Expression --
8406    ----------------------------
8407
8408    procedure Check_Delta_Expression (E : Node_Id) is
8409    begin
8410       if not (Is_Real_Type (Etype (E))) then
8411          Wrong_Type (E, Any_Real);
8412
8413       elsif not Is_OK_Static_Expression (E) then
8414          Flag_Non_Static_Expr
8415            ("non-static expression used for delta value!", E);
8416
8417       elsif not UR_Is_Positive (Expr_Value_R (E)) then
8418          Error_Msg_N ("delta expression must be positive", E);
8419
8420       else
8421          return;
8422       end if;
8423
8424       --  If any of above errors occurred, then replace the incorrect
8425       --  expression by the real 0.1, which should prevent further errors.
8426
8427       Rewrite (E,
8428         Make_Real_Literal (Sloc (E), Ureal_Tenth));
8429       Analyze_And_Resolve (E, Standard_Float);
8430    end Check_Delta_Expression;
8431
8432    -----------------------------
8433    -- Check_Digits_Expression --
8434    -----------------------------
8435
8436    procedure Check_Digits_Expression (E : Node_Id) is
8437    begin
8438       if not (Is_Integer_Type (Etype (E))) then
8439          Wrong_Type (E, Any_Integer);
8440
8441       elsif not Is_OK_Static_Expression (E) then
8442          Flag_Non_Static_Expr
8443            ("non-static expression used for digits value!", E);
8444
8445       elsif Expr_Value (E) <= 0 then
8446          Error_Msg_N ("digits value must be greater than zero", E);
8447
8448       else
8449          return;
8450       end if;
8451
8452       --  If any of above errors occurred, then replace the incorrect
8453       --  expression by the integer 1, which should prevent further errors.
8454
8455       Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
8456       Analyze_And_Resolve (E, Standard_Integer);
8457
8458    end Check_Digits_Expression;
8459
8460    --------------------------
8461    -- Check_Initialization --
8462    --------------------------
8463
8464    procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
8465    begin
8466       if Is_Limited_Type (T)
8467         and then not In_Instance
8468         and then not In_Inlined_Body
8469       then
8470          if not OK_For_Limited_Init (Exp) then
8471
8472             --  In GNAT mode, this is just a warning, to allow it to be evilly
8473             --  turned off. Otherwise it is a real error.
8474
8475             if GNAT_Mode then
8476                Error_Msg_N
8477                  ("?cannot initialize entities of limited type!", Exp);
8478
8479             elsif Ada_Version < Ada_05 then
8480                Error_Msg_N
8481                  ("cannot initialize entities of limited type", Exp);
8482                Explain_Limited_Type (T, Exp);
8483
8484             else
8485                --  Specialize error message according to kind of illegal
8486                --  initial expression.
8487
8488                if Nkind (Exp) = N_Type_Conversion
8489                  and then Nkind (Expression (Exp)) = N_Function_Call
8490                then
8491                   Error_Msg_N
8492                     ("illegal context for call"
8493                       & " to function with limited result", Exp);
8494
8495                else
8496                   Error_Msg_N
8497                     ("initialization of limited object requires aggregate "
8498                       & "or function call",  Exp);
8499                end if;
8500             end if;
8501          end if;
8502       end if;
8503    end Check_Initialization;
8504
8505    ----------------------
8506    -- Check_Interfaces --
8507    ----------------------
8508
8509    procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
8510       Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
8511
8512       Iface       : Node_Id;
8513       Iface_Def   : Node_Id;
8514       Iface_Typ   : Entity_Id;
8515       Parent_Node : Node_Id;
8516
8517       Is_Task : Boolean := False;
8518       --  Set True if parent type or any progenitor is a task interface
8519
8520       Is_Protected : Boolean := False;
8521       --  Set True if parent type or any progenitor is a protected interface
8522
8523       procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
8524       --  Check that a progenitor is compatible with declaration.
8525       --  Error is posted on Error_Node.
8526
8527       ------------------
8528       -- Check_Ifaces --
8529       ------------------
8530
8531       procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
8532          Iface_Id : constant Entity_Id :=
8533                       Defining_Identifier (Parent (Iface_Def));
8534          Type_Def : Node_Id;
8535
8536       begin
8537          if Nkind (N) = N_Private_Extension_Declaration then
8538             Type_Def := N;
8539          else
8540             Type_Def := Type_Definition (N);
8541          end if;
8542
8543          if Is_Task_Interface (Iface_Id) then
8544             Is_Task := True;
8545
8546          elsif Is_Protected_Interface (Iface_Id) then
8547             Is_Protected := True;
8548          end if;
8549
8550          --  Check that the characteristics of the progenitor are compatible
8551          --  with the explicit qualifier in the declaration.
8552          --  The check only applies to qualifiers that come from source.
8553          --  Limited_Present also appears in the declaration of corresponding
8554          --  records, and the check does not apply to them.
8555
8556          if Limited_Present (Type_Def)
8557            and then not
8558              Is_Concurrent_Record_Type (Defining_Identifier (N))
8559          then
8560             if Is_Limited_Interface (Parent_Type)
8561               and then not Is_Limited_Interface (Iface_Id)
8562             then
8563                Error_Msg_NE
8564                  ("progenitor& must be limited interface",
8565                    Error_Node, Iface_Id);
8566
8567             elsif
8568               (Task_Present (Iface_Def)
8569                 or else Protected_Present (Iface_Def)
8570                 or else Synchronized_Present (Iface_Def))
8571               and then Nkind (N) /= N_Private_Extension_Declaration
8572             then
8573                Error_Msg_NE
8574                  ("progenitor& must be limited interface",
8575                    Error_Node, Iface_Id);
8576             end if;
8577
8578          --  Protected interfaces can only inherit from limited, synchronized
8579          --  or protected interfaces.
8580
8581          elsif Nkind (N) = N_Full_Type_Declaration
8582            and then  Protected_Present (Type_Def)
8583          then
8584             if Limited_Present (Iface_Def)
8585               or else Synchronized_Present (Iface_Def)
8586               or else Protected_Present (Iface_Def)
8587             then
8588                null;
8589
8590             elsif Task_Present (Iface_Def) then
8591                Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
8592                             & " from task interface", Error_Node);
8593
8594             else
8595                Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
8596                             & " from non-limited interface", Error_Node);
8597             end if;
8598
8599          --  Ada 2005 (AI-345): Synchronized interfaces can only inherit from
8600          --  limited and synchronized.
8601
8602          elsif Synchronized_Present (Type_Def) then
8603             if Limited_Present (Iface_Def)
8604               or else Synchronized_Present (Iface_Def)
8605             then
8606                null;
8607
8608             elsif Protected_Present (Iface_Def)
8609               and then Nkind (N) /= N_Private_Extension_Declaration
8610             then
8611                Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8612                             & " from protected interface", Error_Node);
8613
8614             elsif Task_Present (Iface_Def)
8615               and then Nkind (N) /= N_Private_Extension_Declaration
8616             then
8617                Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8618                             & " from task interface", Error_Node);
8619
8620             elsif not Is_Limited_Interface (Iface_Id) then
8621                Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8622                             & " from non-limited interface", Error_Node);
8623             end if;
8624
8625          --  Ada 2005 (AI-345): Task interfaces can only inherit from limited,
8626          --  synchronized or task interfaces.
8627
8628          elsif Nkind (N) = N_Full_Type_Declaration
8629            and then Task_Present (Type_Def)
8630          then
8631             if Limited_Present (Iface_Def)
8632               or else Synchronized_Present (Iface_Def)
8633               or else Task_Present (Iface_Def)
8634             then
8635                null;
8636
8637             elsif Protected_Present (Iface_Def) then
8638                Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
8639                             & " protected interface", Error_Node);
8640
8641             else
8642                Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
8643                             & " non-limited interface", Error_Node);
8644             end if;
8645          end if;
8646       end Check_Ifaces;
8647
8648    --  Start of processing for Check_Interfaces
8649
8650    begin
8651       if Is_Interface (Parent_Type) then
8652          if Is_Task_Interface (Parent_Type) then
8653             Is_Task := True;
8654
8655          elsif Is_Protected_Interface (Parent_Type) then
8656             Is_Protected := True;
8657          end if;
8658       end if;
8659
8660       if Nkind (N) = N_Private_Extension_Declaration then
8661
8662          --  Check that progenitors are compatible with declaration
8663
8664          Iface := First (Interface_List (Def));
8665          while Present (Iface) loop
8666             Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
8667
8668             Parent_Node := Parent (Base_Type (Iface_Typ));
8669             Iface_Def   := Type_Definition (Parent_Node);
8670
8671             if not Is_Interface (Iface_Typ) then
8672                Diagnose_Interface (Iface, Iface_Typ);
8673
8674             else
8675                Check_Ifaces (Iface_Def, Iface);
8676             end if;
8677
8678             Next (Iface);
8679          end loop;
8680
8681          if Is_Task and Is_Protected then
8682             Error_Msg_N
8683               ("type cannot derive from task and protected interface", N);
8684          end if;
8685
8686          return;
8687       end if;
8688
8689       --  Full type declaration of derived type.
8690       --  Check compatibility with parent if it is interface type
8691
8692       if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
8693         and then Is_Interface (Parent_Type)
8694       then
8695          Parent_Node := Parent (Parent_Type);
8696
8697          --  More detailed checks for interface varieties
8698
8699          Check_Ifaces
8700            (Iface_Def  => Type_Definition (Parent_Node),
8701             Error_Node => Subtype_Indication (Type_Definition (N)));
8702       end if;
8703
8704       Iface := First (Interface_List (Def));
8705       while Present (Iface) loop
8706          Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
8707
8708          Parent_Node := Parent (Base_Type (Iface_Typ));
8709          Iface_Def   := Type_Definition (Parent_Node);
8710
8711          if not Is_Interface (Iface_Typ) then
8712             Diagnose_Interface (Iface, Iface_Typ);
8713
8714          else
8715             --  "The declaration of a specific descendant of an interface
8716             --   type freezes the interface type" RM 13.14
8717
8718             Freeze_Before (N, Iface_Typ);
8719             Check_Ifaces (Iface_Def, Error_Node => Iface);
8720          end if;
8721
8722          Next (Iface);
8723       end loop;
8724
8725       if Is_Task and Is_Protected then
8726          Error_Msg_N
8727            ("type cannot derive from task and protected interface", N);
8728       end if;
8729    end Check_Interfaces;
8730
8731    ------------------------------------
8732    -- Check_Or_Process_Discriminants --
8733    ------------------------------------
8734
8735    --  If an incomplete or private type declaration was already given for the
8736    --  type, the discriminants may have already been processed if they were
8737    --  present on the incomplete declaration. In this case a full conformance
8738    --  check is performed otherwise just process them.
8739
8740    procedure Check_Or_Process_Discriminants
8741      (N    : Node_Id;
8742       T    : Entity_Id;
8743       Prev : Entity_Id := Empty)
8744    is
8745    begin
8746       if Has_Discriminants (T) then
8747
8748          --  Make the discriminants visible to component declarations
8749
8750          declare
8751             D    : Entity_Id;
8752             Prev : Entity_Id;
8753
8754          begin
8755             D := First_Discriminant (T);
8756             while Present (D) loop
8757                Prev := Current_Entity (D);
8758                Set_Current_Entity (D);
8759                Set_Is_Immediately_Visible (D);
8760                Set_Homonym (D, Prev);
8761
8762                --  Ada 2005 (AI-230): Access discriminant allowed in
8763                --  non-limited record types.
8764
8765                if Ada_Version < Ada_05 then
8766
8767                   --  This restriction gets applied to the full type here. It
8768                   --  has already been applied earlier to the partial view.
8769
8770                   Check_Access_Discriminant_Requires_Limited (Parent (D), N);
8771                end if;
8772
8773                Next_Discriminant (D);
8774             end loop;
8775          end;
8776
8777       elsif Present (Discriminant_Specifications (N)) then
8778          Process_Discriminants (N, Prev);
8779       end if;
8780    end Check_Or_Process_Discriminants;
8781
8782    ----------------------
8783    -- Check_Real_Bound --
8784    ----------------------
8785
8786    procedure Check_Real_Bound (Bound : Node_Id) is
8787    begin
8788       if not Is_Real_Type (Etype (Bound)) then
8789          Error_Msg_N
8790            ("bound in real type definition must be of real type", Bound);
8791
8792       elsif not Is_OK_Static_Expression (Bound) then
8793          Flag_Non_Static_Expr
8794            ("non-static expression used for real type bound!", Bound);
8795
8796       else
8797          return;
8798       end if;
8799
8800       Rewrite
8801         (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
8802       Analyze (Bound);
8803       Resolve (Bound, Standard_Float);
8804    end Check_Real_Bound;
8805
8806    ------------------------------
8807    -- Complete_Private_Subtype --
8808    ------------------------------
8809
8810    procedure Complete_Private_Subtype
8811      (Priv        : Entity_Id;
8812       Full        : Entity_Id;
8813       Full_Base   : Entity_Id;
8814       Related_Nod : Node_Id)
8815    is
8816       Save_Next_Entity : Entity_Id;
8817       Save_Homonym     : Entity_Id;
8818
8819    begin
8820       --  Set semantic attributes for (implicit) private subtype completion.
8821       --  If the full type has no discriminants, then it is a copy of the full
8822       --  view of the base. Otherwise, it is a subtype of the base with a
8823       --  possible discriminant constraint. Save and restore the original
8824       --  Next_Entity field of full to ensure that the calls to Copy_Node
8825       --  do not corrupt the entity chain.
8826
8827       --  Note that the type of the full view is the same entity as the type of
8828       --  the partial view. In this fashion, the subtype has access to the
8829       --  correct view of the parent.
8830
8831       Save_Next_Entity := Next_Entity (Full);
8832       Save_Homonym     := Homonym (Priv);
8833
8834       case Ekind (Full_Base) is
8835          when E_Record_Type    |
8836               E_Record_Subtype |
8837               Class_Wide_Kind  |
8838               Private_Kind     |
8839               Task_Kind        |
8840               Protected_Kind   =>
8841             Copy_Node (Priv, Full);
8842
8843             Set_Has_Discriminants  (Full, Has_Discriminants (Full_Base));
8844             Set_First_Entity       (Full, First_Entity (Full_Base));
8845             Set_Last_Entity        (Full, Last_Entity (Full_Base));
8846
8847          when others =>
8848             Copy_Node (Full_Base, Full);
8849             Set_Chars          (Full, Chars (Priv));
8850             Conditional_Delay  (Full, Priv);
8851             Set_Sloc           (Full, Sloc (Priv));
8852       end case;
8853
8854       Set_Next_Entity (Full, Save_Next_Entity);
8855       Set_Homonym     (Full, Save_Homonym);
8856       Set_Associated_Node_For_Itype (Full, Related_Nod);
8857
8858       --  Set common attributes for all subtypes
8859
8860       Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
8861
8862       --  The Etype of the full view is inconsistent. Gigi needs to see the
8863       --  structural full view,  which is what the current scheme gives:
8864       --  the Etype of the full view is the etype of the full base. However,
8865       --  if the full base is a derived type, the full view then looks like
8866       --  a subtype of the parent, not a subtype of the full base. If instead
8867       --  we write:
8868
8869       --       Set_Etype (Full, Full_Base);
8870
8871       --  then we get inconsistencies in the front-end (confusion between
8872       --  views). Several outstanding bugs are related to this ???
8873
8874       Set_Is_First_Subtype (Full, False);
8875       Set_Scope            (Full, Scope (Priv));
8876       Set_Size_Info        (Full, Full_Base);
8877       Set_RM_Size          (Full, RM_Size (Full_Base));
8878       Set_Is_Itype         (Full);
8879
8880       --  A subtype of a private-type-without-discriminants, whose full-view
8881       --  has discriminants with default expressions, is not constrained!
8882
8883       if not Has_Discriminants (Priv) then
8884          Set_Is_Constrained (Full, Is_Constrained (Full_Base));
8885
8886          if Has_Discriminants (Full_Base) then
8887             Set_Discriminant_Constraint
8888               (Full, Discriminant_Constraint (Full_Base));
8889
8890             --  The partial view may have been indefinite, the full view
8891             --  might not be.
8892
8893             Set_Has_Unknown_Discriminants
8894               (Full, Has_Unknown_Discriminants (Full_Base));
8895          end if;
8896       end if;
8897
8898       Set_First_Rep_Item     (Full, First_Rep_Item (Full_Base));
8899       Set_Depends_On_Private (Full, Has_Private_Component (Full));
8900
8901       --  Freeze the private subtype entity if its parent is delayed, and not
8902       --  already frozen. We skip this processing if the type is an anonymous
8903       --  subtype of a record component, or is the corresponding record of a
8904       --  protected type, since ???
8905
8906       if not Is_Type (Scope (Full)) then
8907          Set_Has_Delayed_Freeze (Full,
8908            Has_Delayed_Freeze (Full_Base)
8909              and then (not Is_Frozen (Full_Base)));
8910       end if;
8911
8912       Set_Freeze_Node (Full, Empty);
8913       Set_Is_Frozen (Full, False);
8914       Set_Full_View (Priv, Full);
8915
8916       if Has_Discriminants (Full) then
8917          Set_Stored_Constraint_From_Discriminant_Constraint (Full);
8918          Set_Stored_Constraint (Priv, Stored_Constraint (Full));
8919
8920          if Has_Unknown_Discriminants (Full) then
8921             Set_Discriminant_Constraint (Full, No_Elist);
8922          end if;
8923       end if;
8924
8925       if Ekind (Full_Base) = E_Record_Type
8926         and then Has_Discriminants (Full_Base)
8927         and then Has_Discriminants (Priv) -- might not, if errors
8928         and then not Has_Unknown_Discriminants (Priv)
8929         and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
8930       then
8931          Create_Constrained_Components
8932            (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
8933
8934       --  If the full base is itself derived from private, build a congruent
8935       --  subtype of its underlying type, for use by the back end. For a
8936       --  constrained record component, the declaration cannot be placed on
8937       --  the component list, but it must nevertheless be built an analyzed, to
8938       --  supply enough information for Gigi to compute the size of component.
8939
8940       elsif Ekind (Full_Base) in Private_Kind
8941         and then Is_Derived_Type (Full_Base)
8942         and then Has_Discriminants (Full_Base)
8943         and then (Ekind (Current_Scope) /= E_Record_Subtype)
8944       then
8945          if not Is_Itype (Priv)
8946            and then
8947              Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
8948          then
8949             Build_Underlying_Full_View
8950               (Parent (Priv), Full, Etype (Full_Base));
8951
8952          elsif Nkind (Related_Nod) = N_Component_Declaration then
8953             Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
8954          end if;
8955
8956       elsif Is_Record_Type (Full_Base) then
8957
8958          --  Show Full is simply a renaming of Full_Base
8959
8960          Set_Cloned_Subtype (Full, Full_Base);
8961       end if;
8962
8963       --  It is unsafe to share to bounds of a scalar type, because the Itype
8964       --  is elaborated on demand, and if a bound is non-static then different
8965       --  orders of elaboration in different units will lead to different
8966       --  external symbols.
8967
8968       if Is_Scalar_Type (Full_Base) then
8969          Set_Scalar_Range (Full,
8970            Make_Range (Sloc (Related_Nod),
8971              Low_Bound  =>
8972                Duplicate_Subexpr_No_Checks (Type_Low_Bound  (Full_Base)),
8973              High_Bound =>
8974                Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
8975
8976          --  This completion inherits the bounds of the full parent, but if
8977          --  the parent is an unconstrained floating point type, so is the
8978          --  completion.
8979
8980          if Is_Floating_Point_Type (Full_Base) then
8981             Set_Includes_Infinities
8982              (Scalar_Range (Full), Has_Infinities (Full_Base));
8983          end if;
8984       end if;
8985
8986       --  ??? It seems that a lot of fields are missing that should be copied
8987       --  from Full_Base to Full. Here are some that are introduced in a
8988       --  non-disruptive way but a cleanup is necessary.
8989
8990       if Is_Tagged_Type (Full_Base) then
8991          Set_Is_Tagged_Type (Full);
8992          Set_Primitive_Operations (Full, Primitive_Operations (Full_Base));
8993          Set_Class_Wide_Type      (Full, Class_Wide_Type (Full_Base));
8994
8995       --  If this is a subtype of a protected or task type, constrain its
8996       --  corresponding record, unless this is a subtype without constraints,
8997       --  i.e. a simple renaming as with an actual subtype in an instance.
8998
8999       elsif Is_Concurrent_Type (Full_Base) then
9000          if Has_Discriminants (Full)
9001            and then Present (Corresponding_Record_Type (Full_Base))
9002            and then
9003              not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
9004          then
9005             Set_Corresponding_Record_Type (Full,
9006               Constrain_Corresponding_Record
9007                 (Full, Corresponding_Record_Type (Full_Base),
9008                   Related_Nod, Full_Base));
9009
9010          else
9011             Set_Corresponding_Record_Type (Full,
9012               Corresponding_Record_Type (Full_Base));
9013          end if;
9014       end if;
9015    end Complete_Private_Subtype;
9016
9017    ----------------------------
9018    -- Constant_Redeclaration --
9019    ----------------------------
9020
9021    procedure Constant_Redeclaration
9022      (Id : Entity_Id;
9023       N  : Node_Id;
9024       T  : out Entity_Id)
9025    is
9026       Prev    : constant Entity_Id := Current_Entity_In_Scope (Id);
9027       Obj_Def : constant Node_Id := Object_Definition (N);
9028       New_T   : Entity_Id;
9029
9030       procedure Check_Possible_Deferred_Completion
9031         (Prev_Id      : Entity_Id;
9032          Prev_Obj_Def : Node_Id;
9033          Curr_Obj_Def : Node_Id);
9034       --  Determine whether the two object definitions describe the partial
9035       --  and the full view of a constrained deferred constant. Generate
9036       --  a subtype for the full view and verify that it statically matches
9037       --  the subtype of the partial view.
9038
9039       procedure Check_Recursive_Declaration (Typ : Entity_Id);
9040       --  If deferred constant is an access type initialized with an allocator,
9041       --  check whether there is an illegal recursion in the definition,
9042       --  through a default value of some record subcomponent. This is normally
9043       --  detected when generating init procs, but requires this additional
9044       --  mechanism when expansion is disabled.
9045
9046       ----------------------------------------
9047       -- Check_Possible_Deferred_Completion --
9048       ----------------------------------------
9049
9050       procedure Check_Possible_Deferred_Completion
9051         (Prev_Id      : Entity_Id;
9052          Prev_Obj_Def : Node_Id;
9053          Curr_Obj_Def : Node_Id)
9054       is
9055       begin
9056          if Nkind (Prev_Obj_Def) = N_Subtype_Indication
9057            and then Present (Constraint (Prev_Obj_Def))
9058            and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
9059            and then Present (Constraint (Curr_Obj_Def))
9060          then
9061             declare
9062                Loc    : constant Source_Ptr := Sloc (N);
9063                Def_Id : constant Entity_Id :=
9064                           Make_Defining_Identifier (Loc,
9065                             New_Internal_Name ('S'));
9066                Decl   : constant Node_Id :=
9067                           Make_Subtype_Declaration (Loc,
9068                             Defining_Identifier =>
9069                               Def_Id,
9070                             Subtype_Indication =>
9071                               Relocate_Node (Curr_Obj_Def));
9072
9073             begin
9074                Insert_Before_And_Analyze (N, Decl);
9075                Set_Etype (Id, Def_Id);
9076
9077                if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
9078                   Error_Msg_Sloc := Sloc (Prev_Id);
9079                   Error_Msg_N ("subtype does not statically match deferred " &
9080                                "declaration#", N);
9081                end if;
9082             end;
9083          end if;
9084       end Check_Possible_Deferred_Completion;
9085
9086       ---------------------------------
9087       -- Check_Recursive_Declaration --
9088       ---------------------------------
9089
9090       procedure Check_Recursive_Declaration (Typ : Entity_Id) is
9091          Comp : Entity_Id;
9092
9093       begin
9094          if Is_Record_Type (Typ) then
9095             Comp := First_Component (Typ);
9096             while Present (Comp) loop
9097                if Comes_From_Source (Comp) then
9098                   if Present (Expression (Parent (Comp)))
9099                     and then Is_Entity_Name (Expression (Parent (Comp)))
9100                     and then Entity (Expression (Parent (Comp))) = Prev
9101                   then
9102                      Error_Msg_Sloc := Sloc (Parent (Comp));
9103                      Error_Msg_NE
9104                        ("illegal circularity with declaration for&#",
9105                          N, Comp);
9106                      return;
9107
9108                   elsif Is_Record_Type (Etype (Comp)) then
9109                      Check_Recursive_Declaration (Etype (Comp));
9110                   end if;
9111                end if;
9112
9113                Next_Component (Comp);
9114             end loop;
9115          end if;
9116       end Check_Recursive_Declaration;
9117
9118    --  Start of processing for Constant_Redeclaration
9119
9120    begin
9121       if Nkind (Parent (Prev)) = N_Object_Declaration then
9122          if Nkind (Object_Definition
9123                      (Parent (Prev))) = N_Subtype_Indication
9124          then
9125             --  Find type of new declaration. The constraints of the two
9126             --  views must match statically, but there is no point in
9127             --  creating an itype for the full view.
9128
9129             if Nkind (Obj_Def) = N_Subtype_Indication then
9130                Find_Type (Subtype_Mark (Obj_Def));
9131                New_T := Entity (Subtype_Mark (Obj_Def));
9132
9133             else
9134                Find_Type (Obj_Def);
9135                New_T := Entity (Obj_Def);
9136             end if;
9137
9138             T := Etype (Prev);
9139
9140          else
9141             --  The full view may impose a constraint, even if the partial
9142             --  view does not, so construct the subtype.
9143
9144             New_T := Find_Type_Of_Object (Obj_Def, N);
9145             T     := New_T;
9146          end if;
9147
9148       else
9149          --  Current declaration is illegal, diagnosed below in Enter_Name
9150
9151          T := Empty;
9152          New_T := Any_Type;
9153       end if;
9154
9155       --  If previous full declaration exists, or if a homograph is present,
9156       --  let Enter_Name handle it, either with an error, or with the removal
9157       --  of an overridden implicit subprogram.
9158
9159       if Ekind (Prev) /= E_Constant
9160         or else Present (Expression (Parent (Prev)))
9161         or else Present (Full_View (Prev))
9162       then
9163          Enter_Name (Id);
9164
9165       --  Verify that types of both declarations match, or else that both types
9166       --  are anonymous access types whose designated subtypes statically match
9167       --  (as allowed in Ada 2005 by AI-385).
9168
9169       elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
9170         and then
9171           (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
9172              or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
9173              or else Is_Access_Constant (Etype (New_T)) /=
9174                      Is_Access_Constant (Etype (Prev))
9175              or else Can_Never_Be_Null (Etype (New_T)) /=
9176                      Can_Never_Be_Null (Etype (Prev))
9177              or else Null_Exclusion_Present (Parent (Prev)) /=
9178                      Null_Exclusion_Present (Parent (Id))
9179              or else not Subtypes_Statically_Match
9180                            (Designated_Type (Etype (Prev)),
9181                             Designated_Type (Etype (New_T))))
9182       then
9183          Error_Msg_Sloc := Sloc (Prev);
9184          Error_Msg_N ("type does not match declaration#", N);
9185          Set_Full_View (Prev, Id);
9186          Set_Etype (Id, Any_Type);
9187
9188       elsif
9189         Null_Exclusion_Present (Parent (Prev))
9190           and then not Null_Exclusion_Present (N)
9191       then
9192          Error_Msg_Sloc := Sloc (Prev);
9193          Error_Msg_N ("null-exclusion does not match declaration#", N);
9194          Set_Full_View (Prev, Id);
9195          Set_Etype (Id, Any_Type);
9196
9197       --  If so, process the full constant declaration
9198
9199       else
9200          --  RM 7.4 (6): If the subtype defined by the subtype_indication in
9201          --  the deferred declaration is constrained, then the subtype defined
9202          --  by the subtype_indication in the full declaration shall match it
9203          --  statically.
9204
9205          Check_Possible_Deferred_Completion
9206            (Prev_Id      => Prev,
9207             Prev_Obj_Def => Object_Definition (Parent (Prev)),
9208             Curr_Obj_Def => Obj_Def);
9209
9210          Set_Full_View (Prev, Id);
9211          Set_Is_Public (Id, Is_Public (Prev));
9212          Set_Is_Internal (Id);
9213          Append_Entity (Id, Current_Scope);
9214
9215          --  Check ALIASED present if present before (RM 7.4(7))
9216
9217          if Is_Aliased (Prev)
9218            and then not Aliased_Present (N)
9219          then
9220             Error_Msg_Sloc := Sloc (Prev);
9221             Error_Msg_N ("ALIASED required (see declaration#)", N);
9222          end if;
9223
9224          --  Allow incomplete declaration of tags (used to handle forward
9225          --  references to tags). The check on Ada_Tags avoids circularities
9226          --  when rebuilding the compiler.
9227
9228          if RTU_Loaded (Ada_Tags)
9229            and then T = RTE (RE_Tag)
9230          then
9231             null;
9232
9233          --  Check that placement is in private part and that the incomplete
9234          --  declaration appeared in the visible part.
9235
9236          elsif Ekind (Current_Scope) = E_Package
9237            and then not In_Private_Part (Current_Scope)
9238          then
9239             Error_Msg_Sloc := Sloc (Prev);
9240             Error_Msg_N ("full constant for declaration#"
9241                          & " must be in private part", N);
9242
9243          elsif Ekind (Current_Scope) = E_Package
9244            and then List_Containing (Parent (Prev))
9245            /= Visible_Declarations
9246              (Specification (Unit_Declaration_Node (Current_Scope)))
9247          then
9248             Error_Msg_N
9249               ("deferred constant must be declared in visible part",
9250                  Parent (Prev));
9251          end if;
9252
9253          if Is_Access_Type (T)
9254            and then Nkind (Expression (N)) = N_Allocator
9255          then
9256             Check_Recursive_Declaration (Designated_Type (T));
9257          end if;
9258       end if;
9259    end Constant_Redeclaration;
9260
9261    ----------------------
9262    -- Constrain_Access --
9263    ----------------------
9264
9265    procedure Constrain_Access
9266      (Def_Id      : in out Entity_Id;
9267       S           : Node_Id;
9268       Related_Nod : Node_Id)
9269    is
9270       T             : constant Entity_Id := Entity (Subtype_Mark (S));
9271       Desig_Type    : constant Entity_Id := Designated_Type (T);
9272       Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
9273       Constraint_OK : Boolean := True;
9274
9275       function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
9276       --  Simple predicate to test for defaulted discriminants
9277       --  Shouldn't this be in sem_util???
9278
9279       ---------------------------------
9280       -- Has_Defaulted_Discriminants --
9281       ---------------------------------
9282
9283       function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
9284       begin
9285          return Has_Discriminants (Typ)
9286           and then Present (First_Discriminant (Typ))
9287           and then Present
9288             (Discriminant_Default_Value (First_Discriminant (Typ)));
9289       end Has_Defaulted_Discriminants;
9290
9291    --  Start of processing for Constrain_Access
9292
9293    begin
9294       if Is_Array_Type (Desig_Type) then
9295          Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
9296
9297       elsif (Is_Record_Type (Desig_Type)
9298               or else Is_Incomplete_Or_Private_Type (Desig_Type))
9299         and then not Is_Constrained (Desig_Type)
9300       then
9301          --  ??? The following code is a temporary kludge to ignore a
9302          --  discriminant constraint on access type if it is constraining
9303          --  the current record. Avoid creating the implicit subtype of the
9304          --  record we are currently compiling since right now, we cannot
9305          --  handle these. For now, just return the access type itself.
9306
9307          if Desig_Type = Current_Scope
9308            and then No (Def_Id)
9309          then
9310             Set_Ekind (Desig_Subtype, E_Record_Subtype);
9311             Def_Id := Entity (Subtype_Mark (S));
9312
9313             --  This call added to ensure that the constraint is analyzed
9314             --  (needed for a B test). Note that we still return early from
9315             --  this procedure to avoid recursive processing. ???
9316
9317             Constrain_Discriminated_Type
9318               (Desig_Subtype, S, Related_Nod, For_Access => True);
9319             return;
9320          end if;
9321
9322          if (Ekind (T) = E_General_Access_Type
9323               or else Ada_Version >= Ada_05)
9324            and then Has_Private_Declaration (Desig_Type)
9325            and then In_Open_Scopes (Scope (Desig_Type))
9326            and then Has_Discriminants (Desig_Type)
9327          then
9328             --  Enforce rule that the constraint is illegal if there is
9329             --  an unconstrained view of the designated type. This means
9330             --  that the partial view (either a private type declaration or
9331             --  a derivation from a private type) has no discriminants.
9332             --  (Defect Report 8652/0008, Technical Corrigendum 1, checked
9333             --  by ACATS B371001).
9334
9335             --  Rule updated for Ada 2005: the private type is said to have
9336             --  a constrained partial view, given that objects of the type
9337             --  can be declared. Furthermore, the rule applies to all access
9338             --  types, unlike the rule concerning default discriminants.
9339
9340             declare
9341                Pack  : constant Node_Id :=
9342                          Unit_Declaration_Node (Scope (Desig_Type));
9343                Decls : List_Id;
9344                Decl  : Node_Id;
9345
9346             begin
9347                if Nkind (Pack) = N_Package_Declaration then
9348                   Decls := Visible_Declarations (Specification (Pack));
9349                   Decl := First (Decls);
9350                   while Present (Decl) loop
9351                      if (Nkind (Decl) = N_Private_Type_Declaration
9352                           and then
9353                             Chars (Defining_Identifier (Decl)) =
9354                                                      Chars (Desig_Type))
9355
9356                        or else
9357                         (Nkind (Decl) = N_Full_Type_Declaration
9358                           and then
9359                             Chars (Defining_Identifier (Decl)) =
9360                                                      Chars (Desig_Type)
9361                           and then Is_Derived_Type (Desig_Type)
9362                           and then
9363                             Has_Private_Declaration (Etype (Desig_Type)))
9364                      then
9365                         if No (Discriminant_Specifications (Decl)) then
9366                            Error_Msg_N
9367                             ("cannot constrain general access type if " &
9368                                "designated type has constrained partial view",
9369                                 S);
9370                         end if;
9371
9372                         exit;
9373                      end if;
9374
9375                      Next (Decl);
9376                   end loop;
9377                end if;
9378             end;
9379          end if;
9380
9381          Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
9382            For_Access => True);
9383
9384       elsif (Is_Task_Type (Desig_Type)
9385               or else Is_Protected_Type (Desig_Type))
9386         and then not Is_Constrained (Desig_Type)
9387       then
9388          Constrain_Concurrent
9389            (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
9390
9391       else
9392          Error_Msg_N ("invalid constraint on access type", S);
9393          Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
9394          Constraint_OK := False;
9395       end if;
9396
9397       if No (Def_Id) then
9398          Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
9399       else
9400          Set_Ekind (Def_Id, E_Access_Subtype);
9401       end if;
9402
9403       if Constraint_OK then
9404          Set_Etype (Def_Id, Base_Type (T));
9405
9406          if Is_Private_Type (Desig_Type) then
9407             Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
9408          end if;
9409       else
9410          Set_Etype (Def_Id, Any_Type);
9411       end if;
9412
9413       Set_Size_Info                (Def_Id, T);
9414       Set_Is_Constrained           (Def_Id, Constraint_OK);
9415       Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
9416       Set_Depends_On_Private       (Def_Id, Has_Private_Component (Def_Id));
9417       Set_Is_Access_Constant       (Def_Id, Is_Access_Constant (T));
9418
9419       Conditional_Delay (Def_Id, T);
9420
9421       --  AI-363 : Subtypes of general access types whose designated types have
9422       --  default discriminants are disallowed. In instances, the rule has to
9423       --  be checked against the actual, of which T is the subtype. In a
9424       --  generic body, the rule is checked assuming that the actual type has
9425       --  defaulted discriminants.
9426
9427       if Ada_Version >= Ada_05 or else Warn_On_Ada_2005_Compatibility then
9428          if Ekind (Base_Type (T)) = E_General_Access_Type
9429            and then Has_Defaulted_Discriminants (Desig_Type)
9430          then
9431             if Ada_Version < Ada_05 then
9432                Error_Msg_N
9433                  ("access subtype of general access type would not " &
9434                   "be allowed in Ada 2005?", S);
9435             else
9436                Error_Msg_N
9437                  ("access subype of general access type not allowed", S);
9438             end if;
9439
9440             Error_Msg_N ("\discriminants have defaults", S);
9441
9442          elsif Is_Access_Type (T)
9443            and then Is_Generic_Type (Desig_Type)
9444            and then Has_Discriminants (Desig_Type)
9445            and then In_Package_Body (Current_Scope)
9446          then
9447             if Ada_Version < Ada_05 then
9448                Error_Msg_N
9449                  ("access subtype would not be allowed in generic body " &
9450                   "in Ada 2005?", S);
9451             else
9452                Error_Msg_N
9453                  ("access subtype not allowed in generic body", S);
9454             end if;
9455
9456             Error_Msg_N
9457               ("\designated type is a discriminated formal", S);
9458          end if;
9459       end if;
9460    end Constrain_Access;
9461
9462    ---------------------
9463    -- Constrain_Array --
9464    ---------------------
9465
9466    procedure Constrain_Array
9467      (Def_Id      : in out Entity_Id;
9468       SI          : Node_Id;
9469       Related_Nod : Node_Id;
9470       Related_Id  : Entity_Id;
9471       Suffix      : Character)
9472    is
9473       C                     : constant Node_Id := Constraint (SI);
9474       Number_Of_Constraints : Nat := 0;
9475       Index                 : Node_Id;
9476       S, T                  : Entity_Id;
9477       Constraint_OK         : Boolean := True;
9478
9479    begin
9480       T := Entity (Subtype_Mark (SI));
9481
9482       if Ekind (T) in Access_Kind then
9483          T := Designated_Type (T);
9484       end if;
9485
9486       --  If an index constraint follows a subtype mark in a subtype indication
9487       --  then the type or subtype denoted by the subtype mark must not already
9488       --  impose an index constraint. The subtype mark must denote either an
9489       --  unconstrained array type or an access type whose designated type
9490       --  is such an array type... (RM 3.6.1)
9491
9492       if Is_Constrained (T) then
9493          Error_Msg_N
9494            ("array type is already constrained", Subtype_Mark (SI));
9495          Constraint_OK := False;
9496
9497       else
9498          S := First (Constraints (C));
9499          while Present (S) loop
9500             Number_Of_Constraints := Number_Of_Constraints + 1;
9501             Next (S);
9502          end loop;
9503
9504          --  In either case, the index constraint must provide a discrete
9505          --  range for each index of the array type and the type of each
9506          --  discrete range must be the same as that of the corresponding
9507          --  index. (RM 3.6.1)
9508
9509          if Number_Of_Constraints /= Number_Dimensions (T) then
9510             Error_Msg_NE ("incorrect number of index constraints for }", C, T);
9511             Constraint_OK := False;
9512
9513          else
9514             S := First (Constraints (C));
9515             Index := First_Index (T);
9516             Analyze (Index);
9517
9518             --  Apply constraints to each index type
9519
9520             for J in 1 .. Number_Of_Constraints loop
9521                Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
9522                Next (Index);
9523                Next (S);
9524             end loop;
9525
9526          end if;
9527       end if;
9528
9529       if No (Def_Id) then
9530          Def_Id :=
9531            Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
9532          Set_Parent (Def_Id, Related_Nod);
9533
9534       else
9535          Set_Ekind (Def_Id, E_Array_Subtype);
9536       end if;
9537
9538       Set_Size_Info      (Def_Id,                (T));
9539       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9540       Set_Etype          (Def_Id, Base_Type      (T));
9541
9542       if Constraint_OK then
9543          Set_First_Index (Def_Id, First (Constraints (C)));
9544       else
9545          Set_First_Index (Def_Id, First_Index (T));
9546       end if;
9547
9548       Set_Is_Constrained     (Def_Id, True);
9549       Set_Is_Aliased         (Def_Id, Is_Aliased (T));
9550       Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
9551
9552       Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
9553       Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
9554
9555       --  A subtype does not inherit the packed_array_type of is parent. We
9556       --  need to initialize the attribute because if Def_Id is previously
9557       --  analyzed through a limited_with clause, it will have the attributes
9558       --  of an incomplete type, one of which is an Elist that overlaps the
9559       --  Packed_Array_Type field.
9560
9561       Set_Packed_Array_Type (Def_Id, Empty);
9562
9563       --  Build a freeze node if parent still needs one. Also make sure that
9564       --  the Depends_On_Private status is set because the subtype will need
9565       --  reprocessing at the time the base type does, and also we must set a
9566       --  conditional delay.
9567
9568       Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9569       Conditional_Delay (Def_Id, T);
9570    end Constrain_Array;
9571
9572    ------------------------------
9573    -- Constrain_Component_Type --
9574    ------------------------------
9575
9576    function Constrain_Component_Type
9577      (Comp            : Entity_Id;
9578       Constrained_Typ : Entity_Id;
9579       Related_Node    : Node_Id;
9580       Typ             : Entity_Id;
9581       Constraints     : Elist_Id) return Entity_Id
9582    is
9583       Loc         : constant Source_Ptr := Sloc (Constrained_Typ);
9584       Compon_Type : constant Entity_Id := Etype (Comp);
9585
9586       function Build_Constrained_Array_Type
9587         (Old_Type : Entity_Id) return Entity_Id;
9588       --  If Old_Type is an array type, one of whose indices is constrained
9589       --  by a discriminant, build an Itype whose constraint replaces the
9590       --  discriminant with its value in the constraint.
9591
9592       function Build_Constrained_Discriminated_Type
9593         (Old_Type : Entity_Id) return Entity_Id;
9594       --  Ditto for record components
9595
9596       function Build_Constrained_Access_Type
9597         (Old_Type : Entity_Id) return Entity_Id;
9598       --  Ditto for access types. Makes use of previous two functions, to
9599       --  constrain designated type.
9600
9601       function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
9602       --  T is an array or discriminated type, C is a list of constraints
9603       --  that apply to T. This routine builds the constrained subtype.
9604
9605       function Is_Discriminant (Expr : Node_Id) return Boolean;
9606       --  Returns True if Expr is a discriminant
9607
9608       function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
9609       --  Find the value of discriminant Discrim in Constraint
9610
9611       -----------------------------------
9612       -- Build_Constrained_Access_Type --
9613       -----------------------------------
9614
9615       function Build_Constrained_Access_Type
9616         (Old_Type : Entity_Id) return Entity_Id
9617       is
9618          Desig_Type    : constant Entity_Id := Designated_Type (Old_Type);
9619          Itype         : Entity_Id;
9620          Desig_Subtype : Entity_Id;
9621          Scop          : Entity_Id;
9622
9623       begin
9624          --  if the original access type was not embedded in the enclosing
9625          --  type definition, there is no need to produce a new access
9626          --  subtype. In fact every access type with an explicit constraint
9627          --  generates an itype whose scope is the enclosing record.
9628
9629          if not Is_Type (Scope (Old_Type)) then
9630             return Old_Type;
9631
9632          elsif Is_Array_Type (Desig_Type) then
9633             Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
9634
9635          elsif Has_Discriminants (Desig_Type) then
9636
9637             --  This may be an access type to an enclosing record type for
9638             --  which we are constructing the constrained components. Return
9639             --  the enclosing record subtype. This is not always correct,
9640             --  but avoids infinite recursion. ???
9641
9642             Desig_Subtype := Any_Type;
9643
9644             for J in reverse 0 .. Scope_Stack.Last loop
9645                Scop := Scope_Stack.Table (J).Entity;
9646
9647                if Is_Type (Scop)
9648                  and then Base_Type (Scop) = Base_Type (Desig_Type)
9649                then
9650                   Desig_Subtype := Scop;
9651                end if;
9652
9653                exit when not Is_Type (Scop);
9654             end loop;
9655
9656             if Desig_Subtype = Any_Type then
9657                Desig_Subtype :=
9658                  Build_Constrained_Discriminated_Type (Desig_Type);
9659             end if;
9660
9661          else
9662             return Old_Type;
9663          end if;
9664
9665          if Desig_Subtype /= Desig_Type then
9666
9667             --  The Related_Node better be here or else we won't be able
9668             --  to attach new itypes to a node in the tree.
9669
9670             pragma Assert (Present (Related_Node));
9671
9672             Itype := Create_Itype (E_Access_Subtype, Related_Node);
9673
9674             Set_Etype                    (Itype, Base_Type      (Old_Type));
9675             Set_Size_Info                (Itype,                (Old_Type));
9676             Set_Directly_Designated_Type (Itype, Desig_Subtype);
9677             Set_Depends_On_Private       (Itype, Has_Private_Component
9678                                                                 (Old_Type));
9679             Set_Is_Access_Constant       (Itype, Is_Access_Constant
9680                                                                 (Old_Type));
9681
9682             --  The new itype needs freezing when it depends on a not frozen
9683             --  type and the enclosing subtype needs freezing.
9684
9685             if Has_Delayed_Freeze (Constrained_Typ)
9686               and then not Is_Frozen (Constrained_Typ)
9687             then
9688                Conditional_Delay (Itype, Base_Type (Old_Type));
9689             end if;
9690
9691             return Itype;
9692
9693          else
9694             return Old_Type;
9695          end if;
9696       end Build_Constrained_Access_Type;
9697
9698       ----------------------------------
9699       -- Build_Constrained_Array_Type --
9700       ----------------------------------
9701
9702       function Build_Constrained_Array_Type
9703         (Old_Type : Entity_Id) return Entity_Id
9704       is
9705          Lo_Expr     : Node_Id;
9706          Hi_Expr     : Node_Id;
9707          Old_Index   : Node_Id;
9708          Range_Node  : Node_Id;
9709          Constr_List : List_Id;
9710
9711          Need_To_Create_Itype : Boolean := False;
9712
9713       begin
9714          Old_Index := First_Index (Old_Type);
9715          while Present (Old_Index) loop
9716             Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
9717
9718             if Is_Discriminant (Lo_Expr)
9719               or else Is_Discriminant (Hi_Expr)
9720             then
9721                Need_To_Create_Itype := True;
9722             end if;
9723
9724             Next_Index (Old_Index);
9725          end loop;
9726
9727          if Need_To_Create_Itype then
9728             Constr_List := New_List;
9729
9730             Old_Index := First_Index (Old_Type);
9731             while Present (Old_Index) loop
9732                Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
9733
9734                if Is_Discriminant (Lo_Expr) then
9735                   Lo_Expr := Get_Discr_Value (Lo_Expr);
9736                end if;
9737
9738                if Is_Discriminant (Hi_Expr) then
9739                   Hi_Expr := Get_Discr_Value (Hi_Expr);
9740                end if;
9741
9742                Range_Node :=
9743                  Make_Range
9744                    (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
9745
9746                Append (Range_Node, To => Constr_List);
9747
9748                Next_Index (Old_Index);
9749             end loop;
9750
9751             return Build_Subtype (Old_Type, Constr_List);
9752
9753          else
9754             return Old_Type;
9755          end if;
9756       end Build_Constrained_Array_Type;
9757
9758       ------------------------------------------
9759       -- Build_Constrained_Discriminated_Type --
9760       ------------------------------------------
9761
9762       function Build_Constrained_Discriminated_Type
9763         (Old_Type : Entity_Id) return Entity_Id
9764       is
9765          Expr           : Node_Id;
9766          Constr_List    : List_Id;
9767          Old_Constraint : Elmt_Id;
9768
9769          Need_To_Create_Itype : Boolean := False;
9770
9771       begin
9772          Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
9773          while Present (Old_Constraint) loop
9774             Expr := Node (Old_Constraint);
9775
9776             if Is_Discriminant (Expr) then
9777                Need_To_Create_Itype := True;
9778             end if;
9779
9780             Next_Elmt (Old_Constraint);
9781          end loop;
9782
9783          if Need_To_Create_Itype then
9784             Constr_List := New_List;
9785
9786             Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
9787             while Present (Old_Constraint) loop
9788                Expr := Node (Old_Constraint);
9789
9790                if Is_Discriminant (Expr) then
9791                   Expr := Get_Discr_Value (Expr);
9792                end if;
9793
9794                Append (New_Copy_Tree (Expr), To => Constr_List);
9795
9796                Next_Elmt (Old_Constraint);
9797             end loop;
9798
9799             return Build_Subtype (Old_Type, Constr_List);
9800
9801          else
9802             return Old_Type;
9803          end if;
9804       end Build_Constrained_Discriminated_Type;
9805
9806       -------------------
9807       -- Build_Subtype --
9808       -------------------
9809
9810       function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
9811          Indic       : Node_Id;
9812          Subtyp_Decl : Node_Id;
9813          Def_Id      : Entity_Id;
9814          Btyp        : Entity_Id := Base_Type (T);
9815
9816       begin
9817          --  The Related_Node better be here or else we won't be able to
9818          --  attach new itypes to a node in the tree.
9819
9820          pragma Assert (Present (Related_Node));
9821
9822          --  If the view of the component's type is incomplete or private
9823          --  with unknown discriminants, then the constraint must be applied
9824          --  to the full type.
9825
9826          if Has_Unknown_Discriminants (Btyp)
9827            and then Present (Underlying_Type (Btyp))
9828          then
9829             Btyp := Underlying_Type (Btyp);
9830          end if;
9831
9832          Indic :=
9833            Make_Subtype_Indication (Loc,
9834              Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
9835              Constraint   => Make_Index_Or_Discriminant_Constraint (Loc, C));
9836
9837          Def_Id := Create_Itype (Ekind (T), Related_Node);
9838
9839          Subtyp_Decl :=
9840            Make_Subtype_Declaration (Loc,
9841              Defining_Identifier => Def_Id,
9842              Subtype_Indication  => Indic);
9843
9844          Set_Parent (Subtyp_Decl, Parent (Related_Node));
9845
9846          --  Itypes must be analyzed with checks off (see package Itypes)
9847
9848          Analyze (Subtyp_Decl, Suppress => All_Checks);
9849
9850          return Def_Id;
9851       end Build_Subtype;
9852
9853       ---------------------
9854       -- Get_Discr_Value --
9855       ---------------------
9856
9857       function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
9858          D : Entity_Id;
9859          E : Elmt_Id;
9860
9861       begin
9862          --  The discriminant may be declared for the type, in which case we
9863          --  find it by iterating over the list of discriminants. If the
9864          --  discriminant is inherited from a parent type, it appears as the
9865          --  corresponding discriminant of the current type. This will be the
9866          --  case when constraining an inherited component whose constraint is
9867          --  given by a discriminant of the parent.
9868
9869          D := First_Discriminant (Typ);
9870          E := First_Elmt (Constraints);
9871
9872          while Present (D) loop
9873             if D = Entity (Discrim)
9874               or else D = CR_Discriminant (Entity (Discrim))
9875               or else Corresponding_Discriminant (D) = Entity (Discrim)
9876             then
9877                return Node (E);
9878             end if;
9879
9880             Next_Discriminant (D);
9881             Next_Elmt (E);
9882          end loop;
9883
9884          --  The corresponding_Discriminant mechanism is incomplete, because
9885          --  the correspondence between new and old discriminants is not one
9886          --  to one: one new discriminant can constrain several old ones. In
9887          --  that case, scan sequentially the stored_constraint, the list of
9888          --  discriminants of the parents, and the constraints.
9889          --  Previous code checked for the present of the Stored_Constraint
9890          --  list for the derived type, but did not use it at all. Should it
9891          --  be present when the component is a discriminated task type?
9892
9893          if Is_Derived_Type (Typ)
9894            and then Scope (Entity (Discrim)) = Etype (Typ)
9895          then
9896             D := First_Discriminant (Etype (Typ));
9897             E := First_Elmt (Constraints);
9898             while Present (D) loop
9899                if D = Entity (Discrim) then
9900                   return Node (E);
9901                end if;
9902
9903                Next_Discriminant (D);
9904                Next_Elmt (E);
9905             end loop;
9906          end if;
9907
9908          --  Something is wrong if we did not find the value
9909
9910          raise Program_Error;
9911       end Get_Discr_Value;
9912
9913       ---------------------
9914       -- Is_Discriminant --
9915       ---------------------
9916
9917       function Is_Discriminant (Expr : Node_Id) return Boolean is
9918          Discrim_Scope : Entity_Id;
9919
9920       begin
9921          if Denotes_Discriminant (Expr) then
9922             Discrim_Scope := Scope (Entity (Expr));
9923
9924             --  Either we have a reference to one of Typ's discriminants,
9925
9926             pragma Assert (Discrim_Scope = Typ
9927
9928                --  or to the discriminants of the parent type, in the case
9929                --  of a derivation of a tagged type with variants.
9930
9931                or else Discrim_Scope = Etype (Typ)
9932                or else Full_View (Discrim_Scope) = Etype (Typ)
9933
9934                --  or same as above for the case where the discriminants
9935                --  were declared in Typ's private view.
9936
9937                or else (Is_Private_Type (Discrim_Scope)
9938                         and then Chars (Discrim_Scope) = Chars (Typ))
9939
9940                --  or else we are deriving from the full view and the
9941                --  discriminant is declared in the private entity.
9942
9943                or else (Is_Private_Type (Typ)
9944                          and then Chars (Discrim_Scope) = Chars (Typ))
9945
9946                --  Or we are constrained the corresponding record of a
9947                --  synchronized type that completes a private declaration.
9948
9949                or else (Is_Concurrent_Record_Type (Typ)
9950                          and then
9951                            Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
9952
9953                --  or we have a class-wide type, in which case make sure the
9954                --  discriminant found belongs to the root type.
9955
9956                or else (Is_Class_Wide_Type (Typ)
9957                          and then Etype (Typ) = Discrim_Scope));
9958
9959             return True;
9960          end if;
9961
9962          --  In all other cases we have something wrong
9963
9964          return False;
9965       end Is_Discriminant;
9966
9967    --  Start of processing for Constrain_Component_Type
9968
9969    begin
9970       if Nkind (Parent (Comp)) = N_Component_Declaration
9971         and then Comes_From_Source (Parent (Comp))
9972         and then Comes_From_Source
9973           (Subtype_Indication (Component_Definition (Parent (Comp))))
9974         and then
9975           Is_Entity_Name
9976             (Subtype_Indication (Component_Definition (Parent (Comp))))
9977       then
9978          return Compon_Type;
9979
9980       elsif Is_Array_Type (Compon_Type) then
9981          return Build_Constrained_Array_Type (Compon_Type);
9982
9983       elsif Has_Discriminants (Compon_Type) then
9984          return Build_Constrained_Discriminated_Type (Compon_Type);
9985
9986       elsif Is_Access_Type (Compon_Type) then
9987          return Build_Constrained_Access_Type (Compon_Type);
9988
9989       else
9990          return Compon_Type;
9991       end if;
9992    end Constrain_Component_Type;
9993
9994    --------------------------
9995    -- Constrain_Concurrent --
9996    --------------------------
9997
9998    --  For concurrent types, the associated record value type carries the same
9999    --  discriminants, so when we constrain a concurrent type, we must constrain
10000    --  the corresponding record type as well.
10001
10002    procedure Constrain_Concurrent
10003      (Def_Id      : in out Entity_Id;
10004       SI          : Node_Id;
10005       Related_Nod : Node_Id;
10006       Related_Id  : Entity_Id;
10007       Suffix      : Character)
10008    is
10009       T_Ent : Entity_Id := Entity (Subtype_Mark (SI));
10010       T_Val : Entity_Id;
10011
10012    begin
10013       if Ekind (T_Ent) in Access_Kind then
10014          T_Ent := Designated_Type (T_Ent);
10015       end if;
10016
10017       T_Val := Corresponding_Record_Type (T_Ent);
10018
10019       if Present (T_Val) then
10020
10021          if No (Def_Id) then
10022             Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
10023          end if;
10024
10025          Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
10026
10027          Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10028          Set_Corresponding_Record_Type (Def_Id,
10029            Constrain_Corresponding_Record
10030              (Def_Id, T_Val, Related_Nod, Related_Id));
10031
10032       else
10033          --  If there is no associated record, expansion is disabled and this
10034          --  is a generic context. Create a subtype in any case, so that
10035          --  semantic analysis can proceed.
10036
10037          if No (Def_Id) then
10038             Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
10039          end if;
10040
10041          Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
10042       end if;
10043    end Constrain_Concurrent;
10044
10045    ------------------------------------
10046    -- Constrain_Corresponding_Record --
10047    ------------------------------------
10048
10049    function Constrain_Corresponding_Record
10050      (Prot_Subt   : Entity_Id;
10051       Corr_Rec    : Entity_Id;
10052       Related_Nod : Node_Id;
10053       Related_Id  : Entity_Id) return Entity_Id
10054    is
10055       T_Sub : constant Entity_Id :=
10056                 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
10057
10058    begin
10059       Set_Etype             (T_Sub, Corr_Rec);
10060       Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
10061       Set_Is_Constrained    (T_Sub, True);
10062       Set_First_Entity      (T_Sub, First_Entity (Corr_Rec));
10063       Set_Last_Entity       (T_Sub, Last_Entity  (Corr_Rec));
10064
10065       --  As elsewhere, we do not want to create a freeze node for this itype
10066       --  if it is created for a constrained component of an enclosing record
10067       --  because references to outer discriminants will appear out of scope.
10068
10069       if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
10070          Conditional_Delay (T_Sub, Corr_Rec);
10071       else
10072          Set_Is_Frozen (T_Sub);
10073       end if;
10074
10075       if Has_Discriminants (Prot_Subt) then -- False only if errors.
10076          Set_Discriminant_Constraint
10077            (T_Sub, Discriminant_Constraint (Prot_Subt));
10078          Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
10079          Create_Constrained_Components
10080            (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
10081       end if;
10082
10083       Set_Depends_On_Private      (T_Sub, Has_Private_Component (T_Sub));
10084
10085       return T_Sub;
10086    end Constrain_Corresponding_Record;
10087
10088    -----------------------
10089    -- Constrain_Decimal --
10090    -----------------------
10091
10092    procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
10093       T           : constant Entity_Id  := Entity (Subtype_Mark (S));
10094       C           : constant Node_Id    := Constraint (S);
10095       Loc         : constant Source_Ptr := Sloc (C);
10096       Range_Expr  : Node_Id;
10097       Digits_Expr : Node_Id;
10098       Digits_Val  : Uint;
10099       Bound_Val   : Ureal;
10100
10101    begin
10102       Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
10103
10104       if Nkind (C) = N_Range_Constraint then
10105          Range_Expr := Range_Expression (C);
10106          Digits_Val := Digits_Value (T);
10107
10108       else
10109          pragma Assert (Nkind (C) = N_Digits_Constraint);
10110          Digits_Expr := Digits_Expression (C);
10111          Analyze_And_Resolve (Digits_Expr, Any_Integer);
10112
10113          Check_Digits_Expression (Digits_Expr);
10114          Digits_Val := Expr_Value (Digits_Expr);
10115
10116          if Digits_Val > Digits_Value (T) then
10117             Error_Msg_N
10118                ("digits expression is incompatible with subtype", C);
10119             Digits_Val := Digits_Value (T);
10120          end if;
10121
10122          if Present (Range_Constraint (C)) then
10123             Range_Expr := Range_Expression (Range_Constraint (C));
10124          else
10125             Range_Expr := Empty;
10126          end if;
10127       end if;
10128
10129       Set_Etype            (Def_Id, Base_Type        (T));
10130       Set_Size_Info        (Def_Id,                  (T));
10131       Set_First_Rep_Item   (Def_Id, First_Rep_Item   (T));
10132       Set_Delta_Value      (Def_Id, Delta_Value      (T));
10133       Set_Scale_Value      (Def_Id, Scale_Value      (T));
10134       Set_Small_Value      (Def_Id, Small_Value      (T));
10135       Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
10136       Set_Digits_Value     (Def_Id, Digits_Val);
10137
10138       --  Manufacture range from given digits value if no range present
10139
10140       if No (Range_Expr) then
10141          Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
10142          Range_Expr :=
10143            Make_Range (Loc,
10144              Low_Bound =>
10145                Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
10146              High_Bound =>
10147                Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
10148       end if;
10149
10150       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
10151       Set_Discrete_RM_Size (Def_Id);
10152
10153       --  Unconditionally delay the freeze, since we cannot set size
10154       --  information in all cases correctly until the freeze point.
10155
10156       Set_Has_Delayed_Freeze (Def_Id);
10157    end Constrain_Decimal;
10158
10159    ----------------------------------
10160    -- Constrain_Discriminated_Type --
10161    ----------------------------------
10162
10163    procedure Constrain_Discriminated_Type
10164      (Def_Id      : Entity_Id;
10165       S           : Node_Id;
10166       Related_Nod : Node_Id;
10167       For_Access  : Boolean := False)
10168    is
10169       E     : constant Entity_Id := Entity (Subtype_Mark (S));
10170       T     : Entity_Id;
10171       C     : Node_Id;
10172       Elist : Elist_Id := New_Elmt_List;
10173
10174       procedure Fixup_Bad_Constraint;
10175       --  This is called after finding a bad constraint, and after having
10176       --  posted an appropriate error message. The mission is to leave the
10177       --  entity T in as reasonable state as possible!
10178
10179       --------------------------
10180       -- Fixup_Bad_Constraint --
10181       --------------------------
10182
10183       procedure Fixup_Bad_Constraint is
10184       begin
10185          --  Set a reasonable Ekind for the entity. For an incomplete type,
10186          --  we can't do much, but for other types, we can set the proper
10187          --  corresponding subtype kind.
10188
10189          if Ekind (T) = E_Incomplete_Type then
10190             Set_Ekind (Def_Id, Ekind (T));
10191          else
10192             Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10193          end if;
10194
10195          --  Set Etype to the known type, to reduce chances of cascaded errors
10196
10197          Set_Etype (Def_Id, E);
10198          Set_Error_Posted (Def_Id);
10199       end Fixup_Bad_Constraint;
10200
10201    --  Start of processing for Constrain_Discriminated_Type
10202
10203    begin
10204       C := Constraint (S);
10205
10206       --  A discriminant constraint is only allowed in a subtype indication,
10207       --  after a subtype mark. This subtype mark must denote either a type
10208       --  with discriminants, or an access type whose designated type is a
10209       --  type with discriminants. A discriminant constraint specifies the
10210       --  values of these discriminants (RM 3.7.2(5)).
10211
10212       T := Base_Type (Entity (Subtype_Mark (S)));
10213
10214       if Ekind (T) in Access_Kind then
10215          T := Designated_Type (T);
10216       end if;
10217
10218       --  Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
10219       --  Avoid generating an error for access-to-incomplete subtypes.
10220
10221       if Ada_Version >= Ada_05
10222         and then Ekind (T) = E_Incomplete_Type
10223         and then Nkind (Parent (S)) = N_Subtype_Declaration
10224         and then not Is_Itype (Def_Id)
10225       then
10226          --  A little sanity check, emit an error message if the type
10227          --  has discriminants to begin with. Type T may be a regular
10228          --  incomplete type or imported via a limited with clause.
10229
10230          if Has_Discriminants (T)
10231            or else
10232              (From_With_Type (T)
10233                 and then Present (Non_Limited_View (T))
10234                 and then Nkind (Parent (Non_Limited_View (T))) =
10235                            N_Full_Type_Declaration
10236                 and then Present (Discriminant_Specifications
10237                           (Parent (Non_Limited_View (T)))))
10238          then
10239             Error_Msg_N
10240               ("(Ada 2005) incomplete subtype may not be constrained", C);
10241          else
10242             Error_Msg_N
10243               ("invalid constraint: type has no discriminant", C);
10244          end if;
10245
10246          Fixup_Bad_Constraint;
10247          return;
10248
10249       --  Check that the type has visible discriminants. The type may be
10250       --  a private type with unknown discriminants whose full view has
10251       --  discriminants which are invisible.
10252
10253       elsif not Has_Discriminants (T)
10254         or else
10255           (Has_Unknown_Discriminants (T)
10256              and then Is_Private_Type (T))
10257       then
10258          Error_Msg_N ("invalid constraint: type has no discriminant", C);
10259          Fixup_Bad_Constraint;
10260          return;
10261
10262       elsif Is_Constrained (E)
10263         or else (Ekind (E) = E_Class_Wide_Subtype
10264                   and then Present (Discriminant_Constraint (E)))
10265       then
10266          Error_Msg_N ("type is already constrained", Subtype_Mark (S));
10267          Fixup_Bad_Constraint;
10268          return;
10269       end if;
10270
10271       --  T may be an unconstrained subtype (e.g. a generic actual).
10272       --  Constraint applies to the base type.
10273
10274       T := Base_Type (T);
10275
10276       Elist := Build_Discriminant_Constraints (T, S);
10277
10278       --  If the list returned was empty we had an error in building the
10279       --  discriminant constraint. We have also already signalled an error
10280       --  in the incomplete type case
10281
10282       if Is_Empty_Elmt_List (Elist) then
10283          Fixup_Bad_Constraint;
10284          return;
10285       end if;
10286
10287       Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
10288    end Constrain_Discriminated_Type;
10289
10290    ---------------------------
10291    -- Constrain_Enumeration --
10292    ---------------------------
10293
10294    procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
10295       T : constant Entity_Id := Entity (Subtype_Mark (S));
10296       C : constant Node_Id   := Constraint (S);
10297
10298    begin
10299       Set_Ekind (Def_Id, E_Enumeration_Subtype);
10300
10301       Set_First_Literal     (Def_Id, First_Literal (Base_Type (T)));
10302
10303       Set_Etype             (Def_Id, Base_Type         (T));
10304       Set_Size_Info         (Def_Id,                   (T));
10305       Set_First_Rep_Item    (Def_Id, First_Rep_Item    (T));
10306       Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
10307
10308       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10309
10310       Set_Discrete_RM_Size (Def_Id);
10311    end Constrain_Enumeration;
10312
10313    ----------------------
10314    -- Constrain_Float --
10315    ----------------------
10316
10317    procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
10318       T    : constant Entity_Id := Entity (Subtype_Mark (S));
10319       C    : Node_Id;
10320       D    : Node_Id;
10321       Rais : Node_Id;
10322
10323    begin
10324       Set_Ekind (Def_Id, E_Floating_Point_Subtype);
10325
10326       Set_Etype          (Def_Id, Base_Type      (T));
10327       Set_Size_Info      (Def_Id,                (T));
10328       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10329
10330       --  Process the constraint
10331
10332       C := Constraint (S);
10333
10334       --  Digits constraint present
10335
10336       if Nkind (C) = N_Digits_Constraint then
10337          Check_Restriction (No_Obsolescent_Features, C);
10338
10339          if Warn_On_Obsolescent_Feature then
10340             Error_Msg_N
10341               ("subtype digits constraint is an " &
10342                "obsolescent feature (RM J.3(8))?", C);
10343          end if;
10344
10345          D := Digits_Expression (C);
10346          Analyze_And_Resolve (D, Any_Integer);
10347          Check_Digits_Expression (D);
10348          Set_Digits_Value (Def_Id, Expr_Value (D));
10349
10350          --  Check that digits value is in range. Obviously we can do this
10351          --  at compile time, but it is strictly a runtime check, and of
10352          --  course there is an ACVC test that checks this!
10353
10354          if Digits_Value (Def_Id) > Digits_Value (T) then
10355             Error_Msg_Uint_1 := Digits_Value (T);
10356             Error_Msg_N ("?digits value is too large, maximum is ^", D);
10357             Rais :=
10358               Make_Raise_Constraint_Error (Sloc (D),
10359                 Reason => CE_Range_Check_Failed);
10360             Insert_Action (Declaration_Node (Def_Id), Rais);
10361          end if;
10362
10363          C := Range_Constraint (C);
10364
10365       --  No digits constraint present
10366
10367       else
10368          Set_Digits_Value (Def_Id, Digits_Value (T));
10369       end if;
10370
10371       --  Range constraint present
10372
10373       if Nkind (C) = N_Range_Constraint then
10374          Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10375
10376       --  No range constraint present
10377
10378       else
10379          pragma Assert (No (C));
10380          Set_Scalar_Range (Def_Id, Scalar_Range (T));
10381       end if;
10382
10383       Set_Is_Constrained (Def_Id);
10384    end Constrain_Float;
10385
10386    ---------------------
10387    -- Constrain_Index --
10388    ---------------------
10389
10390    procedure Constrain_Index
10391      (Index        : Node_Id;
10392       S            : Node_Id;
10393       Related_Nod  : Node_Id;
10394       Related_Id   : Entity_Id;
10395       Suffix       : Character;
10396       Suffix_Index : Nat)
10397    is
10398       Def_Id : Entity_Id;
10399       R      : Node_Id := Empty;
10400       T      : constant Entity_Id := Etype (Index);
10401
10402    begin
10403       if Nkind (S) = N_Range
10404         or else
10405           (Nkind (S) = N_Attribute_Reference
10406             and then Attribute_Name (S) = Name_Range)
10407       then
10408          --  A Range attribute will transformed into N_Range by Resolve
10409
10410          Analyze (S);
10411          Set_Etype (S, T);
10412          R := S;
10413
10414          Process_Range_Expr_In_Decl (R, T, Empty_List);
10415
10416          if not Error_Posted (S)
10417            and then
10418              (Nkind (S) /= N_Range
10419                or else not Covers (T, (Etype (Low_Bound (S))))
10420                or else not Covers (T, (Etype (High_Bound (S)))))
10421          then
10422             if Base_Type (T) /= Any_Type
10423               and then Etype (Low_Bound (S)) /= Any_Type
10424               and then Etype (High_Bound (S)) /= Any_Type
10425             then
10426                Error_Msg_N ("range expected", S);
10427             end if;
10428          end if;
10429
10430       elsif Nkind (S) = N_Subtype_Indication then
10431
10432          --  The parser has verified that this is a discrete indication
10433
10434          Resolve_Discrete_Subtype_Indication (S, T);
10435          R := Range_Expression (Constraint (S));
10436
10437       elsif Nkind (S) = N_Discriminant_Association then
10438
10439          --  Syntactically valid in subtype indication
10440
10441          Error_Msg_N ("invalid index constraint", S);
10442          Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
10443          return;
10444
10445       --  Subtype_Mark case, no anonymous subtypes to construct
10446
10447       else
10448          Analyze (S);
10449
10450          if Is_Entity_Name (S) then
10451             if not Is_Type (Entity (S)) then
10452                Error_Msg_N ("expect subtype mark for index constraint", S);
10453
10454             elsif Base_Type (Entity (S)) /= Base_Type (T) then
10455                Wrong_Type (S, Base_Type (T));
10456             end if;
10457
10458             return;
10459
10460          else
10461             Error_Msg_N ("invalid index constraint", S);
10462             Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
10463             return;
10464          end if;
10465       end if;
10466
10467       Def_Id :=
10468         Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
10469
10470       Set_Etype (Def_Id, Base_Type (T));
10471
10472       if Is_Modular_Integer_Type (T) then
10473          Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
10474
10475       elsif Is_Integer_Type (T) then
10476          Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
10477
10478       else
10479          Set_Ekind (Def_Id, E_Enumeration_Subtype);
10480          Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
10481       end if;
10482
10483       Set_Size_Info      (Def_Id,                (T));
10484       Set_RM_Size        (Def_Id, RM_Size        (T));
10485       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10486
10487       Set_Scalar_Range   (Def_Id, R);
10488
10489       Set_Etype (S, Def_Id);
10490       Set_Discrete_RM_Size (Def_Id);
10491    end Constrain_Index;
10492
10493    -----------------------
10494    -- Constrain_Integer --
10495    -----------------------
10496
10497    procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
10498       T : constant Entity_Id := Entity (Subtype_Mark (S));
10499       C : constant Node_Id   := Constraint (S);
10500
10501    begin
10502       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10503
10504       if Is_Modular_Integer_Type (T) then
10505          Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
10506       else
10507          Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
10508       end if;
10509
10510       Set_Etype            (Def_Id, Base_Type        (T));
10511       Set_Size_Info        (Def_Id,                  (T));
10512       Set_First_Rep_Item   (Def_Id, First_Rep_Item   (T));
10513       Set_Discrete_RM_Size (Def_Id);
10514    end Constrain_Integer;
10515
10516    ------------------------------
10517    -- Constrain_Ordinary_Fixed --
10518    ------------------------------
10519
10520    procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
10521       T    : constant Entity_Id := Entity (Subtype_Mark (S));
10522       C    : Node_Id;
10523       D    : Node_Id;
10524       Rais : Node_Id;
10525
10526    begin
10527       Set_Ekind          (Def_Id, E_Ordinary_Fixed_Point_Subtype);
10528       Set_Etype          (Def_Id, Base_Type        (T));
10529       Set_Size_Info      (Def_Id,                  (T));
10530       Set_First_Rep_Item (Def_Id, First_Rep_Item   (T));
10531       Set_Small_Value    (Def_Id, Small_Value      (T));
10532
10533       --  Process the constraint
10534
10535       C := Constraint (S);
10536
10537       --  Delta constraint present
10538
10539       if Nkind (C) = N_Delta_Constraint then
10540          Check_Restriction (No_Obsolescent_Features, C);
10541
10542          if Warn_On_Obsolescent_Feature then
10543             Error_Msg_S
10544               ("subtype delta constraint is an " &
10545                "obsolescent feature (RM J.3(7))?");
10546          end if;
10547
10548          D := Delta_Expression (C);
10549          Analyze_And_Resolve (D, Any_Real);
10550          Check_Delta_Expression (D);
10551          Set_Delta_Value (Def_Id, Expr_Value_R (D));
10552
10553          --  Check that delta value is in range. Obviously we can do this
10554          --  at compile time, but it is strictly a runtime check, and of
10555          --  course there is an ACVC test that checks this!
10556
10557          if Delta_Value (Def_Id) < Delta_Value (T) then
10558             Error_Msg_N ("?delta value is too small", D);
10559             Rais :=
10560               Make_Raise_Constraint_Error (Sloc (D),
10561                 Reason => CE_Range_Check_Failed);
10562             Insert_Action (Declaration_Node (Def_Id), Rais);
10563          end if;
10564
10565          C := Range_Constraint (C);
10566
10567       --  No delta constraint present
10568
10569       else
10570          Set_Delta_Value (Def_Id, Delta_Value (T));
10571       end if;
10572
10573       --  Range constraint present
10574
10575       if Nkind (C) = N_Range_Constraint then
10576          Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10577
10578       --  No range constraint present
10579
10580       else
10581          pragma Assert (No (C));
10582          Set_Scalar_Range (Def_Id, Scalar_Range (T));
10583
10584       end if;
10585
10586       Set_Discrete_RM_Size (Def_Id);
10587
10588       --  Unconditionally delay the freeze, since we cannot set size
10589       --  information in all cases correctly until the freeze point.
10590
10591       Set_Has_Delayed_Freeze (Def_Id);
10592    end Constrain_Ordinary_Fixed;
10593
10594    -----------------------
10595    -- Contain_Interface --
10596    -----------------------
10597
10598    function Contain_Interface
10599      (Iface  : Entity_Id;
10600       Ifaces : Elist_Id) return Boolean
10601    is
10602       Iface_Elmt : Elmt_Id;
10603
10604    begin
10605       if Present (Ifaces) then
10606          Iface_Elmt := First_Elmt (Ifaces);
10607          while Present (Iface_Elmt) loop
10608             if Node (Iface_Elmt) = Iface then
10609                return True;
10610             end if;
10611
10612             Next_Elmt (Iface_Elmt);
10613          end loop;
10614       end if;
10615
10616       return False;
10617    end Contain_Interface;
10618
10619    ---------------------------
10620    -- Convert_Scalar_Bounds --
10621    ---------------------------
10622
10623    procedure Convert_Scalar_Bounds
10624      (N            : Node_Id;
10625       Parent_Type  : Entity_Id;
10626       Derived_Type : Entity_Id;
10627       Loc          : Source_Ptr)
10628    is
10629       Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
10630
10631       Lo  : Node_Id;
10632       Hi  : Node_Id;
10633       Rng : Node_Id;
10634
10635    begin
10636       Lo := Build_Scalar_Bound
10637               (Type_Low_Bound (Derived_Type),
10638                Parent_Type, Implicit_Base);
10639
10640       Hi := Build_Scalar_Bound
10641               (Type_High_Bound (Derived_Type),
10642                Parent_Type, Implicit_Base);
10643
10644       Rng :=
10645         Make_Range (Loc,
10646           Low_Bound  => Lo,
10647           High_Bound => Hi);
10648
10649       Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
10650
10651       Set_Parent (Rng, N);
10652       Set_Scalar_Range (Derived_Type, Rng);
10653
10654       --  Analyze the bounds
10655
10656       Analyze_And_Resolve (Lo, Implicit_Base);
10657       Analyze_And_Resolve (Hi, Implicit_Base);
10658
10659       --  Analyze the range itself, except that we do not analyze it if
10660       --  the bounds are real literals, and we have a fixed-point type.
10661       --  The reason for this is that we delay setting the bounds in this
10662       --  case till we know the final Small and Size values (see circuit
10663       --  in Freeze.Freeze_Fixed_Point_Type for further details).
10664
10665       if Is_Fixed_Point_Type (Parent_Type)
10666         and then Nkind (Lo) = N_Real_Literal
10667         and then Nkind (Hi) = N_Real_Literal
10668       then
10669          return;
10670
10671       --  Here we do the analysis of the range
10672
10673       --  Note: we do this manually, since if we do a normal Analyze and
10674       --  Resolve call, there are problems with the conversions used for
10675       --  the derived type range.
10676
10677       else
10678          Set_Etype    (Rng, Implicit_Base);
10679          Set_Analyzed (Rng, True);
10680       end if;
10681    end Convert_Scalar_Bounds;
10682
10683    -------------------
10684    -- Copy_And_Swap --
10685    -------------------
10686
10687    procedure Copy_And_Swap (Priv, Full : Entity_Id) is
10688    begin
10689       --  Initialize new full declaration entity by copying the pertinent
10690       --  fields of the corresponding private declaration entity.
10691
10692       --  We temporarily set Ekind to a value appropriate for a type to
10693       --  avoid assert failures in Einfo from checking for setting type
10694       --  attributes on something that is not a type. Ekind (Priv) is an
10695       --  appropriate choice, since it allowed the attributes to be set
10696       --  in the first place. This Ekind value will be modified later.
10697
10698       Set_Ekind (Full, Ekind (Priv));
10699
10700       --  Also set Etype temporarily to Any_Type, again, in the absence
10701       --  of errors, it will be properly reset, and if there are errors,
10702       --  then we want a value of Any_Type to remain.
10703
10704       Set_Etype (Full, Any_Type);
10705
10706       --  Now start copying attributes
10707
10708       Set_Has_Discriminants          (Full, Has_Discriminants       (Priv));
10709
10710       if Has_Discriminants (Full) then
10711          Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
10712          Set_Stored_Constraint       (Full, Stored_Constraint       (Priv));
10713       end if;
10714
10715       Set_First_Rep_Item             (Full, First_Rep_Item          (Priv));
10716       Set_Homonym                    (Full, Homonym                 (Priv));
10717       Set_Is_Immediately_Visible     (Full, Is_Immediately_Visible  (Priv));
10718       Set_Is_Public                  (Full, Is_Public               (Priv));
10719       Set_Is_Pure                    (Full, Is_Pure                 (Priv));
10720       Set_Is_Tagged_Type             (Full, Is_Tagged_Type          (Priv));
10721       Set_Has_Pragma_Unreferenced    (Full, Has_Pragma_Unreferenced (Priv));
10722       Set_Has_Pragma_Unreferenced_Objects
10723                                      (Full, Has_Pragma_Unreferenced_Objects
10724                                                                     (Priv));
10725
10726       Conditional_Delay              (Full,                          Priv);
10727
10728       if Is_Tagged_Type (Full) then
10729          Set_Primitive_Operations    (Full, Primitive_Operations    (Priv));
10730
10731          if Priv = Base_Type (Priv) then
10732             Set_Class_Wide_Type      (Full, Class_Wide_Type         (Priv));
10733          end if;
10734       end if;
10735
10736       Set_Is_Volatile                (Full, Is_Volatile             (Priv));
10737       Set_Treat_As_Volatile          (Full, Treat_As_Volatile       (Priv));
10738       Set_Scope                      (Full, Scope                   (Priv));
10739       Set_Next_Entity                (Full, Next_Entity             (Priv));
10740       Set_First_Entity               (Full, First_Entity            (Priv));
10741       Set_Last_Entity                (Full, Last_Entity             (Priv));
10742
10743       --  If access types have been recorded for later handling, keep them in
10744       --  the full view so that they get handled when the full view freeze
10745       --  node is expanded.
10746
10747       if Present (Freeze_Node (Priv))
10748         and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
10749       then
10750          Ensure_Freeze_Node (Full);
10751          Set_Access_Types_To_Process
10752            (Freeze_Node (Full),
10753             Access_Types_To_Process (Freeze_Node (Priv)));
10754       end if;
10755
10756       --  Swap the two entities. Now Privat is the full type entity and
10757       --  Full is the private one. They will be swapped back at the end
10758       --  of the private part. This swapping ensures that the entity that
10759       --  is visible in the private part is the full declaration.
10760
10761       Exchange_Entities (Priv, Full);
10762       Append_Entity (Full, Scope (Full));
10763    end Copy_And_Swap;
10764
10765    -------------------------------------
10766    -- Copy_Array_Base_Type_Attributes --
10767    -------------------------------------
10768
10769    procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
10770    begin
10771       Set_Component_Alignment      (T1, Component_Alignment      (T2));
10772       Set_Component_Type           (T1, Component_Type           (T2));
10773       Set_Component_Size           (T1, Component_Size           (T2));
10774       Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
10775       Set_Finalize_Storage_Only    (T1, Finalize_Storage_Only    (T2));
10776       Set_Has_Non_Standard_Rep     (T1, Has_Non_Standard_Rep     (T2));
10777       Set_Has_Task                 (T1, Has_Task                 (T2));
10778       Set_Is_Packed                (T1, Is_Packed                (T2));
10779       Set_Has_Aliased_Components   (T1, Has_Aliased_Components   (T2));
10780       Set_Has_Atomic_Components    (T1, Has_Atomic_Components    (T2));
10781       Set_Has_Volatile_Components  (T1, Has_Volatile_Components  (T2));
10782    end Copy_Array_Base_Type_Attributes;
10783
10784    -----------------------------------
10785    -- Copy_Array_Subtype_Attributes --
10786    -----------------------------------
10787
10788    procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
10789    begin
10790       Set_Size_Info (T1, T2);
10791
10792       Set_First_Index          (T1, First_Index           (T2));
10793       Set_Is_Aliased           (T1, Is_Aliased            (T2));
10794       Set_Is_Atomic            (T1, Is_Atomic             (T2));
10795       Set_Is_Volatile          (T1, Is_Volatile           (T2));
10796       Set_Treat_As_Volatile    (T1, Treat_As_Volatile     (T2));
10797       Set_Is_Constrained       (T1, Is_Constrained        (T2));
10798       Set_Depends_On_Private   (T1, Has_Private_Component (T2));
10799       Set_First_Rep_Item       (T1, First_Rep_Item        (T2));
10800       Set_Convention           (T1, Convention            (T2));
10801       Set_Is_Limited_Composite (T1, Is_Limited_Composite  (T2));
10802       Set_Is_Private_Composite (T1, Is_Private_Composite  (T2));
10803    end Copy_Array_Subtype_Attributes;
10804
10805    -----------------------------------
10806    -- Create_Constrained_Components --
10807    -----------------------------------
10808
10809    procedure Create_Constrained_Components
10810      (Subt        : Entity_Id;
10811       Decl_Node   : Node_Id;
10812       Typ         : Entity_Id;
10813       Constraints : Elist_Id)
10814    is
10815       Loc         : constant Source_Ptr := Sloc (Subt);
10816       Comp_List   : constant Elist_Id   := New_Elmt_List;
10817       Parent_Type : constant Entity_Id  := Etype (Typ);
10818       Assoc_List  : constant List_Id    := New_List;
10819       Discr_Val   : Elmt_Id;
10820       Errors      : Boolean;
10821       New_C       : Entity_Id;
10822       Old_C       : Entity_Id;
10823       Is_Static   : Boolean := True;
10824
10825       procedure Collect_Fixed_Components (Typ : Entity_Id);
10826       --  Collect parent type components that do not appear in a variant part
10827
10828       procedure Create_All_Components;
10829       --  Iterate over Comp_List to create the components of the subtype
10830
10831       function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
10832       --  Creates a new component from Old_Compon, copying all the fields from
10833       --  it, including its Etype, inserts the new component in the Subt entity
10834       --  chain and returns the new component.
10835
10836       function Is_Variant_Record (T : Entity_Id) return Boolean;
10837       --  If true, and discriminants are static, collect only components from
10838       --  variants selected by discriminant values.
10839
10840       ------------------------------
10841       -- Collect_Fixed_Components --
10842       ------------------------------
10843
10844       procedure Collect_Fixed_Components (Typ : Entity_Id) is
10845       begin
10846       --  Build association list for discriminants, and find components of the
10847       --  variant part selected by the values of the discriminants.
10848
10849          Old_C := First_Discriminant (Typ);
10850          Discr_Val := First_Elmt (Constraints);
10851          while Present (Old_C) loop
10852             Append_To (Assoc_List,
10853               Make_Component_Association (Loc,
10854                  Choices    => New_List (New_Occurrence_Of (Old_C, Loc)),
10855                  Expression => New_Copy (Node (Discr_Val))));
10856
10857             Next_Elmt (Discr_Val);
10858             Next_Discriminant (Old_C);
10859          end loop;
10860
10861          --  The tag, and the possible parent and controller components
10862          --  are unconditionally in the subtype.
10863
10864          if Is_Tagged_Type (Typ)
10865            or else Has_Controlled_Component (Typ)
10866          then
10867             Old_C := First_Component (Typ);
10868             while Present (Old_C) loop
10869                if Chars ((Old_C)) = Name_uTag
10870                  or else Chars ((Old_C)) = Name_uParent
10871                  or else Chars ((Old_C)) = Name_uController
10872                then
10873                   Append_Elmt (Old_C, Comp_List);
10874                end if;
10875
10876                Next_Component (Old_C);
10877             end loop;
10878          end if;
10879       end Collect_Fixed_Components;
10880
10881       ---------------------------
10882       -- Create_All_Components --
10883       ---------------------------
10884
10885       procedure Create_All_Components is
10886          Comp : Elmt_Id;
10887
10888       begin
10889          Comp := First_Elmt (Comp_List);
10890          while Present (Comp) loop
10891             Old_C := Node (Comp);
10892             New_C := Create_Component (Old_C);
10893
10894             Set_Etype
10895               (New_C,
10896                Constrain_Component_Type
10897                  (Old_C, Subt, Decl_Node, Typ, Constraints));
10898             Set_Is_Public (New_C, Is_Public (Subt));
10899
10900             Next_Elmt (Comp);
10901          end loop;
10902       end Create_All_Components;
10903
10904       ----------------------
10905       -- Create_Component --
10906       ----------------------
10907
10908       function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
10909          New_Compon : constant Entity_Id := New_Copy (Old_Compon);
10910
10911       begin
10912          if Ekind (Old_Compon) = E_Discriminant
10913            and then Is_Completely_Hidden (Old_Compon)
10914          then
10915             --  This is a shadow discriminant created for a discriminant of
10916             --  the parent type that is one of several renamed by the same
10917             --  new discriminant. Give the shadow discriminant an internal
10918             --  name that cannot conflict with that of visible components.
10919
10920             Set_Chars (New_Compon, New_Internal_Name ('C'));
10921          end if;
10922
10923          --  Set the parent so we have a proper link for freezing etc. This is
10924          --  not a real parent pointer, since of course our parent does not own
10925          --  up to us and reference us, we are an illegitimate child of the
10926          --  original parent!
10927
10928          Set_Parent (New_Compon, Parent (Old_Compon));
10929
10930          --  If the old component's Esize was already determined and is a
10931          --  static value, then the new component simply inherits it. Otherwise
10932          --  the old component's size may require run-time determination, but
10933          --  the new component's size still might be statically determinable
10934          --  (if, for example it has a static constraint). In that case we want
10935          --  Layout_Type to recompute the component's size, so we reset its
10936          --  size and positional fields.
10937
10938          if Frontend_Layout_On_Target
10939            and then not Known_Static_Esize (Old_Compon)
10940          then
10941             Set_Esize (New_Compon, Uint_0);
10942             Init_Normalized_First_Bit    (New_Compon);
10943             Init_Normalized_Position     (New_Compon);
10944             Init_Normalized_Position_Max (New_Compon);
10945          end if;
10946
10947          --  We do not want this node marked as Comes_From_Source, since
10948          --  otherwise it would get first class status and a separate cross-
10949          --  reference line would be generated. Illegitimate children do not
10950          --  rate such recognition.
10951
10952          Set_Comes_From_Source (New_Compon, False);
10953
10954          --  But it is a real entity, and a birth certificate must be properly
10955          --  registered by entering it into the entity list.
10956
10957          Enter_Name (New_Compon);
10958
10959          return New_Compon;
10960       end Create_Component;
10961
10962       -----------------------
10963       -- Is_Variant_Record --
10964       -----------------------
10965
10966       function Is_Variant_Record (T : Entity_Id) return Boolean is
10967       begin
10968          return Nkind (Parent (T)) = N_Full_Type_Declaration
10969            and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
10970            and then Present (Component_List (Type_Definition (Parent (T))))
10971            and then
10972              Present
10973                (Variant_Part (Component_List (Type_Definition (Parent (T)))));
10974       end Is_Variant_Record;
10975
10976    --  Start of processing for Create_Constrained_Components
10977
10978    begin
10979       pragma Assert (Subt /= Base_Type (Subt));
10980       pragma Assert (Typ = Base_Type (Typ));
10981
10982       Set_First_Entity (Subt, Empty);
10983       Set_Last_Entity  (Subt, Empty);
10984
10985       --  Check whether constraint is fully static, in which case we can
10986       --  optimize the list of components.
10987
10988       Discr_Val := First_Elmt (Constraints);
10989       while Present (Discr_Val) loop
10990          if not Is_OK_Static_Expression (Node (Discr_Val)) then
10991             Is_Static := False;
10992             exit;
10993          end if;
10994
10995          Next_Elmt (Discr_Val);
10996       end loop;
10997
10998       Set_Has_Static_Discriminants (Subt, Is_Static);
10999
11000       Push_Scope (Subt);
11001
11002       --  Inherit the discriminants of the parent type
11003
11004       Add_Discriminants : declare
11005          Num_Disc : Int;
11006          Num_Gird : Int;
11007
11008       begin
11009          Num_Disc := 0;
11010          Old_C := First_Discriminant (Typ);
11011
11012          while Present (Old_C) loop
11013             Num_Disc := Num_Disc + 1;
11014             New_C := Create_Component (Old_C);
11015             Set_Is_Public (New_C, Is_Public (Subt));
11016             Next_Discriminant (Old_C);
11017          end loop;
11018
11019          --  For an untagged derived subtype, the number of discriminants may
11020          --  be smaller than the number of inherited discriminants, because
11021          --  several of them may be renamed by a single new discriminant.
11022          --  In this case, add the hidden discriminants back into the subtype,
11023          --  because otherwise the size of the subtype is computed incorrectly
11024          --  in GCC 4.1.
11025
11026          Num_Gird := 0;
11027
11028          if Is_Derived_Type (Typ)
11029            and then not Is_Tagged_Type (Typ)
11030          then
11031             Old_C := First_Stored_Discriminant (Typ);
11032
11033             while Present (Old_C) loop
11034                Num_Gird := Num_Gird + 1;
11035                Next_Stored_Discriminant (Old_C);
11036             end loop;
11037          end if;
11038
11039          if Num_Gird > Num_Disc then
11040
11041             --  Find out multiple uses of new discriminants, and add hidden
11042             --  components for the extra renamed discriminants. We recognize
11043             --  multiple uses through the Corresponding_Discriminant of a
11044             --  new discriminant: if it constrains several old discriminants,
11045             --  this field points to the last one in the parent type. The
11046             --  stored discriminants of the derived type have the same name
11047             --  as those of the parent.
11048
11049             declare
11050                Constr    : Elmt_Id;
11051                New_Discr : Entity_Id;
11052                Old_Discr : Entity_Id;
11053
11054             begin
11055                Constr    := First_Elmt (Stored_Constraint (Typ));
11056                Old_Discr := First_Stored_Discriminant (Typ);
11057                while Present (Constr) loop
11058                   if Is_Entity_Name (Node (Constr))
11059                     and then Ekind (Entity (Node (Constr))) = E_Discriminant
11060                   then
11061                      New_Discr := Entity (Node (Constr));
11062
11063                      if Chars (Corresponding_Discriminant (New_Discr)) /=
11064                         Chars (Old_Discr)
11065                      then
11066                         --  The new discriminant has been used to rename a
11067                         --  subsequent old discriminant. Introduce a shadow
11068                         --  component for the current old discriminant.
11069
11070                         New_C := Create_Component (Old_Discr);
11071                         Set_Original_Record_Component  (New_C, Old_Discr);
11072                      end if;
11073                   end if;
11074
11075                   Next_Elmt (Constr);
11076                   Next_Stored_Discriminant (Old_Discr);
11077                end loop;
11078             end;
11079          end if;
11080       end Add_Discriminants;
11081
11082       if Is_Static
11083         and then Is_Variant_Record (Typ)
11084       then
11085          Collect_Fixed_Components (Typ);
11086
11087          Gather_Components (
11088            Typ,
11089            Component_List (Type_Definition (Parent (Typ))),
11090            Governed_By   => Assoc_List,
11091            Into          => Comp_List,
11092            Report_Errors => Errors);
11093          pragma Assert (not Errors);
11094
11095          Create_All_Components;
11096
11097       --  If the subtype declaration is created for a tagged type derivation
11098       --  with constraints, we retrieve the record definition of the parent
11099       --  type to select the components of the proper variant.
11100
11101       elsif Is_Static
11102         and then Is_Tagged_Type (Typ)
11103         and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
11104         and then
11105           Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
11106         and then Is_Variant_Record (Parent_Type)
11107       then
11108          Collect_Fixed_Components (Typ);
11109
11110          Gather_Components (
11111            Typ,
11112            Component_List (Type_Definition (Parent (Parent_Type))),
11113            Governed_By   => Assoc_List,
11114            Into          => Comp_List,
11115            Report_Errors => Errors);
11116          pragma Assert (not Errors);
11117
11118          --  If the tagged derivation has a type extension, collect all the
11119          --  new components therein.
11120
11121          if Present
11122               (Record_Extension_Part (Type_Definition (Parent (Typ))))
11123          then
11124             Old_C := First_Component (Typ);
11125             while Present (Old_C) loop
11126                if Original_Record_Component (Old_C) = Old_C
11127                 and then Chars (Old_C) /= Name_uTag
11128                 and then Chars (Old_C) /= Name_uParent
11129                 and then Chars (Old_C) /= Name_uController
11130                then
11131                   Append_Elmt (Old_C, Comp_List);
11132                end if;
11133
11134                Next_Component (Old_C);
11135             end loop;
11136          end if;
11137
11138          Create_All_Components;
11139
11140       else
11141          --  If discriminants are not static, or if this is a multi-level type
11142          --  extension, we have to include all components of the parent type.
11143
11144          Old_C := First_Component (Typ);
11145          while Present (Old_C) loop
11146             New_C := Create_Component (Old_C);
11147
11148             Set_Etype
11149               (New_C,
11150                Constrain_Component_Type
11151                  (Old_C, Subt, Decl_Node, Typ, Constraints));
11152             Set_Is_Public (New_C, Is_Public (Subt));
11153
11154             Next_Component (Old_C);
11155          end loop;
11156       end if;
11157
11158       End_Scope;
11159    end Create_Constrained_Components;
11160
11161    ------------------------------------------
11162    -- Decimal_Fixed_Point_Type_Declaration --
11163    ------------------------------------------
11164
11165    procedure Decimal_Fixed_Point_Type_Declaration
11166      (T   : Entity_Id;
11167       Def : Node_Id)
11168    is
11169       Loc           : constant Source_Ptr := Sloc (Def);
11170       Digs_Expr     : constant Node_Id    := Digits_Expression (Def);
11171       Delta_Expr    : constant Node_Id    := Delta_Expression (Def);
11172       Implicit_Base : Entity_Id;
11173       Digs_Val      : Uint;
11174       Delta_Val     : Ureal;
11175       Scale_Val     : Uint;
11176       Bound_Val     : Ureal;
11177
11178    begin
11179       Check_Restriction (No_Fixed_Point, Def);
11180
11181       --  Create implicit base type
11182
11183       Implicit_Base :=
11184         Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
11185       Set_Etype (Implicit_Base, Implicit_Base);
11186
11187       --  Analyze and process delta expression
11188
11189       Analyze_And_Resolve (Delta_Expr, Universal_Real);
11190
11191       Check_Delta_Expression (Delta_Expr);
11192       Delta_Val := Expr_Value_R (Delta_Expr);
11193
11194       --  Check delta is power of 10, and determine scale value from it
11195
11196       declare
11197          Val : Ureal;
11198
11199       begin
11200          Scale_Val := Uint_0;
11201          Val := Delta_Val;
11202
11203          if Val < Ureal_1 then
11204             while Val < Ureal_1 loop
11205                Val := Val * Ureal_10;
11206                Scale_Val := Scale_Val + 1;
11207             end loop;
11208
11209             if Scale_Val > 18 then
11210                Error_Msg_N ("scale exceeds maximum value of 18", Def);
11211                Scale_Val := UI_From_Int (+18);
11212             end if;
11213
11214          else
11215             while Val > Ureal_1 loop
11216                Val := Val / Ureal_10;
11217                Scale_Val := Scale_Val - 1;
11218             end loop;
11219
11220             if Scale_Val < -18 then
11221                Error_Msg_N ("scale is less than minimum value of -18", Def);
11222                Scale_Val := UI_From_Int (-18);
11223             end if;
11224          end if;
11225
11226          if Val /= Ureal_1 then
11227             Error_Msg_N ("delta expression must be a power of 10", Def);
11228             Delta_Val := Ureal_10 ** (-Scale_Val);
11229          end if;
11230       end;
11231
11232       --  Set delta, scale and small (small = delta for decimal type)
11233
11234       Set_Delta_Value (Implicit_Base, Delta_Val);
11235       Set_Scale_Value (Implicit_Base, Scale_Val);
11236       Set_Small_Value (Implicit_Base, Delta_Val);
11237
11238       --  Analyze and process digits expression
11239
11240       Analyze_And_Resolve (Digs_Expr, Any_Integer);
11241       Check_Digits_Expression (Digs_Expr);
11242       Digs_Val := Expr_Value (Digs_Expr);
11243
11244       if Digs_Val > 18 then
11245          Digs_Val := UI_From_Int (+18);
11246          Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
11247       end if;
11248
11249       Set_Digits_Value (Implicit_Base, Digs_Val);
11250       Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
11251
11252       --  Set range of base type from digits value for now. This will be
11253       --  expanded to represent the true underlying base range by Freeze.
11254
11255       Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
11256
11257       --  Note: We leave size as zero for now, size will be set at freeze
11258       --  time. We have to do this for ordinary fixed-point, because the size
11259       --  depends on the specified small, and we might as well do the same for
11260       --  decimal fixed-point.
11261
11262       pragma Assert (Esize (Implicit_Base) = Uint_0);
11263
11264       --  If there are bounds given in the declaration use them as the
11265       --  bounds of the first named subtype.
11266
11267       if Present (Real_Range_Specification (Def)) then
11268          declare
11269             RRS      : constant Node_Id := Real_Range_Specification (Def);
11270             Low      : constant Node_Id := Low_Bound (RRS);
11271             High     : constant Node_Id := High_Bound (RRS);
11272             Low_Val  : Ureal;
11273             High_Val : Ureal;
11274
11275          begin
11276             Analyze_And_Resolve (Low, Any_Real);
11277             Analyze_And_Resolve (High, Any_Real);
11278             Check_Real_Bound (Low);
11279             Check_Real_Bound (High);
11280             Low_Val := Expr_Value_R (Low);
11281             High_Val := Expr_Value_R (High);
11282
11283             if Low_Val < (-Bound_Val) then
11284                Error_Msg_N
11285                  ("range low bound too small for digits value", Low);
11286                Low_Val := -Bound_Val;
11287             end if;
11288
11289             if High_Val > Bound_Val then
11290                Error_Msg_N
11291                  ("range high bound too large for digits value", High);
11292                High_Val := Bound_Val;
11293             end if;
11294
11295             Set_Fixed_Range (T, Loc, Low_Val, High_Val);
11296          end;
11297
11298       --  If no explicit range, use range that corresponds to given
11299       --  digits value. This will end up as the final range for the
11300       --  first subtype.
11301
11302       else
11303          Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
11304       end if;
11305
11306       --  Complete entity for first subtype
11307
11308       Set_Ekind          (T, E_Decimal_Fixed_Point_Subtype);
11309       Set_Etype          (T, Implicit_Base);
11310       Set_Size_Info      (T, Implicit_Base);
11311       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
11312       Set_Digits_Value   (T, Digs_Val);
11313       Set_Delta_Value    (T, Delta_Val);
11314       Set_Small_Value    (T, Delta_Val);
11315       Set_Scale_Value    (T, Scale_Val);
11316       Set_Is_Constrained (T);
11317    end Decimal_Fixed_Point_Type_Declaration;
11318
11319    -----------------------------------
11320    -- Derive_Progenitor_Subprograms --
11321    -----------------------------------
11322
11323    procedure Derive_Progenitor_Subprograms
11324      (Parent_Type : Entity_Id;
11325       Tagged_Type : Entity_Id)
11326    is
11327       E          : Entity_Id;
11328       Elmt       : Elmt_Id;
11329       Iface      : Entity_Id;
11330       Iface_Elmt : Elmt_Id;
11331       Iface_Subp : Entity_Id;
11332       New_Subp   : Entity_Id := Empty;
11333       Prim_Elmt  : Elmt_Id;
11334       Subp       : Entity_Id;
11335       Typ        : Entity_Id;
11336
11337    begin
11338       pragma Assert (Ada_Version >= Ada_05
11339         and then Is_Record_Type (Tagged_Type)
11340         and then Is_Tagged_Type (Tagged_Type)
11341         and then Has_Interfaces (Tagged_Type));
11342
11343       --  Step 1: Transfer to the full-view primitives asociated with the
11344       --  partial-view that cover interface primitives. Conceptually this
11345       --  work should be done later by Process_Full_View; done here to
11346       --  simplify its implementation at later stages. It can be safely
11347       --  done here because interfaces must be visible in the partial and
11348       --  private view (RM 7.3(7.3/2)).
11349
11350       --  Small optimization: This work is only required if the parent is
11351       --  abstract. If the tagged type is not abstract, it cannot have
11352       --  abstract primitives (the only entities in the list of primitives of
11353       --  non-abstract tagged types that can reference abstract primitives
11354       --  through its Alias attribute are the internal entities that have
11355       --  attribute Interface_Alias, and these entities are generated later
11356       --  by Freeze_Record_Type).
11357
11358       if In_Private_Part (Current_Scope)
11359         and then Is_Abstract_Type (Parent_Type)
11360       then
11361          Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
11362          while Present (Elmt) loop
11363             Subp := Node (Elmt);
11364
11365             --  At this stage it is not possible to have entities in the list
11366             --  of primitives that have attribute Interface_Alias
11367
11368             pragma Assert (No (Interface_Alias (Subp)));
11369
11370             Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
11371
11372             if Is_Interface (Typ) then
11373                E := Find_Primitive_Covering_Interface
11374                       (Tagged_Type => Tagged_Type,
11375                        Iface_Prim  => Subp);
11376
11377                if Present (E)
11378                  and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
11379                then
11380                   Replace_Elmt (Elmt, E);
11381                   Remove_Homonym (Subp);
11382                end if;
11383             end if;
11384
11385             Next_Elmt (Elmt);
11386          end loop;
11387       end if;
11388
11389       --  Step 2: Add primitives of progenitors that are not implemented by
11390       --  parents of Tagged_Type
11391
11392       if Present (Interfaces (Tagged_Type)) then
11393          Iface_Elmt := First_Elmt (Interfaces (Tagged_Type));
11394          while Present (Iface_Elmt) loop
11395             Iface := Node (Iface_Elmt);
11396
11397             Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
11398             while Present (Prim_Elmt) loop
11399                Iface_Subp := Node (Prim_Elmt);
11400
11401                if not Is_Predefined_Dispatching_Operation (Iface_Subp)
11402                  or else Comes_From_Source (Iface_Subp)
11403                then
11404                   E := Find_Primitive_Covering_Interface
11405                          (Tagged_Type => Tagged_Type,
11406                           Iface_Prim  => Iface_Subp);
11407
11408                   --  If not found we derive a new primitive leaving its alias
11409                   --  attribute referencing the interface primitive
11410
11411                   if No (E) then
11412                      Derive_Subprogram
11413                        (New_Subp, Iface_Subp, Tagged_Type, Iface);
11414
11415                   --  Propagate to the full view interface entities associated
11416                   --  with the partial view
11417
11418                   elsif In_Private_Part (Current_Scope)
11419                     and then Present (Alias (E))
11420                     and then Alias (E) = Iface_Subp
11421                     and then
11422                       List_Containing (Parent (E)) /=
11423                         Private_Declarations
11424                           (Specification
11425                             (Unit_Declaration_Node (Current_Scope)))
11426                   then
11427                      Append_Elmt (E, Primitive_Operations (Tagged_Type));
11428                   end if;
11429                end if;
11430
11431                Next_Elmt (Prim_Elmt);
11432             end loop;
11433
11434             Next_Elmt (Iface_Elmt);
11435          end loop;
11436       end if;
11437    end Derive_Progenitor_Subprograms;
11438
11439    -----------------------
11440    -- Derive_Subprogram --
11441    -----------------------
11442
11443    procedure Derive_Subprogram
11444      (New_Subp     : in out Entity_Id;
11445       Parent_Subp  : Entity_Id;
11446       Derived_Type : Entity_Id;
11447       Parent_Type  : Entity_Id;
11448       Actual_Subp  : Entity_Id := Empty)
11449    is
11450       Formal : Entity_Id;
11451       --  Formal parameter of parent primitive operation
11452
11453       Formal_Of_Actual : Entity_Id;
11454       --  Formal parameter of actual operation, when the derivation is to
11455       --  create a renaming for a primitive operation of an actual in an
11456       --  instantiation.
11457
11458       New_Formal : Entity_Id;
11459       --  Formal of inherited operation
11460
11461       Visible_Subp : Entity_Id := Parent_Subp;
11462
11463       function Is_Private_Overriding return Boolean;
11464       --  If Subp is a private overriding of a visible operation, the inherited
11465       --  operation derives from the overridden op (even though its body is the
11466       --  overriding one) and the inherited operation is visible now. See
11467       --  sem_disp to see the full details of the handling of the overridden
11468       --  subprogram, which is removed from the list of primitive operations of
11469       --  the type. The overridden subprogram is saved locally in Visible_Subp,
11470       --  and used to diagnose abstract operations that need overriding in the
11471       --  derived type.
11472
11473       procedure Replace_Type (Id, New_Id : Entity_Id);
11474       --  When the type is an anonymous access type, create a new access type
11475       --  designating the derived type.
11476
11477       procedure Set_Derived_Name;
11478       --  This procedure sets the appropriate Chars name for New_Subp. This
11479       --  is normally just a copy of the parent name. An exception arises for
11480       --  type support subprograms, where the name is changed to reflect the
11481       --  name of the derived type, e.g. if type foo is derived from type bar,
11482       --  then a procedure barDA is derived with a name fooDA.
11483
11484       ---------------------------
11485       -- Is_Private_Overriding --
11486       ---------------------------
11487
11488       function Is_Private_Overriding return Boolean is
11489          Prev : Entity_Id;
11490
11491       begin
11492          --  If the parent is not a dispatching operation there is no
11493          --  need to investigate overridings
11494
11495          if not Is_Dispatching_Operation (Parent_Subp) then
11496             return False;
11497          end if;
11498
11499          --  The visible operation that is overridden is a homonym of the
11500          --  parent subprogram. We scan the homonym chain to find the one
11501          --  whose alias is the subprogram we are deriving.
11502
11503          Prev := Current_Entity (Parent_Subp);
11504          while Present (Prev) loop
11505             if Ekind (Prev) = Ekind (Parent_Subp)
11506               and then Alias (Prev) = Parent_Subp
11507               and then Scope (Parent_Subp) = Scope (Prev)
11508               and then not Is_Hidden (Prev)
11509             then
11510                Visible_Subp := Prev;
11511                return True;
11512             end if;
11513
11514             Prev := Homonym (Prev);
11515          end loop;
11516
11517          return False;
11518       end Is_Private_Overriding;
11519
11520       ------------------
11521       -- Replace_Type --
11522       ------------------
11523
11524       procedure Replace_Type (Id, New_Id : Entity_Id) is
11525          Acc_Type : Entity_Id;
11526          Par      : constant Node_Id := Parent (Derived_Type);
11527
11528       begin
11529          --  When the type is an anonymous access type, create a new access
11530          --  type designating the derived type. This itype must be elaborated
11531          --  at the point of the derivation, not on subsequent calls that may
11532          --  be out of the proper scope for Gigi, so we insert a reference to
11533          --  it after the derivation.
11534
11535          if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
11536             declare
11537                Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
11538
11539             begin
11540                if Ekind (Desig_Typ) = E_Record_Type_With_Private
11541                  and then Present (Full_View (Desig_Typ))
11542                  and then not Is_Private_Type (Parent_Type)
11543                then
11544                   Desig_Typ := Full_View (Desig_Typ);
11545                end if;
11546
11547                if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
11548
11549                   --  Ada 2005 (AI-251): Handle also derivations of abstract
11550                   --  interface primitives.
11551
11552                  or else (Is_Interface (Desig_Typ)
11553                           and then not Is_Class_Wide_Type (Desig_Typ))
11554                then
11555                   Acc_Type := New_Copy (Etype (Id));
11556                   Set_Etype (Acc_Type, Acc_Type);
11557                   Set_Scope (Acc_Type, New_Subp);
11558
11559                   --  Compute size of anonymous access type
11560
11561                   if Is_Array_Type (Desig_Typ)
11562                     and then not Is_Constrained (Desig_Typ)
11563                   then
11564                      Init_Size (Acc_Type, 2 * System_Address_Size);
11565                   else
11566                      Init_Size (Acc_Type, System_Address_Size);
11567                   end if;
11568
11569                   Init_Alignment (Acc_Type);
11570                   Set_Directly_Designated_Type (Acc_Type, Derived_Type);
11571
11572                   Set_Etype (New_Id, Acc_Type);
11573                   Set_Scope (New_Id, New_Subp);
11574
11575                   --  Create a reference to it
11576                   Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
11577
11578                else
11579                   Set_Etype (New_Id, Etype (Id));
11580                end if;
11581             end;
11582
11583          elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
11584            or else
11585              (Ekind (Etype (Id)) = E_Record_Type_With_Private
11586                and then Present (Full_View (Etype (Id)))
11587                and then
11588                  Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
11589          then
11590             --  Constraint checks on formals are generated during expansion,
11591             --  based on the signature of the original subprogram. The bounds
11592             --  of the derived type are not relevant, and thus we can use
11593             --  the base type for the formals. However, the return type may be
11594             --  used in a context that requires that the proper static bounds
11595             --  be used (a case statement, for example)  and for those cases
11596             --  we must use the derived type (first subtype), not its base.
11597
11598             --  If the derived_type_definition has no constraints, we know that
11599             --  the derived type has the same constraints as the first subtype
11600             --  of the parent, and we can also use it rather than its base,
11601             --  which can lead to more efficient code.
11602
11603             if Etype (Id) = Parent_Type then
11604                if Is_Scalar_Type (Parent_Type)
11605                  and then
11606                    Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
11607                then
11608                   Set_Etype (New_Id, Derived_Type);
11609
11610                elsif Nkind (Par) = N_Full_Type_Declaration
11611                  and then
11612                    Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
11613                  and then
11614                    Is_Entity_Name
11615                      (Subtype_Indication (Type_Definition (Par)))
11616                then
11617                   Set_Etype (New_Id, Derived_Type);
11618
11619                else
11620                   Set_Etype (New_Id, Base_Type (Derived_Type));
11621                end if;
11622
11623             else
11624                Set_Etype (New_Id, Base_Type (Derived_Type));
11625             end if;
11626
11627          --  Ada 2005 (AI-251): Handle derivations of abstract interface
11628          --  primitives.
11629
11630          elsif Is_Interface (Etype (Id))
11631            and then not Is_Class_Wide_Type (Etype (Id))
11632            and then Is_Progenitor (Etype (Id), Derived_Type)
11633          then
11634             Set_Etype (New_Id, Derived_Type);
11635
11636          else
11637             Set_Etype (New_Id, Etype (Id));
11638          end if;
11639       end Replace_Type;
11640
11641       ----------------------
11642       -- Set_Derived_Name --
11643       ----------------------
11644
11645       procedure Set_Derived_Name is
11646          Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
11647       begin
11648          if Nm = TSS_Null then
11649             Set_Chars (New_Subp, Chars (Parent_Subp));
11650          else
11651             Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
11652          end if;
11653       end Set_Derived_Name;
11654
11655       --  Local variables
11656
11657       Parent_Overrides_Interface_Primitive : Boolean := False;
11658
11659    --  Start of processing for Derive_Subprogram
11660
11661    begin
11662       New_Subp :=
11663          New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
11664       Set_Ekind (New_Subp, Ekind (Parent_Subp));
11665
11666       --  Check whether the parent overrides an interface primitive
11667
11668       if Is_Overriding_Operation (Parent_Subp) then
11669          declare
11670             E : Entity_Id := Parent_Subp;
11671          begin
11672             while Present (Overridden_Operation (E)) loop
11673                E := Ultimate_Alias (Overridden_Operation (E));
11674             end loop;
11675
11676             Parent_Overrides_Interface_Primitive :=
11677               Is_Dispatching_Operation (E)
11678                 and then Present (Find_Dispatching_Type (E))
11679                 and then Is_Interface (Find_Dispatching_Type (E));
11680          end;
11681       end if;
11682
11683       --  Check whether the inherited subprogram is a private operation that
11684       --  should be inherited but not yet made visible. Such subprograms can
11685       --  become visible at a later point (e.g., the private part of a public
11686       --  child unit) via Declare_Inherited_Private_Subprograms. If the
11687       --  following predicate is true, then this is not such a private
11688       --  operation and the subprogram simply inherits the name of the parent
11689       --  subprogram. Note the special check for the names of controlled
11690       --  operations, which are currently exempted from being inherited with
11691       --  a hidden name because they must be findable for generation of
11692       --  implicit run-time calls.
11693
11694       if not Is_Hidden (Parent_Subp)
11695         or else Is_Internal (Parent_Subp)
11696         or else Is_Private_Overriding
11697         or else Is_Internal_Name (Chars (Parent_Subp))
11698         or else Chars (Parent_Subp) = Name_Initialize
11699         or else Chars (Parent_Subp) = Name_Adjust
11700         or else Chars (Parent_Subp) = Name_Finalize
11701       then
11702          Set_Derived_Name;
11703
11704       --  If parent is hidden, this can be a regular derivation if the
11705       --  parent is immediately visible in a non-instantiating context,
11706       --  or if we are in the private part of an instance. This test
11707       --  should still be refined ???
11708
11709       --  The test for In_Instance_Not_Visible avoids inheriting the derived
11710       --  operation as a non-visible operation in cases where the parent
11711       --  subprogram might not be visible now, but was visible within the
11712       --  original generic, so it would be wrong to make the inherited
11713       --  subprogram non-visible now. (Not clear if this test is fully
11714       --  correct; are there any cases where we should declare the inherited
11715       --  operation as not visible to avoid it being overridden, e.g., when
11716       --  the parent type is a generic actual with private primitives ???)
11717
11718       --  (they should be treated the same as other private inherited
11719       --  subprograms, but it's not clear how to do this cleanly). ???
11720
11721       elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
11722               and then Is_Immediately_Visible (Parent_Subp)
11723               and then not In_Instance)
11724         or else In_Instance_Not_Visible
11725       then
11726          Set_Derived_Name;
11727
11728       --  Ada 2005 (AI-251): Regular derivation if the parent subprogram
11729       --  overrides an interface primitive because interface primitives
11730       --  must be visible in the partial view of the parent (RM 7.3 (7.3/2))
11731
11732       elsif Parent_Overrides_Interface_Primitive then
11733          Set_Derived_Name;
11734
11735       --  The type is inheriting a private operation, so enter
11736       --  it with a special name so it can't be overridden.
11737
11738       else
11739          Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
11740       end if;
11741
11742       Set_Parent (New_Subp, Parent (Derived_Type));
11743
11744       if Present (Actual_Subp) then
11745          Replace_Type (Actual_Subp, New_Subp);
11746       else
11747          Replace_Type (Parent_Subp, New_Subp);
11748       end if;
11749
11750       Conditional_Delay (New_Subp, Parent_Subp);
11751
11752       --  If we are creating a renaming for a primitive operation of an
11753       --  actual of a generic derived type, we must examine the signature
11754       --  of the actual primitive, not that of the generic formal, which for
11755       --  example may be an interface. However the name and initial value
11756       --  of the inherited operation are those of the formal primitive.
11757
11758       Formal := First_Formal (Parent_Subp);
11759
11760       if Present (Actual_Subp) then
11761          Formal_Of_Actual := First_Formal (Actual_Subp);
11762       else
11763          Formal_Of_Actual := Empty;
11764       end if;
11765
11766       while Present (Formal) loop
11767          New_Formal := New_Copy (Formal);
11768
11769          --  Normally we do not go copying parents, but in the case of
11770          --  formals, we need to link up to the declaration (which is the
11771          --  parameter specification), and it is fine to link up to the
11772          --  original formal's parameter specification in this case.
11773
11774          Set_Parent (New_Formal, Parent (Formal));
11775          Append_Entity (New_Formal, New_Subp);
11776
11777          if Present (Formal_Of_Actual) then
11778             Replace_Type (Formal_Of_Actual, New_Formal);
11779             Next_Formal (Formal_Of_Actual);
11780          else
11781             Replace_Type (Formal, New_Formal);
11782          end if;
11783
11784          Next_Formal (Formal);
11785       end loop;
11786
11787       --  If this derivation corresponds to a tagged generic actual, then
11788       --  primitive operations rename those of the actual. Otherwise the
11789       --  primitive operations rename those of the parent type, If the parent
11790       --  renames an intrinsic operator, so does the new subprogram. We except
11791       --  concatenation, which is always properly typed, and does not get
11792       --  expanded as other intrinsic operations.
11793
11794       if No (Actual_Subp) then
11795          if Is_Intrinsic_Subprogram (Parent_Subp) then
11796             Set_Is_Intrinsic_Subprogram (New_Subp);
11797
11798             if Present (Alias (Parent_Subp))
11799               and then Chars (Parent_Subp) /= Name_Op_Concat
11800             then
11801                Set_Alias (New_Subp, Alias (Parent_Subp));
11802             else
11803                Set_Alias (New_Subp, Parent_Subp);
11804             end if;
11805
11806          else
11807             Set_Alias (New_Subp, Parent_Subp);
11808          end if;
11809
11810       else
11811          Set_Alias (New_Subp, Actual_Subp);
11812       end if;
11813
11814       --  Derived subprograms of a tagged type must inherit the convention
11815       --  of the parent subprogram (a requirement of AI-117). Derived
11816       --  subprograms of untagged types simply get convention Ada by default.
11817
11818       if Is_Tagged_Type (Derived_Type) then
11819          Set_Convention (New_Subp, Convention (Parent_Subp));
11820       end if;
11821
11822       Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
11823       Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
11824
11825       if Ekind (Parent_Subp) = E_Procedure then
11826          Set_Is_Valued_Procedure
11827            (New_Subp, Is_Valued_Procedure (Parent_Subp));
11828       end if;
11829
11830       --  No_Return must be inherited properly. If this is overridden in the
11831       --  case of a dispatching operation, then a check is made in Sem_Disp
11832       --  that the overriding operation is also No_Return (no such check is
11833       --  required for the case of non-dispatching operation.
11834
11835       Set_No_Return (New_Subp, No_Return (Parent_Subp));
11836
11837       --  A derived function with a controlling result is abstract. If the
11838       --  Derived_Type is a nonabstract formal generic derived type, then
11839       --  inherited operations are not abstract: the required check is done at
11840       --  instantiation time. If the derivation is for a generic actual, the
11841       --  function is not abstract unless the actual is.
11842
11843       if Is_Generic_Type (Derived_Type)
11844         and then not Is_Abstract_Type (Derived_Type)
11845       then
11846          null;
11847
11848       --  Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
11849       --  properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
11850
11851       elsif Ada_Version >= Ada_05
11852         and then (Is_Abstract_Subprogram (Alias (New_Subp))
11853                    or else (Is_Tagged_Type (Derived_Type)
11854                             and then Etype (New_Subp) = Derived_Type
11855                             and then not Is_Null_Extension (Derived_Type))
11856                    or else (Is_Tagged_Type (Derived_Type)
11857                             and then Ekind (Etype (New_Subp)) =
11858                                                        E_Anonymous_Access_Type
11859                             and then Designated_Type (Etype (New_Subp)) =
11860                                                        Derived_Type
11861                             and then not Is_Null_Extension (Derived_Type)))
11862         and then No (Actual_Subp)
11863       then
11864          if not Is_Tagged_Type (Derived_Type)
11865            or else Is_Abstract_Type (Derived_Type)
11866            or else Is_Abstract_Subprogram (Alias (New_Subp))
11867          then
11868             Set_Is_Abstract_Subprogram (New_Subp);
11869          else
11870             Set_Requires_Overriding (New_Subp);
11871          end if;
11872
11873       elsif Ada_Version < Ada_05
11874         and then (Is_Abstract_Subprogram (Alias (New_Subp))
11875                    or else (Is_Tagged_Type (Derived_Type)
11876                              and then Etype (New_Subp) = Derived_Type
11877                              and then No (Actual_Subp)))
11878       then
11879          Set_Is_Abstract_Subprogram (New_Subp);
11880
11881       --  Finally, if the parent type is abstract we must verify that all
11882       --  inherited operations are either non-abstract or overridden, or that
11883       --  the derived type itself is abstract (this check is performed at the
11884       --  end of a package declaration, in Check_Abstract_Overriding). A
11885       --  private overriding in the parent type will not be visible in the
11886       --  derivation if we are not in an inner package or in a child unit of
11887       --  the parent type, in which case the abstractness of the inherited
11888       --  operation is carried to the new subprogram.
11889
11890       elsif Is_Abstract_Type (Parent_Type)
11891         and then not In_Open_Scopes (Scope (Parent_Type))
11892         and then Is_Private_Overriding
11893         and then Is_Abstract_Subprogram (Visible_Subp)
11894       then
11895          if No (Actual_Subp) then
11896             Set_Alias (New_Subp, Visible_Subp);
11897             Set_Is_Abstract_Subprogram
11898               (New_Subp, True);
11899          else
11900             --  If this is a derivation for an instance of a formal derived
11901             --  type, abstractness comes from the primitive operation of the
11902             --  actual, not from the operation inherited from the ancestor.
11903
11904             Set_Is_Abstract_Subprogram
11905               (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
11906          end if;
11907       end if;
11908
11909       New_Overloaded_Entity (New_Subp, Derived_Type);
11910
11911       --  Check for case of a derived subprogram for the instantiation of a
11912       --  formal derived tagged type, if so mark the subprogram as dispatching
11913       --  and inherit the dispatching attributes of the parent subprogram. The
11914       --  derived subprogram is effectively renaming of the actual subprogram,
11915       --  so it needs to have the same attributes as the actual.
11916
11917       if Present (Actual_Subp)
11918         and then Is_Dispatching_Operation (Parent_Subp)
11919       then
11920          Set_Is_Dispatching_Operation (New_Subp);
11921
11922          if Present (DTC_Entity (Parent_Subp)) then
11923             Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
11924             Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
11925          end if;
11926       end if;
11927
11928       --  Indicate that a derived subprogram does not require a body and that
11929       --  it does not require processing of default expressions.
11930
11931       Set_Has_Completion (New_Subp);
11932       Set_Default_Expressions_Processed (New_Subp);
11933
11934       if Ekind (New_Subp) = E_Function then
11935          Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
11936       end if;
11937    end Derive_Subprogram;
11938
11939    ------------------------
11940    -- Derive_Subprograms --
11941    ------------------------
11942
11943    procedure Derive_Subprograms
11944      (Parent_Type    : Entity_Id;
11945       Derived_Type   : Entity_Id;
11946       Generic_Actual : Entity_Id := Empty)
11947    is
11948       Op_List : constant Elist_Id :=
11949                   Collect_Primitive_Operations (Parent_Type);
11950
11951       function Check_Derived_Type return Boolean;
11952       --  Check that all primitive inherited from Parent_Type are found in
11953       --  the list of primitives of Derived_Type exactly in the same order.
11954
11955       function Check_Derived_Type return Boolean is
11956          E        : Entity_Id;
11957          Elmt     : Elmt_Id;
11958          List     : Elist_Id;
11959          New_Subp : Entity_Id;
11960          Op_Elmt  : Elmt_Id;
11961          Subp     : Entity_Id;
11962
11963       begin
11964          --  Traverse list of entities in the current scope searching for
11965          --  an incomplete type whose full-view is derived type
11966
11967          E := First_Entity (Scope (Derived_Type));
11968          while Present (E)
11969            and then E /= Derived_Type
11970          loop
11971             if Ekind (E) = E_Incomplete_Type
11972               and then Present (Full_View (E))
11973               and then Full_View (E) = Derived_Type
11974             then
11975                --  Disable this test if Derived_Type completes an incomplete
11976                --  type because in such case more primitives can be added
11977                --  later to the list of primitives of Derived_Type by routine
11978                --  Process_Incomplete_Dependents
11979
11980                return True;
11981             end if;
11982
11983             E := Next_Entity (E);
11984          end loop;
11985
11986          List := Collect_Primitive_Operations (Derived_Type);
11987          Elmt := First_Elmt (List);
11988
11989          Op_Elmt := First_Elmt (Op_List);
11990          while Present (Op_Elmt) loop
11991             Subp     := Node (Op_Elmt);
11992             New_Subp := Node (Elmt);
11993
11994             --  At this early stage Derived_Type has no entities with attribute
11995             --  Interface_Alias. In addition, such primitives are always
11996             --  located at the end of the list of primitives of Parent_Type.
11997             --  Therefore, if found we can safely stop processing pending
11998             --  entities.
11999
12000             exit when Present (Interface_Alias (Subp));
12001
12002             --  Handle hidden entities
12003
12004             if not Is_Predefined_Dispatching_Operation (Subp)
12005               and then Is_Hidden (Subp)
12006             then
12007                if Present (New_Subp)
12008                  and then Primitive_Names_Match (Subp, New_Subp)
12009                then
12010                   Next_Elmt (Elmt);
12011                end if;
12012
12013             else
12014                if not Present (New_Subp)
12015                  or else Ekind (Subp) /= Ekind (New_Subp)
12016                  or else not Primitive_Names_Match (Subp, New_Subp)
12017                then
12018                   return False;
12019                end if;
12020
12021                Next_Elmt (Elmt);
12022             end if;
12023
12024             Next_Elmt (Op_Elmt);
12025          end loop;
12026
12027          return True;
12028       end Check_Derived_Type;
12029
12030       --  Local variables
12031
12032       Alias_Subp   : Entity_Id;
12033       Act_List     : Elist_Id;
12034       Act_Elmt     : Elmt_Id   := No_Elmt;
12035       Act_Subp     : Entity_Id := Empty;
12036       Elmt         : Elmt_Id;
12037       Need_Search  : Boolean   := False;
12038       New_Subp     : Entity_Id := Empty;
12039       Parent_Base  : Entity_Id;
12040       Subp         : Entity_Id;
12041
12042    --  Start of processing for Derive_Subprograms
12043
12044    begin
12045       if Ekind (Parent_Type) = E_Record_Type_With_Private
12046         and then Has_Discriminants (Parent_Type)
12047         and then Present (Full_View (Parent_Type))
12048       then
12049          Parent_Base := Full_View (Parent_Type);
12050       else
12051          Parent_Base := Parent_Type;
12052       end if;
12053
12054       if Present (Generic_Actual) then
12055          Act_List := Collect_Primitive_Operations (Generic_Actual);
12056          Act_Elmt := First_Elmt (Act_List);
12057       end if;
12058
12059       --  Derive primitives inherited from the parent. Note that if the generic
12060       --  actual is present, this is not really a type derivation, it is a
12061       --  completion within an instance.
12062
12063       --  Case 1: Derived_Type does not implement interfaces
12064
12065       if not Is_Tagged_Type (Derived_Type)
12066         or else (not Has_Interfaces (Derived_Type)
12067                   and then not (Present (Generic_Actual)
12068                                   and then
12069                                 Has_Interfaces (Generic_Actual)))
12070       then
12071          Elmt := First_Elmt (Op_List);
12072          while Present (Elmt) loop
12073             Subp := Node (Elmt);
12074
12075             --  Literals are derived earlier in the process of building the
12076             --  derived type, and are skipped here.
12077
12078             if Ekind (Subp) = E_Enumeration_Literal then
12079                null;
12080
12081             --  The actual is a direct descendant and the common primitive
12082             --  operations appear in the same order.
12083
12084             --  If the generic parent type is present, the derived type is an
12085             --  instance of a formal derived type, and within the instance its
12086             --  operations are those of the actual. We derive from the formal
12087             --  type but make the inherited operations aliases of the
12088             --  corresponding operations of the actual.
12089
12090             else
12091                Derive_Subprogram
12092                  (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
12093
12094                if Present (Act_Elmt) then
12095                   Next_Elmt (Act_Elmt);
12096                end if;
12097             end if;
12098
12099             Next_Elmt (Elmt);
12100          end loop;
12101
12102       --  Case 2: Derived_Type implements interfaces
12103
12104       else
12105          --  If the parent type has no predefined primitives we remove
12106          --  predefined primitives from the list of primitives of generic
12107          --  actual to simplify the complexity of this algorithm.
12108
12109          if Present (Generic_Actual) then
12110             declare
12111                Has_Predefined_Primitives : Boolean := False;
12112
12113             begin
12114                --  Check if the parent type has predefined primitives
12115
12116                Elmt := First_Elmt (Op_List);
12117                while Present (Elmt) loop
12118                   Subp := Node (Elmt);
12119
12120                   if Is_Predefined_Dispatching_Operation (Subp)
12121                     and then not Comes_From_Source (Ultimate_Alias (Subp))
12122                   then
12123                      Has_Predefined_Primitives := True;
12124                      exit;
12125                   end if;
12126
12127                   Next_Elmt (Elmt);
12128                end loop;
12129
12130                --  Remove predefined primitives of Generic_Actual. We must use
12131                --  an auxiliary list because in case of tagged types the value
12132                --  returned by Collect_Primitive_Operations is the value stored
12133                --  in its Primitive_Operations attribute (and we don't want to
12134                --  modify its current contents).
12135
12136                if not Has_Predefined_Primitives then
12137                   declare
12138                      Aux_List : constant Elist_Id := New_Elmt_List;
12139
12140                   begin
12141                      Elmt := First_Elmt (Act_List);
12142                      while Present (Elmt) loop
12143                         Subp := Node (Elmt);
12144
12145                         if not Is_Predefined_Dispatching_Operation (Subp)
12146                           or else Comes_From_Source (Subp)
12147                         then
12148                            Append_Elmt (Subp, Aux_List);
12149                         end if;
12150
12151                         Next_Elmt (Elmt);
12152                      end loop;
12153
12154                      Act_List := Aux_List;
12155                   end;
12156                end if;
12157
12158                Act_Elmt := First_Elmt (Act_List);
12159                Act_Subp := Node (Act_Elmt);
12160             end;
12161          end if;
12162
12163          --  Stage 1: If the generic actual is not present we derive the
12164          --  primitives inherited from the parent type. If the generic parent
12165          --  type is present, the derived type is an instance of a formal
12166          --  derived type, and within the instance its operations are those of
12167          --  the actual. We derive from the formal type but make the inherited
12168          --  operations aliases of the corresponding operations of the actual.
12169
12170          Elmt := First_Elmt (Op_List);
12171          while Present (Elmt) loop
12172             Subp       := Node (Elmt);
12173             Alias_Subp := Ultimate_Alias (Subp);
12174
12175             --  At this early stage Derived_Type has no entities with attribute
12176             --  Interface_Alias. In addition, such primitives are always
12177             --  located at the end of the list of primitives of Parent_Type.
12178             --  Therefore, if found we can safely stop processing pending
12179             --  entities.
12180
12181             exit when Present (Interface_Alias (Subp));
12182
12183             --  If the generic actual is present find the corresponding
12184             --  operation in the generic actual. If the parent type is a
12185             --  direct ancestor of the derived type then, even if it is an
12186             --  interface, the operations are inherited from the primary
12187             --  dispatch table and are in the proper order. If we detect here
12188             --  that primitives are not in the same order we traverse the list
12189             --  of primitive operations of the actual to find the one that
12190             --  implements the interface primitive.
12191
12192             if Need_Search
12193               or else
12194                 (Present (Generic_Actual)
12195                    and then Present (Act_Subp)
12196                    and then not Primitive_Names_Match (Subp, Act_Subp))
12197             then
12198                pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual));
12199                pragma Assert (Is_Interface (Parent_Base));
12200
12201                --  Remember that we need searching for all the pending
12202                --  primitives
12203
12204                Need_Search := True;
12205
12206                --  Handle entities associated with interface primitives
12207
12208                if Present (Alias (Subp))
12209                  and then Is_Interface (Find_Dispatching_Type (Alias (Subp)))
12210                  and then not Is_Predefined_Dispatching_Operation (Subp)
12211                then
12212                   Act_Subp :=
12213                     Find_Primitive_Covering_Interface
12214                       (Tagged_Type => Generic_Actual,
12215                        Iface_Prim  => Subp);
12216
12217                --  Handle predefined primitives plus the rest of user-defined
12218                --  primitives
12219
12220                else
12221                   Act_Elmt := First_Elmt (Act_List);
12222                   while Present (Act_Elmt) loop
12223                      Act_Subp := Node (Act_Elmt);
12224
12225                      exit when Primitive_Names_Match (Subp, Act_Subp)
12226                        and then Type_Conformant (Subp, Act_Subp,
12227                                   Skip_Controlling_Formals => True)
12228                        and then No (Interface_Alias (Act_Subp));
12229
12230                      Next_Elmt (Act_Elmt);
12231                   end loop;
12232                end if;
12233             end if;
12234
12235             --   Case 1: If the parent is a limited interface then it has the
12236             --   predefined primitives of synchronized interfaces. However, the
12237             --   actual type may be a non-limited type and hence it does not
12238             --   have such primitives.
12239
12240             if Present (Generic_Actual)
12241               and then not Present (Act_Subp)
12242               and then Is_Limited_Interface (Parent_Base)
12243               and then Is_Predefined_Interface_Primitive (Subp)
12244             then
12245                null;
12246
12247             --  Case 2: Inherit entities associated with interfaces that
12248             --  were not covered by the parent type. We exclude here null
12249             --  interface primitives because they do not need special
12250             --  management.
12251
12252             elsif Present (Alias (Subp))
12253               and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
12254               and then not
12255                 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
12256                    and then Null_Present (Parent (Alias_Subp)))
12257             then
12258                Derive_Subprogram
12259                  (New_Subp     => New_Subp,
12260                   Parent_Subp  => Alias_Subp,
12261                   Derived_Type => Derived_Type,
12262                   Parent_Type  => Find_Dispatching_Type (Alias_Subp),
12263                   Actual_Subp  => Act_Subp);
12264
12265                if No (Generic_Actual) then
12266                   Set_Alias (New_Subp, Subp);
12267                end if;
12268
12269             --  Case 3: Common derivation
12270
12271             else
12272                Derive_Subprogram
12273                  (New_Subp     => New_Subp,
12274                   Parent_Subp  => Subp,
12275                   Derived_Type => Derived_Type,
12276                   Parent_Type  => Parent_Base,
12277                   Actual_Subp  => Act_Subp);
12278             end if;
12279
12280             --  No need to update Act_Elm if we must search for the
12281             --  corresponding operation in the generic actual
12282
12283             if not Need_Search
12284               and then Present (Act_Elmt)
12285             then
12286                Next_Elmt (Act_Elmt);
12287                Act_Subp := Node (Act_Elmt);
12288             end if;
12289
12290             Next_Elmt (Elmt);
12291          end loop;
12292
12293          --  Inherit additional operations from progenitors. If the derived
12294          --  type is a generic actual, there are not new primitive operations
12295          --  for the type because it has those of the actual, and therefore
12296          --  nothing needs to be done. The renamings generated above are not
12297          --  primitive operations, and their purpose is simply to make the
12298          --  proper operations visible within an instantiation.
12299
12300          if No (Generic_Actual) then
12301             Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
12302          end if;
12303       end if;
12304
12305       --  Final check: Direct descendants must have their primitives in the
12306       --  same order. We exclude from this test non-tagged types and instances
12307       --  of formal derived types. We skip this test if we have already
12308       --  reported serious errors in the sources.
12309
12310       pragma Assert (not Is_Tagged_Type (Derived_Type)
12311         or else Present (Generic_Actual)
12312         or else Serious_Errors_Detected > 0
12313         or else Check_Derived_Type);
12314    end Derive_Subprograms;
12315
12316    --------------------------------
12317    -- Derived_Standard_Character --
12318    --------------------------------
12319
12320    procedure Derived_Standard_Character
12321      (N            : Node_Id;
12322       Parent_Type  : Entity_Id;
12323       Derived_Type : Entity_Id)
12324    is
12325       Loc           : constant Source_Ptr := Sloc (N);
12326       Def           : constant Node_Id    := Type_Definition (N);
12327       Indic         : constant Node_Id    := Subtype_Indication (Def);
12328       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
12329       Implicit_Base : constant Entity_Id  :=
12330                         Create_Itype
12331                           (E_Enumeration_Type, N, Derived_Type, 'B');
12332
12333       Lo : Node_Id;
12334       Hi : Node_Id;
12335
12336    begin
12337       Discard_Node (Process_Subtype (Indic, N));
12338
12339       Set_Etype     (Implicit_Base, Parent_Base);
12340       Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
12341       Set_RM_Size   (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
12342
12343       Set_Is_Character_Type  (Implicit_Base, True);
12344       Set_Has_Delayed_Freeze (Implicit_Base);
12345
12346       --  The bounds of the implicit base are the bounds of the parent base.
12347       --  Note that their type is the parent base.
12348
12349       Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Base));
12350       Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
12351
12352       Set_Scalar_Range (Implicit_Base,
12353         Make_Range (Loc,
12354           Low_Bound  => Lo,
12355           High_Bound => Hi));
12356
12357       Conditional_Delay (Derived_Type, Parent_Type);
12358
12359       Set_Ekind (Derived_Type, E_Enumeration_Subtype);
12360       Set_Etype (Derived_Type, Implicit_Base);
12361       Set_Size_Info         (Derived_Type, Parent_Type);
12362
12363       if Unknown_RM_Size (Derived_Type) then
12364          Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
12365       end if;
12366
12367       Set_Is_Character_Type (Derived_Type, True);
12368
12369       if Nkind (Indic) /= N_Subtype_Indication then
12370
12371          --  If no explicit constraint, the bounds are those
12372          --  of the parent type.
12373
12374          Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Type));
12375          Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
12376          Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
12377       end if;
12378
12379       Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
12380
12381       --  Because the implicit base is used in the conversion of the bounds, we
12382       --  have to freeze it now. This is similar to what is done for numeric
12383       --  types, and it equally suspicious, but otherwise a non-static bound
12384       --  will have a reference to an unfrozen type, which is rejected by Gigi
12385       --  (???). This requires specific care for definition of stream
12386       --  attributes. For details, see comments at the end of
12387       --  Build_Derived_Numeric_Type.
12388
12389       Freeze_Before (N, Implicit_Base);
12390    end Derived_Standard_Character;
12391
12392    ------------------------------
12393    -- Derived_Type_Declaration --
12394    ------------------------------
12395
12396    procedure Derived_Type_Declaration
12397      (T             : Entity_Id;
12398       N             : Node_Id;
12399       Is_Completion : Boolean)
12400    is
12401       Parent_Type  : Entity_Id;
12402
12403       function Comes_From_Generic (Typ : Entity_Id) return Boolean;
12404       --  Check whether the parent type is a generic formal, or derives
12405       --  directly or indirectly from one.
12406
12407       ------------------------
12408       -- Comes_From_Generic --
12409       ------------------------
12410
12411       function Comes_From_Generic (Typ : Entity_Id) return Boolean is
12412       begin
12413          if Is_Generic_Type (Typ) then
12414             return True;
12415
12416          elsif Is_Generic_Type (Root_Type (Parent_Type)) then
12417             return True;
12418
12419          elsif Is_Private_Type (Typ)
12420            and then Present (Full_View (Typ))
12421            and then Is_Generic_Type (Root_Type (Full_View (Typ)))
12422          then
12423             return True;
12424
12425          elsif Is_Generic_Actual_Type (Typ) then
12426             return True;
12427
12428          else
12429             return False;
12430          end if;
12431       end Comes_From_Generic;
12432
12433       --  Local variables
12434
12435       Def          : constant Node_Id := Type_Definition (N);
12436       Iface_Def    : Node_Id;
12437       Indic        : constant Node_Id := Subtype_Indication (Def);
12438       Extension    : constant Node_Id := Record_Extension_Part (Def);
12439       Parent_Node  : Node_Id;
12440       Parent_Scope : Entity_Id;
12441       Taggd        : Boolean;
12442
12443    --  Start of processing for Derived_Type_Declaration
12444
12445    begin
12446       Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
12447
12448       --  Ada 2005 (AI-251): In case of interface derivation check that the
12449       --  parent is also an interface.
12450
12451       if Interface_Present (Def) then
12452          if not Is_Interface (Parent_Type) then
12453             Diagnose_Interface (Indic, Parent_Type);
12454
12455          else
12456             Parent_Node := Parent (Base_Type (Parent_Type));
12457             Iface_Def   := Type_Definition (Parent_Node);
12458
12459             --  Ada 2005 (AI-251): Limited interfaces can only inherit from
12460             --  other limited interfaces.
12461
12462             if Limited_Present (Def) then
12463                if Limited_Present (Iface_Def) then
12464                   null;
12465
12466                elsif Protected_Present (Iface_Def) then
12467                   Error_Msg_N
12468                     ("(Ada 2005) limited interface cannot "
12469                      & "inherit from protected interface", Indic);
12470
12471                elsif Synchronized_Present (Iface_Def) then
12472                   Error_Msg_N
12473                     ("(Ada 2005) limited interface cannot "
12474                      & "inherit from synchronized interface", Indic);
12475
12476                elsif Task_Present (Iface_Def) then
12477                   Error_Msg_N
12478                     ("(Ada 2005) limited interface cannot "
12479                      & "inherit from task interface", Indic);
12480
12481                else
12482                   Error_Msg_N
12483                     ("(Ada 2005) limited interface cannot "
12484                      & "inherit from non-limited interface", Indic);
12485                end if;
12486
12487             --  Ada 2005 (AI-345): Non-limited interfaces can only inherit
12488             --  from non-limited or limited interfaces.
12489
12490             elsif not Protected_Present (Def)
12491               and then not Synchronized_Present (Def)
12492               and then not Task_Present (Def)
12493             then
12494                if Limited_Present (Iface_Def) then
12495                   null;
12496
12497                elsif Protected_Present (Iface_Def) then
12498                   Error_Msg_N
12499                     ("(Ada 2005) non-limited interface cannot "
12500                      & "inherit from protected interface", Indic);
12501
12502                elsif Synchronized_Present (Iface_Def) then
12503                   Error_Msg_N
12504                     ("(Ada 2005) non-limited interface cannot "
12505                      & "inherit from synchronized interface", Indic);
12506
12507                elsif Task_Present (Iface_Def) then
12508                   Error_Msg_N
12509                     ("(Ada 2005) non-limited interface cannot "
12510                      & "inherit from task interface", Indic);
12511
12512                else
12513                   null;
12514                end if;
12515             end if;
12516          end if;
12517       end if;
12518
12519       if Is_Tagged_Type (Parent_Type)
12520         and then Is_Concurrent_Type (Parent_Type)
12521         and then not Is_Interface (Parent_Type)
12522       then
12523          Error_Msg_N
12524            ("parent type of a record extension cannot be "
12525             & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
12526          Set_Etype (T, Any_Type);
12527          return;
12528       end if;
12529
12530       --  Ada 2005 (AI-251): Decorate all the names in the list of ancestor
12531       --  interfaces
12532
12533       if Is_Tagged_Type (Parent_Type)
12534         and then Is_Non_Empty_List (Interface_List (Def))
12535       then
12536          declare
12537             Intf : Node_Id;
12538             T    : Entity_Id;
12539
12540          begin
12541             Intf := First (Interface_List (Def));
12542             while Present (Intf) loop
12543                T := Find_Type_Of_Subtype_Indic (Intf);
12544
12545                if not Is_Interface (T) then
12546                   Diagnose_Interface (Intf, T);
12547
12548                --  Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
12549                --  a limited type from having a nonlimited progenitor.
12550
12551                elsif (Limited_Present (Def)
12552                        or else (not Is_Interface (Parent_Type)
12553                                  and then Is_Limited_Type (Parent_Type)))
12554                  and then not Is_Limited_Interface (T)
12555                then
12556                   Error_Msg_NE
12557                    ("progenitor interface& of limited type must be limited",
12558                      N, T);
12559                end if;
12560
12561                Next (Intf);
12562             end loop;
12563          end;
12564       end if;
12565
12566       if Parent_Type = Any_Type
12567         or else Etype (Parent_Type) = Any_Type
12568         or else (Is_Class_Wide_Type (Parent_Type)
12569                    and then Etype (Parent_Type) = T)
12570       then
12571          --  If Parent_Type is undefined or illegal, make new type into a
12572          --  subtype of Any_Type, and set a few attributes to prevent cascaded
12573          --  errors. If this is a self-definition, emit error now.
12574
12575          if T = Parent_Type
12576            or else T = Etype (Parent_Type)
12577          then
12578             Error_Msg_N ("type cannot be used in its own definition", Indic);
12579          end if;
12580
12581          Set_Ekind        (T, Ekind (Parent_Type));
12582          Set_Etype        (T, Any_Type);
12583          Set_Scalar_Range (T, Scalar_Range (Any_Type));
12584
12585          if Is_Tagged_Type (T) then
12586             Set_Primitive_Operations (T, New_Elmt_List);
12587          end if;
12588
12589          return;
12590       end if;
12591
12592       --  Ada 2005 (AI-251): The case in which the parent of the full-view is
12593       --  an interface is special because the list of interfaces in the full
12594       --  view can be given in any order. For example:
12595
12596       --     type A is interface;
12597       --     type B is interface and A;
12598       --     type D is new B with private;
12599       --   private
12600       --     type D is new A and B with null record; -- 1 --
12601
12602       --  In this case we perform the following transformation of -1-:
12603
12604       --     type D is new B and A with null record;
12605
12606       --  If the parent of the full-view covers the parent of the partial-view
12607       --  we have two possible cases:
12608
12609       --     1) They have the same parent
12610       --     2) The parent of the full-view implements some further interfaces
12611
12612       --  In both cases we do not need to perform the transformation. In the
12613       --  first case the source program is correct and the transformation is
12614       --  not needed; in the second case the source program does not fulfill
12615       --  the no-hidden interfaces rule (AI-396) and the error will be reported
12616       --  later.
12617
12618       --  This transformation not only simplifies the rest of the analysis of
12619       --  this type declaration but also simplifies the correct generation of
12620       --  the object layout to the expander.
12621
12622       if In_Private_Part (Current_Scope)
12623         and then Is_Interface (Parent_Type)
12624       then
12625          declare
12626             Iface               : Node_Id;
12627             Partial_View        : Entity_Id;
12628             Partial_View_Parent : Entity_Id;
12629             New_Iface           : Node_Id;
12630
12631          begin
12632             --  Look for the associated private type declaration
12633
12634             Partial_View := First_Entity (Current_Scope);
12635             loop
12636                exit when No (Partial_View)
12637                  or else (Has_Private_Declaration (Partial_View)
12638                            and then Full_View (Partial_View) = T);
12639
12640                Next_Entity (Partial_View);
12641             end loop;
12642
12643             --  If the partial view was not found then the source code has
12644             --  errors and the transformation is not needed.
12645
12646             if Present (Partial_View) then
12647                Partial_View_Parent := Etype (Partial_View);
12648
12649                --  If the parent of the full-view covers the parent of the
12650                --  partial-view we have nothing else to do.
12651
12652                if Interface_Present_In_Ancestor
12653                     (Parent_Type, Partial_View_Parent)
12654                then
12655                   null;
12656
12657                --  Traverse the list of interfaces of the full-view to look
12658                --  for the parent of the partial-view and perform the tree
12659                --  transformation.
12660
12661                else
12662                   Iface := First (Interface_List (Def));
12663                   while Present (Iface) loop
12664                      if Etype (Iface) = Etype (Partial_View) then
12665                         Rewrite (Subtype_Indication (Def),
12666                           New_Copy (Subtype_Indication
12667                                      (Parent (Partial_View))));
12668
12669                         New_Iface := Make_Identifier (Sloc (N),
12670                                        Chars (Parent_Type));
12671                         Append (New_Iface, Interface_List (Def));
12672
12673                         --  Analyze the transformed code
12674
12675                         Derived_Type_Declaration (T, N, Is_Completion);
12676                         return;
12677                      end if;
12678
12679                      Next (Iface);
12680                   end loop;
12681                end if;
12682             end if;
12683          end;
12684       end if;
12685
12686       --  Only composite types other than array types are allowed to have
12687       --  discriminants.
12688
12689       if Present (Discriminant_Specifications (N))
12690         and then (Is_Elementary_Type (Parent_Type)
12691                   or else Is_Array_Type (Parent_Type))
12692         and then not Error_Posted (N)
12693       then
12694          Error_Msg_N
12695            ("elementary or array type cannot have discriminants",
12696             Defining_Identifier (First (Discriminant_Specifications (N))));
12697          Set_Has_Discriminants (T, False);
12698       end if;
12699
12700       --  In Ada 83, a derived type defined in a package specification cannot
12701       --  be used for further derivation until the end of its visible part.
12702       --  Note that derivation in the private part of the package is allowed.
12703
12704       if Ada_Version = Ada_83
12705         and then Is_Derived_Type (Parent_Type)
12706         and then In_Visible_Part (Scope (Parent_Type))
12707       then
12708          if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
12709             Error_Msg_N
12710               ("(Ada 83): premature use of type for derivation", Indic);
12711          end if;
12712       end if;
12713
12714       --  Check for early use of incomplete or private type
12715
12716       if Ekind (Parent_Type) = E_Void
12717         or else Ekind (Parent_Type) = E_Incomplete_Type
12718       then
12719          Error_Msg_N ("premature derivation of incomplete type", Indic);
12720          return;
12721
12722       elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
12723               and then not Comes_From_Generic (Parent_Type))
12724         or else Has_Private_Component (Parent_Type)
12725       then
12726          --  The ancestor type of a formal type can be incomplete, in which
12727          --  case only the operations of the partial view are available in
12728          --  the generic. Subsequent checks may be required when the full
12729          --  view is analyzed, to verify that derivation from a tagged type
12730          --  has an extension.
12731
12732          if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
12733             null;
12734
12735          elsif No (Underlying_Type (Parent_Type))
12736            or else Has_Private_Component (Parent_Type)
12737          then
12738             Error_Msg_N
12739               ("premature derivation of derived or private type", Indic);
12740
12741             --  Flag the type itself as being in error, this prevents some
12742             --  nasty problems with subsequent uses of the malformed type.
12743
12744             Set_Error_Posted (T);
12745
12746          --  Check that within the immediate scope of an untagged partial
12747          --  view it's illegal to derive from the partial view if the
12748          --  full view is tagged. (7.3(7))
12749
12750          --  We verify that the Parent_Type is a partial view by checking
12751          --  that it is not a Full_Type_Declaration (i.e. a private type or
12752          --  private extension declaration), to distinguish a partial view
12753          --  from  a derivation from a private type which also appears as
12754          --  E_Private_Type.
12755
12756          elsif Present (Full_View (Parent_Type))
12757            and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
12758            and then not Is_Tagged_Type (Parent_Type)
12759            and then Is_Tagged_Type (Full_View (Parent_Type))
12760          then
12761             Parent_Scope := Scope (T);
12762             while Present (Parent_Scope)
12763               and then Parent_Scope /= Standard_Standard
12764             loop
12765                if Parent_Scope = Scope (Parent_Type) then
12766                   Error_Msg_N
12767                     ("premature derivation from type with tagged full view",
12768                      Indic);
12769                end if;
12770
12771                Parent_Scope := Scope (Parent_Scope);
12772             end loop;
12773          end if;
12774       end if;
12775
12776       --  Check that form of derivation is appropriate
12777
12778       Taggd := Is_Tagged_Type (Parent_Type);
12779
12780       --  Perhaps the parent type should be changed to the class-wide type's
12781       --  specific type in this case to prevent cascading errors ???
12782
12783       if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
12784          Error_Msg_N ("parent type must not be a class-wide type", Indic);
12785          return;
12786       end if;
12787
12788       if Present (Extension) and then not Taggd then
12789          Error_Msg_N
12790            ("type derived from untagged type cannot have extension", Indic);
12791
12792       elsif No (Extension) and then Taggd then
12793
12794          --  If this declaration is within a private part (or body) of a
12795          --  generic instantiation then the derivation is allowed (the parent
12796          --  type can only appear tagged in this case if it's a generic actual
12797          --  type, since it would otherwise have been rejected in the analysis
12798          --  of the generic template).
12799
12800          if not Is_Generic_Actual_Type (Parent_Type)
12801            or else In_Visible_Part (Scope (Parent_Type))
12802          then
12803             Error_Msg_N
12804               ("type derived from tagged type must have extension", Indic);
12805          end if;
12806       end if;
12807
12808       --  AI-443: Synchronized formal derived types require a private
12809       --  extension. There is no point in checking the ancestor type or
12810       --  the progenitors since the construct is wrong to begin with.
12811
12812       if Ada_Version >= Ada_05
12813         and then Is_Generic_Type (T)
12814         and then Present (Original_Node (N))
12815       then
12816          declare
12817             Decl : constant Node_Id := Original_Node (N);
12818
12819          begin
12820             if Nkind (Decl) = N_Formal_Type_Declaration
12821               and then Nkind (Formal_Type_Definition (Decl)) =
12822                          N_Formal_Derived_Type_Definition
12823               and then Synchronized_Present (Formal_Type_Definition (Decl))
12824               and then No (Extension)
12825
12826                --  Avoid emitting a duplicate error message
12827
12828               and then not Error_Posted (Indic)
12829             then
12830                Error_Msg_N
12831                  ("synchronized derived type must have extension", N);
12832             end if;
12833          end;
12834       end if;
12835
12836       Build_Derived_Type (N, Parent_Type, T, Is_Completion);
12837
12838       --  AI-419: The parent type of an explicitly limited derived type must
12839       --  be a limited type or a limited interface.
12840
12841       if Limited_Present (Def) then
12842          Set_Is_Limited_Record (T);
12843
12844          if Is_Interface (T) then
12845             Set_Is_Limited_Interface (T);
12846          end if;
12847
12848          if not Is_Limited_Type (Parent_Type)
12849            and then
12850              (not Is_Interface (Parent_Type)
12851                or else not Is_Limited_Interface (Parent_Type))
12852          then
12853             Error_Msg_NE ("parent type& of limited type must be limited",
12854               N, Parent_Type);
12855          end if;
12856       end if;
12857    end Derived_Type_Declaration;
12858
12859    ------------------------
12860    -- Diagnose_Interface --
12861    ------------------------
12862
12863    procedure Diagnose_Interface (N : Node_Id;  E : Entity_Id) is
12864    begin
12865       if not Is_Interface (E)
12866         and then  E /= Any_Type
12867       then
12868          Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
12869       end if;
12870    end Diagnose_Interface;
12871
12872    ----------------------------------
12873    -- Enumeration_Type_Declaration --
12874    ----------------------------------
12875
12876    procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
12877       Ev     : Uint;
12878       L      : Node_Id;
12879       R_Node : Node_Id;
12880       B_Node : Node_Id;
12881
12882    begin
12883       --  Create identifier node representing lower bound
12884
12885       B_Node := New_Node (N_Identifier, Sloc (Def));
12886       L := First (Literals (Def));
12887       Set_Chars (B_Node, Chars (L));
12888       Set_Entity (B_Node,  L);
12889       Set_Etype (B_Node, T);
12890       Set_Is_Static_Expression (B_Node, True);
12891
12892       R_Node := New_Node (N_Range, Sloc (Def));
12893       Set_Low_Bound  (R_Node, B_Node);
12894
12895       Set_Ekind (T, E_Enumeration_Type);
12896       Set_First_Literal (T, L);
12897       Set_Etype (T, T);
12898       Set_Is_Constrained (T);
12899
12900       Ev := Uint_0;
12901
12902       --  Loop through literals of enumeration type setting pos and rep values
12903       --  except that if the Ekind is already set, then it means that the
12904       --  literal was already constructed (case of a derived type declaration
12905       --  and we should not disturb the Pos and Rep values.
12906
12907       while Present (L) loop
12908          if Ekind (L) /= E_Enumeration_Literal then
12909             Set_Ekind (L, E_Enumeration_Literal);
12910             Set_Enumeration_Pos (L, Ev);
12911             Set_Enumeration_Rep (L, Ev);
12912             Set_Is_Known_Valid  (L, True);
12913          end if;
12914
12915          Set_Etype (L, T);
12916          New_Overloaded_Entity (L);
12917          Generate_Definition (L);
12918          Set_Convention (L, Convention_Intrinsic);
12919
12920          if Nkind (L) = N_Defining_Character_Literal then
12921             Set_Is_Character_Type (T, True);
12922          end if;
12923
12924          Ev := Ev + 1;
12925          Next (L);
12926       end loop;
12927
12928       --  Now create a node representing upper bound
12929
12930       B_Node := New_Node (N_Identifier, Sloc (Def));
12931       Set_Chars (B_Node, Chars (Last (Literals (Def))));
12932       Set_Entity (B_Node,  Last (Literals (Def)));
12933       Set_Etype (B_Node, T);
12934       Set_Is_Static_Expression (B_Node, True);
12935
12936       Set_High_Bound (R_Node, B_Node);
12937
12938       --  Initialize various fields of the type. Some of this information
12939       --  may be overwritten later through rep.clauses.
12940
12941       Set_Scalar_Range    (T, R_Node);
12942       Set_RM_Size         (T, UI_From_Int (Minimum_Size (T)));
12943       Set_Enum_Esize      (T);
12944       Set_Enum_Pos_To_Rep (T, Empty);
12945
12946       --  Set Discard_Names if configuration pragma set, or if there is
12947       --  a parameterless pragma in the current declarative region
12948
12949       if Global_Discard_Names
12950         or else Discard_Names (Scope (T))
12951       then
12952          Set_Discard_Names (T);
12953       end if;
12954
12955       --  Process end label if there is one
12956
12957       if Present (Def) then
12958          Process_End_Label (Def, 'e', T);
12959       end if;
12960    end Enumeration_Type_Declaration;
12961
12962    ---------------------------------
12963    -- Expand_To_Stored_Constraint --
12964    ---------------------------------
12965
12966    function Expand_To_Stored_Constraint
12967      (Typ        : Entity_Id;
12968       Constraint : Elist_Id) return Elist_Id
12969    is
12970       Explicitly_Discriminated_Type : Entity_Id;
12971       Expansion    : Elist_Id;
12972       Discriminant : Entity_Id;
12973
12974       function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
12975       --  Find the nearest type that actually specifies discriminants
12976
12977       ---------------------------------
12978       -- Type_With_Explicit_Discrims --
12979       ---------------------------------
12980
12981       function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
12982          Typ : constant E := Base_Type (Id);
12983
12984       begin
12985          if Ekind (Typ) in Incomplete_Or_Private_Kind then
12986             if Present (Full_View (Typ)) then
12987                return Type_With_Explicit_Discrims (Full_View (Typ));
12988             end if;
12989
12990          else
12991             if Has_Discriminants (Typ) then
12992                return Typ;
12993             end if;
12994          end if;
12995
12996          if Etype (Typ) = Typ then
12997             return Empty;
12998          elsif Has_Discriminants (Typ) then
12999             return Typ;
13000          else
13001             return Type_With_Explicit_Discrims (Etype (Typ));
13002          end if;
13003
13004       end Type_With_Explicit_Discrims;
13005
13006    --  Start of processing for Expand_To_Stored_Constraint
13007
13008    begin
13009       if No (Constraint)
13010         or else Is_Empty_Elmt_List (Constraint)
13011       then
13012          return No_Elist;
13013       end if;
13014
13015       Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
13016
13017       if No (Explicitly_Discriminated_Type) then
13018          return No_Elist;
13019       end if;
13020
13021       Expansion := New_Elmt_List;
13022
13023       Discriminant :=
13024          First_Stored_Discriminant (Explicitly_Discriminated_Type);
13025       while Present (Discriminant) loop
13026          Append_Elmt (
13027            Get_Discriminant_Value (
13028              Discriminant, Explicitly_Discriminated_Type, Constraint),
13029            Expansion);
13030          Next_Stored_Discriminant (Discriminant);
13031       end loop;
13032
13033       return Expansion;
13034    end Expand_To_Stored_Constraint;
13035
13036    ---------------------------
13037    -- Find_Hidden_Interface --
13038    ---------------------------
13039
13040    function Find_Hidden_Interface
13041      (Src  : Elist_Id;
13042       Dest : Elist_Id) return Entity_Id
13043    is
13044       Iface      : Entity_Id;
13045       Iface_Elmt : Elmt_Id;
13046
13047    begin
13048       if Present (Src) and then Present (Dest) then
13049          Iface_Elmt := First_Elmt (Src);
13050          while Present (Iface_Elmt) loop
13051             Iface := Node (Iface_Elmt);
13052
13053             if Is_Interface (Iface)
13054               and then not Contain_Interface (Iface, Dest)
13055             then
13056                return Iface;
13057             end if;
13058
13059             Next_Elmt (Iface_Elmt);
13060          end loop;
13061       end if;
13062
13063       return Empty;
13064    end Find_Hidden_Interface;
13065
13066    --------------------
13067    -- Find_Type_Name --
13068    --------------------
13069
13070    function Find_Type_Name (N : Node_Id) return Entity_Id is
13071       Id       : constant Entity_Id := Defining_Identifier (N);
13072       Prev     : Entity_Id;
13073       New_Id   : Entity_Id;
13074       Prev_Par : Node_Id;
13075
13076       procedure Tag_Mismatch;
13077       --  Diagnose a tagged partial view whose full view is untagged.
13078       --  We post the message on the full view, with a reference to
13079       --  the previous partial view. The partial view can be private
13080       --  or incomplete, and these are handled in a different manner,
13081       --  so we determine the position of the error message from the
13082       --  respective slocs of both.
13083
13084       ------------------
13085       -- Tag_Mismatch --
13086       ------------------
13087
13088       procedure Tag_Mismatch is
13089       begin
13090          if Sloc (Prev) < Sloc (Id) then
13091             Error_Msg_NE
13092               ("full declaration of } must be a tagged type ", Id, Prev);
13093          else
13094             Error_Msg_NE
13095               ("full declaration of } must be a tagged type ", Prev, Id);
13096          end if;
13097       end Tag_Mismatch;
13098
13099    --  Start processing for Find_Type_Name
13100
13101    begin
13102       --  Find incomplete declaration, if one was given
13103
13104       Prev := Current_Entity_In_Scope (Id);
13105
13106       if Present (Prev) then
13107
13108          --  Previous declaration exists. Error if not incomplete/private case
13109          --  except if previous declaration is implicit, etc. Enter_Name will
13110          --  emit error if appropriate.
13111
13112          Prev_Par := Parent (Prev);
13113
13114          if not Is_Incomplete_Or_Private_Type (Prev) then
13115             Enter_Name (Id);
13116             New_Id := Id;
13117
13118          elsif not Nkind_In (N, N_Full_Type_Declaration,
13119                                 N_Task_Type_Declaration,
13120                                 N_Protected_Type_Declaration)
13121          then
13122             --  Completion must be a full type declarations (RM 7.3(4))
13123
13124             Error_Msg_Sloc := Sloc (Prev);
13125             Error_Msg_NE ("invalid completion of }", Id, Prev);
13126
13127             --  Set scope of Id to avoid cascaded errors. Entity is never
13128             --  examined again, except when saving globals in generics.
13129
13130             Set_Scope (Id, Current_Scope);
13131             New_Id := Id;
13132
13133          --  Case of full declaration of incomplete type
13134
13135          elsif Ekind (Prev) = E_Incomplete_Type then
13136
13137             --  Indicate that the incomplete declaration has a matching full
13138             --  declaration. The defining occurrence of the incomplete
13139             --  declaration remains the visible one, and the procedure
13140             --  Get_Full_View dereferences it whenever the type is used.
13141
13142             if Present (Full_View (Prev)) then
13143                Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
13144             end if;
13145
13146             Set_Full_View (Prev,  Id);
13147             Append_Entity (Id, Current_Scope);
13148             Set_Is_Public (Id, Is_Public (Prev));
13149             Set_Is_Internal (Id);
13150             New_Id := Prev;
13151
13152          --  Case of full declaration of private type
13153
13154          else
13155             if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
13156                if Etype (Prev) /= Prev then
13157
13158                   --  Prev is a private subtype or a derived type, and needs
13159                   --  no completion.
13160
13161                   Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
13162                   New_Id := Id;
13163
13164                elsif Ekind (Prev) = E_Private_Type
13165                  and then Nkind_In (N, N_Task_Type_Declaration,
13166                                        N_Protected_Type_Declaration)
13167                then
13168                   Error_Msg_N
13169                    ("completion of nonlimited type cannot be limited", N);
13170
13171                elsif Ekind (Prev) = E_Record_Type_With_Private
13172                  and then Nkind_In (N, N_Task_Type_Declaration,
13173                                        N_Protected_Type_Declaration)
13174                then
13175                   if not Is_Limited_Record (Prev) then
13176                      Error_Msg_N
13177                         ("completion of nonlimited type cannot be limited", N);
13178
13179                   elsif No (Interface_List (N)) then
13180                      Error_Msg_N
13181                         ("completion of tagged private type must be tagged",
13182                            N);
13183                   end if;
13184                end if;
13185
13186             --  Ada 2005 (AI-251): Private extension declaration of a task
13187             --  type or a protected type. This case arises when covering
13188             --  interface types.
13189
13190             elsif Nkind_In (N, N_Task_Type_Declaration,
13191                                N_Protected_Type_Declaration)
13192             then
13193                null;
13194
13195             elsif Nkind (N) /= N_Full_Type_Declaration
13196               or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
13197             then
13198                Error_Msg_N
13199                  ("full view of private extension must be an extension", N);
13200
13201             elsif not (Abstract_Present (Parent (Prev)))
13202               and then Abstract_Present (Type_Definition (N))
13203             then
13204                Error_Msg_N
13205                  ("full view of non-abstract extension cannot be abstract", N);
13206             end if;
13207
13208             if not In_Private_Part (Current_Scope) then
13209                Error_Msg_N
13210                  ("declaration of full view must appear in private part", N);
13211             end if;
13212
13213             Copy_And_Swap (Prev, Id);
13214             Set_Has_Private_Declaration (Prev);
13215             Set_Has_Private_Declaration (Id);
13216
13217             --  If no error, propagate freeze_node from private to full view.
13218             --  It may have been generated for an early operational item.
13219
13220             if Present (Freeze_Node (Id))
13221               and then Serious_Errors_Detected = 0
13222               and then No (Full_View (Id))
13223             then
13224                Set_Freeze_Node (Prev, Freeze_Node (Id));
13225                Set_Freeze_Node (Id, Empty);
13226                Set_First_Rep_Item (Prev, First_Rep_Item (Id));
13227             end if;
13228
13229             Set_Full_View (Id, Prev);
13230             New_Id := Prev;
13231          end if;
13232
13233          --  Verify that full declaration conforms to partial one
13234
13235          if Is_Incomplete_Or_Private_Type (Prev)
13236            and then Present (Discriminant_Specifications (Prev_Par))
13237          then
13238             if Present (Discriminant_Specifications (N)) then
13239                if Ekind (Prev) = E_Incomplete_Type then
13240                   Check_Discriminant_Conformance (N, Prev, Prev);
13241                else
13242                   Check_Discriminant_Conformance (N, Prev, Id);
13243                end if;
13244
13245             else
13246                Error_Msg_N
13247                  ("missing discriminants in full type declaration", N);
13248
13249                --  To avoid cascaded errors on subsequent use, share the
13250                --  discriminants of the partial view.
13251
13252                Set_Discriminant_Specifications (N,
13253                  Discriminant_Specifications (Prev_Par));
13254             end if;
13255          end if;
13256
13257          --  A prior untagged partial view can have an associated class-wide
13258          --  type due to use of the class attribute, and in this case the full
13259          --  type must also be tagged. This Ada 95 usage is deprecated in favor
13260          --  of incomplete tagged declarations, but we check for it.
13261
13262          if Is_Type (Prev)
13263            and then (Is_Tagged_Type (Prev)
13264                       or else Present (Class_Wide_Type (Prev)))
13265          then
13266             --  The full declaration is either a tagged type (including
13267             --  a synchronized type that implements interfaces) or a
13268             --  type extension, otherwise this is an error.
13269
13270             if Nkind_In (N, N_Task_Type_Declaration,
13271                             N_Protected_Type_Declaration)
13272             then
13273                if No (Interface_List (N))
13274                  and then not Error_Posted (N)
13275                then
13276                   Tag_Mismatch;
13277                end if;
13278
13279             elsif Nkind (Type_Definition (N)) = N_Record_Definition then
13280
13281                --  Indicate that the previous declaration (tagged incomplete
13282                --  or private declaration) requires the same on the full one.
13283
13284                if not Tagged_Present (Type_Definition (N)) then
13285                   Tag_Mismatch;
13286                   Set_Is_Tagged_Type (Id);
13287                   Set_Primitive_Operations (Id, New_Elmt_List);
13288                end if;
13289
13290             elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
13291                if No (Record_Extension_Part (Type_Definition (N))) then
13292                   Error_Msg_NE (
13293                     "full declaration of } must be a record extension",
13294                     Prev, Id);
13295                   Set_Is_Tagged_Type (Id);
13296                   Set_Primitive_Operations (Id, New_Elmt_List);
13297                end if;
13298
13299             else
13300                Tag_Mismatch;
13301             end if;
13302          end if;
13303
13304          return New_Id;
13305
13306       else
13307          --  New type declaration
13308
13309          Enter_Name (Id);
13310          return Id;
13311       end if;
13312    end Find_Type_Name;
13313
13314    -------------------------
13315    -- Find_Type_Of_Object --
13316    -------------------------
13317
13318    function Find_Type_Of_Object
13319      (Obj_Def     : Node_Id;
13320       Related_Nod : Node_Id) return Entity_Id
13321    is
13322       Def_Kind : constant Node_Kind := Nkind (Obj_Def);
13323       P        : Node_Id := Parent (Obj_Def);
13324       T        : Entity_Id;
13325       Nam      : Name_Id;
13326
13327    begin
13328       --  If the parent is a component_definition node we climb to the
13329       --  component_declaration node
13330
13331       if Nkind (P) = N_Component_Definition then
13332          P := Parent (P);
13333       end if;
13334
13335       --  Case of an anonymous array subtype
13336
13337       if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
13338                              N_Unconstrained_Array_Definition)
13339       then
13340          T := Empty;
13341          Array_Type_Declaration (T, Obj_Def);
13342
13343       --  Create an explicit subtype whenever possible
13344
13345       elsif Nkind (P) /= N_Component_Declaration
13346         and then Def_Kind = N_Subtype_Indication
13347       then
13348          --  Base name of subtype on object name, which will be unique in
13349          --  the current scope.
13350
13351          --  If this is a duplicate declaration, return base type, to avoid
13352          --  generating duplicate anonymous types.
13353
13354          if Error_Posted (P) then
13355             Analyze (Subtype_Mark (Obj_Def));
13356             return Entity (Subtype_Mark (Obj_Def));
13357          end if;
13358
13359          Nam :=
13360             New_External_Name
13361              (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
13362
13363          T := Make_Defining_Identifier (Sloc (P), Nam);
13364
13365          Insert_Action (Obj_Def,
13366            Make_Subtype_Declaration (Sloc (P),
13367              Defining_Identifier => T,
13368              Subtype_Indication  => Relocate_Node (Obj_Def)));
13369
13370          --  This subtype may need freezing, and this will not be done
13371          --  automatically if the object declaration is not in declarative
13372          --  part. Since this is an object declaration, the type cannot always
13373          --  be frozen here. Deferred constants do not freeze their type
13374          --  (which often enough will be private).
13375
13376          if Nkind (P) = N_Object_Declaration
13377            and then Constant_Present (P)
13378            and then No (Expression (P))
13379          then
13380             null;
13381          else
13382             Insert_Actions (Obj_Def, Freeze_Entity (T, Sloc (P)));
13383          end if;
13384
13385       --  Ada 2005 AI-406: the object definition in an object declaration
13386       --  can be an access definition.
13387
13388       elsif Def_Kind = N_Access_Definition then
13389          T := Access_Definition (Related_Nod, Obj_Def);
13390          Set_Is_Local_Anonymous_Access (T);
13391
13392       --  Otherwise, the object definition is just a subtype_mark
13393
13394       else
13395          T := Process_Subtype (Obj_Def, Related_Nod);
13396       end if;
13397
13398       return T;
13399    end Find_Type_Of_Object;
13400
13401    --------------------------------
13402    -- Find_Type_Of_Subtype_Indic --
13403    --------------------------------
13404
13405    function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
13406       Typ : Entity_Id;
13407
13408    begin
13409       --  Case of subtype mark with a constraint
13410
13411       if Nkind (S) = N_Subtype_Indication then
13412          Find_Type (Subtype_Mark (S));
13413          Typ := Entity (Subtype_Mark (S));
13414
13415          if not
13416            Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
13417          then
13418             Error_Msg_N
13419               ("incorrect constraint for this kind of type", Constraint (S));
13420             Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
13421          end if;
13422
13423       --  Otherwise we have a subtype mark without a constraint
13424
13425       elsif Error_Posted (S) then
13426          Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
13427          return Any_Type;
13428
13429       else
13430          Find_Type (S);
13431          Typ := Entity (S);
13432       end if;
13433
13434       --  Check No_Wide_Characters restriction
13435
13436       if Typ = Standard_Wide_Character
13437         or else Typ = Standard_Wide_Wide_Character
13438         or else Typ = Standard_Wide_String
13439         or else Typ = Standard_Wide_Wide_String
13440       then
13441          Check_Restriction (No_Wide_Characters, S);
13442       end if;
13443
13444       return Typ;
13445    end Find_Type_Of_Subtype_Indic;
13446
13447    -------------------------------------
13448    -- Floating_Point_Type_Declaration --
13449    -------------------------------------
13450
13451    procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
13452       Digs          : constant Node_Id := Digits_Expression (Def);
13453       Digs_Val      : Uint;
13454       Base_Typ      : Entity_Id;
13455       Implicit_Base : Entity_Id;
13456       Bound         : Node_Id;
13457
13458       function Can_Derive_From (E : Entity_Id) return Boolean;
13459       --  Find if given digits value allows derivation from specified type
13460
13461       ---------------------
13462       -- Can_Derive_From --
13463       ---------------------
13464
13465       function Can_Derive_From (E : Entity_Id) return Boolean is
13466          Spec : constant Entity_Id := Real_Range_Specification (Def);
13467
13468       begin
13469          if Digs_Val > Digits_Value (E) then
13470             return False;
13471          end if;
13472
13473          if Present (Spec) then
13474             if Expr_Value_R (Type_Low_Bound (E)) >
13475                Expr_Value_R (Low_Bound (Spec))
13476             then
13477                return False;
13478             end if;
13479
13480             if Expr_Value_R (Type_High_Bound (E)) <
13481                Expr_Value_R (High_Bound (Spec))
13482             then
13483                return False;
13484             end if;
13485          end if;
13486
13487          return True;
13488       end Can_Derive_From;
13489
13490    --  Start of processing for Floating_Point_Type_Declaration
13491
13492    begin
13493       Check_Restriction (No_Floating_Point, Def);
13494
13495       --  Create an implicit base type
13496
13497       Implicit_Base :=
13498         Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
13499
13500       --  Analyze and verify digits value
13501
13502       Analyze_And_Resolve (Digs, Any_Integer);
13503       Check_Digits_Expression (Digs);
13504       Digs_Val := Expr_Value (Digs);
13505
13506       --  Process possible range spec and find correct type to derive from
13507
13508       Process_Real_Range_Specification (Def);
13509
13510       if Can_Derive_From (Standard_Short_Float) then
13511          Base_Typ := Standard_Short_Float;
13512       elsif Can_Derive_From (Standard_Float) then
13513          Base_Typ := Standard_Float;
13514       elsif Can_Derive_From (Standard_Long_Float) then
13515          Base_Typ := Standard_Long_Float;
13516       elsif Can_Derive_From (Standard_Long_Long_Float) then
13517          Base_Typ := Standard_Long_Long_Float;
13518
13519       --  If we can't derive from any existing type, use long_long_float
13520       --  and give appropriate message explaining the problem.
13521
13522       else
13523          Base_Typ := Standard_Long_Long_Float;
13524
13525          if Digs_Val >= Digits_Value (Standard_Long_Long_Float) then
13526             Error_Msg_Uint_1 := Digits_Value (Standard_Long_Long_Float);
13527             Error_Msg_N ("digits value out of range, maximum is ^", Digs);
13528
13529          else
13530             Error_Msg_N
13531               ("range too large for any predefined type",
13532                Real_Range_Specification (Def));
13533          end if;
13534       end if;
13535
13536       --  If there are bounds given in the declaration use them as the bounds
13537       --  of the type, otherwise use the bounds of the predefined base type
13538       --  that was chosen based on the Digits value.
13539
13540       if Present (Real_Range_Specification (Def)) then
13541          Set_Scalar_Range (T, Real_Range_Specification (Def));
13542          Set_Is_Constrained (T);
13543
13544          --  The bounds of this range must be converted to machine numbers
13545          --  in accordance with RM 4.9(38).
13546
13547          Bound := Type_Low_Bound (T);
13548
13549          if Nkind (Bound) = N_Real_Literal then
13550             Set_Realval
13551               (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
13552             Set_Is_Machine_Number (Bound);
13553          end if;
13554
13555          Bound := Type_High_Bound (T);
13556
13557          if Nkind (Bound) = N_Real_Literal then
13558             Set_Realval
13559               (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
13560             Set_Is_Machine_Number (Bound);
13561          end if;
13562
13563       else
13564          Set_Scalar_Range (T, Scalar_Range (Base_Typ));
13565       end if;
13566
13567       --  Complete definition of implicit base and declared first subtype
13568
13569       Set_Etype          (Implicit_Base, Base_Typ);
13570
13571       Set_Scalar_Range   (Implicit_Base, Scalar_Range   (Base_Typ));
13572       Set_Size_Info      (Implicit_Base,                (Base_Typ));
13573       Set_RM_Size        (Implicit_Base, RM_Size        (Base_Typ));
13574       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
13575       Set_Digits_Value   (Implicit_Base, Digits_Value   (Base_Typ));
13576       Set_Vax_Float      (Implicit_Base, Vax_Float      (Base_Typ));
13577
13578       Set_Ekind          (T, E_Floating_Point_Subtype);
13579       Set_Etype          (T, Implicit_Base);
13580
13581       Set_Size_Info      (T,                (Implicit_Base));
13582       Set_RM_Size        (T, RM_Size        (Implicit_Base));
13583       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
13584       Set_Digits_Value   (T, Digs_Val);
13585    end Floating_Point_Type_Declaration;
13586
13587    ----------------------------
13588    -- Get_Discriminant_Value --
13589    ----------------------------
13590
13591    --  This is the situation:
13592
13593    --  There is a non-derived type
13594
13595    --       type T0 (Dx, Dy, Dz...)
13596
13597    --  There are zero or more levels of derivation, with each derivation
13598    --  either purely inheriting the discriminants, or defining its own.
13599
13600    --       type Ti      is new Ti-1
13601    --  or
13602    --       type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
13603    --  or
13604    --       subtype Ti is ...
13605
13606    --  The subtype issue is avoided by the use of Original_Record_Component,
13607    --  and the fact that derived subtypes also derive the constraints.
13608
13609    --  This chain leads back from
13610
13611    --       Typ_For_Constraint
13612
13613    --  Typ_For_Constraint has discriminants, and the value for each
13614    --  discriminant is given by its corresponding Elmt of Constraints.
13615
13616    --  Discriminant is some discriminant in this hierarchy
13617
13618    --  We need to return its value
13619
13620    --  We do this by recursively searching each level, and looking for
13621    --  Discriminant. Once we get to the bottom, we start backing up
13622    --  returning the value for it which may in turn be a discriminant
13623    --  further up, so on the backup we continue the substitution.
13624
13625    function Get_Discriminant_Value
13626      (Discriminant       : Entity_Id;
13627       Typ_For_Constraint : Entity_Id;
13628       Constraint         : Elist_Id) return Node_Id
13629    is
13630       function Search_Derivation_Levels
13631         (Ti                    : Entity_Id;
13632          Discrim_Values        : Elist_Id;
13633          Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
13634       --  This is the routine that performs the recursive search of levels
13635       --  as described above.
13636
13637       ------------------------------
13638       -- Search_Derivation_Levels --
13639       ------------------------------
13640
13641       function Search_Derivation_Levels
13642         (Ti                    : Entity_Id;
13643          Discrim_Values        : Elist_Id;
13644          Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
13645       is
13646          Assoc          : Elmt_Id;
13647          Disc           : Entity_Id;
13648          Result         : Node_Or_Entity_Id;
13649          Result_Entity  : Node_Id;
13650
13651       begin
13652          --  If inappropriate type, return Error, this happens only in
13653          --  cascaded error situations, and we want to avoid a blow up.
13654
13655          if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
13656             return Error;
13657          end if;
13658
13659          --  Look deeper if possible. Use Stored_Constraints only for
13660          --  untagged types. For tagged types use the given constraint.
13661          --  This asymmetry needs explanation???
13662
13663          if not Stored_Discrim_Values
13664            and then Present (Stored_Constraint (Ti))
13665            and then not Is_Tagged_Type (Ti)
13666          then
13667             Result :=
13668               Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
13669          else
13670             declare
13671                Td : constant Entity_Id := Etype (Ti);
13672
13673             begin
13674                if Td = Ti then
13675                   Result := Discriminant;
13676
13677                else
13678                   if Present (Stored_Constraint (Ti)) then
13679                      Result :=
13680                         Search_Derivation_Levels
13681                           (Td, Stored_Constraint (Ti), True);
13682                   else
13683                      Result :=
13684                         Search_Derivation_Levels
13685                           (Td, Discrim_Values, Stored_Discrim_Values);
13686                   end if;
13687                end if;
13688             end;
13689          end if;
13690
13691          --  Extra underlying places to search, if not found above. For
13692          --  concurrent types, the relevant discriminant appears in the
13693          --  corresponding record. For a type derived from a private type
13694          --  without discriminant, the full view inherits the discriminants
13695          --  of the full view of the parent.
13696
13697          if Result = Discriminant then
13698             if Is_Concurrent_Type (Ti)
13699               and then Present (Corresponding_Record_Type (Ti))
13700             then
13701                Result :=
13702                  Search_Derivation_Levels (
13703                    Corresponding_Record_Type (Ti),
13704                    Discrim_Values,
13705                    Stored_Discrim_Values);
13706
13707             elsif Is_Private_Type (Ti)
13708               and then not Has_Discriminants (Ti)
13709               and then Present (Full_View (Ti))
13710               and then Etype (Full_View (Ti)) /= Ti
13711             then
13712                Result :=
13713                  Search_Derivation_Levels (
13714                    Full_View (Ti),
13715                    Discrim_Values,
13716                    Stored_Discrim_Values);
13717             end if;
13718          end if;
13719
13720          --  If Result is not a (reference to a) discriminant, return it,
13721          --  otherwise set Result_Entity to the discriminant.
13722
13723          if Nkind (Result) = N_Defining_Identifier then
13724             pragma Assert (Result = Discriminant);
13725             Result_Entity := Result;
13726
13727          else
13728             if not Denotes_Discriminant (Result) then
13729                return Result;
13730             end if;
13731
13732             Result_Entity := Entity (Result);
13733          end if;
13734
13735          --  See if this level of derivation actually has discriminants
13736          --  because tagged derivations can add them, hence the lower
13737          --  levels need not have any.
13738
13739          if not Has_Discriminants (Ti) then
13740             return Result;
13741          end if;
13742
13743          --  Scan Ti's discriminants for Result_Entity,
13744          --  and return its corresponding value, if any.
13745
13746          Result_Entity := Original_Record_Component (Result_Entity);
13747
13748          Assoc := First_Elmt (Discrim_Values);
13749
13750          if Stored_Discrim_Values then
13751             Disc := First_Stored_Discriminant (Ti);
13752          else
13753             Disc := First_Discriminant (Ti);
13754          end if;
13755
13756          while Present (Disc) loop
13757             pragma Assert (Present (Assoc));
13758
13759             if Original_Record_Component (Disc) = Result_Entity then
13760                return Node (Assoc);
13761             end if;
13762
13763             Next_Elmt (Assoc);
13764
13765             if Stored_Discrim_Values then
13766                Next_Stored_Discriminant (Disc);
13767             else
13768                Next_Discriminant (Disc);
13769             end if;
13770          end loop;
13771
13772          --  Could not find it
13773          --
13774          return Result;
13775       end Search_Derivation_Levels;
13776
13777       --  Local Variables
13778
13779       Result : Node_Or_Entity_Id;
13780
13781    --  Start of processing for Get_Discriminant_Value
13782
13783    begin
13784       --  ??? This routine is a gigantic mess and will be deleted. For the
13785       --  time being just test for the trivial case before calling recurse.
13786
13787       if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
13788          declare
13789             D : Entity_Id;
13790             E : Elmt_Id;
13791
13792          begin
13793             D := First_Discriminant (Typ_For_Constraint);
13794             E := First_Elmt (Constraint);
13795             while Present (D) loop
13796                if Chars (D) = Chars (Discriminant) then
13797                   return Node (E);
13798                end if;
13799
13800                Next_Discriminant (D);
13801                Next_Elmt (E);
13802             end loop;
13803          end;
13804       end if;
13805
13806       Result := Search_Derivation_Levels
13807         (Typ_For_Constraint, Constraint, False);
13808
13809       --  ??? hack to disappear when this routine is gone
13810
13811       if  Nkind (Result) = N_Defining_Identifier then
13812          declare
13813             D : Entity_Id;
13814             E : Elmt_Id;
13815
13816          begin
13817             D := First_Discriminant (Typ_For_Constraint);
13818             E := First_Elmt (Constraint);
13819             while Present (D) loop
13820                if Corresponding_Discriminant (D) = Discriminant then
13821                   return Node (E);
13822                end if;
13823
13824                Next_Discriminant (D);
13825                Next_Elmt (E);
13826             end loop;
13827          end;
13828       end if;
13829
13830       pragma Assert (Nkind (Result) /= N_Defining_Identifier);
13831       return Result;
13832    end Get_Discriminant_Value;
13833
13834    --------------------------
13835    -- Has_Range_Constraint --
13836    --------------------------
13837
13838    function Has_Range_Constraint (N : Node_Id) return Boolean is
13839       C : constant Node_Id := Constraint (N);
13840
13841    begin
13842       if Nkind (C) = N_Range_Constraint then
13843          return True;
13844
13845       elsif Nkind (C) = N_Digits_Constraint then
13846          return
13847             Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
13848               or else
13849             Present (Range_Constraint (C));
13850
13851       elsif Nkind (C) = N_Delta_Constraint then
13852          return Present (Range_Constraint (C));
13853
13854       else
13855          return False;
13856       end if;
13857    end Has_Range_Constraint;
13858
13859    ------------------------
13860    -- Inherit_Components --
13861    ------------------------
13862
13863    function Inherit_Components
13864      (N             : Node_Id;
13865       Parent_Base   : Entity_Id;
13866       Derived_Base  : Entity_Id;
13867       Is_Tagged     : Boolean;
13868       Inherit_Discr : Boolean;
13869       Discs         : Elist_Id) return Elist_Id
13870    is
13871       Assoc_List : constant Elist_Id := New_Elmt_List;
13872
13873       procedure Inherit_Component
13874         (Old_C          : Entity_Id;
13875          Plain_Discrim  : Boolean := False;
13876          Stored_Discrim : Boolean := False);
13877       --  Inherits component Old_C from Parent_Base to the Derived_Base. If
13878       --  Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
13879       --  True, Old_C is a stored discriminant. If they are both false then
13880       --  Old_C is a regular component.
13881
13882       -----------------------
13883       -- Inherit_Component --
13884       -----------------------
13885
13886       procedure Inherit_Component
13887         (Old_C          : Entity_Id;
13888          Plain_Discrim  : Boolean := False;
13889          Stored_Discrim : Boolean := False)
13890       is
13891          New_C : constant Entity_Id := New_Copy (Old_C);
13892
13893          Discrim      : Entity_Id;
13894          Corr_Discrim : Entity_Id;
13895
13896       begin
13897          pragma Assert (not Is_Tagged or else not Stored_Discrim);
13898
13899          Set_Parent (New_C, Parent (Old_C));
13900
13901          --  Regular discriminants and components must be inserted in the scope
13902          --  of the Derived_Base. Do it here.
13903
13904          if not Stored_Discrim then
13905             Enter_Name (New_C);
13906          end if;
13907
13908          --  For tagged types the Original_Record_Component must point to
13909          --  whatever this field was pointing to in the parent type. This has
13910          --  already been achieved by the call to New_Copy above.
13911
13912          if not Is_Tagged then
13913             Set_Original_Record_Component (New_C, New_C);
13914          end if;
13915
13916          --  If we have inherited a component then see if its Etype contains
13917          --  references to Parent_Base discriminants. In this case, replace
13918          --  these references with the constraints given in Discs. We do not
13919          --  do this for the partial view of private types because this is
13920          --  not needed (only the components of the full view will be used
13921          --  for code generation) and cause problem. We also avoid this
13922          --  transformation in some error situations.
13923
13924          if Ekind (New_C) = E_Component then
13925             if (Is_Private_Type (Derived_Base)
13926                  and then not Is_Generic_Type (Derived_Base))
13927               or else (Is_Empty_Elmt_List (Discs)
13928                         and then  not Expander_Active)
13929             then
13930                Set_Etype (New_C, Etype (Old_C));
13931
13932             else
13933                --  The current component introduces a circularity of the
13934                --  following kind:
13935
13936                --     limited with Pack_2;
13937                --     package Pack_1 is
13938                --        type T_1 is tagged record
13939                --           Comp : access Pack_2.T_2;
13940                --           ...
13941                --        end record;
13942                --     end Pack_1;
13943
13944                --     with Pack_1;
13945                --     package Pack_2 is
13946                --        type T_2 is new Pack_1.T_1 with ...;
13947                --     end Pack_2;
13948
13949                Set_Etype
13950                  (New_C,
13951                   Constrain_Component_Type
13952                   (Old_C, Derived_Base, N, Parent_Base, Discs));
13953             end if;
13954          end if;
13955
13956          --  In derived tagged types it is illegal to reference a non
13957          --  discriminant component in the parent type. To catch this, mark
13958          --  these components with an Ekind of E_Void. This will be reset in
13959          --  Record_Type_Definition after processing the record extension of
13960          --  the derived type.
13961
13962          --  If the declaration is a private extension, there is no further
13963          --  record extension to process, and the components retain their
13964          --  current kind, because they are visible at this point.
13965
13966          if Is_Tagged and then Ekind (New_C) = E_Component
13967            and then Nkind (N) /= N_Private_Extension_Declaration
13968          then
13969             Set_Ekind (New_C, E_Void);
13970          end if;
13971
13972          if Plain_Discrim then
13973             Set_Corresponding_Discriminant (New_C, Old_C);
13974             Build_Discriminal (New_C);
13975
13976          --  If we are explicitly inheriting a stored discriminant it will be
13977          --  completely hidden.
13978
13979          elsif Stored_Discrim then
13980             Set_Corresponding_Discriminant (New_C, Empty);
13981             Set_Discriminal (New_C, Empty);
13982             Set_Is_Completely_Hidden (New_C);
13983
13984             --  Set the Original_Record_Component of each discriminant in the
13985             --  derived base to point to the corresponding stored that we just
13986             --  created.
13987
13988             Discrim := First_Discriminant (Derived_Base);
13989             while Present (Discrim) loop
13990                Corr_Discrim := Corresponding_Discriminant (Discrim);
13991
13992                --  Corr_Discrim could be missing in an error situation
13993
13994                if Present (Corr_Discrim)
13995                  and then Original_Record_Component (Corr_Discrim) = Old_C
13996                then
13997                   Set_Original_Record_Component (Discrim, New_C);
13998                end if;
13999
14000                Next_Discriminant (Discrim);
14001             end loop;
14002
14003             Append_Entity (New_C, Derived_Base);
14004          end if;
14005
14006          if not Is_Tagged then
14007             Append_Elmt (Old_C, Assoc_List);
14008             Append_Elmt (New_C, Assoc_List);
14009          end if;
14010       end Inherit_Component;
14011
14012       --  Variables local to Inherit_Component
14013
14014       Loc : constant Source_Ptr := Sloc (N);
14015
14016       Parent_Discrim : Entity_Id;
14017       Stored_Discrim : Entity_Id;
14018       D              : Entity_Id;
14019       Component      : Entity_Id;
14020
14021    --  Start of processing for Inherit_Components
14022
14023    begin
14024       if not Is_Tagged then
14025          Append_Elmt (Parent_Base,  Assoc_List);
14026          Append_Elmt (Derived_Base, Assoc_List);
14027       end if;
14028
14029       --  Inherit parent discriminants if needed
14030
14031       if Inherit_Discr then
14032          Parent_Discrim := First_Discriminant (Parent_Base);
14033          while Present (Parent_Discrim) loop
14034             Inherit_Component (Parent_Discrim, Plain_Discrim => True);
14035             Next_Discriminant (Parent_Discrim);
14036          end loop;
14037       end if;
14038
14039       --  Create explicit stored discrims for untagged types when necessary
14040
14041       if not Has_Unknown_Discriminants (Derived_Base)
14042         and then Has_Discriminants (Parent_Base)
14043         and then not Is_Tagged
14044         and then
14045           (not Inherit_Discr
14046              or else First_Discriminant (Parent_Base) /=
14047                      First_Stored_Discriminant (Parent_Base))
14048       then
14049          Stored_Discrim := First_Stored_Discriminant (Parent_Base);
14050          while Present (Stored_Discrim) loop
14051             Inherit_Component (Stored_Discrim, Stored_Discrim => True);
14052             Next_Stored_Discriminant (Stored_Discrim);
14053          end loop;
14054       end if;
14055
14056       --  See if we can apply the second transformation for derived types, as
14057       --  explained in point 6. in the comments above Build_Derived_Record_Type
14058       --  This is achieved by appending Derived_Base discriminants into Discs,
14059       --  which has the side effect of returning a non empty Discs list to the
14060       --  caller of Inherit_Components, which is what we want. This must be
14061       --  done for private derived types if there are explicit stored
14062       --  discriminants, to ensure that we can retrieve the values of the
14063       --  constraints provided in the ancestors.
14064
14065       if Inherit_Discr
14066         and then Is_Empty_Elmt_List (Discs)
14067         and then Present (First_Discriminant (Derived_Base))
14068         and then
14069           (not Is_Private_Type (Derived_Base)
14070              or else Is_Completely_Hidden
14071                (First_Stored_Discriminant (Derived_Base))
14072              or else Is_Generic_Type (Derived_Base))
14073       then
14074          D := First_Discriminant (Derived_Base);
14075          while Present (D) loop
14076             Append_Elmt (New_Reference_To (D, Loc), Discs);
14077             Next_Discriminant (D);
14078          end loop;
14079       end if;
14080
14081       --  Finally, inherit non-discriminant components unless they are not
14082       --  visible because defined or inherited from the full view of the
14083       --  parent. Don't inherit the _parent field of the parent type.
14084
14085       Component := First_Entity (Parent_Base);
14086       while Present (Component) loop
14087
14088          --  Ada 2005 (AI-251): Do not inherit components associated with
14089          --  secondary tags of the parent.
14090
14091          if Ekind (Component) = E_Component
14092            and then Present (Related_Type (Component))
14093          then
14094             null;
14095
14096          elsif Ekind (Component) /= E_Component
14097            or else Chars (Component) = Name_uParent
14098          then
14099             null;
14100
14101          --  If the derived type is within the parent type's declarative
14102          --  region, then the components can still be inherited even though
14103          --  they aren't visible at this point. This can occur for cases
14104          --  such as within public child units where the components must
14105          --  become visible upon entering the child unit's private part.
14106
14107          elsif not Is_Visible_Component (Component)
14108            and then not In_Open_Scopes (Scope (Parent_Base))
14109          then
14110             null;
14111
14112          elsif Ekind (Derived_Base) = E_Private_Type
14113            or else Ekind (Derived_Base) = E_Limited_Private_Type
14114          then
14115             null;
14116
14117          else
14118             Inherit_Component (Component);
14119          end if;
14120
14121          Next_Entity (Component);
14122       end loop;
14123
14124       --  For tagged derived types, inherited discriminants cannot be used in
14125       --  component declarations of the record extension part. To achieve this
14126       --  we mark the inherited discriminants as not visible.
14127
14128       if Is_Tagged and then Inherit_Discr then
14129          D := First_Discriminant (Derived_Base);
14130          while Present (D) loop
14131             Set_Is_Immediately_Visible (D, False);
14132             Next_Discriminant (D);
14133          end loop;
14134       end if;
14135
14136       return Assoc_List;
14137    end Inherit_Components;
14138
14139    -----------------------
14140    -- Is_Null_Extension --
14141    -----------------------
14142
14143    function Is_Null_Extension (T : Entity_Id) return Boolean is
14144       Type_Decl : constant Node_Id := Parent (T);
14145       Comp_List : Node_Id;
14146       Comp      : Node_Id;
14147
14148    begin
14149       if Nkind (Type_Decl) /= N_Full_Type_Declaration
14150         or else not Is_Tagged_Type (T)
14151         or else Nkind (Type_Definition (Type_Decl)) /=
14152                                               N_Derived_Type_Definition
14153         or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
14154       then
14155          return False;
14156       end if;
14157
14158       Comp_List :=
14159         Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
14160
14161       if Present (Discriminant_Specifications (Type_Decl)) then
14162          return False;
14163
14164       elsif Present (Comp_List)
14165         and then Is_Non_Empty_List (Component_Items (Comp_List))
14166       then
14167          Comp := First (Component_Items (Comp_List));
14168
14169          --  Only user-defined components are relevant. The component list
14170          --  may also contain a parent component and internal components
14171          --  corresponding to secondary tags, but these do not determine
14172          --  whether this is a null extension.
14173
14174          while Present (Comp) loop
14175             if Comes_From_Source (Comp) then
14176                return False;
14177             end if;
14178
14179             Next (Comp);
14180          end loop;
14181
14182          return True;
14183       else
14184          return True;
14185       end if;
14186    end Is_Null_Extension;
14187
14188    --------------------
14189    --  Is_Progenitor --
14190    --------------------
14191
14192    function Is_Progenitor
14193      (Iface : Entity_Id;
14194       Typ   : Entity_Id) return Boolean
14195    is
14196    begin
14197       return Implements_Interface (Typ, Iface,
14198                Exclude_Parents => True);
14199    end Is_Progenitor;
14200
14201    ------------------------------
14202    -- Is_Valid_Constraint_Kind --
14203    ------------------------------
14204
14205    function Is_Valid_Constraint_Kind
14206      (T_Kind          : Type_Kind;
14207       Constraint_Kind : Node_Kind) return Boolean
14208    is
14209    begin
14210       case T_Kind is
14211          when Enumeration_Kind |
14212               Integer_Kind =>
14213             return Constraint_Kind = N_Range_Constraint;
14214
14215          when Decimal_Fixed_Point_Kind =>
14216             return Nkind_In (Constraint_Kind, N_Digits_Constraint,
14217                                               N_Range_Constraint);
14218
14219          when Ordinary_Fixed_Point_Kind =>
14220             return Nkind_In (Constraint_Kind, N_Delta_Constraint,
14221                                               N_Range_Constraint);
14222
14223          when Float_Kind =>
14224             return Nkind_In (Constraint_Kind, N_Digits_Constraint,
14225                                               N_Range_Constraint);
14226
14227          when Access_Kind       |
14228               Array_Kind        |
14229               E_Record_Type     |
14230               E_Record_Subtype  |
14231               Class_Wide_Kind   |
14232               E_Incomplete_Type |
14233               Private_Kind      |
14234               Concurrent_Kind  =>
14235             return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
14236
14237          when others =>
14238             return True; -- Error will be detected later
14239       end case;
14240    end Is_Valid_Constraint_Kind;
14241
14242    --------------------------
14243    -- Is_Visible_Component --
14244    --------------------------
14245
14246    function Is_Visible_Component (C : Entity_Id) return Boolean is
14247       Original_Comp  : Entity_Id := Empty;
14248       Original_Scope : Entity_Id;
14249       Type_Scope     : Entity_Id;
14250
14251       function Is_Local_Type (Typ : Entity_Id) return Boolean;
14252       --  Check whether parent type of inherited component is declared locally,
14253       --  possibly within a nested package or instance. The current scope is
14254       --  the derived record itself.
14255
14256       -------------------
14257       -- Is_Local_Type --
14258       -------------------
14259
14260       function Is_Local_Type (Typ : Entity_Id) return Boolean is
14261          Scop : Entity_Id;
14262
14263       begin
14264          Scop := Scope (Typ);
14265          while Present (Scop)
14266            and then Scop /= Standard_Standard
14267          loop
14268             if Scop = Scope (Current_Scope) then
14269                return True;
14270             end if;
14271
14272             Scop := Scope (Scop);
14273          end loop;
14274
14275          return False;
14276       end Is_Local_Type;
14277
14278    --  Start of processing for Is_Visible_Component
14279
14280    begin
14281       if Ekind (C) = E_Component
14282         or else Ekind (C) = E_Discriminant
14283       then
14284          Original_Comp := Original_Record_Component (C);
14285       end if;
14286
14287       if No (Original_Comp) then
14288
14289          --  Premature usage, or previous error
14290
14291          return False;
14292
14293       else
14294          Original_Scope := Scope (Original_Comp);
14295          Type_Scope     := Scope (Base_Type (Scope (C)));
14296       end if;
14297
14298       --  This test only concerns tagged types
14299
14300       if not Is_Tagged_Type (Original_Scope) then
14301          return True;
14302
14303       --  If it is _Parent or _Tag, there is no visibility issue
14304
14305       elsif not Comes_From_Source (Original_Comp) then
14306          return True;
14307
14308       --  If we are in the body of an instantiation, the component is visible
14309       --  even when the parent type (possibly defined in an enclosing unit or
14310       --  in a parent unit) might not.
14311
14312       elsif In_Instance_Body then
14313          return True;
14314
14315       --  Discriminants are always visible
14316
14317       elsif Ekind (Original_Comp) = E_Discriminant
14318         and then not Has_Unknown_Discriminants (Original_Scope)
14319       then
14320          return True;
14321
14322       --  If the component has been declared in an ancestor which is currently
14323       --  a private type, then it is not visible. The same applies if the
14324       --  component's containing type is not in an open scope and the original
14325       --  component's enclosing type is a visible full view of a private type
14326       --  (which can occur in cases where an attempt is being made to reference
14327       --  a component in a sibling package that is inherited from a visible
14328       --  component of a type in an ancestor package; the component in the
14329       --  sibling package should not be visible even though the component it
14330       --  inherited from is visible). This does not apply however in the case
14331       --  where the scope of the type is a private child unit, or when the
14332       --  parent comes from a local package in which the ancestor is currently
14333       --  visible. The latter suppression of visibility is needed for cases
14334       --  that are tested in B730006.
14335
14336       elsif Is_Private_Type (Original_Scope)
14337         or else
14338           (not Is_Private_Descendant (Type_Scope)
14339             and then not In_Open_Scopes (Type_Scope)
14340             and then Has_Private_Declaration (Original_Scope))
14341       then
14342          --  If the type derives from an entity in a formal package, there
14343          --  are no additional visible components.
14344
14345          if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
14346             N_Formal_Package_Declaration
14347          then
14348             return False;
14349
14350          --  if we are not in the private part of the current package, there
14351          --  are no additional visible components.
14352
14353          elsif Ekind (Scope (Current_Scope)) = E_Package
14354            and then not In_Private_Part (Scope (Current_Scope))
14355          then
14356             return False;
14357          else
14358             return
14359               Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
14360                 and then In_Open_Scopes (Scope (Original_Scope))
14361                 and then Is_Local_Type (Type_Scope);
14362          end if;
14363
14364       --  There is another weird way in which a component may be invisible
14365       --  when the private and the full view are not derived from the same
14366       --  ancestor. Here is an example :
14367
14368       --       type A1 is tagged      record F1 : integer; end record;
14369       --       type A2 is new A1 with record F2 : integer; end record;
14370       --       type T is new A1 with private;
14371       --     private
14372       --       type T is new A2 with null record;
14373
14374       --  In this case, the full view of T inherits F1 and F2 but the private
14375       --  view inherits only F1
14376
14377       else
14378          declare
14379             Ancestor : Entity_Id := Scope (C);
14380
14381          begin
14382             loop
14383                if Ancestor = Original_Scope then
14384                   return True;
14385                elsif Ancestor = Etype (Ancestor) then
14386                   return False;
14387                end if;
14388
14389                Ancestor := Etype (Ancestor);
14390             end loop;
14391          end;
14392       end if;
14393    end Is_Visible_Component;
14394
14395    --------------------------
14396    -- Make_Class_Wide_Type --
14397    --------------------------
14398
14399    procedure Make_Class_Wide_Type (T : Entity_Id) is
14400       CW_Type : Entity_Id;
14401       CW_Name : Name_Id;
14402       Next_E  : Entity_Id;
14403
14404    begin
14405       --  The class wide type can have been defined by the partial view, in
14406       --  which case everything is already done.
14407
14408       if Present (Class_Wide_Type (T)) then
14409          return;
14410       end if;
14411
14412       CW_Type :=
14413         New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
14414
14415       --  Inherit root type characteristics
14416
14417       CW_Name := Chars (CW_Type);
14418       Next_E  := Next_Entity (CW_Type);
14419       Copy_Node (T, CW_Type);
14420       Set_Comes_From_Source (CW_Type, False);
14421       Set_Chars (CW_Type, CW_Name);
14422       Set_Parent (CW_Type, Parent (T));
14423       Set_Next_Entity (CW_Type, Next_E);
14424
14425       --  Ensure we have a new freeze node for the class-wide type. The partial
14426       --  view may have freeze action of its own, requiring a proper freeze
14427       --  node, and the same freeze node cannot be shared between the two
14428       --  types.
14429
14430       Set_Has_Delayed_Freeze (CW_Type);
14431       Set_Freeze_Node (CW_Type, Empty);
14432
14433       --  Customize the class-wide type: It has no prim. op., it cannot be
14434       --  abstract and its Etype points back to the specific root type.
14435
14436       Set_Ekind                (CW_Type, E_Class_Wide_Type);
14437       Set_Is_Tagged_Type       (CW_Type, True);
14438       Set_Primitive_Operations (CW_Type, New_Elmt_List);
14439       Set_Is_Abstract_Type     (CW_Type, False);
14440       Set_Is_Constrained       (CW_Type, False);
14441       Set_Is_First_Subtype     (CW_Type, Is_First_Subtype (T));
14442
14443       if Ekind (T) = E_Class_Wide_Subtype then
14444          Set_Etype             (CW_Type, Etype (Base_Type (T)));
14445       else
14446          Set_Etype             (CW_Type, T);
14447       end if;
14448
14449       --  If this is the class_wide type of a constrained subtype, it does
14450       --  not have discriminants.
14451
14452       Set_Has_Discriminants (CW_Type,
14453         Has_Discriminants (T) and then not Is_Constrained (T));
14454
14455       Set_Has_Unknown_Discriminants (CW_Type, True);
14456       Set_Class_Wide_Type (T, CW_Type);
14457       Set_Equivalent_Type (CW_Type, Empty);
14458
14459       --  The class-wide type of a class-wide type is itself (RM 3.9(14))
14460
14461       Set_Class_Wide_Type (CW_Type, CW_Type);
14462    end Make_Class_Wide_Type;
14463
14464    ----------------
14465    -- Make_Index --
14466    ----------------
14467
14468    procedure Make_Index
14469      (I            : Node_Id;
14470       Related_Nod  : Node_Id;
14471       Related_Id   : Entity_Id := Empty;
14472       Suffix_Index : Nat := 1)
14473    is
14474       R      : Node_Id;
14475       T      : Entity_Id;
14476       Def_Id : Entity_Id := Empty;
14477       Found  : Boolean := False;
14478
14479    begin
14480       --  For a discrete range used in a constrained array definition and
14481       --  defined by a range, an implicit conversion to the predefined type
14482       --  INTEGER is assumed if each bound is either a numeric literal, a named
14483       --  number, or an attribute, and the type of both bounds (prior to the
14484       --  implicit conversion) is the type universal_integer. Otherwise, both
14485       --  bounds must be of the same discrete type, other than universal
14486       --  integer; this type must be determinable independently of the
14487       --  context, but using the fact that the type must be discrete and that
14488       --  both bounds must have the same type.
14489
14490       --  Character literals also have a universal type in the absence of
14491       --  of additional context,  and are resolved to Standard_Character.
14492
14493       if Nkind (I) = N_Range then
14494
14495          --  The index is given by a range constraint. The bounds are known
14496          --  to be of a consistent type.
14497
14498          if not Is_Overloaded (I) then
14499             T := Etype (I);
14500
14501             --  For universal bounds, choose the specific predefined type
14502
14503             if T = Universal_Integer then
14504                T := Standard_Integer;
14505
14506             elsif T = Any_Character then
14507                Ambiguous_Character (Low_Bound (I));
14508
14509                T := Standard_Character;
14510             end if;
14511
14512          --  The node may be overloaded because some user-defined operators
14513          --  are available, but if a universal interpretation exists it is
14514          --  also the selected one.
14515
14516          elsif Universal_Interpretation (I) = Universal_Integer then
14517             T := Standard_Integer;
14518
14519          else
14520             T := Any_Type;
14521
14522             declare
14523                Ind : Interp_Index;
14524                It  : Interp;
14525
14526             begin
14527                Get_First_Interp (I, Ind, It);
14528                while Present (It.Typ) loop
14529                   if Is_Discrete_Type (It.Typ) then
14530
14531                      if Found
14532                        and then not Covers (It.Typ, T)
14533                        and then not Covers (T, It.Typ)
14534                      then
14535                         Error_Msg_N ("ambiguous bounds in discrete range", I);
14536                         exit;
14537                      else
14538                         T := It.Typ;
14539                         Found := True;
14540                      end if;
14541                   end if;
14542
14543                   Get_Next_Interp (Ind, It);
14544                end loop;
14545
14546                if T = Any_Type then
14547                   Error_Msg_N ("discrete type required for range", I);
14548                   Set_Etype (I, Any_Type);
14549                   return;
14550
14551                elsif T = Universal_Integer then
14552                   T := Standard_Integer;
14553                end if;
14554             end;
14555          end if;
14556
14557          if not Is_Discrete_Type (T) then
14558             Error_Msg_N ("discrete type required for range", I);
14559             Set_Etype (I, Any_Type);
14560             return;
14561          end if;
14562
14563          if Nkind (Low_Bound (I)) = N_Attribute_Reference
14564            and then Attribute_Name (Low_Bound (I)) = Name_First
14565            and then Is_Entity_Name (Prefix (Low_Bound (I)))
14566            and then Is_Type (Entity (Prefix (Low_Bound (I))))
14567            and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
14568          then
14569             --  The type of the index will be the type of the prefix, as long
14570             --  as the upper bound is 'Last of the same type.
14571
14572             Def_Id := Entity (Prefix (Low_Bound (I)));
14573
14574             if Nkind (High_Bound (I)) /= N_Attribute_Reference
14575               or else Attribute_Name (High_Bound (I)) /= Name_Last
14576               or else not Is_Entity_Name (Prefix (High_Bound (I)))
14577               or else Entity (Prefix (High_Bound (I))) /= Def_Id
14578             then
14579                Def_Id := Empty;
14580             end if;
14581          end if;
14582
14583          R := I;
14584          Process_Range_Expr_In_Decl (R, T);
14585
14586       elsif Nkind (I) = N_Subtype_Indication then
14587
14588          --  The index is given by a subtype with a range constraint
14589
14590          T :=  Base_Type (Entity (Subtype_Mark (I)));
14591
14592          if not Is_Discrete_Type (T) then
14593             Error_Msg_N ("discrete type required for range", I);
14594             Set_Etype (I, Any_Type);
14595             return;
14596          end if;
14597
14598          R := Range_Expression (Constraint (I));
14599
14600          Resolve (R, T);
14601          Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (I)));
14602
14603       elsif Nkind (I) = N_Attribute_Reference then
14604
14605          --  The parser guarantees that the attribute is a RANGE attribute
14606
14607          --  If the node denotes the range of a type mark, that is also the
14608          --  resulting type, and we do no need to create an Itype for it.
14609
14610          if Is_Entity_Name (Prefix (I))
14611            and then Comes_From_Source (I)
14612            and then Is_Type (Entity (Prefix (I)))
14613            and then Is_Discrete_Type (Entity (Prefix (I)))
14614          then
14615             Def_Id := Entity (Prefix (I));
14616          end if;
14617
14618          Analyze_And_Resolve (I);
14619          T := Etype (I);
14620          R := I;
14621
14622       --  If none of the above, must be a subtype. We convert this to a
14623       --  range attribute reference because in the case of declared first
14624       --  named subtypes, the types in the range reference can be different
14625       --  from the type of the entity. A range attribute normalizes the
14626       --  reference and obtains the correct types for the bounds.
14627
14628       --  This transformation is in the nature of an expansion, is only
14629       --  done if expansion is active. In particular, it is not done on
14630       --  formal generic types,  because we need to retain the name of the
14631       --  original index for instantiation purposes.
14632
14633       else
14634          if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
14635             Error_Msg_N ("invalid subtype mark in discrete range ", I);
14636             Set_Etype (I, Any_Integer);
14637             return;
14638
14639          else
14640             --  The type mark may be that of an incomplete type. It is only
14641             --  now that we can get the full view, previous analysis does
14642             --  not look specifically for a type mark.
14643
14644             Set_Entity (I, Get_Full_View (Entity (I)));
14645             Set_Etype  (I, Entity (I));
14646             Def_Id := Entity (I);
14647
14648             if not Is_Discrete_Type (Def_Id) then
14649                Error_Msg_N ("discrete type required for index", I);
14650                Set_Etype (I, Any_Type);
14651                return;
14652             end if;
14653          end if;
14654
14655          if Expander_Active then
14656             Rewrite (I,
14657               Make_Attribute_Reference (Sloc (I),
14658                 Attribute_Name => Name_Range,
14659                 Prefix         => Relocate_Node (I)));
14660
14661             --  The original was a subtype mark that does not freeze. This
14662             --  means that the rewritten version must not freeze either.
14663
14664             Set_Must_Not_Freeze (I);
14665             Set_Must_Not_Freeze (Prefix (I));
14666
14667             --  Is order critical??? if so, document why, if not
14668             --  use Analyze_And_Resolve
14669
14670             Analyze_And_Resolve (I);
14671             T := Etype (I);
14672             R := I;
14673
14674          --  If expander is inactive, type is legal, nothing else to construct
14675
14676          else
14677             return;
14678          end if;
14679       end if;
14680
14681       if not Is_Discrete_Type (T) then
14682          Error_Msg_N ("discrete type required for range", I);
14683          Set_Etype (I, Any_Type);
14684          return;
14685
14686       elsif T = Any_Type then
14687          Set_Etype (I, Any_Type);
14688          return;
14689       end if;
14690
14691       --  We will now create the appropriate Itype to describe the range, but
14692       --  first a check. If we originally had a subtype, then we just label
14693       --  the range with this subtype. Not only is there no need to construct
14694       --  a new subtype, but it is wrong to do so for two reasons:
14695
14696       --    1. A legality concern, if we have a subtype, it must not freeze,
14697       --       and the Itype would cause freezing incorrectly
14698
14699       --    2. An efficiency concern, if we created an Itype, it would not be
14700       --       recognized as the same type for the purposes of eliminating
14701       --       checks in some circumstances.
14702
14703       --  We signal this case by setting the subtype entity in Def_Id
14704
14705       if No (Def_Id) then
14706          Def_Id :=
14707            Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
14708          Set_Etype (Def_Id, Base_Type (T));
14709
14710          if Is_Signed_Integer_Type (T) then
14711             Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14712
14713          elsif Is_Modular_Integer_Type (T) then
14714             Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14715
14716          else
14717             Set_Ekind             (Def_Id, E_Enumeration_Subtype);
14718             Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14719             Set_First_Literal     (Def_Id, First_Literal (T));
14720          end if;
14721
14722          Set_Size_Info      (Def_Id,                  (T));
14723          Set_RM_Size        (Def_Id, RM_Size          (T));
14724          Set_First_Rep_Item (Def_Id, First_Rep_Item   (T));
14725
14726          Set_Scalar_Range   (Def_Id, R);
14727          Conditional_Delay  (Def_Id, T);
14728
14729          --  In the subtype indication case, if the immediate parent of the
14730          --  new subtype is non-static, then the subtype we create is non-
14731          --  static, even if its bounds are static.
14732
14733          if Nkind (I) = N_Subtype_Indication
14734            and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
14735          then
14736             Set_Is_Non_Static_Subtype (Def_Id);
14737          end if;
14738       end if;
14739
14740       --  Final step is to label the index with this constructed type
14741
14742       Set_Etype (I, Def_Id);
14743    end Make_Index;
14744
14745    ------------------------------
14746    -- Modular_Type_Declaration --
14747    ------------------------------
14748
14749    procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
14750       Mod_Expr : constant Node_Id := Expression (Def);
14751       M_Val    : Uint;
14752
14753       procedure Set_Modular_Size (Bits : Int);
14754       --  Sets RM_Size to Bits, and Esize to normal word size above this
14755
14756       ----------------------
14757       -- Set_Modular_Size --
14758       ----------------------
14759
14760       procedure Set_Modular_Size (Bits : Int) is
14761       begin
14762          Set_RM_Size (T, UI_From_Int (Bits));
14763
14764          if Bits <= 8 then
14765             Init_Esize (T, 8);
14766
14767          elsif Bits <= 16 then
14768             Init_Esize (T, 16);
14769
14770          elsif Bits <= 32 then
14771             Init_Esize (T, 32);
14772
14773          else
14774             Init_Esize (T, System_Max_Binary_Modulus_Power);
14775          end if;
14776       end Set_Modular_Size;
14777
14778    --  Start of processing for Modular_Type_Declaration
14779
14780    begin
14781       Analyze_And_Resolve (Mod_Expr, Any_Integer);
14782       Set_Etype (T, T);
14783       Set_Ekind (T, E_Modular_Integer_Type);
14784       Init_Alignment (T);
14785       Set_Is_Constrained (T);
14786
14787       if not Is_OK_Static_Expression (Mod_Expr) then
14788          Flag_Non_Static_Expr
14789            ("non-static expression used for modular type bound!", Mod_Expr);
14790          M_Val := 2 ** System_Max_Binary_Modulus_Power;
14791       else
14792          M_Val := Expr_Value (Mod_Expr);
14793       end if;
14794
14795       if M_Val < 1 then
14796          Error_Msg_N ("modulus value must be positive", Mod_Expr);
14797          M_Val := 2 ** System_Max_Binary_Modulus_Power;
14798       end if;
14799
14800       Set_Modulus (T, M_Val);
14801
14802       --   Create bounds for the modular type based on the modulus given in
14803       --   the type declaration and then analyze and resolve those bounds.
14804
14805       Set_Scalar_Range (T,
14806         Make_Range (Sloc (Mod_Expr),
14807           Low_Bound  =>
14808             Make_Integer_Literal (Sloc (Mod_Expr), 0),
14809           High_Bound =>
14810             Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
14811
14812       --  Properly analyze the literals for the range. We do this manually
14813       --  because we can't go calling Resolve, since we are resolving these
14814       --  bounds with the type, and this type is certainly not complete yet!
14815
14816       Set_Etype (Low_Bound  (Scalar_Range (T)), T);
14817       Set_Etype (High_Bound (Scalar_Range (T)), T);
14818       Set_Is_Static_Expression (Low_Bound  (Scalar_Range (T)));
14819       Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
14820
14821       --  Loop through powers of two to find number of bits required
14822
14823       for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
14824
14825          --  Binary case
14826
14827          if M_Val = 2 ** Bits then
14828             Set_Modular_Size (Bits);
14829             return;
14830
14831          --  Non-binary case
14832
14833          elsif M_Val < 2 ** Bits then
14834             Set_Non_Binary_Modulus (T);
14835
14836             if Bits > System_Max_Nonbinary_Modulus_Power then
14837                Error_Msg_Uint_1 :=
14838                  UI_From_Int (System_Max_Nonbinary_Modulus_Power);
14839                Error_Msg_F
14840                  ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
14841                Set_Modular_Size (System_Max_Binary_Modulus_Power);
14842                return;
14843
14844             else
14845                --  In the non-binary case, set size as per RM 13.3(55)
14846
14847                Set_Modular_Size (Bits);
14848                return;
14849             end if;
14850          end if;
14851
14852       end loop;
14853
14854       --  If we fall through, then the size exceed System.Max_Binary_Modulus
14855       --  so we just signal an error and set the maximum size.
14856
14857       Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
14858       Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
14859
14860       Set_Modular_Size (System_Max_Binary_Modulus_Power);
14861       Init_Alignment (T);
14862
14863    end Modular_Type_Declaration;
14864
14865    --------------------------
14866    -- New_Concatenation_Op --
14867    --------------------------
14868
14869    procedure New_Concatenation_Op (Typ : Entity_Id) is
14870       Loc : constant Source_Ptr := Sloc (Typ);
14871       Op  : Entity_Id;
14872
14873       function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
14874       --  Create abbreviated declaration for the formal of a predefined
14875       --  Operator 'Op' of type 'Typ'
14876
14877       --------------------
14878       -- Make_Op_Formal --
14879       --------------------
14880
14881       function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
14882          Formal : Entity_Id;
14883       begin
14884          Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
14885          Set_Etype (Formal, Typ);
14886          Set_Mechanism (Formal, Default_Mechanism);
14887          return Formal;
14888       end Make_Op_Formal;
14889
14890    --  Start of processing for New_Concatenation_Op
14891
14892    begin
14893       Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
14894
14895       Set_Ekind                   (Op, E_Operator);
14896       Set_Scope                   (Op, Current_Scope);
14897       Set_Etype                   (Op, Typ);
14898       Set_Homonym                 (Op, Get_Name_Entity_Id (Name_Op_Concat));
14899       Set_Is_Immediately_Visible  (Op);
14900       Set_Is_Intrinsic_Subprogram (Op);
14901       Set_Has_Completion          (Op);
14902       Append_Entity               (Op, Current_Scope);
14903
14904       Set_Name_Entity_Id (Name_Op_Concat, Op);
14905
14906       Append_Entity (Make_Op_Formal (Typ, Op), Op);
14907       Append_Entity (Make_Op_Formal (Typ, Op), Op);
14908    end New_Concatenation_Op;
14909
14910    -------------------------
14911    -- OK_For_Limited_Init --
14912    -------------------------
14913
14914    --  ???Check all calls of this, and compare the conditions under which it's
14915    --  called.
14916
14917    function OK_For_Limited_Init (Exp : Node_Id) return Boolean is
14918    begin
14919       return Ada_Version >= Ada_05
14920         and then not Debug_Flag_Dot_L
14921         and then OK_For_Limited_Init_In_05 (Exp);
14922    end OK_For_Limited_Init;
14923
14924    -------------------------------
14925    -- OK_For_Limited_Init_In_05 --
14926    -------------------------------
14927
14928    function OK_For_Limited_Init_In_05 (Exp : Node_Id) return Boolean is
14929    begin
14930       --  Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
14931       --  case of limited aggregates (including extension aggregates), and
14932       --  function calls. The function call may have been give in prefixed
14933       --  notation, in which case the original node is an indexed component.
14934
14935       case Nkind (Original_Node (Exp)) is
14936          when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
14937             return True;
14938
14939          when N_Qualified_Expression =>
14940             return
14941               OK_For_Limited_Init_In_05 (Expression (Original_Node (Exp)));
14942
14943          --  Ada 2005 (AI-251): If a class-wide interface object is initialized
14944          --  with a function call, the expander has rewritten the call into an
14945          --  N_Type_Conversion node to force displacement of the pointer to
14946          --  reference the component containing the secondary dispatch table.
14947          --  Otherwise a type conversion is not a legal context.
14948
14949          when N_Type_Conversion =>
14950             return not Comes_From_Source (Exp)
14951               and then
14952                 OK_For_Limited_Init_In_05 (Expression (Original_Node (Exp)));
14953
14954          when N_Indexed_Component | N_Selected_Component  =>
14955             return Nkind (Exp) = N_Function_Call;
14956
14957          --  A use of 'Input is a function call, hence allowed. Normally the
14958          --  attribute will be changed to a call, but the attribute by itself
14959          --  can occur with -gnatc.
14960
14961          when N_Attribute_Reference =>
14962             return Attribute_Name (Original_Node (Exp)) = Name_Input;
14963
14964          when others =>
14965             return False;
14966       end case;
14967    end OK_For_Limited_Init_In_05;
14968
14969    -------------------------------------------
14970    -- Ordinary_Fixed_Point_Type_Declaration --
14971    -------------------------------------------
14972
14973    procedure Ordinary_Fixed_Point_Type_Declaration
14974      (T   : Entity_Id;
14975       Def : Node_Id)
14976    is
14977       Loc           : constant Source_Ptr := Sloc (Def);
14978       Delta_Expr    : constant Node_Id    := Delta_Expression (Def);
14979       RRS           : constant Node_Id    := Real_Range_Specification (Def);
14980       Implicit_Base : Entity_Id;
14981       Delta_Val     : Ureal;
14982       Small_Val     : Ureal;
14983       Low_Val       : Ureal;
14984       High_Val      : Ureal;
14985
14986    begin
14987       Check_Restriction (No_Fixed_Point, Def);
14988
14989       --  Create implicit base type
14990
14991       Implicit_Base :=
14992         Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
14993       Set_Etype (Implicit_Base, Implicit_Base);
14994
14995       --  Analyze and process delta expression
14996
14997       Analyze_And_Resolve (Delta_Expr, Any_Real);
14998
14999       Check_Delta_Expression (Delta_Expr);
15000       Delta_Val := Expr_Value_R (Delta_Expr);
15001
15002       Set_Delta_Value (Implicit_Base, Delta_Val);
15003
15004       --  Compute default small from given delta, which is the largest power
15005       --  of two that does not exceed the given delta value.
15006
15007       declare
15008          Tmp   : Ureal;
15009          Scale : Int;
15010
15011       begin
15012          Tmp := Ureal_1;
15013          Scale := 0;
15014
15015          if Delta_Val < Ureal_1 then
15016             while Delta_Val < Tmp loop
15017                Tmp := Tmp / Ureal_2;
15018                Scale := Scale + 1;
15019             end loop;
15020
15021          else
15022             loop
15023                Tmp := Tmp * Ureal_2;
15024                exit when Tmp > Delta_Val;
15025                Scale := Scale - 1;
15026             end loop;
15027          end if;
15028
15029          Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
15030       end;
15031
15032       Set_Small_Value (Implicit_Base, Small_Val);
15033
15034       --  If no range was given, set a dummy range
15035
15036       if RRS <= Empty_Or_Error then
15037          Low_Val  := -Small_Val;
15038          High_Val := Small_Val;
15039
15040       --  Otherwise analyze and process given range
15041
15042       else
15043          declare
15044             Low  : constant Node_Id := Low_Bound  (RRS);
15045             High : constant Node_Id := High_Bound (RRS);
15046
15047          begin
15048             Analyze_And_Resolve (Low, Any_Real);
15049             Analyze_And_Resolve (High, Any_Real);
15050             Check_Real_Bound (Low);
15051             Check_Real_Bound (High);
15052
15053             --  Obtain and set the range
15054
15055             Low_Val  := Expr_Value_R (Low);
15056             High_Val := Expr_Value_R (High);
15057
15058             if Low_Val > High_Val then
15059                Error_Msg_NE ("?fixed point type& has null range", Def, T);
15060             end if;
15061          end;
15062       end if;
15063
15064       --  The range for both the implicit base and the declared first subtype
15065       --  cannot be set yet, so we use the special routine Set_Fixed_Range to
15066       --  set a temporary range in place. Note that the bounds of the base
15067       --  type will be widened to be symmetrical and to fill the available
15068       --  bits when the type is frozen.
15069
15070       --  We could do this with all discrete types, and probably should, but
15071       --  we absolutely have to do it for fixed-point, since the end-points
15072       --  of the range and the size are determined by the small value, which
15073       --  could be reset before the freeze point.
15074
15075       Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
15076       Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15077
15078       --  Complete definition of first subtype
15079
15080       Set_Ekind          (T, E_Ordinary_Fixed_Point_Subtype);
15081       Set_Etype          (T, Implicit_Base);
15082       Init_Size_Align    (T);
15083       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
15084       Set_Small_Value    (T, Small_Val);
15085       Set_Delta_Value    (T, Delta_Val);
15086       Set_Is_Constrained (T);
15087
15088    end Ordinary_Fixed_Point_Type_Declaration;
15089
15090    ----------------------------------------
15091    -- Prepare_Private_Subtype_Completion --
15092    ----------------------------------------
15093
15094    procedure Prepare_Private_Subtype_Completion
15095      (Id          : Entity_Id;
15096       Related_Nod : Node_Id)
15097    is
15098       Id_B   : constant Entity_Id := Base_Type (Id);
15099       Full_B : constant Entity_Id := Full_View (Id_B);
15100       Full   : Entity_Id;
15101
15102    begin
15103       if Present (Full_B) then
15104
15105          --  The Base_Type is already completed, we can complete the subtype
15106          --  now. We have to create a new entity with the same name, Thus we
15107          --  can't use Create_Itype.
15108
15109          --  This is messy, should be fixed ???
15110
15111          Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
15112          Set_Is_Itype (Full);
15113          Set_Associated_Node_For_Itype (Full, Related_Nod);
15114          Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
15115       end if;
15116
15117       --  The parent subtype may be private, but the base might not, in some
15118       --  nested instances. In that case, the subtype does not need to be
15119       --  exchanged. It would still be nice to make private subtypes and their
15120       --  bases consistent at all times ???
15121
15122       if Is_Private_Type (Id_B) then
15123          Append_Elmt (Id, Private_Dependents (Id_B));
15124       end if;
15125
15126    end Prepare_Private_Subtype_Completion;
15127
15128    ---------------------------
15129    -- Process_Discriminants --
15130    ---------------------------
15131
15132    procedure Process_Discriminants
15133      (N    : Node_Id;
15134       Prev : Entity_Id := Empty)
15135    is
15136       Elist               : constant Elist_Id := New_Elmt_List;
15137       Id                  : Node_Id;
15138       Discr               : Node_Id;
15139       Discr_Number        : Uint;
15140       Discr_Type          : Entity_Id;
15141       Default_Present     : Boolean := False;
15142       Default_Not_Present : Boolean := False;
15143
15144    begin
15145       --  A composite type other than an array type can have discriminants.
15146       --  On entry, the current scope is the composite type.
15147
15148       --  The discriminants are initially entered into the scope of the type
15149       --  via Enter_Name with the default Ekind of E_Void to prevent premature
15150       --  use, as explained at the end of this procedure.
15151
15152       Discr := First (Discriminant_Specifications (N));
15153       while Present (Discr) loop
15154          Enter_Name (Defining_Identifier (Discr));
15155
15156          --  For navigation purposes we add a reference to the discriminant
15157          --  in the entity for the type. If the current declaration is a
15158          --  completion, place references on the partial view. Otherwise the
15159          --  type is the current scope.
15160
15161          if Present (Prev) then
15162
15163             --  The references go on the partial view, if present. If the
15164             --  partial view has discriminants, the references have been
15165             --  generated already.
15166
15167             if not Has_Discriminants (Prev) then
15168                Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
15169             end if;
15170          else
15171             Generate_Reference
15172               (Current_Scope, Defining_Identifier (Discr), 'd');
15173          end if;
15174
15175          if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
15176             Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
15177
15178             --  Ada 2005 (AI-254)
15179
15180             if Present (Access_To_Subprogram_Definition
15181                          (Discriminant_Type (Discr)))
15182               and then Protected_Present (Access_To_Subprogram_Definition
15183                                            (Discriminant_Type (Discr)))
15184             then
15185                Discr_Type :=
15186                  Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
15187             end if;
15188
15189          else
15190             Find_Type (Discriminant_Type (Discr));
15191             Discr_Type := Etype (Discriminant_Type (Discr));
15192
15193             if Error_Posted (Discriminant_Type (Discr)) then
15194                Discr_Type := Any_Type;
15195             end if;
15196          end if;
15197
15198          if Is_Access_Type (Discr_Type) then
15199
15200             --  Ada 2005 (AI-230): Access discriminant allowed in non-limited
15201             --  record types
15202
15203             if Ada_Version < Ada_05 then
15204                Check_Access_Discriminant_Requires_Limited
15205                  (Discr, Discriminant_Type (Discr));
15206             end if;
15207
15208             if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
15209                Error_Msg_N
15210                  ("(Ada 83) access discriminant not allowed", Discr);
15211             end if;
15212
15213          elsif not Is_Discrete_Type (Discr_Type) then
15214             Error_Msg_N ("discriminants must have a discrete or access type",
15215               Discriminant_Type (Discr));
15216          end if;
15217
15218          Set_Etype (Defining_Identifier (Discr), Discr_Type);
15219
15220          --  If a discriminant specification includes the assignment compound
15221          --  delimiter followed by an expression, the expression is the default
15222          --  expression of the discriminant; the default expression must be of
15223          --  the type of the discriminant. (RM 3.7.1) Since this expression is
15224          --  a default expression, we do the special preanalysis, since this
15225          --  expression does not freeze (see "Handling of Default and Per-
15226          --  Object Expressions" in spec of package Sem).
15227
15228          if Present (Expression (Discr)) then
15229             Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
15230
15231             if Nkind (N) = N_Formal_Type_Declaration then
15232                Error_Msg_N
15233                  ("discriminant defaults not allowed for formal type",
15234                   Expression (Discr));
15235
15236             --  Tagged types cannot have defaulted discriminants, but a
15237             --  non-tagged private type with defaulted discriminants
15238             --   can have a tagged completion.
15239
15240             elsif Is_Tagged_Type (Current_Scope)
15241               and then Comes_From_Source (N)
15242             then
15243                Error_Msg_N
15244                  ("discriminants of tagged type cannot have defaults",
15245                   Expression (Discr));
15246
15247             else
15248                Default_Present := True;
15249                Append_Elmt (Expression (Discr), Elist);
15250
15251                --  Tag the defining identifiers for the discriminants with
15252                --  their corresponding default expressions from the tree.
15253
15254                Set_Discriminant_Default_Value
15255                  (Defining_Identifier (Discr), Expression (Discr));
15256             end if;
15257
15258          else
15259             Default_Not_Present := True;
15260          end if;
15261
15262          --  Ada 2005 (AI-231): Create an Itype that is a duplicate of
15263          --  Discr_Type but with the null-exclusion attribute
15264
15265          if Ada_Version >= Ada_05 then
15266
15267             --  Ada 2005 (AI-231): Static checks
15268
15269             if Can_Never_Be_Null (Discr_Type) then
15270                Null_Exclusion_Static_Checks (Discr);
15271
15272             elsif Is_Access_Type (Discr_Type)
15273               and then Null_Exclusion_Present (Discr)
15274
15275                --  No need to check itypes because in their case this check
15276                --  was done at their point of creation
15277
15278               and then not Is_Itype (Discr_Type)
15279             then
15280                if Can_Never_Be_Null (Discr_Type) then
15281                   Error_Msg_NE
15282                     ("`NOT NULL` not allowed (& already excludes null)",
15283                      Discr,
15284                      Discr_Type);
15285                end if;
15286
15287                Set_Etype (Defining_Identifier (Discr),
15288                  Create_Null_Excluding_Itype
15289                    (T           => Discr_Type,
15290                     Related_Nod => Discr));
15291             end if;
15292
15293             --  Ada 2005 (AI-402): access discriminants of nonlimited types
15294             --  can't have defaults. Synchronized types, or types that are
15295             --  explicitly limited are fine, but special tests apply to derived
15296             --  types in generics: in a generic body we have to assume the
15297             --  worst, and therefore defaults are not allowed if the parent is
15298             --  a generic formal private type (see ACATS B370001).
15299
15300             if Is_Access_Type (Discr_Type) then
15301                if Ekind (Discr_Type) /= E_Anonymous_Access_Type
15302                  or else not Default_Present
15303                  or else Is_Limited_Record (Current_Scope)
15304                  or else Is_Concurrent_Type (Current_Scope)
15305                  or else Is_Concurrent_Record_Type (Current_Scope)
15306                  or else Ekind (Current_Scope) = E_Limited_Private_Type
15307                then
15308                   if not Is_Derived_Type (Current_Scope)
15309                     or else not Is_Generic_Type (Etype (Current_Scope))
15310                     or else not In_Package_Body (Scope (Etype (Current_Scope)))
15311                     or else Limited_Present
15312                               (Type_Definition (Parent (Current_Scope)))
15313                   then
15314                      null;
15315
15316                   else
15317                      Error_Msg_N ("access discriminants of nonlimited types",
15318                          Expression (Discr));
15319                      Error_Msg_N ("\cannot have defaults", Expression (Discr));
15320                   end if;
15321
15322                elsif Present (Expression (Discr)) then
15323                   Error_Msg_N
15324                     ("(Ada 2005) access discriminants of nonlimited types",
15325                      Expression (Discr));
15326                   Error_Msg_N ("\cannot have defaults", Expression (Discr));
15327                end if;
15328             end if;
15329          end if;
15330
15331          Next (Discr);
15332       end loop;
15333
15334       --  An element list consisting of the default expressions of the
15335       --  discriminants is constructed in the above loop and used to set
15336       --  the Discriminant_Constraint attribute for the type. If an object
15337       --  is declared of this (record or task) type without any explicit
15338       --  discriminant constraint given, this element list will form the
15339       --  actual parameters for the corresponding initialization procedure
15340       --  for the type.
15341
15342       Set_Discriminant_Constraint (Current_Scope, Elist);
15343       Set_Stored_Constraint (Current_Scope, No_Elist);
15344
15345       --  Default expressions must be provided either for all or for none
15346       --  of the discriminants of a discriminant part. (RM 3.7.1)
15347
15348       if Default_Present and then Default_Not_Present then
15349          Error_Msg_N
15350            ("incomplete specification of defaults for discriminants", N);
15351       end if;
15352
15353       --  The use of the name of a discriminant is not allowed in default
15354       --  expressions of a discriminant part if the specification of the
15355       --  discriminant is itself given in the discriminant part. (RM 3.7.1)
15356
15357       --  To detect this, the discriminant names are entered initially with an
15358       --  Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
15359       --  attempt to use a void entity (for example in an expression that is
15360       --  type-checked) produces the error message: premature usage. Now after
15361       --  completing the semantic analysis of the discriminant part, we can set
15362       --  the Ekind of all the discriminants appropriately.
15363
15364       Discr := First (Discriminant_Specifications (N));
15365       Discr_Number := Uint_1;
15366       while Present (Discr) loop
15367          Id := Defining_Identifier (Discr);
15368          Set_Ekind (Id, E_Discriminant);
15369          Init_Component_Location (Id);
15370          Init_Esize (Id);
15371          Set_Discriminant_Number (Id, Discr_Number);
15372
15373          --  Make sure this is always set, even in illegal programs
15374
15375          Set_Corresponding_Discriminant (Id, Empty);
15376
15377          --  Initialize the Original_Record_Component to the entity itself.
15378          --  Inherit_Components will propagate the right value to
15379          --  discriminants in derived record types.
15380
15381          Set_Original_Record_Component (Id, Id);
15382
15383          --  Create the discriminal for the discriminant
15384
15385          Build_Discriminal (Id);
15386
15387          Next (Discr);
15388          Discr_Number := Discr_Number + 1;
15389       end loop;
15390
15391       Set_Has_Discriminants (Current_Scope);
15392    end Process_Discriminants;
15393
15394    -----------------------
15395    -- Process_Full_View --
15396    -----------------------
15397
15398    procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
15399       Priv_Parent : Entity_Id;
15400       Full_Parent : Entity_Id;
15401       Full_Indic  : Node_Id;
15402
15403       procedure Collect_Implemented_Interfaces
15404         (Typ    : Entity_Id;
15405          Ifaces : Elist_Id);
15406       --  Ada 2005: Gather all the interfaces that Typ directly or
15407       --  inherently implements. Duplicate entries are not added to
15408       --  the list Ifaces.
15409
15410       ------------------------------------
15411       -- Collect_Implemented_Interfaces --
15412       ------------------------------------
15413
15414       procedure Collect_Implemented_Interfaces
15415         (Typ    : Entity_Id;
15416          Ifaces : Elist_Id)
15417       is
15418          Iface      : Entity_Id;
15419          Iface_Elmt : Elmt_Id;
15420
15421       begin
15422          --  Abstract interfaces are only associated with tagged record types
15423
15424          if not Is_Tagged_Type (Typ)
15425            or else not Is_Record_Type (Typ)
15426          then
15427             return;
15428          end if;
15429
15430          --  Recursively climb to the ancestors
15431
15432          if Etype (Typ) /= Typ
15433
15434             --  Protect the frontend against wrong cyclic declarations like:
15435
15436             --     type B is new A with private;
15437             --     type C is new A with private;
15438             --  private
15439             --     type B is new C with null record;
15440             --     type C is new B with null record;
15441
15442            and then Etype (Typ) /= Priv_T
15443            and then Etype (Typ) /= Full_T
15444          then
15445             --  Keep separate the management of private type declarations
15446
15447             if Ekind (Typ) = E_Record_Type_With_Private then
15448
15449                --  Handle the following erronous case:
15450                --      type Private_Type is tagged private;
15451                --   private
15452                --      type Private_Type is new Type_Implementing_Iface;
15453
15454                if Present (Full_View (Typ))
15455                  and then Etype (Typ) /= Full_View (Typ)
15456                then
15457                   if Is_Interface (Etype (Typ)) then
15458                      Append_Unique_Elmt (Etype (Typ), Ifaces);
15459                   end if;
15460
15461                   Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
15462                end if;
15463
15464             --  Non-private types
15465
15466             else
15467                if Is_Interface (Etype (Typ)) then
15468                   Append_Unique_Elmt (Etype (Typ), Ifaces);
15469                end if;
15470
15471                Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
15472             end if;
15473          end if;
15474
15475          --  Handle entities in the list of abstract interfaces
15476
15477          if Present (Interfaces (Typ)) then
15478             Iface_Elmt := First_Elmt (Interfaces (Typ));
15479             while Present (Iface_Elmt) loop
15480                Iface := Node (Iface_Elmt);
15481
15482                pragma Assert (Is_Interface (Iface));
15483
15484                if not Contain_Interface (Iface, Ifaces) then
15485                   Append_Elmt (Iface, Ifaces);
15486                   Collect_Implemented_Interfaces (Iface, Ifaces);
15487                end if;
15488
15489                Next_Elmt (Iface_Elmt);
15490             end loop;
15491          end if;
15492       end Collect_Implemented_Interfaces;
15493
15494    --  Start of processing for Process_Full_View
15495
15496    begin
15497       --  First some sanity checks that must be done after semantic
15498       --  decoration of the full view and thus cannot be placed with other
15499       --  similar checks in Find_Type_Name
15500
15501       if not Is_Limited_Type (Priv_T)
15502         and then (Is_Limited_Type (Full_T)
15503                    or else Is_Limited_Composite (Full_T))
15504       then
15505          Error_Msg_N
15506            ("completion of nonlimited type cannot be limited", Full_T);
15507          Explain_Limited_Type (Full_T, Full_T);
15508
15509       elsif Is_Abstract_Type (Full_T)
15510         and then not Is_Abstract_Type (Priv_T)
15511       then
15512          Error_Msg_N
15513            ("completion of nonabstract type cannot be abstract", Full_T);
15514
15515       elsif Is_Tagged_Type (Priv_T)
15516         and then Is_Limited_Type (Priv_T)
15517         and then not Is_Limited_Type (Full_T)
15518       then
15519          --  If pragma CPP_Class was applied to the private declaration
15520          --  propagate the limitedness to the full-view
15521
15522          if Is_CPP_Class (Priv_T) then
15523             Set_Is_Limited_Record (Full_T);
15524
15525          --  GNAT allow its own definition of Limited_Controlled to disobey
15526          --  this rule in order in ease the implementation. The next test is
15527          --  safe because Root_Controlled is defined in a private system child
15528
15529          elsif Etype (Full_T) = Full_View (RTE (RE_Root_Controlled)) then
15530             Set_Is_Limited_Composite (Full_T);
15531          else
15532             Error_Msg_N
15533               ("completion of limited tagged type must be limited", Full_T);
15534          end if;
15535
15536       elsif Is_Generic_Type (Priv_T) then
15537          Error_Msg_N ("generic type cannot have a completion", Full_T);
15538       end if;
15539
15540       --  Check that ancestor interfaces of private and full views are
15541       --  consistent. We omit this check for synchronized types because
15542       --  they are performed on the corresponding record type when frozen.
15543
15544       if Ada_Version >= Ada_05
15545         and then Is_Tagged_Type (Priv_T)
15546         and then Is_Tagged_Type (Full_T)
15547         and then not Is_Concurrent_Type (Full_T)
15548       then
15549          declare
15550             Iface         : Entity_Id;
15551             Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
15552             Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
15553
15554          begin
15555             Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
15556             Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
15557
15558             --  Ada 2005 (AI-251): The partial view shall be a descendant of
15559             --  an interface type if and only if the full type is descendant
15560             --  of the interface type (AARM 7.3 (7.3/2).
15561
15562             Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
15563
15564             if Present (Iface) then
15565                Error_Msg_NE ("interface & not implemented by full type " &
15566                              "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
15567             end if;
15568
15569             Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
15570
15571             if Present (Iface) then
15572                Error_Msg_NE ("interface & not implemented by partial view " &
15573                              "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
15574             end if;
15575          end;
15576       end if;
15577
15578       if Is_Tagged_Type (Priv_T)
15579         and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15580         and then Is_Derived_Type (Full_T)
15581       then
15582          Priv_Parent := Etype (Priv_T);
15583
15584          --  The full view of a private extension may have been transformed
15585          --  into an unconstrained derived type declaration and a subtype
15586          --  declaration (see build_derived_record_type for details).
15587
15588          if Nkind (N) = N_Subtype_Declaration then
15589             Full_Indic  := Subtype_Indication (N);
15590             Full_Parent := Etype (Base_Type (Full_T));
15591          else
15592             Full_Indic  := Subtype_Indication (Type_Definition (N));
15593             Full_Parent := Etype (Full_T);
15594          end if;
15595
15596          --  Check that the parent type of the full type is a descendant of
15597          --  the ancestor subtype given in the private extension. If either
15598          --  entity has an Etype equal to Any_Type then we had some previous
15599          --  error situation [7.3(8)].
15600
15601          if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
15602             return;
15603
15604          --  Ada 2005 (AI-251): Interfaces in the full-typ can be given in
15605          --  any order. Therefore we don't have to check that its parent must
15606          --  be a descendant of the parent of the private type declaration.
15607
15608          elsif Is_Interface (Priv_Parent)
15609            and then Is_Interface (Full_Parent)
15610          then
15611             null;
15612
15613          --  Ada 2005 (AI-251): If the parent of the private type declaration
15614          --  is an interface there is no need to check that it is an ancestor
15615          --  of the associated full type declaration. The required tests for
15616          --  this case case are performed by Build_Derived_Record_Type.
15617
15618          elsif not Is_Interface (Base_Type (Priv_Parent))
15619            and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
15620          then
15621             Error_Msg_N
15622               ("parent of full type must descend from parent"
15623                   & " of private extension", Full_Indic);
15624
15625          --  Check the rules of 7.3(10): if the private extension inherits
15626          --  known discriminants, then the full type must also inherit those
15627          --  discriminants from the same (ancestor) type, and the parent
15628          --  subtype of the full type must be constrained if and only if
15629          --  the ancestor subtype of the private extension is constrained.
15630
15631          elsif No (Discriminant_Specifications (Parent (Priv_T)))
15632            and then not Has_Unknown_Discriminants (Priv_T)
15633            and then Has_Discriminants (Base_Type (Priv_Parent))
15634          then
15635             declare
15636                Priv_Indic  : constant Node_Id :=
15637                                Subtype_Indication (Parent (Priv_T));
15638
15639                Priv_Constr : constant Boolean :=
15640                                Is_Constrained (Priv_Parent)
15641                                  or else
15642                                    Nkind (Priv_Indic) = N_Subtype_Indication
15643                                  or else Is_Constrained (Entity (Priv_Indic));
15644
15645                Full_Constr : constant Boolean :=
15646                                Is_Constrained (Full_Parent)
15647                                  or else
15648                                    Nkind (Full_Indic) = N_Subtype_Indication
15649                                  or else Is_Constrained (Entity (Full_Indic));
15650
15651                Priv_Discr : Entity_Id;
15652                Full_Discr : Entity_Id;
15653
15654             begin
15655                Priv_Discr := First_Discriminant (Priv_Parent);
15656                Full_Discr := First_Discriminant (Full_Parent);
15657                while Present (Priv_Discr) and then Present (Full_Discr) loop
15658                   if Original_Record_Component (Priv_Discr) =
15659                      Original_Record_Component (Full_Discr)
15660                     or else
15661                      Corresponding_Discriminant (Priv_Discr) =
15662                      Corresponding_Discriminant (Full_Discr)
15663                   then
15664                      null;
15665                   else
15666                      exit;
15667                   end if;
15668
15669                   Next_Discriminant (Priv_Discr);
15670                   Next_Discriminant (Full_Discr);
15671                end loop;
15672
15673                if Present (Priv_Discr) or else Present (Full_Discr) then
15674                   Error_Msg_N
15675                     ("full view must inherit discriminants of the parent type"
15676                      & " used in the private extension", Full_Indic);
15677
15678                elsif Priv_Constr and then not Full_Constr then
15679                   Error_Msg_N
15680                     ("parent subtype of full type must be constrained",
15681                      Full_Indic);
15682
15683                elsif Full_Constr and then not Priv_Constr then
15684                   Error_Msg_N
15685                     ("parent subtype of full type must be unconstrained",
15686                      Full_Indic);
15687                end if;
15688             end;
15689
15690          --  Check the rules of 7.3(12): if a partial view has neither known
15691          --  or unknown discriminants, then the full type declaration shall
15692          --  define a definite subtype.
15693
15694          elsif      not Has_Unknown_Discriminants (Priv_T)
15695            and then not Has_Discriminants (Priv_T)
15696            and then not Is_Constrained (Full_T)
15697          then
15698             Error_Msg_N
15699               ("full view must define a constrained type if partial view"
15700                 & " has no discriminants", Full_T);
15701          end if;
15702
15703          --  ??????? Do we implement the following properly ?????
15704          --  If the ancestor subtype of a private extension has constrained
15705          --  discriminants, then the parent subtype of the full view shall
15706          --  impose a statically matching constraint on those discriminants
15707          --  [7.3(13)].
15708
15709       else
15710          --  For untagged types, verify that a type without discriminants
15711          --  is not completed with an unconstrained type.
15712
15713          if not Is_Indefinite_Subtype (Priv_T)
15714            and then Is_Indefinite_Subtype (Full_T)
15715          then
15716             Error_Msg_N ("full view of type must be definite subtype", Full_T);
15717          end if;
15718       end if;
15719
15720       --  AI-419: verify that the use of "limited" is consistent
15721
15722       declare
15723          Orig_Decl : constant Node_Id := Original_Node (N);
15724
15725       begin
15726          if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15727            and then not Limited_Present (Parent (Priv_T))
15728            and then not Synchronized_Present (Parent (Priv_T))
15729            and then Nkind (Orig_Decl) = N_Full_Type_Declaration
15730            and then Nkind
15731              (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
15732            and then Limited_Present (Type_Definition (Orig_Decl))
15733          then
15734             Error_Msg_N
15735               ("full view of non-limited extension cannot be limited", N);
15736          end if;
15737       end;
15738
15739       --  Ada 2005 (AI-443): A synchronized private extension must be
15740       --  completed by a task or protected type.
15741
15742       if Ada_Version >= Ada_05
15743         and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15744         and then Synchronized_Present (Parent (Priv_T))
15745         and then not Is_Concurrent_Type (Full_T)
15746       then
15747          Error_Msg_N ("full view of synchronized extension must " &
15748                       "be synchronized type", N);
15749       end if;
15750
15751       --  Ada 2005 AI-363: if the full view has discriminants with
15752       --  defaults, it is illegal to declare constrained access subtypes
15753       --  whose designated type is the current type. This allows objects
15754       --  of the type that are declared in the heap to be unconstrained.
15755
15756       if not Has_Unknown_Discriminants (Priv_T)
15757         and then not Has_Discriminants (Priv_T)
15758         and then Has_Discriminants (Full_T)
15759         and then
15760           Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
15761       then
15762          Set_Has_Constrained_Partial_View (Full_T);
15763          Set_Has_Constrained_Partial_View (Priv_T);
15764       end if;
15765
15766       --  Create a full declaration for all its subtypes recorded in
15767       --  Private_Dependents and swap them similarly to the base type. These
15768       --  are subtypes that have been define before the full declaration of
15769       --  the private type. We also swap the entry in Private_Dependents list
15770       --  so we can properly restore the private view on exit from the scope.
15771
15772       declare
15773          Priv_Elmt : Elmt_Id;
15774          Priv      : Entity_Id;
15775          Full      : Entity_Id;
15776
15777       begin
15778          Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
15779          while Present (Priv_Elmt) loop
15780             Priv := Node (Priv_Elmt);
15781
15782             if Ekind (Priv) = E_Private_Subtype
15783               or else Ekind (Priv) = E_Limited_Private_Subtype
15784               or else Ekind (Priv) = E_Record_Subtype_With_Private
15785             then
15786                Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
15787                Set_Is_Itype (Full);
15788                Set_Parent (Full, Parent (Priv));
15789                Set_Associated_Node_For_Itype (Full, N);
15790
15791                --  Now we need to complete the private subtype, but since the
15792                --  base type has already been swapped, we must also swap the
15793                --  subtypes (and thus, reverse the arguments in the call to
15794                --  Complete_Private_Subtype).
15795
15796                Copy_And_Swap (Priv, Full);
15797                Complete_Private_Subtype (Full, Priv, Full_T, N);
15798                Replace_Elmt (Priv_Elmt, Full);
15799             end if;
15800
15801             Next_Elmt (Priv_Elmt);
15802          end loop;
15803       end;
15804
15805       --  If the private view was tagged, copy the new primitive operations
15806       --  from the private view to the full view.
15807
15808       --  Note: Subprograms covering interface primitives were previously
15809       --  propagated to the full view by Derive_Progenitor_Primitives
15810
15811       if Is_Tagged_Type (Full_T)
15812         and then not Is_Concurrent_Type (Full_T)
15813       then
15814          declare
15815             Priv_List : Elist_Id;
15816             Full_List : constant Elist_Id := Primitive_Operations (Full_T);
15817             P1, P2    : Elmt_Id;
15818             Prim      : Entity_Id;
15819             D_Type    : Entity_Id;
15820
15821          begin
15822             if Is_Tagged_Type (Priv_T) then
15823                Priv_List := Primitive_Operations (Priv_T);
15824
15825                P1 := First_Elmt (Priv_List);
15826                while Present (P1) loop
15827                   Prim := Node (P1);
15828
15829                   --  Transfer explicit primitives, not those inherited from
15830                   --  parent of partial view, which will be re-inherited on
15831                   --  the full view.
15832
15833                   if Comes_From_Source (Prim) then
15834                      P2 := First_Elmt (Full_List);
15835                      while Present (P2) and then Node (P2) /= Prim loop
15836                         Next_Elmt (P2);
15837                      end loop;
15838
15839                      --  If not found, that is a new one
15840
15841                      if No (P2) then
15842                         Append_Elmt (Prim, Full_List);
15843                      end if;
15844                   end if;
15845
15846                   Next_Elmt (P1);
15847                end loop;
15848
15849             else
15850                --  In this case the partial view is untagged, so here we locate
15851                --  all of the earlier primitives that need to be treated as
15852                --  dispatching (those that appear between the two views). Note
15853                --  that these additional operations must all be new operations
15854                --  (any earlier operations that override inherited operations
15855                --  of the full view will already have been inserted in the
15856                --  primitives list, marked by Check_Operation_From_Private_View
15857                --  as dispatching. Note that implicit "/=" operators are
15858                --  excluded from being added to the primitives list since they
15859                --  shouldn't be treated as dispatching (tagged "/=" is handled
15860                --  specially).
15861
15862                Prim := Next_Entity (Full_T);
15863                while Present (Prim) and then Prim /= Priv_T loop
15864                   if Ekind (Prim) = E_Procedure
15865                        or else
15866                      Ekind (Prim) = E_Function
15867                   then
15868
15869                      D_Type := Find_Dispatching_Type (Prim);
15870
15871                      if D_Type = Full_T
15872                        and then (Chars (Prim) /= Name_Op_Ne
15873                                   or else Comes_From_Source (Prim))
15874                      then
15875                         Check_Controlling_Formals (Full_T, Prim);
15876
15877                         if not Is_Dispatching_Operation (Prim) then
15878                            Append_Elmt (Prim, Full_List);
15879                            Set_Is_Dispatching_Operation (Prim, True);
15880                            Set_DT_Position (Prim, No_Uint);
15881                         end if;
15882
15883                      elsif Is_Dispatching_Operation (Prim)
15884                        and then D_Type  /= Full_T
15885                      then
15886
15887                         --  Verify that it is not otherwise controlled by a
15888                         --  formal or a return value of type T.
15889
15890                         Check_Controlling_Formals (D_Type, Prim);
15891                      end if;
15892                   end if;
15893
15894                   Next_Entity (Prim);
15895                end loop;
15896             end if;
15897
15898             --  For the tagged case, the two views can share the same
15899             --  Primitive Operation list and the same class wide type.
15900             --  Update attributes of the class-wide type which depend on
15901             --  the full declaration.
15902
15903             if Is_Tagged_Type (Priv_T) then
15904                Set_Primitive_Operations (Priv_T, Full_List);
15905                Set_Class_Wide_Type
15906                  (Base_Type (Full_T), Class_Wide_Type (Priv_T));
15907
15908                Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
15909             end if;
15910          end;
15911       end if;
15912
15913       --  Ada 2005 AI 161: Check preelaboratable initialization consistency
15914
15915       if Known_To_Have_Preelab_Init (Priv_T) then
15916
15917          --  Case where there is a pragma Preelaborable_Initialization. We
15918          --  always allow this in predefined units, which is a bit of a kludge,
15919          --  but it means we don't have to struggle to meet the requirements in
15920          --  the RM for having Preelaborable Initialization. Otherwise we
15921          --  require that the type meets the RM rules. But we can't check that
15922          --  yet, because of the rule about overriding Ininitialize, so we
15923          --  simply set a flag that will be checked at freeze time.
15924
15925          if not In_Predefined_Unit (Full_T) then
15926             Set_Must_Have_Preelab_Init (Full_T);
15927          end if;
15928       end if;
15929
15930       --  If pragma CPP_Class was applied to the private type declaration,
15931       --  propagate it now to the full type declaration.
15932
15933       if Is_CPP_Class (Priv_T) then
15934          Set_Is_CPP_Class (Full_T);
15935          Set_Convention   (Full_T, Convention_CPP);
15936       end if;
15937    end Process_Full_View;
15938
15939    -----------------------------------
15940    -- Process_Incomplete_Dependents --
15941    -----------------------------------
15942
15943    procedure Process_Incomplete_Dependents
15944      (N      : Node_Id;
15945       Full_T : Entity_Id;
15946       Inc_T  : Entity_Id)
15947    is
15948       Inc_Elmt : Elmt_Id;
15949       Priv_Dep : Entity_Id;
15950       New_Subt : Entity_Id;
15951
15952       Disc_Constraint : Elist_Id;
15953
15954    begin
15955       if No (Private_Dependents (Inc_T)) then
15956          return;
15957       end if;
15958
15959       --  Itypes that may be generated by the completion of an incomplete
15960       --  subtype are not used by the back-end and not attached to the tree.
15961       --  They are created only for constraint-checking purposes.
15962
15963       Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
15964       while Present (Inc_Elmt) loop
15965          Priv_Dep := Node (Inc_Elmt);
15966
15967          if Ekind (Priv_Dep) = E_Subprogram_Type then
15968
15969             --  An Access_To_Subprogram type may have a return type or a
15970             --  parameter type that is incomplete. Replace with the full view.
15971
15972             if Etype (Priv_Dep) = Inc_T then
15973                Set_Etype (Priv_Dep, Full_T);
15974             end if;
15975
15976             declare
15977                Formal : Entity_Id;
15978
15979             begin
15980                Formal := First_Formal (Priv_Dep);
15981                while Present (Formal) loop
15982                   if Etype (Formal) = Inc_T then
15983                      Set_Etype (Formal, Full_T);
15984                   end if;
15985
15986                   Next_Formal (Formal);
15987                end loop;
15988             end;
15989
15990          elsif Is_Overloadable (Priv_Dep) then
15991
15992             --  A protected operation is never dispatching: only its
15993             --  wrapper operation (which has convention Ada) is.
15994
15995             if Is_Tagged_Type (Full_T)
15996               and then Convention (Priv_Dep) /= Convention_Protected
15997             then
15998
15999                --  Subprogram has an access parameter whose designated type
16000                --  was incomplete. Reexamine declaration now, because it may
16001                --  be a primitive operation of the full type.
16002
16003                Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
16004                Set_Is_Dispatching_Operation (Priv_Dep);
16005                Check_Controlling_Formals (Full_T, Priv_Dep);
16006             end if;
16007
16008          elsif Ekind (Priv_Dep) = E_Subprogram_Body then
16009
16010             --  Can happen during processing of a body before the completion
16011             --  of a TA type. Ignore, because spec is also on dependent list.
16012
16013             return;
16014
16015          --  Ada 2005 (AI-412): Transform a regular incomplete subtype into a
16016          --  corresponding subtype of the full view.
16017
16018          elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
16019             Set_Subtype_Indication
16020               (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
16021             Set_Etype (Priv_Dep, Full_T);
16022             Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
16023             Set_Analyzed (Parent (Priv_Dep), False);
16024
16025             --  Reanalyze the declaration, suppressing the call to
16026             --  Enter_Name to avoid duplicate names.
16027
16028             Analyze_Subtype_Declaration
16029               (N    => Parent (Priv_Dep),
16030                Skip => True);
16031
16032          --  Dependent is a subtype
16033
16034          else
16035             --  We build a new subtype indication using the full view of the
16036             --  incomplete parent. The discriminant constraints have been
16037             --  elaborated already at the point of the subtype declaration.
16038
16039             New_Subt := Create_Itype (E_Void, N);
16040
16041             if Has_Discriminants (Full_T) then
16042                Disc_Constraint := Discriminant_Constraint (Priv_Dep);
16043             else
16044                Disc_Constraint := No_Elist;
16045             end if;
16046
16047             Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
16048             Set_Full_View (Priv_Dep, New_Subt);
16049          end if;
16050
16051          Next_Elmt (Inc_Elmt);
16052       end loop;
16053    end Process_Incomplete_Dependents;
16054
16055    --------------------------------
16056    -- Process_Range_Expr_In_Decl --
16057    --------------------------------
16058
16059    procedure Process_Range_Expr_In_Decl
16060      (R           : Node_Id;
16061       T           : Entity_Id;
16062       Check_List  : List_Id := Empty_List;
16063       R_Check_Off : Boolean := False)
16064    is
16065       Lo, Hi    : Node_Id;
16066       R_Checks  : Check_Result;
16067       Type_Decl : Node_Id;
16068       Def_Id    : Entity_Id;
16069
16070    begin
16071       Analyze_And_Resolve (R, Base_Type (T));
16072
16073       if Nkind (R) = N_Range then
16074          Lo := Low_Bound (R);
16075          Hi := High_Bound (R);
16076
16077          --  We need to ensure validity of the bounds here, because if we
16078          --  go ahead and do the expansion, then the expanded code will get
16079          --  analyzed with range checks suppressed and we miss the check.
16080
16081          Validity_Check_Range (R);
16082
16083          --  If there were errors in the declaration, try and patch up some
16084          --  common mistakes in the bounds. The cases handled are literals
16085          --  which are Integer where the expected type is Real and vice versa.
16086          --  These corrections allow the compilation process to proceed further
16087          --  along since some basic assumptions of the format of the bounds
16088          --  are guaranteed.
16089
16090          if Etype (R) = Any_Type then
16091
16092             if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
16093                Rewrite (Lo,
16094                  Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
16095
16096             elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
16097                Rewrite (Hi,
16098                  Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
16099
16100             elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
16101                Rewrite (Lo,
16102                  Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
16103
16104             elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
16105                Rewrite (Hi,
16106                  Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
16107             end if;
16108
16109             Set_Etype (Lo, T);
16110             Set_Etype (Hi, T);
16111          end if;
16112
16113          --  If the bounds of the range have been mistakenly given as string
16114          --  literals (perhaps in place of character literals), then an error
16115          --  has already been reported, but we rewrite the string literal as a
16116          --  bound of the range's type to avoid blowups in later processing
16117          --  that looks at static values.
16118
16119          if Nkind (Lo) = N_String_Literal then
16120             Rewrite (Lo,
16121               Make_Attribute_Reference (Sloc (Lo),
16122                 Attribute_Name => Name_First,
16123                 Prefix => New_Reference_To (T, Sloc (Lo))));
16124             Analyze_And_Resolve (Lo);
16125          end if;
16126
16127          if Nkind (Hi) = N_String_Literal then
16128             Rewrite (Hi,
16129               Make_Attribute_Reference (Sloc (Hi),
16130                 Attribute_Name => Name_First,
16131                 Prefix => New_Reference_To (T, Sloc (Hi))));
16132             Analyze_And_Resolve (Hi);
16133          end if;
16134
16135          --  If bounds aren't scalar at this point then exit, avoiding
16136          --  problems with further processing of the range in this procedure.
16137
16138          if not Is_Scalar_Type (Etype (Lo)) then
16139             return;
16140          end if;
16141
16142          --  Resolve (actually Sem_Eval) has checked that the bounds are in
16143          --  then range of the base type. Here we check whether the bounds
16144          --  are in the range of the subtype itself. Note that if the bounds
16145          --  represent the null range the Constraint_Error exception should
16146          --  not be raised.
16147
16148          --  ??? The following code should be cleaned up as follows
16149
16150          --  1. The Is_Null_Range (Lo, Hi) test should disappear since it
16151          --     is done in the call to Range_Check (R, T); below
16152
16153          --  2. The use of R_Check_Off should be investigated and possibly
16154          --     removed, this would clean up things a bit.
16155
16156          if Is_Null_Range (Lo, Hi) then
16157             null;
16158
16159          else
16160             --  Capture values of bounds and generate temporaries for them
16161             --  if needed, before applying checks, since checks may cause
16162             --  duplication of the expression without forcing evaluation.
16163
16164             if Expander_Active then
16165                Force_Evaluation (Lo);
16166                Force_Evaluation (Hi);
16167             end if;
16168
16169             --  We use a flag here instead of suppressing checks on the
16170             --  type because the type we check against isn't necessarily
16171             --  the place where we put the check.
16172
16173             if not R_Check_Off then
16174                R_Checks := Get_Range_Checks (R, T);
16175
16176                --  Look up tree to find an appropriate insertion point.
16177                --  This seems really junk code, and very brittle, couldn't
16178                --  we just use an insert actions call of some kind ???
16179
16180                Type_Decl := Parent (R);
16181                while Present (Type_Decl) and then not
16182                  (Nkind_In (Type_Decl, N_Full_Type_Declaration,
16183                                        N_Subtype_Declaration,
16184                                        N_Loop_Statement,
16185                                        N_Task_Type_Declaration)
16186                     or else
16187                   Nkind_In (Type_Decl, N_Single_Task_Declaration,
16188                                        N_Protected_Type_Declaration,
16189                                        N_Single_Protected_Declaration))
16190                loop
16191                   Type_Decl := Parent (Type_Decl);
16192                end loop;
16193
16194                --  Why would Type_Decl not be present???  Without this test,
16195                --  short regression tests fail.
16196
16197                if Present (Type_Decl) then
16198
16199                   --  Case of loop statement (more comments ???)
16200
16201                   if Nkind (Type_Decl) = N_Loop_Statement then
16202                      declare
16203                         Indic : Node_Id;
16204
16205                      begin
16206                         Indic := Parent (R);
16207                         while Present (Indic)
16208                           and then Nkind (Indic) /= N_Subtype_Indication
16209                         loop
16210                            Indic := Parent (Indic);
16211                         end loop;
16212
16213                         if Present (Indic) then
16214                            Def_Id := Etype (Subtype_Mark (Indic));
16215
16216                            Insert_Range_Checks
16217                              (R_Checks,
16218                               Type_Decl,
16219                               Def_Id,
16220                               Sloc (Type_Decl),
16221                               R,
16222                               Do_Before => True);
16223                         end if;
16224                      end;
16225
16226                   --  All other cases (more comments ???)
16227
16228                   else
16229                      Def_Id := Defining_Identifier (Type_Decl);
16230
16231                      if (Ekind (Def_Id) = E_Record_Type
16232                           and then Depends_On_Discriminant (R))
16233                        or else
16234                         (Ekind (Def_Id) = E_Protected_Type
16235                           and then Has_Discriminants (Def_Id))
16236                      then
16237                         Append_Range_Checks
16238                           (R_Checks, Check_List, Def_Id, Sloc (Type_Decl), R);
16239
16240                      else
16241                         Insert_Range_Checks
16242                           (R_Checks, Type_Decl, Def_Id, Sloc (Type_Decl), R);
16243
16244                      end if;
16245                   end if;
16246                end if;
16247             end if;
16248          end if;
16249
16250       elsif Expander_Active then
16251          Get_Index_Bounds (R, Lo, Hi);
16252          Force_Evaluation (Lo);
16253          Force_Evaluation (Hi);
16254       end if;
16255    end Process_Range_Expr_In_Decl;
16256
16257    --------------------------------------
16258    -- Process_Real_Range_Specification --
16259    --------------------------------------
16260
16261    procedure Process_Real_Range_Specification (Def : Node_Id) is
16262       Spec : constant Node_Id := Real_Range_Specification (Def);
16263       Lo   : Node_Id;
16264       Hi   : Node_Id;
16265       Err  : Boolean := False;
16266
16267       procedure Analyze_Bound (N : Node_Id);
16268       --  Analyze and check one bound
16269
16270       -------------------
16271       -- Analyze_Bound --
16272       -------------------
16273
16274       procedure Analyze_Bound (N : Node_Id) is
16275       begin
16276          Analyze_And_Resolve (N, Any_Real);
16277
16278          if not Is_OK_Static_Expression (N) then
16279             Flag_Non_Static_Expr
16280               ("bound in real type definition is not static!", N);
16281             Err := True;
16282          end if;
16283       end Analyze_Bound;
16284
16285    --  Start of processing for Process_Real_Range_Specification
16286
16287    begin
16288       if Present (Spec) then
16289          Lo := Low_Bound (Spec);
16290          Hi := High_Bound (Spec);
16291          Analyze_Bound (Lo);
16292          Analyze_Bound (Hi);
16293
16294          --  If error, clear away junk range specification
16295
16296          if Err then
16297             Set_Real_Range_Specification (Def, Empty);
16298          end if;
16299       end if;
16300    end Process_Real_Range_Specification;
16301
16302    ---------------------
16303    -- Process_Subtype --
16304    ---------------------
16305
16306    function Process_Subtype
16307      (S           : Node_Id;
16308       Related_Nod : Node_Id;
16309       Related_Id  : Entity_Id := Empty;
16310       Suffix      : Character := ' ') return Entity_Id
16311    is
16312       P               : Node_Id;
16313       Def_Id          : Entity_Id;
16314       Error_Node      : Node_Id;
16315       Full_View_Id    : Entity_Id;
16316       Subtype_Mark_Id : Entity_Id;
16317
16318       May_Have_Null_Exclusion : Boolean;
16319
16320       procedure Check_Incomplete (T : Entity_Id);
16321       --  Called to verify that an incomplete type is not used prematurely
16322
16323       ----------------------
16324       -- Check_Incomplete --
16325       ----------------------
16326
16327       procedure Check_Incomplete (T : Entity_Id) is
16328       begin
16329          --  Ada 2005 (AI-412): Incomplete subtypes are legal
16330
16331          if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
16332            and then
16333              not (Ada_Version >= Ada_05
16334                     and then
16335                        (Nkind (Parent (T)) = N_Subtype_Declaration
16336                           or else
16337                             (Nkind (Parent (T)) = N_Subtype_Indication
16338                                and then Nkind (Parent (Parent (T))) =
16339                                           N_Subtype_Declaration)))
16340          then
16341             Error_Msg_N ("invalid use of type before its full declaration", T);
16342          end if;
16343       end Check_Incomplete;
16344
16345    --  Start of processing for Process_Subtype
16346
16347    begin
16348       --  Case of no constraints present
16349
16350       if Nkind (S) /= N_Subtype_Indication then
16351          Find_Type (S);
16352          Check_Incomplete (S);
16353          P := Parent (S);
16354
16355          --  Ada 2005 (AI-231): Static check
16356
16357          if Ada_Version >= Ada_05
16358            and then Present (P)
16359            and then Null_Exclusion_Present (P)
16360            and then Nkind (P) /= N_Access_To_Object_Definition
16361            and then not Is_Access_Type (Entity (S))
16362          then
16363             Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
16364          end if;
16365
16366          --  The following is ugly, can't we have a range or even a flag???
16367
16368          May_Have_Null_Exclusion :=
16369            Nkind_In (P, N_Access_Definition,
16370                         N_Access_Function_Definition,
16371                         N_Access_Procedure_Definition,
16372                         N_Access_To_Object_Definition,
16373                         N_Allocator,
16374                         N_Component_Definition)
16375              or else
16376            Nkind_In (P, N_Derived_Type_Definition,
16377                         N_Discriminant_Specification,
16378                         N_Object_Declaration,
16379                         N_Parameter_Specification,
16380                         N_Subtype_Declaration);
16381
16382          --  Create an Itype that is a duplicate of Entity (S) but with the
16383          --  null-exclusion attribute
16384
16385          if May_Have_Null_Exclusion
16386            and then Is_Access_Type (Entity (S))
16387            and then Null_Exclusion_Present (P)
16388
16389             --  No need to check the case of an access to object definition.
16390             --  It is correct to define double not-null pointers.
16391
16392             --  Example:
16393             --     type Not_Null_Int_Ptr is not null access Integer;
16394             --     type Acc is not null access Not_Null_Int_Ptr;
16395
16396            and then Nkind (P) /= N_Access_To_Object_Definition
16397          then
16398             if Can_Never_Be_Null (Entity (S)) then
16399                case Nkind (Related_Nod) is
16400                   when N_Full_Type_Declaration =>
16401                      if Nkind (Type_Definition (Related_Nod))
16402                        in N_Array_Type_Definition
16403                      then
16404                         Error_Node :=
16405                           Subtype_Indication
16406                             (Component_Definition
16407                              (Type_Definition (Related_Nod)));
16408                      else
16409                         Error_Node :=
16410                           Subtype_Indication (Type_Definition (Related_Nod));
16411                      end if;
16412
16413                   when N_Subtype_Declaration =>
16414                      Error_Node := Subtype_Indication (Related_Nod);
16415
16416                   when N_Object_Declaration =>
16417                      Error_Node := Object_Definition (Related_Nod);
16418
16419                   when N_Component_Declaration =>
16420                      Error_Node :=
16421                        Subtype_Indication (Component_Definition (Related_Nod));
16422
16423                   when others =>
16424                      pragma Assert (False);
16425                      Error_Node := Related_Nod;
16426                end case;
16427
16428                Error_Msg_NE
16429                  ("`NOT NULL` not allowed (& already excludes null)",
16430                   Error_Node,
16431                   Entity (S));
16432             end if;
16433
16434             Set_Etype  (S,
16435               Create_Null_Excluding_Itype
16436                 (T           => Entity (S),
16437                  Related_Nod => P));
16438             Set_Entity (S, Etype (S));
16439          end if;
16440
16441          return Entity (S);
16442
16443       --  Case of constraint present, so that we have an N_Subtype_Indication
16444       --  node (this node is created only if constraints are present).
16445
16446       else
16447          Find_Type (Subtype_Mark (S));
16448
16449          if Nkind (Parent (S)) /= N_Access_To_Object_Definition
16450            and then not
16451             (Nkind (Parent (S)) = N_Subtype_Declaration
16452               and then Is_Itype (Defining_Identifier (Parent (S))))
16453          then
16454             Check_Incomplete (Subtype_Mark (S));
16455          end if;
16456
16457          P := Parent (S);
16458          Subtype_Mark_Id := Entity (Subtype_Mark (S));
16459
16460          --  Explicit subtype declaration case
16461
16462          if Nkind (P) = N_Subtype_Declaration then
16463             Def_Id := Defining_Identifier (P);
16464
16465          --  Explicit derived type definition case
16466
16467          elsif Nkind (P) = N_Derived_Type_Definition then
16468             Def_Id := Defining_Identifier (Parent (P));
16469
16470          --  Implicit case, the Def_Id must be created as an implicit type.
16471          --  The one exception arises in the case of concurrent types, array
16472          --  and access types, where other subsidiary implicit types may be
16473          --  created and must appear before the main implicit type. In these
16474          --  cases we leave Def_Id set to Empty as a signal that Create_Itype
16475          --  has not yet been called to create Def_Id.
16476
16477          else
16478             if Is_Array_Type (Subtype_Mark_Id)
16479               or else Is_Concurrent_Type (Subtype_Mark_Id)
16480               or else Is_Access_Type (Subtype_Mark_Id)
16481             then
16482                Def_Id := Empty;
16483
16484             --  For the other cases, we create a new unattached Itype,
16485             --  and set the indication to ensure it gets attached later.
16486
16487             else
16488                Def_Id :=
16489                  Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
16490             end if;
16491          end if;
16492
16493          --  If the kind of constraint is invalid for this kind of type,
16494          --  then give an error, and then pretend no constraint was given.
16495
16496          if not Is_Valid_Constraint_Kind
16497                    (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
16498          then
16499             Error_Msg_N
16500               ("incorrect constraint for this kind of type", Constraint (S));
16501
16502             Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16503
16504             --  Set Ekind of orphan itype, to prevent cascaded errors
16505
16506             if Present (Def_Id) then
16507                Set_Ekind (Def_Id, Ekind (Any_Type));
16508             end if;
16509
16510             --  Make recursive call, having got rid of the bogus constraint
16511
16512             return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
16513          end if;
16514
16515          --  Remaining processing depends on type
16516
16517          case Ekind (Subtype_Mark_Id) is
16518             when Access_Kind =>
16519                Constrain_Access (Def_Id, S, Related_Nod);
16520
16521                if Expander_Active
16522                  and then  Is_Itype (Designated_Type (Def_Id))
16523                  and then Nkind (Related_Nod) = N_Subtype_Declaration
16524                  and then not Is_Incomplete_Type (Designated_Type (Def_Id))
16525                then
16526                   Build_Itype_Reference
16527                     (Designated_Type (Def_Id), Related_Nod);
16528                end if;
16529
16530             when Array_Kind =>
16531                Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
16532
16533             when Decimal_Fixed_Point_Kind =>
16534                Constrain_Decimal (Def_Id, S);
16535
16536             when Enumeration_Kind =>
16537                Constrain_Enumeration (Def_Id, S);
16538
16539             when Ordinary_Fixed_Point_Kind =>
16540                Constrain_Ordinary_Fixed (Def_Id, S);
16541
16542             when Float_Kind =>
16543                Constrain_Float (Def_Id, S);
16544
16545             when Integer_Kind =>
16546                Constrain_Integer (Def_Id, S);
16547
16548             when E_Record_Type     |
16549                  E_Record_Subtype  |
16550                  Class_Wide_Kind   |
16551                  E_Incomplete_Type =>
16552                Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
16553
16554             when Private_Kind =>
16555                Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
16556                Set_Private_Dependents (Def_Id, New_Elmt_List);
16557
16558                --  In case of an invalid constraint prevent further processing
16559                --  since the type constructed is missing expected fields.
16560
16561                if Etype (Def_Id) = Any_Type then
16562                   return Def_Id;
16563                end if;
16564
16565                --  If the full view is that of a task with discriminants,
16566                --  we must constrain both the concurrent type and its
16567                --  corresponding record type. Otherwise we will just propagate
16568                --  the constraint to the full view, if available.
16569
16570                if Present (Full_View (Subtype_Mark_Id))
16571                  and then Has_Discriminants (Subtype_Mark_Id)
16572                  and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
16573                then
16574                   Full_View_Id :=
16575                     Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
16576
16577                   Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
16578                   Constrain_Concurrent (Full_View_Id, S,
16579                     Related_Nod, Related_Id, Suffix);
16580                   Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
16581                   Set_Full_View (Def_Id, Full_View_Id);
16582
16583                   --  Introduce an explicit reference to the private subtype,
16584                   --  to prevent scope anomalies in gigi if first use appears
16585                   --  in a nested context, e.g. a later function body.
16586                   --  Should this be generated in other contexts than a full
16587                   --  type declaration?
16588
16589                   if Is_Itype (Def_Id)
16590                     and then
16591                       Nkind (Parent (P)) = N_Full_Type_Declaration
16592                   then
16593                      Build_Itype_Reference (Def_Id, Parent (P));
16594                   end if;
16595
16596                else
16597                   Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
16598                end if;
16599
16600             when Concurrent_Kind  =>
16601                Constrain_Concurrent (Def_Id, S,
16602                  Related_Nod, Related_Id, Suffix);
16603
16604             when others =>
16605                Error_Msg_N ("invalid subtype mark in subtype indication", S);
16606          end case;
16607
16608          --  Size and Convention are always inherited from the base type
16609
16610          Set_Size_Info  (Def_Id,            (Subtype_Mark_Id));
16611          Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
16612
16613          return Def_Id;
16614       end if;
16615    end Process_Subtype;
16616
16617    ---------------------------------------
16618    -- Check_Anonymous_Access_Components --
16619    ---------------------------------------
16620
16621    procedure Check_Anonymous_Access_Components
16622       (Typ_Decl  : Node_Id;
16623        Typ       : Entity_Id;
16624        Prev      : Entity_Id;
16625        Comp_List : Node_Id)
16626    is
16627       Loc         : constant Source_Ptr := Sloc (Typ_Decl);
16628       Anon_Access : Entity_Id;
16629       Acc_Def     : Node_Id;
16630       Comp        : Node_Id;
16631       Comp_Def    : Node_Id;
16632       Decl        : Node_Id;
16633       Type_Def    : Node_Id;
16634
16635       procedure Build_Incomplete_Type_Declaration;
16636       --  If the record type contains components that include an access to the
16637       --  current record, then create an incomplete type declaration for the
16638       --  record, to be used as the designated type of the anonymous access.
16639       --  This is done only once, and only if there is no previous partial
16640       --  view of the type.
16641
16642       function Designates_T (Subt : Node_Id) return Boolean;
16643       --  Check whether a node designates the enclosing record type, or 'Class
16644       --  of that type
16645
16646       function Mentions_T (Acc_Def : Node_Id) return Boolean;
16647       --  Check whether an access definition includes a reference to
16648       --  the enclosing record type. The reference can be a subtype mark
16649       --  in the access definition itself, a 'Class attribute reference, or
16650       --  recursively a reference appearing in a parameter specification
16651       --  or result definition of an access_to_subprogram definition.
16652
16653       --------------------------------------
16654       -- Build_Incomplete_Type_Declaration --
16655       --------------------------------------
16656
16657       procedure Build_Incomplete_Type_Declaration is
16658          Decl  : Node_Id;
16659          Inc_T : Entity_Id;
16660          H     : Entity_Id;
16661
16662          --  Is_Tagged indicates whether the type is tagged. It is tagged if
16663          --  it's "is new ... with record" or else "is tagged record ...".
16664
16665          Is_Tagged : constant Boolean :=
16666              (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
16667                  and then
16668                    Present
16669                      (Record_Extension_Part (Type_Definition (Typ_Decl))))
16670            or else
16671              (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
16672                  and then Tagged_Present (Type_Definition (Typ_Decl)));
16673
16674       begin
16675          --  If there is a previous partial view, no need to create a new one
16676          --  If the partial view, given by Prev, is incomplete,  If Prev is
16677          --  a private declaration, full declaration is flagged accordingly.
16678
16679          if Prev /= Typ then
16680             if Is_Tagged then
16681                Make_Class_Wide_Type (Prev);
16682                Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
16683                Set_Etype (Class_Wide_Type (Typ), Typ);
16684             end if;
16685
16686             return;
16687
16688          elsif Has_Private_Declaration (Typ) then
16689
16690             --  If we refer to T'Class inside T, and T is the completion of a
16691             --  private type, then we need to make sure the class-wide type
16692             --  exists.
16693
16694             if Is_Tagged then
16695                Make_Class_Wide_Type (Typ);
16696             end if;
16697
16698             return;
16699
16700          --  If there was a previous anonymous access type, the incomplete
16701          --  type declaration will have been created already.
16702
16703          elsif Present (Current_Entity (Typ))
16704            and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
16705            and then Full_View (Current_Entity (Typ)) = Typ
16706          then
16707             return;
16708
16709          else
16710             Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
16711             Decl  := Make_Incomplete_Type_Declaration (Loc, Inc_T);
16712
16713             --  Type has already been inserted into the current scope.
16714             --  Remove it, and add incomplete declaration for type, so
16715             --  that subsequent anonymous access types can use it.
16716             --  The entity is unchained from the homonym list and from
16717             --  immediate visibility. After analysis, the entity in the
16718             --  incomplete declaration becomes immediately visible in the
16719             --  record declaration that follows.
16720
16721             H := Current_Entity (Typ);
16722
16723             if H = Typ then
16724                Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
16725             else
16726                while Present (H)
16727                  and then Homonym (H) /= Typ
16728                loop
16729                   H := Homonym (Typ);
16730                end loop;
16731
16732                Set_Homonym (H, Homonym (Typ));
16733             end if;
16734
16735             Insert_Before (Typ_Decl, Decl);
16736             Analyze (Decl);
16737             Set_Full_View (Inc_T, Typ);
16738
16739             if Is_Tagged then
16740                --  Create a common class-wide type for both views, and set
16741                --  the Etype of the class-wide type to the full view.
16742
16743                Make_Class_Wide_Type (Inc_T);
16744                Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
16745                Set_Etype (Class_Wide_Type (Typ), Typ);
16746             end if;
16747          end if;
16748       end Build_Incomplete_Type_Declaration;
16749
16750       ------------------
16751       -- Designates_T --
16752       ------------------
16753
16754       function Designates_T (Subt : Node_Id) return Boolean is
16755          Type_Id : constant Name_Id := Chars (Typ);
16756
16757          function Names_T (Nam : Node_Id) return Boolean;
16758          --  The record type has not been introduced in the current scope
16759          --  yet, so we must examine the name of the type itself, either
16760          --  an identifier T, or an expanded name of the form P.T, where
16761          --  P denotes the current scope.
16762
16763          -------------
16764          -- Names_T --
16765          -------------
16766
16767          function Names_T (Nam : Node_Id) return Boolean is
16768          begin
16769             if Nkind (Nam) = N_Identifier then
16770                return Chars (Nam) = Type_Id;
16771
16772             elsif Nkind (Nam) = N_Selected_Component then
16773                if Chars (Selector_Name (Nam)) = Type_Id then
16774                   if Nkind (Prefix (Nam)) = N_Identifier then
16775                      return Chars (Prefix (Nam)) = Chars (Current_Scope);
16776
16777                   elsif Nkind (Prefix (Nam)) = N_Selected_Component then
16778                      return Chars (Selector_Name (Prefix (Nam))) =
16779                             Chars (Current_Scope);
16780                   else
16781                      return False;
16782                   end if;
16783
16784                else
16785                   return False;
16786                end if;
16787
16788             else
16789                return False;
16790             end if;
16791          end Names_T;
16792
16793       --  Start of processing for Designates_T
16794
16795       begin
16796          if Nkind (Subt) = N_Identifier then
16797             return Chars (Subt) = Type_Id;
16798
16799             --  Reference can be through an expanded name which has not been
16800             --  analyzed yet, and which designates enclosing scopes.
16801
16802          elsif Nkind (Subt) = N_Selected_Component then
16803             if Names_T (Subt) then
16804                return True;
16805
16806             --  Otherwise it must denote an entity that is already visible.
16807             --  The access definition may name a subtype of the enclosing
16808             --  type, if there is a previous incomplete declaration for it.
16809
16810             else
16811                Find_Selected_Component (Subt);
16812                return
16813                  Is_Entity_Name (Subt)
16814                    and then Scope (Entity (Subt)) = Current_Scope
16815                    and then
16816                      (Chars (Base_Type (Entity (Subt))) = Type_Id
16817                        or else
16818                          (Is_Class_Wide_Type (Entity (Subt))
16819                            and then
16820                            Chars (Etype (Base_Type (Entity (Subt)))) =
16821                                                                   Type_Id));
16822             end if;
16823
16824          --  A reference to the current type may appear as the prefix of
16825          --  a 'Class attribute.
16826
16827          elsif Nkind (Subt) = N_Attribute_Reference
16828            and then Attribute_Name (Subt) = Name_Class
16829          then
16830             return Names_T (Prefix (Subt));
16831
16832          else
16833             return False;
16834          end if;
16835       end Designates_T;
16836
16837       ----------------
16838       -- Mentions_T --
16839       ----------------
16840
16841       function Mentions_T (Acc_Def : Node_Id) return Boolean is
16842          Param_Spec : Node_Id;
16843
16844          Acc_Subprg : constant Node_Id :=
16845                         Access_To_Subprogram_Definition (Acc_Def);
16846
16847       begin
16848          if No (Acc_Subprg) then
16849             return Designates_T (Subtype_Mark (Acc_Def));
16850          end if;
16851
16852          --  Component is an access_to_subprogram: examine its formals,
16853          --  and result definition in the case of an access_to_function.
16854
16855          Param_Spec := First (Parameter_Specifications (Acc_Subprg));
16856          while Present (Param_Spec) loop
16857             if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
16858               and then Mentions_T (Parameter_Type (Param_Spec))
16859             then
16860                return True;
16861
16862             elsif Designates_T (Parameter_Type (Param_Spec)) then
16863                return True;
16864             end if;
16865
16866             Next (Param_Spec);
16867          end loop;
16868
16869          if Nkind (Acc_Subprg) = N_Access_Function_Definition then
16870             if Nkind (Result_Definition (Acc_Subprg)) =
16871                  N_Access_Definition
16872             then
16873                return Mentions_T (Result_Definition (Acc_Subprg));
16874             else
16875                return Designates_T (Result_Definition (Acc_Subprg));
16876             end if;
16877          end if;
16878
16879          return False;
16880       end Mentions_T;
16881
16882    --  Start of processing for Check_Anonymous_Access_Components
16883
16884    begin
16885       if No (Comp_List) then
16886          return;
16887       end if;
16888
16889       Comp := First (Component_Items (Comp_List));
16890       while Present (Comp) loop
16891          if Nkind (Comp) = N_Component_Declaration
16892            and then Present
16893              (Access_Definition (Component_Definition (Comp)))
16894            and then
16895              Mentions_T (Access_Definition (Component_Definition (Comp)))
16896          then
16897             Comp_Def := Component_Definition (Comp);
16898             Acc_Def :=
16899               Access_To_Subprogram_Definition
16900                 (Access_Definition (Comp_Def));
16901
16902             Build_Incomplete_Type_Declaration;
16903             Anon_Access :=
16904               Make_Defining_Identifier (Loc,
16905                 Chars => New_Internal_Name ('S'));
16906
16907             --  Create a declaration for the anonymous access type: either
16908             --  an access_to_object or an access_to_subprogram.
16909
16910             if Present (Acc_Def) then
16911                if Nkind  (Acc_Def) = N_Access_Function_Definition then
16912                   Type_Def :=
16913                     Make_Access_Function_Definition (Loc,
16914                       Parameter_Specifications =>
16915                         Parameter_Specifications (Acc_Def),
16916                       Result_Definition => Result_Definition (Acc_Def));
16917                else
16918                   Type_Def :=
16919                     Make_Access_Procedure_Definition (Loc,
16920                       Parameter_Specifications =>
16921                         Parameter_Specifications (Acc_Def));
16922                end if;
16923
16924             else
16925                Type_Def :=
16926                  Make_Access_To_Object_Definition (Loc,
16927                    Subtype_Indication =>
16928                       Relocate_Node
16929                         (Subtype_Mark
16930                           (Access_Definition (Comp_Def))));
16931
16932                Set_Constant_Present
16933                  (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
16934                Set_All_Present
16935                  (Type_Def, All_Present (Access_Definition (Comp_Def)));
16936             end if;
16937
16938             Set_Null_Exclusion_Present
16939               (Type_Def,
16940                Null_Exclusion_Present (Access_Definition (Comp_Def)));
16941
16942             Decl :=
16943               Make_Full_Type_Declaration (Loc,
16944                 Defining_Identifier => Anon_Access,
16945                 Type_Definition     => Type_Def);
16946
16947             Insert_Before (Typ_Decl, Decl);
16948             Analyze (Decl);
16949
16950             --  If an access to object, Preserve entity of designated type,
16951             --  for ASIS use, before rewriting the component definition.
16952
16953             if No (Acc_Def) then
16954                declare
16955                   Desig : Entity_Id;
16956
16957                begin
16958                   Desig := Entity (Subtype_Indication (Type_Def));
16959
16960                   --  If the access definition is to the current  record,
16961                   --  the visible entity at this point is an  incomplete
16962                   --  type. Retrieve the full view to simplify  ASIS queries
16963
16964                   if Ekind (Desig) = E_Incomplete_Type then
16965                      Desig := Full_View (Desig);
16966                   end if;
16967
16968                   Set_Entity
16969                     (Subtype_Mark (Access_Definition  (Comp_Def)), Desig);
16970                end;
16971             end if;
16972
16973             Rewrite (Comp_Def,
16974               Make_Component_Definition (Loc,
16975                 Subtype_Indication =>
16976                New_Occurrence_Of (Anon_Access, Loc)));
16977
16978             if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
16979                Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
16980             else
16981                Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
16982             end if;
16983
16984             Set_Is_Local_Anonymous_Access (Anon_Access);
16985          end if;
16986
16987          Next (Comp);
16988       end loop;
16989
16990       if Present (Variant_Part (Comp_List)) then
16991          declare
16992             V : Node_Id;
16993          begin
16994             V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
16995             while Present (V) loop
16996                Check_Anonymous_Access_Components
16997                  (Typ_Decl, Typ, Prev, Component_List (V));
16998                Next_Non_Pragma (V);
16999             end loop;
17000          end;
17001       end if;
17002    end Check_Anonymous_Access_Components;
17003
17004    --------------------------------
17005    -- Preanalyze_Spec_Expression --
17006    --------------------------------
17007
17008    procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
17009       Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
17010    begin
17011       In_Spec_Expression := True;
17012       Preanalyze_And_Resolve (N, T);
17013       In_Spec_Expression := Save_In_Spec_Expression;
17014    end Preanalyze_Spec_Expression;
17015
17016    -----------------------------
17017    -- Record_Type_Declaration --
17018    -----------------------------
17019
17020    procedure Record_Type_Declaration
17021      (T    : Entity_Id;
17022       N    : Node_Id;
17023       Prev : Entity_Id)
17024    is
17025       Def       : constant Node_Id := Type_Definition (N);
17026       Is_Tagged : Boolean;
17027       Tag_Comp  : Entity_Id;
17028
17029    begin
17030       --  These flags must be initialized before calling Process_Discriminants
17031       --  because this routine makes use of them.
17032
17033       Set_Ekind             (T, E_Record_Type);
17034       Set_Etype             (T, T);
17035       Init_Size_Align       (T);
17036       Set_Interfaces        (T, No_Elist);
17037       Set_Stored_Constraint (T, No_Elist);
17038
17039       --  Normal case
17040
17041       if Ada_Version < Ada_05
17042         or else not Interface_Present (Def)
17043       then
17044          --  The flag Is_Tagged_Type might have already been set by
17045          --  Find_Type_Name if it detected an error for declaration T. This
17046          --  arises in the case of private tagged types where the full view
17047          --  omits the word tagged.
17048
17049          Is_Tagged :=
17050            Tagged_Present (Def)
17051              or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
17052
17053          Set_Is_Tagged_Type      (T, Is_Tagged);
17054          Set_Is_Limited_Record   (T, Limited_Present (Def));
17055
17056          --  Type is abstract if full declaration carries keyword, or if
17057          --  previous partial view did.
17058
17059          Set_Is_Abstract_Type    (T, Is_Abstract_Type (T)
17060                                       or else Abstract_Present (Def));
17061
17062       else
17063          Is_Tagged := True;
17064          Analyze_Interface_Declaration (T, Def);
17065
17066          if Present (Discriminant_Specifications (N)) then
17067             Error_Msg_N
17068               ("interface types cannot have discriminants",
17069                 Defining_Identifier
17070                   (First (Discriminant_Specifications (N))));
17071          end if;
17072       end if;
17073
17074       --  First pass: if there are self-referential access components,
17075       --  create the required anonymous access type declarations, and if
17076       --  need be an incomplete type declaration for T itself.
17077
17078       Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
17079
17080       if Ada_Version >= Ada_05
17081         and then Present (Interface_List (Def))
17082       then
17083          Check_Interfaces (N, Def);
17084
17085          declare
17086             Ifaces_List : Elist_Id;
17087
17088          begin
17089             --  Ada 2005 (AI-251): Collect the list of progenitors that are not
17090             --  already in the parents.
17091
17092             Collect_Interfaces
17093               (T               => T,
17094                Ifaces_List     => Ifaces_List,
17095                Exclude_Parents => True);
17096
17097             Set_Interfaces (T, Ifaces_List);
17098          end;
17099       end if;
17100
17101       --  Records constitute a scope for the component declarations within.
17102       --  The scope is created prior to the processing of these declarations.
17103       --  Discriminants are processed first, so that they are visible when
17104       --  processing the other components. The Ekind of the record type itself
17105       --  is set to E_Record_Type (subtypes appear as E_Record_Subtype).
17106
17107       --  Enter record scope
17108
17109       Push_Scope (T);
17110
17111       --  If an incomplete or private type declaration was already given for
17112       --  the type, then this scope already exists, and the discriminants have
17113       --  been declared within. We must verify that the full declaration
17114       --  matches the incomplete one.
17115
17116       Check_Or_Process_Discriminants (N, T, Prev);
17117
17118       Set_Is_Constrained     (T, not Has_Discriminants (T));
17119       Set_Has_Delayed_Freeze (T, True);
17120
17121       --  For tagged types add a manually analyzed component corresponding
17122       --  to the component _tag, the corresponding piece of tree will be
17123       --  expanded as part of the freezing actions if it is not a CPP_Class.
17124
17125       if Is_Tagged then
17126
17127          --  Do not add the tag unless we are in expansion mode
17128
17129          if Expander_Active then
17130             Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
17131             Enter_Name (Tag_Comp);
17132
17133             Set_Ekind                     (Tag_Comp, E_Component);
17134             Set_Is_Tag                    (Tag_Comp);
17135             Set_Is_Aliased                (Tag_Comp);
17136             Set_Etype                     (Tag_Comp, RTE (RE_Tag));
17137             Set_DT_Entry_Count            (Tag_Comp, No_Uint);
17138             Set_Original_Record_Component (Tag_Comp, Tag_Comp);
17139             Init_Component_Location       (Tag_Comp);
17140
17141             --  Ada 2005 (AI-251): Addition of the Tag corresponding to all the
17142             --  implemented interfaces.
17143
17144             if Has_Interfaces (T) then
17145                Add_Interface_Tag_Components (N, T);
17146             end if;
17147          end if;
17148
17149          Make_Class_Wide_Type (T);
17150          Set_Primitive_Operations (T, New_Elmt_List);
17151       end if;
17152
17153       --  We must suppress range checks when processing the components
17154       --  of a record in the presence of discriminants, since we don't
17155       --  want spurious checks to be generated during their analysis, but
17156       --  must reset the Suppress_Range_Checks flags after having processed
17157       --  the record definition.
17158
17159       --  Note: this is the only use of Kill_Range_Checks, and is a bit odd,
17160       --  couldn't we just use the normal range check suppression method here.
17161       --  That would seem cleaner ???
17162
17163       if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
17164          Set_Kill_Range_Checks (T, True);
17165          Record_Type_Definition (Def, Prev);
17166          Set_Kill_Range_Checks (T, False);
17167       else
17168          Record_Type_Definition (Def, Prev);
17169       end if;
17170
17171       --  Exit from record scope
17172
17173       End_Scope;
17174
17175       --  Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
17176       --  the implemented interfaces and associate them an aliased entity.
17177
17178       if Is_Tagged
17179         and then not Is_Empty_List (Interface_List (Def))
17180       then
17181          Derive_Progenitor_Subprograms (T, T);
17182       end if;
17183    end Record_Type_Declaration;
17184
17185    ----------------------------
17186    -- Record_Type_Definition --
17187    ----------------------------
17188
17189    procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
17190       Component          : Entity_Id;
17191       Ctrl_Components    : Boolean := False;
17192       Final_Storage_Only : Boolean;
17193       T                  : Entity_Id;
17194
17195    begin
17196       if Ekind (Prev_T) = E_Incomplete_Type then
17197          T := Full_View (Prev_T);
17198       else
17199          T := Prev_T;
17200       end if;
17201
17202       Final_Storage_Only := not Is_Controlled (T);
17203
17204       --  Ada 2005: check whether an explicit Limited is present in a derived
17205       --  type declaration.
17206
17207       if Nkind (Parent (Def)) = N_Derived_Type_Definition
17208         and then Limited_Present (Parent (Def))
17209       then
17210          Set_Is_Limited_Record (T);
17211       end if;
17212
17213       --  If the component list of a record type is defined by the reserved
17214       --  word null and there is no discriminant part, then the record type has
17215       --  no components and all records of the type are null records (RM 3.7)
17216       --  This procedure is also called to process the extension part of a
17217       --  record extension, in which case the current scope may have inherited
17218       --  components.
17219
17220       if No (Def)
17221         or else No (Component_List (Def))
17222         or else Null_Present (Component_List (Def))
17223       then
17224          null;
17225
17226       else
17227          Analyze_Declarations (Component_Items (Component_List (Def)));
17228
17229          if Present (Variant_Part (Component_List (Def))) then
17230             Analyze (Variant_Part (Component_List (Def)));
17231          end if;
17232       end if;
17233
17234       --  After completing the semantic analysis of the record definition,
17235       --  record components, both new and inherited, are accessible. Set their
17236       --  kind accordingly. Exclude malformed itypes from illegal declarations,
17237       --  whose Ekind may be void.
17238
17239       Component := First_Entity (Current_Scope);
17240       while Present (Component) loop
17241          if Ekind (Component) = E_Void
17242            and then not Is_Itype (Component)
17243          then
17244             Set_Ekind (Component, E_Component);
17245             Init_Component_Location (Component);
17246          end if;
17247
17248          if Has_Task (Etype (Component)) then
17249             Set_Has_Task (T);
17250          end if;
17251
17252          if Ekind (Component) /= E_Component then
17253             null;
17254
17255          elsif Has_Controlled_Component (Etype (Component))
17256            or else (Chars (Component) /= Name_uParent
17257                      and then Is_Controlled (Etype (Component)))
17258          then
17259             Set_Has_Controlled_Component (T, True);
17260             Final_Storage_Only :=
17261               Final_Storage_Only
17262                 and then Finalize_Storage_Only (Etype (Component));
17263             Ctrl_Components := True;
17264          end if;
17265
17266          Next_Entity (Component);
17267       end loop;
17268
17269       --  A Type is Finalize_Storage_Only only if all its controlled components
17270       --  are also.
17271
17272       if Ctrl_Components then
17273          Set_Finalize_Storage_Only (T, Final_Storage_Only);
17274       end if;
17275
17276       --  Place reference to end record on the proper entity, which may
17277       --  be a partial view.
17278
17279       if Present (Def) then
17280          Process_End_Label (Def, 'e', Prev_T);
17281       end if;
17282    end Record_Type_Definition;
17283
17284    ------------------------
17285    -- Replace_Components --
17286    ------------------------
17287
17288    procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
17289       function Process (N : Node_Id) return Traverse_Result;
17290
17291       -------------
17292       -- Process --
17293       -------------
17294
17295       function Process (N : Node_Id) return Traverse_Result is
17296          Comp : Entity_Id;
17297
17298       begin
17299          if Nkind (N) = N_Discriminant_Specification then
17300             Comp := First_Discriminant (Typ);
17301             while Present (Comp) loop
17302                if Chars (Comp) = Chars (Defining_Identifier (N)) then
17303                   Set_Defining_Identifier (N, Comp);
17304                   exit;
17305                end if;
17306
17307                Next_Discriminant (Comp);
17308             end loop;
17309
17310          elsif Nkind (N) = N_Component_Declaration then
17311             Comp := First_Component (Typ);
17312             while Present (Comp) loop
17313                if Chars (Comp) = Chars (Defining_Identifier (N)) then
17314                   Set_Defining_Identifier (N, Comp);
17315                   exit;
17316                end if;
17317
17318                Next_Component (Comp);
17319             end loop;
17320          end if;
17321
17322          return OK;
17323       end Process;
17324
17325       procedure Replace is new Traverse_Proc (Process);
17326
17327    --  Start of processing for Replace_Components
17328
17329    begin
17330       Replace (Decl);
17331    end Replace_Components;
17332
17333    -------------------------------
17334    -- Set_Completion_Referenced --
17335    -------------------------------
17336
17337    procedure Set_Completion_Referenced (E : Entity_Id) is
17338    begin
17339       --  If in main unit, mark entity that is a completion as referenced,
17340       --  warnings go on the partial view when needed.
17341
17342       if In_Extended_Main_Source_Unit (E) then
17343          Set_Referenced (E);
17344       end if;
17345    end Set_Completion_Referenced;
17346
17347    ---------------------
17348    -- Set_Fixed_Range --
17349    ---------------------
17350
17351    --  The range for fixed-point types is complicated by the fact that we
17352    --  do not know the exact end points at the time of the declaration. This
17353    --  is true for three reasons:
17354
17355    --     A size clause may affect the fudging of the end-points
17356    --     A small clause may affect the values of the end-points
17357    --     We try to include the end-points if it does not affect the size
17358
17359    --  This means that the actual end-points must be established at the point
17360    --  when the type is frozen. Meanwhile, we first narrow the range as
17361    --  permitted (so that it will fit if necessary in a small specified size),
17362    --  and then build a range subtree with these narrowed bounds.
17363
17364    --  Set_Fixed_Range constructs the range from real literal values, and sets
17365    --  the range as the Scalar_Range of the given fixed-point type entity.
17366
17367    --  The parent of this range is set to point to the entity so that it is
17368    --  properly hooked into the tree (unlike normal Scalar_Range entries for
17369    --  other scalar types, which are just pointers to the range in the
17370    --  original tree, this would otherwise be an orphan).
17371
17372    --  The tree is left unanalyzed. When the type is frozen, the processing
17373    --  in Freeze.Freeze_Fixed_Point_Type notices that the range is not
17374    --  analyzed, and uses this as an indication that it should complete
17375    --  work on the range (it will know the final small and size values).
17376
17377    procedure Set_Fixed_Range
17378      (E   : Entity_Id;
17379       Loc : Source_Ptr;
17380       Lo  : Ureal;
17381       Hi  : Ureal)
17382    is
17383       S : constant Node_Id :=
17384             Make_Range (Loc,
17385               Low_Bound  => Make_Real_Literal (Loc, Lo),
17386               High_Bound => Make_Real_Literal (Loc, Hi));
17387    begin
17388       Set_Scalar_Range (E, S);
17389       Set_Parent (S, E);
17390    end Set_Fixed_Range;
17391
17392    ----------------------------------
17393    -- Set_Scalar_Range_For_Subtype --
17394    ----------------------------------
17395
17396    procedure Set_Scalar_Range_For_Subtype
17397      (Def_Id : Entity_Id;
17398       R      : Node_Id;
17399       Subt   : Entity_Id)
17400    is
17401       Kind : constant Entity_Kind :=  Ekind (Def_Id);
17402
17403    begin
17404       Set_Scalar_Range (Def_Id, R);
17405
17406       --  We need to link the range into the tree before resolving it so
17407       --  that types that are referenced, including importantly the subtype
17408       --  itself, are properly frozen (Freeze_Expression requires that the
17409       --  expression be properly linked into the tree). Of course if it is
17410       --  already linked in, then we do not disturb the current link.
17411
17412       if No (Parent (R)) then
17413          Set_Parent (R, Def_Id);
17414       end if;
17415
17416       --  Reset the kind of the subtype during analysis of the range, to
17417       --  catch possible premature use in the bounds themselves.
17418
17419       Set_Ekind (Def_Id, E_Void);
17420       Process_Range_Expr_In_Decl (R, Subt);
17421       Set_Ekind (Def_Id, Kind);
17422    end Set_Scalar_Range_For_Subtype;
17423
17424    --------------------------------------------------------
17425    -- Set_Stored_Constraint_From_Discriminant_Constraint --
17426    --------------------------------------------------------
17427
17428    procedure Set_Stored_Constraint_From_Discriminant_Constraint
17429      (E : Entity_Id)
17430    is
17431    begin
17432       --  Make sure set if encountered during Expand_To_Stored_Constraint
17433
17434       Set_Stored_Constraint (E, No_Elist);
17435
17436       --  Give it the right value
17437
17438       if Is_Constrained (E) and then Has_Discriminants (E) then
17439          Set_Stored_Constraint (E,
17440            Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
17441       end if;
17442    end Set_Stored_Constraint_From_Discriminant_Constraint;
17443
17444    -------------------------------------
17445    -- Signed_Integer_Type_Declaration --
17446    -------------------------------------
17447
17448    procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17449       Implicit_Base : Entity_Id;
17450       Base_Typ      : Entity_Id;
17451       Lo_Val        : Uint;
17452       Hi_Val        : Uint;
17453       Errs          : Boolean := False;
17454       Lo            : Node_Id;
17455       Hi            : Node_Id;
17456
17457       function Can_Derive_From (E : Entity_Id) return Boolean;
17458       --  Determine whether given bounds allow derivation from specified type
17459
17460       procedure Check_Bound (Expr : Node_Id);
17461       --  Check bound to make sure it is integral and static. If not, post
17462       --  appropriate error message and set Errs flag
17463
17464       ---------------------
17465       -- Can_Derive_From --
17466       ---------------------
17467
17468       --  Note we check both bounds against both end values, to deal with
17469       --  strange types like ones with a range of 0 .. -12341234.
17470
17471       function Can_Derive_From (E : Entity_Id) return Boolean is
17472          Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
17473          Hi : constant Uint := Expr_Value (Type_High_Bound (E));
17474       begin
17475          return Lo <= Lo_Val and then Lo_Val <= Hi
17476                   and then
17477                 Lo <= Hi_Val and then Hi_Val <= Hi;
17478       end Can_Derive_From;
17479
17480       -----------------
17481       -- Check_Bound --
17482       -----------------
17483
17484       procedure Check_Bound (Expr : Node_Id) is
17485       begin
17486          --  If a range constraint is used as an integer type definition, each
17487          --  bound of the range must be defined by a static expression of some
17488          --  integer type, but the two bounds need not have the same integer
17489          --  type (Negative bounds are allowed.) (RM 3.5.4)
17490
17491          if not Is_Integer_Type (Etype (Expr)) then
17492             Error_Msg_N
17493               ("integer type definition bounds must be of integer type", Expr);
17494             Errs := True;
17495
17496          elsif not Is_OK_Static_Expression (Expr) then
17497             Flag_Non_Static_Expr
17498               ("non-static expression used for integer type bound!", Expr);
17499             Errs := True;
17500
17501          --  The bounds are folded into literals, and we set their type to be
17502          --  universal, to avoid typing difficulties: we cannot set the type
17503          --  of the literal to the new type, because this would be a forward
17504          --  reference for the back end,  and if the original type is user-
17505          --  defined this can lead to spurious semantic errors (e.g. 2928-003).
17506
17507          else
17508             if Is_Entity_Name (Expr) then
17509                Fold_Uint (Expr, Expr_Value (Expr), True);
17510             end if;
17511
17512             Set_Etype (Expr, Universal_Integer);
17513          end if;
17514       end Check_Bound;
17515
17516    --  Start of processing for Signed_Integer_Type_Declaration
17517
17518    begin
17519       --  Create an anonymous base type
17520
17521       Implicit_Base :=
17522         Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
17523
17524       --  Analyze and check the bounds, they can be of any integer type
17525
17526       Lo := Low_Bound (Def);
17527       Hi := High_Bound (Def);
17528
17529       --  Arbitrarily use Integer as the type if either bound had an error
17530
17531       if Hi = Error or else Lo = Error then
17532          Base_Typ := Any_Integer;
17533          Set_Error_Posted (T, True);
17534
17535       --  Here both bounds are OK expressions
17536
17537       else
17538          Analyze_And_Resolve (Lo, Any_Integer);
17539          Analyze_And_Resolve (Hi, Any_Integer);
17540
17541          Check_Bound (Lo);
17542          Check_Bound (Hi);
17543
17544          if Errs then
17545             Hi := Type_High_Bound (Standard_Long_Long_Integer);
17546             Lo := Type_Low_Bound (Standard_Long_Long_Integer);
17547          end if;
17548
17549          --  Find type to derive from
17550
17551          Lo_Val := Expr_Value (Lo);
17552          Hi_Val := Expr_Value (Hi);
17553
17554          if Can_Derive_From (Standard_Short_Short_Integer) then
17555             Base_Typ := Base_Type (Standard_Short_Short_Integer);
17556
17557          elsif Can_Derive_From (Standard_Short_Integer) then
17558             Base_Typ := Base_Type (Standard_Short_Integer);
17559
17560          elsif Can_Derive_From (Standard_Integer) then
17561             Base_Typ := Base_Type (Standard_Integer);
17562
17563          elsif Can_Derive_From (Standard_Long_Integer) then
17564             Base_Typ := Base_Type (Standard_Long_Integer);
17565
17566          elsif Can_Derive_From (Standard_Long_Long_Integer) then
17567             Base_Typ := Base_Type (Standard_Long_Long_Integer);
17568
17569          else
17570             Base_Typ := Base_Type (Standard_Long_Long_Integer);
17571             Error_Msg_N ("integer type definition bounds out of range", Def);
17572             Hi := Type_High_Bound (Standard_Long_Long_Integer);
17573             Lo := Type_Low_Bound (Standard_Long_Long_Integer);
17574          end if;
17575       end if;
17576
17577       --  Complete both implicit base and declared first subtype entities
17578
17579       Set_Etype          (Implicit_Base, Base_Typ);
17580       Set_Scalar_Range   (Implicit_Base, Scalar_Range   (Base_Typ));
17581       Set_Size_Info      (Implicit_Base,                (Base_Typ));
17582       Set_RM_Size        (Implicit_Base, RM_Size        (Base_Typ));
17583       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17584
17585       Set_Ekind          (T, E_Signed_Integer_Subtype);
17586       Set_Etype          (T, Implicit_Base);
17587
17588       Set_Size_Info      (T,                (Implicit_Base));
17589       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
17590       Set_Scalar_Range   (T, Def);
17591       Set_RM_Size        (T, UI_From_Int (Minimum_Size (T)));
17592       Set_Is_Constrained (T);
17593    end Signed_Integer_Type_Declaration;
17594
17595 end Sem_Ch3;