OSDN Git Service

2008-04-30 Paul Thomas <pault@gcc.gnu.org>
[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_Dist; use Exp_Dist;
35 with Exp_Tss;  use Exp_Tss;
36 with Exp_Util; use Exp_Util;
37 with Fname;    use Fname;
38 with Freeze;   use Freeze;
39 with Itypes;   use Itypes;
40 with Layout;   use Layout;
41 with Lib;      use Lib;
42 with Lib.Xref; use Lib.Xref;
43 with Namet;    use Namet;
44 with Nmake;    use Nmake;
45 with Opt;      use Opt;
46 with Restrict; use Restrict;
47 with Rident;   use Rident;
48 with Rtsfind;  use Rtsfind;
49 with Sem;      use Sem;
50 with Sem_Case; use Sem_Case;
51 with Sem_Cat;  use Sem_Cat;
52 with Sem_Ch6;  use Sem_Ch6;
53 with Sem_Ch7;  use Sem_Ch7;
54 with Sem_Ch8;  use Sem_Ch8;
55 with Sem_Ch13; use Sem_Ch13;
56 with Sem_Disp; use Sem_Disp;
57 with Sem_Dist; use Sem_Dist;
58 with Sem_Elim; use Sem_Elim;
59 with Sem_Eval; use Sem_Eval;
60 with Sem_Mech; use Sem_Mech;
61 with Sem_Res;  use Sem_Res;
62 with Sem_Smem; use Sem_Smem;
63 with Sem_Type; use Sem_Type;
64 with Sem_Util; use Sem_Util;
65 with Sem_Warn; use Sem_Warn;
66 with Stand;    use Stand;
67 with Sinfo;    use Sinfo;
68 with Snames;   use Snames;
69 with Targparm; use Targparm;
70 with Tbuild;   use Tbuild;
71 with Ttypes;   use Ttypes;
72 with Uintp;    use Uintp;
73 with Urealp;   use Urealp;
74
75 package body Sem_Ch3 is
76
77    -----------------------
78    -- Local Subprograms --
79    -----------------------
80
81    procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
82    --  Ada 2005 (AI-251): Add the tag components corresponding to all the
83    --  abstract interface types implemented by a record type or a derived
84    --  record type.
85
86    procedure Build_Derived_Type
87      (N             : Node_Id;
88       Parent_Type   : Entity_Id;
89       Derived_Type  : Entity_Id;
90       Is_Completion : Boolean;
91       Derive_Subps  : Boolean := True);
92    --  Create and decorate a Derived_Type given the Parent_Type entity. N is
93    --  the N_Full_Type_Declaration node containing the derived type definition.
94    --  Parent_Type is the entity for the parent type in the derived type
95    --  definition and Derived_Type the actual derived type. Is_Completion must
96    --  be set to False if Derived_Type is the N_Defining_Identifier node in N
97    --  (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
98    --  completion of a private type declaration. If Is_Completion is set to
99    --  True, N is the completion of a private type declaration and Derived_Type
100    --  is different from the defining identifier inside N (i.e. Derived_Type /=
101    --  Defining_Identifier (N)). Derive_Subps indicates whether the parent
102    --  subprograms should be derived. The only case where this parameter is
103    --  False is when Build_Derived_Type is recursively called to process an
104    --  implicit derived full type for a type derived from a private type (in
105    --  that case the subprograms must only be derived for the private view of
106    --  the type).
107    --
108    --  ??? These flags need a bit of re-examination and re-documentation:
109    --  ???  are they both necessary (both seem related to the recursion)?
110
111    procedure Build_Derived_Access_Type
112      (N            : Node_Id;
113       Parent_Type  : Entity_Id;
114       Derived_Type : Entity_Id);
115    --  Subsidiary procedure to Build_Derived_Type. For a derived access type,
116    --  create an implicit base if the parent type is constrained or if the
117    --  subtype indication has a constraint.
118
119    procedure Build_Derived_Array_Type
120      (N            : Node_Id;
121       Parent_Type  : Entity_Id;
122       Derived_Type : Entity_Id);
123    --  Subsidiary procedure to Build_Derived_Type. For a derived array type,
124    --  create an implicit base if the parent type is constrained or if the
125    --  subtype indication has a constraint.
126
127    procedure Build_Derived_Concurrent_Type
128      (N            : Node_Id;
129       Parent_Type  : Entity_Id;
130       Derived_Type : Entity_Id);
131    --  Subsidiary procedure to Build_Derived_Type. For a derived task or
132    --  protected type, inherit entries and protected subprograms, check
133    --  legality of discriminant constraints if any.
134
135    procedure Build_Derived_Enumeration_Type
136      (N            : Node_Id;
137       Parent_Type  : Entity_Id;
138       Derived_Type : Entity_Id);
139    --  Subsidiary procedure to Build_Derived_Type. For a derived enumeration
140    --  type, we must create a new list of literals. Types derived from
141    --  Character and Wide_Character are special-cased.
142
143    procedure Build_Derived_Numeric_Type
144      (N            : Node_Id;
145       Parent_Type  : Entity_Id;
146       Derived_Type : Entity_Id);
147    --  Subsidiary procedure to Build_Derived_Type. For numeric types, create
148    --  an anonymous base type, and propagate constraint to subtype if needed.
149
150    procedure Build_Derived_Private_Type
151      (N             : Node_Id;
152       Parent_Type   : Entity_Id;
153       Derived_Type  : Entity_Id;
154       Is_Completion : Boolean;
155       Derive_Subps  : Boolean := True);
156    --  Subsidiary procedure to Build_Derived_Type. This procedure is complex
157    --  because the parent may or may not have a completion, and the derivation
158    --  may itself be a completion.
159
160    procedure Build_Derived_Record_Type
161      (N            : Node_Id;
162       Parent_Type  : Entity_Id;
163       Derived_Type : Entity_Id;
164       Derive_Subps : Boolean := True);
165    --  Subsidiary procedure for Build_Derived_Type and
166    --  Analyze_Private_Extension_Declaration used for tagged and untagged
167    --  record types. All parameters are as in Build_Derived_Type except that
168    --  N, in addition to being an N_Full_Type_Declaration node, can also be an
169    --  N_Private_Extension_Declaration node. See the definition of this routine
170    --  for much more info. Derive_Subps indicates whether subprograms should
171    --  be derived from the parent type. The only case where Derive_Subps is
172    --  False is for an implicit derived full type for a type derived from a
173    --  private type (see Build_Derived_Type).
174
175    procedure Build_Discriminal (Discrim : Entity_Id);
176    --  Create the discriminal corresponding to discriminant Discrim, that is
177    --  the parameter corresponding to Discrim to be used in initialization
178    --  procedures for the type where Discrim is a discriminant. Discriminals
179    --  are not used during semantic analysis, and are not fully defined
180    --  entities until expansion. Thus they are not given a scope until
181    --  initialization procedures are built.
182
183    function Build_Discriminant_Constraints
184      (T           : Entity_Id;
185       Def         : Node_Id;
186       Derived_Def : Boolean := False) return Elist_Id;
187    --  Validate discriminant constraints and return the list of the constraints
188    --  in order of discriminant declarations, where T is the discriminated
189    --  unconstrained type. Def is the N_Subtype_Indication node where the
190    --  discriminants constraints for T are specified. Derived_Def is True
191    --  when building the discriminant constraints in a derived type definition
192    --  of the form "type D (...) is new T (xxx)". In this case T is the parent
193    --  type and Def is the constraint "(xxx)" on T and this routine sets the
194    --  Corresponding_Discriminant field of the discriminants in the derived
195    --  type D to point to the corresponding discriminants in the parent type T.
196
197    procedure Build_Discriminated_Subtype
198      (T           : Entity_Id;
199       Def_Id      : Entity_Id;
200       Elist       : Elist_Id;
201       Related_Nod : Node_Id;
202       For_Access  : Boolean := False);
203    --  Subsidiary procedure to Constrain_Discriminated_Type and to
204    --  Process_Incomplete_Dependents. Given
205    --
206    --     T (a possibly discriminated base type)
207    --     Def_Id (a very partially built subtype for T),
208    --
209    --  the call completes Def_Id to be the appropriate E_*_Subtype.
210    --
211    --  The Elist is the list of discriminant constraints if any (it is set
212    --  to No_Elist if T is not a discriminated type, and to an empty list if
213    --  T has discriminants but there are no discriminant constraints). The
214    --  Related_Nod is the same as Decl_Node in Create_Constrained_Components.
215    --  The For_Access says whether or not this subtype is really constraining
216    --  an access type. That is its sole purpose is the designated type of an
217    --  access type -- in which case a Private_Subtype Is_For_Access_Subtype
218    --  is built to avoid freezing T when the access subtype is frozen.
219
220    function Build_Scalar_Bound
221      (Bound : Node_Id;
222       Par_T : Entity_Id;
223       Der_T : Entity_Id) return Node_Id;
224    --  The bounds of a derived scalar type are conversions of the bounds of
225    --  the parent type. Optimize the representation if the bounds are literals.
226    --  Needs a more complete spec--what are the parameters exactly, and what
227    --  exactly is the returned value, and how is Bound affected???
228
229    procedure Build_Itype_Reference
230      (Ityp : Entity_Id;
231       Nod  : Node_Id);
232    --  Create a reference to an internal type, for use by Gigi. The back-end
233    --  elaborates itypes on demand, i.e. when their first use is seen. This
234    --  can lead to scope anomalies if the first use is within a scope that is
235    --  nested within the scope that contains  the point of definition of the
236    --  itype. The Itype_Reference node forces the elaboration of the itype
237    --  in the proper scope. The node is inserted after Nod, which is the
238    --  enclosing declaration that generated Ityp.
239    --
240    --  A related mechanism is used during expansion, for itypes created in
241    --  branches of conditionals. See Ensure_Defined in exp_util.
242    --  Could both mechanisms be merged ???
243
244    procedure Build_Underlying_Full_View
245      (N   : Node_Id;
246       Typ : Entity_Id;
247       Par : Entity_Id);
248    --  If the completion of a private type is itself derived from a private
249    --  type, or if the full view of a private subtype is itself private, the
250    --  back-end has no way to compute the actual size of this type. We build
251    --  an internal subtype declaration of the proper parent type to convey
252    --  this information. This extra mechanism is needed because a full
253    --  view cannot itself have a full view (it would get clobbered during
254    --  view exchanges).
255
256    procedure Check_Abstract_Interfaces (N : Node_Id; Def : Node_Id);
257    --  Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
258
259    procedure Check_Access_Discriminant_Requires_Limited
260      (D   : Node_Id;
261       Loc : Node_Id);
262    --  Check the restriction that the type to which an access discriminant
263    --  belongs must be a concurrent type or a descendant of a type with
264    --  the reserved word 'limited' in its declaration.
265
266    procedure Check_Anonymous_Access_Components
267       (Typ_Decl  : Node_Id;
268        Typ       : Entity_Id;
269        Prev      : Entity_Id;
270        Comp_List : Node_Id);
271    --  Ada 2005 AI-382: an access component in a record definition can refer to
272    --  the enclosing record, in which case it denotes the type itself, and not
273    --  the current instance of the type. We create an anonymous access type for
274    --  the component, and flag it as an access to a component, so accessibility
275    --  checks are properly performed on it. The declaration of the access type
276    --  is placed ahead of that of the record to prevent order-of-elaboration
277    --  circularity issues in Gigi. We create an incomplete type for the record
278    --  declaration, which is the designated type of the anonymous access.
279
280    procedure Check_Delta_Expression (E : Node_Id);
281    --  Check that the expression represented by E is suitable for use as a
282    --  delta expression, i.e. it is of real type and is static.
283
284    procedure Check_Digits_Expression (E : Node_Id);
285    --  Check that the expression represented by E is suitable for use as a
286    --  digits expression, i.e. it is of integer type, positive and static.
287
288    procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
289    --  Validate the initialization of an object declaration. T is the required
290    --  type, and Exp is the initialization expression.
291
292    procedure Check_Or_Process_Discriminants
293      (N    : Node_Id;
294       T    : Entity_Id;
295       Prev : Entity_Id := Empty);
296    --  If T is the full declaration of an incomplete or private type, check the
297    --  conformance of the discriminants, otherwise process them. Prev is the
298    --  entity of the partial declaration, if any.
299
300    procedure Check_Real_Bound (Bound : Node_Id);
301    --  Check given bound for being of real type and static. If not, post an
302    --  appropriate message, and rewrite the bound with the real literal zero.
303
304    procedure Constant_Redeclaration
305      (Id : Entity_Id;
306       N  : Node_Id;
307       T  : out Entity_Id);
308    --  Various checks on legality of full declaration of deferred constant.
309    --  Id is the entity for the redeclaration, N is the N_Object_Declaration,
310    --  node. The caller has not yet set any attributes of this entity.
311
312    function Contain_Interface
313      (Iface  : Entity_Id;
314       Ifaces : Elist_Id) return Boolean;
315    --  Ada 2005: Determine whether Iface is present in the list Ifaces
316
317    procedure Convert_Scalar_Bounds
318      (N            : Node_Id;
319       Parent_Type  : Entity_Id;
320       Derived_Type : Entity_Id;
321       Loc          : Source_Ptr);
322    --  For derived scalar types, convert the bounds in the type definition to
323    --  the derived type, and complete their analysis. Given a constraint of the
324    --  form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
325    --  T'Base, the parent_type. The bounds of the derived type (the anonymous
326    --  base) are copies of Lo and Hi. Finally, the bounds of the derived
327    --  subtype are conversions of those bounds to the derived_type, so that
328    --  their typing is consistent.
329
330    procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
331    --  Copies attributes from array base type T2 to array base type T1. Copies
332    --  only attributes that apply to base types, but not subtypes.
333
334    procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
335    --  Copies attributes from array subtype T2 to array subtype T1. Copies
336    --  attributes that apply to both subtypes and base types.
337
338    procedure Create_Constrained_Components
339      (Subt        : Entity_Id;
340       Decl_Node   : Node_Id;
341       Typ         : Entity_Id;
342       Constraints : Elist_Id);
343    --  Build the list of entities for a constrained discriminated record
344    --  subtype. If a component depends on a discriminant, replace its subtype
345    --  using the discriminant values in the discriminant constraint. Subt
346    --  is the defining identifier for the subtype whose list of constrained
347    --  entities we will create. Decl_Node is the type declaration node where
348    --  we will attach all the itypes created. Typ is the base discriminated
349    --  type for the subtype Subt. Constraints is the list of discriminant
350    --  constraints for Typ.
351
352    function Constrain_Component_Type
353      (Comp            : Entity_Id;
354       Constrained_Typ : Entity_Id;
355       Related_Node    : Node_Id;
356       Typ             : Entity_Id;
357       Constraints     : Elist_Id) return Entity_Id;
358    --  Given a discriminated base type Typ, a list of discriminant constraint
359    --  Constraints for Typ and a component of Typ, with type Compon_Type,
360    --  create and return the type corresponding to Compon_type where all
361    --  discriminant references are replaced with the corresponding constraint.
362    --  If no discriminant references occur in Compon_Typ then return it as is.
363    --  Constrained_Typ is the final constrained subtype to which the
364    --  constrained Compon_Type belongs. Related_Node is the node where we will
365    --  attach all the itypes created.
366    --
367    --  Above description is confused, what is Compon_Type???
368
369    procedure Constrain_Access
370      (Def_Id      : in out Entity_Id;
371       S           : Node_Id;
372       Related_Nod : Node_Id);
373    --  Apply a list of constraints to an access type. If Def_Id is empty, it is
374    --  an anonymous type created for a subtype indication. In that case it is
375    --  created in the procedure and attached to Related_Nod.
376
377    procedure Constrain_Array
378      (Def_Id      : in out Entity_Id;
379       SI          : Node_Id;
380       Related_Nod : Node_Id;
381       Related_Id  : Entity_Id;
382       Suffix      : Character);
383    --  Apply a list of index constraints to an unconstrained array type. The
384    --  first parameter is the entity for the resulting subtype. A value of
385    --  Empty for Def_Id indicates that an implicit type must be created, but
386    --  creation is delayed (and must be done by this procedure) because other
387    --  subsidiary implicit types must be created first (which is why Def_Id
388    --  is an in/out parameter). The second parameter is a subtype indication
389    --  node for the constrained array to be created (e.g. something of the
390    --  form string (1 .. 10)). Related_Nod gives the place where this type
391    --  has to be inserted in the tree. The Related_Id and Suffix parameters
392    --  are used to build the associated Implicit type name.
393
394    procedure Constrain_Concurrent
395      (Def_Id      : in out Entity_Id;
396       SI          : Node_Id;
397       Related_Nod : Node_Id;
398       Related_Id  : Entity_Id;
399       Suffix      : Character);
400    --  Apply list of discriminant constraints to an unconstrained concurrent
401    --  type.
402    --
403    --    SI is the N_Subtype_Indication node containing the constraint and
404    --    the unconstrained type to constrain.
405    --
406    --    Def_Id is the entity for the resulting constrained subtype. A value
407    --    of Empty for Def_Id indicates that an implicit type must be created,
408    --    but creation is delayed (and must be done by this procedure) because
409    --    other subsidiary implicit types must be created first (which is why
410    --    Def_Id is an in/out parameter).
411    --
412    --    Related_Nod gives the place where this type has to be inserted
413    --    in the tree
414    --
415    --  The last two arguments are used to create its external name if needed.
416
417    function Constrain_Corresponding_Record
418      (Prot_Subt   : Entity_Id;
419       Corr_Rec    : Entity_Id;
420       Related_Nod : Node_Id;
421       Related_Id  : Entity_Id) return Entity_Id;
422    --  When constraining a protected type or task type with discriminants,
423    --  constrain the corresponding record with the same discriminant values.
424
425    procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
426    --  Constrain a decimal fixed point type with a digits constraint and/or a
427    --  range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
428
429    procedure Constrain_Discriminated_Type
430      (Def_Id      : Entity_Id;
431       S           : Node_Id;
432       Related_Nod : Node_Id;
433       For_Access  : Boolean := False);
434    --  Process discriminant constraints of composite type. Verify that values
435    --  have been provided for all discriminants, that the original type is
436    --  unconstrained, and that the types of the supplied expressions match
437    --  the discriminant types. The first three parameters are like in routine
438    --  Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
439    --  of For_Access.
440
441    procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
442    --  Constrain an enumeration type with a range constraint. This is identical
443    --  to Constrain_Integer, but for the Ekind of the resulting subtype.
444
445    procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
446    --  Constrain a floating point type with either a digits constraint
447    --  and/or a range constraint, building a E_Floating_Point_Subtype.
448
449    procedure Constrain_Index
450      (Index        : Node_Id;
451       S            : Node_Id;
452       Related_Nod  : Node_Id;
453       Related_Id   : Entity_Id;
454       Suffix       : Character;
455       Suffix_Index : Nat);
456    --  Process an index constraint in a constrained array declaration. The
457    --  constraint can be a subtype name, or a range with or without an explicit
458    --  subtype mark. The index is the corresponding index of the unconstrained
459    --  array. The Related_Id and Suffix parameters are used to build the
460    --  associated Implicit type name.
461
462    procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
463    --  Build subtype of a signed or modular integer type
464
465    procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
466    --  Constrain an ordinary fixed point type with a range constraint, and
467    --  build an E_Ordinary_Fixed_Point_Subtype entity.
468
469    procedure Copy_And_Swap (Priv, Full : Entity_Id);
470    --  Copy the Priv entity into the entity of its full declaration then swap
471    --  the two entities in such a manner that the former private type is now
472    --  seen as a full type.
473
474    procedure Decimal_Fixed_Point_Type_Declaration
475      (T   : Entity_Id;
476       Def : Node_Id);
477    --  Create a new decimal fixed point type, and apply the constraint to
478    --  obtain a subtype of this new type.
479
480    procedure Complete_Private_Subtype
481      (Priv        : Entity_Id;
482       Full        : Entity_Id;
483       Full_Base   : Entity_Id;
484       Related_Nod : Node_Id);
485    --  Complete the implicit full view of a private subtype by setting the
486    --  appropriate semantic fields. If the full view of the parent is a record
487    --  type, build constrained components of subtype.
488
489    procedure Derive_Interface_Subprograms
490      (Parent_Type : Entity_Id;
491       Tagged_Type : Entity_Id;
492       Ifaces_List : Elist_Id);
493    --  Ada 2005 (AI-251): Derive primitives of abstract interface types that
494    --  are not immediate ancestors of Tagged type and associate them their
495    --  aliased primitive. Ifaces_List contains the abstract interface
496    --  primitives that have been derived from Parent_Type.
497
498    procedure Derived_Standard_Character
499      (N             : Node_Id;
500       Parent_Type   : Entity_Id;
501       Derived_Type  : Entity_Id);
502    --  Subsidiary procedure to Build_Derived_Enumeration_Type which handles
503    --  derivations from types Standard.Character and Standard.Wide_Character.
504
505    procedure Derived_Type_Declaration
506      (T             : Entity_Id;
507       N             : Node_Id;
508       Is_Completion : Boolean);
509    --  Process a derived type declaration. Build_Derived_Type is invoked
510    --  to process the actual derived type definition. Parameters N and
511    --  Is_Completion have the same meaning as in Build_Derived_Type.
512    --  T is the N_Defining_Identifier for the entity defined in the
513    --  N_Full_Type_Declaration node N, that is T is the derived type.
514
515    procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
516    --  Insert each literal in symbol table, as an overloadable identifier. Each
517    --  enumeration type is mapped into a sequence of integers, and each literal
518    --  is defined as a constant with integer value. If any of the literals are
519    --  character literals, the type is a character type, which means that
520    --  strings are legal aggregates for arrays of components of the type.
521
522    function Expand_To_Stored_Constraint
523      (Typ        : Entity_Id;
524       Constraint : Elist_Id) return Elist_Id;
525    --  Given a constraint (i.e. a list of expressions) on the discriminants of
526    --  Typ, expand it into a constraint on the stored discriminants and return
527    --  the new list of expressions constraining the stored discriminants.
528
529    function Find_Type_Of_Object
530      (Obj_Def     : Node_Id;
531       Related_Nod : Node_Id) return Entity_Id;
532    --  Get type entity for object referenced by Obj_Def, attaching the
533    --  implicit types generated to Related_Nod
534
535    procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
536    --  Create a new float and apply the constraint to obtain subtype of it
537
538    function Has_Range_Constraint (N : Node_Id) return Boolean;
539    --  Given an N_Subtype_Indication node N, return True if a range constraint
540    --  is present, either directly, or as part of a digits or delta constraint.
541    --  In addition, a digits constraint in the decimal case returns True, since
542    --  it establishes a default range if no explicit range is present.
543
544    function Inherit_Components
545      (N             : Node_Id;
546       Parent_Base   : Entity_Id;
547       Derived_Base  : Entity_Id;
548       Is_Tagged     : Boolean;
549       Inherit_Discr : Boolean;
550       Discs         : Elist_Id) return Elist_Id;
551    --  Called from Build_Derived_Record_Type to inherit the components of
552    --  Parent_Base (a base type) into the Derived_Base (the derived base type).
553    --  For more information on derived types and component inheritance please
554    --  consult the comment above the body of Build_Derived_Record_Type.
555    --
556    --    N is the original derived type declaration
557    --
558    --    Is_Tagged is set if we are dealing with tagged types
559    --
560    --    If Inherit_Discr is set, Derived_Base inherits its discriminants from
561    --    Parent_Base, otherwise no discriminants are inherited.
562    --
563    --    Discs gives the list of constraints that apply to Parent_Base in the
564    --    derived type declaration. If Discs is set to No_Elist, then we have
565    --    the following situation:
566    --
567    --      type Parent (D1..Dn : ..) is [tagged] record ...;
568    --      type Derived is new Parent [with ...];
569    --
570    --    which gets treated as
571    --
572    --      type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
573    --
574    --  For untagged types the returned value is an association list. The list
575    --  starts from the association (Parent_Base => Derived_Base), and then it
576    --  contains a sequence of the associations of the form
577    --
578    --    (Old_Component => New_Component),
579    --
580    --  where Old_Component is the Entity_Id of a component in Parent_Base and
581    --  New_Component is the Entity_Id of the corresponding component in
582    --  Derived_Base. For untagged records, this association list is needed when
583    --  copying the record declaration for the derived base. In the tagged case
584    --  the value returned is irrelevant.
585
586    function Is_Progenitor
587      (Iface : Entity_Id;
588       Typ   :  Entity_Id) return Boolean;
589    --  Determine whether type Typ implements interface Iface. This requires
590    --  traversing the list of abstract interfaces of the type, as well as that
591    --  of the ancestor types. The predicate is used to determine when a formal
592    --  in the signature of an inherited operation must carry the derived type.
593
594    function Is_Valid_Constraint_Kind
595      (T_Kind          : Type_Kind;
596       Constraint_Kind : Node_Kind) return Boolean;
597    --  Returns True if it is legal to apply the given kind of constraint to the
598    --  given kind of type (index constraint to an array type, for example).
599
600    procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
601    --  Create new modular type. Verify that modulus is in  bounds and is
602    --  a power of two (implementation restriction).
603
604    procedure New_Concatenation_Op (Typ : Entity_Id);
605    --  Create an abbreviated declaration for an operator in order to
606    --  materialize concatenation on array types.
607
608    procedure Ordinary_Fixed_Point_Type_Declaration
609      (T   : Entity_Id;
610       Def : Node_Id);
611    --  Create a new ordinary fixed point type, and apply the constraint to
612    --  obtain subtype of it.
613
614    procedure Prepare_Private_Subtype_Completion
615      (Id          : Entity_Id;
616       Related_Nod : Node_Id);
617    --  Id is a subtype of some private type. Creates the full declaration
618    --  associated with Id whenever possible, i.e. when the full declaration
619    --  of the base type is already known. Records each subtype into
620    --  Private_Dependents of the base type.
621
622    procedure Process_Incomplete_Dependents
623      (N      : Node_Id;
624       Full_T : Entity_Id;
625       Inc_T  : Entity_Id);
626    --  Process all entities that depend on an incomplete type. There include
627    --  subtypes, subprogram types that mention the incomplete type in their
628    --  profiles, and subprogram with access parameters that designate the
629    --  incomplete type.
630
631    --  Inc_T is the defining identifier of an incomplete type declaration, its
632    --  Ekind is E_Incomplete_Type.
633    --
634    --    N is the corresponding N_Full_Type_Declaration for Inc_T.
635    --
636    --    Full_T is N's defining identifier.
637    --
638    --  Subtypes of incomplete types with discriminants are completed when the
639    --  parent type is. This is simpler than private subtypes, because they can
640    --  only appear in the same scope, and there is no need to exchange views.
641    --  Similarly, access_to_subprogram types may have a parameter or a return
642    --  type that is an incomplete type, and that must be replaced with the
643    --  full type.
644    --
645    --  If the full type is tagged, subprogram with access parameters that
646    --  designated the incomplete may be primitive operations of the full type,
647    --  and have to be processed accordingly.
648
649    procedure Process_Real_Range_Specification (Def : Node_Id);
650    --  Given the type definition for a real type, this procedure processes and
651    --  checks the real range specification of this type definition if one is
652    --  present. If errors are found, error messages are posted, and the
653    --  Real_Range_Specification of Def is reset to Empty.
654
655    procedure Record_Type_Declaration
656      (T    : Entity_Id;
657       N    : Node_Id;
658       Prev : Entity_Id);
659    --  Process a record type declaration (for both untagged and tagged
660    --  records). Parameters T and N are exactly like in procedure
661    --  Derived_Type_Declaration, except that no flag Is_Completion is needed
662    --  for this routine. If this is the completion of an incomplete type
663    --  declaration, Prev is the entity of the incomplete declaration, used for
664    --  cross-referencing. Otherwise Prev = T.
665
666    procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
667    --  This routine is used to process the actual record type definition (both
668    --  for untagged and tagged records). Def is a record type definition node.
669    --  This procedure analyzes the components in this record type definition.
670    --  Prev_T is the entity for the enclosing record type. It is provided so
671    --  that its Has_Task flag can be set if any of the component have Has_Task
672    --  set. If the declaration is the completion of an incomplete type
673    --  declaration, Prev_T is the original incomplete type, whose full view is
674    --  the record type.
675
676    procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
677    --  Subsidiary to Build_Derived_Record_Type. For untagged records, we
678    --  build a copy of the declaration tree of the parent, and we create
679    --  independently the list of components for the derived type. Semantic
680    --  information uses the component entities, but record representation
681    --  clauses are validated on the declaration tree. This procedure replaces
682    --  discriminants and components in the declaration with those that have
683    --  been created by Inherit_Components.
684
685    procedure Set_Fixed_Range
686      (E   : Entity_Id;
687       Loc : Source_Ptr;
688       Lo  : Ureal;
689       Hi  : Ureal);
690    --  Build a range node with the given bounds and set it as the Scalar_Range
691    --  of the given fixed-point type entity. Loc is the source location used
692    --  for the constructed range. See body for further details.
693
694    procedure Set_Scalar_Range_For_Subtype
695      (Def_Id : Entity_Id;
696       R      : Node_Id;
697       Subt   : Entity_Id);
698    --  This routine is used to set the scalar range field for a subtype given
699    --  Def_Id, the entity for the subtype, and R, the range expression for the
700    --  scalar range. Subt provides the parent subtype to be used to analyze,
701    --  resolve, and check the given range.
702
703    procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
704    --  Create a new signed integer entity, and apply the constraint to obtain
705    --  the required first named subtype of this type.
706
707    procedure Set_Stored_Constraint_From_Discriminant_Constraint
708      (E : Entity_Id);
709    --  E is some record type. This routine computes E's Stored_Constraint
710    --  from its Discriminant_Constraint.
711
712    -----------------------
713    -- Access_Definition --
714    -----------------------
715
716    function Access_Definition
717      (Related_Nod : Node_Id;
718       N           : Node_Id) return Entity_Id
719    is
720       Loc        : constant Source_Ptr := Sloc (Related_Nod);
721       Anon_Type  : Entity_Id;
722       Anon_Scope : Entity_Id;
723       Desig_Type : Entity_Id;
724       Decl       : Entity_Id;
725
726    begin
727       if Is_Entry (Current_Scope)
728         and then Is_Task_Type (Etype (Scope (Current_Scope)))
729       then
730          Error_Msg_N ("task entries cannot have access parameters", N);
731          return Empty;
732       end if;
733
734       --  Ada 2005: for an object declaration the corresponding anonymous
735       --  type is declared in the current scope.
736
737       --  If the access definition is the return type of another access to
738       --  function, scope is the current one, because it is the one of the
739       --  current type declaration.
740
741       if Nkind_In (Related_Nod, N_Object_Declaration,
742                                 N_Access_Function_Definition)
743       then
744          Anon_Scope := Current_Scope;
745
746       --  For the anonymous function result case, retrieve the scope of the
747       --  function specification's associated entity rather than using the
748       --  current scope. The current scope will be the function itself if the
749       --  formal part is currently being analyzed, but will be the parent scope
750       --  in the case of a parameterless function, and we always want to use
751       --  the function's parent scope. Finally, if the function is a child
752       --  unit, we must traverse the tree to retrieve the proper entity.
753
754       elsif Nkind (Related_Nod) = N_Function_Specification
755         and then Nkind (Parent (N)) /= N_Parameter_Specification
756       then
757          --  If the current scope is a protected type, the anonymous access
758          --  is associated with one of the protected operations, and must
759          --  be available in the scope that encloses the protected declaration.
760          --  Otherwise the type is is in the scope enclosing the subprogram.
761
762          if Ekind (Current_Scope) = E_Protected_Type then
763             Anon_Scope := Scope (Scope (Defining_Entity (Related_Nod)));
764          else
765             Anon_Scope := Scope (Defining_Entity (Related_Nod));
766          end if;
767
768       else
769          --  For access formals, access components, and access discriminants,
770          --  the scope is that of the enclosing declaration,
771
772          Anon_Scope := Scope (Current_Scope);
773       end if;
774
775       Anon_Type :=
776         Create_Itype
777          (E_Anonymous_Access_Type, Related_Nod, Scope_Id =>  Anon_Scope);
778
779       if All_Present (N)
780         and then Ada_Version >= Ada_05
781       then
782          Error_Msg_N ("ALL is not permitted for anonymous access types", N);
783       end if;
784
785       --  Ada 2005 (AI-254): In case of anonymous access to subprograms call
786       --  the corresponding semantic routine
787
788       if Present (Access_To_Subprogram_Definition (N)) then
789          Access_Subprogram_Declaration
790            (T_Name => Anon_Type,
791             T_Def  => Access_To_Subprogram_Definition (N));
792
793          if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
794             Set_Ekind
795               (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
796          else
797             Set_Ekind
798               (Anon_Type, E_Anonymous_Access_Subprogram_Type);
799          end if;
800
801          Set_Can_Use_Internal_Rep
802            (Anon_Type, not Always_Compatible_Rep_On_Target);
803
804          --  If the anonymous access is associated with a protected operation
805          --  create a reference to it after the enclosing protected definition
806          --  because the itype will be used in the subsequent bodies.
807
808          if Ekind (Current_Scope) = E_Protected_Type then
809             Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
810          end if;
811
812          return Anon_Type;
813       end if;
814
815       Find_Type (Subtype_Mark (N));
816       Desig_Type := Entity (Subtype_Mark (N));
817
818       Set_Directly_Designated_Type
819                              (Anon_Type, Desig_Type);
820       Set_Etype              (Anon_Type, Anon_Type);
821
822       --  Make sure the anonymous access type has size and alignment fields
823       --  set, as required by gigi. This is necessary in the case of the
824       --  Task_Body_Procedure.
825
826       if not Has_Private_Component (Desig_Type) then
827          Layout_Type (Anon_Type);
828       end if;
829
830       --  ???The following makes no sense, because Anon_Type is an access type
831       --  and therefore cannot have components, private or otherwise. Hence
832       --  the assertion. Not sure what was meant, here.
833       Set_Depends_On_Private (Anon_Type, Has_Private_Component (Anon_Type));
834       pragma Assert (not Depends_On_Private (Anon_Type));
835
836       --  Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
837       --  from Ada 95 semantics. In Ada 2005, anonymous access must specify if
838       --  the null value is allowed. In Ada 95 the null value is never allowed.
839
840       if Ada_Version >= Ada_05 then
841          Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
842       else
843          Set_Can_Never_Be_Null (Anon_Type, True);
844       end if;
845
846       --  The anonymous access type is as public as the discriminated type or
847       --  subprogram that defines it. It is imported (for back-end purposes)
848       --  if the designated type is.
849
850       Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
851
852       --  Ada 2005 (AI-50217): Propagate the attribute that indicates that the
853       --  designated type comes from the limited view.
854
855       Set_From_With_Type (Anon_Type, From_With_Type (Desig_Type));
856
857       --  Ada 2005 (AI-231): Propagate the access-constant attribute
858
859       Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
860
861       --  The context is either a subprogram declaration, object declaration,
862       --  or an access discriminant, in a private or a full type declaration.
863       --  In the case of a subprogram, if the designated type is incomplete,
864       --  the operation will be a primitive operation of the full type, to be
865       --  updated subsequently. If the type is imported through a limited_with
866       --  clause, the subprogram is not a primitive operation of the type
867       --  (which is declared elsewhere in some other scope).
868
869       if Ekind (Desig_Type) = E_Incomplete_Type
870         and then not From_With_Type (Desig_Type)
871         and then Is_Overloadable (Current_Scope)
872       then
873          Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
874          Set_Has_Delayed_Freeze (Current_Scope);
875       end if;
876
877       --  Ada 2005: if the designated type is an interface that may contain
878       --  tasks, create a Master entity for the declaration. This must be done
879       --  before expansion of the full declaration, because the declaration may
880       --  include an expression that is an allocator, whose expansion needs the
881       --  proper Master for the created tasks.
882
883       if Nkind (Related_Nod) = N_Object_Declaration
884          and then Expander_Active
885       then
886          if Is_Interface (Desig_Type)
887            and then Is_Limited_Record (Desig_Type)
888          then
889             Build_Class_Wide_Master (Anon_Type);
890
891          --  Similarly, if the type is an anonymous access that designates
892          --  tasks, create a master entity for it in the current context.
893
894          elsif Has_Task (Desig_Type)
895            and then Comes_From_Source (Related_Nod)
896          then
897             if not Has_Master_Entity (Current_Scope) then
898                Decl :=
899                  Make_Object_Declaration (Loc,
900                    Defining_Identifier =>
901                      Make_Defining_Identifier (Loc, Name_uMaster),
902                    Constant_Present => True,
903                    Object_Definition =>
904                      New_Reference_To (RTE (RE_Master_Id), Loc),
905                    Expression =>
906                      Make_Explicit_Dereference (Loc,
907                        New_Reference_To (RTE (RE_Current_Master), Loc)));
908
909                Insert_Before (Related_Nod, Decl);
910                Analyze (Decl);
911
912                Set_Master_Id (Anon_Type, Defining_Identifier (Decl));
913                Set_Has_Master_Entity (Current_Scope);
914             else
915                Build_Master_Renaming (Related_Nod, Anon_Type);
916             end if;
917          end if;
918       end if;
919
920       --  For a private component of a protected type, it is imperative that
921       --  the back-end elaborate the type immediately after the protected
922       --  declaration, because this type will be used in the declarations
923       --  created for the component within each protected body, so we must
924       --  create an itype reference for it now.
925
926       if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
927          Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
928
929       --  Similarly, if the access definition is the return result of a
930       --  protected function, create an itype reference for it because it
931       --  will be used within the function body.
932
933       elsif Nkind (Related_Nod) = N_Function_Specification
934         and then  Ekind (Current_Scope) = E_Protected_Type
935       then
936          Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
937
938       --  Finally, create an itype reference for an object declaration of
939       --  an anonymous access type. This is strictly necessary only for
940       --  deferred constants, but in any case will avoid out-of-scope
941       --  problems in the back-end.
942
943       elsif Nkind (Related_Nod) = N_Object_Declaration then
944          Build_Itype_Reference (Anon_Type, Related_Nod);
945       end if;
946
947       return Anon_Type;
948    end Access_Definition;
949
950    -----------------------------------
951    -- Access_Subprogram_Declaration --
952    -----------------------------------
953
954    procedure Access_Subprogram_Declaration
955      (T_Name : Entity_Id;
956       T_Def  : Node_Id)
957    is
958
959       procedure Check_For_Premature_Usage (Def : Node_Id);
960       --  Check that type T_Name is not used, directly or recursively,
961       --  as a parameter or a return type in Def. Def is either a subtype,
962       --  an access_definition, or an access_to_subprogram_definition.
963
964       -------------------------------
965       -- Check_For_Premature_Usage --
966       -------------------------------
967
968       procedure Check_For_Premature_Usage (Def : Node_Id) is
969          Param : Node_Id;
970
971       begin
972          --  Check for a subtype mark
973
974          if Nkind (Def) in N_Has_Etype then
975             if Etype (Def) = T_Name then
976                Error_Msg_N
977                  ("type& cannot be used before end of its declaration", Def);
978             end if;
979
980          --  If this is not a subtype, then this is an access_definition
981
982          elsif Nkind (Def) = N_Access_Definition then
983             if Present (Access_To_Subprogram_Definition (Def)) then
984                Check_For_Premature_Usage
985                  (Access_To_Subprogram_Definition (Def));
986             else
987                Check_For_Premature_Usage (Subtype_Mark (Def));
988             end if;
989
990          --  The only cases left are N_Access_Function_Definition and
991          --  N_Access_Procedure_Definition.
992
993          else
994             if Present (Parameter_Specifications (Def)) then
995                Param := First (Parameter_Specifications (Def));
996                while Present (Param) loop
997                   Check_For_Premature_Usage (Parameter_Type (Param));
998                   Param := Next (Param);
999                end loop;
1000             end if;
1001
1002             if Nkind (Def) = N_Access_Function_Definition then
1003                Check_For_Premature_Usage (Result_Definition (Def));
1004             end if;
1005          end if;
1006       end Check_For_Premature_Usage;
1007
1008       --  Local variables
1009
1010       Formals    : constant List_Id := Parameter_Specifications (T_Def);
1011       Formal     : Entity_Id;
1012       D_Ityp     : Node_Id;
1013       Desig_Type : constant Entity_Id :=
1014                      Create_Itype (E_Subprogram_Type, Parent (T_Def));
1015
1016    --  Start of processing for Access_Subprogram_Declaration
1017
1018    begin
1019       --  Associate the Itype node with the inner full-type declaration or
1020       --  subprogram spec. This is required to handle nested anonymous
1021       --  declarations. For example:
1022
1023       --      procedure P
1024       --       (X : access procedure
1025       --                     (Y : access procedure
1026       --                                   (Z : access T)))
1027
1028       D_Ityp := Associated_Node_For_Itype (Desig_Type);
1029       while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1030                                    N_Private_Type_Declaration,
1031                                    N_Private_Extension_Declaration,
1032                                    N_Procedure_Specification,
1033                                    N_Function_Specification)
1034                    or else
1035                  Nkind_In (D_Ityp, N_Object_Declaration,
1036                                    N_Object_Renaming_Declaration,
1037                                    N_Formal_Type_Declaration,
1038                                    N_Task_Type_Declaration,
1039                                    N_Protected_Type_Declaration))
1040       loop
1041          D_Ityp := Parent (D_Ityp);
1042          pragma Assert (D_Ityp /= Empty);
1043       end loop;
1044
1045       Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1046
1047       if Nkind_In (D_Ityp, N_Procedure_Specification,
1048                            N_Function_Specification)
1049       then
1050          Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1051
1052       elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1053                               N_Object_Declaration,
1054                               N_Object_Renaming_Declaration,
1055                               N_Formal_Type_Declaration)
1056       then
1057          Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1058       end if;
1059
1060       if Nkind (T_Def) = N_Access_Function_Definition then
1061          if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1062             declare
1063                Acc : constant Node_Id := Result_Definition (T_Def);
1064
1065             begin
1066                if Present (Access_To_Subprogram_Definition (Acc))
1067                  and then
1068                    Protected_Present (Access_To_Subprogram_Definition (Acc))
1069                then
1070                   Set_Etype
1071                     (Desig_Type,
1072                        Replace_Anonymous_Access_To_Protected_Subprogram
1073                          (T_Def));
1074
1075                else
1076                   Set_Etype
1077                     (Desig_Type,
1078                        Access_Definition (T_Def, Result_Definition (T_Def)));
1079                end if;
1080             end;
1081
1082          else
1083             Analyze (Result_Definition (T_Def));
1084             Set_Etype (Desig_Type, Entity (Result_Definition (T_Def)));
1085          end if;
1086
1087          if not (Is_Type (Etype (Desig_Type))) then
1088             Error_Msg_N
1089               ("expect type in function specification",
1090                Result_Definition (T_Def));
1091          end if;
1092
1093       else
1094          Set_Etype (Desig_Type, Standard_Void_Type);
1095       end if;
1096
1097       if Present (Formals) then
1098          Push_Scope (Desig_Type);
1099          Process_Formals (Formals, Parent (T_Def));
1100
1101          --  A bit of a kludge here, End_Scope requires that the parent
1102          --  pointer be set to something reasonable, but Itypes don't have
1103          --  parent pointers. So we set it and then unset it ??? If and when
1104          --  Itypes have proper parent pointers to their declarations, this
1105          --  kludge can be removed.
1106
1107          Set_Parent (Desig_Type, T_Name);
1108          End_Scope;
1109          Set_Parent (Desig_Type, Empty);
1110       end if;
1111
1112       --  Check for premature usage of the type being defined
1113
1114       Check_For_Premature_Usage (T_Def);
1115
1116       --  The return type and/or any parameter type may be incomplete. Mark
1117       --  the subprogram_type as depending on the incomplete type, so that
1118       --  it can be updated when the full type declaration is seen. This
1119       --  only applies to incomplete types declared in some enclosing scope,
1120       --  not to limited views from other packages.
1121
1122       if Present (Formals) then
1123          Formal := First_Formal (Desig_Type);
1124          while Present (Formal) loop
1125             if Ekind (Formal) /= E_In_Parameter
1126               and then Nkind (T_Def) = N_Access_Function_Definition
1127             then
1128                Error_Msg_N ("functions can only have IN parameters", Formal);
1129             end if;
1130
1131             if Ekind (Etype (Formal)) = E_Incomplete_Type
1132               and then In_Open_Scopes (Scope (Etype (Formal)))
1133             then
1134                Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1135                Set_Has_Delayed_Freeze (Desig_Type);
1136             end if;
1137
1138             Next_Formal (Formal);
1139          end loop;
1140       end if;
1141
1142       if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1143         and then not Has_Delayed_Freeze (Desig_Type)
1144       then
1145          Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1146          Set_Has_Delayed_Freeze (Desig_Type);
1147       end if;
1148
1149       Check_Delayed_Subprogram (Desig_Type);
1150
1151       if Protected_Present (T_Def) then
1152          Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1153          Set_Convention (Desig_Type, Convention_Protected);
1154       else
1155          Set_Ekind (T_Name, E_Access_Subprogram_Type);
1156       end if;
1157
1158       Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1159
1160       Set_Etype                    (T_Name, T_Name);
1161       Init_Size_Align              (T_Name);
1162       Set_Directly_Designated_Type (T_Name, Desig_Type);
1163
1164       --  Ada 2005 (AI-231): Propagate the null-excluding attribute
1165
1166       Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1167
1168       Check_Restriction (No_Access_Subprograms, T_Def);
1169    end Access_Subprogram_Declaration;
1170
1171    ----------------------------
1172    -- Access_Type_Declaration --
1173    ----------------------------
1174
1175    procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1176       S : constant Node_Id := Subtype_Indication (Def);
1177       P : constant Node_Id := Parent (Def);
1178
1179       Desig : Entity_Id;
1180       --  Designated type
1181
1182    begin
1183       --  Check for permissible use of incomplete type
1184
1185       if Nkind (S) /= N_Subtype_Indication then
1186          Analyze (S);
1187
1188          if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1189             Set_Directly_Designated_Type (T, Entity (S));
1190          else
1191             Set_Directly_Designated_Type (T,
1192               Process_Subtype (S, P, T, 'P'));
1193          end if;
1194
1195       else
1196          Set_Directly_Designated_Type (T,
1197            Process_Subtype (S, P, T, 'P'));
1198       end if;
1199
1200       if All_Present (Def) or Constant_Present (Def) then
1201          Set_Ekind (T, E_General_Access_Type);
1202       else
1203          Set_Ekind (T, E_Access_Type);
1204       end if;
1205
1206       if Base_Type (Designated_Type (T)) = T then
1207          Error_Msg_N ("access type cannot designate itself", S);
1208
1209       --  In Ada 2005, the type may have a limited view through some unit
1210       --  in its own context, allowing the following circularity that cannot
1211       --  be detected earlier
1212
1213       elsif Is_Class_Wide_Type (Designated_Type (T))
1214         and then Etype (Designated_Type (T)) = T
1215       then
1216          Error_Msg_N
1217            ("access type cannot designate its own classwide type", S);
1218
1219          --  Clean up indication of tagged status to prevent cascaded errors
1220
1221          Set_Is_Tagged_Type (T, False);
1222       end if;
1223
1224       Set_Etype (T, T);
1225
1226       --  If the type has appeared already in a with_type clause, it is
1227       --  frozen and the pointer size is already set. Else, initialize.
1228
1229       if not From_With_Type (T) then
1230          Init_Size_Align (T);
1231       end if;
1232
1233       Desig := Designated_Type (T);
1234
1235       --  If designated type is an imported tagged type, indicate that the
1236       --  access type is also imported, and therefore restricted in its use.
1237       --  The access type may already be imported, so keep setting otherwise.
1238
1239       --  Ada 2005 (AI-50217): If the non-limited view of the designated type
1240       --  is available, use it as the designated type of the access type, so
1241       --  that the back-end gets a usable entity.
1242
1243       if From_With_Type (Desig)
1244         and then Ekind (Desig) /= E_Access_Type
1245       then
1246          Set_From_With_Type (T);
1247       end if;
1248
1249       --  Note that Has_Task is always false, since the access type itself
1250       --  is not a task type. See Einfo for more description on this point.
1251       --  Exactly the same consideration applies to Has_Controlled_Component.
1252
1253       Set_Has_Task (T, False);
1254       Set_Has_Controlled_Component (T, False);
1255
1256       --  Initialize Associated_Final_Chain explicitly to Empty, to avoid
1257       --  problems where an incomplete view of this entity has been previously
1258       --  established by a limited with and an overlaid version of this field
1259       --  (Stored_Constraint) was initialized for the incomplete view.
1260
1261       Set_Associated_Final_Chain (T, Empty);
1262
1263       --  Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1264       --  attributes
1265
1266       Set_Can_Never_Be_Null  (T, Null_Exclusion_Present (Def));
1267       Set_Is_Access_Constant (T, Constant_Present (Def));
1268    end Access_Type_Declaration;
1269
1270    ----------------------------------
1271    -- Add_Interface_Tag_Components --
1272    ----------------------------------
1273
1274    procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1275       Loc      : constant Source_Ptr := Sloc (N);
1276       Elmt     : Elmt_Id;
1277       Ext      : Node_Id;
1278       L        : List_Id;
1279       Last_Tag : Node_Id;
1280       Comp     : Node_Id;
1281
1282       procedure Add_Sync_Iface_Tags (T : Entity_Id);
1283       --  Local subprogram used to recursively climb through the parents
1284       --  of T to add the tags of all the progenitor interfaces.
1285
1286       procedure Add_Tag (Iface : Entity_Id);
1287       --  Add tag for one of the progenitor interfaces
1288
1289       -------------------------
1290       -- Add_Sync_Iface_Tags --
1291       -------------------------
1292
1293       procedure Add_Sync_Iface_Tags (T : Entity_Id) is
1294       begin
1295          if Etype (T) /= T then
1296             Add_Sync_Iface_Tags (Etype (T));
1297          end if;
1298
1299          Elmt := First_Elmt (Abstract_Interfaces (T));
1300          while Present (Elmt) loop
1301             Add_Tag (Node (Elmt));
1302             Next_Elmt (Elmt);
1303          end loop;
1304       end Add_Sync_Iface_Tags;
1305
1306       -------------
1307       -- Add_Tag --
1308       -------------
1309
1310       procedure Add_Tag (Iface : Entity_Id) is
1311          Decl   : Node_Id;
1312          Def    : Node_Id;
1313          Tag    : Entity_Id;
1314          Offset : Entity_Id;
1315
1316       begin
1317          pragma Assert (Is_Tagged_Type (Iface)
1318            and then Is_Interface (Iface));
1319
1320          Def :=
1321            Make_Component_Definition (Loc,
1322              Aliased_Present    => True,
1323              Subtype_Indication =>
1324                New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1325
1326          Tag := Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1327
1328          Decl :=
1329            Make_Component_Declaration (Loc,
1330              Defining_Identifier  => Tag,
1331              Component_Definition => Def);
1332
1333          Analyze_Component_Declaration (Decl);
1334
1335          Set_Analyzed (Decl);
1336          Set_Ekind               (Tag, E_Component);
1337          Set_Is_Tag              (Tag);
1338          Set_Is_Aliased          (Tag);
1339          Set_Related_Type        (Tag, Iface);
1340          Init_Component_Location (Tag);
1341
1342          pragma Assert (Is_Frozen (Iface));
1343
1344          Set_DT_Entry_Count    (Tag,
1345            DT_Entry_Count (First_Entity (Iface)));
1346
1347          if No (Last_Tag) then
1348             Prepend (Decl, L);
1349          else
1350             Insert_After (Last_Tag, Decl);
1351          end if;
1352
1353          Last_Tag := Decl;
1354
1355          --  If the ancestor has discriminants we need to give special support
1356          --  to store the offset_to_top value of the secondary dispatch tables.
1357          --  For this purpose we add a supplementary component just after the
1358          --  field that contains the tag associated with each secondary DT.
1359
1360          if Typ /= Etype (Typ)
1361            and then Has_Discriminants (Etype (Typ))
1362          then
1363             Def :=
1364               Make_Component_Definition (Loc,
1365                 Subtype_Indication =>
1366                   New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1367
1368             Offset :=
1369               Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1370
1371             Decl :=
1372               Make_Component_Declaration (Loc,
1373                 Defining_Identifier  => Offset,
1374                 Component_Definition => Def);
1375
1376             Analyze_Component_Declaration (Decl);
1377
1378             Set_Analyzed (Decl);
1379             Set_Ekind               (Offset, E_Component);
1380             Set_Is_Aliased          (Offset);
1381             Set_Related_Type        (Offset, Iface);
1382             Init_Component_Location (Offset);
1383             Insert_After (Last_Tag, Decl);
1384             Last_Tag := Decl;
1385          end if;
1386       end Add_Tag;
1387
1388       --  Local variables
1389
1390       Iface_List : List_Id;
1391
1392    --  Start of processing for Add_Interface_Tag_Components
1393
1394    begin
1395       if not RTE_Available (RE_Interface_Tag) then
1396          Error_Msg
1397            ("(Ada 2005) interface types not supported by this run-time!",
1398             Sloc (N));
1399          return;
1400       end if;
1401
1402       if Ekind (Typ) /= E_Record_Type
1403         or else (Is_Concurrent_Record_Type (Typ)
1404                   and then Is_Empty_List (Abstract_Interface_List (Typ)))
1405         or else (not Is_Concurrent_Record_Type (Typ)
1406                   and then No (Abstract_Interfaces (Typ))
1407                   and then Is_Empty_Elmt_List (Abstract_Interfaces (Typ)))
1408       then
1409          return;
1410       end if;
1411
1412       --  Find the current last tag
1413
1414       if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1415          Ext := Record_Extension_Part (Type_Definition (N));
1416       else
1417          pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1418          Ext := Type_Definition (N);
1419       end if;
1420
1421       Last_Tag := Empty;
1422
1423       if not (Present (Component_List (Ext))) then
1424          Set_Null_Present (Ext, False);
1425          L := New_List;
1426          Set_Component_List (Ext,
1427            Make_Component_List (Loc,
1428              Component_Items => L,
1429              Null_Present => False));
1430       else
1431          if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1432             L := Component_Items
1433                    (Component_List
1434                      (Record_Extension_Part
1435                        (Type_Definition (N))));
1436          else
1437             L := Component_Items
1438                    (Component_List
1439                      (Type_Definition (N)));
1440          end if;
1441
1442          --  Find the last tag component
1443
1444          Comp := First (L);
1445          while Present (Comp) loop
1446             if Nkind (Comp) = N_Component_Declaration
1447               and then Is_Tag (Defining_Identifier (Comp))
1448             then
1449                Last_Tag := Comp;
1450             end if;
1451
1452             Next (Comp);
1453          end loop;
1454       end if;
1455
1456       --  At this point L references the list of components and Last_Tag
1457       --  references the current last tag (if any). Now we add the tag
1458       --  corresponding with all the interfaces that are not implemented
1459       --  by the parent.
1460
1461       if Is_Concurrent_Record_Type (Typ) then
1462          Iface_List := Abstract_Interface_List (Typ);
1463
1464          if Is_Non_Empty_List (Iface_List) then
1465             Add_Sync_Iface_Tags (Etype (First (Iface_List)));
1466          end if;
1467       end if;
1468
1469       if Present (Abstract_Interfaces (Typ)) then
1470          Elmt := First_Elmt (Abstract_Interfaces (Typ));
1471          while Present (Elmt) loop
1472             Add_Tag (Node (Elmt));
1473             Next_Elmt (Elmt);
1474          end loop;
1475       end if;
1476    end Add_Interface_Tag_Components;
1477
1478    -----------------------------------
1479    -- Analyze_Component_Declaration --
1480    -----------------------------------
1481
1482    procedure Analyze_Component_Declaration (N : Node_Id) is
1483       Id : constant Entity_Id := Defining_Identifier (N);
1484       E  : constant Node_Id   := Expression (N);
1485       T  : Entity_Id;
1486       P  : Entity_Id;
1487
1488       function Contains_POC (Constr : Node_Id) return Boolean;
1489       --  Determines whether a constraint uses the discriminant of a record
1490       --  type thus becoming a per-object constraint (POC).
1491
1492       function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1493       --  Typ is the type of the current component, check whether this type is
1494       --  a limited type. Used to validate declaration against that of
1495       --  enclosing record.
1496
1497       ------------------
1498       -- Contains_POC --
1499       ------------------
1500
1501       function Contains_POC (Constr : Node_Id) return Boolean is
1502       begin
1503          --  Prevent cascaded errors
1504
1505          if Error_Posted (Constr) then
1506             return False;
1507          end if;
1508
1509          case Nkind (Constr) is
1510             when N_Attribute_Reference =>
1511                return
1512                  Attribute_Name (Constr) = Name_Access
1513                    and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1514
1515             when N_Discriminant_Association =>
1516                return Denotes_Discriminant (Expression (Constr));
1517
1518             when N_Identifier =>
1519                return Denotes_Discriminant (Constr);
1520
1521             when N_Index_Or_Discriminant_Constraint =>
1522                declare
1523                   IDC : Node_Id;
1524
1525                begin
1526                   IDC := First (Constraints (Constr));
1527                   while Present (IDC) loop
1528
1529                      --  One per-object constraint is sufficient
1530
1531                      if Contains_POC (IDC) then
1532                         return True;
1533                      end if;
1534
1535                      Next (IDC);
1536                   end loop;
1537
1538                   return False;
1539                end;
1540
1541             when N_Range =>
1542                return Denotes_Discriminant (Low_Bound (Constr))
1543                         or else
1544                       Denotes_Discriminant (High_Bound (Constr));
1545
1546             when N_Range_Constraint =>
1547                return Denotes_Discriminant (Range_Expression (Constr));
1548
1549             when others =>
1550                return False;
1551
1552          end case;
1553       end Contains_POC;
1554
1555       ----------------------
1556       -- Is_Known_Limited --
1557       ----------------------
1558
1559       function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1560          P : constant Entity_Id := Etype (Typ);
1561          R : constant Entity_Id := Root_Type (Typ);
1562
1563       begin
1564          if Is_Limited_Record (Typ) then
1565             return True;
1566
1567          --  If the root type is limited (and not a limited interface)
1568          --  so is the current type
1569
1570          elsif Is_Limited_Record (R)
1571            and then
1572              (not Is_Interface (R)
1573                or else not Is_Limited_Interface (R))
1574          then
1575             return True;
1576
1577          --  Else the type may have a limited interface progenitor, but a
1578          --  limited record parent.
1579
1580          elsif R /= P
1581            and then Is_Limited_Record (P)
1582          then
1583             return True;
1584
1585          else
1586             return False;
1587          end if;
1588       end Is_Known_Limited;
1589
1590    --  Start of processing for Analyze_Component_Declaration
1591
1592    begin
1593       Generate_Definition (Id);
1594       Enter_Name (Id);
1595
1596       if Present (Subtype_Indication (Component_Definition (N))) then
1597          T := Find_Type_Of_Object
1598                 (Subtype_Indication (Component_Definition (N)), N);
1599
1600       --  Ada 2005 (AI-230): Access Definition case
1601
1602       else
1603          pragma Assert (Present
1604                           (Access_Definition (Component_Definition (N))));
1605
1606          T := Access_Definition
1607                 (Related_Nod => N,
1608                  N => Access_Definition (Component_Definition (N)));
1609          Set_Is_Local_Anonymous_Access (T);
1610
1611          --  Ada 2005 (AI-254)
1612
1613          if Present (Access_To_Subprogram_Definition
1614                       (Access_Definition (Component_Definition (N))))
1615            and then Protected_Present (Access_To_Subprogram_Definition
1616                                         (Access_Definition
1617                                           (Component_Definition (N))))
1618          then
1619             T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1620          end if;
1621       end if;
1622
1623       --  If the subtype is a constrained subtype of the enclosing record,
1624       --  (which must have a partial view) the back-end does not properly
1625       --  handle the recursion. Rewrite the component declaration with an
1626       --  explicit subtype indication, which is acceptable to Gigi. We can copy
1627       --  the tree directly because side effects have already been removed from
1628       --  discriminant constraints.
1629
1630       if Ekind (T) = E_Access_Subtype
1631         and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1632         and then Comes_From_Source (T)
1633         and then Nkind (Parent (T)) = N_Subtype_Declaration
1634         and then Etype (Directly_Designated_Type (T)) = Current_Scope
1635       then
1636          Rewrite
1637            (Subtype_Indication (Component_Definition (N)),
1638              New_Copy_Tree (Subtype_Indication (Parent (T))));
1639          T := Find_Type_Of_Object
1640                  (Subtype_Indication (Component_Definition (N)), N);
1641       end if;
1642
1643       --  If the component declaration includes a default expression, then we
1644       --  check that the component is not of a limited type (RM 3.7(5)),
1645       --  and do the special preanalysis of the expression (see section on
1646       --  "Handling of Default and Per-Object Expressions" in the spec of
1647       --  package Sem).
1648
1649       if Present (E) then
1650          Preanalyze_Spec_Expression (E, T);
1651          Check_Initialization (T, E);
1652
1653          if Ada_Version >= Ada_05
1654            and then Ekind (T) = E_Anonymous_Access_Type
1655          then
1656             --  Check RM 3.9.2(9): "if the expected type for an expression is
1657             --  an anonymous access-to-specific tagged type, then the object
1658             --  designated by the expression shall not be dynamically tagged
1659             --  unless it is a controlling operand in a call on a dispatching
1660             --  operation"
1661
1662             if Is_Tagged_Type (Directly_Designated_Type (T))
1663               and then
1664                 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1665               and then
1666                 Ekind (Directly_Designated_Type (Etype (E))) =
1667                   E_Class_Wide_Type
1668             then
1669                Error_Msg_N
1670                  ("access to specific tagged type required (RM 3.9.2(9))", E);
1671             end if;
1672
1673             --  (Ada 2005: AI-230): Accessibility check for anonymous
1674             --  components
1675
1676             if Type_Access_Level (Etype (E)) > Type_Access_Level (T) then
1677                Error_Msg_N
1678                  ("expression has deeper access level than component " &
1679                   "(RM 3.10.2 (12.2))", E);
1680             end if;
1681
1682             --  The initialization expression is a reference to an access
1683             --  discriminant. The type of the discriminant is always deeper
1684             --  than any access type.
1685
1686             if Ekind (Etype (E)) = E_Anonymous_Access_Type
1687               and then Is_Entity_Name (E)
1688               and then Ekind (Entity (E)) = E_In_Parameter
1689               and then Present (Discriminal_Link (Entity (E)))
1690             then
1691                Error_Msg_N
1692                  ("discriminant has deeper accessibility level than target",
1693                   E);
1694             end if;
1695          end if;
1696       end if;
1697
1698       --  The parent type may be a private view with unknown discriminants,
1699       --  and thus unconstrained. Regular components must be constrained.
1700
1701       if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1702          if Is_Class_Wide_Type (T) then
1703             Error_Msg_N
1704                ("class-wide subtype with unknown discriminants" &
1705                  " in component declaration",
1706                  Subtype_Indication (Component_Definition (N)));
1707          else
1708             Error_Msg_N
1709               ("unconstrained subtype in component declaration",
1710                Subtype_Indication (Component_Definition (N)));
1711          end if;
1712
1713       --  Components cannot be abstract, except for the special case of
1714       --  the _Parent field (case of extending an abstract tagged type)
1715
1716       elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
1717          Error_Msg_N ("type of a component cannot be abstract", N);
1718       end if;
1719
1720       Set_Etype (Id, T);
1721       Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1722
1723       --  The component declaration may have a per-object constraint, set
1724       --  the appropriate flag in the defining identifier of the subtype.
1725
1726       if Present (Subtype_Indication (Component_Definition (N))) then
1727          declare
1728             Sindic : constant Node_Id :=
1729                        Subtype_Indication (Component_Definition (N));
1730          begin
1731             if Nkind (Sindic) = N_Subtype_Indication
1732               and then Present (Constraint (Sindic))
1733               and then Contains_POC (Constraint (Sindic))
1734             then
1735                Set_Has_Per_Object_Constraint (Id);
1736             end if;
1737          end;
1738       end if;
1739
1740       --  Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1741       --  out some static checks.
1742
1743       if Ada_Version >= Ada_05
1744         and then Can_Never_Be_Null (T)
1745       then
1746          Null_Exclusion_Static_Checks (N);
1747       end if;
1748
1749       --  If this component is private (or depends on a private type), flag the
1750       --  record type to indicate that some operations are not available.
1751
1752       P := Private_Component (T);
1753
1754       if Present (P) then
1755
1756          --  Check for circular definitions
1757
1758          if P = Any_Type then
1759             Set_Etype (Id, Any_Type);
1760
1761          --  There is a gap in the visibility of operations only if the
1762          --  component type is not defined in the scope of the record type.
1763
1764          elsif Scope (P) = Scope (Current_Scope) then
1765             null;
1766
1767          elsif Is_Limited_Type (P) then
1768             Set_Is_Limited_Composite (Current_Scope);
1769
1770          else
1771             Set_Is_Private_Composite (Current_Scope);
1772          end if;
1773       end if;
1774
1775       if P /= Any_Type
1776         and then Is_Limited_Type (T)
1777         and then Chars (Id) /= Name_uParent
1778         and then Is_Tagged_Type (Current_Scope)
1779       then
1780          if Is_Derived_Type (Current_Scope)
1781            and then not Is_Known_Limited (Current_Scope)
1782          then
1783             Error_Msg_N
1784               ("extension of nonlimited type cannot have limited components",
1785                N);
1786
1787             if Is_Interface (Root_Type (Current_Scope)) then
1788                Error_Msg_N
1789                  ("\limitedness is not inherited from limited interface", N);
1790                Error_Msg_N
1791                  ("\add LIMITED to type indication", N);
1792             end if;
1793
1794             Explain_Limited_Type (T, N);
1795             Set_Etype (Id, Any_Type);
1796             Set_Is_Limited_Composite (Current_Scope, False);
1797
1798          elsif not Is_Derived_Type (Current_Scope)
1799            and then not Is_Limited_Record (Current_Scope)
1800            and then not Is_Concurrent_Type (Current_Scope)
1801          then
1802             Error_Msg_N
1803               ("nonlimited tagged type cannot have limited components", N);
1804             Explain_Limited_Type (T, N);
1805             Set_Etype (Id, Any_Type);
1806             Set_Is_Limited_Composite (Current_Scope, False);
1807          end if;
1808       end if;
1809
1810       Set_Original_Record_Component (Id, Id);
1811    end Analyze_Component_Declaration;
1812
1813    --------------------------
1814    -- Analyze_Declarations --
1815    --------------------------
1816
1817    procedure Analyze_Declarations (L : List_Id) is
1818       D           : Node_Id;
1819       Freeze_From : Entity_Id := Empty;
1820       Next_Node   : Node_Id;
1821
1822       procedure Adjust_D;
1823       --  Adjust D not to include implicit label declarations, since these
1824       --  have strange Sloc values that result in elaboration check problems.
1825       --  (They have the sloc of the label as found in the source, and that
1826       --  is ahead of the current declarative part).
1827
1828       --------------
1829       -- Adjust_D --
1830       --------------
1831
1832       procedure Adjust_D is
1833       begin
1834          while Present (Prev (D))
1835            and then Nkind (D) = N_Implicit_Label_Declaration
1836          loop
1837             Prev (D);
1838          end loop;
1839       end Adjust_D;
1840
1841    --  Start of processing for Analyze_Declarations
1842
1843    begin
1844       D := First (L);
1845       while Present (D) loop
1846
1847          --  Complete analysis of declaration
1848
1849          Analyze (D);
1850          Next_Node := Next (D);
1851
1852          if No (Freeze_From) then
1853             Freeze_From := First_Entity (Current_Scope);
1854          end if;
1855
1856          --  At the end of a declarative part, freeze remaining entities
1857          --  declared in it. The end of the visible declarations of package
1858          --  specification is not the end of a declarative part if private
1859          --  declarations are present. The end of a package declaration is a
1860          --  freezing point only if it a library package. A task definition or
1861          --  protected type definition is not a freeze point either. Finally,
1862          --  we do not freeze entities in generic scopes, because there is no
1863          --  code generated for them and freeze nodes will be generated for
1864          --  the instance.
1865
1866          --  The end of a package instantiation is not a freeze point, but
1867          --  for now we make it one, because the generic body is inserted
1868          --  (currently) immediately after. Generic instantiations will not
1869          --  be a freeze point once delayed freezing of bodies is implemented.
1870          --  (This is needed in any case for early instantiations ???).
1871
1872          if No (Next_Node) then
1873             if Nkind_In (Parent (L), N_Component_List,
1874                                      N_Task_Definition,
1875                                      N_Protected_Definition)
1876             then
1877                null;
1878
1879             elsif Nkind (Parent (L)) /= N_Package_Specification then
1880                if Nkind (Parent (L)) = N_Package_Body then
1881                   Freeze_From := First_Entity (Current_Scope);
1882                end if;
1883
1884                Adjust_D;
1885                Freeze_All (Freeze_From, D);
1886                Freeze_From := Last_Entity (Current_Scope);
1887
1888             elsif Scope (Current_Scope) /= Standard_Standard
1889               and then not Is_Child_Unit (Current_Scope)
1890               and then No (Generic_Parent (Parent (L)))
1891             then
1892                null;
1893
1894             elsif L /= Visible_Declarations (Parent (L))
1895                or else No (Private_Declarations (Parent (L)))
1896                or else Is_Empty_List (Private_Declarations (Parent (L)))
1897             then
1898                Adjust_D;
1899                Freeze_All (Freeze_From, D);
1900                Freeze_From := Last_Entity (Current_Scope);
1901             end if;
1902
1903          --  If next node is a body then freeze all types before the body.
1904          --  An exception occurs for some expander-generated bodies. If these
1905          --  are generated at places where in general language rules would not
1906          --  allow a freeze point, then we assume that the expander has
1907          --  explicitly checked that all required types are properly frozen,
1908          --  and we do not cause general freezing here. This special circuit
1909          --  is used when the encountered body is marked as having already
1910          --  been analyzed.
1911
1912          --  In all other cases (bodies that come from source, and expander
1913          --  generated bodies that have not been analyzed yet), freeze all
1914          --  types now. Note that in the latter case, the expander must take
1915          --  care to attach the bodies at a proper place in the tree so as to
1916          --  not cause unwanted freezing at that point.
1917
1918          elsif not Analyzed (Next_Node)
1919            and then (Nkind_In (Next_Node, N_Subprogram_Body,
1920                                           N_Entry_Body,
1921                                           N_Package_Body,
1922                                           N_Protected_Body,
1923                                           N_Task_Body)
1924                        or else
1925                      Nkind (Next_Node) in N_Body_Stub)
1926          then
1927             Adjust_D;
1928             Freeze_All (Freeze_From, D);
1929             Freeze_From := Last_Entity (Current_Scope);
1930          end if;
1931
1932          D := Next_Node;
1933       end loop;
1934    end Analyze_Declarations;
1935
1936    ----------------------------------
1937    -- Analyze_Incomplete_Type_Decl --
1938    ----------------------------------
1939
1940    procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
1941       F : constant Boolean := Is_Pure (Current_Scope);
1942       T : Entity_Id;
1943
1944    begin
1945       Generate_Definition (Defining_Identifier (N));
1946
1947       --  Process an incomplete declaration. The identifier must not have been
1948       --  declared already in the scope. However, an incomplete declaration may
1949       --  appear in the private part of a package, for a private type that has
1950       --  already been declared.
1951
1952       --  In this case, the discriminants (if any) must match
1953
1954       T := Find_Type_Name (N);
1955
1956       Set_Ekind (T, E_Incomplete_Type);
1957       Init_Size_Align (T);
1958       Set_Is_First_Subtype (T, True);
1959       Set_Etype (T, T);
1960
1961       --  Ada 2005 (AI-326): Minimum decoration to give support to tagged
1962       --  incomplete types.
1963
1964       if Tagged_Present (N) then
1965          Set_Is_Tagged_Type (T);
1966          Make_Class_Wide_Type (T);
1967          Set_Primitive_Operations (T, New_Elmt_List);
1968       end if;
1969
1970       Push_Scope (T);
1971
1972       Set_Stored_Constraint (T, No_Elist);
1973
1974       if Present (Discriminant_Specifications (N)) then
1975          Process_Discriminants (N);
1976       end if;
1977
1978       End_Scope;
1979
1980       --  If the type has discriminants, non-trivial subtypes may be be
1981       --  declared before the full view of the type. The full views of those
1982       --  subtypes will be built after the full view of the type.
1983
1984       Set_Private_Dependents (T, New_Elmt_List);
1985       Set_Is_Pure (T, F);
1986    end Analyze_Incomplete_Type_Decl;
1987
1988    -----------------------------------
1989    -- Analyze_Interface_Declaration --
1990    -----------------------------------
1991
1992    procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
1993       CW : constant Entity_Id := Class_Wide_Type (T);
1994
1995    begin
1996       Set_Is_Tagged_Type      (T);
1997
1998       Set_Is_Limited_Record   (T, Limited_Present (Def)
1999                                    or else Task_Present (Def)
2000                                    or else Protected_Present (Def)
2001                                    or else Synchronized_Present (Def));
2002
2003       --  Type is abstract if full declaration carries keyword, or if previous
2004       --  partial view did.
2005
2006       Set_Is_Abstract_Type (T);
2007       Set_Is_Interface     (T);
2008
2009       --  Type is a limited interface if it includes the keyword limited, task,
2010       --  protected, or synchronized.
2011
2012       Set_Is_Limited_Interface
2013         (T, Limited_Present (Def)
2014               or else Protected_Present (Def)
2015               or else Synchronized_Present (Def)
2016               or else Task_Present (Def));
2017
2018       Set_Is_Protected_Interface    (T, Protected_Present (Def));
2019       Set_Is_Task_Interface         (T, Task_Present (Def));
2020
2021       --  Type is a synchronized interface if it includes the keyword task,
2022       --  protected, or synchronized.
2023
2024       Set_Is_Synchronized_Interface
2025         (T, Synchronized_Present (Def)
2026               or else Protected_Present (Def)
2027               or else Task_Present (Def));
2028
2029       Set_Abstract_Interfaces       (T, New_Elmt_List);
2030       Set_Primitive_Operations      (T, New_Elmt_List);
2031
2032       --  Complete the decoration of the class-wide entity if it was already
2033       --  built (i.e. during the creation of the limited view)
2034
2035       if Present (CW) then
2036          Set_Is_Interface (CW);
2037          Set_Is_Limited_Interface      (CW, Is_Limited_Interface (T));
2038          Set_Is_Protected_Interface    (CW, Is_Protected_Interface (T));
2039          Set_Is_Synchronized_Interface (CW, Is_Synchronized_Interface (T));
2040          Set_Is_Task_Interface         (CW, Is_Task_Interface (T));
2041       end if;
2042
2043       --  Check runtime support for synchronized interfaces
2044
2045       if VM_Target = No_VM
2046         and then (Is_Task_Interface (T)
2047                     or else Is_Protected_Interface (T)
2048                     or else Is_Synchronized_Interface (T))
2049         and then not RTE_Available (RE_Select_Specific_Data)
2050       then
2051          Error_Msg_CRT ("synchronized interfaces", T);
2052       end if;
2053    end Analyze_Interface_Declaration;
2054
2055    -----------------------------
2056    -- Analyze_Itype_Reference --
2057    -----------------------------
2058
2059    --  Nothing to do. This node is placed in the tree only for the benefit of
2060    --  back end processing, and has no effect on the semantic processing.
2061
2062    procedure Analyze_Itype_Reference (N : Node_Id) is
2063    begin
2064       pragma Assert (Is_Itype (Itype (N)));
2065       null;
2066    end Analyze_Itype_Reference;
2067
2068    --------------------------------
2069    -- Analyze_Number_Declaration --
2070    --------------------------------
2071
2072    procedure Analyze_Number_Declaration (N : Node_Id) is
2073       Id    : constant Entity_Id := Defining_Identifier (N);
2074       E     : constant Node_Id   := Expression (N);
2075       T     : Entity_Id;
2076       Index : Interp_Index;
2077       It    : Interp;
2078
2079    begin
2080       Generate_Definition (Id);
2081       Enter_Name (Id);
2082
2083       --  This is an optimization of a common case of an integer literal
2084
2085       if Nkind (E) = N_Integer_Literal then
2086          Set_Is_Static_Expression (E, True);
2087          Set_Etype                (E, Universal_Integer);
2088
2089          Set_Etype     (Id, Universal_Integer);
2090          Set_Ekind     (Id, E_Named_Integer);
2091          Set_Is_Frozen (Id, True);
2092          return;
2093       end if;
2094
2095       Set_Is_Pure (Id, Is_Pure (Current_Scope));
2096
2097       --  Process expression, replacing error by integer zero, to avoid
2098       --  cascaded errors or aborts further along in the processing
2099
2100       --  Replace Error by integer zero, which seems least likely to
2101       --  cause cascaded errors.
2102
2103       if E = Error then
2104          Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2105          Set_Error_Posted (E);
2106       end if;
2107
2108       Analyze (E);
2109
2110       --  Verify that the expression is static and numeric. If
2111       --  the expression is overloaded, we apply the preference
2112       --  rule that favors root numeric types.
2113
2114       if not Is_Overloaded (E) then
2115          T := Etype (E);
2116
2117       else
2118          T := Any_Type;
2119
2120          Get_First_Interp (E, Index, It);
2121          while Present (It.Typ) loop
2122             if (Is_Integer_Type (It.Typ)
2123                  or else Is_Real_Type (It.Typ))
2124               and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2125             then
2126                if T = Any_Type then
2127                   T := It.Typ;
2128
2129                elsif It.Typ = Universal_Real
2130                  or else It.Typ = Universal_Integer
2131                then
2132                   --  Choose universal interpretation over any other
2133
2134                   T := It.Typ;
2135                   exit;
2136                end if;
2137             end if;
2138
2139             Get_Next_Interp (Index, It);
2140          end loop;
2141       end if;
2142
2143       if Is_Integer_Type (T)  then
2144          Resolve (E, T);
2145          Set_Etype (Id, Universal_Integer);
2146          Set_Ekind (Id, E_Named_Integer);
2147
2148       elsif Is_Real_Type (T) then
2149
2150          --  Because the real value is converted to universal_real, this is a
2151          --  legal context for a universal fixed expression.
2152
2153          if T = Universal_Fixed then
2154             declare
2155                Loc  : constant Source_Ptr := Sloc (N);
2156                Conv : constant Node_Id := Make_Type_Conversion (Loc,
2157                         Subtype_Mark =>
2158                           New_Occurrence_Of (Universal_Real, Loc),
2159                         Expression => Relocate_Node (E));
2160
2161             begin
2162                Rewrite (E, Conv);
2163                Analyze (E);
2164             end;
2165
2166          elsif T = Any_Fixed then
2167             Error_Msg_N ("illegal context for mixed mode operation", E);
2168
2169             --  Expression is of the form : universal_fixed * integer. Try to
2170             --  resolve as universal_real.
2171
2172             T := Universal_Real;
2173             Set_Etype (E, T);
2174          end if;
2175
2176          Resolve (E, T);
2177          Set_Etype (Id, Universal_Real);
2178          Set_Ekind (Id, E_Named_Real);
2179
2180       else
2181          Wrong_Type (E, Any_Numeric);
2182          Resolve (E, T);
2183
2184          Set_Etype               (Id, T);
2185          Set_Ekind               (Id, E_Constant);
2186          Set_Never_Set_In_Source (Id, True);
2187          Set_Is_True_Constant    (Id, True);
2188          return;
2189       end if;
2190
2191       if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
2192          Set_Etype (E, Etype (Id));
2193       end if;
2194
2195       if not Is_OK_Static_Expression (E) then
2196          Flag_Non_Static_Expr
2197            ("non-static expression used in number declaration!", E);
2198          Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2199          Set_Etype (E, Any_Type);
2200       end if;
2201    end Analyze_Number_Declaration;
2202
2203    --------------------------------
2204    -- Analyze_Object_Declaration --
2205    --------------------------------
2206
2207    procedure Analyze_Object_Declaration (N : Node_Id) is
2208       Loc   : constant Source_Ptr := Sloc (N);
2209       Id    : constant Entity_Id  := Defining_Identifier (N);
2210       T     : Entity_Id;
2211       Act_T : Entity_Id;
2212
2213       E : Node_Id := Expression (N);
2214       --  E is set to Expression (N) throughout this routine. When
2215       --  Expression (N) is modified, E is changed accordingly.
2216
2217       Prev_Entity : Entity_Id := Empty;
2218
2219       function Count_Tasks (T : Entity_Id) return Uint;
2220       --  This function is called when a non-generic library level object of a
2221       --  task type is declared. Its function is to count the static number of
2222       --  tasks declared within the type (it is only called if Has_Tasks is set
2223       --  for T). As a side effect, if an array of tasks with non-static bounds
2224       --  or a variant record type is encountered, Check_Restrictions is called
2225       --  indicating the count is unknown.
2226
2227       -----------------
2228       -- Count_Tasks --
2229       -----------------
2230
2231       function Count_Tasks (T : Entity_Id) return Uint is
2232          C : Entity_Id;
2233          X : Node_Id;
2234          V : Uint;
2235
2236       begin
2237          if Is_Task_Type (T) then
2238             return Uint_1;
2239
2240          elsif Is_Record_Type (T) then
2241             if Has_Discriminants (T) then
2242                Check_Restriction (Max_Tasks, N);
2243                return Uint_0;
2244
2245             else
2246                V := Uint_0;
2247                C := First_Component (T);
2248                while Present (C) loop
2249                   V := V + Count_Tasks (Etype (C));
2250                   Next_Component (C);
2251                end loop;
2252
2253                return V;
2254             end if;
2255
2256          elsif Is_Array_Type (T) then
2257             X := First_Index (T);
2258             V := Count_Tasks (Component_Type (T));
2259             while Present (X) loop
2260                C := Etype (X);
2261
2262                if not Is_Static_Subtype (C) then
2263                   Check_Restriction (Max_Tasks, N);
2264                   return Uint_0;
2265                else
2266                   V := V * (UI_Max (Uint_0,
2267                                     Expr_Value (Type_High_Bound (C)) -
2268                                     Expr_Value (Type_Low_Bound (C)) + Uint_1));
2269                end if;
2270
2271                Next_Index (X);
2272             end loop;
2273
2274             return V;
2275
2276          else
2277             return Uint_0;
2278          end if;
2279       end Count_Tasks;
2280
2281    --  Start of processing for Analyze_Object_Declaration
2282
2283    begin
2284       --  There are three kinds of implicit types generated by an
2285       --  object declaration:
2286
2287       --   1. Those for generated by the original Object Definition
2288
2289       --   2. Those generated by the Expression
2290
2291       --   3. Those used to constrained the Object Definition with the
2292       --       expression constraints when it is unconstrained
2293
2294       --  They must be generated in this order to avoid order of elaboration
2295       --  issues. Thus the first step (after entering the name) is to analyze
2296       --  the object definition.
2297
2298       if Constant_Present (N) then
2299          Prev_Entity := Current_Entity_In_Scope (Id);
2300
2301          --  If the homograph is an implicit subprogram, it is overridden by
2302          --  the current declaration.
2303
2304          if Present (Prev_Entity)
2305            and then
2306              ((Is_Overloadable (Prev_Entity)
2307                  and then Is_Inherited_Operation (Prev_Entity))
2308
2309                --  The current object is a discriminal generated for an entry
2310                --  family index. Even though the index is a constant, in this
2311                --  particular context there is no true constant redeclaration.
2312                --  Enter_Name will handle the visibility.
2313
2314                or else
2315                 (Is_Discriminal (Id)
2316                    and then Ekind (Discriminal_Link (Id)) =
2317                               E_Entry_Index_Parameter))
2318          then
2319             Prev_Entity := Empty;
2320          end if;
2321       end if;
2322
2323       if Present (Prev_Entity) then
2324          Constant_Redeclaration (Id, N, T);
2325
2326          Generate_Reference (Prev_Entity, Id, 'c');
2327          Set_Completion_Referenced (Id);
2328
2329          if Error_Posted (N) then
2330
2331             --  Type mismatch or illegal redeclaration, Do not analyze
2332             --  expression to avoid cascaded errors.
2333
2334             T := Find_Type_Of_Object (Object_Definition (N), N);
2335             Set_Etype (Id, T);
2336             Set_Ekind (Id, E_Variable);
2337             return;
2338          end if;
2339
2340       --  In the normal case, enter identifier at the start to catch premature
2341       --  usage in the initialization expression.
2342
2343       else
2344          Generate_Definition (Id);
2345          Enter_Name (Id);
2346
2347          Mark_Coextensions (N, Object_Definition (N));
2348
2349          T := Find_Type_Of_Object (Object_Definition (N), N);
2350
2351          if Nkind (Object_Definition (N)) = N_Access_Definition
2352            and then Present
2353              (Access_To_Subprogram_Definition (Object_Definition (N)))
2354            and then Protected_Present
2355              (Access_To_Subprogram_Definition (Object_Definition (N)))
2356          then
2357             T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2358          end if;
2359
2360          if Error_Posted (Id) then
2361             Set_Etype (Id, T);
2362             Set_Ekind (Id, E_Variable);
2363             return;
2364          end if;
2365       end if;
2366
2367       --  Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2368       --  out some static checks
2369
2370       if Ada_Version >= Ada_05
2371         and then Can_Never_Be_Null (T)
2372       then
2373          --  In case of aggregates we must also take care of the correct
2374          --  initialization of nested aggregates bug this is done at the
2375          --  point of the analysis of the aggregate (see sem_aggr.adb)
2376
2377          if Present (Expression (N))
2378            and then Nkind (Expression (N)) = N_Aggregate
2379          then
2380             null;
2381
2382          else
2383             declare
2384                Save_Typ : constant Entity_Id := Etype (Id);
2385             begin
2386                Set_Etype (Id, T); --  Temp. decoration for static checks
2387                Null_Exclusion_Static_Checks (N);
2388                Set_Etype (Id, Save_Typ);
2389             end;
2390          end if;
2391       end if;
2392
2393       Set_Is_Pure (Id, Is_Pure (Current_Scope));
2394
2395       --  If deferred constant, make sure context is appropriate. We detect
2396       --  a deferred constant as a constant declaration with no expression.
2397       --  A deferred constant can appear in a package body if its completion
2398       --  is by means of an interface pragma.
2399
2400       if Constant_Present (N)
2401         and then No (E)
2402       then
2403          --  We exclude forward references to tags
2404
2405          if Is_Imported (Defining_Identifier (N))
2406            and then
2407             (T = RTE (RE_Tag)
2408               or else (Present (Full_View (T))
2409                         and then Full_View (T) = RTE (RE_Tag)))
2410          then
2411             null;
2412
2413          elsif not Is_Package_Or_Generic_Package (Current_Scope) then
2414             Error_Msg_N
2415               ("invalid context for deferred constant declaration (RM 7.4)",
2416                 N);
2417             Error_Msg_N
2418               ("\declaration requires an initialization expression",
2419                 N);
2420             Set_Constant_Present (N, False);
2421
2422          --  In Ada 83, deferred constant must be of private type
2423
2424          elsif not Is_Private_Type (T) then
2425             if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2426                Error_Msg_N
2427                  ("(Ada 83) deferred constant must be private type", N);
2428             end if;
2429          end if;
2430
2431       --  If not a deferred constant, then object declaration freezes its type
2432
2433       else
2434          Check_Fully_Declared (T, N);
2435          Freeze_Before (N, T);
2436       end if;
2437
2438       --  If the object was created by a constrained array definition, then
2439       --  set the link in both the anonymous base type and anonymous subtype
2440       --  that are built to represent the array type to point to the object.
2441
2442       if Nkind (Object_Definition (Declaration_Node (Id))) =
2443                         N_Constrained_Array_Definition
2444       then
2445          Set_Related_Array_Object (T, Id);
2446          Set_Related_Array_Object (Base_Type (T), Id);
2447       end if;
2448
2449       --  Special checks for protected objects not at library level
2450
2451       if Is_Protected_Type (T)
2452         and then not Is_Library_Level_Entity (Id)
2453       then
2454          Check_Restriction (No_Local_Protected_Objects, Id);
2455
2456          --  Protected objects with interrupt handlers must be at library level
2457
2458          --  Ada 2005: this test is not needed (and the corresponding clause
2459          --  in the RM is removed) because accessibility checks are sufficient
2460          --  to make handlers not at the library level illegal.
2461
2462          if Has_Interrupt_Handler (T)
2463            and then Ada_Version < Ada_05
2464          then
2465             Error_Msg_N
2466               ("interrupt object can only be declared at library level", Id);
2467          end if;
2468       end if;
2469
2470       --  The actual subtype of the object is the nominal subtype, unless
2471       --  the nominal one is unconstrained and obtained from the expression.
2472
2473       Act_T := T;
2474
2475       --  Process initialization expression if present and not in error
2476
2477       if Present (E) and then E /= Error then
2478
2479          --  Generate an error in case of CPP class-wide object initialization.
2480          --  Required because otherwise the expansion of the class-wide
2481          --  assignment would try to use 'size to initialize the object
2482          --  (primitive that is not available in CPP tagged types).
2483
2484          if Is_Class_Wide_Type (Act_T)
2485            and then Convention (Act_T) = Convention_CPP
2486          then
2487             Error_Msg_N
2488               ("predefined assignment not available in CPP tagged types", E);
2489          end if;
2490
2491          Mark_Coextensions (N, E);
2492          Analyze (E);
2493
2494          --  In case of errors detected in the analysis of the expression,
2495          --  decorate it with the expected type to avoid cascaded errors
2496
2497          if No (Etype (E)) then
2498             Set_Etype (E, T);
2499          end if;
2500
2501          --  If an initialization expression is present, then we set the
2502          --  Is_True_Constant flag. It will be reset if this is a variable
2503          --  and it is indeed modified.
2504
2505          Set_Is_True_Constant (Id, True);
2506
2507          --  If we are analyzing a constant declaration, set its completion
2508          --  flag after analyzing and resolving the expression.
2509
2510          if Constant_Present (N) then
2511             Set_Has_Completion (Id);
2512          end if;
2513
2514          --  Set type and resolve (type may be overridden later on)
2515
2516          Set_Etype (Id, T);
2517          Resolve (E, T);
2518
2519          --  If the object is an access to variable, the initialization
2520          --  expression cannot be an access to constant.
2521
2522          if Is_Access_Type (T)
2523            and then not Is_Access_Constant (T)
2524            and then Is_Access_Type (Etype (E))
2525            and then Is_Access_Constant (Etype (E))
2526          then
2527             Error_Msg_N
2528               ("object that is an access to variable cannot be initialized " &
2529                 "with an access-to-constant expression", E);
2530          end if;
2531
2532          if not Assignment_OK (N) then
2533             Check_Initialization (T, E);
2534          end if;
2535
2536          Check_Unset_Reference (E);
2537
2538          --  If this is a variable, then set current value
2539
2540          if not Constant_Present (N) then
2541             if Compile_Time_Known_Value (E) then
2542                Set_Current_Value (Id, E);
2543             end if;
2544          end if;
2545
2546          --  Deal with setting of null flags
2547
2548          if Is_Access_Type (T) then
2549             if Known_Non_Null (E) then
2550                Set_Is_Known_Non_Null (Id, True);
2551             elsif Known_Null (E)
2552               and then not Can_Never_Be_Null (Id)
2553             then
2554                Set_Is_Known_Null (Id, True);
2555             end if;
2556          end if;
2557
2558          --  Check incorrect use of dynamically tagged expressions. Note
2559          --  the use of Is_Tagged_Type (T) which seems redundant but is in
2560          --  fact important to avoid spurious errors due to expanded code
2561          --  for dispatching functions over an anonymous access type
2562
2563          if (Is_Class_Wide_Type (Etype (E)) or else Is_Dynamically_Tagged (E))
2564            and then Is_Tagged_Type (T)
2565            and then not Is_Class_Wide_Type (T)
2566          then
2567             Error_Msg_N ("dynamically tagged expression not allowed!", E);
2568          end if;
2569
2570          Apply_Scalar_Range_Check (E, T);
2571          Apply_Static_Length_Check (E, T);
2572       end if;
2573
2574       --  If the No_Streams restriction is set, check that the type of the
2575       --  object is not, and does not contain, any subtype derived from
2576       --  Ada.Streams.Root_Stream_Type. Note that we guard the call to
2577       --  Has_Stream just for efficiency reasons. There is no point in
2578       --  spending time on a Has_Stream check if the restriction is not set.
2579
2580       if Restrictions.Set (No_Streams) then
2581          if Has_Stream (T) then
2582             Check_Restriction (No_Streams, N);
2583          end if;
2584       end if;
2585
2586       --  Abstract type is never permitted for a variable or constant.
2587       --  Note: we inhibit this check for objects that do not come from
2588       --  source because there is at least one case (the expansion of
2589       --  x'class'input where x is abstract) where we legitimately
2590       --  generate an abstract object.
2591
2592       if Is_Abstract_Type (T) and then Comes_From_Source (N) then
2593          Error_Msg_N ("type of object cannot be abstract",
2594                       Object_Definition (N));
2595
2596          if Is_CPP_Class (T) then
2597             Error_Msg_NE ("\} may need a cpp_constructor",
2598               Object_Definition (N), T);
2599          end if;
2600
2601       --  Case of unconstrained type
2602
2603       elsif Is_Indefinite_Subtype (T) then
2604
2605          --  Nothing to do in deferred constant case
2606
2607          if Constant_Present (N) and then No (E) then
2608             null;
2609
2610          --  Case of no initialization present
2611
2612          elsif No (E) then
2613             if No_Initialization (N) then
2614                null;
2615
2616             elsif Is_Class_Wide_Type (T) then
2617                Error_Msg_N
2618                  ("initialization required in class-wide declaration ", N);
2619
2620             else
2621                Error_Msg_N
2622                  ("unconstrained subtype not allowed (need initialization)",
2623                   Object_Definition (N));
2624
2625                if Is_Record_Type (T) and then Has_Discriminants (T) then
2626                   Error_Msg_N
2627                     ("\provide initial value or explicit discriminant values",
2628                      Object_Definition (N));
2629
2630                   Error_Msg_NE
2631                     ("\or give default discriminant values for type&",
2632                      Object_Definition (N), T);
2633
2634                elsif Is_Array_Type (T) then
2635                   Error_Msg_N
2636                     ("\provide initial value or explicit array bounds",
2637                      Object_Definition (N));
2638                end if;
2639             end if;
2640
2641          --  Case of initialization present but in error. Set initial
2642          --  expression as absent (but do not make above complaints)
2643
2644          elsif E = Error then
2645             Set_Expression (N, Empty);
2646             E := Empty;
2647
2648          --  Case of initialization present
2649
2650          else
2651             --  Not allowed in Ada 83
2652
2653             if not Constant_Present (N) then
2654                if Ada_Version = Ada_83
2655                  and then Comes_From_Source (Object_Definition (N))
2656                then
2657                   Error_Msg_N
2658                     ("(Ada 83) unconstrained variable not allowed",
2659                      Object_Definition (N));
2660                end if;
2661             end if;
2662
2663             --  Now we constrain the variable from the initializing expression
2664
2665             --  If the expression is an aggregate, it has been expanded into
2666             --  individual assignments. Retrieve the actual type from the
2667             --  expanded construct.
2668
2669             if Is_Array_Type (T)
2670               and then No_Initialization (N)
2671               and then Nkind (Original_Node (E)) = N_Aggregate
2672             then
2673                Act_T := Etype (E);
2674
2675             else
2676                Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
2677                Act_T := Find_Type_Of_Object (Object_Definition (N), N);
2678             end if;
2679
2680             Set_Is_Constr_Subt_For_U_Nominal (Act_T);
2681
2682             if Aliased_Present (N) then
2683                Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2684             end if;
2685
2686             Freeze_Before (N, Act_T);
2687             Freeze_Before (N, T);
2688          end if;
2689
2690       elsif Is_Array_Type (T)
2691         and then No_Initialization (N)
2692         and then Nkind (Original_Node (E)) = N_Aggregate
2693       then
2694          if not Is_Entity_Name (Object_Definition (N)) then
2695             Act_T := Etype (E);
2696             Check_Compile_Time_Size (Act_T);
2697
2698             if Aliased_Present (N) then
2699                Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2700             end if;
2701          end if;
2702
2703          --  When the given object definition and the aggregate are specified
2704          --  independently, and their lengths might differ do a length check.
2705          --  This cannot happen if the aggregate is of the form (others =>...)
2706
2707          if not Is_Constrained (T) then
2708             null;
2709
2710          elsif Nkind (E) = N_Raise_Constraint_Error then
2711
2712             --  Aggregate is statically illegal. Place back in declaration
2713
2714             Set_Expression (N, E);
2715             Set_No_Initialization (N, False);
2716
2717          elsif T = Etype (E) then
2718             null;
2719
2720          elsif Nkind (E) = N_Aggregate
2721            and then Present (Component_Associations (E))
2722            and then Present (Choices (First (Component_Associations (E))))
2723            and then Nkind (First
2724             (Choices (First (Component_Associations (E))))) = N_Others_Choice
2725          then
2726             null;
2727
2728          else
2729             Apply_Length_Check (E, T);
2730          end if;
2731
2732       --  If the type is limited unconstrained with defaulted discriminants
2733       --  and there is no expression, then the object is constrained by the
2734       --  defaults, so it is worthwhile building the corresponding subtype.
2735
2736       elsif (Is_Limited_Record (T)
2737                or else Is_Concurrent_Type (T))
2738         and then not Is_Constrained (T)
2739         and then Has_Discriminants (T)
2740       then
2741          if No (E) then
2742             Act_T := Build_Default_Subtype (T, N);
2743          else
2744             --  Ada 2005:  a limited object may be initialized by means of an
2745             --  aggregate. If the type has default discriminants it has an
2746             --  unconstrained nominal type, Its actual subtype will be obtained
2747             --  from the aggregate, and not from the default discriminants.
2748
2749             Act_T := Etype (E);
2750          end if;
2751
2752          Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
2753
2754       elsif Present (Underlying_Type (T))
2755         and then not Is_Constrained (Underlying_Type (T))
2756         and then Has_Discriminants (Underlying_Type (T))
2757         and then Nkind (E) = N_Function_Call
2758         and then Constant_Present (N)
2759       then
2760          --  The back-end has problems with constants of a discriminated type
2761          --  with defaults, if the initial value is a function call. We
2762          --  generate an intermediate temporary for the result of the call.
2763          --  It is unclear why this should make it acceptable to gcc. ???
2764
2765          Remove_Side_Effects (E);
2766       end if;
2767
2768       --  Check No_Wide_Characters restriction
2769
2770       if T = Standard_Wide_Character
2771         or else T = Standard_Wide_Wide_Character
2772         or else Root_Type (T) = Standard_Wide_String
2773         or else Root_Type (T) = Standard_Wide_Wide_String
2774       then
2775          Check_Restriction (No_Wide_Characters, Object_Definition (N));
2776       end if;
2777
2778       --  Indicate this is not set in source. Certainly true for constants,
2779       --  and true for variables so far (will be reset for a variable if and
2780       --  when we encounter a modification in the source).
2781
2782       Set_Never_Set_In_Source (Id, True);
2783
2784       --  Now establish the proper kind and type of the object
2785
2786       if Constant_Present (N) then
2787          Set_Ekind            (Id, E_Constant);
2788          Set_Is_True_Constant (Id, True);
2789
2790       else
2791          Set_Ekind (Id, E_Variable);
2792
2793          --  A variable is set as shared passive if it appears in a shared
2794          --  passive package, and is at the outer level. This is not done
2795          --  for entities generated during expansion, because those are
2796          --  always manipulated locally.
2797
2798          if Is_Shared_Passive (Current_Scope)
2799            and then Is_Library_Level_Entity (Id)
2800            and then Comes_From_Source (Id)
2801          then
2802             Set_Is_Shared_Passive (Id);
2803             Check_Shared_Var (Id, T, N);
2804          end if;
2805
2806          --  Set Has_Initial_Value if initializing expression present. Note
2807          --  that if there is no initializing expression, we leave the state
2808          --  of this flag unchanged (usually it will be False, but notably in
2809          --  the case of exception choice variables, it will already be true).
2810
2811          if Present (E) then
2812             Set_Has_Initial_Value (Id, True);
2813          end if;
2814       end if;
2815
2816       --  Initialize alignment and size and capture alignment setting
2817
2818       Init_Alignment               (Id);
2819       Init_Esize                   (Id);
2820       Set_Optimize_Alignment_Flags (Id);
2821
2822       --  Deal with aliased case
2823
2824       if Aliased_Present (N) then
2825          Set_Is_Aliased (Id);
2826
2827          --  If the object is aliased and the type is unconstrained with
2828          --  defaulted discriminants and there is no expression, then the
2829          --  object is constrained by the defaults, so it is worthwhile
2830          --  building the corresponding subtype.
2831
2832          --  Ada 2005 (AI-363): If the aliased object is discriminated and
2833          --  unconstrained, then only establish an actual subtype if the
2834          --  nominal subtype is indefinite. In definite cases the object is
2835          --  unconstrained in Ada 2005.
2836
2837          if No (E)
2838            and then Is_Record_Type (T)
2839            and then not Is_Constrained (T)
2840            and then Has_Discriminants (T)
2841            and then (Ada_Version < Ada_05 or else Is_Indefinite_Subtype (T))
2842          then
2843             Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
2844          end if;
2845       end if;
2846
2847       --  Now we can set the type of the object
2848
2849       Set_Etype (Id, Act_T);
2850
2851       --  Deal with controlled types
2852
2853       if Has_Controlled_Component (Etype (Id))
2854         or else Is_Controlled (Etype (Id))
2855       then
2856          if not Is_Library_Level_Entity (Id) then
2857             Check_Restriction (No_Nested_Finalization, N);
2858          else
2859             Validate_Controlled_Object (Id);
2860          end if;
2861
2862          --  Generate a warning when an initialization causes an obvious ABE
2863          --  violation. If the init expression is a simple aggregate there
2864          --  shouldn't be any initialize/adjust call generated. This will be
2865          --  true as soon as aggregates are built in place when possible.
2866
2867          --  ??? at the moment we do not generate warnings for temporaries
2868          --  created for those aggregates although Program_Error might be
2869          --  generated if compiled with -gnato.
2870
2871          if Is_Controlled (Etype (Id))
2872             and then Comes_From_Source (Id)
2873          then
2874             declare
2875                BT : constant Entity_Id := Base_Type (Etype (Id));
2876
2877                Implicit_Call : Entity_Id;
2878                pragma Warnings (Off, Implicit_Call);
2879                --  ??? what is this for (never referenced!)
2880
2881                function Is_Aggr (N : Node_Id) return Boolean;
2882                --  Check that N is an aggregate
2883
2884                -------------
2885                -- Is_Aggr --
2886                -------------
2887
2888                function Is_Aggr (N : Node_Id) return Boolean is
2889                begin
2890                   case Nkind (Original_Node (N)) is
2891                      when N_Aggregate | N_Extension_Aggregate =>
2892                         return True;
2893
2894                      when N_Qualified_Expression |
2895                           N_Type_Conversion      |
2896                           N_Unchecked_Type_Conversion =>
2897                         return Is_Aggr (Expression (Original_Node (N)));
2898
2899                      when others =>
2900                         return False;
2901                   end case;
2902                end Is_Aggr;
2903
2904             begin
2905                --  If no underlying type, we already are in an error situation.
2906                --  Do not try to add a warning since we do not have access to
2907                --  prim-op list.
2908
2909                if No (Underlying_Type (BT)) then
2910                   Implicit_Call := Empty;
2911
2912                --  A generic type does not have usable primitive operators.
2913                --  Initialization calls are built for instances.
2914
2915                elsif Is_Generic_Type (BT) then
2916                   Implicit_Call := Empty;
2917
2918                --  If the init expression is not an aggregate, an adjust call
2919                --  will be generated
2920
2921                elsif Present (E) and then not Is_Aggr (E) then
2922                   Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
2923
2924                --  If no init expression and we are not in the deferred
2925                --  constant case, an Initialize call will be generated
2926
2927                elsif No (E) and then not Constant_Present (N) then
2928                   Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
2929
2930                else
2931                   Implicit_Call := Empty;
2932                end if;
2933             end;
2934          end if;
2935       end if;
2936
2937       if Has_Task (Etype (Id)) then
2938          Check_Restriction (No_Tasking, N);
2939
2940          --  Deal with counting max tasks
2941
2942          --  Nothing to do if inside a generic
2943
2944          if Inside_A_Generic then
2945             null;
2946
2947          --  If library level entity, then count tasks
2948
2949          elsif Is_Library_Level_Entity (Id) then
2950             Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
2951
2952          --  If not library level entity, then indicate we don't know max
2953          --  tasks and also check task hierarchy restriction and blocking
2954          --  operation (since starting a task is definitely blocking!)
2955
2956          else
2957             Check_Restriction (Max_Tasks, N);
2958             Check_Restriction (No_Task_Hierarchy, N);
2959             Check_Potentially_Blocking_Operation (N);
2960          end if;
2961
2962          --  A rather specialized test. If we see two tasks being declared
2963          --  of the same type in the same object declaration, and the task
2964          --  has an entry with an address clause, we know that program error
2965          --  will be raised at run-time since we can't have two tasks with
2966          --  entries at the same address.
2967
2968          if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
2969             declare
2970                E : Entity_Id;
2971
2972             begin
2973                E := First_Entity (Etype (Id));
2974                while Present (E) loop
2975                   if Ekind (E) = E_Entry
2976                     and then Present (Get_Attribute_Definition_Clause
2977                                         (E, Attribute_Address))
2978                   then
2979                      Error_Msg_N
2980                        ("?more than one task with same entry address", N);
2981                      Error_Msg_N
2982                        ("\?Program_Error will be raised at run time", N);
2983                      Insert_Action (N,
2984                        Make_Raise_Program_Error (Loc,
2985                          Reason => PE_Duplicated_Entry_Address));
2986                      exit;
2987                   end if;
2988
2989                   Next_Entity (E);
2990                end loop;
2991             end;
2992          end if;
2993       end if;
2994
2995       --  Some simple constant-propagation: if the expression is a constant
2996       --  string initialized with a literal, share the literal. This avoids
2997       --  a run-time copy.
2998
2999       if Present (E)
3000         and then Is_Entity_Name (E)
3001         and then Ekind (Entity (E)) = E_Constant
3002         and then Base_Type (Etype (E)) = Standard_String
3003       then
3004          declare
3005             Val : constant Node_Id := Constant_Value (Entity (E));
3006          begin
3007             if Present (Val)
3008               and then Nkind (Val) = N_String_Literal
3009             then
3010                Rewrite (E, New_Copy (Val));
3011             end if;
3012          end;
3013       end if;
3014
3015       --  Another optimization: if the nominal subtype is unconstrained and
3016       --  the expression is a function call that returns an unconstrained
3017       --  type, rewrite the declaration as a renaming of the result of the
3018       --  call. The exceptions below are cases where the copy is expected,
3019       --  either by the back end (Aliased case) or by the semantics, as for
3020       --  initializing controlled types or copying tags for classwide types.
3021
3022       if Present (E)
3023         and then Nkind (E) = N_Explicit_Dereference
3024         and then Nkind (Original_Node (E)) = N_Function_Call
3025         and then not Is_Library_Level_Entity (Id)
3026         and then not Is_Constrained (Underlying_Type (T))
3027         and then not Is_Aliased (Id)
3028         and then not Is_Class_Wide_Type (T)
3029         and then not Is_Controlled (T)
3030         and then not Has_Controlled_Component (Base_Type (T))
3031         and then Expander_Active
3032       then
3033          Rewrite (N,
3034            Make_Object_Renaming_Declaration (Loc,
3035              Defining_Identifier => Id,
3036              Access_Definition   => Empty,
3037              Subtype_Mark        => New_Occurrence_Of
3038                                       (Base_Type (Etype (Id)), Loc),
3039              Name                => E));
3040
3041          Set_Renamed_Object (Id, E);
3042
3043          --  Force generation of debugging information for the constant and for
3044          --  the renamed function call.
3045
3046          Set_Debug_Info_Needed (Id);
3047          Set_Debug_Info_Needed (Entity (Prefix (E)));
3048       end if;
3049
3050       if Present (Prev_Entity)
3051         and then Is_Frozen (Prev_Entity)
3052         and then not Error_Posted (Id)
3053       then
3054          Error_Msg_N ("full constant declaration appears too late", N);
3055       end if;
3056
3057       Check_Eliminated (Id);
3058
3059       --  Deal with setting In_Private_Part flag if in private part
3060
3061       if Ekind (Scope (Id)) = E_Package
3062         and then In_Private_Part (Scope (Id))
3063       then
3064          Set_In_Private_Part (Id);
3065       end if;
3066    end Analyze_Object_Declaration;
3067
3068    ---------------------------
3069    -- Analyze_Others_Choice --
3070    ---------------------------
3071
3072    --  Nothing to do for the others choice node itself, the semantic analysis
3073    --  of the others choice will occur as part of the processing of the parent
3074
3075    procedure Analyze_Others_Choice (N : Node_Id) is
3076       pragma Warnings (Off, N);
3077    begin
3078       null;
3079    end Analyze_Others_Choice;
3080
3081    -------------------------------------------
3082    -- Analyze_Private_Extension_Declaration --
3083    -------------------------------------------
3084
3085    procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
3086       T           : constant Entity_Id := Defining_Identifier (N);
3087       Indic       : constant Node_Id   := Subtype_Indication (N);
3088       Parent_Type : Entity_Id;
3089       Parent_Base : Entity_Id;
3090
3091    begin
3092       --  Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
3093
3094       if Is_Non_Empty_List (Interface_List (N)) then
3095          declare
3096             Intf : Node_Id;
3097             T    : Entity_Id;
3098
3099          begin
3100             Intf := First (Interface_List (N));
3101             while Present (Intf) loop
3102                T := Find_Type_Of_Subtype_Indic (Intf);
3103
3104                if not Is_Interface (T) then
3105                   Error_Msg_NE ("(Ada 2005) & must be an interface", Intf, T);
3106                end if;
3107
3108                Next (Intf);
3109             end loop;
3110          end;
3111       end if;
3112
3113       Generate_Definition (T);
3114       Enter_Name (T);
3115
3116       Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
3117       Parent_Base := Base_Type (Parent_Type);
3118
3119       if Parent_Type = Any_Type
3120         or else Etype (Parent_Type) = Any_Type
3121       then
3122          Set_Ekind (T, Ekind (Parent_Type));
3123          Set_Etype (T, Any_Type);
3124          return;
3125
3126       elsif not Is_Tagged_Type (Parent_Type) then
3127          Error_Msg_N
3128            ("parent of type extension must be a tagged type ", Indic);
3129          return;
3130
3131       elsif Ekind (Parent_Type) = E_Void
3132         or else Ekind (Parent_Type) = E_Incomplete_Type
3133       then
3134          Error_Msg_N ("premature derivation of incomplete type", Indic);
3135          return;
3136
3137       elsif Is_Concurrent_Type (Parent_Type) then
3138          Error_Msg_N
3139            ("parent type of a private extension cannot be "
3140             & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
3141
3142          Set_Etype              (T, Any_Type);
3143          Set_Ekind              (T, E_Limited_Private_Type);
3144          Set_Private_Dependents (T, New_Elmt_List);
3145          Set_Error_Posted       (T);
3146          return;
3147       end if;
3148
3149       --  Perhaps the parent type should be changed to the class-wide type's
3150       --  specific type in this case to prevent cascading errors ???
3151
3152       if Is_Class_Wide_Type (Parent_Type) then
3153          Error_Msg_N
3154            ("parent of type extension must not be a class-wide type", Indic);
3155          return;
3156       end if;
3157
3158       if (not Is_Package_Or_Generic_Package (Current_Scope)
3159            and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
3160         or else In_Private_Part (Current_Scope)
3161
3162       then
3163          Error_Msg_N ("invalid context for private extension", N);
3164       end if;
3165
3166       --  Set common attributes
3167
3168       Set_Is_Pure          (T, Is_Pure (Current_Scope));
3169       Set_Scope            (T, Current_Scope);
3170       Set_Ekind            (T, E_Record_Type_With_Private);
3171       Init_Size_Align      (T);
3172
3173       Set_Etype            (T,            Parent_Base);
3174       Set_Has_Task         (T, Has_Task  (Parent_Base));
3175
3176       Set_Convention       (T, Convention     (Parent_Type));
3177       Set_First_Rep_Item   (T, First_Rep_Item (Parent_Type));
3178       Set_Is_First_Subtype (T);
3179       Make_Class_Wide_Type (T);
3180
3181       if Unknown_Discriminants_Present (N) then
3182          Set_Discriminant_Constraint (T, No_Elist);
3183       end if;
3184
3185       Build_Derived_Record_Type (N, Parent_Type, T);
3186
3187       --  Ada 2005 (AI-443): Synchronized private extension or a rewritten
3188       --  synchronized formal derived type.
3189
3190       if Ada_Version >= Ada_05
3191         and then Synchronized_Present (N)
3192       then
3193          Set_Is_Limited_Record (T);
3194
3195          --  Formal derived type case
3196
3197          if Is_Generic_Type (T) then
3198
3199             --  The parent must be a tagged limited type or a synchronized
3200             --  interface.
3201
3202             if (not Is_Tagged_Type (Parent_Type)
3203                   or else not Is_Limited_Type (Parent_Type))
3204               and then
3205                (not Is_Interface (Parent_Type)
3206                   or else not Is_Synchronized_Interface (Parent_Type))
3207             then
3208                Error_Msg_NE ("parent type of & must be tagged limited " &
3209                              "or synchronized", N, T);
3210             end if;
3211
3212             --  The progenitors (if any) must be limited or synchronized
3213             --  interfaces.
3214
3215             if Present (Abstract_Interfaces (T)) then
3216                declare
3217                   Iface      : Entity_Id;
3218                   Iface_Elmt : Elmt_Id;
3219
3220                begin
3221                   Iface_Elmt := First_Elmt (Abstract_Interfaces (T));
3222                   while Present (Iface_Elmt) loop
3223                      Iface := Node (Iface_Elmt);
3224
3225                      if not Is_Limited_Interface (Iface)
3226                        and then not Is_Synchronized_Interface (Iface)
3227                      then
3228                         Error_Msg_NE ("progenitor & must be limited " &
3229                                       "or synchronized", N, Iface);
3230                      end if;
3231
3232                      Next_Elmt (Iface_Elmt);
3233                   end loop;
3234                end;
3235             end if;
3236
3237          --  Regular derived extension, the parent must be a limited or
3238          --  synchronized interface.
3239
3240          else
3241             if not Is_Interface (Parent_Type)
3242               or else (not Is_Limited_Interface (Parent_Type)
3243                          and then
3244                        not Is_Synchronized_Interface (Parent_Type))
3245             then
3246                Error_Msg_NE
3247                  ("parent type of & must be limited interface", N, T);
3248             end if;
3249          end if;
3250
3251       elsif Limited_Present (N) then
3252          Set_Is_Limited_Record (T);
3253
3254          if not Is_Limited_Type (Parent_Type)
3255            and then
3256              (not Is_Interface (Parent_Type)
3257                or else not Is_Limited_Interface (Parent_Type))
3258          then
3259             Error_Msg_NE ("parent type& of limited extension must be limited",
3260               N, Parent_Type);
3261          end if;
3262       end if;
3263    end Analyze_Private_Extension_Declaration;
3264
3265    ---------------------------------
3266    -- Analyze_Subtype_Declaration --
3267    ---------------------------------
3268
3269    procedure Analyze_Subtype_Declaration
3270      (N    : Node_Id;
3271       Skip : Boolean := False)
3272    is
3273       Id       : constant Entity_Id := Defining_Identifier (N);
3274       T        : Entity_Id;
3275       R_Checks : Check_Result;
3276
3277    begin
3278       Generate_Definition (Id);
3279       Set_Is_Pure (Id, Is_Pure (Current_Scope));
3280       Init_Size_Align (Id);
3281
3282       --  The following guard condition on Enter_Name is to handle cases where
3283       --  the defining identifier has already been entered into the scope but
3284       --  the declaration as a whole needs to be analyzed.
3285
3286       --  This case in particular happens for derived enumeration types. The
3287       --  derived enumeration type is processed as an inserted enumeration type
3288       --  declaration followed by a rewritten subtype declaration. The defining
3289       --  identifier, however, is entered into the name scope very early in the
3290       --  processing of the original type declaration and therefore needs to be
3291       --  avoided here, when the created subtype declaration is analyzed. (See
3292       --  Build_Derived_Types)
3293
3294       --  This also happens when the full view of a private type is derived
3295       --  type with constraints. In this case the entity has been introduced
3296       --  in the private declaration.
3297
3298       if Skip
3299         or else (Present (Etype (Id))
3300                    and then (Is_Private_Type (Etype (Id))
3301                                or else Is_Task_Type (Etype (Id))
3302                                or else Is_Rewrite_Substitution (N)))
3303       then
3304          null;
3305
3306       else
3307          Enter_Name (Id);
3308       end if;
3309
3310       T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
3311
3312       --  Inherit common attributes
3313
3314       Set_Is_Generic_Type   (Id, Is_Generic_Type   (Base_Type (T)));
3315       Set_Is_Volatile       (Id, Is_Volatile       (T));
3316       Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
3317       Set_Is_Atomic         (Id, Is_Atomic         (T));
3318       Set_Is_Ada_2005_Only  (Id, Is_Ada_2005_Only  (T));
3319       Set_Convention        (Id, Convention        (T));
3320
3321       --  In the case where there is no constraint given in the subtype
3322       --  indication, Process_Subtype just returns the Subtype_Mark, so its
3323       --  semantic attributes must be established here.
3324
3325       if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
3326          Set_Etype (Id, Base_Type (T));
3327
3328          case Ekind (T) is
3329             when Array_Kind =>
3330                Set_Ekind                       (Id, E_Array_Subtype);
3331                Copy_Array_Subtype_Attributes   (Id, T);
3332
3333             when Decimal_Fixed_Point_Kind =>
3334                Set_Ekind                (Id, E_Decimal_Fixed_Point_Subtype);
3335                Set_Digits_Value         (Id, Digits_Value       (T));
3336                Set_Delta_Value          (Id, Delta_Value        (T));
3337                Set_Scale_Value          (Id, Scale_Value        (T));
3338                Set_Small_Value          (Id, Small_Value        (T));
3339                Set_Scalar_Range         (Id, Scalar_Range       (T));
3340                Set_Machine_Radix_10     (Id, Machine_Radix_10   (T));
3341                Set_Is_Constrained       (Id, Is_Constrained     (T));
3342                Set_RM_Size              (Id, RM_Size            (T));
3343
3344             when Enumeration_Kind =>
3345                Set_Ekind                (Id, E_Enumeration_Subtype);
3346                Set_First_Literal        (Id, First_Literal (Base_Type (T)));
3347                Set_Scalar_Range         (Id, Scalar_Range       (T));
3348                Set_Is_Character_Type    (Id, Is_Character_Type  (T));
3349                Set_Is_Constrained       (Id, Is_Constrained     (T));
3350                Set_RM_Size              (Id, RM_Size            (T));
3351
3352             when Ordinary_Fixed_Point_Kind =>
3353                Set_Ekind                (Id, E_Ordinary_Fixed_Point_Subtype);
3354                Set_Scalar_Range         (Id, Scalar_Range       (T));
3355                Set_Small_Value          (Id, Small_Value        (T));
3356                Set_Delta_Value          (Id, Delta_Value        (T));
3357                Set_Is_Constrained       (Id, Is_Constrained     (T));
3358                Set_RM_Size              (Id, RM_Size            (T));
3359
3360             when Float_Kind =>
3361                Set_Ekind                (Id, E_Floating_Point_Subtype);
3362                Set_Scalar_Range         (Id, Scalar_Range       (T));
3363                Set_Digits_Value         (Id, Digits_Value       (T));
3364                Set_Is_Constrained       (Id, Is_Constrained     (T));
3365
3366             when Signed_Integer_Kind =>
3367                Set_Ekind                (Id, E_Signed_Integer_Subtype);
3368                Set_Scalar_Range         (Id, Scalar_Range       (T));
3369                Set_Is_Constrained       (Id, Is_Constrained     (T));
3370                Set_RM_Size              (Id, RM_Size            (T));
3371
3372             when Modular_Integer_Kind =>
3373                Set_Ekind                (Id, E_Modular_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 Class_Wide_Kind =>
3379                Set_Ekind                (Id, E_Class_Wide_Subtype);
3380                Set_First_Entity         (Id, First_Entity       (T));
3381                Set_Last_Entity          (Id, Last_Entity        (T));
3382                Set_Class_Wide_Type      (Id, Class_Wide_Type    (T));
3383                Set_Cloned_Subtype       (Id, T);
3384                Set_Is_Tagged_Type       (Id, True);
3385                Set_Has_Unknown_Discriminants
3386                                         (Id, True);
3387
3388                if Ekind (T) = E_Class_Wide_Subtype then
3389                   Set_Equivalent_Type   (Id, Equivalent_Type    (T));
3390                end if;
3391
3392             when E_Record_Type | E_Record_Subtype =>
3393                Set_Ekind                (Id, E_Record_Subtype);
3394
3395                if Ekind (T) = E_Record_Subtype
3396                  and then Present (Cloned_Subtype (T))
3397                then
3398                   Set_Cloned_Subtype    (Id, Cloned_Subtype (T));
3399                else
3400                   Set_Cloned_Subtype    (Id, T);
3401                end if;
3402
3403                Set_First_Entity         (Id, First_Entity       (T));
3404                Set_Last_Entity          (Id, Last_Entity        (T));
3405                Set_Has_Discriminants    (Id, Has_Discriminants  (T));
3406                Set_Is_Constrained       (Id, Is_Constrained     (T));
3407                Set_Is_Limited_Record    (Id, Is_Limited_Record  (T));
3408                Set_Has_Unknown_Discriminants
3409                                         (Id, Has_Unknown_Discriminants (T));
3410
3411                if Has_Discriminants (T) then
3412                   Set_Discriminant_Constraint
3413                                         (Id, Discriminant_Constraint (T));
3414                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3415
3416                elsif Has_Unknown_Discriminants (Id) then
3417                   Set_Discriminant_Constraint (Id, No_Elist);
3418                end if;
3419
3420                if Is_Tagged_Type (T) then
3421                   Set_Is_Tagged_Type    (Id);
3422                   Set_Is_Abstract_Type  (Id, Is_Abstract_Type (T));
3423                   Set_Primitive_Operations
3424                                         (Id, Primitive_Operations (T));
3425                   Set_Class_Wide_Type   (Id, Class_Wide_Type (T));
3426
3427                   if Is_Interface (T) then
3428                      Set_Is_Interface (Id);
3429                      Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
3430                   end if;
3431                end if;
3432
3433             when Private_Kind =>
3434                Set_Ekind              (Id, Subtype_Kind (Ekind   (T)));
3435                Set_Has_Discriminants  (Id, Has_Discriminants     (T));
3436                Set_Is_Constrained     (Id, Is_Constrained        (T));
3437                Set_First_Entity       (Id, First_Entity          (T));
3438                Set_Last_Entity        (Id, Last_Entity           (T));
3439                Set_Private_Dependents (Id, New_Elmt_List);
3440                Set_Is_Limited_Record  (Id, Is_Limited_Record     (T));
3441                Set_Has_Unknown_Discriminants
3442                                       (Id, Has_Unknown_Discriminants (T));
3443                Set_Known_To_Have_Preelab_Init
3444                                       (Id, Known_To_Have_Preelab_Init (T));
3445
3446                if Is_Tagged_Type (T) then
3447                   Set_Is_Tagged_Type       (Id);
3448                   Set_Is_Abstract_Type     (Id, Is_Abstract_Type (T));
3449                   Set_Primitive_Operations (Id, Primitive_Operations (T));
3450                   Set_Class_Wide_Type      (Id, Class_Wide_Type (T));
3451                end if;
3452
3453                --  In general the attributes of the subtype of a private type
3454                --  are the attributes of the partial view of parent. However,
3455                --  the full view may be a discriminated type, and the subtype
3456                --  must share the discriminant constraint to generate correct
3457                --  calls to initialization procedures.
3458
3459                if Has_Discriminants (T) then
3460                   Set_Discriminant_Constraint
3461                                      (Id, Discriminant_Constraint (T));
3462                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3463
3464                elsif Present (Full_View (T))
3465                  and then Has_Discriminants (Full_View (T))
3466                then
3467                   Set_Discriminant_Constraint
3468                                (Id, Discriminant_Constraint (Full_View (T)));
3469                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3470
3471                   --  This would seem semantically correct, but apparently
3472                   --  confuses the back-end. To be explained and checked with
3473                   --  current version ???
3474
3475                   --  Set_Has_Discriminants (Id);
3476                end if;
3477
3478                Prepare_Private_Subtype_Completion (Id, N);
3479
3480             when Access_Kind =>
3481                Set_Ekind             (Id, E_Access_Subtype);
3482                Set_Is_Constrained    (Id, Is_Constrained        (T));
3483                Set_Is_Access_Constant
3484                                      (Id, Is_Access_Constant    (T));
3485                Set_Directly_Designated_Type
3486                                      (Id, Designated_Type       (T));
3487                Set_Can_Never_Be_Null (Id, Can_Never_Be_Null     (T));
3488
3489                --  A Pure library_item must not contain the declaration of a
3490                --  named access type, except within a subprogram, generic
3491                --  subprogram, task unit, or protected unit (RM 10.2.1(16)).
3492
3493                if Comes_From_Source (Id)
3494                  and then In_Pure_Unit
3495                  and then not In_Subprogram_Task_Protected_Unit
3496                then
3497                   Error_Msg_N
3498                     ("named access types not allowed in pure unit", N);
3499                end if;
3500
3501             when Concurrent_Kind =>
3502                Set_Ekind                (Id, Subtype_Kind (Ekind   (T)));
3503                Set_Corresponding_Record_Type (Id,
3504                                          Corresponding_Record_Type (T));
3505                Set_First_Entity         (Id, First_Entity          (T));
3506                Set_First_Private_Entity (Id, First_Private_Entity  (T));
3507                Set_Has_Discriminants    (Id, Has_Discriminants     (T));
3508                Set_Is_Constrained       (Id, Is_Constrained        (T));
3509                Set_Is_Tagged_Type       (Id, Is_Tagged_Type        (T));
3510                Set_Last_Entity          (Id, Last_Entity           (T));
3511
3512                if Has_Discriminants (T) then
3513                   Set_Discriminant_Constraint (Id,
3514                                            Discriminant_Constraint (T));
3515                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3516                end if;
3517
3518             when E_Incomplete_Type =>
3519                if Ada_Version >= Ada_05 then
3520                   Set_Ekind (Id, E_Incomplete_Subtype);
3521
3522                   --  Ada 2005 (AI-412): Decorate an incomplete subtype
3523                   --  of an incomplete type visible through a limited
3524                   --  with clause.
3525
3526                   if From_With_Type (T)
3527                     and then Present (Non_Limited_View (T))
3528                   then
3529                      Set_From_With_Type   (Id);
3530                      Set_Non_Limited_View (Id, Non_Limited_View (T));
3531
3532                   --  Ada 2005 (AI-412): Add the regular incomplete subtype
3533                   --  to the private dependents of the original incomplete
3534                   --  type for future transformation.
3535
3536                   else
3537                      Append_Elmt (Id, Private_Dependents (T));
3538                   end if;
3539
3540                --  If the subtype name denotes an incomplete type an error
3541                --  was already reported by Process_Subtype.
3542
3543                else
3544                   Set_Etype (Id, Any_Type);
3545                end if;
3546
3547             when others =>
3548                raise Program_Error;
3549          end case;
3550       end if;
3551
3552       if Etype (Id) = Any_Type then
3553          return;
3554       end if;
3555
3556       --  Some common processing on all types
3557
3558       Set_Size_Info      (Id,                 T);
3559       Set_First_Rep_Item (Id, First_Rep_Item (T));
3560
3561       T := Etype (Id);
3562
3563       Set_Is_Immediately_Visible   (Id, True);
3564       Set_Depends_On_Private       (Id, Has_Private_Component (T));
3565       Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
3566
3567       if Is_Interface (T) then
3568          Set_Is_Interface (Id);
3569       end if;
3570
3571       if Present (Generic_Parent_Type (N))
3572         and then
3573           (Nkind
3574              (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
3575             or else Nkind
3576               (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
3577                 /=  N_Formal_Private_Type_Definition)
3578       then
3579          if Is_Tagged_Type (Id) then
3580
3581             --  If this is a generic actual subtype for a synchronized type,
3582             --  the primitive operations are those of the corresponding record
3583             --  for which there is a separate subtype declaration.
3584
3585             if Is_Concurrent_Type (Id) then
3586                null;
3587             elsif Is_Class_Wide_Type (Id) then
3588                Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
3589             else
3590                Derive_Subprograms (Generic_Parent_Type (N), Id, T);
3591             end if;
3592
3593          elsif Scope (Etype (Id)) /= Standard_Standard then
3594             Derive_Subprograms (Generic_Parent_Type (N), Id);
3595          end if;
3596       end if;
3597
3598       if Is_Private_Type (T)
3599         and then Present (Full_View (T))
3600       then
3601          Conditional_Delay (Id, Full_View (T));
3602
3603       --  The subtypes of components or subcomponents of protected types
3604       --  do not need freeze nodes, which would otherwise appear in the
3605       --  wrong scope (before the freeze node for the protected type). The
3606       --  proper subtypes are those of the subcomponents of the corresponding
3607       --  record.
3608
3609       elsif Ekind (Scope (Id)) /= E_Protected_Type
3610         and then Present (Scope (Scope (Id))) -- error defense!
3611         and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
3612       then
3613          Conditional_Delay (Id, T);
3614       end if;
3615
3616       --  Check that constraint_error is raised for a scalar subtype
3617       --  indication when the lower or upper bound of a non-null range
3618       --  lies outside the range of the type mark.
3619
3620       if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
3621          if Is_Scalar_Type (Etype (Id))
3622             and then Scalar_Range (Id) /=
3623                      Scalar_Range (Etype (Subtype_Mark
3624                                            (Subtype_Indication (N))))
3625          then
3626             Apply_Range_Check
3627               (Scalar_Range (Id),
3628                Etype (Subtype_Mark (Subtype_Indication (N))));
3629
3630          elsif Is_Array_Type (Etype (Id))
3631            and then Present (First_Index (Id))
3632          then
3633             --  This really should be a subprogram that finds the indications
3634             --  to check???
3635
3636             if ((Nkind (First_Index (Id)) = N_Identifier
3637                    and then Ekind (Entity (First_Index (Id))) in Scalar_Kind)
3638                  or else Nkind (First_Index (Id)) = N_Subtype_Indication)
3639               and then
3640                 Nkind (Scalar_Range (Etype (First_Index (Id)))) = N_Range
3641             then
3642                declare
3643                   Target_Typ : constant Entity_Id :=
3644                                  Etype
3645                                    (First_Index (Etype
3646                                      (Subtype_Mark (Subtype_Indication (N)))));
3647                begin
3648                   R_Checks :=
3649                     Get_Range_Checks
3650                       (Scalar_Range (Etype (First_Index (Id))),
3651                        Target_Typ,
3652                        Etype (First_Index (Id)),
3653                        Defining_Identifier (N));
3654
3655                   Insert_Range_Checks
3656                     (R_Checks,
3657                      N,
3658                      Target_Typ,
3659                      Sloc (Defining_Identifier (N)));
3660                end;
3661             end if;
3662          end if;
3663       end if;
3664
3665       Set_Optimize_Alignment_Flags (Id);
3666       Check_Eliminated (Id);
3667    end Analyze_Subtype_Declaration;
3668
3669    --------------------------------
3670    -- Analyze_Subtype_Indication --
3671    --------------------------------
3672
3673    procedure Analyze_Subtype_Indication (N : Node_Id) is
3674       T : constant Entity_Id := Subtype_Mark (N);
3675       R : constant Node_Id   := Range_Expression (Constraint (N));
3676
3677    begin
3678       Analyze (T);
3679
3680       if R /= Error then
3681          Analyze (R);
3682          Set_Etype (N, Etype (R));
3683          Resolve (R, Entity (T));
3684       else
3685          Set_Error_Posted (R);
3686          Set_Error_Posted (T);
3687       end if;
3688    end Analyze_Subtype_Indication;
3689
3690    ------------------------------
3691    -- Analyze_Type_Declaration --
3692    ------------------------------
3693
3694    procedure Analyze_Type_Declaration (N : Node_Id) is
3695       Def    : constant Node_Id   := Type_Definition (N);
3696       Def_Id : constant Entity_Id := Defining_Identifier (N);
3697       T      : Entity_Id;
3698       Prev   : Entity_Id;
3699
3700       Is_Remote : constant Boolean :=
3701                     (Is_Remote_Types (Current_Scope)
3702                        or else Is_Remote_Call_Interface (Current_Scope))
3703                     and then not (In_Private_Part (Current_Scope)
3704                                     or else In_Package_Body (Current_Scope));
3705
3706       procedure Check_Ops_From_Incomplete_Type;
3707       --  If there is a tagged incomplete partial view of the type, transfer
3708       --  its operations to the full view, and indicate that the type of the
3709       --  controlling parameter (s) is this full view.
3710
3711       ------------------------------------
3712       -- Check_Ops_From_Incomplete_Type --
3713       ------------------------------------
3714
3715       procedure Check_Ops_From_Incomplete_Type is
3716          Elmt   : Elmt_Id;
3717          Formal : Entity_Id;
3718          Op     : Entity_Id;
3719
3720       begin
3721          if Prev /= T
3722            and then Ekind (Prev) = E_Incomplete_Type
3723            and then Is_Tagged_Type (Prev)
3724            and then Is_Tagged_Type (T)
3725          then
3726             Elmt := First_Elmt (Primitive_Operations (Prev));
3727             while Present (Elmt) loop
3728                Op := Node (Elmt);
3729                Prepend_Elmt (Op, Primitive_Operations (T));
3730
3731                Formal := First_Formal (Op);
3732                while Present (Formal) loop
3733                   if Etype (Formal) = Prev then
3734                      Set_Etype (Formal, T);
3735                   end if;
3736
3737                   Next_Formal (Formal);
3738                end loop;
3739
3740                if Etype (Op) = Prev then
3741                   Set_Etype (Op, T);
3742                end if;
3743
3744                Next_Elmt (Elmt);
3745             end loop;
3746          end if;
3747       end Check_Ops_From_Incomplete_Type;
3748
3749    --  Start of processing for Analyze_Type_Declaration
3750
3751    begin
3752       Prev := Find_Type_Name (N);
3753
3754       --  The full view, if present, now points to the current type
3755
3756       --  Ada 2005 (AI-50217): If the type was previously decorated when
3757       --  imported through a LIMITED WITH clause, it appears as incomplete
3758       --  but has no full view.
3759       --  If the incomplete view is tagged, a class_wide type has been
3760       --  created already. Use it for the full view as well, to prevent
3761       --  multiple incompatible class-wide types that may be  created for
3762       --  self-referential anonymous access components.
3763
3764       if Ekind (Prev) = E_Incomplete_Type
3765         and then Present (Full_View (Prev))
3766       then
3767          T := Full_View (Prev);
3768
3769          if Is_Tagged_Type (Prev)
3770            and then Present (Class_Wide_Type (Prev))
3771          then
3772             Set_Ekind (T, Ekind (Prev));         --  will be reset later
3773             Set_Class_Wide_Type (T, Class_Wide_Type (Prev));
3774             Set_Etype (Class_Wide_Type (T), T);
3775          end if;
3776
3777       else
3778          T := Prev;
3779       end if;
3780
3781       Set_Is_Pure (T, Is_Pure (Current_Scope));
3782
3783       --  We set the flag Is_First_Subtype here. It is needed to set the
3784       --  corresponding flag for the Implicit class-wide-type created
3785       --  during tagged types processing.
3786
3787       Set_Is_First_Subtype (T, True);
3788
3789       --  Only composite types other than array types are allowed to have
3790       --  discriminants.
3791
3792       case Nkind (Def) is
3793
3794          --  For derived types, the rule will be checked once we've figured
3795          --  out the parent type.
3796
3797          when N_Derived_Type_Definition =>
3798             null;
3799
3800          --  For record types, discriminants are allowed
3801
3802          when N_Record_Definition =>
3803             null;
3804
3805          when others =>
3806             if Present (Discriminant_Specifications (N)) then
3807                Error_Msg_N
3808                  ("elementary or array type cannot have discriminants",
3809                   Defining_Identifier
3810                   (First (Discriminant_Specifications (N))));
3811             end if;
3812       end case;
3813
3814       --  Elaborate the type definition according to kind, and generate
3815       --  subsidiary (implicit) subtypes where needed. We skip this if it was
3816       --  already done (this happens during the reanalysis that follows a call
3817       --  to the high level optimizer).
3818
3819       if not Analyzed (T) then
3820          Set_Analyzed (T);
3821
3822          case Nkind (Def) is
3823
3824             when N_Access_To_Subprogram_Definition =>
3825                Access_Subprogram_Declaration (T, Def);
3826
3827                --  If this is a remote access to subprogram, we must create the
3828                --  equivalent fat pointer type, and related subprograms.
3829
3830                if Is_Remote then
3831                   Process_Remote_AST_Declaration (N);
3832                end if;
3833
3834                --  Validate categorization rule against access type declaration
3835                --  usually a violation in Pure unit, Shared_Passive unit.
3836
3837                Validate_Access_Type_Declaration (T, N);
3838
3839             when N_Access_To_Object_Definition =>
3840                Access_Type_Declaration (T, Def);
3841
3842                --  Validate categorization rule against access type declaration
3843                --  usually a violation in Pure unit, Shared_Passive unit.
3844
3845                Validate_Access_Type_Declaration (T, N);
3846
3847                --  If we are in a Remote_Call_Interface package and define
3848                --  a RACW, Read and Write attribute must be added.
3849
3850                if Is_Remote
3851                  and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3852                then
3853                   Add_RACW_Features (Def_Id);
3854                end if;
3855
3856                --  Set no strict aliasing flag if config pragma seen
3857
3858                if Opt.No_Strict_Aliasing then
3859                   Set_No_Strict_Aliasing (Base_Type (Def_Id));
3860                end if;
3861
3862             when N_Array_Type_Definition =>
3863                Array_Type_Declaration (T, Def);
3864
3865             when N_Derived_Type_Definition =>
3866                Derived_Type_Declaration (T, N, T /= Def_Id);
3867
3868             when N_Enumeration_Type_Definition =>
3869                Enumeration_Type_Declaration (T, Def);
3870
3871             when N_Floating_Point_Definition =>
3872                Floating_Point_Type_Declaration (T, Def);
3873
3874             when N_Decimal_Fixed_Point_Definition =>
3875                Decimal_Fixed_Point_Type_Declaration (T, Def);
3876
3877             when N_Ordinary_Fixed_Point_Definition =>
3878                Ordinary_Fixed_Point_Type_Declaration (T, Def);
3879
3880             when N_Signed_Integer_Type_Definition =>
3881                Signed_Integer_Type_Declaration (T, Def);
3882
3883             when N_Modular_Type_Definition =>
3884                Modular_Type_Declaration (T, Def);
3885
3886             when N_Record_Definition =>
3887                Record_Type_Declaration (T, N, Prev);
3888
3889             when others =>
3890                raise Program_Error;
3891
3892          end case;
3893       end if;
3894
3895       if Etype (T) = Any_Type then
3896          return;
3897       end if;
3898
3899       --  Some common processing for all types
3900
3901       Set_Depends_On_Private (T, Has_Private_Component (T));
3902       Check_Ops_From_Incomplete_Type;
3903
3904       --  Both the declared entity, and its anonymous base type if one
3905       --  was created, need freeze nodes allocated.
3906
3907       declare
3908          B : constant Entity_Id := Base_Type (T);
3909
3910       begin
3911          --  In the case where the base type is different from the first
3912          --  subtype, we pre-allocate a freeze node, and set the proper link
3913          --  to the first subtype. Freeze_Entity will use this preallocated
3914          --  freeze node when it freezes the entity.
3915
3916          if B /= T then
3917             Ensure_Freeze_Node (B);
3918             Set_First_Subtype_Link (Freeze_Node (B), T);
3919          end if;
3920
3921          if not From_With_Type (T) then
3922             Set_Has_Delayed_Freeze (T);
3923          end if;
3924       end;
3925
3926       --  Case of T is the full declaration of some private type which has
3927       --  been swapped in Defining_Identifier (N).
3928
3929       if T /= Def_Id and then Is_Private_Type (Def_Id) then
3930          Process_Full_View (N, T, Def_Id);
3931
3932          --  Record the reference. The form of this is a little strange,
3933          --  since the full declaration has been swapped in. So the first
3934          --  parameter here represents the entity to which a reference is
3935          --  made which is the "real" entity, i.e. the one swapped in,
3936          --  and the second parameter provides the reference location.
3937
3938          --  Also, we want to kill Has_Pragma_Unreferenced temporarily here
3939          --  since we don't want a complaint about the full type being an
3940          --  unwanted reference to the private type
3941
3942          declare
3943             B : constant Boolean := Has_Pragma_Unreferenced (T);
3944          begin
3945             Set_Has_Pragma_Unreferenced (T, False);
3946             Generate_Reference (T, T, 'c');
3947             Set_Has_Pragma_Unreferenced (T, B);
3948          end;
3949
3950          Set_Completion_Referenced (Def_Id);
3951
3952       --  For completion of incomplete type, process incomplete dependents
3953       --  and always mark the full type as referenced (it is the incomplete
3954       --  type that we get for any real reference).
3955
3956       elsif Ekind (Prev) = E_Incomplete_Type then
3957          Process_Incomplete_Dependents (N, T, Prev);
3958          Generate_Reference (Prev, Def_Id, 'c');
3959          Set_Completion_Referenced (Def_Id);
3960
3961       --  If not private type or incomplete type completion, this is a real
3962       --  definition of a new entity, so record it.
3963
3964       else
3965          Generate_Definition (Def_Id);
3966       end if;
3967
3968       if Chars (Scope (Def_Id)) =  Name_System
3969         and then Chars (Def_Id) = Name_Address
3970         and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
3971       then
3972          Set_Is_Descendent_Of_Address (Def_Id);
3973          Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
3974          Set_Is_Descendent_Of_Address (Prev);
3975       end if;
3976
3977       Set_Optimize_Alignment_Flags (Def_Id);
3978       Check_Eliminated (Def_Id);
3979    end Analyze_Type_Declaration;
3980
3981    --------------------------
3982    -- Analyze_Variant_Part --
3983    --------------------------
3984
3985    procedure Analyze_Variant_Part (N : Node_Id) is
3986
3987       procedure Non_Static_Choice_Error (Choice : Node_Id);
3988       --  Error routine invoked by the generic instantiation below when
3989       --  the variant part has a non static choice.
3990
3991       procedure Process_Declarations (Variant : Node_Id);
3992       --  Analyzes all the declarations associated with a Variant.
3993       --  Needed by the generic instantiation below.
3994
3995       package Variant_Choices_Processing is new
3996         Generic_Choices_Processing
3997           (Get_Alternatives          => Variants,
3998            Get_Choices               => Discrete_Choices,
3999            Process_Empty_Choice      => No_OP,
4000            Process_Non_Static_Choice => Non_Static_Choice_Error,
4001            Process_Associated_Node   => Process_Declarations);
4002       use Variant_Choices_Processing;
4003       --  Instantiation of the generic choice processing package
4004
4005       -----------------------------
4006       -- Non_Static_Choice_Error --
4007       -----------------------------
4008
4009       procedure Non_Static_Choice_Error (Choice : Node_Id) is
4010       begin
4011          Flag_Non_Static_Expr
4012            ("choice given in variant part is not static!", Choice);
4013       end Non_Static_Choice_Error;
4014
4015       --------------------------
4016       -- Process_Declarations --
4017       --------------------------
4018
4019       procedure Process_Declarations (Variant : Node_Id) is
4020       begin
4021          if not Null_Present (Component_List (Variant)) then
4022             Analyze_Declarations (Component_Items (Component_List (Variant)));
4023
4024             if Present (Variant_Part (Component_List (Variant))) then
4025                Analyze (Variant_Part (Component_List (Variant)));
4026             end if;
4027          end if;
4028       end Process_Declarations;
4029
4030       --  Local Variables
4031
4032       Discr_Name : Node_Id;
4033       Discr_Type : Entity_Id;
4034
4035       Case_Table     : Choice_Table_Type (1 .. Number_Of_Choices (N));
4036       Last_Choice    : Nat;
4037       Dont_Care      : Boolean;
4038       Others_Present : Boolean := False;
4039
4040       pragma Warnings (Off, Case_Table);
4041       pragma Warnings (Off, Last_Choice);
4042       pragma Warnings (Off, Dont_Care);
4043       pragma Warnings (Off, Others_Present);
4044       --  We don't care about the assigned values of any of these
4045
4046    --  Start of processing for Analyze_Variant_Part
4047
4048    begin
4049       Discr_Name := Name (N);
4050       Analyze (Discr_Name);
4051
4052       --  If Discr_Name bad, get out (prevent cascaded errors)
4053
4054       if Etype (Discr_Name) = Any_Type then
4055          return;
4056       end if;
4057
4058       --  Check invalid discriminant in variant part
4059
4060       if Ekind (Entity (Discr_Name)) /= E_Discriminant then
4061          Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
4062       end if;
4063
4064       Discr_Type := Etype (Entity (Discr_Name));
4065
4066       if not Is_Discrete_Type (Discr_Type) then
4067          Error_Msg_N
4068            ("discriminant in a variant part must be of a discrete type",
4069              Name (N));
4070          return;
4071       end if;
4072
4073       --  Call the instantiated Analyze_Choices which does the rest of the work
4074
4075       Analyze_Choices
4076         (N, Discr_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
4077    end Analyze_Variant_Part;
4078
4079    ----------------------------
4080    -- Array_Type_Declaration --
4081    ----------------------------
4082
4083    procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
4084       Component_Def : constant Node_Id := Component_Definition (Def);
4085       Element_Type  : Entity_Id;
4086       Implicit_Base : Entity_Id;
4087       Index         : Node_Id;
4088       Related_Id    : Entity_Id := Empty;
4089       Nb_Index      : Nat;
4090       P             : constant Node_Id := Parent (Def);
4091       Priv          : Entity_Id;
4092
4093    begin
4094       if Nkind (Def) = N_Constrained_Array_Definition then
4095          Index := First (Discrete_Subtype_Definitions (Def));
4096       else
4097          Index := First (Subtype_Marks (Def));
4098       end if;
4099
4100       --  Find proper names for the implicit types which may be public.
4101       --  in case of anonymous arrays we use the name of the first object
4102       --  of that type as prefix.
4103
4104       if No (T) then
4105          Related_Id :=  Defining_Identifier (P);
4106       else
4107          Related_Id := T;
4108       end if;
4109
4110       Nb_Index := 1;
4111       while Present (Index) loop
4112          Analyze (Index);
4113
4114          --  Add a subtype declaration for each index of private array type
4115          --  declaration whose etype is also private. For example:
4116
4117          --     package Pkg is
4118          --        type Index is private;
4119          --     private
4120          --        type Table is array (Index) of ...
4121          --     end;
4122
4123          --  This is currently required by the expander to generate the
4124          --  internally generated equality subprogram of records with variant
4125          --  parts in which the etype of some component is such private type.
4126
4127          if Ekind (Current_Scope) = E_Package
4128            and then In_Private_Part (Current_Scope)
4129            and then Has_Private_Declaration (Etype (Index))
4130          then
4131             declare
4132                Loc   : constant Source_Ptr := Sloc (Def);
4133                New_E : Entity_Id;
4134                Decl  : Entity_Id;
4135
4136             begin
4137                New_E :=
4138                  Make_Defining_Identifier (Loc,
4139                    Chars => New_Internal_Name ('T'));
4140                Set_Is_Internal (New_E);
4141
4142                Decl :=
4143                  Make_Subtype_Declaration (Loc,
4144                    Defining_Identifier => New_E,
4145                    Subtype_Indication  =>
4146                      New_Occurrence_Of (Etype (Index), Loc));
4147
4148                Insert_Before (Parent (Def), Decl);
4149                Analyze (Decl);
4150                Set_Etype (Index, New_E);
4151
4152                --  If the index is a range the Entity attribute is not
4153                --  available. Example:
4154
4155                --     package Pkg is
4156                --        type T is private;
4157                --     private
4158                --        type T is new Natural;
4159                --        Table : array (T(1) .. T(10)) of Boolean;
4160                --     end Pkg;
4161
4162                if Nkind (Index) /= N_Range then
4163                   Set_Entity (Index, New_E);
4164                end if;
4165             end;
4166          end if;
4167
4168          Make_Index (Index, P, Related_Id, Nb_Index);
4169          Next_Index (Index);
4170          Nb_Index := Nb_Index + 1;
4171       end loop;
4172
4173       --  Process subtype indication if one is present
4174
4175       if Present (Subtype_Indication (Component_Def)) then
4176          Element_Type :=
4177            Process_Subtype
4178              (Subtype_Indication (Component_Def), P, Related_Id, 'C');
4179
4180       --  Ada 2005 (AI-230): Access Definition case
4181
4182       else pragma Assert (Present (Access_Definition (Component_Def)));
4183
4184          --  Indicate that the anonymous access type is created by the
4185          --  array type declaration.
4186
4187          Element_Type := Access_Definition
4188                            (Related_Nod => P,
4189                             N           => Access_Definition (Component_Def));
4190          Set_Is_Local_Anonymous_Access (Element_Type);
4191
4192          --  Propagate the parent. This field is needed if we have to generate
4193          --  the master_id associated with an anonymous access to task type
4194          --  component (see Expand_N_Full_Type_Declaration.Build_Master)
4195
4196          Set_Parent (Element_Type, Parent (T));
4197
4198          --  Ada 2005 (AI-230): In case of components that are anonymous
4199          --  access types the level of accessibility depends on the enclosing
4200          --  type declaration
4201
4202          Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
4203
4204          --  Ada 2005 (AI-254)
4205
4206          declare
4207             CD : constant Node_Id :=
4208                    Access_To_Subprogram_Definition
4209                      (Access_Definition (Component_Def));
4210          begin
4211             if Present (CD) and then Protected_Present (CD) then
4212                Element_Type :=
4213                  Replace_Anonymous_Access_To_Protected_Subprogram (Def);
4214             end if;
4215          end;
4216       end if;
4217
4218       --  Constrained array case
4219
4220       if No (T) then
4221          T := Create_Itype (E_Void, P, Related_Id, 'T');
4222       end if;
4223
4224       if Nkind (Def) = N_Constrained_Array_Definition then
4225
4226          --  Establish Implicit_Base as unconstrained base type
4227
4228          Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
4229
4230          Set_Etype              (Implicit_Base, Implicit_Base);
4231          Set_Scope              (Implicit_Base, Current_Scope);
4232          Set_Has_Delayed_Freeze (Implicit_Base);
4233
4234          --  The constrained array type is a subtype of the unconstrained one
4235
4236          Set_Ekind          (T, E_Array_Subtype);
4237          Init_Size_Align    (T);
4238          Set_Etype          (T, Implicit_Base);
4239          Set_Scope          (T, Current_Scope);
4240          Set_Is_Constrained (T, True);
4241          Set_First_Index    (T, First (Discrete_Subtype_Definitions (Def)));
4242          Set_Has_Delayed_Freeze (T);
4243
4244          --  Complete setup of implicit base type
4245
4246          Set_First_Index       (Implicit_Base, First_Index (T));
4247          Set_Component_Type    (Implicit_Base, Element_Type);
4248          Set_Has_Task          (Implicit_Base, Has_Task (Element_Type));
4249          Set_Component_Size    (Implicit_Base, Uint_0);
4250          Set_Packed_Array_Type (Implicit_Base, Empty);
4251          Set_Has_Controlled_Component
4252                                (Implicit_Base, Has_Controlled_Component
4253                                                         (Element_Type)
4254                                                  or else Is_Controlled
4255                                                         (Element_Type));
4256          Set_Finalize_Storage_Only
4257                                (Implicit_Base, Finalize_Storage_Only
4258                                                         (Element_Type));
4259
4260       --  Unconstrained array case
4261
4262       else
4263          Set_Ekind                    (T, E_Array_Type);
4264          Init_Size_Align              (T);
4265          Set_Etype                    (T, T);
4266          Set_Scope                    (T, Current_Scope);
4267          Set_Component_Size           (T, Uint_0);
4268          Set_Is_Constrained           (T, False);
4269          Set_First_Index              (T, First (Subtype_Marks (Def)));
4270          Set_Has_Delayed_Freeze       (T, True);
4271          Set_Has_Task                 (T, Has_Task      (Element_Type));
4272          Set_Has_Controlled_Component (T, Has_Controlled_Component
4273                                                         (Element_Type)
4274                                             or else
4275                                           Is_Controlled (Element_Type));
4276          Set_Finalize_Storage_Only    (T, Finalize_Storage_Only
4277                                                         (Element_Type));
4278       end if;
4279
4280       --  Common attributes for both cases
4281
4282       Set_Component_Type (Base_Type (T), Element_Type);
4283       Set_Packed_Array_Type (T, Empty);
4284
4285       if Aliased_Present (Component_Definition (Def)) then
4286          Set_Has_Aliased_Components (Etype (T));
4287       end if;
4288
4289       --  Ada 2005 (AI-231): Propagate the null-excluding attribute to the
4290       --  array type to ensure that objects of this type are initialized.
4291
4292       if Ada_Version >= Ada_05
4293         and then Can_Never_Be_Null (Element_Type)
4294       then
4295          Set_Can_Never_Be_Null (T);
4296
4297          if Null_Exclusion_Present (Component_Definition (Def))
4298
4299             --  No need to check itypes because in their case this check
4300             --  was done at their point of creation
4301
4302            and then not Is_Itype (Element_Type)
4303          then
4304             Error_Msg_N
4305               ("`NOT NULL` not allowed (null already excluded)",
4306                Subtype_Indication (Component_Definition (Def)));
4307          end if;
4308       end if;
4309
4310       Priv := Private_Component (Element_Type);
4311
4312       if Present (Priv) then
4313
4314          --  Check for circular definitions
4315
4316          if Priv = Any_Type then
4317             Set_Component_Type (Etype (T), Any_Type);
4318
4319          --  There is a gap in the visibility of operations on the composite
4320          --  type only if the component type is defined in a different scope.
4321
4322          elsif Scope (Priv) = Current_Scope then
4323             null;
4324
4325          elsif Is_Limited_Type (Priv) then
4326             Set_Is_Limited_Composite (Etype (T));
4327             Set_Is_Limited_Composite (T);
4328          else
4329             Set_Is_Private_Composite (Etype (T));
4330             Set_Is_Private_Composite (T);
4331          end if;
4332       end if;
4333
4334       --  A syntax error in the declaration itself may lead to an empty
4335       --  index list, in which case do a minimal patch.
4336
4337       if No (First_Index (T)) then
4338          Error_Msg_N ("missing index definition in array type declaration", T);
4339
4340          declare
4341             Indices : constant List_Id :=
4342                         New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
4343          begin
4344             Set_Discrete_Subtype_Definitions (Def, Indices);
4345             Set_First_Index (T, First (Indices));
4346             return;
4347          end;
4348       end if;
4349
4350       --  Create a concatenation operator for the new type. Internal array
4351       --  types created for packed entities do not need such, they are
4352       --  compatible with the user-defined type.
4353
4354       if Number_Dimensions (T) = 1
4355          and then not Is_Packed_Array_Type (T)
4356       then
4357          New_Concatenation_Op (T);
4358       end if;
4359
4360       --  In the case of an unconstrained array the parser has already verified
4361       --  that all the indices are unconstrained but we still need to make sure
4362       --  that the element type is constrained.
4363
4364       if Is_Indefinite_Subtype (Element_Type) then
4365          Error_Msg_N
4366            ("unconstrained element type in array declaration",
4367             Subtype_Indication (Component_Def));
4368
4369       elsif Is_Abstract_Type (Element_Type) then
4370          Error_Msg_N
4371            ("the type of a component cannot be abstract",
4372             Subtype_Indication (Component_Def));
4373       end if;
4374    end Array_Type_Declaration;
4375
4376    ------------------------------------------------------
4377    -- Replace_Anonymous_Access_To_Protected_Subprogram --
4378    ------------------------------------------------------
4379
4380    function Replace_Anonymous_Access_To_Protected_Subprogram
4381      (N : Node_Id) return Entity_Id
4382    is
4383       Loc : constant Source_Ptr := Sloc (N);
4384
4385       Curr_Scope : constant Scope_Stack_Entry :=
4386                      Scope_Stack.Table (Scope_Stack.Last);
4387
4388       Anon : constant Entity_Id :=
4389                Make_Defining_Identifier (Loc,
4390                  Chars => New_Internal_Name ('S'));
4391
4392       Acc  : Node_Id;
4393       Comp : Node_Id;
4394       Decl : Node_Id;
4395       P    : Node_Id;
4396
4397    begin
4398       Set_Is_Internal (Anon);
4399
4400       case Nkind (N) is
4401          when N_Component_Declaration       |
4402            N_Unconstrained_Array_Definition |
4403            N_Constrained_Array_Definition   =>
4404             Comp := Component_Definition (N);
4405             Acc  := Access_Definition (Comp);
4406
4407          when N_Discriminant_Specification =>
4408             Comp := Discriminant_Type (N);
4409             Acc  := Comp;
4410
4411          when N_Parameter_Specification =>
4412             Comp := Parameter_Type (N);
4413             Acc  := Comp;
4414
4415          when N_Access_Function_Definition  =>
4416             Comp := Result_Definition (N);
4417             Acc  := Comp;
4418
4419          when N_Object_Declaration  =>
4420             Comp := Object_Definition (N);
4421             Acc  := Comp;
4422
4423          when others =>
4424             raise Program_Error;
4425       end case;
4426
4427       Decl := Make_Full_Type_Declaration (Loc,
4428                 Defining_Identifier => Anon,
4429                 Type_Definition   =>
4430                   Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
4431
4432       Mark_Rewrite_Insertion (Decl);
4433
4434       --  Insert the new declaration in the nearest enclosing scope
4435
4436       P := Parent (N);
4437       while Present (P) and then not Has_Declarations (P) loop
4438          P := Parent (P);
4439       end loop;
4440
4441       pragma Assert (Present (P));
4442
4443       if Nkind (P) = N_Package_Specification then
4444          Prepend (Decl, Visible_Declarations (P));
4445       else
4446          Prepend (Decl, Declarations (P));
4447       end if;
4448
4449       --  Replace the anonymous type with an occurrence of the new declaration.
4450       --  In all cases the rewritten node does not have the null-exclusion
4451       --  attribute because (if present) it was already inherited by the
4452       --  anonymous entity (Anon). Thus, in case of components we do not
4453       --  inherit this attribute.
4454
4455       if Nkind (N) = N_Parameter_Specification then
4456          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4457          Set_Etype (Defining_Identifier (N), Anon);
4458          Set_Null_Exclusion_Present (N, False);
4459
4460       elsif Nkind (N) = N_Object_Declaration then
4461          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4462          Set_Etype (Defining_Identifier (N), Anon);
4463
4464       elsif Nkind (N) = N_Access_Function_Definition then
4465          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4466
4467       else
4468          Rewrite (Comp,
4469            Make_Component_Definition (Loc,
4470              Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
4471       end if;
4472
4473       Mark_Rewrite_Insertion (Comp);
4474
4475       --  Temporarily remove the current scope from the stack to add the new
4476       --  declarations to the enclosing scope
4477
4478       if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
4479          Analyze (Decl);
4480
4481       else
4482          Scope_Stack.Decrement_Last;
4483          Analyze (Decl);
4484          Set_Is_Itype (Anon);
4485          Scope_Stack.Append (Curr_Scope);
4486       end if;
4487
4488       Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
4489       Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
4490       return Anon;
4491    end Replace_Anonymous_Access_To_Protected_Subprogram;
4492
4493    -------------------------------
4494    -- Build_Derived_Access_Type --
4495    -------------------------------
4496
4497    procedure Build_Derived_Access_Type
4498      (N            : Node_Id;
4499       Parent_Type  : Entity_Id;
4500       Derived_Type : Entity_Id)
4501    is
4502       S : constant Node_Id := Subtype_Indication (Type_Definition (N));
4503
4504       Desig_Type      : Entity_Id;
4505       Discr           : Entity_Id;
4506       Discr_Con_Elist : Elist_Id;
4507       Discr_Con_El    : Elmt_Id;
4508       Subt            : Entity_Id;
4509
4510    begin
4511       --  Set the designated type so it is available in case this is an access
4512       --  to a self-referential type, e.g. a standard list type with a next
4513       --  pointer. Will be reset after subtype is built.
4514
4515       Set_Directly_Designated_Type
4516         (Derived_Type, Designated_Type (Parent_Type));
4517
4518       Subt := Process_Subtype (S, N);
4519
4520       if Nkind (S) /= N_Subtype_Indication
4521         and then Subt /= Base_Type (Subt)
4522       then
4523          Set_Ekind (Derived_Type, E_Access_Subtype);
4524       end if;
4525
4526       if Ekind (Derived_Type) = E_Access_Subtype then
4527          declare
4528             Pbase      : constant Entity_Id := Base_Type (Parent_Type);
4529             Ibase      : constant Entity_Id :=
4530                            Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
4531             Svg_Chars  : constant Name_Id   := Chars (Ibase);
4532             Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
4533
4534          begin
4535             Copy_Node (Pbase, Ibase);
4536
4537             Set_Chars             (Ibase, Svg_Chars);
4538             Set_Next_Entity       (Ibase, Svg_Next_E);
4539             Set_Sloc              (Ibase, Sloc (Derived_Type));
4540             Set_Scope             (Ibase, Scope (Derived_Type));
4541             Set_Freeze_Node       (Ibase, Empty);
4542             Set_Is_Frozen         (Ibase, False);
4543             Set_Comes_From_Source (Ibase, False);
4544             Set_Is_First_Subtype  (Ibase, False);
4545
4546             Set_Etype (Ibase, Pbase);
4547             Set_Etype (Derived_Type, Ibase);
4548          end;
4549       end if;
4550
4551       Set_Directly_Designated_Type
4552         (Derived_Type, Designated_Type (Subt));
4553
4554       Set_Is_Constrained     (Derived_Type, Is_Constrained (Subt));
4555       Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
4556       Set_Size_Info          (Derived_Type,                     Parent_Type);
4557       Set_RM_Size            (Derived_Type, RM_Size            (Parent_Type));
4558       Set_Depends_On_Private (Derived_Type,
4559                               Has_Private_Component (Derived_Type));
4560       Conditional_Delay      (Derived_Type, Subt);
4561
4562       --  Ada 2005 (AI-231). Set the null-exclusion attribute
4563
4564       if Null_Exclusion_Present (Type_Definition (N))
4565         or else Can_Never_Be_Null (Parent_Type)
4566       then
4567          Set_Can_Never_Be_Null (Derived_Type);
4568       end if;
4569
4570       --  Note: we do not copy the Storage_Size_Variable, since we always go to
4571       --  the root type for this information.
4572
4573       --  Apply range checks to discriminants for derived record case
4574       --  ??? THIS CODE SHOULD NOT BE HERE REALLY.
4575
4576       Desig_Type := Designated_Type (Derived_Type);
4577       if Is_Composite_Type (Desig_Type)
4578         and then (not Is_Array_Type (Desig_Type))
4579         and then Has_Discriminants (Desig_Type)
4580         and then Base_Type (Desig_Type) /= Desig_Type
4581       then
4582          Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
4583          Discr_Con_El := First_Elmt (Discr_Con_Elist);
4584
4585          Discr := First_Discriminant (Base_Type (Desig_Type));
4586          while Present (Discr_Con_El) loop
4587             Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
4588             Next_Elmt (Discr_Con_El);
4589             Next_Discriminant (Discr);
4590          end loop;
4591       end if;
4592    end Build_Derived_Access_Type;
4593
4594    ------------------------------
4595    -- Build_Derived_Array_Type --
4596    ------------------------------
4597
4598    procedure Build_Derived_Array_Type
4599      (N            : Node_Id;
4600       Parent_Type  : Entity_Id;
4601       Derived_Type : Entity_Id)
4602    is
4603       Loc           : constant Source_Ptr := Sloc (N);
4604       Tdef          : constant Node_Id    := Type_Definition (N);
4605       Indic         : constant Node_Id    := Subtype_Indication (Tdef);
4606       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
4607       Implicit_Base : Entity_Id;
4608       New_Indic     : Node_Id;
4609
4610       procedure Make_Implicit_Base;
4611       --  If the parent subtype is constrained, the derived type is a subtype
4612       --  of an implicit base type derived from the parent base.
4613
4614       ------------------------
4615       -- Make_Implicit_Base --
4616       ------------------------
4617
4618       procedure Make_Implicit_Base is
4619       begin
4620          Implicit_Base :=
4621            Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
4622
4623          Set_Ekind (Implicit_Base, Ekind (Parent_Base));
4624          Set_Etype (Implicit_Base, Parent_Base);
4625
4626          Copy_Array_Subtype_Attributes   (Implicit_Base, Parent_Base);
4627          Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
4628
4629          Set_Has_Delayed_Freeze (Implicit_Base, True);
4630       end Make_Implicit_Base;
4631
4632    --  Start of processing for Build_Derived_Array_Type
4633
4634    begin
4635       if not Is_Constrained (Parent_Type) then
4636          if Nkind (Indic) /= N_Subtype_Indication then
4637             Set_Ekind (Derived_Type, E_Array_Type);
4638
4639             Copy_Array_Subtype_Attributes   (Derived_Type, Parent_Type);
4640             Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
4641
4642             Set_Has_Delayed_Freeze (Derived_Type, True);
4643
4644          else
4645             Make_Implicit_Base;
4646             Set_Etype (Derived_Type, Implicit_Base);
4647
4648             New_Indic :=
4649               Make_Subtype_Declaration (Loc,
4650                 Defining_Identifier => Derived_Type,
4651                 Subtype_Indication  =>
4652                   Make_Subtype_Indication (Loc,
4653                     Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
4654                     Constraint => Constraint (Indic)));
4655
4656             Rewrite (N, New_Indic);
4657             Analyze (N);
4658          end if;
4659
4660       else
4661          if Nkind (Indic) /= N_Subtype_Indication then
4662             Make_Implicit_Base;
4663
4664             Set_Ekind             (Derived_Type, Ekind (Parent_Type));
4665             Set_Etype             (Derived_Type, Implicit_Base);
4666             Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
4667
4668          else
4669             Error_Msg_N ("illegal constraint on constrained type", Indic);
4670          end if;
4671       end if;
4672
4673       --  If parent type is not a derived type itself, and is declared in
4674       --  closed scope (e.g. a subprogram), then we must explicitly introduce
4675       --  the new type's concatenation operator since Derive_Subprograms
4676       --  will not inherit the parent's operator. If the parent type is
4677       --  unconstrained, the operator is of the unconstrained base type.
4678
4679       if Number_Dimensions (Parent_Type) = 1
4680         and then not Is_Limited_Type (Parent_Type)
4681         and then not Is_Derived_Type (Parent_Type)
4682         and then not Is_Package_Or_Generic_Package
4683                        (Scope (Base_Type (Parent_Type)))
4684       then
4685          if not Is_Constrained (Parent_Type)
4686            and then Is_Constrained (Derived_Type)
4687          then
4688             New_Concatenation_Op (Implicit_Base);
4689          else
4690             New_Concatenation_Op (Derived_Type);
4691          end if;
4692       end if;
4693    end Build_Derived_Array_Type;
4694
4695    -----------------------------------
4696    -- Build_Derived_Concurrent_Type --
4697    -----------------------------------
4698
4699    procedure Build_Derived_Concurrent_Type
4700      (N            : Node_Id;
4701       Parent_Type  : Entity_Id;
4702       Derived_Type : Entity_Id)
4703    is
4704       D_Constraint : Node_Id;
4705       Disc_Spec    : Node_Id;
4706       Old_Disc     : Entity_Id;
4707       New_Disc     : Entity_Id;
4708
4709       Constraint_Present : constant Boolean :=
4710                              Nkind (Subtype_Indication (Type_Definition (N)))
4711                                                      = N_Subtype_Indication;
4712
4713    begin
4714       Set_Stored_Constraint (Derived_Type, No_Elist);
4715
4716       --  Copy Storage_Size and Relative_Deadline variables if task case
4717
4718       if Is_Task_Type (Parent_Type) then
4719          Set_Storage_Size_Variable (Derived_Type,
4720            Storage_Size_Variable (Parent_Type));
4721          Set_Relative_Deadline_Variable (Derived_Type,
4722            Relative_Deadline_Variable (Parent_Type));
4723       end if;
4724
4725       if Present (Discriminant_Specifications (N)) then
4726          Push_Scope (Derived_Type);
4727          Check_Or_Process_Discriminants (N, Derived_Type);
4728          End_Scope;
4729
4730       elsif Constraint_Present then
4731
4732          --  Build constrained subtype and derive from it
4733
4734          declare
4735             Loc  : constant Source_Ptr := Sloc (N);
4736             Anon : constant Entity_Id :=
4737                      Make_Defining_Identifier (Loc,
4738                        New_External_Name (Chars (Derived_Type), 'T'));
4739             Decl : Node_Id;
4740
4741          begin
4742             Decl :=
4743               Make_Subtype_Declaration (Loc,
4744                 Defining_Identifier => Anon,
4745                 Subtype_Indication =>
4746                   Subtype_Indication (Type_Definition (N)));
4747             Insert_Before (N, Decl);
4748             Analyze (Decl);
4749
4750             Rewrite (Subtype_Indication (Type_Definition (N)),
4751               New_Occurrence_Of (Anon, Loc));
4752             Set_Analyzed (Derived_Type, False);
4753             Analyze (N);
4754             return;
4755          end;
4756       end if;
4757
4758       --  All attributes are inherited from parent. In particular,
4759       --  entries and the corresponding record type are the same.
4760       --  Discriminants may be renamed, and must be treated separately.
4761
4762       Set_Has_Discriminants
4763         (Derived_Type, Has_Discriminants         (Parent_Type));
4764       Set_Corresponding_Record_Type
4765         (Derived_Type, Corresponding_Record_Type (Parent_Type));
4766
4767       --  Is_Constrained is set according the parent subtype, but is set to
4768       --  False if the derived type is declared with new discriminants.
4769
4770       Set_Is_Constrained
4771         (Derived_Type,
4772          (Is_Constrained (Parent_Type) or else Constraint_Present)
4773            and then not Present (Discriminant_Specifications (N)));
4774
4775       if Constraint_Present then
4776          if not Has_Discriminants (Parent_Type) then
4777             Error_Msg_N ("untagged parent must have discriminants", N);
4778
4779          elsif Present (Discriminant_Specifications (N)) then
4780
4781             --  Verify that new discriminants are used to constrain old ones
4782
4783             D_Constraint :=
4784               First
4785                 (Constraints
4786                   (Constraint (Subtype_Indication (Type_Definition (N)))));
4787
4788             Old_Disc  := First_Discriminant (Parent_Type);
4789             New_Disc  := First_Discriminant (Derived_Type);
4790             Disc_Spec := First (Discriminant_Specifications (N));
4791             while Present (Old_Disc) and then Present (Disc_Spec) loop
4792                if Nkind (Discriminant_Type (Disc_Spec)) /=
4793                                               N_Access_Definition
4794                then
4795                   Analyze (Discriminant_Type (Disc_Spec));
4796
4797                   if not Subtypes_Statically_Compatible (
4798                              Etype (Discriminant_Type (Disc_Spec)),
4799                                Etype (Old_Disc))
4800                   then
4801                      Error_Msg_N
4802                        ("not statically compatible with parent discriminant",
4803                         Discriminant_Type (Disc_Spec));
4804                   end if;
4805                end if;
4806
4807                if Nkind (D_Constraint) = N_Identifier
4808                  and then Chars (D_Constraint) /=
4809                           Chars (Defining_Identifier (Disc_Spec))
4810                then
4811                   Error_Msg_N ("new discriminants must constrain old ones",
4812                     D_Constraint);
4813                else
4814                   Set_Corresponding_Discriminant (New_Disc, Old_Disc);
4815                end if;
4816
4817                Next_Discriminant (Old_Disc);
4818                Next_Discriminant (New_Disc);
4819                Next (Disc_Spec);
4820             end loop;
4821
4822             if Present (Old_Disc) or else Present (Disc_Spec) then
4823                Error_Msg_N ("discriminant mismatch in derivation", N);
4824             end if;
4825
4826          end if;
4827
4828       elsif Present (Discriminant_Specifications (N)) then
4829          Error_Msg_N
4830            ("missing discriminant constraint in untagged derivation",
4831             N);
4832       end if;
4833
4834       if Present (Discriminant_Specifications (N)) then
4835          Old_Disc := First_Discriminant (Parent_Type);
4836          while Present (Old_Disc) loop
4837
4838             if No (Next_Entity (Old_Disc))
4839               or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
4840             then
4841                Set_Next_Entity (Last_Entity (Derived_Type),
4842                                          Next_Entity (Old_Disc));
4843                exit;
4844             end if;
4845
4846             Next_Discriminant (Old_Disc);
4847          end loop;
4848
4849       else
4850          Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
4851          if Has_Discriminants (Parent_Type) then
4852             Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
4853             Set_Discriminant_Constraint (
4854               Derived_Type, Discriminant_Constraint (Parent_Type));
4855          end if;
4856       end if;
4857
4858       Set_Last_Entity  (Derived_Type, Last_Entity  (Parent_Type));
4859
4860       Set_Has_Completion (Derived_Type);
4861    end Build_Derived_Concurrent_Type;
4862
4863    ------------------------------------
4864    -- Build_Derived_Enumeration_Type --
4865    ------------------------------------
4866
4867    procedure Build_Derived_Enumeration_Type
4868      (N            : Node_Id;
4869       Parent_Type  : Entity_Id;
4870       Derived_Type : Entity_Id)
4871    is
4872       Loc           : constant Source_Ptr := Sloc (N);
4873       Def           : constant Node_Id    := Type_Definition (N);
4874       Indic         : constant Node_Id    := Subtype_Indication (Def);
4875       Implicit_Base : Entity_Id;
4876       Literal       : Entity_Id;
4877       New_Lit       : Entity_Id;
4878       Literals_List : List_Id;
4879       Type_Decl     : Node_Id;
4880       Hi, Lo        : Node_Id;
4881       Rang_Expr     : Node_Id;
4882
4883    begin
4884       --  Since types Standard.Character and Standard.Wide_Character do
4885       --  not have explicit literals lists we need to process types derived
4886       --  from them specially. This is handled by Derived_Standard_Character.
4887       --  If the parent type is a generic type, there are no literals either,
4888       --  and we construct the same skeletal representation as for the generic
4889       --  parent type.
4890
4891       if Is_Standard_Character_Type (Parent_Type) then
4892          Derived_Standard_Character (N, Parent_Type, Derived_Type);
4893
4894       elsif Is_Generic_Type (Root_Type (Parent_Type)) then
4895          declare
4896             Lo : Node_Id;
4897             Hi : Node_Id;
4898
4899          begin
4900             Lo :=
4901                Make_Attribute_Reference (Loc,
4902                  Attribute_Name => Name_First,
4903                  Prefix => New_Reference_To (Derived_Type, Loc));
4904             Set_Etype (Lo, Derived_Type);
4905
4906             Hi :=
4907                Make_Attribute_Reference (Loc,
4908                  Attribute_Name => Name_Last,
4909                  Prefix => New_Reference_To (Derived_Type, Loc));
4910             Set_Etype (Hi, Derived_Type);
4911
4912             Set_Scalar_Range (Derived_Type,
4913                Make_Range (Loc,
4914                  Low_Bound => Lo,
4915                  High_Bound => Hi));
4916          end;
4917
4918       else
4919          --  If a constraint is present, analyze the bounds to catch
4920          --  premature usage of the derived literals.
4921
4922          if Nkind (Indic) = N_Subtype_Indication
4923            and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
4924          then
4925             Analyze (Low_Bound  (Range_Expression (Constraint (Indic))));
4926             Analyze (High_Bound (Range_Expression (Constraint (Indic))));
4927          end if;
4928
4929          --  Introduce an implicit base type for the derived type even if there
4930          --  is no constraint attached to it, since this seems closer to the
4931          --  Ada semantics. Build a full type declaration tree for the derived
4932          --  type using the implicit base type as the defining identifier. The
4933          --  build a subtype declaration tree which applies the constraint (if
4934          --  any) have it replace the derived type declaration.
4935
4936          Literal := First_Literal (Parent_Type);
4937          Literals_List := New_List;
4938          while Present (Literal)
4939            and then Ekind (Literal) = E_Enumeration_Literal
4940          loop
4941             --  Literals of the derived type have the same representation as
4942             --  those of the parent type, but this representation can be
4943             --  overridden by an explicit representation clause. Indicate
4944             --  that there is no explicit representation given yet. These
4945             --  derived literals are implicit operations of the new type,
4946             --  and can be overridden by explicit ones.
4947
4948             if Nkind (Literal) = N_Defining_Character_Literal then
4949                New_Lit :=
4950                  Make_Defining_Character_Literal (Loc, Chars (Literal));
4951             else
4952                New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
4953             end if;
4954
4955             Set_Ekind                (New_Lit, E_Enumeration_Literal);
4956             Set_Enumeration_Pos      (New_Lit, Enumeration_Pos (Literal));
4957             Set_Enumeration_Rep      (New_Lit, Enumeration_Rep (Literal));
4958             Set_Enumeration_Rep_Expr (New_Lit, Empty);
4959             Set_Alias                (New_Lit, Literal);
4960             Set_Is_Known_Valid       (New_Lit, True);
4961
4962             Append (New_Lit, Literals_List);
4963             Next_Literal (Literal);
4964          end loop;
4965
4966          Implicit_Base :=
4967            Make_Defining_Identifier (Sloc (Derived_Type),
4968              New_External_Name (Chars (Derived_Type), 'B'));
4969
4970          --  Indicate the proper nature of the derived type. This must be done
4971          --  before analysis of the literals, to recognize cases when a literal
4972          --  may be hidden by a previous explicit function definition (cf.
4973          --  c83031a).
4974
4975          Set_Ekind (Derived_Type, E_Enumeration_Subtype);
4976          Set_Etype (Derived_Type, Implicit_Base);
4977
4978          Type_Decl :=
4979            Make_Full_Type_Declaration (Loc,
4980              Defining_Identifier => Implicit_Base,
4981              Discriminant_Specifications => No_List,
4982              Type_Definition =>
4983                Make_Enumeration_Type_Definition (Loc, Literals_List));
4984
4985          Mark_Rewrite_Insertion (Type_Decl);
4986          Insert_Before (N, Type_Decl);
4987          Analyze (Type_Decl);
4988
4989          --  After the implicit base is analyzed its Etype needs to be changed
4990          --  to reflect the fact that it is derived from the parent type which
4991          --  was ignored during analysis. We also set the size at this point.
4992
4993          Set_Etype (Implicit_Base, Parent_Type);
4994
4995          Set_Size_Info      (Implicit_Base,                 Parent_Type);
4996          Set_RM_Size        (Implicit_Base, RM_Size        (Parent_Type));
4997          Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
4998
4999          Set_Has_Non_Standard_Rep
5000                             (Implicit_Base, Has_Non_Standard_Rep
5001                                                            (Parent_Type));
5002          Set_Has_Delayed_Freeze (Implicit_Base);
5003
5004          --  Process the subtype indication including a validation check on the
5005          --  constraint, if any. If a constraint is given, its bounds must be
5006          --  implicitly converted to the new type.
5007
5008          if Nkind (Indic) = N_Subtype_Indication then
5009             declare
5010                R : constant Node_Id :=
5011                      Range_Expression (Constraint (Indic));
5012
5013             begin
5014                if Nkind (R) = N_Range then
5015                   Hi := Build_Scalar_Bound
5016                           (High_Bound (R), Parent_Type, Implicit_Base);
5017                   Lo := Build_Scalar_Bound
5018                           (Low_Bound  (R), Parent_Type, Implicit_Base);
5019
5020                else
5021                   --  Constraint is a Range attribute. Replace with explicit
5022                   --  mention of the bounds of the prefix, which must be a
5023                   --  subtype.
5024
5025                   Analyze (Prefix (R));
5026                   Hi :=
5027                     Convert_To (Implicit_Base,
5028                       Make_Attribute_Reference (Loc,
5029                         Attribute_Name => Name_Last,
5030                         Prefix =>
5031                           New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5032
5033                   Lo :=
5034                     Convert_To (Implicit_Base,
5035                       Make_Attribute_Reference (Loc,
5036                         Attribute_Name => Name_First,
5037                         Prefix =>
5038                           New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5039                end if;
5040             end;
5041
5042          else
5043             Hi :=
5044               Build_Scalar_Bound
5045                 (Type_High_Bound (Parent_Type),
5046                  Parent_Type, Implicit_Base);
5047             Lo :=
5048                Build_Scalar_Bound
5049                  (Type_Low_Bound (Parent_Type),
5050                   Parent_Type, Implicit_Base);
5051          end if;
5052
5053          Rang_Expr :=
5054            Make_Range (Loc,
5055              Low_Bound  => Lo,
5056              High_Bound => Hi);
5057
5058          --  If we constructed a default range for the case where no range
5059          --  was given, then the expressions in the range must not freeze
5060          --  since they do not correspond to expressions in the source.
5061
5062          if Nkind (Indic) /= N_Subtype_Indication then
5063             Set_Must_Not_Freeze (Lo);
5064             Set_Must_Not_Freeze (Hi);
5065             Set_Must_Not_Freeze (Rang_Expr);
5066          end if;
5067
5068          Rewrite (N,
5069            Make_Subtype_Declaration (Loc,
5070              Defining_Identifier => Derived_Type,
5071              Subtype_Indication =>
5072                Make_Subtype_Indication (Loc,
5073                  Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
5074                  Constraint =>
5075                    Make_Range_Constraint (Loc,
5076                      Range_Expression => Rang_Expr))));
5077
5078          Analyze (N);
5079
5080          --  If pragma Discard_Names applies on the first subtype of the parent
5081          --  type, then it must be applied on this subtype as well.
5082
5083          if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
5084             Set_Discard_Names (Derived_Type);
5085          end if;
5086
5087          --  Apply a range check. Since this range expression doesn't have an
5088          --  Etype, we have to specifically pass the Source_Typ parameter. Is
5089          --  this right???
5090
5091          if Nkind (Indic) = N_Subtype_Indication then
5092             Apply_Range_Check (Range_Expression (Constraint (Indic)),
5093                                Parent_Type,
5094                                Source_Typ => Entity (Subtype_Mark (Indic)));
5095          end if;
5096       end if;
5097    end Build_Derived_Enumeration_Type;
5098
5099    --------------------------------
5100    -- Build_Derived_Numeric_Type --
5101    --------------------------------
5102
5103    procedure Build_Derived_Numeric_Type
5104      (N            : Node_Id;
5105       Parent_Type  : Entity_Id;
5106       Derived_Type : Entity_Id)
5107    is
5108       Loc           : constant Source_Ptr := Sloc (N);
5109       Tdef          : constant Node_Id    := Type_Definition (N);
5110       Indic         : constant Node_Id    := Subtype_Indication (Tdef);
5111       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
5112       No_Constraint : constant Boolean    := Nkind (Indic) /=
5113                                                   N_Subtype_Indication;
5114       Implicit_Base : Entity_Id;
5115
5116       Lo : Node_Id;
5117       Hi : Node_Id;
5118
5119    begin
5120       --  Process the subtype indication including a validation check on
5121       --  the constraint if any.
5122
5123       Discard_Node (Process_Subtype (Indic, N));
5124
5125       --  Introduce an implicit base type for the derived type even if there
5126       --  is no constraint attached to it, since this seems closer to the Ada
5127       --  semantics.
5128
5129       Implicit_Base :=
5130         Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5131
5132       Set_Etype          (Implicit_Base, Parent_Base);
5133       Set_Ekind          (Implicit_Base, Ekind          (Parent_Base));
5134       Set_Size_Info      (Implicit_Base,                 Parent_Base);
5135       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
5136       Set_Parent         (Implicit_Base, Parent (Derived_Type));
5137
5138       --  Set RM Size for discrete type or decimal fixed-point type
5139       --  Ordinary fixed-point is excluded, why???
5140
5141       if Is_Discrete_Type (Parent_Base)
5142         or else Is_Decimal_Fixed_Point_Type (Parent_Base)
5143       then
5144          Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
5145       end if;
5146
5147       Set_Has_Delayed_Freeze (Implicit_Base);
5148
5149       Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Base));
5150       Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
5151
5152       Set_Scalar_Range (Implicit_Base,
5153         Make_Range (Loc,
5154           Low_Bound  => Lo,
5155           High_Bound => Hi));
5156
5157       if Has_Infinities (Parent_Base) then
5158          Set_Includes_Infinities (Scalar_Range (Implicit_Base));
5159       end if;
5160
5161       --  The Derived_Type, which is the entity of the declaration, is a
5162       --  subtype of the implicit base. Its Ekind is a subtype, even in the
5163       --  absence of an explicit constraint.
5164
5165       Set_Etype (Derived_Type, Implicit_Base);
5166
5167       --  If we did not have a constraint, then the Ekind is set from the
5168       --  parent type (otherwise Process_Subtype has set the bounds)
5169
5170       if No_Constraint then
5171          Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
5172       end if;
5173
5174       --  If we did not have a range constraint, then set the range from the
5175       --  parent type. Otherwise, the call to Process_Subtype has set the
5176       --  bounds.
5177
5178       if No_Constraint
5179         or else not Has_Range_Constraint (Indic)
5180       then
5181          Set_Scalar_Range (Derived_Type,
5182            Make_Range (Loc,
5183              Low_Bound  => New_Copy_Tree (Type_Low_Bound  (Parent_Type)),
5184              High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
5185          Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5186
5187          if Has_Infinities (Parent_Type) then
5188             Set_Includes_Infinities (Scalar_Range (Derived_Type));
5189          end if;
5190       end if;
5191
5192       Set_Is_Descendent_Of_Address (Derived_Type,
5193         Is_Descendent_Of_Address (Parent_Type));
5194       Set_Is_Descendent_Of_Address (Implicit_Base,
5195         Is_Descendent_Of_Address (Parent_Type));
5196
5197       --  Set remaining type-specific fields, depending on numeric type
5198
5199       if Is_Modular_Integer_Type (Parent_Type) then
5200          Set_Modulus (Implicit_Base, Modulus (Parent_Base));
5201
5202          Set_Non_Binary_Modulus
5203            (Implicit_Base, Non_Binary_Modulus (Parent_Base));
5204
5205       elsif Is_Floating_Point_Type (Parent_Type) then
5206
5207          --  Digits of base type is always copied from the digits value of
5208          --  the parent base type, but the digits of the derived type will
5209          --  already have been set if there was a constraint present.
5210
5211          Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5212          Set_Vax_Float    (Implicit_Base, Vax_Float    (Parent_Base));
5213
5214          if No_Constraint then
5215             Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
5216          end if;
5217
5218       elsif Is_Fixed_Point_Type (Parent_Type) then
5219
5220          --  Small of base type and derived type are always copied from the
5221          --  parent base type, since smalls never change. The delta of the
5222          --  base type is also copied from the parent base type. However the
5223          --  delta of the derived type will have been set already if a
5224          --  constraint was present.
5225
5226          Set_Small_Value (Derived_Type,  Small_Value (Parent_Base));
5227          Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
5228          Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
5229
5230          if No_Constraint then
5231             Set_Delta_Value (Derived_Type,  Delta_Value (Parent_Type));
5232          end if;
5233
5234          --  The scale and machine radix in the decimal case are always
5235          --  copied from the parent base type.
5236
5237          if Is_Decimal_Fixed_Point_Type (Parent_Type) then
5238             Set_Scale_Value (Derived_Type,  Scale_Value (Parent_Base));
5239             Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
5240
5241             Set_Machine_Radix_10
5242               (Derived_Type,  Machine_Radix_10 (Parent_Base));
5243             Set_Machine_Radix_10
5244               (Implicit_Base, Machine_Radix_10 (Parent_Base));
5245
5246             Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5247
5248             if No_Constraint then
5249                Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
5250
5251             else
5252                --  the analysis of the subtype_indication sets the
5253                --  digits value of the derived type.
5254
5255                null;
5256             end if;
5257          end if;
5258       end if;
5259
5260       --  The type of the bounds is that of the parent type, and they
5261       --  must be converted to the derived type.
5262
5263       Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
5264
5265       --  The implicit_base should be frozen when the derived type is frozen,
5266       --  but note that it is used in the conversions of the bounds. For fixed
5267       --  types we delay the determination of the bounds until the proper
5268       --  freezing point. For other numeric types this is rejected by GCC, for
5269       --  reasons that are currently unclear (???), so we choose to freeze the
5270       --  implicit base now. In the case of integers and floating point types
5271       --  this is harmless because subsequent representation clauses cannot
5272       --  affect anything, but it is still baffling that we cannot use the
5273       --  same mechanism for all derived numeric types.
5274
5275       --  There is a further complication: actually *some* representation
5276       --  clauses can affect the implicit base type. Namely, attribute
5277       --  definition clauses for stream-oriented attributes need to set the
5278       --  corresponding TSS entries on the base type, and this normally cannot
5279       --  be done after the base type is frozen, so the circuitry in
5280       --  Sem_Ch13.New_Stream_Subprogram must account for this possibility and
5281       --  not use Set_TSS in this case.
5282
5283       if Is_Fixed_Point_Type (Parent_Type) then
5284          Conditional_Delay (Implicit_Base, Parent_Type);
5285       else
5286          Freeze_Before (N, Implicit_Base);
5287       end if;
5288    end Build_Derived_Numeric_Type;
5289
5290    --------------------------------
5291    -- Build_Derived_Private_Type --
5292    --------------------------------
5293
5294    procedure Build_Derived_Private_Type
5295      (N             : Node_Id;
5296       Parent_Type   : Entity_Id;
5297       Derived_Type  : Entity_Id;
5298       Is_Completion : Boolean;
5299       Derive_Subps  : Boolean := True)
5300    is
5301       Der_Base    : Entity_Id;
5302       Discr       : Entity_Id;
5303       Full_Decl   : Node_Id := Empty;
5304       Full_Der    : Entity_Id;
5305       Full_P      : Entity_Id;
5306       Last_Discr  : Entity_Id;
5307       Par_Scope   : constant Entity_Id := Scope (Base_Type (Parent_Type));
5308       Swapped     : Boolean := False;
5309
5310       procedure Copy_And_Build;
5311       --  Copy derived type declaration, replace parent with its full view,
5312       --  and analyze new declaration.
5313
5314       --------------------
5315       -- Copy_And_Build --
5316       --------------------
5317
5318       procedure Copy_And_Build is
5319          Full_N : Node_Id;
5320
5321       begin
5322          if Ekind (Parent_Type) in Record_Kind
5323            or else
5324              (Ekind (Parent_Type) in Enumeration_Kind
5325                and then not Is_Standard_Character_Type (Parent_Type)
5326                and then not Is_Generic_Type (Root_Type (Parent_Type)))
5327          then
5328             Full_N := New_Copy_Tree (N);
5329             Insert_After (N, Full_N);
5330             Build_Derived_Type (
5331               Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
5332
5333          else
5334             Build_Derived_Type (
5335               N, Parent_Type, Full_Der, True, Derive_Subps => False);
5336          end if;
5337       end Copy_And_Build;
5338
5339    --  Start of processing for Build_Derived_Private_Type
5340
5341    begin
5342       if Is_Tagged_Type (Parent_Type) then
5343          Build_Derived_Record_Type
5344            (N, Parent_Type, Derived_Type, Derive_Subps);
5345          return;
5346
5347       elsif Has_Discriminants (Parent_Type) then
5348          if Present (Full_View (Parent_Type)) then
5349             if not Is_Completion then
5350
5351                --  Copy declaration for subsequent analysis, to provide a
5352                --  completion for what is a private declaration. Indicate that
5353                --  the full type is internally generated.
5354
5355                Full_Decl := New_Copy_Tree (N);
5356                Full_Der  := New_Copy (Derived_Type);
5357                Set_Comes_From_Source (Full_Decl, False);
5358                Set_Comes_From_Source (Full_Der, False);
5359
5360                Insert_After (N, Full_Decl);
5361
5362             else
5363                --  If this is a completion, the full view being built is
5364                --  itself private. We build a subtype of the parent with
5365                --  the same constraints as this full view, to convey to the
5366                --  back end the constrained components and the size of this
5367                --  subtype. If the parent is constrained, its full view can
5368                --  serve as the underlying full view of the derived type.
5369
5370                if No (Discriminant_Specifications (N)) then
5371                   if Nkind (Subtype_Indication (Type_Definition (N))) =
5372                                                         N_Subtype_Indication
5373                   then
5374                      Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
5375
5376                   elsif Is_Constrained (Full_View (Parent_Type)) then
5377                      Set_Underlying_Full_View (Derived_Type,
5378                        Full_View (Parent_Type));
5379                   end if;
5380
5381                else
5382                   --  If there are new discriminants, the parent subtype is
5383                   --  constrained by them, but it is not clear how to build
5384                   --  the underlying_full_view in this case ???
5385
5386                   null;
5387                end if;
5388             end if;
5389          end if;
5390
5391          --  Build partial view of derived type from partial view of parent
5392
5393          Build_Derived_Record_Type
5394            (N, Parent_Type, Derived_Type, Derive_Subps);
5395
5396          if Present (Full_View (Parent_Type))
5397            and then not Is_Completion
5398          then
5399             if not In_Open_Scopes (Par_Scope)
5400               or else not In_Same_Source_Unit (N, Parent_Type)
5401             then
5402                --  Swap partial and full views temporarily
5403
5404                Install_Private_Declarations (Par_Scope);
5405                Install_Visible_Declarations (Par_Scope);
5406                Swapped := True;
5407             end if;
5408
5409             --  Build full view of derived type from full view of parent which
5410             --  is now installed. Subprograms have been derived on the partial
5411             --  view, the completion does not derive them anew.
5412
5413             if not Is_Tagged_Type (Parent_Type) then
5414
5415                --  If the parent is itself derived from another private type,
5416                --  installing the private declarations has not affected its
5417                --  privacy status, so use its own full view explicitly.
5418
5419                if Is_Private_Type (Parent_Type) then
5420                   Build_Derived_Record_Type
5421                     (Full_Decl, Full_View (Parent_Type), Full_Der, False);
5422                else
5423                   Build_Derived_Record_Type
5424                     (Full_Decl, Parent_Type, Full_Der, False);
5425                end if;
5426
5427             else
5428                --  If full view of parent is tagged, the completion
5429                --  inherits the proper primitive operations.
5430
5431                Set_Defining_Identifier (Full_Decl, Full_Der);
5432                Build_Derived_Record_Type
5433                  (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
5434                Set_Analyzed (Full_Decl);
5435             end if;
5436
5437             if Swapped then
5438                Uninstall_Declarations (Par_Scope);
5439
5440                if In_Open_Scopes (Par_Scope) then
5441                   Install_Visible_Declarations (Par_Scope);
5442                end if;
5443             end if;
5444
5445             Der_Base := Base_Type (Derived_Type);
5446             Set_Full_View (Derived_Type, Full_Der);
5447             Set_Full_View (Der_Base, Base_Type (Full_Der));
5448
5449             --  Copy the discriminant list from full view to the partial views
5450             --  (base type and its subtype). Gigi requires that the partial
5451             --  and full views have the same discriminants.
5452
5453             --  Note that since the partial view is pointing to discriminants
5454             --  in the full view, their scope will be that of the full view.
5455             --  This might cause some front end problems and need
5456             --  adjustment???
5457
5458             Discr := First_Discriminant (Base_Type (Full_Der));
5459             Set_First_Entity (Der_Base, Discr);
5460
5461             loop
5462                Last_Discr := Discr;
5463                Next_Discriminant (Discr);
5464                exit when No (Discr);
5465             end loop;
5466
5467             Set_Last_Entity (Der_Base, Last_Discr);
5468
5469             Set_First_Entity (Derived_Type, First_Entity (Der_Base));
5470             Set_Last_Entity  (Derived_Type, Last_Entity  (Der_Base));
5471             Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
5472
5473          else
5474             --  If this is a completion, the derived type stays private
5475             --  and there is no need to create a further full view, except
5476             --  in the unusual case when the derivation is nested within a
5477             --  child unit, see below.
5478
5479             null;
5480          end if;
5481
5482       elsif Present (Full_View (Parent_Type))
5483         and then  Has_Discriminants (Full_View (Parent_Type))
5484       then
5485          if Has_Unknown_Discriminants (Parent_Type)
5486            and then Nkind (Subtype_Indication (Type_Definition (N))) =
5487                                                          N_Subtype_Indication
5488          then
5489             Error_Msg_N
5490               ("cannot constrain type with unknown discriminants",
5491                Subtype_Indication (Type_Definition (N)));
5492             return;
5493          end if;
5494
5495          --  If full view of parent is a record type, Build full view as
5496          --  a derivation from the parent's full view. Partial view remains
5497          --  private. For code generation and linking, the full view must
5498          --  have the same public status as the partial one. This full view
5499          --  is only needed if the parent type is in an enclosing scope, so
5500          --  that the full view may actually become visible, e.g. in a child
5501          --  unit. This is both more efficient, and avoids order of freezing
5502          --  problems with the added entities.
5503
5504          if not Is_Private_Type (Full_View (Parent_Type))
5505            and then (In_Open_Scopes (Scope (Parent_Type)))
5506          then
5507             Full_Der := Make_Defining_Identifier (Sloc (Derived_Type),
5508                                               Chars (Derived_Type));
5509             Set_Is_Itype (Full_Der);
5510             Set_Has_Private_Declaration (Full_Der);
5511             Set_Has_Private_Declaration (Derived_Type);
5512             Set_Associated_Node_For_Itype (Full_Der, N);
5513             Set_Parent (Full_Der, Parent (Derived_Type));
5514             Set_Full_View (Derived_Type, Full_Der);
5515             Set_Is_Public (Full_Der, Is_Public (Derived_Type));
5516             Full_P := Full_View (Parent_Type);
5517             Exchange_Declarations (Parent_Type);
5518             Copy_And_Build;
5519             Exchange_Declarations (Full_P);
5520
5521          else
5522             Build_Derived_Record_Type
5523               (N, Full_View (Parent_Type), Derived_Type,
5524                 Derive_Subps => False);
5525          end if;
5526
5527          --  In any case, the primitive operations are inherited from
5528          --  the parent type, not from the internal full view.
5529
5530          Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
5531
5532          if Derive_Subps then
5533             Derive_Subprograms (Parent_Type, Derived_Type);
5534          end if;
5535
5536       else
5537          --  Untagged type, No discriminants on either view
5538
5539          if Nkind (Subtype_Indication (Type_Definition (N))) =
5540                                                    N_Subtype_Indication
5541          then
5542             Error_Msg_N
5543               ("illegal constraint on type without discriminants", N);
5544          end if;
5545
5546          if Present (Discriminant_Specifications (N))
5547            and then Present (Full_View (Parent_Type))
5548            and then not Is_Tagged_Type (Full_View (Parent_Type))
5549          then
5550             Error_Msg_N
5551               ("cannot add discriminants to untagged type", N);
5552          end if;
5553
5554          Set_Stored_Constraint (Derived_Type, No_Elist);
5555          Set_Is_Constrained    (Derived_Type, Is_Constrained (Parent_Type));
5556          Set_Is_Controlled     (Derived_Type, Is_Controlled  (Parent_Type));
5557          Set_Has_Controlled_Component
5558                                (Derived_Type, Has_Controlled_Component
5559                                                              (Parent_Type));
5560
5561          --  Direct controlled types do not inherit Finalize_Storage_Only flag
5562
5563          if not Is_Controlled  (Parent_Type) then
5564             Set_Finalize_Storage_Only
5565               (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
5566          end if;
5567
5568          --  Construct the implicit full view by deriving from full view of
5569          --  the parent type. In order to get proper visibility, we install
5570          --  the parent scope and its declarations.
5571
5572          --  ??? if the parent is untagged private and its completion is
5573          --  tagged, this mechanism will not work because we cannot derive
5574          --  from the tagged full view unless we have an extension
5575
5576          if Present (Full_View (Parent_Type))
5577            and then not Is_Tagged_Type (Full_View (Parent_Type))
5578            and then not Is_Completion
5579          then
5580             Full_Der :=
5581               Make_Defining_Identifier (Sloc (Derived_Type),
5582                 Chars => Chars (Derived_Type));
5583             Set_Is_Itype (Full_Der);
5584             Set_Has_Private_Declaration (Full_Der);
5585             Set_Has_Private_Declaration (Derived_Type);
5586             Set_Associated_Node_For_Itype (Full_Der, N);
5587             Set_Parent (Full_Der, Parent (Derived_Type));
5588             Set_Full_View (Derived_Type, Full_Der);
5589
5590             if not In_Open_Scopes (Par_Scope) then
5591                Install_Private_Declarations (Par_Scope);
5592                Install_Visible_Declarations (Par_Scope);
5593                Copy_And_Build;
5594                Uninstall_Declarations (Par_Scope);
5595
5596             --  If parent scope is open and in another unit, and parent has a
5597             --  completion, then the derivation is taking place in the visible
5598             --  part of a child unit. In that case retrieve the full view of
5599             --  the parent momentarily.
5600
5601             elsif not In_Same_Source_Unit (N, Parent_Type) then
5602                Full_P := Full_View (Parent_Type);
5603                Exchange_Declarations (Parent_Type);
5604                Copy_And_Build;
5605                Exchange_Declarations (Full_P);
5606
5607             --  Otherwise it is a local derivation
5608
5609             else
5610                Copy_And_Build;
5611             end if;
5612
5613             Set_Scope                (Full_Der, Current_Scope);
5614             Set_Is_First_Subtype     (Full_Der,
5615                                        Is_First_Subtype (Derived_Type));
5616             Set_Has_Size_Clause      (Full_Der, False);
5617             Set_Has_Alignment_Clause (Full_Der, False);
5618             Set_Next_Entity          (Full_Der, Empty);
5619             Set_Has_Delayed_Freeze   (Full_Der);
5620             Set_Is_Frozen            (Full_Der, False);
5621             Set_Freeze_Node          (Full_Der, Empty);
5622             Set_Depends_On_Private   (Full_Der,
5623                                         Has_Private_Component    (Full_Der));
5624             Set_Public_Status        (Full_Der);
5625          end if;
5626       end if;
5627
5628       Set_Has_Unknown_Discriminants (Derived_Type,
5629         Has_Unknown_Discriminants (Parent_Type));
5630
5631       if Is_Private_Type (Derived_Type) then
5632          Set_Private_Dependents (Derived_Type, New_Elmt_List);
5633       end if;
5634
5635       if Is_Private_Type (Parent_Type)
5636         and then Base_Type (Parent_Type) = Parent_Type
5637         and then In_Open_Scopes (Scope (Parent_Type))
5638       then
5639          Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
5640
5641          if Is_Child_Unit (Scope (Current_Scope))
5642            and then Is_Completion
5643            and then In_Private_Part (Current_Scope)
5644            and then Scope (Parent_Type) /= Current_Scope
5645          then
5646             --  This is the unusual case where a type completed by a private
5647             --  derivation occurs within a package nested in a child unit,
5648             --  and the parent is declared in an ancestor. In this case, the
5649             --  full view of the parent type will become visible in the body
5650             --  of the enclosing child, and only then will the current type
5651             --  be possibly non-private. We build a underlying full view that
5652             --  will be installed when the enclosing child body is compiled.
5653
5654             Full_Der :=
5655               Make_Defining_Identifier (Sloc (Derived_Type),
5656                 Chars => Chars (Derived_Type));
5657             Set_Is_Itype (Full_Der);
5658             Build_Itype_Reference (Full_Der, N);
5659
5660             --  The full view will be used to swap entities on entry/exit to
5661             --  the body, and must appear in the entity list for the package.
5662
5663             Append_Entity (Full_Der, Scope (Derived_Type));
5664             Set_Has_Private_Declaration (Full_Der);
5665             Set_Has_Private_Declaration (Derived_Type);
5666             Set_Associated_Node_For_Itype (Full_Der, N);
5667             Set_Parent (Full_Der, Parent (Derived_Type));
5668             Full_P := Full_View (Parent_Type);
5669             Exchange_Declarations (Parent_Type);
5670             Copy_And_Build;
5671             Exchange_Declarations (Full_P);
5672             Set_Underlying_Full_View (Derived_Type, Full_Der);
5673          end if;
5674       end if;
5675    end Build_Derived_Private_Type;
5676
5677    -------------------------------
5678    -- Build_Derived_Record_Type --
5679    -------------------------------
5680
5681    --  1. INTRODUCTION
5682
5683    --  Ideally we would like to use the same model of type derivation for
5684    --  tagged and untagged record types. Unfortunately this is not quite
5685    --  possible because the semantics of representation clauses is different
5686    --  for tagged and untagged records under inheritance. Consider the
5687    --  following:
5688
5689    --     type R (...) is [tagged] record ... end record;
5690    --     type T (...) is new R (...) [with ...];
5691
5692    --  The representation clauses for T can specify a completely different
5693    --  record layout from R's. Hence the same component can be placed in two
5694    --  very different positions in objects of type T and R. If R and are tagged
5695    --  types, representation clauses for T can only specify the layout of non
5696    --  inherited components, thus components that are common in R and T have
5697    --  the same position in objects of type R and T.
5698
5699    --  This has two implications. The first is that the entire tree for R's
5700    --  declaration needs to be copied for T in the untagged case, so that T
5701    --  can be viewed as a record type of its own with its own representation
5702    --  clauses. The second implication is the way we handle discriminants.
5703    --  Specifically, in the untagged case we need a way to communicate to Gigi
5704    --  what are the real discriminants in the record, while for the semantics
5705    --  we need to consider those introduced by the user to rename the
5706    --  discriminants in the parent type. This is handled by introducing the
5707    --  notion of stored discriminants. See below for more.
5708
5709    --  Fortunately the way regular components are inherited can be handled in
5710    --  the same way in tagged and untagged types.
5711
5712    --  To complicate things a bit more the private view of a private extension
5713    --  cannot be handled in the same way as the full view (for one thing the
5714    --  semantic rules are somewhat different). We will explain what differs
5715    --  below.
5716
5717    --  2. DISCRIMINANTS UNDER INHERITANCE
5718
5719    --  The semantic rules governing the discriminants of derived types are
5720    --  quite subtle.
5721
5722    --   type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
5723    --      [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
5724
5725    --  If parent type has discriminants, then the discriminants that are
5726    --  declared in the derived type are [3.4 (11)]:
5727
5728    --  o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
5729    --    there is one;
5730
5731    --  o Otherwise, each discriminant of the parent type (implicitly declared
5732    --    in the same order with the same specifications). In this case, the
5733    --    discriminants are said to be "inherited", or if unknown in the parent
5734    --    are also unknown in the derived type.
5735
5736    --  Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
5737
5738    --  o The parent subtype shall be constrained;
5739
5740    --  o If the parent type is not a tagged type, then each discriminant of
5741    --    the derived type shall be used in the constraint defining a parent
5742    --    subtype. [Implementation note: This ensures that the new discriminant
5743    --    can share storage with an existing discriminant.]
5744
5745    --  For the derived type each discriminant of the parent type is either
5746    --  inherited, constrained to equal some new discriminant of the derived
5747    --  type, or constrained to the value of an expression.
5748
5749    --  When inherited or constrained to equal some new discriminant, the
5750    --  parent discriminant and the discriminant of the derived type are said
5751    --  to "correspond".
5752
5753    --  If a discriminant of the parent type is constrained to a specific value
5754    --  in the derived type definition, then the discriminant is said to be
5755    --  "specified" by that derived type definition.
5756
5757    --  3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
5758
5759    --  We have spoken about stored discriminants in point 1 (introduction)
5760    --  above. There are two sort of stored discriminants: implicit and
5761    --  explicit. As long as the derived type inherits the same discriminants as
5762    --  the root record type, stored discriminants are the same as regular
5763    --  discriminants, and are said to be implicit. However, if any discriminant
5764    --  in the root type was renamed in the derived type, then the derived
5765    --  type will contain explicit stored discriminants. Explicit stored
5766    --  discriminants are discriminants in addition to the semantically visible
5767    --  discriminants defined for the derived type. Stored discriminants are
5768    --  used by Gigi to figure out what are the physical discriminants in
5769    --  objects of the derived type (see precise definition in einfo.ads).
5770    --  As an example, consider the following:
5771
5772    --           type R  (D1, D2, D3 : Int) is record ... end record;
5773    --           type T1 is new R;
5774    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
5775    --           type T3 is new T2;
5776    --           type T4 (Y : Int) is new T3 (Y, 99);
5777
5778    --  The following table summarizes the discriminants and stored
5779    --  discriminants in R and T1 through T4.
5780
5781    --   Type      Discrim     Stored Discrim  Comment
5782    --    R      (D1, D2, D3)   (D1, D2, D3)   Girder discrims implicit in R
5783    --    T1     (D1, D2, D3)   (D1, D2, D3)   Girder discrims implicit in T1
5784    --    T2     (X1, X2)       (D1, D2, D3)   Girder discrims EXPLICIT in T2
5785    --    T3     (X1, X2)       (D1, D2, D3)   Girder discrims EXPLICIT in T3
5786    --    T4     (Y)            (D1, D2, D3)   Girder discrims EXPLICIT in T4
5787
5788    --  Field Corresponding_Discriminant (abbreviated CD below) allows us to
5789    --  find the corresponding discriminant in the parent type, while
5790    --  Original_Record_Component (abbreviated ORC below), the actual physical
5791    --  component that is renamed. Finally the field Is_Completely_Hidden
5792    --  (abbreviated ICH below) is set for all explicit stored discriminants
5793    --  (see einfo.ads for more info). For the above example this gives:
5794
5795    --                 Discrim     CD        ORC     ICH
5796    --                 ^^^^^^^     ^^        ^^^     ^^^
5797    --                 D1 in R    empty     itself    no
5798    --                 D2 in R    empty     itself    no
5799    --                 D3 in R    empty     itself    no
5800
5801    --                 D1 in T1  D1 in R    itself    no
5802    --                 D2 in T1  D2 in R    itself    no
5803    --                 D3 in T1  D3 in R    itself    no
5804
5805    --                 X1 in T2  D3 in T1  D3 in T2   no
5806    --                 X2 in T2  D1 in T1  D1 in T2   no
5807    --                 D1 in T2   empty    itself    yes
5808    --                 D2 in T2   empty    itself    yes
5809    --                 D3 in T2   empty    itself    yes
5810
5811    --                 X1 in T3  X1 in T2  D3 in T3   no
5812    --                 X2 in T3  X2 in T2  D1 in T3   no
5813    --                 D1 in T3   empty    itself    yes
5814    --                 D2 in T3   empty    itself    yes
5815    --                 D3 in T3   empty    itself    yes
5816
5817    --                 Y  in T4  X1 in T3  D3 in T3   no
5818    --                 D1 in T3   empty    itself    yes
5819    --                 D2 in T3   empty    itself    yes
5820    --                 D3 in T3   empty    itself    yes
5821
5822    --  4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
5823
5824    --  Type derivation for tagged types is fairly straightforward. If no
5825    --  discriminants are specified by the derived type, these are inherited
5826    --  from the parent. No explicit stored discriminants are ever necessary.
5827    --  The only manipulation that is done to the tree is that of adding a
5828    --  _parent field with parent type and constrained to the same constraint
5829    --  specified for the parent in the derived type definition. For instance:
5830
5831    --           type R  (D1, D2, D3 : Int) is tagged record ... end record;
5832    --           type T1 is new R with null record;
5833    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
5834
5835    --  are changed into:
5836
5837    --           type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
5838    --              _parent : R (D1, D2, D3);
5839    --           end record;
5840
5841    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
5842    --              _parent : T1 (X2, 88, X1);
5843    --           end record;
5844
5845    --  The discriminants actually present in R, T1 and T2 as well as their CD,
5846    --  ORC and ICH fields are:
5847
5848    --                 Discrim     CD        ORC     ICH
5849    --                 ^^^^^^^     ^^        ^^^     ^^^
5850    --                 D1 in R    empty     itself    no
5851    --                 D2 in R    empty     itself    no
5852    --                 D3 in R    empty     itself    no
5853
5854    --                 D1 in T1  D1 in R    D1 in R   no
5855    --                 D2 in T1  D2 in R    D2 in R   no
5856    --                 D3 in T1  D3 in R    D3 in R   no
5857
5858    --                 X1 in T2  D3 in T1   D3 in R   no
5859    --                 X2 in T2  D1 in T1   D1 in R   no
5860
5861    --  5. FIRST TRANSFORMATION FOR DERIVED RECORDS
5862    --
5863    --  Regardless of whether we dealing with a tagged or untagged type
5864    --  we will transform all derived type declarations of the form
5865    --
5866    --               type T is new R (...) [with ...];
5867    --  or
5868    --               subtype S is R (...);
5869    --               type T is new S [with ...];
5870    --  into
5871    --               type BT is new R [with ...];
5872    --               subtype T is BT (...);
5873    --
5874    --  That is, the base derived type is constrained only if it has no
5875    --  discriminants. The reason for doing this is that GNAT's semantic model
5876    --  assumes that a base type with discriminants is unconstrained.
5877    --
5878    --  Note that, strictly speaking, the above transformation is not always
5879    --  correct. Consider for instance the following excerpt from ACVC b34011a:
5880    --
5881    --       procedure B34011A is
5882    --          type REC (D : integer := 0) is record
5883    --             I : Integer;
5884    --          end record;
5885
5886    --          package P is
5887    --             type T6 is new Rec;
5888    --             function F return T6;
5889    --          end P;
5890
5891    --          use P;
5892    --          package Q6 is
5893    --             type U is new T6 (Q6.F.I);                   -- ERROR: Q6.F.
5894    --          end Q6;
5895    --
5896    --  The definition of Q6.U is illegal. However transforming Q6.U into
5897
5898    --             type BaseU is new T6;
5899    --             subtype U is BaseU (Q6.F.I)
5900
5901    --  turns U into a legal subtype, which is incorrect. To avoid this problem
5902    --  we always analyze the constraint (in this case (Q6.F.I)) before applying
5903    --  the transformation described above.
5904
5905    --  There is another instance where the above transformation is incorrect.
5906    --  Consider:
5907
5908    --          package Pack is
5909    --             type Base (D : Integer) is tagged null record;
5910    --             procedure P (X : Base);
5911
5912    --             type Der is new Base (2) with null record;
5913    --             procedure P (X : Der);
5914    --          end Pack;
5915
5916    --  Then the above transformation turns this into
5917
5918    --             type Der_Base is new Base with null record;
5919    --             --  procedure P (X : Base) is implicitly inherited here
5920    --             --  as procedure P (X : Der_Base).
5921
5922    --             subtype Der is Der_Base (2);
5923    --             procedure P (X : Der);
5924    --             --  The overriding of P (X : Der_Base) is illegal since we
5925    --             --  have a parameter conformance problem.
5926
5927    --  To get around this problem, after having semantically processed Der_Base
5928    --  and the rewritten subtype declaration for Der, we copy Der_Base field
5929    --  Discriminant_Constraint from Der so that when parameter conformance is
5930    --  checked when P is overridden, no semantic errors are flagged.
5931
5932    --  6. SECOND TRANSFORMATION FOR DERIVED RECORDS
5933
5934    --  Regardless of whether we are dealing with a tagged or untagged type
5935    --  we will transform all derived type declarations of the form
5936
5937    --               type R (D1, .., Dn : ...) is [tagged] record ...;
5938    --               type T is new R [with ...];
5939    --  into
5940    --               type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
5941
5942    --  The reason for such transformation is that it allows us to implement a
5943    --  very clean form of component inheritance as explained below.
5944
5945    --  Note that this transformation is not achieved by direct tree rewriting
5946    --  and manipulation, but rather by redoing the semantic actions that the
5947    --  above transformation will entail. This is done directly in routine
5948    --  Inherit_Components.
5949
5950    --  7. TYPE DERIVATION AND COMPONENT INHERITANCE
5951
5952    --  In both tagged and untagged derived types, regular non discriminant
5953    --  components are inherited in the derived type from the parent type. In
5954    --  the absence of discriminants component, inheritance is straightforward
5955    --  as components can simply be copied from the parent.
5956
5957    --  If the parent has discriminants, inheriting components constrained with
5958    --  these discriminants requires caution. Consider the following example:
5959
5960    --      type R  (D1, D2 : Positive) is [tagged] record
5961    --         S : String (D1 .. D2);
5962    --      end record;
5963
5964    --      type T1                is new R        [with null record];
5965    --      type T2 (X : positive) is new R (1, X) [with null record];
5966
5967    --  As explained in 6. above, T1 is rewritten as
5968    --      type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
5969    --  which makes the treatment for T1 and T2 identical.
5970
5971    --  What we want when inheriting S, is that references to D1 and D2 in R are
5972    --  replaced with references to their correct constraints, i.e. D1 and D2 in
5973    --  T1 and 1 and X in T2. So all R's discriminant references are replaced
5974    --  with either discriminant references in the derived type or expressions.
5975    --  This replacement is achieved as follows: before inheriting R's
5976    --  components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
5977    --  created in the scope of T1 (resp. scope of T2) so that discriminants D1
5978    --  and D2 of T1 are visible (resp. discriminant X of T2 is visible).
5979    --  For T2, for instance, this has the effect of replacing String (D1 .. D2)
5980    --  by String (1 .. X).
5981
5982    --  8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
5983
5984    --  We explain here the rules governing private type extensions relevant to
5985    --  type derivation. These rules are explained on the following example:
5986
5987    --      type D [(...)] is new A [(...)] with private;      <-- partial view
5988    --      type D [(...)] is new P [(...)] with null record;  <-- full view
5989
5990    --  Type A is called the ancestor subtype of the private extension.
5991    --  Type P is the parent type of the full view of the private extension. It
5992    --  must be A or a type derived from A.
5993
5994    --  The rules concerning the discriminants of private type extensions are
5995    --  [7.3(10-13)]:
5996
5997    --  o If a private extension inherits known discriminants from the ancestor
5998    --    subtype, then the full view shall also inherit its discriminants from
5999    --    the ancestor subtype and the parent subtype of the full view shall be
6000    --    constrained if and only if the ancestor subtype is constrained.
6001
6002    --  o If a partial view has unknown discriminants, then the full view may
6003    --    define a definite or an indefinite subtype, with or without
6004    --    discriminants.
6005
6006    --  o If a partial view has neither known nor unknown discriminants, then
6007    --    the full view shall define a definite subtype.
6008
6009    --  o If the ancestor subtype of a private extension has constrained
6010    --    discriminants, then the parent subtype of the full view shall impose a
6011    --    statically matching constraint on those discriminants.
6012
6013    --  This means that only the following forms of private extensions are
6014    --  allowed:
6015
6016    --      type D is new A with private;      <-- partial view
6017    --      type D is new P with null record;  <-- full view
6018
6019    --  If A has no discriminants than P has no discriminants, otherwise P must
6020    --  inherit A's discriminants.
6021
6022    --      type D is new A (...) with private;      <-- partial view
6023    --      type D is new P (:::) with null record;  <-- full view
6024
6025    --  P must inherit A's discriminants and (...) and (:::) must statically
6026    --  match.
6027
6028    --      subtype A is R (...);
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 have inherited R's discriminants and must be derived from A or
6033    --  any of its subtypes.
6034
6035    --      type D (..) is new A with private;              <-- partial view
6036    --      type D (..) is new P [(:::)] with null record;  <-- full view
6037
6038    --  No specific constraints on P's discriminants or constraint (:::).
6039    --  Note that A can be unconstrained, but the parent subtype P must either
6040    --  be constrained or (:::) must be present.
6041
6042    --      type D (..) is new A [(...)] with private;      <-- partial view
6043    --      type D (..) is new P [(:::)] with null record;  <-- full view
6044
6045    --  P's constraints on A's discriminants must statically match those
6046    --  imposed by (...).
6047
6048    --  9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
6049
6050    --  The full view of a private extension is handled exactly as described
6051    --  above. The model chose for the private view of a private extension is
6052    --  the same for what concerns discriminants (i.e. they receive the same
6053    --  treatment as in the tagged case). However, the private view of the
6054    --  private extension always inherits the components of the parent base,
6055    --  without replacing any discriminant reference. Strictly speaking this is
6056    --  incorrect. However, Gigi never uses this view to generate code so this
6057    --  is a purely semantic issue. In theory, a set of transformations similar
6058    --  to those given in 5. and 6. above could be applied to private views of
6059    --  private extensions to have the same model of component inheritance as
6060    --  for non private extensions. However, this is not done because it would
6061    --  further complicate private type processing. Semantically speaking, this
6062    --  leaves us in an uncomfortable situation. As an example consider:
6063
6064    --          package Pack is
6065    --             type R (D : integer) is tagged record
6066    --                S : String (1 .. D);
6067    --             end record;
6068    --             procedure P (X : R);
6069    --             type T is new R (1) with private;
6070    --          private
6071    --             type T is new R (1) with null record;
6072    --          end;
6073
6074    --  This is transformed into:
6075
6076    --          package Pack is
6077    --             type R (D : integer) is tagged record
6078    --                S : String (1 .. D);
6079    --             end record;
6080    --             procedure P (X : R);
6081    --             type T is new R (1) with private;
6082    --          private
6083    --             type BaseT is new R with null record;
6084    --             subtype  T is BaseT (1);
6085    --          end;
6086
6087    --  (strictly speaking the above is incorrect Ada)
6088
6089    --  From the semantic standpoint the private view of private extension T
6090    --  should be flagged as constrained since one can clearly have
6091    --
6092    --             Obj : T;
6093    --
6094    --  in a unit withing Pack. However, when deriving subprograms for the
6095    --  private view of private extension T, T must be seen as unconstrained
6096    --  since T has discriminants (this is a constraint of the current
6097    --  subprogram derivation model). Thus, when processing the private view of
6098    --  a private extension such as T, we first mark T as unconstrained, we
6099    --  process it, we perform program derivation and just before returning from
6100    --  Build_Derived_Record_Type we mark T as constrained.
6101
6102    --  ??? Are there are other uncomfortable cases that we will have to
6103    --      deal with.
6104
6105    --  10. RECORD_TYPE_WITH_PRIVATE complications
6106
6107    --  Types that are derived from a visible record type and have a private
6108    --  extension present other peculiarities. They behave mostly like private
6109    --  types, but if they have primitive operations defined, these will not
6110    --  have the proper signatures for further inheritance, because other
6111    --  primitive operations will use the implicit base that we define for
6112    --  private derivations below. This affect subprogram inheritance (see
6113    --  Derive_Subprograms for details). We also derive the implicit base from
6114    --  the base type of the full view, so that the implicit base is a record
6115    --  type and not another private type, This avoids infinite loops.
6116
6117    procedure Build_Derived_Record_Type
6118      (N            : Node_Id;
6119       Parent_Type  : Entity_Id;
6120       Derived_Type : Entity_Id;
6121       Derive_Subps : Boolean := True)
6122    is
6123       Loc          : constant Source_Ptr := Sloc (N);
6124       Parent_Base  : Entity_Id;
6125       Type_Def     : Node_Id;
6126       Indic        : Node_Id;
6127       Discrim      : Entity_Id;
6128       Last_Discrim : Entity_Id;
6129       Constrs      : Elist_Id;
6130
6131       Discs : Elist_Id := New_Elmt_List;
6132       --  An empty Discs list means that there were no constraints in the
6133       --  subtype indication or that there was an error processing it.
6134
6135       Assoc_List : Elist_Id;
6136       New_Discrs : Elist_Id;
6137       New_Base   : Entity_Id;
6138       New_Decl   : Node_Id;
6139       New_Indic  : Node_Id;
6140
6141       Is_Tagged          : constant Boolean := Is_Tagged_Type (Parent_Type);
6142       Discriminant_Specs : constant Boolean :=
6143                              Present (Discriminant_Specifications (N));
6144       Private_Extension  : constant Boolean :=
6145                              Nkind (N) = N_Private_Extension_Declaration;
6146
6147       Constraint_Present : Boolean;
6148       Inherit_Discrims   : Boolean := False;
6149       Save_Etype         : Entity_Id;
6150       Save_Discr_Constr  : Elist_Id;
6151       Save_Next_Entity   : Entity_Id;
6152
6153    begin
6154       if Ekind (Parent_Type) = E_Record_Type_With_Private
6155         and then Present (Full_View (Parent_Type))
6156         and then Has_Discriminants (Parent_Type)
6157       then
6158          Parent_Base := Base_Type (Full_View (Parent_Type));
6159       else
6160          Parent_Base := Base_Type (Parent_Type);
6161       end if;
6162
6163       --  Before we start the previously documented transformations, here is
6164       --  little fix for size and alignment of tagged types. Normally when we
6165       --  derive type D from type P, we copy the size and alignment of P as the
6166       --  default for D, and in the absence of explicit representation clauses
6167       --  for D, the size and alignment are indeed the same as the parent.
6168
6169       --  But this is wrong for tagged types, since fields may be added, and
6170       --  the default size may need to be larger, and the default alignment may
6171       --  need to be larger.
6172
6173       --  We therefore reset the size and alignment fields in the tagged case.
6174       --  Note that the size and alignment will in any case be at least as
6175       --  large as the parent type (since the derived type has a copy of the
6176       --  parent type in the _parent field)
6177
6178       --  The type is also marked as being tagged here, which is needed when
6179       --  processing components with a self-referential anonymous access type
6180       --  in the call to Check_Anonymous_Access_Components below. Note that
6181       --  this flag is also set later on for completeness.
6182
6183       if Is_Tagged then
6184          Set_Is_Tagged_Type (Derived_Type);
6185          Init_Size_Align    (Derived_Type);
6186       end if;
6187
6188       --  STEP 0a: figure out what kind of derived type declaration we have
6189
6190       if Private_Extension then
6191          Type_Def := N;
6192          Set_Ekind (Derived_Type, E_Record_Type_With_Private);
6193
6194       else
6195          Type_Def := Type_Definition (N);
6196
6197          --  Ekind (Parent_Base) is not necessarily E_Record_Type since
6198          --  Parent_Base can be a private type or private extension. However,
6199          --  for tagged types with an extension the newly added fields are
6200          --  visible and hence the Derived_Type is always an E_Record_Type.
6201          --  (except that the parent may have its own private fields).
6202          --  For untagged types we preserve the Ekind of the Parent_Base.
6203
6204          if Present (Record_Extension_Part (Type_Def)) then
6205             Set_Ekind (Derived_Type, E_Record_Type);
6206
6207             --  Create internal access types for components with anonymous
6208             --  access types.
6209
6210             if Ada_Version >= Ada_05 then
6211                Check_Anonymous_Access_Components
6212                  (N, Derived_Type, Derived_Type,
6213                    Component_List (Record_Extension_Part (Type_Def)));
6214             end if;
6215
6216          else
6217             Set_Ekind (Derived_Type, Ekind (Parent_Base));
6218          end if;
6219       end if;
6220
6221       --  Indic can either be an N_Identifier if the subtype indication
6222       --  contains no constraint or an N_Subtype_Indication if the subtype
6223       --  indication has a constraint.
6224
6225       Indic := Subtype_Indication (Type_Def);
6226       Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
6227
6228       --  Check that the type has visible discriminants. The type may be
6229       --  a private type with unknown discriminants whose full view has
6230       --  discriminants which are invisible.
6231
6232       if Constraint_Present then
6233          if not Has_Discriminants (Parent_Base)
6234            or else
6235              (Has_Unknown_Discriminants (Parent_Base)
6236                 and then Is_Private_Type (Parent_Base))
6237          then
6238             Error_Msg_N
6239               ("invalid constraint: type has no discriminant",
6240                  Constraint (Indic));
6241
6242             Constraint_Present := False;
6243             Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6244
6245          elsif Is_Constrained (Parent_Type) then
6246             Error_Msg_N
6247                ("invalid constraint: parent type is already constrained",
6248                   Constraint (Indic));
6249
6250             Constraint_Present := False;
6251             Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6252          end if;
6253       end if;
6254
6255       --  STEP 0b: If needed, apply transformation given in point 5. above
6256
6257       if not Private_Extension
6258         and then Has_Discriminants (Parent_Type)
6259         and then not Discriminant_Specs
6260         and then (Is_Constrained (Parent_Type) or else Constraint_Present)
6261       then
6262          --  First, we must analyze the constraint (see comment in point 5.)
6263
6264          if Constraint_Present then
6265             New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
6266
6267             if Has_Discriminants (Derived_Type)
6268               and then Has_Private_Declaration (Derived_Type)
6269               and then Present (Discriminant_Constraint (Derived_Type))
6270             then
6271                --  Verify that constraints of the full view conform to those
6272                --  given in partial view.
6273
6274                declare
6275                   C1, C2 : Elmt_Id;
6276
6277                begin
6278                   C1 := First_Elmt (New_Discrs);
6279                   C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
6280                   while Present (C1) and then Present (C2) loop
6281                      if not
6282                        Fully_Conformant_Expressions (Node (C1), Node (C2))
6283                      then
6284                         Error_Msg_N (
6285                           "constraint not conformant to previous declaration",
6286                              Node (C1));
6287                      end if;
6288
6289                      Next_Elmt (C1);
6290                      Next_Elmt (C2);
6291                   end loop;
6292                end;
6293             end if;
6294          end if;
6295
6296          --  Insert and analyze the declaration for the unconstrained base type
6297
6298          New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
6299
6300          New_Decl :=
6301            Make_Full_Type_Declaration (Loc,
6302               Defining_Identifier => New_Base,
6303               Type_Definition     =>
6304                 Make_Derived_Type_Definition (Loc,
6305                   Abstract_Present      => Abstract_Present (Type_Def),
6306                   Subtype_Indication    =>
6307                     New_Occurrence_Of (Parent_Base, Loc),
6308                   Record_Extension_Part =>
6309                     Relocate_Node (Record_Extension_Part (Type_Def))));
6310
6311          Set_Parent (New_Decl, Parent (N));
6312          Mark_Rewrite_Insertion (New_Decl);
6313          Insert_Before (N, New_Decl);
6314
6315          --  Note that this call passes False for the Derive_Subps parameter
6316          --  because subprogram derivation is deferred until after creating
6317          --  the subtype (see below).
6318
6319          Build_Derived_Type
6320            (New_Decl, Parent_Base, New_Base,
6321             Is_Completion => True, Derive_Subps => False);
6322
6323          --  ??? This needs re-examination to determine whether the
6324          --  above call can simply be replaced by a call to Analyze.
6325
6326          Set_Analyzed (New_Decl);
6327
6328          --  Insert and analyze the declaration for the constrained subtype
6329
6330          if Constraint_Present then
6331             New_Indic :=
6332               Make_Subtype_Indication (Loc,
6333                 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6334                 Constraint   => Relocate_Node (Constraint (Indic)));
6335
6336          else
6337             declare
6338                Constr_List : constant List_Id := New_List;
6339                C           : Elmt_Id;
6340                Expr        : Node_Id;
6341
6342             begin
6343                C := First_Elmt (Discriminant_Constraint (Parent_Type));
6344                while Present (C) loop
6345                   Expr := Node (C);
6346
6347                   --  It is safe here to call New_Copy_Tree since
6348                   --  Force_Evaluation was called on each constraint in
6349                   --  Build_Discriminant_Constraints.
6350
6351                   Append (New_Copy_Tree (Expr), To => Constr_List);
6352
6353                   Next_Elmt (C);
6354                end loop;
6355
6356                New_Indic :=
6357                  Make_Subtype_Indication (Loc,
6358                    Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6359                    Constraint   =>
6360                      Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
6361             end;
6362          end if;
6363
6364          Rewrite (N,
6365            Make_Subtype_Declaration (Loc,
6366              Defining_Identifier => Derived_Type,
6367              Subtype_Indication  => New_Indic));
6368
6369          Analyze (N);
6370
6371          --  Derivation of subprograms must be delayed until the full subtype
6372          --  has been established to ensure proper overriding of subprograms
6373          --  inherited by full types. If the derivations occurred as part of
6374          --  the call to Build_Derived_Type above, then the check for type
6375          --  conformance would fail because earlier primitive subprograms
6376          --  could still refer to the full type prior the change to the new
6377          --  subtype and hence would not match the new base type created here.
6378
6379          Derive_Subprograms (Parent_Type, Derived_Type);
6380
6381          --  For tagged types the Discriminant_Constraint of the new base itype
6382          --  is inherited from the first subtype so that no subtype conformance
6383          --  problem arise when the first subtype overrides primitive
6384          --  operations inherited by the implicit base type.
6385
6386          if Is_Tagged then
6387             Set_Discriminant_Constraint
6388               (New_Base, Discriminant_Constraint (Derived_Type));
6389          end if;
6390
6391          return;
6392       end if;
6393
6394       --  If we get here Derived_Type will have no discriminants or it will be
6395       --  a discriminated unconstrained base type.
6396
6397       --  STEP 1a: perform preliminary actions/checks for derived tagged types
6398
6399       if Is_Tagged then
6400
6401          --  The parent type is frozen for non-private extensions (RM 13.14(7))
6402          --  The declaration of a specific descendant of an interface type
6403          --  freezes the interface type (RM 13.14).
6404
6405          if not Private_Extension
6406            or else Is_Interface (Parent_Base)
6407          then
6408             Freeze_Before (N, Parent_Type);
6409          end if;
6410
6411          --  In Ada 2005 (AI-344), the restriction that a derived tagged type
6412          --  cannot be declared at a deeper level than its parent type is
6413          --  removed. The check on derivation within a generic body is also
6414          --  relaxed, but there's a restriction that a derived tagged type
6415          --  cannot be declared in a generic body if it's derived directly
6416          --  or indirectly from a formal type of that generic.
6417
6418          if Ada_Version >= Ada_05 then
6419             if Present (Enclosing_Generic_Body (Derived_Type)) then
6420                declare
6421                   Ancestor_Type : Entity_Id;
6422
6423                begin
6424                   --  Check to see if any ancestor of the derived type is a
6425                   --  formal type.
6426
6427                   Ancestor_Type := Parent_Type;
6428                   while not Is_Generic_Type (Ancestor_Type)
6429                     and then Etype (Ancestor_Type) /= Ancestor_Type
6430                   loop
6431                      Ancestor_Type := Etype (Ancestor_Type);
6432                   end loop;
6433
6434                   --  If the derived type does have a formal type as an
6435                   --  ancestor, then it's an error if the derived type is
6436                   --  declared within the body of the generic unit that
6437                   --  declares the formal type in its generic formal part. It's
6438                   --  sufficient to check whether the ancestor type is declared
6439                   --  inside the same generic body as the derived type (such as
6440                   --  within a nested generic spec), in which case the
6441                   --  derivation is legal. If the formal type is declared
6442                   --  outside of that generic body, then it's guaranteed that
6443                   --  the derived type is declared within the generic body of
6444                   --  the generic unit declaring the formal type.
6445
6446                   if Is_Generic_Type (Ancestor_Type)
6447                     and then Enclosing_Generic_Body (Ancestor_Type) /=
6448                                Enclosing_Generic_Body (Derived_Type)
6449                   then
6450                      Error_Msg_NE
6451                        ("parent type of& must not be descendant of formal type"
6452                           & " of an enclosing generic body",
6453                             Indic, Derived_Type);
6454                   end if;
6455                end;
6456             end if;
6457
6458          elsif Type_Access_Level (Derived_Type) /=
6459                  Type_Access_Level (Parent_Type)
6460            and then not Is_Generic_Type (Derived_Type)
6461          then
6462             if Is_Controlled (Parent_Type) then
6463                Error_Msg_N
6464                  ("controlled type must be declared at the library level",
6465                   Indic);
6466             else
6467                Error_Msg_N
6468                  ("type extension at deeper accessibility level than parent",
6469                   Indic);
6470             end if;
6471
6472          else
6473             declare
6474                GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
6475
6476             begin
6477                if Present (GB)
6478                  and then GB /= Enclosing_Generic_Body (Parent_Base)
6479                then
6480                   Error_Msg_NE
6481                     ("parent type of& must not be outside generic body"
6482                        & " (RM 3.9.1(4))",
6483                          Indic, Derived_Type);
6484                end if;
6485             end;
6486          end if;
6487       end if;
6488
6489       --  Ada 2005 (AI-251)
6490
6491       if Ada_Version = Ada_05
6492         and then Is_Tagged
6493       then
6494          --  "The declaration of a specific descendant of an interface type
6495          --  freezes the interface type" (RM 13.14).
6496
6497          declare
6498             Iface : Node_Id;
6499          begin
6500             if Is_Non_Empty_List (Interface_List (Type_Def)) then
6501                Iface := First (Interface_List (Type_Def));
6502                while Present (Iface) loop
6503                   Freeze_Before (N, Etype (Iface));
6504                   Next (Iface);
6505                end loop;
6506             end if;
6507          end;
6508       end if;
6509
6510       --  STEP 1b : preliminary cleanup of the full view of private types
6511
6512       --  If the type is already marked as having discriminants, then it's the
6513       --  completion of a private type or private extension and we need to
6514       --  retain the discriminants from the partial view if the current
6515       --  declaration has Discriminant_Specifications so that we can verify
6516       --  conformance. However, we must remove any existing components that
6517       --  were inherited from the parent (and attached in Copy_And_Swap)
6518       --  because the full type inherits all appropriate components anyway, and
6519       --  we do not want the partial view's components interfering.
6520
6521       if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
6522          Discrim := First_Discriminant (Derived_Type);
6523          loop
6524             Last_Discrim := Discrim;
6525             Next_Discriminant (Discrim);
6526             exit when No (Discrim);
6527          end loop;
6528
6529          Set_Last_Entity (Derived_Type, Last_Discrim);
6530
6531       --  In all other cases wipe out the list of inherited components (even
6532       --  inherited discriminants), it will be properly rebuilt here.
6533
6534       else
6535          Set_First_Entity (Derived_Type, Empty);
6536          Set_Last_Entity  (Derived_Type, Empty);
6537       end if;
6538
6539       --  STEP 1c: Initialize some flags for the Derived_Type
6540
6541       --  The following flags must be initialized here so that
6542       --  Process_Discriminants can check that discriminants of tagged types do
6543       --  not have a default initial value and that access discriminants are
6544       --  only specified for limited records. For completeness, these flags are
6545       --  also initialized along with all the other flags below.
6546
6547       --  AI-419: Limitedness is not inherited from an interface parent, so to
6548       --  be limited in that case the type must be explicitly declared as
6549       --  limited. However, task and protected interfaces are always limited.
6550
6551       if Limited_Present (Type_Def) then
6552          Set_Is_Limited_Record (Derived_Type);
6553
6554       elsif Is_Limited_Record (Parent_Type)
6555         or else (Present (Full_View (Parent_Type))
6556                    and then Is_Limited_Record (Full_View (Parent_Type)))
6557       then
6558          if not Is_Interface (Parent_Type)
6559            or else Is_Synchronized_Interface (Parent_Type)
6560            or else Is_Protected_Interface (Parent_Type)
6561            or else Is_Task_Interface (Parent_Type)
6562          then
6563             Set_Is_Limited_Record (Derived_Type);
6564          end if;
6565       end if;
6566
6567       --  STEP 2a: process discriminants of derived type if any
6568
6569       Push_Scope (Derived_Type);
6570
6571       if Discriminant_Specs then
6572          Set_Has_Unknown_Discriminants (Derived_Type, False);
6573
6574          --  The following call initializes fields Has_Discriminants and
6575          --  Discriminant_Constraint, unless we are processing the completion
6576          --  of a private type declaration.
6577
6578          Check_Or_Process_Discriminants (N, Derived_Type);
6579
6580          --  For non-tagged types the constraint on the Parent_Type must be
6581          --  present and is used to rename the discriminants.
6582
6583          if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
6584             Error_Msg_N ("untagged parent must have discriminants", Indic);
6585
6586          elsif not Is_Tagged and then not Constraint_Present then
6587             Error_Msg_N
6588               ("discriminant constraint needed for derived untagged records",
6589                Indic);
6590
6591          --  Otherwise the parent subtype must be constrained unless we have a
6592          --  private extension.
6593
6594          elsif not Constraint_Present
6595            and then not Private_Extension
6596            and then not Is_Constrained (Parent_Type)
6597          then
6598             Error_Msg_N
6599               ("unconstrained type not allowed in this context", Indic);
6600
6601          elsif Constraint_Present then
6602             --  The following call sets the field Corresponding_Discriminant
6603             --  for the discriminants in the Derived_Type.
6604
6605             Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
6606
6607             --  For untagged types all new discriminants must rename
6608             --  discriminants in the parent. For private extensions new
6609             --  discriminants cannot rename old ones (implied by [7.3(13)]).
6610
6611             Discrim := First_Discriminant (Derived_Type);
6612             while Present (Discrim) loop
6613                if not Is_Tagged
6614                  and then No (Corresponding_Discriminant (Discrim))
6615                then
6616                   Error_Msg_N
6617                     ("new discriminants must constrain old ones", Discrim);
6618
6619                elsif Private_Extension
6620                  and then Present (Corresponding_Discriminant (Discrim))
6621                then
6622                   Error_Msg_N
6623                     ("only static constraints allowed for parent"
6624                      & " discriminants in the partial view", Indic);
6625                   exit;
6626                end if;
6627
6628                --  If a new discriminant is used in the constraint, then its
6629                --  subtype must be statically compatible with the parent
6630                --  discriminant's subtype (3.7(15)).
6631
6632                if Present (Corresponding_Discriminant (Discrim))
6633                  and then
6634                    not Subtypes_Statically_Compatible
6635                          (Etype (Discrim),
6636                           Etype (Corresponding_Discriminant (Discrim)))
6637                then
6638                   Error_Msg_N
6639                     ("subtype must be compatible with parent discriminant",
6640                      Discrim);
6641                end if;
6642
6643                Next_Discriminant (Discrim);
6644             end loop;
6645
6646             --  Check whether the constraints of the full view statically
6647             --  match those imposed by the parent subtype [7.3(13)].
6648
6649             if Present (Stored_Constraint (Derived_Type)) then
6650                declare
6651                   C1, C2 : Elmt_Id;
6652
6653                begin
6654                   C1 := First_Elmt (Discs);
6655                   C2 := First_Elmt (Stored_Constraint (Derived_Type));
6656                   while Present (C1) and then Present (C2) loop
6657                      if not
6658                        Fully_Conformant_Expressions (Node (C1), Node (C2))
6659                      then
6660                         Error_Msg_N
6661                           ("not conformant with previous declaration",
6662                            Node (C1));
6663                      end if;
6664
6665                      Next_Elmt (C1);
6666                      Next_Elmt (C2);
6667                   end loop;
6668                end;
6669             end if;
6670          end if;
6671
6672       --  STEP 2b: No new discriminants, inherit discriminants if any
6673
6674       else
6675          if Private_Extension then
6676             Set_Has_Unknown_Discriminants
6677               (Derived_Type,
6678                Has_Unknown_Discriminants (Parent_Type)
6679                  or else Unknown_Discriminants_Present (N));
6680
6681          --  The partial view of the parent may have unknown discriminants,
6682          --  but if the full view has discriminants and the parent type is
6683          --  in scope they must be inherited.
6684
6685          elsif Has_Unknown_Discriminants (Parent_Type)
6686            and then
6687             (not Has_Discriminants (Parent_Type)
6688               or else not In_Open_Scopes (Scope (Parent_Type)))
6689          then
6690             Set_Has_Unknown_Discriminants (Derived_Type);
6691          end if;
6692
6693          if not Has_Unknown_Discriminants (Derived_Type)
6694            and then not Has_Unknown_Discriminants (Parent_Base)
6695            and then Has_Discriminants (Parent_Type)
6696          then
6697             Inherit_Discrims := True;
6698             Set_Has_Discriminants
6699               (Derived_Type, True);
6700             Set_Discriminant_Constraint
6701               (Derived_Type, Discriminant_Constraint (Parent_Base));
6702          end if;
6703
6704          --  The following test is true for private types (remember
6705          --  transformation 5. is not applied to those) and in an error
6706          --  situation.
6707
6708          if Constraint_Present then
6709             Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
6710          end if;
6711
6712          --  For now mark a new derived type as constrained only if it has no
6713          --  discriminants. At the end of Build_Derived_Record_Type we properly
6714          --  set this flag in the case of private extensions. See comments in
6715          --  point 9. just before body of Build_Derived_Record_Type.
6716
6717          Set_Is_Constrained
6718            (Derived_Type,
6719             not (Inherit_Discrims
6720                    or else Has_Unknown_Discriminants (Derived_Type)));
6721       end if;
6722
6723       --  STEP 3: initialize fields of derived type
6724
6725       Set_Is_Tagged_Type    (Derived_Type, Is_Tagged);
6726       Set_Stored_Constraint (Derived_Type, No_Elist);
6727
6728       --  Ada 2005 (AI-251): Private type-declarations can implement interfaces
6729       --  but cannot be interfaces
6730
6731       if not Private_Extension
6732          and then Ekind (Derived_Type) /= E_Private_Type
6733          and then Ekind (Derived_Type) /= E_Limited_Private_Type
6734       then
6735          if Interface_Present (Type_Def) then
6736             Analyze_Interface_Declaration (Derived_Type, Type_Def);
6737          end if;
6738
6739          Set_Abstract_Interfaces (Derived_Type, No_Elist);
6740       end if;
6741
6742       --  Fields inherited from the Parent_Type
6743
6744       Set_Discard_Names
6745         (Derived_Type, Einfo.Discard_Names      (Parent_Type));
6746       Set_Has_Specified_Layout
6747         (Derived_Type, Has_Specified_Layout     (Parent_Type));
6748       Set_Is_Limited_Composite
6749         (Derived_Type, Is_Limited_Composite     (Parent_Type));
6750       Set_Is_Private_Composite
6751         (Derived_Type, Is_Private_Composite     (Parent_Type));
6752
6753       --  Fields inherited from the Parent_Base
6754
6755       Set_Has_Controlled_Component
6756         (Derived_Type, Has_Controlled_Component (Parent_Base));
6757       Set_Has_Non_Standard_Rep
6758         (Derived_Type, Has_Non_Standard_Rep     (Parent_Base));
6759       Set_Has_Primitive_Operations
6760         (Derived_Type, Has_Primitive_Operations (Parent_Base));
6761
6762       --  Fields inherited from the Parent_Base in the non-private case
6763
6764       if Ekind (Derived_Type) = E_Record_Type then
6765          Set_Has_Complex_Representation
6766            (Derived_Type, Has_Complex_Representation (Parent_Base));
6767       end if;
6768
6769       --  Fields inherited from the Parent_Base for record types
6770
6771       if Is_Record_Type (Derived_Type) then
6772          Set_OK_To_Reorder_Components
6773            (Derived_Type, OK_To_Reorder_Components   (Parent_Base));
6774          Set_Reverse_Bit_Order
6775            (Derived_Type, Reverse_Bit_Order          (Parent_Base));
6776       end if;
6777
6778       --  Direct controlled types do not inherit Finalize_Storage_Only flag
6779
6780       if not Is_Controlled (Parent_Type) then
6781          Set_Finalize_Storage_Only
6782            (Derived_Type, Finalize_Storage_Only (Parent_Type));
6783       end if;
6784
6785       --  Set fields for private derived types
6786
6787       if Is_Private_Type (Derived_Type) then
6788          Set_Depends_On_Private (Derived_Type, True);
6789          Set_Private_Dependents (Derived_Type, New_Elmt_List);
6790
6791       --  Inherit fields from non private record types. If this is the
6792       --  completion of a derivation from a private type, the parent itself
6793       --  is private, and the attributes come from its full view, which must
6794       --  be present.
6795
6796       else
6797          if Is_Private_Type (Parent_Base)
6798            and then not Is_Record_Type (Parent_Base)
6799          then
6800             Set_Component_Alignment
6801               (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
6802             Set_C_Pass_By_Copy
6803               (Derived_Type, C_Pass_By_Copy      (Full_View (Parent_Base)));
6804          else
6805             Set_Component_Alignment
6806               (Derived_Type, Component_Alignment (Parent_Base));
6807
6808             Set_C_Pass_By_Copy
6809               (Derived_Type, C_Pass_By_Copy      (Parent_Base));
6810          end if;
6811       end if;
6812
6813       --  Set fields for tagged types
6814
6815       if Is_Tagged then
6816          Set_Primitive_Operations (Derived_Type, New_Elmt_List);
6817
6818          --  All tagged types defined in Ada.Finalization are controlled
6819
6820          if Chars (Scope (Derived_Type)) = Name_Finalization
6821            and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
6822            and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
6823          then
6824             Set_Is_Controlled (Derived_Type);
6825          else
6826             Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
6827          end if;
6828
6829          Make_Class_Wide_Type (Derived_Type);
6830          Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
6831
6832          if Has_Discriminants (Derived_Type)
6833            and then Constraint_Present
6834          then
6835             Set_Stored_Constraint
6836               (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
6837          end if;
6838
6839          if Ada_Version >= Ada_05 then
6840             declare
6841                Ifaces_List : Elist_Id;
6842
6843             begin
6844                --  Checks rules 3.9.4 (13/2 and 14/2)
6845
6846                if Comes_From_Source (Derived_Type)
6847                  and then not Is_Private_Type (Derived_Type)
6848                  and then Is_Interface (Parent_Type)
6849                  and then not Is_Interface (Derived_Type)
6850                then
6851                   if Is_Task_Interface (Parent_Type) then
6852                      Error_Msg_N
6853                        ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
6854                         Derived_Type);
6855
6856                   elsif Is_Protected_Interface (Parent_Type) then
6857                      Error_Msg_N
6858                        ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
6859                         Derived_Type);
6860                   end if;
6861                end if;
6862
6863                --  Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
6864
6865                Check_Abstract_Interfaces (N, Type_Def);
6866
6867                --  Ada 2005 (AI-251): Collect the list of progenitors that are
6868                --  not already in the parents.
6869
6870                Collect_Abstract_Interfaces
6871                  (T                         => Derived_Type,
6872                   Ifaces_List               => Ifaces_List,
6873                   Exclude_Parent_Interfaces => True);
6874                Set_Abstract_Interfaces (Derived_Type, Ifaces_List);
6875             end;
6876          end if;
6877
6878       else
6879          Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
6880          Set_Has_Non_Standard_Rep
6881                        (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
6882       end if;
6883
6884       --  STEP 4: Inherit components from the parent base and constrain them.
6885       --          Apply the second transformation described in point 6. above.
6886
6887       if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
6888         or else not Has_Discriminants (Parent_Type)
6889         or else not Is_Constrained (Parent_Type)
6890       then
6891          Constrs := Discs;
6892       else
6893          Constrs := Discriminant_Constraint (Parent_Type);
6894       end if;
6895
6896       Assoc_List :=
6897         Inherit_Components
6898           (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
6899
6900       --  STEP 5a: Copy the parent record declaration for untagged types
6901
6902       if not Is_Tagged then
6903
6904          --  Discriminant_Constraint (Derived_Type) has been properly
6905          --  constructed. Save it and temporarily set it to Empty because we
6906          --  do not want the call to New_Copy_Tree below to mess this list.
6907
6908          if Has_Discriminants (Derived_Type) then
6909             Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
6910             Set_Discriminant_Constraint (Derived_Type, No_Elist);
6911          else
6912             Save_Discr_Constr := No_Elist;
6913          end if;
6914
6915          --  Save the Etype field of Derived_Type. It is correctly set now,
6916          --  but the call to New_Copy tree may remap it to point to itself,
6917          --  which is not what we want. Ditto for the Next_Entity field.
6918
6919          Save_Etype       := Etype (Derived_Type);
6920          Save_Next_Entity := Next_Entity (Derived_Type);
6921
6922          --  Assoc_List maps all stored discriminants in the Parent_Base to
6923          --  stored discriminants in the Derived_Type. It is fundamental that
6924          --  no types or itypes with discriminants other than the stored
6925          --  discriminants appear in the entities declared inside
6926          --  Derived_Type, since the back end cannot deal with it.
6927
6928          New_Decl :=
6929            New_Copy_Tree
6930              (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
6931
6932          --  Restore the fields saved prior to the New_Copy_Tree call
6933          --  and compute the stored constraint.
6934
6935          Set_Etype       (Derived_Type, Save_Etype);
6936          Set_Next_Entity (Derived_Type, Save_Next_Entity);
6937
6938          if Has_Discriminants (Derived_Type) then
6939             Set_Discriminant_Constraint
6940               (Derived_Type, Save_Discr_Constr);
6941             Set_Stored_Constraint
6942               (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
6943             Replace_Components (Derived_Type, New_Decl);
6944          end if;
6945
6946          --  Insert the new derived type declaration
6947
6948          Rewrite (N, New_Decl);
6949
6950       --  STEP 5b: Complete the processing for record extensions in generics
6951
6952       --  There is no completion for record extensions declared in the
6953       --  parameter part of a generic, so we need to complete processing for
6954       --  these generic record extensions here. The Record_Type_Definition call
6955       --  will change the Ekind of the components from E_Void to E_Component.
6956
6957       elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
6958          Record_Type_Definition (Empty, Derived_Type);
6959
6960       --  STEP 5c: Process the record extension for non private tagged types
6961
6962       elsif not Private_Extension then
6963
6964          --  Add the _parent field in the derived type
6965
6966          Expand_Record_Extension (Derived_Type, Type_Def);
6967
6968          --  Ada 2005 (AI-251): Addition of the Tag corresponding to all the
6969          --  implemented interfaces if we are in expansion mode
6970
6971          if Expander_Active
6972            and then Has_Abstract_Interfaces (Derived_Type)
6973          then
6974             Add_Interface_Tag_Components (N, Derived_Type);
6975          end if;
6976
6977          --  Analyze the record extension
6978
6979          Record_Type_Definition
6980            (Record_Extension_Part (Type_Def), Derived_Type);
6981       end if;
6982
6983       End_Scope;
6984
6985       --  Nothing else to do if there is an error in the derivation.
6986       --  An unusual case: the full view may be derived from a type in an
6987       --  instance, when the partial view was used illegally as an actual
6988       --  in that instance, leading to a circular definition.
6989
6990       if Etype (Derived_Type) = Any_Type
6991         or else Etype (Parent_Type) = Derived_Type
6992       then
6993          return;
6994       end if;
6995
6996       --  Set delayed freeze and then derive subprograms, we need to do
6997       --  this in this order so that derived subprograms inherit the
6998       --  derived freeze if necessary.
6999
7000       Set_Has_Delayed_Freeze (Derived_Type);
7001
7002       if Derive_Subps then
7003          Derive_Subprograms (Parent_Type, Derived_Type);
7004       end if;
7005
7006       --  If we have a private extension which defines a constrained derived
7007       --  type mark as constrained here after we have derived subprograms. See
7008       --  comment on point 9. just above the body of Build_Derived_Record_Type.
7009
7010       if Private_Extension and then Inherit_Discrims then
7011          if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
7012             Set_Is_Constrained          (Derived_Type, True);
7013             Set_Discriminant_Constraint (Derived_Type, Discs);
7014
7015          elsif Is_Constrained (Parent_Type) then
7016             Set_Is_Constrained
7017               (Derived_Type, True);
7018             Set_Discriminant_Constraint
7019               (Derived_Type, Discriminant_Constraint (Parent_Type));
7020          end if;
7021       end if;
7022
7023       --  Update the class_wide type, which shares the now-completed
7024       --  entity list with its specific type.
7025
7026       if Is_Tagged then
7027          Set_First_Entity
7028            (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
7029          Set_Last_Entity
7030            (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
7031       end if;
7032
7033       --  Update the scope of anonymous access types of discriminants and other
7034       --  components, to prevent scope anomalies in gigi, when the derivation
7035       --  appears in a scope nested within that of the parent.
7036
7037       declare
7038          D : Entity_Id;
7039
7040       begin
7041          D := First_Entity (Derived_Type);
7042          while Present (D) loop
7043             if Ekind (D) = E_Discriminant
7044               or else Ekind (D) = E_Component
7045             then
7046                if Is_Itype (Etype (D))
7047                   and then Ekind (Etype (D)) = E_Anonymous_Access_Type
7048                then
7049                   Set_Scope (Etype (D), Current_Scope);
7050                end if;
7051             end if;
7052
7053             Next_Entity (D);
7054          end loop;
7055       end;
7056    end Build_Derived_Record_Type;
7057
7058    ------------------------
7059    -- Build_Derived_Type --
7060    ------------------------
7061
7062    procedure Build_Derived_Type
7063      (N             : Node_Id;
7064       Parent_Type   : Entity_Id;
7065       Derived_Type  : Entity_Id;
7066       Is_Completion : Boolean;
7067       Derive_Subps  : Boolean := True)
7068    is
7069       Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7070
7071    begin
7072       --  Set common attributes
7073
7074       Set_Scope         (Derived_Type, Current_Scope);
7075
7076       Set_Ekind         (Derived_Type, Ekind    (Parent_Base));
7077       Set_Etype         (Derived_Type,           Parent_Base);
7078       Set_Has_Task      (Derived_Type, Has_Task (Parent_Base));
7079
7080       Set_Size_Info     (Derived_Type,                Parent_Type);
7081       Set_RM_Size       (Derived_Type, RM_Size       (Parent_Type));
7082       Set_Convention    (Derived_Type, Convention    (Parent_Type));
7083       Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7084
7085       --  The derived type inherits the representation clauses of the parent.
7086       --  However, for a private type that is completed by a derivation, there
7087       --  may be operation attributes that have been specified already (stream
7088       --  attributes and External_Tag) and those must be provided. Finally,
7089       --  if the partial view is a private extension, the representation items
7090       --  of the parent have been inherited already, and should not be chained
7091       --  twice to the derived type.
7092
7093       if Is_Tagged_Type (Parent_Type)
7094         and then Present (First_Rep_Item (Derived_Type))
7095       then
7096          --  The existing items are either operational items or items inherited
7097          --  from a private extension declaration.
7098
7099          declare
7100             Rep : Node_Id;
7101             --  Used to iterate over representation items of the derived type
7102
7103             Last_Rep : Node_Id;
7104             --  Last representation item of the (non-empty) representation
7105             --  item list of the derived type.
7106
7107             Found : Boolean := False;
7108
7109          begin
7110             Rep      := First_Rep_Item (Derived_Type);
7111             Last_Rep := Rep;
7112             while Present (Rep) loop
7113                if Rep = First_Rep_Item (Parent_Type) then
7114                   Found := True;
7115                   exit;
7116
7117                else
7118                   Rep := Next_Rep_Item (Rep);
7119
7120                   if Present (Rep) then
7121                      Last_Rep := Rep;
7122                   end if;
7123                end if;
7124             end loop;
7125
7126             --  Here if we either encountered the parent type's first rep
7127             --  item on the derived type's rep item list (in which case
7128             --  Found is True, and we have nothing else to do), or if we
7129             --  reached the last rep item of the derived type, which is
7130             --  Last_Rep, in which case we further chain the parent type's
7131             --  rep items to those of the derived type.
7132
7133             if not Found then
7134                Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
7135             end if;
7136          end;
7137
7138       else
7139          Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
7140       end if;
7141
7142       case Ekind (Parent_Type) is
7143          when Numeric_Kind =>
7144             Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
7145
7146          when Array_Kind =>
7147             Build_Derived_Array_Type (N, Parent_Type,  Derived_Type);
7148
7149          when E_Record_Type
7150             | E_Record_Subtype
7151             | Class_Wide_Kind  =>
7152             Build_Derived_Record_Type
7153               (N, Parent_Type, Derived_Type, Derive_Subps);
7154             return;
7155
7156          when Enumeration_Kind =>
7157             Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
7158
7159          when Access_Kind =>
7160             Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
7161
7162          when Incomplete_Or_Private_Kind =>
7163             Build_Derived_Private_Type
7164               (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
7165
7166             --  For discriminated types, the derivation includes deriving
7167             --  primitive operations. For others it is done below.
7168
7169             if Is_Tagged_Type (Parent_Type)
7170               or else Has_Discriminants (Parent_Type)
7171               or else (Present (Full_View (Parent_Type))
7172                         and then Has_Discriminants (Full_View (Parent_Type)))
7173             then
7174                return;
7175             end if;
7176
7177          when Concurrent_Kind =>
7178             Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
7179
7180          when others =>
7181             raise Program_Error;
7182       end case;
7183
7184       if Etype (Derived_Type) = Any_Type then
7185          return;
7186       end if;
7187
7188       --  Set delayed freeze and then derive subprograms, we need to do this
7189       --  in this order so that derived subprograms inherit the derived freeze
7190       --  if necessary.
7191
7192       Set_Has_Delayed_Freeze (Derived_Type);
7193       if Derive_Subps then
7194          Derive_Subprograms (Parent_Type, Derived_Type);
7195       end if;
7196
7197       Set_Has_Primitive_Operations
7198         (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
7199    end Build_Derived_Type;
7200
7201    -----------------------
7202    -- Build_Discriminal --
7203    -----------------------
7204
7205    procedure Build_Discriminal (Discrim : Entity_Id) is
7206       D_Minal : Entity_Id;
7207       CR_Disc : Entity_Id;
7208
7209    begin
7210       --  A discriminal has the same name as the discriminant
7211
7212       D_Minal :=
7213         Make_Defining_Identifier (Sloc (Discrim),
7214           Chars => Chars (Discrim));
7215
7216       Set_Ekind     (D_Minal, E_In_Parameter);
7217       Set_Mechanism (D_Minal, Default_Mechanism);
7218       Set_Etype     (D_Minal, Etype (Discrim));
7219
7220       Set_Discriminal (Discrim, D_Minal);
7221       Set_Discriminal_Link (D_Minal, Discrim);
7222
7223       --  For task types, build at once the discriminants of the corresponding
7224       --  record, which are needed if discriminants are used in entry defaults
7225       --  and in family bounds.
7226
7227       if Is_Concurrent_Type (Current_Scope)
7228         or else Is_Limited_Type (Current_Scope)
7229       then
7230          CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
7231
7232          Set_Ekind            (CR_Disc, E_In_Parameter);
7233          Set_Mechanism        (CR_Disc, Default_Mechanism);
7234          Set_Etype            (CR_Disc, Etype (Discrim));
7235          Set_Discriminal_Link (CR_Disc, Discrim);
7236          Set_CR_Discriminant  (Discrim, CR_Disc);
7237       end if;
7238    end Build_Discriminal;
7239
7240    ------------------------------------
7241    -- Build_Discriminant_Constraints --
7242    ------------------------------------
7243
7244    function Build_Discriminant_Constraints
7245      (T           : Entity_Id;
7246       Def         : Node_Id;
7247       Derived_Def : Boolean := False) return Elist_Id
7248    is
7249       C        : constant Node_Id := Constraint (Def);
7250       Nb_Discr : constant Nat     := Number_Discriminants (T);
7251
7252       Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
7253       --  Saves the expression corresponding to a given discriminant in T
7254
7255       function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
7256       --  Return the Position number within array Discr_Expr of a discriminant
7257       --  D within the discriminant list of the discriminated type T.
7258
7259       ------------------
7260       -- Pos_Of_Discr --
7261       ------------------
7262
7263       function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
7264          Disc : Entity_Id;
7265
7266       begin
7267          Disc := First_Discriminant (T);
7268          for J in Discr_Expr'Range loop
7269             if Disc = D then
7270                return J;
7271             end if;
7272
7273             Next_Discriminant (Disc);
7274          end loop;
7275
7276          --  Note: Since this function is called on discriminants that are
7277          --  known to belong to the discriminated type, falling through the
7278          --  loop with no match signals an internal compiler error.
7279
7280          raise Program_Error;
7281       end Pos_Of_Discr;
7282
7283       --  Declarations local to Build_Discriminant_Constraints
7284
7285       Discr : Entity_Id;
7286       E     : Entity_Id;
7287       Elist : constant Elist_Id := New_Elmt_List;
7288
7289       Constr   : Node_Id;
7290       Expr     : Node_Id;
7291       Id       : Node_Id;
7292       Position : Nat;
7293       Found    : Boolean;
7294
7295       Discrim_Present : Boolean := False;
7296
7297    --  Start of processing for Build_Discriminant_Constraints
7298
7299    begin
7300       --  The following loop will process positional associations only.
7301       --  For a positional association, the (single) discriminant is
7302       --  implicitly specified by position, in textual order (RM 3.7.2).
7303
7304       Discr  := First_Discriminant (T);
7305       Constr := First (Constraints (C));
7306       for D in Discr_Expr'Range loop
7307          exit when Nkind (Constr) = N_Discriminant_Association;
7308
7309          if No (Constr) then
7310             Error_Msg_N ("too few discriminants given in constraint", C);
7311             return New_Elmt_List;
7312
7313          elsif Nkind (Constr) = N_Range
7314            or else (Nkind (Constr) = N_Attribute_Reference
7315                      and then
7316                     Attribute_Name (Constr) = Name_Range)
7317          then
7318             Error_Msg_N
7319               ("a range is not a valid discriminant constraint", Constr);
7320             Discr_Expr (D) := Error;
7321
7322          else
7323             Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
7324             Discr_Expr (D) := Constr;
7325          end if;
7326
7327          Next_Discriminant (Discr);
7328          Next (Constr);
7329       end loop;
7330
7331       if No (Discr) and then Present (Constr) then
7332          Error_Msg_N ("too many discriminants given in constraint", Constr);
7333          return New_Elmt_List;
7334       end if;
7335
7336       --  Named associations can be given in any order, but if both positional
7337       --  and named associations are used in the same discriminant constraint,
7338       --  then positional associations must occur first, at their normal
7339       --  position. Hence once a named association is used, the rest of the
7340       --  discriminant constraint must use only named associations.
7341
7342       while Present (Constr) loop
7343
7344          --  Positional association forbidden after a named association
7345
7346          if Nkind (Constr) /= N_Discriminant_Association then
7347             Error_Msg_N ("positional association follows named one", Constr);
7348             return New_Elmt_List;
7349
7350          --  Otherwise it is a named association
7351
7352          else
7353             --  E records the type of the discriminants in the named
7354             --  association. All the discriminants specified in the same name
7355             --  association must have the same type.
7356
7357             E := Empty;
7358
7359             --  Search the list of discriminants in T to see if the simple name
7360             --  given in the constraint matches any of them.
7361
7362             Id := First (Selector_Names (Constr));
7363             while Present (Id) loop
7364                Found := False;
7365
7366                --  If Original_Discriminant is present, we are processing a
7367                --  generic instantiation and this is an instance node. We need
7368                --  to find the name of the corresponding discriminant in the
7369                --  actual record type T and not the name of the discriminant in
7370                --  the generic formal. Example:
7371
7372                --    generic
7373                --       type G (D : int) is private;
7374                --    package P is
7375                --       subtype W is G (D => 1);
7376                --    end package;
7377                --    type Rec (X : int) is record ... end record;
7378                --    package Q is new P (G => Rec);
7379
7380                --  At the point of the instantiation, formal type G is Rec
7381                --  and therefore when reanalyzing "subtype W is G (D => 1);"
7382                --  which really looks like "subtype W is Rec (D => 1);" at
7383                --  the point of instantiation, we want to find the discriminant
7384                --  that corresponds to D in Rec, i.e. X.
7385
7386                if Present (Original_Discriminant (Id)) then
7387                   Discr := Find_Corresponding_Discriminant (Id, T);
7388                   Found := True;
7389
7390                else
7391                   Discr := First_Discriminant (T);
7392                   while Present (Discr) loop
7393                      if Chars (Discr) = Chars (Id) then
7394                         Found := True;
7395                         exit;
7396                      end if;
7397
7398                      Next_Discriminant (Discr);
7399                   end loop;
7400
7401                   if not Found then
7402                      Error_Msg_N ("& does not match any discriminant", Id);
7403                      return New_Elmt_List;
7404
7405                   --  The following is only useful for the benefit of generic
7406                   --  instances but it does not interfere with other
7407                   --  processing for the non-generic case so we do it in all
7408                   --  cases (for generics this statement is executed when
7409                   --  processing the generic definition, see comment at the
7410                   --  beginning of this if statement).
7411
7412                   else
7413                      Set_Original_Discriminant (Id, Discr);
7414                   end if;
7415                end if;
7416
7417                Position := Pos_Of_Discr (T, Discr);
7418
7419                if Present (Discr_Expr (Position)) then
7420                   Error_Msg_N ("duplicate constraint for discriminant&", Id);
7421
7422                else
7423                   --  Each discriminant specified in the same named association
7424                   --  must be associated with a separate copy of the
7425                   --  corresponding expression.
7426
7427                   if Present (Next (Id)) then
7428                      Expr := New_Copy_Tree (Expression (Constr));
7429                      Set_Parent (Expr, Parent (Expression (Constr)));
7430                   else
7431                      Expr := Expression (Constr);
7432                   end if;
7433
7434                   Discr_Expr (Position) := Expr;
7435                   Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
7436                end if;
7437
7438                --  A discriminant association with more than one discriminant
7439                --  name is only allowed if the named discriminants are all of
7440                --  the same type (RM 3.7.1(8)).
7441
7442                if E = Empty then
7443                   E := Base_Type (Etype (Discr));
7444
7445                elsif Base_Type (Etype (Discr)) /= E then
7446                   Error_Msg_N
7447                     ("all discriminants in an association " &
7448                      "must have the same type", Id);
7449                end if;
7450
7451                Next (Id);
7452             end loop;
7453          end if;
7454
7455          Next (Constr);
7456       end loop;
7457
7458       --  A discriminant constraint must provide exactly one value for each
7459       --  discriminant of the type (RM 3.7.1(8)).
7460
7461       for J in Discr_Expr'Range loop
7462          if No (Discr_Expr (J)) then
7463             Error_Msg_N ("too few discriminants given in constraint", C);
7464             return New_Elmt_List;
7465          end if;
7466       end loop;
7467
7468       --  Determine if there are discriminant expressions in the constraint
7469
7470       for J in Discr_Expr'Range loop
7471          if Denotes_Discriminant
7472               (Discr_Expr (J), Check_Concurrent => True)
7473          then
7474             Discrim_Present := True;
7475          end if;
7476       end loop;
7477
7478       --  Build an element list consisting of the expressions given in the
7479       --  discriminant constraint and apply the appropriate checks. The list
7480       --  is constructed after resolving any named discriminant associations
7481       --  and therefore the expressions appear in the textual order of the
7482       --  discriminants.
7483
7484       Discr := First_Discriminant (T);
7485       for J in Discr_Expr'Range loop
7486          if Discr_Expr (J) /= Error then
7487             Append_Elmt (Discr_Expr (J), Elist);
7488
7489             --  If any of the discriminant constraints is given by a
7490             --  discriminant and we are in a derived type declaration we
7491             --  have a discriminant renaming. Establish link between new
7492             --  and old discriminant.
7493
7494             if Denotes_Discriminant (Discr_Expr (J)) then
7495                if Derived_Def then
7496                   Set_Corresponding_Discriminant
7497                     (Entity (Discr_Expr (J)), Discr);
7498                end if;
7499
7500             --  Force the evaluation of non-discriminant expressions.
7501             --  If we have found a discriminant in the constraint 3.4(26)
7502             --  and 3.8(18) demand that no range checks are performed are
7503             --  after evaluation. If the constraint is for a component
7504             --  definition that has a per-object constraint, expressions are
7505             --  evaluated but not checked either. In all other cases perform
7506             --  a range check.
7507
7508             else
7509                if Discrim_Present then
7510                   null;
7511
7512                elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
7513                  and then
7514                    Has_Per_Object_Constraint
7515                      (Defining_Identifier (Parent (Parent (Def))))
7516                then
7517                   null;
7518
7519                elsif Is_Access_Type (Etype (Discr)) then
7520                   Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
7521
7522                else
7523                   Apply_Range_Check (Discr_Expr (J), Etype (Discr));
7524                end if;
7525
7526                Force_Evaluation (Discr_Expr (J));
7527             end if;
7528
7529             --  Check that the designated type of an access discriminant's
7530             --  expression is not a class-wide type unless the discriminant's
7531             --  designated type is also class-wide.
7532
7533             if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
7534               and then not Is_Class_Wide_Type
7535                          (Designated_Type (Etype (Discr)))
7536               and then Etype (Discr_Expr (J)) /= Any_Type
7537               and then Is_Class_Wide_Type
7538                          (Designated_Type (Etype (Discr_Expr (J))))
7539             then
7540                Wrong_Type (Discr_Expr (J), Etype (Discr));
7541             end if;
7542          end if;
7543
7544          Next_Discriminant (Discr);
7545       end loop;
7546
7547       return Elist;
7548    end Build_Discriminant_Constraints;
7549
7550    ---------------------------------
7551    -- Build_Discriminated_Subtype --
7552    ---------------------------------
7553
7554    procedure Build_Discriminated_Subtype
7555      (T           : Entity_Id;
7556       Def_Id      : Entity_Id;
7557       Elist       : Elist_Id;
7558       Related_Nod : Node_Id;
7559       For_Access  : Boolean := False)
7560    is
7561       Has_Discrs  : constant Boolean := Has_Discriminants (T);
7562       Constrained : constant Boolean :=
7563                       (Has_Discrs
7564                          and then not Is_Empty_Elmt_List (Elist)
7565                          and then not Is_Class_Wide_Type (T))
7566                         or else Is_Constrained (T);
7567
7568    begin
7569       if Ekind (T) = E_Record_Type then
7570          if For_Access then
7571             Set_Ekind (Def_Id, E_Private_Subtype);
7572             Set_Is_For_Access_Subtype (Def_Id, True);
7573          else
7574             Set_Ekind (Def_Id, E_Record_Subtype);
7575          end if;
7576
7577          --  Inherit preelaboration flag from base, for types for which it
7578          --  may have been set: records, private types, protected types.
7579
7580          Set_Known_To_Have_Preelab_Init
7581            (Def_Id, Known_To_Have_Preelab_Init (T));
7582
7583       elsif Ekind (T) = E_Task_Type then
7584          Set_Ekind (Def_Id, E_Task_Subtype);
7585
7586       elsif Ekind (T) = E_Protected_Type then
7587          Set_Ekind (Def_Id, E_Protected_Subtype);
7588          Set_Known_To_Have_Preelab_Init
7589            (Def_Id, Known_To_Have_Preelab_Init (T));
7590
7591       elsif Is_Private_Type (T) then
7592          Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
7593          Set_Known_To_Have_Preelab_Init
7594            (Def_Id, Known_To_Have_Preelab_Init (T));
7595
7596       elsif Is_Class_Wide_Type (T) then
7597          Set_Ekind (Def_Id, E_Class_Wide_Subtype);
7598
7599       else
7600          --  Incomplete type. Attach subtype to list of dependents, to be
7601          --  completed with full view of parent type,  unless is it the
7602          --  designated subtype of a record component within an init_proc.
7603          --  This last case arises for a component of an access type whose
7604          --  designated type is incomplete (e.g. a Taft Amendment type).
7605          --  The designated subtype is within an inner scope, and needs no
7606          --  elaboration, because only the access type is needed in the
7607          --  initialization procedure.
7608
7609          Set_Ekind (Def_Id, Ekind (T));
7610
7611          if For_Access and then Within_Init_Proc then
7612             null;
7613          else
7614             Append_Elmt (Def_Id, Private_Dependents (T));
7615          end if;
7616       end if;
7617
7618       Set_Etype             (Def_Id, T);
7619       Init_Size_Align       (Def_Id);
7620       Set_Has_Discriminants (Def_Id, Has_Discrs);
7621       Set_Is_Constrained    (Def_Id, Constrained);
7622
7623       Set_First_Entity      (Def_Id, First_Entity   (T));
7624       Set_Last_Entity       (Def_Id, Last_Entity    (T));
7625       Set_First_Rep_Item    (Def_Id, First_Rep_Item (T));
7626
7627       if Is_Tagged_Type (T) then
7628          Set_Is_Tagged_Type  (Def_Id);
7629          Make_Class_Wide_Type (Def_Id);
7630       end if;
7631
7632       Set_Stored_Constraint (Def_Id, No_Elist);
7633
7634       if Has_Discrs then
7635          Set_Discriminant_Constraint (Def_Id, Elist);
7636          Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
7637       end if;
7638
7639       if Is_Tagged_Type (T) then
7640
7641          --  Ada 2005 (AI-251): In case of concurrent types we inherit the
7642          --  concurrent record type (which has the list of primitive
7643          --  operations).
7644
7645          if Ada_Version >= Ada_05
7646            and then Is_Concurrent_Type (T)
7647          then
7648             Set_Corresponding_Record_Type (Def_Id,
7649                Corresponding_Record_Type (T));
7650          else
7651             Set_Primitive_Operations (Def_Id, Primitive_Operations (T));
7652          end if;
7653
7654          Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
7655       end if;
7656
7657       --  Subtypes introduced by component declarations do not need to be
7658       --  marked as delayed, and do not get freeze nodes, because the semantics
7659       --  verifies that the parents of the subtypes are frozen before the
7660       --  enclosing record is frozen.
7661
7662       if not Is_Type (Scope (Def_Id)) then
7663          Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
7664
7665          if Is_Private_Type (T)
7666            and then Present (Full_View (T))
7667          then
7668             Conditional_Delay (Def_Id, Full_View (T));
7669          else
7670             Conditional_Delay (Def_Id, T);
7671          end if;
7672       end if;
7673
7674       if Is_Record_Type (T) then
7675          Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
7676
7677          if Has_Discrs
7678             and then not Is_Empty_Elmt_List (Elist)
7679             and then not For_Access
7680          then
7681             Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
7682          elsif not For_Access then
7683             Set_Cloned_Subtype (Def_Id, T);
7684          end if;
7685       end if;
7686    end Build_Discriminated_Subtype;
7687
7688    ---------------------------
7689    -- Build_Itype_Reference --
7690    ---------------------------
7691
7692    procedure Build_Itype_Reference
7693      (Ityp : Entity_Id;
7694       Nod  : Node_Id)
7695    is
7696       IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
7697    begin
7698       Set_Itype (IR, Ityp);
7699       Insert_After (Nod, IR);
7700    end Build_Itype_Reference;
7701
7702    ------------------------
7703    -- Build_Scalar_Bound --
7704    ------------------------
7705
7706    function Build_Scalar_Bound
7707      (Bound : Node_Id;
7708       Par_T : Entity_Id;
7709       Der_T : Entity_Id) return Node_Id
7710    is
7711       New_Bound : Entity_Id;
7712
7713    begin
7714       --  Note: not clear why this is needed, how can the original bound
7715       --  be unanalyzed at this point? and if it is, what business do we
7716       --  have messing around with it? and why is the base type of the
7717       --  parent type the right type for the resolution. It probably is
7718       --  not! It is OK for the new bound we are creating, but not for
7719       --  the old one??? Still if it never happens, no problem!
7720
7721       Analyze_And_Resolve (Bound, Base_Type (Par_T));
7722
7723       if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
7724          New_Bound := New_Copy (Bound);
7725          Set_Etype (New_Bound, Der_T);
7726          Set_Analyzed (New_Bound);
7727
7728       elsif Is_Entity_Name (Bound) then
7729          New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
7730
7731       --  The following is almost certainly wrong. What business do we have
7732       --  relocating a node (Bound) that is presumably still attached to
7733       --  the tree elsewhere???
7734
7735       else
7736          New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
7737       end if;
7738
7739       Set_Etype (New_Bound, Der_T);
7740       return New_Bound;
7741    end Build_Scalar_Bound;
7742
7743    --------------------------------
7744    -- Build_Underlying_Full_View --
7745    --------------------------------
7746
7747    procedure Build_Underlying_Full_View
7748      (N   : Node_Id;
7749       Typ : Entity_Id;
7750       Par : Entity_Id)
7751    is
7752       Loc  : constant Source_Ptr := Sloc (N);
7753       Subt : constant Entity_Id :=
7754                Make_Defining_Identifier
7755                  (Loc, New_External_Name (Chars (Typ), 'S'));
7756
7757       Constr : Node_Id;
7758       Indic  : Node_Id;
7759       C      : Node_Id;
7760       Id     : Node_Id;
7761
7762       procedure Set_Discriminant_Name (Id : Node_Id);
7763       --  If the derived type has discriminants, they may rename discriminants
7764       --  of the parent. When building the full view of the parent, we need to
7765       --  recover the names of the original discriminants if the constraint is
7766       --  given by named associations.
7767
7768       ---------------------------
7769       -- Set_Discriminant_Name --
7770       ---------------------------
7771
7772       procedure Set_Discriminant_Name (Id : Node_Id) is
7773          Disc : Entity_Id;
7774
7775       begin
7776          Set_Original_Discriminant (Id, Empty);
7777
7778          if Has_Discriminants (Typ) then
7779             Disc := First_Discriminant (Typ);
7780             while Present (Disc) loop
7781                if Chars (Disc) = Chars (Id)
7782                  and then Present (Corresponding_Discriminant (Disc))
7783                then
7784                   Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
7785                end if;
7786                Next_Discriminant (Disc);
7787             end loop;
7788          end if;
7789       end Set_Discriminant_Name;
7790
7791    --  Start of processing for Build_Underlying_Full_View
7792
7793    begin
7794       if Nkind (N) = N_Full_Type_Declaration then
7795          Constr := Constraint (Subtype_Indication (Type_Definition (N)));
7796
7797       elsif Nkind (N) = N_Subtype_Declaration then
7798          Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
7799
7800       elsif Nkind (N) = N_Component_Declaration then
7801          Constr :=
7802            New_Copy_Tree
7803              (Constraint (Subtype_Indication (Component_Definition (N))));
7804
7805       else
7806          raise Program_Error;
7807       end if;
7808
7809       C := First (Constraints (Constr));
7810       while Present (C) loop
7811          if Nkind (C) = N_Discriminant_Association then
7812             Id := First (Selector_Names (C));
7813             while Present (Id) loop
7814                Set_Discriminant_Name (Id);
7815                Next (Id);
7816             end loop;
7817          end if;
7818
7819          Next (C);
7820       end loop;
7821
7822       Indic :=
7823         Make_Subtype_Declaration (Loc,
7824           Defining_Identifier => Subt,
7825           Subtype_Indication  =>
7826             Make_Subtype_Indication (Loc,
7827               Subtype_Mark => New_Reference_To (Par, Loc),
7828               Constraint   => New_Copy_Tree (Constr)));
7829
7830       --  If this is a component subtype for an outer itype, it is not
7831       --  a list member, so simply set the parent link for analysis: if
7832       --  the enclosing type does not need to be in a declarative list,
7833       --  neither do the components.
7834
7835       if Is_List_Member (N)
7836         and then Nkind (N) /= N_Component_Declaration
7837       then
7838          Insert_Before (N, Indic);
7839       else
7840          Set_Parent (Indic, Parent (N));
7841       end if;
7842
7843       Analyze (Indic);
7844       Set_Underlying_Full_View (Typ, Full_View (Subt));
7845    end Build_Underlying_Full_View;
7846
7847    -------------------------------
7848    -- Check_Abstract_Interfaces --
7849    -------------------------------
7850
7851    procedure Check_Abstract_Interfaces (N : Node_Id; Def : Node_Id) is
7852       Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
7853
7854       Iface       : Node_Id;
7855       Iface_Def   : Node_Id;
7856       Iface_Typ   : Entity_Id;
7857       Parent_Node : Node_Id;
7858
7859       Is_Task : Boolean := False;
7860       --  Set True if parent type or any progenitor is a task interface
7861
7862       Is_Protected : Boolean := False;
7863       --  Set True if parent type or any progenitor is a protected interface
7864
7865       procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
7866       --  Check that a progenitor is compatible with declaration.
7867       --  Error is posted on Error_Node.
7868
7869       ------------------
7870       -- Check_Ifaces --
7871       ------------------
7872
7873       procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
7874          Iface_Id : constant Entity_Id :=
7875                       Defining_Identifier (Parent (Iface_Def));
7876          Type_Def : Node_Id;
7877
7878       begin
7879          if Nkind (N) = N_Private_Extension_Declaration then
7880             Type_Def := N;
7881          else
7882             Type_Def := Type_Definition (N);
7883          end if;
7884
7885          if Is_Task_Interface (Iface_Id) then
7886             Is_Task := True;
7887
7888          elsif Is_Protected_Interface (Iface_Id) then
7889             Is_Protected := True;
7890          end if;
7891
7892          --  Check that the characteristics of the progenitor are compatible
7893          --  with the explicit qualifier in the declaration.
7894          --  The check only applies to qualifiers that come from source.
7895          --  Limited_Present also appears in the declaration of corresponding
7896          --  records, and the check does not apply to them.
7897
7898          if Limited_Present (Type_Def)
7899            and then not
7900              Is_Concurrent_Record_Type (Defining_Identifier (N))
7901          then
7902             if Is_Limited_Interface (Parent_Type)
7903               and then not Is_Limited_Interface (Iface_Id)
7904             then
7905                Error_Msg_NE
7906                  ("progenitor& must be limited interface",
7907                    Error_Node, Iface_Id);
7908
7909             elsif
7910               (Task_Present (Iface_Def)
7911                 or else Protected_Present (Iface_Def)
7912                 or else Synchronized_Present (Iface_Def))
7913               and then Nkind (N) /= N_Private_Extension_Declaration
7914             then
7915                Error_Msg_NE
7916                  ("progenitor& must be limited interface",
7917                    Error_Node, Iface_Id);
7918             end if;
7919
7920          --  Protected interfaces can only inherit from limited, synchronized
7921          --  or protected interfaces.
7922
7923          elsif Nkind (N) = N_Full_Type_Declaration
7924            and then  Protected_Present (Type_Def)
7925          then
7926             if Limited_Present (Iface_Def)
7927               or else Synchronized_Present (Iface_Def)
7928               or else Protected_Present (Iface_Def)
7929             then
7930                null;
7931
7932             elsif Task_Present (Iface_Def) then
7933                Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
7934                             & " from task interface", Error_Node);
7935
7936             else
7937                Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
7938                             & " from non-limited interface", Error_Node);
7939             end if;
7940
7941          --  Ada 2005 (AI-345): Synchronized interfaces can only inherit from
7942          --  limited and synchronized.
7943
7944          elsif Synchronized_Present (Type_Def) then
7945             if Limited_Present (Iface_Def)
7946               or else Synchronized_Present (Iface_Def)
7947             then
7948                null;
7949
7950             elsif Protected_Present (Iface_Def)
7951               and then Nkind (N) /= N_Private_Extension_Declaration
7952             then
7953                Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
7954                             & " from protected interface", Error_Node);
7955
7956             elsif Task_Present (Iface_Def)
7957               and then Nkind (N) /= N_Private_Extension_Declaration
7958             then
7959                Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
7960                             & " from task interface", Error_Node);
7961
7962             elsif not Is_Limited_Interface (Iface_Id) then
7963                Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
7964                             & " from non-limited interface", Error_Node);
7965             end if;
7966
7967          --  Ada 2005 (AI-345): Task interfaces can only inherit from limited,
7968          --  synchronized or task interfaces.
7969
7970          elsif Nkind (N) = N_Full_Type_Declaration
7971            and then Task_Present (Type_Def)
7972          then
7973             if Limited_Present (Iface_Def)
7974               or else Synchronized_Present (Iface_Def)
7975               or else Task_Present (Iface_Def)
7976             then
7977                null;
7978
7979             elsif Protected_Present (Iface_Def) then
7980                Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
7981                             & " protected interface", Error_Node);
7982
7983             else
7984                Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
7985                             & " non-limited interface", Error_Node);
7986             end if;
7987          end if;
7988       end Check_Ifaces;
7989
7990    --  Start of processing for Check_Abstract_Interfaces
7991
7992    begin
7993       if Is_Interface (Parent_Type) then
7994          if Is_Task_Interface (Parent_Type) then
7995             Is_Task := True;
7996
7997          elsif Is_Protected_Interface (Parent_Type) then
7998             Is_Protected := True;
7999          end if;
8000       end if;
8001
8002       if Nkind (N) = N_Private_Extension_Declaration then
8003
8004          --  Check that progenitors are compatible with declaration
8005
8006          Iface := First (Interface_List (Def));
8007          while Present (Iface) loop
8008             Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
8009
8010             Parent_Node := Parent (Base_Type (Iface_Typ));
8011             Iface_Def   := Type_Definition (Parent_Node);
8012
8013             if not Is_Interface (Iface_Typ) then
8014                Error_Msg_NE ("(Ada 2005) & must be an interface",
8015                           Iface, Iface_Typ);
8016
8017             else
8018                Check_Ifaces (Iface_Def, Iface);
8019             end if;
8020
8021             Next (Iface);
8022          end loop;
8023
8024          if Is_Task and Is_Protected then
8025             Error_Msg_N
8026               ("type cannot derive from task and protected interface", N);
8027          end if;
8028
8029          return;
8030       end if;
8031
8032       --  Full type declaration of derived type.
8033       --  Check compatibility with parent if it is interface type
8034
8035       if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
8036         and then Is_Interface (Parent_Type)
8037       then
8038          Parent_Node := Parent (Parent_Type);
8039
8040          --  More detailed checks for interface varieties
8041
8042          Check_Ifaces
8043            (Iface_Def  => Type_Definition (Parent_Node),
8044             Error_Node => Subtype_Indication (Type_Definition (N)));
8045       end if;
8046
8047       Iface := First (Interface_List (Def));
8048
8049       while Present (Iface) loop
8050          Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
8051
8052          Parent_Node := Parent (Base_Type (Iface_Typ));
8053          Iface_Def   := Type_Definition (Parent_Node);
8054
8055          if not Is_Interface (Iface_Typ) then
8056             Error_Msg_NE ("(Ada 2005) & must be an interface",
8057                           Iface, Iface_Typ);
8058
8059          else
8060             --  "The declaration of a specific descendant of an interface
8061             --   type freezes the interface type" RM 13.14
8062
8063             Freeze_Before (N, Iface_Typ);
8064             Check_Ifaces (Iface_Def, Error_Node => Iface);
8065          end if;
8066
8067          Next (Iface);
8068       end loop;
8069
8070       if Is_Task and Is_Protected then
8071          Error_Msg_N
8072            ("type cannot derive from task and protected interface", N);
8073       end if;
8074
8075    end Check_Abstract_Interfaces;
8076
8077    -------------------------------
8078    -- Check_Abstract_Overriding --
8079    -------------------------------
8080
8081    procedure Check_Abstract_Overriding (T : Entity_Id) is
8082       Alias_Subp : Entity_Id;
8083       Elmt       : Elmt_Id;
8084       Op_List    : Elist_Id;
8085       Subp       : Entity_Id;
8086       Type_Def   : Node_Id;
8087
8088    begin
8089       Op_List := Primitive_Operations (T);
8090
8091       --  Loop to check primitive operations
8092
8093       Elmt := First_Elmt (Op_List);
8094       while Present (Elmt) loop
8095          Subp := Node (Elmt);
8096          Alias_Subp := Alias (Subp);
8097
8098          --  Inherited subprograms are identified by the fact that they do not
8099          --  come from source, and the associated source location is the
8100          --  location of the first subtype of the derived type.
8101
8102          --  Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
8103          --  subprograms that "require overriding".
8104
8105          --  Special exception, do not complain about failure to override the
8106          --  stream routines _Input and _Output, as well as the primitive
8107          --  operations used in dispatching selects since we always provide
8108          --  automatic overridings for these subprograms.
8109
8110          --  Also ignore this rule for convention CIL since .NET libraries
8111          --  do bizarre things with interfaces???
8112
8113          --  The partial view of T may have been a private extension, for
8114          --  which inherited functions dispatching on result are abstract.
8115          --  If the full view is a null extension, there is no need for
8116          --  overriding in Ada2005, but wrappers need to be built for them
8117          --  (see exp_ch3, Build_Controlling_Function_Wrappers).
8118
8119          if Is_Null_Extension (T)
8120            and then Has_Controlling_Result (Subp)
8121            and then Ada_Version >= Ada_05
8122            and then Present (Alias (Subp))
8123            and then not Comes_From_Source (Subp)
8124            and then not Is_Abstract_Subprogram (Alias (Subp))
8125            and then not Is_Access_Type (Etype (Subp))
8126          then
8127             null;
8128
8129          elsif (Is_Abstract_Subprogram (Subp)
8130                  or else Requires_Overriding (Subp)
8131                  or else
8132                    (Has_Controlling_Result (Subp)
8133                      and then Present (Alias_Subp)
8134                      and then not Comes_From_Source (Subp)
8135                      and then Sloc (Subp) = Sloc (First_Subtype (T))))
8136            and then not Is_TSS (Subp, TSS_Stream_Input)
8137            and then not Is_TSS (Subp, TSS_Stream_Output)
8138            and then not Is_Abstract_Type (T)
8139            and then Convention (T) /= Convention_CIL
8140            and then Chars (Subp) /= Name_uDisp_Asynchronous_Select
8141            and then Chars (Subp) /= Name_uDisp_Conditional_Select
8142            and then Chars (Subp) /= Name_uDisp_Get_Prim_Op_Kind
8143            and then Chars (Subp) /= Name_uDisp_Requeue
8144            and then Chars (Subp) /= Name_uDisp_Timed_Select
8145
8146             --  Ada 2005 (AI-251): Do not consider hidden entities associated
8147             --  with abstract interface types because the check will be done
8148             --  with the aliased entity (otherwise we generate a duplicated
8149             --  error message).
8150
8151            and then not Present (Abstract_Interface_Alias (Subp))
8152          then
8153             if Present (Alias_Subp) then
8154
8155                --  Only perform the check for a derived subprogram when the
8156                --  type has an explicit record extension. This avoids incorrect
8157                --  flagging of abstract subprograms for the case of a type
8158                --  without an extension that is derived from a formal type
8159                --  with a tagged actual (can occur within a private part).
8160
8161                --  Ada 2005 (AI-391): In the case of an inherited function with
8162                --  a controlling result of the type, the rule does not apply if
8163                --  the type is a null extension (unless the parent function
8164                --  itself is abstract, in which case the function must still be
8165                --  be overridden). The expander will generate an overriding
8166                --  wrapper function calling the parent subprogram (see
8167                --  Exp_Ch3.Make_Controlling_Wrapper_Functions).
8168
8169                Type_Def := Type_Definition (Parent (T));
8170
8171                if Nkind (Type_Def) = N_Derived_Type_Definition
8172                  and then Present (Record_Extension_Part (Type_Def))
8173                  and then
8174                    (Ada_Version < Ada_05
8175                       or else not Is_Null_Extension (T)
8176                       or else Ekind (Subp) = E_Procedure
8177                       or else not Has_Controlling_Result (Subp)
8178                       or else Is_Abstract_Subprogram (Alias_Subp)
8179                       or else Requires_Overriding (Subp)
8180                       or else Is_Access_Type (Etype (Subp)))
8181                then
8182                   --  The body of predefined primitives of tagged types derived
8183                   --  from interface types are generated later by Freeze_Type.
8184
8185                   if Is_Predefined_Dispatching_Operation (Subp)
8186                     and then Is_Abstract_Subprogram (Alias_Subp)
8187                     and then Is_Interface
8188                                (Root_Type (Find_Dispatching_Type (Subp)))
8189                   then
8190                      null;
8191
8192                   else
8193                      Error_Msg_NE
8194                        ("type must be declared abstract or & overridden",
8195                         T, Subp);
8196
8197                      --  Traverse the whole chain of aliased subprograms to
8198                      --  complete the error notification. This is especially
8199                      --  useful for traceability of the chain of entities when
8200                      --  the subprogram corresponds with an interface
8201                      --  subprogram (which may be defined in another package).
8202
8203                      if Present (Alias_Subp) then
8204                         declare
8205                            E : Entity_Id;
8206
8207                         begin
8208                            E := Subp;
8209                            while Present (Alias (E)) loop
8210                               Error_Msg_Sloc := Sloc (E);
8211                               Error_Msg_NE
8212                                 ("\& has been inherited #", T, Subp);
8213                               E := Alias (E);
8214                            end loop;
8215
8216                            Error_Msg_Sloc := Sloc (E);
8217                            Error_Msg_NE
8218                              ("\& has been inherited from subprogram #",
8219                               T, Subp);
8220                         end;
8221                      end if;
8222                   end if;
8223
8224                --  Ada 2005 (AI-345): Protected or task type implementing
8225                --  abstract interfaces.
8226
8227                elsif Is_Concurrent_Record_Type (T)
8228                  and then Present (Abstract_Interfaces (T))
8229                then
8230                   --  The controlling formal of Subp must be of mode "out",
8231                   --  "in out" or an access-to-variable to be overridden.
8232
8233                   --  Error message below needs rewording (remember comma
8234                   --  in -gnatj mode) ???
8235
8236                   if Ekind (First_Formal (Subp)) = E_In_Parameter then
8237                      Error_Msg_NE
8238                        ("first formal of & must be of mode `OUT`, `IN OUT` " &
8239                         "or access-to-variable", T, Subp);
8240                      Error_Msg_N
8241                        ("\to be overridden by protected procedure or " &
8242                         "entry (RM 9.4(11.9/2))", T);
8243
8244                   --  Some other kind of overriding failure
8245
8246                   else
8247                      Error_Msg_NE
8248                        ("interface subprogram & must be overridden",
8249                         T, Subp);
8250                   end if;
8251                end if;
8252
8253             else
8254                Error_Msg_Node_2 := T;
8255                Error_Msg_N
8256                  ("abstract subprogram& not allowed for type&", Subp);
8257
8258                --  Also post unconditional warning on the type (unconditional
8259                --  so that if there are more than one of these cases, we get
8260                --  them all, and not just the first one).
8261
8262                Error_Msg_Node_2 := Subp;
8263                Error_Msg_N
8264                  ("nonabstract type& has abstract subprogram&!", T);
8265             end if;
8266          end if;
8267
8268          --  Ada 2005 (AI05-0030): Inspect hidden subprograms which provide
8269          --  the mapping between interface and implementing type primitives.
8270          --  If the interface alias is marked as Implemented_By_Entry, the
8271          --  alias must be an entry wrapper.
8272
8273          if Ada_Version >= Ada_05
8274            and then Is_Hidden (Subp)
8275            and then Present (Abstract_Interface_Alias (Subp))
8276            and then Implemented_By_Entry (Abstract_Interface_Alias (Subp))
8277            and then Present (Alias_Subp)
8278            and then
8279              (not Is_Primitive_Wrapper (Alias_Subp)
8280                 or else Ekind (Wrapped_Entity (Alias_Subp)) /= E_Entry)
8281          then
8282             declare
8283                Error_Ent : Entity_Id := T;
8284
8285             begin
8286                if Is_Concurrent_Record_Type (Error_Ent) then
8287                   Error_Ent := Corresponding_Concurrent_Type (Error_Ent);
8288                end if;
8289
8290                Error_Msg_Node_2 := Abstract_Interface_Alias (Subp);
8291                Error_Msg_NE
8292                  ("type & must implement abstract subprogram & with an entry",
8293                   Error_Ent, Error_Ent);
8294             end;
8295          end if;
8296
8297          Next_Elmt (Elmt);
8298       end loop;
8299    end Check_Abstract_Overriding;
8300
8301    ------------------------------------------------
8302    -- Check_Access_Discriminant_Requires_Limited --
8303    ------------------------------------------------
8304
8305    procedure Check_Access_Discriminant_Requires_Limited
8306      (D   : Node_Id;
8307       Loc : Node_Id)
8308    is
8309    begin
8310       --  A discriminant_specification for an access discriminant shall appear
8311       --  only in the declaration for a task or protected type, or for a type
8312       --  with the reserved word 'limited' in its definition or in one of its
8313       --  ancestors. (RM 3.7(10))
8314
8315       if Nkind (Discriminant_Type (D)) = N_Access_Definition
8316         and then not Is_Concurrent_Type (Current_Scope)
8317         and then not Is_Concurrent_Record_Type (Current_Scope)
8318         and then not Is_Limited_Record (Current_Scope)
8319         and then Ekind (Current_Scope) /= E_Limited_Private_Type
8320       then
8321          Error_Msg_N
8322            ("access discriminants allowed only for limited types", Loc);
8323       end if;
8324    end Check_Access_Discriminant_Requires_Limited;
8325
8326    -----------------------------------
8327    -- Check_Aliased_Component_Types --
8328    -----------------------------------
8329
8330    procedure Check_Aliased_Component_Types (T : Entity_Id) is
8331       C : Entity_Id;
8332
8333    begin
8334       --  ??? Also need to check components of record extensions, but not
8335       --  components of protected types (which are always limited).
8336
8337       --  Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
8338       --  types to be unconstrained. This is safe because it is illegal to
8339       --  create access subtypes to such types with explicit discriminant
8340       --  constraints.
8341
8342       if not Is_Limited_Type (T) then
8343          if Ekind (T) = E_Record_Type then
8344             C := First_Component (T);
8345             while Present (C) loop
8346                if Is_Aliased (C)
8347                  and then Has_Discriminants (Etype (C))
8348                  and then not Is_Constrained (Etype (C))
8349                  and then not In_Instance_Body
8350                  and then Ada_Version < Ada_05
8351                then
8352                   Error_Msg_N
8353                     ("aliased component must be constrained (RM 3.6(11))",
8354                       C);
8355                end if;
8356
8357                Next_Component (C);
8358             end loop;
8359
8360          elsif Ekind (T) = E_Array_Type then
8361             if Has_Aliased_Components (T)
8362               and then Has_Discriminants (Component_Type (T))
8363               and then not Is_Constrained (Component_Type (T))
8364               and then not In_Instance_Body
8365               and then Ada_Version < Ada_05
8366             then
8367                Error_Msg_N
8368                  ("aliased component type must be constrained (RM 3.6(11))",
8369                     T);
8370             end if;
8371          end if;
8372       end if;
8373    end Check_Aliased_Component_Types;
8374
8375    ----------------------
8376    -- Check_Completion --
8377    ----------------------
8378
8379    procedure Check_Completion (Body_Id : Node_Id := Empty) is
8380       E : Entity_Id;
8381
8382       procedure Post_Error;
8383       --  Post error message for lack of completion for entity E
8384
8385       ----------------
8386       -- Post_Error --
8387       ----------------
8388
8389       procedure Post_Error is
8390       begin
8391          if not Comes_From_Source (E) then
8392
8393             if Ekind (E) = E_Task_Type
8394               or else Ekind (E) = E_Protected_Type
8395             then
8396                --  It may be an anonymous protected type created for a
8397                --  single variable. Post error on variable, if present.
8398
8399                declare
8400                   Var : Entity_Id;
8401
8402                begin
8403                   Var := First_Entity (Current_Scope);
8404                   while Present (Var) loop
8405                      exit when Etype (Var) = E
8406                        and then Comes_From_Source (Var);
8407
8408                      Next_Entity (Var);
8409                   end loop;
8410
8411                   if Present (Var) then
8412                      E := Var;
8413                   end if;
8414                end;
8415             end if;
8416          end if;
8417
8418          --  If a generated entity has no completion, then either previous
8419          --  semantic errors have disabled the expansion phase, or else we had
8420          --  missing subunits, or else we are compiling without expansion,
8421          --  or else something is very wrong.
8422
8423          if not Comes_From_Source (E) then
8424             pragma Assert
8425               (Serious_Errors_Detected > 0
8426                 or else Configurable_Run_Time_Violations > 0
8427                 or else Subunits_Missing
8428                 or else not Expander_Active);
8429             return;
8430
8431          --  Here for source entity
8432
8433          else
8434             --  Here if no body to post the error message, so we post the error
8435             --  on the declaration that has no completion. This is not really
8436             --  the right place to post it, think about this later ???
8437
8438             if No (Body_Id) then
8439                if Is_Type (E) then
8440                   Error_Msg_NE
8441                     ("missing full declaration for }", Parent (E), E);
8442                else
8443                   Error_Msg_NE
8444                     ("missing body for &", Parent (E), E);
8445                end if;
8446
8447             --  Package body has no completion for a declaration that appears
8448             --  in the corresponding spec. Post error on the body, with a
8449             --  reference to the non-completed declaration.
8450
8451             else
8452                Error_Msg_Sloc := Sloc (E);
8453
8454                if Is_Type (E) then
8455                   Error_Msg_NE
8456                     ("missing full declaration for }!", Body_Id, E);
8457
8458                elsif Is_Overloadable (E)
8459                  and then Current_Entity_In_Scope (E) /= E
8460                then
8461                   --  It may be that the completion is mistyped and appears as
8462                   --  a distinct overloading of the entity.
8463
8464                   declare
8465                      Candidate : constant Entity_Id :=
8466                                    Current_Entity_In_Scope (E);
8467                      Decl      : constant Node_Id :=
8468                                    Unit_Declaration_Node (Candidate);
8469
8470                   begin
8471                      if Is_Overloadable (Candidate)
8472                        and then Ekind (Candidate) = Ekind (E)
8473                        and then Nkind (Decl) = N_Subprogram_Body
8474                        and then Acts_As_Spec (Decl)
8475                      then
8476                         Check_Type_Conformant (Candidate, E);
8477
8478                      else
8479                         Error_Msg_NE ("missing body for & declared#!",
8480                            Body_Id, E);
8481                      end if;
8482                   end;
8483                else
8484                   Error_Msg_NE ("missing body for & declared#!",
8485                      Body_Id, E);
8486                end if;
8487             end if;
8488          end if;
8489       end Post_Error;
8490
8491    --  Start processing for Check_Completion
8492
8493    begin
8494       E := First_Entity (Current_Scope);
8495       while Present (E) loop
8496          if Is_Intrinsic_Subprogram (E) then
8497             null;
8498
8499          --  The following situation requires special handling: a child unit
8500          --  that appears in the context clause of the body of its parent:
8501
8502          --    procedure Parent.Child (...);
8503
8504          --    with Parent.Child;
8505          --    package body Parent is
8506
8507          --  Here Parent.Child appears as a local entity, but should not be
8508          --  flagged as requiring completion, because it is a compilation
8509          --  unit.
8510
8511          --  Ignore missing completion for a subprogram that does not come from
8512          --  source (including the _Call primitive operation of RAS types,
8513          --  which has to have the flag Comes_From_Source for other purposes):
8514          --  we assume that the expander will provide the missing completion.
8515
8516          elsif     Ekind (E) = E_Function
8517            or else Ekind (E) = E_Procedure
8518            or else Ekind (E) = E_Generic_Function
8519            or else Ekind (E) = E_Generic_Procedure
8520          then
8521             if not Has_Completion (E)
8522               and then not (Is_Subprogram (E)
8523                             and then Is_Abstract_Subprogram (E))
8524               and then not (Is_Subprogram (E)
8525                               and then
8526                             (not Comes_From_Source (E)
8527                               or else Chars (E) = Name_uCall))
8528               and then Nkind (Parent (Unit_Declaration_Node (E))) /=
8529                                                        N_Compilation_Unit
8530               and then Chars (E) /= Name_uSize
8531             then
8532                Post_Error;
8533             end if;
8534
8535          elsif Is_Entry (E) then
8536             if not Has_Completion (E) and then
8537               (Ekind (Scope (E)) = E_Protected_Object
8538                 or else Ekind (Scope (E)) = E_Protected_Type)
8539             then
8540                Post_Error;
8541             end if;
8542
8543          elsif Is_Package_Or_Generic_Package (E) then
8544             if Unit_Requires_Body (E) then
8545                if not Has_Completion (E)
8546                  and then Nkind (Parent (Unit_Declaration_Node (E))) /=
8547                                                        N_Compilation_Unit
8548                then
8549                   Post_Error;
8550                end if;
8551
8552             elsif not Is_Child_Unit (E) then
8553                May_Need_Implicit_Body (E);
8554             end if;
8555
8556          elsif Ekind (E) = E_Incomplete_Type
8557            and then No (Underlying_Type (E))
8558          then
8559             Post_Error;
8560
8561          elsif (Ekind (E) = E_Task_Type or else
8562                 Ekind (E) = E_Protected_Type)
8563            and then not Has_Completion (E)
8564          then
8565             Post_Error;
8566
8567          --  A single task declared in the current scope is a constant, verify
8568          --  that the body of its anonymous type is in the same scope. If the
8569          --  task is defined elsewhere, this may be a renaming declaration for
8570          --  which no completion is needed.
8571
8572          elsif Ekind (E) = E_Constant
8573            and then Ekind (Etype (E)) = E_Task_Type
8574            and then not Has_Completion (Etype (E))
8575            and then Scope (Etype (E)) = Current_Scope
8576          then
8577             Post_Error;
8578
8579          elsif Ekind (E) = E_Protected_Object
8580            and then not Has_Completion (Etype (E))
8581          then
8582             Post_Error;
8583
8584          elsif Ekind (E) = E_Record_Type then
8585             if Is_Tagged_Type (E) then
8586                Check_Abstract_Overriding (E);
8587                Check_Conventions (E);
8588             end if;
8589
8590             Check_Aliased_Component_Types (E);
8591
8592          elsif Ekind (E) = E_Array_Type then
8593             Check_Aliased_Component_Types (E);
8594
8595          end if;
8596
8597          Next_Entity (E);
8598       end loop;
8599    end Check_Completion;
8600
8601    ----------------------------
8602    -- Check_Delta_Expression --
8603    ----------------------------
8604
8605    procedure Check_Delta_Expression (E : Node_Id) is
8606    begin
8607       if not (Is_Real_Type (Etype (E))) then
8608          Wrong_Type (E, Any_Real);
8609
8610       elsif not Is_OK_Static_Expression (E) then
8611          Flag_Non_Static_Expr
8612            ("non-static expression used for delta value!", E);
8613
8614       elsif not UR_Is_Positive (Expr_Value_R (E)) then
8615          Error_Msg_N ("delta expression must be positive", E);
8616
8617       else
8618          return;
8619       end if;
8620
8621       --  If any of above errors occurred, then replace the incorrect
8622       --  expression by the real 0.1, which should prevent further errors.
8623
8624       Rewrite (E,
8625         Make_Real_Literal (Sloc (E), Ureal_Tenth));
8626       Analyze_And_Resolve (E, Standard_Float);
8627    end Check_Delta_Expression;
8628
8629    -----------------------------
8630    -- Check_Digits_Expression --
8631    -----------------------------
8632
8633    procedure Check_Digits_Expression (E : Node_Id) is
8634    begin
8635       if not (Is_Integer_Type (Etype (E))) then
8636          Wrong_Type (E, Any_Integer);
8637
8638       elsif not Is_OK_Static_Expression (E) then
8639          Flag_Non_Static_Expr
8640            ("non-static expression used for digits value!", E);
8641
8642       elsif Expr_Value (E) <= 0 then
8643          Error_Msg_N ("digits value must be greater than zero", E);
8644
8645       else
8646          return;
8647       end if;
8648
8649       --  If any of above errors occurred, then replace the incorrect
8650       --  expression by the integer 1, which should prevent further errors.
8651
8652       Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
8653       Analyze_And_Resolve (E, Standard_Integer);
8654
8655    end Check_Digits_Expression;
8656
8657    --------------------------
8658    -- Check_Initialization --
8659    --------------------------
8660
8661    procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
8662    begin
8663       if Is_Limited_Type (T)
8664         and then not In_Instance
8665         and then not In_Inlined_Body
8666       then
8667          if not OK_For_Limited_Init (Exp) then
8668
8669             --  In GNAT mode, this is just a warning, to allow it to be evilly
8670             --  turned off. Otherwise it is a real error.
8671
8672             if GNAT_Mode then
8673                Error_Msg_N
8674                  ("?cannot initialize entities of limited type!", Exp);
8675
8676             elsif Ada_Version < Ada_05 then
8677                Error_Msg_N
8678                  ("cannot initialize entities of limited type", Exp);
8679                Explain_Limited_Type (T, Exp);
8680
8681             else
8682                --  Specialize error message according to kind of illegal
8683                --  initial expression.
8684
8685                if Nkind (Exp) = N_Type_Conversion
8686                  and then Nkind (Expression (Exp)) = N_Function_Call
8687                then
8688                   Error_Msg_N
8689                     ("illegal context for call"
8690                       & " to function with limited result", Exp);
8691
8692                else
8693                   Error_Msg_N
8694                     ("initialization of limited object requires aggregate "
8695                       & "or function call",  Exp);
8696                end if;
8697             end if;
8698          end if;
8699       end if;
8700    end Check_Initialization;
8701
8702    ------------------------------------
8703    -- Check_Or_Process_Discriminants --
8704    ------------------------------------
8705
8706    --  If an incomplete or private type declaration was already given for the
8707    --  type, the discriminants may have already been processed if they were
8708    --  present on the incomplete declaration. In this case a full conformance
8709    --  check is performed otherwise just process them.
8710
8711    procedure Check_Or_Process_Discriminants
8712      (N    : Node_Id;
8713       T    : Entity_Id;
8714       Prev : Entity_Id := Empty)
8715    is
8716    begin
8717       if Has_Discriminants (T) then
8718
8719          --  Make the discriminants visible to component declarations
8720
8721          declare
8722             D    : Entity_Id;
8723             Prev : Entity_Id;
8724
8725          begin
8726             D := First_Discriminant (T);
8727             while Present (D) loop
8728                Prev := Current_Entity (D);
8729                Set_Current_Entity (D);
8730                Set_Is_Immediately_Visible (D);
8731                Set_Homonym (D, Prev);
8732
8733                --  Ada 2005 (AI-230): Access discriminant allowed in
8734                --  non-limited record types.
8735
8736                if Ada_Version < Ada_05 then
8737
8738                   --  This restriction gets applied to the full type here. It
8739                   --  has already been applied earlier to the partial view.
8740
8741                   Check_Access_Discriminant_Requires_Limited (Parent (D), N);
8742                end if;
8743
8744                Next_Discriminant (D);
8745             end loop;
8746          end;
8747
8748       elsif Present (Discriminant_Specifications (N)) then
8749          Process_Discriminants (N, Prev);
8750       end if;
8751    end Check_Or_Process_Discriminants;
8752
8753    ----------------------
8754    -- Check_Real_Bound --
8755    ----------------------
8756
8757    procedure Check_Real_Bound (Bound : Node_Id) is
8758    begin
8759       if not Is_Real_Type (Etype (Bound)) then
8760          Error_Msg_N
8761            ("bound in real type definition must be of real type", Bound);
8762
8763       elsif not Is_OK_Static_Expression (Bound) then
8764          Flag_Non_Static_Expr
8765            ("non-static expression used for real type bound!", Bound);
8766
8767       else
8768          return;
8769       end if;
8770
8771       Rewrite
8772         (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
8773       Analyze (Bound);
8774       Resolve (Bound, Standard_Float);
8775    end Check_Real_Bound;
8776
8777    ------------------------------
8778    -- Complete_Private_Subtype --
8779    ------------------------------
8780
8781    procedure Complete_Private_Subtype
8782      (Priv        : Entity_Id;
8783       Full        : Entity_Id;
8784       Full_Base   : Entity_Id;
8785       Related_Nod : Node_Id)
8786    is
8787       Save_Next_Entity : Entity_Id;
8788       Save_Homonym     : Entity_Id;
8789
8790    begin
8791       --  Set semantic attributes for (implicit) private subtype completion.
8792       --  If the full type has no discriminants, then it is a copy of the full
8793       --  view of the base. Otherwise, it is a subtype of the base with a
8794       --  possible discriminant constraint. Save and restore the original
8795       --  Next_Entity field of full to ensure that the calls to Copy_Node
8796       --  do not corrupt the entity chain.
8797
8798       --  Note that the type of the full view is the same entity as the type of
8799       --  the partial view. In this fashion, the subtype has access to the
8800       --  correct view of the parent.
8801
8802       Save_Next_Entity := Next_Entity (Full);
8803       Save_Homonym     := Homonym (Priv);
8804
8805       case Ekind (Full_Base) is
8806          when E_Record_Type    |
8807               E_Record_Subtype |
8808               Class_Wide_Kind  |
8809               Private_Kind     |
8810               Task_Kind        |
8811               Protected_Kind   =>
8812             Copy_Node (Priv, Full);
8813
8814             Set_Has_Discriminants  (Full, Has_Discriminants (Full_Base));
8815             Set_First_Entity       (Full, First_Entity (Full_Base));
8816             Set_Last_Entity        (Full, Last_Entity (Full_Base));
8817
8818          when others =>
8819             Copy_Node (Full_Base, Full);
8820             Set_Chars          (Full, Chars (Priv));
8821             Conditional_Delay  (Full, Priv);
8822             Set_Sloc           (Full, Sloc (Priv));
8823       end case;
8824
8825       Set_Next_Entity (Full, Save_Next_Entity);
8826       Set_Homonym     (Full, Save_Homonym);
8827       Set_Associated_Node_For_Itype (Full, Related_Nod);
8828
8829       --  Set common attributes for all subtypes
8830
8831       Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
8832
8833       --  The Etype of the full view is inconsistent. Gigi needs to see the
8834       --  structural full view,  which is what the current scheme gives:
8835       --  the Etype of the full view is the etype of the full base. However,
8836       --  if the full base is a derived type, the full view then looks like
8837       --  a subtype of the parent, not a subtype of the full base. If instead
8838       --  we write:
8839
8840       --       Set_Etype (Full, Full_Base);
8841
8842       --  then we get inconsistencies in the front-end (confusion between
8843       --  views). Several outstanding bugs are related to this ???
8844
8845       Set_Is_First_Subtype (Full, False);
8846       Set_Scope            (Full, Scope (Priv));
8847       Set_Size_Info        (Full, Full_Base);
8848       Set_RM_Size          (Full, RM_Size (Full_Base));
8849       Set_Is_Itype         (Full);
8850
8851       --  A subtype of a private-type-without-discriminants, whose full-view
8852       --  has discriminants with default expressions, is not constrained!
8853
8854       if not Has_Discriminants (Priv) then
8855          Set_Is_Constrained (Full, Is_Constrained (Full_Base));
8856
8857          if Has_Discriminants (Full_Base) then
8858             Set_Discriminant_Constraint
8859               (Full, Discriminant_Constraint (Full_Base));
8860
8861             --  The partial view may have been indefinite, the full view
8862             --  might not be.
8863
8864             Set_Has_Unknown_Discriminants
8865               (Full, Has_Unknown_Discriminants (Full_Base));
8866          end if;
8867       end if;
8868
8869       Set_First_Rep_Item     (Full, First_Rep_Item (Full_Base));
8870       Set_Depends_On_Private (Full, Has_Private_Component (Full));
8871
8872       --  Freeze the private subtype entity if its parent is delayed, and not
8873       --  already frozen. We skip this processing if the type is an anonymous
8874       --  subtype of a record component, or is the corresponding record of a
8875       --  protected type, since ???
8876
8877       if not Is_Type (Scope (Full)) then
8878          Set_Has_Delayed_Freeze (Full,
8879            Has_Delayed_Freeze (Full_Base)
8880              and then (not Is_Frozen (Full_Base)));
8881       end if;
8882
8883       Set_Freeze_Node (Full, Empty);
8884       Set_Is_Frozen (Full, False);
8885       Set_Full_View (Priv, Full);
8886
8887       if Has_Discriminants (Full) then
8888          Set_Stored_Constraint_From_Discriminant_Constraint (Full);
8889          Set_Stored_Constraint (Priv, Stored_Constraint (Full));
8890
8891          if Has_Unknown_Discriminants (Full) then
8892             Set_Discriminant_Constraint (Full, No_Elist);
8893          end if;
8894       end if;
8895
8896       if Ekind (Full_Base) = E_Record_Type
8897         and then Has_Discriminants (Full_Base)
8898         and then Has_Discriminants (Priv) -- might not, if errors
8899         and then not Has_Unknown_Discriminants (Priv)
8900         and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
8901       then
8902          Create_Constrained_Components
8903            (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
8904
8905       --  If the full base is itself derived from private, build a congruent
8906       --  subtype of its underlying type, for use by the back end. For a
8907       --  constrained record component, the declaration cannot be placed on
8908       --  the component list, but it must nevertheless be built an analyzed, to
8909       --  supply enough information for Gigi to compute the size of component.
8910
8911       elsif Ekind (Full_Base) in Private_Kind
8912         and then Is_Derived_Type (Full_Base)
8913         and then Has_Discriminants (Full_Base)
8914         and then (Ekind (Current_Scope) /= E_Record_Subtype)
8915       then
8916          if not Is_Itype (Priv)
8917            and then
8918              Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
8919          then
8920             Build_Underlying_Full_View
8921               (Parent (Priv), Full, Etype (Full_Base));
8922
8923          elsif Nkind (Related_Nod) = N_Component_Declaration then
8924             Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
8925          end if;
8926
8927       elsif Is_Record_Type (Full_Base) then
8928
8929          --  Show Full is simply a renaming of Full_Base
8930
8931          Set_Cloned_Subtype (Full, Full_Base);
8932       end if;
8933
8934       --  It is unsafe to share to bounds of a scalar type, because the Itype
8935       --  is elaborated on demand, and if a bound is non-static then different
8936       --  orders of elaboration in different units will lead to different
8937       --  external symbols.
8938
8939       if Is_Scalar_Type (Full_Base) then
8940          Set_Scalar_Range (Full,
8941            Make_Range (Sloc (Related_Nod),
8942              Low_Bound  =>
8943                Duplicate_Subexpr_No_Checks (Type_Low_Bound  (Full_Base)),
8944              High_Bound =>
8945                Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
8946
8947          --  This completion inherits the bounds of the full parent, but if
8948          --  the parent is an unconstrained floating point type, so is the
8949          --  completion.
8950
8951          if Is_Floating_Point_Type (Full_Base) then
8952             Set_Includes_Infinities
8953              (Scalar_Range (Full), Has_Infinities (Full_Base));
8954          end if;
8955       end if;
8956
8957       --  ??? It seems that a lot of fields are missing that should be copied
8958       --  from Full_Base to Full. Here are some that are introduced in a
8959       --  non-disruptive way but a cleanup is necessary.
8960
8961       if Is_Tagged_Type (Full_Base) then
8962          Set_Is_Tagged_Type (Full);
8963          Set_Primitive_Operations (Full, Primitive_Operations (Full_Base));
8964          Set_Class_Wide_Type      (Full, Class_Wide_Type (Full_Base));
8965
8966       --  If this is a subtype of a protected or task type, constrain its
8967       --  corresponding record, unless this is a subtype without constraints,
8968       --  i.e. a simple renaming as with an actual subtype in an instance.
8969
8970       elsif Is_Concurrent_Type (Full_Base) then
8971          if Has_Discriminants (Full)
8972            and then Present (Corresponding_Record_Type (Full_Base))
8973            and then
8974              not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
8975          then
8976             Set_Corresponding_Record_Type (Full,
8977               Constrain_Corresponding_Record
8978                 (Full, Corresponding_Record_Type (Full_Base),
8979                   Related_Nod, Full_Base));
8980
8981          else
8982             Set_Corresponding_Record_Type (Full,
8983               Corresponding_Record_Type (Full_Base));
8984          end if;
8985       end if;
8986    end Complete_Private_Subtype;
8987
8988    ----------------------------
8989    -- Constant_Redeclaration --
8990    ----------------------------
8991
8992    procedure Constant_Redeclaration
8993      (Id : Entity_Id;
8994       N  : Node_Id;
8995       T  : out Entity_Id)
8996    is
8997       Prev    : constant Entity_Id := Current_Entity_In_Scope (Id);
8998       Obj_Def : constant Node_Id := Object_Definition (N);
8999       New_T   : Entity_Id;
9000
9001       procedure Check_Possible_Deferred_Completion
9002         (Prev_Id      : Entity_Id;
9003          Prev_Obj_Def : Node_Id;
9004          Curr_Obj_Def : Node_Id);
9005       --  Determine whether the two object definitions describe the partial
9006       --  and the full view of a constrained deferred constant. Generate
9007       --  a subtype for the full view and verify that it statically matches
9008       --  the subtype of the partial view.
9009
9010       procedure Check_Recursive_Declaration (Typ : Entity_Id);
9011       --  If deferred constant is an access type initialized with an allocator,
9012       --  check whether there is an illegal recursion in the definition,
9013       --  through a default value of some record subcomponent. This is normally
9014       --  detected when generating init procs, but requires this additional
9015       --  mechanism when expansion is disabled.
9016
9017       ----------------------------------------
9018       -- Check_Possible_Deferred_Completion --
9019       ----------------------------------------
9020
9021       procedure Check_Possible_Deferred_Completion
9022         (Prev_Id      : Entity_Id;
9023          Prev_Obj_Def : Node_Id;
9024          Curr_Obj_Def : Node_Id)
9025       is
9026       begin
9027          if Nkind (Prev_Obj_Def) = N_Subtype_Indication
9028            and then Present (Constraint (Prev_Obj_Def))
9029            and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
9030            and then Present (Constraint (Curr_Obj_Def))
9031          then
9032             declare
9033                Loc    : constant Source_Ptr := Sloc (N);
9034                Def_Id : constant Entity_Id :=
9035                           Make_Defining_Identifier (Loc,
9036                             New_Internal_Name ('S'));
9037                Decl   : constant Node_Id :=
9038                           Make_Subtype_Declaration (Loc,
9039                             Defining_Identifier =>
9040                               Def_Id,
9041                             Subtype_Indication =>
9042                               Relocate_Node (Curr_Obj_Def));
9043
9044             begin
9045                Insert_Before_And_Analyze (N, Decl);
9046                Set_Etype (Id, Def_Id);
9047
9048                if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
9049                   Error_Msg_Sloc := Sloc (Prev_Id);
9050                   Error_Msg_N ("subtype does not statically match deferred " &
9051                                "declaration#", N);
9052                end if;
9053             end;
9054          end if;
9055       end Check_Possible_Deferred_Completion;
9056
9057       ---------------------------------
9058       -- Check_Recursive_Declaration --
9059       ---------------------------------
9060
9061       procedure Check_Recursive_Declaration (Typ : Entity_Id) is
9062          Comp : Entity_Id;
9063
9064       begin
9065          if Is_Record_Type (Typ) then
9066             Comp := First_Component (Typ);
9067             while Present (Comp) loop
9068                if Comes_From_Source (Comp) then
9069                   if Present (Expression (Parent (Comp)))
9070                     and then Is_Entity_Name (Expression (Parent (Comp)))
9071                     and then Entity (Expression (Parent (Comp))) = Prev
9072                   then
9073                      Error_Msg_Sloc := Sloc (Parent (Comp));
9074                      Error_Msg_NE
9075                        ("illegal circularity with declaration for&#",
9076                          N, Comp);
9077                      return;
9078
9079                   elsif Is_Record_Type (Etype (Comp)) then
9080                      Check_Recursive_Declaration (Etype (Comp));
9081                   end if;
9082                end if;
9083
9084                Next_Component (Comp);
9085             end loop;
9086          end if;
9087       end Check_Recursive_Declaration;
9088
9089    --  Start of processing for Constant_Redeclaration
9090
9091    begin
9092       if Nkind (Parent (Prev)) = N_Object_Declaration then
9093          if Nkind (Object_Definition
9094                      (Parent (Prev))) = N_Subtype_Indication
9095          then
9096             --  Find type of new declaration. The constraints of the two
9097             --  views must match statically, but there is no point in
9098             --  creating an itype for the full view.
9099
9100             if Nkind (Obj_Def) = N_Subtype_Indication then
9101                Find_Type (Subtype_Mark (Obj_Def));
9102                New_T := Entity (Subtype_Mark (Obj_Def));
9103
9104             else
9105                Find_Type (Obj_Def);
9106                New_T := Entity (Obj_Def);
9107             end if;
9108
9109             T := Etype (Prev);
9110
9111          else
9112             --  The full view may impose a constraint, even if the partial
9113             --  view does not, so construct the subtype.
9114
9115             New_T := Find_Type_Of_Object (Obj_Def, N);
9116             T     := New_T;
9117          end if;
9118
9119       else
9120          --  Current declaration is illegal, diagnosed below in Enter_Name
9121
9122          T := Empty;
9123          New_T := Any_Type;
9124       end if;
9125
9126       --  If previous full declaration exists, or if a homograph is present,
9127       --  let Enter_Name handle it, either with an error, or with the removal
9128       --  of an overridden implicit subprogram.
9129
9130       if Ekind (Prev) /= E_Constant
9131         or else Present (Expression (Parent (Prev)))
9132         or else Present (Full_View (Prev))
9133       then
9134          Enter_Name (Id);
9135
9136       --  Verify that types of both declarations match, or else that both types
9137       --  are anonymous access types whose designated subtypes statically match
9138       --  (as allowed in Ada 2005 by AI-385).
9139
9140       elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
9141         and then
9142           (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
9143              or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
9144              or else Is_Access_Constant (Etype (New_T)) /=
9145                      Is_Access_Constant (Etype (Prev))
9146              or else Can_Never_Be_Null (Etype (New_T)) /=
9147                      Can_Never_Be_Null (Etype (Prev))
9148              or else Null_Exclusion_Present (Parent (Prev)) /=
9149                      Null_Exclusion_Present (Parent (Id))
9150              or else not Subtypes_Statically_Match
9151                            (Designated_Type (Etype (Prev)),
9152                             Designated_Type (Etype (New_T))))
9153       then
9154          Error_Msg_Sloc := Sloc (Prev);
9155          Error_Msg_N ("type does not match declaration#", N);
9156          Set_Full_View (Prev, Id);
9157          Set_Etype (Id, Any_Type);
9158
9159       elsif
9160         Null_Exclusion_Present (Parent (Prev))
9161           and then not Null_Exclusion_Present (N)
9162       then
9163          Error_Msg_Sloc := Sloc (Prev);
9164          Error_Msg_N ("null-exclusion does not match declaration#", N);
9165          Set_Full_View (Prev, Id);
9166          Set_Etype (Id, Any_Type);
9167
9168       --  If so, process the full constant declaration
9169
9170       else
9171          --  RM 7.4 (6): If the subtype defined by the subtype_indication in
9172          --  the deferred declaration is constrained, then the subtype defined
9173          --  by the subtype_indication in the full declaration shall match it
9174          --  statically.
9175
9176          Check_Possible_Deferred_Completion
9177            (Prev_Id      => Prev,
9178             Prev_Obj_Def => Object_Definition (Parent (Prev)),
9179             Curr_Obj_Def => Obj_Def);
9180
9181          Set_Full_View (Prev, Id);
9182          Set_Is_Public (Id, Is_Public (Prev));
9183          Set_Is_Internal (Id);
9184          Append_Entity (Id, Current_Scope);
9185
9186          --  Check ALIASED present if present before (RM 7.4(7))
9187
9188          if Is_Aliased (Prev)
9189            and then not Aliased_Present (N)
9190          then
9191             Error_Msg_Sloc := Sloc (Prev);
9192             Error_Msg_N ("ALIASED required (see declaration#)", N);
9193          end if;
9194
9195          --  Allow incomplete declaration of tags (used to handle forward
9196          --  references to tags). The check on Ada_Tags avoids circularities
9197          --  when rebuilding the compiler.
9198
9199          if RTU_Loaded (Ada_Tags)
9200            and then T = RTE (RE_Tag)
9201          then
9202             null;
9203
9204          --  Check that placement is in private part and that the incomplete
9205          --  declaration appeared in the visible part.
9206
9207          elsif Ekind (Current_Scope) = E_Package
9208            and then not In_Private_Part (Current_Scope)
9209          then
9210             Error_Msg_Sloc := Sloc (Prev);
9211             Error_Msg_N ("full constant for declaration#"
9212                          & " must be in private part", N);
9213
9214          elsif Ekind (Current_Scope) = E_Package
9215            and then List_Containing (Parent (Prev))
9216            /= Visible_Declarations
9217              (Specification (Unit_Declaration_Node (Current_Scope)))
9218          then
9219             Error_Msg_N
9220               ("deferred constant must be declared in visible part",
9221                  Parent (Prev));
9222          end if;
9223
9224          if Is_Access_Type (T)
9225            and then Nkind (Expression (N)) = N_Allocator
9226          then
9227             Check_Recursive_Declaration (Designated_Type (T));
9228          end if;
9229       end if;
9230    end Constant_Redeclaration;
9231
9232    ----------------------
9233    -- Constrain_Access --
9234    ----------------------
9235
9236    procedure Constrain_Access
9237      (Def_Id      : in out Entity_Id;
9238       S           : Node_Id;
9239       Related_Nod : Node_Id)
9240    is
9241       T             : constant Entity_Id := Entity (Subtype_Mark (S));
9242       Desig_Type    : constant Entity_Id := Designated_Type (T);
9243       Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
9244       Constraint_OK : Boolean := True;
9245
9246       function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
9247       --  Simple predicate to test for defaulted discriminants
9248       --  Shouldn't this be in sem_util???
9249
9250       ---------------------------------
9251       -- Has_Defaulted_Discriminants --
9252       ---------------------------------
9253
9254       function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
9255       begin
9256          return Has_Discriminants (Typ)
9257           and then Present (First_Discriminant (Typ))
9258           and then Present
9259             (Discriminant_Default_Value (First_Discriminant (Typ)));
9260       end Has_Defaulted_Discriminants;
9261
9262    --  Start of processing for Constrain_Access
9263
9264    begin
9265       if Is_Array_Type (Desig_Type) then
9266          Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
9267
9268       elsif (Is_Record_Type (Desig_Type)
9269               or else Is_Incomplete_Or_Private_Type (Desig_Type))
9270         and then not Is_Constrained (Desig_Type)
9271       then
9272          --  ??? The following code is a temporary kludge to ignore a
9273          --  discriminant constraint on access type if it is constraining
9274          --  the current record. Avoid creating the implicit subtype of the
9275          --  record we are currently compiling since right now, we cannot
9276          --  handle these. For now, just return the access type itself.
9277
9278          if Desig_Type = Current_Scope
9279            and then No (Def_Id)
9280          then
9281             Set_Ekind (Desig_Subtype, E_Record_Subtype);
9282             Def_Id := Entity (Subtype_Mark (S));
9283
9284             --  This call added to ensure that the constraint is analyzed
9285             --  (needed for a B test). Note that we still return early from
9286             --  this procedure to avoid recursive processing. ???
9287
9288             Constrain_Discriminated_Type
9289               (Desig_Subtype, S, Related_Nod, For_Access => True);
9290             return;
9291          end if;
9292
9293          if (Ekind (T) = E_General_Access_Type
9294               or else Ada_Version >= Ada_05)
9295            and then Has_Private_Declaration (Desig_Type)
9296            and then In_Open_Scopes (Scope (Desig_Type))
9297            and then Has_Discriminants (Desig_Type)
9298          then
9299             --  Enforce rule that the constraint is illegal if there is
9300             --  an unconstrained view of the designated type. This means
9301             --  that the partial view (either a private type declaration or
9302             --  a derivation from a private type) has no discriminants.
9303             --  (Defect Report 8652/0008, Technical Corrigendum 1, checked
9304             --  by ACATS B371001).
9305
9306             --  Rule updated for Ada 2005: the private type is said to have
9307             --  a constrained partial view, given that objects of the type
9308             --  can be declared. Furthermore, the rule applies to all access
9309             --  types, unlike the rule concerning default discriminants.
9310
9311             declare
9312                Pack  : constant Node_Id :=
9313                          Unit_Declaration_Node (Scope (Desig_Type));
9314                Decls : List_Id;
9315                Decl  : Node_Id;
9316
9317             begin
9318                if Nkind (Pack) = N_Package_Declaration then
9319                   Decls := Visible_Declarations (Specification (Pack));
9320                   Decl := First (Decls);
9321                   while Present (Decl) loop
9322                      if (Nkind (Decl) = N_Private_Type_Declaration
9323                           and then
9324                             Chars (Defining_Identifier (Decl)) =
9325                                                      Chars (Desig_Type))
9326
9327                        or else
9328                         (Nkind (Decl) = N_Full_Type_Declaration
9329                           and then
9330                             Chars (Defining_Identifier (Decl)) =
9331                                                      Chars (Desig_Type)
9332                           and then Is_Derived_Type (Desig_Type)
9333                           and then
9334                             Has_Private_Declaration (Etype (Desig_Type)))
9335                      then
9336                         if No (Discriminant_Specifications (Decl)) then
9337                            Error_Msg_N
9338                             ("cannot constrain general access type if " &
9339                                "designated type has constrained partial view",
9340                                 S);
9341                         end if;
9342
9343                         exit;
9344                      end if;
9345
9346                      Next (Decl);
9347                   end loop;
9348                end if;
9349             end;
9350          end if;
9351
9352          Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
9353            For_Access => True);
9354
9355       elsif (Is_Task_Type (Desig_Type)
9356               or else Is_Protected_Type (Desig_Type))
9357         and then not Is_Constrained (Desig_Type)
9358       then
9359          Constrain_Concurrent
9360            (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
9361
9362       else
9363          Error_Msg_N ("invalid constraint on access type", S);
9364          Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
9365          Constraint_OK := False;
9366       end if;
9367
9368       if No (Def_Id) then
9369          Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
9370       else
9371          Set_Ekind (Def_Id, E_Access_Subtype);
9372       end if;
9373
9374       if Constraint_OK then
9375          Set_Etype (Def_Id, Base_Type (T));
9376
9377          if Is_Private_Type (Desig_Type) then
9378             Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
9379          end if;
9380       else
9381          Set_Etype (Def_Id, Any_Type);
9382       end if;
9383
9384       Set_Size_Info                (Def_Id, T);
9385       Set_Is_Constrained           (Def_Id, Constraint_OK);
9386       Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
9387       Set_Depends_On_Private       (Def_Id, Has_Private_Component (Def_Id));
9388       Set_Is_Access_Constant       (Def_Id, Is_Access_Constant (T));
9389
9390       Conditional_Delay (Def_Id, T);
9391
9392       --  AI-363 : Subtypes of general access types whose designated types have
9393       --  default discriminants are disallowed. In instances, the rule has to
9394       --  be checked against the actual, of which T is the subtype. In a
9395       --  generic body, the rule is checked assuming that the actual type has
9396       --  defaulted discriminants.
9397
9398       if Ada_Version >= Ada_05 or else Warn_On_Ada_2005_Compatibility then
9399          if Ekind (Base_Type (T)) = E_General_Access_Type
9400            and then Has_Defaulted_Discriminants (Desig_Type)
9401          then
9402             if Ada_Version < Ada_05 then
9403                Error_Msg_N
9404                  ("access subtype of general access type would not " &
9405                   "be allowed in Ada 2005?", S);
9406             else
9407                Error_Msg_N
9408                  ("access subype of general access type not allowed", S);
9409             end if;
9410
9411             Error_Msg_N ("\discriminants have defaults", S);
9412
9413          elsif Is_Access_Type (T)
9414            and then Is_Generic_Type (Desig_Type)
9415            and then Has_Discriminants (Desig_Type)
9416            and then In_Package_Body (Current_Scope)
9417          then
9418             if Ada_Version < Ada_05 then
9419                Error_Msg_N
9420                  ("access subtype would not be allowed in generic body " &
9421                   "in Ada 2005?", S);
9422             else
9423                Error_Msg_N
9424                  ("access subtype not allowed in generic body", S);
9425             end if;
9426
9427             Error_Msg_N
9428               ("\designated type is a discriminated formal", S);
9429          end if;
9430       end if;
9431    end Constrain_Access;
9432
9433    ---------------------
9434    -- Constrain_Array --
9435    ---------------------
9436
9437    procedure Constrain_Array
9438      (Def_Id      : in out Entity_Id;
9439       SI          : Node_Id;
9440       Related_Nod : Node_Id;
9441       Related_Id  : Entity_Id;
9442       Suffix      : Character)
9443    is
9444       C                     : constant Node_Id := Constraint (SI);
9445       Number_Of_Constraints : Nat := 0;
9446       Index                 : Node_Id;
9447       S, T                  : Entity_Id;
9448       Constraint_OK         : Boolean := True;
9449
9450    begin
9451       T := Entity (Subtype_Mark (SI));
9452
9453       if Ekind (T) in Access_Kind then
9454          T := Designated_Type (T);
9455       end if;
9456
9457       --  If an index constraint follows a subtype mark in a subtype indication
9458       --  then the type or subtype denoted by the subtype mark must not already
9459       --  impose an index constraint. The subtype mark must denote either an
9460       --  unconstrained array type or an access type whose designated type
9461       --  is such an array type... (RM 3.6.1)
9462
9463       if Is_Constrained (T) then
9464          Error_Msg_N
9465            ("array type is already constrained", Subtype_Mark (SI));
9466          Constraint_OK := False;
9467
9468       else
9469          S := First (Constraints (C));
9470          while Present (S) loop
9471             Number_Of_Constraints := Number_Of_Constraints + 1;
9472             Next (S);
9473          end loop;
9474
9475          --  In either case, the index constraint must provide a discrete
9476          --  range for each index of the array type and the type of each
9477          --  discrete range must be the same as that of the corresponding
9478          --  index. (RM 3.6.1)
9479
9480          if Number_Of_Constraints /= Number_Dimensions (T) then
9481             Error_Msg_NE ("incorrect number of index constraints for }", C, T);
9482             Constraint_OK := False;
9483
9484          else
9485             S := First (Constraints (C));
9486             Index := First_Index (T);
9487             Analyze (Index);
9488
9489             --  Apply constraints to each index type
9490
9491             for J in 1 .. Number_Of_Constraints loop
9492                Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
9493                Next (Index);
9494                Next (S);
9495             end loop;
9496
9497          end if;
9498       end if;
9499
9500       if No (Def_Id) then
9501          Def_Id :=
9502            Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
9503          Set_Parent (Def_Id, Related_Nod);
9504
9505       else
9506          Set_Ekind (Def_Id, E_Array_Subtype);
9507       end if;
9508
9509       Set_Size_Info      (Def_Id,                (T));
9510       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9511       Set_Etype          (Def_Id, Base_Type      (T));
9512
9513       if Constraint_OK then
9514          Set_First_Index (Def_Id, First (Constraints (C)));
9515       else
9516          Set_First_Index (Def_Id, First_Index (T));
9517       end if;
9518
9519       Set_Is_Constrained     (Def_Id, True);
9520       Set_Is_Aliased         (Def_Id, Is_Aliased (T));
9521       Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
9522
9523       Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
9524       Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
9525
9526       --  A subtype does not inherit the packed_array_type of is parent. We
9527       --  need to initialize the attribute because if Def_Id is previously
9528       --  analyzed through a limited_with clause, it will have the attributes
9529       --  of an incomplete type, one of which is an Elist that overlaps the
9530       --  Packed_Array_Type field.
9531
9532       Set_Packed_Array_Type (Def_Id, Empty);
9533
9534       --  Build a freeze node if parent still needs one. Also make sure that
9535       --  the Depends_On_Private status is set because the subtype will need
9536       --  reprocessing at the time the base type does, and also we must set a
9537       --  conditional delay.
9538
9539       Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9540       Conditional_Delay (Def_Id, T);
9541    end Constrain_Array;
9542
9543    ------------------------------
9544    -- Constrain_Component_Type --
9545    ------------------------------
9546
9547    function Constrain_Component_Type
9548      (Comp            : Entity_Id;
9549       Constrained_Typ : Entity_Id;
9550       Related_Node    : Node_Id;
9551       Typ             : Entity_Id;
9552       Constraints     : Elist_Id) return Entity_Id
9553    is
9554       Loc         : constant Source_Ptr := Sloc (Constrained_Typ);
9555       Compon_Type : constant Entity_Id := Etype (Comp);
9556
9557       function Build_Constrained_Array_Type
9558         (Old_Type : Entity_Id) return Entity_Id;
9559       --  If Old_Type is an array type, one of whose indices is constrained
9560       --  by a discriminant, build an Itype whose constraint replaces the
9561       --  discriminant with its value in the constraint.
9562
9563       function Build_Constrained_Discriminated_Type
9564         (Old_Type : Entity_Id) return Entity_Id;
9565       --  Ditto for record components
9566
9567       function Build_Constrained_Access_Type
9568         (Old_Type : Entity_Id) return Entity_Id;
9569       --  Ditto for access types. Makes use of previous two functions, to
9570       --  constrain designated type.
9571
9572       function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
9573       --  T is an array or discriminated type, C is a list of constraints
9574       --  that apply to T. This routine builds the constrained subtype.
9575
9576       function Is_Discriminant (Expr : Node_Id) return Boolean;
9577       --  Returns True if Expr is a discriminant
9578
9579       function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
9580       --  Find the value of discriminant Discrim in Constraint
9581
9582       -----------------------------------
9583       -- Build_Constrained_Access_Type --
9584       -----------------------------------
9585
9586       function Build_Constrained_Access_Type
9587         (Old_Type : Entity_Id) return Entity_Id
9588       is
9589          Desig_Type    : constant Entity_Id := Designated_Type (Old_Type);
9590          Itype         : Entity_Id;
9591          Desig_Subtype : Entity_Id;
9592          Scop          : Entity_Id;
9593
9594       begin
9595          --  if the original access type was not embedded in the enclosing
9596          --  type definition, there is no need to produce a new access
9597          --  subtype. In fact every access type with an explicit constraint
9598          --  generates an itype whose scope is the enclosing record.
9599
9600          if not Is_Type (Scope (Old_Type)) then
9601             return Old_Type;
9602
9603          elsif Is_Array_Type (Desig_Type) then
9604             Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
9605
9606          elsif Has_Discriminants (Desig_Type) then
9607
9608             --  This may be an access type to an enclosing record type for
9609             --  which we are constructing the constrained components. Return
9610             --  the enclosing record subtype. This is not always correct,
9611             --  but avoids infinite recursion. ???
9612
9613             Desig_Subtype := Any_Type;
9614
9615             for J in reverse 0 .. Scope_Stack.Last loop
9616                Scop := Scope_Stack.Table (J).Entity;
9617
9618                if Is_Type (Scop)
9619                  and then Base_Type (Scop) = Base_Type (Desig_Type)
9620                then
9621                   Desig_Subtype := Scop;
9622                end if;
9623
9624                exit when not Is_Type (Scop);
9625             end loop;
9626
9627             if Desig_Subtype = Any_Type then
9628                Desig_Subtype :=
9629                  Build_Constrained_Discriminated_Type (Desig_Type);
9630             end if;
9631
9632          else
9633             return Old_Type;
9634          end if;
9635
9636          if Desig_Subtype /= Desig_Type then
9637
9638             --  The Related_Node better be here or else we won't be able
9639             --  to attach new itypes to a node in the tree.
9640
9641             pragma Assert (Present (Related_Node));
9642
9643             Itype := Create_Itype (E_Access_Subtype, Related_Node);
9644
9645             Set_Etype                    (Itype, Base_Type      (Old_Type));
9646             Set_Size_Info                (Itype,                (Old_Type));
9647             Set_Directly_Designated_Type (Itype, Desig_Subtype);
9648             Set_Depends_On_Private       (Itype, Has_Private_Component
9649                                                                 (Old_Type));
9650             Set_Is_Access_Constant       (Itype, Is_Access_Constant
9651                                                                 (Old_Type));
9652
9653             --  The new itype needs freezing when it depends on a not frozen
9654             --  type and the enclosing subtype needs freezing.
9655
9656             if Has_Delayed_Freeze (Constrained_Typ)
9657               and then not Is_Frozen (Constrained_Typ)
9658             then
9659                Conditional_Delay (Itype, Base_Type (Old_Type));
9660             end if;
9661
9662             return Itype;
9663
9664          else
9665             return Old_Type;
9666          end if;
9667       end Build_Constrained_Access_Type;
9668
9669       ----------------------------------
9670       -- Build_Constrained_Array_Type --
9671       ----------------------------------
9672
9673       function Build_Constrained_Array_Type
9674         (Old_Type : Entity_Id) return Entity_Id
9675       is
9676          Lo_Expr     : Node_Id;
9677          Hi_Expr     : Node_Id;
9678          Old_Index   : Node_Id;
9679          Range_Node  : Node_Id;
9680          Constr_List : List_Id;
9681
9682          Need_To_Create_Itype : Boolean := False;
9683
9684       begin
9685          Old_Index := First_Index (Old_Type);
9686          while Present (Old_Index) loop
9687             Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
9688
9689             if Is_Discriminant (Lo_Expr)
9690               or else Is_Discriminant (Hi_Expr)
9691             then
9692                Need_To_Create_Itype := True;
9693             end if;
9694
9695             Next_Index (Old_Index);
9696          end loop;
9697
9698          if Need_To_Create_Itype then
9699             Constr_List := New_List;
9700
9701             Old_Index := First_Index (Old_Type);
9702             while Present (Old_Index) loop
9703                Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
9704
9705                if Is_Discriminant (Lo_Expr) then
9706                   Lo_Expr := Get_Discr_Value (Lo_Expr);
9707                end if;
9708
9709                if Is_Discriminant (Hi_Expr) then
9710                   Hi_Expr := Get_Discr_Value (Hi_Expr);
9711                end if;
9712
9713                Range_Node :=
9714                  Make_Range
9715                    (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
9716
9717                Append (Range_Node, To => Constr_List);
9718
9719                Next_Index (Old_Index);
9720             end loop;
9721
9722             return Build_Subtype (Old_Type, Constr_List);
9723
9724          else
9725             return Old_Type;
9726          end if;
9727       end Build_Constrained_Array_Type;
9728
9729       ------------------------------------------
9730       -- Build_Constrained_Discriminated_Type --
9731       ------------------------------------------
9732
9733       function Build_Constrained_Discriminated_Type
9734         (Old_Type : Entity_Id) return Entity_Id
9735       is
9736          Expr           : Node_Id;
9737          Constr_List    : List_Id;
9738          Old_Constraint : Elmt_Id;
9739
9740          Need_To_Create_Itype : Boolean := False;
9741
9742       begin
9743          Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
9744          while Present (Old_Constraint) loop
9745             Expr := Node (Old_Constraint);
9746
9747             if Is_Discriminant (Expr) then
9748                Need_To_Create_Itype := True;
9749             end if;
9750
9751             Next_Elmt (Old_Constraint);
9752          end loop;
9753
9754          if Need_To_Create_Itype then
9755             Constr_List := New_List;
9756
9757             Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
9758             while Present (Old_Constraint) loop
9759                Expr := Node (Old_Constraint);
9760
9761                if Is_Discriminant (Expr) then
9762                   Expr := Get_Discr_Value (Expr);
9763                end if;
9764
9765                Append (New_Copy_Tree (Expr), To => Constr_List);
9766
9767                Next_Elmt (Old_Constraint);
9768             end loop;
9769
9770             return Build_Subtype (Old_Type, Constr_List);
9771
9772          else
9773             return Old_Type;
9774          end if;
9775       end Build_Constrained_Discriminated_Type;
9776
9777       -------------------
9778       -- Build_Subtype --
9779       -------------------
9780
9781       function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
9782          Indic       : Node_Id;
9783          Subtyp_Decl : Node_Id;
9784          Def_Id      : Entity_Id;
9785          Btyp        : Entity_Id := Base_Type (T);
9786
9787       begin
9788          --  The Related_Node better be here or else we won't be able to
9789          --  attach new itypes to a node in the tree.
9790
9791          pragma Assert (Present (Related_Node));
9792
9793          --  If the view of the component's type is incomplete or private
9794          --  with unknown discriminants, then the constraint must be applied
9795          --  to the full type.
9796
9797          if Has_Unknown_Discriminants (Btyp)
9798            and then Present (Underlying_Type (Btyp))
9799          then
9800             Btyp := Underlying_Type (Btyp);
9801          end if;
9802
9803          Indic :=
9804            Make_Subtype_Indication (Loc,
9805              Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
9806              Constraint   => Make_Index_Or_Discriminant_Constraint (Loc, C));
9807
9808          Def_Id := Create_Itype (Ekind (T), Related_Node);
9809
9810          Subtyp_Decl :=
9811            Make_Subtype_Declaration (Loc,
9812              Defining_Identifier => Def_Id,
9813              Subtype_Indication  => Indic);
9814
9815          Set_Parent (Subtyp_Decl, Parent (Related_Node));
9816
9817          --  Itypes must be analyzed with checks off (see package Itypes)
9818
9819          Analyze (Subtyp_Decl, Suppress => All_Checks);
9820
9821          return Def_Id;
9822       end Build_Subtype;
9823
9824       ---------------------
9825       -- Get_Discr_Value --
9826       ---------------------
9827
9828       function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
9829          D : Entity_Id;
9830          E : Elmt_Id;
9831          G : Elmt_Id;
9832
9833       begin
9834          --  The discriminant may be declared for the type, in which case we
9835          --  find it by iterating over the list of discriminants. If the
9836          --  discriminant is inherited from a parent type, it appears as the
9837          --  corresponding discriminant of the current type. This will be the
9838          --  case when constraining an inherited component whose constraint is
9839          --  given by a discriminant of the parent.
9840
9841          D := First_Discriminant (Typ);
9842          E := First_Elmt (Constraints);
9843
9844          while Present (D) loop
9845             if D = Entity (Discrim)
9846               or else D = CR_Discriminant (Entity (Discrim))
9847               or else Corresponding_Discriminant (D) = Entity (Discrim)
9848             then
9849                return Node (E);
9850             end if;
9851
9852             Next_Discriminant (D);
9853             Next_Elmt (E);
9854          end loop;
9855
9856          --  The corresponding_Discriminant mechanism is incomplete, because
9857          --  the correspondence between new and old discriminants is not one
9858          --  to one: one new discriminant can constrain several old ones. In
9859          --  that case, scan sequentially the stored_constraint, the list of
9860          --  discriminants of the parents, and the constraints.
9861
9862          if Is_Derived_Type (Typ)
9863            and then Present (Stored_Constraint (Typ))
9864            and then Scope (Entity (Discrim)) = Etype (Typ)
9865          then
9866             D := First_Discriminant (Etype (Typ));
9867             E := First_Elmt (Constraints);
9868             G := First_Elmt (Stored_Constraint (Typ));
9869             while Present (D) loop
9870                if D = Entity (Discrim) then
9871                   return Node (E);
9872                end if;
9873
9874                Next_Discriminant (D);
9875                Next_Elmt (E);
9876                Next_Elmt (G);
9877             end loop;
9878          end if;
9879
9880          --  Something is wrong if we did not find the value
9881
9882          raise Program_Error;
9883       end Get_Discr_Value;
9884
9885       ---------------------
9886       -- Is_Discriminant --
9887       ---------------------
9888
9889       function Is_Discriminant (Expr : Node_Id) return Boolean is
9890          Discrim_Scope : Entity_Id;
9891
9892       begin
9893          if Denotes_Discriminant (Expr) then
9894             Discrim_Scope := Scope (Entity (Expr));
9895
9896             --  Either we have a reference to one of Typ's discriminants,
9897
9898             pragma Assert (Discrim_Scope = Typ
9899
9900                --  or to the discriminants of the parent type, in the case
9901                --  of a derivation of a tagged type with variants.
9902
9903                or else Discrim_Scope = Etype (Typ)
9904                or else Full_View (Discrim_Scope) = Etype (Typ)
9905
9906                --  or same as above for the case where the discriminants
9907                --  were declared in Typ's private view.
9908
9909                or else (Is_Private_Type (Discrim_Scope)
9910                         and then Chars (Discrim_Scope) = Chars (Typ))
9911
9912                --  or else we are deriving from the full view and the
9913                --  discriminant is declared in the private entity.
9914
9915                or else (Is_Private_Type (Typ)
9916                         and then Chars (Discrim_Scope) = Chars (Typ))
9917
9918                --  Or we are constrained the corresponding record of a
9919                --  synchronized type that completes a private declaration.
9920
9921                or else (Is_Concurrent_Record_Type (Typ)
9922                          and then
9923                            Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
9924
9925                --  or we have a class-wide type, in which case make sure the
9926                --  discriminant found belongs to the root type.
9927
9928                or else (Is_Class_Wide_Type (Typ)
9929                         and then Etype (Typ) = Discrim_Scope));
9930
9931             return True;
9932          end if;
9933
9934          --  In all other cases we have something wrong
9935
9936          return False;
9937       end Is_Discriminant;
9938
9939    --  Start of processing for Constrain_Component_Type
9940
9941    begin
9942       if Nkind (Parent (Comp)) = N_Component_Declaration
9943         and then Comes_From_Source (Parent (Comp))
9944         and then Comes_From_Source
9945           (Subtype_Indication (Component_Definition (Parent (Comp))))
9946         and then
9947           Is_Entity_Name
9948             (Subtype_Indication (Component_Definition (Parent (Comp))))
9949       then
9950          return Compon_Type;
9951
9952       elsif Is_Array_Type (Compon_Type) then
9953          return Build_Constrained_Array_Type (Compon_Type);
9954
9955       elsif Has_Discriminants (Compon_Type) then
9956          return Build_Constrained_Discriminated_Type (Compon_Type);
9957
9958       elsif Is_Access_Type (Compon_Type) then
9959          return Build_Constrained_Access_Type (Compon_Type);
9960
9961       else
9962          return Compon_Type;
9963       end if;
9964    end Constrain_Component_Type;
9965
9966    --------------------------
9967    -- Constrain_Concurrent --
9968    --------------------------
9969
9970    --  For concurrent types, the associated record value type carries the same
9971    --  discriminants, so when we constrain a concurrent type, we must constrain
9972    --  the corresponding record type as well.
9973
9974    procedure Constrain_Concurrent
9975      (Def_Id      : in out Entity_Id;
9976       SI          : Node_Id;
9977       Related_Nod : Node_Id;
9978       Related_Id  : Entity_Id;
9979       Suffix      : Character)
9980    is
9981       T_Ent : Entity_Id := Entity (Subtype_Mark (SI));
9982       T_Val : Entity_Id;
9983
9984    begin
9985       if Ekind (T_Ent) in Access_Kind then
9986          T_Ent := Designated_Type (T_Ent);
9987       end if;
9988
9989       T_Val := Corresponding_Record_Type (T_Ent);
9990
9991       if Present (T_Val) then
9992
9993          if No (Def_Id) then
9994             Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
9995          end if;
9996
9997          Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
9998
9999          Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10000          Set_Corresponding_Record_Type (Def_Id,
10001            Constrain_Corresponding_Record
10002              (Def_Id, T_Val, Related_Nod, Related_Id));
10003
10004       else
10005          --  If there is no associated record, expansion is disabled and this
10006          --  is a generic context. Create a subtype in any case, so that
10007          --  semantic analysis can proceed.
10008
10009          if No (Def_Id) then
10010             Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
10011          end if;
10012
10013          Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
10014       end if;
10015    end Constrain_Concurrent;
10016
10017    ------------------------------------
10018    -- Constrain_Corresponding_Record --
10019    ------------------------------------
10020
10021    function Constrain_Corresponding_Record
10022      (Prot_Subt   : Entity_Id;
10023       Corr_Rec    : Entity_Id;
10024       Related_Nod : Node_Id;
10025       Related_Id  : Entity_Id) return Entity_Id
10026    is
10027       T_Sub : constant Entity_Id :=
10028                 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
10029
10030    begin
10031       Set_Etype             (T_Sub, Corr_Rec);
10032       Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
10033       Set_Is_Constrained    (T_Sub, True);
10034       Set_First_Entity      (T_Sub, First_Entity (Corr_Rec));
10035       Set_Last_Entity       (T_Sub, Last_Entity  (Corr_Rec));
10036
10037       --  As elsewhere, we do not want to create a freeze node for this itype
10038       --  if it is created for a constrained component of an enclosing record
10039       --  because references to outer discriminants will appear out of scope.
10040
10041       if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
10042          Conditional_Delay (T_Sub, Corr_Rec);
10043       else
10044          Set_Is_Frozen (T_Sub);
10045       end if;
10046
10047       if Has_Discriminants (Prot_Subt) then -- False only if errors.
10048          Set_Discriminant_Constraint
10049            (T_Sub, Discriminant_Constraint (Prot_Subt));
10050          Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
10051          Create_Constrained_Components
10052            (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
10053       end if;
10054
10055       Set_Depends_On_Private      (T_Sub, Has_Private_Component (T_Sub));
10056
10057       return T_Sub;
10058    end Constrain_Corresponding_Record;
10059
10060    -----------------------
10061    -- Constrain_Decimal --
10062    -----------------------
10063
10064    procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
10065       T           : constant Entity_Id  := Entity (Subtype_Mark (S));
10066       C           : constant Node_Id    := Constraint (S);
10067       Loc         : constant Source_Ptr := Sloc (C);
10068       Range_Expr  : Node_Id;
10069       Digits_Expr : Node_Id;
10070       Digits_Val  : Uint;
10071       Bound_Val   : Ureal;
10072
10073    begin
10074       Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
10075
10076       if Nkind (C) = N_Range_Constraint then
10077          Range_Expr := Range_Expression (C);
10078          Digits_Val := Digits_Value (T);
10079
10080       else
10081          pragma Assert (Nkind (C) = N_Digits_Constraint);
10082          Digits_Expr := Digits_Expression (C);
10083          Analyze_And_Resolve (Digits_Expr, Any_Integer);
10084
10085          Check_Digits_Expression (Digits_Expr);
10086          Digits_Val := Expr_Value (Digits_Expr);
10087
10088          if Digits_Val > Digits_Value (T) then
10089             Error_Msg_N
10090                ("digits expression is incompatible with subtype", C);
10091             Digits_Val := Digits_Value (T);
10092          end if;
10093
10094          if Present (Range_Constraint (C)) then
10095             Range_Expr := Range_Expression (Range_Constraint (C));
10096          else
10097             Range_Expr := Empty;
10098          end if;
10099       end if;
10100
10101       Set_Etype            (Def_Id, Base_Type        (T));
10102       Set_Size_Info        (Def_Id,                  (T));
10103       Set_First_Rep_Item   (Def_Id, First_Rep_Item   (T));
10104       Set_Delta_Value      (Def_Id, Delta_Value      (T));
10105       Set_Scale_Value      (Def_Id, Scale_Value      (T));
10106       Set_Small_Value      (Def_Id, Small_Value      (T));
10107       Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
10108       Set_Digits_Value     (Def_Id, Digits_Val);
10109
10110       --  Manufacture range from given digits value if no range present
10111
10112       if No (Range_Expr) then
10113          Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
10114          Range_Expr :=
10115            Make_Range (Loc,
10116              Low_Bound =>
10117                Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
10118              High_Bound =>
10119                Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
10120       end if;
10121
10122       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
10123       Set_Discrete_RM_Size (Def_Id);
10124
10125       --  Unconditionally delay the freeze, since we cannot set size
10126       --  information in all cases correctly until the freeze point.
10127
10128       Set_Has_Delayed_Freeze (Def_Id);
10129    end Constrain_Decimal;
10130
10131    ----------------------------------
10132    -- Constrain_Discriminated_Type --
10133    ----------------------------------
10134
10135    procedure Constrain_Discriminated_Type
10136      (Def_Id      : Entity_Id;
10137       S           : Node_Id;
10138       Related_Nod : Node_Id;
10139       For_Access  : Boolean := False)
10140    is
10141       E     : constant Entity_Id := Entity (Subtype_Mark (S));
10142       T     : Entity_Id;
10143       C     : Node_Id;
10144       Elist : Elist_Id := New_Elmt_List;
10145
10146       procedure Fixup_Bad_Constraint;
10147       --  This is called after finding a bad constraint, and after having
10148       --  posted an appropriate error message. The mission is to leave the
10149       --  entity T in as reasonable state as possible!
10150
10151       --------------------------
10152       -- Fixup_Bad_Constraint --
10153       --------------------------
10154
10155       procedure Fixup_Bad_Constraint is
10156       begin
10157          --  Set a reasonable Ekind for the entity. For an incomplete type,
10158          --  we can't do much, but for other types, we can set the proper
10159          --  corresponding subtype kind.
10160
10161          if Ekind (T) = E_Incomplete_Type then
10162             Set_Ekind (Def_Id, Ekind (T));
10163          else
10164             Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10165          end if;
10166
10167          Set_Etype (Def_Id, Any_Type);
10168          Set_Error_Posted (Def_Id);
10169       end Fixup_Bad_Constraint;
10170
10171    --  Start of processing for Constrain_Discriminated_Type
10172
10173    begin
10174       C := Constraint (S);
10175
10176       --  A discriminant constraint is only allowed in a subtype indication,
10177       --  after a subtype mark. This subtype mark must denote either a type
10178       --  with discriminants, or an access type whose designated type is a
10179       --  type with discriminants. A discriminant constraint specifies the
10180       --  values of these discriminants (RM 3.7.2(5)).
10181
10182       T := Base_Type (Entity (Subtype_Mark (S)));
10183
10184       if Ekind (T) in Access_Kind then
10185          T := Designated_Type (T);
10186       end if;
10187
10188       --  Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
10189       --  Avoid generating an error for access-to-incomplete subtypes.
10190
10191       if Ada_Version >= Ada_05
10192         and then Ekind (T) = E_Incomplete_Type
10193         and then Nkind (Parent (S)) = N_Subtype_Declaration
10194         and then not Is_Itype (Def_Id)
10195       then
10196          --  A little sanity check, emit an error message if the type
10197          --  has discriminants to begin with. Type T may be a regular
10198          --  incomplete type or imported via a limited with clause.
10199
10200          if Has_Discriminants (T)
10201            or else
10202              (From_With_Type (T)
10203                 and then Present (Non_Limited_View (T))
10204                 and then Nkind (Parent (Non_Limited_View (T))) =
10205                            N_Full_Type_Declaration
10206                 and then Present (Discriminant_Specifications
10207                           (Parent (Non_Limited_View (T)))))
10208          then
10209             Error_Msg_N
10210               ("(Ada 2005) incomplete subtype may not be constrained", C);
10211          else
10212             Error_Msg_N
10213               ("invalid constraint: type has no discriminant", C);
10214          end if;
10215
10216          Fixup_Bad_Constraint;
10217          return;
10218
10219       --  Check that the type has visible discriminants. The type may be
10220       --  a private type with unknown discriminants whose full view has
10221       --  discriminants which are invisible.
10222
10223       elsif not Has_Discriminants (T)
10224         or else
10225           (Has_Unknown_Discriminants (T)
10226              and then Is_Private_Type (T))
10227       then
10228          Error_Msg_N ("invalid constraint: type has no discriminant", C);
10229          Fixup_Bad_Constraint;
10230          return;
10231
10232       elsif Is_Constrained (E)
10233         or else (Ekind (E) = E_Class_Wide_Subtype
10234                   and then Present (Discriminant_Constraint (E)))
10235       then
10236          Error_Msg_N ("type is already constrained", Subtype_Mark (S));
10237          Fixup_Bad_Constraint;
10238          return;
10239       end if;
10240
10241       --  T may be an unconstrained subtype (e.g. a generic actual).
10242       --  Constraint applies to the base type.
10243
10244       T := Base_Type (T);
10245
10246       Elist := Build_Discriminant_Constraints (T, S);
10247
10248       --  If the list returned was empty we had an error in building the
10249       --  discriminant constraint. We have also already signalled an error
10250       --  in the incomplete type case
10251
10252       if Is_Empty_Elmt_List (Elist) then
10253          Fixup_Bad_Constraint;
10254          return;
10255       end if;
10256
10257       Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
10258    end Constrain_Discriminated_Type;
10259
10260    ---------------------------
10261    -- Constrain_Enumeration --
10262    ---------------------------
10263
10264    procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
10265       T : constant Entity_Id := Entity (Subtype_Mark (S));
10266       C : constant Node_Id   := Constraint (S);
10267
10268    begin
10269       Set_Ekind (Def_Id, E_Enumeration_Subtype);
10270
10271       Set_First_Literal     (Def_Id, First_Literal (Base_Type (T)));
10272
10273       Set_Etype             (Def_Id, Base_Type         (T));
10274       Set_Size_Info         (Def_Id,                   (T));
10275       Set_First_Rep_Item    (Def_Id, First_Rep_Item    (T));
10276       Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
10277
10278       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10279
10280       Set_Discrete_RM_Size (Def_Id);
10281    end Constrain_Enumeration;
10282
10283    ----------------------
10284    -- Constrain_Float --
10285    ----------------------
10286
10287    procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
10288       T    : constant Entity_Id := Entity (Subtype_Mark (S));
10289       C    : Node_Id;
10290       D    : Node_Id;
10291       Rais : Node_Id;
10292
10293    begin
10294       Set_Ekind (Def_Id, E_Floating_Point_Subtype);
10295
10296       Set_Etype          (Def_Id, Base_Type      (T));
10297       Set_Size_Info      (Def_Id,                (T));
10298       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10299
10300       --  Process the constraint
10301
10302       C := Constraint (S);
10303
10304       --  Digits constraint present
10305
10306       if Nkind (C) = N_Digits_Constraint then
10307          Check_Restriction (No_Obsolescent_Features, C);
10308
10309          if Warn_On_Obsolescent_Feature then
10310             Error_Msg_N
10311               ("subtype digits constraint is an " &
10312                "obsolescent feature (RM J.3(8))?", C);
10313          end if;
10314
10315          D := Digits_Expression (C);
10316          Analyze_And_Resolve (D, Any_Integer);
10317          Check_Digits_Expression (D);
10318          Set_Digits_Value (Def_Id, Expr_Value (D));
10319
10320          --  Check that digits value is in range. Obviously we can do this
10321          --  at compile time, but it is strictly a runtime check, and of
10322          --  course there is an ACVC test that checks this!
10323
10324          if Digits_Value (Def_Id) > Digits_Value (T) then
10325             Error_Msg_Uint_1 := Digits_Value (T);
10326             Error_Msg_N ("?digits value is too large, maximum is ^", D);
10327             Rais :=
10328               Make_Raise_Constraint_Error (Sloc (D),
10329                 Reason => CE_Range_Check_Failed);
10330             Insert_Action (Declaration_Node (Def_Id), Rais);
10331          end if;
10332
10333          C := Range_Constraint (C);
10334
10335       --  No digits constraint present
10336
10337       else
10338          Set_Digits_Value (Def_Id, Digits_Value (T));
10339       end if;
10340
10341       --  Range constraint present
10342
10343       if Nkind (C) = N_Range_Constraint then
10344          Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10345
10346       --  No range constraint present
10347
10348       else
10349          pragma Assert (No (C));
10350          Set_Scalar_Range (Def_Id, Scalar_Range (T));
10351       end if;
10352
10353       Set_Is_Constrained (Def_Id);
10354    end Constrain_Float;
10355
10356    ---------------------
10357    -- Constrain_Index --
10358    ---------------------
10359
10360    procedure Constrain_Index
10361      (Index        : Node_Id;
10362       S            : Node_Id;
10363       Related_Nod  : Node_Id;
10364       Related_Id   : Entity_Id;
10365       Suffix       : Character;
10366       Suffix_Index : Nat)
10367    is
10368       Def_Id : Entity_Id;
10369       R      : Node_Id := Empty;
10370       T      : constant Entity_Id := Etype (Index);
10371
10372    begin
10373       if Nkind (S) = N_Range
10374         or else
10375           (Nkind (S) = N_Attribute_Reference
10376             and then Attribute_Name (S) = Name_Range)
10377       then
10378          --  A Range attribute will transformed into N_Range by Resolve
10379
10380          Analyze (S);
10381          Set_Etype (S, T);
10382          R := S;
10383
10384          Process_Range_Expr_In_Decl (R, T, Empty_List);
10385
10386          if not Error_Posted (S)
10387            and then
10388              (Nkind (S) /= N_Range
10389                or else not Covers (T, (Etype (Low_Bound (S))))
10390                or else not Covers (T, (Etype (High_Bound (S)))))
10391          then
10392             if Base_Type (T) /= Any_Type
10393               and then Etype (Low_Bound (S)) /= Any_Type
10394               and then Etype (High_Bound (S)) /= Any_Type
10395             then
10396                Error_Msg_N ("range expected", S);
10397             end if;
10398          end if;
10399
10400       elsif Nkind (S) = N_Subtype_Indication then
10401
10402          --  The parser has verified that this is a discrete indication
10403
10404          Resolve_Discrete_Subtype_Indication (S, T);
10405          R := Range_Expression (Constraint (S));
10406
10407       elsif Nkind (S) = N_Discriminant_Association then
10408
10409          --  Syntactically valid in subtype indication
10410
10411          Error_Msg_N ("invalid index constraint", S);
10412          Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
10413          return;
10414
10415       --  Subtype_Mark case, no anonymous subtypes to construct
10416
10417       else
10418          Analyze (S);
10419
10420          if Is_Entity_Name (S) then
10421             if not Is_Type (Entity (S)) then
10422                Error_Msg_N ("expect subtype mark for index constraint", S);
10423
10424             elsif Base_Type (Entity (S)) /= Base_Type (T) then
10425                Wrong_Type (S, Base_Type (T));
10426             end if;
10427
10428             return;
10429
10430          else
10431             Error_Msg_N ("invalid index constraint", S);
10432             Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
10433             return;
10434          end if;
10435       end if;
10436
10437       Def_Id :=
10438         Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
10439
10440       Set_Etype (Def_Id, Base_Type (T));
10441
10442       if Is_Modular_Integer_Type (T) then
10443          Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
10444
10445       elsif Is_Integer_Type (T) then
10446          Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
10447
10448       else
10449          Set_Ekind (Def_Id, E_Enumeration_Subtype);
10450          Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
10451       end if;
10452
10453       Set_Size_Info      (Def_Id,                (T));
10454       Set_RM_Size        (Def_Id, RM_Size        (T));
10455       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10456
10457       Set_Scalar_Range   (Def_Id, R);
10458
10459       Set_Etype (S, Def_Id);
10460       Set_Discrete_RM_Size (Def_Id);
10461    end Constrain_Index;
10462
10463    -----------------------
10464    -- Constrain_Integer --
10465    -----------------------
10466
10467    procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
10468       T : constant Entity_Id := Entity (Subtype_Mark (S));
10469       C : constant Node_Id   := Constraint (S);
10470
10471    begin
10472       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10473
10474       if Is_Modular_Integer_Type (T) then
10475          Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
10476       else
10477          Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
10478       end if;
10479
10480       Set_Etype            (Def_Id, Base_Type        (T));
10481       Set_Size_Info        (Def_Id,                  (T));
10482       Set_First_Rep_Item   (Def_Id, First_Rep_Item   (T));
10483       Set_Discrete_RM_Size (Def_Id);
10484    end Constrain_Integer;
10485
10486    ------------------------------
10487    -- Constrain_Ordinary_Fixed --
10488    ------------------------------
10489
10490    procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
10491       T    : constant Entity_Id := Entity (Subtype_Mark (S));
10492       C    : Node_Id;
10493       D    : Node_Id;
10494       Rais : Node_Id;
10495
10496    begin
10497       Set_Ekind          (Def_Id, E_Ordinary_Fixed_Point_Subtype);
10498       Set_Etype          (Def_Id, Base_Type        (T));
10499       Set_Size_Info      (Def_Id,                  (T));
10500       Set_First_Rep_Item (Def_Id, First_Rep_Item   (T));
10501       Set_Small_Value    (Def_Id, Small_Value      (T));
10502
10503       --  Process the constraint
10504
10505       C := Constraint (S);
10506
10507       --  Delta constraint present
10508
10509       if Nkind (C) = N_Delta_Constraint then
10510          Check_Restriction (No_Obsolescent_Features, C);
10511
10512          if Warn_On_Obsolescent_Feature then
10513             Error_Msg_S
10514               ("subtype delta constraint is an " &
10515                "obsolescent feature (RM J.3(7))?");
10516          end if;
10517
10518          D := Delta_Expression (C);
10519          Analyze_And_Resolve (D, Any_Real);
10520          Check_Delta_Expression (D);
10521          Set_Delta_Value (Def_Id, Expr_Value_R (D));
10522
10523          --  Check that delta value is in range. Obviously we can do this
10524          --  at compile time, but it is strictly a runtime check, and of
10525          --  course there is an ACVC test that checks this!
10526
10527          if Delta_Value (Def_Id) < Delta_Value (T) then
10528             Error_Msg_N ("?delta value is too small", D);
10529             Rais :=
10530               Make_Raise_Constraint_Error (Sloc (D),
10531                 Reason => CE_Range_Check_Failed);
10532             Insert_Action (Declaration_Node (Def_Id), Rais);
10533          end if;
10534
10535          C := Range_Constraint (C);
10536
10537       --  No delta constraint present
10538
10539       else
10540          Set_Delta_Value (Def_Id, Delta_Value (T));
10541       end if;
10542
10543       --  Range constraint present
10544
10545       if Nkind (C) = N_Range_Constraint then
10546          Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10547
10548       --  No range constraint present
10549
10550       else
10551          pragma Assert (No (C));
10552          Set_Scalar_Range (Def_Id, Scalar_Range (T));
10553
10554       end if;
10555
10556       Set_Discrete_RM_Size (Def_Id);
10557
10558       --  Unconditionally delay the freeze, since we cannot set size
10559       --  information in all cases correctly until the freeze point.
10560
10561       Set_Has_Delayed_Freeze (Def_Id);
10562    end Constrain_Ordinary_Fixed;
10563
10564    -----------------------
10565    -- Contain_Interface --
10566    -----------------------
10567
10568    function Contain_Interface
10569      (Iface  : Entity_Id;
10570       Ifaces : Elist_Id) return Boolean
10571    is
10572       Iface_Elmt : Elmt_Id;
10573
10574    begin
10575       if Present (Ifaces) then
10576          Iface_Elmt := First_Elmt (Ifaces);
10577          while Present (Iface_Elmt) loop
10578             if Node (Iface_Elmt) = Iface then
10579                return True;
10580             end if;
10581
10582             Next_Elmt (Iface_Elmt);
10583          end loop;
10584       end if;
10585
10586       return False;
10587    end Contain_Interface;
10588
10589    ---------------------------
10590    -- Convert_Scalar_Bounds --
10591    ---------------------------
10592
10593    procedure Convert_Scalar_Bounds
10594      (N            : Node_Id;
10595       Parent_Type  : Entity_Id;
10596       Derived_Type : Entity_Id;
10597       Loc          : Source_Ptr)
10598    is
10599       Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
10600
10601       Lo  : Node_Id;
10602       Hi  : Node_Id;
10603       Rng : Node_Id;
10604
10605    begin
10606       Lo := Build_Scalar_Bound
10607               (Type_Low_Bound (Derived_Type),
10608                Parent_Type, Implicit_Base);
10609
10610       Hi := Build_Scalar_Bound
10611               (Type_High_Bound (Derived_Type),
10612                Parent_Type, Implicit_Base);
10613
10614       Rng :=
10615         Make_Range (Loc,
10616           Low_Bound  => Lo,
10617           High_Bound => Hi);
10618
10619       Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
10620
10621       Set_Parent (Rng, N);
10622       Set_Scalar_Range (Derived_Type, Rng);
10623
10624       --  Analyze the bounds
10625
10626       Analyze_And_Resolve (Lo, Implicit_Base);
10627       Analyze_And_Resolve (Hi, Implicit_Base);
10628
10629       --  Analyze the range itself, except that we do not analyze it if
10630       --  the bounds are real literals, and we have a fixed-point type.
10631       --  The reason for this is that we delay setting the bounds in this
10632       --  case till we know the final Small and Size values (see circuit
10633       --  in Freeze.Freeze_Fixed_Point_Type for further details).
10634
10635       if Is_Fixed_Point_Type (Parent_Type)
10636         and then Nkind (Lo) = N_Real_Literal
10637         and then Nkind (Hi) = N_Real_Literal
10638       then
10639          return;
10640
10641       --  Here we do the analysis of the range
10642
10643       --  Note: we do this manually, since if we do a normal Analyze and
10644       --  Resolve call, there are problems with the conversions used for
10645       --  the derived type range.
10646
10647       else
10648          Set_Etype    (Rng, Implicit_Base);
10649          Set_Analyzed (Rng, True);
10650       end if;
10651    end Convert_Scalar_Bounds;
10652
10653    -------------------
10654    -- Copy_And_Swap --
10655    -------------------
10656
10657    procedure Copy_And_Swap (Priv, Full : Entity_Id) is
10658    begin
10659       --  Initialize new full declaration entity by copying the pertinent
10660       --  fields of the corresponding private declaration entity.
10661
10662       --  We temporarily set Ekind to a value appropriate for a type to
10663       --  avoid assert failures in Einfo from checking for setting type
10664       --  attributes on something that is not a type. Ekind (Priv) is an
10665       --  appropriate choice, since it allowed the attributes to be set
10666       --  in the first place. This Ekind value will be modified later.
10667
10668       Set_Ekind (Full, Ekind (Priv));
10669
10670       --  Also set Etype temporarily to Any_Type, again, in the absence
10671       --  of errors, it will be properly reset, and if there are errors,
10672       --  then we want a value of Any_Type to remain.
10673
10674       Set_Etype (Full, Any_Type);
10675
10676       --  Now start copying attributes
10677
10678       Set_Has_Discriminants          (Full, Has_Discriminants       (Priv));
10679
10680       if Has_Discriminants (Full) then
10681          Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
10682          Set_Stored_Constraint       (Full, Stored_Constraint       (Priv));
10683       end if;
10684
10685       Set_First_Rep_Item             (Full, First_Rep_Item          (Priv));
10686       Set_Homonym                    (Full, Homonym                 (Priv));
10687       Set_Is_Immediately_Visible     (Full, Is_Immediately_Visible  (Priv));
10688       Set_Is_Public                  (Full, Is_Public               (Priv));
10689       Set_Is_Pure                    (Full, Is_Pure                 (Priv));
10690       Set_Is_Tagged_Type             (Full, Is_Tagged_Type          (Priv));
10691       Set_Has_Pragma_Unreferenced    (Full, Has_Pragma_Unreferenced (Priv));
10692       Set_Has_Pragma_Unreferenced_Objects
10693                                      (Full, Has_Pragma_Unreferenced_Objects
10694                                                                     (Priv));
10695
10696       Conditional_Delay              (Full,                          Priv);
10697
10698       if Is_Tagged_Type (Full) then
10699          Set_Primitive_Operations    (Full, Primitive_Operations    (Priv));
10700
10701          if Priv = Base_Type (Priv) then
10702             Set_Class_Wide_Type      (Full, Class_Wide_Type         (Priv));
10703          end if;
10704       end if;
10705
10706       Set_Is_Volatile                (Full, Is_Volatile             (Priv));
10707       Set_Treat_As_Volatile          (Full, Treat_As_Volatile       (Priv));
10708       Set_Scope                      (Full, Scope                   (Priv));
10709       Set_Next_Entity                (Full, Next_Entity             (Priv));
10710       Set_First_Entity               (Full, First_Entity            (Priv));
10711       Set_Last_Entity                (Full, Last_Entity             (Priv));
10712
10713       --  If access types have been recorded for later handling, keep them in
10714       --  the full view so that they get handled when the full view freeze
10715       --  node is expanded.
10716
10717       if Present (Freeze_Node (Priv))
10718         and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
10719       then
10720          Ensure_Freeze_Node (Full);
10721          Set_Access_Types_To_Process
10722            (Freeze_Node (Full),
10723             Access_Types_To_Process (Freeze_Node (Priv)));
10724       end if;
10725
10726       --  Swap the two entities. Now Privat is the full type entity and
10727       --  Full is the private one. They will be swapped back at the end
10728       --  of the private part. This swapping ensures that the entity that
10729       --  is visible in the private part is the full declaration.
10730
10731       Exchange_Entities (Priv, Full);
10732       Append_Entity (Full, Scope (Full));
10733    end Copy_And_Swap;
10734
10735    -------------------------------------
10736    -- Copy_Array_Base_Type_Attributes --
10737    -------------------------------------
10738
10739    procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
10740    begin
10741       Set_Component_Alignment      (T1, Component_Alignment      (T2));
10742       Set_Component_Type           (T1, Component_Type           (T2));
10743       Set_Component_Size           (T1, Component_Size           (T2));
10744       Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
10745       Set_Finalize_Storage_Only    (T1, Finalize_Storage_Only    (T2));
10746       Set_Has_Non_Standard_Rep     (T1, Has_Non_Standard_Rep     (T2));
10747       Set_Has_Task                 (T1, Has_Task                 (T2));
10748       Set_Is_Packed                (T1, Is_Packed                (T2));
10749       Set_Has_Aliased_Components   (T1, Has_Aliased_Components   (T2));
10750       Set_Has_Atomic_Components    (T1, Has_Atomic_Components    (T2));
10751       Set_Has_Volatile_Components  (T1, Has_Volatile_Components  (T2));
10752    end Copy_Array_Base_Type_Attributes;
10753
10754    -----------------------------------
10755    -- Copy_Array_Subtype_Attributes --
10756    -----------------------------------
10757
10758    procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
10759    begin
10760       Set_Size_Info (T1, T2);
10761
10762       Set_First_Index          (T1, First_Index           (T2));
10763       Set_Is_Aliased           (T1, Is_Aliased            (T2));
10764       Set_Is_Atomic            (T1, Is_Atomic             (T2));
10765       Set_Is_Volatile          (T1, Is_Volatile           (T2));
10766       Set_Treat_As_Volatile    (T1, Treat_As_Volatile     (T2));
10767       Set_Is_Constrained       (T1, Is_Constrained        (T2));
10768       Set_Depends_On_Private   (T1, Has_Private_Component (T2));
10769       Set_First_Rep_Item       (T1, First_Rep_Item        (T2));
10770       Set_Convention           (T1, Convention            (T2));
10771       Set_Is_Limited_Composite (T1, Is_Limited_Composite  (T2));
10772       Set_Is_Private_Composite (T1, Is_Private_Composite  (T2));
10773    end Copy_Array_Subtype_Attributes;
10774
10775    -----------------------------------
10776    -- Create_Constrained_Components --
10777    -----------------------------------
10778
10779    procedure Create_Constrained_Components
10780      (Subt        : Entity_Id;
10781       Decl_Node   : Node_Id;
10782       Typ         : Entity_Id;
10783       Constraints : Elist_Id)
10784    is
10785       Loc         : constant Source_Ptr := Sloc (Subt);
10786       Comp_List   : constant Elist_Id   := New_Elmt_List;
10787       Parent_Type : constant Entity_Id  := Etype (Typ);
10788       Assoc_List  : constant List_Id    := New_List;
10789       Discr_Val   : Elmt_Id;
10790       Errors      : Boolean;
10791       New_C       : Entity_Id;
10792       Old_C       : Entity_Id;
10793       Is_Static   : Boolean := True;
10794
10795       procedure Collect_Fixed_Components (Typ : Entity_Id);
10796       --  Collect parent type components that do not appear in a variant part
10797
10798       procedure Create_All_Components;
10799       --  Iterate over Comp_List to create the components of the subtype
10800
10801       function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
10802       --  Creates a new component from Old_Compon, copying all the fields from
10803       --  it, including its Etype, inserts the new component in the Subt entity
10804       --  chain and returns the new component.
10805
10806       function Is_Variant_Record (T : Entity_Id) return Boolean;
10807       --  If true, and discriminants are static, collect only components from
10808       --  variants selected by discriminant values.
10809
10810       ------------------------------
10811       -- Collect_Fixed_Components --
10812       ------------------------------
10813
10814       procedure Collect_Fixed_Components (Typ : Entity_Id) is
10815       begin
10816       --  Build association list for discriminants, and find components of the
10817       --  variant part selected by the values of the discriminants.
10818
10819          Old_C := First_Discriminant (Typ);
10820          Discr_Val := First_Elmt (Constraints);
10821          while Present (Old_C) loop
10822             Append_To (Assoc_List,
10823               Make_Component_Association (Loc,
10824                  Choices    => New_List (New_Occurrence_Of (Old_C, Loc)),
10825                  Expression => New_Copy (Node (Discr_Val))));
10826
10827             Next_Elmt (Discr_Val);
10828             Next_Discriminant (Old_C);
10829          end loop;
10830
10831          --  The tag, and the possible parent and controller components
10832          --  are unconditionally in the subtype.
10833
10834          if Is_Tagged_Type (Typ)
10835            or else Has_Controlled_Component (Typ)
10836          then
10837             Old_C := First_Component (Typ);
10838             while Present (Old_C) loop
10839                if Chars ((Old_C)) = Name_uTag
10840                  or else Chars ((Old_C)) = Name_uParent
10841                  or else Chars ((Old_C)) = Name_uController
10842                then
10843                   Append_Elmt (Old_C, Comp_List);
10844                end if;
10845
10846                Next_Component (Old_C);
10847             end loop;
10848          end if;
10849       end Collect_Fixed_Components;
10850
10851       ---------------------------
10852       -- Create_All_Components --
10853       ---------------------------
10854
10855       procedure Create_All_Components is
10856          Comp : Elmt_Id;
10857
10858       begin
10859          Comp := First_Elmt (Comp_List);
10860          while Present (Comp) loop
10861             Old_C := Node (Comp);
10862             New_C := Create_Component (Old_C);
10863
10864             Set_Etype
10865               (New_C,
10866                Constrain_Component_Type
10867                  (Old_C, Subt, Decl_Node, Typ, Constraints));
10868             Set_Is_Public (New_C, Is_Public (Subt));
10869
10870             Next_Elmt (Comp);
10871          end loop;
10872       end Create_All_Components;
10873
10874       ----------------------
10875       -- Create_Component --
10876       ----------------------
10877
10878       function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
10879          New_Compon : constant Entity_Id := New_Copy (Old_Compon);
10880
10881       begin
10882          if Ekind (Old_Compon) = E_Discriminant
10883            and then Is_Completely_Hidden (Old_Compon)
10884          then
10885             --  This is a shadow discriminant created for a discriminant of
10886             --  the parent type that is one of several renamed by the same
10887             --  new discriminant. Give the shadow discriminant an internal
10888             --  name that cannot conflict with that of visible components.
10889
10890             Set_Chars (New_Compon, New_Internal_Name ('C'));
10891          end if;
10892
10893          --  Set the parent so we have a proper link for freezing etc. This is
10894          --  not a real parent pointer, since of course our parent does not own
10895          --  up to us and reference us, we are an illegitimate child of the
10896          --  original parent!
10897
10898          Set_Parent (New_Compon, Parent (Old_Compon));
10899
10900          --  If the old component's Esize was already determined and is a
10901          --  static value, then the new component simply inherits it. Otherwise
10902          --  the old component's size may require run-time determination, but
10903          --  the new component's size still might be statically determinable
10904          --  (if, for example it has a static constraint). In that case we want
10905          --  Layout_Type to recompute the component's size, so we reset its
10906          --  size and positional fields.
10907
10908          if Frontend_Layout_On_Target
10909            and then not Known_Static_Esize (Old_Compon)
10910          then
10911             Set_Esize (New_Compon, Uint_0);
10912             Init_Normalized_First_Bit    (New_Compon);
10913             Init_Normalized_Position     (New_Compon);
10914             Init_Normalized_Position_Max (New_Compon);
10915          end if;
10916
10917          --  We do not want this node marked as Comes_From_Source, since
10918          --  otherwise it would get first class status and a separate cross-
10919          --  reference line would be generated. Illegitimate children do not
10920          --  rate such recognition.
10921
10922          Set_Comes_From_Source (New_Compon, False);
10923
10924          --  But it is a real entity, and a birth certificate must be properly
10925          --  registered by entering it into the entity list.
10926
10927          Enter_Name (New_Compon);
10928
10929          return New_Compon;
10930       end Create_Component;
10931
10932       -----------------------
10933       -- Is_Variant_Record --
10934       -----------------------
10935
10936       function Is_Variant_Record (T : Entity_Id) return Boolean is
10937       begin
10938          return Nkind (Parent (T)) = N_Full_Type_Declaration
10939            and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
10940            and then Present (Component_List (Type_Definition (Parent (T))))
10941            and then
10942              Present
10943                (Variant_Part (Component_List (Type_Definition (Parent (T)))));
10944       end Is_Variant_Record;
10945
10946    --  Start of processing for Create_Constrained_Components
10947
10948    begin
10949       pragma Assert (Subt /= Base_Type (Subt));
10950       pragma Assert (Typ = Base_Type (Typ));
10951
10952       Set_First_Entity (Subt, Empty);
10953       Set_Last_Entity  (Subt, Empty);
10954
10955       --  Check whether constraint is fully static, in which case we can
10956       --  optimize the list of components.
10957
10958       Discr_Val := First_Elmt (Constraints);
10959       while Present (Discr_Val) loop
10960          if not Is_OK_Static_Expression (Node (Discr_Val)) then
10961             Is_Static := False;
10962             exit;
10963          end if;
10964
10965          Next_Elmt (Discr_Val);
10966       end loop;
10967
10968       Set_Has_Static_Discriminants (Subt, Is_Static);
10969
10970       Push_Scope (Subt);
10971
10972       --  Inherit the discriminants of the parent type
10973
10974       Add_Discriminants : declare
10975          Num_Disc : Int;
10976          Num_Gird : Int;
10977
10978       begin
10979          Num_Disc := 0;
10980          Old_C := First_Discriminant (Typ);
10981
10982          while Present (Old_C) loop
10983             Num_Disc := Num_Disc + 1;
10984             New_C := Create_Component (Old_C);
10985             Set_Is_Public (New_C, Is_Public (Subt));
10986             Next_Discriminant (Old_C);
10987          end loop;
10988
10989          --  For an untagged derived subtype, the number of discriminants may
10990          --  be smaller than the number of inherited discriminants, because
10991          --  several of them may be renamed by a single new discriminant.
10992          --  In this case, add the hidden discriminants back into the subtype,
10993          --  because otherwise the size of the subtype is computed incorrectly
10994          --  in GCC 4.1.
10995
10996          Num_Gird := 0;
10997
10998          if Is_Derived_Type (Typ)
10999            and then not Is_Tagged_Type (Typ)
11000          then
11001             Old_C := First_Stored_Discriminant (Typ);
11002
11003             while Present (Old_C) loop
11004                Num_Gird := Num_Gird + 1;
11005                Next_Stored_Discriminant (Old_C);
11006             end loop;
11007          end if;
11008
11009          if Num_Gird > Num_Disc then
11010
11011             --  Find out multiple uses of new discriminants, and add hidden
11012             --  components for the extra renamed discriminants. We recognize
11013             --  multiple uses through the Corresponding_Discriminant of a
11014             --  new discriminant: if it constrains several old discriminants,
11015             --  this field points to the last one in the parent type. The
11016             --  stored discriminants of the derived type have the same name
11017             --  as those of the parent.
11018
11019             declare
11020                Constr    : Elmt_Id;
11021                New_Discr : Entity_Id;
11022                Old_Discr : Entity_Id;
11023
11024             begin
11025                Constr    := First_Elmt (Stored_Constraint (Typ));
11026                Old_Discr := First_Stored_Discriminant (Typ);
11027                while Present (Constr) loop
11028                   if Is_Entity_Name (Node (Constr))
11029                     and then Ekind (Entity (Node (Constr))) = E_Discriminant
11030                   then
11031                      New_Discr := Entity (Node (Constr));
11032
11033                      if Chars (Corresponding_Discriminant (New_Discr)) /=
11034                         Chars (Old_Discr)
11035                      then
11036                         --  The new discriminant has been used to rename a
11037                         --  subsequent old discriminant. Introduce a shadow
11038                         --  component for the current old discriminant.
11039
11040                         New_C := Create_Component (Old_Discr);
11041                         Set_Original_Record_Component  (New_C, Old_Discr);
11042                      end if;
11043                   end if;
11044
11045                   Next_Elmt (Constr);
11046                   Next_Stored_Discriminant (Old_Discr);
11047                end loop;
11048             end;
11049          end if;
11050       end Add_Discriminants;
11051
11052       if Is_Static
11053         and then Is_Variant_Record (Typ)
11054       then
11055          Collect_Fixed_Components (Typ);
11056
11057          Gather_Components (
11058            Typ,
11059            Component_List (Type_Definition (Parent (Typ))),
11060            Governed_By   => Assoc_List,
11061            Into          => Comp_List,
11062            Report_Errors => Errors);
11063          pragma Assert (not Errors);
11064
11065          Create_All_Components;
11066
11067       --  If the subtype declaration is created for a tagged type derivation
11068       --  with constraints, we retrieve the record definition of the parent
11069       --  type to select the components of the proper variant.
11070
11071       elsif Is_Static
11072         and then Is_Tagged_Type (Typ)
11073         and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
11074         and then
11075           Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
11076         and then Is_Variant_Record (Parent_Type)
11077       then
11078          Collect_Fixed_Components (Typ);
11079
11080          Gather_Components (
11081            Typ,
11082            Component_List (Type_Definition (Parent (Parent_Type))),
11083            Governed_By   => Assoc_List,
11084            Into          => Comp_List,
11085            Report_Errors => Errors);
11086          pragma Assert (not Errors);
11087
11088          --  If the tagged derivation has a type extension, collect all the
11089          --  new components therein.
11090
11091          if Present
11092               (Record_Extension_Part (Type_Definition (Parent (Typ))))
11093          then
11094             Old_C := First_Component (Typ);
11095             while Present (Old_C) loop
11096                if Original_Record_Component (Old_C) = Old_C
11097                 and then Chars (Old_C) /= Name_uTag
11098                 and then Chars (Old_C) /= Name_uParent
11099                 and then Chars (Old_C) /= Name_uController
11100                then
11101                   Append_Elmt (Old_C, Comp_List);
11102                end if;
11103
11104                Next_Component (Old_C);
11105             end loop;
11106          end if;
11107
11108          Create_All_Components;
11109
11110       else
11111          --  If discriminants are not static, or if this is a multi-level type
11112          --  extension, we have to include all components of the parent type.
11113
11114          Old_C := First_Component (Typ);
11115          while Present (Old_C) loop
11116             New_C := Create_Component (Old_C);
11117
11118             Set_Etype
11119               (New_C,
11120                Constrain_Component_Type
11121                  (Old_C, Subt, Decl_Node, Typ, Constraints));
11122             Set_Is_Public (New_C, Is_Public (Subt));
11123
11124             Next_Component (Old_C);
11125          end loop;
11126       end if;
11127
11128       End_Scope;
11129    end Create_Constrained_Components;
11130
11131    ------------------------------------------
11132    -- Decimal_Fixed_Point_Type_Declaration --
11133    ------------------------------------------
11134
11135    procedure Decimal_Fixed_Point_Type_Declaration
11136      (T   : Entity_Id;
11137       Def : Node_Id)
11138    is
11139       Loc           : constant Source_Ptr := Sloc (Def);
11140       Digs_Expr     : constant Node_Id    := Digits_Expression (Def);
11141       Delta_Expr    : constant Node_Id    := Delta_Expression (Def);
11142       Implicit_Base : Entity_Id;
11143       Digs_Val      : Uint;
11144       Delta_Val     : Ureal;
11145       Scale_Val     : Uint;
11146       Bound_Val     : Ureal;
11147
11148    --  Start of processing for Decimal_Fixed_Point_Type_Declaration
11149
11150    begin
11151       Check_Restriction (No_Fixed_Point, Def);
11152
11153       --  Create implicit base type
11154
11155       Implicit_Base :=
11156         Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
11157       Set_Etype (Implicit_Base, Implicit_Base);
11158
11159       --  Analyze and process delta expression
11160
11161       Analyze_And_Resolve (Delta_Expr, Universal_Real);
11162
11163       Check_Delta_Expression (Delta_Expr);
11164       Delta_Val := Expr_Value_R (Delta_Expr);
11165
11166       --  Check delta is power of 10, and determine scale value from it
11167
11168       declare
11169          Val : Ureal;
11170
11171       begin
11172          Scale_Val := Uint_0;
11173          Val := Delta_Val;
11174
11175          if Val < Ureal_1 then
11176             while Val < Ureal_1 loop
11177                Val := Val * Ureal_10;
11178                Scale_Val := Scale_Val + 1;
11179             end loop;
11180
11181             if Scale_Val > 18 then
11182                Error_Msg_N ("scale exceeds maximum value of 18", Def);
11183                Scale_Val := UI_From_Int (+18);
11184             end if;
11185
11186          else
11187             while Val > Ureal_1 loop
11188                Val := Val / Ureal_10;
11189                Scale_Val := Scale_Val - 1;
11190             end loop;
11191
11192             if Scale_Val < -18 then
11193                Error_Msg_N ("scale is less than minimum value of -18", Def);
11194                Scale_Val := UI_From_Int (-18);
11195             end if;
11196          end if;
11197
11198          if Val /= Ureal_1 then
11199             Error_Msg_N ("delta expression must be a power of 10", Def);
11200             Delta_Val := Ureal_10 ** (-Scale_Val);
11201          end if;
11202       end;
11203
11204       --  Set delta, scale and small (small = delta for decimal type)
11205
11206       Set_Delta_Value (Implicit_Base, Delta_Val);
11207       Set_Scale_Value (Implicit_Base, Scale_Val);
11208       Set_Small_Value (Implicit_Base, Delta_Val);
11209
11210       --  Analyze and process digits expression
11211
11212       Analyze_And_Resolve (Digs_Expr, Any_Integer);
11213       Check_Digits_Expression (Digs_Expr);
11214       Digs_Val := Expr_Value (Digs_Expr);
11215
11216       if Digs_Val > 18 then
11217          Digs_Val := UI_From_Int (+18);
11218          Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
11219       end if;
11220
11221       Set_Digits_Value (Implicit_Base, Digs_Val);
11222       Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
11223
11224       --  Set range of base type from digits value for now. This will be
11225       --  expanded to represent the true underlying base range by Freeze.
11226
11227       Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
11228
11229       --  Note: We leave size as zero for now, size will be set at freeze
11230       --  time. We have to do this for ordinary fixed-point, because the size
11231       --  depends on the specified small, and we might as well do the same for
11232       --  decimal fixed-point.
11233
11234       pragma Assert (Esize (Implicit_Base) = Uint_0);
11235
11236       --  If there are bounds given in the declaration use them as the
11237       --  bounds of the first named subtype.
11238
11239       if Present (Real_Range_Specification (Def)) then
11240          declare
11241             RRS      : constant Node_Id := Real_Range_Specification (Def);
11242             Low      : constant Node_Id := Low_Bound (RRS);
11243             High     : constant Node_Id := High_Bound (RRS);
11244             Low_Val  : Ureal;
11245             High_Val : Ureal;
11246
11247          begin
11248             Analyze_And_Resolve (Low, Any_Real);
11249             Analyze_And_Resolve (High, Any_Real);
11250             Check_Real_Bound (Low);
11251             Check_Real_Bound (High);
11252             Low_Val := Expr_Value_R (Low);
11253             High_Val := Expr_Value_R (High);
11254
11255             if Low_Val < (-Bound_Val) then
11256                Error_Msg_N
11257                  ("range low bound too small for digits value", Low);
11258                Low_Val := -Bound_Val;
11259             end if;
11260
11261             if High_Val > Bound_Val then
11262                Error_Msg_N
11263                  ("range high bound too large for digits value", High);
11264                High_Val := Bound_Val;
11265             end if;
11266
11267             Set_Fixed_Range (T, Loc, Low_Val, High_Val);
11268          end;
11269
11270       --  If no explicit range, use range that corresponds to given
11271       --  digits value. This will end up as the final range for the
11272       --  first subtype.
11273
11274       else
11275          Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
11276       end if;
11277
11278       --  Complete entity for first subtype
11279
11280       Set_Ekind          (T, E_Decimal_Fixed_Point_Subtype);
11281       Set_Etype          (T, Implicit_Base);
11282       Set_Size_Info      (T, Implicit_Base);
11283       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
11284       Set_Digits_Value   (T, Digs_Val);
11285       Set_Delta_Value    (T, Delta_Val);
11286       Set_Small_Value    (T, Delta_Val);
11287       Set_Scale_Value    (T, Scale_Val);
11288       Set_Is_Constrained (T);
11289    end Decimal_Fixed_Point_Type_Declaration;
11290
11291    ----------------------------------
11292    -- Derive_Interface_Subprograms --
11293    ----------------------------------
11294
11295    procedure Derive_Interface_Subprograms
11296      (Parent_Type : Entity_Id;
11297       Tagged_Type : Entity_Id;
11298       Ifaces_List : Elist_Id)
11299    is
11300       function Collect_Interface_Primitives
11301         (Tagged_Type : Entity_Id) return Elist_Id;
11302       --  Ada 2005 (AI-251): Collect the primitives of all the implemented
11303       --  interfaces.
11304
11305       function In_List (L : Elist_Id; Subp : Entity_Id) return Boolean;
11306       --  Determine if Subp already in the list L
11307
11308       procedure Remove_Homonym (E : Entity_Id);
11309       --  Removes E from the homonym chain
11310
11311       ----------------------------------
11312       -- Collect_Interface_Primitives --
11313       ----------------------------------
11314
11315       function Collect_Interface_Primitives
11316          (Tagged_Type : Entity_Id) return Elist_Id
11317       is
11318          Op_List     : constant Elist_Id := New_Elmt_List;
11319          Elmt        : Elmt_Id;
11320          Ifaces_List : Elist_Id;
11321          Iface_Elmt  : Elmt_Id;
11322          Prim        : Entity_Id;
11323
11324       begin
11325          pragma Assert (Is_Tagged_Type (Tagged_Type)
11326            and then Has_Abstract_Interfaces (Tagged_Type));
11327
11328          Collect_Abstract_Interfaces (Tagged_Type, Ifaces_List);
11329
11330          Iface_Elmt := First_Elmt (Ifaces_List);
11331          while Present (Iface_Elmt) loop
11332             Elmt := First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
11333             while Present (Elmt) loop
11334                Prim := Node (Elmt);
11335
11336                if not Is_Predefined_Dispatching_Operation (Prim) then
11337                   Append_Elmt (Prim, Op_List);
11338                end if;
11339
11340                Next_Elmt (Elmt);
11341             end loop;
11342
11343             Next_Elmt (Iface_Elmt);
11344          end loop;
11345
11346          return Op_List;
11347       end Collect_Interface_Primitives;
11348
11349       -------------
11350       -- In_List --
11351       -------------
11352
11353       function In_List (L : Elist_Id; Subp : Entity_Id) return Boolean is
11354          Elmt : Elmt_Id;
11355
11356       begin
11357          Elmt := First_Elmt (L);
11358          while Present (Elmt) loop
11359             if Node (Elmt) = Subp then
11360                return True;
11361             end if;
11362
11363             Next_Elmt (Elmt);
11364          end loop;
11365
11366          return False;
11367       end In_List;
11368
11369       --------------------
11370       -- Remove_Homonym --
11371       --------------------
11372
11373       procedure Remove_Homonym (E : Entity_Id) is
11374          Prev  : Entity_Id := Empty;
11375          H     : Entity_Id;
11376
11377       begin
11378          if E = Current_Entity (E) then
11379             Set_Current_Entity (Homonym (E));
11380          else
11381             H := Current_Entity (E);
11382             while Present (H) and then H /= E loop
11383                Prev := H;
11384                H    := Homonym (H);
11385             end loop;
11386
11387             Set_Homonym (Prev, Homonym (E));
11388          end if;
11389       end Remove_Homonym;
11390
11391       --  Local Variables
11392
11393       E           : Entity_Id;
11394       Elmt        : Elmt_Id;
11395       Iface       : Entity_Id;
11396       Iface_Subp  : Entity_Id;
11397       New_Subp    : Entity_Id := Empty;
11398       Op_List     : Elist_Id;
11399       Parent_Base : Entity_Id;
11400       Subp        : Entity_Id;
11401
11402    --  Start of processing for Derive_Interface_Subprograms
11403
11404    begin
11405       if Ada_Version < Ada_05
11406         or else not Is_Record_Type (Tagged_Type)
11407         or else not Is_Tagged_Type (Tagged_Type)
11408         or else not Has_Abstract_Interfaces (Tagged_Type)
11409       then
11410          return;
11411       end if;
11412
11413       --  Add to the list of interface subprograms all the primitives inherited
11414       --  from abstract interfaces that are not immediate ancestors and also
11415       --  add their derivation to the list of interface primitives.
11416
11417       Op_List := Collect_Interface_Primitives (Tagged_Type);
11418
11419       Elmt := First_Elmt (Op_List);
11420       while Present (Elmt) loop
11421          Subp  := Node (Elmt);
11422          Iface := Find_Dispatching_Type (Subp);
11423
11424          if Is_Concurrent_Record_Type (Tagged_Type) then
11425             if not Present (Abstract_Interface_Alias (Subp)) then
11426                Derive_Subprogram (New_Subp, Subp, Tagged_Type, Iface);
11427                Append_Elmt (New_Subp, Ifaces_List);
11428             end if;
11429
11430          elsif not Is_Parent (Iface, Tagged_Type) then
11431             Derive_Subprogram (New_Subp, Subp, Tagged_Type, Iface);
11432             Append_Elmt (New_Subp, Ifaces_List);
11433          end if;
11434
11435          Next_Elmt (Elmt);
11436       end loop;
11437
11438       --  Complete the derivation of the interface subprograms. Assign to each
11439       --  entity associated with abstract interfaces their aliased entity and
11440       --  complete their decoration as hidden interface entities that will be
11441       --  used later to build the secondary dispatch tables.
11442
11443       if not Is_Empty_Elmt_List (Ifaces_List) then
11444          if Ekind (Parent_Type) = E_Record_Type_With_Private
11445            and then Has_Discriminants (Parent_Type)
11446            and then Present (Full_View (Parent_Type))
11447          then
11448             Parent_Base := Full_View (Parent_Type);
11449          else
11450             Parent_Base := Parent_Type;
11451          end if;
11452
11453          Elmt := First_Elmt (Ifaces_List);
11454          while Present (Elmt) loop
11455             Iface_Subp := Node (Elmt);
11456
11457             --  Look for the first overriding entity in the homonym chain.
11458             --  In this way if we are in the private part of a package spec
11459             --  we get the last overriding subprogram.
11460
11461             E  := Current_Entity_In_Scope (Iface_Subp);
11462             while Present (E) loop
11463                if Is_Dispatching_Operation (E)
11464                  and then Scope (E) = Scope (Iface_Subp)
11465                  and then Type_Conformant (E, Iface_Subp)
11466                  and then not In_List (Ifaces_List, E)
11467                then
11468                   exit;
11469                end if;
11470
11471                E := Homonym (E);
11472             end loop;
11473
11474             --  Create an overriding entity if not found in the homonym chain
11475
11476             if not Present (E) then
11477                Derive_Subprogram
11478                  (E, Alias (Iface_Subp), Tagged_Type, Parent_Base);
11479
11480             elsif not In_List (Primitive_Operations (Tagged_Type), E) then
11481
11482                --  Inherit the operation from the private view
11483
11484                Append_Elmt (E, Primitive_Operations (Tagged_Type));
11485             end if;
11486
11487             --  Complete the decoration of the hidden interface entity
11488
11489             Set_Is_Hidden                (Iface_Subp);
11490             Set_Abstract_Interface_Alias (Iface_Subp, Alias (Iface_Subp));
11491             Set_Alias                    (Iface_Subp, E);
11492             Set_Is_Abstract_Subprogram   (Iface_Subp,
11493                                           Is_Abstract_Subprogram (E));
11494             Remove_Homonym               (Iface_Subp);
11495
11496             --  Hidden entities associated with interfaces must have set the
11497             --  Has_Delay_Freeze attribute to ensure that the corresponding
11498             --  entry of the secondary dispatch table is filled when such
11499             --  entity is frozen.
11500
11501             Set_Has_Delayed_Freeze (Iface_Subp);
11502
11503             Next_Elmt (Elmt);
11504          end loop;
11505       end if;
11506    end Derive_Interface_Subprograms;
11507
11508    -----------------------
11509    -- Derive_Subprogram --
11510    -----------------------
11511
11512    procedure Derive_Subprogram
11513      (New_Subp     : in out Entity_Id;
11514       Parent_Subp  : Entity_Id;
11515       Derived_Type : Entity_Id;
11516       Parent_Type  : Entity_Id;
11517       Actual_Subp  : Entity_Id := Empty)
11518    is
11519       Formal : Entity_Id;
11520       --  Formal parameter of parent primitive operation
11521
11522       Formal_Of_Actual : Entity_Id;
11523       --  Formal parameter of actual operation, when the derivation is to
11524       --  create a renaming for a primitive operation of an actual in an
11525       --  instantiation.
11526
11527       New_Formal : Entity_Id;
11528       --  Formal of inherited operation
11529
11530       Visible_Subp : Entity_Id := Parent_Subp;
11531
11532       function Is_Private_Overriding return Boolean;
11533       --  If Subp is a private overriding of a visible operation, the inherited
11534       --  operation derives from the overridden op (even though its body is the
11535       --  overriding one) and the inherited operation is visible now. See
11536       --  sem_disp to see the full details of the handling of the overridden
11537       --  subprogram, which is removed from the list of primitive operations of
11538       --  the type. The overridden subprogram is saved locally in Visible_Subp,
11539       --  and used to diagnose abstract operations that need overriding in the
11540       --  derived type.
11541
11542       procedure Replace_Type (Id, New_Id : Entity_Id);
11543       --  When the type is an anonymous access type, create a new access type
11544       --  designating the derived type.
11545
11546       procedure Set_Derived_Name;
11547       --  This procedure sets the appropriate Chars name for New_Subp. This
11548       --  is normally just a copy of the parent name. An exception arises for
11549       --  type support subprograms, where the name is changed to reflect the
11550       --  name of the derived type, e.g. if type foo is derived from type bar,
11551       --  then a procedure barDA is derived with a name fooDA.
11552
11553       ---------------------------
11554       -- Is_Private_Overriding --
11555       ---------------------------
11556
11557       function Is_Private_Overriding return Boolean is
11558          Prev : Entity_Id;
11559
11560       begin
11561          --  If the parent is not a dispatching operation there is no
11562          --  need to investigate overridings
11563
11564          if not Is_Dispatching_Operation (Parent_Subp) then
11565             return False;
11566          end if;
11567
11568          --  The visible operation that is overridden is a homonym of the
11569          --  parent subprogram. We scan the homonym chain to find the one
11570          --  whose alias is the subprogram we are deriving.
11571
11572          Prev := Current_Entity (Parent_Subp);
11573          while Present (Prev) loop
11574             if Ekind (Prev) = Ekind (Parent_Subp)
11575               and then Alias (Prev) = Parent_Subp
11576               and then Scope (Parent_Subp) = Scope (Prev)
11577               and then not Is_Hidden (Prev)
11578             then
11579                Visible_Subp := Prev;
11580                return True;
11581             end if;
11582
11583             Prev := Homonym (Prev);
11584          end loop;
11585
11586          return False;
11587       end Is_Private_Overriding;
11588
11589       ------------------
11590       -- Replace_Type --
11591       ------------------
11592
11593       procedure Replace_Type (Id, New_Id : Entity_Id) is
11594          Acc_Type : Entity_Id;
11595          Par      : constant Node_Id := Parent (Derived_Type);
11596
11597       begin
11598          --  When the type is an anonymous access type, create a new access
11599          --  type designating the derived type. This itype must be elaborated
11600          --  at the point of the derivation, not on subsequent calls that may
11601          --  be out of the proper scope for Gigi, so we insert a reference to
11602          --  it after the derivation.
11603
11604          if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
11605             declare
11606                Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
11607
11608             begin
11609                if Ekind (Desig_Typ) = E_Record_Type_With_Private
11610                  and then Present (Full_View (Desig_Typ))
11611                  and then not Is_Private_Type (Parent_Type)
11612                then
11613                   Desig_Typ := Full_View (Desig_Typ);
11614                end if;
11615
11616                if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
11617
11618                   --  Ada 2005 (AI-251): Handle also derivations of abstract
11619                   --  interface primitives.
11620
11621                  or else (Is_Interface (Desig_Typ)
11622                           and then not Is_Class_Wide_Type (Desig_Typ))
11623                then
11624                   Acc_Type := New_Copy (Etype (Id));
11625                   Set_Etype (Acc_Type, Acc_Type);
11626                   Set_Scope (Acc_Type, New_Subp);
11627
11628                   --  Compute size of anonymous access type
11629
11630                   if Is_Array_Type (Desig_Typ)
11631                     and then not Is_Constrained (Desig_Typ)
11632                   then
11633                      Init_Size (Acc_Type, 2 * System_Address_Size);
11634                   else
11635                      Init_Size (Acc_Type, System_Address_Size);
11636                   end if;
11637
11638                   Init_Alignment (Acc_Type);
11639                   Set_Directly_Designated_Type (Acc_Type, Derived_Type);
11640
11641                   Set_Etype (New_Id, Acc_Type);
11642                   Set_Scope (New_Id, New_Subp);
11643
11644                   --  Create a reference to it
11645                   Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
11646
11647                else
11648                   Set_Etype (New_Id, Etype (Id));
11649                end if;
11650             end;
11651
11652          elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
11653            or else
11654              (Ekind (Etype (Id)) = E_Record_Type_With_Private
11655                and then Present (Full_View (Etype (Id)))
11656                and then
11657                  Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
11658          then
11659             --  Constraint checks on formals are generated during expansion,
11660             --  based on the signature of the original subprogram. The bounds
11661             --  of the derived type are not relevant, and thus we can use
11662             --  the base type for the formals. However, the return type may be
11663             --  used in a context that requires that the proper static bounds
11664             --  be used (a case statement, for example)  and for those cases
11665             --  we must use the derived type (first subtype), not its base.
11666
11667             --  If the derived_type_definition has no constraints, we know that
11668             --  the derived type has the same constraints as the first subtype
11669             --  of the parent, and we can also use it rather than its base,
11670             --  which can lead to more efficient code.
11671
11672             if Etype (Id) = Parent_Type then
11673                if Is_Scalar_Type (Parent_Type)
11674                  and then
11675                    Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
11676                then
11677                   Set_Etype (New_Id, Derived_Type);
11678
11679                elsif Nkind (Par) = N_Full_Type_Declaration
11680                  and then
11681                    Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
11682                  and then
11683                    Is_Entity_Name
11684                      (Subtype_Indication (Type_Definition (Par)))
11685                then
11686                   Set_Etype (New_Id, Derived_Type);
11687
11688                else
11689                   Set_Etype (New_Id, Base_Type (Derived_Type));
11690                end if;
11691
11692             else
11693                Set_Etype (New_Id, Base_Type (Derived_Type));
11694             end if;
11695
11696          --  Ada 2005 (AI-251): Handle derivations of abstract interface
11697          --  primitives.
11698
11699          elsif Is_Interface (Etype (Id))
11700            and then not Is_Class_Wide_Type (Etype (Id))
11701            and then Is_Progenitor (Etype (Id), Derived_Type)
11702          then
11703             Set_Etype (New_Id, Derived_Type);
11704
11705          else
11706             Set_Etype (New_Id, Etype (Id));
11707          end if;
11708       end Replace_Type;
11709
11710       ----------------------
11711       -- Set_Derived_Name --
11712       ----------------------
11713
11714       procedure Set_Derived_Name is
11715          Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
11716       begin
11717          if Nm = TSS_Null then
11718             Set_Chars (New_Subp, Chars (Parent_Subp));
11719          else
11720             Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
11721          end if;
11722       end Set_Derived_Name;
11723
11724    --  Start of processing for Derive_Subprogram
11725
11726    begin
11727       New_Subp :=
11728          New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
11729       Set_Ekind (New_Subp, Ekind (Parent_Subp));
11730
11731       --  Check whether the inherited subprogram is a private operation that
11732       --  should be inherited but not yet made visible. Such subprograms can
11733       --  become visible at a later point (e.g., the private part of a public
11734       --  child unit) via Declare_Inherited_Private_Subprograms. If the
11735       --  following predicate is true, then this is not such a private
11736       --  operation and the subprogram simply inherits the name of the parent
11737       --  subprogram. Note the special check for the names of controlled
11738       --  operations, which are currently exempted from being inherited with
11739       --  a hidden name because they must be findable for generation of
11740       --  implicit run-time calls.
11741
11742       if not Is_Hidden (Parent_Subp)
11743         or else Is_Internal (Parent_Subp)
11744         or else Is_Private_Overriding
11745         or else Is_Internal_Name (Chars (Parent_Subp))
11746         or else Chars (Parent_Subp) = Name_Initialize
11747         or else Chars (Parent_Subp) = Name_Adjust
11748         or else Chars (Parent_Subp) = Name_Finalize
11749       then
11750          Set_Derived_Name;
11751
11752       --  If parent is hidden, this can be a regular derivation if the
11753       --  parent is immediately visible in a non-instantiating context,
11754       --  or if we are in the private part of an instance. This test
11755       --  should still be refined ???
11756
11757       --  The test for In_Instance_Not_Visible avoids inheriting the derived
11758       --  operation as a non-visible operation in cases where the parent
11759       --  subprogram might not be visible now, but was visible within the
11760       --  original generic, so it would be wrong to make the inherited
11761       --  subprogram non-visible now. (Not clear if this test is fully
11762       --  correct; are there any cases where we should declare the inherited
11763       --  operation as not visible to avoid it being overridden, e.g., when
11764       --  the parent type is a generic actual with private primitives ???)
11765
11766       --  (they should be treated the same as other private inherited
11767       --  subprograms, but it's not clear how to do this cleanly). ???
11768
11769       elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
11770               and then Is_Immediately_Visible (Parent_Subp)
11771               and then not In_Instance)
11772         or else In_Instance_Not_Visible
11773       then
11774          Set_Derived_Name;
11775
11776       --  Ada 2005 (AI-251): Hidden entity associated with abstract interface
11777       --  primitive
11778
11779       elsif Present (Abstract_Interface_Alias (Parent_Subp)) then
11780          Set_Derived_Name;
11781
11782       --  The type is inheriting a private operation, so enter
11783       --  it with a special name so it can't be overridden.
11784
11785       else
11786          Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
11787       end if;
11788
11789       Set_Parent (New_Subp, Parent (Derived_Type));
11790
11791       if Present (Actual_Subp) then
11792          Replace_Type (Actual_Subp, New_Subp);
11793       else
11794          Replace_Type (Parent_Subp, New_Subp);
11795       end if;
11796
11797       Conditional_Delay (New_Subp, Parent_Subp);
11798
11799       --  If we are creating a renaming for a primitive operation of an
11800       --  actual of a generic derived type, we must examine the signature
11801       --  of the actual primitive, not that of the generic formal, which for
11802       --  example may be an interface. However the name and initial value
11803       --  of the inherited operation are those of the formal primitive.
11804
11805       Formal := First_Formal (Parent_Subp);
11806
11807       if Present (Actual_Subp) then
11808          Formal_Of_Actual := First_Formal (Actual_Subp);
11809       else
11810          Formal_Of_Actual := Empty;
11811       end if;
11812
11813       while Present (Formal) loop
11814          New_Formal := New_Copy (Formal);
11815
11816          --  Normally we do not go copying parents, but in the case of
11817          --  formals, we need to link up to the declaration (which is the
11818          --  parameter specification), and it is fine to link up to the
11819          --  original formal's parameter specification in this case.
11820
11821          Set_Parent (New_Formal, Parent (Formal));
11822          Append_Entity (New_Formal, New_Subp);
11823
11824          if Present (Formal_Of_Actual) then
11825             Replace_Type (Formal_Of_Actual, New_Formal);
11826             Next_Formal (Formal_Of_Actual);
11827          else
11828             Replace_Type (Formal, New_Formal);
11829          end if;
11830
11831          Next_Formal (Formal);
11832       end loop;
11833
11834       --  If this derivation corresponds to a tagged generic actual, then
11835       --  primitive operations rename those of the actual. Otherwise the
11836       --  primitive operations rename those of the parent type, If the parent
11837       --  renames an intrinsic operator, so does the new subprogram. We except
11838       --  concatenation, which is always properly typed, and does not get
11839       --  expanded as other intrinsic operations.
11840
11841       if No (Actual_Subp) then
11842          if Is_Intrinsic_Subprogram (Parent_Subp) then
11843             Set_Is_Intrinsic_Subprogram (New_Subp);
11844
11845             if Present (Alias (Parent_Subp))
11846               and then Chars (Parent_Subp) /= Name_Op_Concat
11847             then
11848                Set_Alias (New_Subp, Alias (Parent_Subp));
11849             else
11850                Set_Alias (New_Subp, Parent_Subp);
11851             end if;
11852
11853          else
11854             Set_Alias (New_Subp, Parent_Subp);
11855          end if;
11856
11857       else
11858          Set_Alias (New_Subp, Actual_Subp);
11859       end if;
11860
11861       --  Derived subprograms of a tagged type must inherit the convention
11862       --  of the parent subprogram (a requirement of AI-117). Derived
11863       --  subprograms of untagged types simply get convention Ada by default.
11864
11865       if Is_Tagged_Type (Derived_Type) then
11866          Set_Convention (New_Subp, Convention (Parent_Subp));
11867       end if;
11868
11869       Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
11870       Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
11871
11872       if Ekind (Parent_Subp) = E_Procedure then
11873          Set_Is_Valued_Procedure
11874            (New_Subp, Is_Valued_Procedure (Parent_Subp));
11875       end if;
11876
11877       --  No_Return must be inherited properly. If this is overridden in the
11878       --  case of a dispatching operation, then a check is made in Sem_Disp
11879       --  that the overriding operation is also No_Return (no such check is
11880       --  required for the case of non-dispatching operation.
11881
11882       Set_No_Return (New_Subp, No_Return (Parent_Subp));
11883
11884       --  A derived function with a controlling result is abstract. If the
11885       --  Derived_Type is a nonabstract formal generic derived type, then
11886       --  inherited operations are not abstract: the required check is done at
11887       --  instantiation time. If the derivation is for a generic actual, the
11888       --  function is not abstract unless the actual is.
11889
11890       if Is_Generic_Type (Derived_Type)
11891         and then not Is_Abstract_Type (Derived_Type)
11892       then
11893          null;
11894
11895       --  Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
11896       --  properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
11897
11898       elsif Ada_Version >= Ada_05
11899         and then (Is_Abstract_Subprogram (Alias (New_Subp))
11900                    or else (Is_Tagged_Type (Derived_Type)
11901                             and then Etype (New_Subp) = Derived_Type
11902                             and then not Is_Null_Extension (Derived_Type))
11903                    or else (Is_Tagged_Type (Derived_Type)
11904                             and then Ekind (Etype (New_Subp)) =
11905                                                        E_Anonymous_Access_Type
11906                             and then Designated_Type (Etype (New_Subp)) =
11907                                                        Derived_Type
11908                             and then not Is_Null_Extension (Derived_Type)))
11909         and then No (Actual_Subp)
11910       then
11911          if not Is_Tagged_Type (Derived_Type)
11912            or else Is_Abstract_Type (Derived_Type)
11913            or else Is_Abstract_Subprogram (Alias (New_Subp))
11914          then
11915             Set_Is_Abstract_Subprogram (New_Subp);
11916          else
11917             Set_Requires_Overriding (New_Subp);
11918          end if;
11919
11920       elsif Ada_Version < Ada_05
11921         and then (Is_Abstract_Subprogram (Alias (New_Subp))
11922                    or else (Is_Tagged_Type (Derived_Type)
11923                              and then Etype (New_Subp) = Derived_Type
11924                              and then No (Actual_Subp)))
11925       then
11926          Set_Is_Abstract_Subprogram (New_Subp);
11927
11928       --  Finally, if the parent type is abstract we must verify that all
11929       --  inherited operations are either non-abstract or overridden, or that
11930       --  the derived type itself is abstract (this check is performed at the
11931       --  end of a package declaration, in Check_Abstract_Overriding). A
11932       --  private overriding in the parent type will not be visible in the
11933       --  derivation if we are not in an inner package or in a child unit of
11934       --  the parent type, in which case the abstractness of the inherited
11935       --  operation is carried to the new subprogram.
11936
11937       elsif Is_Abstract_Type (Parent_Type)
11938         and then not In_Open_Scopes (Scope (Parent_Type))
11939         and then Is_Private_Overriding
11940         and then Is_Abstract_Subprogram (Visible_Subp)
11941       then
11942          if No (Actual_Subp) then
11943             Set_Alias (New_Subp, Visible_Subp);
11944             Set_Is_Abstract_Subprogram
11945               (New_Subp, True);
11946          else
11947             --  If this is a derivation for an instance of a formal derived
11948             --  type, abstractness comes from the primitive operation of the
11949             --  actual, not from the operation inherited from the ancestor.
11950
11951             Set_Is_Abstract_Subprogram
11952               (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
11953          end if;
11954       end if;
11955
11956       New_Overloaded_Entity (New_Subp, Derived_Type);
11957
11958       --  Check for case of a derived subprogram for the instantiation of a
11959       --  formal derived tagged type, if so mark the subprogram as dispatching
11960       --  and inherit the dispatching attributes of the parent subprogram. The
11961       --  derived subprogram is effectively renaming of the actual subprogram,
11962       --  so it needs to have the same attributes as the actual.
11963
11964       if Present (Actual_Subp)
11965         and then Is_Dispatching_Operation (Parent_Subp)
11966       then
11967          Set_Is_Dispatching_Operation (New_Subp);
11968
11969          if Present (DTC_Entity (Parent_Subp)) then
11970             Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
11971             Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
11972          end if;
11973       end if;
11974
11975       --  Indicate that a derived subprogram does not require a body and that
11976       --  it does not require processing of default expressions.
11977
11978       Set_Has_Completion (New_Subp);
11979       Set_Default_Expressions_Processed (New_Subp);
11980
11981       if Ekind (New_Subp) = E_Function then
11982          Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
11983       end if;
11984    end Derive_Subprogram;
11985
11986    ------------------------
11987    -- Derive_Subprograms --
11988    ------------------------
11989
11990    procedure Derive_Subprograms
11991      (Parent_Type    : Entity_Id;
11992       Derived_Type   : Entity_Id;
11993       Generic_Actual : Entity_Id := Empty)
11994    is
11995       Op_List      : constant Elist_Id :=
11996                        Collect_Primitive_Operations (Parent_Type);
11997       Ifaces_List  : constant Elist_Id := New_Elmt_List;
11998       Predef_Prims : constant Elist_Id := New_Elmt_List;
11999       Act_List     : Elist_Id;
12000       Act_Elmt     : Elmt_Id;
12001       Elmt         : Elmt_Id;
12002       New_Subp     : Entity_Id := Empty;
12003       Parent_Base  : Entity_Id;
12004       Subp         : Entity_Id;
12005
12006    begin
12007       if Ekind (Parent_Type) = E_Record_Type_With_Private
12008         and then Has_Discriminants (Parent_Type)
12009         and then Present (Full_View (Parent_Type))
12010       then
12011          Parent_Base := Full_View (Parent_Type);
12012       else
12013          Parent_Base := Parent_Type;
12014       end if;
12015
12016       --  Derive primitives inherited from the parent. Note that if the generic
12017       --  actual is present, this is not really a type derivation, it is a
12018       --  completion within an instance.
12019
12020       if Present (Generic_Actual) then
12021          Act_List := Collect_Primitive_Operations (Generic_Actual);
12022          Act_Elmt := First_Elmt (Act_List);
12023       else
12024          Act_Elmt := No_Elmt;
12025       end if;
12026
12027       --  Literals are derived earlier in the process of building the derived
12028       --  type, and are skipped here.
12029
12030       Elmt := First_Elmt (Op_List);
12031       while Present (Elmt) loop
12032          Subp := Node (Elmt);
12033
12034          if Ekind (Subp) /= E_Enumeration_Literal then
12035
12036             if Ada_Version >= Ada_05
12037               and then Present (Abstract_Interface_Alias (Subp))
12038             then
12039                null;
12040
12041             --  We derive predefined primitives in a later round to ensure that
12042             --  they are always added to the list of primitives after user
12043             --  defined primitives (because predefined primitives have to be
12044             --  skipped when matching the operations of a parent interface to
12045             --  those of a concrete type). However it is unclear why those
12046             --  primitives would be needed in an instantiation???
12047
12048             elsif Is_Predefined_Dispatching_Operation (Subp) then
12049                Append_Elmt (Subp, Predef_Prims);
12050
12051             elsif No (Generic_Actual) then
12052                Derive_Subprogram (New_Subp, Subp, Derived_Type, Parent_Base);
12053
12054                --  Ada 2005 (AI-251): Add derivation of an abstract interface
12055                --  primitive to the list of entities to which we have to
12056                --  associate an aliased entity.
12057
12058                if Ada_Version >= Ada_05
12059                  and then Is_Dispatching_Operation (Subp)
12060                  and then Present (Find_Dispatching_Type (Subp))
12061                  and then Is_Interface (Find_Dispatching_Type (Subp))
12062                then
12063                   Append_Elmt (New_Subp, Ifaces_List);
12064                end if;
12065
12066             else
12067                --  If the generic parent type is present, the derived type
12068                --  is an instance of a formal derived type, and within the
12069                --  instance its operations are those of the actual. We derive
12070                --  from the formal type but make the inherited operations
12071                --  aliases of the corresponding operations of the actual.
12072
12073                if Is_Interface (Parent_Type)
12074                  and then Root_Type (Derived_Type) /= Parent_Type
12075                then
12076                   --  Find the corresponding operation in the generic actual.
12077                   --  Given that the actual is not a direct descendant of the
12078                   --  parent, as in Ada 95, the primitives are not necessarily
12079                   --  in the same order, so we have to traverse the list of
12080                   --  primitive operations of the actual to find the one that
12081                   --  implements the interface operation.
12082
12083                   --  Note that if the parent type is the direct ancestor of
12084                   --  the derived type, then even if it is an interface the
12085                   --  operations are inherited from the primary dispatch table
12086                   --  and are in the proper order.
12087
12088                   Act_Elmt := First_Elmt (Act_List);
12089                   while Present (Act_Elmt) loop
12090                      exit when
12091                        Abstract_Interface_Alias (Node (Act_Elmt)) = Subp;
12092                      Next_Elmt (Act_Elmt);
12093                   end loop;
12094                end if;
12095
12096                --  If the formal is not an interface, the actual is a direct
12097                --  descendant and the common  primitive operations appear in
12098                --  the same order.
12099
12100                Derive_Subprogram
12101                  (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
12102
12103                if Present (Act_Elmt) then
12104                   Next_Elmt (Act_Elmt);
12105                end if;
12106             end if;
12107          end if;
12108
12109          Next_Elmt (Elmt);
12110       end loop;
12111
12112       --  Inherit additional operations from progenitor interfaces. However,
12113       --  if the derived type is a generic actual, there are not new primitive
12114       --  operations for the type, because it has those of the actual, so
12115       --  nothing needs to be done. The renamings generated above are not
12116       --  primitive operations, and their purpose is simply to make the proper
12117       --  operations visible within an instantiation.
12118
12119       if Ada_Version >= Ada_05
12120         and then Is_Tagged_Type (Derived_Type)
12121         and then No (Generic_Actual)
12122       then
12123          Derive_Interface_Subprograms (Parent_Type, Derived_Type, Ifaces_List);
12124       end if;
12125
12126       --  Derive predefined primitives
12127
12128       if not Is_Empty_Elmt_List (Predef_Prims) then
12129          Elmt := First_Elmt (Predef_Prims);
12130          while Present (Elmt) loop
12131             Derive_Subprogram
12132               (New_Subp, Node (Elmt), Derived_Type, Parent_Base);
12133             Next_Elmt (Elmt);
12134          end loop;
12135       end if;
12136    end Derive_Subprograms;
12137
12138    --------------------------------
12139    -- Derived_Standard_Character --
12140    --------------------------------
12141
12142    procedure Derived_Standard_Character
12143      (N            : Node_Id;
12144       Parent_Type  : Entity_Id;
12145       Derived_Type : Entity_Id)
12146    is
12147       Loc           : constant Source_Ptr := Sloc (N);
12148       Def           : constant Node_Id    := Type_Definition (N);
12149       Indic         : constant Node_Id    := Subtype_Indication (Def);
12150       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
12151       Implicit_Base : constant Entity_Id  :=
12152                         Create_Itype
12153                           (E_Enumeration_Type, N, Derived_Type, 'B');
12154
12155       Lo : Node_Id;
12156       Hi : Node_Id;
12157
12158    begin
12159       Discard_Node (Process_Subtype (Indic, N));
12160
12161       Set_Etype     (Implicit_Base, Parent_Base);
12162       Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
12163       Set_RM_Size   (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
12164
12165       Set_Is_Character_Type  (Implicit_Base, True);
12166       Set_Has_Delayed_Freeze (Implicit_Base);
12167
12168       --  The bounds of the implicit base are the bounds of the parent base.
12169       --  Note that their type is the parent base.
12170
12171       Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Base));
12172       Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
12173
12174       Set_Scalar_Range (Implicit_Base,
12175         Make_Range (Loc,
12176           Low_Bound  => Lo,
12177           High_Bound => Hi));
12178
12179       Conditional_Delay (Derived_Type, Parent_Type);
12180
12181       Set_Ekind (Derived_Type, E_Enumeration_Subtype);
12182       Set_Etype (Derived_Type, Implicit_Base);
12183       Set_Size_Info         (Derived_Type, Parent_Type);
12184
12185       if Unknown_RM_Size (Derived_Type) then
12186          Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
12187       end if;
12188
12189       Set_Is_Character_Type (Derived_Type, True);
12190
12191       if Nkind (Indic) /= N_Subtype_Indication then
12192
12193          --  If no explicit constraint, the bounds are those
12194          --  of the parent type.
12195
12196          Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Type));
12197          Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
12198          Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
12199       end if;
12200
12201       Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
12202
12203       --  Because the implicit base is used in the conversion of the bounds, we
12204       --  have to freeze it now. This is similar to what is done for numeric
12205       --  types, and it equally suspicious, but otherwise a non-static bound
12206       --  will have a reference to an unfrozen type, which is rejected by Gigi
12207       --  (???). This requires specific care for definition of stream
12208       --  attributes. For details, see comments at the end of
12209       --  Build_Derived_Numeric_Type.
12210
12211       Freeze_Before (N, Implicit_Base);
12212    end Derived_Standard_Character;
12213
12214    ------------------------------
12215    -- Derived_Type_Declaration --
12216    ------------------------------
12217
12218    procedure Derived_Type_Declaration
12219      (T             : Entity_Id;
12220       N             : Node_Id;
12221       Is_Completion : Boolean)
12222    is
12223       Parent_Type  : Entity_Id;
12224
12225       function Comes_From_Generic (Typ : Entity_Id) return Boolean;
12226       --  Check whether the parent type is a generic formal, or derives
12227       --  directly or indirectly from one.
12228
12229       ------------------------
12230       -- Comes_From_Generic --
12231       ------------------------
12232
12233       function Comes_From_Generic (Typ : Entity_Id) return Boolean is
12234       begin
12235          if Is_Generic_Type (Typ) then
12236             return True;
12237
12238          elsif Is_Generic_Type (Root_Type (Parent_Type)) then
12239             return True;
12240
12241          elsif Is_Private_Type (Typ)
12242            and then Present (Full_View (Typ))
12243            and then Is_Generic_Type (Root_Type (Full_View (Typ)))
12244          then
12245             return True;
12246
12247          elsif Is_Generic_Actual_Type (Typ) then
12248             return True;
12249
12250          else
12251             return False;
12252          end if;
12253       end Comes_From_Generic;
12254
12255       --  Local variables
12256
12257       Def          : constant Node_Id := Type_Definition (N);
12258       Iface_Def    : Node_Id;
12259       Indic        : constant Node_Id := Subtype_Indication (Def);
12260       Extension    : constant Node_Id := Record_Extension_Part (Def);
12261       Parent_Node  : Node_Id;
12262       Parent_Scope : Entity_Id;
12263       Taggd        : Boolean;
12264
12265    --  Start of processing for Derived_Type_Declaration
12266
12267    begin
12268       Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
12269
12270       --  Ada 2005 (AI-251): In case of interface derivation check that the
12271       --  parent is also an interface.
12272
12273       if Interface_Present (Def) then
12274          if not Is_Interface (Parent_Type) then
12275             Error_Msg_NE
12276               ("(Ada 2005) & must be an interface", Indic, Parent_Type);
12277
12278          else
12279             Parent_Node := Parent (Base_Type (Parent_Type));
12280             Iface_Def   := Type_Definition (Parent_Node);
12281
12282             --  Ada 2005 (AI-251): Limited interfaces can only inherit from
12283             --  other limited interfaces.
12284
12285             if Limited_Present (Def) then
12286                if Limited_Present (Iface_Def) then
12287                   null;
12288
12289                elsif Protected_Present (Iface_Def) then
12290                   Error_Msg_N
12291                     ("(Ada 2005) limited interface cannot "
12292                      & "inherit from protected interface", Indic);
12293
12294                elsif Synchronized_Present (Iface_Def) then
12295                   Error_Msg_N
12296                     ("(Ada 2005) limited interface cannot "
12297                      & "inherit from synchronized interface", Indic);
12298
12299                elsif Task_Present (Iface_Def) then
12300                   Error_Msg_N
12301                     ("(Ada 2005) limited interface cannot "
12302                      & "inherit from task interface", Indic);
12303
12304                else
12305                   Error_Msg_N
12306                     ("(Ada 2005) limited interface cannot "
12307                      & "inherit from non-limited interface", Indic);
12308                end if;
12309
12310             --  Ada 2005 (AI-345): Non-limited interfaces can only inherit
12311             --  from non-limited or limited interfaces.
12312
12313             elsif not Protected_Present (Def)
12314               and then not Synchronized_Present (Def)
12315               and then not Task_Present (Def)
12316             then
12317                if Limited_Present (Iface_Def) then
12318                   null;
12319
12320                elsif Protected_Present (Iface_Def) then
12321                   Error_Msg_N
12322                     ("(Ada 2005) non-limited interface cannot "
12323                      & "inherit from protected interface", Indic);
12324
12325                elsif Synchronized_Present (Iface_Def) then
12326                   Error_Msg_N
12327                     ("(Ada 2005) non-limited interface cannot "
12328                      & "inherit from synchronized interface", Indic);
12329
12330                elsif Task_Present (Iface_Def) then
12331                   Error_Msg_N
12332                     ("(Ada 2005) non-limited interface cannot "
12333                      & "inherit from task interface", Indic);
12334
12335                else
12336                   null;
12337                end if;
12338             end if;
12339          end if;
12340       end if;
12341
12342       if Is_Tagged_Type (Parent_Type)
12343         and then Is_Concurrent_Type (Parent_Type)
12344         and then not Is_Interface (Parent_Type)
12345       then
12346          Error_Msg_N
12347            ("parent type of a record extension cannot be "
12348             & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
12349          Set_Etype (T, Any_Type);
12350          return;
12351       end if;
12352
12353       --  Ada 2005 (AI-251): Decorate all the names in the list of ancestor
12354       --  interfaces
12355
12356       if Is_Tagged_Type (Parent_Type)
12357         and then Is_Non_Empty_List (Interface_List (Def))
12358       then
12359          declare
12360             Intf : Node_Id;
12361             T    : Entity_Id;
12362
12363          begin
12364             Intf := First (Interface_List (Def));
12365             while Present (Intf) loop
12366                T := Find_Type_Of_Subtype_Indic (Intf);
12367
12368                if not Is_Interface (T) then
12369                   Error_Msg_NE ("(Ada 2005) & must be an interface", Intf, T);
12370
12371                --  Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
12372                --  a limited type from having a nonlimited progenitor.
12373
12374                elsif (Limited_Present (Def)
12375                        or else (not Is_Interface (Parent_Type)
12376                                  and then Is_Limited_Type (Parent_Type)))
12377                  and then not Is_Limited_Interface (T)
12378                then
12379                   Error_Msg_NE
12380                    ("progenitor interface& of limited type must be limited",
12381                      N, T);
12382                end if;
12383
12384                Next (Intf);
12385             end loop;
12386          end;
12387       end if;
12388
12389       if Parent_Type = Any_Type
12390         or else Etype (Parent_Type) = Any_Type
12391         or else (Is_Class_Wide_Type (Parent_Type)
12392                    and then Etype (Parent_Type) = T)
12393       then
12394          --  If Parent_Type is undefined or illegal, make new type into a
12395          --  subtype of Any_Type, and set a few attributes to prevent cascaded
12396          --  errors. If this is a self-definition, emit error now.
12397
12398          if T = Parent_Type
12399            or else T = Etype (Parent_Type)
12400          then
12401             Error_Msg_N ("type cannot be used in its own definition", Indic);
12402          end if;
12403
12404          Set_Ekind        (T, Ekind (Parent_Type));
12405          Set_Etype        (T, Any_Type);
12406          Set_Scalar_Range (T, Scalar_Range (Any_Type));
12407
12408          if Is_Tagged_Type (T) then
12409             Set_Primitive_Operations (T, New_Elmt_List);
12410          end if;
12411
12412          return;
12413       end if;
12414
12415       --  Ada 2005 (AI-251): The case in which the parent of the full-view is
12416       --  an interface is special because the list of interfaces in the full
12417       --  view can be given in any order. For example:
12418
12419       --     type A is interface;
12420       --     type B is interface and A;
12421       --     type D is new B with private;
12422       --   private
12423       --     type D is new A and B with null record; -- 1 --
12424
12425       --  In this case we perform the following transformation of -1-:
12426
12427       --     type D is new B and A with null record;
12428
12429       --  If the parent of the full-view covers the parent of the partial-view
12430       --  we have two possible cases:
12431
12432       --     1) They have the same parent
12433       --     2) The parent of the full-view implements some further interfaces
12434
12435       --  In both cases we do not need to perform the transformation. In the
12436       --  first case the source program is correct and the transformation is
12437       --  not needed; in the second case the source program does not fulfill
12438       --  the no-hidden interfaces rule (AI-396) and the error will be reported
12439       --  later.
12440
12441       --  This transformation not only simplifies the rest of the analysis of
12442       --  this type declaration but also simplifies the correct generation of
12443       --  the object layout to the expander.
12444
12445       if In_Private_Part (Current_Scope)
12446         and then Is_Interface (Parent_Type)
12447       then
12448          declare
12449             Iface               : Node_Id;
12450             Partial_View        : Entity_Id;
12451             Partial_View_Parent : Entity_Id;
12452             New_Iface           : Node_Id;
12453
12454          begin
12455             --  Look for the associated private type declaration
12456
12457             Partial_View := First_Entity (Current_Scope);
12458             loop
12459                exit when No (Partial_View)
12460                  or else (Has_Private_Declaration (Partial_View)
12461                            and then Full_View (Partial_View) = T);
12462
12463                Next_Entity (Partial_View);
12464             end loop;
12465
12466             --  If the partial view was not found then the source code has
12467             --  errors and the transformation is not needed.
12468
12469             if Present (Partial_View) then
12470                Partial_View_Parent := Etype (Partial_View);
12471
12472                --  If the parent of the full-view covers the parent of the
12473                --  partial-view we have nothing else to do.
12474
12475                if Interface_Present_In_Ancestor
12476                     (Parent_Type, Partial_View_Parent)
12477                then
12478                   null;
12479
12480                --  Traverse the list of interfaces of the full-view to look
12481                --  for the parent of the partial-view and perform the tree
12482                --  transformation.
12483
12484                else
12485                   Iface := First (Interface_List (Def));
12486                   while Present (Iface) loop
12487                      if Etype (Iface) = Etype (Partial_View) then
12488                         Rewrite (Subtype_Indication (Def),
12489                           New_Copy (Subtype_Indication
12490                                      (Parent (Partial_View))));
12491
12492                         New_Iface := Make_Identifier (Sloc (N),
12493                                        Chars (Parent_Type));
12494                         Append (New_Iface, Interface_List (Def));
12495
12496                         --  Analyze the transformed code
12497
12498                         Derived_Type_Declaration (T, N, Is_Completion);
12499                         return;
12500                      end if;
12501
12502                      Next (Iface);
12503                   end loop;
12504                end if;
12505             end if;
12506          end;
12507       end if;
12508
12509       --  Only composite types other than array types are allowed to have
12510       --  discriminants.
12511
12512       if Present (Discriminant_Specifications (N))
12513         and then (Is_Elementary_Type (Parent_Type)
12514                   or else Is_Array_Type (Parent_Type))
12515         and then not Error_Posted (N)
12516       then
12517          Error_Msg_N
12518            ("elementary or array type cannot have discriminants",
12519             Defining_Identifier (First (Discriminant_Specifications (N))));
12520          Set_Has_Discriminants (T, False);
12521       end if;
12522
12523       --  In Ada 83, a derived type defined in a package specification cannot
12524       --  be used for further derivation until the end of its visible part.
12525       --  Note that derivation in the private part of the package is allowed.
12526
12527       if Ada_Version = Ada_83
12528         and then Is_Derived_Type (Parent_Type)
12529         and then In_Visible_Part (Scope (Parent_Type))
12530       then
12531          if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
12532             Error_Msg_N
12533               ("(Ada 83): premature use of type for derivation", Indic);
12534          end if;
12535       end if;
12536
12537       --  Check for early use of incomplete or private type
12538
12539       if Ekind (Parent_Type) = E_Void
12540         or else Ekind (Parent_Type) = E_Incomplete_Type
12541       then
12542          Error_Msg_N ("premature derivation of incomplete type", Indic);
12543          return;
12544
12545       elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
12546               and then not Comes_From_Generic (Parent_Type))
12547         or else Has_Private_Component (Parent_Type)
12548       then
12549          --  The ancestor type of a formal type can be incomplete, in which
12550          --  case only the operations of the partial view are available in
12551          --  the generic. Subsequent checks may be required when the full
12552          --  view is analyzed, to verify that derivation from a tagged type
12553          --  has an extension.
12554
12555          if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
12556             null;
12557
12558          elsif No (Underlying_Type (Parent_Type))
12559            or else Has_Private_Component (Parent_Type)
12560          then
12561             Error_Msg_N
12562               ("premature derivation of derived or private type", Indic);
12563
12564             --  Flag the type itself as being in error, this prevents some
12565             --  nasty problems with subsequent uses of the malformed type.
12566
12567             Set_Error_Posted (T);
12568
12569          --  Check that within the immediate scope of an untagged partial
12570          --  view it's illegal to derive from the partial view if the
12571          --  full view is tagged. (7.3(7))
12572
12573          --  We verify that the Parent_Type is a partial view by checking
12574          --  that it is not a Full_Type_Declaration (i.e. a private type or
12575          --  private extension declaration), to distinguish a partial view
12576          --  from  a derivation from a private type which also appears as
12577          --  E_Private_Type.
12578
12579          elsif Present (Full_View (Parent_Type))
12580            and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
12581            and then not Is_Tagged_Type (Parent_Type)
12582            and then Is_Tagged_Type (Full_View (Parent_Type))
12583          then
12584             Parent_Scope := Scope (T);
12585             while Present (Parent_Scope)
12586               and then Parent_Scope /= Standard_Standard
12587             loop
12588                if Parent_Scope = Scope (Parent_Type) then
12589                   Error_Msg_N
12590                     ("premature derivation from type with tagged full view",
12591                      Indic);
12592                end if;
12593
12594                Parent_Scope := Scope (Parent_Scope);
12595             end loop;
12596          end if;
12597       end if;
12598
12599       --  Check that form of derivation is appropriate
12600
12601       Taggd := Is_Tagged_Type (Parent_Type);
12602
12603       --  Perhaps the parent type should be changed to the class-wide type's
12604       --  specific type in this case to prevent cascading errors ???
12605
12606       if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
12607          Error_Msg_N ("parent type must not be a class-wide type", Indic);
12608          return;
12609       end if;
12610
12611       if Present (Extension) and then not Taggd then
12612          Error_Msg_N
12613            ("type derived from untagged type cannot have extension", Indic);
12614
12615       elsif No (Extension) and then Taggd then
12616
12617          --  If this declaration is within a private part (or body) of a
12618          --  generic instantiation then the derivation is allowed (the parent
12619          --  type can only appear tagged in this case if it's a generic actual
12620          --  type, since it would otherwise have been rejected in the analysis
12621          --  of the generic template).
12622
12623          if not Is_Generic_Actual_Type (Parent_Type)
12624            or else In_Visible_Part (Scope (Parent_Type))
12625          then
12626             Error_Msg_N
12627               ("type derived from tagged type must have extension", Indic);
12628          end if;
12629       end if;
12630
12631       --  AI-443: Synchronized formal derived types require a private
12632       --  extension. There is no point in checking the ancestor type or
12633       --  the progenitors since the construct is wrong to begin with.
12634
12635       if Ada_Version >= Ada_05
12636         and then Is_Generic_Type (T)
12637         and then Present (Original_Node (N))
12638       then
12639          declare
12640             Decl : constant Node_Id := Original_Node (N);
12641
12642          begin
12643             if Nkind (Decl) = N_Formal_Type_Declaration
12644               and then Nkind (Formal_Type_Definition (Decl)) =
12645                          N_Formal_Derived_Type_Definition
12646               and then Synchronized_Present (Formal_Type_Definition (Decl))
12647               and then No (Extension)
12648
12649                --  Avoid emitting a duplicate error message
12650
12651               and then not Error_Posted (Indic)
12652             then
12653                Error_Msg_N
12654                  ("synchronized derived type must have extension", N);
12655             end if;
12656          end;
12657       end if;
12658
12659       Build_Derived_Type (N, Parent_Type, T, Is_Completion);
12660
12661       --  AI-419: The parent type of an explicitly limited derived type must
12662       --  be a limited type or a limited interface.
12663
12664       if Limited_Present (Def) then
12665          Set_Is_Limited_Record (T);
12666
12667          if Is_Interface (T) then
12668             Set_Is_Limited_Interface (T);
12669          end if;
12670
12671          if not Is_Limited_Type (Parent_Type)
12672            and then
12673              (not Is_Interface (Parent_Type)
12674                or else not Is_Limited_Interface (Parent_Type))
12675          then
12676             Error_Msg_NE ("parent type& of limited type must be limited",
12677               N, Parent_Type);
12678          end if;
12679       end if;
12680    end Derived_Type_Declaration;
12681
12682    ----------------------------------
12683    -- Enumeration_Type_Declaration --
12684    ----------------------------------
12685
12686    procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
12687       Ev     : Uint;
12688       L      : Node_Id;
12689       R_Node : Node_Id;
12690       B_Node : Node_Id;
12691
12692    begin
12693       --  Create identifier node representing lower bound
12694
12695       B_Node := New_Node (N_Identifier, Sloc (Def));
12696       L := First (Literals (Def));
12697       Set_Chars (B_Node, Chars (L));
12698       Set_Entity (B_Node,  L);
12699       Set_Etype (B_Node, T);
12700       Set_Is_Static_Expression (B_Node, True);
12701
12702       R_Node := New_Node (N_Range, Sloc (Def));
12703       Set_Low_Bound  (R_Node, B_Node);
12704
12705       Set_Ekind (T, E_Enumeration_Type);
12706       Set_First_Literal (T, L);
12707       Set_Etype (T, T);
12708       Set_Is_Constrained (T);
12709
12710       Ev := Uint_0;
12711
12712       --  Loop through literals of enumeration type setting pos and rep values
12713       --  except that if the Ekind is already set, then it means that the
12714       --  literal was already constructed (case of a derived type declaration
12715       --  and we should not disturb the Pos and Rep values.
12716
12717       while Present (L) loop
12718          if Ekind (L) /= E_Enumeration_Literal then
12719             Set_Ekind (L, E_Enumeration_Literal);
12720             Set_Enumeration_Pos (L, Ev);
12721             Set_Enumeration_Rep (L, Ev);
12722             Set_Is_Known_Valid  (L, True);
12723          end if;
12724
12725          Set_Etype (L, T);
12726          New_Overloaded_Entity (L);
12727          Generate_Definition (L);
12728          Set_Convention (L, Convention_Intrinsic);
12729
12730          if Nkind (L) = N_Defining_Character_Literal then
12731             Set_Is_Character_Type (T, True);
12732          end if;
12733
12734          Ev := Ev + 1;
12735          Next (L);
12736       end loop;
12737
12738       --  Now create a node representing upper bound
12739
12740       B_Node := New_Node (N_Identifier, Sloc (Def));
12741       Set_Chars (B_Node, Chars (Last (Literals (Def))));
12742       Set_Entity (B_Node,  Last (Literals (Def)));
12743       Set_Etype (B_Node, T);
12744       Set_Is_Static_Expression (B_Node, True);
12745
12746       Set_High_Bound (R_Node, B_Node);
12747
12748       --  Initialize various fields of the type. Some of this information
12749       --  may be overwritten later through rep.clauses.
12750
12751       Set_Scalar_Range    (T, R_Node);
12752       Set_RM_Size         (T, UI_From_Int (Minimum_Size (T)));
12753       Set_Enum_Esize      (T);
12754       Set_Enum_Pos_To_Rep (T, Empty);
12755
12756       --  Set Discard_Names if configuration pragma set, or if there is
12757       --  a parameterless pragma in the current declarative region
12758
12759       if Global_Discard_Names
12760         or else Discard_Names (Scope (T))
12761       then
12762          Set_Discard_Names (T);
12763       end if;
12764
12765       --  Process end label if there is one
12766
12767       if Present (Def) then
12768          Process_End_Label (Def, 'e', T);
12769       end if;
12770    end Enumeration_Type_Declaration;
12771
12772    ---------------------------------
12773    -- Expand_To_Stored_Constraint --
12774    ---------------------------------
12775
12776    function Expand_To_Stored_Constraint
12777      (Typ        : Entity_Id;
12778       Constraint : Elist_Id) return Elist_Id
12779    is
12780       Explicitly_Discriminated_Type : Entity_Id;
12781       Expansion    : Elist_Id;
12782       Discriminant : Entity_Id;
12783
12784       function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
12785       --  Find the nearest type that actually specifies discriminants
12786
12787       ---------------------------------
12788       -- Type_With_Explicit_Discrims --
12789       ---------------------------------
12790
12791       function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
12792          Typ : constant E := Base_Type (Id);
12793
12794       begin
12795          if Ekind (Typ) in Incomplete_Or_Private_Kind then
12796             if Present (Full_View (Typ)) then
12797                return Type_With_Explicit_Discrims (Full_View (Typ));
12798             end if;
12799
12800          else
12801             if Has_Discriminants (Typ) then
12802                return Typ;
12803             end if;
12804          end if;
12805
12806          if Etype (Typ) = Typ then
12807             return Empty;
12808          elsif Has_Discriminants (Typ) then
12809             return Typ;
12810          else
12811             return Type_With_Explicit_Discrims (Etype (Typ));
12812          end if;
12813
12814       end Type_With_Explicit_Discrims;
12815
12816    --  Start of processing for Expand_To_Stored_Constraint
12817
12818    begin
12819       if No (Constraint)
12820         or else Is_Empty_Elmt_List (Constraint)
12821       then
12822          return No_Elist;
12823       end if;
12824
12825       Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
12826
12827       if No (Explicitly_Discriminated_Type) then
12828          return No_Elist;
12829       end if;
12830
12831       Expansion := New_Elmt_List;
12832
12833       Discriminant :=
12834          First_Stored_Discriminant (Explicitly_Discriminated_Type);
12835       while Present (Discriminant) loop
12836          Append_Elmt (
12837            Get_Discriminant_Value (
12838              Discriminant, Explicitly_Discriminated_Type, Constraint),
12839            Expansion);
12840          Next_Stored_Discriminant (Discriminant);
12841       end loop;
12842
12843       return Expansion;
12844    end Expand_To_Stored_Constraint;
12845
12846    ---------------------------
12847    -- Find_Hidden_Interface --
12848    ---------------------------
12849
12850    function Find_Hidden_Interface
12851      (Src  : Elist_Id;
12852       Dest : Elist_Id) return Entity_Id
12853    is
12854       Iface      : Entity_Id;
12855       Iface_Elmt : Elmt_Id;
12856
12857    begin
12858       if Present (Src) and then Present (Dest) then
12859          Iface_Elmt := First_Elmt (Src);
12860          while Present (Iface_Elmt) loop
12861             Iface := Node (Iface_Elmt);
12862
12863             if Is_Interface (Iface)
12864               and then not Contain_Interface (Iface, Dest)
12865             then
12866                return Iface;
12867             end if;
12868
12869             Next_Elmt (Iface_Elmt);
12870          end loop;
12871       end if;
12872
12873       return Empty;
12874    end Find_Hidden_Interface;
12875
12876    --------------------
12877    -- Find_Type_Name --
12878    --------------------
12879
12880    function Find_Type_Name (N : Node_Id) return Entity_Id is
12881       Id       : constant Entity_Id := Defining_Identifier (N);
12882       Prev     : Entity_Id;
12883       New_Id   : Entity_Id;
12884       Prev_Par : Node_Id;
12885
12886    begin
12887       --  Find incomplete declaration, if one was given
12888
12889       Prev := Current_Entity_In_Scope (Id);
12890
12891       if Present (Prev) then
12892
12893          --  Previous declaration exists. Error if not incomplete/private case
12894          --  except if previous declaration is implicit, etc. Enter_Name will
12895          --  emit error if appropriate.
12896
12897          Prev_Par := Parent (Prev);
12898
12899          if not Is_Incomplete_Or_Private_Type (Prev) then
12900             Enter_Name (Id);
12901             New_Id := Id;
12902
12903          elsif not Nkind_In (N, N_Full_Type_Declaration,
12904                                 N_Task_Type_Declaration,
12905                                 N_Protected_Type_Declaration)
12906          then
12907             --  Completion must be a full type declarations (RM 7.3(4))
12908
12909             Error_Msg_Sloc := Sloc (Prev);
12910             Error_Msg_NE ("invalid completion of }", Id, Prev);
12911
12912             --  Set scope of Id to avoid cascaded errors. Entity is never
12913             --  examined again, except when saving globals in generics.
12914
12915             Set_Scope (Id, Current_Scope);
12916             New_Id := Id;
12917
12918          --  Case of full declaration of incomplete type
12919
12920          elsif Ekind (Prev) = E_Incomplete_Type then
12921
12922             --  Indicate that the incomplete declaration has a matching full
12923             --  declaration. The defining occurrence of the incomplete
12924             --  declaration remains the visible one, and the procedure
12925             --  Get_Full_View dereferences it whenever the type is used.
12926
12927             if Present (Full_View (Prev)) then
12928                Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
12929             end if;
12930
12931             Set_Full_View (Prev,  Id);
12932             Append_Entity (Id, Current_Scope);
12933             Set_Is_Public (Id, Is_Public (Prev));
12934             Set_Is_Internal (Id);
12935             New_Id := Prev;
12936
12937          --  Case of full declaration of private type
12938
12939          else
12940             if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
12941                if Etype (Prev) /= Prev then
12942
12943                   --  Prev is a private subtype or a derived type, and needs
12944                   --  no completion.
12945
12946                   Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
12947                   New_Id := Id;
12948
12949                elsif Ekind (Prev) = E_Private_Type
12950                  and then Nkind_In (N, N_Task_Type_Declaration,
12951                                        N_Protected_Type_Declaration)
12952                then
12953                   Error_Msg_N
12954                    ("completion of nonlimited type cannot be limited", N);
12955
12956                elsif Ekind (Prev) = E_Record_Type_With_Private
12957                  and then Nkind_In (N, N_Task_Type_Declaration,
12958                                        N_Protected_Type_Declaration)
12959                then
12960                   if not Is_Limited_Record (Prev) then
12961                      Error_Msg_N
12962                         ("completion of nonlimited type cannot be limited", N);
12963
12964                   elsif No (Interface_List (N)) then
12965                      Error_Msg_N
12966                         ("completion of tagged private type must be tagged",
12967                            N);
12968                   end if;
12969                end if;
12970
12971             --  Ada 2005 (AI-251): Private extension declaration of a task
12972             --  type or a protected type. This case arises when covering
12973             --  interface types.
12974
12975             elsif Nkind_In (N, N_Task_Type_Declaration,
12976                                N_Protected_Type_Declaration)
12977             then
12978                null;
12979
12980             elsif Nkind (N) /= N_Full_Type_Declaration
12981               or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
12982             then
12983                Error_Msg_N
12984                  ("full view of private extension must be an extension", N);
12985
12986             elsif not (Abstract_Present (Parent (Prev)))
12987               and then Abstract_Present (Type_Definition (N))
12988             then
12989                Error_Msg_N
12990                  ("full view of non-abstract extension cannot be abstract", N);
12991             end if;
12992
12993             if not In_Private_Part (Current_Scope) then
12994                Error_Msg_N
12995                  ("declaration of full view must appear in private part", N);
12996             end if;
12997
12998             Copy_And_Swap (Prev, Id);
12999             Set_Has_Private_Declaration (Prev);
13000             Set_Has_Private_Declaration (Id);
13001
13002             --  If no error, propagate freeze_node from private to full view.
13003             --  It may have been generated for an early operational item.
13004
13005             if Present (Freeze_Node (Id))
13006               and then Serious_Errors_Detected = 0
13007               and then No (Full_View (Id))
13008             then
13009                Set_Freeze_Node (Prev, Freeze_Node (Id));
13010                Set_Freeze_Node (Id, Empty);
13011                Set_First_Rep_Item (Prev, First_Rep_Item (Id));
13012             end if;
13013
13014             Set_Full_View (Id, Prev);
13015             New_Id := Prev;
13016          end if;
13017
13018          --  Verify that full declaration conforms to incomplete one
13019
13020          if Is_Incomplete_Or_Private_Type (Prev)
13021            and then Present (Discriminant_Specifications (Prev_Par))
13022          then
13023             if Present (Discriminant_Specifications (N)) then
13024                if Ekind (Prev) = E_Incomplete_Type then
13025                   Check_Discriminant_Conformance (N, Prev, Prev);
13026                else
13027                   Check_Discriminant_Conformance (N, Prev, Id);
13028                end if;
13029
13030             else
13031                Error_Msg_N
13032                  ("missing discriminants in full type declaration", N);
13033
13034                --  To avoid cascaded errors on subsequent use, share the
13035                --  discriminants of the partial view.
13036
13037                Set_Discriminant_Specifications (N,
13038                  Discriminant_Specifications (Prev_Par));
13039             end if;
13040          end if;
13041
13042          --  A prior untagged private type can have an associated class-wide
13043          --  type due to use of the class attribute, and in this case also the
13044          --  full type is required to be tagged.
13045
13046          if Is_Type (Prev)
13047            and then (Is_Tagged_Type (Prev)
13048                       or else Present (Class_Wide_Type (Prev)))
13049          then
13050             --  The full declaration is either a tagged type (including
13051             --  a synchronized type that implements interfaces) or a
13052             --  type extension, otherwise this is an error.
13053
13054             if Nkind_In (N, N_Task_Type_Declaration,
13055                          N_Protected_Type_Declaration)
13056             then
13057                if No (Interface_List (N))
13058                  and then not Error_Posted (N)
13059                then
13060                   Error_Msg_NE
13061                     ("full declaration of } must be a tagged type ", Id, Prev);
13062                end if;
13063
13064             elsif Nkind (Type_Definition (N)) = N_Record_Definition then
13065
13066                --  Indicate that the previous declaration (tagged incomplete
13067                --  or private declaration) requires the same on the full one.
13068
13069                if not Tagged_Present (Type_Definition (N)) then
13070                   Error_Msg_NE
13071                     ("full declaration of } must be tagged", Prev, Id);
13072                   Set_Is_Tagged_Type (Id);
13073                   Set_Primitive_Operations (Id, New_Elmt_List);
13074                end if;
13075
13076             elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
13077                if No (Record_Extension_Part (Type_Definition (N))) then
13078                   Error_Msg_NE (
13079                     "full declaration of } must be a record extension",
13080                     Prev, Id);
13081                   Set_Is_Tagged_Type (Id);
13082                   Set_Primitive_Operations (Id, New_Elmt_List);
13083                end if;
13084
13085             else
13086                Error_Msg_NE
13087                  ("full declaration of } must be a tagged type", Prev, Id);
13088
13089             end if;
13090          end if;
13091
13092          return New_Id;
13093
13094       else
13095          --  New type declaration
13096
13097          Enter_Name (Id);
13098          return Id;
13099       end if;
13100    end Find_Type_Name;
13101
13102    -------------------------
13103    -- Find_Type_Of_Object --
13104    -------------------------
13105
13106    function Find_Type_Of_Object
13107      (Obj_Def     : Node_Id;
13108       Related_Nod : Node_Id) return Entity_Id
13109    is
13110       Def_Kind : constant Node_Kind := Nkind (Obj_Def);
13111       P        : Node_Id := Parent (Obj_Def);
13112       T        : Entity_Id;
13113       Nam      : Name_Id;
13114
13115    begin
13116       --  If the parent is a component_definition node we climb to the
13117       --  component_declaration node
13118
13119       if Nkind (P) = N_Component_Definition then
13120          P := Parent (P);
13121       end if;
13122
13123       --  Case of an anonymous array subtype
13124
13125       if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
13126                              N_Unconstrained_Array_Definition)
13127       then
13128          T := Empty;
13129          Array_Type_Declaration (T, Obj_Def);
13130
13131       --  Create an explicit subtype whenever possible
13132
13133       elsif Nkind (P) /= N_Component_Declaration
13134         and then Def_Kind = N_Subtype_Indication
13135       then
13136          --  Base name of subtype on object name, which will be unique in
13137          --  the current scope.
13138
13139          --  If this is a duplicate declaration, return base type, to avoid
13140          --  generating duplicate anonymous types.
13141
13142          if Error_Posted (P) then
13143             Analyze (Subtype_Mark (Obj_Def));
13144             return Entity (Subtype_Mark (Obj_Def));
13145          end if;
13146
13147          Nam :=
13148             New_External_Name
13149              (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
13150
13151          T := Make_Defining_Identifier (Sloc (P), Nam);
13152
13153          Insert_Action (Obj_Def,
13154            Make_Subtype_Declaration (Sloc (P),
13155              Defining_Identifier => T,
13156              Subtype_Indication  => Relocate_Node (Obj_Def)));
13157
13158          --  This subtype may need freezing, and this will not be done
13159          --  automatically if the object declaration is not in declarative
13160          --  part. Since this is an object declaration, the type cannot always
13161          --  be frozen here. Deferred constants do not freeze their type
13162          --  (which often enough will be private).
13163
13164          if Nkind (P) = N_Object_Declaration
13165            and then Constant_Present (P)
13166            and then No (Expression (P))
13167          then
13168             null;
13169          else
13170             Insert_Actions (Obj_Def, Freeze_Entity (T, Sloc (P)));
13171          end if;
13172
13173       --  Ada 2005 AI-406: the object definition in an object declaration
13174       --  can be an access definition.
13175
13176       elsif Def_Kind = N_Access_Definition then
13177          T := Access_Definition (Related_Nod, Obj_Def);
13178          Set_Is_Local_Anonymous_Access (T);
13179
13180       --  Otherwise, the object definition is just a subtype_mark
13181
13182       else
13183          T := Process_Subtype (Obj_Def, Related_Nod);
13184       end if;
13185
13186       return T;
13187    end Find_Type_Of_Object;
13188
13189    --------------------------------
13190    -- Find_Type_Of_Subtype_Indic --
13191    --------------------------------
13192
13193    function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
13194       Typ : Entity_Id;
13195
13196    begin
13197       --  Case of subtype mark with a constraint
13198
13199       if Nkind (S) = N_Subtype_Indication then
13200          Find_Type (Subtype_Mark (S));
13201          Typ := Entity (Subtype_Mark (S));
13202
13203          if not
13204            Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
13205          then
13206             Error_Msg_N
13207               ("incorrect constraint for this kind of type", Constraint (S));
13208             Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
13209          end if;
13210
13211       --  Otherwise we have a subtype mark without a constraint
13212
13213       elsif Error_Posted (S) then
13214          Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
13215          return Any_Type;
13216
13217       else
13218          Find_Type (S);
13219          Typ := Entity (S);
13220       end if;
13221
13222       --  Check No_Wide_Characters restriction
13223
13224       if Typ = Standard_Wide_Character
13225         or else Typ = Standard_Wide_Wide_Character
13226         or else Typ = Standard_Wide_String
13227         or else Typ = Standard_Wide_Wide_String
13228       then
13229          Check_Restriction (No_Wide_Characters, S);
13230       end if;
13231
13232       return Typ;
13233    end Find_Type_Of_Subtype_Indic;
13234
13235    -------------------------------------
13236    -- Floating_Point_Type_Declaration --
13237    -------------------------------------
13238
13239    procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
13240       Digs          : constant Node_Id := Digits_Expression (Def);
13241       Digs_Val      : Uint;
13242       Base_Typ      : Entity_Id;
13243       Implicit_Base : Entity_Id;
13244       Bound         : Node_Id;
13245
13246       function Can_Derive_From (E : Entity_Id) return Boolean;
13247       --  Find if given digits value allows derivation from specified type
13248
13249       ---------------------
13250       -- Can_Derive_From --
13251       ---------------------
13252
13253       function Can_Derive_From (E : Entity_Id) return Boolean is
13254          Spec : constant Entity_Id := Real_Range_Specification (Def);
13255
13256       begin
13257          if Digs_Val > Digits_Value (E) then
13258             return False;
13259          end if;
13260
13261          if Present (Spec) then
13262             if Expr_Value_R (Type_Low_Bound (E)) >
13263                Expr_Value_R (Low_Bound (Spec))
13264             then
13265                return False;
13266             end if;
13267
13268             if Expr_Value_R (Type_High_Bound (E)) <
13269                Expr_Value_R (High_Bound (Spec))
13270             then
13271                return False;
13272             end if;
13273          end if;
13274
13275          return True;
13276       end Can_Derive_From;
13277
13278    --  Start of processing for Floating_Point_Type_Declaration
13279
13280    begin
13281       Check_Restriction (No_Floating_Point, Def);
13282
13283       --  Create an implicit base type
13284
13285       Implicit_Base :=
13286         Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
13287
13288       --  Analyze and verify digits value
13289
13290       Analyze_And_Resolve (Digs, Any_Integer);
13291       Check_Digits_Expression (Digs);
13292       Digs_Val := Expr_Value (Digs);
13293
13294       --  Process possible range spec and find correct type to derive from
13295
13296       Process_Real_Range_Specification (Def);
13297
13298       if Can_Derive_From (Standard_Short_Float) then
13299          Base_Typ := Standard_Short_Float;
13300       elsif Can_Derive_From (Standard_Float) then
13301          Base_Typ := Standard_Float;
13302       elsif Can_Derive_From (Standard_Long_Float) then
13303          Base_Typ := Standard_Long_Float;
13304       elsif Can_Derive_From (Standard_Long_Long_Float) then
13305          Base_Typ := Standard_Long_Long_Float;
13306
13307       --  If we can't derive from any existing type, use long_long_float
13308       --  and give appropriate message explaining the problem.
13309
13310       else
13311          Base_Typ := Standard_Long_Long_Float;
13312
13313          if Digs_Val >= Digits_Value (Standard_Long_Long_Float) then
13314             Error_Msg_Uint_1 := Digits_Value (Standard_Long_Long_Float);
13315             Error_Msg_N ("digits value out of range, maximum is ^", Digs);
13316
13317          else
13318             Error_Msg_N
13319               ("range too large for any predefined type",
13320                Real_Range_Specification (Def));
13321          end if;
13322       end if;
13323
13324       --  If there are bounds given in the declaration use them as the bounds
13325       --  of the type, otherwise use the bounds of the predefined base type
13326       --  that was chosen based on the Digits value.
13327
13328       if Present (Real_Range_Specification (Def)) then
13329          Set_Scalar_Range (T, Real_Range_Specification (Def));
13330          Set_Is_Constrained (T);
13331
13332          --  The bounds of this range must be converted to machine numbers
13333          --  in accordance with RM 4.9(38).
13334
13335          Bound := Type_Low_Bound (T);
13336
13337          if Nkind (Bound) = N_Real_Literal then
13338             Set_Realval
13339               (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
13340             Set_Is_Machine_Number (Bound);
13341          end if;
13342
13343          Bound := Type_High_Bound (T);
13344
13345          if Nkind (Bound) = N_Real_Literal then
13346             Set_Realval
13347               (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
13348             Set_Is_Machine_Number (Bound);
13349          end if;
13350
13351       else
13352          Set_Scalar_Range (T, Scalar_Range (Base_Typ));
13353       end if;
13354
13355       --  Complete definition of implicit base and declared first subtype
13356
13357       Set_Etype          (Implicit_Base, Base_Typ);
13358
13359       Set_Scalar_Range   (Implicit_Base, Scalar_Range   (Base_Typ));
13360       Set_Size_Info      (Implicit_Base,                (Base_Typ));
13361       Set_RM_Size        (Implicit_Base, RM_Size        (Base_Typ));
13362       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
13363       Set_Digits_Value   (Implicit_Base, Digits_Value   (Base_Typ));
13364       Set_Vax_Float      (Implicit_Base, Vax_Float      (Base_Typ));
13365
13366       Set_Ekind          (T, E_Floating_Point_Subtype);
13367       Set_Etype          (T, Implicit_Base);
13368
13369       Set_Size_Info      (T,                (Implicit_Base));
13370       Set_RM_Size        (T, RM_Size        (Implicit_Base));
13371       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
13372       Set_Digits_Value   (T, Digs_Val);
13373    end Floating_Point_Type_Declaration;
13374
13375    ----------------------------
13376    -- Get_Discriminant_Value --
13377    ----------------------------
13378
13379    --  This is the situation:
13380
13381    --  There is a non-derived type
13382
13383    --       type T0 (Dx, Dy, Dz...)
13384
13385    --  There are zero or more levels of derivation, with each derivation
13386    --  either purely inheriting the discriminants, or defining its own.
13387
13388    --       type Ti      is new Ti-1
13389    --  or
13390    --       type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
13391    --  or
13392    --       subtype Ti is ...
13393
13394    --  The subtype issue is avoided by the use of Original_Record_Component,
13395    --  and the fact that derived subtypes also derive the constraints.
13396
13397    --  This chain leads back from
13398
13399    --       Typ_For_Constraint
13400
13401    --  Typ_For_Constraint has discriminants, and the value for each
13402    --  discriminant is given by its corresponding Elmt of Constraints.
13403
13404    --  Discriminant is some discriminant in this hierarchy
13405
13406    --  We need to return its value
13407
13408    --  We do this by recursively searching each level, and looking for
13409    --  Discriminant. Once we get to the bottom, we start backing up
13410    --  returning the value for it which may in turn be a discriminant
13411    --  further up, so on the backup we continue the substitution.
13412
13413    function Get_Discriminant_Value
13414      (Discriminant       : Entity_Id;
13415       Typ_For_Constraint : Entity_Id;
13416       Constraint         : Elist_Id) return Node_Id
13417    is
13418       function Search_Derivation_Levels
13419         (Ti                    : Entity_Id;
13420          Discrim_Values        : Elist_Id;
13421          Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
13422       --  This is the routine that performs the recursive search of levels
13423       --  as described above.
13424
13425       ------------------------------
13426       -- Search_Derivation_Levels --
13427       ------------------------------
13428
13429       function Search_Derivation_Levels
13430         (Ti                    : Entity_Id;
13431          Discrim_Values        : Elist_Id;
13432          Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
13433       is
13434          Assoc          : Elmt_Id;
13435          Disc           : Entity_Id;
13436          Result         : Node_Or_Entity_Id;
13437          Result_Entity  : Node_Id;
13438
13439       begin
13440          --  If inappropriate type, return Error, this happens only in
13441          --  cascaded error situations, and we want to avoid a blow up.
13442
13443          if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
13444             return Error;
13445          end if;
13446
13447          --  Look deeper if possible. Use Stored_Constraints only for
13448          --  untagged types. For tagged types use the given constraint.
13449          --  This asymmetry needs explanation???
13450
13451          if not Stored_Discrim_Values
13452            and then Present (Stored_Constraint (Ti))
13453            and then not Is_Tagged_Type (Ti)
13454          then
13455             Result :=
13456               Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
13457          else
13458             declare
13459                Td : constant Entity_Id := Etype (Ti);
13460
13461             begin
13462                if Td = Ti then
13463                   Result := Discriminant;
13464
13465                else
13466                   if Present (Stored_Constraint (Ti)) then
13467                      Result :=
13468                         Search_Derivation_Levels
13469                           (Td, Stored_Constraint (Ti), True);
13470                   else
13471                      Result :=
13472                         Search_Derivation_Levels
13473                           (Td, Discrim_Values, Stored_Discrim_Values);
13474                   end if;
13475                end if;
13476             end;
13477          end if;
13478
13479          --  Extra underlying places to search, if not found above. For
13480          --  concurrent types, the relevant discriminant appears in the
13481          --  corresponding record. For a type derived from a private type
13482          --  without discriminant, the full view inherits the discriminants
13483          --  of the full view of the parent.
13484
13485          if Result = Discriminant then
13486             if Is_Concurrent_Type (Ti)
13487               and then Present (Corresponding_Record_Type (Ti))
13488             then
13489                Result :=
13490                  Search_Derivation_Levels (
13491                    Corresponding_Record_Type (Ti),
13492                    Discrim_Values,
13493                    Stored_Discrim_Values);
13494
13495             elsif Is_Private_Type (Ti)
13496               and then not Has_Discriminants (Ti)
13497               and then Present (Full_View (Ti))
13498               and then Etype (Full_View (Ti)) /= Ti
13499             then
13500                Result :=
13501                  Search_Derivation_Levels (
13502                    Full_View (Ti),
13503                    Discrim_Values,
13504                    Stored_Discrim_Values);
13505             end if;
13506          end if;
13507
13508          --  If Result is not a (reference to a) discriminant, return it,
13509          --  otherwise set Result_Entity to the discriminant.
13510
13511          if Nkind (Result) = N_Defining_Identifier then
13512             pragma Assert (Result = Discriminant);
13513             Result_Entity := Result;
13514
13515          else
13516             if not Denotes_Discriminant (Result) then
13517                return Result;
13518             end if;
13519
13520             Result_Entity := Entity (Result);
13521          end if;
13522
13523          --  See if this level of derivation actually has discriminants
13524          --  because tagged derivations can add them, hence the lower
13525          --  levels need not have any.
13526
13527          if not Has_Discriminants (Ti) then
13528             return Result;
13529          end if;
13530
13531          --  Scan Ti's discriminants for Result_Entity,
13532          --  and return its corresponding value, if any.
13533
13534          Result_Entity := Original_Record_Component (Result_Entity);
13535
13536          Assoc := First_Elmt (Discrim_Values);
13537
13538          if Stored_Discrim_Values then
13539             Disc := First_Stored_Discriminant (Ti);
13540          else
13541             Disc := First_Discriminant (Ti);
13542          end if;
13543
13544          while Present (Disc) loop
13545             pragma Assert (Present (Assoc));
13546
13547             if Original_Record_Component (Disc) = Result_Entity then
13548                return Node (Assoc);
13549             end if;
13550
13551             Next_Elmt (Assoc);
13552
13553             if Stored_Discrim_Values then
13554                Next_Stored_Discriminant (Disc);
13555             else
13556                Next_Discriminant (Disc);
13557             end if;
13558          end loop;
13559
13560          --  Could not find it
13561          --
13562          return Result;
13563       end Search_Derivation_Levels;
13564
13565       --  Local Variables
13566
13567       Result : Node_Or_Entity_Id;
13568
13569    --  Start of processing for Get_Discriminant_Value
13570
13571    begin
13572       --  ??? This routine is a gigantic mess and will be deleted. For the
13573       --  time being just test for the trivial case before calling recurse.
13574
13575       if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
13576          declare
13577             D : Entity_Id;
13578             E : Elmt_Id;
13579
13580          begin
13581             D := First_Discriminant (Typ_For_Constraint);
13582             E := First_Elmt (Constraint);
13583             while Present (D) loop
13584                if Chars (D) = Chars (Discriminant) then
13585                   return Node (E);
13586                end if;
13587
13588                Next_Discriminant (D);
13589                Next_Elmt (E);
13590             end loop;
13591          end;
13592       end if;
13593
13594       Result := Search_Derivation_Levels
13595         (Typ_For_Constraint, Constraint, False);
13596
13597       --  ??? hack to disappear when this routine is gone
13598
13599       if  Nkind (Result) = N_Defining_Identifier then
13600          declare
13601             D : Entity_Id;
13602             E : Elmt_Id;
13603
13604          begin
13605             D := First_Discriminant (Typ_For_Constraint);
13606             E := First_Elmt (Constraint);
13607             while Present (D) loop
13608                if Corresponding_Discriminant (D) = Discriminant then
13609                   return Node (E);
13610                end if;
13611
13612                Next_Discriminant (D);
13613                Next_Elmt (E);
13614             end loop;
13615          end;
13616       end if;
13617
13618       pragma Assert (Nkind (Result) /= N_Defining_Identifier);
13619       return Result;
13620    end Get_Discriminant_Value;
13621
13622    --------------------------
13623    -- Has_Range_Constraint --
13624    --------------------------
13625
13626    function Has_Range_Constraint (N : Node_Id) return Boolean is
13627       C : constant Node_Id := Constraint (N);
13628
13629    begin
13630       if Nkind (C) = N_Range_Constraint then
13631          return True;
13632
13633       elsif Nkind (C) = N_Digits_Constraint then
13634          return
13635             Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
13636               or else
13637             Present (Range_Constraint (C));
13638
13639       elsif Nkind (C) = N_Delta_Constraint then
13640          return Present (Range_Constraint (C));
13641
13642       else
13643          return False;
13644       end if;
13645    end Has_Range_Constraint;
13646
13647    ------------------------
13648    -- Inherit_Components --
13649    ------------------------
13650
13651    function Inherit_Components
13652      (N             : Node_Id;
13653       Parent_Base   : Entity_Id;
13654       Derived_Base  : Entity_Id;
13655       Is_Tagged     : Boolean;
13656       Inherit_Discr : Boolean;
13657       Discs         : Elist_Id) return Elist_Id
13658    is
13659       Assoc_List : constant Elist_Id := New_Elmt_List;
13660
13661       procedure Inherit_Component
13662         (Old_C          : Entity_Id;
13663          Plain_Discrim  : Boolean := False;
13664          Stored_Discrim : Boolean := False);
13665       --  Inherits component Old_C from Parent_Base to the Derived_Base. If
13666       --  Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
13667       --  True, Old_C is a stored discriminant. If they are both false then
13668       --  Old_C is a regular component.
13669
13670       -----------------------
13671       -- Inherit_Component --
13672       -----------------------
13673
13674       procedure Inherit_Component
13675         (Old_C          : Entity_Id;
13676          Plain_Discrim  : Boolean := False;
13677          Stored_Discrim : Boolean := False)
13678       is
13679          New_C : constant Entity_Id := New_Copy (Old_C);
13680
13681          Discrim      : Entity_Id;
13682          Corr_Discrim : Entity_Id;
13683
13684       begin
13685          pragma Assert (not Is_Tagged or else not Stored_Discrim);
13686
13687          Set_Parent (New_C, Parent (Old_C));
13688
13689          --  Regular discriminants and components must be inserted in the scope
13690          --  of the Derived_Base. Do it here.
13691
13692          if not Stored_Discrim then
13693             Enter_Name (New_C);
13694          end if;
13695
13696          --  For tagged types the Original_Record_Component must point to
13697          --  whatever this field was pointing to in the parent type. This has
13698          --  already been achieved by the call to New_Copy above.
13699
13700          if not Is_Tagged then
13701             Set_Original_Record_Component (New_C, New_C);
13702          end if;
13703
13704          --  If we have inherited a component then see if its Etype contains
13705          --  references to Parent_Base discriminants. In this case, replace
13706          --  these references with the constraints given in Discs. We do not
13707          --  do this for the partial view of private types because this is
13708          --  not needed (only the components of the full view will be used
13709          --  for code generation) and cause problem. We also avoid this
13710          --  transformation in some error situations.
13711
13712          if Ekind (New_C) = E_Component then
13713             if (Is_Private_Type (Derived_Base)
13714                  and then not Is_Generic_Type (Derived_Base))
13715               or else (Is_Empty_Elmt_List (Discs)
13716                         and then  not Expander_Active)
13717             then
13718                Set_Etype (New_C, Etype (Old_C));
13719
13720             else
13721                --  The current component introduces a circularity of the
13722                --  following kind:
13723
13724                --     limited with Pack_2;
13725                --     package Pack_1 is
13726                --        type T_1 is tagged record
13727                --           Comp : access Pack_2.T_2;
13728                --           ...
13729                --        end record;
13730                --     end Pack_1;
13731
13732                --     with Pack_1;
13733                --     package Pack_2 is
13734                --        type T_2 is new Pack_1.T_1 with ...;
13735                --     end Pack_2;
13736
13737                Set_Etype
13738                  (New_C,
13739                   Constrain_Component_Type
13740                   (Old_C, Derived_Base, N, Parent_Base, Discs));
13741             end if;
13742          end if;
13743
13744          --  In derived tagged types it is illegal to reference a non
13745          --  discriminant component in the parent type. To catch this, mark
13746          --  these components with an Ekind of E_Void. This will be reset in
13747          --  Record_Type_Definition after processing the record extension of
13748          --  the derived type.
13749
13750          --  If the declaration is a private extension, there is no further
13751          --  record extension to process, and the components retain their
13752          --  current kind, because they are visible at this point.
13753
13754          if Is_Tagged and then Ekind (New_C) = E_Component
13755            and then Nkind (N) /= N_Private_Extension_Declaration
13756          then
13757             Set_Ekind (New_C, E_Void);
13758          end if;
13759
13760          if Plain_Discrim then
13761             Set_Corresponding_Discriminant (New_C, Old_C);
13762             Build_Discriminal (New_C);
13763
13764          --  If we are explicitly inheriting a stored discriminant it will be
13765          --  completely hidden.
13766
13767          elsif Stored_Discrim then
13768             Set_Corresponding_Discriminant (New_C, Empty);
13769             Set_Discriminal (New_C, Empty);
13770             Set_Is_Completely_Hidden (New_C);
13771
13772             --  Set the Original_Record_Component of each discriminant in the
13773             --  derived base to point to the corresponding stored that we just
13774             --  created.
13775
13776             Discrim := First_Discriminant (Derived_Base);
13777             while Present (Discrim) loop
13778                Corr_Discrim := Corresponding_Discriminant (Discrim);
13779
13780                --  Corr_Discrim could be missing in an error situation
13781
13782                if Present (Corr_Discrim)
13783                  and then Original_Record_Component (Corr_Discrim) = Old_C
13784                then
13785                   Set_Original_Record_Component (Discrim, New_C);
13786                end if;
13787
13788                Next_Discriminant (Discrim);
13789             end loop;
13790
13791             Append_Entity (New_C, Derived_Base);
13792          end if;
13793
13794          if not Is_Tagged then
13795             Append_Elmt (Old_C, Assoc_List);
13796             Append_Elmt (New_C, Assoc_List);
13797          end if;
13798       end Inherit_Component;
13799
13800       --  Variables local to Inherit_Component
13801
13802       Loc : constant Source_Ptr := Sloc (N);
13803
13804       Parent_Discrim : Entity_Id;
13805       Stored_Discrim : Entity_Id;
13806       D              : Entity_Id;
13807       Component      : Entity_Id;
13808
13809    --  Start of processing for Inherit_Components
13810
13811    begin
13812       if not Is_Tagged then
13813          Append_Elmt (Parent_Base,  Assoc_List);
13814          Append_Elmt (Derived_Base, Assoc_List);
13815       end if;
13816
13817       --  Inherit parent discriminants if needed
13818
13819       if Inherit_Discr then
13820          Parent_Discrim := First_Discriminant (Parent_Base);
13821          while Present (Parent_Discrim) loop
13822             Inherit_Component (Parent_Discrim, Plain_Discrim => True);
13823             Next_Discriminant (Parent_Discrim);
13824          end loop;
13825       end if;
13826
13827       --  Create explicit stored discrims for untagged types when necessary
13828
13829       if not Has_Unknown_Discriminants (Derived_Base)
13830         and then Has_Discriminants (Parent_Base)
13831         and then not Is_Tagged
13832         and then
13833           (not Inherit_Discr
13834              or else First_Discriminant (Parent_Base) /=
13835                      First_Stored_Discriminant (Parent_Base))
13836       then
13837          Stored_Discrim := First_Stored_Discriminant (Parent_Base);
13838          while Present (Stored_Discrim) loop
13839             Inherit_Component (Stored_Discrim, Stored_Discrim => True);
13840             Next_Stored_Discriminant (Stored_Discrim);
13841          end loop;
13842       end if;
13843
13844       --  See if we can apply the second transformation for derived types, as
13845       --  explained in point 6. in the comments above Build_Derived_Record_Type
13846       --  This is achieved by appending Derived_Base discriminants into Discs,
13847       --  which has the side effect of returning a non empty Discs list to the
13848       --  caller of Inherit_Components, which is what we want. This must be
13849       --  done for private derived types if there are explicit stored
13850       --  discriminants, to ensure that we can retrieve the values of the
13851       --  constraints provided in the ancestors.
13852
13853       if Inherit_Discr
13854         and then Is_Empty_Elmt_List (Discs)
13855         and then Present (First_Discriminant (Derived_Base))
13856         and then
13857           (not Is_Private_Type (Derived_Base)
13858              or else Is_Completely_Hidden
13859                (First_Stored_Discriminant (Derived_Base))
13860              or else Is_Generic_Type (Derived_Base))
13861       then
13862          D := First_Discriminant (Derived_Base);
13863          while Present (D) loop
13864             Append_Elmt (New_Reference_To (D, Loc), Discs);
13865             Next_Discriminant (D);
13866          end loop;
13867       end if;
13868
13869       --  Finally, inherit non-discriminant components unless they are not
13870       --  visible because defined or inherited from the full view of the
13871       --  parent. Don't inherit the _parent field of the parent type.
13872
13873       Component := First_Entity (Parent_Base);
13874       while Present (Component) loop
13875
13876          --  Ada 2005 (AI-251): Do not inherit components associated with
13877          --  secondary tags of the parent.
13878
13879          if Ekind (Component) = E_Component
13880            and then Present (Related_Type (Component))
13881          then
13882             null;
13883
13884          elsif Ekind (Component) /= E_Component
13885            or else Chars (Component) = Name_uParent
13886          then
13887             null;
13888
13889          --  If the derived type is within the parent type's declarative
13890          --  region, then the components can still be inherited even though
13891          --  they aren't visible at this point. This can occur for cases
13892          --  such as within public child units where the components must
13893          --  become visible upon entering the child unit's private part.
13894
13895          elsif not Is_Visible_Component (Component)
13896            and then not In_Open_Scopes (Scope (Parent_Base))
13897          then
13898             null;
13899
13900          elsif Ekind (Derived_Base) = E_Private_Type
13901            or else Ekind (Derived_Base) = E_Limited_Private_Type
13902          then
13903             null;
13904
13905          else
13906             Inherit_Component (Component);
13907          end if;
13908
13909          Next_Entity (Component);
13910       end loop;
13911
13912       --  For tagged derived types, inherited discriminants cannot be used in
13913       --  component declarations of the record extension part. To achieve this
13914       --  we mark the inherited discriminants as not visible.
13915
13916       if Is_Tagged and then Inherit_Discr then
13917          D := First_Discriminant (Derived_Base);
13918          while Present (D) loop
13919             Set_Is_Immediately_Visible (D, False);
13920             Next_Discriminant (D);
13921          end loop;
13922       end if;
13923
13924       return Assoc_List;
13925    end Inherit_Components;
13926
13927    -----------------------
13928    -- Is_Null_Extension --
13929    -----------------------
13930
13931    function Is_Null_Extension (T : Entity_Id) return Boolean is
13932       Type_Decl : constant Node_Id := Parent (T);
13933       Comp_List : Node_Id;
13934       Comp      : Node_Id;
13935
13936    begin
13937       if Nkind (Type_Decl) /= N_Full_Type_Declaration
13938         or else not Is_Tagged_Type (T)
13939         or else Nkind (Type_Definition (Type_Decl)) /=
13940                                               N_Derived_Type_Definition
13941         or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
13942       then
13943          return False;
13944       end if;
13945
13946       Comp_List :=
13947         Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
13948
13949       if Present (Discriminant_Specifications (Type_Decl)) then
13950          return False;
13951
13952       elsif Present (Comp_List)
13953         and then Is_Non_Empty_List (Component_Items (Comp_List))
13954       then
13955          Comp := First (Component_Items (Comp_List));
13956
13957          --  Only user-defined components are relevant. The component list
13958          --  may also contain a parent component and internal components
13959          --  corresponding to secondary tags, but these do not determine
13960          --  whether this is a null extension.
13961
13962          while Present (Comp) loop
13963             if Comes_From_Source (Comp) then
13964                return False;
13965             end if;
13966
13967             Next (Comp);
13968          end loop;
13969
13970          return True;
13971       else
13972          return True;
13973       end if;
13974    end Is_Null_Extension;
13975
13976    --------------------
13977    --  Is_Progenitor --
13978    --------------------
13979
13980    function Is_Progenitor
13981      (Iface : Entity_Id;
13982       Typ   : Entity_Id) return Boolean
13983    is
13984       Iface_Elmt  : Elmt_Id;
13985       I_Name      : Entity_Id;
13986
13987    begin
13988       if No (Abstract_Interfaces (Typ)) then
13989          return False;
13990
13991       else
13992          Iface_Elmt := First_Elmt (Abstract_Interfaces (Typ));
13993          while Present (Iface_Elmt) loop
13994             I_Name := Node (Iface_Elmt);
13995             if Base_Type (I_Name) = Base_Type (Iface) then
13996                return True;
13997
13998             elsif Is_Derived_Type (I_Name)
13999               and then Is_Ancestor (Iface, I_Name)
14000             then
14001                return True;
14002
14003             else
14004                Next_Elmt (Iface_Elmt);
14005             end if;
14006          end loop;
14007
14008          --  For concurrent record types, they have the interfaces of the
14009          --  parent synchronized type. However these have no ancestors that
14010          --  implement anything, so assume it is a progenitor.
14011          --  Should be cleaned up in Collect_Abstract_Interfaces???
14012
14013          if Is_Concurrent_Record_Type (Typ) then
14014             return Present (Abstract_Interfaces (Typ));
14015          end if;
14016
14017          --  If type is a derived type, check recursively its ancestors
14018
14019          if Is_Derived_Type (Typ) then
14020             return Etype (Typ) = Iface
14021               or else  Is_Progenitor (Iface, Etype (Typ));
14022          else
14023             return False;
14024          end if;
14025       end if;
14026    end Is_Progenitor;
14027
14028    ------------------------------
14029    -- Is_Valid_Constraint_Kind --
14030    ------------------------------
14031
14032    function Is_Valid_Constraint_Kind
14033      (T_Kind          : Type_Kind;
14034       Constraint_Kind : Node_Kind) return Boolean
14035    is
14036    begin
14037       case T_Kind is
14038          when Enumeration_Kind |
14039               Integer_Kind =>
14040             return Constraint_Kind = N_Range_Constraint;
14041
14042          when Decimal_Fixed_Point_Kind =>
14043             return Nkind_In (Constraint_Kind, N_Digits_Constraint,
14044                                               N_Range_Constraint);
14045
14046          when Ordinary_Fixed_Point_Kind =>
14047             return Nkind_In (Constraint_Kind, N_Delta_Constraint,
14048                                               N_Range_Constraint);
14049
14050          when Float_Kind =>
14051             return Nkind_In (Constraint_Kind, N_Digits_Constraint,
14052                                               N_Range_Constraint);
14053
14054          when Access_Kind       |
14055               Array_Kind        |
14056               E_Record_Type     |
14057               E_Record_Subtype  |
14058               Class_Wide_Kind   |
14059               E_Incomplete_Type |
14060               Private_Kind      |
14061               Concurrent_Kind  =>
14062             return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
14063
14064          when others =>
14065             return True; -- Error will be detected later
14066       end case;
14067    end Is_Valid_Constraint_Kind;
14068
14069    --------------------------
14070    -- Is_Visible_Component --
14071    --------------------------
14072
14073    function Is_Visible_Component (C : Entity_Id) return Boolean is
14074       Original_Comp  : Entity_Id := Empty;
14075       Original_Scope : Entity_Id;
14076       Type_Scope     : Entity_Id;
14077
14078       function Is_Local_Type (Typ : Entity_Id) return Boolean;
14079       --  Check whether parent type of inherited component is declared locally,
14080       --  possibly within a nested package or instance. The current scope is
14081       --  the derived record itself.
14082
14083       -------------------
14084       -- Is_Local_Type --
14085       -------------------
14086
14087       function Is_Local_Type (Typ : Entity_Id) return Boolean is
14088          Scop : Entity_Id;
14089
14090       begin
14091          Scop := Scope (Typ);
14092          while Present (Scop)
14093            and then Scop /= Standard_Standard
14094          loop
14095             if Scop = Scope (Current_Scope) then
14096                return True;
14097             end if;
14098
14099             Scop := Scope (Scop);
14100          end loop;
14101
14102          return False;
14103       end Is_Local_Type;
14104
14105    --  Start of processing for Is_Visible_Component
14106
14107    begin
14108       if Ekind (C) = E_Component
14109         or else Ekind (C) = E_Discriminant
14110       then
14111          Original_Comp := Original_Record_Component (C);
14112       end if;
14113
14114       if No (Original_Comp) then
14115
14116          --  Premature usage, or previous error
14117
14118          return False;
14119
14120       else
14121          Original_Scope := Scope (Original_Comp);
14122          Type_Scope     := Scope (Base_Type (Scope (C)));
14123       end if;
14124
14125       --  This test only concerns tagged types
14126
14127       if not Is_Tagged_Type (Original_Scope) then
14128          return True;
14129
14130       --  If it is _Parent or _Tag, there is no visibility issue
14131
14132       elsif not Comes_From_Source (Original_Comp) then
14133          return True;
14134
14135       --  If we are in the body of an instantiation, the component is visible
14136       --  even when the parent type (possibly defined in an enclosing unit or
14137       --  in a parent unit) might not.
14138
14139       elsif In_Instance_Body then
14140          return True;
14141
14142       --  Discriminants are always visible
14143
14144       elsif Ekind (Original_Comp) = E_Discriminant
14145         and then not Has_Unknown_Discriminants (Original_Scope)
14146       then
14147          return True;
14148
14149       --  If the component has been declared in an ancestor which is currently
14150       --  a private type, then it is not visible. The same applies if the
14151       --  component's containing type is not in an open scope and the original
14152       --  component's enclosing type is a visible full view of a private type
14153       --  (which can occur in cases where an attempt is being made to reference
14154       --  a component in a sibling package that is inherited from a visible
14155       --  component of a type in an ancestor package; the component in the
14156       --  sibling package should not be visible even though the component it
14157       --  inherited from is visible). This does not apply however in the case
14158       --  where the scope of the type is a private child unit, or when the
14159       --  parent comes from a local package in which the ancestor is currently
14160       --  visible. The latter suppression of visibility is needed for cases
14161       --  that are tested in B730006.
14162
14163       elsif Is_Private_Type (Original_Scope)
14164         or else
14165           (not Is_Private_Descendant (Type_Scope)
14166             and then not In_Open_Scopes (Type_Scope)
14167             and then Has_Private_Declaration (Original_Scope))
14168       then
14169          --  If the type derives from an entity in a formal package, there
14170          --  are no additional visible components.
14171
14172          if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
14173             N_Formal_Package_Declaration
14174          then
14175             return False;
14176
14177          --  if we are not in the private part of the current package, there
14178          --  are no additional visible components.
14179
14180          elsif Ekind (Scope (Current_Scope)) = E_Package
14181            and then not In_Private_Part (Scope (Current_Scope))
14182          then
14183             return False;
14184          else
14185             return
14186               Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
14187                 and then In_Open_Scopes (Scope (Original_Scope))
14188                 and then Is_Local_Type (Type_Scope);
14189          end if;
14190
14191       --  There is another weird way in which a component may be invisible
14192       --  when the private and the full view are not derived from the same
14193       --  ancestor. Here is an example :
14194
14195       --       type A1 is tagged      record F1 : integer; end record;
14196       --       type A2 is new A1 with record F2 : integer; end record;
14197       --       type T is new A1 with private;
14198       --     private
14199       --       type T is new A2 with null record;
14200
14201       --  In this case, the full view of T inherits F1 and F2 but the private
14202       --  view inherits only F1
14203
14204       else
14205          declare
14206             Ancestor : Entity_Id := Scope (C);
14207
14208          begin
14209             loop
14210                if Ancestor = Original_Scope then
14211                   return True;
14212                elsif Ancestor = Etype (Ancestor) then
14213                   return False;
14214                end if;
14215
14216                Ancestor := Etype (Ancestor);
14217             end loop;
14218          end;
14219       end if;
14220    end Is_Visible_Component;
14221
14222    --------------------------
14223    -- Make_Class_Wide_Type --
14224    --------------------------
14225
14226    procedure Make_Class_Wide_Type (T : Entity_Id) is
14227       CW_Type : Entity_Id;
14228       CW_Name : Name_Id;
14229       Next_E  : Entity_Id;
14230
14231    begin
14232       --  The class wide type can have been defined by the partial view, in
14233       --  which case everything is already done.
14234
14235       if Present (Class_Wide_Type (T)) then
14236          return;
14237       end if;
14238
14239       CW_Type :=
14240         New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
14241
14242       --  Inherit root type characteristics
14243
14244       CW_Name := Chars (CW_Type);
14245       Next_E  := Next_Entity (CW_Type);
14246       Copy_Node (T, CW_Type);
14247       Set_Comes_From_Source (CW_Type, False);
14248       Set_Chars (CW_Type, CW_Name);
14249       Set_Parent (CW_Type, Parent (T));
14250       Set_Next_Entity (CW_Type, Next_E);
14251
14252       --  Ensure we have a new freeze node for the class-wide type. The partial
14253       --  view may have freeze action of its own, requiring a proper freeze
14254       --  node, and the same freeze node cannot be shared between the two
14255       --  types.
14256
14257       Set_Has_Delayed_Freeze (CW_Type);
14258       Set_Freeze_Node (CW_Type, Empty);
14259
14260       --  Customize the class-wide type: It has no prim. op., it cannot be
14261       --  abstract and its Etype points back to the specific root type.
14262
14263       Set_Ekind                (CW_Type, E_Class_Wide_Type);
14264       Set_Is_Tagged_Type       (CW_Type, True);
14265       Set_Primitive_Operations (CW_Type, New_Elmt_List);
14266       Set_Is_Abstract_Type     (CW_Type, False);
14267       Set_Is_Constrained       (CW_Type, False);
14268       Set_Is_First_Subtype     (CW_Type, Is_First_Subtype (T));
14269
14270       if Ekind (T) = E_Class_Wide_Subtype then
14271          Set_Etype             (CW_Type, Etype (Base_Type (T)));
14272       else
14273          Set_Etype             (CW_Type, T);
14274       end if;
14275
14276       --  If this is the class_wide type of a constrained subtype, it does
14277       --  not have discriminants.
14278
14279       Set_Has_Discriminants (CW_Type,
14280         Has_Discriminants (T) and then not Is_Constrained (T));
14281
14282       Set_Has_Unknown_Discriminants (CW_Type, True);
14283       Set_Class_Wide_Type (T, CW_Type);
14284       Set_Equivalent_Type (CW_Type, Empty);
14285
14286       --  The class-wide type of a class-wide type is itself (RM 3.9(14))
14287
14288       Set_Class_Wide_Type (CW_Type, CW_Type);
14289    end Make_Class_Wide_Type;
14290
14291    ----------------
14292    -- Make_Index --
14293    ----------------
14294
14295    procedure Make_Index
14296      (I            : Node_Id;
14297       Related_Nod  : Node_Id;
14298       Related_Id   : Entity_Id := Empty;
14299       Suffix_Index : Nat := 1)
14300    is
14301       R      : Node_Id;
14302       T      : Entity_Id;
14303       Def_Id : Entity_Id := Empty;
14304       Found  : Boolean := False;
14305
14306    begin
14307       --  For a discrete range used in a constrained array definition and
14308       --  defined by a range, an implicit conversion to the predefined type
14309       --  INTEGER is assumed if each bound is either a numeric literal, a named
14310       --  number, or an attribute, and the type of both bounds (prior to the
14311       --  implicit conversion) is the type universal_integer. Otherwise, both
14312       --  bounds must be of the same discrete type, other than universal
14313       --  integer; this type must be determinable independently of the
14314       --  context, but using the fact that the type must be discrete and that
14315       --  both bounds must have the same type.
14316
14317       --  Character literals also have a universal type in the absence of
14318       --  of additional context,  and are resolved to Standard_Character.
14319
14320       if Nkind (I) = N_Range then
14321
14322          --  The index is given by a range constraint. The bounds are known
14323          --  to be of a consistent type.
14324
14325          if not Is_Overloaded (I) then
14326             T := Etype (I);
14327
14328             --  For universal bounds, choose the specific predefined type
14329
14330             if T = Universal_Integer then
14331                T := Standard_Integer;
14332
14333             elsif T = Any_Character then
14334                Ambiguous_Character (Low_Bound (I));
14335
14336                T := Standard_Character;
14337             end if;
14338
14339          --  The node may be overloaded because some user-defined operators
14340          --  are available, but if a universal interpretation exists it is
14341          --  also the selected one.
14342
14343          elsif Universal_Interpretation (I) = Universal_Integer then
14344             T := Standard_Integer;
14345
14346          else
14347             T := Any_Type;
14348
14349             declare
14350                Ind : Interp_Index;
14351                It  : Interp;
14352
14353             begin
14354                Get_First_Interp (I, Ind, It);
14355                while Present (It.Typ) loop
14356                   if Is_Discrete_Type (It.Typ) then
14357
14358                      if Found
14359                        and then not Covers (It.Typ, T)
14360                        and then not Covers (T, It.Typ)
14361                      then
14362                         Error_Msg_N ("ambiguous bounds in discrete range", I);
14363                         exit;
14364                      else
14365                         T := It.Typ;
14366                         Found := True;
14367                      end if;
14368                   end if;
14369
14370                   Get_Next_Interp (Ind, It);
14371                end loop;
14372
14373                if T = Any_Type then
14374                   Error_Msg_N ("discrete type required for range", I);
14375                   Set_Etype (I, Any_Type);
14376                   return;
14377
14378                elsif T = Universal_Integer then
14379                   T := Standard_Integer;
14380                end if;
14381             end;
14382          end if;
14383
14384          if not Is_Discrete_Type (T) then
14385             Error_Msg_N ("discrete type required for range", I);
14386             Set_Etype (I, Any_Type);
14387             return;
14388          end if;
14389
14390          if Nkind (Low_Bound (I)) = N_Attribute_Reference
14391            and then Attribute_Name (Low_Bound (I)) = Name_First
14392            and then Is_Entity_Name (Prefix (Low_Bound (I)))
14393            and then Is_Type (Entity (Prefix (Low_Bound (I))))
14394            and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
14395          then
14396             --  The type of the index will be the type of the prefix, as long
14397             --  as the upper bound is 'Last of the same type.
14398
14399             Def_Id := Entity (Prefix (Low_Bound (I)));
14400
14401             if Nkind (High_Bound (I)) /= N_Attribute_Reference
14402               or else Attribute_Name (High_Bound (I)) /= Name_Last
14403               or else not Is_Entity_Name (Prefix (High_Bound (I)))
14404               or else Entity (Prefix (High_Bound (I))) /= Def_Id
14405             then
14406                Def_Id := Empty;
14407             end if;
14408          end if;
14409
14410          R := I;
14411          Process_Range_Expr_In_Decl (R, T);
14412
14413       elsif Nkind (I) = N_Subtype_Indication then
14414
14415          --  The index is given by a subtype with a range constraint
14416
14417          T :=  Base_Type (Entity (Subtype_Mark (I)));
14418
14419          if not Is_Discrete_Type (T) then
14420             Error_Msg_N ("discrete type required for range", I);
14421             Set_Etype (I, Any_Type);
14422             return;
14423          end if;
14424
14425          R := Range_Expression (Constraint (I));
14426
14427          Resolve (R, T);
14428          Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (I)));
14429
14430       elsif Nkind (I) = N_Attribute_Reference then
14431
14432          --  The parser guarantees that the attribute is a RANGE attribute
14433
14434          --  If the node denotes the range of a type mark, that is also the
14435          --  resulting type, and we do no need to create an Itype for it.
14436
14437          if Is_Entity_Name (Prefix (I))
14438            and then Comes_From_Source (I)
14439            and then Is_Type (Entity (Prefix (I)))
14440            and then Is_Discrete_Type (Entity (Prefix (I)))
14441          then
14442             Def_Id := Entity (Prefix (I));
14443          end if;
14444
14445          Analyze_And_Resolve (I);
14446          T := Etype (I);
14447          R := I;
14448
14449       --  If none of the above, must be a subtype. We convert this to a
14450       --  range attribute reference because in the case of declared first
14451       --  named subtypes, the types in the range reference can be different
14452       --  from the type of the entity. A range attribute normalizes the
14453       --  reference and obtains the correct types for the bounds.
14454
14455       --  This transformation is in the nature of an expansion, is only
14456       --  done if expansion is active. In particular, it is not done on
14457       --  formal generic types,  because we need to retain the name of the
14458       --  original index for instantiation purposes.
14459
14460       else
14461          if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
14462             Error_Msg_N ("invalid subtype mark in discrete range ", I);
14463             Set_Etype (I, Any_Integer);
14464             return;
14465
14466          else
14467             --  The type mark may be that of an incomplete type. It is only
14468             --  now that we can get the full view, previous analysis does
14469             --  not look specifically for a type mark.
14470
14471             Set_Entity (I, Get_Full_View (Entity (I)));
14472             Set_Etype  (I, Entity (I));
14473             Def_Id := Entity (I);
14474
14475             if not Is_Discrete_Type (Def_Id) then
14476                Error_Msg_N ("discrete type required for index", I);
14477                Set_Etype (I, Any_Type);
14478                return;
14479             end if;
14480          end if;
14481
14482          if Expander_Active then
14483             Rewrite (I,
14484               Make_Attribute_Reference (Sloc (I),
14485                 Attribute_Name => Name_Range,
14486                 Prefix         => Relocate_Node (I)));
14487
14488             --  The original was a subtype mark that does not freeze. This
14489             --  means that the rewritten version must not freeze either.
14490
14491             Set_Must_Not_Freeze (I);
14492             Set_Must_Not_Freeze (Prefix (I));
14493
14494             --  Is order critical??? if so, document why, if not
14495             --  use Analyze_And_Resolve
14496
14497             Analyze_And_Resolve (I);
14498             T := Etype (I);
14499             R := I;
14500
14501          --  If expander is inactive, type is legal, nothing else to construct
14502
14503          else
14504             return;
14505          end if;
14506       end if;
14507
14508       if not Is_Discrete_Type (T) then
14509          Error_Msg_N ("discrete type required for range", I);
14510          Set_Etype (I, Any_Type);
14511          return;
14512
14513       elsif T = Any_Type then
14514          Set_Etype (I, Any_Type);
14515          return;
14516       end if;
14517
14518       --  We will now create the appropriate Itype to describe the range, but
14519       --  first a check. If we originally had a subtype, then we just label
14520       --  the range with this subtype. Not only is there no need to construct
14521       --  a new subtype, but it is wrong to do so for two reasons:
14522
14523       --    1. A legality concern, if we have a subtype, it must not freeze,
14524       --       and the Itype would cause freezing incorrectly
14525
14526       --    2. An efficiency concern, if we created an Itype, it would not be
14527       --       recognized as the same type for the purposes of eliminating
14528       --       checks in some circumstances.
14529
14530       --  We signal this case by setting the subtype entity in Def_Id
14531
14532       if No (Def_Id) then
14533          Def_Id :=
14534            Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
14535          Set_Etype (Def_Id, Base_Type (T));
14536
14537          if Is_Signed_Integer_Type (T) then
14538             Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14539
14540          elsif Is_Modular_Integer_Type (T) then
14541             Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14542
14543          else
14544             Set_Ekind             (Def_Id, E_Enumeration_Subtype);
14545             Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14546             Set_First_Literal     (Def_Id, First_Literal (T));
14547          end if;
14548
14549          Set_Size_Info      (Def_Id,                  (T));
14550          Set_RM_Size        (Def_Id, RM_Size          (T));
14551          Set_First_Rep_Item (Def_Id, First_Rep_Item   (T));
14552
14553          Set_Scalar_Range   (Def_Id, R);
14554          Conditional_Delay  (Def_Id, T);
14555
14556          --  In the subtype indication case, if the immediate parent of the
14557          --  new subtype is non-static, then the subtype we create is non-
14558          --  static, even if its bounds are static.
14559
14560          if Nkind (I) = N_Subtype_Indication
14561            and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
14562          then
14563             Set_Is_Non_Static_Subtype (Def_Id);
14564          end if;
14565       end if;
14566
14567       --  Final step is to label the index with this constructed type
14568
14569       Set_Etype (I, Def_Id);
14570    end Make_Index;
14571
14572    ------------------------------
14573    -- Modular_Type_Declaration --
14574    ------------------------------
14575
14576    procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
14577       Mod_Expr : constant Node_Id := Expression (Def);
14578       M_Val    : Uint;
14579
14580       procedure Set_Modular_Size (Bits : Int);
14581       --  Sets RM_Size to Bits, and Esize to normal word size above this
14582
14583       ----------------------
14584       -- Set_Modular_Size --
14585       ----------------------
14586
14587       procedure Set_Modular_Size (Bits : Int) is
14588       begin
14589          Set_RM_Size (T, UI_From_Int (Bits));
14590
14591          if Bits <= 8 then
14592             Init_Esize (T, 8);
14593
14594          elsif Bits <= 16 then
14595             Init_Esize (T, 16);
14596
14597          elsif Bits <= 32 then
14598             Init_Esize (T, 32);
14599
14600          else
14601             Init_Esize (T, System_Max_Binary_Modulus_Power);
14602          end if;
14603       end Set_Modular_Size;
14604
14605    --  Start of processing for Modular_Type_Declaration
14606
14607    begin
14608       Analyze_And_Resolve (Mod_Expr, Any_Integer);
14609       Set_Etype (T, T);
14610       Set_Ekind (T, E_Modular_Integer_Type);
14611       Init_Alignment (T);
14612       Set_Is_Constrained (T);
14613
14614       if not Is_OK_Static_Expression (Mod_Expr) then
14615          Flag_Non_Static_Expr
14616            ("non-static expression used for modular type bound!", Mod_Expr);
14617          M_Val := 2 ** System_Max_Binary_Modulus_Power;
14618       else
14619          M_Val := Expr_Value (Mod_Expr);
14620       end if;
14621
14622       if M_Val < 1 then
14623          Error_Msg_N ("modulus value must be positive", Mod_Expr);
14624          M_Val := 2 ** System_Max_Binary_Modulus_Power;
14625       end if;
14626
14627       Set_Modulus (T, M_Val);
14628
14629       --   Create bounds for the modular type based on the modulus given in
14630       --   the type declaration and then analyze and resolve those bounds.
14631
14632       Set_Scalar_Range (T,
14633         Make_Range (Sloc (Mod_Expr),
14634           Low_Bound  =>
14635             Make_Integer_Literal (Sloc (Mod_Expr), 0),
14636           High_Bound =>
14637             Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
14638
14639       --  Properly analyze the literals for the range. We do this manually
14640       --  because we can't go calling Resolve, since we are resolving these
14641       --  bounds with the type, and this type is certainly not complete yet!
14642
14643       Set_Etype (Low_Bound  (Scalar_Range (T)), T);
14644       Set_Etype (High_Bound (Scalar_Range (T)), T);
14645       Set_Is_Static_Expression (Low_Bound  (Scalar_Range (T)));
14646       Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
14647
14648       --  Loop through powers of two to find number of bits required
14649
14650       for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
14651
14652          --  Binary case
14653
14654          if M_Val = 2 ** Bits then
14655             Set_Modular_Size (Bits);
14656             return;
14657
14658          --  Non-binary case
14659
14660          elsif M_Val < 2 ** Bits then
14661             Set_Non_Binary_Modulus (T);
14662
14663             if Bits > System_Max_Nonbinary_Modulus_Power then
14664                Error_Msg_Uint_1 :=
14665                  UI_From_Int (System_Max_Nonbinary_Modulus_Power);
14666                Error_Msg_F
14667                  ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
14668                Set_Modular_Size (System_Max_Binary_Modulus_Power);
14669                return;
14670
14671             else
14672                --  In the non-binary case, set size as per RM 13.3(55)
14673
14674                Set_Modular_Size (Bits);
14675                return;
14676             end if;
14677          end if;
14678
14679       end loop;
14680
14681       --  If we fall through, then the size exceed System.Max_Binary_Modulus
14682       --  so we just signal an error and set the maximum size.
14683
14684       Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
14685       Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
14686
14687       Set_Modular_Size (System_Max_Binary_Modulus_Power);
14688       Init_Alignment (T);
14689
14690    end Modular_Type_Declaration;
14691
14692    --------------------------
14693    -- New_Concatenation_Op --
14694    --------------------------
14695
14696    procedure New_Concatenation_Op (Typ : Entity_Id) is
14697       Loc : constant Source_Ptr := Sloc (Typ);
14698       Op  : Entity_Id;
14699
14700       function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
14701       --  Create abbreviated declaration for the formal of a predefined
14702       --  Operator 'Op' of type 'Typ'
14703
14704       --------------------
14705       -- Make_Op_Formal --
14706       --------------------
14707
14708       function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
14709          Formal : Entity_Id;
14710       begin
14711          Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
14712          Set_Etype (Formal, Typ);
14713          Set_Mechanism (Formal, Default_Mechanism);
14714          return Formal;
14715       end Make_Op_Formal;
14716
14717    --  Start of processing for New_Concatenation_Op
14718
14719    begin
14720       Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
14721
14722       Set_Ekind                   (Op, E_Operator);
14723       Set_Scope                   (Op, Current_Scope);
14724       Set_Etype                   (Op, Typ);
14725       Set_Homonym                 (Op, Get_Name_Entity_Id (Name_Op_Concat));
14726       Set_Is_Immediately_Visible  (Op);
14727       Set_Is_Intrinsic_Subprogram (Op);
14728       Set_Has_Completion          (Op);
14729       Append_Entity               (Op, Current_Scope);
14730
14731       Set_Name_Entity_Id (Name_Op_Concat, Op);
14732
14733       Append_Entity (Make_Op_Formal (Typ, Op), Op);
14734       Append_Entity (Make_Op_Formal (Typ, Op), Op);
14735    end New_Concatenation_Op;
14736
14737    -------------------------
14738    -- OK_For_Limited_Init --
14739    -------------------------
14740
14741    --  ???Check all calls of this, and compare the conditions under which it's
14742    --  called.
14743
14744    function OK_For_Limited_Init (Exp : Node_Id) return Boolean is
14745    begin
14746       return Ada_Version >= Ada_05
14747         and then not Debug_Flag_Dot_L
14748         and then OK_For_Limited_Init_In_05 (Exp);
14749    end OK_For_Limited_Init;
14750
14751    -------------------------------
14752    -- OK_For_Limited_Init_In_05 --
14753    -------------------------------
14754
14755    function OK_For_Limited_Init_In_05 (Exp : Node_Id) return Boolean is
14756    begin
14757       --  Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
14758       --  case of limited aggregates (including extension aggregates), and
14759       --  function calls. The function call may have been give in prefixed
14760       --  notation, in which case the original node is an indexed component.
14761
14762       case Nkind (Original_Node (Exp)) is
14763          when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
14764             return True;
14765
14766          when N_Qualified_Expression =>
14767             return
14768               OK_For_Limited_Init_In_05 (Expression (Original_Node (Exp)));
14769
14770          --  Ada 2005 (AI-251): If a class-wide interface object is initialized
14771          --  with a function call, the expander has rewritten the call into an
14772          --  N_Type_Conversion node to force displacement of the pointer to
14773          --  reference the component containing the secondary dispatch table.
14774          --  Otherwise a type conversion is not a legal context.
14775
14776          when N_Type_Conversion =>
14777             return not Comes_From_Source (Exp)
14778               and then
14779                 OK_For_Limited_Init_In_05 (Expression (Original_Node (Exp)));
14780
14781          when N_Indexed_Component | N_Selected_Component  =>
14782             return Nkind (Exp) = N_Function_Call;
14783
14784          --  A use of 'Input is a function call, hence allowed. Normally the
14785          --  attribute will be changed to a call, but the attribute by itself
14786          --  can occur with -gnatc.
14787
14788          when N_Attribute_Reference =>
14789             return Attribute_Name (Original_Node (Exp)) = Name_Input;
14790
14791          when others =>
14792             return False;
14793       end case;
14794    end OK_For_Limited_Init_In_05;
14795
14796    -------------------------------------------
14797    -- Ordinary_Fixed_Point_Type_Declaration --
14798    -------------------------------------------
14799
14800    procedure Ordinary_Fixed_Point_Type_Declaration
14801      (T   : Entity_Id;
14802       Def : Node_Id)
14803    is
14804       Loc           : constant Source_Ptr := Sloc (Def);
14805       Delta_Expr    : constant Node_Id    := Delta_Expression (Def);
14806       RRS           : constant Node_Id    := Real_Range_Specification (Def);
14807       Implicit_Base : Entity_Id;
14808       Delta_Val     : Ureal;
14809       Small_Val     : Ureal;
14810       Low_Val       : Ureal;
14811       High_Val      : Ureal;
14812
14813    begin
14814       Check_Restriction (No_Fixed_Point, Def);
14815
14816       --  Create implicit base type
14817
14818       Implicit_Base :=
14819         Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
14820       Set_Etype (Implicit_Base, Implicit_Base);
14821
14822       --  Analyze and process delta expression
14823
14824       Analyze_And_Resolve (Delta_Expr, Any_Real);
14825
14826       Check_Delta_Expression (Delta_Expr);
14827       Delta_Val := Expr_Value_R (Delta_Expr);
14828
14829       Set_Delta_Value (Implicit_Base, Delta_Val);
14830
14831       --  Compute default small from given delta, which is the largest power
14832       --  of two that does not exceed the given delta value.
14833
14834       declare
14835          Tmp   : Ureal;
14836          Scale : Int;
14837
14838       begin
14839          Tmp := Ureal_1;
14840          Scale := 0;
14841
14842          if Delta_Val < Ureal_1 then
14843             while Delta_Val < Tmp loop
14844                Tmp := Tmp / Ureal_2;
14845                Scale := Scale + 1;
14846             end loop;
14847
14848          else
14849             loop
14850                Tmp := Tmp * Ureal_2;
14851                exit when Tmp > Delta_Val;
14852                Scale := Scale - 1;
14853             end loop;
14854          end if;
14855
14856          Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
14857       end;
14858
14859       Set_Small_Value (Implicit_Base, Small_Val);
14860
14861       --  If no range was given, set a dummy range
14862
14863       if RRS <= Empty_Or_Error then
14864          Low_Val  := -Small_Val;
14865          High_Val := Small_Val;
14866
14867       --  Otherwise analyze and process given range
14868
14869       else
14870          declare
14871             Low  : constant Node_Id := Low_Bound  (RRS);
14872             High : constant Node_Id := High_Bound (RRS);
14873
14874          begin
14875             Analyze_And_Resolve (Low, Any_Real);
14876             Analyze_And_Resolve (High, Any_Real);
14877             Check_Real_Bound (Low);
14878             Check_Real_Bound (High);
14879
14880             --  Obtain and set the range
14881
14882             Low_Val  := Expr_Value_R (Low);
14883             High_Val := Expr_Value_R (High);
14884
14885             if Low_Val > High_Val then
14886                Error_Msg_NE ("?fixed point type& has null range", Def, T);
14887             end if;
14888          end;
14889       end if;
14890
14891       --  The range for both the implicit base and the declared first subtype
14892       --  cannot be set yet, so we use the special routine Set_Fixed_Range to
14893       --  set a temporary range in place. Note that the bounds of the base
14894       --  type will be widened to be symmetrical and to fill the available
14895       --  bits when the type is frozen.
14896
14897       --  We could do this with all discrete types, and probably should, but
14898       --  we absolutely have to do it for fixed-point, since the end-points
14899       --  of the range and the size are determined by the small value, which
14900       --  could be reset before the freeze point.
14901
14902       Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
14903       Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14904
14905       --  Complete definition of first subtype
14906
14907       Set_Ekind          (T, E_Ordinary_Fixed_Point_Subtype);
14908       Set_Etype          (T, Implicit_Base);
14909       Init_Size_Align    (T);
14910       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
14911       Set_Small_Value    (T, Small_Val);
14912       Set_Delta_Value    (T, Delta_Val);
14913       Set_Is_Constrained (T);
14914
14915    end Ordinary_Fixed_Point_Type_Declaration;
14916
14917    ----------------------------------------
14918    -- Prepare_Private_Subtype_Completion --
14919    ----------------------------------------
14920
14921    procedure Prepare_Private_Subtype_Completion
14922      (Id          : Entity_Id;
14923       Related_Nod : Node_Id)
14924    is
14925       Id_B   : constant Entity_Id := Base_Type (Id);
14926       Full_B : constant Entity_Id := Full_View (Id_B);
14927       Full   : Entity_Id;
14928
14929    begin
14930       if Present (Full_B) then
14931
14932          --  The Base_Type is already completed, we can complete the subtype
14933          --  now. We have to create a new entity with the same name, Thus we
14934          --  can't use Create_Itype.
14935
14936          --  This is messy, should be fixed ???
14937
14938          Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
14939          Set_Is_Itype (Full);
14940          Set_Associated_Node_For_Itype (Full, Related_Nod);
14941          Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
14942       end if;
14943
14944       --  The parent subtype may be private, but the base might not, in some
14945       --  nested instances. In that case, the subtype does not need to be
14946       --  exchanged. It would still be nice to make private subtypes and their
14947       --  bases consistent at all times ???
14948
14949       if Is_Private_Type (Id_B) then
14950          Append_Elmt (Id, Private_Dependents (Id_B));
14951       end if;
14952
14953    end Prepare_Private_Subtype_Completion;
14954
14955    ---------------------------
14956    -- Process_Discriminants --
14957    ---------------------------
14958
14959    procedure Process_Discriminants
14960      (N    : Node_Id;
14961       Prev : Entity_Id := Empty)
14962    is
14963       Elist               : constant Elist_Id := New_Elmt_List;
14964       Id                  : Node_Id;
14965       Discr               : Node_Id;
14966       Discr_Number        : Uint;
14967       Discr_Type          : Entity_Id;
14968       Default_Present     : Boolean := False;
14969       Default_Not_Present : Boolean := False;
14970
14971    begin
14972       --  A composite type other than an array type can have discriminants.
14973       --  On entry, the current scope is the composite type.
14974
14975       --  The discriminants are initially entered into the scope of the type
14976       --  via Enter_Name with the default Ekind of E_Void to prevent premature
14977       --  use, as explained at the end of this procedure.
14978
14979       Discr := First (Discriminant_Specifications (N));
14980       while Present (Discr) loop
14981          Enter_Name (Defining_Identifier (Discr));
14982
14983          --  For navigation purposes we add a reference to the discriminant
14984          --  in the entity for the type. If the current declaration is a
14985          --  completion, place references on the partial view. Otherwise the
14986          --  type is the current scope.
14987
14988          if Present (Prev) then
14989
14990             --  The references go on the partial view, if present. If the
14991             --  partial view has discriminants, the references have been
14992             --  generated already.
14993
14994             if not Has_Discriminants (Prev) then
14995                Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
14996             end if;
14997          else
14998             Generate_Reference
14999               (Current_Scope, Defining_Identifier (Discr), 'd');
15000          end if;
15001
15002          if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
15003             Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
15004
15005             --  Ada 2005 (AI-254)
15006
15007             if Present (Access_To_Subprogram_Definition
15008                          (Discriminant_Type (Discr)))
15009               and then Protected_Present (Access_To_Subprogram_Definition
15010                                            (Discriminant_Type (Discr)))
15011             then
15012                Discr_Type :=
15013                  Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
15014             end if;
15015
15016          else
15017             Find_Type (Discriminant_Type (Discr));
15018             Discr_Type := Etype (Discriminant_Type (Discr));
15019
15020             if Error_Posted (Discriminant_Type (Discr)) then
15021                Discr_Type := Any_Type;
15022             end if;
15023          end if;
15024
15025          if Is_Access_Type (Discr_Type) then
15026
15027             --  Ada 2005 (AI-230): Access discriminant allowed in non-limited
15028             --  record types
15029
15030             if Ada_Version < Ada_05 then
15031                Check_Access_Discriminant_Requires_Limited
15032                  (Discr, Discriminant_Type (Discr));
15033             end if;
15034
15035             if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
15036                Error_Msg_N
15037                  ("(Ada 83) access discriminant not allowed", Discr);
15038             end if;
15039
15040          elsif not Is_Discrete_Type (Discr_Type) then
15041             Error_Msg_N ("discriminants must have a discrete or access type",
15042               Discriminant_Type (Discr));
15043          end if;
15044
15045          Set_Etype (Defining_Identifier (Discr), Discr_Type);
15046
15047          --  If a discriminant specification includes the assignment compound
15048          --  delimiter followed by an expression, the expression is the default
15049          --  expression of the discriminant; the default expression must be of
15050          --  the type of the discriminant. (RM 3.7.1) Since this expression is
15051          --  a default expression, we do the special preanalysis, since this
15052          --  expression does not freeze (see "Handling of Default and Per-
15053          --  Object Expressions" in spec of package Sem).
15054
15055          if Present (Expression (Discr)) then
15056             Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
15057
15058             if Nkind (N) = N_Formal_Type_Declaration then
15059                Error_Msg_N
15060                  ("discriminant defaults not allowed for formal type",
15061                   Expression (Discr));
15062
15063             --  Tagged types cannot have defaulted discriminants, but a
15064             --  non-tagged private type with defaulted discriminants
15065             --   can have a tagged completion.
15066
15067             elsif Is_Tagged_Type (Current_Scope)
15068               and then Comes_From_Source (N)
15069             then
15070                Error_Msg_N
15071                  ("discriminants of tagged type cannot have defaults",
15072                   Expression (Discr));
15073
15074             else
15075                Default_Present := True;
15076                Append_Elmt (Expression (Discr), Elist);
15077
15078                --  Tag the defining identifiers for the discriminants with
15079                --  their corresponding default expressions from the tree.
15080
15081                Set_Discriminant_Default_Value
15082                  (Defining_Identifier (Discr), Expression (Discr));
15083             end if;
15084
15085          else
15086             Default_Not_Present := True;
15087          end if;
15088
15089          --  Ada 2005 (AI-231): Create an Itype that is a duplicate of
15090          --  Discr_Type but with the null-exclusion attribute
15091
15092          if Ada_Version >= Ada_05 then
15093
15094             --  Ada 2005 (AI-231): Static checks
15095
15096             if Can_Never_Be_Null (Discr_Type) then
15097                Null_Exclusion_Static_Checks (Discr);
15098
15099             elsif Is_Access_Type (Discr_Type)
15100               and then Null_Exclusion_Present (Discr)
15101
15102                --  No need to check itypes because in their case this check
15103                --  was done at their point of creation
15104
15105               and then not Is_Itype (Discr_Type)
15106             then
15107                if Can_Never_Be_Null (Discr_Type) then
15108                   Error_Msg_NE
15109                     ("`NOT NULL` not allowed (& already excludes null)",
15110                      Discr,
15111                      Discr_Type);
15112                end if;
15113
15114                Set_Etype (Defining_Identifier (Discr),
15115                  Create_Null_Excluding_Itype
15116                    (T           => Discr_Type,
15117                     Related_Nod => Discr));
15118             end if;
15119
15120             --  Ada 2005 (AI-402): access discriminants of nonlimited types
15121             --  can't have defaults. Synchronized types, or types that are
15122             --  explicitly limited are fine, but special tests apply to derived
15123             --  types in generics: in a generic body we have to assume the
15124             --  worst, and therefore defaults are not allowed if the parent is
15125             --  a generic formal private type (see ACATS B370001).
15126
15127             if Is_Access_Type (Discr_Type) then
15128                if Ekind (Discr_Type) /= E_Anonymous_Access_Type
15129                  or else not Default_Present
15130                  or else Is_Limited_Record (Current_Scope)
15131                  or else Is_Concurrent_Type (Current_Scope)
15132                  or else Is_Concurrent_Record_Type (Current_Scope)
15133                  or else Ekind (Current_Scope) = E_Limited_Private_Type
15134                then
15135                   if not Is_Derived_Type (Current_Scope)
15136                     or else not Is_Generic_Type (Etype (Current_Scope))
15137                     or else not In_Package_Body (Scope (Etype (Current_Scope)))
15138                     or else Limited_Present
15139                               (Type_Definition (Parent (Current_Scope)))
15140                   then
15141                      null;
15142
15143                   else
15144                      Error_Msg_N ("access discriminants of nonlimited types",
15145                          Expression (Discr));
15146                      Error_Msg_N ("\cannot have defaults", Expression (Discr));
15147                   end if;
15148
15149                elsif Present (Expression (Discr)) then
15150                   Error_Msg_N
15151                     ("(Ada 2005) access discriminants of nonlimited types",
15152                      Expression (Discr));
15153                   Error_Msg_N ("\cannot have defaults", Expression (Discr));
15154                end if;
15155             end if;
15156          end if;
15157
15158          Next (Discr);
15159       end loop;
15160
15161       --  An element list consisting of the default expressions of the
15162       --  discriminants is constructed in the above loop and used to set
15163       --  the Discriminant_Constraint attribute for the type. If an object
15164       --  is declared of this (record or task) type without any explicit
15165       --  discriminant constraint given, this element list will form the
15166       --  actual parameters for the corresponding initialization procedure
15167       --  for the type.
15168
15169       Set_Discriminant_Constraint (Current_Scope, Elist);
15170       Set_Stored_Constraint (Current_Scope, No_Elist);
15171
15172       --  Default expressions must be provided either for all or for none
15173       --  of the discriminants of a discriminant part. (RM 3.7.1)
15174
15175       if Default_Present and then Default_Not_Present then
15176          Error_Msg_N
15177            ("incomplete specification of defaults for discriminants", N);
15178       end if;
15179
15180       --  The use of the name of a discriminant is not allowed in default
15181       --  expressions of a discriminant part if the specification of the
15182       --  discriminant is itself given in the discriminant part. (RM 3.7.1)
15183
15184       --  To detect this, the discriminant names are entered initially with an
15185       --  Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
15186       --  attempt to use a void entity (for example in an expression that is
15187       --  type-checked) produces the error message: premature usage. Now after
15188       --  completing the semantic analysis of the discriminant part, we can set
15189       --  the Ekind of all the discriminants appropriately.
15190
15191       Discr := First (Discriminant_Specifications (N));
15192       Discr_Number := Uint_1;
15193       while Present (Discr) loop
15194          Id := Defining_Identifier (Discr);
15195          Set_Ekind (Id, E_Discriminant);
15196          Init_Component_Location (Id);
15197          Init_Esize (Id);
15198          Set_Discriminant_Number (Id, Discr_Number);
15199
15200          --  Make sure this is always set, even in illegal programs
15201
15202          Set_Corresponding_Discriminant (Id, Empty);
15203
15204          --  Initialize the Original_Record_Component to the entity itself.
15205          --  Inherit_Components will propagate the right value to
15206          --  discriminants in derived record types.
15207
15208          Set_Original_Record_Component (Id, Id);
15209
15210          --  Create the discriminal for the discriminant
15211
15212          Build_Discriminal (Id);
15213
15214          Next (Discr);
15215          Discr_Number := Discr_Number + 1;
15216       end loop;
15217
15218       Set_Has_Discriminants (Current_Scope);
15219    end Process_Discriminants;
15220
15221    -----------------------
15222    -- Process_Full_View --
15223    -----------------------
15224
15225    procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
15226       Priv_Parent : Entity_Id;
15227       Full_Parent : Entity_Id;
15228       Full_Indic  : Node_Id;
15229
15230       procedure Collect_Implemented_Interfaces
15231         (Typ    : Entity_Id;
15232          Ifaces : Elist_Id);
15233       --  Ada 2005: Gather all the interfaces that Typ directly or
15234       --  inherently implements. Duplicate entries are not added to
15235       --  the list Ifaces.
15236
15237       ------------------------------------
15238       -- Collect_Implemented_Interfaces --
15239       ------------------------------------
15240
15241       procedure Collect_Implemented_Interfaces
15242         (Typ    : Entity_Id;
15243          Ifaces : Elist_Id)
15244       is
15245          Iface      : Entity_Id;
15246          Iface_Elmt : Elmt_Id;
15247
15248       begin
15249          --  Abstract interfaces are only associated with tagged record types
15250
15251          if not Is_Tagged_Type (Typ)
15252            or else not Is_Record_Type (Typ)
15253          then
15254             return;
15255          end if;
15256
15257          --  Recursively climb to the ancestors
15258
15259          if Etype (Typ) /= Typ
15260
15261             --  Protect the frontend against wrong cyclic declarations like:
15262
15263             --     type B is new A with private;
15264             --     type C is new A with private;
15265             --  private
15266             --     type B is new C with null record;
15267             --     type C is new B with null record;
15268
15269            and then Etype (Typ) /= Priv_T
15270            and then Etype (Typ) /= Full_T
15271          then
15272             --  Keep separate the management of private type declarations
15273
15274             if Ekind (Typ) = E_Record_Type_With_Private then
15275
15276                --  Handle the following erronous case:
15277                --      type Private_Type is tagged private;
15278                --   private
15279                --      type Private_Type is new Type_Implementing_Iface;
15280
15281                if Present (Full_View (Typ))
15282                  and then Etype (Typ) /= Full_View (Typ)
15283                then
15284                   if Is_Interface (Etype (Typ)) then
15285                      Append_Unique_Elmt (Etype (Typ), Ifaces);
15286                   end if;
15287
15288                   Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
15289                end if;
15290
15291             --  Non-private types
15292
15293             else
15294                if Is_Interface (Etype (Typ)) then
15295                   Append_Unique_Elmt (Etype (Typ), Ifaces);
15296                end if;
15297
15298                Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
15299             end if;
15300          end if;
15301
15302          --  Handle entities in the list of abstract interfaces
15303
15304          if Present (Abstract_Interfaces (Typ)) then
15305             Iface_Elmt := First_Elmt (Abstract_Interfaces (Typ));
15306             while Present (Iface_Elmt) loop
15307                Iface := Node (Iface_Elmt);
15308
15309                pragma Assert (Is_Interface (Iface));
15310
15311                if not Contain_Interface (Iface, Ifaces) then
15312                   Append_Elmt (Iface, Ifaces);
15313                   Collect_Implemented_Interfaces (Iface, Ifaces);
15314                end if;
15315
15316                Next_Elmt (Iface_Elmt);
15317             end loop;
15318          end if;
15319       end Collect_Implemented_Interfaces;
15320
15321    --  Start of processing for Process_Full_View
15322
15323    begin
15324       --  First some sanity checks that must be done after semantic
15325       --  decoration of the full view and thus cannot be placed with other
15326       --  similar checks in Find_Type_Name
15327
15328       if not Is_Limited_Type (Priv_T)
15329         and then (Is_Limited_Type (Full_T)
15330                    or else Is_Limited_Composite (Full_T))
15331       then
15332          Error_Msg_N
15333            ("completion of nonlimited type cannot be limited", Full_T);
15334          Explain_Limited_Type (Full_T, Full_T);
15335
15336       elsif Is_Abstract_Type (Full_T)
15337         and then not Is_Abstract_Type (Priv_T)
15338       then
15339          Error_Msg_N
15340            ("completion of nonabstract type cannot be abstract", Full_T);
15341
15342       elsif Is_Tagged_Type (Priv_T)
15343         and then Is_Limited_Type (Priv_T)
15344         and then not Is_Limited_Type (Full_T)
15345       then
15346          --  If pragma CPP_Class was applied to the private declaration
15347          --  propagate the limitedness to the full-view
15348
15349          if Is_CPP_Class (Priv_T) then
15350             Set_Is_Limited_Record (Full_T);
15351
15352          --  GNAT allow its own definition of Limited_Controlled to disobey
15353          --  this rule in order in ease the implementation. The next test is
15354          --  safe because Root_Controlled is defined in a private system child
15355
15356          elsif Etype (Full_T) = Full_View (RTE (RE_Root_Controlled)) then
15357             Set_Is_Limited_Composite (Full_T);
15358          else
15359             Error_Msg_N
15360               ("completion of limited tagged type must be limited", Full_T);
15361          end if;
15362
15363       elsif Is_Generic_Type (Priv_T) then
15364          Error_Msg_N ("generic type cannot have a completion", Full_T);
15365       end if;
15366
15367       --  Check that ancestor interfaces of private and full views are
15368       --  consistent. We omit this check for synchronized types because
15369       --  they are performed on the corresponding record type when frozen.
15370
15371       if Ada_Version >= Ada_05
15372         and then Is_Tagged_Type (Priv_T)
15373         and then Is_Tagged_Type (Full_T)
15374         and then not Is_Concurrent_Type (Full_T)
15375       then
15376          declare
15377             Iface         : Entity_Id;
15378             Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
15379             Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
15380
15381          begin
15382             Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
15383             Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
15384
15385             --  Ada 2005 (AI-251): The partial view shall be a descendant of
15386             --  an interface type if and only if the full type is descendant
15387             --  of the interface type (AARM 7.3 (7.3/2).
15388
15389             Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
15390
15391             if Present (Iface) then
15392                Error_Msg_NE ("interface & not implemented by full type " &
15393                              "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
15394             end if;
15395
15396             Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
15397
15398             if Present (Iface) then
15399                Error_Msg_NE ("interface & not implemented by partial view " &
15400                              "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
15401             end if;
15402          end;
15403       end if;
15404
15405       if Is_Tagged_Type (Priv_T)
15406         and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15407         and then Is_Derived_Type (Full_T)
15408       then
15409          Priv_Parent := Etype (Priv_T);
15410
15411          --  The full view of a private extension may have been transformed
15412          --  into an unconstrained derived type declaration and a subtype
15413          --  declaration (see build_derived_record_type for details).
15414
15415          if Nkind (N) = N_Subtype_Declaration then
15416             Full_Indic  := Subtype_Indication (N);
15417             Full_Parent := Etype (Base_Type (Full_T));
15418          else
15419             Full_Indic  := Subtype_Indication (Type_Definition (N));
15420             Full_Parent := Etype (Full_T);
15421          end if;
15422
15423          --  Check that the parent type of the full type is a descendant of
15424          --  the ancestor subtype given in the private extension. If either
15425          --  entity has an Etype equal to Any_Type then we had some previous
15426          --  error situation [7.3(8)].
15427
15428          if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
15429             return;
15430
15431          --  Ada 2005 (AI-251): Interfaces in the full-typ can be given in
15432          --  any order. Therefore we don't have to check that its parent must
15433          --  be a descendant of the parent of the private type declaration.
15434
15435          elsif Is_Interface (Priv_Parent)
15436            and then Is_Interface (Full_Parent)
15437          then
15438             null;
15439
15440          --  Ada 2005 (AI-251): If the parent of the private type declaration
15441          --  is an interface there is no need to check that it is an ancestor
15442          --  of the associated full type declaration. The required tests for
15443          --  this case case are performed by Build_Derived_Record_Type.
15444
15445          elsif not Is_Interface (Base_Type (Priv_Parent))
15446            and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
15447          then
15448             Error_Msg_N
15449               ("parent of full type must descend from parent"
15450                   & " of private extension", Full_Indic);
15451
15452          --  Check the rules of 7.3(10): if the private extension inherits
15453          --  known discriminants, then the full type must also inherit those
15454          --  discriminants from the same (ancestor) type, and the parent
15455          --  subtype of the full type must be constrained if and only if
15456          --  the ancestor subtype of the private extension is constrained.
15457
15458          elsif No (Discriminant_Specifications (Parent (Priv_T)))
15459            and then not Has_Unknown_Discriminants (Priv_T)
15460            and then Has_Discriminants (Base_Type (Priv_Parent))
15461          then
15462             declare
15463                Priv_Indic  : constant Node_Id :=
15464                                Subtype_Indication (Parent (Priv_T));
15465
15466                Priv_Constr : constant Boolean :=
15467                                Is_Constrained (Priv_Parent)
15468                                  or else
15469                                    Nkind (Priv_Indic) = N_Subtype_Indication
15470                                  or else Is_Constrained (Entity (Priv_Indic));
15471
15472                Full_Constr : constant Boolean :=
15473                                Is_Constrained (Full_Parent)
15474                                  or else
15475                                    Nkind (Full_Indic) = N_Subtype_Indication
15476                                  or else Is_Constrained (Entity (Full_Indic));
15477
15478                Priv_Discr : Entity_Id;
15479                Full_Discr : Entity_Id;
15480
15481             begin
15482                Priv_Discr := First_Discriminant (Priv_Parent);
15483                Full_Discr := First_Discriminant (Full_Parent);
15484                while Present (Priv_Discr) and then Present (Full_Discr) loop
15485                   if Original_Record_Component (Priv_Discr) =
15486                      Original_Record_Component (Full_Discr)
15487                     or else
15488                      Corresponding_Discriminant (Priv_Discr) =
15489                      Corresponding_Discriminant (Full_Discr)
15490                   then
15491                      null;
15492                   else
15493                      exit;
15494                   end if;
15495
15496                   Next_Discriminant (Priv_Discr);
15497                   Next_Discriminant (Full_Discr);
15498                end loop;
15499
15500                if Present (Priv_Discr) or else Present (Full_Discr) then
15501                   Error_Msg_N
15502                     ("full view must inherit discriminants of the parent type"
15503                      & " used in the private extension", Full_Indic);
15504
15505                elsif Priv_Constr and then not Full_Constr then
15506                   Error_Msg_N
15507                     ("parent subtype of full type must be constrained",
15508                      Full_Indic);
15509
15510                elsif Full_Constr and then not Priv_Constr then
15511                   Error_Msg_N
15512                     ("parent subtype of full type must be unconstrained",
15513                      Full_Indic);
15514                end if;
15515             end;
15516
15517          --  Check the rules of 7.3(12): if a partial view has neither known
15518          --  or unknown discriminants, then the full type declaration shall
15519          --  define a definite subtype.
15520
15521          elsif      not Has_Unknown_Discriminants (Priv_T)
15522            and then not Has_Discriminants (Priv_T)
15523            and then not Is_Constrained (Full_T)
15524          then
15525             Error_Msg_N
15526               ("full view must define a constrained type if partial view"
15527                 & " has no discriminants", Full_T);
15528          end if;
15529
15530          --  ??????? Do we implement the following properly ?????
15531          --  If the ancestor subtype of a private extension has constrained
15532          --  discriminants, then the parent subtype of the full view shall
15533          --  impose a statically matching constraint on those discriminants
15534          --  [7.3(13)].
15535
15536       else
15537          --  For untagged types, verify that a type without discriminants
15538          --  is not completed with an unconstrained type.
15539
15540          if not Is_Indefinite_Subtype (Priv_T)
15541            and then Is_Indefinite_Subtype (Full_T)
15542          then
15543             Error_Msg_N ("full view of type must be definite subtype", Full_T);
15544          end if;
15545       end if;
15546
15547       --  AI-419: verify that the use of "limited" is consistent
15548
15549       declare
15550          Orig_Decl : constant Node_Id := Original_Node (N);
15551
15552       begin
15553          if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15554            and then not Limited_Present (Parent (Priv_T))
15555            and then not Synchronized_Present (Parent (Priv_T))
15556            and then Nkind (Orig_Decl) = N_Full_Type_Declaration
15557            and then Nkind
15558              (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
15559            and then Limited_Present (Type_Definition (Orig_Decl))
15560          then
15561             Error_Msg_N
15562               ("full view of non-limited extension cannot be limited", N);
15563          end if;
15564       end;
15565
15566       --  Ada 2005 (AI-443): A synchronized private extension must be
15567       --  completed by a task or protected type.
15568
15569       if Ada_Version >= Ada_05
15570         and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15571         and then Synchronized_Present (Parent (Priv_T))
15572         and then not Is_Concurrent_Type (Full_T)
15573       then
15574          Error_Msg_N ("full view of synchronized extension must " &
15575                       "be synchronized type", N);
15576       end if;
15577
15578       --  Ada 2005 AI-363: if the full view has discriminants with
15579       --  defaults, it is illegal to declare constrained access subtypes
15580       --  whose designated type is the current type. This allows objects
15581       --  of the type that are declared in the heap to be unconstrained.
15582
15583       if not Has_Unknown_Discriminants (Priv_T)
15584         and then not Has_Discriminants (Priv_T)
15585         and then Has_Discriminants (Full_T)
15586         and then
15587           Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
15588       then
15589          Set_Has_Constrained_Partial_View (Full_T);
15590          Set_Has_Constrained_Partial_View (Priv_T);
15591       end if;
15592
15593       --  Create a full declaration for all its subtypes recorded in
15594       --  Private_Dependents and swap them similarly to the base type. These
15595       --  are subtypes that have been define before the full declaration of
15596       --  the private type. We also swap the entry in Private_Dependents list
15597       --  so we can properly restore the private view on exit from the scope.
15598
15599       declare
15600          Priv_Elmt : Elmt_Id;
15601          Priv      : Entity_Id;
15602          Full      : Entity_Id;
15603
15604       begin
15605          Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
15606          while Present (Priv_Elmt) loop
15607             Priv := Node (Priv_Elmt);
15608
15609             if Ekind (Priv) = E_Private_Subtype
15610               or else Ekind (Priv) = E_Limited_Private_Subtype
15611               or else Ekind (Priv) = E_Record_Subtype_With_Private
15612             then
15613                Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
15614                Set_Is_Itype (Full);
15615                Set_Parent (Full, Parent (Priv));
15616                Set_Associated_Node_For_Itype (Full, N);
15617
15618                --  Now we need to complete the private subtype, but since the
15619                --  base type has already been swapped, we must also swap the
15620                --  subtypes (and thus, reverse the arguments in the call to
15621                --  Complete_Private_Subtype).
15622
15623                Copy_And_Swap (Priv, Full);
15624                Complete_Private_Subtype (Full, Priv, Full_T, N);
15625                Replace_Elmt (Priv_Elmt, Full);
15626             end if;
15627
15628             Next_Elmt (Priv_Elmt);
15629          end loop;
15630       end;
15631
15632       --  If the private view was tagged, copy the new primitive operations
15633       --  from the private view to the full view.
15634
15635       if Is_Tagged_Type (Full_T)
15636         and then not Is_Concurrent_Type (Full_T)
15637       then
15638          declare
15639             Priv_List : Elist_Id;
15640             Full_List : constant Elist_Id := Primitive_Operations (Full_T);
15641             P1, P2    : Elmt_Id;
15642             Prim      : Entity_Id;
15643             D_Type    : Entity_Id;
15644
15645          begin
15646             if Is_Tagged_Type (Priv_T) then
15647                Priv_List := Primitive_Operations (Priv_T);
15648
15649                P1 := First_Elmt (Priv_List);
15650                while Present (P1) loop
15651                   Prim := Node (P1);
15652
15653                   --  Transfer explicit primitives, not those inherited from
15654                   --  parent of partial view, which will be re-inherited on
15655                   --  the full view.
15656
15657                   if Comes_From_Source (Prim) then
15658                      P2 := First_Elmt (Full_List);
15659                      while Present (P2) and then Node (P2) /= Prim loop
15660                         Next_Elmt (P2);
15661                      end loop;
15662
15663                      --  If not found, that is a new one
15664
15665                      if No (P2) then
15666                         Append_Elmt (Prim, Full_List);
15667                      end if;
15668                   end if;
15669
15670                   Next_Elmt (P1);
15671                end loop;
15672
15673             else
15674                --  In this case the partial view is untagged, so here we locate
15675                --  all of the earlier primitives that need to be treated as
15676                --  dispatching (those that appear between the two views). Note
15677                --  that these additional operations must all be new operations
15678                --  (any earlier operations that override inherited operations
15679                --  of the full view will already have been inserted in the
15680                --  primitives list, marked by Check_Operation_From_Private_View
15681                --  as dispatching. Note that implicit "/=" operators are
15682                --  excluded from being added to the primitives list since they
15683                --  shouldn't be treated as dispatching (tagged "/=" is handled
15684                --  specially).
15685
15686                Prim := Next_Entity (Full_T);
15687                while Present (Prim) and then Prim /= Priv_T loop
15688                   if Ekind (Prim) = E_Procedure
15689                        or else
15690                      Ekind (Prim) = E_Function
15691                   then
15692
15693                      D_Type := Find_Dispatching_Type (Prim);
15694
15695                      if D_Type = Full_T
15696                        and then (Chars (Prim) /= Name_Op_Ne
15697                                   or else Comes_From_Source (Prim))
15698                      then
15699                         Check_Controlling_Formals (Full_T, Prim);
15700
15701                         if not Is_Dispatching_Operation (Prim) then
15702                            Append_Elmt (Prim, Full_List);
15703                            Set_Is_Dispatching_Operation (Prim, True);
15704                            Set_DT_Position (Prim, No_Uint);
15705                         end if;
15706
15707                      elsif Is_Dispatching_Operation (Prim)
15708                        and then D_Type  /= Full_T
15709                      then
15710
15711                         --  Verify that it is not otherwise controlled by a
15712                         --  formal or a return value of type T.
15713
15714                         Check_Controlling_Formals (D_Type, Prim);
15715                      end if;
15716                   end if;
15717
15718                   Next_Entity (Prim);
15719                end loop;
15720             end if;
15721
15722             --  For the tagged case, the two views can share the same
15723             --  Primitive Operation list and the same class wide type.
15724             --  Update attributes of the class-wide type which depend on
15725             --  the full declaration.
15726
15727             if Is_Tagged_Type (Priv_T) then
15728                Set_Primitive_Operations (Priv_T, Full_List);
15729                Set_Class_Wide_Type
15730                  (Base_Type (Full_T), Class_Wide_Type (Priv_T));
15731
15732                Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
15733             end if;
15734          end;
15735       end if;
15736
15737       --  Ada 2005 AI 161: Check preelaboratable initialization consistency
15738
15739       if Known_To_Have_Preelab_Init (Priv_T) then
15740
15741          --  Case where there is a pragma Preelaborable_Initialization. We
15742          --  always allow this in predefined units, which is a bit of a kludge,
15743          --  but it means we don't have to struggle to meet the requirements in
15744          --  the RM for having Preelaborable Initialization. Otherwise we
15745          --  require that the type meets the RM rules. But we can't check that
15746          --  yet, because of the rule about overriding Ininitialize, so we
15747          --  simply set a flag that will be checked at freeze time.
15748
15749          if not In_Predefined_Unit (Full_T) then
15750             Set_Must_Have_Preelab_Init (Full_T);
15751          end if;
15752       end if;
15753
15754       --  If pragma CPP_Class was applied to the private type declaration,
15755       --  propagate it now to the full type declaration.
15756
15757       if Is_CPP_Class (Priv_T) then
15758          Set_Is_CPP_Class (Full_T);
15759          Set_Convention   (Full_T, Convention_CPP);
15760       end if;
15761    end Process_Full_View;
15762
15763    -----------------------------------
15764    -- Process_Incomplete_Dependents --
15765    -----------------------------------
15766
15767    procedure Process_Incomplete_Dependents
15768      (N      : Node_Id;
15769       Full_T : Entity_Id;
15770       Inc_T  : Entity_Id)
15771    is
15772       Inc_Elmt : Elmt_Id;
15773       Priv_Dep : Entity_Id;
15774       New_Subt : Entity_Id;
15775
15776       Disc_Constraint : Elist_Id;
15777
15778    begin
15779       if No (Private_Dependents (Inc_T)) then
15780          return;
15781       end if;
15782
15783       --  Itypes that may be generated by the completion of an incomplete
15784       --  subtype are not used by the back-end and not attached to the tree.
15785       --  They are created only for constraint-checking purposes.
15786
15787       Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
15788       while Present (Inc_Elmt) loop
15789          Priv_Dep := Node (Inc_Elmt);
15790
15791          if Ekind (Priv_Dep) = E_Subprogram_Type then
15792
15793             --  An Access_To_Subprogram type may have a return type or a
15794             --  parameter type that is incomplete. Replace with the full view.
15795
15796             if Etype (Priv_Dep) = Inc_T then
15797                Set_Etype (Priv_Dep, Full_T);
15798             end if;
15799
15800             declare
15801                Formal : Entity_Id;
15802
15803             begin
15804                Formal := First_Formal (Priv_Dep);
15805                while Present (Formal) loop
15806                   if Etype (Formal) = Inc_T then
15807                      Set_Etype (Formal, Full_T);
15808                   end if;
15809
15810                   Next_Formal (Formal);
15811                end loop;
15812             end;
15813
15814          elsif Is_Overloadable (Priv_Dep) then
15815
15816             --  A protected operation is never dispatching: only its
15817             --  wrapper operation (which has convention Ada) is.
15818
15819             if Is_Tagged_Type (Full_T)
15820               and then Convention (Priv_Dep) /= Convention_Protected
15821             then
15822
15823                --  Subprogram has an access parameter whose designated type
15824                --  was incomplete. Reexamine declaration now, because it may
15825                --  be a primitive operation of the full type.
15826
15827                Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
15828                Set_Is_Dispatching_Operation (Priv_Dep);
15829                Check_Controlling_Formals (Full_T, Priv_Dep);
15830             end if;
15831
15832          elsif Ekind (Priv_Dep) = E_Subprogram_Body then
15833
15834             --  Can happen during processing of a body before the completion
15835             --  of a TA type. Ignore, because spec is also on dependent list.
15836
15837             return;
15838
15839          --  Ada 2005 (AI-412): Transform a regular incomplete subtype into a
15840          --  corresponding subtype of the full view.
15841
15842          elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
15843             Set_Subtype_Indication
15844               (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
15845             Set_Etype (Priv_Dep, Full_T);
15846             Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
15847             Set_Analyzed (Parent (Priv_Dep), False);
15848
15849             --  Reanalyze the declaration, suppressing the call to
15850             --  Enter_Name to avoid duplicate names.
15851
15852             Analyze_Subtype_Declaration
15853               (N    => Parent (Priv_Dep),
15854                Skip => True);
15855
15856          --  Dependent is a subtype
15857
15858          else
15859             --  We build a new subtype indication using the full view of the
15860             --  incomplete parent. The discriminant constraints have been
15861             --  elaborated already at the point of the subtype declaration.
15862
15863             New_Subt := Create_Itype (E_Void, N);
15864
15865             if Has_Discriminants (Full_T) then
15866                Disc_Constraint := Discriminant_Constraint (Priv_Dep);
15867             else
15868                Disc_Constraint := No_Elist;
15869             end if;
15870
15871             Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
15872             Set_Full_View (Priv_Dep, New_Subt);
15873          end if;
15874
15875          Next_Elmt (Inc_Elmt);
15876       end loop;
15877    end Process_Incomplete_Dependents;
15878
15879    --------------------------------
15880    -- Process_Range_Expr_In_Decl --
15881    --------------------------------
15882
15883    procedure Process_Range_Expr_In_Decl
15884      (R           : Node_Id;
15885       T           : Entity_Id;
15886       Check_List  : List_Id := Empty_List;
15887       R_Check_Off : Boolean := False)
15888    is
15889       Lo, Hi    : Node_Id;
15890       R_Checks  : Check_Result;
15891       Type_Decl : Node_Id;
15892       Def_Id    : Entity_Id;
15893
15894    begin
15895       Analyze_And_Resolve (R, Base_Type (T));
15896
15897       if Nkind (R) = N_Range then
15898          Lo := Low_Bound (R);
15899          Hi := High_Bound (R);
15900
15901          --  We need to ensure validity of the bounds here, because if we
15902          --  go ahead and do the expansion, then the expanded code will get
15903          --  analyzed with range checks suppressed and we miss the check.
15904
15905          Validity_Check_Range (R);
15906
15907          --  If there were errors in the declaration, try and patch up some
15908          --  common mistakes in the bounds. The cases handled are literals
15909          --  which are Integer where the expected type is Real and vice versa.
15910          --  These corrections allow the compilation process to proceed further
15911          --  along since some basic assumptions of the format of the bounds
15912          --  are guaranteed.
15913
15914          if Etype (R) = Any_Type then
15915
15916             if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
15917                Rewrite (Lo,
15918                  Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
15919
15920             elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
15921                Rewrite (Hi,
15922                  Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
15923
15924             elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
15925                Rewrite (Lo,
15926                  Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
15927
15928             elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
15929                Rewrite (Hi,
15930                  Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
15931             end if;
15932
15933             Set_Etype (Lo, T);
15934             Set_Etype (Hi, T);
15935          end if;
15936
15937          --  If the bounds of the range have been mistakenly given as string
15938          --  literals (perhaps in place of character literals), then an error
15939          --  has already been reported, but we rewrite the string literal as a
15940          --  bound of the range's type to avoid blowups in later processing
15941          --  that looks at static values.
15942
15943          if Nkind (Lo) = N_String_Literal then
15944             Rewrite (Lo,
15945               Make_Attribute_Reference (Sloc (Lo),
15946                 Attribute_Name => Name_First,
15947                 Prefix => New_Reference_To (T, Sloc (Lo))));
15948             Analyze_And_Resolve (Lo);
15949          end if;
15950
15951          if Nkind (Hi) = N_String_Literal then
15952             Rewrite (Hi,
15953               Make_Attribute_Reference (Sloc (Hi),
15954                 Attribute_Name => Name_First,
15955                 Prefix => New_Reference_To (T, Sloc (Hi))));
15956             Analyze_And_Resolve (Hi);
15957          end if;
15958
15959          --  If bounds aren't scalar at this point then exit, avoiding
15960          --  problems with further processing of the range in this procedure.
15961
15962          if not Is_Scalar_Type (Etype (Lo)) then
15963             return;
15964          end if;
15965
15966          --  Resolve (actually Sem_Eval) has checked that the bounds are in
15967          --  then range of the base type. Here we check whether the bounds
15968          --  are in the range of the subtype itself. Note that if the bounds
15969          --  represent the null range the Constraint_Error exception should
15970          --  not be raised.
15971
15972          --  ??? The following code should be cleaned up as follows
15973
15974          --  1. The Is_Null_Range (Lo, Hi) test should disappear since it
15975          --     is done in the call to Range_Check (R, T); below
15976
15977          --  2. The use of R_Check_Off should be investigated and possibly
15978          --     removed, this would clean up things a bit.
15979
15980          if Is_Null_Range (Lo, Hi) then
15981             null;
15982
15983          else
15984             --  Capture values of bounds and generate temporaries for them
15985             --  if needed, before applying checks, since checks may cause
15986             --  duplication of the expression without forcing evaluation.
15987
15988             if Expander_Active then
15989                Force_Evaluation (Lo);
15990                Force_Evaluation (Hi);
15991             end if;
15992
15993             --  We use a flag here instead of suppressing checks on the
15994             --  type because the type we check against isn't necessarily
15995             --  the place where we put the check.
15996
15997             if not R_Check_Off then
15998                R_Checks := Get_Range_Checks (R, T);
15999
16000                --  Look up tree to find an appropriate insertion point.
16001                --  This seems really junk code, and very brittle, couldn't
16002                --  we just use an insert actions call of some kind ???
16003
16004                Type_Decl := Parent (R);
16005                while Present (Type_Decl) and then not
16006                  (Nkind_In (Type_Decl, N_Full_Type_Declaration,
16007                                        N_Subtype_Declaration,
16008                                        N_Loop_Statement,
16009                                        N_Task_Type_Declaration)
16010                     or else
16011                   Nkind_In (Type_Decl, N_Single_Task_Declaration,
16012                                        N_Protected_Type_Declaration,
16013                                        N_Single_Protected_Declaration))
16014                loop
16015                   Type_Decl := Parent (Type_Decl);
16016                end loop;
16017
16018                --  Why would Type_Decl not be present???  Without this test,
16019                --  short regression tests fail.
16020
16021                if Present (Type_Decl) then
16022
16023                   --  Case of loop statement (more comments ???)
16024
16025                   if Nkind (Type_Decl) = N_Loop_Statement then
16026                      declare
16027                         Indic : Node_Id;
16028
16029                      begin
16030                         Indic := Parent (R);
16031                         while Present (Indic)
16032                           and then Nkind (Indic) /= N_Subtype_Indication
16033                         loop
16034                            Indic := Parent (Indic);
16035                         end loop;
16036
16037                         if Present (Indic) then
16038                            Def_Id := Etype (Subtype_Mark (Indic));
16039
16040                            Insert_Range_Checks
16041                              (R_Checks,
16042                               Type_Decl,
16043                               Def_Id,
16044                               Sloc (Type_Decl),
16045                               R,
16046                               Do_Before => True);
16047                         end if;
16048                      end;
16049
16050                   --  All other cases (more comments ???)
16051
16052                   else
16053                      Def_Id := Defining_Identifier (Type_Decl);
16054
16055                      if (Ekind (Def_Id) = E_Record_Type
16056                           and then Depends_On_Discriminant (R))
16057                        or else
16058                         (Ekind (Def_Id) = E_Protected_Type
16059                           and then Has_Discriminants (Def_Id))
16060                      then
16061                         Append_Range_Checks
16062                           (R_Checks, Check_List, Def_Id, Sloc (Type_Decl), R);
16063
16064                      else
16065                         Insert_Range_Checks
16066                           (R_Checks, Type_Decl, Def_Id, Sloc (Type_Decl), R);
16067
16068                      end if;
16069                   end if;
16070                end if;
16071             end if;
16072          end if;
16073
16074       elsif Expander_Active then
16075          Get_Index_Bounds (R, Lo, Hi);
16076          Force_Evaluation (Lo);
16077          Force_Evaluation (Hi);
16078       end if;
16079    end Process_Range_Expr_In_Decl;
16080
16081    --------------------------------------
16082    -- Process_Real_Range_Specification --
16083    --------------------------------------
16084
16085    procedure Process_Real_Range_Specification (Def : Node_Id) is
16086       Spec : constant Node_Id := Real_Range_Specification (Def);
16087       Lo   : Node_Id;
16088       Hi   : Node_Id;
16089       Err  : Boolean := False;
16090
16091       procedure Analyze_Bound (N : Node_Id);
16092       --  Analyze and check one bound
16093
16094       -------------------
16095       -- Analyze_Bound --
16096       -------------------
16097
16098       procedure Analyze_Bound (N : Node_Id) is
16099       begin
16100          Analyze_And_Resolve (N, Any_Real);
16101
16102          if not Is_OK_Static_Expression (N) then
16103             Flag_Non_Static_Expr
16104               ("bound in real type definition is not static!", N);
16105             Err := True;
16106          end if;
16107       end Analyze_Bound;
16108
16109    --  Start of processing for Process_Real_Range_Specification
16110
16111    begin
16112       if Present (Spec) then
16113          Lo := Low_Bound (Spec);
16114          Hi := High_Bound (Spec);
16115          Analyze_Bound (Lo);
16116          Analyze_Bound (Hi);
16117
16118          --  If error, clear away junk range specification
16119
16120          if Err then
16121             Set_Real_Range_Specification (Def, Empty);
16122          end if;
16123       end if;
16124    end Process_Real_Range_Specification;
16125
16126    ---------------------
16127    -- Process_Subtype --
16128    ---------------------
16129
16130    function Process_Subtype
16131      (S           : Node_Id;
16132       Related_Nod : Node_Id;
16133       Related_Id  : Entity_Id := Empty;
16134       Suffix      : Character := ' ') return Entity_Id
16135    is
16136       P               : Node_Id;
16137       Def_Id          : Entity_Id;
16138       Error_Node      : Node_Id;
16139       Full_View_Id    : Entity_Id;
16140       Subtype_Mark_Id : Entity_Id;
16141
16142       May_Have_Null_Exclusion : Boolean;
16143
16144       procedure Check_Incomplete (T : Entity_Id);
16145       --  Called to verify that an incomplete type is not used prematurely
16146
16147       ----------------------
16148       -- Check_Incomplete --
16149       ----------------------
16150
16151       procedure Check_Incomplete (T : Entity_Id) is
16152       begin
16153          --  Ada 2005 (AI-412): Incomplete subtypes are legal
16154
16155          if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
16156            and then
16157              not (Ada_Version >= Ada_05
16158                     and then
16159                        (Nkind (Parent (T)) = N_Subtype_Declaration
16160                           or else
16161                             (Nkind (Parent (T)) = N_Subtype_Indication
16162                                and then Nkind (Parent (Parent (T))) =
16163                                           N_Subtype_Declaration)))
16164          then
16165             Error_Msg_N ("invalid use of type before its full declaration", T);
16166          end if;
16167       end Check_Incomplete;
16168
16169    --  Start of processing for Process_Subtype
16170
16171    begin
16172       --  Case of no constraints present
16173
16174       if Nkind (S) /= N_Subtype_Indication then
16175          Find_Type (S);
16176          Check_Incomplete (S);
16177          P := Parent (S);
16178
16179          --  Ada 2005 (AI-231): Static check
16180
16181          if Ada_Version >= Ada_05
16182            and then Present (P)
16183            and then Null_Exclusion_Present (P)
16184            and then Nkind (P) /= N_Access_To_Object_Definition
16185            and then not Is_Access_Type (Entity (S))
16186          then
16187             Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
16188          end if;
16189
16190          --  The following is ugly, can't we have a range or even a flag???
16191
16192          May_Have_Null_Exclusion :=
16193            Nkind_In (P, N_Access_Definition,
16194                         N_Access_Function_Definition,
16195                         N_Access_Procedure_Definition,
16196                         N_Access_To_Object_Definition,
16197                         N_Allocator,
16198                         N_Component_Definition)
16199              or else
16200            Nkind_In (P, N_Derived_Type_Definition,
16201                         N_Discriminant_Specification,
16202                         N_Object_Declaration,
16203                         N_Parameter_Specification,
16204                         N_Subtype_Declaration);
16205
16206          --  Create an Itype that is a duplicate of Entity (S) but with the
16207          --  null-exclusion attribute
16208
16209          if May_Have_Null_Exclusion
16210            and then Is_Access_Type (Entity (S))
16211            and then Null_Exclusion_Present (P)
16212
16213             --  No need to check the case of an access to object definition.
16214             --  It is correct to define double not-null pointers.
16215
16216             --  Example:
16217             --     type Not_Null_Int_Ptr is not null access Integer;
16218             --     type Acc is not null access Not_Null_Int_Ptr;
16219
16220            and then Nkind (P) /= N_Access_To_Object_Definition
16221          then
16222             if Can_Never_Be_Null (Entity (S)) then
16223                case Nkind (Related_Nod) is
16224                   when N_Full_Type_Declaration =>
16225                      if Nkind (Type_Definition (Related_Nod))
16226                        in N_Array_Type_Definition
16227                      then
16228                         Error_Node :=
16229                           Subtype_Indication
16230                             (Component_Definition
16231                              (Type_Definition (Related_Nod)));
16232                      else
16233                         Error_Node :=
16234                           Subtype_Indication (Type_Definition (Related_Nod));
16235                      end if;
16236
16237                   when N_Subtype_Declaration =>
16238                      Error_Node := Subtype_Indication (Related_Nod);
16239
16240                   when N_Object_Declaration =>
16241                      Error_Node := Object_Definition (Related_Nod);
16242
16243                   when N_Component_Declaration =>
16244                      Error_Node :=
16245                        Subtype_Indication (Component_Definition (Related_Nod));
16246
16247                   when others =>
16248                      pragma Assert (False);
16249                      Error_Node := Related_Nod;
16250                end case;
16251
16252                Error_Msg_NE
16253                  ("`NOT NULL` not allowed (& already excludes null)",
16254                   Error_Node,
16255                   Entity (S));
16256             end if;
16257
16258             Set_Etype  (S,
16259               Create_Null_Excluding_Itype
16260                 (T           => Entity (S),
16261                  Related_Nod => P));
16262             Set_Entity (S, Etype (S));
16263          end if;
16264
16265          return Entity (S);
16266
16267       --  Case of constraint present, so that we have an N_Subtype_Indication
16268       --  node (this node is created only if constraints are present).
16269
16270       else
16271          Find_Type (Subtype_Mark (S));
16272
16273          if Nkind (Parent (S)) /= N_Access_To_Object_Definition
16274            and then not
16275             (Nkind (Parent (S)) = N_Subtype_Declaration
16276               and then Is_Itype (Defining_Identifier (Parent (S))))
16277          then
16278             Check_Incomplete (Subtype_Mark (S));
16279          end if;
16280
16281          P := Parent (S);
16282          Subtype_Mark_Id := Entity (Subtype_Mark (S));
16283
16284          --  Explicit subtype declaration case
16285
16286          if Nkind (P) = N_Subtype_Declaration then
16287             Def_Id := Defining_Identifier (P);
16288
16289          --  Explicit derived type definition case
16290
16291          elsif Nkind (P) = N_Derived_Type_Definition then
16292             Def_Id := Defining_Identifier (Parent (P));
16293
16294          --  Implicit case, the Def_Id must be created as an implicit type.
16295          --  The one exception arises in the case of concurrent types, array
16296          --  and access types, where other subsidiary implicit types may be
16297          --  created and must appear before the main implicit type. In these
16298          --  cases we leave Def_Id set to Empty as a signal that Create_Itype
16299          --  has not yet been called to create Def_Id.
16300
16301          else
16302             if Is_Array_Type (Subtype_Mark_Id)
16303               or else Is_Concurrent_Type (Subtype_Mark_Id)
16304               or else Is_Access_Type (Subtype_Mark_Id)
16305             then
16306                Def_Id := Empty;
16307
16308             --  For the other cases, we create a new unattached Itype,
16309             --  and set the indication to ensure it gets attached later.
16310
16311             else
16312                Def_Id :=
16313                  Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
16314             end if;
16315          end if;
16316
16317          --  If the kind of constraint is invalid for this kind of type,
16318          --  then give an error, and then pretend no constraint was given.
16319
16320          if not Is_Valid_Constraint_Kind
16321                    (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
16322          then
16323             Error_Msg_N
16324               ("incorrect constraint for this kind of type", Constraint (S));
16325
16326             Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16327
16328             --  Set Ekind of orphan itype, to prevent cascaded errors
16329
16330             if Present (Def_Id) then
16331                Set_Ekind (Def_Id, Ekind (Any_Type));
16332             end if;
16333
16334             --  Make recursive call, having got rid of the bogus constraint
16335
16336             return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
16337          end if;
16338
16339          --  Remaining processing depends on type
16340
16341          case Ekind (Subtype_Mark_Id) is
16342             when Access_Kind =>
16343                Constrain_Access (Def_Id, S, Related_Nod);
16344
16345                if Expander_Active
16346                  and then  Is_Itype (Designated_Type (Def_Id))
16347                  and then Nkind (Related_Nod) = N_Subtype_Declaration
16348                  and then not Is_Incomplete_Type (Designated_Type (Def_Id))
16349                then
16350                   Build_Itype_Reference
16351                     (Designated_Type (Def_Id), Related_Nod);
16352                end if;
16353
16354             when Array_Kind =>
16355                Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
16356
16357             when Decimal_Fixed_Point_Kind =>
16358                Constrain_Decimal (Def_Id, S);
16359
16360             when Enumeration_Kind =>
16361                Constrain_Enumeration (Def_Id, S);
16362
16363             when Ordinary_Fixed_Point_Kind =>
16364                Constrain_Ordinary_Fixed (Def_Id, S);
16365
16366             when Float_Kind =>
16367                Constrain_Float (Def_Id, S);
16368
16369             when Integer_Kind =>
16370                Constrain_Integer (Def_Id, S);
16371
16372             when E_Record_Type     |
16373                  E_Record_Subtype  |
16374                  Class_Wide_Kind   |
16375                  E_Incomplete_Type =>
16376                Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
16377
16378             when Private_Kind =>
16379                Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
16380                Set_Private_Dependents (Def_Id, New_Elmt_List);
16381
16382                --  In case of an invalid constraint prevent further processing
16383                --  since the type constructed is missing expected fields.
16384
16385                if Etype (Def_Id) = Any_Type then
16386                   return Def_Id;
16387                end if;
16388
16389                --  If the full view is that of a task with discriminants,
16390                --  we must constrain both the concurrent type and its
16391                --  corresponding record type. Otherwise we will just propagate
16392                --  the constraint to the full view, if available.
16393
16394                if Present (Full_View (Subtype_Mark_Id))
16395                  and then Has_Discriminants (Subtype_Mark_Id)
16396                  and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
16397                then
16398                   Full_View_Id :=
16399                     Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
16400
16401                   Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
16402                   Constrain_Concurrent (Full_View_Id, S,
16403                     Related_Nod, Related_Id, Suffix);
16404                   Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
16405                   Set_Full_View (Def_Id, Full_View_Id);
16406
16407                   --  Introduce an explicit reference to the private subtype,
16408                   --  to prevent scope anomalies in gigi if first use appears
16409                   --  in a nested context, e.g. a later function body.
16410                   --  Should this be generated in other contexts than a full
16411                   --  type declaration?
16412
16413                   if Is_Itype (Def_Id)
16414                     and then
16415                       Nkind (Parent (P)) = N_Full_Type_Declaration
16416                   then
16417                      Build_Itype_Reference (Def_Id, Parent (P));
16418                   end if;
16419
16420                else
16421                   Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
16422                end if;
16423
16424             when Concurrent_Kind  =>
16425                Constrain_Concurrent (Def_Id, S,
16426                  Related_Nod, Related_Id, Suffix);
16427
16428             when others =>
16429                Error_Msg_N ("invalid subtype mark in subtype indication", S);
16430          end case;
16431
16432          --  Size and Convention are always inherited from the base type
16433
16434          Set_Size_Info  (Def_Id,            (Subtype_Mark_Id));
16435          Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
16436
16437          return Def_Id;
16438       end if;
16439    end Process_Subtype;
16440
16441    ---------------------------------------
16442    -- Check_Anonymous_Access_Components --
16443    ---------------------------------------
16444
16445    procedure Check_Anonymous_Access_Components
16446       (Typ_Decl  : Node_Id;
16447        Typ       : Entity_Id;
16448        Prev      : Entity_Id;
16449        Comp_List : Node_Id)
16450    is
16451       Loc         : constant Source_Ptr := Sloc (Typ_Decl);
16452       Anon_Access : Entity_Id;
16453       Acc_Def     : Node_Id;
16454       Comp        : Node_Id;
16455       Comp_Def    : Node_Id;
16456       Decl        : Node_Id;
16457       Type_Def    : Node_Id;
16458
16459       procedure Build_Incomplete_Type_Declaration;
16460       --  If the record type contains components that include an access to the
16461       --  current record, then create an incomplete type declaration for the
16462       --  record, to be used as the designated type of the anonymous access.
16463       --  This is done only once, and only if there is no previous partial
16464       --  view of the type.
16465
16466       function Designates_T (Subt : Node_Id) return Boolean;
16467       --  Check whether a node designates the enclosing record type
16468
16469       function Mentions_T (Acc_Def : Node_Id) return Boolean;
16470       --  Check whether an access definition includes a reference to
16471       --  the enclosing record type. The reference can be a subtype mark
16472       --  in the access definition itself, a 'Class attribute reference, or
16473       --  recursively a reference appearing in a parameter specification
16474       --  or result definition of an access_to_subprogram definition.
16475
16476       --------------------------------------
16477       -- Build_Incomplete_Type_Declaration --
16478       --------------------------------------
16479
16480       procedure Build_Incomplete_Type_Declaration is
16481          Decl  : Node_Id;
16482          Inc_T : Entity_Id;
16483          H     : Entity_Id;
16484
16485       begin
16486          --  If there is a previous partial view, no need to create a new one
16487          --  If the partial view, given by Prev, is incomplete,  If Prev is
16488          --  a private declaration, full declaration is flagged accordingly.
16489
16490          if Prev /= Typ then
16491             if Tagged_Present (Type_Definition (Typ_Decl)) then
16492                Make_Class_Wide_Type (Prev);
16493                Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
16494                Set_Etype (Class_Wide_Type (Typ), Typ);
16495             end if;
16496
16497             return;
16498
16499          elsif Has_Private_Declaration (Typ) then
16500             return;
16501
16502          --  If there was a previous anonymous access type, the incomplete
16503          --  type declaration will have been created already.
16504
16505          elsif Present (Current_Entity (Typ))
16506            and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
16507            and then Full_View (Current_Entity (Typ)) = Typ
16508          then
16509             return;
16510
16511          else
16512             Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
16513             Decl  := Make_Incomplete_Type_Declaration (Loc, Inc_T);
16514
16515             --  Type has already been inserted into the current scope.
16516             --  Remove it, and add incomplete declaration for type, so
16517             --  that subsequent anonymous access types can use it.
16518             --  The entity is unchained from the homonym list and from
16519             --  immediate visibility. After analysis, the entity in the
16520             --  incomplete declaration becomes immediately visible in the
16521             --  record declaration that follows.
16522
16523             H := Current_Entity (Typ);
16524
16525             if H = Typ then
16526                Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
16527             else
16528                while Present (H)
16529                  and then Homonym (H) /= Typ
16530                loop
16531                   H := Homonym (Typ);
16532                end loop;
16533
16534                Set_Homonym (H, Homonym (Typ));
16535             end if;
16536
16537             Insert_Before (Typ_Decl, Decl);
16538             Analyze (Decl);
16539             Set_Full_View (Inc_T, Typ);
16540
16541             if (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
16542                  and then
16543                    Present
16544                      (Record_Extension_Part (Type_Definition (Typ_Decl))))
16545               or else Tagged_Present (Type_Definition (Typ_Decl))
16546             then
16547                --  Create a common class-wide type for both views, and set
16548                --  the etype of the class-wide type to the full view.
16549
16550                Make_Class_Wide_Type (Inc_T);
16551                Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
16552                Set_Etype (Class_Wide_Type (Typ), Typ);
16553             end if;
16554          end if;
16555       end Build_Incomplete_Type_Declaration;
16556
16557       ------------------
16558       -- Designates_T --
16559       ------------------
16560
16561       function Designates_T (Subt : Node_Id) return Boolean is
16562          Type_Id : constant Name_Id := Chars (Typ);
16563
16564          function Names_T (Nam : Node_Id) return Boolean;
16565          --  The record type has not been introduced in the current scope
16566          --  yet, so we must examine the name of the type itself, either
16567          --  an identifier T, or an expanded name of the form P.T, where
16568          --  P denotes the current scope.
16569
16570          -------------
16571          -- Names_T --
16572          -------------
16573
16574          function Names_T (Nam : Node_Id) return Boolean is
16575          begin
16576             if Nkind (Nam) = N_Identifier then
16577                return Chars (Nam) = Type_Id;
16578
16579             elsif Nkind (Nam) = N_Selected_Component then
16580                if Chars (Selector_Name (Nam)) = Type_Id then
16581                   if Nkind (Prefix (Nam)) = N_Identifier then
16582                      return Chars (Prefix (Nam)) = Chars (Current_Scope);
16583
16584                   elsif Nkind (Prefix (Nam)) = N_Selected_Component then
16585                      return Chars (Selector_Name (Prefix (Nam))) =
16586                             Chars (Current_Scope);
16587                   else
16588                      return False;
16589                   end if;
16590
16591                else
16592                   return False;
16593                end if;
16594
16595             else
16596                return False;
16597             end if;
16598          end Names_T;
16599
16600       --  Start of processing for Designates_T
16601
16602       begin
16603          if Nkind (Subt) = N_Identifier then
16604             return Chars (Subt) = Type_Id;
16605
16606             --  Reference can be through an expanded name which has not been
16607             --  analyzed yet, and which designates enclosing scopes.
16608
16609          elsif Nkind (Subt) = N_Selected_Component then
16610             if Names_T (Subt) then
16611                return True;
16612
16613             --  Otherwise it must denote an entity that is already visible.
16614             --  The access definition may name a subtype of the enclosing
16615             --  type, if there is a previous incomplete declaration for it.
16616
16617             else
16618                Find_Selected_Component (Subt);
16619                return
16620                  Is_Entity_Name (Subt)
16621                    and then Scope (Entity (Subt)) = Current_Scope
16622                    and then
16623                      (Chars (Base_Type (Entity (Subt))) = Type_Id
16624                        or else
16625                          (Is_Class_Wide_Type (Entity (Subt))
16626                            and then
16627                            Chars (Etype (Base_Type (Entity (Subt)))) =
16628                                                                   Type_Id));
16629             end if;
16630
16631          --  A reference to the current type may appear as the prefix of
16632          --  a 'Class attribute.
16633
16634          elsif Nkind (Subt) = N_Attribute_Reference
16635            and then Attribute_Name (Subt) = Name_Class
16636          then
16637             return Names_T (Prefix (Subt));
16638
16639          else
16640             return False;
16641          end if;
16642       end Designates_T;
16643
16644       ----------------
16645       -- Mentions_T --
16646       ----------------
16647
16648       function Mentions_T (Acc_Def : Node_Id) return Boolean is
16649          Param_Spec : Node_Id;
16650
16651          Acc_Subprg : constant Node_Id :=
16652                         Access_To_Subprogram_Definition (Acc_Def);
16653
16654       begin
16655          if No (Acc_Subprg) then
16656             return Designates_T (Subtype_Mark (Acc_Def));
16657          end if;
16658
16659          --  Component is an access_to_subprogram: examine its formals,
16660          --  and result definition in the case of an access_to_function.
16661
16662          Param_Spec := First (Parameter_Specifications (Acc_Subprg));
16663          while Present (Param_Spec) loop
16664             if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
16665               and then Mentions_T (Parameter_Type (Param_Spec))
16666             then
16667                return True;
16668
16669             elsif Designates_T (Parameter_Type (Param_Spec)) then
16670                return True;
16671             end if;
16672
16673             Next (Param_Spec);
16674          end loop;
16675
16676          if Nkind (Acc_Subprg) = N_Access_Function_Definition then
16677             if Nkind (Result_Definition (Acc_Subprg)) =
16678                  N_Access_Definition
16679             then
16680                return Mentions_T (Result_Definition (Acc_Subprg));
16681             else
16682                return Designates_T (Result_Definition (Acc_Subprg));
16683             end if;
16684          end if;
16685
16686          return False;
16687       end Mentions_T;
16688
16689    --  Start of processing for Check_Anonymous_Access_Components
16690
16691    begin
16692       if No (Comp_List) then
16693          return;
16694       end if;
16695
16696       Comp := First (Component_Items (Comp_List));
16697       while Present (Comp) loop
16698          if Nkind (Comp) = N_Component_Declaration
16699            and then Present
16700              (Access_Definition (Component_Definition (Comp)))
16701            and then
16702              Mentions_T (Access_Definition (Component_Definition (Comp)))
16703          then
16704             Comp_Def := Component_Definition (Comp);
16705             Acc_Def :=
16706               Access_To_Subprogram_Definition
16707                 (Access_Definition (Comp_Def));
16708
16709             Build_Incomplete_Type_Declaration;
16710             Anon_Access :=
16711               Make_Defining_Identifier (Loc,
16712                 Chars => New_Internal_Name ('S'));
16713
16714             --  Create a declaration for the anonymous access type: either
16715             --  an access_to_object or an access_to_subprogram.
16716
16717             if Present (Acc_Def) then
16718                if Nkind  (Acc_Def) = N_Access_Function_Definition then
16719                   Type_Def :=
16720                     Make_Access_Function_Definition (Loc,
16721                       Parameter_Specifications =>
16722                         Parameter_Specifications (Acc_Def),
16723                       Result_Definition => Result_Definition (Acc_Def));
16724                else
16725                   Type_Def :=
16726                     Make_Access_Procedure_Definition (Loc,
16727                       Parameter_Specifications =>
16728                         Parameter_Specifications (Acc_Def));
16729                end if;
16730
16731             else
16732                Type_Def :=
16733                  Make_Access_To_Object_Definition (Loc,
16734                    Subtype_Indication =>
16735                       Relocate_Node
16736                         (Subtype_Mark
16737                           (Access_Definition (Comp_Def))));
16738
16739                Set_Constant_Present
16740                  (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
16741                Set_All_Present
16742                  (Type_Def, All_Present (Access_Definition (Comp_Def)));
16743             end if;
16744
16745             Set_Null_Exclusion_Present
16746               (Type_Def,
16747                Null_Exclusion_Present (Access_Definition (Comp_Def)));
16748
16749             Decl :=
16750               Make_Full_Type_Declaration (Loc,
16751                 Defining_Identifier => Anon_Access,
16752                 Type_Definition     => Type_Def);
16753
16754             Insert_Before (Typ_Decl, Decl);
16755             Analyze (Decl);
16756
16757             --  If an access to object, Preserve entity of designated type,
16758             --  for ASIS use, before rewriting the component definition.
16759
16760             if No (Acc_Def) then
16761                declare
16762                   Desig : Entity_Id;
16763
16764                begin
16765                   Desig := Entity (Subtype_Indication (Type_Def));
16766
16767                   --  If the access definition is to the current  record,
16768                   --  the visible entity at this point is an  incomplete
16769                   --  type. Retrieve the full view to simplify  ASIS queries
16770
16771                   if Ekind (Desig) = E_Incomplete_Type then
16772                      Desig := Full_View (Desig);
16773                   end if;
16774
16775                   Set_Entity
16776                     (Subtype_Mark (Access_Definition  (Comp_Def)), Desig);
16777                end;
16778             end if;
16779
16780             Rewrite (Comp_Def,
16781               Make_Component_Definition (Loc,
16782                 Subtype_Indication =>
16783                New_Occurrence_Of (Anon_Access, Loc)));
16784
16785             if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
16786                Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
16787             else
16788                Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
16789             end if;
16790
16791             Set_Is_Local_Anonymous_Access (Anon_Access);
16792          end if;
16793
16794          Next (Comp);
16795       end loop;
16796
16797       if Present (Variant_Part (Comp_List)) then
16798          declare
16799             V : Node_Id;
16800          begin
16801             V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
16802             while Present (V) loop
16803                Check_Anonymous_Access_Components
16804                  (Typ_Decl, Typ, Prev, Component_List (V));
16805                Next_Non_Pragma (V);
16806             end loop;
16807          end;
16808       end if;
16809    end Check_Anonymous_Access_Components;
16810
16811    --------------------------------
16812    -- Preanalyze_Spec_Expression --
16813    --------------------------------
16814
16815    procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
16816       Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
16817    begin
16818       In_Spec_Expression := True;
16819       Preanalyze_And_Resolve (N, T);
16820       In_Spec_Expression := Save_In_Spec_Expression;
16821    end Preanalyze_Spec_Expression;
16822
16823    -----------------------------
16824    -- Record_Type_Declaration --
16825    -----------------------------
16826
16827    procedure Record_Type_Declaration
16828      (T    : Entity_Id;
16829       N    : Node_Id;
16830       Prev : Entity_Id)
16831    is
16832       Def       : constant Node_Id := Type_Definition (N);
16833       Is_Tagged : Boolean;
16834       Tag_Comp  : Entity_Id;
16835
16836    begin
16837       --  These flags must be initialized before calling Process_Discriminants
16838       --  because this routine makes use of them.
16839
16840       Set_Ekind               (T, E_Record_Type);
16841       Set_Etype               (T, T);
16842       Init_Size_Align         (T);
16843       Set_Abstract_Interfaces (T, No_Elist);
16844       Set_Stored_Constraint   (T, No_Elist);
16845
16846       --  Normal case
16847
16848       if Ada_Version < Ada_05
16849         or else not Interface_Present (Def)
16850       then
16851          --  The flag Is_Tagged_Type might have already been set by
16852          --  Find_Type_Name if it detected an error for declaration T. This
16853          --  arises in the case of private tagged types where the full view
16854          --  omits the word tagged.
16855
16856          Is_Tagged :=
16857            Tagged_Present (Def)
16858              or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
16859
16860          Set_Is_Tagged_Type      (T, Is_Tagged);
16861          Set_Is_Limited_Record   (T, Limited_Present (Def));
16862
16863          --  Type is abstract if full declaration carries keyword, or if
16864          --  previous partial view did.
16865
16866          Set_Is_Abstract_Type    (T, Is_Abstract_Type (T)
16867                                       or else Abstract_Present (Def));
16868
16869       else
16870          Is_Tagged := True;
16871          Analyze_Interface_Declaration (T, Def);
16872
16873          if Present (Discriminant_Specifications (N)) then
16874             Error_Msg_N
16875               ("interface types cannot have discriminants",
16876                 Defining_Identifier
16877                   (First (Discriminant_Specifications (N))));
16878          end if;
16879       end if;
16880
16881       --  First pass: if there are self-referential access components,
16882       --  create the required anonymous access type declarations, and if
16883       --  need be an incomplete type declaration for T itself.
16884
16885       Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
16886
16887       if Ada_Version >= Ada_05
16888         and then Present (Interface_List (Def))
16889       then
16890          Check_Abstract_Interfaces (N, Def);
16891
16892          declare
16893             Ifaces_List : Elist_Id;
16894
16895          begin
16896             --  Ada 2005 (AI-251): Collect the list of progenitors that are not
16897             --  already in the parents.
16898
16899             Collect_Abstract_Interfaces
16900               (T                         => T,
16901                Ifaces_List               => Ifaces_List,
16902                Exclude_Parent_Interfaces => True);
16903
16904             Set_Abstract_Interfaces (T, Ifaces_List);
16905          end;
16906       end if;
16907
16908       --  Records constitute a scope for the component declarations within.
16909       --  The scope is created prior to the processing of these declarations.
16910       --  Discriminants are processed first, so that they are visible when
16911       --  processing the other components. The Ekind of the record type itself
16912       --  is set to E_Record_Type (subtypes appear as E_Record_Subtype).
16913
16914       --  Enter record scope
16915
16916       Push_Scope (T);
16917
16918       --  If an incomplete or private type declaration was already given for
16919       --  the type, then this scope already exists, and the discriminants have
16920       --  been declared within. We must verify that the full declaration
16921       --  matches the incomplete one.
16922
16923       Check_Or_Process_Discriminants (N, T, Prev);
16924
16925       Set_Is_Constrained     (T, not Has_Discriminants (T));
16926       Set_Has_Delayed_Freeze (T, True);
16927
16928       --  For tagged types add a manually analyzed component corresponding
16929       --  to the component _tag, the corresponding piece of tree will be
16930       --  expanded as part of the freezing actions if it is not a CPP_Class.
16931
16932       if Is_Tagged then
16933
16934          --  Do not add the tag unless we are in expansion mode
16935
16936          if Expander_Active then
16937             Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
16938             Enter_Name (Tag_Comp);
16939
16940             Set_Ekind                     (Tag_Comp, E_Component);
16941             Set_Is_Tag                    (Tag_Comp);
16942             Set_Is_Aliased                (Tag_Comp);
16943             Set_Etype                     (Tag_Comp, RTE (RE_Tag));
16944             Set_DT_Entry_Count            (Tag_Comp, No_Uint);
16945             Set_Original_Record_Component (Tag_Comp, Tag_Comp);
16946             Init_Component_Location       (Tag_Comp);
16947
16948             --  Ada 2005 (AI-251): Addition of the Tag corresponding to all the
16949             --  implemented interfaces.
16950
16951             if Has_Abstract_Interfaces (T) then
16952                Add_Interface_Tag_Components (N, T);
16953             end if;
16954          end if;
16955
16956          Make_Class_Wide_Type (T);
16957          Set_Primitive_Operations (T, New_Elmt_List);
16958       end if;
16959
16960       --  We must suppress range checks when processing the components
16961       --  of a record in the presence of discriminants, since we don't
16962       --  want spurious checks to be generated during their analysis, but
16963       --  must reset the Suppress_Range_Checks flags after having processed
16964       --  the record definition.
16965
16966       --  Note: this is the only use of Kill_Range_Checks, and is a bit odd,
16967       --  couldn't we just use the normal range check suppression method here.
16968       --  That would seem cleaner ???
16969
16970       if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
16971          Set_Kill_Range_Checks (T, True);
16972          Record_Type_Definition (Def, Prev);
16973          Set_Kill_Range_Checks (T, False);
16974       else
16975          Record_Type_Definition (Def, Prev);
16976       end if;
16977
16978       --  Exit from record scope
16979
16980       End_Scope;
16981
16982       --  Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
16983       --  the implemented interfaces and associate them an aliased entity.
16984
16985       if Is_Tagged
16986         and then not Is_Empty_List (Interface_List (Def))
16987       then
16988          declare
16989             Ifaces_List : constant Elist_Id := New_Elmt_List;
16990          begin
16991             Derive_Interface_Subprograms (T, T, Ifaces_List);
16992          end;
16993       end if;
16994    end Record_Type_Declaration;
16995
16996    ----------------------------
16997    -- Record_Type_Definition --
16998    ----------------------------
16999
17000    procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
17001       Component          : Entity_Id;
17002       Ctrl_Components    : Boolean := False;
17003       Final_Storage_Only : Boolean;
17004       T                  : Entity_Id;
17005
17006    begin
17007       if Ekind (Prev_T) = E_Incomplete_Type then
17008          T := Full_View (Prev_T);
17009       else
17010          T := Prev_T;
17011       end if;
17012
17013       Final_Storage_Only := not Is_Controlled (T);
17014
17015       --  Ada 2005: check whether an explicit Limited is present in a derived
17016       --  type declaration.
17017
17018       if Nkind (Parent (Def)) = N_Derived_Type_Definition
17019         and then Limited_Present (Parent (Def))
17020       then
17021          Set_Is_Limited_Record (T);
17022       end if;
17023
17024       --  If the component list of a record type is defined by the reserved
17025       --  word null and there is no discriminant part, then the record type has
17026       --  no components and all records of the type are null records (RM 3.7)
17027       --  This procedure is also called to process the extension part of a
17028       --  record extension, in which case the current scope may have inherited
17029       --  components.
17030
17031       if No (Def)
17032         or else No (Component_List (Def))
17033         or else Null_Present (Component_List (Def))
17034       then
17035          null;
17036
17037       else
17038          Analyze_Declarations (Component_Items (Component_List (Def)));
17039
17040          if Present (Variant_Part (Component_List (Def))) then
17041             Analyze (Variant_Part (Component_List (Def)));
17042          end if;
17043       end if;
17044
17045       --  After completing the semantic analysis of the record definition,
17046       --  record components, both new and inherited, are accessible. Set their
17047       --  kind accordingly. Exclude malformed itypes from illegal declarations,
17048       --  whose Ekind may be void.
17049
17050       Component := First_Entity (Current_Scope);
17051       while Present (Component) loop
17052          if Ekind (Component) = E_Void
17053            and then not Is_Itype (Component)
17054          then
17055             Set_Ekind (Component, E_Component);
17056             Init_Component_Location (Component);
17057          end if;
17058
17059          if Has_Task (Etype (Component)) then
17060             Set_Has_Task (T);
17061          end if;
17062
17063          if Ekind (Component) /= E_Component then
17064             null;
17065
17066          elsif Has_Controlled_Component (Etype (Component))
17067            or else (Chars (Component) /= Name_uParent
17068                     and then Is_Controlled (Etype (Component)))
17069          then
17070             Set_Has_Controlled_Component (T, True);
17071             Final_Storage_Only := Final_Storage_Only
17072               and then Finalize_Storage_Only (Etype (Component));
17073             Ctrl_Components := True;
17074          end if;
17075
17076          Next_Entity (Component);
17077       end loop;
17078
17079       --  A Type is Finalize_Storage_Only only if all its controlled components
17080       --  are also.
17081
17082       if Ctrl_Components then
17083          Set_Finalize_Storage_Only (T, Final_Storage_Only);
17084       end if;
17085
17086       --  Place reference to end record on the proper entity, which may
17087       --  be a partial view.
17088
17089       if Present (Def) then
17090          Process_End_Label (Def, 'e', Prev_T);
17091       end if;
17092    end Record_Type_Definition;
17093
17094    ------------------------
17095    -- Replace_Components --
17096    ------------------------
17097
17098    procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
17099       function Process (N : Node_Id) return Traverse_Result;
17100
17101       -------------
17102       -- Process --
17103       -------------
17104
17105       function Process (N : Node_Id) return Traverse_Result is
17106          Comp : Entity_Id;
17107
17108       begin
17109          if Nkind (N) = N_Discriminant_Specification then
17110             Comp := First_Discriminant (Typ);
17111             while Present (Comp) loop
17112                if Chars (Comp) = Chars (Defining_Identifier (N)) then
17113                   Set_Defining_Identifier (N, Comp);
17114                   exit;
17115                end if;
17116
17117                Next_Discriminant (Comp);
17118             end loop;
17119
17120          elsif Nkind (N) = N_Component_Declaration then
17121             Comp := First_Component (Typ);
17122             while Present (Comp) loop
17123                if Chars (Comp) = Chars (Defining_Identifier (N)) then
17124                   Set_Defining_Identifier (N, Comp);
17125                   exit;
17126                end if;
17127
17128                Next_Component (Comp);
17129             end loop;
17130          end if;
17131
17132          return OK;
17133       end Process;
17134
17135       procedure Replace is new Traverse_Proc (Process);
17136
17137    --  Start of processing for Replace_Components
17138
17139    begin
17140       Replace (Decl);
17141    end Replace_Components;
17142
17143    -------------------------------
17144    -- Set_Completion_Referenced --
17145    -------------------------------
17146
17147    procedure Set_Completion_Referenced (E : Entity_Id) is
17148    begin
17149       --  If in main unit, mark entity that is a completion as referenced,
17150       --  warnings go on the partial view when needed.
17151
17152       if In_Extended_Main_Source_Unit (E) then
17153          Set_Referenced (E);
17154       end if;
17155    end Set_Completion_Referenced;
17156
17157    ---------------------
17158    -- Set_Fixed_Range --
17159    ---------------------
17160
17161    --  The range for fixed-point types is complicated by the fact that we
17162    --  do not know the exact end points at the time of the declaration. This
17163    --  is true for three reasons:
17164
17165    --     A size clause may affect the fudging of the end-points
17166    --     A small clause may affect the values of the end-points
17167    --     We try to include the end-points if it does not affect the size
17168
17169    --  This means that the actual end-points must be established at the point
17170    --  when the type is frozen. Meanwhile, we first narrow the range as
17171    --  permitted (so that it will fit if necessary in a small specified size),
17172    --  and then build a range subtree with these narrowed bounds.
17173
17174    --  Set_Fixed_Range constructs the range from real literal values, and sets
17175    --  the range as the Scalar_Range of the given fixed-point type entity.
17176
17177    --  The parent of this range is set to point to the entity so that it is
17178    --  properly hooked into the tree (unlike normal Scalar_Range entries for
17179    --  other scalar types, which are just pointers to the range in the
17180    --  original tree, this would otherwise be an orphan).
17181
17182    --  The tree is left unanalyzed. When the type is frozen, the processing
17183    --  in Freeze.Freeze_Fixed_Point_Type notices that the range is not
17184    --  analyzed, and uses this as an indication that it should complete
17185    --  work on the range (it will know the final small and size values).
17186
17187    procedure Set_Fixed_Range
17188      (E   : Entity_Id;
17189       Loc : Source_Ptr;
17190       Lo  : Ureal;
17191       Hi  : Ureal)
17192    is
17193       S : constant Node_Id :=
17194             Make_Range (Loc,
17195               Low_Bound  => Make_Real_Literal (Loc, Lo),
17196               High_Bound => Make_Real_Literal (Loc, Hi));
17197    begin
17198       Set_Scalar_Range (E, S);
17199       Set_Parent (S, E);
17200    end Set_Fixed_Range;
17201
17202    ----------------------------------
17203    -- Set_Scalar_Range_For_Subtype --
17204    ----------------------------------
17205
17206    procedure Set_Scalar_Range_For_Subtype
17207      (Def_Id : Entity_Id;
17208       R      : Node_Id;
17209       Subt   : Entity_Id)
17210    is
17211       Kind : constant Entity_Kind :=  Ekind (Def_Id);
17212
17213    begin
17214       Set_Scalar_Range (Def_Id, R);
17215
17216       --  We need to link the range into the tree before resolving it so
17217       --  that types that are referenced, including importantly the subtype
17218       --  itself, are properly frozen (Freeze_Expression requires that the
17219       --  expression be properly linked into the tree). Of course if it is
17220       --  already linked in, then we do not disturb the current link.
17221
17222       if No (Parent (R)) then
17223          Set_Parent (R, Def_Id);
17224       end if;
17225
17226       --  Reset the kind of the subtype during analysis of the range, to
17227       --  catch possible premature use in the bounds themselves.
17228
17229       Set_Ekind (Def_Id, E_Void);
17230       Process_Range_Expr_In_Decl (R, Subt);
17231       Set_Ekind (Def_Id, Kind);
17232    end Set_Scalar_Range_For_Subtype;
17233
17234    --------------------------------------------------------
17235    -- Set_Stored_Constraint_From_Discriminant_Constraint --
17236    --------------------------------------------------------
17237
17238    procedure Set_Stored_Constraint_From_Discriminant_Constraint
17239      (E : Entity_Id)
17240    is
17241    begin
17242       --  Make sure set if encountered during Expand_To_Stored_Constraint
17243
17244       Set_Stored_Constraint (E, No_Elist);
17245
17246       --  Give it the right value
17247
17248       if Is_Constrained (E) and then Has_Discriminants (E) then
17249          Set_Stored_Constraint (E,
17250            Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
17251       end if;
17252    end Set_Stored_Constraint_From_Discriminant_Constraint;
17253
17254    -------------------------------------
17255    -- Signed_Integer_Type_Declaration --
17256    -------------------------------------
17257
17258    procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17259       Implicit_Base : Entity_Id;
17260       Base_Typ      : Entity_Id;
17261       Lo_Val        : Uint;
17262       Hi_Val        : Uint;
17263       Errs          : Boolean := False;
17264       Lo            : Node_Id;
17265       Hi            : Node_Id;
17266
17267       function Can_Derive_From (E : Entity_Id) return Boolean;
17268       --  Determine whether given bounds allow derivation from specified type
17269
17270       procedure Check_Bound (Expr : Node_Id);
17271       --  Check bound to make sure it is integral and static. If not, post
17272       --  appropriate error message and set Errs flag
17273
17274       ---------------------
17275       -- Can_Derive_From --
17276       ---------------------
17277
17278       --  Note we check both bounds against both end values, to deal with
17279       --  strange types like ones with a range of 0 .. -12341234.
17280
17281       function Can_Derive_From (E : Entity_Id) return Boolean is
17282          Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
17283          Hi : constant Uint := Expr_Value (Type_High_Bound (E));
17284       begin
17285          return Lo <= Lo_Val and then Lo_Val <= Hi
17286                   and then
17287                 Lo <= Hi_Val and then Hi_Val <= Hi;
17288       end Can_Derive_From;
17289
17290       -----------------
17291       -- Check_Bound --
17292       -----------------
17293
17294       procedure Check_Bound (Expr : Node_Id) is
17295       begin
17296          --  If a range constraint is used as an integer type definition, each
17297          --  bound of the range must be defined by a static expression of some
17298          --  integer type, but the two bounds need not have the same integer
17299          --  type (Negative bounds are allowed.) (RM 3.5.4)
17300
17301          if not Is_Integer_Type (Etype (Expr)) then
17302             Error_Msg_N
17303               ("integer type definition bounds must be of integer type", Expr);
17304             Errs := True;
17305
17306          elsif not Is_OK_Static_Expression (Expr) then
17307             Flag_Non_Static_Expr
17308               ("non-static expression used for integer type bound!", Expr);
17309             Errs := True;
17310
17311          --  The bounds are folded into literals, and we set their type to be
17312          --  universal, to avoid typing difficulties: we cannot set the type
17313          --  of the literal to the new type, because this would be a forward
17314          --  reference for the back end,  and if the original type is user-
17315          --  defined this can lead to spurious semantic errors (e.g. 2928-003).
17316
17317          else
17318             if Is_Entity_Name (Expr) then
17319                Fold_Uint (Expr, Expr_Value (Expr), True);
17320             end if;
17321
17322             Set_Etype (Expr, Universal_Integer);
17323          end if;
17324       end Check_Bound;
17325
17326    --  Start of processing for Signed_Integer_Type_Declaration
17327
17328    begin
17329       --  Create an anonymous base type
17330
17331       Implicit_Base :=
17332         Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
17333
17334       --  Analyze and check the bounds, they can be of any integer type
17335
17336       Lo := Low_Bound (Def);
17337       Hi := High_Bound (Def);
17338
17339       --  Arbitrarily use Integer as the type if either bound had an error
17340
17341       if Hi = Error or else Lo = Error then
17342          Base_Typ := Any_Integer;
17343          Set_Error_Posted (T, True);
17344
17345       --  Here both bounds are OK expressions
17346
17347       else
17348          Analyze_And_Resolve (Lo, Any_Integer);
17349          Analyze_And_Resolve (Hi, Any_Integer);
17350
17351          Check_Bound (Lo);
17352          Check_Bound (Hi);
17353
17354          if Errs then
17355             Hi := Type_High_Bound (Standard_Long_Long_Integer);
17356             Lo := Type_Low_Bound (Standard_Long_Long_Integer);
17357          end if;
17358
17359          --  Find type to derive from
17360
17361          Lo_Val := Expr_Value (Lo);
17362          Hi_Val := Expr_Value (Hi);
17363
17364          if Can_Derive_From (Standard_Short_Short_Integer) then
17365             Base_Typ := Base_Type (Standard_Short_Short_Integer);
17366
17367          elsif Can_Derive_From (Standard_Short_Integer) then
17368             Base_Typ := Base_Type (Standard_Short_Integer);
17369
17370          elsif Can_Derive_From (Standard_Integer) then
17371             Base_Typ := Base_Type (Standard_Integer);
17372
17373          elsif Can_Derive_From (Standard_Long_Integer) then
17374             Base_Typ := Base_Type (Standard_Long_Integer);
17375
17376          elsif Can_Derive_From (Standard_Long_Long_Integer) then
17377             Base_Typ := Base_Type (Standard_Long_Long_Integer);
17378
17379          else
17380             Base_Typ := Base_Type (Standard_Long_Long_Integer);
17381             Error_Msg_N ("integer type definition bounds out of range", Def);
17382             Hi := Type_High_Bound (Standard_Long_Long_Integer);
17383             Lo := Type_Low_Bound (Standard_Long_Long_Integer);
17384          end if;
17385       end if;
17386
17387       --  Complete both implicit base and declared first subtype entities
17388
17389       Set_Etype          (Implicit_Base, Base_Typ);
17390       Set_Scalar_Range   (Implicit_Base, Scalar_Range   (Base_Typ));
17391       Set_Size_Info      (Implicit_Base,                (Base_Typ));
17392       Set_RM_Size        (Implicit_Base, RM_Size        (Base_Typ));
17393       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17394
17395       Set_Ekind          (T, E_Signed_Integer_Subtype);
17396       Set_Etype          (T, Implicit_Base);
17397
17398       Set_Size_Info      (T,                (Implicit_Base));
17399       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
17400       Set_Scalar_Range   (T, Def);
17401       Set_RM_Size        (T, UI_From_Int (Minimum_Size (T)));
17402       Set_Is_Constrained (T);
17403    end Signed_Integer_Type_Declaration;
17404
17405 end Sem_Ch3;