OSDN Git Service

2006-10-31 Bob Duff <duff@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_ch3.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              S E M _ C H 3                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2006, 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 2,  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 COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Atree;    use Atree;
28 with Checks;   use Checks;
29 with Debug;    use Debug;
30 with Elists;   use Elists;
31 with Einfo;    use Einfo;
32 with Errout;   use Errout;
33 with Eval_Fat; use Eval_Fat;
34 with Exp_Ch3;  use Exp_Ch3;
35 with Exp_Dist; use Exp_Dist;
36 with Exp_Tss;  use Exp_Tss;
37 with Exp_Util; use Exp_Util;
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    --  (ie 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
188    --  constraints in order of discriminant declarations. T is the
189    --  discriminated unconstrained type. Def is the N_Subtype_Indication node
190    --  where the discriminants constraints for T are specified. Derived_Def is
191    --  True if we are building the discriminant constraints in a derived type
192    --  definition of the form "type D (...) is new T (xxx)". In this case T is
193    --  the parent type and Def is the constraint "(xxx)" on T and this routine
194    --  sets the Corresponding_Discriminant field of the discriminants in the
195    --  derived type D to point to the corresponding discriminants in the parent
196    --  type T.
197
198    procedure Build_Discriminated_Subtype
199      (T           : Entity_Id;
200       Def_Id      : Entity_Id;
201       Elist       : Elist_Id;
202       Related_Nod : Node_Id;
203       For_Access  : Boolean := False);
204    --  Subsidiary procedure to Constrain_Discriminated_Type and to
205    --  Process_Incomplete_Dependents. Given
206    --
207    --     T (a possibly discriminated base type)
208    --     Def_Id (a very partially built subtype for T),
209    --
210    --  the call completes Def_Id to be the appropriate E_*_Subtype.
211    --
212    --  The Elist is the list of discriminant constraints if any (it is set to
213    --  No_Elist if T is not a discriminated type, and to an empty list if
214    --  T has discriminants but there are no discriminant constraints). The
215    --  Related_Nod is the same as Decl_Node in Create_Constrained_Components.
216    --  The For_Access says whether or not this subtype is really constraining
217    --  an access type. That is its sole purpose is the designated type of an
218    --  access type -- in which case a Private_Subtype Is_For_Access_Subtype
219    --  is built to avoid freezing T when the access subtype is frozen.
220
221    function Build_Scalar_Bound
222      (Bound : Node_Id;
223       Par_T : Entity_Id;
224       Der_T : Entity_Id) return Node_Id;
225    --  The bounds of a derived scalar type are conversions of the bounds of
226    --  the parent type. Optimize the representation if the bounds are literals.
227    --  Needs a more complete spec--what are the parameters exactly, and what
228    --  exactly is the returned value, and how is Bound affected???
229
230    procedure Build_Underlying_Full_View
231      (N   : Node_Id;
232       Typ : Entity_Id;
233       Par : Entity_Id);
234    --  If the completion of a private type is itself derived from a private
235    --  type, or if the full view of a private subtype is itself private, the
236    --  back-end has no way to compute the actual size of this type. We build
237    --  an internal subtype declaration of the proper parent type to convey
238    --  this information. This extra mechanism is needed because a full
239    --  view cannot itself have a full view (it would get clobbered during
240    --  view exchanges).
241
242    procedure Check_Access_Discriminant_Requires_Limited
243      (D   : Node_Id;
244       Loc : Node_Id);
245    --  Check the restriction that the type to which an access discriminant
246    --  belongs must be a concurrent type or a descendant of a type with
247    --  the reserved word 'limited' in its declaration.
248
249    procedure Check_Delta_Expression (E : Node_Id);
250    --  Check that the expression represented by E is suitable for use
251    --  as a delta expression, i.e. it is of real type and is static.
252
253    procedure Check_Digits_Expression (E : Node_Id);
254    --  Check that the expression represented by E is suitable for use as
255    --  a digits expression, i.e. it is of integer type, positive and static.
256
257    procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
258    --  Validate the initialization of an object declaration. T is the
259    --  required type, and Exp is the initialization expression.
260
261    procedure Check_Or_Process_Discriminants
262      (N    : Node_Id;
263       T    : Entity_Id;
264       Prev : Entity_Id := Empty);
265    --  If T is the full declaration of an incomplete or private type, check
266    --  the conformance of the discriminants, otherwise process them. Prev
267    --  is the entity of the partial declaration, if any.
268
269    procedure Check_Real_Bound (Bound : Node_Id);
270    --  Check given bound for being of real type and static. If not, post an
271    --  appropriate message, and rewrite the bound with the real literal zero.
272
273    procedure Constant_Redeclaration
274      (Id : Entity_Id;
275       N  : Node_Id;
276       T  : out Entity_Id);
277    --  Various checks on legality of full declaration of deferred constant.
278    --  Id is the entity for the redeclaration, N is the N_Object_Declaration,
279    --  node. The caller has not yet set any attributes of this entity.
280
281    procedure Convert_Scalar_Bounds
282      (N            : Node_Id;
283       Parent_Type  : Entity_Id;
284       Derived_Type : Entity_Id;
285       Loc          : Source_Ptr);
286    --  For derived scalar types, convert the bounds in the type definition
287    --  to the derived type, and complete their analysis. Given a constraint
288    --  of the form:
289    --                   ..  new T range Lo .. Hi;
290    --  Lo and Hi are analyzed and resolved with T'Base, the parent_type.
291    --  The bounds of the derived type (the anonymous base) are copies of
292    --  Lo and Hi. Finally, the bounds of the derived subtype are conversions
293    --  of those bounds to the derived_type, so that their typing is
294    --  consistent.
295
296    procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
297    --  Copies attributes from array base type T2 to array base type T1.
298    --  Copies only attributes that apply to base types, but not subtypes.
299
300    procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
301    --  Copies attributes from array subtype T2 to array subtype T1. Copies
302    --  attributes that apply to both subtypes and base types.
303
304    procedure Create_Constrained_Components
305      (Subt        : Entity_Id;
306       Decl_Node   : Node_Id;
307       Typ         : Entity_Id;
308       Constraints : Elist_Id);
309    --  Build the list of entities for a constrained discriminated record
310    --  subtype. If a component depends on a discriminant, replace its subtype
311    --  using the discriminant values in the discriminant constraint.
312    --  Subt is the defining identifier for the subtype whose list of
313    --  constrained entities we will create. Decl_Node is the type declaration
314    --  node where we will attach all the itypes created. Typ is the base
315    --  discriminated type for the subtype Subt. Constraints is the list of
316    --  discriminant constraints for Typ.
317
318    function Constrain_Component_Type
319      (Comp            : Entity_Id;
320       Constrained_Typ : Entity_Id;
321       Related_Node    : Node_Id;
322       Typ             : Entity_Id;
323       Constraints     : Elist_Id) return Entity_Id;
324    --  Given a discriminated base type Typ, a list of discriminant constraint
325    --  Constraints for Typ and a component of Typ, with type Compon_Type,
326    --  create and return the type corresponding to Compon_type where all
327    --  discriminant references are replaced with the corresponding
328    --  constraint. If no discriminant references occur in Compon_Typ then
329    --  return it as is. Constrained_Typ is the final constrained subtype to
330    --  which the constrained Compon_Type belongs. Related_Node is the node
331    --  where we will attach all the itypes created.
332
333    procedure Constrain_Access
334      (Def_Id      : in out Entity_Id;
335       S           : Node_Id;
336       Related_Nod : Node_Id);
337    --  Apply a list of constraints to an access type. If Def_Id is empty, it is
338    --  an anonymous type created for a subtype indication. In that case it is
339    --  created in the procedure and attached to Related_Nod.
340
341    procedure Constrain_Array
342      (Def_Id      : in out Entity_Id;
343       SI          : Node_Id;
344       Related_Nod : Node_Id;
345       Related_Id  : Entity_Id;
346       Suffix      : Character);
347    --  Apply a list of index constraints to an unconstrained array type. The
348    --  first parameter is the entity for the resulting subtype. A value of
349    --  Empty for Def_Id indicates that an implicit type must be created, but
350    --  creation is delayed (and must be done by this procedure) because other
351    --  subsidiary implicit types must be created first (which is why Def_Id
352    --  is an in/out parameter). The second parameter is a subtype indication
353    --  node for the constrained array to be created (e.g. something of the
354    --  form string (1 .. 10)). Related_Nod gives the place where this type
355    --  has to be inserted in the tree. The Related_Id and Suffix parameters
356    --  are used to build the associated Implicit type name.
357
358    procedure Constrain_Concurrent
359      (Def_Id      : in out Entity_Id;
360       SI          : Node_Id;
361       Related_Nod : Node_Id;
362       Related_Id  : Entity_Id;
363       Suffix      : Character);
364    --  Apply list of discriminant constraints to an unconstrained concurrent
365    --  type.
366    --
367    --    SI is the N_Subtype_Indication node containing the constraint and
368    --    the unconstrained type to constrain.
369    --
370    --    Def_Id is the entity for the resulting constrained subtype. A value
371    --    of Empty for Def_Id indicates that an implicit type must be created,
372    --    but creation is delayed (and must be done by this procedure) because
373    --    other subsidiary implicit types must be created first (which is why
374    --    Def_Id is an in/out parameter).
375    --
376    --    Related_Nod gives the place where this type has to be inserted
377    --    in the tree
378    --
379    --  The last two arguments are used to create its external name if needed.
380
381    function Constrain_Corresponding_Record
382      (Prot_Subt   : Entity_Id;
383       Corr_Rec    : Entity_Id;
384       Related_Nod : Node_Id;
385       Related_Id  : Entity_Id) return Entity_Id;
386    --  When constraining a protected type or task type with discriminants,
387    --  constrain the corresponding record with the same discriminant values.
388
389    procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
390    --  Constrain a decimal fixed point type with a digits constraint and/or a
391    --  range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
392
393    procedure Constrain_Discriminated_Type
394      (Def_Id      : Entity_Id;
395       S           : Node_Id;
396       Related_Nod : Node_Id;
397       For_Access  : Boolean := False);
398    --  Process discriminant constraints of composite type. Verify that values
399    --  have been provided for all discriminants, that the original type is
400    --  unconstrained, and that the types of the supplied expressions match
401    --  the discriminant types. The first three parameters are like in routine
402    --  Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
403    --  of For_Access.
404
405    procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
406    --  Constrain an enumeration type with a range constraint. This is identical
407    --  to Constrain_Integer, but for the Ekind of the resulting subtype.
408
409    procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
410    --  Constrain a floating point type with either a digits constraint
411    --  and/or a range constraint, building a E_Floating_Point_Subtype.
412
413    procedure Constrain_Index
414      (Index        : Node_Id;
415       S            : Node_Id;
416       Related_Nod  : Node_Id;
417       Related_Id   : Entity_Id;
418       Suffix       : Character;
419       Suffix_Index : Nat);
420    --  Process an index constraint in a constrained array declaration. The
421    --  constraint can be a subtype name, or a range with or without an
422    --  explicit subtype mark. The index is the corresponding index of the
423    --  unconstrained array. The Related_Id and Suffix parameters are used to
424    --  build the associated Implicit type name.
425
426    procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
427    --  Build subtype of a signed or modular integer type
428
429    procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
430    --  Constrain an ordinary fixed point type with a range constraint, and
431    --  build an E_Ordinary_Fixed_Point_Subtype entity.
432
433    procedure Copy_And_Swap (Priv, Full : Entity_Id);
434    --  Copy the Priv entity into the entity of its full declaration
435    --  then swap the two entities in such a manner that the former private
436    --  type is now seen as a full type.
437
438    procedure Decimal_Fixed_Point_Type_Declaration
439      (T   : Entity_Id;
440       Def : Node_Id);
441    --  Create a new decimal fixed point type, and apply the constraint to
442    --  obtain a subtype of this new type.
443
444    procedure Complete_Private_Subtype
445      (Priv        : Entity_Id;
446       Full        : Entity_Id;
447       Full_Base   : Entity_Id;
448       Related_Nod : Node_Id);
449    --  Complete the implicit full view of a private subtype by setting the
450    --  appropriate semantic fields. If the full view of the parent is a record
451    --  type, build constrained components of subtype.
452
453    procedure Derive_Interface_Subprograms
454      (Parent_Type : Entity_Id;
455       Tagged_Type : Entity_Id;
456       Ifaces_List : Elist_Id);
457    --  Ada 2005 (AI-251): Derive primitives of abstract interface types that
458    --  are not immediate ancestors of Tagged type and associate them their
459    --  aliased primitive. Ifaces_List contains the abstract interface
460    --  primitives that have been derived from Parent_Type.
461
462    procedure Derived_Standard_Character
463      (N             : Node_Id;
464       Parent_Type   : Entity_Id;
465       Derived_Type  : Entity_Id);
466    --  Subsidiary procedure to Build_Derived_Enumeration_Type which handles
467    --  derivations from types Standard.Character and Standard.Wide_Character.
468
469    procedure Derived_Type_Declaration
470      (T             : Entity_Id;
471       N             : Node_Id;
472       Is_Completion : Boolean);
473    --  Process a derived type declaration. This routine will invoke
474    --  Build_Derived_Type to process the actual derived type definition.
475    --  Parameters N and Is_Completion have the same meaning as in
476    --  Build_Derived_Type. T is the N_Defining_Identifier for the entity
477    --  defined in the N_Full_Type_Declaration node N, that is T is the derived
478    --  type.
479
480    procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
481    --  Insert each literal in symbol table, as an overloadable identifier. Each
482    --  enumeration type is mapped into a sequence of integers, and each literal
483    --  is defined as a constant with integer value. If any of the literals are
484    --  character literals, the type is a character type, which means that
485    --  strings are legal aggregates for arrays of components of the type.
486
487    function Expand_To_Stored_Constraint
488      (Typ        : Entity_Id;
489       Constraint : Elist_Id) return Elist_Id;
490    --  Given a Constraint (i.e. a list of expressions) on the discriminants of
491    --  Typ, expand it into a constraint on the stored discriminants and return
492    --  the new list of expressions constraining the stored discriminants.
493
494    function Find_Type_Of_Object
495      (Obj_Def     : Node_Id;
496       Related_Nod : Node_Id) return Entity_Id;
497    --  Get type entity for object referenced by Obj_Def, attaching the
498    --  implicit types generated to Related_Nod
499
500    procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
501    --  Create a new float, and apply the constraint to obtain subtype of it
502
503    function Has_Range_Constraint (N : Node_Id) return Boolean;
504    --  Given an N_Subtype_Indication node N, return True if a range constraint
505    --  is present, either directly, or as part of a digits or delta constraint.
506    --  In addition, a digits constraint in the decimal case returns True, since
507    --  it establishes a default range if no explicit range is present.
508
509    function Inherit_Components
510      (N             : Node_Id;
511       Parent_Base   : Entity_Id;
512       Derived_Base  : Entity_Id;
513       Is_Tagged     : Boolean;
514       Inherit_Discr : Boolean;
515       Discs         : Elist_Id) return Elist_Id;
516    --  Called from Build_Derived_Record_Type to inherit the components of
517    --  Parent_Base (a base type) into the Derived_Base (the derived base type).
518    --  For more information on derived types and component inheritance please
519    --  consult the comment above the body of Build_Derived_Record_Type.
520    --
521    --    N is the original derived type declaration
522    --
523    --    Is_Tagged is set if we are dealing with tagged types
524    --
525    --    If Inherit_Discr is set, Derived_Base inherits its discriminants
526    --    from Parent_Base, otherwise no discriminants are inherited.
527    --
528    --    Discs gives the list of constraints that apply to Parent_Base in the
529    --    derived type declaration. If Discs is set to No_Elist, then we have
530    --    the following situation:
531    --
532    --      type Parent (D1..Dn : ..) is [tagged] record ...;
533    --      type Derived is new Parent [with ...];
534    --
535    --    which gets treated as
536    --
537    --      type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
538    --
539    --  For untagged types the returned value is an association list. The list
540    --  starts from the association (Parent_Base => Derived_Base), and then it
541    --  contains a sequence of the associations of the form
542    --
543    --    (Old_Component => New_Component),
544    --
545    --  where Old_Component is the Entity_Id of a component in Parent_Base
546    --  and New_Component is the Entity_Id of the corresponding component in
547    --  Derived_Base. For untagged records, this association list is needed when
548    --  copying the record declaration for the derived base. In the tagged case
549    --  the value returned is irrelevant.
550
551    function Is_Valid_Constraint_Kind
552      (T_Kind          : Type_Kind;
553       Constraint_Kind : Node_Kind) return Boolean;
554    --  Returns True if it is legal to apply the given kind of constraint to the
555    --  given kind of type (index constraint to an array type, for example).
556
557    procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
558    --  Create new modular type. Verify that modulus is in  bounds and is
559    --  a power of two (implementation restriction).
560
561    procedure New_Concatenation_Op (Typ : Entity_Id);
562    --  Create an abbreviated declaration for an operator in order to
563    --  materialize concatenation on array types.
564
565    procedure Ordinary_Fixed_Point_Type_Declaration
566      (T   : Entity_Id;
567       Def : Node_Id);
568    --  Create a new ordinary fixed point type, and apply the constraint to
569    --  obtain subtype of it.
570
571    procedure Prepare_Private_Subtype_Completion
572      (Id          : Entity_Id;
573       Related_Nod : Node_Id);
574    --  Id is a subtype of some private type. Creates the full declaration
575    --  associated with Id whenever possible, i.e. when the full declaration
576    --  of the base type is already known. Records each subtype into
577    --  Private_Dependents of the base type.
578
579    procedure Process_Incomplete_Dependents
580      (N      : Node_Id;
581       Full_T : Entity_Id;
582       Inc_T  : Entity_Id);
583    --  Process all entities that depend on an incomplete type. There include
584    --  subtypes, subprogram types that mention the incomplete type in their
585    --  profiles, and subprogram with access parameters that designate the
586    --  incomplete type.
587
588    --  Inc_T is the defining identifier of an incomplete type declaration, its
589    --  Ekind is E_Incomplete_Type.
590    --
591    --    N is the corresponding N_Full_Type_Declaration for Inc_T.
592    --
593    --    Full_T is N's defining identifier.
594    --
595    --  Subtypes of incomplete types with discriminants are completed when the
596    --  parent type is. This is simpler than private subtypes, because they can
597    --  only appear in the same scope, and there is no need to exchange views.
598    --  Similarly, access_to_subprogram types may have a parameter or a return
599    --  type that is an incomplete type, and that must be replaced with the
600    --  full type.
601
602    --  If the full type is tagged, subprogram with access parameters that
603    --  designated the incomplete may be primitive operations of the full type,
604    --  and have to be processed accordingly.
605
606    procedure Process_Real_Range_Specification (Def : Node_Id);
607    --  Given the type definition for a real type, this procedure processes
608    --  and checks the real range specification of this type definition if
609    --  one is present. If errors are found, error messages are posted, and
610    --  the Real_Range_Specification of Def is reset to Empty.
611
612    procedure Record_Type_Declaration
613      (T    : Entity_Id;
614       N    : Node_Id;
615       Prev : Entity_Id);
616    --  Process a record type declaration (for both untagged and tagged
617    --  records). Parameters T and N are exactly like in procedure
618    --  Derived_Type_Declaration, except that no flag Is_Completion is needed
619    --  for this routine. If this is the completion of an incomplete type
620    --  declaration, Prev is the entity of the incomplete declaration, used for
621    --  cross-referencing. Otherwise Prev = T.
622
623    procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
624    --  This routine is used to process the actual record type definition
625    --  (both for untagged and tagged records). Def is a record type
626    --  definition node. This procedure analyzes the components in this
627    --  record type definition. Prev_T is the entity for the enclosing record
628    --  type. It is provided so that its Has_Task flag can be set if any of
629    --  the component have Has_Task set. If the declaration is the completion
630    --  of an incomplete type declaration, Prev_T is the original incomplete
631    --  type, whose full view is the record type.
632
633    procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
634    --  Subsidiary to Build_Derived_Record_Type. For untagged records, we
635    --  build a copy of the declaration tree of the parent, and we create
636    --  independently the list of components for the derived type. Semantic
637    --  information uses the component entities, but record representation
638    --  clauses are validated on the declaration tree. This procedure replaces
639    --  discriminants and components in the declaration with those that have
640    --  been created by Inherit_Components.
641
642    procedure Set_Fixed_Range
643      (E   : Entity_Id;
644       Loc : Source_Ptr;
645       Lo  : Ureal;
646       Hi  : Ureal);
647    --  Build a range node with the given bounds and set it as the Scalar_Range
648    --  of the given fixed-point type entity. Loc is the source location used
649    --  for the constructed range. See body for further details.
650
651    procedure Set_Scalar_Range_For_Subtype
652      (Def_Id : Entity_Id;
653       R      : Node_Id;
654       Subt   : Entity_Id);
655    --  This routine is used to set the scalar range field for a subtype given
656    --  Def_Id, the entity for the subtype, and R, the range expression for the
657    --  scalar range. Subt provides the parent subtype to be used to analyze,
658    --  resolve, and check the given range.
659
660    procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
661    --  Create a new signed integer entity, and apply the constraint to obtain
662    --  the required first named subtype of this type.
663
664    procedure Set_Stored_Constraint_From_Discriminant_Constraint
665      (E : Entity_Id);
666    --  E is some record type. This routine computes E's Stored_Constraint
667    --  from its Discriminant_Constraint.
668
669    -----------------------
670    -- Access_Definition --
671    -----------------------
672
673    function Access_Definition
674      (Related_Nod : Node_Id;
675       N           : Node_Id) return Entity_Id
676    is
677       Loc        : constant Source_Ptr := Sloc (Related_Nod);
678       Anon_Type  : Entity_Id;
679       Desig_Type : Entity_Id;
680       Decl       : Entity_Id;
681
682    begin
683       if Is_Entry (Current_Scope)
684         and then Is_Task_Type (Etype (Scope (Current_Scope)))
685       then
686          Error_Msg_N ("task entries cannot have access parameters", N);
687       end if;
688
689       --  Ada 2005: for an object declaration the corresponding anonymous
690       --  type is declared in the current scope.
691
692       --  If the access definition is the return type of another access to
693       --  function, scope is the current one, because it is the one of the
694       --  current type declaration.
695
696       if Nkind (Related_Nod) = N_Object_Declaration
697         or else Nkind (Related_Nod) = N_Access_Function_Definition
698       then
699          Anon_Type :=
700            Create_Itype
701             (E_Anonymous_Access_Type, Related_Nod,
702                Scope_Id => Current_Scope);
703
704       --  For the anonymous function result case, retrieve the scope of
705       --  the function specification's associated entity rather than using
706       --  the current scope. The current scope will be the function itself
707       --  if the formal part is currently being analyzed, but will be the
708       --  parent scope in the case of a parameterless function, and we
709       --  always want to use the function's parent scope.
710
711       elsif Nkind (Related_Nod) = N_Function_Specification
712          and then Nkind (Parent (N)) /= N_Parameter_Specification
713       then
714          Anon_Type :=
715            Create_Itype
716             (E_Anonymous_Access_Type, Related_Nod,
717                Scope_Id => Scope (Defining_Unit_Name (Related_Nod)));
718
719       else
720          --  For access formals, access components, and access
721          --  discriminants, the scope is that of the enclosing declaration,
722
723          Anon_Type :=
724            Create_Itype
725             (E_Anonymous_Access_Type, Related_Nod,
726                Scope_Id => Scope (Current_Scope));
727       end if;
728
729       if All_Present (N)
730         and then Ada_Version >= Ada_05
731       then
732          Error_Msg_N ("ALL is not permitted for anonymous access types", N);
733       end if;
734
735       --  Ada 2005 (AI-254): In case of anonymous access to subprograms
736       --  call the corresponding semantic routine
737
738       if Present (Access_To_Subprogram_Definition (N)) then
739          Access_Subprogram_Declaration
740            (T_Name => Anon_Type,
741             T_Def  => Access_To_Subprogram_Definition (N));
742
743          if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
744             Set_Ekind
745               (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
746          else
747             Set_Ekind
748               (Anon_Type, E_Anonymous_Access_Subprogram_Type);
749          end if;
750
751          return Anon_Type;
752       end if;
753
754       Find_Type (Subtype_Mark (N));
755       Desig_Type := Entity (Subtype_Mark (N));
756
757       Set_Directly_Designated_Type
758                              (Anon_Type, Desig_Type);
759       Set_Etype              (Anon_Type, Anon_Type);
760       Init_Size_Align        (Anon_Type);
761       Set_Depends_On_Private (Anon_Type, Has_Private_Component (Anon_Type));
762
763       --  Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
764       --  from Ada 95 semantics. In Ada 2005, anonymous access must specify
765       --  if the null value is allowed. In Ada 95 the null value is never
766       --  allowed.
767
768       if Ada_Version >= Ada_05 then
769          Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
770       else
771          Set_Can_Never_Be_Null (Anon_Type, True);
772       end if;
773
774       --  The anonymous access type is as public as the discriminated type or
775       --  subprogram that defines it. It is imported (for back-end purposes)
776       --  if the designated type is.
777
778       Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
779
780       --  Ada 2005 (AI-50217): Propagate the attribute that indicates that the
781       --  designated type comes from the limited view (for back-end purposes).
782
783       Set_From_With_Type (Anon_Type, From_With_Type (Desig_Type));
784
785       --  Ada 2005 (AI-231): Propagate the access-constant attribute
786
787       Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
788
789       --  The context is either a subprogram declaration, object declaration,
790       --  or an access discriminant, in a private or a full type declaration.
791       --  In the case of a subprogram, if the designated type is incomplete,
792       --  the operation will be a primitive operation of the full type, to be
793       --  updated subsequently. If the type is imported through a limited_with
794       --  clause, the subprogram is not a primitive operation of the type
795       --  (which is declared elsewhere in some other scope).
796
797       if Ekind (Desig_Type) = E_Incomplete_Type
798         and then not From_With_Type (Desig_Type)
799         and then Is_Overloadable (Current_Scope)
800       then
801          Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
802          Set_Has_Delayed_Freeze (Current_Scope);
803       end if;
804
805       --  Ada 2005: if the designated type is an interface that may contain
806       --  tasks, create a Master entity for the declaration. This must be done
807       --  before expansion of the full declaration, because the declaration
808       --  may include an expression that is an allocator, whose expansion needs
809       --  the proper Master for the created tasks.
810
811       if Nkind (Related_Nod) = N_Object_Declaration
812          and then Expander_Active
813       then
814          if Is_Interface (Desig_Type)
815            and then Is_Limited_Record (Desig_Type)
816          then
817             Build_Class_Wide_Master (Anon_Type);
818
819          --  Similarly, if the type is an anonymous access that designates
820          --  tasks, create a master entity for it in the current context.
821
822          elsif Has_Task (Desig_Type)
823            and then Comes_From_Source (Related_Nod)
824          then
825             if not Has_Master_Entity (Current_Scope) then
826                Decl :=
827                  Make_Object_Declaration (Loc,
828                    Defining_Identifier =>
829                      Make_Defining_Identifier (Loc, Name_uMaster),
830                    Constant_Present => True,
831                    Object_Definition =>
832                      New_Reference_To (RTE (RE_Master_Id), Loc),
833                    Expression =>
834                      Make_Explicit_Dereference (Loc,
835                        New_Reference_To (RTE (RE_Current_Master), Loc)));
836
837                Insert_Before (Related_Nod, Decl);
838                Analyze (Decl);
839
840                Set_Master_Id (Anon_Type, Defining_Identifier (Decl));
841                Set_Has_Master_Entity (Current_Scope);
842             else
843                Build_Master_Renaming (Related_Nod, Anon_Type);
844             end if;
845          end if;
846       end if;
847
848       return Anon_Type;
849    end Access_Definition;
850
851    -----------------------------------
852    -- Access_Subprogram_Declaration --
853    -----------------------------------
854
855    procedure Access_Subprogram_Declaration
856      (T_Name : Entity_Id;
857       T_Def  : Node_Id)
858    is
859       Formals : constant List_Id := Parameter_Specifications (T_Def);
860       Formal  : Entity_Id;
861       D_Ityp  : Node_Id;
862
863       Desig_Type : constant Entity_Id :=
864                      Create_Itype (E_Subprogram_Type, Parent (T_Def));
865
866    begin
867       --  Associate the Itype node with the inner full-type declaration
868       --  or subprogram spec. This is required to handle nested anonymous
869       --  declarations. For example:
870
871       --      procedure P
872       --       (X : access procedure
873       --                     (Y : access procedure
874       --                                   (Z : access T)))
875
876       D_Ityp := Associated_Node_For_Itype (Desig_Type);
877       while Nkind (D_Ityp) /= N_Full_Type_Declaration
878          and then Nkind (D_Ityp) /= N_Procedure_Specification
879          and then Nkind (D_Ityp) /= N_Function_Specification
880          and then Nkind (D_Ityp) /= N_Object_Declaration
881          and then Nkind (D_Ityp) /= N_Object_Renaming_Declaration
882          and then Nkind (D_Ityp) /= N_Formal_Type_Declaration
883       loop
884          D_Ityp := Parent (D_Ityp);
885          pragma Assert (D_Ityp /= Empty);
886       end loop;
887
888       Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
889
890       if Nkind (D_Ityp) = N_Procedure_Specification
891         or else Nkind (D_Ityp) = N_Function_Specification
892       then
893          Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
894
895       elsif Nkind (D_Ityp) = N_Full_Type_Declaration
896         or else Nkind (D_Ityp) = N_Object_Declaration
897         or else Nkind (D_Ityp) = N_Object_Renaming_Declaration
898         or else Nkind (D_Ityp) = N_Formal_Type_Declaration
899       then
900          Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
901       end if;
902
903       if Nkind (T_Def) = N_Access_Function_Definition then
904          if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
905             Set_Etype
906               (Desig_Type,
907                Access_Definition (T_Def, Result_Definition (T_Def)));
908          else
909             Analyze (Result_Definition (T_Def));
910             Set_Etype (Desig_Type, Entity (Result_Definition (T_Def)));
911          end if;
912
913          if not (Is_Type (Etype (Desig_Type))) then
914             Error_Msg_N
915               ("expect type in function specification",
916                Result_Definition (T_Def));
917          end if;
918
919       else
920          Set_Etype (Desig_Type, Standard_Void_Type);
921       end if;
922
923       if Present (Formals) then
924          New_Scope (Desig_Type);
925          Process_Formals (Formals, Parent (T_Def));
926
927          --  A bit of a kludge here, End_Scope requires that the parent
928          --  pointer be set to something reasonable, but Itypes don't have
929          --  parent pointers. So we set it and then unset it ??? If and when
930          --  Itypes have proper parent pointers to their declarations, this
931          --  kludge can be removed.
932
933          Set_Parent (Desig_Type, T_Name);
934          End_Scope;
935          Set_Parent (Desig_Type, Empty);
936       end if;
937
938       --  The return type and/or any parameter type may be incomplete. Mark
939       --  the subprogram_type as depending on the incomplete type, so that
940       --  it can be updated when the full type declaration is seen.
941
942       if Present (Formals) then
943          Formal := First_Formal (Desig_Type);
944          while Present (Formal) loop
945             if Ekind (Formal) /= E_In_Parameter
946               and then Nkind (T_Def) = N_Access_Function_Definition
947             then
948                Error_Msg_N ("functions can only have IN parameters", Formal);
949             end if;
950
951             if Ekind (Etype (Formal)) = E_Incomplete_Type then
952                Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
953                Set_Has_Delayed_Freeze (Desig_Type);
954             end if;
955
956             Next_Formal (Formal);
957          end loop;
958       end if;
959
960       if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
961         and then not Has_Delayed_Freeze (Desig_Type)
962       then
963          Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
964          Set_Has_Delayed_Freeze (Desig_Type);
965       end if;
966
967       Check_Delayed_Subprogram (Desig_Type);
968
969       if Protected_Present (T_Def) then
970          Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
971          Set_Convention (Desig_Type, Convention_Protected);
972       else
973          Set_Ekind (T_Name, E_Access_Subprogram_Type);
974       end if;
975
976       Set_Etype                    (T_Name, T_Name);
977       Init_Size_Align              (T_Name);
978       Set_Directly_Designated_Type (T_Name, Desig_Type);
979
980       --  Ada 2005 (AI-231): Propagate the null-excluding attribute
981
982       Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
983
984       Check_Restriction (No_Access_Subprograms, T_Def);
985    end Access_Subprogram_Declaration;
986
987    ----------------------------
988    -- Access_Type_Declaration --
989    ----------------------------
990
991    procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
992       S : constant Node_Id := Subtype_Indication (Def);
993       P : constant Node_Id := Parent (Def);
994
995       Desig : Entity_Id;
996       --  Designated type
997
998    begin
999       --  Check for permissible use of incomplete type
1000
1001       if Nkind (S) /= N_Subtype_Indication then
1002          Analyze (S);
1003
1004          if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1005             Set_Directly_Designated_Type (T, Entity (S));
1006          else
1007             Set_Directly_Designated_Type (T,
1008               Process_Subtype (S, P, T, 'P'));
1009          end if;
1010
1011       else
1012          Set_Directly_Designated_Type (T,
1013            Process_Subtype (S, P, T, 'P'));
1014       end if;
1015
1016       if All_Present (Def) or Constant_Present (Def) then
1017          Set_Ekind (T, E_General_Access_Type);
1018       else
1019          Set_Ekind (T, E_Access_Type);
1020       end if;
1021
1022       if Base_Type (Designated_Type (T)) = T then
1023          Error_Msg_N ("access type cannot designate itself", S);
1024
1025       --  In Ada 2005, the type may have a limited view through some unit
1026       --  in its own context, allowing the following circularity that cannot
1027       --  be detected earlier
1028
1029       elsif Is_Class_Wide_Type (Designated_Type (T))
1030         and then Etype (Designated_Type (T)) = T
1031       then
1032          Error_Msg_N
1033            ("access type cannot designate its own classwide type", S);
1034
1035          --  Clean up indication of tagged status to prevent cascaded errors
1036
1037          Set_Is_Tagged_Type (T, False);
1038       end if;
1039
1040       Set_Etype (T, T);
1041
1042       --  If the type has appeared already in a with_type clause, it is
1043       --  frozen and the pointer size is already set. Else, initialize.
1044
1045       if not From_With_Type (T) then
1046          Init_Size_Align (T);
1047       end if;
1048
1049       Set_Is_Access_Constant (T, Constant_Present (Def));
1050
1051       Desig := Designated_Type (T);
1052
1053       --  If designated type is an imported tagged type, indicate that the
1054       --  access type is also imported, and therefore restricted in its use.
1055       --  The access type may already be imported, so keep setting otherwise.
1056
1057       --  Ada 2005 (AI-50217): If the non-limited view of the designated type
1058       --  is available, use it as the designated type of the access type, so
1059       --  that the back-end gets a usable entity.
1060
1061       declare
1062          N_Desig : Entity_Id;
1063
1064       begin
1065          if From_With_Type (Desig)
1066            and then Ekind (Desig) /= E_Access_Type
1067          then
1068             Set_From_With_Type (T);
1069
1070             if Is_Incomplete_Type (Desig) then
1071                N_Desig := Non_Limited_View (Desig);
1072
1073             else pragma Assert (Ekind (Desig) = E_Class_Wide_Type);
1074                if From_With_Type (Etype (Desig)) then
1075                   N_Desig := Non_Limited_View (Etype (Desig));
1076                else
1077                   N_Desig := Etype (Desig);
1078                end if;
1079             end if;
1080
1081             pragma Assert (Present (N_Desig));
1082             Set_Directly_Designated_Type (T, N_Desig);
1083          end if;
1084       end;
1085
1086       --  Note that Has_Task is always false, since the access type itself
1087       --  is not a task type. See Einfo for more description on this point.
1088       --  Exactly the same consideration applies to Has_Controlled_Component.
1089
1090       Set_Has_Task (T, False);
1091       Set_Has_Controlled_Component (T, False);
1092
1093       --  Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1094       --  attributes
1095
1096       Set_Can_Never_Be_Null  (T, Null_Exclusion_Present (Def));
1097       Set_Is_Access_Constant (T, Constant_Present (Def));
1098    end Access_Type_Declaration;
1099
1100    ----------------------------------
1101    -- Add_Interface_Tag_Components --
1102    ----------------------------------
1103
1104    procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1105       Loc      : constant Source_Ptr := Sloc (N);
1106       Elmt     : Elmt_Id;
1107       Ext      : Node_Id;
1108       L        : List_Id;
1109       Last_Tag : Node_Id;
1110       Comp     : Node_Id;
1111
1112       procedure Add_Tag (Iface : Entity_Id);
1113       --  Add tag for one of the progenitor interfaces
1114
1115       -------------
1116       -- Add_Tag --
1117       -------------
1118
1119       procedure Add_Tag (Iface : Entity_Id) is
1120          Decl   : Node_Id;
1121          Def    : Node_Id;
1122          Tag    : Entity_Id;
1123          Offset : Entity_Id;
1124
1125       begin
1126          pragma Assert (Is_Tagged_Type (Iface)
1127            and then Is_Interface (Iface));
1128
1129          Def :=
1130            Make_Component_Definition (Loc,
1131              Aliased_Present    => True,
1132              Subtype_Indication =>
1133                New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1134
1135          Tag := Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1136
1137          Decl :=
1138            Make_Component_Declaration (Loc,
1139              Defining_Identifier  => Tag,
1140              Component_Definition => Def);
1141
1142          Analyze_Component_Declaration (Decl);
1143
1144          Set_Analyzed (Decl);
1145          Set_Ekind               (Tag, E_Component);
1146          Set_Is_Limited_Record   (Tag);
1147          Set_Is_Tag              (Tag);
1148          Init_Component_Location (Tag);
1149
1150          pragma Assert (Is_Frozen (Iface));
1151
1152          Set_DT_Entry_Count    (Tag,
1153            DT_Entry_Count (First_Entity (Iface)));
1154
1155          if No (Last_Tag) then
1156             Prepend (Decl, L);
1157          else
1158             Insert_After (Last_Tag, Decl);
1159          end if;
1160
1161          Last_Tag := Decl;
1162
1163          --  If the ancestor has discriminants we need to give special support
1164          --  to store the offset_to_top value of the secondary dispatch tables.
1165          --  For this purpose we add a supplementary component just after the
1166          --  field that contains the tag associated with each secondary DT.
1167
1168          if Typ /= Etype (Typ)
1169            and then Has_Discriminants (Etype (Typ))
1170          then
1171             Def :=
1172               Make_Component_Definition (Loc,
1173                 Subtype_Indication =>
1174                   New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1175
1176             Offset :=
1177               Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1178
1179             Decl :=
1180               Make_Component_Declaration (Loc,
1181                 Defining_Identifier  => Offset,
1182                 Component_Definition => Def);
1183
1184             Analyze_Component_Declaration (Decl);
1185
1186             Set_Analyzed (Decl);
1187             Set_Ekind               (Offset, E_Component);
1188             Init_Component_Location (Offset);
1189             Insert_After (Last_Tag, Decl);
1190             Last_Tag := Decl;
1191          end if;
1192       end Add_Tag;
1193
1194    --  Start of processing for Add_Interface_Tag_Components
1195
1196    begin
1197       if Ekind (Typ) /= E_Record_Type
1198         or else No (Abstract_Interfaces (Typ))
1199         or else Is_Empty_Elmt_List (Abstract_Interfaces (Typ))
1200         or else not RTE_Available (RE_Interface_Tag)
1201       then
1202          return;
1203       end if;
1204
1205       if Present (Abstract_Interfaces (Typ)) then
1206
1207          --  Find the current last tag
1208
1209          if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1210             Ext := Record_Extension_Part (Type_Definition (N));
1211          else
1212             pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1213             Ext := Type_Definition (N);
1214          end if;
1215
1216          Last_Tag := Empty;
1217
1218          if not (Present (Component_List (Ext))) then
1219             Set_Null_Present (Ext, False);
1220             L := New_List;
1221             Set_Component_List (Ext,
1222               Make_Component_List (Loc,
1223                 Component_Items => L,
1224                 Null_Present => False));
1225          else
1226             if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1227                L := Component_Items
1228                       (Component_List
1229                         (Record_Extension_Part
1230                           (Type_Definition (N))));
1231             else
1232                L := Component_Items
1233                       (Component_List
1234                         (Type_Definition (N)));
1235             end if;
1236
1237             --  Find the last tag component
1238
1239             Comp := First (L);
1240             while Present (Comp) loop
1241                if Is_Tag (Defining_Identifier (Comp)) then
1242                   Last_Tag := Comp;
1243                end if;
1244
1245                Next (Comp);
1246             end loop;
1247          end if;
1248
1249          --  At this point L references the list of components and Last_Tag
1250          --  references the current last tag (if any). Now we add the tag
1251          --  corresponding with all the interfaces that are not implemented
1252          --  by the parent.
1253
1254          pragma Assert (Present
1255                         (First_Elmt (Abstract_Interfaces (Typ))));
1256
1257          Elmt := First_Elmt (Abstract_Interfaces (Typ));
1258          while Present (Elmt) loop
1259             Add_Tag (Node (Elmt));
1260             Next_Elmt (Elmt);
1261          end loop;
1262       end if;
1263    end Add_Interface_Tag_Components;
1264
1265    -----------------------------------
1266    -- Analyze_Component_Declaration --
1267    -----------------------------------
1268
1269    procedure Analyze_Component_Declaration (N : Node_Id) is
1270       Id : constant Entity_Id := Defining_Identifier (N);
1271       T  : Entity_Id;
1272       P  : Entity_Id;
1273
1274       function Contains_POC (Constr : Node_Id) return Boolean;
1275       --  Determines whether a constraint uses the discriminant of a record
1276       --  type thus becoming a per-object constraint (POC).
1277
1278       function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1279       --  Typ is the type of the current component, check whether this type is
1280       --  a limited type. Used to validate declaration against that of
1281       --  enclosing record.
1282
1283       ------------------
1284       -- Contains_POC --
1285       ------------------
1286
1287       function Contains_POC (Constr : Node_Id) return Boolean is
1288       begin
1289          case Nkind (Constr) is
1290             when N_Attribute_Reference =>
1291                return Attribute_Name (Constr) = Name_Access
1292                         and
1293                       Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1294
1295             when N_Discriminant_Association =>
1296                return Denotes_Discriminant (Expression (Constr));
1297
1298             when N_Identifier =>
1299                return Denotes_Discriminant (Constr);
1300
1301             when N_Index_Or_Discriminant_Constraint =>
1302                declare
1303                   IDC : Node_Id;
1304
1305                begin
1306                   IDC := First (Constraints (Constr));
1307                   while Present (IDC) loop
1308
1309                      --  One per-object constraint is sufficient
1310
1311                      if Contains_POC (IDC) then
1312                         return True;
1313                      end if;
1314
1315                      Next (IDC);
1316                   end loop;
1317
1318                   return False;
1319                end;
1320
1321             when N_Range =>
1322                return Denotes_Discriminant (Low_Bound (Constr))
1323                         or else
1324                       Denotes_Discriminant (High_Bound (Constr));
1325
1326             when N_Range_Constraint =>
1327                return Denotes_Discriminant (Range_Expression (Constr));
1328
1329             when others =>
1330                return False;
1331
1332          end case;
1333       end Contains_POC;
1334
1335       ----------------------
1336       -- Is_Known_Limited --
1337       ----------------------
1338
1339       function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1340          P : constant Entity_Id := Etype (Typ);
1341          R : constant Entity_Id := Root_Type (Typ);
1342
1343       begin
1344          if Is_Limited_Record (Typ) then
1345             return True;
1346
1347          --  If the root type is limited (and not a limited interface)
1348          --  so is the current type
1349
1350          elsif Is_Limited_Record (R)
1351            and then
1352              (not Is_Interface (R)
1353                or else not Is_Limited_Interface (R))
1354          then
1355             return True;
1356
1357          --  Else the type may have a limited interface progenitor, but a
1358          --  limited record parent.
1359
1360          elsif R /= P
1361            and then Is_Limited_Record (P)
1362          then
1363             return True;
1364
1365          else
1366             return False;
1367          end if;
1368       end Is_Known_Limited;
1369
1370    --  Start of processing for Analyze_Component_Declaration
1371
1372    begin
1373       Generate_Definition (Id);
1374       Enter_Name (Id);
1375
1376       if Present (Subtype_Indication (Component_Definition (N))) then
1377          T := Find_Type_Of_Object
1378                 (Subtype_Indication (Component_Definition (N)), N);
1379
1380       --  Ada 2005 (AI-230): Access Definition case
1381
1382       else
1383          pragma Assert (Present
1384                           (Access_Definition (Component_Definition (N))));
1385
1386          T := Access_Definition
1387                 (Related_Nod => N,
1388                  N => Access_Definition (Component_Definition (N)));
1389          Set_Is_Local_Anonymous_Access (T);
1390
1391          --  Ada 2005 (AI-254)
1392
1393          if Present (Access_To_Subprogram_Definition
1394                       (Access_Definition (Component_Definition (N))))
1395            and then Protected_Present (Access_To_Subprogram_Definition
1396                                         (Access_Definition
1397                                           (Component_Definition (N))))
1398          then
1399             T := Replace_Anonymous_Access_To_Protected_Subprogram (N, T);
1400          end if;
1401       end if;
1402
1403       --  If the subtype is a constrained subtype of the enclosing record,
1404       --  (which must have a partial view) the back-end does not properly
1405       --  handle the recursion. Rewrite the component declaration with an
1406       --  explicit subtype indication, which is acceptable to Gigi. We can copy
1407       --  the tree directly because side effects have already been removed from
1408       --  discriminant constraints.
1409
1410       if Ekind (T) = E_Access_Subtype
1411         and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1412         and then Comes_From_Source (T)
1413         and then Nkind (Parent (T)) = N_Subtype_Declaration
1414         and then Etype (Directly_Designated_Type (T)) = Current_Scope
1415       then
1416          Rewrite
1417            (Subtype_Indication (Component_Definition (N)),
1418              New_Copy_Tree (Subtype_Indication (Parent (T))));
1419          T := Find_Type_Of_Object
1420                  (Subtype_Indication (Component_Definition (N)), N);
1421       end if;
1422
1423       --  If the component declaration includes a default expression, then we
1424       --  check that the component is not of a limited type (RM 3.7(5)),
1425       --  and do the special preanalysis of the expression (see section on
1426       --  "Handling of Default and Per-Object Expressions" in the spec of
1427       --  package Sem).
1428
1429       if Present (Expression (N)) then
1430          Analyze_Per_Use_Expression (Expression (N), T);
1431          Check_Initialization (T, Expression (N));
1432
1433          if Ada_Version >= Ada_05
1434            and then Is_Access_Type (T)
1435            and then Ekind (T) = E_Anonymous_Access_Type
1436          then
1437             --  Check RM 3.9.2(9): "if the expected type for an expression is
1438             --  an anonymous access-to-specific tagged type, then the object
1439             --  designated by the expression shall not be dynamically tagged
1440             --  unless it is a controlling operand in a call on a dispatching
1441             --  operation"
1442
1443             if Is_Tagged_Type (Directly_Designated_Type (T))
1444               and then
1445                 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1446               and then
1447                 Ekind (Directly_Designated_Type (Etype (Expression (N)))) =
1448                                                         E_Class_Wide_Type
1449             then
1450                Error_Msg_N
1451                  ("access to specific tagged type required ('R'M 3.9.2(9))",
1452                   Expression (N));
1453             end if;
1454
1455             --  (Ada 2005: AI-230): Accessibility check for anonymous
1456             --  components
1457
1458             --  Missing barrier Ada_Version >= Ada_05???
1459
1460             if Type_Access_Level (Etype (Expression (N))) >
1461                Type_Access_Level (T)
1462             then
1463                Error_Msg_N
1464                  ("expression has deeper access level than component " &
1465                   "('R'M 3.10.2 (12.2))", Expression (N));
1466             end if;
1467          end if;
1468       end if;
1469
1470       --  The parent type may be a private view with unknown discriminants,
1471       --  and thus unconstrained. Regular components must be constrained.
1472
1473       if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1474          if Is_Class_Wide_Type (T) then
1475             Error_Msg_N
1476                ("class-wide subtype with unknown discriminants" &
1477                  " in component declaration",
1478                  Subtype_Indication (Component_Definition (N)));
1479          else
1480             Error_Msg_N
1481               ("unconstrained subtype in component declaration",
1482                Subtype_Indication (Component_Definition (N)));
1483          end if;
1484
1485       --  Components cannot be abstract, except for the special case of
1486       --  the _Parent field (case of extending an abstract tagged type)
1487
1488       elsif Is_Abstract (T) and then Chars (Id) /= Name_uParent then
1489          Error_Msg_N ("type of a component cannot be abstract", N);
1490       end if;
1491
1492       Set_Etype (Id, T);
1493       Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1494
1495       --  The component declaration may have a per-object constraint, set
1496       --  the appropriate flag in the defining identifier of the subtype.
1497
1498       if Present (Subtype_Indication (Component_Definition (N))) then
1499          declare
1500             Sindic : constant Node_Id :=
1501                        Subtype_Indication (Component_Definition (N));
1502
1503          begin
1504             if Nkind (Sindic) = N_Subtype_Indication
1505               and then Present (Constraint (Sindic))
1506               and then Contains_POC (Constraint (Sindic))
1507             then
1508                Set_Has_Per_Object_Constraint (Id);
1509             end if;
1510          end;
1511       end if;
1512
1513       --  Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1514       --  out some static checks.
1515
1516       if Ada_Version >= Ada_05
1517         and then Can_Never_Be_Null (T)
1518       then
1519          Null_Exclusion_Static_Checks (N);
1520       end if;
1521
1522       --  If this component is private (or depends on a private type), flag the
1523       --  record type to indicate that some operations are not available.
1524
1525       P := Private_Component (T);
1526
1527       if Present (P) then
1528
1529          --  Check for circular definitions
1530
1531          if P = Any_Type then
1532             Set_Etype (Id, Any_Type);
1533
1534          --  There is a gap in the visibility of operations only if the
1535          --  component type is not defined in the scope of the record type.
1536
1537          elsif Scope (P) = Scope (Current_Scope) then
1538             null;
1539
1540          elsif Is_Limited_Type (P) then
1541             Set_Is_Limited_Composite (Current_Scope);
1542
1543          else
1544             Set_Is_Private_Composite (Current_Scope);
1545          end if;
1546       end if;
1547
1548       if P /= Any_Type
1549         and then Is_Limited_Type (T)
1550         and then Chars (Id) /= Name_uParent
1551         and then Is_Tagged_Type (Current_Scope)
1552       then
1553          if Is_Derived_Type (Current_Scope)
1554            and then not Is_Known_Limited (Current_Scope)
1555          then
1556             Error_Msg_N
1557               ("extension of nonlimited type cannot have limited components",
1558                N);
1559
1560             if Is_Interface (Root_Type (Current_Scope)) then
1561                Error_Msg_N
1562                  ("\limitedness is not inherited from limited interface", N);
1563                Error_Msg_N
1564                  ("\add LIMITED to type indication", N);
1565             end if;
1566
1567             Explain_Limited_Type (T, N);
1568             Set_Etype (Id, Any_Type);
1569             Set_Is_Limited_Composite (Current_Scope, False);
1570
1571          elsif not Is_Derived_Type (Current_Scope)
1572            and then not Is_Limited_Record (Current_Scope)
1573            and then not Is_Concurrent_Type (Current_Scope)
1574          then
1575             Error_Msg_N
1576               ("nonlimited tagged type cannot have limited components", N);
1577             Explain_Limited_Type (T, N);
1578             Set_Etype (Id, Any_Type);
1579             Set_Is_Limited_Composite (Current_Scope, False);
1580          end if;
1581       end if;
1582
1583       Set_Original_Record_Component (Id, Id);
1584    end Analyze_Component_Declaration;
1585
1586    --------------------------
1587    -- Analyze_Declarations --
1588    --------------------------
1589
1590    procedure Analyze_Declarations (L : List_Id) is
1591       D           : Node_Id;
1592       Freeze_From : Entity_Id := Empty;
1593       Next_Node   : Node_Id;
1594
1595       procedure Adjust_D;
1596       --  Adjust D not to include implicit label declarations, since these
1597       --  have strange Sloc values that result in elaboration check problems.
1598       --  (They have the sloc of the label as found in the source, and that
1599       --  is ahead of the current declarative part).
1600
1601       --------------
1602       -- Adjust_D --
1603       --------------
1604
1605       procedure Adjust_D is
1606       begin
1607          while Present (Prev (D))
1608            and then Nkind (D) = N_Implicit_Label_Declaration
1609          loop
1610             Prev (D);
1611          end loop;
1612       end Adjust_D;
1613
1614    --  Start of processing for Analyze_Declarations
1615
1616    begin
1617       D := First (L);
1618       while Present (D) loop
1619
1620          --  Complete analysis of declaration
1621
1622          Analyze (D);
1623          Next_Node := Next (D);
1624
1625          if No (Freeze_From) then
1626             Freeze_From := First_Entity (Current_Scope);
1627          end if;
1628
1629          --  At the end of a declarative part, freeze remaining entities
1630          --  declared in it. The end of the visible declarations of package
1631          --  specification is not the end of a declarative part if private
1632          --  declarations are present. The end of a package declaration is a
1633          --  freezing point only if it a library package. A task definition or
1634          --  protected type definition is not a freeze point either. Finally,
1635          --  we do not freeze entities in generic scopes, because there is no
1636          --  code generated for them and freeze nodes will be generated for
1637          --  the instance.
1638
1639          --  The end of a package instantiation is not a freeze point, but
1640          --  for now we make it one, because the generic body is inserted
1641          --  (currently) immediately after. Generic instantiations will not
1642          --  be a freeze point once delayed freezing of bodies is implemented.
1643          --  (This is needed in any case for early instantiations ???).
1644
1645          if No (Next_Node) then
1646             if Nkind (Parent (L)) = N_Component_List
1647               or else Nkind (Parent (L)) = N_Task_Definition
1648               or else Nkind (Parent (L)) = N_Protected_Definition
1649             then
1650                null;
1651
1652             elsif Nkind (Parent (L)) /= N_Package_Specification then
1653                if Nkind (Parent (L)) = N_Package_Body then
1654                   Freeze_From := First_Entity (Current_Scope);
1655                end if;
1656
1657                Adjust_D;
1658                Freeze_All (Freeze_From, D);
1659                Freeze_From := Last_Entity (Current_Scope);
1660
1661             elsif Scope (Current_Scope) /= Standard_Standard
1662               and then not Is_Child_Unit (Current_Scope)
1663               and then No (Generic_Parent (Parent (L)))
1664             then
1665                null;
1666
1667             elsif L /= Visible_Declarations (Parent (L))
1668                or else No (Private_Declarations (Parent (L)))
1669                or else Is_Empty_List (Private_Declarations (Parent (L)))
1670             then
1671                Adjust_D;
1672                Freeze_All (Freeze_From, D);
1673                Freeze_From := Last_Entity (Current_Scope);
1674             end if;
1675
1676          --  If next node is a body then freeze all types before the body.
1677          --  An exception occurs for expander generated bodies, which can
1678          --  be recognized by their already being analyzed. The expander
1679          --  ensures that all types needed by these bodies have been frozen
1680          --  but it is not necessary to freeze all types (and would be wrong
1681          --  since it would not correspond to an RM defined freeze point).
1682
1683          elsif not Analyzed (Next_Node)
1684            and then (Nkind (Next_Node) = N_Subprogram_Body
1685              or else Nkind (Next_Node) = N_Entry_Body
1686              or else Nkind (Next_Node) = N_Package_Body
1687              or else Nkind (Next_Node) = N_Protected_Body
1688              or else Nkind (Next_Node) = N_Task_Body
1689              or else Nkind (Next_Node) in N_Body_Stub)
1690          then
1691             Adjust_D;
1692             Freeze_All (Freeze_From, D);
1693             Freeze_From := Last_Entity (Current_Scope);
1694          end if;
1695
1696          D := Next_Node;
1697       end loop;
1698    end Analyze_Declarations;
1699
1700    ----------------------------------
1701    -- Analyze_Incomplete_Type_Decl --
1702    ----------------------------------
1703
1704    procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
1705       F : constant Boolean := Is_Pure (Current_Scope);
1706       T : Entity_Id;
1707
1708    begin
1709       Generate_Definition (Defining_Identifier (N));
1710
1711       --  Process an incomplete declaration. The identifier must not have been
1712       --  declared already in the scope. However, an incomplete declaration may
1713       --  appear in the private part of a package, for a private type that has
1714       --  already been declared.
1715
1716       --  In this case, the discriminants (if any) must match
1717
1718       T := Find_Type_Name (N);
1719
1720       Set_Ekind (T, E_Incomplete_Type);
1721       Init_Size_Align (T);
1722       Set_Is_First_Subtype (T, True);
1723       Set_Etype (T, T);
1724
1725       --  Ada 2005 (AI-326): Minimum decoration to give support to tagged
1726       --  incomplete types.
1727
1728       if Tagged_Present (N) then
1729          Set_Is_Tagged_Type (T);
1730          Make_Class_Wide_Type (T);
1731          Set_Primitive_Operations (T, New_Elmt_List);
1732       end if;
1733
1734       New_Scope (T);
1735
1736       Set_Stored_Constraint (T, No_Elist);
1737
1738       if Present (Discriminant_Specifications (N)) then
1739          Process_Discriminants (N);
1740       end if;
1741
1742       End_Scope;
1743
1744       --  If the type has discriminants, non-trivial subtypes may be be
1745       --  declared before the full view of the type. The full views of those
1746       --  subtypes will be built after the full view of the type.
1747
1748       Set_Private_Dependents (T, New_Elmt_List);
1749       Set_Is_Pure (T, F);
1750    end Analyze_Incomplete_Type_Decl;
1751
1752    -----------------------------------
1753    -- Analyze_Interface_Declaration --
1754    -----------------------------------
1755
1756    procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
1757    begin
1758       Set_Is_Tagged_Type      (T);
1759
1760       Set_Is_Limited_Record   (T, Limited_Present (Def)
1761                                    or else Task_Present (Def)
1762                                    or else Protected_Present (Def)
1763                                    or else Synchronized_Present (Def));
1764
1765       --  Type is abstract if full declaration carries keyword, or if
1766       --  previous partial view did.
1767
1768       Set_Is_Abstract  (T);
1769       Set_Is_Interface (T);
1770
1771       Set_Is_Limited_Interface      (T, Limited_Present (Def));
1772       Set_Is_Protected_Interface    (T, Protected_Present (Def));
1773       Set_Is_Synchronized_Interface (T, Synchronized_Present (Def));
1774       Set_Is_Task_Interface         (T, Task_Present (Def));
1775       Set_Abstract_Interfaces       (T, New_Elmt_List);
1776       Set_Primitive_Operations      (T, New_Elmt_List);
1777    end Analyze_Interface_Declaration;
1778
1779    -----------------------------
1780    -- Analyze_Itype_Reference --
1781    -----------------------------
1782
1783    --  Nothing to do. This node is placed in the tree only for the benefit of
1784    --  back end processing, and has no effect on the semantic processing.
1785
1786    procedure Analyze_Itype_Reference (N : Node_Id) is
1787    begin
1788       pragma Assert (Is_Itype (Itype (N)));
1789       null;
1790    end Analyze_Itype_Reference;
1791
1792    --------------------------------
1793    -- Analyze_Number_Declaration --
1794    --------------------------------
1795
1796    procedure Analyze_Number_Declaration (N : Node_Id) is
1797       Id    : constant Entity_Id := Defining_Identifier (N);
1798       E     : constant Node_Id   := Expression (N);
1799       T     : Entity_Id;
1800       Index : Interp_Index;
1801       It    : Interp;
1802
1803    begin
1804       Generate_Definition (Id);
1805       Enter_Name (Id);
1806
1807       --  This is an optimization of a common case of an integer literal
1808
1809       if Nkind (E) = N_Integer_Literal then
1810          Set_Is_Static_Expression (E, True);
1811          Set_Etype                (E, Universal_Integer);
1812
1813          Set_Etype     (Id, Universal_Integer);
1814          Set_Ekind     (Id, E_Named_Integer);
1815          Set_Is_Frozen (Id, True);
1816          return;
1817       end if;
1818
1819       Set_Is_Pure (Id, Is_Pure (Current_Scope));
1820
1821       --  Process expression, replacing error by integer zero, to avoid
1822       --  cascaded errors or aborts further along in the processing
1823
1824       --  Replace Error by integer zero, which seems least likely to
1825       --  cause cascaded errors.
1826
1827       if E = Error then
1828          Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
1829          Set_Error_Posted (E);
1830       end if;
1831
1832       Analyze (E);
1833
1834       --  Verify that the expression is static and numeric. If
1835       --  the expression is overloaded, we apply the preference
1836       --  rule that favors root numeric types.
1837
1838       if not Is_Overloaded (E) then
1839          T := Etype (E);
1840
1841       else
1842          T := Any_Type;
1843
1844          Get_First_Interp (E, Index, It);
1845          while Present (It.Typ) loop
1846             if (Is_Integer_Type (It.Typ)
1847                  or else Is_Real_Type (It.Typ))
1848               and then (Scope (Base_Type (It.Typ))) = Standard_Standard
1849             then
1850                if T = Any_Type then
1851                   T := It.Typ;
1852
1853                elsif It.Typ = Universal_Real
1854                  or else It.Typ = Universal_Integer
1855                then
1856                   --  Choose universal interpretation over any other
1857
1858                   T := It.Typ;
1859                   exit;
1860                end if;
1861             end if;
1862
1863             Get_Next_Interp (Index, It);
1864          end loop;
1865       end if;
1866
1867       if Is_Integer_Type (T)  then
1868          Resolve (E, T);
1869          Set_Etype (Id, Universal_Integer);
1870          Set_Ekind (Id, E_Named_Integer);
1871
1872       elsif Is_Real_Type (T) then
1873
1874          --  Because the real value is converted to universal_real, this is a
1875          --  legal context for a universal fixed expression.
1876
1877          if T = Universal_Fixed then
1878             declare
1879                Loc  : constant Source_Ptr := Sloc (N);
1880                Conv : constant Node_Id := Make_Type_Conversion (Loc,
1881                         Subtype_Mark =>
1882                           New_Occurrence_Of (Universal_Real, Loc),
1883                         Expression => Relocate_Node (E));
1884
1885             begin
1886                Rewrite (E, Conv);
1887                Analyze (E);
1888             end;
1889
1890          elsif T = Any_Fixed then
1891             Error_Msg_N ("illegal context for mixed mode operation", E);
1892
1893             --  Expression is of the form : universal_fixed * integer. Try to
1894             --  resolve as universal_real.
1895
1896             T := Universal_Real;
1897             Set_Etype (E, T);
1898          end if;
1899
1900          Resolve (E, T);
1901          Set_Etype (Id, Universal_Real);
1902          Set_Ekind (Id, E_Named_Real);
1903
1904       else
1905          Wrong_Type (E, Any_Numeric);
1906          Resolve (E, T);
1907
1908          Set_Etype               (Id, T);
1909          Set_Ekind               (Id, E_Constant);
1910          Set_Never_Set_In_Source (Id, True);
1911          Set_Is_True_Constant    (Id, True);
1912          return;
1913       end if;
1914
1915       if Nkind (E) = N_Integer_Literal
1916         or else Nkind (E) = N_Real_Literal
1917       then
1918          Set_Etype (E, Etype (Id));
1919       end if;
1920
1921       if not Is_OK_Static_Expression (E) then
1922          Flag_Non_Static_Expr
1923            ("non-static expression used in number declaration!", E);
1924          Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
1925          Set_Etype (E, Any_Type);
1926       end if;
1927    end Analyze_Number_Declaration;
1928
1929    --------------------------------
1930    -- Analyze_Object_Declaration --
1931    --------------------------------
1932
1933    procedure Analyze_Object_Declaration (N : Node_Id) is
1934       Loc   : constant Source_Ptr := Sloc (N);
1935       Id    : constant Entity_Id  := Defining_Identifier (N);
1936       T     : Entity_Id;
1937       Act_T : Entity_Id;
1938
1939       E : Node_Id := Expression (N);
1940       --  E is set to Expression (N) throughout this routine. When
1941       --  Expression (N) is modified, E is changed accordingly.
1942
1943       Prev_Entity : Entity_Id := Empty;
1944
1945       function Count_Tasks (T : Entity_Id) return Uint;
1946       --  This function is called when a library level object of type is
1947       --  declared. It's function is to count the static number of tasks
1948       --  declared within the type (it is only called if Has_Tasks is set for
1949       --  T). As a side effect, if an array of tasks with non-static bounds or
1950       --  a variant record type is encountered, Check_Restrictions is called
1951       --  indicating the count is unknown.
1952
1953       -----------------
1954       -- Count_Tasks --
1955       -----------------
1956
1957       function Count_Tasks (T : Entity_Id) return Uint is
1958          C : Entity_Id;
1959          X : Node_Id;
1960          V : Uint;
1961
1962       begin
1963          if Is_Task_Type (T) then
1964             return Uint_1;
1965
1966          elsif Is_Record_Type (T) then
1967             if Has_Discriminants (T) then
1968                Check_Restriction (Max_Tasks, N);
1969                return Uint_0;
1970
1971             else
1972                V := Uint_0;
1973                C := First_Component (T);
1974                while Present (C) loop
1975                   V := V + Count_Tasks (Etype (C));
1976                   Next_Component (C);
1977                end loop;
1978
1979                return V;
1980             end if;
1981
1982          elsif Is_Array_Type (T) then
1983             X := First_Index (T);
1984             V := Count_Tasks (Component_Type (T));
1985             while Present (X) loop
1986                C := Etype (X);
1987
1988                if not Is_Static_Subtype (C) then
1989                   Check_Restriction (Max_Tasks, N);
1990                   return Uint_0;
1991                else
1992                   V := V * (UI_Max (Uint_0,
1993                                     Expr_Value (Type_High_Bound (C)) -
1994                                     Expr_Value (Type_Low_Bound (C)) + Uint_1));
1995                end if;
1996
1997                Next_Index (X);
1998             end loop;
1999
2000             return V;
2001
2002          else
2003             return Uint_0;
2004          end if;
2005       end Count_Tasks;
2006
2007    --  Start of processing for Analyze_Object_Declaration
2008
2009    begin
2010       --  There are three kinds of implicit types generated by an
2011       --  object declaration:
2012
2013       --   1. Those for generated by the original Object Definition
2014
2015       --   2. Those generated by the Expression
2016
2017       --   3. Those used to constrained the Object Definition with the
2018       --       expression constraints when it is unconstrained
2019
2020       --  They must be generated in this order to avoid order of elaboration
2021       --  issues. Thus the first step (after entering the name) is to analyze
2022       --  the object definition.
2023
2024       if Constant_Present (N) then
2025          Prev_Entity := Current_Entity_In_Scope (Id);
2026
2027          --  If homograph is an implicit subprogram, it is overridden by the
2028          --  current declaration.
2029
2030          if Present (Prev_Entity)
2031            and then Is_Overloadable (Prev_Entity)
2032            and then Is_Inherited_Operation (Prev_Entity)
2033          then
2034             Prev_Entity := Empty;
2035          end if;
2036       end if;
2037
2038       if Present (Prev_Entity) then
2039          Constant_Redeclaration (Id, N, T);
2040
2041          Generate_Reference (Prev_Entity, Id, 'c');
2042          Set_Completion_Referenced (Id);
2043
2044          if Error_Posted (N) then
2045
2046             --  Type mismatch or illegal redeclaration, Do not analyze
2047             --  expression to avoid cascaded errors.
2048
2049             T := Find_Type_Of_Object (Object_Definition (N), N);
2050             Set_Etype (Id, T);
2051             Set_Ekind (Id, E_Variable);
2052             return;
2053          end if;
2054
2055       --  In the normal case, enter identifier at the start to catch premature
2056       --  usage in the initialization expression.
2057
2058       else
2059          Generate_Definition (Id);
2060          Enter_Name (Id);
2061
2062          T := Find_Type_Of_Object (Object_Definition (N), N);
2063
2064          if Error_Posted (Id) then
2065             Set_Etype (Id, T);
2066             Set_Ekind (Id, E_Variable);
2067             return;
2068          end if;
2069       end if;
2070
2071       --  Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2072       --  out some static checks
2073
2074       if Ada_Version >= Ada_05
2075         and then Can_Never_Be_Null (T)
2076       then
2077          --  In case of aggregates we must also take care of the correct
2078          --  initialization of nested aggregates bug this is done at the
2079          --  point of the analysis of the aggregate (see sem_aggr.adb)
2080
2081          if Present (Expression (N))
2082            and then Nkind (Expression (N)) = N_Aggregate
2083          then
2084             null;
2085
2086          else
2087             declare
2088                Save_Typ : constant Entity_Id := Etype (Id);
2089             begin
2090                Set_Etype (Id, T); --  Temp. decoration for static checks
2091                Null_Exclusion_Static_Checks (N);
2092                Set_Etype (Id, Save_Typ);
2093             end;
2094          end if;
2095       end if;
2096
2097       Set_Is_Pure (Id, Is_Pure (Current_Scope));
2098
2099       --  If deferred constant, make sure context is appropriate. We detect
2100       --  a deferred constant as a constant declaration with no expression.
2101       --  A deferred constant can appear in a package body if its completion
2102       --  is by means of an interface pragma.
2103
2104       if Constant_Present (N)
2105         and then No (E)
2106       then
2107          if not Is_Package_Or_Generic_Package (Current_Scope) then
2108             Error_Msg_N
2109               ("invalid context for deferred constant declaration ('R'M 7.4)",
2110                 N);
2111             Error_Msg_N
2112               ("\declaration requires an initialization expression",
2113                 N);
2114             Set_Constant_Present (N, False);
2115
2116          --  In Ada 83, deferred constant must be of private type
2117
2118          elsif not Is_Private_Type (T) then
2119             if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2120                Error_Msg_N
2121                  ("(Ada 83) deferred constant must be private type", N);
2122             end if;
2123          end if;
2124
2125       --  If not a deferred constant, then object declaration freezes its type
2126
2127       else
2128          Check_Fully_Declared (T, N);
2129          Freeze_Before (N, T);
2130       end if;
2131
2132       --  If the object was created by a constrained array definition, then
2133       --  set the link in both the anonymous base type and anonymous subtype
2134       --  that are built to represent the array type to point to the object.
2135
2136       if Nkind (Object_Definition (Declaration_Node (Id))) =
2137                         N_Constrained_Array_Definition
2138       then
2139          Set_Related_Array_Object (T, Id);
2140          Set_Related_Array_Object (Base_Type (T), Id);
2141       end if;
2142
2143       --  Special checks for protected objects not at library level
2144
2145       if Is_Protected_Type (T)
2146         and then not Is_Library_Level_Entity (Id)
2147       then
2148          Check_Restriction (No_Local_Protected_Objects, Id);
2149
2150          --  Protected objects with interrupt handlers must be at library level
2151
2152          --  Ada 2005: this test is not needed (and the corresponding clause
2153          --  in the RM is removed) because accessibility checks are sufficient
2154          --  to make handlers not at the library level illegal.
2155
2156          if Has_Interrupt_Handler (T)
2157            and then Ada_Version < Ada_05
2158          then
2159             Error_Msg_N
2160               ("interrupt object can only be declared at library level", Id);
2161          end if;
2162       end if;
2163
2164       --  The actual subtype of the object is the nominal subtype, unless
2165       --  the nominal one is unconstrained and obtained from the expression.
2166
2167       Act_T := T;
2168
2169       --  Process initialization expression if present and not in error
2170
2171       if Present (E) and then E /= Error then
2172          Analyze (E);
2173
2174          --  In case of errors detected in the analysis of the expression,
2175          --  decorate it with the expected type to avoid cascade errors
2176
2177          if No (Etype (E)) then
2178             Set_Etype (E, T);
2179          end if;
2180
2181          --  If an initialization expression is present, then we set the
2182          --  Is_True_Constant flag. It will be reset if this is a variable
2183          --  and it is indeed modified.
2184
2185          Set_Is_True_Constant (Id, True);
2186
2187          --  If we are analyzing a constant declaration, set its completion
2188          --  flag after analyzing the expression.
2189
2190          if Constant_Present (N) then
2191             Set_Has_Completion (Id);
2192          end if;
2193
2194          Set_Etype (Id, T);             --  may be overridden later on
2195          Resolve (E, T);
2196
2197          if not Assignment_OK (N) then
2198             Check_Initialization (T, E);
2199          end if;
2200          Check_Unset_Reference (E);
2201
2202          --  If this is a variable, then set current value
2203
2204          if not Constant_Present (N) then
2205             if Compile_Time_Known_Value (E) then
2206                Set_Current_Value (Id, E);
2207             end if;
2208          end if;
2209
2210          --  Check incorrect use of dynamically tagged expressions. Note
2211          --  the use of Is_Tagged_Type (T) which seems redundant but is in
2212          --  fact important to avoid spurious errors due to expanded code
2213          --  for dispatching functions over an anonymous access type
2214
2215          if (Is_Class_Wide_Type (Etype (E)) or else Is_Dynamically_Tagged (E))
2216            and then Is_Tagged_Type (T)
2217            and then not Is_Class_Wide_Type (T)
2218          then
2219             Error_Msg_N ("dynamically tagged expression not allowed!", E);
2220          end if;
2221
2222          Apply_Scalar_Range_Check (E, T);
2223          Apply_Static_Length_Check (E, T);
2224       end if;
2225
2226       --  If the No_Streams restriction is set, check that the type of the
2227       --  object is not, and does not contain, any subtype derived from
2228       --  Ada.Streams.Root_Stream_Type. Note that we guard the call to
2229       --  Has_Stream just for efficiency reasons. There is no point in
2230       --  spending time on a Has_Stream check if the restriction is not set.
2231
2232       if Restrictions.Set (No_Streams) then
2233          if Has_Stream (T) then
2234             Check_Restriction (No_Streams, N);
2235          end if;
2236       end if;
2237
2238       --  Abstract type is never permitted for a variable or constant.
2239       --  Note: we inhibit this check for objects that do not come from
2240       --  source because there is at least one case (the expansion of
2241       --  x'class'input where x is abstract) where we legitimately
2242       --  generate an abstract object.
2243
2244       if Is_Abstract (T) and then Comes_From_Source (N) then
2245          Error_Msg_N ("type of object cannot be abstract",
2246                       Object_Definition (N));
2247
2248          if Is_CPP_Class (T) then
2249             Error_Msg_NE ("\} may need a cpp_constructor",
2250               Object_Definition (N), T);
2251          end if;
2252
2253       --  Case of unconstrained type
2254
2255       elsif Is_Indefinite_Subtype (T) then
2256
2257          --  Nothing to do in deferred constant case
2258
2259          if Constant_Present (N) and then No (E) then
2260             null;
2261
2262          --  Case of no initialization present
2263
2264          elsif No (E) then
2265             if No_Initialization (N) then
2266                null;
2267
2268             elsif Is_Class_Wide_Type (T) then
2269                Error_Msg_N
2270                  ("initialization required in class-wide declaration ", N);
2271
2272             else
2273                Error_Msg_N
2274                  ("unconstrained subtype not allowed (need initialization)",
2275                   Object_Definition (N));
2276             end if;
2277
2278          --  Case of initialization present but in error. Set initial
2279          --  expression as absent (but do not make above complaints)
2280
2281          elsif E = Error then
2282             Set_Expression (N, Empty);
2283             E := Empty;
2284
2285          --  Case of initialization present
2286
2287          else
2288             --  Not allowed in Ada 83
2289
2290             if not Constant_Present (N) then
2291                if Ada_Version = Ada_83
2292                  and then Comes_From_Source (Object_Definition (N))
2293                then
2294                   Error_Msg_N
2295                     ("(Ada 83) unconstrained variable not allowed",
2296                      Object_Definition (N));
2297                end if;
2298             end if;
2299
2300             --  Now we constrain the variable from the initializing expression
2301
2302             --  If the expression is an aggregate, it has been expanded into
2303             --  individual assignments. Retrieve the actual type from the
2304             --  expanded construct.
2305
2306             if Is_Array_Type (T)
2307               and then No_Initialization (N)
2308               and then Nkind (Original_Node (E)) = N_Aggregate
2309             then
2310                Act_T := Etype (E);
2311
2312             else
2313                Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
2314                Act_T := Find_Type_Of_Object (Object_Definition (N), N);
2315             end if;
2316
2317             Set_Is_Constr_Subt_For_U_Nominal (Act_T);
2318
2319             if Aliased_Present (N) then
2320                Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2321             end if;
2322
2323             Freeze_Before (N, Act_T);
2324             Freeze_Before (N, T);
2325          end if;
2326
2327       elsif Is_Array_Type (T)
2328         and then No_Initialization (N)
2329         and then Nkind (Original_Node (E)) = N_Aggregate
2330       then
2331          if not Is_Entity_Name (Object_Definition (N)) then
2332             Act_T := Etype (E);
2333             Check_Compile_Time_Size (Act_T);
2334
2335             if Aliased_Present (N) then
2336                Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2337             end if;
2338          end if;
2339
2340          --  When the given object definition and the aggregate are specified
2341          --  independently, and their lengths might differ do a length check.
2342          --  This cannot happen if the aggregate is of the form (others =>...)
2343
2344          if not Is_Constrained (T) then
2345             null;
2346
2347          elsif Nkind (E) = N_Raise_Constraint_Error then
2348
2349             --  Aggregate is statically illegal. Place back in declaration
2350
2351             Set_Expression (N, E);
2352             Set_No_Initialization (N, False);
2353
2354          elsif T = Etype (E) then
2355             null;
2356
2357          elsif Nkind (E) = N_Aggregate
2358            and then Present (Component_Associations (E))
2359            and then Present (Choices (First (Component_Associations (E))))
2360            and then Nkind (First
2361             (Choices (First (Component_Associations (E))))) = N_Others_Choice
2362          then
2363             null;
2364
2365          else
2366             Apply_Length_Check (E, T);
2367          end if;
2368
2369       --  If the type is limited unconstrained with defaulted discriminants
2370       --  and there is no expression, then the object is constrained by the
2371       --  defaults, so it is worthwhile building the corresponding subtype.
2372
2373       elsif (Is_Limited_Record (T)
2374                or else Is_Concurrent_Type (T))
2375         and then not Is_Constrained (T)
2376         and then Has_Discriminants (T)
2377       then
2378          if No (E) then
2379             Act_T := Build_Default_Subtype (T, N);
2380          else
2381             --  Ada 2005:  a limited object may be initialized by means of an
2382             --  aggregate. If the type has default discriminants it has an
2383             --  unconstrained nominal type, Its actual subtype will be obtained
2384             --  from the aggregate, and not from the default discriminants.
2385
2386             Act_T := Etype (E);
2387          end if;
2388
2389          Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
2390
2391       elsif Present (Underlying_Type (T))
2392         and then not Is_Constrained (Underlying_Type (T))
2393         and then Has_Discriminants (Underlying_Type (T))
2394         and then Nkind (E) = N_Function_Call
2395         and then Constant_Present (N)
2396       then
2397          --  The back-end has problems with constants of a discriminated type
2398          --  with defaults, if the initial value is a function call. We
2399          --  generate an intermediate temporary for the result of the call.
2400          --  It is unclear why this should make it acceptable to gcc. ???
2401
2402          Remove_Side_Effects (E);
2403       end if;
2404
2405       if T = Standard_Wide_Character or else T = Standard_Wide_Wide_Character
2406         or else Root_Type (T) = Standard_Wide_String
2407         or else Root_Type (T) = Standard_Wide_Wide_String
2408       then
2409          Check_Restriction (No_Wide_Characters, Object_Definition (N));
2410       end if;
2411
2412       --  Now establish the proper kind and type of the object
2413
2414       if Constant_Present (N) then
2415          Set_Ekind               (Id, E_Constant);
2416          Set_Never_Set_In_Source (Id, True);
2417          Set_Is_True_Constant    (Id, True);
2418
2419       else
2420          Set_Ekind (Id, E_Variable);
2421
2422          --  A variable is set as shared passive if it appears in a shared
2423          --  passive package, and is at the outer level. This is not done
2424          --  for entities generated during expansion, because those are
2425          --  always manipulated locally.
2426
2427          if Is_Shared_Passive (Current_Scope)
2428            and then Is_Library_Level_Entity (Id)
2429            and then Comes_From_Source (Id)
2430          then
2431             Set_Is_Shared_Passive (Id);
2432             Check_Shared_Var (Id, T, N);
2433          end if;
2434
2435          --  Case of no initializing expression present. If the type is not
2436          --  fully initialized, then we set Never_Set_In_Source, since this
2437          --  is a case of a potentially uninitialized object. Note that we
2438          --  do not consider access variables to be fully initialized for
2439          --  this purpose, since it still seems dubious if someone declares
2440
2441          --  Note that we only do this for source declarations. If the object
2442          --  is declared by a generated declaration, we assume that it is not
2443          --  appropriate to generate warnings in that case.
2444
2445          if No (E) then
2446             if (Is_Access_Type (T)
2447                  or else not Is_Fully_Initialized_Type (T))
2448               and then Comes_From_Source (N)
2449             then
2450                Set_Never_Set_In_Source (Id);
2451             end if;
2452          end if;
2453       end if;
2454
2455       Init_Alignment (Id);
2456       Init_Esize     (Id);
2457
2458       if Aliased_Present (N) then
2459          Set_Is_Aliased (Id);
2460
2461          --  If the object is aliased and the type is unconstrained with
2462          --  defaulted discriminants and there is no expression, then the
2463          --  object is constrained by the defaults, so it is worthwhile
2464          --  building the corresponding subtype.
2465
2466          --  Ada 2005 (AI-363): If the aliased object is discriminated and
2467          --  unconstrained, then only establish an actual subtype if the
2468          --  nominal subtype is indefinite. In definite cases the object is
2469          --  unconstrained in Ada 2005.
2470
2471          if No (E)
2472            and then Is_Record_Type (T)
2473            and then not Is_Constrained (T)
2474            and then Has_Discriminants (T)
2475            and then (Ada_Version < Ada_05 or else Is_Indefinite_Subtype (T))
2476          then
2477             Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
2478          end if;
2479       end if;
2480
2481       Set_Etype (Id, Act_T);
2482
2483       if Has_Controlled_Component (Etype (Id))
2484         or else Is_Controlled (Etype (Id))
2485       then
2486          if not Is_Library_Level_Entity (Id) then
2487             Check_Restriction (No_Nested_Finalization, N);
2488          else
2489             Validate_Controlled_Object (Id);
2490          end if;
2491
2492          --  Generate a warning when an initialization causes an obvious ABE
2493          --  violation. If the init expression is a simple aggregate there
2494          --  shouldn't be any initialize/adjust call generated. This will be
2495          --  true as soon as aggregates are built in place when possible.
2496
2497          --  ??? at the moment we do not generate warnings for temporaries
2498          --  created for those aggregates although Program_Error might be
2499          --  generated if compiled with -gnato.
2500
2501          if Is_Controlled (Etype (Id))
2502             and then Comes_From_Source (Id)
2503          then
2504             declare
2505                BT : constant Entity_Id := Base_Type (Etype (Id));
2506
2507                Implicit_Call : Entity_Id;
2508                pragma Warnings (Off, Implicit_Call);
2509                --  ??? what is this for (never referenced!)
2510
2511                function Is_Aggr (N : Node_Id) return Boolean;
2512                --  Check that N is an aggregate
2513
2514                -------------
2515                -- Is_Aggr --
2516                -------------
2517
2518                function Is_Aggr (N : Node_Id) return Boolean is
2519                begin
2520                   case Nkind (Original_Node (N)) is
2521                      when N_Aggregate | N_Extension_Aggregate =>
2522                         return True;
2523
2524                      when N_Qualified_Expression |
2525                           N_Type_Conversion      |
2526                           N_Unchecked_Type_Conversion =>
2527                         return Is_Aggr (Expression (Original_Node (N)));
2528
2529                      when others =>
2530                         return False;
2531                   end case;
2532                end Is_Aggr;
2533
2534             begin
2535                --  If no underlying type, we already are in an error situation.
2536                --  Do not try to add a warning since we do not have access to
2537                --  prim-op list.
2538
2539                if No (Underlying_Type (BT)) then
2540                   Implicit_Call := Empty;
2541
2542                --  A generic type does not have usable primitive operators.
2543                --  Initialization calls are built for instances.
2544
2545                elsif Is_Generic_Type (BT) then
2546                   Implicit_Call := Empty;
2547
2548                --  If the init expression is not an aggregate, an adjust call
2549                --  will be generated
2550
2551                elsif Present (E) and then not Is_Aggr (E) then
2552                   Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
2553
2554                --  If no init expression and we are not in the deferred
2555                --  constant case, an Initialize call will be generated
2556
2557                elsif No (E) and then not Constant_Present (N) then
2558                   Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
2559
2560                else
2561                   Implicit_Call := Empty;
2562                end if;
2563             end;
2564          end if;
2565       end if;
2566
2567       if Has_Task (Etype (Id)) then
2568          Check_Restriction (No_Tasking, N);
2569
2570          if Is_Library_Level_Entity (Id) then
2571             Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
2572          else
2573             Check_Restriction (Max_Tasks, N);
2574             Check_Restriction (No_Task_Hierarchy, N);
2575             Check_Potentially_Blocking_Operation (N);
2576          end if;
2577
2578          --  A rather specialized test. If we see two tasks being declared
2579          --  of the same type in the same object declaration, and the task
2580          --  has an entry with an address clause, we know that program error
2581          --  will be raised at run-time since we can't have two tasks with
2582          --  entries at the same address.
2583
2584          if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
2585             declare
2586                E : Entity_Id;
2587
2588             begin
2589                E := First_Entity (Etype (Id));
2590                while Present (E) loop
2591                   if Ekind (E) = E_Entry
2592                     and then Present (Get_Attribute_Definition_Clause
2593                                         (E, Attribute_Address))
2594                   then
2595                      Error_Msg_N
2596                        ("?more than one task with same entry address", N);
2597                      Error_Msg_N
2598                        ("\?Program_Error will be raised at run time", N);
2599                      Insert_Action (N,
2600                        Make_Raise_Program_Error (Loc,
2601                          Reason => PE_Duplicated_Entry_Address));
2602                      exit;
2603                   end if;
2604
2605                   Next_Entity (E);
2606                end loop;
2607             end;
2608          end if;
2609       end if;
2610
2611       --  Some simple constant-propagation: if the expression is a constant
2612       --  string initialized with a literal, share the literal. This avoids
2613       --  a run-time copy.
2614
2615       if Present (E)
2616         and then Is_Entity_Name (E)
2617         and then Ekind (Entity (E)) = E_Constant
2618         and then Base_Type (Etype (E)) = Standard_String
2619       then
2620          declare
2621             Val : constant Node_Id := Constant_Value (Entity (E));
2622          begin
2623             if Present (Val)
2624               and then Nkind (Val) = N_String_Literal
2625             then
2626                Rewrite (E, New_Copy (Val));
2627             end if;
2628          end;
2629       end if;
2630
2631       --  Another optimization: if the nominal subtype is unconstrained and
2632       --  the expression is a function call that returns an unconstrained
2633       --  type, rewrite the declaration as a renaming of the result of the
2634       --  call. The exceptions below are cases where the copy is expected,
2635       --  either by the back end (Aliased case) or by the semantics, as for
2636       --  initializing controlled types or copying tags for classwide types.
2637
2638       if Present (E)
2639         and then Nkind (E) = N_Explicit_Dereference
2640         and then Nkind (Original_Node (E)) = N_Function_Call
2641         and then not Is_Library_Level_Entity (Id)
2642         and then not Is_Constrained (Underlying_Type (T))
2643         and then not Is_Aliased (Id)
2644         and then not Is_Class_Wide_Type (T)
2645         and then not Is_Controlled (T)
2646         and then not Has_Controlled_Component (Base_Type (T))
2647         and then Expander_Active
2648       then
2649          Rewrite (N,
2650            Make_Object_Renaming_Declaration (Loc,
2651              Defining_Identifier => Id,
2652              Access_Definition   => Empty,
2653              Subtype_Mark        => New_Occurrence_Of
2654                                       (Base_Type (Etype (Id)), Loc),
2655              Name                => E));
2656
2657          Set_Renamed_Object (Id, E);
2658
2659          --  Force generation of debugging information for the constant and for
2660          --  the renamed function call.
2661
2662          Set_Needs_Debug_Info (Id);
2663          Set_Needs_Debug_Info (Entity (Prefix (E)));
2664       end if;
2665
2666       if Present (Prev_Entity)
2667         and then Is_Frozen (Prev_Entity)
2668         and then not Error_Posted (Id)
2669       then
2670          Error_Msg_N ("full constant declaration appears too late", N);
2671       end if;
2672
2673       Check_Eliminated (Id);
2674
2675       --  Deal with setting In_Private_Part flag if in private part
2676
2677       if Ekind (Scope (Id)) = E_Package
2678         and then In_Private_Part (Scope (Id))
2679       then
2680          Set_In_Private_Part (Id);
2681       end if;
2682    end Analyze_Object_Declaration;
2683
2684    ---------------------------
2685    -- Analyze_Others_Choice --
2686    ---------------------------
2687
2688    --  Nothing to do for the others choice node itself, the semantic analysis
2689    --  of the others choice will occur as part of the processing of the parent
2690
2691    procedure Analyze_Others_Choice (N : Node_Id) is
2692       pragma Warnings (Off, N);
2693    begin
2694       null;
2695    end Analyze_Others_Choice;
2696
2697    --------------------------------
2698    -- Analyze_Per_Use_Expression --
2699    --------------------------------
2700
2701    procedure Analyze_Per_Use_Expression (N : Node_Id; T : Entity_Id) is
2702       Save_In_Default_Expression : constant Boolean := In_Default_Expression;
2703    begin
2704       In_Default_Expression := True;
2705       Pre_Analyze_And_Resolve (N, T);
2706       In_Default_Expression := Save_In_Default_Expression;
2707    end Analyze_Per_Use_Expression;
2708
2709    -------------------------------------------
2710    -- Analyze_Private_Extension_Declaration --
2711    -------------------------------------------
2712
2713    procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
2714       T           : constant Entity_Id := Defining_Identifier (N);
2715       Indic       : constant Node_Id   := Subtype_Indication (N);
2716       Parent_Type : Entity_Id;
2717       Parent_Base : Entity_Id;
2718
2719    begin
2720       --  Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
2721
2722       if Is_Non_Empty_List (Interface_List (N)) then
2723          declare
2724             Intf : Node_Id;
2725             T    : Entity_Id;
2726
2727          begin
2728             Intf := First (Interface_List (N));
2729             while Present (Intf) loop
2730                T := Find_Type_Of_Subtype_Indic (Intf);
2731
2732                if not Is_Interface (T) then
2733                   Error_Msg_NE ("(Ada 2005) & must be an interface", Intf, T);
2734                end if;
2735
2736                Next (Intf);
2737             end loop;
2738          end;
2739       end if;
2740
2741       Generate_Definition (T);
2742       Enter_Name (T);
2743
2744       Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
2745       Parent_Base := Base_Type (Parent_Type);
2746
2747       if Parent_Type = Any_Type
2748         or else Etype (Parent_Type) = Any_Type
2749       then
2750          Set_Ekind (T, Ekind (Parent_Type));
2751          Set_Etype (T, Any_Type);
2752          return;
2753
2754       elsif not Is_Tagged_Type (Parent_Type) then
2755          Error_Msg_N
2756            ("parent of type extension must be a tagged type ", Indic);
2757          return;
2758
2759       elsif Ekind (Parent_Type) = E_Void
2760         or else Ekind (Parent_Type) = E_Incomplete_Type
2761       then
2762          Error_Msg_N ("premature derivation of incomplete type", Indic);
2763          return;
2764       end if;
2765
2766       --  Perhaps the parent type should be changed to the class-wide type's
2767       --  specific type in this case to prevent cascading errors ???
2768
2769       if Is_Class_Wide_Type (Parent_Type) then
2770          Error_Msg_N
2771            ("parent of type extension must not be a class-wide type", Indic);
2772          return;
2773       end if;
2774
2775       if (not Is_Package_Or_Generic_Package (Current_Scope)
2776            and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
2777         or else In_Private_Part (Current_Scope)
2778
2779       then
2780          Error_Msg_N ("invalid context for private extension", N);
2781       end if;
2782
2783       --  Set common attributes
2784
2785       Set_Is_Pure          (T, Is_Pure (Current_Scope));
2786       Set_Scope            (T, Current_Scope);
2787       Set_Ekind            (T, E_Record_Type_With_Private);
2788       Init_Size_Align      (T);
2789
2790       Set_Etype            (T,            Parent_Base);
2791       Set_Has_Task         (T, Has_Task  (Parent_Base));
2792
2793       Set_Convention       (T, Convention     (Parent_Type));
2794       Set_First_Rep_Item   (T, First_Rep_Item (Parent_Type));
2795       Set_Is_First_Subtype (T);
2796       Make_Class_Wide_Type (T);
2797
2798       if Unknown_Discriminants_Present (N) then
2799          Set_Discriminant_Constraint (T, No_Elist);
2800       end if;
2801
2802       Build_Derived_Record_Type (N, Parent_Type, T);
2803
2804       --  Ada 2005 (AI-443): Synchronized private extension or a rewritten
2805       --  synchronized formal derived type.
2806
2807       if Ada_Version >= Ada_05
2808         and then Synchronized_Present (N)
2809       then
2810          Set_Is_Limited_Record (T);
2811
2812          --  Formal derived type case
2813
2814          if Is_Generic_Type (T) then
2815
2816             --  The parent must be a tagged limited type or a synchronized
2817             --  interface.
2818
2819             if (not Is_Tagged_Type (Parent_Type)
2820                   or else not Is_Limited_Type (Parent_Type))
2821               and then
2822                (not Is_Interface (Parent_Type)
2823                   or else not Is_Synchronized_Interface (Parent_Type))
2824             then
2825                Error_Msg_NE ("parent type of & must be tagged limited " &
2826                              "or synchronized", N, T);
2827             end if;
2828
2829             --  The progenitors (if any) must be limited or synchronized
2830             --  interfaces.
2831
2832             if Present (Abstract_Interfaces (T)) then
2833                declare
2834                   Iface      : Entity_Id;
2835                   Iface_Elmt : Elmt_Id;
2836
2837                begin
2838                   Iface_Elmt := First_Elmt (Abstract_Interfaces (T));
2839                   while Present (Iface_Elmt) loop
2840                      Iface := Node (Iface_Elmt);
2841
2842                      if not Is_Limited_Interface (Iface)
2843                        and then not Is_Synchronized_Interface (Iface)
2844                      then
2845                         Error_Msg_NE ("progenitor & must be limited " &
2846                                       "or synchronized", N, Iface);
2847                      end if;
2848
2849                      Next_Elmt (Iface_Elmt);
2850                   end loop;
2851                end;
2852             end if;
2853
2854          --  Regular derived extension, the parent must be a limited or
2855          --  synchronized interface.
2856
2857          else
2858             if not Is_Interface (Parent_Type)
2859               or else (not Is_Limited_Interface (Parent_Type)
2860                          and then
2861                        not Is_Synchronized_Interface (Parent_Type))
2862             then
2863                Error_Msg_NE
2864                  ("parent type of & must be limited interface", N, T);
2865             end if;
2866          end if;
2867
2868       elsif Limited_Present (N) then
2869          Set_Is_Limited_Record (T);
2870
2871          if not Is_Limited_Type (Parent_Type)
2872            and then
2873              (not Is_Interface (Parent_Type)
2874                or else not Is_Limited_Interface (Parent_Type))
2875          then
2876             Error_Msg_NE ("parent type& of limited extension must be limited",
2877               N, Parent_Type);
2878          end if;
2879       end if;
2880    end Analyze_Private_Extension_Declaration;
2881
2882    ---------------------------------
2883    -- Analyze_Subtype_Declaration --
2884    ---------------------------------
2885
2886    procedure Analyze_Subtype_Declaration
2887      (N    : Node_Id;
2888       Skip : Boolean := False)
2889    is
2890       Id       : constant Entity_Id := Defining_Identifier (N);
2891       T        : Entity_Id;
2892       R_Checks : Check_Result;
2893
2894    begin
2895       Generate_Definition (Id);
2896       Set_Is_Pure (Id, Is_Pure (Current_Scope));
2897       Init_Size_Align (Id);
2898
2899       --  The following guard condition on Enter_Name is to handle cases where
2900       --  the defining identifier has already been entered into the scope but
2901       --  the declaration as a whole needs to be analyzed.
2902
2903       --  This case in particular happens for derived enumeration types. The
2904       --  derived enumeration type is processed as an inserted enumeration type
2905       --  declaration followed by a rewritten subtype declaration. The defining
2906       --  identifier, however, is entered into the name scope very early in the
2907       --  processing of the original type declaration and therefore needs to be
2908       --  avoided here, when the created subtype declaration is analyzed. (See
2909       --  Build_Derived_Types)
2910
2911       --  This also happens when the full view of a private type is derived
2912       --  type with constraints. In this case the entity has been introduced
2913       --  in the private declaration.
2914
2915       if Skip
2916         or else (Present (Etype (Id))
2917                    and then (Is_Private_Type (Etype (Id))
2918                                or else Is_Task_Type (Etype (Id))
2919                                or else Is_Rewrite_Substitution (N)))
2920       then
2921          null;
2922
2923       else
2924          Enter_Name (Id);
2925       end if;
2926
2927       T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
2928
2929       --  Inherit common attributes
2930
2931       Set_Is_Generic_Type   (Id, Is_Generic_Type   (Base_Type (T)));
2932       Set_Is_Volatile       (Id, Is_Volatile       (T));
2933       Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
2934       Set_Is_Atomic         (Id, Is_Atomic         (T));
2935       Set_Is_Ada_2005_Only  (Id, Is_Ada_2005_Only  (T));
2936
2937       --  In the case where there is no constraint given in the subtype
2938       --  indication, Process_Subtype just returns the Subtype_Mark, so its
2939       --  semantic attributes must be established here.
2940
2941       if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
2942          Set_Etype (Id, Base_Type (T));
2943
2944          case Ekind (T) is
2945             when Array_Kind =>
2946                Set_Ekind                       (Id, E_Array_Subtype);
2947                Copy_Array_Subtype_Attributes   (Id, T);
2948
2949             when Decimal_Fixed_Point_Kind =>
2950                Set_Ekind                (Id, E_Decimal_Fixed_Point_Subtype);
2951                Set_Digits_Value         (Id, Digits_Value       (T));
2952                Set_Delta_Value          (Id, Delta_Value        (T));
2953                Set_Scale_Value          (Id, Scale_Value        (T));
2954                Set_Small_Value          (Id, Small_Value        (T));
2955                Set_Scalar_Range         (Id, Scalar_Range       (T));
2956                Set_Machine_Radix_10     (Id, Machine_Radix_10   (T));
2957                Set_Is_Constrained       (Id, Is_Constrained     (T));
2958                Set_RM_Size              (Id, RM_Size            (T));
2959
2960             when Enumeration_Kind =>
2961                Set_Ekind                (Id, E_Enumeration_Subtype);
2962                Set_First_Literal        (Id, First_Literal (Base_Type (T)));
2963                Set_Scalar_Range         (Id, Scalar_Range       (T));
2964                Set_Is_Character_Type    (Id, Is_Character_Type  (T));
2965                Set_Is_Constrained       (Id, Is_Constrained     (T));
2966                Set_RM_Size              (Id, RM_Size            (T));
2967
2968             when Ordinary_Fixed_Point_Kind =>
2969                Set_Ekind                (Id, E_Ordinary_Fixed_Point_Subtype);
2970                Set_Scalar_Range         (Id, Scalar_Range       (T));
2971                Set_Small_Value          (Id, Small_Value        (T));
2972                Set_Delta_Value          (Id, Delta_Value        (T));
2973                Set_Is_Constrained       (Id, Is_Constrained     (T));
2974                Set_RM_Size              (Id, RM_Size            (T));
2975
2976             when Float_Kind =>
2977                Set_Ekind                (Id, E_Floating_Point_Subtype);
2978                Set_Scalar_Range         (Id, Scalar_Range       (T));
2979                Set_Digits_Value         (Id, Digits_Value       (T));
2980                Set_Is_Constrained       (Id, Is_Constrained     (T));
2981
2982             when Signed_Integer_Kind =>
2983                Set_Ekind                (Id, E_Signed_Integer_Subtype);
2984                Set_Scalar_Range         (Id, Scalar_Range       (T));
2985                Set_Is_Constrained       (Id, Is_Constrained     (T));
2986                Set_RM_Size              (Id, RM_Size            (T));
2987
2988             when Modular_Integer_Kind =>
2989                Set_Ekind                (Id, E_Modular_Integer_Subtype);
2990                Set_Scalar_Range         (Id, Scalar_Range       (T));
2991                Set_Is_Constrained       (Id, Is_Constrained     (T));
2992                Set_RM_Size              (Id, RM_Size            (T));
2993
2994             when Class_Wide_Kind =>
2995                Set_Ekind                (Id, E_Class_Wide_Subtype);
2996                Set_First_Entity         (Id, First_Entity       (T));
2997                Set_Last_Entity          (Id, Last_Entity        (T));
2998                Set_Class_Wide_Type      (Id, Class_Wide_Type    (T));
2999                Set_Cloned_Subtype       (Id, T);
3000                Set_Is_Tagged_Type       (Id, True);
3001                Set_Has_Unknown_Discriminants
3002                                         (Id, True);
3003
3004                if Ekind (T) = E_Class_Wide_Subtype then
3005                   Set_Equivalent_Type   (Id, Equivalent_Type    (T));
3006                end if;
3007
3008             when E_Record_Type | E_Record_Subtype =>
3009                Set_Ekind                (Id, E_Record_Subtype);
3010
3011                if Ekind (T) = E_Record_Subtype
3012                  and then Present (Cloned_Subtype (T))
3013                then
3014                   Set_Cloned_Subtype    (Id, Cloned_Subtype (T));
3015                else
3016                   Set_Cloned_Subtype    (Id, T);
3017                end if;
3018
3019                Set_First_Entity         (Id, First_Entity       (T));
3020                Set_Last_Entity          (Id, Last_Entity        (T));
3021                Set_Has_Discriminants    (Id, Has_Discriminants  (T));
3022                Set_Is_Constrained       (Id, Is_Constrained     (T));
3023                Set_Is_Limited_Record    (Id, Is_Limited_Record  (T));
3024                Set_Has_Unknown_Discriminants
3025                                         (Id, Has_Unknown_Discriminants (T));
3026
3027                if Has_Discriminants (T) then
3028                   Set_Discriminant_Constraint
3029                                         (Id, Discriminant_Constraint (T));
3030                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3031
3032                elsif Has_Unknown_Discriminants (Id) then
3033                   Set_Discriminant_Constraint (Id, No_Elist);
3034                end if;
3035
3036                if Is_Tagged_Type (T) then
3037                   Set_Is_Tagged_Type    (Id);
3038                   Set_Is_Abstract       (Id, Is_Abstract (T));
3039                   Set_Primitive_Operations
3040                                         (Id, Primitive_Operations (T));
3041                   Set_Class_Wide_Type   (Id, Class_Wide_Type (T));
3042                end if;
3043
3044             when Private_Kind =>
3045                Set_Ekind              (Id, Subtype_Kind (Ekind   (T)));
3046                Set_Has_Discriminants  (Id, Has_Discriminants     (T));
3047                Set_Is_Constrained     (Id, Is_Constrained        (T));
3048                Set_First_Entity       (Id, First_Entity          (T));
3049                Set_Last_Entity        (Id, Last_Entity           (T));
3050                Set_Private_Dependents (Id, New_Elmt_List);
3051                Set_Is_Limited_Record  (Id, Is_Limited_Record     (T));
3052                Set_Has_Unknown_Discriminants
3053                                       (Id, Has_Unknown_Discriminants (T));
3054
3055                if Is_Tagged_Type (T) then
3056                   Set_Is_Tagged_Type  (Id);
3057                   Set_Is_Abstract     (Id, Is_Abstract (T));
3058                   Set_Primitive_Operations
3059                                         (Id, Primitive_Operations (T));
3060                   Set_Class_Wide_Type (Id, Class_Wide_Type (T));
3061                end if;
3062
3063                --  In general the attributes of the subtype of a private type
3064                --  are the attributes of the partial view of parent. However,
3065                --  the full view may be a discriminated type, and the subtype
3066                --  must share the discriminant constraint to generate correct
3067                --  calls to initialization procedures.
3068
3069                if Has_Discriminants (T) then
3070                   Set_Discriminant_Constraint
3071                                      (Id, Discriminant_Constraint (T));
3072                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3073
3074                elsif Present (Full_View (T))
3075                  and then Has_Discriminants (Full_View (T))
3076                then
3077                   Set_Discriminant_Constraint
3078                                (Id, Discriminant_Constraint (Full_View (T)));
3079                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3080
3081                   --  This would seem semantically correct, but apparently
3082                   --  confuses the back-end (4412-009). To be explained ???
3083
3084                   --  Set_Has_Discriminants (Id);
3085                end if;
3086
3087                Prepare_Private_Subtype_Completion (Id, N);
3088
3089             when Access_Kind =>
3090                Set_Ekind             (Id, E_Access_Subtype);
3091                Set_Is_Constrained    (Id, Is_Constrained        (T));
3092                Set_Is_Access_Constant
3093                                      (Id, Is_Access_Constant    (T));
3094                Set_Directly_Designated_Type
3095                                      (Id, Designated_Type       (T));
3096                Set_Can_Never_Be_Null (Id, Can_Never_Be_Null     (T));
3097
3098                --  A Pure library_item must not contain the declaration of a
3099                --  named access type, except within a subprogram, generic
3100                --  subprogram, task unit, or protected unit (RM 10.2.1(16)).
3101
3102                if Comes_From_Source (Id)
3103                  and then In_Pure_Unit
3104                  and then not In_Subprogram_Task_Protected_Unit
3105                then
3106                   Error_Msg_N
3107                     ("named access types not allowed in pure unit", N);
3108                end if;
3109
3110             when Concurrent_Kind =>
3111                Set_Ekind                (Id, Subtype_Kind (Ekind   (T)));
3112                Set_Corresponding_Record_Type (Id,
3113                                          Corresponding_Record_Type (T));
3114                Set_First_Entity         (Id, First_Entity          (T));
3115                Set_First_Private_Entity (Id, First_Private_Entity  (T));
3116                Set_Has_Discriminants    (Id, Has_Discriminants     (T));
3117                Set_Is_Constrained       (Id, Is_Constrained        (T));
3118                Set_Last_Entity          (Id, Last_Entity           (T));
3119
3120                if Has_Discriminants (T) then
3121                   Set_Discriminant_Constraint (Id,
3122                                            Discriminant_Constraint (T));
3123                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3124                end if;
3125
3126             when E_Incomplete_Type =>
3127                if Ada_Version >= Ada_05 then
3128                   Set_Ekind (Id, E_Incomplete_Subtype);
3129
3130                   --  Ada 2005 (AI-412): Decorate an incomplete subtype
3131                   --  of an incomplete type visible through a limited
3132                   --  with clause.
3133
3134                   if From_With_Type (T)
3135                     and then Present (Non_Limited_View (T))
3136                   then
3137                      Set_From_With_Type   (Id);
3138                      Set_Non_Limited_View (Id, Non_Limited_View (T));
3139
3140                   --  Ada 2005 (AI-412): Add the regular incomplete subtype
3141                   --  to the private dependents of the original incomplete
3142                   --  type for future transformation.
3143
3144                   else
3145                      Append_Elmt (Id, Private_Dependents (T));
3146                   end if;
3147
3148                --  If the subtype name denotes an incomplete type an error
3149                --  was already reported by Process_Subtype.
3150
3151                else
3152                   Set_Etype (Id, Any_Type);
3153                end if;
3154
3155             when others =>
3156                raise Program_Error;
3157          end case;
3158       end if;
3159
3160       if Etype (Id) = Any_Type then
3161          return;
3162       end if;
3163
3164       --  Some common processing on all types
3165
3166       Set_Size_Info      (Id,                 T);
3167       Set_First_Rep_Item (Id, First_Rep_Item (T));
3168
3169       T := Etype (Id);
3170
3171       Set_Is_Immediately_Visible (Id, True);
3172       Set_Depends_On_Private     (Id, Has_Private_Component (T));
3173
3174       if Present (Generic_Parent_Type (N))
3175         and then
3176           (Nkind
3177              (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
3178             or else Nkind
3179               (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
3180                 /=  N_Formal_Private_Type_Definition)
3181       then
3182          if Is_Tagged_Type (Id) then
3183             if Is_Class_Wide_Type (Id) then
3184                Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
3185             else
3186                Derive_Subprograms (Generic_Parent_Type (N), Id, T);
3187             end if;
3188
3189          elsif Scope (Etype (Id)) /= Standard_Standard then
3190             Derive_Subprograms (Generic_Parent_Type (N), Id);
3191          end if;
3192       end if;
3193
3194       if Is_Private_Type (T)
3195         and then Present (Full_View (T))
3196       then
3197          Conditional_Delay (Id, Full_View (T));
3198
3199       --  The subtypes of components or subcomponents of protected types
3200       --  do not need freeze nodes, which would otherwise appear in the
3201       --  wrong scope (before the freeze node for the protected type). The
3202       --  proper subtypes are those of the subcomponents of the corresponding
3203       --  record.
3204
3205       elsif Ekind (Scope (Id)) /= E_Protected_Type
3206         and then Present (Scope (Scope (Id))) -- error defense!
3207         and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
3208       then
3209          Conditional_Delay (Id, T);
3210       end if;
3211
3212       --  Check that constraint_error is raised for a scalar subtype
3213       --  indication when the lower or upper bound of a non-null range
3214       --  lies outside the range of the type mark.
3215
3216       if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
3217          if Is_Scalar_Type (Etype (Id))
3218             and then Scalar_Range (Id) /=
3219                      Scalar_Range (Etype (Subtype_Mark
3220                                            (Subtype_Indication (N))))
3221          then
3222             Apply_Range_Check
3223               (Scalar_Range (Id),
3224                Etype (Subtype_Mark (Subtype_Indication (N))));
3225
3226          elsif Is_Array_Type (Etype (Id))
3227            and then Present (First_Index (Id))
3228          then
3229             --  This really should be a subprogram that finds the indications
3230             --  to check???
3231
3232             if ((Nkind (First_Index (Id)) = N_Identifier
3233                    and then Ekind (Entity (First_Index (Id))) in Scalar_Kind)
3234                  or else Nkind (First_Index (Id)) = N_Subtype_Indication)
3235               and then
3236                 Nkind (Scalar_Range (Etype (First_Index (Id)))) = N_Range
3237             then
3238                declare
3239                   Target_Typ : constant Entity_Id :=
3240                                  Etype
3241                                    (First_Index (Etype
3242                                      (Subtype_Mark (Subtype_Indication (N)))));
3243                begin
3244                   R_Checks :=
3245                     Range_Check
3246                       (Scalar_Range (Etype (First_Index (Id))),
3247                        Target_Typ,
3248                        Etype (First_Index (Id)),
3249                        Defining_Identifier (N));
3250
3251                   Insert_Range_Checks
3252                     (R_Checks,
3253                      N,
3254                      Target_Typ,
3255                      Sloc (Defining_Identifier (N)));
3256                end;
3257             end if;
3258          end if;
3259       end if;
3260
3261       Check_Eliminated (Id);
3262    end Analyze_Subtype_Declaration;
3263
3264    --------------------------------
3265    -- Analyze_Subtype_Indication --
3266    --------------------------------
3267
3268    procedure Analyze_Subtype_Indication (N : Node_Id) is
3269       T : constant Entity_Id := Subtype_Mark (N);
3270       R : constant Node_Id   := Range_Expression (Constraint (N));
3271
3272    begin
3273       Analyze (T);
3274
3275       if R /= Error then
3276          Analyze (R);
3277          Set_Etype (N, Etype (R));
3278       else
3279          Set_Error_Posted (R);
3280          Set_Error_Posted (T);
3281       end if;
3282    end Analyze_Subtype_Indication;
3283
3284    ------------------------------
3285    -- Analyze_Type_Declaration --
3286    ------------------------------
3287
3288    procedure Analyze_Type_Declaration (N : Node_Id) is
3289       Def    : constant Node_Id   := Type_Definition (N);
3290       Def_Id : constant Entity_Id := Defining_Identifier (N);
3291       T      : Entity_Id;
3292       Prev   : Entity_Id;
3293
3294       Is_Remote : constant Boolean :=
3295                     (Is_Remote_Types (Current_Scope)
3296                           or else Is_Remote_Call_Interface (Current_Scope))
3297                        and then not (In_Private_Part (Current_Scope)
3298                                        or else
3299                                      In_Package_Body (Current_Scope));
3300
3301       procedure Check_Ops_From_Incomplete_Type;
3302       --  If there is a tagged incomplete partial view of the type, transfer
3303       --  its operations to the full view, and indicate that the type of the
3304       --  controlling parameter (s) is this full view.
3305
3306       ------------------------------------
3307       -- Check_Ops_From_Incomplete_Type --
3308       ------------------------------------
3309
3310       procedure Check_Ops_From_Incomplete_Type is
3311          Elmt   : Elmt_Id;
3312          Formal : Entity_Id;
3313          Op     : Entity_Id;
3314
3315       begin
3316          if Prev /= T
3317            and then Ekind (Prev) = E_Incomplete_Type
3318            and then Is_Tagged_Type (Prev)
3319            and then Is_Tagged_Type (T)
3320          then
3321             Elmt := First_Elmt (Primitive_Operations (Prev));
3322             while Present (Elmt) loop
3323                Op := Node (Elmt);
3324                Prepend_Elmt (Op, Primitive_Operations (T));
3325
3326                Formal := First_Formal (Op);
3327                while Present (Formal) loop
3328                   if Etype (Formal) = Prev then
3329                      Set_Etype (Formal, T);
3330                   end if;
3331
3332                   Next_Formal (Formal);
3333                end loop;
3334
3335                if Etype (Op) = Prev then
3336                   Set_Etype (Op, T);
3337                end if;
3338
3339                Next_Elmt (Elmt);
3340             end loop;
3341          end if;
3342       end Check_Ops_From_Incomplete_Type;
3343
3344    --  Start of processing for Analyze_Type_Declaration
3345
3346    begin
3347       Prev := Find_Type_Name (N);
3348
3349       --  The full view, if present, now points to the current type
3350
3351       --  Ada 2005 (AI-50217): If the type was previously decorated when
3352       --  imported through a LIMITED WITH clause, it appears as incomplete
3353       --  but has no full view.
3354
3355       if Ekind (Prev) = E_Incomplete_Type
3356         and then Present (Full_View (Prev))
3357       then
3358          T := Full_View (Prev);
3359       else
3360          T := Prev;
3361       end if;
3362
3363       Set_Is_Pure (T, Is_Pure (Current_Scope));
3364
3365       --  We set the flag Is_First_Subtype here. It is needed to set the
3366       --  corresponding flag for the Implicit class-wide-type created
3367       --  during tagged types processing.
3368
3369       Set_Is_First_Subtype (T, True);
3370
3371       --  Only composite types other than array types are allowed to have
3372       --  discriminants.
3373
3374       case Nkind (Def) is
3375
3376          --  For derived types, the rule will be checked once we've figured
3377          --  out the parent type.
3378
3379          when N_Derived_Type_Definition =>
3380             null;
3381
3382          --  For record types, discriminants are allowed
3383
3384          when N_Record_Definition =>
3385             null;
3386
3387          when others =>
3388             if Present (Discriminant_Specifications (N)) then
3389                Error_Msg_N
3390                  ("elementary or array type cannot have discriminants",
3391                   Defining_Identifier
3392                   (First (Discriminant_Specifications (N))));
3393             end if;
3394       end case;
3395
3396       --  Elaborate the type definition according to kind, and generate
3397       --  subsidiary (implicit) subtypes where needed. We skip this if it was
3398       --  already done (this happens during the reanalysis that follows a call
3399       --  to the high level optimizer).
3400
3401       if not Analyzed (T) then
3402          Set_Analyzed (T);
3403
3404          case Nkind (Def) is
3405
3406             when N_Access_To_Subprogram_Definition =>
3407                Access_Subprogram_Declaration (T, Def);
3408
3409                --  If this is a remote access to subprogram, we must create the
3410                --  equivalent fat pointer type, and related subprograms.
3411
3412                if Is_Remote then
3413                   Process_Remote_AST_Declaration (N);
3414                end if;
3415
3416                --  Validate categorization rule against access type declaration
3417                --  usually a violation in Pure unit, Shared_Passive unit.
3418
3419                Validate_Access_Type_Declaration (T, N);
3420
3421             when N_Access_To_Object_Definition =>
3422                Access_Type_Declaration (T, Def);
3423
3424                --  Validate categorization rule against access type declaration
3425                --  usually a violation in Pure unit, Shared_Passive unit.
3426
3427                Validate_Access_Type_Declaration (T, N);
3428
3429                --  If we are in a Remote_Call_Interface package and define
3430                --  a RACW, Read and Write attribute must be added.
3431
3432                if Is_Remote
3433                  and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3434                then
3435                   Add_RACW_Features (Def_Id);
3436                end if;
3437
3438                --  Set no strict aliasing flag if config pragma seen
3439
3440                if Opt.No_Strict_Aliasing then
3441                   Set_No_Strict_Aliasing (Base_Type (Def_Id));
3442                end if;
3443
3444             when N_Array_Type_Definition =>
3445                Array_Type_Declaration (T, Def);
3446
3447             when N_Derived_Type_Definition =>
3448                Derived_Type_Declaration (T, N, T /= Def_Id);
3449
3450             when N_Enumeration_Type_Definition =>
3451                Enumeration_Type_Declaration (T, Def);
3452
3453             when N_Floating_Point_Definition =>
3454                Floating_Point_Type_Declaration (T, Def);
3455
3456             when N_Decimal_Fixed_Point_Definition =>
3457                Decimal_Fixed_Point_Type_Declaration (T, Def);
3458
3459             when N_Ordinary_Fixed_Point_Definition =>
3460                Ordinary_Fixed_Point_Type_Declaration (T, Def);
3461
3462             when N_Signed_Integer_Type_Definition =>
3463                Signed_Integer_Type_Declaration (T, Def);
3464
3465             when N_Modular_Type_Definition =>
3466                Modular_Type_Declaration (T, Def);
3467
3468             when N_Record_Definition =>
3469                Record_Type_Declaration (T, N, Prev);
3470
3471             when others =>
3472                raise Program_Error;
3473
3474          end case;
3475       end if;
3476
3477       if Etype (T) = Any_Type then
3478          return;
3479       end if;
3480
3481       --  Some common processing for all types
3482
3483       Set_Depends_On_Private (T, Has_Private_Component (T));
3484       Check_Ops_From_Incomplete_Type;
3485
3486       --  Both the declared entity, and its anonymous base type if one
3487       --  was created, need freeze nodes allocated.
3488
3489       declare
3490          B : constant Entity_Id := Base_Type (T);
3491
3492       begin
3493          --  In the case where the base type is different from the first
3494          --  subtype, we pre-allocate a freeze node, and set the proper link
3495          --  to the first subtype. Freeze_Entity will use this preallocated
3496          --  freeze node when it freezes the entity.
3497
3498          if B /= T then
3499             Ensure_Freeze_Node (B);
3500             Set_First_Subtype_Link (Freeze_Node (B), T);
3501          end if;
3502
3503          if not From_With_Type (T) then
3504             Set_Has_Delayed_Freeze (T);
3505          end if;
3506       end;
3507
3508       --  Case of T is the full declaration of some private type which has
3509       --  been swapped in Defining_Identifier (N).
3510
3511       if T /= Def_Id and then Is_Private_Type (Def_Id) then
3512          Process_Full_View (N, T, Def_Id);
3513
3514          --  Record the reference. The form of this is a little strange,
3515          --  since the full declaration has been swapped in. So the first
3516          --  parameter here represents the entity to which a reference is
3517          --  made which is the "real" entity, i.e. the one swapped in,
3518          --  and the second parameter provides the reference location.
3519
3520          Generate_Reference (T, T, 'c');
3521          Set_Completion_Referenced (Def_Id);
3522
3523       --  For completion of incomplete type, process incomplete dependents
3524       --  and always mark the full type as referenced (it is the incomplete
3525       --  type that we get for any real reference).
3526
3527       elsif Ekind (Prev) = E_Incomplete_Type then
3528          Process_Incomplete_Dependents (N, T, Prev);
3529          Generate_Reference (Prev, Def_Id, 'c');
3530          Set_Completion_Referenced (Def_Id);
3531
3532       --  If not private type or incomplete type completion, this is a real
3533       --  definition of a new entity, so record it.
3534
3535       else
3536          Generate_Definition (Def_Id);
3537       end if;
3538
3539       Check_Eliminated (Def_Id);
3540    end Analyze_Type_Declaration;
3541
3542    --------------------------
3543    -- Analyze_Variant_Part --
3544    --------------------------
3545
3546    procedure Analyze_Variant_Part (N : Node_Id) is
3547
3548       procedure Non_Static_Choice_Error (Choice : Node_Id);
3549       --  Error routine invoked by the generic instantiation below when
3550       --  the variant part has a non static choice.
3551
3552       procedure Process_Declarations (Variant : Node_Id);
3553       --  Analyzes all the declarations associated with a Variant.
3554       --  Needed by the generic instantiation below.
3555
3556       package Variant_Choices_Processing is new
3557         Generic_Choices_Processing
3558           (Get_Alternatives          => Variants,
3559            Get_Choices               => Discrete_Choices,
3560            Process_Empty_Choice      => No_OP,
3561            Process_Non_Static_Choice => Non_Static_Choice_Error,
3562            Process_Associated_Node   => Process_Declarations);
3563       use Variant_Choices_Processing;
3564       --  Instantiation of the generic choice processing package
3565
3566       -----------------------------
3567       -- Non_Static_Choice_Error --
3568       -----------------------------
3569
3570       procedure Non_Static_Choice_Error (Choice : Node_Id) is
3571       begin
3572          Flag_Non_Static_Expr
3573            ("choice given in variant part is not static!", Choice);
3574       end Non_Static_Choice_Error;
3575
3576       --------------------------
3577       -- Process_Declarations --
3578       --------------------------
3579
3580       procedure Process_Declarations (Variant : Node_Id) is
3581       begin
3582          if not Null_Present (Component_List (Variant)) then
3583             Analyze_Declarations (Component_Items (Component_List (Variant)));
3584
3585             if Present (Variant_Part (Component_List (Variant))) then
3586                Analyze (Variant_Part (Component_List (Variant)));
3587             end if;
3588          end if;
3589       end Process_Declarations;
3590
3591       --  Variables local to Analyze_Case_Statement
3592
3593       Discr_Name : Node_Id;
3594       Discr_Type : Entity_Id;
3595
3596       Case_Table     : Choice_Table_Type (1 .. Number_Of_Choices (N));
3597       Last_Choice    : Nat;
3598       Dont_Care      : Boolean;
3599       Others_Present : Boolean := False;
3600
3601    --  Start of processing for Analyze_Variant_Part
3602
3603    begin
3604       Discr_Name := Name (N);
3605       Analyze (Discr_Name);
3606
3607       if Ekind (Entity (Discr_Name)) /= E_Discriminant then
3608          Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
3609       end if;
3610
3611       Discr_Type := Etype (Entity (Discr_Name));
3612
3613       if not Is_Discrete_Type (Discr_Type) then
3614          Error_Msg_N
3615            ("discriminant in a variant part must be of a discrete type",
3616              Name (N));
3617          return;
3618       end if;
3619
3620       --  Call the instantiated Analyze_Choices which does the rest of the work
3621
3622       Analyze_Choices
3623         (N, Discr_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
3624    end Analyze_Variant_Part;
3625
3626    ----------------------------
3627    -- Array_Type_Declaration --
3628    ----------------------------
3629
3630    procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
3631       Component_Def : constant Node_Id := Component_Definition (Def);
3632       Element_Type  : Entity_Id;
3633       Implicit_Base : Entity_Id;
3634       Index         : Node_Id;
3635       Related_Id    : Entity_Id := Empty;
3636       Nb_Index      : Nat;
3637       P             : constant Node_Id := Parent (Def);
3638       Priv          : Entity_Id;
3639
3640    begin
3641       if Nkind (Def) = N_Constrained_Array_Definition then
3642          Index := First (Discrete_Subtype_Definitions (Def));
3643       else
3644          Index := First (Subtype_Marks (Def));
3645       end if;
3646
3647       --  Find proper names for the implicit types which may be public.
3648       --  in case of anonymous arrays we use the name of the first object
3649       --  of that type as prefix.
3650
3651       if No (T) then
3652          Related_Id :=  Defining_Identifier (P);
3653       else
3654          Related_Id := T;
3655       end if;
3656
3657       Nb_Index := 1;
3658       while Present (Index) loop
3659          Analyze (Index);
3660
3661          --  Add a subtype declaration for each index of private array type
3662          --  declaration whose etype is also private. For example:
3663
3664          --     package Pkg is
3665          --        type Index is private;
3666          --     private
3667          --        type Table is array (Index) of ...
3668          --     end;
3669
3670          --  This is currently required by the expander to generate the
3671          --  internally generated equality subprogram of records with variant
3672          --  parts in which the etype of some component is such private type.
3673
3674          if Ekind (Current_Scope) = E_Package
3675            and then In_Private_Part (Current_Scope)
3676            and then Has_Private_Declaration (Etype (Index))
3677          then
3678             declare
3679                Loc   : constant Source_Ptr := Sloc (Def);
3680                New_E : Entity_Id;
3681                Decl  : Entity_Id;
3682
3683             begin
3684                New_E :=
3685                  Make_Defining_Identifier (Loc,
3686                    Chars => New_Internal_Name ('T'));
3687                Set_Is_Internal (New_E);
3688
3689                Decl :=
3690                  Make_Subtype_Declaration (Loc,
3691                    Defining_Identifier => New_E,
3692                    Subtype_Indication  =>
3693                      New_Occurrence_Of (Etype (Index), Loc));
3694
3695                Insert_Before (Parent (Def), Decl);
3696                Analyze (Decl);
3697                Set_Etype (Index, New_E);
3698
3699                --  If the index is a range the Entity attribute is not
3700                --  available. Example:
3701
3702                --     package Pkg is
3703                --        type T is private;
3704                --     private
3705                --        type T is new Natural;
3706                --        Table : array (T(1) .. T(10)) of Boolean;
3707                --     end Pkg;
3708
3709                if Nkind (Index) /= N_Range then
3710                   Set_Entity (Index, New_E);
3711                end if;
3712             end;
3713          end if;
3714
3715          Make_Index (Index, P, Related_Id, Nb_Index);
3716          Next_Index (Index);
3717          Nb_Index := Nb_Index + 1;
3718       end loop;
3719
3720       --  Process subtype indication if one is present
3721
3722       if Present (Subtype_Indication (Component_Def)) then
3723          Element_Type :=
3724            Process_Subtype
3725              (Subtype_Indication (Component_Def), P, Related_Id, 'C');
3726
3727       --  Ada 2005 (AI-230): Access Definition case
3728
3729       else pragma Assert (Present (Access_Definition (Component_Def)));
3730          Element_Type := Access_Definition
3731                            (Related_Nod => Related_Id,
3732                             N           => Access_Definition (Component_Def));
3733          Set_Is_Local_Anonymous_Access (Element_Type);
3734
3735          --  Ada 2005 (AI-230): In case of components that are anonymous
3736          --  access types the level of accessibility depends on the enclosing
3737          --  type declaration
3738
3739          Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
3740
3741          --  Ada 2005 (AI-254)
3742
3743          declare
3744             CD : constant Node_Id :=
3745                    Access_To_Subprogram_Definition
3746                      (Access_Definition (Component_Def));
3747          begin
3748             if Present (CD) and then Protected_Present (CD) then
3749                Element_Type :=
3750                  Replace_Anonymous_Access_To_Protected_Subprogram
3751                    (Def, Element_Type);
3752             end if;
3753          end;
3754       end if;
3755
3756       --  Constrained array case
3757
3758       if No (T) then
3759          T := Create_Itype (E_Void, P, Related_Id, 'T');
3760       end if;
3761
3762       if Nkind (Def) = N_Constrained_Array_Definition then
3763
3764          --  Establish Implicit_Base as unconstrained base type
3765
3766          Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
3767
3768          Init_Size_Align        (Implicit_Base);
3769          Set_Etype              (Implicit_Base, Implicit_Base);
3770          Set_Scope              (Implicit_Base, Current_Scope);
3771          Set_Has_Delayed_Freeze (Implicit_Base);
3772
3773          --  The constrained array type is a subtype of the unconstrained one
3774
3775          Set_Ekind          (T, E_Array_Subtype);
3776          Init_Size_Align    (T);
3777          Set_Etype          (T, Implicit_Base);
3778          Set_Scope          (T, Current_Scope);
3779          Set_Is_Constrained (T, True);
3780          Set_First_Index    (T, First (Discrete_Subtype_Definitions (Def)));
3781          Set_Has_Delayed_Freeze (T);
3782
3783          --  Complete setup of implicit base type
3784
3785          Set_First_Index    (Implicit_Base, First_Index (T));
3786          Set_Component_Type (Implicit_Base, Element_Type);
3787          Set_Has_Task       (Implicit_Base, Has_Task      (Element_Type));
3788          Set_Component_Size (Implicit_Base, Uint_0);
3789          Set_Has_Controlled_Component
3790                             (Implicit_Base, Has_Controlled_Component
3791                                                           (Element_Type)
3792                                               or else
3793                                             Is_Controlled (Element_Type));
3794          Set_Finalize_Storage_Only
3795                             (Implicit_Base, Finalize_Storage_Only
3796                                                           (Element_Type));
3797
3798       --  Unconstrained array case
3799
3800       else
3801          Set_Ekind                    (T, E_Array_Type);
3802          Init_Size_Align              (T);
3803          Set_Etype                    (T, T);
3804          Set_Scope                    (T, Current_Scope);
3805          Set_Component_Size           (T, Uint_0);
3806          Set_Is_Constrained           (T, False);
3807          Set_First_Index              (T, First (Subtype_Marks (Def)));
3808          Set_Has_Delayed_Freeze       (T, True);
3809          Set_Has_Task                 (T, Has_Task      (Element_Type));
3810          Set_Has_Controlled_Component (T, Has_Controlled_Component
3811                                                         (Element_Type)
3812                                             or else
3813                                           Is_Controlled (Element_Type));
3814          Set_Finalize_Storage_Only    (T, Finalize_Storage_Only
3815                                                         (Element_Type));
3816       end if;
3817
3818       Set_Component_Type (Base_Type (T), Element_Type);
3819
3820       if Aliased_Present (Component_Definition (Def)) then
3821          Set_Has_Aliased_Components (Etype (T));
3822       end if;
3823
3824       --  Ada 2005 (AI-231): Propagate the null-excluding attribute to the
3825       --  array type to ensure that objects of this type are initialized.
3826
3827       if Ada_Version >= Ada_05
3828         and then Can_Never_Be_Null (Element_Type)
3829       then
3830          Set_Can_Never_Be_Null (T);
3831
3832          if Null_Exclusion_Present (Component_Definition (Def))
3833
3834             --  No need to check itypes because in their case this check
3835             --  was done at their point of creation
3836
3837            and then not Is_Itype (Element_Type)
3838          then
3839             Error_Msg_N
3840               ("null-exclusion cannot be applied to a null excluding type",
3841                Subtype_Indication (Component_Definition (Def)));
3842          end if;
3843       end if;
3844
3845       Priv := Private_Component (Element_Type);
3846
3847       if Present (Priv) then
3848
3849          --  Check for circular definitions
3850
3851          if Priv = Any_Type then
3852             Set_Component_Type (Etype (T), Any_Type);
3853
3854          --  There is a gap in the visibility of operations on the composite
3855          --  type only if the component type is defined in a different scope.
3856
3857          elsif Scope (Priv) = Current_Scope then
3858             null;
3859
3860          elsif Is_Limited_Type (Priv) then
3861             Set_Is_Limited_Composite (Etype (T));
3862             Set_Is_Limited_Composite (T);
3863          else
3864             Set_Is_Private_Composite (Etype (T));
3865             Set_Is_Private_Composite (T);
3866          end if;
3867       end if;
3868
3869       --  Create a concatenation operator for the new type. Internal
3870       --  array types created for packed entities do not need such, they
3871       --  are compatible with the user-defined type.
3872
3873       if Number_Dimensions (T) = 1
3874          and then not Is_Packed_Array_Type (T)
3875       then
3876          New_Concatenation_Op (T);
3877       end if;
3878
3879       --  In the case of an unconstrained array the parser has already
3880       --  verified that all the indices are unconstrained but we still
3881       --  need to make sure that the element type is constrained.
3882
3883       if Is_Indefinite_Subtype (Element_Type) then
3884          Error_Msg_N
3885            ("unconstrained element type in array declaration",
3886             Subtype_Indication (Component_Def));
3887
3888       elsif Is_Abstract (Element_Type) then
3889          Error_Msg_N
3890            ("the type of a component cannot be abstract",
3891             Subtype_Indication (Component_Def));
3892       end if;
3893
3894    end Array_Type_Declaration;
3895
3896    ------------------------------------------------------
3897    -- Replace_Anonymous_Access_To_Protected_Subprogram --
3898    ------------------------------------------------------
3899
3900    function Replace_Anonymous_Access_To_Protected_Subprogram
3901      (N      : Node_Id;
3902       Prev_E : Entity_Id) return Entity_Id
3903    is
3904       Loc : constant Source_Ptr := Sloc (N);
3905
3906       Curr_Scope : constant Scope_Stack_Entry :=
3907                      Scope_Stack.Table (Scope_Stack.Last);
3908
3909       Anon : constant Entity_Id :=
3910                Make_Defining_Identifier (Loc,
3911                  Chars => New_Internal_Name ('S'));
3912
3913       Acc  : Node_Id;
3914       Comp : Node_Id;
3915       Decl : Node_Id;
3916       P    : Node_Id;
3917
3918    begin
3919       Set_Is_Internal (Anon);
3920
3921       case Nkind (N) is
3922          when N_Component_Declaration       |
3923            N_Unconstrained_Array_Definition |
3924            N_Constrained_Array_Definition   =>
3925             Comp := Component_Definition (N);
3926             Acc  := Access_Definition (Component_Definition (N));
3927
3928          when N_Discriminant_Specification =>
3929             Comp := Discriminant_Type (N);
3930             Acc  := Discriminant_Type (N);
3931
3932          when N_Parameter_Specification =>
3933             Comp := Parameter_Type (N);
3934             Acc  := Parameter_Type (N);
3935
3936          when others =>
3937             raise Program_Error;
3938       end case;
3939
3940       Decl := Make_Full_Type_Declaration (Loc,
3941                 Defining_Identifier => Anon,
3942                 Type_Definition   =>
3943                   Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
3944
3945       Mark_Rewrite_Insertion (Decl);
3946
3947       --  Insert the new declaration in the nearest enclosing scope
3948
3949       P := Parent (N);
3950       while Present (P) and then not Has_Declarations (P) loop
3951          P := Parent (P);
3952       end loop;
3953
3954       pragma Assert (Present (P));
3955
3956       if Nkind (P) = N_Package_Specification then
3957          Prepend (Decl, Visible_Declarations (P));
3958       else
3959          Prepend (Decl, Declarations (P));
3960       end if;
3961
3962       --  Replace the anonymous type with an occurrence of the new declaration.
3963       --  In all cases the rewritten node does not have the null-exclusion
3964       --  attribute because (if present) it was already inherited by the
3965       --  anonymous entity (Anon). Thus, in case of components we do not
3966       --  inherit this attribute.
3967
3968       if Nkind (N) = N_Parameter_Specification then
3969          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
3970          Set_Etype (Defining_Identifier (N), Anon);
3971          Set_Null_Exclusion_Present (N, False);
3972       else
3973          Rewrite (Comp,
3974            Make_Component_Definition (Loc,
3975              Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
3976       end if;
3977
3978       Mark_Rewrite_Insertion (Comp);
3979
3980       --  Temporarily remove the current scope from the stack to add the new
3981       --  declarations to the enclosing scope
3982
3983       Scope_Stack.Decrement_Last;
3984       Analyze (Decl);
3985       Scope_Stack.Append (Curr_Scope);
3986
3987       Set_Original_Access_Type (Anon, Prev_E);
3988       return Anon;
3989    end Replace_Anonymous_Access_To_Protected_Subprogram;
3990
3991    -------------------------------
3992    -- Build_Derived_Access_Type --
3993    -------------------------------
3994
3995    procedure Build_Derived_Access_Type
3996      (N            : Node_Id;
3997       Parent_Type  : Entity_Id;
3998       Derived_Type : Entity_Id)
3999    is
4000       S : constant Node_Id := Subtype_Indication (Type_Definition (N));
4001
4002       Desig_Type      : Entity_Id;
4003       Discr           : Entity_Id;
4004       Discr_Con_Elist : Elist_Id;
4005       Discr_Con_El    : Elmt_Id;
4006       Subt            : Entity_Id;
4007
4008    begin
4009       --  Set the designated type so it is available in case this is
4010       --  an access to a self-referential type, e.g. a standard list
4011       --  type with a next pointer. Will be reset after subtype is built.
4012
4013       Set_Directly_Designated_Type
4014         (Derived_Type, Designated_Type (Parent_Type));
4015
4016       Subt := Process_Subtype (S, N);
4017
4018       if Nkind (S) /= N_Subtype_Indication
4019         and then Subt /= Base_Type (Subt)
4020       then
4021          Set_Ekind (Derived_Type, E_Access_Subtype);
4022       end if;
4023
4024       if Ekind (Derived_Type) = E_Access_Subtype then
4025          declare
4026             Pbase      : constant Entity_Id := Base_Type (Parent_Type);
4027             Ibase      : constant Entity_Id :=
4028                            Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
4029             Svg_Chars  : constant Name_Id   := Chars (Ibase);
4030             Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
4031
4032          begin
4033             Copy_Node (Pbase, Ibase);
4034
4035             Set_Chars             (Ibase, Svg_Chars);
4036             Set_Next_Entity       (Ibase, Svg_Next_E);
4037             Set_Sloc              (Ibase, Sloc (Derived_Type));
4038             Set_Scope             (Ibase, Scope (Derived_Type));
4039             Set_Freeze_Node       (Ibase, Empty);
4040             Set_Is_Frozen         (Ibase, False);
4041             Set_Comes_From_Source (Ibase, False);
4042             Set_Is_First_Subtype  (Ibase, False);
4043
4044             Set_Etype (Ibase, Pbase);
4045             Set_Etype (Derived_Type, Ibase);
4046          end;
4047       end if;
4048
4049       Set_Directly_Designated_Type
4050         (Derived_Type, Designated_Type (Subt));
4051
4052       Set_Is_Constrained     (Derived_Type, Is_Constrained (Subt));
4053       Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
4054       Set_Size_Info          (Derived_Type,                     Parent_Type);
4055       Set_RM_Size            (Derived_Type, RM_Size            (Parent_Type));
4056       Set_Depends_On_Private (Derived_Type,
4057                               Has_Private_Component (Derived_Type));
4058       Conditional_Delay      (Derived_Type, Subt);
4059
4060       --  Ada 2005 (AI-231). Set the null-exclusion attribute
4061
4062       if Null_Exclusion_Present (Type_Definition (N))
4063         or else Can_Never_Be_Null (Parent_Type)
4064       then
4065          Set_Can_Never_Be_Null (Derived_Type);
4066       end if;
4067
4068       --  Note: we do not copy the Storage_Size_Variable, since
4069       --  we always go to the root type for this information.
4070
4071       --  Apply range checks to discriminants for derived record case
4072       --  ??? THIS CODE SHOULD NOT BE HERE REALLY.
4073
4074       Desig_Type := Designated_Type (Derived_Type);
4075       if Is_Composite_Type (Desig_Type)
4076         and then (not Is_Array_Type (Desig_Type))
4077         and then Has_Discriminants (Desig_Type)
4078         and then Base_Type (Desig_Type) /= Desig_Type
4079       then
4080          Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
4081          Discr_Con_El := First_Elmt (Discr_Con_Elist);
4082
4083          Discr := First_Discriminant (Base_Type (Desig_Type));
4084          while Present (Discr_Con_El) loop
4085             Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
4086             Next_Elmt (Discr_Con_El);
4087             Next_Discriminant (Discr);
4088          end loop;
4089       end if;
4090    end Build_Derived_Access_Type;
4091
4092    ------------------------------
4093    -- Build_Derived_Array_Type --
4094    ------------------------------
4095
4096    procedure Build_Derived_Array_Type
4097      (N            : Node_Id;
4098       Parent_Type  : Entity_Id;
4099       Derived_Type : Entity_Id)
4100    is
4101       Loc           : constant Source_Ptr := Sloc (N);
4102       Tdef          : constant Node_Id    := Type_Definition (N);
4103       Indic         : constant Node_Id    := Subtype_Indication (Tdef);
4104       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
4105       Implicit_Base : Entity_Id;
4106       New_Indic     : Node_Id;
4107
4108       procedure Make_Implicit_Base;
4109       --  If the parent subtype is constrained, the derived type is a
4110       --  subtype of an implicit base type derived from the parent base.
4111
4112       ------------------------
4113       -- Make_Implicit_Base --
4114       ------------------------
4115
4116       procedure Make_Implicit_Base is
4117       begin
4118          Implicit_Base :=
4119            Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
4120
4121          Set_Ekind (Implicit_Base, Ekind (Parent_Base));
4122          Set_Etype (Implicit_Base, Parent_Base);
4123
4124          Copy_Array_Subtype_Attributes   (Implicit_Base, Parent_Base);
4125          Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
4126
4127          Set_Has_Delayed_Freeze (Implicit_Base, True);
4128       end Make_Implicit_Base;
4129
4130    --  Start of processing for Build_Derived_Array_Type
4131
4132    begin
4133       if not Is_Constrained (Parent_Type) then
4134          if Nkind (Indic) /= N_Subtype_Indication then
4135             Set_Ekind (Derived_Type, E_Array_Type);
4136
4137             Copy_Array_Subtype_Attributes   (Derived_Type, Parent_Type);
4138             Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
4139
4140             Set_Has_Delayed_Freeze (Derived_Type, True);
4141
4142          else
4143             Make_Implicit_Base;
4144             Set_Etype (Derived_Type, Implicit_Base);
4145
4146             New_Indic :=
4147               Make_Subtype_Declaration (Loc,
4148                 Defining_Identifier => Derived_Type,
4149                 Subtype_Indication  =>
4150                   Make_Subtype_Indication (Loc,
4151                     Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
4152                     Constraint => Constraint (Indic)));
4153
4154             Rewrite (N, New_Indic);
4155             Analyze (N);
4156          end if;
4157
4158       else
4159          if Nkind (Indic) /= N_Subtype_Indication then
4160             Make_Implicit_Base;
4161
4162             Set_Ekind             (Derived_Type, Ekind (Parent_Type));
4163             Set_Etype             (Derived_Type, Implicit_Base);
4164             Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
4165
4166          else
4167             Error_Msg_N ("illegal constraint on constrained type", Indic);
4168          end if;
4169       end if;
4170
4171       --  If parent type is not a derived type itself, and is declared in
4172       --  closed scope (e.g. a subprogram), then we must explicitly introduce
4173       --  the new type's concatenation operator since Derive_Subprograms
4174       --  will not inherit the parent's operator. If the parent type is
4175       --  unconstrained, the operator is of the unconstrained base type.
4176
4177       if Number_Dimensions (Parent_Type) = 1
4178         and then not Is_Limited_Type (Parent_Type)
4179         and then not Is_Derived_Type (Parent_Type)
4180         and then not Is_Package_Or_Generic_Package
4181                        (Scope (Base_Type (Parent_Type)))
4182       then
4183          if not Is_Constrained (Parent_Type)
4184            and then Is_Constrained (Derived_Type)
4185          then
4186             New_Concatenation_Op (Implicit_Base);
4187          else
4188             New_Concatenation_Op (Derived_Type);
4189          end if;
4190       end if;
4191    end Build_Derived_Array_Type;
4192
4193    -----------------------------------
4194    -- Build_Derived_Concurrent_Type --
4195    -----------------------------------
4196
4197    procedure Build_Derived_Concurrent_Type
4198      (N            : Node_Id;
4199       Parent_Type  : Entity_Id;
4200       Derived_Type : Entity_Id)
4201    is
4202       D_Constraint : Node_Id;
4203       Disc_Spec    : Node_Id;
4204       Old_Disc     : Entity_Id;
4205       New_Disc     : Entity_Id;
4206
4207       Constraint_Present : constant Boolean :=
4208                              Nkind (Subtype_Indication (Type_Definition (N)))
4209                                                      = N_Subtype_Indication;
4210
4211    begin
4212       Set_Stored_Constraint (Derived_Type, No_Elist);
4213
4214       if Is_Task_Type (Parent_Type) then
4215          Set_Storage_Size_Variable (Derived_Type,
4216            Storage_Size_Variable (Parent_Type));
4217       end if;
4218
4219       if Present (Discriminant_Specifications (N)) then
4220          New_Scope (Derived_Type);
4221          Check_Or_Process_Discriminants (N, Derived_Type);
4222          End_Scope;
4223
4224       elsif Constraint_Present then
4225
4226          --  Build constrained subtype and derive from it
4227
4228          declare
4229             Loc  : constant Source_Ptr := Sloc (N);
4230             Anon : constant Entity_Id :=
4231                      Make_Defining_Identifier (Loc,
4232                        New_External_Name (Chars (Derived_Type), 'T'));
4233             Decl : Node_Id;
4234
4235          begin
4236             Decl :=
4237               Make_Subtype_Declaration (Loc,
4238                 Defining_Identifier => Anon,
4239                 Subtype_Indication =>
4240                   Subtype_Indication (Type_Definition (N)));
4241             Insert_Before (N, Decl);
4242             Analyze (Decl);
4243
4244             Rewrite (Subtype_Indication (Type_Definition (N)),
4245               New_Occurrence_Of (Anon, Loc));
4246             Set_Analyzed (Derived_Type, False);
4247             Analyze (N);
4248             return;
4249          end;
4250       end if;
4251
4252       --  All attributes are inherited from parent. In particular,
4253       --  entries and the corresponding record type are the same.
4254       --  Discriminants may be renamed, and must be treated separately.
4255
4256       Set_Has_Discriminants
4257         (Derived_Type, Has_Discriminants         (Parent_Type));
4258       Set_Corresponding_Record_Type
4259         (Derived_Type, Corresponding_Record_Type (Parent_Type));
4260
4261       if Constraint_Present then
4262          if not Has_Discriminants (Parent_Type) then
4263             Error_Msg_N ("untagged parent must have discriminants", N);
4264
4265          elsif Present (Discriminant_Specifications (N)) then
4266
4267             --  Verify that new discriminants are used to constrain old ones
4268
4269             D_Constraint :=
4270               First
4271                 (Constraints
4272                   (Constraint (Subtype_Indication (Type_Definition (N)))));
4273
4274             Old_Disc  := First_Discriminant (Parent_Type);
4275             New_Disc  := First_Discriminant (Derived_Type);
4276             Disc_Spec := First (Discriminant_Specifications (N));
4277             while Present (Old_Disc) and then Present (Disc_Spec) loop
4278                if Nkind (Discriminant_Type (Disc_Spec)) /=
4279                                               N_Access_Definition
4280                then
4281                   Analyze (Discriminant_Type (Disc_Spec));
4282
4283                   if not Subtypes_Statically_Compatible (
4284                              Etype (Discriminant_Type (Disc_Spec)),
4285                                Etype (Old_Disc))
4286                   then
4287                      Error_Msg_N
4288                        ("not statically compatible with parent discriminant",
4289                         Discriminant_Type (Disc_Spec));
4290                   end if;
4291                end if;
4292
4293                if Nkind (D_Constraint) = N_Identifier
4294                  and then Chars (D_Constraint) /=
4295                    Chars (Defining_Identifier (Disc_Spec))
4296                then
4297                   Error_Msg_N ("new discriminants must constrain old ones",
4298                     D_Constraint);
4299                else
4300                   Set_Corresponding_Discriminant (New_Disc, Old_Disc);
4301                end if;
4302
4303                Next_Discriminant (Old_Disc);
4304                Next_Discriminant (New_Disc);
4305                Next (Disc_Spec);
4306             end loop;
4307
4308             if Present (Old_Disc) or else Present (Disc_Spec) then
4309                Error_Msg_N ("discriminant mismatch in derivation", N);
4310             end if;
4311
4312          end if;
4313
4314       elsif Present (Discriminant_Specifications (N)) then
4315          Error_Msg_N
4316            ("missing discriminant constraint in untagged derivation",
4317             N);
4318       end if;
4319
4320       if Present (Discriminant_Specifications (N)) then
4321          Old_Disc := First_Discriminant (Parent_Type);
4322          while Present (Old_Disc) loop
4323
4324             if No (Next_Entity (Old_Disc))
4325               or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
4326             then
4327                Set_Next_Entity (Last_Entity (Derived_Type),
4328                                          Next_Entity (Old_Disc));
4329                exit;
4330             end if;
4331
4332             Next_Discriminant (Old_Disc);
4333          end loop;
4334
4335       else
4336          Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
4337          if Has_Discriminants (Parent_Type) then
4338             Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
4339             Set_Discriminant_Constraint (
4340               Derived_Type, Discriminant_Constraint (Parent_Type));
4341          end if;
4342       end if;
4343
4344       Set_Last_Entity  (Derived_Type, Last_Entity  (Parent_Type));
4345
4346       Set_Has_Completion (Derived_Type);
4347    end Build_Derived_Concurrent_Type;
4348
4349    ------------------------------------
4350    -- Build_Derived_Enumeration_Type --
4351    ------------------------------------
4352
4353    procedure Build_Derived_Enumeration_Type
4354      (N            : Node_Id;
4355       Parent_Type  : Entity_Id;
4356       Derived_Type : Entity_Id)
4357    is
4358       Loc           : constant Source_Ptr := Sloc (N);
4359       Def           : constant Node_Id    := Type_Definition (N);
4360       Indic         : constant Node_Id    := Subtype_Indication (Def);
4361       Implicit_Base : Entity_Id;
4362       Literal       : Entity_Id;
4363       New_Lit       : Entity_Id;
4364       Literals_List : List_Id;
4365       Type_Decl     : Node_Id;
4366       Hi, Lo        : Node_Id;
4367       Rang_Expr     : Node_Id;
4368
4369    begin
4370       --  Since types Standard.Character and Standard.Wide_Character do
4371       --  not have explicit literals lists we need to process types derived
4372       --  from them specially. This is handled by Derived_Standard_Character.
4373       --  If the parent type is a generic type, there are no literals either,
4374       --  and we construct the same skeletal representation as for the generic
4375       --  parent type.
4376
4377       if Root_Type (Parent_Type) = Standard_Character
4378         or else Root_Type (Parent_Type) = Standard_Wide_Character
4379         or else Root_Type (Parent_Type) = Standard_Wide_Wide_Character
4380       then
4381          Derived_Standard_Character (N, Parent_Type, Derived_Type);
4382
4383       elsif Is_Generic_Type (Root_Type (Parent_Type)) then
4384          declare
4385             Lo : Node_Id;
4386             Hi : Node_Id;
4387
4388          begin
4389             Lo :=
4390                Make_Attribute_Reference (Loc,
4391                  Attribute_Name => Name_First,
4392                  Prefix => New_Reference_To (Derived_Type, Loc));
4393             Set_Etype (Lo, Derived_Type);
4394
4395             Hi :=
4396                Make_Attribute_Reference (Loc,
4397                  Attribute_Name => Name_Last,
4398                  Prefix => New_Reference_To (Derived_Type, Loc));
4399             Set_Etype (Hi, Derived_Type);
4400
4401             Set_Scalar_Range (Derived_Type,
4402                Make_Range (Loc,
4403                  Low_Bound => Lo,
4404                  High_Bound => Hi));
4405          end;
4406
4407       else
4408          --  If a constraint is present, analyze the bounds to catch
4409          --  premature usage of the derived literals.
4410
4411          if Nkind (Indic) = N_Subtype_Indication
4412            and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
4413          then
4414             Analyze (Low_Bound  (Range_Expression (Constraint (Indic))));
4415             Analyze (High_Bound (Range_Expression (Constraint (Indic))));
4416          end if;
4417
4418          --  Introduce an implicit base type for the derived type even
4419          --  if there is no constraint attached to it, since this seems
4420          --  closer to the Ada semantics. Build a full type declaration
4421          --  tree for the derived type using the implicit base type as
4422          --  the defining identifier. The build a subtype declaration
4423          --  tree which applies the constraint (if any) have it replace
4424          --  the derived type declaration.
4425
4426          Literal := First_Literal (Parent_Type);
4427          Literals_List := New_List;
4428          while Present (Literal)
4429            and then Ekind (Literal) = E_Enumeration_Literal
4430          loop
4431             --  Literals of the derived type have the same representation as
4432             --  those of the parent type, but this representation can be
4433             --  overridden by an explicit representation clause. Indicate
4434             --  that there is no explicit representation given yet. These
4435             --  derived literals are implicit operations of the new type,
4436             --  and can be overridden by explicit ones.
4437
4438             if Nkind (Literal) = N_Defining_Character_Literal then
4439                New_Lit :=
4440                  Make_Defining_Character_Literal (Loc, Chars (Literal));
4441             else
4442                New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
4443             end if;
4444
4445             Set_Ekind                (New_Lit, E_Enumeration_Literal);
4446             Set_Enumeration_Pos      (New_Lit, Enumeration_Pos (Literal));
4447             Set_Enumeration_Rep      (New_Lit, Enumeration_Rep (Literal));
4448             Set_Enumeration_Rep_Expr (New_Lit, Empty);
4449             Set_Alias                (New_Lit, Literal);
4450             Set_Is_Known_Valid       (New_Lit, True);
4451
4452             Append (New_Lit, Literals_List);
4453             Next_Literal (Literal);
4454          end loop;
4455
4456          Implicit_Base :=
4457            Make_Defining_Identifier (Sloc (Derived_Type),
4458              New_External_Name (Chars (Derived_Type), 'B'));
4459
4460          --  Indicate the proper nature of the derived type. This must
4461          --  be done before analysis of the literals, to recognize cases
4462          --  when a literal may be hidden by a previous explicit function
4463          --  definition (cf. c83031a).
4464
4465          Set_Ekind (Derived_Type, E_Enumeration_Subtype);
4466          Set_Etype (Derived_Type, Implicit_Base);
4467
4468          Type_Decl :=
4469            Make_Full_Type_Declaration (Loc,
4470              Defining_Identifier => Implicit_Base,
4471              Discriminant_Specifications => No_List,
4472              Type_Definition =>
4473                Make_Enumeration_Type_Definition (Loc, Literals_List));
4474
4475          Mark_Rewrite_Insertion (Type_Decl);
4476          Insert_Before (N, Type_Decl);
4477          Analyze (Type_Decl);
4478
4479          --  After the implicit base is analyzed its Etype needs to be changed
4480          --  to reflect the fact that it is derived from the parent type which
4481          --  was ignored during analysis. We also set the size at this point.
4482
4483          Set_Etype (Implicit_Base, Parent_Type);
4484
4485          Set_Size_Info      (Implicit_Base,                 Parent_Type);
4486          Set_RM_Size        (Implicit_Base, RM_Size        (Parent_Type));
4487          Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
4488
4489          Set_Has_Non_Standard_Rep
4490                             (Implicit_Base, Has_Non_Standard_Rep
4491                                                            (Parent_Type));
4492          Set_Has_Delayed_Freeze (Implicit_Base);
4493
4494          --  Process the subtype indication including a validation check
4495          --  on the constraint, if any. If a constraint is given, its bounds
4496          --  must be implicitly converted to the new type.
4497
4498          if Nkind (Indic) = N_Subtype_Indication then
4499             declare
4500                R : constant Node_Id :=
4501                      Range_Expression (Constraint (Indic));
4502
4503             begin
4504                if Nkind (R) = N_Range then
4505                   Hi := Build_Scalar_Bound
4506                           (High_Bound (R), Parent_Type, Implicit_Base);
4507                   Lo := Build_Scalar_Bound
4508                           (Low_Bound  (R), Parent_Type, Implicit_Base);
4509
4510                else
4511                   --  Constraint is a Range attribute. Replace with the
4512                   --  explicit mention of the bounds of the prefix, which must
4513                   --  be a subtype.
4514
4515                   Analyze (Prefix (R));
4516                   Hi :=
4517                     Convert_To (Implicit_Base,
4518                       Make_Attribute_Reference (Loc,
4519                         Attribute_Name => Name_Last,
4520                         Prefix =>
4521                           New_Occurrence_Of (Entity (Prefix (R)), Loc)));
4522
4523                   Lo :=
4524                     Convert_To (Implicit_Base,
4525                       Make_Attribute_Reference (Loc,
4526                         Attribute_Name => Name_First,
4527                         Prefix =>
4528                           New_Occurrence_Of (Entity (Prefix (R)), Loc)));
4529                end if;
4530             end;
4531
4532          else
4533             Hi :=
4534               Build_Scalar_Bound
4535                 (Type_High_Bound (Parent_Type),
4536                  Parent_Type, Implicit_Base);
4537             Lo :=
4538                Build_Scalar_Bound
4539                  (Type_Low_Bound (Parent_Type),
4540                   Parent_Type, Implicit_Base);
4541          end if;
4542
4543          Rang_Expr :=
4544            Make_Range (Loc,
4545              Low_Bound  => Lo,
4546              High_Bound => Hi);
4547
4548          --  If we constructed a default range for the case where no range
4549          --  was given, then the expressions in the range must not freeze
4550          --  since they do not correspond to expressions in the source.
4551
4552          if Nkind (Indic) /= N_Subtype_Indication then
4553             Set_Must_Not_Freeze (Lo);
4554             Set_Must_Not_Freeze (Hi);
4555             Set_Must_Not_Freeze (Rang_Expr);
4556          end if;
4557
4558          Rewrite (N,
4559            Make_Subtype_Declaration (Loc,
4560              Defining_Identifier => Derived_Type,
4561              Subtype_Indication =>
4562                Make_Subtype_Indication (Loc,
4563                  Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
4564                  Constraint =>
4565                    Make_Range_Constraint (Loc,
4566                      Range_Expression => Rang_Expr))));
4567
4568          Analyze (N);
4569
4570          --  If pragma Discard_Names applies on the first subtype of the
4571          --  parent type, then it must be applied on this subtype as well.
4572
4573          if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
4574             Set_Discard_Names (Derived_Type);
4575          end if;
4576
4577          --  Apply a range check. Since this range expression doesn't have an
4578          --  Etype, we have to specifically pass the Source_Typ parameter. Is
4579          --  this right???
4580
4581          if Nkind (Indic) = N_Subtype_Indication then
4582             Apply_Range_Check (Range_Expression (Constraint (Indic)),
4583                                Parent_Type,
4584                                Source_Typ => Entity (Subtype_Mark (Indic)));
4585          end if;
4586       end if;
4587    end Build_Derived_Enumeration_Type;
4588
4589    --------------------------------
4590    -- Build_Derived_Numeric_Type --
4591    --------------------------------
4592
4593    procedure Build_Derived_Numeric_Type
4594      (N            : Node_Id;
4595       Parent_Type  : Entity_Id;
4596       Derived_Type : Entity_Id)
4597    is
4598       Loc           : constant Source_Ptr := Sloc (N);
4599       Tdef          : constant Node_Id    := Type_Definition (N);
4600       Indic         : constant Node_Id    := Subtype_Indication (Tdef);
4601       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
4602       No_Constraint : constant Boolean    := Nkind (Indic) /=
4603                                                   N_Subtype_Indication;
4604       Implicit_Base : Entity_Id;
4605
4606       Lo : Node_Id;
4607       Hi : Node_Id;
4608
4609    begin
4610       --  Process the subtype indication including a validation check on
4611       --  the constraint if any.
4612
4613       Discard_Node (Process_Subtype (Indic, N));
4614
4615       --  Introduce an implicit base type for the derived type even if there
4616       --  is no constraint attached to it, since this seems closer to the Ada
4617       --  semantics.
4618
4619       Implicit_Base :=
4620         Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
4621
4622       Set_Etype          (Implicit_Base, Parent_Base);
4623       Set_Ekind          (Implicit_Base, Ekind          (Parent_Base));
4624       Set_Size_Info      (Implicit_Base,                 Parent_Base);
4625       Set_RM_Size        (Implicit_Base, RM_Size        (Parent_Base));
4626       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
4627       Set_Parent         (Implicit_Base, Parent (Derived_Type));
4628
4629       if Is_Discrete_Or_Fixed_Point_Type (Parent_Base) then
4630          Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
4631       end if;
4632
4633       Set_Has_Delayed_Freeze (Implicit_Base);
4634
4635       Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Base));
4636       Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
4637
4638       Set_Scalar_Range (Implicit_Base,
4639         Make_Range (Loc,
4640           Low_Bound  => Lo,
4641           High_Bound => Hi));
4642
4643       if Has_Infinities (Parent_Base) then
4644          Set_Includes_Infinities (Scalar_Range (Implicit_Base));
4645       end if;
4646
4647       --  The Derived_Type, which is the entity of the declaration, is a
4648       --  subtype of the implicit base. Its Ekind is a subtype, even in the
4649       --  absence of an explicit constraint.
4650
4651       Set_Etype (Derived_Type, Implicit_Base);
4652
4653       --  If we did not have a constraint, then the Ekind is set from the
4654       --  parent type (otherwise Process_Subtype has set the bounds)
4655
4656       if No_Constraint then
4657          Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
4658       end if;
4659
4660       --  If we did not have a range constraint, then set the range from the
4661       --  parent type. Otherwise, the call to Process_Subtype has set the
4662       --  bounds.
4663
4664       if No_Constraint
4665         or else not Has_Range_Constraint (Indic)
4666       then
4667          Set_Scalar_Range (Derived_Type,
4668            Make_Range (Loc,
4669              Low_Bound  => New_Copy_Tree (Type_Low_Bound  (Parent_Type)),
4670              High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
4671          Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
4672
4673          if Has_Infinities (Parent_Type) then
4674             Set_Includes_Infinities (Scalar_Range (Derived_Type));
4675          end if;
4676       end if;
4677
4678       --  Set remaining type-specific fields, depending on numeric type
4679
4680       if Is_Modular_Integer_Type (Parent_Type) then
4681          Set_Modulus (Implicit_Base, Modulus (Parent_Base));
4682
4683          Set_Non_Binary_Modulus
4684            (Implicit_Base, Non_Binary_Modulus (Parent_Base));
4685
4686       elsif Is_Floating_Point_Type (Parent_Type) then
4687
4688          --  Digits of base type is always copied from the digits value of
4689          --  the parent base type, but the digits of the derived type will
4690          --  already have been set if there was a constraint present.
4691
4692          Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
4693          Set_Vax_Float    (Implicit_Base, Vax_Float    (Parent_Base));
4694
4695          if No_Constraint then
4696             Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
4697          end if;
4698
4699       elsif Is_Fixed_Point_Type (Parent_Type) then
4700
4701          --  Small of base type and derived type are always copied from the
4702          --  parent base type, since smalls never change. The delta of the
4703          --  base type is also copied from the parent base type. However the
4704          --  delta of the derived type will have been set already if a
4705          --  constraint was present.
4706
4707          Set_Small_Value (Derived_Type,  Small_Value (Parent_Base));
4708          Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
4709          Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
4710
4711          if No_Constraint then
4712             Set_Delta_Value (Derived_Type,  Delta_Value (Parent_Type));
4713          end if;
4714
4715          --  The scale and machine radix in the decimal case are always
4716          --  copied from the parent base type.
4717
4718          if Is_Decimal_Fixed_Point_Type (Parent_Type) then
4719             Set_Scale_Value (Derived_Type,  Scale_Value (Parent_Base));
4720             Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
4721
4722             Set_Machine_Radix_10
4723               (Derived_Type,  Machine_Radix_10 (Parent_Base));
4724             Set_Machine_Radix_10
4725               (Implicit_Base, Machine_Radix_10 (Parent_Base));
4726
4727             Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
4728
4729             if No_Constraint then
4730                Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
4731
4732             else
4733                --  the analysis of the subtype_indication sets the
4734                --  digits value of the derived type.
4735
4736                null;
4737             end if;
4738          end if;
4739       end if;
4740
4741       --  The type of the bounds is that of the parent type, and they
4742       --  must be converted to the derived type.
4743
4744       Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
4745
4746       --  The implicit_base should be frozen when the derived type is frozen,
4747       --  but note that it is used in the conversions of the bounds. For fixed
4748       --  types we delay the determination of the bounds until the proper
4749       --  freezing point. For other numeric types this is rejected by GCC, for
4750       --  reasons that are currently unclear (???), so we choose to freeze the
4751       --  implicit base now. In the case of integers and floating point types
4752       --  this is harmless because subsequent representation clauses cannot
4753       --  affect anything, but it is still baffling that we cannot use the
4754       --  same mechanism for all derived numeric types.
4755
4756       --  There is a further complication: actually *some* representation
4757       --  clauses can affect the implicit base type. Namely, attribute
4758       --  definition clauses for stream-oriented attributes need to set the
4759       --  corresponding TSS entries on the base type, and this normally cannot
4760       --  be done after the base type is frozen, so the circuitry in
4761       --  Sem_Ch13.New_Stream_Subprogram must account for this possibility and
4762       --  not use Set_TSS in this case.
4763
4764       if Is_Fixed_Point_Type (Parent_Type) then
4765          Conditional_Delay (Implicit_Base, Parent_Type);
4766       else
4767          Freeze_Before (N, Implicit_Base);
4768       end if;
4769    end Build_Derived_Numeric_Type;
4770
4771    --------------------------------
4772    -- Build_Derived_Private_Type --
4773    --------------------------------
4774
4775    procedure Build_Derived_Private_Type
4776      (N             : Node_Id;
4777       Parent_Type   : Entity_Id;
4778       Derived_Type  : Entity_Id;
4779       Is_Completion : Boolean;
4780       Derive_Subps  : Boolean := True)
4781    is
4782       Der_Base    : Entity_Id;
4783       Discr       : Entity_Id;
4784       Full_Decl   : Node_Id := Empty;
4785       Full_Der    : Entity_Id;
4786       Full_P      : Entity_Id;
4787       Last_Discr  : Entity_Id;
4788       Par_Scope   : constant Entity_Id := Scope (Base_Type (Parent_Type));
4789       Swapped     : Boolean := False;
4790
4791       procedure Copy_And_Build;
4792       --  Copy derived type declaration, replace parent with its full view,
4793       --  and analyze new declaration.
4794
4795       --------------------
4796       -- Copy_And_Build --
4797       --------------------
4798
4799       procedure Copy_And_Build is
4800          Full_N : Node_Id;
4801
4802       begin
4803          if Ekind (Parent_Type) in Record_Kind
4804            or else
4805              (Ekind (Parent_Type) in Enumeration_Kind
4806                and then Root_Type (Parent_Type) /= Standard_Character
4807                and then Root_Type (Parent_Type) /= Standard_Wide_Character
4808                and then Root_Type (Parent_Type) /= Standard_Wide_Wide_Character
4809                and then not Is_Generic_Type (Root_Type (Parent_Type)))
4810          then
4811             Full_N := New_Copy_Tree (N);
4812             Insert_After (N, Full_N);
4813             Build_Derived_Type (
4814               Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
4815
4816          else
4817             Build_Derived_Type (
4818               N, Parent_Type, Full_Der, True, Derive_Subps => False);
4819          end if;
4820       end Copy_And_Build;
4821
4822    --  Start of processing for Build_Derived_Private_Type
4823
4824    begin
4825       if Is_Tagged_Type (Parent_Type) then
4826          Build_Derived_Record_Type
4827            (N, Parent_Type, Derived_Type, Derive_Subps);
4828          return;
4829
4830       elsif Has_Discriminants (Parent_Type) then
4831          if Present (Full_View (Parent_Type)) then
4832             if not Is_Completion then
4833
4834                --  Copy declaration for subsequent analysis, to provide a
4835                --  completion for what is a private declaration. Indicate that
4836                --  the full type is internally generated.
4837
4838                Full_Decl := New_Copy_Tree (N);
4839                Full_Der  := New_Copy (Derived_Type);
4840                Set_Comes_From_Source (Full_Decl, False);
4841                Set_Comes_From_Source (Full_Der, False);
4842
4843                Insert_After (N, Full_Decl);
4844
4845             else
4846                --  If this is a completion, the full view being built is
4847                --  itself private. We build a subtype of the parent with
4848                --  the same constraints as this full view, to convey to the
4849                --  back end the constrained components and the size of this
4850                --  subtype. If the parent is constrained, its full view can
4851                --  serve as the underlying full view of the derived type.
4852
4853                if No (Discriminant_Specifications (N)) then
4854                   if Nkind (Subtype_Indication (Type_Definition (N))) =
4855                                                         N_Subtype_Indication
4856                   then
4857                      Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
4858
4859                   elsif Is_Constrained (Full_View (Parent_Type)) then
4860                      Set_Underlying_Full_View (Derived_Type,
4861                        Full_View (Parent_Type));
4862                   end if;
4863
4864                else
4865                   --  If there are new discriminants, the parent subtype is
4866                   --  constrained by them, but it is not clear how to build
4867                   --  the underlying_full_view in this case ???
4868
4869                   null;
4870                end if;
4871             end if;
4872          end if;
4873
4874          --  Build partial view of derived type from partial view of parent
4875
4876          Build_Derived_Record_Type
4877            (N, Parent_Type, Derived_Type, Derive_Subps);
4878
4879          if Present (Full_View (Parent_Type))
4880            and then not Is_Completion
4881          then
4882             if not In_Open_Scopes (Par_Scope)
4883               or else not In_Same_Source_Unit (N, Parent_Type)
4884             then
4885                --  Swap partial and full views temporarily
4886
4887                Install_Private_Declarations (Par_Scope);
4888                Install_Visible_Declarations (Par_Scope);
4889                Swapped := True;
4890             end if;
4891
4892             --  Build full view of derived type from full view of parent which
4893             --  is now installed. Subprograms have been derived on the partial
4894             --  view, the completion does not derive them anew.
4895
4896             if not Is_Tagged_Type (Parent_Type) then
4897
4898                --  If the parent is itself derived from another private type,
4899                --  installing the private declarations has not affected its
4900                --  privacy status, so use its own full view explicitly.
4901
4902                if Is_Private_Type (Parent_Type) then
4903                   Build_Derived_Record_Type
4904                     (Full_Decl, Full_View (Parent_Type), Full_Der, False);
4905                else
4906                   Build_Derived_Record_Type
4907                     (Full_Decl, Parent_Type, Full_Der, False);
4908                end if;
4909
4910             else
4911                --  If full view of parent is tagged, the completion
4912                --  inherits the proper primitive operations.
4913
4914                Set_Defining_Identifier (Full_Decl, Full_Der);
4915                Build_Derived_Record_Type
4916                  (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
4917                Set_Analyzed (Full_Decl);
4918             end if;
4919
4920             if Swapped then
4921                Uninstall_Declarations (Par_Scope);
4922
4923                if In_Open_Scopes (Par_Scope) then
4924                   Install_Visible_Declarations (Par_Scope);
4925                end if;
4926             end if;
4927
4928             Der_Base := Base_Type (Derived_Type);
4929             Set_Full_View (Derived_Type, Full_Der);
4930             Set_Full_View (Der_Base, Base_Type (Full_Der));
4931
4932             --  Copy the discriminant list from full view to the partial views
4933             --  (base type and its subtype). Gigi requires that the partial
4934             --  and full views have the same discriminants.
4935
4936             --  Note that since the partial view is pointing to discriminants
4937             --  in the full view, their scope will be that of the full view.
4938             --  This might cause some front end problems and need
4939             --  adjustment???
4940
4941             Discr := First_Discriminant (Base_Type (Full_Der));
4942             Set_First_Entity (Der_Base, Discr);
4943
4944             loop
4945                Last_Discr := Discr;
4946                Next_Discriminant (Discr);
4947                exit when No (Discr);
4948             end loop;
4949
4950             Set_Last_Entity (Der_Base, Last_Discr);
4951
4952             Set_First_Entity (Derived_Type, First_Entity (Der_Base));
4953             Set_Last_Entity  (Derived_Type, Last_Entity  (Der_Base));
4954             Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
4955
4956          else
4957             --  If this is a completion, the derived type stays private
4958             --  and there is no need to create a further full view, except
4959             --  in the unusual case when the derivation is nested within a
4960             --  child unit, see below.
4961
4962             null;
4963          end if;
4964
4965       elsif Present (Full_View (Parent_Type))
4966         and then  Has_Discriminants (Full_View (Parent_Type))
4967       then
4968          if Has_Unknown_Discriminants (Parent_Type)
4969            and then Nkind (Subtype_Indication (Type_Definition (N)))
4970              = N_Subtype_Indication
4971          then
4972             Error_Msg_N
4973               ("cannot constrain type with unknown discriminants",
4974                Subtype_Indication (Type_Definition (N)));
4975             return;
4976          end if;
4977
4978          --  If full view of parent is a record type, Build full view as
4979          --  a derivation from the parent's full view. Partial view remains
4980          --  private. For code generation and linking, the full view must
4981          --  have the same public status as the partial one. This full view
4982          --  is only needed if the parent type is in an enclosing scope, so
4983          --  that the full view may actually become visible, e.g. in a child
4984          --  unit. This is both more efficient, and avoids order of freezing
4985          --  problems with the added entities.
4986
4987          if not Is_Private_Type (Full_View (Parent_Type))
4988            and then (In_Open_Scopes (Scope (Parent_Type)))
4989          then
4990             Full_Der := Make_Defining_Identifier (Sloc (Derived_Type),
4991                                               Chars (Derived_Type));
4992             Set_Is_Itype (Full_Der);
4993             Set_Has_Private_Declaration (Full_Der);
4994             Set_Has_Private_Declaration (Derived_Type);
4995             Set_Associated_Node_For_Itype (Full_Der, N);
4996             Set_Parent (Full_Der, Parent (Derived_Type));
4997             Set_Full_View (Derived_Type, Full_Der);
4998             Set_Is_Public (Full_Der, Is_Public (Derived_Type));
4999             Full_P := Full_View (Parent_Type);
5000             Exchange_Declarations (Parent_Type);
5001             Copy_And_Build;
5002             Exchange_Declarations (Full_P);
5003
5004          else
5005             Build_Derived_Record_Type
5006               (N, Full_View (Parent_Type), Derived_Type,
5007                 Derive_Subps => False);
5008          end if;
5009
5010          --  In any case, the primitive operations are inherited from
5011          --  the parent type, not from the internal full view.
5012
5013          Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
5014
5015          if Derive_Subps then
5016             Derive_Subprograms (Parent_Type, Derived_Type);
5017          end if;
5018
5019       else
5020          --  Untagged type, No discriminants on either view
5021
5022          if Nkind (Subtype_Indication (Type_Definition (N))) =
5023                                                    N_Subtype_Indication
5024          then
5025             Error_Msg_N
5026               ("illegal constraint on type without discriminants", N);
5027          end if;
5028
5029          if Present (Discriminant_Specifications (N))
5030            and then Present (Full_View (Parent_Type))
5031            and then not Is_Tagged_Type (Full_View (Parent_Type))
5032          then
5033             Error_Msg_N
5034               ("cannot add discriminants to untagged type", N);
5035          end if;
5036
5037          Set_Stored_Constraint (Derived_Type, No_Elist);
5038          Set_Is_Constrained    (Derived_Type, Is_Constrained (Parent_Type));
5039          Set_Is_Controlled     (Derived_Type, Is_Controlled  (Parent_Type));
5040          Set_Has_Controlled_Component
5041                                (Derived_Type, Has_Controlled_Component
5042                                                              (Parent_Type));
5043
5044          --  Direct controlled types do not inherit Finalize_Storage_Only flag
5045
5046          if not Is_Controlled  (Parent_Type) then
5047             Set_Finalize_Storage_Only
5048               (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
5049          end if;
5050
5051          --  Construct the implicit full view by deriving from full view of
5052          --  the parent type. In order to get proper visibility, we install
5053          --  the parent scope and its declarations.
5054
5055          --  ??? if the parent is untagged private and its completion is
5056          --  tagged, this mechanism will not work because we cannot derive
5057          --  from the tagged full view unless we have an extension
5058
5059          if Present (Full_View (Parent_Type))
5060            and then not Is_Tagged_Type (Full_View (Parent_Type))
5061            and then not Is_Completion
5062          then
5063             Full_Der :=
5064               Make_Defining_Identifier (Sloc (Derived_Type),
5065                 Chars => Chars (Derived_Type));
5066             Set_Is_Itype (Full_Der);
5067             Set_Has_Private_Declaration (Full_Der);
5068             Set_Has_Private_Declaration (Derived_Type);
5069             Set_Associated_Node_For_Itype (Full_Der, N);
5070             Set_Parent (Full_Der, Parent (Derived_Type));
5071             Set_Full_View (Derived_Type, Full_Der);
5072
5073             if not In_Open_Scopes (Par_Scope) then
5074                Install_Private_Declarations (Par_Scope);
5075                Install_Visible_Declarations (Par_Scope);
5076                Copy_And_Build;
5077                Uninstall_Declarations (Par_Scope);
5078
5079             --  If parent scope is open and in another unit, and parent has a
5080             --  completion, then the derivation is taking place in the visible
5081             --  part of a child unit. In that case retrieve the full view of
5082             --  the parent momentarily.
5083
5084             elsif not In_Same_Source_Unit (N, Parent_Type) then
5085                Full_P := Full_View (Parent_Type);
5086                Exchange_Declarations (Parent_Type);
5087                Copy_And_Build;
5088                Exchange_Declarations (Full_P);
5089
5090             --  Otherwise it is a local derivation
5091
5092             else
5093                Copy_And_Build;
5094             end if;
5095
5096             Set_Scope                (Full_Der, Current_Scope);
5097             Set_Is_First_Subtype     (Full_Der,
5098                                        Is_First_Subtype (Derived_Type));
5099             Set_Has_Size_Clause      (Full_Der, False);
5100             Set_Has_Alignment_Clause (Full_Der, False);
5101             Set_Next_Entity          (Full_Der, Empty);
5102             Set_Has_Delayed_Freeze   (Full_Der);
5103             Set_Is_Frozen            (Full_Der, False);
5104             Set_Freeze_Node          (Full_Der, Empty);
5105             Set_Depends_On_Private   (Full_Der,
5106                                         Has_Private_Component    (Full_Der));
5107             Set_Public_Status        (Full_Der);
5108          end if;
5109       end if;
5110
5111       Set_Has_Unknown_Discriminants (Derived_Type,
5112         Has_Unknown_Discriminants (Parent_Type));
5113
5114       if Is_Private_Type (Derived_Type) then
5115          Set_Private_Dependents (Derived_Type, New_Elmt_List);
5116       end if;
5117
5118       if Is_Private_Type (Parent_Type)
5119         and then Base_Type (Parent_Type) = Parent_Type
5120         and then In_Open_Scopes (Scope (Parent_Type))
5121       then
5122          Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
5123
5124          if Is_Child_Unit (Scope (Current_Scope))
5125            and then Is_Completion
5126            and then In_Private_Part (Current_Scope)
5127            and then Scope (Parent_Type) /= Current_Scope
5128          then
5129             --  This is the unusual case where a type completed by a private
5130             --  derivation occurs within a package nested in a child unit,
5131             --  and the parent is declared in an ancestor. In this case, the
5132             --  full view of the parent type will become visible in the body
5133             --  of the enclosing child, and only then will the current type
5134             --  be possibly non-private. We build a underlying full view that
5135             --  will be installed when the enclosing child body is compiled.
5136
5137             declare
5138                IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
5139
5140             begin
5141                Full_Der :=
5142                  Make_Defining_Identifier (Sloc (Derived_Type),
5143                    Chars (Derived_Type));
5144                Set_Is_Itype (Full_Der);
5145                Set_Itype (IR, Full_Der);
5146                Insert_After (N, IR);
5147
5148                --  The full view will be used to swap entities on entry/exit
5149                --  to the body, and must appear in the entity list for the
5150                --  package.
5151
5152                Append_Entity (Full_Der, Scope (Derived_Type));
5153                Set_Has_Private_Declaration (Full_Der);
5154                Set_Has_Private_Declaration (Derived_Type);
5155                Set_Associated_Node_For_Itype (Full_Der, N);
5156                Set_Parent (Full_Der, Parent (Derived_Type));
5157                Full_P := Full_View (Parent_Type);
5158                Exchange_Declarations (Parent_Type);
5159                Copy_And_Build;
5160                Exchange_Declarations (Full_P);
5161                Set_Underlying_Full_View (Derived_Type, Full_Der);
5162             end;
5163          end if;
5164       end if;
5165    end Build_Derived_Private_Type;
5166
5167    -------------------------------
5168    -- Build_Derived_Record_Type --
5169    -------------------------------
5170
5171    --  1. INTRODUCTION
5172
5173    --  Ideally we would like to use the same model of type derivation for
5174    --  tagged and untagged record types. Unfortunately this is not quite
5175    --  possible because the semantics of representation clauses is different
5176    --  for tagged and untagged records under inheritance. Consider the
5177    --  following:
5178
5179    --     type R (...) is [tagged] record ... end record;
5180    --     type T (...) is new R (...) [with ...];
5181
5182    --  The representation clauses of T can specify a completely different
5183    --  record layout from R's. Hence the same component can be placed in
5184    --  two very different positions in objects of type T and R. If R and T
5185    --  are tagged types, representation clauses for T can only specify the
5186    --  layout of non inherited components, thus components that are common
5187    --  in R and T have the same position in objects of type R and T.
5188
5189    --  This has two implications. The first is that the entire tree for R's
5190    --  declaration needs to be copied for T in the untagged case, so that T
5191    --  can be viewed as a record type of its own with its own representation
5192    --  clauses. The second implication is the way we handle discriminants.
5193    --  Specifically, in the untagged case we need a way to communicate to Gigi
5194    --  what are the real discriminants in the record, while for the semantics
5195    --  we need to consider those introduced by the user to rename the
5196    --  discriminants in the parent type. This is handled by introducing the
5197    --  notion of stored discriminants. See below for more.
5198
5199    --  Fortunately the way regular components are inherited can be handled in
5200    --  the same way in tagged and untagged types.
5201
5202    --  To complicate things a bit more the private view of a private extension
5203    --  cannot be handled in the same way as the full view (for one thing the
5204    --  semantic rules are somewhat different). We will explain what differs
5205    --  below.
5206
5207    --  2. DISCRIMINANTS UNDER INHERITANCE
5208
5209    --  The semantic rules governing the discriminants of derived types are
5210    --  quite subtle.
5211
5212    --   type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
5213    --      [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
5214
5215    --  If parent type has discriminants, then the discriminants that are
5216    --  declared in the derived type are [3.4 (11)]:
5217
5218    --  o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
5219    --    there is one;
5220
5221    --  o Otherwise, each discriminant of the parent type (implicitly declared
5222    --    in the same order with the same specifications). In this case, the
5223    --    discriminants are said to be "inherited", or if unknown in the parent
5224    --    are also unknown in the derived type.
5225
5226    --  Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
5227
5228    --  o The parent subtype shall be constrained;
5229
5230    --  o If the parent type is not a tagged type, then each discriminant of
5231    --    the derived type shall be used in the constraint defining a parent
5232    --    subtype. [Implementation note: This ensures that the new discriminant
5233    --    can share storage with an existing discriminant.]
5234
5235    --  For the derived type each discriminant of the parent type is either
5236    --  inherited, constrained to equal some new discriminant of the derived
5237    --  type, or constrained to the value of an expression.
5238
5239    --  When inherited or constrained to equal some new discriminant, the
5240    --  parent discriminant and the discriminant of the derived type are said
5241    --  to "correspond".
5242
5243    --  If a discriminant of the parent type is constrained to a specific value
5244    --  in the derived type definition, then the discriminant is said to be
5245    --  "specified" by that derived type definition.
5246
5247    --  3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
5248
5249    --  We have spoken about stored discriminants in point 1 (introduction)
5250    --  above. There are two sort of stored discriminants: implicit and
5251    --  explicit. As long as the derived type inherits the same discriminants as
5252    --  the root record type, stored discriminants are the same as regular
5253    --  discriminants, and are said to be implicit. However, if any discriminant
5254    --  in the root type was renamed in the derived type, then the derived
5255    --  type will contain explicit stored discriminants. Explicit stored
5256    --  discriminants are discriminants in addition to the semantically visible
5257    --  discriminants defined for the derived type. Stored discriminants are
5258    --  used by Gigi to figure out what are the physical discriminants in
5259    --  objects of the derived type (see precise definition in einfo.ads).
5260    --  As an example, consider the following:
5261
5262    --           type R  (D1, D2, D3 : Int) is record ... end record;
5263    --           type T1 is new R;
5264    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
5265    --           type T3 is new T2;
5266    --           type T4 (Y : Int) is new T3 (Y, 99);
5267
5268    --  The following table summarizes the discriminants and stored
5269    --  discriminants in R and T1 through T4.
5270
5271    --   Type      Discrim     Stored Discrim  Comment
5272    --    R      (D1, D2, D3)   (D1, D2, D3)   Girder discrims implicit in R
5273    --    T1     (D1, D2, D3)   (D1, D2, D3)   Girder discrims implicit in T1
5274    --    T2     (X1, X2)       (D1, D2, D3)   Girder discrims EXPLICIT in T2
5275    --    T3     (X1, X2)       (D1, D2, D3)   Girder discrims EXPLICIT in T3
5276    --    T4     (Y)            (D1, D2, D3)   Girder discrims EXPLICIT in T4
5277
5278    --  Field Corresponding_Discriminant (abbreviated CD below) allows us to
5279    --  find the corresponding discriminant in the parent type, while
5280    --  Original_Record_Component (abbreviated ORC below), the actual physical
5281    --  component that is renamed. Finally the field Is_Completely_Hidden
5282    --  (abbreviated ICH below) is set for all explicit stored discriminants
5283    --  (see einfo.ads for more info). For the above example this gives:
5284
5285    --                 Discrim     CD        ORC     ICH
5286    --                 ^^^^^^^     ^^        ^^^     ^^^
5287    --                 D1 in R    empty     itself    no
5288    --                 D2 in R    empty     itself    no
5289    --                 D3 in R    empty     itself    no
5290
5291    --                 D1 in T1  D1 in R    itself    no
5292    --                 D2 in T1  D2 in R    itself    no
5293    --                 D3 in T1  D3 in R    itself    no
5294
5295    --                 X1 in T2  D3 in T1  D3 in T2   no
5296    --                 X2 in T2  D1 in T1  D1 in T2   no
5297    --                 D1 in T2   empty    itself    yes
5298    --                 D2 in T2   empty    itself    yes
5299    --                 D3 in T2   empty    itself    yes
5300
5301    --                 X1 in T3  X1 in T2  D3 in T3   no
5302    --                 X2 in T3  X2 in T2  D1 in T3   no
5303    --                 D1 in T3   empty    itself    yes
5304    --                 D2 in T3   empty    itself    yes
5305    --                 D3 in T3   empty    itself    yes
5306
5307    --                 Y  in T4  X1 in T3  D3 in T3   no
5308    --                 D1 in T3   empty    itself    yes
5309    --                 D2 in T3   empty    itself    yes
5310    --                 D3 in T3   empty    itself    yes
5311
5312    --  4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
5313
5314    --  Type derivation for tagged types is fairly straightforward. If no
5315    --  discriminants are specified by the derived type, these are inherited
5316    --  from the parent. No explicit stored discriminants are ever necessary.
5317    --  The only manipulation that is done to the tree is that of adding a
5318    --  _parent field with parent type and constrained to the same constraint
5319    --  specified for the parent in the derived type definition. For instance:
5320
5321    --           type R  (D1, D2, D3 : Int) is tagged record ... end record;
5322    --           type T1 is new R with null record;
5323    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
5324
5325    --  are changed into:
5326
5327    --           type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
5328    --              _parent : R (D1, D2, D3);
5329    --           end record;
5330
5331    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
5332    --              _parent : T1 (X2, 88, X1);
5333    --           end record;
5334
5335    --  The discriminants actually present in R, T1 and T2 as well as their CD,
5336    --  ORC and ICH fields are:
5337
5338    --                 Discrim     CD        ORC     ICH
5339    --                 ^^^^^^^     ^^        ^^^     ^^^
5340    --                 D1 in R    empty     itself    no
5341    --                 D2 in R    empty     itself    no
5342    --                 D3 in R    empty     itself    no
5343
5344    --                 D1 in T1  D1 in R    D1 in R   no
5345    --                 D2 in T1  D2 in R    D2 in R   no
5346    --                 D3 in T1  D3 in R    D3 in R   no
5347
5348    --                 X1 in T2  D3 in T1   D3 in R   no
5349    --                 X2 in T2  D1 in T1   D1 in R   no
5350
5351    --  5. FIRST TRANSFORMATION FOR DERIVED RECORDS
5352    --
5353    --  Regardless of whether we dealing with a tagged or untagged type
5354    --  we will transform all derived type declarations of the form
5355    --
5356    --               type T is new R (...) [with ...];
5357    --  or
5358    --               subtype S is R (...);
5359    --               type T is new S [with ...];
5360    --  into
5361    --               type BT is new R [with ...];
5362    --               subtype T is BT (...);
5363    --
5364    --  That is, the base derived type is constrained only if it has no
5365    --  discriminants. The reason for doing this is that GNAT's semantic model
5366    --  assumes that a base type with discriminants is unconstrained.
5367    --
5368    --  Note that, strictly speaking, the above transformation is not always
5369    --  correct. Consider for instance the following excerpt from ACVC b34011a:
5370    --
5371    --       procedure B34011A is
5372    --          type REC (D : integer := 0) is record
5373    --             I : Integer;
5374    --          end record;
5375
5376    --          package P is
5377    --             type T6 is new Rec;
5378    --             function F return T6;
5379    --          end P;
5380
5381    --          use P;
5382    --          package Q6 is
5383    --             type U is new T6 (Q6.F.I);                   -- ERROR: Q6.F.
5384    --          end Q6;
5385    --
5386    --  The definition of Q6.U is illegal. However transforming Q6.U into
5387
5388    --             type BaseU is new T6;
5389    --             subtype U is BaseU (Q6.F.I)
5390
5391    --  turns U into a legal subtype, which is incorrect. To avoid this problem
5392    --  we always analyze the constraint (in this case (Q6.F.I)) before applying
5393    --  the transformation described above.
5394
5395    --  There is another instance where the above transformation is incorrect.
5396    --  Consider:
5397
5398    --          package Pack is
5399    --             type Base (D : Integer) is tagged null record;
5400    --             procedure P (X : Base);
5401
5402    --             type Der is new Base (2) with null record;
5403    --             procedure P (X : Der);
5404    --          end Pack;
5405
5406    --  Then the above transformation turns this into
5407
5408    --             type Der_Base is new Base with null record;
5409    --             --  procedure P (X : Base) is implicitly inherited here
5410    --             --  as procedure P (X : Der_Base).
5411
5412    --             subtype Der is Der_Base (2);
5413    --             procedure P (X : Der);
5414    --             --  The overriding of P (X : Der_Base) is illegal since we
5415    --             --  have a parameter conformance problem.
5416
5417    --  To get around this problem, after having semantically processed Der_Base
5418    --  and the rewritten subtype declaration for Der, we copy Der_Base field
5419    --  Discriminant_Constraint from Der so that when parameter conformance is
5420    --  checked when P is overridden, no semantic errors are flagged.
5421
5422    --  6. SECOND TRANSFORMATION FOR DERIVED RECORDS
5423
5424    --  Regardless of whether we are dealing with a tagged or untagged type
5425    --  we will transform all derived type declarations of the form
5426
5427    --               type R (D1, .., Dn : ...) is [tagged] record ...;
5428    --               type T is new R [with ...];
5429    --  into
5430    --               type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
5431
5432    --  The reason for such transformation is that it allows us to implement a
5433    --  very clean form of component inheritance as explained below.
5434
5435    --  Note that this transformation is not achieved by direct tree rewriting
5436    --  and manipulation, but rather by redoing the semantic actions that the
5437    --  above transformation will entail. This is done directly in routine
5438    --  Inherit_Components.
5439
5440    --  7. TYPE DERIVATION AND COMPONENT INHERITANCE
5441
5442    --  In both tagged and untagged derived types, regular non discriminant
5443    --  components are inherited in the derived type from the parent type. In
5444    --  the absence of discriminants component, inheritance is straightforward
5445    --  as components can simply be copied from the parent.
5446
5447    --  If the parent has discriminants, inheriting components constrained with
5448    --  these discriminants requires caution. Consider the following example:
5449
5450    --      type R  (D1, D2 : Positive) is [tagged] record
5451    --         S : String (D1 .. D2);
5452    --      end record;
5453
5454    --      type T1                is new R        [with null record];
5455    --      type T2 (X : positive) is new R (1, X) [with null record];
5456
5457    --  As explained in 6. above, T1 is rewritten as
5458    --      type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
5459    --  which makes the treatment for T1 and T2 identical.
5460
5461    --  What we want when inheriting S, is that references to D1 and D2 in R are
5462    --  replaced with references to their correct constraints, ie D1 and D2 in
5463    --  T1 and 1 and X in T2. So all R's discriminant references are replaced
5464    --  with either discriminant references in the derived type or expressions.
5465    --  This replacement is achieved as follows: before inheriting R's
5466    --  components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
5467    --  created in the scope of T1 (resp. scope of T2) so that discriminants D1
5468    --  and D2 of T1 are visible (resp. discriminant X of T2 is visible).
5469    --  For T2, for instance, this has the effect of replacing String (D1 .. D2)
5470    --  by String (1 .. X).
5471
5472    --  8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
5473
5474    --  We explain here the rules governing private type extensions relevant to
5475    --  type derivation. These rules are explained on the following example:
5476
5477    --      type D [(...)] is new A [(...)] with private;      <-- partial view
5478    --      type D [(...)] is new P [(...)] with null record;  <-- full view
5479
5480    --  Type A is called the ancestor subtype of the private extension.
5481    --  Type P is the parent type of the full view of the private extension. It
5482    --  must be A or a type derived from A.
5483
5484    --  The rules concerning the discriminants of private type extensions are
5485    --  [7.3(10-13)]:
5486
5487    --  o If a private extension inherits known discriminants from the ancestor
5488    --    subtype, then the full view shall also inherit its discriminants from
5489    --    the ancestor subtype and the parent subtype of the full view shall be
5490    --    constrained if and only if the ancestor subtype is constrained.
5491
5492    --  o If a partial view has unknown discriminants, then the full view may
5493    --    define a definite or an indefinite subtype, with or without
5494    --    discriminants.
5495
5496    --  o If a partial view has neither known nor unknown discriminants, then
5497    --    the full view shall define a definite subtype.
5498
5499    --  o If the ancestor subtype of a private extension has constrained
5500    --    discriminants, then the parent subtype of the full view shall impose a
5501    --    statically matching constraint on those discriminants.
5502
5503    --  This means that only the following forms of private extensions are
5504    --  allowed:
5505
5506    --      type D is new A with private;      <-- partial view
5507    --      type D is new P with null record;  <-- full view
5508
5509    --  If A has no discriminants than P has no discriminants, otherwise P must
5510    --  inherit A's discriminants.
5511
5512    --      type D is new A (...) with private;      <-- partial view
5513    --      type D is new P (:::) with null record;  <-- full view
5514
5515    --  P must inherit A's discriminants and (...) and (:::) must statically
5516    --  match.
5517
5518    --      subtype A is R (...);
5519    --      type D is new A with private;      <-- partial view
5520    --      type D is new P with null record;  <-- full view
5521
5522    --  P must have inherited R's discriminants and must be derived from A or
5523    --  any of its subtypes.
5524
5525    --      type D (..) is new A with private;              <-- partial view
5526    --      type D (..) is new P [(:::)] with null record;  <-- full view
5527
5528    --  No specific constraints on P's discriminants or constraint (:::).
5529    --  Note that A can be unconstrained, but the parent subtype P must either
5530    --  be constrained or (:::) must be present.
5531
5532    --      type D (..) is new A [(...)] with private;      <-- partial view
5533    --      type D (..) is new P [(:::)] with null record;  <-- full view
5534
5535    --  P's constraints on A's discriminants must statically match those
5536    --  imposed by (...).
5537
5538    --  9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
5539
5540    --  The full view of a private extension is handled exactly as described
5541    --  above. The model chose for the private view of a private extension is
5542    --  the same for what concerns discriminants (ie they receive the same
5543    --  treatment as in the tagged case). However, the private view of the
5544    --  private extension always inherits the components of the parent base,
5545    --  without replacing any discriminant reference. Strictly speaking this is
5546    --  incorrect. However, Gigi never uses this view to generate code so this
5547    --  is a purely semantic issue. In theory, a set of transformations similar
5548    --  to those given in 5. and 6. above could be applied to private views of
5549    --  private extensions to have the same model of component inheritance as
5550    --  for non private extensions. However, this is not done because it would
5551    --  further complicate private type processing. Semantically speaking, this
5552    --  leaves us in an uncomfortable situation. As an example consider:
5553
5554    --          package Pack is
5555    --             type R (D : integer) is tagged record
5556    --                S : String (1 .. D);
5557    --             end record;
5558    --             procedure P (X : R);
5559    --             type T is new R (1) with private;
5560    --          private
5561    --             type T is new R (1) with null record;
5562    --          end;
5563
5564    --  This is transformed into:
5565
5566    --          package Pack is
5567    --             type R (D : integer) is tagged record
5568    --                S : String (1 .. D);
5569    --             end record;
5570    --             procedure P (X : R);
5571    --             type T is new R (1) with private;
5572    --          private
5573    --             type BaseT is new R with null record;
5574    --             subtype  T is BaseT (1);
5575    --          end;
5576
5577    --  (strictly speaking the above is incorrect Ada)
5578
5579    --  From the semantic standpoint the private view of private extension T
5580    --  should be flagged as constrained since one can clearly have
5581    --
5582    --             Obj : T;
5583    --
5584    --  in a unit withing Pack. However, when deriving subprograms for the
5585    --  private view of private extension T, T must be seen as unconstrained
5586    --  since T has discriminants (this is a constraint of the current
5587    --  subprogram derivation model). Thus, when processing the private view of
5588    --  a private extension such as T, we first mark T as unconstrained, we
5589    --  process it, we perform program derivation and just before returning from
5590    --  Build_Derived_Record_Type we mark T as constrained.
5591
5592    --  ??? Are there are other uncomfortable cases that we will have to
5593    --      deal with.
5594
5595    --  10. RECORD_TYPE_WITH_PRIVATE complications
5596
5597    --  Types that are derived from a visible record type and have a private
5598    --  extension present other peculiarities. They behave mostly like private
5599    --  types, but if they have primitive operations defined, these will not
5600    --  have the proper signatures for further inheritance, because other
5601    --  primitive operations will use the implicit base that we define for
5602    --  private derivations below. This affect subprogram inheritance (see
5603    --  Derive_Subprograms for details). We also derive the implicit base from
5604    --  the base type of the full view, so that the implicit base is a record
5605    --  type and not another private type, This avoids infinite loops.
5606
5607    procedure Build_Derived_Record_Type
5608      (N            : Node_Id;
5609       Parent_Type  : Entity_Id;
5610       Derived_Type : Entity_Id;
5611       Derive_Subps : Boolean := True)
5612    is
5613       Loc          : constant Source_Ptr := Sloc (N);
5614       Parent_Base  : Entity_Id;
5615       Type_Def     : Node_Id;
5616       Indic        : Node_Id;
5617       Discrim      : Entity_Id;
5618       Last_Discrim : Entity_Id;
5619       Constrs      : Elist_Id;
5620
5621       Discs        : Elist_Id := New_Elmt_List;
5622       --  An empty Discs list means that there were no constraints in the
5623       --  subtype indication or that there was an error processing it.
5624
5625       Assoc_List         : Elist_Id;
5626       New_Discrs         : Elist_Id;
5627       New_Base           : Entity_Id;
5628       New_Decl           : Node_Id;
5629       New_Indic          : Node_Id;
5630
5631       Is_Tagged          : constant Boolean := Is_Tagged_Type (Parent_Type);
5632       Discriminant_Specs : constant Boolean :=
5633                              Present (Discriminant_Specifications (N));
5634       Private_Extension  : constant Boolean :=
5635                              (Nkind (N) = N_Private_Extension_Declaration);
5636
5637       Constraint_Present     : Boolean;
5638       Inherit_Discrims       : Boolean := False;
5639       Save_Etype             : Entity_Id;
5640       Save_Discr_Constr      : Elist_Id;
5641       Save_Next_Entity       : Entity_Id;
5642
5643    begin
5644       if Ekind (Parent_Type) = E_Record_Type_With_Private
5645         and then Present (Full_View (Parent_Type))
5646         and then Has_Discriminants (Parent_Type)
5647       then
5648          Parent_Base := Base_Type (Full_View (Parent_Type));
5649       else
5650          Parent_Base := Base_Type (Parent_Type);
5651       end if;
5652
5653       --  Before we start the previously documented transformations, here is
5654       --  a little fix for size and alignment of tagged types. Normally when
5655       --  we derive type D from type P, we copy the size and alignment of P
5656       --  as the default for D, and in the absence of explicit representation
5657       --  clauses for D, the size and alignment are indeed the same as the
5658       --  parent.
5659
5660       --  But this is wrong for tagged types, since fields may be added,
5661       --  and the default size may need to be larger, and the default
5662       --  alignment may need to be larger.
5663
5664       --  We therefore reset the size and alignment fields in the tagged
5665       --  case. Note that the size and alignment will in any case be at
5666       --  least as large as the parent type (since the derived type has
5667       --  a copy of the parent type in the _parent field)
5668
5669       if Is_Tagged then
5670          Init_Size_Align (Derived_Type);
5671       end if;
5672
5673       --  STEP 0a: figure out what kind of derived type declaration we have
5674
5675       if Private_Extension then
5676          Type_Def := N;
5677          Set_Ekind (Derived_Type, E_Record_Type_With_Private);
5678
5679       else
5680          Type_Def := Type_Definition (N);
5681
5682          --  Ekind (Parent_Base) in not necessarily E_Record_Type since
5683          --  Parent_Base can be a private type or private extension. However,
5684          --  for tagged types with an extension the newly added fields are
5685          --  visible and hence the Derived_Type is always an E_Record_Type.
5686          --  (except that the parent may have its own private fields).
5687          --  For untagged types we preserve the Ekind of the Parent_Base.
5688
5689          if Present (Record_Extension_Part (Type_Def)) then
5690             Set_Ekind (Derived_Type, E_Record_Type);
5691          else
5692             Set_Ekind (Derived_Type, Ekind (Parent_Base));
5693          end if;
5694       end if;
5695
5696       --  Indic can either be an N_Identifier if the subtype indication
5697       --  contains no constraint or an N_Subtype_Indication if the subtype
5698       --  indication has a constraint.
5699
5700       Indic := Subtype_Indication (Type_Def);
5701       Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
5702
5703       --  Check that the type has visible discriminants. The type may be
5704       --  a private type with unknown discriminants whose full view has
5705       --  discriminants which are invisible.
5706
5707       if Constraint_Present then
5708          if not Has_Discriminants (Parent_Base)
5709            or else
5710              (Has_Unknown_Discriminants (Parent_Base)
5711                 and then Is_Private_Type (Parent_Base))
5712          then
5713             Error_Msg_N
5714               ("invalid constraint: type has no discriminant",
5715                  Constraint (Indic));
5716
5717             Constraint_Present := False;
5718             Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
5719
5720          elsif Is_Constrained (Parent_Type) then
5721             Error_Msg_N
5722                ("invalid constraint: parent type is already constrained",
5723                   Constraint (Indic));
5724
5725             Constraint_Present := False;
5726             Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
5727          end if;
5728       end if;
5729
5730       --  STEP 0b: If needed, apply transformation given in point 5. above
5731
5732       if not Private_Extension
5733         and then Has_Discriminants (Parent_Type)
5734         and then not Discriminant_Specs
5735         and then (Is_Constrained (Parent_Type) or else Constraint_Present)
5736       then
5737          --  First, we must analyze the constraint (see comment in point 5.)
5738
5739          if Constraint_Present then
5740             New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
5741
5742             if Has_Discriminants (Derived_Type)
5743               and then Has_Private_Declaration (Derived_Type)
5744               and then Present (Discriminant_Constraint (Derived_Type))
5745             then
5746                --  Verify that constraints of the full view conform to those
5747                --  given in partial view.
5748
5749                declare
5750                   C1, C2 : Elmt_Id;
5751
5752                begin
5753                   C1 := First_Elmt (New_Discrs);
5754                   C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
5755                   while Present (C1) and then Present (C2) loop
5756                      if not
5757                        Fully_Conformant_Expressions (Node (C1), Node (C2))
5758                      then
5759                         Error_Msg_N (
5760                           "constraint not conformant to previous declaration",
5761                              Node (C1));
5762                      end if;
5763
5764                      Next_Elmt (C1);
5765                      Next_Elmt (C2);
5766                   end loop;
5767                end;
5768             end if;
5769          end if;
5770
5771          --  Insert and analyze the declaration for the unconstrained base type
5772
5773          New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
5774
5775          New_Decl :=
5776            Make_Full_Type_Declaration (Loc,
5777               Defining_Identifier => New_Base,
5778               Type_Definition     =>
5779                 Make_Derived_Type_Definition (Loc,
5780                   Abstract_Present      => Abstract_Present (Type_Def),
5781                   Subtype_Indication    =>
5782                     New_Occurrence_Of (Parent_Base, Loc),
5783                   Record_Extension_Part =>
5784                     Relocate_Node (Record_Extension_Part (Type_Def))));
5785
5786          Set_Parent (New_Decl, Parent (N));
5787          Mark_Rewrite_Insertion (New_Decl);
5788          Insert_Before (N, New_Decl);
5789
5790          --  Note that this call passes False for the Derive_Subps parameter
5791          --  because subprogram derivation is deferred until after creating
5792          --  the subtype (see below).
5793
5794          Build_Derived_Type
5795            (New_Decl, Parent_Base, New_Base,
5796             Is_Completion => True, Derive_Subps => False);
5797
5798          --  ??? This needs re-examination to determine whether the
5799          --  above call can simply be replaced by a call to Analyze.
5800
5801          Set_Analyzed (New_Decl);
5802
5803          --  Insert and analyze the declaration for the constrained subtype
5804
5805          if Constraint_Present then
5806             New_Indic :=
5807               Make_Subtype_Indication (Loc,
5808                 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
5809                 Constraint   => Relocate_Node (Constraint (Indic)));
5810
5811          else
5812             declare
5813                Constr_List : constant List_Id := New_List;
5814                C           : Elmt_Id;
5815                Expr        : Node_Id;
5816
5817             begin
5818                C := First_Elmt (Discriminant_Constraint (Parent_Type));
5819                while Present (C) loop
5820                   Expr := Node (C);
5821
5822                   --  It is safe here to call New_Copy_Tree since
5823                   --  Force_Evaluation was called on each constraint in
5824                   --  Build_Discriminant_Constraints.
5825
5826                   Append (New_Copy_Tree (Expr), To => Constr_List);
5827
5828                   Next_Elmt (C);
5829                end loop;
5830
5831                New_Indic :=
5832                  Make_Subtype_Indication (Loc,
5833                    Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
5834                    Constraint   =>
5835                      Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
5836             end;
5837          end if;
5838
5839          Rewrite (N,
5840            Make_Subtype_Declaration (Loc,
5841              Defining_Identifier => Derived_Type,
5842              Subtype_Indication  => New_Indic));
5843
5844          Analyze (N);
5845
5846          --  Derivation of subprograms must be delayed until the full subtype
5847          --  has been established to ensure proper overriding of subprograms
5848          --  inherited by full types. If the derivations occurred as part of
5849          --  the call to Build_Derived_Type above, then the check for type
5850          --  conformance would fail because earlier primitive subprograms
5851          --  could still refer to the full type prior the change to the new
5852          --  subtype and hence would not match the new base type created here.
5853
5854          Derive_Subprograms (Parent_Type, Derived_Type);
5855
5856          --  For tagged types the Discriminant_Constraint of the new base itype
5857          --  is inherited from the first subtype so that no subtype conformance
5858          --  problem arise when the first subtype overrides primitive
5859          --  operations inherited by the implicit base type.
5860
5861          if Is_Tagged then
5862             Set_Discriminant_Constraint
5863               (New_Base, Discriminant_Constraint (Derived_Type));
5864          end if;
5865
5866          return;
5867       end if;
5868
5869       --  If we get here Derived_Type will have no discriminants or it will be
5870       --  a discriminated unconstrained base type.
5871
5872       --  STEP 1a: perform preliminary actions/checks for derived tagged types
5873
5874       if Is_Tagged then
5875
5876          --  The parent type is frozen for non-private extensions (RM 13.14(7))
5877          --  The declaration of a specific descendant of an interface type
5878          --  freezes the interface type (RM 13.14).
5879
5880          if not Private_Extension
5881            or else Is_Interface (Parent_Base)
5882          then
5883             Freeze_Before (N, Parent_Type);
5884          end if;
5885
5886          --  In Ada 2005 (AI-344), the restriction that a derived tagged type
5887          --  cannot be declared at a deeper level than its parent type is
5888          --  removed. The check on derivation within a generic body is also
5889          --  relaxed, but there's a restriction that a derived tagged type
5890          --  cannot be declared in a generic body if it's derived directly
5891          --  or indirectly from a formal type of that generic.
5892
5893          if Ada_Version >= Ada_05 then
5894             if Present (Enclosing_Generic_Body (Derived_Type)) then
5895                declare
5896                   Ancestor_Type : Entity_Id;
5897
5898                begin
5899                   --  Check to see if any ancestor of the derived type is a
5900                   --  formal type.
5901
5902                   Ancestor_Type := Parent_Type;
5903                   while not Is_Generic_Type (Ancestor_Type)
5904                     and then Etype (Ancestor_Type) /= Ancestor_Type
5905                   loop
5906                      Ancestor_Type := Etype (Ancestor_Type);
5907                   end loop;
5908
5909                   --  If the derived type does have a formal type as an
5910                   --  ancestor, then it's an error if the derived type is
5911                   --  declared within the body of the generic unit that
5912                   --  declares the formal type in its generic formal part. It's
5913                   --  sufficient to check whether the ancestor type is declared
5914                   --  inside the same generic body as the derived type (such as
5915                   --  within a nested generic spec), in which case the
5916                   --  derivation is legal. If the formal type is declared
5917                   --  outside of that generic body, then it's guaranteed that
5918                   --  the derived type is declared within the generic body of
5919                   --  the generic unit declaring the formal type.
5920
5921                   if Is_Generic_Type (Ancestor_Type)
5922                     and then Enclosing_Generic_Body (Ancestor_Type) /=
5923                                Enclosing_Generic_Body (Derived_Type)
5924                   then
5925                      Error_Msg_NE
5926                        ("parent type of& must not be descendant of formal type"
5927                           & " of an enclosing generic body",
5928                             Indic, Derived_Type);
5929                   end if;
5930                end;
5931             end if;
5932
5933          elsif Type_Access_Level (Derived_Type) /=
5934                  Type_Access_Level (Parent_Type)
5935            and then not Is_Generic_Type (Derived_Type)
5936          then
5937             if Is_Controlled (Parent_Type) then
5938                Error_Msg_N
5939                  ("controlled type must be declared at the library level",
5940                   Indic);
5941             else
5942                Error_Msg_N
5943                  ("type extension at deeper accessibility level than parent",
5944                   Indic);
5945             end if;
5946
5947          else
5948             declare
5949                GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
5950
5951             begin
5952                if Present (GB)
5953                  and then GB /= Enclosing_Generic_Body (Parent_Base)
5954                then
5955                   Error_Msg_NE
5956                     ("parent type of& must not be outside generic body"
5957                        & " ('R'M 3.9.1(4))",
5958                          Indic, Derived_Type);
5959                end if;
5960             end;
5961          end if;
5962       end if;
5963
5964       --  Ada 2005 (AI-251)
5965
5966       if Ada_Version = Ada_05
5967         and then Is_Tagged
5968       then
5969
5970          --  "The declaration of a specific descendant of an interface type
5971          --  freezes the interface type" (RM 13.14).
5972
5973          declare
5974             Iface : Node_Id;
5975          begin
5976             if Is_Non_Empty_List (Interface_List (Type_Def)) then
5977                Iface := First (Interface_List (Type_Def));
5978                while Present (Iface) loop
5979                   Freeze_Before (N, Etype (Iface));
5980                   Next (Iface);
5981                end loop;
5982             end if;
5983          end;
5984       end if;
5985
5986       --  STEP 1b : preliminary cleanup of the full view of private types
5987
5988       --  If the type is already marked as having discriminants, then it's the
5989       --  completion of a private type or private extension and we need to
5990       --  retain the discriminants from the partial view if the current
5991       --  declaration has Discriminant_Specifications so that we can verify
5992       --  conformance. However, we must remove any existing components that
5993       --  were inherited from the parent (and attached in Copy_And_Swap)
5994       --  because the full type inherits all appropriate components anyway, and
5995       --  we do not want the partial view's components interfering.
5996
5997       if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
5998          Discrim := First_Discriminant (Derived_Type);
5999          loop
6000             Last_Discrim := Discrim;
6001             Next_Discriminant (Discrim);
6002             exit when No (Discrim);
6003          end loop;
6004
6005          Set_Last_Entity (Derived_Type, Last_Discrim);
6006
6007       --  In all other cases wipe out the list of inherited components (even
6008       --  inherited discriminants), it will be properly rebuilt here.
6009
6010       else
6011          Set_First_Entity (Derived_Type, Empty);
6012          Set_Last_Entity  (Derived_Type, Empty);
6013       end if;
6014
6015       --  STEP 1c: Initialize some flags for the Derived_Type
6016
6017       --  The following flags must be initialized here so that
6018       --  Process_Discriminants can check that discriminants of tagged types do
6019       --  not have a default initial value and that access discriminants are
6020       --  only specified for limited records. For completeness, these flags are
6021       --  also initialized along with all the other flags below.
6022
6023       --  AI-419: Limitedness is not inherited from an interface parent, so to
6024       --  be limited in that case the type must be explicitly declared as
6025       --  limited.
6026
6027       Set_Is_Tagged_Type    (Derived_Type, Is_Tagged);
6028       Set_Is_Limited_Record (Derived_Type,
6029         Limited_Present (Type_Def)
6030           or else (Is_Limited_Record (Parent_Type)
6031                     and then not Is_Interface (Parent_Type)));
6032
6033       --  STEP 2a: process discriminants of derived type if any
6034
6035       New_Scope (Derived_Type);
6036
6037       if Discriminant_Specs then
6038          Set_Has_Unknown_Discriminants (Derived_Type, False);
6039
6040          --  The following call initializes fields Has_Discriminants and
6041          --  Discriminant_Constraint, unless we are processing the completion
6042          --  of a private type declaration.
6043
6044          Check_Or_Process_Discriminants (N, Derived_Type);
6045
6046          --  For non-tagged types the constraint on the Parent_Type must be
6047          --  present and is used to rename the discriminants.
6048
6049          if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
6050             Error_Msg_N ("untagged parent must have discriminants", Indic);
6051
6052          elsif not Is_Tagged and then not Constraint_Present then
6053             Error_Msg_N
6054               ("discriminant constraint needed for derived untagged records",
6055                Indic);
6056
6057          --  Otherwise the parent subtype must be constrained unless we have a
6058          --  private extension.
6059
6060          elsif not Constraint_Present
6061            and then not Private_Extension
6062            and then not Is_Constrained (Parent_Type)
6063          then
6064             Error_Msg_N
6065               ("unconstrained type not allowed in this context", Indic);
6066
6067          elsif Constraint_Present then
6068             --  The following call sets the field Corresponding_Discriminant
6069             --  for the discriminants in the Derived_Type.
6070
6071             Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
6072
6073             --  For untagged types all new discriminants must rename
6074             --  discriminants in the parent. For private extensions new
6075             --  discriminants cannot rename old ones (implied by [7.3(13)]).
6076
6077             Discrim := First_Discriminant (Derived_Type);
6078             while Present (Discrim) loop
6079                if not Is_Tagged
6080                  and then No (Corresponding_Discriminant (Discrim))
6081                then
6082                   Error_Msg_N
6083                     ("new discriminants must constrain old ones", Discrim);
6084
6085                elsif Private_Extension
6086                  and then Present (Corresponding_Discriminant (Discrim))
6087                then
6088                   Error_Msg_N
6089                     ("only static constraints allowed for parent"
6090                      & " discriminants in the partial view", Indic);
6091                   exit;
6092                end if;
6093
6094                --  If a new discriminant is used in the constraint, then its
6095                --  subtype must be statically compatible with the parent
6096                --  discriminant's subtype (3.7(15)).
6097
6098                if Present (Corresponding_Discriminant (Discrim))
6099                  and then
6100                    not Subtypes_Statically_Compatible
6101                          (Etype (Discrim),
6102                           Etype (Corresponding_Discriminant (Discrim)))
6103                then
6104                   Error_Msg_N
6105                     ("subtype must be compatible with parent discriminant",
6106                      Discrim);
6107                end if;
6108
6109                Next_Discriminant (Discrim);
6110             end loop;
6111
6112             --  Check whether the constraints of the full view statically
6113             --  match those imposed by the parent subtype [7.3(13)].
6114
6115             if Present (Stored_Constraint (Derived_Type)) then
6116                declare
6117                   C1, C2 : Elmt_Id;
6118
6119                begin
6120                   C1 := First_Elmt (Discs);
6121                   C2 := First_Elmt (Stored_Constraint (Derived_Type));
6122                   while Present (C1) and then Present (C2) loop
6123                      if not
6124                        Fully_Conformant_Expressions (Node (C1), Node (C2))
6125                      then
6126                         Error_Msg_N
6127                           ("not conformant with previous declaration",
6128                            Node (C1));
6129                      end if;
6130
6131                      Next_Elmt (C1);
6132                      Next_Elmt (C2);
6133                   end loop;
6134                end;
6135             end if;
6136          end if;
6137
6138       --  STEP 2b: No new discriminants, inherit discriminants if any
6139
6140       else
6141          if Private_Extension then
6142             Set_Has_Unknown_Discriminants
6143               (Derived_Type,
6144                Has_Unknown_Discriminants (Parent_Type)
6145                  or else Unknown_Discriminants_Present (N));
6146
6147          --  The partial view of the parent may have unknown discriminants,
6148          --  but if the full view has discriminants and the parent type is
6149          --  in scope they must be inherited.
6150
6151          elsif Has_Unknown_Discriminants (Parent_Type)
6152            and then
6153             (not Has_Discriminants (Parent_Type)
6154               or else not In_Open_Scopes (Scope (Parent_Type)))
6155          then
6156             Set_Has_Unknown_Discriminants (Derived_Type);
6157          end if;
6158
6159          if not Has_Unknown_Discriminants (Derived_Type)
6160            and then not Has_Unknown_Discriminants (Parent_Base)
6161            and then Has_Discriminants (Parent_Type)
6162          then
6163             Inherit_Discrims := True;
6164             Set_Has_Discriminants
6165               (Derived_Type, True);
6166             Set_Discriminant_Constraint
6167               (Derived_Type, Discriminant_Constraint (Parent_Base));
6168          end if;
6169
6170          --  The following test is true for private types (remember
6171          --  transformation 5. is not applied to those) and in an error
6172          --  situation.
6173
6174          if Constraint_Present then
6175             Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
6176          end if;
6177
6178          --  For now mark a new derived type as constrained only if it has no
6179          --  discriminants. At the end of Build_Derived_Record_Type we properly
6180          --  set this flag in the case of private extensions. See comments in
6181          --  point 9. just before body of Build_Derived_Record_Type.
6182
6183          Set_Is_Constrained
6184            (Derived_Type,
6185             not (Inherit_Discrims
6186                    or else Has_Unknown_Discriminants (Derived_Type)));
6187       end if;
6188
6189       --  STEP 3: initialize fields of derived type
6190
6191       Set_Is_Tagged_Type    (Derived_Type, Is_Tagged);
6192       Set_Stored_Constraint (Derived_Type, No_Elist);
6193
6194       --  Ada 2005 (AI-251): Private type-declarations can implement interfaces
6195       --  but cannot be interfaces
6196
6197       if not Private_Extension
6198          and then Ekind (Derived_Type) /= E_Private_Type
6199          and then Ekind (Derived_Type) /= E_Limited_Private_Type
6200       then
6201          Set_Is_Interface (Derived_Type, Interface_Present (Type_Def));
6202          Set_Abstract_Interfaces (Derived_Type, No_Elist);
6203       end if;
6204
6205       --  Fields inherited from the Parent_Type
6206
6207       Set_Discard_Names
6208         (Derived_Type, Einfo.Discard_Names      (Parent_Type));
6209       Set_Has_Specified_Layout
6210         (Derived_Type, Has_Specified_Layout     (Parent_Type));
6211       Set_Is_Limited_Composite
6212         (Derived_Type, Is_Limited_Composite     (Parent_Type));
6213       Set_Is_Limited_Record
6214         (Derived_Type,
6215            Is_Limited_Record        (Parent_Type)
6216              and then not Is_Interface (Parent_Type));
6217       Set_Is_Private_Composite
6218         (Derived_Type, Is_Private_Composite     (Parent_Type));
6219
6220       --  Fields inherited from the Parent_Base
6221
6222       Set_Has_Controlled_Component
6223         (Derived_Type, Has_Controlled_Component (Parent_Base));
6224       Set_Has_Non_Standard_Rep
6225         (Derived_Type, Has_Non_Standard_Rep     (Parent_Base));
6226       Set_Has_Primitive_Operations
6227         (Derived_Type, Has_Primitive_Operations (Parent_Base));
6228
6229       --  Direct controlled types do not inherit Finalize_Storage_Only flag
6230
6231       if not Is_Controlled  (Parent_Type) then
6232          Set_Finalize_Storage_Only
6233            (Derived_Type, Finalize_Storage_Only (Parent_Type));
6234       end if;
6235
6236       --  Set fields for private derived types
6237
6238       if Is_Private_Type (Derived_Type) then
6239          Set_Depends_On_Private (Derived_Type, True);
6240          Set_Private_Dependents (Derived_Type, New_Elmt_List);
6241
6242       --  Inherit fields from non private record types. If this is the
6243       --  completion of a derivation from a private type, the parent itself
6244       --  is private, and the attributes come from its full view, which must
6245       --  be present.
6246
6247       else
6248          if Is_Private_Type (Parent_Base)
6249            and then not Is_Record_Type (Parent_Base)
6250          then
6251             Set_Component_Alignment
6252               (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
6253             Set_C_Pass_By_Copy
6254               (Derived_Type, C_Pass_By_Copy      (Full_View (Parent_Base)));
6255          else
6256             Set_Component_Alignment
6257               (Derived_Type, Component_Alignment (Parent_Base));
6258
6259             Set_C_Pass_By_Copy
6260               (Derived_Type, C_Pass_By_Copy      (Parent_Base));
6261          end if;
6262       end if;
6263
6264       --  Set fields for tagged types
6265
6266       if Is_Tagged then
6267          Set_Primitive_Operations (Derived_Type, New_Elmt_List);
6268
6269          --  All tagged types defined in Ada.Finalization are controlled
6270
6271          if Chars (Scope (Derived_Type)) = Name_Finalization
6272            and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
6273            and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
6274          then
6275             Set_Is_Controlled (Derived_Type);
6276          else
6277             Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
6278          end if;
6279
6280          Make_Class_Wide_Type (Derived_Type);
6281          Set_Is_Abstract      (Derived_Type, Abstract_Present (Type_Def));
6282
6283          if Has_Discriminants (Derived_Type)
6284            and then Constraint_Present
6285          then
6286             Set_Stored_Constraint
6287               (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
6288          end if;
6289
6290          --  Ada 2005 (AI-251): Collect the list of progenitors that are not
6291          --  already in the parents.
6292
6293          if Ada_Version >= Ada_05 then
6294             declare
6295                Ifaces_List : Elist_Id;
6296             begin
6297                Collect_Abstract_Interfaces
6298                  (T                         => Derived_Type,
6299                   Ifaces_List               => Ifaces_List,
6300                   Exclude_Parent_Interfaces => True);
6301                Set_Abstract_Interfaces (Derived_Type, Ifaces_List);
6302             end;
6303          end if;
6304
6305       else
6306          Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
6307          Set_Has_Non_Standard_Rep
6308                        (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
6309       end if;
6310
6311       --  STEP 4: Inherit components from the parent base and constrain them.
6312       --          Apply the second transformation described in point 6. above.
6313
6314       if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
6315         or else not Has_Discriminants (Parent_Type)
6316         or else not Is_Constrained (Parent_Type)
6317       then
6318          Constrs := Discs;
6319       else
6320          Constrs := Discriminant_Constraint (Parent_Type);
6321       end if;
6322
6323       Assoc_List :=
6324         Inherit_Components
6325           (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
6326
6327       --  STEP 5a: Copy the parent record declaration for untagged types
6328
6329       if not Is_Tagged then
6330
6331          --  Discriminant_Constraint (Derived_Type) has been properly
6332          --  constructed. Save it and temporarily set it to Empty because we
6333          --  do not want the call to New_Copy_Tree below to mess this list.
6334
6335          if Has_Discriminants (Derived_Type) then
6336             Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
6337             Set_Discriminant_Constraint (Derived_Type, No_Elist);
6338          else
6339             Save_Discr_Constr := No_Elist;
6340          end if;
6341
6342          --  Save the Etype field of Derived_Type. It is correctly set now,
6343          --  but the call to New_Copy tree may remap it to point to itself,
6344          --  which is not what we want. Ditto for the Next_Entity field.
6345
6346          Save_Etype       := Etype (Derived_Type);
6347          Save_Next_Entity := Next_Entity (Derived_Type);
6348
6349          --  Assoc_List maps all stored discriminants in the Parent_Base to
6350          --  stored discriminants in the Derived_Type. It is fundamental that
6351          --  no types or itypes with discriminants other than the stored
6352          --  discriminants appear in the entities declared inside
6353          --  Derived_Type, since the back end cannot deal with it.
6354
6355          New_Decl :=
6356            New_Copy_Tree
6357              (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
6358
6359          --  Restore the fields saved prior to the New_Copy_Tree call
6360          --  and compute the stored constraint.
6361
6362          Set_Etype       (Derived_Type, Save_Etype);
6363          Set_Next_Entity (Derived_Type, Save_Next_Entity);
6364
6365          if Has_Discriminants (Derived_Type) then
6366             Set_Discriminant_Constraint
6367               (Derived_Type, Save_Discr_Constr);
6368             Set_Stored_Constraint
6369               (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
6370             Replace_Components (Derived_Type, New_Decl);
6371          end if;
6372
6373          --  Insert the new derived type declaration
6374
6375          Rewrite (N, New_Decl);
6376
6377       --  STEP 5b: Complete the processing for record extensions in generics
6378
6379       --  There is no completion for record extensions declared in the
6380       --  parameter part of a generic, so we need to complete processing for
6381       --  these generic record extensions here. The Record_Type_Definition call
6382       --  will change the Ekind of the components from E_Void to E_Component.
6383
6384       elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
6385          Record_Type_Definition (Empty, Derived_Type);
6386
6387       --  STEP 5c: Process the record extension for non private tagged types
6388
6389       elsif not Private_Extension then
6390
6391          --  Add the _parent field in the derived type
6392
6393          Expand_Record_Extension (Derived_Type, Type_Def);
6394
6395          --  Ada 2005 (AI-251): Addition of the Tag corresponding to all the
6396          --  implemented interfaces if we are in expansion mode
6397
6398          if Expander_Active then
6399             Add_Interface_Tag_Components (N, Derived_Type);
6400          end if;
6401
6402          --  Analyze the record extension
6403
6404          Record_Type_Definition
6405            (Record_Extension_Part (Type_Def), Derived_Type);
6406       end if;
6407
6408       End_Scope;
6409
6410       --  Nothing else to do if there is an error in the derivation.
6411       --  An unusual case: the full view may be derived from a type in an
6412       --  instance, when the partial view was used illegally as an actual
6413       --  in that instance, leading to a circular definition.
6414
6415       if Etype (Derived_Type) = Any_Type
6416         or else Etype (Parent_Type) = Derived_Type
6417       then
6418          return;
6419       end if;
6420
6421       --  Set delayed freeze and then derive subprograms, we need to do
6422       --  this in this order so that derived subprograms inherit the
6423       --  derived freeze if necessary.
6424
6425       Set_Has_Delayed_Freeze (Derived_Type);
6426
6427       if Derive_Subps then
6428          Derive_Subprograms (Parent_Type, Derived_Type);
6429       end if;
6430
6431       --  If we have a private extension which defines a constrained derived
6432       --  type mark as constrained here after we have derived subprograms. See
6433       --  comment on point 9. just above the body of Build_Derived_Record_Type.
6434
6435       if Private_Extension and then Inherit_Discrims then
6436          if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
6437             Set_Is_Constrained          (Derived_Type, True);
6438             Set_Discriminant_Constraint (Derived_Type, Discs);
6439
6440          elsif Is_Constrained (Parent_Type) then
6441             Set_Is_Constrained
6442               (Derived_Type, True);
6443             Set_Discriminant_Constraint
6444               (Derived_Type, Discriminant_Constraint (Parent_Type));
6445          end if;
6446       end if;
6447
6448       --  Update the class_wide type, which shares the now-completed
6449       --  entity list with its specific type.
6450
6451       if Is_Tagged then
6452          Set_First_Entity
6453            (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
6454          Set_Last_Entity
6455            (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
6456       end if;
6457
6458    end Build_Derived_Record_Type;
6459
6460    ------------------------
6461    -- Build_Derived_Type --
6462    ------------------------
6463
6464    procedure Build_Derived_Type
6465      (N             : Node_Id;
6466       Parent_Type   : Entity_Id;
6467       Derived_Type  : Entity_Id;
6468       Is_Completion : Boolean;
6469       Derive_Subps  : Boolean := True)
6470    is
6471       Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6472
6473    begin
6474       --  Set common attributes
6475
6476       Set_Scope          (Derived_Type, Current_Scope);
6477
6478       Set_Ekind          (Derived_Type, Ekind     (Parent_Base));
6479       Set_Etype          (Derived_Type,            Parent_Base);
6480       Set_Has_Task       (Derived_Type, Has_Task  (Parent_Base));
6481
6482       Set_Size_Info      (Derived_Type,                 Parent_Type);
6483       Set_RM_Size        (Derived_Type, RM_Size        (Parent_Type));
6484       Set_Convention     (Derived_Type, Convention     (Parent_Type));
6485       Set_Is_Controlled  (Derived_Type, Is_Controlled  (Parent_Type));
6486
6487       --  The derived type inherits the representation clauses of the parent.
6488       --  However, for a private type that is completed by a derivation, there
6489       --  may be operation attributes that have been specified already (stream
6490       --  attributes and External_Tag) and those must be provided. Finally,
6491       --  if the partial view is a private extension, the representation items
6492       --  of the parent have been inherited already, and should not be chained
6493       --  twice to the derived type.
6494
6495       if Is_Tagged_Type (Parent_Type)
6496         and then Present (First_Rep_Item (Derived_Type))
6497       then
6498          --  The existing items are either operational items or items inherited
6499          --  from a private extension declaration.
6500
6501          declare
6502             Rep   : Node_Id;
6503             Found : Boolean := False;
6504
6505          begin
6506             Rep := First_Rep_Item (Derived_Type);
6507             while Present (Rep) loop
6508                if Rep = First_Rep_Item (Parent_Type) then
6509                   Found := True;
6510                   exit;
6511                else
6512                   Rep := Next_Rep_Item (Rep);
6513                end if;
6514             end loop;
6515
6516             if not Found then
6517                Set_Next_Rep_Item
6518                  (First_Rep_Item (Derived_Type), First_Rep_Item (Parent_Type));
6519             end if;
6520          end;
6521
6522       else
6523          Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
6524       end if;
6525
6526       case Ekind (Parent_Type) is
6527          when Numeric_Kind =>
6528             Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
6529
6530          when Array_Kind =>
6531             Build_Derived_Array_Type (N, Parent_Type,  Derived_Type);
6532
6533          when E_Record_Type
6534             | E_Record_Subtype
6535             | Class_Wide_Kind  =>
6536             Build_Derived_Record_Type
6537               (N, Parent_Type, Derived_Type, Derive_Subps);
6538             return;
6539
6540          when Enumeration_Kind =>
6541             Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
6542
6543          when Access_Kind =>
6544             Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
6545
6546          when Incomplete_Or_Private_Kind =>
6547             Build_Derived_Private_Type
6548               (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
6549
6550             --  For discriminated types, the derivation includes deriving
6551             --  primitive operations. For others it is done below.
6552
6553             if Is_Tagged_Type (Parent_Type)
6554               or else Has_Discriminants (Parent_Type)
6555               or else (Present (Full_View (Parent_Type))
6556                         and then Has_Discriminants (Full_View (Parent_Type)))
6557             then
6558                return;
6559             end if;
6560
6561          when Concurrent_Kind =>
6562             Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
6563
6564          when others =>
6565             raise Program_Error;
6566       end case;
6567
6568       if Etype (Derived_Type) = Any_Type then
6569          return;
6570       end if;
6571
6572       --  Set delayed freeze and then derive subprograms, we need to do this
6573       --  in this order so that derived subprograms inherit the derived freeze
6574       --  if necessary.
6575
6576       Set_Has_Delayed_Freeze (Derived_Type);
6577       if Derive_Subps then
6578          Derive_Subprograms (Parent_Type, Derived_Type);
6579       end if;
6580
6581       Set_Has_Primitive_Operations
6582         (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
6583    end Build_Derived_Type;
6584
6585    -----------------------
6586    -- Build_Discriminal --
6587    -----------------------
6588
6589    procedure Build_Discriminal (Discrim : Entity_Id) is
6590       D_Minal : Entity_Id;
6591       CR_Disc : Entity_Id;
6592
6593    begin
6594       --  A discriminal has the same name as the discriminant
6595
6596       D_Minal :=
6597         Make_Defining_Identifier (Sloc (Discrim),
6598           Chars => Chars (Discrim));
6599
6600       Set_Ekind     (D_Minal, E_In_Parameter);
6601       Set_Mechanism (D_Minal, Default_Mechanism);
6602       Set_Etype     (D_Minal, Etype (Discrim));
6603
6604       Set_Discriminal (Discrim, D_Minal);
6605       Set_Discriminal_Link (D_Minal, Discrim);
6606
6607       --  For task types, build at once the discriminants of the corresponding
6608       --  record, which are needed if discriminants are used in entry defaults
6609       --  and in family bounds.
6610
6611       if Is_Concurrent_Type (Current_Scope)
6612         or else Is_Limited_Type (Current_Scope)
6613       then
6614          CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
6615
6616          Set_Ekind            (CR_Disc, E_In_Parameter);
6617          Set_Mechanism        (CR_Disc, Default_Mechanism);
6618          Set_Etype            (CR_Disc, Etype (Discrim));
6619          Set_Discriminal_Link (CR_Disc, Discrim);
6620          Set_CR_Discriminant  (Discrim, CR_Disc);
6621       end if;
6622    end Build_Discriminal;
6623
6624    ------------------------------------
6625    -- Build_Discriminant_Constraints --
6626    ------------------------------------
6627
6628    function Build_Discriminant_Constraints
6629      (T           : Entity_Id;
6630       Def         : Node_Id;
6631       Derived_Def : Boolean := False) return Elist_Id
6632    is
6633       C        : constant Node_Id := Constraint (Def);
6634       Nb_Discr : constant Nat     := Number_Discriminants (T);
6635
6636       Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
6637       --  Saves the expression corresponding to a given discriminant in T
6638
6639       function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
6640       --  Return the Position number within array Discr_Expr of a discriminant
6641       --  D within the discriminant list of the discriminated type T.
6642
6643       ------------------
6644       -- Pos_Of_Discr --
6645       ------------------
6646
6647       function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
6648          Disc : Entity_Id;
6649
6650       begin
6651          Disc := First_Discriminant (T);
6652          for J in Discr_Expr'Range loop
6653             if Disc = D then
6654                return J;
6655             end if;
6656
6657             Next_Discriminant (Disc);
6658          end loop;
6659
6660          --  Note: Since this function is called on discriminants that are
6661          --  known to belong to the discriminated type, falling through the
6662          --  loop with no match signals an internal compiler error.
6663
6664          raise Program_Error;
6665       end Pos_Of_Discr;
6666
6667       --  Declarations local to Build_Discriminant_Constraints
6668
6669       Discr : Entity_Id;
6670       E     : Entity_Id;
6671       Elist : constant Elist_Id := New_Elmt_List;
6672
6673       Constr   : Node_Id;
6674       Expr     : Node_Id;
6675       Id       : Node_Id;
6676       Position : Nat;
6677       Found    : Boolean;
6678
6679       Discrim_Present : Boolean := False;
6680
6681    --  Start of processing for Build_Discriminant_Constraints
6682
6683    begin
6684       --  The following loop will process positional associations only.
6685       --  For a positional association, the (single) discriminant is
6686       --  implicitly specified by position, in textual order (RM 3.7.2).
6687
6688       Discr  := First_Discriminant (T);
6689       Constr := First (Constraints (C));
6690       for D in Discr_Expr'Range loop
6691          exit when Nkind (Constr) = N_Discriminant_Association;
6692
6693          if No (Constr) then
6694             Error_Msg_N ("too few discriminants given in constraint", C);
6695             return New_Elmt_List;
6696
6697          elsif Nkind (Constr) = N_Range
6698            or else (Nkind (Constr) = N_Attribute_Reference
6699                      and then
6700                     Attribute_Name (Constr) = Name_Range)
6701          then
6702             Error_Msg_N
6703               ("a range is not a valid discriminant constraint", Constr);
6704             Discr_Expr (D) := Error;
6705
6706          else
6707             Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
6708             Discr_Expr (D) := Constr;
6709          end if;
6710
6711          Next_Discriminant (Discr);
6712          Next (Constr);
6713       end loop;
6714
6715       if No (Discr) and then Present (Constr) then
6716          Error_Msg_N ("too many discriminants given in constraint", Constr);
6717          return New_Elmt_List;
6718       end if;
6719
6720       --  Named associations can be given in any order, but if both positional
6721       --  and named associations are used in the same discriminant constraint,
6722       --  then positional associations must occur first, at their normal
6723       --  position. Hence once a named association is used, the rest of the
6724       --  discriminant constraint must use only named associations.
6725
6726       while Present (Constr) loop
6727
6728          --  Positional association forbidden after a named association
6729
6730          if Nkind (Constr) /= N_Discriminant_Association then
6731             Error_Msg_N ("positional association follows named one", Constr);
6732             return New_Elmt_List;
6733
6734          --  Otherwise it is a named association
6735
6736          else
6737             --  E records the type of the discriminants in the named
6738             --  association. All the discriminants specified in the same name
6739             --  association must have the same type.
6740
6741             E := Empty;
6742
6743             --  Search the list of discriminants in T to see if the simple name
6744             --  given in the constraint matches any of them.
6745
6746             Id := First (Selector_Names (Constr));
6747             while Present (Id) loop
6748                Found := False;
6749
6750                --  If Original_Discriminant is present, we are processing a
6751                --  generic instantiation and this is an instance node. We need
6752                --  to find the name of the corresponding discriminant in the
6753                --  actual record type T and not the name of the discriminant in
6754                --  the generic formal. Example:
6755
6756                --    generic
6757                --       type G (D : int) is private;
6758                --    package P is
6759                --       subtype W is G (D => 1);
6760                --    end package;
6761                --    type Rec (X : int) is record ... end record;
6762                --    package Q is new P (G => Rec);
6763
6764                --  At the point of the instantiation, formal type G is Rec
6765                --  and therefore when reanalyzing "subtype W is G (D => 1);"
6766                --  which really looks like "subtype W is Rec (D => 1);" at
6767                --  the point of instantiation, we want to find the discriminant
6768                --  that corresponds to D in Rec, ie X.
6769
6770                if Present (Original_Discriminant (Id)) then
6771                   Discr := Find_Corresponding_Discriminant (Id, T);
6772                   Found := True;
6773
6774                else
6775                   Discr := First_Discriminant (T);
6776                   while Present (Discr) loop
6777                      if Chars (Discr) = Chars (Id) then
6778                         Found := True;
6779                         exit;
6780                      end if;
6781
6782                      Next_Discriminant (Discr);
6783                   end loop;
6784
6785                   if not Found then
6786                      Error_Msg_N ("& does not match any discriminant", Id);
6787                      return New_Elmt_List;
6788
6789                   --  The following is only useful for the benefit of generic
6790                   --  instances but it does not interfere with other
6791                   --  processing for the non-generic case so we do it in all
6792                   --  cases (for generics this statement is executed when
6793                   --  processing the generic definition, see comment at the
6794                   --  beginning of this if statement).
6795
6796                   else
6797                      Set_Original_Discriminant (Id, Discr);
6798                   end if;
6799                end if;
6800
6801                Position := Pos_Of_Discr (T, Discr);
6802
6803                if Present (Discr_Expr (Position)) then
6804                   Error_Msg_N ("duplicate constraint for discriminant&", Id);
6805
6806                else
6807                   --  Each discriminant specified in the same named association
6808                   --  must be associated with a separate copy of the
6809                   --  corresponding expression.
6810
6811                   if Present (Next (Id)) then
6812                      Expr := New_Copy_Tree (Expression (Constr));
6813                      Set_Parent (Expr, Parent (Expression (Constr)));
6814                   else
6815                      Expr := Expression (Constr);
6816                   end if;
6817
6818                   Discr_Expr (Position) := Expr;
6819                   Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
6820                end if;
6821
6822                --  A discriminant association with more than one discriminant
6823                --  name is only allowed if the named discriminants are all of
6824                --  the same type (RM 3.7.1(8)).
6825
6826                if E = Empty then
6827                   E := Base_Type (Etype (Discr));
6828
6829                elsif Base_Type (Etype (Discr)) /= E then
6830                   Error_Msg_N
6831                     ("all discriminants in an association " &
6832                      "must have the same type", Id);
6833                end if;
6834
6835                Next (Id);
6836             end loop;
6837          end if;
6838
6839          Next (Constr);
6840       end loop;
6841
6842       --  A discriminant constraint must provide exactly one value for each
6843       --  discriminant of the type (RM 3.7.1(8)).
6844
6845       for J in Discr_Expr'Range loop
6846          if No (Discr_Expr (J)) then
6847             Error_Msg_N ("too few discriminants given in constraint", C);
6848             return New_Elmt_List;
6849          end if;
6850       end loop;
6851
6852       --  Determine if there are discriminant expressions in the constraint
6853
6854       for J in Discr_Expr'Range loop
6855          if Denotes_Discriminant
6856               (Discr_Expr (J), Check_Concurrent => True)
6857          then
6858             Discrim_Present := True;
6859          end if;
6860       end loop;
6861
6862       --  Build an element list consisting of the expressions given in the
6863       --  discriminant constraint and apply the appropriate checks. The list
6864       --  is constructed after resolving any named discriminant associations
6865       --  and therefore the expressions appear in the textual order of the
6866       --  discriminants.
6867
6868       Discr := First_Discriminant (T);
6869       for J in Discr_Expr'Range loop
6870          if Discr_Expr (J) /= Error then
6871             Append_Elmt (Discr_Expr (J), Elist);
6872
6873             --  If any of the discriminant constraints is given by a
6874             --  discriminant and we are in a derived type declaration we
6875             --  have a discriminant renaming. Establish link between new
6876             --  and old discriminant.
6877
6878             if Denotes_Discriminant (Discr_Expr (J)) then
6879                if Derived_Def then
6880                   Set_Corresponding_Discriminant
6881                     (Entity (Discr_Expr (J)), Discr);
6882                end if;
6883
6884             --  Force the evaluation of non-discriminant expressions.
6885             --  If we have found a discriminant in the constraint 3.4(26)
6886             --  and 3.8(18) demand that no range checks are performed are
6887             --  after evaluation. If the constraint is for a component
6888             --  definition that has a per-object constraint, expressions are
6889             --  evaluated but not checked either. In all other cases perform
6890             --  a range check.
6891
6892             else
6893                if Discrim_Present then
6894                   null;
6895
6896                elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
6897                  and then
6898                    Has_Per_Object_Constraint
6899                      (Defining_Identifier (Parent (Parent (Def))))
6900                then
6901                   null;
6902
6903                elsif Is_Access_Type (Etype (Discr)) then
6904                   Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
6905
6906                else
6907                   Apply_Range_Check (Discr_Expr (J), Etype (Discr));
6908                end if;
6909
6910                Force_Evaluation (Discr_Expr (J));
6911             end if;
6912
6913             --  Check that the designated type of an access discriminant's
6914             --  expression is not a class-wide type unless the discriminant's
6915             --  designated type is also class-wide.
6916
6917             if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
6918               and then not Is_Class_Wide_Type
6919                          (Designated_Type (Etype (Discr)))
6920               and then Etype (Discr_Expr (J)) /= Any_Type
6921               and then Is_Class_Wide_Type
6922                          (Designated_Type (Etype (Discr_Expr (J))))
6923             then
6924                Wrong_Type (Discr_Expr (J), Etype (Discr));
6925             end if;
6926          end if;
6927
6928          Next_Discriminant (Discr);
6929       end loop;
6930
6931       return Elist;
6932    end Build_Discriminant_Constraints;
6933
6934    ---------------------------------
6935    -- Build_Discriminated_Subtype --
6936    ---------------------------------
6937
6938    procedure Build_Discriminated_Subtype
6939      (T           : Entity_Id;
6940       Def_Id      : Entity_Id;
6941       Elist       : Elist_Id;
6942       Related_Nod : Node_Id;
6943       For_Access  : Boolean := False)
6944    is
6945       Has_Discrs  : constant Boolean := Has_Discriminants (T);
6946       Constrained : constant Boolean :=
6947                       (Has_Discrs
6948                          and then not Is_Empty_Elmt_List (Elist)
6949                          and then not Is_Class_Wide_Type (T))
6950                         or else Is_Constrained (T);
6951
6952    begin
6953       if Ekind (T) = E_Record_Type then
6954          if For_Access then
6955             Set_Ekind (Def_Id, E_Private_Subtype);
6956             Set_Is_For_Access_Subtype (Def_Id, True);
6957          else
6958             Set_Ekind (Def_Id, E_Record_Subtype);
6959          end if;
6960
6961       elsif Ekind (T) = E_Task_Type then
6962          Set_Ekind (Def_Id, E_Task_Subtype);
6963
6964       elsif Ekind (T) = E_Protected_Type then
6965          Set_Ekind (Def_Id, E_Protected_Subtype);
6966
6967       elsif Is_Private_Type (T) then
6968          Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
6969
6970       elsif Is_Class_Wide_Type (T) then
6971          Set_Ekind (Def_Id, E_Class_Wide_Subtype);
6972
6973       else
6974          --  Incomplete type. Attach subtype to list of dependents, to be
6975          --  completed with full view of parent type,  unless is it the
6976          --  designated subtype of a record component within an init_proc.
6977          --  This last case arises for a component of an access type whose
6978          --  designated type is incomplete (e.g. a Taft Amendment type).
6979          --  The designated subtype is within an inner scope, and needs no
6980          --  elaboration, because only the access type is needed in the
6981          --  initialization procedure.
6982
6983          Set_Ekind (Def_Id, Ekind (T));
6984
6985          if For_Access and then Within_Init_Proc then
6986             null;
6987          else
6988             Append_Elmt (Def_Id, Private_Dependents (T));
6989          end if;
6990       end if;
6991
6992       Set_Etype             (Def_Id, T);
6993       Init_Size_Align       (Def_Id);
6994       Set_Has_Discriminants (Def_Id, Has_Discrs);
6995       Set_Is_Constrained    (Def_Id, Constrained);
6996
6997       Set_First_Entity      (Def_Id, First_Entity   (T));
6998       Set_Last_Entity       (Def_Id, Last_Entity    (T));
6999       Set_First_Rep_Item    (Def_Id, First_Rep_Item (T));
7000
7001       if Is_Tagged_Type (T) then
7002          Set_Is_Tagged_Type  (Def_Id);
7003          Make_Class_Wide_Type (Def_Id);
7004       end if;
7005
7006       Set_Stored_Constraint (Def_Id, No_Elist);
7007
7008       if Has_Discrs then
7009          Set_Discriminant_Constraint (Def_Id, Elist);
7010          Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
7011       end if;
7012
7013       if Is_Tagged_Type (T) then
7014
7015          --  Ada 2005 (AI-251): In case of concurrent types we inherit the
7016          --  concurrent record type (which has the list of primitive
7017          --  operations).
7018
7019          if Ada_Version >= Ada_05
7020            and then Is_Concurrent_Type (T)
7021          then
7022             Set_Corresponding_Record_Type (Def_Id,
7023                Corresponding_Record_Type (T));
7024          else
7025             Set_Primitive_Operations (Def_Id, Primitive_Operations (T));
7026          end if;
7027
7028          Set_Is_Abstract (Def_Id, Is_Abstract (T));
7029       end if;
7030
7031       --  Subtypes introduced by component declarations do not need to be
7032       --  marked as delayed, and do not get freeze nodes, because the semantics
7033       --  verifies that the parents of the subtypes are frozen before the
7034       --  enclosing record is frozen.
7035
7036       if not Is_Type (Scope (Def_Id)) then
7037          Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
7038
7039          if Is_Private_Type (T)
7040            and then Present (Full_View (T))
7041          then
7042             Conditional_Delay (Def_Id, Full_View (T));
7043          else
7044             Conditional_Delay (Def_Id, T);
7045          end if;
7046       end if;
7047
7048       if Is_Record_Type (T) then
7049          Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
7050
7051          if Has_Discrs
7052             and then not Is_Empty_Elmt_List (Elist)
7053             and then not For_Access
7054          then
7055             Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
7056          elsif not For_Access then
7057             Set_Cloned_Subtype (Def_Id, T);
7058          end if;
7059       end if;
7060    end Build_Discriminated_Subtype;
7061
7062    ------------------------
7063    -- Build_Scalar_Bound --
7064    ------------------------
7065
7066    function Build_Scalar_Bound
7067      (Bound : Node_Id;
7068       Par_T : Entity_Id;
7069       Der_T : Entity_Id) return Node_Id
7070    is
7071       New_Bound : Entity_Id;
7072
7073    begin
7074       --  Note: not clear why this is needed, how can the original bound
7075       --  be unanalyzed at this point? and if it is, what business do we
7076       --  have messing around with it? and why is the base type of the
7077       --  parent type the right type for the resolution. It probably is
7078       --  not! It is OK for the new bound we are creating, but not for
7079       --  the old one??? Still if it never happens, no problem!
7080
7081       Analyze_And_Resolve (Bound, Base_Type (Par_T));
7082
7083       if Nkind (Bound) = N_Integer_Literal
7084         or else Nkind (Bound) = N_Real_Literal
7085       then
7086          New_Bound := New_Copy (Bound);
7087          Set_Etype (New_Bound, Der_T);
7088          Set_Analyzed (New_Bound);
7089
7090       elsif Is_Entity_Name (Bound) then
7091          New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
7092
7093       --  The following is almost certainly wrong. What business do we have
7094       --  relocating a node (Bound) that is presumably still attached to
7095       --  the tree elsewhere???
7096
7097       else
7098          New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
7099       end if;
7100
7101       Set_Etype (New_Bound, Der_T);
7102       return New_Bound;
7103    end Build_Scalar_Bound;
7104
7105    --------------------------------
7106    -- Build_Underlying_Full_View --
7107    --------------------------------
7108
7109    procedure Build_Underlying_Full_View
7110      (N   : Node_Id;
7111       Typ : Entity_Id;
7112       Par : Entity_Id)
7113    is
7114       Loc  : constant Source_Ptr := Sloc (N);
7115       Subt : constant Entity_Id :=
7116                Make_Defining_Identifier
7117                  (Loc, New_External_Name (Chars (Typ), 'S'));
7118
7119       Constr : Node_Id;
7120       Indic  : Node_Id;
7121       C      : Node_Id;
7122       Id     : Node_Id;
7123
7124       procedure Set_Discriminant_Name (Id : Node_Id);
7125       --  If the derived type has discriminants, they may rename discriminants
7126       --  of the parent. When building the full view of the parent, we need to
7127       --  recover the names of the original discriminants if the constraint is
7128       --  given by named associations.
7129
7130       ---------------------------
7131       -- Set_Discriminant_Name --
7132       ---------------------------
7133
7134       procedure Set_Discriminant_Name (Id : Node_Id) is
7135          Disc : Entity_Id;
7136
7137       begin
7138          Set_Original_Discriminant (Id, Empty);
7139
7140          if Has_Discriminants (Typ) then
7141             Disc := First_Discriminant (Typ);
7142             while Present (Disc) loop
7143                if Chars (Disc) = Chars (Id)
7144                  and then Present (Corresponding_Discriminant (Disc))
7145                then
7146                   Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
7147                end if;
7148                Next_Discriminant (Disc);
7149             end loop;
7150          end if;
7151       end Set_Discriminant_Name;
7152
7153    --  Start of processing for Build_Underlying_Full_View
7154
7155    begin
7156       if Nkind (N) = N_Full_Type_Declaration then
7157          Constr := Constraint (Subtype_Indication (Type_Definition (N)));
7158
7159       elsif Nkind (N) = N_Subtype_Declaration then
7160          Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
7161
7162       elsif Nkind (N) = N_Component_Declaration then
7163          Constr :=
7164            New_Copy_Tree
7165              (Constraint (Subtype_Indication (Component_Definition (N))));
7166
7167       else
7168          raise Program_Error;
7169       end if;
7170
7171       C := First (Constraints (Constr));
7172       while Present (C) loop
7173          if Nkind (C) = N_Discriminant_Association then
7174             Id := First (Selector_Names (C));
7175             while Present (Id) loop
7176                Set_Discriminant_Name (Id);
7177                Next (Id);
7178             end loop;
7179          end if;
7180
7181          Next (C);
7182       end loop;
7183
7184       Indic :=
7185         Make_Subtype_Declaration (Loc,
7186           Defining_Identifier => Subt,
7187           Subtype_Indication  =>
7188             Make_Subtype_Indication (Loc,
7189               Subtype_Mark => New_Reference_To (Par, Loc),
7190               Constraint   => New_Copy_Tree (Constr)));
7191
7192       --  If this is a component subtype for an outer itype, it is not
7193       --  a list member, so simply set the parent link for analysis: if
7194       --  the enclosing type does not need to be in a declarative list,
7195       --  neither do the components.
7196
7197       if Is_List_Member (N)
7198         and then Nkind (N) /= N_Component_Declaration
7199       then
7200          Insert_Before (N, Indic);
7201       else
7202          Set_Parent (Indic, Parent (N));
7203       end if;
7204
7205       Analyze (Indic);
7206       Set_Underlying_Full_View (Typ, Full_View (Subt));
7207    end Build_Underlying_Full_View;
7208
7209    -------------------------------
7210    -- Check_Abstract_Overriding --
7211    -------------------------------
7212
7213    procedure Check_Abstract_Overriding (T : Entity_Id) is
7214       Alias_Subp : Entity_Id;
7215       Elmt       : Elmt_Id;
7216       Op_List    : Elist_Id;
7217       Subp       : Entity_Id;
7218       Type_Def   : Node_Id;
7219
7220    begin
7221       Op_List := Primitive_Operations (T);
7222
7223       --  Loop to check primitive operations
7224
7225       Elmt := First_Elmt (Op_List);
7226       while Present (Elmt) loop
7227          Subp := Node (Elmt);
7228          Alias_Subp := Alias (Subp);
7229
7230          --  Inherited subprograms are identified by the fact that they do not
7231          --  come from source, and the associated source location is the
7232          --  location of the first subtype of the derived type.
7233
7234          --  Special exception, do not complain about failure to override the
7235          --  stream routines _Input and _Output, as well as the primitive
7236          --  operations used in dispatching selects since we always provide
7237          --  automatic overridings for these subprograms.
7238
7239          if (Is_Abstract (Subp)
7240                or else (Has_Controlling_Result (Subp)
7241                          and then Present (Alias_Subp)
7242                          and then not Comes_From_Source (Subp)
7243                          and then Sloc (Subp) = Sloc (First_Subtype (T))))
7244            and then not Is_TSS (Subp, TSS_Stream_Input)
7245            and then not Is_TSS (Subp, TSS_Stream_Output)
7246            and then not Is_Abstract (T)
7247            and then Chars (Subp) /= Name_uDisp_Asynchronous_Select
7248            and then Chars (Subp) /= Name_uDisp_Conditional_Select
7249            and then Chars (Subp) /= Name_uDisp_Get_Prim_Op_Kind
7250            and then Chars (Subp) /= Name_uDisp_Timed_Select
7251
7252             --  Ada 2005 (AI-251): Do not consider hidden entities associated
7253             --  with abstract interface types because the check will be done
7254             --  with the aliased entity (otherwise we generate a duplicated
7255             --  error message).
7256
7257            and then not Present (Abstract_Interface_Alias (Subp))
7258          then
7259             if Present (Alias_Subp) then
7260
7261                --  Only perform the check for a derived subprogram when the
7262                --  type has an explicit record extension. This avoids
7263                --  incorrectly flagging abstract subprograms for the case of a
7264                --  type without an extension derived from a formal type with a
7265                --  tagged actual (can occur within a private part).
7266
7267                --  Ada 2005 (AI-391): In the case of an inherited function with
7268                --  a controlling result of the type, the rule does not apply if
7269                --  the type is a null extension (unless the parent function
7270                --  itself is abstract, in which case the function must still be
7271                --  be overridden). The expander will generate an overriding
7272                --  wrapper function calling the parent subprogram (see
7273                --  Exp_Ch3.Make_Controlling_Wrapper_Functions).
7274
7275                Type_Def := Type_Definition (Parent (T));
7276                if Nkind (Type_Def) = N_Derived_Type_Definition
7277                  and then Present (Record_Extension_Part (Type_Def))
7278                  and then
7279                    (Ada_Version < Ada_05
7280                       or else not Is_Null_Extension (T)
7281                       or else Ekind (Subp) = E_Procedure
7282                       or else not Has_Controlling_Result (Subp)
7283                       or else Is_Abstract (Alias_Subp)
7284                       or else Is_Access_Type (Etype (Subp)))
7285                then
7286                   Error_Msg_NE
7287                     ("type must be declared abstract or & overridden",
7288                      T, Subp);
7289
7290                   --  Traverse the whole chain of aliased subprograms to
7291                   --  complete the error notification. This is especially
7292                   --  useful for traceability of the chain of entities when the
7293                   --  subprogram corresponds with an interface subprogram
7294                   --  (which might be defined in another package)
7295
7296                   if Present (Alias_Subp) then
7297                      declare
7298                         E : Entity_Id;
7299
7300                      begin
7301                         E := Subp;
7302                         while Present (Alias (E)) loop
7303                            Error_Msg_Sloc := Sloc (E);
7304                            Error_Msg_NE ("\& has been inherited #", T, Subp);
7305                            E := Alias (E);
7306                         end loop;
7307
7308                         Error_Msg_Sloc := Sloc (E);
7309                         Error_Msg_NE
7310                           ("\& has been inherited from subprogram #", T, Subp);
7311                      end;
7312                   end if;
7313
7314                --  Ada 2005 (AI-345): Protected or task type implementing
7315                --  abstract interfaces.
7316
7317                elsif Is_Concurrent_Record_Type (T)
7318                  and then Present (Abstract_Interfaces (T))
7319                then
7320                   --  The controlling formal of Subp must be of mode "out",
7321                   --  "in out" or an access-to-variable to be overridden.
7322
7323                   if Ekind (First_Formal (Subp)) = E_In_Parameter then
7324                      Error_Msg_NE
7325                        ("first formal of & must be of mode `OUT`, `IN OUT` " &
7326                         "or access-to-variable", T, Subp);
7327
7328                      if Is_Protected_Type
7329                           (Corresponding_Concurrent_Type (T))
7330                      then
7331                         Error_Msg_N
7332                           ("\to be overridden by protected procedure or " &
7333                            "entry (`R`M 9.4(11))", T);
7334                      else
7335                         Error_Msg_N
7336                           ("\to be overridden by task entry (`R`M 9.4(11))",
7337                            T);
7338                      end if;
7339
7340                   --  Some other kind of overriding failure
7341
7342                   else
7343                      Error_Msg_NE
7344                        ("interface subprogram & must be overridden",
7345                         T, Subp);
7346                   end if;
7347                end if;
7348
7349             else
7350                Error_Msg_NE
7351                  ("abstract subprogram not allowed for type&",
7352                   Subp, T);
7353                Error_Msg_NE
7354                  ("nonabstract type has abstract subprogram&",
7355                   T, Subp);
7356             end if;
7357          end if;
7358
7359          Next_Elmt (Elmt);
7360       end loop;
7361    end Check_Abstract_Overriding;
7362
7363    ------------------------------------------------
7364    -- Check_Access_Discriminant_Requires_Limited --
7365    ------------------------------------------------
7366
7367    procedure Check_Access_Discriminant_Requires_Limited
7368      (D   : Node_Id;
7369       Loc : Node_Id)
7370    is
7371    begin
7372       --  A discriminant_specification for an access discriminant shall appear
7373       --  only in the declaration for a task or protected type, or for a type
7374       --  with the reserved word 'limited' in its definition or in one of its
7375       --  ancestors. (RM 3.7(10))
7376
7377       if Nkind (Discriminant_Type (D)) = N_Access_Definition
7378         and then not Is_Concurrent_Type (Current_Scope)
7379         and then not Is_Concurrent_Record_Type (Current_Scope)
7380         and then not Is_Limited_Record (Current_Scope)
7381         and then Ekind (Current_Scope) /= E_Limited_Private_Type
7382       then
7383          Error_Msg_N
7384            ("access discriminants allowed only for limited types", Loc);
7385       end if;
7386    end Check_Access_Discriminant_Requires_Limited;
7387
7388    -----------------------------------
7389    -- Check_Aliased_Component_Types --
7390    -----------------------------------
7391
7392    procedure Check_Aliased_Component_Types (T : Entity_Id) is
7393       C : Entity_Id;
7394
7395    begin
7396       --  ??? Also need to check components of record extensions, but not
7397       --  components of protected types (which are always limited).
7398
7399       --  Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
7400       --  types to be unconstrained. This is safe because it is illegal to
7401       --  create access subtypes to such types with explicit discriminant
7402       --  constraints.
7403
7404       if not Is_Limited_Type (T) then
7405          if Ekind (T) = E_Record_Type then
7406             C := First_Component (T);
7407             while Present (C) loop
7408                if Is_Aliased (C)
7409                  and then Has_Discriminants (Etype (C))
7410                  and then not Is_Constrained (Etype (C))
7411                  and then not In_Instance_Body
7412                  and then Ada_Version < Ada_05
7413                then
7414                   Error_Msg_N
7415                     ("aliased component must be constrained ('R'M 3.6(11))",
7416                       C);
7417                end if;
7418
7419                Next_Component (C);
7420             end loop;
7421
7422          elsif Ekind (T) = E_Array_Type then
7423             if Has_Aliased_Components (T)
7424               and then Has_Discriminants (Component_Type (T))
7425               and then not Is_Constrained (Component_Type (T))
7426               and then not In_Instance_Body
7427               and then Ada_Version < Ada_05
7428             then
7429                Error_Msg_N
7430                  ("aliased component type must be constrained ('R'M 3.6(11))",
7431                     T);
7432             end if;
7433          end if;
7434       end if;
7435    end Check_Aliased_Component_Types;
7436
7437    ----------------------
7438    -- Check_Completion --
7439    ----------------------
7440
7441    procedure Check_Completion (Body_Id : Node_Id := Empty) is
7442       E : Entity_Id;
7443
7444       procedure Post_Error;
7445       --  Post error message for lack of completion for entity E
7446
7447       ----------------
7448       -- Post_Error --
7449       ----------------
7450
7451       procedure Post_Error is
7452       begin
7453          if not Comes_From_Source (E) then
7454
7455             if Ekind (E) = E_Task_Type
7456               or else Ekind (E) = E_Protected_Type
7457             then
7458                --  It may be an anonymous protected type created for a
7459                --  single variable. Post error on variable, if present.
7460
7461                declare
7462                   Var : Entity_Id;
7463
7464                begin
7465                   Var := First_Entity (Current_Scope);
7466                   while Present (Var) loop
7467                      exit when Etype (Var) = E
7468                        and then Comes_From_Source (Var);
7469
7470                      Next_Entity (Var);
7471                   end loop;
7472
7473                   if Present (Var) then
7474                      E := Var;
7475                   end if;
7476                end;
7477             end if;
7478          end if;
7479
7480          --  If a generated entity has no completion, then either previous
7481          --  semantic errors have disabled the expansion phase, or else we had
7482          --  missing subunits, or else we are compiling without expan- sion,
7483          --  or else something is very wrong.
7484
7485          if not Comes_From_Source (E) then
7486             pragma Assert
7487               (Serious_Errors_Detected > 0
7488                 or else Configurable_Run_Time_Violations > 0
7489                 or else Subunits_Missing
7490                 or else not Expander_Active);
7491             return;
7492
7493          --  Here for source entity
7494
7495          else
7496             --  Here if no body to post the error message, so we post the error
7497             --  on the declaration that has no completion. This is not really
7498             --  the right place to post it, think about this later ???
7499
7500             if No (Body_Id) then
7501                if Is_Type (E) then
7502                   Error_Msg_NE
7503                     ("missing full declaration for }", Parent (E), E);
7504                else
7505                   Error_Msg_NE
7506                     ("missing body for &", Parent (E), E);
7507                end if;
7508
7509             --  Package body has no completion for a declaration that appears
7510             --  in the corresponding spec. Post error on the body, with a
7511             --  reference to the non-completed declaration.
7512
7513             else
7514                Error_Msg_Sloc := Sloc (E);
7515
7516                if Is_Type (E) then
7517                   Error_Msg_NE
7518                     ("missing full declaration for }!", Body_Id, E);
7519
7520                elsif Is_Overloadable (E)
7521                  and then Current_Entity_In_Scope (E) /= E
7522                then
7523                   --  It may be that the completion is mistyped and appears
7524                   --  as a  distinct overloading of the entity.
7525
7526                   declare
7527                      Candidate : constant Entity_Id :=
7528                                    Current_Entity_In_Scope (E);
7529                      Decl      : constant Node_Id :=
7530                                    Unit_Declaration_Node (Candidate);
7531
7532                   begin
7533                      if Is_Overloadable (Candidate)
7534                        and then Ekind (Candidate) = Ekind (E)
7535                        and then Nkind (Decl) = N_Subprogram_Body
7536                        and then Acts_As_Spec (Decl)
7537                      then
7538                         Check_Type_Conformant (Candidate, E);
7539
7540                      else
7541                         Error_Msg_NE ("missing body for & declared#!",
7542                            Body_Id, E);
7543                      end if;
7544                   end;
7545                else
7546                   Error_Msg_NE ("missing body for & declared#!",
7547                      Body_Id, E);
7548                end if;
7549             end if;
7550          end if;
7551       end Post_Error;
7552
7553    --  Start processing for Check_Completion
7554
7555    begin
7556       E := First_Entity (Current_Scope);
7557       while Present (E) loop
7558          if Is_Intrinsic_Subprogram (E) then
7559             null;
7560
7561          --  The following situation requires special handling: a child
7562          --  unit that appears in the context clause of the body of its
7563          --  parent:
7564
7565          --    procedure Parent.Child (...);
7566
7567          --    with Parent.Child;
7568          --    package body Parent is
7569
7570          --  Here Parent.Child appears as a local entity, but should not
7571          --  be flagged as requiring completion, because it is a
7572          --  compilation unit.
7573
7574          elsif     Ekind (E) = E_Function
7575            or else Ekind (E) = E_Procedure
7576            or else Ekind (E) = E_Generic_Function
7577            or else Ekind (E) = E_Generic_Procedure
7578          then
7579             if not Has_Completion (E)
7580               and then not Is_Abstract (E)
7581               and then Nkind (Parent (Unit_Declaration_Node (E))) /=
7582                                                        N_Compilation_Unit
7583               and then Chars (E) /= Name_uSize
7584             then
7585                Post_Error;
7586             end if;
7587
7588          elsif Is_Entry (E) then
7589             if not Has_Completion (E) and then
7590               (Ekind (Scope (E)) = E_Protected_Object
7591                 or else Ekind (Scope (E)) = E_Protected_Type)
7592             then
7593                Post_Error;
7594             end if;
7595
7596          elsif Is_Package_Or_Generic_Package (E) then
7597             if Unit_Requires_Body (E) then
7598                if not Has_Completion (E)
7599                  and then Nkind (Parent (Unit_Declaration_Node (E))) /=
7600                                                        N_Compilation_Unit
7601                then
7602                   Post_Error;
7603                end if;
7604
7605             elsif not Is_Child_Unit (E) then
7606                May_Need_Implicit_Body (E);
7607             end if;
7608
7609          elsif Ekind (E) = E_Incomplete_Type
7610            and then No (Underlying_Type (E))
7611          then
7612             Post_Error;
7613
7614          elsif (Ekind (E) = E_Task_Type or else
7615                 Ekind (E) = E_Protected_Type)
7616            and then not Has_Completion (E)
7617          then
7618             Post_Error;
7619
7620          --  A single task declared in the current scope is a constant, verify
7621          --  that the body of its anonymous type is in the same scope. If the
7622          --  task is defined elsewhere, this may be a renaming declaration for
7623          --  which no completion is needed.
7624
7625          elsif Ekind (E) = E_Constant
7626            and then Ekind (Etype (E)) = E_Task_Type
7627            and then not Has_Completion (Etype (E))
7628            and then Scope (Etype (E)) = Current_Scope
7629          then
7630             Post_Error;
7631
7632          elsif Ekind (E) = E_Protected_Object
7633            and then not Has_Completion (Etype (E))
7634          then
7635             Post_Error;
7636
7637          elsif Ekind (E) = E_Record_Type then
7638             if Is_Tagged_Type (E) then
7639                Check_Abstract_Overriding (E);
7640                Check_Conventions (E);
7641             end if;
7642
7643             Check_Aliased_Component_Types (E);
7644
7645          elsif Ekind (E) = E_Array_Type then
7646             Check_Aliased_Component_Types (E);
7647
7648          end if;
7649
7650          Next_Entity (E);
7651       end loop;
7652    end Check_Completion;
7653
7654    ----------------------------
7655    -- Check_Delta_Expression --
7656    ----------------------------
7657
7658    procedure Check_Delta_Expression (E : Node_Id) is
7659    begin
7660       if not (Is_Real_Type (Etype (E))) then
7661          Wrong_Type (E, Any_Real);
7662
7663       elsif not Is_OK_Static_Expression (E) then
7664          Flag_Non_Static_Expr
7665            ("non-static expression used for delta value!", E);
7666
7667       elsif not UR_Is_Positive (Expr_Value_R (E)) then
7668          Error_Msg_N ("delta expression must be positive", E);
7669
7670       else
7671          return;
7672       end if;
7673
7674       --  If any of above errors occurred, then replace the incorrect
7675       --  expression by the real 0.1, which should prevent further errors.
7676
7677       Rewrite (E,
7678         Make_Real_Literal (Sloc (E), Ureal_Tenth));
7679       Analyze_And_Resolve (E, Standard_Float);
7680    end Check_Delta_Expression;
7681
7682    -----------------------------
7683    -- Check_Digits_Expression --
7684    -----------------------------
7685
7686    procedure Check_Digits_Expression (E : Node_Id) is
7687    begin
7688       if not (Is_Integer_Type (Etype (E))) then
7689          Wrong_Type (E, Any_Integer);
7690
7691       elsif not Is_OK_Static_Expression (E) then
7692          Flag_Non_Static_Expr
7693            ("non-static expression used for digits value!", E);
7694
7695       elsif Expr_Value (E) <= 0 then
7696          Error_Msg_N ("digits value must be greater than zero", E);
7697
7698       else
7699          return;
7700       end if;
7701
7702       --  If any of above errors occurred, then replace the incorrect
7703       --  expression by the integer 1, which should prevent further errors.
7704
7705       Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
7706       Analyze_And_Resolve (E, Standard_Integer);
7707
7708    end Check_Digits_Expression;
7709
7710    --------------------------
7711    -- Check_Initialization --
7712    --------------------------
7713
7714    procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
7715    begin
7716       if Is_Limited_Type (T)
7717         and then not In_Instance
7718         and then not In_Inlined_Body
7719       then
7720          if not OK_For_Limited_Init (Exp) then
7721             --  In GNAT mode, this is just a warning, to allow it to be
7722             --  evilly turned off. Otherwise it is a real error.
7723
7724             if GNAT_Mode then
7725                Error_Msg_N
7726                  ("cannot initialize entities of limited type?", Exp);
7727             else
7728                Error_Msg_N
7729                  ("cannot initialize entities of limited type", Exp);
7730                Explain_Limited_Type (T, Exp);
7731             end if;
7732          end if;
7733       end if;
7734    end Check_Initialization;
7735
7736    ------------------------------------
7737    -- Check_Or_Process_Discriminants --
7738    ------------------------------------
7739
7740    --  If an incomplete or private type declaration was already given for the
7741    --  type, the discriminants may have already been processed if they were
7742    --  present on the incomplete declaration. In this case a full conformance
7743    --  check is performed otherwise just process them.
7744
7745    procedure Check_Or_Process_Discriminants
7746      (N    : Node_Id;
7747       T    : Entity_Id;
7748       Prev : Entity_Id := Empty)
7749    is
7750    begin
7751       if Has_Discriminants (T) then
7752
7753          --  Make the discriminants visible to component declarations
7754
7755          declare
7756             D    : Entity_Id;
7757             Prev : Entity_Id;
7758
7759          begin
7760             D := First_Discriminant (T);
7761             while Present (D) loop
7762                Prev := Current_Entity (D);
7763                Set_Current_Entity (D);
7764                Set_Is_Immediately_Visible (D);
7765                Set_Homonym (D, Prev);
7766
7767                --  Ada 2005 (AI-230): Access discriminant allowed in
7768                --  non-limited record types.
7769
7770                if Ada_Version < Ada_05 then
7771
7772                   --  This restriction gets applied to the full type here. It
7773                   --  has already been applied earlier to the partial view.
7774
7775                   Check_Access_Discriminant_Requires_Limited (Parent (D), N);
7776                end if;
7777
7778                Next_Discriminant (D);
7779             end loop;
7780          end;
7781
7782       elsif Present (Discriminant_Specifications (N)) then
7783          Process_Discriminants (N, Prev);
7784       end if;
7785    end Check_Or_Process_Discriminants;
7786
7787    ----------------------
7788    -- Check_Real_Bound --
7789    ----------------------
7790
7791    procedure Check_Real_Bound (Bound : Node_Id) is
7792    begin
7793       if not Is_Real_Type (Etype (Bound)) then
7794          Error_Msg_N
7795            ("bound in real type definition must be of real type", Bound);
7796
7797       elsif not Is_OK_Static_Expression (Bound) then
7798          Flag_Non_Static_Expr
7799            ("non-static expression used for real type bound!", Bound);
7800
7801       else
7802          return;
7803       end if;
7804
7805       Rewrite
7806         (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
7807       Analyze (Bound);
7808       Resolve (Bound, Standard_Float);
7809    end Check_Real_Bound;
7810
7811    ------------------------------
7812    -- Complete_Private_Subtype --
7813    ------------------------------
7814
7815    procedure Complete_Private_Subtype
7816      (Priv        : Entity_Id;
7817       Full        : Entity_Id;
7818       Full_Base   : Entity_Id;
7819       Related_Nod : Node_Id)
7820    is
7821       Save_Next_Entity : Entity_Id;
7822       Save_Homonym     : Entity_Id;
7823
7824    begin
7825       --  Set semantic attributes for (implicit) private subtype completion.
7826       --  If the full type has no discriminants, then it is a copy of the full
7827       --  view of the base. Otherwise, it is a subtype of the base with a
7828       --  possible discriminant constraint. Save and restore the original
7829       --  Next_Entity field of full to ensure that the calls to Copy_Node
7830       --  do not corrupt the entity chain.
7831
7832       --  Note that the type of the full view is the same entity as the type of
7833       --  the partial view. In this fashion, the subtype has access to the
7834       --  correct view of the parent.
7835
7836       Save_Next_Entity := Next_Entity (Full);
7837       Save_Homonym     := Homonym (Priv);
7838
7839       case Ekind (Full_Base) is
7840          when E_Record_Type    |
7841               E_Record_Subtype |
7842               Class_Wide_Kind  |
7843               Private_Kind     |
7844               Task_Kind        |
7845               Protected_Kind   =>
7846             Copy_Node (Priv, Full);
7847
7848             Set_Has_Discriminants  (Full, Has_Discriminants (Full_Base));
7849             Set_First_Entity       (Full, First_Entity (Full_Base));
7850             Set_Last_Entity        (Full, Last_Entity (Full_Base));
7851
7852          when others =>
7853             Copy_Node (Full_Base, Full);
7854             Set_Chars          (Full, Chars (Priv));
7855             Conditional_Delay  (Full, Priv);
7856             Set_Sloc           (Full, Sloc (Priv));
7857       end case;
7858
7859       Set_Next_Entity (Full, Save_Next_Entity);
7860       Set_Homonym     (Full, Save_Homonym);
7861       Set_Associated_Node_For_Itype (Full, Related_Nod);
7862
7863       --  Set common attributes for all subtypes
7864
7865       Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
7866
7867       --  The Etype of the full view is inconsistent. Gigi needs to see the
7868       --  structural full view,  which is what the current scheme gives:
7869       --  the Etype of the full view is the etype of the full base. However,
7870       --  if the full base is a derived type, the full view then looks like
7871       --  a subtype of the parent, not a subtype of the full base. If instead
7872       --  we write:
7873
7874       --       Set_Etype (Full, Full_Base);
7875
7876       --  then we get inconsistencies in the front-end (confusion between
7877       --  views). Several outstanding bugs are related to this ???
7878
7879       Set_Is_First_Subtype (Full, False);
7880       Set_Scope            (Full, Scope (Priv));
7881       Set_Size_Info        (Full, Full_Base);
7882       Set_RM_Size          (Full, RM_Size (Full_Base));
7883       Set_Is_Itype         (Full);
7884
7885       --  A subtype of a private-type-without-discriminants, whose full-view
7886       --  has discriminants with default expressions, is not constrained!
7887
7888       if not Has_Discriminants (Priv) then
7889          Set_Is_Constrained (Full, Is_Constrained (Full_Base));
7890
7891          if Has_Discriminants (Full_Base) then
7892             Set_Discriminant_Constraint
7893               (Full, Discriminant_Constraint (Full_Base));
7894
7895             --  The partial view may have been indefinite, the full view
7896             --  might not be.
7897
7898             Set_Has_Unknown_Discriminants
7899               (Full, Has_Unknown_Discriminants (Full_Base));
7900          end if;
7901       end if;
7902
7903       Set_First_Rep_Item     (Full, First_Rep_Item (Full_Base));
7904       Set_Depends_On_Private (Full, Has_Private_Component (Full));
7905
7906       --  Freeze the private subtype entity if its parent is delayed, and not
7907       --  already frozen. We skip this processing if the type is an anonymous
7908       --  subtype of a record component, or is the corresponding record of a
7909       --  protected type, since ???
7910
7911       if not Is_Type (Scope (Full)) then
7912          Set_Has_Delayed_Freeze (Full,
7913            Has_Delayed_Freeze (Full_Base)
7914              and then (not Is_Frozen (Full_Base)));
7915       end if;
7916
7917       Set_Freeze_Node (Full, Empty);
7918       Set_Is_Frozen (Full, False);
7919       Set_Full_View (Priv, Full);
7920
7921       if Has_Discriminants (Full) then
7922          Set_Stored_Constraint_From_Discriminant_Constraint (Full);
7923          Set_Stored_Constraint (Priv, Stored_Constraint (Full));
7924
7925          if Has_Unknown_Discriminants (Full) then
7926             Set_Discriminant_Constraint (Full, No_Elist);
7927          end if;
7928       end if;
7929
7930       if Ekind (Full_Base) = E_Record_Type
7931         and then Has_Discriminants (Full_Base)
7932         and then Has_Discriminants (Priv) -- might not, if errors
7933         and then not Has_Unknown_Discriminants (Priv)
7934         and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
7935       then
7936          Create_Constrained_Components
7937            (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
7938
7939       --  If the full base is itself derived from private, build a congruent
7940       --  subtype of its underlying type, for use by the back end. For a
7941       --  constrained record component, the declaration cannot be placed on
7942       --  the component list, but it must nevertheless be built an analyzed, to
7943       --  supply enough information for Gigi to compute the size of component.
7944
7945       elsif Ekind (Full_Base) in Private_Kind
7946         and then Is_Derived_Type (Full_Base)
7947         and then Has_Discriminants (Full_Base)
7948         and then (Ekind (Current_Scope) /= E_Record_Subtype)
7949       then
7950          if not Is_Itype (Priv)
7951            and then
7952              Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
7953          then
7954             Build_Underlying_Full_View
7955               (Parent (Priv), Full, Etype (Full_Base));
7956
7957          elsif Nkind (Related_Nod) = N_Component_Declaration then
7958             Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
7959          end if;
7960
7961       elsif Is_Record_Type (Full_Base) then
7962
7963          --  Show Full is simply a renaming of Full_Base
7964
7965          Set_Cloned_Subtype (Full, Full_Base);
7966       end if;
7967
7968       --  It is unsafe to share to bounds of a scalar type, because the Itype
7969       --  is elaborated on demand, and if a bound is non-static then different
7970       --  orders of elaboration in different units will lead to different
7971       --  external symbols.
7972
7973       if Is_Scalar_Type (Full_Base) then
7974          Set_Scalar_Range (Full,
7975            Make_Range (Sloc (Related_Nod),
7976              Low_Bound  =>
7977                Duplicate_Subexpr_No_Checks (Type_Low_Bound  (Full_Base)),
7978              High_Bound =>
7979                Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
7980
7981          --  This completion inherits the bounds of the full parent, but if
7982          --  the parent is an unconstrained floating point type, so is the
7983          --  completion.
7984
7985          if Is_Floating_Point_Type (Full_Base) then
7986             Set_Includes_Infinities
7987              (Scalar_Range (Full), Has_Infinities (Full_Base));
7988          end if;
7989       end if;
7990
7991       --  ??? It seems that a lot of fields are missing that should be copied
7992       --  from Full_Base to Full. Here are some that are introduced in a
7993       --  non-disruptive way but a cleanup is necessary.
7994
7995       if Is_Tagged_Type (Full_Base) then
7996          Set_Is_Tagged_Type (Full);
7997          Set_Primitive_Operations (Full, Primitive_Operations (Full_Base));
7998          Set_Class_Wide_Type      (Full, Class_Wide_Type (Full_Base));
7999
8000       --  If this is a subtype of a protected or task type, constrain its
8001       --  corresponding record, unless this is a subtype without constraints,
8002       --  i.e. a simple renaming as with an actual subtype in an instance.
8003
8004       elsif Is_Concurrent_Type (Full_Base) then
8005          if Has_Discriminants (Full)
8006            and then Present (Corresponding_Record_Type (Full_Base))
8007            and then
8008              not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
8009          then
8010             Set_Corresponding_Record_Type (Full,
8011               Constrain_Corresponding_Record
8012                 (Full, Corresponding_Record_Type (Full_Base),
8013                   Related_Nod, Full_Base));
8014
8015          else
8016             Set_Corresponding_Record_Type (Full,
8017               Corresponding_Record_Type (Full_Base));
8018          end if;
8019       end if;
8020    end Complete_Private_Subtype;
8021
8022    ----------------------------
8023    -- Constant_Redeclaration --
8024    ----------------------------
8025
8026    procedure Constant_Redeclaration
8027      (Id : Entity_Id;
8028       N  : Node_Id;
8029       T  : out Entity_Id)
8030    is
8031       Prev    : constant Entity_Id := Current_Entity_In_Scope (Id);
8032       Obj_Def : constant Node_Id := Object_Definition (N);
8033       New_T   : Entity_Id;
8034
8035       procedure Check_Possible_Deferred_Completion
8036         (Prev_Id      : Entity_Id;
8037          Prev_Obj_Def : Node_Id;
8038          Curr_Obj_Def : Node_Id);
8039       --  Determine whether the two object definitions describe the partial
8040       --  and the full view of a constrained deferred constant. Generate
8041       --  a subtype for the full view and verify that it statically matches
8042       --  the subtype of the partial view.
8043
8044       procedure Check_Recursive_Declaration (Typ : Entity_Id);
8045       --  If deferred constant is an access type initialized with an allocator,
8046       --  check whether there is an illegal recursion in the definition,
8047       --  through a default value of some record subcomponent. This is normally
8048       --  detected when generating init procs, but requires this additional
8049       --  mechanism when expansion is disabled.
8050
8051       ----------------------------------------
8052       -- Check_Possible_Deferred_Completion --
8053       ----------------------------------------
8054
8055       procedure Check_Possible_Deferred_Completion
8056         (Prev_Id      : Entity_Id;
8057          Prev_Obj_Def : Node_Id;
8058          Curr_Obj_Def : Node_Id)
8059       is
8060       begin
8061          if Nkind (Prev_Obj_Def) = N_Subtype_Indication
8062            and then Present (Constraint (Prev_Obj_Def))
8063            and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
8064            and then Present (Constraint (Curr_Obj_Def))
8065          then
8066             declare
8067                Loc    : constant Source_Ptr := Sloc (N);
8068                Def_Id : constant Entity_Id :=
8069                           Make_Defining_Identifier (Loc,
8070                             New_Internal_Name ('S'));
8071                Decl   : constant Node_Id :=
8072                           Make_Subtype_Declaration (Loc,
8073                             Defining_Identifier =>
8074                               Def_Id,
8075                             Subtype_Indication =>
8076                               Relocate_Node (Curr_Obj_Def));
8077
8078             begin
8079                Insert_Before_And_Analyze (N, Decl);
8080                Set_Etype (Id, Def_Id);
8081
8082                if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
8083                   Error_Msg_Sloc := Sloc (Prev_Id);
8084                   Error_Msg_N ("subtype does not statically match deferred " &
8085                                "declaration#", N);
8086                end if;
8087             end;
8088          end if;
8089       end Check_Possible_Deferred_Completion;
8090
8091       ---------------------------------
8092       -- Check_Recursive_Declaration --
8093       ---------------------------------
8094
8095       procedure Check_Recursive_Declaration (Typ : Entity_Id) is
8096          Comp : Entity_Id;
8097
8098       begin
8099          if Is_Record_Type (Typ) then
8100             Comp := First_Component (Typ);
8101             while Present (Comp) loop
8102                if Comes_From_Source (Comp) then
8103                   if Present (Expression (Parent (Comp)))
8104                     and then Is_Entity_Name (Expression (Parent (Comp)))
8105                     and then Entity (Expression (Parent (Comp))) = Prev
8106                   then
8107                      Error_Msg_Sloc := Sloc (Parent (Comp));
8108                      Error_Msg_NE
8109                        ("illegal circularity with declaration for&#",
8110                          N, Comp);
8111                      return;
8112
8113                   elsif Is_Record_Type (Etype (Comp)) then
8114                      Check_Recursive_Declaration (Etype (Comp));
8115                   end if;
8116                end if;
8117
8118                Next_Component (Comp);
8119             end loop;
8120          end if;
8121       end Check_Recursive_Declaration;
8122
8123    --  Start of processing for Constant_Redeclaration
8124
8125    begin
8126       if Nkind (Parent (Prev)) = N_Object_Declaration then
8127          if Nkind (Object_Definition
8128                      (Parent (Prev))) = N_Subtype_Indication
8129          then
8130             --  Find type of new declaration. The constraints of the two
8131             --  views must match statically, but there is no point in
8132             --  creating an itype for the full view.
8133
8134             if Nkind (Obj_Def) = N_Subtype_Indication then
8135                Find_Type (Subtype_Mark (Obj_Def));
8136                New_T := Entity (Subtype_Mark (Obj_Def));
8137
8138             else
8139                Find_Type (Obj_Def);
8140                New_T := Entity (Obj_Def);
8141             end if;
8142
8143             T := Etype (Prev);
8144
8145          else
8146             --  The full view may impose a constraint, even if the partial
8147             --  view does not, so construct the subtype.
8148
8149             New_T := Find_Type_Of_Object (Obj_Def, N);
8150             T     := New_T;
8151          end if;
8152
8153       else
8154          --  Current declaration is illegal, diagnosed below in Enter_Name
8155
8156          T := Empty;
8157          New_T := Any_Type;
8158       end if;
8159
8160       --  If previous full declaration exists, or if a homograph is present,
8161       --  let Enter_Name handle it, either with an error, or with the removal
8162       --  of an overridden implicit subprogram.
8163
8164       if Ekind (Prev) /= E_Constant
8165         or else Present (Expression (Parent (Prev)))
8166         or else Present (Full_View (Prev))
8167       then
8168          Enter_Name (Id);
8169
8170       --  Verify that types of both declarations match, or else that both types
8171       --  are anonymous access types whose designated subtypes statically match
8172       --  (as allowed in Ada 2005 by AI-385).
8173
8174       elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
8175         and then
8176           (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
8177              or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
8178              or else not Subtypes_Statically_Match
8179                            (Designated_Type (Etype (Prev)),
8180                             Designated_Type (Etype (New_T))))
8181       then
8182          Error_Msg_Sloc := Sloc (Prev);
8183          Error_Msg_N ("type does not match declaration#", N);
8184          Set_Full_View (Prev, Id);
8185          Set_Etype (Id, Any_Type);
8186
8187       --  If so, process the full constant declaration
8188
8189       else
8190          --  RM 7.4 (6): If the subtype defined by the subtype_indication in
8191          --  the deferred declaration is constrained, then the subtype defined
8192          --  by the subtype_indication in the full declaration shall match it
8193          --  statically.
8194
8195          Check_Possible_Deferred_Completion
8196            (Prev_Id      => Prev,
8197             Prev_Obj_Def => Object_Definition (Parent (Prev)),
8198             Curr_Obj_Def => Obj_Def);
8199
8200          Set_Full_View (Prev, Id);
8201          Set_Is_Public (Id, Is_Public (Prev));
8202          Set_Is_Internal (Id);
8203          Append_Entity (Id, Current_Scope);
8204
8205          --  Check ALIASED present if present before (RM 7.4(7))
8206
8207          if Is_Aliased (Prev)
8208            and then not Aliased_Present (N)
8209          then
8210             Error_Msg_Sloc := Sloc (Prev);
8211             Error_Msg_N ("ALIASED required (see declaration#)", N);
8212          end if;
8213
8214          --  Check that placement is in private part and that the incomplete
8215          --  declaration appeared in the visible part.
8216
8217          if Ekind (Current_Scope) = E_Package
8218            and then not In_Private_Part (Current_Scope)
8219          then
8220             Error_Msg_Sloc := Sloc (Prev);
8221             Error_Msg_N ("full constant for declaration#"
8222                          & " must be in private part", N);
8223
8224          elsif Ekind (Current_Scope) = E_Package
8225            and then List_Containing (Parent (Prev))
8226            /= Visible_Declarations
8227              (Specification (Unit_Declaration_Node (Current_Scope)))
8228          then
8229             Error_Msg_N
8230               ("deferred constant must be declared in visible part",
8231                  Parent (Prev));
8232          end if;
8233
8234          if Is_Access_Type (T)
8235            and then Nkind (Expression (N)) = N_Allocator
8236          then
8237             Check_Recursive_Declaration (Designated_Type (T));
8238          end if;
8239       end if;
8240    end Constant_Redeclaration;
8241
8242    ----------------------
8243    -- Constrain_Access --
8244    ----------------------
8245
8246    procedure Constrain_Access
8247      (Def_Id      : in out Entity_Id;
8248       S           : Node_Id;
8249       Related_Nod : Node_Id)
8250    is
8251       T             : constant Entity_Id := Entity (Subtype_Mark (S));
8252       Desig_Type    : constant Entity_Id := Designated_Type (T);
8253       Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
8254       Constraint_OK : Boolean := True;
8255
8256       function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
8257       --  Simple predicate to test for defaulted discriminants
8258       --  Shouldn't this be in sem_util???
8259
8260       ---------------------------------
8261       -- Has_Defaulted_Discriminants --
8262       ---------------------------------
8263
8264       function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
8265       begin
8266          return Has_Discriminants (Typ)
8267           and then Present (First_Discriminant (Typ))
8268           and then Present
8269             (Discriminant_Default_Value (First_Discriminant (Typ)));
8270       end Has_Defaulted_Discriminants;
8271
8272    --  Start of processing for Constrain_Access
8273
8274    begin
8275       if Is_Array_Type (Desig_Type) then
8276          Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
8277
8278       elsif (Is_Record_Type (Desig_Type)
8279               or else Is_Incomplete_Or_Private_Type (Desig_Type))
8280         and then not Is_Constrained (Desig_Type)
8281       then
8282          --  ??? The following code is a temporary kludge to ignore a
8283          --  discriminant constraint on access type if it is constraining
8284          --  the current record. Avoid creating the implicit subtype of the
8285          --  record we are currently compiling since right now, we cannot
8286          --  handle these. For now, just return the access type itself.
8287
8288          if Desig_Type = Current_Scope
8289            and then No (Def_Id)
8290          then
8291             Set_Ekind (Desig_Subtype, E_Record_Subtype);
8292             Def_Id := Entity (Subtype_Mark (S));
8293
8294             --  This call added to ensure that the constraint is analyzed
8295             --  (needed for a B test). Note that we still return early from
8296             --  this procedure to avoid recursive processing. ???
8297
8298             Constrain_Discriminated_Type
8299               (Desig_Subtype, S, Related_Nod, For_Access => True);
8300             return;
8301          end if;
8302
8303          if Ekind (T) = E_General_Access_Type
8304            and then Has_Private_Declaration (Desig_Type)
8305            and then In_Open_Scopes (Scope (Desig_Type))
8306          then
8307             --  Enforce rule that the constraint is illegal if there is
8308             --  an unconstrained view of the designated type. This means
8309             --  that the partial view (either a private type declaration or
8310             --  a derivation from a private type) has no discriminants.
8311             --  (Defect Report 8652/0008, Technical Corrigendum 1, checked
8312             --  by ACATS B371001).
8313             --  Rule updated for Ada 2005: the private type is said to have
8314             --  a constrained partial view, given that objects of the type
8315             --  can be declared.
8316
8317             declare
8318                Pack  : constant Node_Id :=
8319                          Unit_Declaration_Node (Scope (Desig_Type));
8320                Decls : List_Id;
8321                Decl  : Node_Id;
8322
8323             begin
8324                if Nkind (Pack) = N_Package_Declaration then
8325                   Decls := Visible_Declarations (Specification (Pack));
8326                   Decl := First (Decls);
8327                   while Present (Decl) loop
8328                      if (Nkind (Decl) = N_Private_Type_Declaration
8329                           and then
8330                             Chars (Defining_Identifier (Decl)) =
8331                                                      Chars (Desig_Type))
8332
8333                        or else
8334                         (Nkind (Decl) = N_Full_Type_Declaration
8335                           and then
8336                             Chars (Defining_Identifier (Decl)) =
8337                                                      Chars (Desig_Type)
8338                           and then Is_Derived_Type (Desig_Type)
8339                           and then
8340                             Has_Private_Declaration (Etype (Desig_Type)))
8341                      then
8342                         if No (Discriminant_Specifications (Decl)) then
8343                            Error_Msg_N
8344                             ("cannot constrain general access type if " &
8345                                "designated type has constrained partial view",
8346                                 S);
8347                         end if;
8348
8349                         exit;
8350                      end if;
8351
8352                      Next (Decl);
8353                   end loop;
8354                end if;
8355             end;
8356          end if;
8357
8358          Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
8359            For_Access => True);
8360
8361       elsif (Is_Task_Type (Desig_Type)
8362               or else Is_Protected_Type (Desig_Type))
8363         and then not Is_Constrained (Desig_Type)
8364       then
8365          Constrain_Concurrent
8366            (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
8367
8368       else
8369          Error_Msg_N ("invalid constraint on access type", S);
8370          Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
8371          Constraint_OK := False;
8372       end if;
8373
8374       if No (Def_Id) then
8375          Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
8376       else
8377          Set_Ekind (Def_Id, E_Access_Subtype);
8378       end if;
8379
8380       if Constraint_OK then
8381          Set_Etype (Def_Id, Base_Type (T));
8382
8383          if Is_Private_Type (Desig_Type) then
8384             Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
8385          end if;
8386       else
8387          Set_Etype (Def_Id, Any_Type);
8388       end if;
8389
8390       Set_Size_Info                (Def_Id, T);
8391       Set_Is_Constrained           (Def_Id, Constraint_OK);
8392       Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
8393       Set_Depends_On_Private       (Def_Id, Has_Private_Component (Def_Id));
8394       Set_Is_Access_Constant       (Def_Id, Is_Access_Constant (T));
8395
8396       Conditional_Delay (Def_Id, T);
8397
8398       --  AI-363 : Subtypes of general access types whose designated types have
8399       --  default discriminants are disallowed. In instances, the rule has to
8400       --  be checked against the actual, of which T is the subtype. In a
8401       --  generic body, the rule is checked assuming that the actual type has
8402       --  defaulted discriminants.
8403
8404       if Ada_Version >=  Ada_05 then
8405          if Ekind (Base_Type (T)) = E_General_Access_Type
8406            and then Has_Defaulted_Discriminants (Desig_Type)
8407          then
8408             Error_Msg_N
8409               ("access subype of general access type not allowed", S);
8410             Error_Msg_N ("\discriminants have defaults", S);
8411
8412          elsif Is_Access_Type (T)
8413            and then Is_Generic_Type (Desig_Type)
8414            and then Has_Discriminants (Desig_Type)
8415            and then In_Package_Body (Current_Scope)
8416          then
8417             Error_Msg_N ("access subtype not allowed in generic body", S);
8418             Error_Msg_N
8419               ("\designated type is a discriminated formal", S);
8420          end if;
8421       end if;
8422    end Constrain_Access;
8423
8424    ---------------------
8425    -- Constrain_Array --
8426    ---------------------
8427
8428    procedure Constrain_Array
8429      (Def_Id      : in out Entity_Id;
8430       SI          : Node_Id;
8431       Related_Nod : Node_Id;
8432       Related_Id  : Entity_Id;
8433       Suffix      : Character)
8434    is
8435       C                     : constant Node_Id := Constraint (SI);
8436       Number_Of_Constraints : Nat := 0;
8437       Index                 : Node_Id;
8438       S, T                  : Entity_Id;
8439       Constraint_OK         : Boolean := True;
8440
8441    begin
8442       T := Entity (Subtype_Mark (SI));
8443
8444       if Ekind (T) in Access_Kind then
8445          T := Designated_Type (T);
8446       end if;
8447
8448       --  If an index constraint follows a subtype mark in a subtype indication
8449       --  then the type or subtype denoted by the subtype mark must not already
8450       --  impose an index constraint. The subtype mark must denote either an
8451       --  unconstrained array type or an access type whose designated type
8452       --  is such an array type... (RM 3.6.1)
8453
8454       if Is_Constrained (T) then
8455          Error_Msg_N
8456            ("array type is already constrained", Subtype_Mark (SI));
8457          Constraint_OK := False;
8458
8459       else
8460          S := First (Constraints (C));
8461          while Present (S) loop
8462             Number_Of_Constraints := Number_Of_Constraints + 1;
8463             Next (S);
8464          end loop;
8465
8466          --  In either case, the index constraint must provide a discrete
8467          --  range for each index of the array type and the type of each
8468          --  discrete range must be the same as that of the corresponding
8469          --  index. (RM 3.6.1)
8470
8471          if Number_Of_Constraints /= Number_Dimensions (T) then
8472             Error_Msg_NE ("incorrect number of index constraints for }", C, T);
8473             Constraint_OK := False;
8474
8475          else
8476             S := First (Constraints (C));
8477             Index := First_Index (T);
8478             Analyze (Index);
8479
8480             --  Apply constraints to each index type
8481
8482             for J in 1 .. Number_Of_Constraints loop
8483                Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
8484                Next (Index);
8485                Next (S);
8486             end loop;
8487
8488          end if;
8489       end if;
8490
8491       if No (Def_Id) then
8492          Def_Id :=
8493            Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
8494          Set_Parent (Def_Id, Related_Nod);
8495
8496       else
8497          Set_Ekind (Def_Id, E_Array_Subtype);
8498       end if;
8499
8500       Set_Size_Info      (Def_Id,                (T));
8501       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8502       Set_Etype          (Def_Id, Base_Type      (T));
8503
8504       if Constraint_OK then
8505          Set_First_Index (Def_Id, First (Constraints (C)));
8506       else
8507          Set_First_Index (Def_Id, First_Index (T));
8508       end if;
8509
8510       Set_Is_Constrained     (Def_Id, True);
8511       Set_Is_Aliased         (Def_Id, Is_Aliased (T));
8512       Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
8513
8514       Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
8515       Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
8516
8517       --  Build a freeze node if parent still needs one. Also, make sure
8518       --  that the Depends_On_Private status is set because the subtype
8519       --  will need reprocessing at the time the base type does.
8520       --  and also that a conditional delay is set.
8521
8522       Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
8523       Conditional_Delay (Def_Id, T);
8524
8525    end Constrain_Array;
8526
8527    ------------------------------
8528    -- Constrain_Component_Type --
8529    ------------------------------
8530
8531    function Constrain_Component_Type
8532      (Comp            : Entity_Id;
8533       Constrained_Typ : Entity_Id;
8534       Related_Node    : Node_Id;
8535       Typ             : Entity_Id;
8536       Constraints     : Elist_Id) return Entity_Id
8537    is
8538       Loc         : constant Source_Ptr := Sloc (Constrained_Typ);
8539       Compon_Type : constant Entity_Id := Etype (Comp);
8540
8541       function Build_Constrained_Array_Type
8542         (Old_Type : Entity_Id) return Entity_Id;
8543       --  If Old_Type is an array type, one of whose indices is constrained
8544       --  by a discriminant, build an Itype whose constraint replaces the
8545       --  discriminant with its value in the constraint.
8546
8547       function Build_Constrained_Discriminated_Type
8548         (Old_Type : Entity_Id) return Entity_Id;
8549       --  Ditto for record components
8550
8551       function Build_Constrained_Access_Type
8552         (Old_Type : Entity_Id) return Entity_Id;
8553       --  Ditto for access types. Makes use of previous two functions, to
8554       --  constrain designated type.
8555
8556       function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
8557       --  T is an array or discriminated type, C is a list of constraints
8558       --  that apply to T. This routine builds the constrained subtype.
8559
8560       function Is_Discriminant (Expr : Node_Id) return Boolean;
8561       --  Returns True if Expr is a discriminant
8562
8563       function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
8564       --  Find the value of discriminant Discrim in Constraint
8565
8566       -----------------------------------
8567       -- Build_Constrained_Access_Type --
8568       -----------------------------------
8569
8570       function Build_Constrained_Access_Type
8571         (Old_Type : Entity_Id) return Entity_Id
8572       is
8573          Desig_Type    : constant Entity_Id := Designated_Type (Old_Type);
8574          Itype         : Entity_Id;
8575          Desig_Subtype : Entity_Id;
8576          Scop          : Entity_Id;
8577
8578       begin
8579          --  if the original access type was not embedded in the enclosing
8580          --  type definition, there is no need to produce a new access
8581          --  subtype. In fact every access type with an explicit constraint
8582          --  generates an itype whose scope is the enclosing record.
8583
8584          if not Is_Type (Scope (Old_Type)) then
8585             return Old_Type;
8586
8587          elsif Is_Array_Type (Desig_Type) then
8588             Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
8589
8590          elsif Has_Discriminants (Desig_Type) then
8591
8592             --  This may be an access type to an enclosing record type for
8593             --  which we are constructing the constrained components. Return
8594             --  the enclosing record subtype. This is not always correct,
8595             --  but avoids infinite recursion. ???
8596
8597             Desig_Subtype := Any_Type;
8598
8599             for J in reverse 0 .. Scope_Stack.Last loop
8600                Scop := Scope_Stack.Table (J).Entity;
8601
8602                if Is_Type (Scop)
8603                  and then Base_Type (Scop) = Base_Type (Desig_Type)
8604                then
8605                   Desig_Subtype := Scop;
8606                end if;
8607
8608                exit when not Is_Type (Scop);
8609             end loop;
8610
8611             if Desig_Subtype = Any_Type then
8612                Desig_Subtype :=
8613                  Build_Constrained_Discriminated_Type (Desig_Type);
8614             end if;
8615
8616          else
8617             return Old_Type;
8618          end if;
8619
8620          if Desig_Subtype /= Desig_Type then
8621
8622             --  The Related_Node better be here or else we won't be able
8623             --  to attach new itypes to a node in the tree.
8624
8625             pragma Assert (Present (Related_Node));
8626
8627             Itype := Create_Itype (E_Access_Subtype, Related_Node);
8628
8629             Set_Etype                    (Itype, Base_Type      (Old_Type));
8630             Set_Size_Info                (Itype,                (Old_Type));
8631             Set_Directly_Designated_Type (Itype, Desig_Subtype);
8632             Set_Depends_On_Private       (Itype, Has_Private_Component
8633                                                                 (Old_Type));
8634             Set_Is_Access_Constant       (Itype, Is_Access_Constant
8635                                                                 (Old_Type));
8636
8637             --  The new itype needs freezing when it depends on a not frozen
8638             --  type and the enclosing subtype needs freezing.
8639
8640             if Has_Delayed_Freeze (Constrained_Typ)
8641               and then not Is_Frozen (Constrained_Typ)
8642             then
8643                Conditional_Delay (Itype, Base_Type (Old_Type));
8644             end if;
8645
8646             return Itype;
8647
8648          else
8649             return Old_Type;
8650          end if;
8651       end Build_Constrained_Access_Type;
8652
8653       ----------------------------------
8654       -- Build_Constrained_Array_Type --
8655       ----------------------------------
8656
8657       function Build_Constrained_Array_Type
8658         (Old_Type : Entity_Id) return Entity_Id
8659       is
8660          Lo_Expr     : Node_Id;
8661          Hi_Expr     : Node_Id;
8662          Old_Index   : Node_Id;
8663          Range_Node  : Node_Id;
8664          Constr_List : List_Id;
8665
8666          Need_To_Create_Itype : Boolean := False;
8667
8668       begin
8669          Old_Index := First_Index (Old_Type);
8670          while Present (Old_Index) loop
8671             Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
8672
8673             if Is_Discriminant (Lo_Expr)
8674               or else Is_Discriminant (Hi_Expr)
8675             then
8676                Need_To_Create_Itype := True;
8677             end if;
8678
8679             Next_Index (Old_Index);
8680          end loop;
8681
8682          if Need_To_Create_Itype then
8683             Constr_List := New_List;
8684
8685             Old_Index := First_Index (Old_Type);
8686             while Present (Old_Index) loop
8687                Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
8688
8689                if Is_Discriminant (Lo_Expr) then
8690                   Lo_Expr := Get_Discr_Value (Lo_Expr);
8691                end if;
8692
8693                if Is_Discriminant (Hi_Expr) then
8694                   Hi_Expr := Get_Discr_Value (Hi_Expr);
8695                end if;
8696
8697                Range_Node :=
8698                  Make_Range
8699                    (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
8700
8701                Append (Range_Node, To => Constr_List);
8702
8703                Next_Index (Old_Index);
8704             end loop;
8705
8706             return Build_Subtype (Old_Type, Constr_List);
8707
8708          else
8709             return Old_Type;
8710          end if;
8711       end Build_Constrained_Array_Type;
8712
8713       ------------------------------------------
8714       -- Build_Constrained_Discriminated_Type --
8715       ------------------------------------------
8716
8717       function Build_Constrained_Discriminated_Type
8718         (Old_Type : Entity_Id) return Entity_Id
8719       is
8720          Expr           : Node_Id;
8721          Constr_List    : List_Id;
8722          Old_Constraint : Elmt_Id;
8723
8724          Need_To_Create_Itype : Boolean := False;
8725
8726       begin
8727          Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
8728          while Present (Old_Constraint) loop
8729             Expr := Node (Old_Constraint);
8730
8731             if Is_Discriminant (Expr) then
8732                Need_To_Create_Itype := True;
8733             end if;
8734
8735             Next_Elmt (Old_Constraint);
8736          end loop;
8737
8738          if Need_To_Create_Itype then
8739             Constr_List := New_List;
8740
8741             Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
8742             while Present (Old_Constraint) loop
8743                Expr := Node (Old_Constraint);
8744
8745                if Is_Discriminant (Expr) then
8746                   Expr := Get_Discr_Value (Expr);
8747                end if;
8748
8749                Append (New_Copy_Tree (Expr), To => Constr_List);
8750
8751                Next_Elmt (Old_Constraint);
8752             end loop;
8753
8754             return Build_Subtype (Old_Type, Constr_List);
8755
8756          else
8757             return Old_Type;
8758          end if;
8759       end Build_Constrained_Discriminated_Type;
8760
8761       -------------------
8762       -- Build_Subtype --
8763       -------------------
8764
8765       function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
8766          Indic       : Node_Id;
8767          Subtyp_Decl : Node_Id;
8768          Def_Id      : Entity_Id;
8769          Btyp        : Entity_Id := Base_Type (T);
8770
8771       begin
8772          --  The Related_Node better be here or else we won't be able to
8773          --  attach new itypes to a node in the tree.
8774
8775          pragma Assert (Present (Related_Node));
8776
8777          --  If the view of the component's type is incomplete or private
8778          --  with unknown discriminants, then the constraint must be applied
8779          --  to the full type.
8780
8781          if Has_Unknown_Discriminants (Btyp)
8782            and then Present (Underlying_Type (Btyp))
8783          then
8784             Btyp := Underlying_Type (Btyp);
8785          end if;
8786
8787          Indic :=
8788            Make_Subtype_Indication (Loc,
8789              Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
8790              Constraint   => Make_Index_Or_Discriminant_Constraint (Loc, C));
8791
8792          Def_Id := Create_Itype (Ekind (T), Related_Node);
8793
8794          Subtyp_Decl :=
8795            Make_Subtype_Declaration (Loc,
8796              Defining_Identifier => Def_Id,
8797              Subtype_Indication  => Indic);
8798
8799          Set_Parent (Subtyp_Decl, Parent (Related_Node));
8800
8801          --  Itypes must be analyzed with checks off (see package Itypes)
8802
8803          Analyze (Subtyp_Decl, Suppress => All_Checks);
8804
8805          return Def_Id;
8806       end Build_Subtype;
8807
8808       ---------------------
8809       -- Get_Discr_Value --
8810       ---------------------
8811
8812       function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
8813          D : Entity_Id;
8814          E : Elmt_Id;
8815          G : Elmt_Id;
8816
8817       begin
8818          --  The discriminant may be declared for the type, in which case we
8819          --  find it by iterating over the list of discriminants. If the
8820          --  discriminant is inherited from a parent type, it appears as the
8821          --  corresponding discriminant of the current type. This will be the
8822          --  case when constraining an inherited component whose constraint is
8823          --  given by a discriminant of the parent.
8824
8825          D := First_Discriminant (Typ);
8826          E := First_Elmt (Constraints);
8827
8828          while Present (D) loop
8829             if D = Entity (Discrim)
8830               or else D = CR_Discriminant (Entity (Discrim))
8831               or else Corresponding_Discriminant (D) = Entity (Discrim)
8832             then
8833                return Node (E);
8834             end if;
8835
8836             Next_Discriminant (D);
8837             Next_Elmt (E);
8838          end loop;
8839
8840          --  The corresponding_Discriminant mechanism is incomplete, because
8841          --  the correspondence between new and old discriminants is not one
8842          --  to one: one new discriminant can constrain several old ones. In
8843          --  that case, scan sequentially the stored_constraint, the list of
8844          --  discriminants of the parents, and the constraints.
8845
8846          if Is_Derived_Type (Typ)
8847            and then Present (Stored_Constraint (Typ))
8848            and then Scope (Entity (Discrim)) = Etype (Typ)
8849          then
8850             D := First_Discriminant (Etype (Typ));
8851             E := First_Elmt (Constraints);
8852             G := First_Elmt (Stored_Constraint (Typ));
8853             while Present (D) loop
8854                if D = Entity (Discrim) then
8855                   return Node (E);
8856                end if;
8857
8858                Next_Discriminant (D);
8859                Next_Elmt (E);
8860                Next_Elmt (G);
8861             end loop;
8862          end if;
8863
8864          --  Something is wrong if we did not find the value
8865
8866          raise Program_Error;
8867       end Get_Discr_Value;
8868
8869       ---------------------
8870       -- Is_Discriminant --
8871       ---------------------
8872
8873       function Is_Discriminant (Expr : Node_Id) return Boolean is
8874          Discrim_Scope : Entity_Id;
8875
8876       begin
8877          if Denotes_Discriminant (Expr) then
8878             Discrim_Scope := Scope (Entity (Expr));
8879
8880             --  Either we have a reference to one of Typ's discriminants,
8881
8882             pragma Assert (Discrim_Scope = Typ
8883
8884                --  or to the discriminants of the parent type, in the case
8885                --  of a derivation of a tagged type with variants.
8886
8887                or else Discrim_Scope = Etype (Typ)
8888                or else Full_View (Discrim_Scope) = Etype (Typ)
8889
8890                --  or same as above for the case where the discriminants
8891                --  were declared in Typ's private view.
8892
8893                or else (Is_Private_Type (Discrim_Scope)
8894                         and then Chars (Discrim_Scope) = Chars (Typ))
8895
8896                --  or else we are deriving from the full view and the
8897                --  discriminant is declared in the private entity.
8898
8899                or else (Is_Private_Type (Typ)
8900                         and then Chars (Discrim_Scope) = Chars (Typ))
8901
8902                --  Or we are constrained the corresponding record of a
8903                --  synchronized type that completes a private declaration.
8904
8905                or else (Is_Concurrent_Record_Type (Typ)
8906                          and then
8907                            Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
8908
8909                --  or we have a class-wide type, in which case make sure the
8910                --  discriminant found belongs to the root type.
8911
8912                or else (Is_Class_Wide_Type (Typ)
8913                         and then Etype (Typ) = Discrim_Scope));
8914
8915             return True;
8916          end if;
8917
8918          --  In all other cases we have something wrong
8919
8920          return False;
8921       end Is_Discriminant;
8922
8923    --  Start of processing for Constrain_Component_Type
8924
8925    begin
8926       if Nkind (Parent (Comp)) = N_Component_Declaration
8927         and then Comes_From_Source (Parent (Comp))
8928         and then Comes_From_Source
8929           (Subtype_Indication (Component_Definition (Parent (Comp))))
8930         and then
8931           Is_Entity_Name
8932             (Subtype_Indication (Component_Definition (Parent (Comp))))
8933       then
8934          return Compon_Type;
8935
8936       elsif Is_Array_Type (Compon_Type) then
8937          return Build_Constrained_Array_Type (Compon_Type);
8938
8939       elsif Has_Discriminants (Compon_Type) then
8940          return Build_Constrained_Discriminated_Type (Compon_Type);
8941
8942       elsif Is_Access_Type (Compon_Type) then
8943          return Build_Constrained_Access_Type (Compon_Type);
8944
8945       else
8946          return Compon_Type;
8947       end if;
8948    end Constrain_Component_Type;
8949
8950    --------------------------
8951    -- Constrain_Concurrent --
8952    --------------------------
8953
8954    --  For concurrent types, the associated record value type carries the same
8955    --  discriminants, so when we constrain a concurrent type, we must constrain
8956    --  the corresponding record type as well.
8957
8958    procedure Constrain_Concurrent
8959      (Def_Id      : in out Entity_Id;
8960       SI          : Node_Id;
8961       Related_Nod : Node_Id;
8962       Related_Id  : Entity_Id;
8963       Suffix      : Character)
8964    is
8965       T_Ent : Entity_Id := Entity (Subtype_Mark (SI));
8966       T_Val : Entity_Id;
8967
8968    begin
8969       if Ekind (T_Ent) in Access_Kind then
8970          T_Ent := Designated_Type (T_Ent);
8971       end if;
8972
8973       T_Val := Corresponding_Record_Type (T_Ent);
8974
8975       if Present (T_Val) then
8976
8977          if No (Def_Id) then
8978             Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
8979          end if;
8980
8981          Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
8982
8983          Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
8984          Set_Corresponding_Record_Type (Def_Id,
8985            Constrain_Corresponding_Record
8986              (Def_Id, T_Val, Related_Nod, Related_Id));
8987
8988       else
8989          --  If there is no associated record, expansion is disabled and this
8990          --  is a generic context. Create a subtype in any case, so that
8991          --  semantic analysis can proceed.
8992
8993          if No (Def_Id) then
8994             Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
8995          end if;
8996
8997          Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
8998       end if;
8999    end Constrain_Concurrent;
9000
9001    ------------------------------------
9002    -- Constrain_Corresponding_Record --
9003    ------------------------------------
9004
9005    function Constrain_Corresponding_Record
9006      (Prot_Subt   : Entity_Id;
9007       Corr_Rec    : Entity_Id;
9008       Related_Nod : Node_Id;
9009       Related_Id  : Entity_Id) return Entity_Id
9010    is
9011       T_Sub : constant Entity_Id :=
9012                 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
9013
9014    begin
9015       Set_Etype             (T_Sub, Corr_Rec);
9016       Init_Size_Align       (T_Sub);
9017       Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
9018       Set_Is_Constrained    (T_Sub, True);
9019       Set_First_Entity      (T_Sub, First_Entity (Corr_Rec));
9020       Set_Last_Entity       (T_Sub, Last_Entity  (Corr_Rec));
9021
9022       --  As elsewhere, we do not want to create a freeze node for this itype
9023       --  if it is created for a constrained component of an enclosing record
9024       --  because references to outer discriminants will appear out of scope.
9025
9026       if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
9027          Conditional_Delay (T_Sub, Corr_Rec);
9028       else
9029          Set_Is_Frozen (T_Sub);
9030       end if;
9031
9032       if Has_Discriminants (Prot_Subt) then -- False only if errors.
9033          Set_Discriminant_Constraint
9034            (T_Sub, Discriminant_Constraint (Prot_Subt));
9035          Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
9036          Create_Constrained_Components
9037            (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
9038       end if;
9039
9040       Set_Depends_On_Private      (T_Sub, Has_Private_Component (T_Sub));
9041
9042       return T_Sub;
9043    end Constrain_Corresponding_Record;
9044
9045    -----------------------
9046    -- Constrain_Decimal --
9047    -----------------------
9048
9049    procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
9050       T           : constant Entity_Id  := Entity (Subtype_Mark (S));
9051       C           : constant Node_Id    := Constraint (S);
9052       Loc         : constant Source_Ptr := Sloc (C);
9053       Range_Expr  : Node_Id;
9054       Digits_Expr : Node_Id;
9055       Digits_Val  : Uint;
9056       Bound_Val   : Ureal;
9057
9058    begin
9059       Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
9060
9061       if Nkind (C) = N_Range_Constraint then
9062          Range_Expr := Range_Expression (C);
9063          Digits_Val := Digits_Value (T);
9064
9065       else
9066          pragma Assert (Nkind (C) = N_Digits_Constraint);
9067          Digits_Expr := Digits_Expression (C);
9068          Analyze_And_Resolve (Digits_Expr, Any_Integer);
9069
9070          Check_Digits_Expression (Digits_Expr);
9071          Digits_Val := Expr_Value (Digits_Expr);
9072
9073          if Digits_Val > Digits_Value (T) then
9074             Error_Msg_N
9075                ("digits expression is incompatible with subtype", C);
9076             Digits_Val := Digits_Value (T);
9077          end if;
9078
9079          if Present (Range_Constraint (C)) then
9080             Range_Expr := Range_Expression (Range_Constraint (C));
9081          else
9082             Range_Expr := Empty;
9083          end if;
9084       end if;
9085
9086       Set_Etype            (Def_Id, Base_Type        (T));
9087       Set_Size_Info        (Def_Id,                  (T));
9088       Set_First_Rep_Item   (Def_Id, First_Rep_Item   (T));
9089       Set_Delta_Value      (Def_Id, Delta_Value      (T));
9090       Set_Scale_Value      (Def_Id, Scale_Value      (T));
9091       Set_Small_Value      (Def_Id, Small_Value      (T));
9092       Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
9093       Set_Digits_Value     (Def_Id, Digits_Val);
9094
9095       --  Manufacture range from given digits value if no range present
9096
9097       if No (Range_Expr) then
9098          Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
9099          Range_Expr :=
9100            Make_Range (Loc,
9101              Low_Bound =>
9102                Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
9103              High_Bound =>
9104                Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
9105       end if;
9106
9107       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
9108       Set_Discrete_RM_Size (Def_Id);
9109
9110       --  Unconditionally delay the freeze, since we cannot set size
9111       --  information in all cases correctly until the freeze point.
9112
9113       Set_Has_Delayed_Freeze (Def_Id);
9114    end Constrain_Decimal;
9115
9116    ----------------------------------
9117    -- Constrain_Discriminated_Type --
9118    ----------------------------------
9119
9120    procedure Constrain_Discriminated_Type
9121      (Def_Id      : Entity_Id;
9122       S           : Node_Id;
9123       Related_Nod : Node_Id;
9124       For_Access  : Boolean := False)
9125    is
9126       E     : constant Entity_Id := Entity (Subtype_Mark (S));
9127       T     : Entity_Id;
9128       C     : Node_Id;
9129       Elist : Elist_Id := New_Elmt_List;
9130
9131       procedure Fixup_Bad_Constraint;
9132       --  This is called after finding a bad constraint, and after having
9133       --  posted an appropriate error message. The mission is to leave the
9134       --  entity T in as reasonable state as possible!
9135
9136       --------------------------
9137       -- Fixup_Bad_Constraint --
9138       --------------------------
9139
9140       procedure Fixup_Bad_Constraint is
9141       begin
9142          --  Set a reasonable Ekind for the entity. For an incomplete type,
9143          --  we can't do much, but for other types, we can set the proper
9144          --  corresponding subtype kind.
9145
9146          if Ekind (T) = E_Incomplete_Type then
9147             Set_Ekind (Def_Id, Ekind (T));
9148          else
9149             Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9150          end if;
9151
9152          Set_Etype (Def_Id, Any_Type);
9153          Set_Error_Posted (Def_Id);
9154       end Fixup_Bad_Constraint;
9155
9156    --  Start of processing for Constrain_Discriminated_Type
9157
9158    begin
9159       C := Constraint (S);
9160
9161       --  A discriminant constraint is only allowed in a subtype indication,
9162       --  after a subtype mark. This subtype mark must denote either a type
9163       --  with discriminants, or an access type whose designated type is a
9164       --  type with discriminants. A discriminant constraint specifies the
9165       --  values of these discriminants (RM 3.7.2(5)).
9166
9167       T := Base_Type (Entity (Subtype_Mark (S)));
9168
9169       if Ekind (T) in Access_Kind then
9170          T := Designated_Type (T);
9171       end if;
9172
9173       --  Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
9174       --  Avoid generating an error for access-to-incomplete subtypes.
9175
9176       if Ada_Version >= Ada_05
9177         and then Ekind (T) = E_Incomplete_Type
9178         and then Nkind (Parent (S)) = N_Subtype_Declaration
9179         and then not Is_Itype (Def_Id)
9180       then
9181          --  A little sanity check, emit an error message if the type
9182          --  has discriminants to begin with. Type T may be a regular
9183          --  incomplete type or imported via a limited with clause.
9184
9185          if Has_Discriminants (T)
9186            or else
9187              (From_With_Type (T)
9188                 and then Present (Non_Limited_View (T))
9189                 and then Nkind (Parent (Non_Limited_View (T))) =
9190                            N_Full_Type_Declaration
9191                 and then Present (Discriminant_Specifications
9192                           (Parent (Non_Limited_View (T)))))
9193          then
9194             Error_Msg_N
9195               ("(Ada 2005) incomplete subtype may not be constrained", C);
9196          else
9197             Error_Msg_N
9198               ("invalid constraint: type has no discriminant", C);
9199          end if;
9200
9201          Fixup_Bad_Constraint;
9202          return;
9203
9204       --  Check that the type has visible discriminants. The type may be
9205       --  a private type with unknown discriminants whose full view has
9206       --  discriminants which are invisible.
9207
9208       elsif not Has_Discriminants (T)
9209         or else
9210           (Has_Unknown_Discriminants (T)
9211              and then Is_Private_Type (T))
9212       then
9213          Error_Msg_N ("invalid constraint: type has no discriminant", C);
9214          Fixup_Bad_Constraint;
9215          return;
9216
9217       elsif Is_Constrained (E)
9218         or else (Ekind (E) = E_Class_Wide_Subtype
9219                   and then Present (Discriminant_Constraint (E)))
9220       then
9221          Error_Msg_N ("type is already constrained", Subtype_Mark (S));
9222          Fixup_Bad_Constraint;
9223          return;
9224       end if;
9225
9226       --  T may be an unconstrained subtype (e.g. a generic actual).
9227       --  Constraint applies to the base type.
9228
9229       T := Base_Type (T);
9230
9231       Elist := Build_Discriminant_Constraints (T, S);
9232
9233       --  If the list returned was empty we had an error in building the
9234       --  discriminant constraint. We have also already signalled an error
9235       --  in the incomplete type case
9236
9237       if Is_Empty_Elmt_List (Elist) then
9238          Fixup_Bad_Constraint;
9239          return;
9240       end if;
9241
9242       Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
9243    end Constrain_Discriminated_Type;
9244
9245    ---------------------------
9246    -- Constrain_Enumeration --
9247    ---------------------------
9248
9249    procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
9250       T : constant Entity_Id := Entity (Subtype_Mark (S));
9251       C : constant Node_Id   := Constraint (S);
9252
9253    begin
9254       Set_Ekind (Def_Id, E_Enumeration_Subtype);
9255
9256       Set_First_Literal     (Def_Id, First_Literal (Base_Type (T)));
9257
9258       Set_Etype             (Def_Id, Base_Type         (T));
9259       Set_Size_Info         (Def_Id,                   (T));
9260       Set_First_Rep_Item    (Def_Id, First_Rep_Item    (T));
9261       Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
9262
9263       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
9264
9265       Set_Discrete_RM_Size (Def_Id);
9266    end Constrain_Enumeration;
9267
9268    ----------------------
9269    -- Constrain_Float --
9270    ----------------------
9271
9272    procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
9273       T    : constant Entity_Id := Entity (Subtype_Mark (S));
9274       C    : Node_Id;
9275       D    : Node_Id;
9276       Rais : Node_Id;
9277
9278    begin
9279       Set_Ekind (Def_Id, E_Floating_Point_Subtype);
9280
9281       Set_Etype          (Def_Id, Base_Type      (T));
9282       Set_Size_Info      (Def_Id,                (T));
9283       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9284
9285       --  Process the constraint
9286
9287       C := Constraint (S);
9288
9289       --  Digits constraint present
9290
9291       if Nkind (C) = N_Digits_Constraint then
9292          Check_Restriction (No_Obsolescent_Features, C);
9293
9294          if Warn_On_Obsolescent_Feature then
9295             Error_Msg_N
9296               ("subtype digits constraint is an " &
9297                "obsolescent feature ('R'M 'J.3(8))?", C);
9298          end if;
9299
9300          D := Digits_Expression (C);
9301          Analyze_And_Resolve (D, Any_Integer);
9302          Check_Digits_Expression (D);
9303          Set_Digits_Value (Def_Id, Expr_Value (D));
9304
9305          --  Check that digits value is in range. Obviously we can do this
9306          --  at compile time, but it is strictly a runtime check, and of
9307          --  course there is an ACVC test that checks this!
9308
9309          if Digits_Value (Def_Id) > Digits_Value (T) then
9310             Error_Msg_Uint_1 := Digits_Value (T);
9311             Error_Msg_N ("?digits value is too large, maximum is ^", D);
9312             Rais :=
9313               Make_Raise_Constraint_Error (Sloc (D),
9314                 Reason => CE_Range_Check_Failed);
9315             Insert_Action (Declaration_Node (Def_Id), Rais);
9316          end if;
9317
9318          C := Range_Constraint (C);
9319
9320       --  No digits constraint present
9321
9322       else
9323          Set_Digits_Value (Def_Id, Digits_Value (T));
9324       end if;
9325
9326       --  Range constraint present
9327
9328       if Nkind (C) = N_Range_Constraint then
9329          Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
9330
9331       --  No range constraint present
9332
9333       else
9334          pragma Assert (No (C));
9335          Set_Scalar_Range (Def_Id, Scalar_Range (T));
9336       end if;
9337
9338       Set_Is_Constrained (Def_Id);
9339    end Constrain_Float;
9340
9341    ---------------------
9342    -- Constrain_Index --
9343    ---------------------
9344
9345    procedure Constrain_Index
9346      (Index        : Node_Id;
9347       S            : Node_Id;
9348       Related_Nod  : Node_Id;
9349       Related_Id   : Entity_Id;
9350       Suffix       : Character;
9351       Suffix_Index : Nat)
9352    is
9353       Def_Id : Entity_Id;
9354       R      : Node_Id := Empty;
9355       T      : constant Entity_Id := Etype (Index);
9356
9357    begin
9358       if Nkind (S) = N_Range
9359         or else
9360           (Nkind (S) = N_Attribute_Reference
9361             and then Attribute_Name (S) = Name_Range)
9362       then
9363          --  A Range attribute will transformed into N_Range by Resolve
9364
9365          Analyze (S);
9366          Set_Etype (S, T);
9367          R := S;
9368
9369          Process_Range_Expr_In_Decl (R, T, Empty_List);
9370
9371          if not Error_Posted (S)
9372            and then
9373              (Nkind (S) /= N_Range
9374                or else not Covers (T, (Etype (Low_Bound (S))))
9375                or else not Covers (T, (Etype (High_Bound (S)))))
9376          then
9377             if Base_Type (T) /= Any_Type
9378               and then Etype (Low_Bound (S)) /= Any_Type
9379               and then Etype (High_Bound (S)) /= Any_Type
9380             then
9381                Error_Msg_N ("range expected", S);
9382             end if;
9383          end if;
9384
9385       elsif Nkind (S) = N_Subtype_Indication then
9386
9387          --  The parser has verified that this is a discrete indication
9388
9389          Resolve_Discrete_Subtype_Indication (S, T);
9390          R := Range_Expression (Constraint (S));
9391
9392       elsif Nkind (S) = N_Discriminant_Association then
9393
9394          --  Syntactically valid in subtype indication
9395
9396          Error_Msg_N ("invalid index constraint", S);
9397          Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
9398          return;
9399
9400       --  Subtype_Mark case, no anonymous subtypes to construct
9401
9402       else
9403          Analyze (S);
9404
9405          if Is_Entity_Name (S) then
9406             if not Is_Type (Entity (S)) then
9407                Error_Msg_N ("expect subtype mark for index constraint", S);
9408
9409             elsif Base_Type (Entity (S)) /= Base_Type (T) then
9410                Wrong_Type (S, Base_Type (T));
9411             end if;
9412
9413             return;
9414
9415          else
9416             Error_Msg_N ("invalid index constraint", S);
9417             Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
9418             return;
9419          end if;
9420       end if;
9421
9422       Def_Id :=
9423         Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
9424
9425       Set_Etype (Def_Id, Base_Type (T));
9426
9427       if Is_Modular_Integer_Type (T) then
9428          Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
9429
9430       elsif Is_Integer_Type (T) then
9431          Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
9432
9433       else
9434          Set_Ekind (Def_Id, E_Enumeration_Subtype);
9435          Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
9436       end if;
9437
9438       Set_Size_Info      (Def_Id,                (T));
9439       Set_RM_Size        (Def_Id, RM_Size        (T));
9440       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9441
9442       Set_Scalar_Range   (Def_Id, R);
9443
9444       Set_Etype (S, Def_Id);
9445       Set_Discrete_RM_Size (Def_Id);
9446    end Constrain_Index;
9447
9448    -----------------------
9449    -- Constrain_Integer --
9450    -----------------------
9451
9452    procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
9453       T : constant Entity_Id := Entity (Subtype_Mark (S));
9454       C : constant Node_Id   := Constraint (S);
9455
9456    begin
9457       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
9458
9459       if Is_Modular_Integer_Type (T) then
9460          Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
9461       else
9462          Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
9463       end if;
9464
9465       Set_Etype            (Def_Id, Base_Type        (T));
9466       Set_Size_Info        (Def_Id,                  (T));
9467       Set_First_Rep_Item   (Def_Id, First_Rep_Item   (T));
9468       Set_Discrete_RM_Size (Def_Id);
9469    end Constrain_Integer;
9470
9471    ------------------------------
9472    -- Constrain_Ordinary_Fixed --
9473    ------------------------------
9474
9475    procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
9476       T    : constant Entity_Id := Entity (Subtype_Mark (S));
9477       C    : Node_Id;
9478       D    : Node_Id;
9479       Rais : Node_Id;
9480
9481    begin
9482       Set_Ekind          (Def_Id, E_Ordinary_Fixed_Point_Subtype);
9483       Set_Etype          (Def_Id, Base_Type        (T));
9484       Set_Size_Info      (Def_Id,                  (T));
9485       Set_First_Rep_Item (Def_Id, First_Rep_Item   (T));
9486       Set_Small_Value    (Def_Id, Small_Value      (T));
9487
9488       --  Process the constraint
9489
9490       C := Constraint (S);
9491
9492       --  Delta constraint present
9493
9494       if Nkind (C) = N_Delta_Constraint then
9495          Check_Restriction (No_Obsolescent_Features, C);
9496
9497          if Warn_On_Obsolescent_Feature then
9498             Error_Msg_S
9499               ("subtype delta constraint is an " &
9500                "obsolescent feature ('R'M 'J.3(7))?");
9501          end if;
9502
9503          D := Delta_Expression (C);
9504          Analyze_And_Resolve (D, Any_Real);
9505          Check_Delta_Expression (D);
9506          Set_Delta_Value (Def_Id, Expr_Value_R (D));
9507
9508          --  Check that delta value is in range. Obviously we can do this
9509          --  at compile time, but it is strictly a runtime check, and of
9510          --  course there is an ACVC test that checks this!
9511
9512          if Delta_Value (Def_Id) < Delta_Value (T) then
9513             Error_Msg_N ("?delta value is too small", D);
9514             Rais :=
9515               Make_Raise_Constraint_Error (Sloc (D),
9516                 Reason => CE_Range_Check_Failed);
9517             Insert_Action (Declaration_Node (Def_Id), Rais);
9518          end if;
9519
9520          C := Range_Constraint (C);
9521
9522       --  No delta constraint present
9523
9524       else
9525          Set_Delta_Value (Def_Id, Delta_Value (T));
9526       end if;
9527
9528       --  Range constraint present
9529
9530       if Nkind (C) = N_Range_Constraint then
9531          Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
9532
9533       --  No range constraint present
9534
9535       else
9536          pragma Assert (No (C));
9537          Set_Scalar_Range (Def_Id, Scalar_Range (T));
9538
9539       end if;
9540
9541       Set_Discrete_RM_Size (Def_Id);
9542
9543       --  Unconditionally delay the freeze, since we cannot set size
9544       --  information in all cases correctly until the freeze point.
9545
9546       Set_Has_Delayed_Freeze (Def_Id);
9547    end Constrain_Ordinary_Fixed;
9548
9549    ---------------------------
9550    -- Convert_Scalar_Bounds --
9551    ---------------------------
9552
9553    procedure Convert_Scalar_Bounds
9554      (N            : Node_Id;
9555       Parent_Type  : Entity_Id;
9556       Derived_Type : Entity_Id;
9557       Loc          : Source_Ptr)
9558    is
9559       Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
9560
9561       Lo  : Node_Id;
9562       Hi  : Node_Id;
9563       Rng : Node_Id;
9564
9565    begin
9566       Lo := Build_Scalar_Bound
9567               (Type_Low_Bound (Derived_Type),
9568                Parent_Type, Implicit_Base);
9569
9570       Hi := Build_Scalar_Bound
9571               (Type_High_Bound (Derived_Type),
9572                Parent_Type, Implicit_Base);
9573
9574       Rng :=
9575         Make_Range (Loc,
9576           Low_Bound  => Lo,
9577           High_Bound => Hi);
9578
9579       Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
9580
9581       Set_Parent (Rng, N);
9582       Set_Scalar_Range (Derived_Type, Rng);
9583
9584       --  Analyze the bounds
9585
9586       Analyze_And_Resolve (Lo, Implicit_Base);
9587       Analyze_And_Resolve (Hi, Implicit_Base);
9588
9589       --  Analyze the range itself, except that we do not analyze it if
9590       --  the bounds are real literals, and we have a fixed-point type.
9591       --  The reason for this is that we delay setting the bounds in this
9592       --  case till we know the final Small and Size values (see circuit
9593       --  in Freeze.Freeze_Fixed_Point_Type for further details).
9594
9595       if Is_Fixed_Point_Type (Parent_Type)
9596         and then Nkind (Lo) = N_Real_Literal
9597         and then Nkind (Hi) = N_Real_Literal
9598       then
9599          return;
9600
9601       --  Here we do the analysis of the range
9602
9603       --  Note: we do this manually, since if we do a normal Analyze and
9604       --  Resolve call, there are problems with the conversions used for
9605       --  the derived type range.
9606
9607       else
9608          Set_Etype    (Rng, Implicit_Base);
9609          Set_Analyzed (Rng, True);
9610       end if;
9611    end Convert_Scalar_Bounds;
9612
9613    -------------------
9614    -- Copy_And_Swap --
9615    -------------------
9616
9617    procedure Copy_And_Swap (Priv, Full : Entity_Id) is
9618    begin
9619       --  Initialize new full declaration entity by copying the pertinent
9620       --  fields of the corresponding private declaration entity.
9621
9622       --  We temporarily set Ekind to a value appropriate for a type to
9623       --  avoid assert failures in Einfo from checking for setting type
9624       --  attributes on something that is not a type. Ekind (Priv) is an
9625       --  appropriate choice, since it allowed the attributes to be set
9626       --  in the first place. This Ekind value will be modified later.
9627
9628       Set_Ekind (Full, Ekind (Priv));
9629
9630       --  Also set Etype temporarily to Any_Type, again, in the absence
9631       --  of errors, it will be properly reset, and if there are errors,
9632       --  then we want a value of Any_Type to remain.
9633
9634       Set_Etype (Full, Any_Type);
9635
9636       --  Now start copying attributes
9637
9638       Set_Has_Discriminants          (Full, Has_Discriminants       (Priv));
9639
9640       if Has_Discriminants (Full) then
9641          Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
9642          Set_Stored_Constraint       (Full, Stored_Constraint       (Priv));
9643       end if;
9644
9645       Set_First_Rep_Item             (Full, First_Rep_Item          (Priv));
9646       Set_Homonym                    (Full, Homonym                 (Priv));
9647       Set_Is_Immediately_Visible     (Full, Is_Immediately_Visible  (Priv));
9648       Set_Is_Public                  (Full, Is_Public               (Priv));
9649       Set_Is_Pure                    (Full, Is_Pure                 (Priv));
9650       Set_Is_Tagged_Type             (Full, Is_Tagged_Type          (Priv));
9651
9652       Conditional_Delay              (Full,                          Priv);
9653
9654       if Is_Tagged_Type (Full) then
9655          Set_Primitive_Operations    (Full, Primitive_Operations    (Priv));
9656
9657          if Priv = Base_Type (Priv) then
9658             Set_Class_Wide_Type      (Full, Class_Wide_Type         (Priv));
9659          end if;
9660       end if;
9661
9662       Set_Is_Volatile                (Full, Is_Volatile             (Priv));
9663       Set_Treat_As_Volatile          (Full, Treat_As_Volatile       (Priv));
9664       Set_Scope                      (Full, Scope                   (Priv));
9665       Set_Next_Entity                (Full, Next_Entity             (Priv));
9666       Set_First_Entity               (Full, First_Entity            (Priv));
9667       Set_Last_Entity                (Full, Last_Entity             (Priv));
9668
9669       --  If access types have been recorded for later handling, keep them in
9670       --  the full view so that they get handled when the full view freeze
9671       --  node is expanded.
9672
9673       if Present (Freeze_Node (Priv))
9674         and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
9675       then
9676          Ensure_Freeze_Node (Full);
9677          Set_Access_Types_To_Process
9678            (Freeze_Node (Full),
9679             Access_Types_To_Process (Freeze_Node (Priv)));
9680       end if;
9681
9682       --  Swap the two entities. Now Privat is the full type entity and
9683       --  Full is the private one. They will be swapped back at the end
9684       --  of the private part. This swapping ensures that the entity that
9685       --  is visible in the private part is the full declaration.
9686
9687       Exchange_Entities (Priv, Full);
9688       Append_Entity (Full, Scope (Full));
9689    end Copy_And_Swap;
9690
9691    -------------------------------------
9692    -- Copy_Array_Base_Type_Attributes --
9693    -------------------------------------
9694
9695    procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
9696    begin
9697       Set_Component_Alignment      (T1, Component_Alignment      (T2));
9698       Set_Component_Type           (T1, Component_Type           (T2));
9699       Set_Component_Size           (T1, Component_Size           (T2));
9700       Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
9701       Set_Finalize_Storage_Only    (T1, Finalize_Storage_Only    (T2));
9702       Set_Has_Non_Standard_Rep     (T1, Has_Non_Standard_Rep     (T2));
9703       Set_Has_Task                 (T1, Has_Task                 (T2));
9704       Set_Is_Packed                (T1, Is_Packed                (T2));
9705       Set_Has_Aliased_Components   (T1, Has_Aliased_Components   (T2));
9706       Set_Has_Atomic_Components    (T1, Has_Atomic_Components    (T2));
9707       Set_Has_Volatile_Components  (T1, Has_Volatile_Components  (T2));
9708    end Copy_Array_Base_Type_Attributes;
9709
9710    -----------------------------------
9711    -- Copy_Array_Subtype_Attributes --
9712    -----------------------------------
9713
9714    procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
9715    begin
9716       Set_Size_Info (T1, T2);
9717
9718       Set_First_Index          (T1, First_Index           (T2));
9719       Set_Is_Aliased           (T1, Is_Aliased            (T2));
9720       Set_Is_Atomic            (T1, Is_Atomic             (T2));
9721       Set_Is_Volatile          (T1, Is_Volatile           (T2));
9722       Set_Treat_As_Volatile    (T1, Treat_As_Volatile     (T2));
9723       Set_Is_Constrained       (T1, Is_Constrained        (T2));
9724       Set_Depends_On_Private   (T1, Has_Private_Component (T2));
9725       Set_First_Rep_Item       (T1, First_Rep_Item        (T2));
9726       Set_Convention           (T1, Convention            (T2));
9727       Set_Is_Limited_Composite (T1, Is_Limited_Composite  (T2));
9728       Set_Is_Private_Composite (T1, Is_Private_Composite  (T2));
9729    end Copy_Array_Subtype_Attributes;
9730
9731    -----------------------------------
9732    -- Create_Constrained_Components --
9733    -----------------------------------
9734
9735    procedure Create_Constrained_Components
9736      (Subt        : Entity_Id;
9737       Decl_Node   : Node_Id;
9738       Typ         : Entity_Id;
9739       Constraints : Elist_Id)
9740    is
9741       Loc         : constant Source_Ptr := Sloc (Subt);
9742       Comp_List   : constant Elist_Id   := New_Elmt_List;
9743       Parent_Type : constant Entity_Id  := Etype (Typ);
9744       Assoc_List  : constant List_Id    := New_List;
9745       Discr_Val   : Elmt_Id;
9746       Errors      : Boolean;
9747       New_C       : Entity_Id;
9748       Old_C       : Entity_Id;
9749       Is_Static   : Boolean := True;
9750
9751       procedure Collect_Fixed_Components (Typ : Entity_Id);
9752       --  Collect parent type components that do not appear in a variant part
9753
9754       procedure Create_All_Components;
9755       --  Iterate over Comp_List to create the components of the subtype
9756
9757       function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
9758       --  Creates a new component from Old_Compon, copying all the fields from
9759       --  it, including its Etype, inserts the new component in the Subt entity
9760       --  chain and returns the new component.
9761
9762       function Is_Variant_Record (T : Entity_Id) return Boolean;
9763       --  If true, and discriminants are static, collect only components from
9764       --  variants selected by discriminant values.
9765
9766       ------------------------------
9767       -- Collect_Fixed_Components --
9768       ------------------------------
9769
9770       procedure Collect_Fixed_Components (Typ : Entity_Id) is
9771       begin
9772       --  Build association list for discriminants, and find components of the
9773       --  variant part selected by the values of the discriminants.
9774
9775          Old_C := First_Discriminant (Typ);
9776          Discr_Val := First_Elmt (Constraints);
9777          while Present (Old_C) loop
9778             Append_To (Assoc_List,
9779               Make_Component_Association (Loc,
9780                  Choices    => New_List (New_Occurrence_Of (Old_C, Loc)),
9781                  Expression => New_Copy (Node (Discr_Val))));
9782
9783             Next_Elmt (Discr_Val);
9784             Next_Discriminant (Old_C);
9785          end loop;
9786
9787          --  The tag, and the possible parent and controller components
9788          --  are unconditionally in the subtype.
9789
9790          if Is_Tagged_Type (Typ)
9791            or else Has_Controlled_Component (Typ)
9792          then
9793             Old_C := First_Component (Typ);
9794             while Present (Old_C) loop
9795                if Chars ((Old_C)) = Name_uTag
9796                  or else Chars ((Old_C)) = Name_uParent
9797                  or else Chars ((Old_C)) = Name_uController
9798                then
9799                   Append_Elmt (Old_C, Comp_List);
9800                end if;
9801
9802                Next_Component (Old_C);
9803             end loop;
9804          end if;
9805       end Collect_Fixed_Components;
9806
9807       ---------------------------
9808       -- Create_All_Components --
9809       ---------------------------
9810
9811       procedure Create_All_Components is
9812          Comp : Elmt_Id;
9813
9814       begin
9815          Comp := First_Elmt (Comp_List);
9816          while Present (Comp) loop
9817             Old_C := Node (Comp);
9818             New_C := Create_Component (Old_C);
9819
9820             Set_Etype
9821               (New_C,
9822                Constrain_Component_Type
9823                  (Old_C, Subt, Decl_Node, Typ, Constraints));
9824             Set_Is_Public (New_C, Is_Public (Subt));
9825
9826             Next_Elmt (Comp);
9827          end loop;
9828       end Create_All_Components;
9829
9830       ----------------------
9831       -- Create_Component --
9832       ----------------------
9833
9834       function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
9835          New_Compon : constant Entity_Id := New_Copy (Old_Compon);
9836
9837       begin
9838          --  Set the parent so we have a proper link for freezing etc. This is
9839          --  not a real parent pointer, since of course our parent does not own
9840          --  up to us and reference us, we are an illegitimate child of the
9841          --  original parent!
9842
9843          Set_Parent (New_Compon, Parent (Old_Compon));
9844
9845          --  If the old component's Esize was already determined and is a
9846          --  static value, then the new component simply inherits it. Otherwise
9847          --  the old component's size may require run-time determination, but
9848          --  the new component's size still might be statically determinable
9849          --  (if, for example it has a static constraint). In that case we want
9850          --  Layout_Type to recompute the component's size, so we reset its
9851          --  size and positional fields.
9852
9853          if Frontend_Layout_On_Target
9854            and then not Known_Static_Esize (Old_Compon)
9855          then
9856             Set_Esize (New_Compon, Uint_0);
9857             Init_Normalized_First_Bit    (New_Compon);
9858             Init_Normalized_Position     (New_Compon);
9859             Init_Normalized_Position_Max (New_Compon);
9860          end if;
9861
9862          --  We do not want this node marked as Comes_From_Source, since
9863          --  otherwise it would get first class status and a separate cross-
9864          --  reference line would be generated. Illegitimate children do not
9865          --  rate such recognition.
9866
9867          Set_Comes_From_Source (New_Compon, False);
9868
9869          --  But it is a real entity, and a birth certificate must be properly
9870          --  registered by entering it into the entity list.
9871
9872          Enter_Name (New_Compon);
9873
9874          return New_Compon;
9875       end Create_Component;
9876
9877       -----------------------
9878       -- Is_Variant_Record --
9879       -----------------------
9880
9881       function Is_Variant_Record (T : Entity_Id) return Boolean is
9882       begin
9883          return Nkind (Parent (T)) = N_Full_Type_Declaration
9884            and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
9885            and then Present (Component_List (Type_Definition (Parent (T))))
9886            and then Present (
9887              Variant_Part (Component_List (Type_Definition (Parent (T)))));
9888       end Is_Variant_Record;
9889
9890    --  Start of processing for Create_Constrained_Components
9891
9892    begin
9893       pragma Assert (Subt /= Base_Type (Subt));
9894       pragma Assert (Typ = Base_Type (Typ));
9895
9896       Set_First_Entity (Subt, Empty);
9897       Set_Last_Entity  (Subt, Empty);
9898
9899       --  Check whether constraint is fully static, in which case we can
9900       --  optimize the list of components.
9901
9902       Discr_Val := First_Elmt (Constraints);
9903       while Present (Discr_Val) loop
9904          if not Is_OK_Static_Expression (Node (Discr_Val)) then
9905             Is_Static := False;
9906             exit;
9907          end if;
9908
9909          Next_Elmt (Discr_Val);
9910       end loop;
9911
9912       Set_Has_Static_Discriminants (Subt, Is_Static);
9913
9914       New_Scope (Subt);
9915
9916       --  Inherit the discriminants of the parent type
9917
9918       Old_C := First_Discriminant (Typ);
9919       while Present (Old_C) loop
9920          New_C := Create_Component (Old_C);
9921          Set_Is_Public (New_C, Is_Public (Subt));
9922          Next_Discriminant (Old_C);
9923       end loop;
9924
9925       if Is_Static
9926         and then Is_Variant_Record (Typ)
9927       then
9928          Collect_Fixed_Components (Typ);
9929
9930          Gather_Components (
9931            Typ,
9932            Component_List (Type_Definition (Parent (Typ))),
9933            Governed_By   => Assoc_List,
9934            Into          => Comp_List,
9935            Report_Errors => Errors);
9936          pragma Assert (not Errors);
9937
9938          Create_All_Components;
9939
9940       --  If the subtype declaration is created for a tagged type derivation
9941       --  with constraints, we retrieve the record definition of the parent
9942       --  type to select the components of the proper variant.
9943
9944       elsif Is_Static
9945         and then Is_Tagged_Type (Typ)
9946         and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
9947         and then
9948           Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
9949         and then Is_Variant_Record (Parent_Type)
9950       then
9951          Collect_Fixed_Components (Typ);
9952
9953          Gather_Components (
9954            Typ,
9955            Component_List (Type_Definition (Parent (Parent_Type))),
9956            Governed_By   => Assoc_List,
9957            Into          => Comp_List,
9958            Report_Errors => Errors);
9959          pragma Assert (not Errors);
9960
9961          --  If the tagged derivation has a type extension, collect all the
9962          --  new components therein.
9963
9964          if Present
9965               (Record_Extension_Part (Type_Definition (Parent (Typ))))
9966          then
9967             Old_C := First_Component (Typ);
9968             while Present (Old_C) loop
9969                if Original_Record_Component (Old_C) = Old_C
9970                 and then Chars (Old_C) /= Name_uTag
9971                 and then Chars (Old_C) /= Name_uParent
9972                 and then Chars (Old_C) /= Name_uController
9973                then
9974                   Append_Elmt (Old_C, Comp_List);
9975                end if;
9976
9977                Next_Component (Old_C);
9978             end loop;
9979          end if;
9980
9981          Create_All_Components;
9982
9983       else
9984          --  If discriminants are not static, or if this is a multi-level type
9985          --  extension, we have to include all components of the parent type.
9986
9987          Old_C := First_Component (Typ);
9988          while Present (Old_C) loop
9989             New_C := Create_Component (Old_C);
9990
9991             Set_Etype
9992               (New_C,
9993                Constrain_Component_Type
9994                  (Old_C, Subt, Decl_Node, Typ, Constraints));
9995             Set_Is_Public (New_C, Is_Public (Subt));
9996
9997             Next_Component (Old_C);
9998          end loop;
9999       end if;
10000
10001       End_Scope;
10002    end Create_Constrained_Components;
10003
10004    ------------------------------------------
10005    -- Decimal_Fixed_Point_Type_Declaration --
10006    ------------------------------------------
10007
10008    procedure Decimal_Fixed_Point_Type_Declaration
10009      (T   : Entity_Id;
10010       Def : Node_Id)
10011    is
10012       Loc           : constant Source_Ptr := Sloc (Def);
10013       Digs_Expr     : constant Node_Id    := Digits_Expression (Def);
10014       Delta_Expr    : constant Node_Id    := Delta_Expression (Def);
10015       Implicit_Base : Entity_Id;
10016       Digs_Val      : Uint;
10017       Delta_Val     : Ureal;
10018       Scale_Val     : Uint;
10019       Bound_Val     : Ureal;
10020
10021    --  Start of processing for Decimal_Fixed_Point_Type_Declaration
10022
10023    begin
10024       Check_Restriction (No_Fixed_Point, Def);
10025
10026       --  Create implicit base type
10027
10028       Implicit_Base :=
10029         Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
10030       Set_Etype (Implicit_Base, Implicit_Base);
10031
10032       --  Analyze and process delta expression
10033
10034       Analyze_And_Resolve (Delta_Expr, Universal_Real);
10035
10036       Check_Delta_Expression (Delta_Expr);
10037       Delta_Val := Expr_Value_R (Delta_Expr);
10038
10039       --  Check delta is power of 10, and determine scale value from it
10040
10041       declare
10042          Val : Ureal;
10043
10044       begin
10045          Scale_Val := Uint_0;
10046          Val := Delta_Val;
10047
10048          if Val < Ureal_1 then
10049             while Val < Ureal_1 loop
10050                Val := Val * Ureal_10;
10051                Scale_Val := Scale_Val + 1;
10052             end loop;
10053
10054             if Scale_Val > 18 then
10055                Error_Msg_N ("scale exceeds maximum value of 18", Def);
10056                Scale_Val := UI_From_Int (+18);
10057             end if;
10058
10059          else
10060             while Val > Ureal_1 loop
10061                Val := Val / Ureal_10;
10062                Scale_Val := Scale_Val - 1;
10063             end loop;
10064
10065             if Scale_Val < -18 then
10066                Error_Msg_N ("scale is less than minimum value of -18", Def);
10067                Scale_Val := UI_From_Int (-18);
10068             end if;
10069          end if;
10070
10071          if Val /= Ureal_1 then
10072             Error_Msg_N ("delta expression must be a power of 10", Def);
10073             Delta_Val := Ureal_10 ** (-Scale_Val);
10074          end if;
10075       end;
10076
10077       --  Set delta, scale and small (small = delta for decimal type)
10078
10079       Set_Delta_Value (Implicit_Base, Delta_Val);
10080       Set_Scale_Value (Implicit_Base, Scale_Val);
10081       Set_Small_Value (Implicit_Base, Delta_Val);
10082
10083       --  Analyze and process digits expression
10084
10085       Analyze_And_Resolve (Digs_Expr, Any_Integer);
10086       Check_Digits_Expression (Digs_Expr);
10087       Digs_Val := Expr_Value (Digs_Expr);
10088
10089       if Digs_Val > 18 then
10090          Digs_Val := UI_From_Int (+18);
10091          Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
10092       end if;
10093
10094       Set_Digits_Value (Implicit_Base, Digs_Val);
10095       Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
10096
10097       --  Set range of base type from digits value for now. This will be
10098       --  expanded to represent the true underlying base range by Freeze.
10099
10100       Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
10101
10102       --  Set size to zero for now, size will be set at freeze time. We have
10103       --  to do this for ordinary fixed-point, because the size depends on
10104       --  the specified small, and we might as well do the same for decimal
10105       --  fixed-point.
10106
10107       Init_Size_Align (Implicit_Base);
10108
10109       --  If there are bounds given in the declaration use them as the
10110       --  bounds of the first named subtype.
10111
10112       if Present (Real_Range_Specification (Def)) then
10113          declare
10114             RRS      : constant Node_Id := Real_Range_Specification (Def);
10115             Low      : constant Node_Id := Low_Bound (RRS);
10116             High     : constant Node_Id := High_Bound (RRS);
10117             Low_Val  : Ureal;
10118             High_Val : Ureal;
10119
10120          begin
10121             Analyze_And_Resolve (Low, Any_Real);
10122             Analyze_And_Resolve (High, Any_Real);
10123             Check_Real_Bound (Low);
10124             Check_Real_Bound (High);
10125             Low_Val := Expr_Value_R (Low);
10126             High_Val := Expr_Value_R (High);
10127
10128             if Low_Val < (-Bound_Val) then
10129                Error_Msg_N
10130                  ("range low bound too small for digits value", Low);
10131                Low_Val := -Bound_Val;
10132             end if;
10133
10134             if High_Val > Bound_Val then
10135                Error_Msg_N
10136                  ("range high bound too large for digits value", High);
10137                High_Val := Bound_Val;
10138             end if;
10139
10140             Set_Fixed_Range (T, Loc, Low_Val, High_Val);
10141          end;
10142
10143       --  If no explicit range, use range that corresponds to given
10144       --  digits value. This will end up as the final range for the
10145       --  first subtype.
10146
10147       else
10148          Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
10149       end if;
10150
10151       --  Complete entity for first subtype
10152
10153       Set_Ekind          (T, E_Decimal_Fixed_Point_Subtype);
10154       Set_Etype          (T, Implicit_Base);
10155       Set_Size_Info      (T, Implicit_Base);
10156       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
10157       Set_Digits_Value   (T, Digs_Val);
10158       Set_Delta_Value    (T, Delta_Val);
10159       Set_Small_Value    (T, Delta_Val);
10160       Set_Scale_Value    (T, Scale_Val);
10161       Set_Is_Constrained (T);
10162    end Decimal_Fixed_Point_Type_Declaration;
10163
10164    ----------------------------------
10165    -- Derive_Interface_Subprograms --
10166    ----------------------------------
10167
10168    procedure Derive_Interface_Subprograms
10169      (Parent_Type : Entity_Id;
10170       Tagged_Type : Entity_Id;
10171       Ifaces_List : Elist_Id)
10172    is
10173       function Collect_Interface_Primitives
10174         (Tagged_Type : Entity_Id) return Elist_Id;
10175       --  Ada 2005 (AI-251): Collect the primitives of all the implemented
10176       --  interfaces.
10177
10178       function In_List (L : Elist_Id; Subp : Entity_Id) return Boolean;
10179       --  Determine if Subp already in the list L
10180
10181       procedure Remove_Homonym (E : Entity_Id);
10182       --  Removes E from the homonym chain
10183
10184       ----------------------------------
10185       -- Collect_Interface_Primitives --
10186       ----------------------------------
10187
10188       function Collect_Interface_Primitives
10189          (Tagged_Type : Entity_Id) return Elist_Id
10190       is
10191          Op_List     : constant Elist_Id := New_Elmt_List;
10192          Elmt        : Elmt_Id;
10193          Ifaces_List : Elist_Id;
10194          Iface_Elmt  : Elmt_Id;
10195          Prim        : Entity_Id;
10196
10197       begin
10198          pragma Assert (Is_Tagged_Type (Tagged_Type)
10199            and then Has_Abstract_Interfaces (Tagged_Type));
10200
10201          Collect_Abstract_Interfaces (Tagged_Type, Ifaces_List);
10202
10203          Iface_Elmt := First_Elmt (Ifaces_List);
10204          while Present (Iface_Elmt) loop
10205             Elmt := First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
10206
10207             while Present (Elmt) loop
10208                Prim := Node (Elmt);
10209
10210                if not Is_Predefined_Dispatching_Operation (Prim) then
10211                   Append_Elmt (Prim, Op_List);
10212                end if;
10213
10214                Next_Elmt (Elmt);
10215             end loop;
10216
10217             Next_Elmt (Iface_Elmt);
10218          end loop;
10219
10220          return Op_List;
10221       end Collect_Interface_Primitives;
10222
10223       -------------
10224       -- In_List --
10225       -------------
10226
10227       function In_List (L : Elist_Id; Subp : Entity_Id) return Boolean is
10228          Elmt : Elmt_Id;
10229       begin
10230          Elmt := First_Elmt (L);
10231          while Present (Elmt) loop
10232             if Node (Elmt) = Subp then
10233                return True;
10234             end if;
10235
10236             Next_Elmt (Elmt);
10237          end loop;
10238
10239          return False;
10240       end In_List;
10241
10242       --------------------
10243       -- Remove_Homonym --
10244       --------------------
10245
10246       procedure Remove_Homonym (E : Entity_Id) is
10247          Prev  : Entity_Id := Empty;
10248          H     : Entity_Id;
10249
10250       begin
10251          if E = Current_Entity (E) then
10252             Set_Current_Entity (Homonym (E));
10253          else
10254             H := Current_Entity (E);
10255             while Present (H) and then H /= E loop
10256                Prev := H;
10257                H    := Homonym (H);
10258             end loop;
10259
10260             Set_Homonym (Prev, Homonym (E));
10261          end if;
10262       end Remove_Homonym;
10263
10264       --  Local Variables
10265
10266       E           : Entity_Id;
10267       Elmt        : Elmt_Id;
10268       Iface       : Entity_Id;
10269       Iface_Subp  : Entity_Id;
10270       New_Subp    : Entity_Id := Empty;
10271       Op_List     : Elist_Id;
10272       Parent_Base : Entity_Id;
10273       Subp        : Entity_Id;
10274
10275    --  Start of processing for Derive_Interface_Subprograms
10276
10277    begin
10278       if Ada_Version < Ada_05
10279         or else not Is_Record_Type (Tagged_Type)
10280         or else not Is_Tagged_Type (Tagged_Type)
10281         or else not Has_Abstract_Interfaces (Tagged_Type)
10282       then
10283          return;
10284       end if;
10285
10286       --  Add to the list of interface subprograms all the primitives inherited
10287       --  from abstract interfaces that are not immediate ancestors and also
10288       --  add their derivation to the list of interface primitives.
10289
10290       Op_List := Collect_Interface_Primitives (Tagged_Type);
10291
10292       Elmt := First_Elmt (Op_List);
10293       while Present (Elmt) loop
10294          Subp  := Node (Elmt);
10295          Iface := Find_Dispatching_Type (Subp);
10296
10297          if not Is_Ancestor (Iface, Tagged_Type) then
10298             Derive_Subprogram (New_Subp, Subp, Tagged_Type, Iface);
10299             Append_Elmt (New_Subp, Ifaces_List);
10300          end if;
10301
10302          Next_Elmt (Elmt);
10303       end loop;
10304
10305       --  Complete the derivation of the interface subprograms. Assignate to
10306       --  each entity associated with abstract interfaces their aliased entity
10307       --  and complete their decoration as hidden interface entities that will
10308       --  be used later to build the secondary dispatch tables.
10309
10310       if not Is_Empty_Elmt_List (Ifaces_List) then
10311          if Ekind (Parent_Type) = E_Record_Type_With_Private
10312            and then Has_Discriminants (Parent_Type)
10313            and then Present (Full_View (Parent_Type))
10314          then
10315             Parent_Base := Full_View (Parent_Type);
10316          else
10317             Parent_Base := Parent_Type;
10318          end if;
10319
10320          Elmt := First_Elmt (Ifaces_List);
10321          while Present (Elmt) loop
10322             Iface_Subp := Node (Elmt);
10323
10324             --  Look for the first overriding entity in the homonym chain.
10325             --  In this way if we are in the private part of a package spec
10326             --  we get the last overriding subprogram.
10327
10328             E  := Current_Entity_In_Scope (Iface_Subp);
10329             while Present (E) loop
10330                if Is_Dispatching_Operation (E)
10331                  and then Scope (E) = Scope (Iface_Subp)
10332                  and then Type_Conformant (E, Iface_Subp)
10333                  and then not In_List (Ifaces_List, E)
10334                then
10335                   exit;
10336                end if;
10337
10338                E := Homonym (E);
10339             end loop;
10340
10341             --  Create an overriding entity if not found in the homonym chain
10342
10343             if not Present (E) then
10344                Derive_Subprogram
10345                  (E, Alias (Iface_Subp), Tagged_Type, Parent_Base);
10346
10347             elsif not In_List (Primitive_Operations (Tagged_Type), E) then
10348
10349                --  Inherit the operation from the private view
10350
10351                Append_Elmt (E, Primitive_Operations (Tagged_Type));
10352             end if;
10353
10354             --  Complete the decoration of the hidden interface entity
10355
10356             Set_Is_Hidden                (Iface_Subp);
10357             Set_Abstract_Interface_Alias (Iface_Subp, Alias (Iface_Subp));
10358             Set_Alias                    (Iface_Subp, E);
10359             Set_Is_Abstract              (Iface_Subp, Is_Abstract (E));
10360             Remove_Homonym               (Iface_Subp);
10361
10362             Next_Elmt (Elmt);
10363          end loop;
10364       end if;
10365    end Derive_Interface_Subprograms;
10366
10367    -----------------------
10368    -- Derive_Subprogram --
10369    -----------------------
10370
10371    procedure Derive_Subprogram
10372      (New_Subp     : in out Entity_Id;
10373       Parent_Subp  : Entity_Id;
10374       Derived_Type : Entity_Id;
10375       Parent_Type  : Entity_Id;
10376       Actual_Subp  : Entity_Id := Empty)
10377    is
10378       Formal       : Entity_Id;
10379       New_Formal   : Entity_Id;
10380       Visible_Subp : Entity_Id := Parent_Subp;
10381
10382       function Is_Private_Overriding return Boolean;
10383       --  If Subp is a private overriding of a visible operation, the in-
10384       --  herited operation derives from the overridden op (even though
10385       --  its body is the overriding one) and the inherited operation is
10386       --  visible now. See sem_disp to see the details of the handling of
10387       --  the overridden subprogram, which is removed from the list of
10388       --  primitive operations of the type. The overridden subprogram is
10389       --  saved locally in Visible_Subp, and used to diagnose abstract
10390       --  operations that need overriding in the derived type.
10391
10392       procedure Replace_Type (Id, New_Id : Entity_Id);
10393       --  When the type is an anonymous access type, create a new access type
10394       --  designating the derived type.
10395
10396       procedure Set_Derived_Name;
10397       --  This procedure sets the appropriate Chars name for New_Subp. This
10398       --  is normally just a copy of the parent name. An exception arises for
10399       --  type support subprograms, where the name is changed to reflect the
10400       --  name of the derived type, e.g. if type foo is derived from type bar,
10401       --  then a procedure barDA is derived with a name fooDA.
10402
10403       ---------------------------
10404       -- Is_Private_Overriding --
10405       ---------------------------
10406
10407       function Is_Private_Overriding return Boolean is
10408          Prev : Entity_Id;
10409
10410       begin
10411          --  If the parent is not a dispatching operation there is no
10412          --  need to investigate overridings
10413
10414          if not Is_Dispatching_Operation (Parent_Subp) then
10415             return False;
10416          end if;
10417
10418          --  The visible operation that is overridden is a homonym of the
10419          --  parent subprogram. We scan the homonym chain to find the one
10420          --  whose alias is the subprogram we are deriving.
10421
10422          Prev := Current_Entity (Parent_Subp);
10423          while Present (Prev) loop
10424             if Ekind (Prev) = Ekind (Parent_Subp)
10425               and then Alias (Prev) = Parent_Subp
10426               and then Scope (Parent_Subp) = Scope (Prev)
10427               and then not Is_Hidden (Prev)
10428             then
10429                Visible_Subp := Prev;
10430                return True;
10431             end if;
10432
10433             Prev := Homonym (Prev);
10434          end loop;
10435
10436          return False;
10437       end Is_Private_Overriding;
10438
10439       ------------------
10440       -- Replace_Type --
10441       ------------------
10442
10443       procedure Replace_Type (Id, New_Id : Entity_Id) is
10444          Acc_Type : Entity_Id;
10445          IR       : Node_Id;
10446          Par      : constant Node_Id := Parent (Derived_Type);
10447
10448       begin
10449          --  When the type is an anonymous access type, create a new access
10450          --  type designating the derived type. This itype must be elaborated
10451          --  at the point of the derivation, not on subsequent calls that may
10452          --  be out of the proper scope for Gigi, so we insert a reference to
10453          --  it after the derivation.
10454
10455          if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
10456             declare
10457                Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
10458
10459             begin
10460                if Ekind (Desig_Typ) = E_Record_Type_With_Private
10461                  and then Present (Full_View (Desig_Typ))
10462                  and then not Is_Private_Type (Parent_Type)
10463                then
10464                   Desig_Typ := Full_View (Desig_Typ);
10465                end if;
10466
10467                if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
10468
10469                   --  Ada 2005 (AI-251): Handle also derivations of abstract
10470                   --  interface primitives.
10471
10472                  or else (Is_Interface (Desig_Typ)
10473                           and then not Is_Class_Wide_Type (Desig_Typ))
10474                then
10475                   Acc_Type := New_Copy (Etype (Id));
10476                   Set_Etype (Acc_Type, Acc_Type);
10477                   Set_Scope (Acc_Type, New_Subp);
10478
10479                   --  Compute size of anonymous access type
10480
10481                   if Is_Array_Type (Desig_Typ)
10482                     and then not Is_Constrained (Desig_Typ)
10483                   then
10484                      Init_Size (Acc_Type, 2 * System_Address_Size);
10485                   else
10486                      Init_Size (Acc_Type, System_Address_Size);
10487                   end if;
10488
10489                   Init_Alignment (Acc_Type);
10490                   Set_Directly_Designated_Type (Acc_Type, Derived_Type);
10491
10492                   Set_Etype (New_Id, Acc_Type);
10493                   Set_Scope (New_Id, New_Subp);
10494
10495                   --  Create a reference to it
10496
10497                   IR := Make_Itype_Reference (Sloc (Parent (Derived_Type)));
10498                   Set_Itype (IR, Acc_Type);
10499                   Insert_After (Parent (Derived_Type), IR);
10500
10501                else
10502                   Set_Etype (New_Id, Etype (Id));
10503                end if;
10504             end;
10505
10506          elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
10507            or else
10508              (Ekind (Etype (Id)) = E_Record_Type_With_Private
10509                and then Present (Full_View (Etype (Id)))
10510                and then
10511                  Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
10512          then
10513             --  Constraint checks on formals are generated during expansion,
10514             --  based on the signature of the original subprogram. The bounds
10515             --  of the derived type are not relevant, and thus we can use
10516             --  the base type for the formals. However, the return type may be
10517             --  used in a context that requires that the proper static bounds
10518             --  be used (a case statement, for example)  and for those cases
10519             --  we must use the derived type (first subtype), not its base.
10520
10521             --  If the derived_type_definition has no constraints, we know that
10522             --  the derived type has the same constraints as the first subtype
10523             --  of the parent, and we can also use it rather than its base,
10524             --  which can lead to more efficient code.
10525
10526             if Etype (Id) = Parent_Type then
10527                if Is_Scalar_Type (Parent_Type)
10528                  and then
10529                    Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
10530                then
10531                   Set_Etype (New_Id, Derived_Type);
10532
10533                elsif Nkind (Par) = N_Full_Type_Declaration
10534                  and then
10535                    Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
10536                  and then
10537                    Is_Entity_Name
10538                      (Subtype_Indication (Type_Definition (Par)))
10539                then
10540                   Set_Etype (New_Id, Derived_Type);
10541
10542                else
10543                   Set_Etype (New_Id, Base_Type (Derived_Type));
10544                end if;
10545
10546             else
10547                Set_Etype (New_Id, Base_Type (Derived_Type));
10548             end if;
10549
10550          --  Ada 2005 (AI-251): Handle derivations of abstract interface
10551          --  primitives.
10552
10553          elsif Is_Interface (Etype (Id))
10554            and then not Is_Class_Wide_Type (Etype (Id))
10555          then
10556             Set_Etype (New_Id, Derived_Type);
10557
10558          else
10559             Set_Etype (New_Id, Etype (Id));
10560          end if;
10561       end Replace_Type;
10562
10563       ----------------------
10564       -- Set_Derived_Name --
10565       ----------------------
10566
10567       procedure Set_Derived_Name is
10568          Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
10569       begin
10570          if Nm = TSS_Null then
10571             Set_Chars (New_Subp, Chars (Parent_Subp));
10572          else
10573             Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
10574          end if;
10575       end Set_Derived_Name;
10576
10577    --  Start of processing for Derive_Subprogram
10578
10579    begin
10580       New_Subp :=
10581          New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
10582       Set_Ekind (New_Subp, Ekind (Parent_Subp));
10583
10584       --  Check whether the inherited subprogram is a private operation that
10585       --  should be inherited but not yet made visible. Such subprograms can
10586       --  become visible at a later point (e.g., the private part of a public
10587       --  child unit) via Declare_Inherited_Private_Subprograms. If the
10588       --  following predicate is true, then this is not such a private
10589       --  operation and the subprogram simply inherits the name of the parent
10590       --  subprogram. Note the special check for the names of controlled
10591       --  operations, which are currently exempted from being inherited with
10592       --  a hidden name because they must be findable for generation of
10593       --  implicit run-time calls.
10594
10595       if not Is_Hidden (Parent_Subp)
10596         or else Is_Internal (Parent_Subp)
10597         or else Is_Private_Overriding
10598         or else Is_Internal_Name (Chars (Parent_Subp))
10599         or else Chars (Parent_Subp) = Name_Initialize
10600         or else Chars (Parent_Subp) = Name_Adjust
10601         or else Chars (Parent_Subp) = Name_Finalize
10602       then
10603          Set_Derived_Name;
10604
10605       --  If parent is hidden, this can be a regular derivation if the
10606       --  parent is immediately visible in a non-instantiating context,
10607       --  or if we are in the private part of an instance. This test
10608       --  should still be refined ???
10609
10610       --  The test for In_Instance_Not_Visible avoids inheriting the derived
10611       --  operation as a non-visible operation in cases where the parent
10612       --  subprogram might not be visible now, but was visible within the
10613       --  original generic, so it would be wrong to make the inherited
10614       --  subprogram non-visible now. (Not clear if this test is fully
10615       --  correct; are there any cases where we should declare the inherited
10616       --  operation as not visible to avoid it being overridden, e.g., when
10617       --  the parent type is a generic actual with private primitives ???)
10618
10619       --  (they should be treated the same as other private inherited
10620       --  subprograms, but it's not clear how to do this cleanly). ???
10621
10622       elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
10623               and then Is_Immediately_Visible (Parent_Subp)
10624               and then not In_Instance)
10625         or else In_Instance_Not_Visible
10626       then
10627          Set_Derived_Name;
10628
10629       --  Ada 2005 (AI-251): Hidden entity associated with abstract interface
10630       --  primitive
10631
10632       elsif Present (Abstract_Interface_Alias (Parent_Subp)) then
10633          Set_Derived_Name;
10634
10635       --  The type is inheriting a private operation, so enter
10636       --  it with a special name so it can't be overridden.
10637
10638       else
10639          Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
10640       end if;
10641
10642       Set_Parent (New_Subp, Parent (Derived_Type));
10643       Replace_Type (Parent_Subp, New_Subp);
10644       Conditional_Delay (New_Subp, Parent_Subp);
10645
10646       Formal := First_Formal (Parent_Subp);
10647       while Present (Formal) loop
10648          New_Formal := New_Copy (Formal);
10649
10650          --  Normally we do not go copying parents, but in the case of
10651          --  formals, we need to link up to the declaration (which is the
10652          --  parameter specification), and it is fine to link up to the
10653          --  original formal's parameter specification in this case.
10654
10655          Set_Parent (New_Formal, Parent (Formal));
10656
10657          Append_Entity (New_Formal, New_Subp);
10658
10659          Replace_Type (Formal, New_Formal);
10660          Next_Formal (Formal);
10661       end loop;
10662
10663       --  If this derivation corresponds to a tagged generic actual, then
10664       --  primitive operations rename those of the actual. Otherwise the
10665       --  primitive operations rename those of the parent type, If the
10666       --  parent renames an intrinsic operator, so does the new subprogram.
10667       --  We except concatenation, which is always properly typed, and does
10668       --  not get expanded as other intrinsic operations.
10669
10670       if No (Actual_Subp) then
10671          if Is_Intrinsic_Subprogram (Parent_Subp) then
10672             Set_Is_Intrinsic_Subprogram (New_Subp);
10673
10674             if Present (Alias (Parent_Subp))
10675               and then Chars (Parent_Subp) /= Name_Op_Concat
10676             then
10677                Set_Alias (New_Subp, Alias (Parent_Subp));
10678             else
10679                Set_Alias (New_Subp, Parent_Subp);
10680             end if;
10681
10682          else
10683             Set_Alias (New_Subp, Parent_Subp);
10684          end if;
10685
10686       else
10687          Set_Alias (New_Subp, Actual_Subp);
10688       end if;
10689
10690       --  Derived subprograms of a tagged type must inherit the convention
10691       --  of the parent subprogram (a requirement of AI-117). Derived
10692       --  subprograms of untagged types simply get convention Ada by default.
10693
10694       if Is_Tagged_Type (Derived_Type) then
10695          Set_Convention (New_Subp, Convention (Parent_Subp));
10696       end if;
10697
10698       Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
10699       Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
10700
10701       if Ekind (Parent_Subp) = E_Procedure then
10702          Set_Is_Valued_Procedure
10703            (New_Subp, Is_Valued_Procedure (Parent_Subp));
10704       end if;
10705
10706       --  No_Return must be inherited properly. If this is overridden in the
10707       --  case of a dispatching operation, then a check is made in Sem_Disp
10708       --  that the overriding operation is also No_Return (no such check is
10709       --  required for the case of non-dispatching operation.
10710
10711       Set_No_Return (New_Subp, No_Return (Parent_Subp));
10712
10713       --  A derived function with a controlling result is abstract. If the
10714       --  Derived_Type is a nonabstract formal generic derived type, then
10715       --  inherited operations are not abstract: the required check is done at
10716       --  instantiation time. If the derivation is for a generic actual, the
10717       --  function is not abstract unless the actual is.
10718
10719       if Is_Generic_Type (Derived_Type)
10720         and then not Is_Abstract (Derived_Type)
10721       then
10722          null;
10723
10724       elsif Is_Abstract (Alias (New_Subp))
10725         or else (Is_Tagged_Type (Derived_Type)
10726                    and then Etype (New_Subp) = Derived_Type
10727                    and then No (Actual_Subp))
10728       then
10729          Set_Is_Abstract (New_Subp);
10730
10731       --  Finally, if the parent type is abstract  we must verify that all
10732       --  inherited operations are either non-abstract or overridden, or
10733       --  that the derived type itself is abstract (this check is performed
10734       --  at the end of a package declaration, in Check_Abstract_Overriding).
10735       --  A private overriding in the parent type will not be visible in the
10736       --  derivation if we are not in an inner package or in a child unit of
10737       --  the parent type, in which case the abstractness of the inherited
10738       --  operation is carried to the new subprogram.
10739
10740       elsif Is_Abstract (Parent_Type)
10741         and then not In_Open_Scopes (Scope (Parent_Type))
10742         and then Is_Private_Overriding
10743         and then Is_Abstract (Visible_Subp)
10744       then
10745          Set_Alias (New_Subp, Visible_Subp);
10746          Set_Is_Abstract (New_Subp);
10747       end if;
10748
10749       New_Overloaded_Entity (New_Subp, Derived_Type);
10750
10751       --  Check for case of a derived subprogram for the instantiation of a
10752       --  formal derived tagged type, if so mark the subprogram as dispatching
10753       --  and inherit the dispatching attributes of the parent subprogram. The
10754       --  derived subprogram is effectively renaming of the actual subprogram,
10755       --  so it needs to have the same attributes as the actual.
10756
10757       if Present (Actual_Subp)
10758         and then Is_Dispatching_Operation (Parent_Subp)
10759       then
10760          Set_Is_Dispatching_Operation (New_Subp);
10761
10762          if Present (DTC_Entity (Parent_Subp)) then
10763             Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
10764             Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
10765          end if;
10766       end if;
10767
10768       --  Indicate that a derived subprogram does not require a body and that
10769       --  it does not require processing of default expressions.
10770
10771       Set_Has_Completion (New_Subp);
10772       Set_Default_Expressions_Processed (New_Subp);
10773
10774       if Ekind (New_Subp) = E_Function then
10775          Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
10776       end if;
10777    end Derive_Subprogram;
10778
10779    ------------------------
10780    -- Derive_Subprograms --
10781    ------------------------
10782
10783    procedure Derive_Subprograms
10784      (Parent_Type           : Entity_Id;
10785       Derived_Type          : Entity_Id;
10786       Generic_Actual        : Entity_Id := Empty)
10787    is
10788       Op_List      : constant Elist_Id :=
10789                        Collect_Primitive_Operations (Parent_Type);
10790       Ifaces_List  : constant Elist_Id := New_Elmt_List;
10791       Act_List     : Elist_Id;
10792       Act_Elmt     : Elmt_Id;
10793       Elmt         : Elmt_Id;
10794       New_Subp     : Entity_Id := Empty;
10795       Parent_Base  : Entity_Id;
10796       Subp         : Entity_Id;
10797
10798    begin
10799       if Ekind (Parent_Type) = E_Record_Type_With_Private
10800         and then Has_Discriminants (Parent_Type)
10801         and then Present (Full_View (Parent_Type))
10802       then
10803          Parent_Base := Full_View (Parent_Type);
10804       else
10805          Parent_Base := Parent_Type;
10806       end if;
10807
10808       --  Derive primitives inherited from the parent
10809
10810       if Present (Generic_Actual) then
10811          Act_List := Collect_Primitive_Operations (Generic_Actual);
10812          Act_Elmt := First_Elmt (Act_List);
10813       else
10814          Act_Elmt := No_Elmt;
10815       end if;
10816
10817       --  Literals are derived earlier in the process of building the derived
10818       --  type, and are skipped here.
10819
10820       Elmt := First_Elmt (Op_List);
10821       while Present (Elmt) loop
10822          Subp := Node (Elmt);
10823
10824          if Ekind (Subp) /= E_Enumeration_Literal then
10825
10826             if Ada_Version >= Ada_05
10827               and then Present (Abstract_Interface_Alias (Subp))
10828             then
10829                null;
10830
10831             elsif No (Generic_Actual) then
10832                Derive_Subprogram (New_Subp, Subp, Derived_Type, Parent_Base);
10833
10834                --  Ada 2005 (AI-251): Add the derivation of an abstract
10835                --  interface primitive to the list of entities to which
10836                --  we have to associate aliased entity.
10837
10838                if Ada_Version >= Ada_05
10839                  and then Is_Dispatching_Operation (Subp)
10840                  and then Present (Find_Dispatching_Type (Subp))
10841                  and then Is_Interface (Find_Dispatching_Type (Subp))
10842                  and then not Is_Predefined_Dispatching_Operation (Subp)
10843                then
10844                   Append_Elmt (New_Subp, Ifaces_List);
10845                end if;
10846
10847             else
10848                Derive_Subprogram
10849                  (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
10850                Next_Elmt (Act_Elmt);
10851             end if;
10852          end if;
10853
10854          Next_Elmt (Elmt);
10855       end loop;
10856
10857       Derive_Interface_Subprograms (Parent_Type, Derived_Type, Ifaces_List);
10858    end Derive_Subprograms;
10859
10860    --------------------------------
10861    -- Derived_Standard_Character --
10862    --------------------------------
10863
10864    procedure Derived_Standard_Character
10865      (N             : Node_Id;
10866       Parent_Type   : Entity_Id;
10867       Derived_Type  : Entity_Id)
10868    is
10869       Loc           : constant Source_Ptr := Sloc (N);
10870       Def           : constant Node_Id    := Type_Definition (N);
10871       Indic         : constant Node_Id    := Subtype_Indication (Def);
10872       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
10873       Implicit_Base : constant Entity_Id  :=
10874                         Create_Itype
10875                           (E_Enumeration_Type, N, Derived_Type, 'B');
10876
10877       Lo : Node_Id;
10878       Hi : Node_Id;
10879
10880    begin
10881       Discard_Node (Process_Subtype (Indic, N));
10882
10883       Set_Etype     (Implicit_Base, Parent_Base);
10884       Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
10885       Set_RM_Size   (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
10886
10887       Set_Is_Character_Type  (Implicit_Base, True);
10888       Set_Has_Delayed_Freeze (Implicit_Base);
10889
10890       --  The bounds of the implicit base are the bounds of the parent base.
10891       --  Note that their type is the parent base.
10892
10893       Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Base));
10894       Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
10895
10896       Set_Scalar_Range (Implicit_Base,
10897         Make_Range (Loc,
10898           Low_Bound  => Lo,
10899           High_Bound => Hi));
10900
10901       Conditional_Delay (Derived_Type, Parent_Type);
10902
10903       Set_Ekind (Derived_Type, E_Enumeration_Subtype);
10904       Set_Etype (Derived_Type, Implicit_Base);
10905       Set_Size_Info         (Derived_Type, Parent_Type);
10906
10907       if Unknown_RM_Size (Derived_Type) then
10908          Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
10909       end if;
10910
10911       Set_Is_Character_Type (Derived_Type, True);
10912
10913       if Nkind (Indic) /= N_Subtype_Indication then
10914
10915          --  If no explicit constraint, the bounds are those
10916          --  of the parent type.
10917
10918          Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Type));
10919          Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
10920          Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
10921       end if;
10922
10923       Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
10924
10925       --  Because the implicit base is used in the conversion of the bounds,
10926       --  we have to freeze it now. This is similar to what is done for
10927       --  numeric types, and it equally suspicious, but otherwise a non-
10928       --  static bound will have a reference to an unfrozen type, which is
10929       --  rejected by Gigi (???). This requires specific care for definition
10930       --  of stream attributes. For details, see comments at the end of
10931       --  Build_Derived_Numeric_Type.
10932
10933       Freeze_Before (N, Implicit_Base);
10934    end Derived_Standard_Character;
10935
10936    ------------------------------
10937    -- Derived_Type_Declaration --
10938    ------------------------------
10939
10940    procedure Derived_Type_Declaration
10941      (T             : Entity_Id;
10942       N             : Node_Id;
10943       Is_Completion : Boolean)
10944    is
10945       Def          : constant Node_Id := Type_Definition (N);
10946       Iface_Def    : Node_Id;
10947       Indic        : constant Node_Id := Subtype_Indication (Def);
10948       Extension    : constant Node_Id := Record_Extension_Part (Def);
10949       Parent_Type  : Entity_Id;
10950       Parent_Scope : Entity_Id;
10951       Taggd        : Boolean;
10952
10953       function Comes_From_Generic (Typ : Entity_Id) return Boolean;
10954       --  Check whether the parent type is a generic formal, or derives
10955       --  directly or indirectly from one.
10956
10957       ------------------------
10958       -- Comes_From_Generic --
10959       ------------------------
10960
10961       function Comes_From_Generic (Typ : Entity_Id) return Boolean is
10962       begin
10963          if Is_Generic_Type (Typ) then
10964             return True;
10965
10966          elsif Is_Generic_Type (Root_Type (Parent_Type)) then
10967             return True;
10968
10969          elsif Is_Private_Type (Typ)
10970            and then Present (Full_View (Typ))
10971            and then Is_Generic_Type (Root_Type (Full_View (Typ)))
10972          then
10973             return True;
10974
10975          elsif Is_Generic_Actual_Type (Typ) then
10976             return True;
10977
10978          else
10979             return False;
10980          end if;
10981       end Comes_From_Generic;
10982
10983    --  Start of processing for Derived_Type_Declaration
10984
10985    begin
10986       Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
10987
10988       --  Ada 2005 (AI-251): In case of interface derivation check that the
10989       --  parent is also an interface.
10990
10991       if Interface_Present (Def) then
10992          if not Is_Interface (Parent_Type) then
10993             Error_Msg_NE ("(Ada 2005) & must be an interface",
10994                           Indic, Parent_Type);
10995
10996          else
10997             Iface_Def := Type_Definition (Parent (Parent_Type));
10998
10999             --  Ada 2005 (AI-251): Limited interfaces can only inherit from
11000             --  other limited interfaces.
11001
11002             if Limited_Present (Def) then
11003                if Limited_Present (Iface_Def) then
11004                   null;
11005
11006                elsif Protected_Present (Iface_Def) then
11007                   Error_Msg_N ("(Ada 2005) limited interface cannot" &
11008                     " inherit from protected interface", Indic);
11009
11010                elsif Synchronized_Present (Iface_Def) then
11011                   Error_Msg_N ("(Ada 2005) limited interface cannot" &
11012                     " inherit from synchronized interface", Indic);
11013
11014                elsif Task_Present (Iface_Def) then
11015                   Error_Msg_N ("(Ada 2005) limited interface cannot" &
11016                     " inherit from task interface", Indic);
11017
11018                else
11019                   Error_Msg_N ("(Ada 2005) limited interface cannot" &
11020                     " inherit from non-limited interface", Indic);
11021                end if;
11022
11023             --  Ada 2005 (AI-345): Non-limited interfaces can only inherit
11024             --  from non-limited or limited interfaces.
11025
11026             elsif not Protected_Present (Def)
11027               and then not Synchronized_Present (Def)
11028               and then not Task_Present (Def)
11029             then
11030                if Limited_Present (Iface_Def) then
11031                   null;
11032
11033                elsif Protected_Present (Iface_Def) then
11034                   Error_Msg_N ("(Ada 2005) non-limited interface cannot" &
11035                     " inherit from protected interface", Indic);
11036
11037                elsif Synchronized_Present (Iface_Def) then
11038                   Error_Msg_N ("(Ada 2005) non-limited interface cannot" &
11039                     " inherit from synchronized interface", Indic);
11040
11041                elsif Task_Present (Iface_Def) then
11042                   Error_Msg_N ("(Ada 2005) non-limited interface cannot" &
11043                     " inherit from task interface", Indic);
11044
11045                else
11046                   null;
11047                end if;
11048             end if;
11049          end if;
11050       end if;
11051
11052       --  Ada 2005 (AI-251): Decorate all the names in the list of ancestor
11053       --  interfaces
11054
11055       if Is_Tagged_Type (Parent_Type)
11056         and then Is_Non_Empty_List (Interface_List (Def))
11057       then
11058          declare
11059             Intf : Node_Id;
11060             T    : Entity_Id;
11061
11062          begin
11063             Intf := First (Interface_List (Def));
11064             while Present (Intf) loop
11065                T := Find_Type_Of_Subtype_Indic (Intf);
11066
11067                if not Is_Interface (T) then
11068                   Error_Msg_NE ("(Ada 2005) & must be an interface", Intf, T);
11069
11070                elsif Limited_Present (Def)
11071                  and then not Is_Limited_Interface (T)
11072                then
11073                   Error_Msg_NE
11074                    ("progenitor interface& of limited type must be limited",
11075                      N, T);
11076                end if;
11077
11078                Next (Intf);
11079             end loop;
11080          end;
11081       end if;
11082
11083       if Parent_Type = Any_Type
11084         or else Etype (Parent_Type) = Any_Type
11085         or else (Is_Class_Wide_Type (Parent_Type)
11086                    and then Etype (Parent_Type) = T)
11087       then
11088          --  If Parent_Type is undefined or illegal, make new type into a
11089          --  subtype of Any_Type, and set a few attributes to prevent cascaded
11090          --  errors. If this is a self-definition, emit error now.
11091
11092          if T = Parent_Type
11093            or else T = Etype (Parent_Type)
11094          then
11095             Error_Msg_N ("type cannot be used in its own definition", Indic);
11096          end if;
11097
11098          Set_Ekind        (T, Ekind (Parent_Type));
11099          Set_Etype        (T, Any_Type);
11100          Set_Scalar_Range (T, Scalar_Range (Any_Type));
11101
11102          if Is_Tagged_Type (T) then
11103             Set_Primitive_Operations (T, New_Elmt_List);
11104          end if;
11105
11106          return;
11107       end if;
11108
11109       --  Ada 2005 (AI-251): The case in which the parent of the full-view is
11110       --  an interface is special because the list of interfaces in the full
11111       --  view can be given in any order. For example:
11112
11113       --     type A is interface;
11114       --     type B is interface and A;
11115       --     type D is new B with private;
11116       --   private
11117       --     type D is new A and B with null record; -- 1 --
11118
11119       --  In this case we perform the following transformation of -1-:
11120
11121       --     type D is new B and A with null record;
11122
11123       --  If the parent of the full-view covers the parent of the partial-view
11124       --  we have two possible cases:
11125
11126       --     1) They have the same parent
11127       --     2) The parent of the full-view implements some further interfaces
11128
11129       --  In both cases we do not need to perform the transformation. In the
11130       --  first case the source program is correct and the transformation is
11131       --  not needed; in the second case the source program does not fulfill
11132       --  the no-hidden interfaces rule (AI-396) and the error will be reported
11133       --  later.
11134
11135       --  This transformation not only simplifies the rest of the analysis of
11136       --  this type declaration but also simplifies the correct generation of
11137       --  the object layout to the expander.
11138
11139       if In_Private_Part (Current_Scope)
11140         and then Is_Interface (Parent_Type)
11141       then
11142          declare
11143             Iface               : Node_Id;
11144             Partial_View        : Entity_Id;
11145             Partial_View_Parent : Entity_Id;
11146             New_Iface           : Node_Id;
11147
11148          begin
11149             --  Look for the associated private type declaration
11150
11151             Partial_View := First_Entity (Current_Scope);
11152             loop
11153                exit when No (Partial_View)
11154                  or else (Has_Private_Declaration (Partial_View)
11155                            and then Full_View (Partial_View) = T);
11156
11157                Next_Entity (Partial_View);
11158             end loop;
11159
11160             --  If the partial view was not found then the source code has
11161             --  errors and the transformation is not needed.
11162
11163             if Present (Partial_View) then
11164                Partial_View_Parent := Etype (Partial_View);
11165
11166                --  If the parent of the full-view covers the parent of the
11167                --  partial-view we have nothing else to do.
11168
11169                if Interface_Present_In_Ancestor
11170                     (Parent_Type, Partial_View_Parent)
11171                then
11172                   null;
11173
11174                --  Traverse the list of interfaces of the full-view to look
11175                --  for the parent of the partial-view and perform the tree
11176                --  transformation.
11177
11178                else
11179                   Iface := First (Interface_List (Def));
11180                   while Present (Iface) loop
11181                      if Etype (Iface) = Etype (Partial_View) then
11182                         Rewrite (Subtype_Indication (Def),
11183                           New_Copy (Subtype_Indication
11184                                      (Parent (Partial_View))));
11185
11186                         New_Iface := Make_Identifier (Sloc (N),
11187                                        Chars (Parent_Type));
11188                         Append (New_Iface, Interface_List (Def));
11189
11190                         --  Analyze the transformed code
11191
11192                         Derived_Type_Declaration (T, N, Is_Completion);
11193                         return;
11194                      end if;
11195
11196                      Next (Iface);
11197                   end loop;
11198                end if;
11199             end if;
11200          end;
11201       end if;
11202
11203       --  Only composite types other than array types are allowed to have
11204       --  discriminants.
11205
11206       if Present (Discriminant_Specifications (N))
11207         and then (Is_Elementary_Type (Parent_Type)
11208                   or else Is_Array_Type (Parent_Type))
11209         and then not Error_Posted (N)
11210       then
11211          Error_Msg_N
11212            ("elementary or array type cannot have discriminants",
11213             Defining_Identifier (First (Discriminant_Specifications (N))));
11214          Set_Has_Discriminants (T, False);
11215       end if;
11216
11217       --  In Ada 83, a derived type defined in a package specification cannot
11218       --  be used for further derivation until the end of its visible part.
11219       --  Note that derivation in the private part of the package is allowed.
11220
11221       if Ada_Version = Ada_83
11222         and then Is_Derived_Type (Parent_Type)
11223         and then In_Visible_Part (Scope (Parent_Type))
11224       then
11225          if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
11226             Error_Msg_N
11227               ("(Ada 83): premature use of type for derivation", Indic);
11228          end if;
11229       end if;
11230
11231       --  Check for early use of incomplete or private type
11232
11233       if Ekind (Parent_Type) = E_Void
11234         or else Ekind (Parent_Type) = E_Incomplete_Type
11235       then
11236          Error_Msg_N ("premature derivation of incomplete type", Indic);
11237          return;
11238
11239       elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
11240               and then not Comes_From_Generic (Parent_Type))
11241         or else Has_Private_Component (Parent_Type)
11242       then
11243          --  The ancestor type of a formal type can be incomplete, in which
11244          --  case only the operations of the partial view are available in
11245          --  the generic. Subsequent checks may be required when the full
11246          --  view is analyzed, to verify that derivation from a tagged type
11247          --  has an extension.
11248
11249          if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
11250             null;
11251
11252          elsif No (Underlying_Type (Parent_Type))
11253            or else Has_Private_Component (Parent_Type)
11254          then
11255             Error_Msg_N
11256               ("premature derivation of derived or private type", Indic);
11257
11258             --  Flag the type itself as being in error, this prevents some
11259             --  nasty problems with subsequent uses of the malformed type.
11260
11261             Set_Error_Posted (T);
11262
11263          --  Check that within the immediate scope of an untagged partial
11264          --  view it's illegal to derive from the partial view if the
11265          --  full view is tagged. (7.3(7))
11266
11267          --  We verify that the Parent_Type is a partial view by checking
11268          --  that it is not a Full_Type_Declaration (i.e. a private type or
11269          --  private extension declaration), to distinguish a partial view
11270          --  from  a derivation from a private type which also appears as
11271          --  E_Private_Type.
11272
11273          elsif Present (Full_View (Parent_Type))
11274            and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
11275            and then not Is_Tagged_Type (Parent_Type)
11276            and then Is_Tagged_Type (Full_View (Parent_Type))
11277          then
11278             Parent_Scope := Scope (T);
11279             while Present (Parent_Scope)
11280               and then Parent_Scope /= Standard_Standard
11281             loop
11282                if Parent_Scope = Scope (Parent_Type) then
11283                   Error_Msg_N
11284                     ("premature derivation from type with tagged full view",
11285                      Indic);
11286                end if;
11287
11288                Parent_Scope := Scope (Parent_Scope);
11289             end loop;
11290          end if;
11291       end if;
11292
11293       --  Check that form of derivation is appropriate
11294
11295       Taggd := Is_Tagged_Type (Parent_Type);
11296
11297       --  Perhaps the parent type should be changed to the class-wide type's
11298       --  specific type in this case to prevent cascading errors ???
11299
11300       if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
11301          Error_Msg_N ("parent type must not be a class-wide type", Indic);
11302          return;
11303       end if;
11304
11305       if Present (Extension) and then not Taggd then
11306          Error_Msg_N
11307            ("type derived from untagged type cannot have extension", Indic);
11308
11309       elsif No (Extension) and then Taggd then
11310
11311          --  If this declaration is within a private part (or body) of a
11312          --  generic instantiation then the derivation is allowed (the parent
11313          --  type can only appear tagged in this case if it's a generic actual
11314          --  type, since it would otherwise have been rejected in the analysis
11315          --  of the generic template).
11316
11317          if not Is_Generic_Actual_Type (Parent_Type)
11318            or else In_Visible_Part (Scope (Parent_Type))
11319          then
11320             Error_Msg_N
11321               ("type derived from tagged type must have extension", Indic);
11322          end if;
11323       end if;
11324
11325       --  AI-443: Synchronized formal derived types require a private
11326       --  extension. There is no point in checking the ancestor type or
11327       --  the progenitors since the construct is wrong to begin with.
11328
11329       if Ada_Version >= Ada_05
11330         and then Is_Generic_Type (T)
11331         and then Present (Original_Node (N))
11332       then
11333          declare
11334             Decl : constant Node_Id := Original_Node (N);
11335
11336          begin
11337             if Nkind (Decl) = N_Formal_Type_Declaration
11338               and then Nkind (Formal_Type_Definition (Decl)) =
11339                          N_Formal_Derived_Type_Definition
11340               and then Synchronized_Present (Formal_Type_Definition (Decl))
11341               and then No (Extension)
11342
11343                --  Avoid emitting a duplicate error message
11344
11345               and then not Error_Posted (Indic)
11346             then
11347                Error_Msg_N
11348                  ("synchronized derived type must have extension", N);
11349             end if;
11350          end;
11351       end if;
11352
11353       Build_Derived_Type (N, Parent_Type, T, Is_Completion);
11354
11355       --  AI-419: The parent type of an explicitly limited derived type must
11356       --  be a limited type or a limited interface.
11357
11358       if Limited_Present (Def) then
11359          Set_Is_Limited_Record (T);
11360
11361          if Is_Interface (T) then
11362             Set_Is_Limited_Interface (T);
11363          end if;
11364
11365          if not Is_Limited_Type (Parent_Type)
11366            and then
11367              (not Is_Interface (Parent_Type)
11368                or else not Is_Limited_Interface (Parent_Type))
11369          then
11370             Error_Msg_NE ("parent type& of limited type must be limited",
11371               N, Parent_Type);
11372          end if;
11373       end if;
11374    end Derived_Type_Declaration;
11375
11376    ----------------------------------
11377    -- Enumeration_Type_Declaration --
11378    ----------------------------------
11379
11380    procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
11381       Ev     : Uint;
11382       L      : Node_Id;
11383       R_Node : Node_Id;
11384       B_Node : Node_Id;
11385
11386    begin
11387       --  Create identifier node representing lower bound
11388
11389       B_Node := New_Node (N_Identifier, Sloc (Def));
11390       L := First (Literals (Def));
11391       Set_Chars (B_Node, Chars (L));
11392       Set_Entity (B_Node,  L);
11393       Set_Etype (B_Node, T);
11394       Set_Is_Static_Expression (B_Node, True);
11395
11396       R_Node := New_Node (N_Range, Sloc (Def));
11397       Set_Low_Bound  (R_Node, B_Node);
11398
11399       Set_Ekind (T, E_Enumeration_Type);
11400       Set_First_Literal (T, L);
11401       Set_Etype (T, T);
11402       Set_Is_Constrained (T);
11403
11404       Ev := Uint_0;
11405
11406       --  Loop through literals of enumeration type setting pos and rep values
11407       --  except that if the Ekind is already set, then it means that the
11408       --  literal was already constructed (case of a derived type declaration
11409       --  and we should not disturb the Pos and Rep values.
11410
11411       while Present (L) loop
11412          if Ekind (L) /= E_Enumeration_Literal then
11413             Set_Ekind (L, E_Enumeration_Literal);
11414             Set_Enumeration_Pos (L, Ev);
11415             Set_Enumeration_Rep (L, Ev);
11416             Set_Is_Known_Valid  (L, True);
11417          end if;
11418
11419          Set_Etype (L, T);
11420          New_Overloaded_Entity (L);
11421          Generate_Definition (L);
11422          Set_Convention (L, Convention_Intrinsic);
11423
11424          if Nkind (L) = N_Defining_Character_Literal then
11425             Set_Is_Character_Type (T, True);
11426          end if;
11427
11428          Ev := Ev + 1;
11429          Next (L);
11430       end loop;
11431
11432       --  Now create a node representing upper bound
11433
11434       B_Node := New_Node (N_Identifier, Sloc (Def));
11435       Set_Chars (B_Node, Chars (Last (Literals (Def))));
11436       Set_Entity (B_Node,  Last (Literals (Def)));
11437       Set_Etype (B_Node, T);
11438       Set_Is_Static_Expression (B_Node, True);
11439
11440       Set_High_Bound (R_Node, B_Node);
11441       Set_Scalar_Range (T, R_Node);
11442       Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
11443       Set_Enum_Esize (T);
11444
11445       --  Set Discard_Names if configuration pragma set, or if there is
11446       --  a parameterless pragma in the current declarative region
11447
11448       if Global_Discard_Names
11449         or else Discard_Names (Scope (T))
11450       then
11451          Set_Discard_Names (T);
11452       end if;
11453
11454       --  Process end label if there is one
11455
11456       if Present (Def) then
11457          Process_End_Label (Def, 'e', T);
11458       end if;
11459    end Enumeration_Type_Declaration;
11460
11461    ---------------------------------
11462    -- Expand_To_Stored_Constraint --
11463    ---------------------------------
11464
11465    function Expand_To_Stored_Constraint
11466      (Typ        : Entity_Id;
11467       Constraint : Elist_Id) return Elist_Id
11468    is
11469       Explicitly_Discriminated_Type : Entity_Id;
11470       Expansion    : Elist_Id;
11471       Discriminant : Entity_Id;
11472
11473       function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
11474       --  Find the nearest type that actually specifies discriminants
11475
11476       ---------------------------------
11477       -- Type_With_Explicit_Discrims --
11478       ---------------------------------
11479
11480       function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
11481          Typ : constant E := Base_Type (Id);
11482
11483       begin
11484          if Ekind (Typ) in Incomplete_Or_Private_Kind then
11485             if Present (Full_View (Typ)) then
11486                return Type_With_Explicit_Discrims (Full_View (Typ));
11487             end if;
11488
11489          else
11490             if Has_Discriminants (Typ) then
11491                return Typ;
11492             end if;
11493          end if;
11494
11495          if Etype (Typ) = Typ then
11496             return Empty;
11497          elsif Has_Discriminants (Typ) then
11498             return Typ;
11499          else
11500             return Type_With_Explicit_Discrims (Etype (Typ));
11501          end if;
11502
11503       end Type_With_Explicit_Discrims;
11504
11505    --  Start of processing for Expand_To_Stored_Constraint
11506
11507    begin
11508       if No (Constraint)
11509         or else Is_Empty_Elmt_List (Constraint)
11510       then
11511          return No_Elist;
11512       end if;
11513
11514       Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
11515
11516       if No (Explicitly_Discriminated_Type) then
11517          return No_Elist;
11518       end if;
11519
11520       Expansion := New_Elmt_List;
11521
11522       Discriminant :=
11523          First_Stored_Discriminant (Explicitly_Discriminated_Type);
11524       while Present (Discriminant) loop
11525          Append_Elmt (
11526            Get_Discriminant_Value (
11527              Discriminant, Explicitly_Discriminated_Type, Constraint),
11528            Expansion);
11529          Next_Stored_Discriminant (Discriminant);
11530       end loop;
11531
11532       return Expansion;
11533    end Expand_To_Stored_Constraint;
11534
11535    --------------------
11536    -- Find_Type_Name --
11537    --------------------
11538
11539    function Find_Type_Name (N : Node_Id) return Entity_Id is
11540       Id       : constant Entity_Id := Defining_Identifier (N);
11541       Prev     : Entity_Id;
11542       New_Id   : Entity_Id;
11543       Prev_Par : Node_Id;
11544
11545    begin
11546       --  Find incomplete declaration, if one was given
11547
11548       Prev := Current_Entity_In_Scope (Id);
11549
11550       if Present (Prev) then
11551
11552          --  Previous declaration exists. Error if not incomplete/private case
11553          --  except if previous declaration is implicit, etc. Enter_Name will
11554          --  emit error if appropriate.
11555
11556          Prev_Par := Parent (Prev);
11557
11558          if not Is_Incomplete_Or_Private_Type (Prev) then
11559             Enter_Name (Id);
11560             New_Id := Id;
11561
11562          elsif Nkind (N) /= N_Full_Type_Declaration
11563            and then Nkind (N) /= N_Task_Type_Declaration
11564            and then Nkind (N) /= N_Protected_Type_Declaration
11565          then
11566             --  Completion must be a full type declarations (RM 7.3(4))
11567
11568             Error_Msg_Sloc := Sloc (Prev);
11569             Error_Msg_NE ("invalid completion of }", Id, Prev);
11570
11571             --  Set scope of Id to avoid cascaded errors. Entity is never
11572             --  examined again, except when saving globals in generics.
11573
11574             Set_Scope (Id, Current_Scope);
11575             New_Id := Id;
11576
11577          --  Case of full declaration of incomplete type
11578
11579          elsif Ekind (Prev) = E_Incomplete_Type then
11580
11581             --  Indicate that the incomplete declaration has a matching full
11582             --  declaration. The defining occurrence of the incomplete
11583             --  declaration remains the visible one, and the procedure
11584             --  Get_Full_View dereferences it whenever the type is used.
11585
11586             if Present (Full_View (Prev)) then
11587                Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
11588             end if;
11589
11590             Set_Full_View (Prev,  Id);
11591             Append_Entity (Id, Current_Scope);
11592             Set_Is_Public (Id, Is_Public (Prev));
11593             Set_Is_Internal (Id);
11594             New_Id := Prev;
11595
11596          --  Case of full declaration of private type
11597
11598          else
11599             if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
11600                if Etype (Prev) /= Prev then
11601
11602                   --  Prev is a private subtype or a derived type, and needs
11603                   --  no completion.
11604
11605                   Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
11606                   New_Id := Id;
11607
11608                elsif Ekind (Prev) = E_Private_Type
11609                  and then
11610                    (Nkind (N) = N_Task_Type_Declaration
11611                      or else Nkind (N) = N_Protected_Type_Declaration)
11612                then
11613                   Error_Msg_N
11614                    ("completion of nonlimited type cannot be limited", N);
11615
11616                elsif Ekind (Prev) = E_Record_Type_With_Private
11617                  and then
11618                    (Nkind (N) = N_Task_Type_Declaration
11619                      or else Nkind (N) = N_Protected_Type_Declaration)
11620                then
11621                   if not Is_Limited_Record (Prev) then
11622                      Error_Msg_N
11623                         ("completion of nonlimited type cannot be limited", N);
11624
11625                   elsif No (Interface_List (N)) then
11626                      Error_Msg_N
11627                         ("completion of tagged private type must be tagged",
11628                            N);
11629                   end if;
11630                end if;
11631
11632             --  Ada 2005 (AI-251): Private extension declaration of a
11633             --  task type. This case arises with tasks implementing interfaces
11634
11635             elsif Nkind (N) = N_Task_Type_Declaration
11636               or else Nkind (N) = N_Protected_Type_Declaration
11637             then
11638                null;
11639
11640             elsif Nkind (N) /= N_Full_Type_Declaration
11641               or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
11642             then
11643                Error_Msg_N
11644                  ("full view of private extension must be an extension", N);
11645
11646             elsif not (Abstract_Present (Parent (Prev)))
11647               and then Abstract_Present (Type_Definition (N))
11648             then
11649                Error_Msg_N
11650                  ("full view of non-abstract extension cannot be abstract", N);
11651             end if;
11652
11653             if not In_Private_Part (Current_Scope) then
11654                Error_Msg_N
11655                  ("declaration of full view must appear in private part", N);
11656             end if;
11657
11658             Copy_And_Swap (Prev, Id);
11659             Set_Has_Private_Declaration (Prev);
11660             Set_Has_Private_Declaration (Id);
11661
11662             --  If no error, propagate freeze_node from private to full view.
11663             --  It may have been generated for an early operational item.
11664
11665             if Present (Freeze_Node (Id))
11666               and then Serious_Errors_Detected = 0
11667               and then No (Full_View (Id))
11668             then
11669                Set_Freeze_Node (Prev, Freeze_Node (Id));
11670                Set_Freeze_Node (Id, Empty);
11671                Set_First_Rep_Item (Prev, First_Rep_Item (Id));
11672             end if;
11673
11674             Set_Full_View (Id, Prev);
11675             New_Id := Prev;
11676          end if;
11677
11678          --  Verify that full declaration conforms to incomplete one
11679
11680          if Is_Incomplete_Or_Private_Type (Prev)
11681            and then Present (Discriminant_Specifications (Prev_Par))
11682          then
11683             if Present (Discriminant_Specifications (N)) then
11684                if Ekind (Prev) = E_Incomplete_Type then
11685                   Check_Discriminant_Conformance (N, Prev, Prev);
11686                else
11687                   Check_Discriminant_Conformance (N, Prev, Id);
11688                end if;
11689
11690             else
11691                Error_Msg_N
11692                  ("missing discriminants in full type declaration", N);
11693
11694                --  To avoid cascaded errors on subsequent use, share the
11695                --  discriminants of the partial view.
11696
11697                Set_Discriminant_Specifications (N,
11698                  Discriminant_Specifications (Prev_Par));
11699             end if;
11700          end if;
11701
11702          --  A prior untagged private type can have an associated class-wide
11703          --  type due to use of the class attribute, and in this case also the
11704          --  full type is required to be tagged.
11705
11706          if Is_Type (Prev)
11707            and then (Is_Tagged_Type (Prev)
11708                       or else Present (Class_Wide_Type (Prev)))
11709            and then (Nkind (N) /= N_Task_Type_Declaration
11710                       and then Nkind (N) /= N_Protected_Type_Declaration)
11711          then
11712             --  The full declaration is either a tagged record or an
11713             --  extension otherwise this is an error
11714
11715             if Nkind (Type_Definition (N)) = N_Record_Definition then
11716                if not Tagged_Present (Type_Definition (N)) then
11717                   Error_Msg_NE
11718                     ("full declaration of } must be tagged", Prev, Id);
11719                   Set_Is_Tagged_Type (Id);
11720                   Set_Primitive_Operations (Id, New_Elmt_List);
11721                end if;
11722
11723             elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
11724                if No (Record_Extension_Part (Type_Definition (N))) then
11725                   Error_Msg_NE (
11726                     "full declaration of } must be a record extension",
11727                     Prev, Id);
11728                   Set_Is_Tagged_Type (Id);
11729                   Set_Primitive_Operations (Id, New_Elmt_List);
11730                end if;
11731
11732             else
11733                Error_Msg_NE
11734                  ("full declaration of } must be a tagged type", Prev, Id);
11735
11736             end if;
11737          end if;
11738
11739          return New_Id;
11740
11741       else
11742          --  New type declaration
11743
11744          Enter_Name (Id);
11745          return Id;
11746       end if;
11747    end Find_Type_Name;
11748
11749    -------------------------
11750    -- Find_Type_Of_Object --
11751    -------------------------
11752
11753    function Find_Type_Of_Object
11754      (Obj_Def     : Node_Id;
11755       Related_Nod : Node_Id) return Entity_Id
11756    is
11757       Def_Kind : constant Node_Kind := Nkind (Obj_Def);
11758       P        : Node_Id := Parent (Obj_Def);
11759       T        : Entity_Id;
11760       Nam      : Name_Id;
11761
11762    begin
11763       --  If the parent is a component_definition node we climb to the
11764       --  component_declaration node
11765
11766       if Nkind (P) = N_Component_Definition then
11767          P := Parent (P);
11768       end if;
11769
11770       --  Case of an anonymous array subtype
11771
11772       if Def_Kind = N_Constrained_Array_Definition
11773         or else Def_Kind = N_Unconstrained_Array_Definition
11774       then
11775          T := Empty;
11776          Array_Type_Declaration (T, Obj_Def);
11777
11778       --  Create an explicit subtype whenever possible
11779
11780       elsif Nkind (P) /= N_Component_Declaration
11781         and then Def_Kind = N_Subtype_Indication
11782       then
11783          --  Base name of subtype on object name, which will be unique in
11784          --  the current scope.
11785
11786          --  If this is a duplicate declaration, return base type, to avoid
11787          --  generating duplicate anonymous types.
11788
11789          if Error_Posted (P) then
11790             Analyze (Subtype_Mark (Obj_Def));
11791             return Entity (Subtype_Mark (Obj_Def));
11792          end if;
11793
11794          Nam :=
11795             New_External_Name
11796              (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
11797
11798          T := Make_Defining_Identifier (Sloc (P), Nam);
11799
11800          Insert_Action (Obj_Def,
11801            Make_Subtype_Declaration (Sloc (P),
11802              Defining_Identifier => T,
11803              Subtype_Indication  => Relocate_Node (Obj_Def)));
11804
11805          --  This subtype may need freezing, and this will not be done
11806          --  automatically if the object declaration is not in declarative
11807          --  part. Since this is an object declaration, the type cannot always
11808          --  be frozen here. Deferred constants do not freeze their type
11809          --  (which often enough will be private).
11810
11811          if Nkind (P) = N_Object_Declaration
11812            and then Constant_Present (P)
11813            and then No (Expression (P))
11814          then
11815             null;
11816          else
11817             Insert_Actions (Obj_Def, Freeze_Entity (T, Sloc (P)));
11818          end if;
11819
11820       --  Ada 2005 AI-406: the object definition in an object declaration
11821       --  can be an access definition.
11822
11823       elsif Def_Kind = N_Access_Definition then
11824          T := Access_Definition (Related_Nod, Obj_Def);
11825
11826          if Nkind (Parent (Related_Nod)) /= N_Extended_Return_Statement then
11827             Set_Is_Local_Anonymous_Access (T);
11828          end if;
11829
11830       --  Otherwise, the object definition is just a subtype_mark
11831
11832       else
11833          T := Process_Subtype (Obj_Def, Related_Nod);
11834       end if;
11835
11836       return T;
11837    end Find_Type_Of_Object;
11838
11839    --------------------------------
11840    -- Find_Type_Of_Subtype_Indic --
11841    --------------------------------
11842
11843    function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
11844       Typ : Entity_Id;
11845
11846    begin
11847       --  Case of subtype mark with a constraint
11848
11849       if Nkind (S) = N_Subtype_Indication then
11850          Find_Type (Subtype_Mark (S));
11851          Typ := Entity (Subtype_Mark (S));
11852
11853          if not
11854            Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
11855          then
11856             Error_Msg_N
11857               ("incorrect constraint for this kind of type", Constraint (S));
11858             Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
11859          end if;
11860
11861       --  Otherwise we have a subtype mark without a constraint
11862
11863       elsif Error_Posted (S) then
11864          Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
11865          return Any_Type;
11866
11867       else
11868          Find_Type (S);
11869          Typ := Entity (S);
11870       end if;
11871
11872       if Typ = Standard_Wide_Character
11873         or else Typ = Standard_Wide_Wide_Character
11874         or else Typ = Standard_Wide_String
11875         or else Typ = Standard_Wide_Wide_String
11876       then
11877          Check_Restriction (No_Wide_Characters, S);
11878       end if;
11879
11880       return Typ;
11881    end Find_Type_Of_Subtype_Indic;
11882
11883    -------------------------------------
11884    -- Floating_Point_Type_Declaration --
11885    -------------------------------------
11886
11887    procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
11888       Digs          : constant Node_Id := Digits_Expression (Def);
11889       Digs_Val      : Uint;
11890       Base_Typ      : Entity_Id;
11891       Implicit_Base : Entity_Id;
11892       Bound         : Node_Id;
11893
11894       function Can_Derive_From (E : Entity_Id) return Boolean;
11895       --  Find if given digits value allows derivation from specified type
11896
11897       ---------------------
11898       -- Can_Derive_From --
11899       ---------------------
11900
11901       function Can_Derive_From (E : Entity_Id) return Boolean is
11902          Spec : constant Entity_Id := Real_Range_Specification (Def);
11903
11904       begin
11905          if Digs_Val > Digits_Value (E) then
11906             return False;
11907          end if;
11908
11909          if Present (Spec) then
11910             if Expr_Value_R (Type_Low_Bound (E)) >
11911                Expr_Value_R (Low_Bound (Spec))
11912             then
11913                return False;
11914             end if;
11915
11916             if Expr_Value_R (Type_High_Bound (E)) <
11917                Expr_Value_R (High_Bound (Spec))
11918             then
11919                return False;
11920             end if;
11921          end if;
11922
11923          return True;
11924       end Can_Derive_From;
11925
11926    --  Start of processing for Floating_Point_Type_Declaration
11927
11928    begin
11929       Check_Restriction (No_Floating_Point, Def);
11930
11931       --  Create an implicit base type
11932
11933       Implicit_Base :=
11934         Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
11935
11936       --  Analyze and verify digits value
11937
11938       Analyze_And_Resolve (Digs, Any_Integer);
11939       Check_Digits_Expression (Digs);
11940       Digs_Val := Expr_Value (Digs);
11941
11942       --  Process possible range spec and find correct type to derive from
11943
11944       Process_Real_Range_Specification (Def);
11945
11946       if Can_Derive_From (Standard_Short_Float) then
11947          Base_Typ := Standard_Short_Float;
11948       elsif Can_Derive_From (Standard_Float) then
11949          Base_Typ := Standard_Float;
11950       elsif Can_Derive_From (Standard_Long_Float) then
11951          Base_Typ := Standard_Long_Float;
11952       elsif Can_Derive_From (Standard_Long_Long_Float) then
11953          Base_Typ := Standard_Long_Long_Float;
11954
11955       --  If we can't derive from any existing type, use long_long_float
11956       --  and give appropriate message explaining the problem.
11957
11958       else
11959          Base_Typ := Standard_Long_Long_Float;
11960
11961          if Digs_Val >= Digits_Value (Standard_Long_Long_Float) then
11962             Error_Msg_Uint_1 := Digits_Value (Standard_Long_Long_Float);
11963             Error_Msg_N ("digits value out of range, maximum is ^", Digs);
11964
11965          else
11966             Error_Msg_N
11967               ("range too large for any predefined type",
11968                Real_Range_Specification (Def));
11969          end if;
11970       end if;
11971
11972       --  If there are bounds given in the declaration use them as the bounds
11973       --  of the type, otherwise use the bounds of the predefined base type
11974       --  that was chosen based on the Digits value.
11975
11976       if Present (Real_Range_Specification (Def)) then
11977          Set_Scalar_Range (T, Real_Range_Specification (Def));
11978          Set_Is_Constrained (T);
11979
11980          --  The bounds of this range must be converted to machine numbers
11981          --  in accordance with RM 4.9(38).
11982
11983          Bound := Type_Low_Bound (T);
11984
11985          if Nkind (Bound) = N_Real_Literal then
11986             Set_Realval
11987               (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
11988             Set_Is_Machine_Number (Bound);
11989          end if;
11990
11991          Bound := Type_High_Bound (T);
11992
11993          if Nkind (Bound) = N_Real_Literal then
11994             Set_Realval
11995               (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
11996             Set_Is_Machine_Number (Bound);
11997          end if;
11998
11999       else
12000          Set_Scalar_Range (T, Scalar_Range (Base_Typ));
12001       end if;
12002
12003       --  Complete definition of implicit base and declared first subtype
12004
12005       Set_Etype          (Implicit_Base, Base_Typ);
12006
12007       Set_Scalar_Range   (Implicit_Base, Scalar_Range   (Base_Typ));
12008       Set_Size_Info      (Implicit_Base,                (Base_Typ));
12009       Set_RM_Size        (Implicit_Base, RM_Size        (Base_Typ));
12010       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
12011       Set_Digits_Value   (Implicit_Base, Digits_Value   (Base_Typ));
12012       Set_Vax_Float      (Implicit_Base, Vax_Float      (Base_Typ));
12013
12014       Set_Ekind          (T, E_Floating_Point_Subtype);
12015       Set_Etype          (T, Implicit_Base);
12016
12017       Set_Size_Info      (T,                (Implicit_Base));
12018       Set_RM_Size        (T, RM_Size        (Implicit_Base));
12019       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
12020       Set_Digits_Value   (T, Digs_Val);
12021    end Floating_Point_Type_Declaration;
12022
12023    ----------------------------
12024    -- Get_Discriminant_Value --
12025    ----------------------------
12026
12027    --  This is the situation:
12028
12029    --  There is a non-derived type
12030
12031    --       type T0 (Dx, Dy, Dz...)
12032
12033    --  There are zero or more levels of derivation, with each derivation
12034    --  either purely inheriting the discriminants, or defining its own.
12035
12036    --       type Ti      is new Ti-1
12037    --  or
12038    --       type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
12039    --  or
12040    --       subtype Ti is ...
12041
12042    --  The subtype issue is avoided by the use of Original_Record_Component,
12043    --  and the fact that derived subtypes also derive the constraints.
12044
12045    --  This chain leads back from
12046
12047    --       Typ_For_Constraint
12048
12049    --  Typ_For_Constraint has discriminants, and the value for each
12050    --  discriminant is given by its corresponding Elmt of Constraints.
12051
12052    --  Discriminant is some discriminant in this hierarchy
12053
12054    --  We need to return its value
12055
12056    --  We do this by recursively searching each level, and looking for
12057    --  Discriminant. Once we get to the bottom, we start backing up
12058    --  returning the value for it which may in turn be a discriminant
12059    --  further up, so on the backup we continue the substitution.
12060
12061    function Get_Discriminant_Value
12062      (Discriminant       : Entity_Id;
12063       Typ_For_Constraint : Entity_Id;
12064       Constraint         : Elist_Id) return Node_Id
12065    is
12066       function Search_Derivation_Levels
12067         (Ti                    : Entity_Id;
12068          Discrim_Values        : Elist_Id;
12069          Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
12070       --  This is the routine that performs the recursive search of levels
12071       --  as described above.
12072
12073       ------------------------------
12074       -- Search_Derivation_Levels --
12075       ------------------------------
12076
12077       function Search_Derivation_Levels
12078         (Ti                    : Entity_Id;
12079          Discrim_Values        : Elist_Id;
12080          Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
12081       is
12082          Assoc          : Elmt_Id;
12083          Disc           : Entity_Id;
12084          Result         : Node_Or_Entity_Id;
12085          Result_Entity  : Node_Id;
12086
12087       begin
12088          --  If inappropriate type, return Error, this happens only in
12089          --  cascaded error situations, and we want to avoid a blow up.
12090
12091          if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
12092             return Error;
12093          end if;
12094
12095          --  Look deeper if possible. Use Stored_Constraints only for
12096          --  untagged types. For tagged types use the given constraint.
12097          --  This asymmetry needs explanation???
12098
12099          if not Stored_Discrim_Values
12100            and then Present (Stored_Constraint (Ti))
12101            and then not Is_Tagged_Type (Ti)
12102          then
12103             Result :=
12104               Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
12105          else
12106             declare
12107                Td : constant Entity_Id := Etype (Ti);
12108
12109             begin
12110                if Td = Ti then
12111                   Result := Discriminant;
12112
12113                else
12114                   if Present (Stored_Constraint (Ti)) then
12115                      Result :=
12116                         Search_Derivation_Levels
12117                           (Td, Stored_Constraint (Ti), True);
12118                   else
12119                      Result :=
12120                         Search_Derivation_Levels
12121                           (Td, Discrim_Values, Stored_Discrim_Values);
12122                   end if;
12123                end if;
12124             end;
12125          end if;
12126
12127          --  Extra underlying places to search, if not found above. For
12128          --  concurrent types, the relevant discriminant appears in the
12129          --  corresponding record. For a type derived from a private type
12130          --  without discriminant, the full view inherits the discriminants
12131          --  of the full view of the parent.
12132
12133          if Result = Discriminant then
12134             if Is_Concurrent_Type (Ti)
12135               and then Present (Corresponding_Record_Type (Ti))
12136             then
12137                Result :=
12138                  Search_Derivation_Levels (
12139                    Corresponding_Record_Type (Ti),
12140                    Discrim_Values,
12141                    Stored_Discrim_Values);
12142
12143             elsif Is_Private_Type (Ti)
12144               and then not Has_Discriminants (Ti)
12145               and then Present (Full_View (Ti))
12146               and then Etype (Full_View (Ti)) /= Ti
12147             then
12148                Result :=
12149                  Search_Derivation_Levels (
12150                    Full_View (Ti),
12151                    Discrim_Values,
12152                    Stored_Discrim_Values);
12153             end if;
12154          end if;
12155
12156          --  If Result is not a (reference to a) discriminant, return it,
12157          --  otherwise set Result_Entity to the discriminant.
12158
12159          if Nkind (Result) = N_Defining_Identifier then
12160             pragma Assert (Result = Discriminant);
12161             Result_Entity := Result;
12162
12163          else
12164             if not Denotes_Discriminant (Result) then
12165                return Result;
12166             end if;
12167
12168             Result_Entity := Entity (Result);
12169          end if;
12170
12171          --  See if this level of derivation actually has discriminants
12172          --  because tagged derivations can add them, hence the lower
12173          --  levels need not have any.
12174
12175          if not Has_Discriminants (Ti) then
12176             return Result;
12177          end if;
12178
12179          --  Scan Ti's discriminants for Result_Entity,
12180          --  and return its corresponding value, if any.
12181
12182          Result_Entity := Original_Record_Component (Result_Entity);
12183
12184          Assoc := First_Elmt (Discrim_Values);
12185
12186          if Stored_Discrim_Values then
12187             Disc := First_Stored_Discriminant (Ti);
12188          else
12189             Disc := First_Discriminant (Ti);
12190          end if;
12191
12192          while Present (Disc) loop
12193             pragma Assert (Present (Assoc));
12194
12195             if Original_Record_Component (Disc) = Result_Entity then
12196                return Node (Assoc);
12197             end if;
12198
12199             Next_Elmt (Assoc);
12200
12201             if Stored_Discrim_Values then
12202                Next_Stored_Discriminant (Disc);
12203             else
12204                Next_Discriminant (Disc);
12205             end if;
12206          end loop;
12207
12208          --  Could not find it
12209          --
12210          return Result;
12211       end Search_Derivation_Levels;
12212
12213       Result : Node_Or_Entity_Id;
12214
12215    --  Start of processing for Get_Discriminant_Value
12216
12217    begin
12218       --  ??? This routine is a gigantic mess and will be deleted. For the
12219       --  time being just test for the trivial case before calling recurse.
12220
12221       if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
12222          declare
12223             D : Entity_Id;
12224             E : Elmt_Id;
12225
12226          begin
12227             D := First_Discriminant (Typ_For_Constraint);
12228             E := First_Elmt (Constraint);
12229             while Present (D) loop
12230                if Chars (D) = Chars (Discriminant) then
12231                   return Node (E);
12232                end if;
12233
12234                Next_Discriminant (D);
12235                Next_Elmt (E);
12236             end loop;
12237          end;
12238       end if;
12239
12240       Result := Search_Derivation_Levels
12241         (Typ_For_Constraint, Constraint, False);
12242
12243       --  ??? hack to disappear when this routine is gone
12244
12245       if  Nkind (Result) = N_Defining_Identifier then
12246          declare
12247             D : Entity_Id;
12248             E : Elmt_Id;
12249
12250          begin
12251             D := First_Discriminant (Typ_For_Constraint);
12252             E := First_Elmt (Constraint);
12253             while Present (D) loop
12254                if Corresponding_Discriminant (D) = Discriminant then
12255                   return Node (E);
12256                end if;
12257
12258                Next_Discriminant (D);
12259                Next_Elmt (E);
12260             end loop;
12261          end;
12262       end if;
12263
12264       pragma Assert (Nkind (Result) /= N_Defining_Identifier);
12265       return Result;
12266    end Get_Discriminant_Value;
12267
12268    --------------------------
12269    -- Has_Range_Constraint --
12270    --------------------------
12271
12272    function Has_Range_Constraint (N : Node_Id) return Boolean is
12273       C : constant Node_Id := Constraint (N);
12274
12275    begin
12276       if Nkind (C) = N_Range_Constraint then
12277          return True;
12278
12279       elsif Nkind (C) = N_Digits_Constraint then
12280          return
12281             Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
12282               or else
12283             Present (Range_Constraint (C));
12284
12285       elsif Nkind (C) = N_Delta_Constraint then
12286          return Present (Range_Constraint (C));
12287
12288       else
12289          return False;
12290       end if;
12291    end Has_Range_Constraint;
12292
12293    ------------------------
12294    -- Inherit_Components --
12295    ------------------------
12296
12297    function Inherit_Components
12298      (N             : Node_Id;
12299       Parent_Base   : Entity_Id;
12300       Derived_Base  : Entity_Id;
12301       Is_Tagged     : Boolean;
12302       Inherit_Discr : Boolean;
12303       Discs         : Elist_Id) return Elist_Id
12304    is
12305       Assoc_List : constant Elist_Id := New_Elmt_List;
12306
12307       procedure Inherit_Component
12308         (Old_C          : Entity_Id;
12309          Plain_Discrim  : Boolean := False;
12310          Stored_Discrim : Boolean := False);
12311       --  Inherits component Old_C from Parent_Base to the Derived_Base. If
12312       --  Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
12313       --  True, Old_C is a stored discriminant. If they are both false then
12314       --  Old_C is a regular component.
12315
12316       -----------------------
12317       -- Inherit_Component --
12318       -----------------------
12319
12320       procedure Inherit_Component
12321         (Old_C          : Entity_Id;
12322          Plain_Discrim  : Boolean := False;
12323          Stored_Discrim : Boolean := False)
12324       is
12325          New_C : constant Entity_Id := New_Copy (Old_C);
12326
12327          Discrim      : Entity_Id;
12328          Corr_Discrim : Entity_Id;
12329
12330       begin
12331          pragma Assert (not Is_Tagged or else not Stored_Discrim);
12332
12333          Set_Parent (New_C, Parent (Old_C));
12334
12335          --  Regular discriminants and components must be inserted in the scope
12336          --  of the Derived_Base. Do it here.
12337
12338          if not Stored_Discrim then
12339             Enter_Name (New_C);
12340          end if;
12341
12342          --  For tagged types the Original_Record_Component must point to
12343          --  whatever this field was pointing to in the parent type. This has
12344          --  already been achieved by the call to New_Copy above.
12345
12346          if not Is_Tagged then
12347             Set_Original_Record_Component (New_C, New_C);
12348          end if;
12349
12350          --  If we have inherited a component then see if its Etype contains
12351          --  references to Parent_Base discriminants. In this case, replace
12352          --  these references with the constraints given in Discs. We do not
12353          --  do this for the partial view of private types because this is
12354          --  not needed (only the components of the full view will be used
12355          --  for code generation) and cause problem. We also avoid this
12356          --  transformation in some error situations.
12357
12358          if Ekind (New_C) = E_Component then
12359             if (Is_Private_Type (Derived_Base)
12360                  and then not Is_Generic_Type (Derived_Base))
12361               or else (Is_Empty_Elmt_List (Discs)
12362                         and then  not Expander_Active)
12363             then
12364                Set_Etype (New_C, Etype (Old_C));
12365
12366             else
12367                --  The current component introduces a circularity of the
12368                --  following kind:
12369
12370                --     limited with Pack_2;
12371                --     package Pack_1 is
12372                --        type T_1 is tagged record
12373                --           Comp : access Pack_2.T_2;
12374                --           ...
12375                --        end record;
12376                --     end Pack_1;
12377
12378                --     with Pack_1;
12379                --     package Pack_2 is
12380                --        type T_2 is new Pack_1.T_1 with ...;
12381                --     end Pack_2;
12382
12383                --  When Comp is being duplicated for type T_2, its designated
12384                --  type must be set to point to the non-limited view of T_2.
12385
12386                if Ada_Version >= Ada_05
12387                  and then
12388                    Ekind (Etype (New_C)) = E_Anonymous_Access_Type
12389                  and then
12390                    Ekind (Directly_Designated_Type
12391                            (Etype (New_C))) = E_Incomplete_Type
12392                  and then
12393                    From_With_Type (Directly_Designated_Type (Etype (New_C)))
12394                  and then
12395                    Present (Non_Limited_View
12396                              (Directly_Designated_Type (Etype (New_C))))
12397                  and then
12398                    Non_Limited_View (Directly_Designated_Type
12399                                       (Etype (New_C))) = Derived_Base
12400                then
12401                   Set_Directly_Designated_Type
12402                     (Etype (New_C),
12403                      Non_Limited_View
12404                        (Directly_Designated_Type (Etype (New_C))));
12405
12406                else
12407                   Set_Etype
12408                     (New_C,
12409                      Constrain_Component_Type
12410                        (Old_C, Derived_Base, N, Parent_Base, Discs));
12411                end if;
12412             end if;
12413          end if;
12414
12415          --  In derived tagged types it is illegal to reference a non
12416          --  discriminant component in the parent type. To catch this, mark
12417          --  these components with an Ekind of E_Void. This will be reset in
12418          --  Record_Type_Definition after processing the record extension of
12419          --  the derived type.
12420
12421          if Is_Tagged and then Ekind (New_C) = E_Component then
12422             Set_Ekind (New_C, E_Void);
12423          end if;
12424
12425          if Plain_Discrim then
12426             Set_Corresponding_Discriminant (New_C, Old_C);
12427             Build_Discriminal (New_C);
12428
12429          --  If we are explicitly inheriting a stored discriminant it will be
12430          --  completely hidden.
12431
12432          elsif Stored_Discrim then
12433             Set_Corresponding_Discriminant (New_C, Empty);
12434             Set_Discriminal (New_C, Empty);
12435             Set_Is_Completely_Hidden (New_C);
12436
12437             --  Set the Original_Record_Component of each discriminant in the
12438             --  derived base to point to the corresponding stored that we just
12439             --  created.
12440
12441             Discrim := First_Discriminant (Derived_Base);
12442             while Present (Discrim) loop
12443                Corr_Discrim := Corresponding_Discriminant (Discrim);
12444
12445                --  Corr_Discrim could be missing in an error situation
12446
12447                if Present (Corr_Discrim)
12448                  and then Original_Record_Component (Corr_Discrim) = Old_C
12449                then
12450                   Set_Original_Record_Component (Discrim, New_C);
12451                end if;
12452
12453                Next_Discriminant (Discrim);
12454             end loop;
12455
12456             Append_Entity (New_C, Derived_Base);
12457          end if;
12458
12459          if not Is_Tagged then
12460             Append_Elmt (Old_C, Assoc_List);
12461             Append_Elmt (New_C, Assoc_List);
12462          end if;
12463       end Inherit_Component;
12464
12465       --  Variables local to Inherit_Component
12466
12467       Loc : constant Source_Ptr := Sloc (N);
12468
12469       Parent_Discrim : Entity_Id;
12470       Stored_Discrim : Entity_Id;
12471       D              : Entity_Id;
12472       Component      : Entity_Id;
12473
12474    --  Start of processing for Inherit_Components
12475
12476    begin
12477       if not Is_Tagged then
12478          Append_Elmt (Parent_Base,  Assoc_List);
12479          Append_Elmt (Derived_Base, Assoc_List);
12480       end if;
12481
12482       --  Inherit parent discriminants if needed
12483
12484       if Inherit_Discr then
12485          Parent_Discrim := First_Discriminant (Parent_Base);
12486          while Present (Parent_Discrim) loop
12487             Inherit_Component (Parent_Discrim, Plain_Discrim => True);
12488             Next_Discriminant (Parent_Discrim);
12489          end loop;
12490       end if;
12491
12492       --  Create explicit stored discrims for untagged types when necessary
12493
12494       if not Has_Unknown_Discriminants (Derived_Base)
12495         and then Has_Discriminants (Parent_Base)
12496         and then not Is_Tagged
12497         and then
12498           (not Inherit_Discr
12499              or else First_Discriminant (Parent_Base) /=
12500                      First_Stored_Discriminant (Parent_Base))
12501       then
12502          Stored_Discrim := First_Stored_Discriminant (Parent_Base);
12503          while Present (Stored_Discrim) loop
12504             Inherit_Component (Stored_Discrim, Stored_Discrim => True);
12505             Next_Stored_Discriminant (Stored_Discrim);
12506          end loop;
12507       end if;
12508
12509       --  See if we can apply the second transformation for derived types, as
12510       --  explained in point 6. in the comments above Build_Derived_Record_Type
12511       --  This is achieved by appending Derived_Base discriminants into Discs,
12512       --  which has the side effect of returning a non empty Discs list to the
12513       --  caller of Inherit_Components, which is what we want. This must be
12514       --  done for private derived types if there are explicit stored
12515       --  discriminants, to ensure that we can retrieve the values of the
12516       --  constraints provided in the ancestors.
12517
12518       if Inherit_Discr
12519         and then Is_Empty_Elmt_List (Discs)
12520         and then Present (First_Discriminant (Derived_Base))
12521         and then
12522           (not Is_Private_Type (Derived_Base)
12523              or else Is_Completely_Hidden
12524                (First_Stored_Discriminant (Derived_Base))
12525              or else Is_Generic_Type (Derived_Base))
12526       then
12527          D := First_Discriminant (Derived_Base);
12528          while Present (D) loop
12529             Append_Elmt (New_Reference_To (D, Loc), Discs);
12530             Next_Discriminant (D);
12531          end loop;
12532       end if;
12533
12534       --  Finally, inherit non-discriminant components unless they are not
12535       --  visible because defined or inherited from the full view of the
12536       --  parent. Don't inherit the _parent field of the parent type.
12537
12538       Component := First_Entity (Parent_Base);
12539       while Present (Component) loop
12540
12541          --  Ada 2005 (AI-251): Do not inherit tags corresponding with the
12542          --  interfaces of the parent
12543
12544          if Ekind (Component) = E_Component
12545            and then Is_Tag (Component)
12546            and then RTE_Available (RE_Interface_Tag)
12547            and then Etype  (Component) = RTE (RE_Interface_Tag)
12548          then
12549             null;
12550
12551          elsif Ekind (Component) /= E_Component
12552            or else Chars (Component) = Name_uParent
12553          then
12554             null;
12555
12556          --  If the derived type is within the parent type's declarative
12557          --  region, then the components can still be inherited even though
12558          --  they aren't visible at this point. This can occur for cases
12559          --  such as within public child units where the components must
12560          --  become visible upon entering the child unit's private part.
12561
12562          elsif not Is_Visible_Component (Component)
12563            and then not In_Open_Scopes (Scope (Parent_Base))
12564          then
12565             null;
12566
12567          elsif Ekind (Derived_Base) = E_Private_Type
12568            or else Ekind (Derived_Base) = E_Limited_Private_Type
12569          then
12570             null;
12571
12572          else
12573             Inherit_Component (Component);
12574          end if;
12575
12576          Next_Entity (Component);
12577       end loop;
12578
12579       --  For tagged derived types, inherited discriminants cannot be used in
12580       --  component declarations of the record extension part. To achieve this
12581       --  we mark the inherited discriminants as not visible.
12582
12583       if Is_Tagged and then Inherit_Discr then
12584          D := First_Discriminant (Derived_Base);
12585          while Present (D) loop
12586             Set_Is_Immediately_Visible (D, False);
12587             Next_Discriminant (D);
12588          end loop;
12589       end if;
12590
12591       return Assoc_List;
12592    end Inherit_Components;
12593
12594    -----------------------
12595    -- Is_Null_Extension --
12596    -----------------------
12597
12598    function Is_Null_Extension (T : Entity_Id) return Boolean is
12599       Full_Type_Decl : constant Node_Id := Parent (T);
12600       Full_Type_Defn : constant Node_Id := Type_Definition (Full_Type_Decl);
12601       Comp_List      : Node_Id;
12602       First_Comp     : Node_Id;
12603
12604    begin
12605       if not Is_Tagged_Type (T)
12606         or else Nkind (Full_Type_Defn) /= N_Derived_Type_Definition
12607       then
12608          return False;
12609       end if;
12610
12611       Comp_List := Component_List (Record_Extension_Part (Full_Type_Defn));
12612
12613       if Present (Discriminant_Specifications (Full_Type_Decl)) then
12614          return False;
12615
12616       elsif Present (Comp_List)
12617         and then Is_Non_Empty_List (Component_Items (Comp_List))
12618       then
12619          First_Comp := First (Component_Items (Comp_List));
12620
12621          return Chars (Defining_Identifier (First_Comp)) = Name_uParent
12622            and then No (Next (First_Comp));
12623
12624       else
12625          return True;
12626       end if;
12627    end Is_Null_Extension;
12628
12629    ------------------------------
12630    -- Is_Valid_Constraint_Kind --
12631    ------------------------------
12632
12633    function Is_Valid_Constraint_Kind
12634      (T_Kind          : Type_Kind;
12635       Constraint_Kind : Node_Kind) return Boolean
12636    is
12637    begin
12638       case T_Kind is
12639          when Enumeration_Kind |
12640               Integer_Kind =>
12641             return Constraint_Kind = N_Range_Constraint;
12642
12643          when Decimal_Fixed_Point_Kind =>
12644             return
12645               Constraint_Kind = N_Digits_Constraint
12646                 or else
12647               Constraint_Kind = N_Range_Constraint;
12648
12649          when Ordinary_Fixed_Point_Kind =>
12650             return
12651               Constraint_Kind = N_Delta_Constraint
12652                 or else
12653               Constraint_Kind = N_Range_Constraint;
12654
12655          when Float_Kind =>
12656             return
12657               Constraint_Kind = N_Digits_Constraint
12658                 or else
12659               Constraint_Kind = N_Range_Constraint;
12660
12661          when Access_Kind       |
12662               Array_Kind        |
12663               E_Record_Type     |
12664               E_Record_Subtype  |
12665               Class_Wide_Kind   |
12666               E_Incomplete_Type |
12667               Private_Kind      |
12668               Concurrent_Kind  =>
12669             return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
12670
12671          when others =>
12672             return True; -- Error will be detected later
12673       end case;
12674    end Is_Valid_Constraint_Kind;
12675
12676    --------------------------
12677    -- Is_Visible_Component --
12678    --------------------------
12679
12680    function Is_Visible_Component (C : Entity_Id) return Boolean is
12681       Original_Comp  : Entity_Id := Empty;
12682       Original_Scope : Entity_Id;
12683       Type_Scope     : Entity_Id;
12684
12685       function Is_Local_Type (Typ : Entity_Id) return Boolean;
12686       --  Check whether parent type of inherited component is declared locally,
12687       --  possibly within a nested package or instance. The current scope is
12688       --  the derived record itself.
12689
12690       -------------------
12691       -- Is_Local_Type --
12692       -------------------
12693
12694       function Is_Local_Type (Typ : Entity_Id) return Boolean is
12695          Scop : Entity_Id;
12696
12697       begin
12698          Scop := Scope (Typ);
12699          while Present (Scop)
12700            and then Scop /= Standard_Standard
12701          loop
12702             if Scop = Scope (Current_Scope) then
12703                return True;
12704             end if;
12705
12706             Scop := Scope (Scop);
12707          end loop;
12708
12709          return False;
12710       end Is_Local_Type;
12711
12712    --  Start of processing for Is_Visible_Component
12713
12714    begin
12715       if Ekind (C) = E_Component
12716         or else Ekind (C) = E_Discriminant
12717       then
12718          Original_Comp := Original_Record_Component (C);
12719       end if;
12720
12721       if No (Original_Comp) then
12722
12723          --  Premature usage, or previous error
12724
12725          return False;
12726
12727       else
12728          Original_Scope := Scope (Original_Comp);
12729          Type_Scope     := Scope (Base_Type (Scope (C)));
12730       end if;
12731
12732       --  This test only concerns tagged types
12733
12734       if not Is_Tagged_Type (Original_Scope) then
12735          return True;
12736
12737       --  If it is _Parent or _Tag, there is no visibility issue
12738
12739       elsif not Comes_From_Source (Original_Comp) then
12740          return True;
12741
12742       --  If we are in the body of an instantiation, the component is visible
12743       --  even when the parent type (possibly defined in an enclosing unit or
12744       --  in a parent unit) might not.
12745
12746       elsif In_Instance_Body then
12747          return True;
12748
12749       --  Discriminants are always visible
12750
12751       elsif Ekind (Original_Comp) = E_Discriminant
12752         and then not Has_Unknown_Discriminants (Original_Scope)
12753       then
12754          return True;
12755
12756       --  If the component has been declared in an ancestor which is currently
12757       --  a private type, then it is not visible. The same applies if the
12758       --  component's containing type is not in an open scope and the original
12759       --  component's enclosing type is a visible full type of a private type
12760       --  (which can occur in cases where an attempt is being made to reference
12761       --  a component in a sibling package that is inherited from a visible
12762       --  component of a type in an ancestor package; the component in the
12763       --  sibling package should not be visible even though the component it
12764       --  inherited from is visible). This does not apply however in the case
12765       --  where the scope of the type is a private child unit, or when the
12766       --  parent comes from a local package in which the ancestor is currently
12767       --  visible. The latter suppression of visibility is needed for cases
12768       --  that are tested in B730006.
12769
12770       elsif Is_Private_Type (Original_Scope)
12771         or else
12772           (not Is_Private_Descendant (Type_Scope)
12773             and then not In_Open_Scopes (Type_Scope)
12774             and then Has_Private_Declaration (Original_Scope))
12775       then
12776          --  If the type derives from an entity in a formal package, there
12777          --  are no additional visible components.
12778
12779          if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
12780             N_Formal_Package_Declaration
12781          then
12782             return False;
12783
12784          --  if we are not in the private part of the current package, there
12785          --  are no additional visible components.
12786
12787          elsif Ekind (Scope (Current_Scope)) = E_Package
12788            and then not In_Private_Part (Scope (Current_Scope))
12789          then
12790             return False;
12791          else
12792             return
12793               Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
12794                 and then Is_Local_Type (Type_Scope);
12795          end if;
12796
12797       --  There is another weird way in which a component may be invisible
12798       --  when the private and the full view are not derived from the same
12799       --  ancestor. Here is an example :
12800
12801       --       type A1 is tagged      record F1 : integer; end record;
12802       --       type A2 is new A1 with record F2 : integer; end record;
12803       --       type T is new A1 with private;
12804       --     private
12805       --       type T is new A2 with null record;
12806
12807       --  In this case, the full view of T inherits F1 and F2 but the private
12808       --  view inherits only F1
12809
12810       else
12811          declare
12812             Ancestor : Entity_Id := Scope (C);
12813
12814          begin
12815             loop
12816                if Ancestor = Original_Scope then
12817                   return True;
12818                elsif Ancestor = Etype (Ancestor) then
12819                   return False;
12820                end if;
12821
12822                Ancestor := Etype (Ancestor);
12823             end loop;
12824
12825             return True;
12826          end;
12827       end if;
12828    end Is_Visible_Component;
12829
12830    --------------------------
12831    -- Make_Class_Wide_Type --
12832    --------------------------
12833
12834    procedure Make_Class_Wide_Type (T : Entity_Id) is
12835       CW_Type : Entity_Id;
12836       CW_Name : Name_Id;
12837       Next_E  : Entity_Id;
12838
12839    begin
12840       --  The class wide type can have been defined by the partial view, in
12841       --  which case everything is already done.
12842
12843       if Present (Class_Wide_Type (T)) then
12844          return;
12845       end if;
12846
12847       CW_Type :=
12848         New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
12849
12850       --  Inherit root type characteristics
12851
12852       CW_Name := Chars (CW_Type);
12853       Next_E  := Next_Entity (CW_Type);
12854       Copy_Node (T, CW_Type);
12855       Set_Comes_From_Source (CW_Type, False);
12856       Set_Chars (CW_Type, CW_Name);
12857       Set_Parent (CW_Type, Parent (T));
12858       Set_Next_Entity (CW_Type, Next_E);
12859
12860       --  Ensure we have a new freeze node for the class-wide type. The partial
12861       --  view may have freeze action of its own, requiring a proper freeze
12862       --  node, and the same freeze node cannot be shared between the two
12863       --  types.
12864
12865       Set_Has_Delayed_Freeze (CW_Type);
12866       Set_Freeze_Node (CW_Type, Empty);
12867
12868       --  Customize the class-wide type: It has no prim. op., it cannot be
12869       --  abstract and its Etype points back to the specific root type.
12870
12871       Set_Ekind                (CW_Type, E_Class_Wide_Type);
12872       Set_Is_Tagged_Type       (CW_Type, True);
12873       Set_Primitive_Operations (CW_Type, New_Elmt_List);
12874       Set_Is_Abstract          (CW_Type, False);
12875       Set_Is_Constrained       (CW_Type, False);
12876       Set_Is_First_Subtype     (CW_Type, Is_First_Subtype (T));
12877       Init_Size_Align          (CW_Type);
12878
12879       if Ekind (T) = E_Class_Wide_Subtype then
12880          Set_Etype             (CW_Type, Etype (Base_Type (T)));
12881       else
12882          Set_Etype             (CW_Type, T);
12883       end if;
12884
12885       --  If this is the class_wide type of a constrained subtype, it does
12886       --  not have discriminants.
12887
12888       Set_Has_Discriminants (CW_Type,
12889         Has_Discriminants (T) and then not Is_Constrained (T));
12890
12891       Set_Has_Unknown_Discriminants (CW_Type, True);
12892       Set_Class_Wide_Type (T, CW_Type);
12893       Set_Equivalent_Type (CW_Type, Empty);
12894
12895       --  The class-wide type of a class-wide type is itself (RM 3.9(14))
12896
12897       Set_Class_Wide_Type (CW_Type, CW_Type);
12898    end Make_Class_Wide_Type;
12899
12900    ----------------
12901    -- Make_Index --
12902    ----------------
12903
12904    procedure Make_Index
12905      (I            : Node_Id;
12906       Related_Nod  : Node_Id;
12907       Related_Id   : Entity_Id := Empty;
12908       Suffix_Index : Nat := 1)
12909    is
12910       R      : Node_Id;
12911       T      : Entity_Id;
12912       Def_Id : Entity_Id := Empty;
12913       Found  : Boolean := False;
12914
12915    begin
12916       --  For a discrete range used in a constrained array definition and
12917       --  defined by a range, an implicit conversion to the predefined type
12918       --  INTEGER is assumed if each bound is either a numeric literal, a named
12919       --  number, or an attribute, and the type of both bounds (prior to the
12920       --  implicit conversion) is the type universal_integer. Otherwise, both
12921       --  bounds must be of the same discrete type, other than universal
12922       --  integer; this type must be determinable independently of the
12923       --  context, but using the fact that the type must be discrete and that
12924       --  both bounds must have the same type.
12925
12926       --  Character literals also have a universal type in the absence of
12927       --  of additional context,  and are resolved to Standard_Character.
12928
12929       if Nkind (I) = N_Range then
12930
12931          --  The index is given by a range constraint. The bounds are known
12932          --  to be of a consistent type.
12933
12934          if not Is_Overloaded (I) then
12935             T := Etype (I);
12936
12937             --  If the bounds are universal, choose the specific predefined
12938             --  type.
12939
12940             if T = Universal_Integer then
12941                T := Standard_Integer;
12942
12943             elsif T = Any_Character then
12944
12945                if Ada_Version >= Ada_95 then
12946                   Error_Msg_N
12947                     ("ambiguous character literals (could be Wide_Character)",
12948                       I);
12949                end if;
12950
12951                T := Standard_Character;
12952             end if;
12953
12954          else
12955             T := Any_Type;
12956
12957             declare
12958                Ind : Interp_Index;
12959                It  : Interp;
12960
12961             begin
12962                Get_First_Interp (I, Ind, It);
12963                while Present (It.Typ) loop
12964                   if Is_Discrete_Type (It.Typ) then
12965
12966                      if Found
12967                        and then not Covers (It.Typ, T)
12968                        and then not Covers (T, It.Typ)
12969                      then
12970                         Error_Msg_N ("ambiguous bounds in discrete range", I);
12971                         exit;
12972                      else
12973                         T := It.Typ;
12974                         Found := True;
12975                      end if;
12976                   end if;
12977
12978                   Get_Next_Interp (Ind, It);
12979                end loop;
12980
12981                if T = Any_Type then
12982                   Error_Msg_N ("discrete type required for range", I);
12983                   Set_Etype (I, Any_Type);
12984                   return;
12985
12986                elsif T = Universal_Integer then
12987                   T := Standard_Integer;
12988                end if;
12989             end;
12990          end if;
12991
12992          if not Is_Discrete_Type (T) then
12993             Error_Msg_N ("discrete type required for range", I);
12994             Set_Etype (I, Any_Type);
12995             return;
12996          end if;
12997
12998          if Nkind (Low_Bound (I)) = N_Attribute_Reference
12999            and then Attribute_Name (Low_Bound (I)) = Name_First
13000            and then Is_Entity_Name (Prefix (Low_Bound (I)))
13001            and then Is_Type (Entity (Prefix (Low_Bound (I))))
13002            and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
13003          then
13004             --  The type of the index will be the type of the prefix, as long
13005             --  as the upper bound is 'Last of the same type.
13006
13007             Def_Id := Entity (Prefix (Low_Bound (I)));
13008
13009             if Nkind (High_Bound (I)) /= N_Attribute_Reference
13010               or else Attribute_Name (High_Bound (I)) /= Name_Last
13011               or else not Is_Entity_Name (Prefix (High_Bound (I)))
13012               or else Entity (Prefix (High_Bound (I))) /= Def_Id
13013             then
13014                Def_Id := Empty;
13015             end if;
13016          end if;
13017
13018          R := I;
13019          Process_Range_Expr_In_Decl (R, T);
13020
13021       elsif Nkind (I) = N_Subtype_Indication then
13022
13023          --  The index is given by a subtype with a range constraint
13024
13025          T :=  Base_Type (Entity (Subtype_Mark (I)));
13026
13027          if not Is_Discrete_Type (T) then
13028             Error_Msg_N ("discrete type required for range", I);
13029             Set_Etype (I, Any_Type);
13030             return;
13031          end if;
13032
13033          R := Range_Expression (Constraint (I));
13034
13035          Resolve (R, T);
13036          Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (I)));
13037
13038       elsif Nkind (I) = N_Attribute_Reference then
13039
13040          --  The parser guarantees that the attribute is a RANGE attribute
13041
13042          --  If the node denotes the range of a type mark, that is also the
13043          --  resulting type, and we do no need to create an Itype for it.
13044
13045          if Is_Entity_Name (Prefix (I))
13046            and then Comes_From_Source (I)
13047            and then Is_Type (Entity (Prefix (I)))
13048            and then Is_Discrete_Type (Entity (Prefix (I)))
13049          then
13050             Def_Id := Entity (Prefix (I));
13051          end if;
13052
13053          Analyze_And_Resolve (I);
13054          T := Etype (I);
13055          R := I;
13056
13057       --  If none of the above, must be a subtype. We convert this to a
13058       --  range attribute reference because in the case of declared first
13059       --  named subtypes, the types in the range reference can be different
13060       --  from the type of the entity. A range attribute normalizes the
13061       --  reference and obtains the correct types for the bounds.
13062
13063       --  This transformation is in the nature of an expansion, is only
13064       --  done if expansion is active. In particular, it is not done on
13065       --  formal generic types,  because we need to retain the name of the
13066       --  original index for instantiation purposes.
13067
13068       else
13069          if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
13070             Error_Msg_N ("invalid subtype mark in discrete range ", I);
13071             Set_Etype (I, Any_Integer);
13072             return;
13073
13074          else
13075             --  The type mark may be that of an incomplete type. It is only
13076             --  now that we can get the full view, previous analysis does
13077             --  not look specifically for a type mark.
13078
13079             Set_Entity (I, Get_Full_View (Entity (I)));
13080             Set_Etype  (I, Entity (I));
13081             Def_Id := Entity (I);
13082
13083             if not Is_Discrete_Type (Def_Id) then
13084                Error_Msg_N ("discrete type required for index", I);
13085                Set_Etype (I, Any_Type);
13086                return;
13087             end if;
13088          end if;
13089
13090          if Expander_Active then
13091             Rewrite (I,
13092               Make_Attribute_Reference (Sloc (I),
13093                 Attribute_Name => Name_Range,
13094                 Prefix         => Relocate_Node (I)));
13095
13096             --  The original was a subtype mark that does not freeze. This
13097             --  means that the rewritten version must not freeze either.
13098
13099             Set_Must_Not_Freeze (I);
13100             Set_Must_Not_Freeze (Prefix (I));
13101
13102             --  Is order critical??? if so, document why, if not
13103             --  use Analyze_And_Resolve
13104
13105             Analyze_And_Resolve (I);
13106             T := Etype (I);
13107             R := I;
13108
13109          --  If expander is inactive, type is legal, nothing else to construct
13110
13111          else
13112             return;
13113          end if;
13114       end if;
13115
13116       if not Is_Discrete_Type (T) then
13117          Error_Msg_N ("discrete type required for range", I);
13118          Set_Etype (I, Any_Type);
13119          return;
13120
13121       elsif T = Any_Type then
13122          Set_Etype (I, Any_Type);
13123          return;
13124       end if;
13125
13126       --  We will now create the appropriate Itype to describe the range, but
13127       --  first a check. If we originally had a subtype, then we just label
13128       --  the range with this subtype. Not only is there no need to construct
13129       --  a new subtype, but it is wrong to do so for two reasons:
13130
13131       --    1. A legality concern, if we have a subtype, it must not freeze,
13132       --       and the Itype would cause freezing incorrectly
13133
13134       --    2. An efficiency concern, if we created an Itype, it would not be
13135       --       recognized as the same type for the purposes of eliminating
13136       --       checks in some circumstances.
13137
13138       --  We signal this case by setting the subtype entity in Def_Id
13139
13140       if No (Def_Id) then
13141          Def_Id :=
13142            Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
13143          Set_Etype (Def_Id, Base_Type (T));
13144
13145          if Is_Signed_Integer_Type (T) then
13146             Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13147
13148          elsif Is_Modular_Integer_Type (T) then
13149             Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13150
13151          else
13152             Set_Ekind             (Def_Id, E_Enumeration_Subtype);
13153             Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13154             Set_First_Literal     (Def_Id, First_Literal (T));
13155          end if;
13156
13157          Set_Size_Info      (Def_Id,                  (T));
13158          Set_RM_Size        (Def_Id, RM_Size          (T));
13159          Set_First_Rep_Item (Def_Id, First_Rep_Item   (T));
13160
13161          Set_Scalar_Range   (Def_Id, R);
13162          Conditional_Delay  (Def_Id, T);
13163
13164          --  In the subtype indication case, if the immediate parent of the
13165          --  new subtype is non-static, then the subtype we create is non-
13166          --  static, even if its bounds are static.
13167
13168          if Nkind (I) = N_Subtype_Indication
13169            and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
13170          then
13171             Set_Is_Non_Static_Subtype (Def_Id);
13172          end if;
13173       end if;
13174
13175       --  Final step is to label the index with this constructed type
13176
13177       Set_Etype (I, Def_Id);
13178    end Make_Index;
13179
13180    ------------------------------
13181    -- Modular_Type_Declaration --
13182    ------------------------------
13183
13184    procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
13185       Mod_Expr : constant Node_Id := Expression (Def);
13186       M_Val    : Uint;
13187
13188       procedure Set_Modular_Size (Bits : Int);
13189       --  Sets RM_Size to Bits, and Esize to normal word size above this
13190
13191       ----------------------
13192       -- Set_Modular_Size --
13193       ----------------------
13194
13195       procedure Set_Modular_Size (Bits : Int) is
13196       begin
13197          Set_RM_Size (T, UI_From_Int (Bits));
13198
13199          if Bits <= 8 then
13200             Init_Esize (T, 8);
13201
13202          elsif Bits <= 16 then
13203             Init_Esize (T, 16);
13204
13205          elsif Bits <= 32 then
13206             Init_Esize (T, 32);
13207
13208          else
13209             Init_Esize (T, System_Max_Binary_Modulus_Power);
13210          end if;
13211       end Set_Modular_Size;
13212
13213    --  Start of processing for Modular_Type_Declaration
13214
13215    begin
13216       Analyze_And_Resolve (Mod_Expr, Any_Integer);
13217       Set_Etype (T, T);
13218       Set_Ekind (T, E_Modular_Integer_Type);
13219       Init_Alignment (T);
13220       Set_Is_Constrained (T);
13221
13222       if not Is_OK_Static_Expression (Mod_Expr) then
13223          Flag_Non_Static_Expr
13224            ("non-static expression used for modular type bound!", Mod_Expr);
13225          M_Val := 2 ** System_Max_Binary_Modulus_Power;
13226       else
13227          M_Val := Expr_Value (Mod_Expr);
13228       end if;
13229
13230       if M_Val < 1 then
13231          Error_Msg_N ("modulus value must be positive", Mod_Expr);
13232          M_Val := 2 ** System_Max_Binary_Modulus_Power;
13233       end if;
13234
13235       Set_Modulus (T, M_Val);
13236
13237       --   Create bounds for the modular type based on the modulus given in
13238       --   the type declaration and then analyze and resolve those bounds.
13239
13240       Set_Scalar_Range (T,
13241         Make_Range (Sloc (Mod_Expr),
13242           Low_Bound  =>
13243             Make_Integer_Literal (Sloc (Mod_Expr), 0),
13244           High_Bound =>
13245             Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
13246
13247       --  Properly analyze the literals for the range. We do this manually
13248       --  because we can't go calling Resolve, since we are resolving these
13249       --  bounds with the type, and this type is certainly not complete yet!
13250
13251       Set_Etype (Low_Bound  (Scalar_Range (T)), T);
13252       Set_Etype (High_Bound (Scalar_Range (T)), T);
13253       Set_Is_Static_Expression (Low_Bound  (Scalar_Range (T)));
13254       Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
13255
13256       --  Loop through powers of two to find number of bits required
13257
13258       for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
13259
13260          --  Binary case
13261
13262          if M_Val = 2 ** Bits then
13263             Set_Modular_Size (Bits);
13264             return;
13265
13266          --  Non-binary case
13267
13268          elsif M_Val < 2 ** Bits then
13269             Set_Non_Binary_Modulus (T);
13270
13271             if Bits > System_Max_Nonbinary_Modulus_Power then
13272                Error_Msg_Uint_1 :=
13273                  UI_From_Int (System_Max_Nonbinary_Modulus_Power);
13274                Error_Msg_N
13275                  ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
13276                Set_Modular_Size (System_Max_Binary_Modulus_Power);
13277                return;
13278
13279             else
13280                --  In the non-binary case, set size as per RM 13.3(55)
13281
13282                Set_Modular_Size (Bits);
13283                return;
13284             end if;
13285          end if;
13286
13287       end loop;
13288
13289       --  If we fall through, then the size exceed System.Max_Binary_Modulus
13290       --  so we just signal an error and set the maximum size.
13291
13292       Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
13293       Error_Msg_N ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
13294
13295       Set_Modular_Size (System_Max_Binary_Modulus_Power);
13296       Init_Alignment (T);
13297
13298    end Modular_Type_Declaration;
13299
13300    --------------------------
13301    -- New_Concatenation_Op --
13302    --------------------------
13303
13304    procedure New_Concatenation_Op (Typ : Entity_Id) is
13305       Loc : constant Source_Ptr := Sloc (Typ);
13306       Op  : Entity_Id;
13307
13308       function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
13309       --  Create abbreviated declaration for the formal of a predefined
13310       --  Operator 'Op' of type 'Typ'
13311
13312       --------------------
13313       -- Make_Op_Formal --
13314       --------------------
13315
13316       function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
13317          Formal : Entity_Id;
13318       begin
13319          Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
13320          Set_Etype (Formal, Typ);
13321          Set_Mechanism (Formal, Default_Mechanism);
13322          return Formal;
13323       end Make_Op_Formal;
13324
13325    --  Start of processing for New_Concatenation_Op
13326
13327    begin
13328       Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
13329
13330       Set_Ekind                   (Op, E_Operator);
13331       Set_Scope                   (Op, Current_Scope);
13332       Set_Etype                   (Op, Typ);
13333       Set_Homonym                 (Op, Get_Name_Entity_Id (Name_Op_Concat));
13334       Set_Is_Immediately_Visible  (Op);
13335       Set_Is_Intrinsic_Subprogram (Op);
13336       Set_Has_Completion          (Op);
13337       Append_Entity               (Op, Current_Scope);
13338
13339       Set_Name_Entity_Id (Name_Op_Concat, Op);
13340
13341       Append_Entity (Make_Op_Formal (Typ, Op), Op);
13342       Append_Entity (Make_Op_Formal (Typ, Op), Op);
13343    end New_Concatenation_Op;
13344
13345    -------------------------
13346    -- OK_For_Limited_Init --
13347    -------------------------
13348
13349    --  ???Check all calls of this, and compare the conditions under which it's
13350    --  called.
13351
13352    function OK_For_Limited_Init (Exp : Node_Id) return Boolean is
13353    begin
13354       return Ada_Version >= Ada_05
13355         and then not Debug_Flag_Dot_L
13356         and then OK_For_Limited_Init_In_05 (Exp);
13357    end OK_For_Limited_Init;
13358
13359    -------------------------------
13360    -- OK_For_Limited_Init_In_05 --
13361    -------------------------------
13362
13363    function OK_For_Limited_Init_In_05 (Exp : Node_Id) return Boolean is
13364    begin
13365       --  ???Expand_N_Extended_Return_Statement generates code that would
13366       --  violate the rules in some cases. Once we have build-in-place
13367       --  function returns working, we can probably remove the following
13368       --  check.
13369
13370       if not Comes_From_Source (Exp) then
13371          return True;
13372       end if;
13373
13374       --  Ada 2005 (AI-287, AI-318): Relax the strictness of the front-end in
13375       --  case of limited aggregates (including extension aggregates),
13376       --  and function calls.
13377
13378       case Nkind (Original_Node (Exp)) is
13379          when N_Aggregate | N_Extension_Aggregate | N_Function_Call =>
13380             return True;
13381
13382          when N_Qualified_Expression =>
13383             return OK_For_Limited_Init_In_05
13384                      (Expression (Original_Node (Exp)));
13385
13386          when others =>
13387             return False;
13388       end case;
13389    end OK_For_Limited_Init_In_05;
13390
13391    -------------------------------------------
13392    -- Ordinary_Fixed_Point_Type_Declaration --
13393    -------------------------------------------
13394
13395    procedure Ordinary_Fixed_Point_Type_Declaration
13396      (T   : Entity_Id;
13397       Def : Node_Id)
13398    is
13399       Loc           : constant Source_Ptr := Sloc (Def);
13400       Delta_Expr    : constant Node_Id    := Delta_Expression (Def);
13401       RRS           : constant Node_Id    := Real_Range_Specification (Def);
13402       Implicit_Base : Entity_Id;
13403       Delta_Val     : Ureal;
13404       Small_Val     : Ureal;
13405       Low_Val       : Ureal;
13406       High_Val      : Ureal;
13407
13408    begin
13409       Check_Restriction (No_Fixed_Point, Def);
13410
13411       --  Create implicit base type
13412
13413       Implicit_Base :=
13414         Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
13415       Set_Etype (Implicit_Base, Implicit_Base);
13416
13417       --  Analyze and process delta expression
13418
13419       Analyze_And_Resolve (Delta_Expr, Any_Real);
13420
13421       Check_Delta_Expression (Delta_Expr);
13422       Delta_Val := Expr_Value_R (Delta_Expr);
13423
13424       Set_Delta_Value (Implicit_Base, Delta_Val);
13425
13426       --  Compute default small from given delta, which is the largest power
13427       --  of two that does not exceed the given delta value.
13428
13429       declare
13430          Tmp   : Ureal;
13431          Scale : Int;
13432
13433       begin
13434          Tmp := Ureal_1;
13435          Scale := 0;
13436
13437          if Delta_Val < Ureal_1 then
13438             while Delta_Val < Tmp loop
13439                Tmp := Tmp / Ureal_2;
13440                Scale := Scale + 1;
13441             end loop;
13442
13443          else
13444             loop
13445                Tmp := Tmp * Ureal_2;
13446                exit when Tmp > Delta_Val;
13447                Scale := Scale - 1;
13448             end loop;
13449          end if;
13450
13451          Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
13452       end;
13453
13454       Set_Small_Value (Implicit_Base, Small_Val);
13455
13456       --  If no range was given, set a dummy range
13457
13458       if RRS <= Empty_Or_Error then
13459          Low_Val  := -Small_Val;
13460          High_Val := Small_Val;
13461
13462       --  Otherwise analyze and process given range
13463
13464       else
13465          declare
13466             Low  : constant Node_Id := Low_Bound  (RRS);
13467             High : constant Node_Id := High_Bound (RRS);
13468
13469          begin
13470             Analyze_And_Resolve (Low, Any_Real);
13471             Analyze_And_Resolve (High, Any_Real);
13472             Check_Real_Bound (Low);
13473             Check_Real_Bound (High);
13474
13475             --  Obtain and set the range
13476
13477             Low_Val  := Expr_Value_R (Low);
13478             High_Val := Expr_Value_R (High);
13479
13480             if Low_Val > High_Val then
13481                Error_Msg_NE ("?fixed point type& has null range", Def, T);
13482             end if;
13483          end;
13484       end if;
13485
13486       --  The range for both the implicit base and the declared first subtype
13487       --  cannot be set yet, so we use the special routine Set_Fixed_Range to
13488       --  set a temporary range in place. Note that the bounds of the base
13489       --  type will be widened to be symmetrical and to fill the available
13490       --  bits when the type is frozen.
13491
13492       --  We could do this with all discrete types, and probably should, but
13493       --  we absolutely have to do it for fixed-point, since the end-points
13494       --  of the range and the size are determined by the small value, which
13495       --  could be reset before the freeze point.
13496
13497       Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
13498       Set_Fixed_Range (T, Loc, Low_Val, High_Val);
13499
13500       Init_Size_Align (Implicit_Base);
13501
13502       --  Complete definition of first subtype
13503
13504       Set_Ekind          (T, E_Ordinary_Fixed_Point_Subtype);
13505       Set_Etype          (T, Implicit_Base);
13506       Init_Size_Align    (T);
13507       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
13508       Set_Small_Value    (T, Small_Val);
13509       Set_Delta_Value    (T, Delta_Val);
13510       Set_Is_Constrained (T);
13511
13512    end Ordinary_Fixed_Point_Type_Declaration;
13513
13514    ----------------------------------------
13515    -- Prepare_Private_Subtype_Completion --
13516    ----------------------------------------
13517
13518    procedure Prepare_Private_Subtype_Completion
13519      (Id          : Entity_Id;
13520       Related_Nod : Node_Id)
13521    is
13522       Id_B   : constant Entity_Id := Base_Type (Id);
13523       Full_B : constant Entity_Id := Full_View (Id_B);
13524       Full   : Entity_Id;
13525
13526    begin
13527       if Present (Full_B) then
13528
13529          --  The Base_Type is already completed, we can complete the subtype
13530          --  now. We have to create a new entity with the same name, Thus we
13531          --  can't use Create_Itype.
13532
13533          --  This is messy, should be fixed ???
13534
13535          Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
13536          Set_Is_Itype (Full);
13537          Set_Associated_Node_For_Itype (Full, Related_Nod);
13538          Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
13539       end if;
13540
13541       --  The parent subtype may be private, but the base might not, in some
13542       --  nested instances. In that case, the subtype does not need to be
13543       --  exchanged. It would still be nice to make private subtypes and their
13544       --  bases consistent at all times ???
13545
13546       if Is_Private_Type (Id_B) then
13547          Append_Elmt (Id, Private_Dependents (Id_B));
13548       end if;
13549
13550    end Prepare_Private_Subtype_Completion;
13551
13552    ---------------------------
13553    -- Process_Discriminants --
13554    ---------------------------
13555
13556    procedure Process_Discriminants
13557      (N    : Node_Id;
13558       Prev : Entity_Id := Empty)
13559    is
13560       Elist               : constant Elist_Id := New_Elmt_List;
13561       Id                  : Node_Id;
13562       Discr               : Node_Id;
13563       Discr_Number        : Uint;
13564       Discr_Type          : Entity_Id;
13565       Default_Present     : Boolean := False;
13566       Default_Not_Present : Boolean := False;
13567
13568    begin
13569       --  A composite type other than an array type can have discriminants.
13570       --  Discriminants of non-limited types must have a discrete type.
13571       --  On entry, the current scope is the composite type.
13572
13573       --  The discriminants are initially entered into the scope of the type
13574       --  via Enter_Name with the default Ekind of E_Void to prevent premature
13575       --  use, as explained at the end of this procedure.
13576
13577       Discr := First (Discriminant_Specifications (N));
13578       while Present (Discr) loop
13579          Enter_Name (Defining_Identifier (Discr));
13580
13581          --  For navigation purposes we add a reference to the discriminant
13582          --  in the entity for the type. If the current declaration is a
13583          --  completion, place references on the partial view. Otherwise the
13584          --  type is the current scope.
13585
13586          if Present (Prev) then
13587
13588             --  The references go on the partial view, if present. If the
13589             --  partial view has discriminants, the references have been
13590             --  generated already.
13591
13592             if not Has_Discriminants (Prev) then
13593                Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
13594             end if;
13595          else
13596             Generate_Reference
13597               (Current_Scope, Defining_Identifier (Discr), 'd');
13598          end if;
13599
13600          if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
13601             Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
13602
13603             --  Ada 2005 (AI-230): Access discriminants are now allowed for
13604             --  nonlimited types, and are treated like other components of
13605             --  anonymous access types in terms of accessibility.
13606
13607             if not Is_Concurrent_Type (Current_Scope)
13608               and then not Is_Concurrent_Record_Type (Current_Scope)
13609               and then not Is_Limited_Record (Current_Scope)
13610               and then Ekind (Current_Scope) /= E_Limited_Private_Type
13611             then
13612                Set_Is_Local_Anonymous_Access (Discr_Type);
13613             end if;
13614
13615             --  Ada 2005 (AI-254)
13616
13617             if Present (Access_To_Subprogram_Definition
13618                          (Discriminant_Type (Discr)))
13619               and then Protected_Present (Access_To_Subprogram_Definition
13620                                            (Discriminant_Type (Discr)))
13621             then
13622                Discr_Type :=
13623                  Replace_Anonymous_Access_To_Protected_Subprogram
13624                    (Discr, Discr_Type);
13625             end if;
13626
13627          else
13628             Find_Type (Discriminant_Type (Discr));
13629             Discr_Type := Etype (Discriminant_Type (Discr));
13630
13631             if Error_Posted (Discriminant_Type (Discr)) then
13632                Discr_Type := Any_Type;
13633             end if;
13634          end if;
13635
13636          if Is_Access_Type (Discr_Type) then
13637
13638             --  Ada 2005 (AI-230): Access discriminant allowed in non-limited
13639             --  record types
13640
13641             if Ada_Version < Ada_05 then
13642                Check_Access_Discriminant_Requires_Limited
13643                  (Discr, Discriminant_Type (Discr));
13644             end if;
13645
13646             if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
13647                Error_Msg_N
13648                  ("(Ada 83) access discriminant not allowed", Discr);
13649             end if;
13650
13651          elsif not Is_Discrete_Type (Discr_Type) then
13652             Error_Msg_N ("discriminants must have a discrete or access type",
13653               Discriminant_Type (Discr));
13654          end if;
13655
13656          Set_Etype (Defining_Identifier (Discr), Discr_Type);
13657
13658          --  If a discriminant specification includes the assignment compound
13659          --  delimiter followed by an expression, the expression is the default
13660          --  expression of the discriminant; the default expression must be of
13661          --  the type of the discriminant. (RM 3.7.1) Since this expression is
13662          --  a default expression, we do the special preanalysis, since this
13663          --  expression does not freeze (see "Handling of Default and Per-
13664          --  Object Expressions" in spec of package Sem).
13665
13666          if Present (Expression (Discr)) then
13667             Analyze_Per_Use_Expression (Expression (Discr), Discr_Type);
13668
13669             if Nkind (N) = N_Formal_Type_Declaration then
13670                Error_Msg_N
13671                  ("discriminant defaults not allowed for formal type",
13672                   Expression (Discr));
13673
13674             --  Tagged types cannot have defaulted discriminants, but a
13675             --  non-tagged private type with defaulted discriminants
13676             --   can have a tagged completion.
13677
13678             elsif Is_Tagged_Type (Current_Scope)
13679               and then Comes_From_Source (N)
13680             then
13681                Error_Msg_N
13682                  ("discriminants of tagged type cannot have defaults",
13683                   Expression (Discr));
13684
13685             else
13686                Default_Present := True;
13687                Append_Elmt (Expression (Discr), Elist);
13688
13689                --  Tag the defining identifiers for the discriminants with
13690                --  their corresponding default expressions from the tree.
13691
13692                Set_Discriminant_Default_Value
13693                  (Defining_Identifier (Discr), Expression (Discr));
13694             end if;
13695
13696          else
13697             Default_Not_Present := True;
13698          end if;
13699
13700          --  Ada 2005 (AI-231): Create an Itype that is a duplicate of
13701          --  Discr_Type but with the null-exclusion attribute
13702
13703          if Ada_Version >= Ada_05 then
13704
13705             --  Ada 2005 (AI-231): Static checks
13706
13707             if Can_Never_Be_Null (Discr_Type) then
13708                Null_Exclusion_Static_Checks (Discr);
13709
13710             elsif Is_Access_Type (Discr_Type)
13711               and then Null_Exclusion_Present (Discr)
13712
13713                --  No need to check itypes because in their case this check
13714                --  was done at their point of creation
13715
13716               and then not Is_Itype (Discr_Type)
13717             then
13718                if Can_Never_Be_Null (Discr_Type) then
13719                   Error_Msg_N
13720                     ("null-exclusion cannot be applied to " &
13721                      "a null excluding type", Discr);
13722                end if;
13723
13724                Set_Etype (Defining_Identifier (Discr),
13725                  Create_Null_Excluding_Itype
13726                    (T           => Discr_Type,
13727                     Related_Nod => Discr));
13728             end if;
13729
13730             --  Ada 2005 (AI-402): access discriminants of nonlimited types
13731             --  can't have defaults
13732
13733             if Is_Access_Type (Discr_Type) then
13734                if Ekind (Discr_Type) /= E_Anonymous_Access_Type
13735                  or else not Default_Present
13736                  or else Is_Limited_Record (Current_Scope)
13737                  or else Is_Concurrent_Type (Current_Scope)
13738                  or else Is_Concurrent_Record_Type (Current_Scope)
13739                  or else Ekind (Current_Scope) = E_Limited_Private_Type
13740                then
13741                   null;
13742                else
13743                   Error_Msg_N
13744                     ("(Ada 2005) access discriminants of nonlimited types",
13745                      Expression (Discr));
13746                   Error_Msg_N ("\cannot have defaults", Expression (Discr));
13747                end if;
13748             end if;
13749          end if;
13750
13751          Next (Discr);
13752       end loop;
13753
13754       --  An element list consisting of the default expressions of the
13755       --  discriminants is constructed in the above loop and used to set
13756       --  the Discriminant_Constraint attribute for the type. If an object
13757       --  is declared of this (record or task) type without any explicit
13758       --  discriminant constraint given, this element list will form the
13759       --  actual parameters for the corresponding initialization procedure
13760       --  for the type.
13761
13762       Set_Discriminant_Constraint (Current_Scope, Elist);
13763       Set_Stored_Constraint (Current_Scope, No_Elist);
13764
13765       --  Default expressions must be provided either for all or for none
13766       --  of the discriminants of a discriminant part. (RM 3.7.1)
13767
13768       if Default_Present and then Default_Not_Present then
13769          Error_Msg_N
13770            ("incomplete specification of defaults for discriminants", N);
13771       end if;
13772
13773       --  The use of the name of a discriminant is not allowed in default
13774       --  expressions of a discriminant part if the specification of the
13775       --  discriminant is itself given in the discriminant part. (RM 3.7.1)
13776
13777       --  To detect this, the discriminant names are entered initially with an
13778       --  Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
13779       --  attempt to use a void entity (for example in an expression that is
13780       --  type-checked) produces the error message: premature usage. Now after
13781       --  completing the semantic analysis of the discriminant part, we can set
13782       --  the Ekind of all the discriminants appropriately.
13783
13784       Discr := First (Discriminant_Specifications (N));
13785       Discr_Number := Uint_1;
13786       while Present (Discr) loop
13787          Id := Defining_Identifier (Discr);
13788          Set_Ekind (Id, E_Discriminant);
13789          Init_Component_Location (Id);
13790          Init_Esize (Id);
13791          Set_Discriminant_Number (Id, Discr_Number);
13792
13793          --  Make sure this is always set, even in illegal programs
13794
13795          Set_Corresponding_Discriminant (Id, Empty);
13796
13797          --  Initialize the Original_Record_Component to the entity itself.
13798          --  Inherit_Components will propagate the right value to
13799          --  discriminants in derived record types.
13800
13801          Set_Original_Record_Component (Id, Id);
13802
13803          --  Create the discriminal for the discriminant
13804
13805          Build_Discriminal (Id);
13806
13807          Next (Discr);
13808          Discr_Number := Discr_Number + 1;
13809       end loop;
13810
13811       Set_Has_Discriminants (Current_Scope);
13812    end Process_Discriminants;
13813
13814    -----------------------
13815    -- Process_Full_View --
13816    -----------------------
13817
13818    procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
13819       Priv_Parent : Entity_Id;
13820       Full_Parent : Entity_Id;
13821       Full_Indic  : Node_Id;
13822
13823       procedure Collect_Implemented_Interfaces
13824         (Typ    : Entity_Id;
13825          Ifaces : Elist_Id);
13826       --  Ada 2005: Gather all the interfaces that Typ directly or
13827       --  inherently implements. Duplicate entries are not added to
13828       --  the list Ifaces.
13829
13830       function Contain_Interface
13831         (Iface  : Entity_Id;
13832          Ifaces : Elist_Id) return Boolean;
13833       --  Ada 2005: Determine whether Iface is present in the list Ifaces
13834
13835       function Find_Hidden_Interface
13836         (Src  : Elist_Id;
13837          Dest : Elist_Id) return Entity_Id;
13838       --  Ada 2005: Determine whether the interfaces in list Src are all
13839       --  present in the list Dest. Return the first differing interface,
13840       --  or Empty otherwise.
13841
13842       ------------------------------------
13843       -- Collect_Implemented_Interfaces --
13844       ------------------------------------
13845
13846       procedure Collect_Implemented_Interfaces
13847         (Typ    : Entity_Id;
13848          Ifaces : Elist_Id)
13849       is
13850          Iface      : Entity_Id;
13851          Iface_Elmt : Elmt_Id;
13852
13853       begin
13854          --  Abstract interfaces are only associated with tagged record types
13855
13856          if not Is_Tagged_Type (Typ)
13857            or else not Is_Record_Type (Typ)
13858          then
13859             return;
13860          end if;
13861
13862          --  Recursively climb to the ancestors
13863
13864          if Etype (Typ) /= Typ
13865
13866             --  Protect the frontend against wrong cyclic declarations like:
13867
13868             --     type B is new A with private;
13869             --     type C is new A with private;
13870             --  private
13871             --     type B is new C with null record;
13872             --     type C is new B with null record;
13873
13874            and then Etype (Typ) /= Priv_T
13875            and then Etype (Typ) /= Full_T
13876          then
13877             --  Keep separate the management of private type declarations
13878
13879             if Ekind (Typ) = E_Record_Type_With_Private then
13880
13881                --  Handle the following erronous case:
13882                --      type Private_Type is tagged private;
13883                --   private
13884                --      type Private_Type is new Type_Implementing_Iface;
13885
13886                if Present (Full_View (Typ))
13887                  and then Etype (Typ) /= Full_View (Typ)
13888                then
13889                   if Is_Interface (Etype (Typ))
13890                     and then not Contain_Interface (Etype (Typ), Ifaces)
13891                   then
13892                      Append_Elmt (Etype (Typ), Ifaces);
13893                   end if;
13894
13895                   Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
13896                end if;
13897
13898             --  Non-private types
13899
13900             else
13901                if Is_Interface (Etype (Typ))
13902                  and then not Contain_Interface (Etype (Typ), Ifaces)
13903                then
13904                   Append_Elmt (Etype (Typ), Ifaces);
13905                end if;
13906
13907                Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
13908             end if;
13909          end if;
13910
13911          --  Handle entities in the list of abstract interfaces
13912
13913          if Present (Abstract_Interfaces (Typ)) then
13914             Iface_Elmt := First_Elmt (Abstract_Interfaces (Typ));
13915             while Present (Iface_Elmt) loop
13916                Iface := Node (Iface_Elmt);
13917
13918                pragma Assert (Is_Interface (Iface));
13919
13920                if not Contain_Interface (Iface, Ifaces) then
13921                   Append_Elmt (Iface, Ifaces);
13922                   Collect_Implemented_Interfaces (Iface, Ifaces);
13923                end if;
13924
13925                Next_Elmt (Iface_Elmt);
13926             end loop;
13927          end if;
13928       end Collect_Implemented_Interfaces;
13929
13930       -----------------------
13931       -- Contain_Interface --
13932       -----------------------
13933
13934       function Contain_Interface
13935         (Iface  : Entity_Id;
13936          Ifaces : Elist_Id) return Boolean
13937       is
13938          Iface_Elmt : Elmt_Id;
13939
13940       begin
13941          if Present (Ifaces) then
13942             Iface_Elmt := First_Elmt (Ifaces);
13943             while Present (Iface_Elmt) loop
13944                if Node (Iface_Elmt) = Iface then
13945                   return True;
13946                end if;
13947
13948                Next_Elmt (Iface_Elmt);
13949             end loop;
13950          end if;
13951
13952          return False;
13953       end Contain_Interface;
13954
13955       ---------------------------
13956       -- Find_Hidden_Interface --
13957       ---------------------------
13958
13959       function Find_Hidden_Interface
13960         (Src  : Elist_Id;
13961          Dest : Elist_Id) return Entity_Id
13962       is
13963          Iface      : Entity_Id;
13964          Iface_Elmt : Elmt_Id;
13965
13966       begin
13967          if Present (Src) and then Present (Dest) then
13968             Iface_Elmt := First_Elmt (Src);
13969             while Present (Iface_Elmt) loop
13970                Iface := Node (Iface_Elmt);
13971
13972                if not Contain_Interface (Iface, Dest) then
13973                   return Iface;
13974                end if;
13975
13976                Next_Elmt (Iface_Elmt);
13977             end loop;
13978          end if;
13979
13980          return Empty;
13981       end Find_Hidden_Interface;
13982
13983    --  Start of processing for Process_Full_View
13984
13985    begin
13986       --  First some sanity checks that must be done after semantic
13987       --  decoration of the full view and thus cannot be placed with other
13988       --  similar checks in Find_Type_Name
13989
13990       if not Is_Limited_Type (Priv_T)
13991         and then (Is_Limited_Type (Full_T)
13992                    or else Is_Limited_Composite (Full_T))
13993       then
13994          Error_Msg_N
13995            ("completion of nonlimited type cannot be limited", Full_T);
13996          Explain_Limited_Type (Full_T, Full_T);
13997
13998       elsif Is_Abstract (Full_T) and then not Is_Abstract (Priv_T) then
13999          Error_Msg_N
14000            ("completion of nonabstract type cannot be abstract", Full_T);
14001
14002       elsif Is_Tagged_Type (Priv_T)
14003         and then Is_Limited_Type (Priv_T)
14004         and then not Is_Limited_Type (Full_T)
14005       then
14006          --  GNAT allow its own definition of Limited_Controlled to disobey
14007          --  this rule in order in ease the implementation. The next test is
14008          --  safe because Root_Controlled is defined in a private system child
14009
14010          if Etype (Full_T) = Full_View (RTE (RE_Root_Controlled)) then
14011             Set_Is_Limited_Composite (Full_T);
14012          else
14013             Error_Msg_N
14014               ("completion of limited tagged type must be limited", Full_T);
14015          end if;
14016
14017       elsif Is_Generic_Type (Priv_T) then
14018          Error_Msg_N ("generic type cannot have a completion", Full_T);
14019       end if;
14020
14021       --  Check that ancestor interfaces of private and full views are
14022       --  consistent. We omit this check for synchronized types because
14023       --  they are performed on thecorresponding record type when frozen.
14024
14025       if Ada_Version >= Ada_05
14026         and then Is_Tagged_Type (Priv_T)
14027         and then Is_Tagged_Type (Full_T)
14028         and then Ekind (Full_T) /= E_Task_Type
14029         and then Ekind (Full_T) /= E_Protected_Type
14030       then
14031          declare
14032             Iface         : Entity_Id;
14033             Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
14034             Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
14035
14036          begin
14037             Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
14038             Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
14039
14040             --  Ada 2005 (AI-251): The partial view shall be a descendant of
14041             --  an interface type if and only if the full type is descendant
14042             --  of the interface type (AARM 7.3 (7.3/2).
14043
14044             Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
14045
14046             if Present (Iface) then
14047                Error_Msg_NE ("interface & not implemented by full type " &
14048                              "('R'M'-2005 7.3 (7.3/2))", Priv_T, Iface);
14049             end if;
14050
14051             Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
14052
14053             if Present (Iface) then
14054                Error_Msg_NE ("interface & not implemented by partial view " &
14055                              "('R'M'-2005 7.3 (7.3/2))", Full_T, Iface);
14056             end if;
14057          end;
14058       end if;
14059
14060       if Is_Tagged_Type (Priv_T)
14061         and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
14062         and then Is_Derived_Type (Full_T)
14063       then
14064          Priv_Parent := Etype (Priv_T);
14065
14066          --  The full view of a private extension may have been transformed
14067          --  into an unconstrained derived type declaration and a subtype
14068          --  declaration (see build_derived_record_type for details).
14069
14070          if Nkind (N) = N_Subtype_Declaration then
14071             Full_Indic  := Subtype_Indication (N);
14072             Full_Parent := Etype (Base_Type (Full_T));
14073          else
14074             Full_Indic  := Subtype_Indication (Type_Definition (N));
14075             Full_Parent := Etype (Full_T);
14076          end if;
14077
14078          --  Check that the parent type of the full type is a descendant of
14079          --  the ancestor subtype given in the private extension. If either
14080          --  entity has an Etype equal to Any_Type then we had some previous
14081          --  error situation [7.3(8)].
14082
14083          if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
14084             return;
14085
14086          --  Ada 2005 (AI-251): Interfaces in the full-typ can be given in
14087          --  any order. Therefore we don't have to check that its parent must
14088          --  be a descendant of the parent of the private type declaration.
14089
14090          elsif Is_Interface (Priv_Parent)
14091            and then Is_Interface (Full_Parent)
14092          then
14093             null;
14094
14095          --  Ada 2005 (AI-251): If the parent of the private type declaration
14096          --  is an interface there is no need to check that it is an ancestor
14097          --  of the associated full type declaration. The required tests for
14098          --  this case case are performed by Build_Derived_Record_Type.
14099
14100          elsif not Is_Interface (Base_Type (Priv_Parent))
14101            and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
14102          then
14103             Error_Msg_N
14104               ("parent of full type must descend from parent"
14105                   & " of private extension", Full_Indic);
14106
14107          --  Check the rules of 7.3(10): if the private extension inherits
14108          --  known discriminants, then the full type must also inherit those
14109          --  discriminants from the same (ancestor) type, and the parent
14110          --  subtype of the full type must be constrained if and only if
14111          --  the ancestor subtype of the private extension is constrained.
14112
14113          elsif No (Discriminant_Specifications (Parent (Priv_T)))
14114            and then not Has_Unknown_Discriminants (Priv_T)
14115            and then Has_Discriminants (Base_Type (Priv_Parent))
14116          then
14117             declare
14118                Priv_Indic  : constant Node_Id :=
14119                                Subtype_Indication (Parent (Priv_T));
14120
14121                Priv_Constr : constant Boolean :=
14122                                Is_Constrained (Priv_Parent)
14123                                  or else
14124                                    Nkind (Priv_Indic) = N_Subtype_Indication
14125                                  or else Is_Constrained (Entity (Priv_Indic));
14126
14127                Full_Constr : constant Boolean :=
14128                                Is_Constrained (Full_Parent)
14129                                  or else
14130                                    Nkind (Full_Indic) = N_Subtype_Indication
14131                                  or else Is_Constrained (Entity (Full_Indic));
14132
14133                Priv_Discr : Entity_Id;
14134                Full_Discr : Entity_Id;
14135
14136             begin
14137                Priv_Discr := First_Discriminant (Priv_Parent);
14138                Full_Discr := First_Discriminant (Full_Parent);
14139                while Present (Priv_Discr) and then Present (Full_Discr) loop
14140                   if Original_Record_Component (Priv_Discr) =
14141                      Original_Record_Component (Full_Discr)
14142                     or else
14143                      Corresponding_Discriminant (Priv_Discr) =
14144                      Corresponding_Discriminant (Full_Discr)
14145                   then
14146                      null;
14147                   else
14148                      exit;
14149                   end if;
14150
14151                   Next_Discriminant (Priv_Discr);
14152                   Next_Discriminant (Full_Discr);
14153                end loop;
14154
14155                if Present (Priv_Discr) or else Present (Full_Discr) then
14156                   Error_Msg_N
14157                     ("full view must inherit discriminants of the parent type"
14158                      & " used in the private extension", Full_Indic);
14159
14160                elsif Priv_Constr and then not Full_Constr then
14161                   Error_Msg_N
14162                     ("parent subtype of full type must be constrained",
14163                      Full_Indic);
14164
14165                elsif Full_Constr and then not Priv_Constr then
14166                   Error_Msg_N
14167                     ("parent subtype of full type must be unconstrained",
14168                      Full_Indic);
14169                end if;
14170             end;
14171
14172          --  Check the rules of 7.3(12): if a partial view has neither known
14173          --  or unknown discriminants, then the full type declaration shall
14174          --  define a definite subtype.
14175
14176          elsif      not Has_Unknown_Discriminants (Priv_T)
14177            and then not Has_Discriminants (Priv_T)
14178            and then not Is_Constrained (Full_T)
14179          then
14180             Error_Msg_N
14181               ("full view must define a constrained type if partial view"
14182                 & " has no discriminants", Full_T);
14183          end if;
14184
14185          --  ??????? Do we implement the following properly ?????
14186          --  If the ancestor subtype of a private extension has constrained
14187          --  discriminants, then the parent subtype of the full view shall
14188          --  impose a statically matching constraint on those discriminants
14189          --  [7.3(13)].
14190
14191       else
14192          --  For untagged types, verify that a type without discriminants
14193          --  is not completed with an unconstrained type.
14194
14195          if not Is_Indefinite_Subtype (Priv_T)
14196            and then Is_Indefinite_Subtype (Full_T)
14197          then
14198             Error_Msg_N ("full view of type must be definite subtype", Full_T);
14199          end if;
14200       end if;
14201
14202       --  AI-419: verify that the use of "limited" is consistent
14203
14204       declare
14205          Orig_Decl : constant Node_Id := Original_Node (N);
14206
14207       begin
14208          if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
14209            and then not Limited_Present (Parent (Priv_T))
14210            and then not Synchronized_Present (Parent (Priv_T))
14211            and then Nkind (Orig_Decl) = N_Full_Type_Declaration
14212            and then Nkind
14213              (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
14214            and then Limited_Present (Type_Definition (Orig_Decl))
14215          then
14216             Error_Msg_N
14217               ("full view of non-limited extension cannot be limited", N);
14218          end if;
14219       end;
14220
14221       --  Ada 2005 (AI-443): A synchronized private extension must be
14222       --  completed by a task or protected type.
14223
14224       if Ada_Version >= Ada_05
14225         and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
14226         and then Synchronized_Present (Parent (Priv_T))
14227         and then Ekind (Full_T) /= E_Task_Type
14228         and then Ekind (Full_T) /= E_Protected_Type
14229       then
14230          Error_Msg_N ("full view of synchronized extension must " &
14231                       "be synchronized type", N);
14232       end if;
14233
14234       --  Ada 2005 AI-363: if the full view has discriminants with
14235       --  defaults, it is illegal to declare constrained access subtypes
14236       --  whose designated type is the current type. This allows objects
14237       --  of the type that are declared in the heap to be unconstrained.
14238
14239       if not Has_Unknown_Discriminants (Priv_T)
14240         and then not Has_Discriminants (Priv_T)
14241         and then Has_Discriminants (Full_T)
14242         and then
14243           Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
14244       then
14245          Set_Has_Constrained_Partial_View (Full_T);
14246          Set_Has_Constrained_Partial_View (Priv_T);
14247       end if;
14248
14249       --  Create a full declaration for all its subtypes recorded in
14250       --  Private_Dependents and swap them similarly to the base type. These
14251       --  are subtypes that have been define before the full declaration of
14252       --  the private type. We also swap the entry in Private_Dependents list
14253       --  so we can properly restore the private view on exit from the scope.
14254
14255       declare
14256          Priv_Elmt : Elmt_Id;
14257          Priv      : Entity_Id;
14258          Full      : Entity_Id;
14259
14260       begin
14261          Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
14262          while Present (Priv_Elmt) loop
14263             Priv := Node (Priv_Elmt);
14264
14265             if Ekind (Priv) = E_Private_Subtype
14266               or else Ekind (Priv) = E_Limited_Private_Subtype
14267               or else Ekind (Priv) = E_Record_Subtype_With_Private
14268             then
14269                Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
14270                Set_Is_Itype (Full);
14271                Set_Parent (Full, Parent (Priv));
14272                Set_Associated_Node_For_Itype (Full, N);
14273
14274                --  Now we need to complete the private subtype, but since the
14275                --  base type has already been swapped, we must also swap the
14276                --  subtypes (and thus, reverse the arguments in the call to
14277                --  Complete_Private_Subtype).
14278
14279                Copy_And_Swap (Priv, Full);
14280                Complete_Private_Subtype (Full, Priv, Full_T, N);
14281                Replace_Elmt (Priv_Elmt, Full);
14282             end if;
14283
14284             Next_Elmt (Priv_Elmt);
14285          end loop;
14286       end;
14287
14288       --  If the private view was tagged, copy the new Primitive
14289       --  operations from the private view to the full view.
14290
14291       if Is_Tagged_Type (Full_T)
14292         and then Ekind (Full_T) /= E_Task_Type
14293         and then Ekind (Full_T) /= E_Protected_Type
14294       then
14295          declare
14296             Priv_List : Elist_Id;
14297             Full_List : constant Elist_Id := Primitive_Operations (Full_T);
14298             P1, P2    : Elmt_Id;
14299             Prim      : Entity_Id;
14300             D_Type    : Entity_Id;
14301
14302          begin
14303             if Is_Tagged_Type (Priv_T) then
14304                Priv_List := Primitive_Operations (Priv_T);
14305
14306                P1 := First_Elmt (Priv_List);
14307                while Present (P1) loop
14308                   Prim := Node (P1);
14309
14310                   --  Transfer explicit primitives, not those inherited from
14311                   --  parent of partial view, which will be re-inherited on
14312                   --  the full view.
14313
14314                   if Comes_From_Source (Prim) then
14315                      P2 := First_Elmt (Full_List);
14316                      while Present (P2) and then Node (P2) /= Prim loop
14317                         Next_Elmt (P2);
14318                      end loop;
14319
14320                      --  If not found, that is a new one
14321
14322                      if No (P2) then
14323                         Append_Elmt (Prim, Full_List);
14324                      end if;
14325                   end if;
14326
14327                   Next_Elmt (P1);
14328                end loop;
14329
14330             else
14331                --  In this case the partial view is untagged, so here we locate
14332                --  all of the earlier primitives that need to be treated as
14333                --  dispatching (those that appear between the two views). Note
14334                --  that these additional operations must all be new operations
14335                --  (any earlier operations that override inherited operations
14336                --  of the full view will already have been inserted in the
14337                --  primitives list, marked by Check_Operation_From_Private_View
14338                --  as dispatching. Note that implicit "/=" operators are
14339                --  excluded from being added to the primitives list since they
14340                --  shouldn't be treated as dispatching (tagged "/=" is handled
14341                --  specially).
14342
14343                Prim := Next_Entity (Full_T);
14344                while Present (Prim) and then Prim /= Priv_T loop
14345                   if Ekind (Prim) = E_Procedure
14346                        or else
14347                      Ekind (Prim) = E_Function
14348                   then
14349
14350                      D_Type := Find_Dispatching_Type (Prim);
14351
14352                      if D_Type = Full_T
14353                        and then (Chars (Prim) /= Name_Op_Ne
14354                                   or else Comes_From_Source (Prim))
14355                      then
14356                         Check_Controlling_Formals (Full_T, Prim);
14357
14358                         if not Is_Dispatching_Operation (Prim) then
14359                            Append_Elmt (Prim, Full_List);
14360                            Set_Is_Dispatching_Operation (Prim, True);
14361                            Set_DT_Position (Prim, No_Uint);
14362                         end if;
14363
14364                      elsif Is_Dispatching_Operation (Prim)
14365                        and then D_Type  /= Full_T
14366                      then
14367
14368                         --  Verify that it is not otherwise controlled by a
14369                         --  formal or a return value of type T.
14370
14371                         Check_Controlling_Formals (D_Type, Prim);
14372                      end if;
14373                   end if;
14374
14375                   Next_Entity (Prim);
14376                end loop;
14377             end if;
14378
14379             --  For the tagged case, the two views can share the same
14380             --  Primitive Operation list and the same class wide type.
14381             --  Update attributes of the class-wide type which depend on
14382             --  the full declaration.
14383
14384             if Is_Tagged_Type (Priv_T) then
14385                Set_Primitive_Operations (Priv_T, Full_List);
14386                Set_Class_Wide_Type
14387                  (Base_Type (Full_T), Class_Wide_Type (Priv_T));
14388
14389                Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
14390             end if;
14391          end;
14392       end if;
14393
14394       --  Ada 2005 AI 161: Check preelaboratable initialization consistency
14395
14396       if Known_To_Have_Preelab_Init (Priv_T) then
14397
14398          --  Case where there is a pragma Preelaborable_Initialization. We
14399          --  always allow this in predefined units, which is a bit of a kludge,
14400          --  but it means we don't have to struggle to meet the requirements in
14401          --  the RM for having Preelaborable Initialization. Otherwise we
14402          --  require that the type meets the RM rules. But we can't check that
14403          --  yet, because of the rule about overriding Ininitialize, so we
14404          --  simply set a flag that will be checked at freeze time.
14405
14406          if not In_Predefined_Unit (Full_T) then
14407             Set_Must_Have_Preelab_Init (Full_T);
14408          end if;
14409       end if;
14410    end Process_Full_View;
14411
14412    -----------------------------------
14413    -- Process_Incomplete_Dependents --
14414    -----------------------------------
14415
14416    procedure Process_Incomplete_Dependents
14417      (N      : Node_Id;
14418       Full_T : Entity_Id;
14419       Inc_T  : Entity_Id)
14420    is
14421       Inc_Elmt : Elmt_Id;
14422       Priv_Dep : Entity_Id;
14423       New_Subt : Entity_Id;
14424
14425       Disc_Constraint : Elist_Id;
14426
14427    begin
14428       if No (Private_Dependents (Inc_T)) then
14429          return;
14430       end if;
14431
14432       --  Itypes that may be generated by the completion of an incomplete
14433       --  subtype are not used by the back-end and not attached to the tree.
14434       --  They are created only for constraint-checking purposes.
14435
14436       Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
14437       while Present (Inc_Elmt) loop
14438          Priv_Dep := Node (Inc_Elmt);
14439
14440          if Ekind (Priv_Dep) = E_Subprogram_Type then
14441
14442             --  An Access_To_Subprogram type may have a return type or a
14443             --  parameter type that is incomplete. Replace with the full view.
14444
14445             if Etype (Priv_Dep) = Inc_T then
14446                Set_Etype (Priv_Dep, Full_T);
14447             end if;
14448
14449             declare
14450                Formal : Entity_Id;
14451
14452             begin
14453                Formal := First_Formal (Priv_Dep);
14454                while Present (Formal) loop
14455                   if Etype (Formal) = Inc_T then
14456                      Set_Etype (Formal, Full_T);
14457                   end if;
14458
14459                   Next_Formal (Formal);
14460                end loop;
14461             end;
14462
14463          elsif Is_Overloadable (Priv_Dep) then
14464
14465             --  A protected operation is never dispatching: only its
14466             --  wrapper operation (which has convention Ada) is.
14467
14468             if Is_Tagged_Type (Full_T)
14469               and then Convention (Priv_Dep) /= Convention_Protected
14470             then
14471
14472                --  Subprogram has an access parameter whose designated type
14473                --  was incomplete. Reexamine declaration now, because it may
14474                --  be a primitive operation of the full type.
14475
14476                Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
14477                Set_Is_Dispatching_Operation (Priv_Dep);
14478                Check_Controlling_Formals (Full_T, Priv_Dep);
14479             end if;
14480
14481          elsif Ekind (Priv_Dep) = E_Subprogram_Body then
14482
14483             --  Can happen during processing of a body before the completion
14484             --  of a TA type. Ignore, because spec is also on dependent list.
14485
14486             return;
14487
14488          --  Ada 2005 (AI-412): Transform a regular incomplete subtype into a
14489          --  corresponding subtype of the full view.
14490
14491          elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
14492             Set_Subtype_Indication
14493               (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
14494             Set_Etype (Priv_Dep, Full_T);
14495             Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
14496             Set_Analyzed (Parent (Priv_Dep), False);
14497
14498             --  Reanalyze the declaration, suppressing the call to
14499             --  Enter_Name to avoid duplicate names.
14500
14501             Analyze_Subtype_Declaration
14502               (N    => Parent (Priv_Dep),
14503                Skip => True);
14504
14505          --  Dependent is a subtype
14506
14507          else
14508             --  We build a new subtype indication using the full view of the
14509             --  incomplete parent. The discriminant constraints have been
14510             --  elaborated already at the point of the subtype declaration.
14511
14512             New_Subt := Create_Itype (E_Void, N);
14513
14514             if Has_Discriminants (Full_T) then
14515                Disc_Constraint := Discriminant_Constraint (Priv_Dep);
14516             else
14517                Disc_Constraint := No_Elist;
14518             end if;
14519
14520             Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
14521             Set_Full_View (Priv_Dep, New_Subt);
14522          end if;
14523
14524          Next_Elmt (Inc_Elmt);
14525       end loop;
14526    end Process_Incomplete_Dependents;
14527
14528    --------------------------------
14529    -- Process_Range_Expr_In_Decl --
14530    --------------------------------
14531
14532    procedure Process_Range_Expr_In_Decl
14533      (R           : Node_Id;
14534       T           : Entity_Id;
14535       Check_List  : List_Id := Empty_List;
14536       R_Check_Off : Boolean := False)
14537    is
14538       Lo, Hi    : Node_Id;
14539       R_Checks  : Check_Result;
14540       Type_Decl : Node_Id;
14541       Def_Id    : Entity_Id;
14542
14543    begin
14544       Analyze_And_Resolve (R, Base_Type (T));
14545
14546       if Nkind (R) = N_Range then
14547          Lo := Low_Bound (R);
14548          Hi := High_Bound (R);
14549
14550          --  We need to ensure validity of the bounds here, because if we
14551          --  go ahead and do the expansion, then the expanded code will get
14552          --  analyzed with range checks suppressed and we miss the check.
14553
14554          Validity_Check_Range (R);
14555
14556          --  If there were errors in the declaration, try and patch up some
14557          --  common mistakes in the bounds. The cases handled are literals
14558          --  which are Integer where the expected type is Real and vice versa.
14559          --  These corrections allow the compilation process to proceed further
14560          --  along since some basic assumptions of the format of the bounds
14561          --  are guaranteed.
14562
14563          if Etype (R) = Any_Type then
14564
14565             if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
14566                Rewrite (Lo,
14567                  Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
14568
14569             elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
14570                Rewrite (Hi,
14571                  Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
14572
14573             elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
14574                Rewrite (Lo,
14575                  Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
14576
14577             elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
14578                Rewrite (Hi,
14579                  Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
14580             end if;
14581
14582             Set_Etype (Lo, T);
14583             Set_Etype (Hi, T);
14584          end if;
14585
14586          --  If the bounds of the range have been mistakenly given as string
14587          --  literals (perhaps in place of character literals), then an error
14588          --  has already been reported, but we rewrite the string literal as a
14589          --  bound of the range's type to avoid blowups in later processing
14590          --  that looks at static values.
14591
14592          if Nkind (Lo) = N_String_Literal then
14593             Rewrite (Lo,
14594               Make_Attribute_Reference (Sloc (Lo),
14595                 Attribute_Name => Name_First,
14596                 Prefix => New_Reference_To (T, Sloc (Lo))));
14597             Analyze_And_Resolve (Lo);
14598          end if;
14599
14600          if Nkind (Hi) = N_String_Literal then
14601             Rewrite (Hi,
14602               Make_Attribute_Reference (Sloc (Hi),
14603                 Attribute_Name => Name_First,
14604                 Prefix => New_Reference_To (T, Sloc (Hi))));
14605             Analyze_And_Resolve (Hi);
14606          end if;
14607
14608          --  If bounds aren't scalar at this point then exit, avoiding
14609          --  problems with further processing of the range in this procedure.
14610
14611          if not Is_Scalar_Type (Etype (Lo)) then
14612             return;
14613          end if;
14614
14615          --  Resolve (actually Sem_Eval) has checked that the bounds are in
14616          --  then range of the base type. Here we check whether the bounds
14617          --  are in the range of the subtype itself. Note that if the bounds
14618          --  represent the null range the Constraint_Error exception should
14619          --  not be raised.
14620
14621          --  ??? The following code should be cleaned up as follows
14622
14623          --  1. The Is_Null_Range (Lo, Hi) test should disappear since it
14624          --     is done in the call to Range_Check (R, T); below
14625
14626          --  2. The use of R_Check_Off should be investigated and possibly
14627          --     removed, this would clean up things a bit.
14628
14629          if Is_Null_Range (Lo, Hi) then
14630             null;
14631
14632          else
14633             --  Capture values of bounds and generate temporaries for them
14634             --  if needed, before applying checks, since checks may cause
14635             --  duplication of the expression without forcing evaluation.
14636
14637             if Expander_Active then
14638                Force_Evaluation (Lo);
14639                Force_Evaluation (Hi);
14640             end if;
14641
14642             --  We use a flag here instead of suppressing checks on the
14643             --  type because the type we check against isn't necessarily
14644             --  the place where we put the check.
14645
14646             if not R_Check_Off then
14647                R_Checks := Range_Check (R, T);
14648
14649                --  Look up tree to find an appropriate insertion point.
14650                --  This seems really junk code, and very brittle, couldn't
14651                --  we just use an insert actions call of some kind ???
14652
14653                Type_Decl := Parent (R);
14654                while Present (Type_Decl) and then not
14655                  (Nkind (Type_Decl) = N_Full_Type_Declaration
14656                     or else
14657                   Nkind (Type_Decl) = N_Subtype_Declaration
14658                     or else
14659                   Nkind (Type_Decl) = N_Loop_Statement
14660                     or else
14661                   Nkind (Type_Decl) = N_Task_Type_Declaration
14662                     or else
14663                   Nkind (Type_Decl) = N_Single_Task_Declaration
14664                     or else
14665                   Nkind (Type_Decl) = N_Protected_Type_Declaration
14666                     or else
14667                   Nkind (Type_Decl) = N_Single_Protected_Declaration)
14668                loop
14669                   Type_Decl := Parent (Type_Decl);
14670                end loop;
14671
14672                --  Why would Type_Decl not be present???  Without this test,
14673                --  short regression tests fail.
14674
14675                if Present (Type_Decl) then
14676
14677                   --  Case of loop statement (more comments ???)
14678
14679                   if Nkind (Type_Decl) = N_Loop_Statement then
14680                      declare
14681                         Indic : Node_Id;
14682
14683                      begin
14684                         Indic := Parent (R);
14685                         while Present (Indic) and then not
14686                           (Nkind (Indic) = N_Subtype_Indication)
14687                         loop
14688                            Indic := Parent (Indic);
14689                         end loop;
14690
14691                         if Present (Indic) then
14692                            Def_Id := Etype (Subtype_Mark (Indic));
14693
14694                            Insert_Range_Checks
14695                              (R_Checks,
14696                               Type_Decl,
14697                               Def_Id,
14698                               Sloc (Type_Decl),
14699                               R,
14700                               Do_Before => True);
14701                         end if;
14702                      end;
14703
14704                   --  All other cases (more comments ???)
14705
14706                   else
14707                      Def_Id := Defining_Identifier (Type_Decl);
14708
14709                      if (Ekind (Def_Id) = E_Record_Type
14710                           and then Depends_On_Discriminant (R))
14711                        or else
14712                         (Ekind (Def_Id) = E_Protected_Type
14713                           and then Has_Discriminants (Def_Id))
14714                      then
14715                         Append_Range_Checks
14716                           (R_Checks, Check_List, Def_Id, Sloc (Type_Decl), R);
14717
14718                      else
14719                         Insert_Range_Checks
14720                           (R_Checks, Type_Decl, Def_Id, Sloc (Type_Decl), R);
14721
14722                      end if;
14723                   end if;
14724                end if;
14725             end if;
14726          end if;
14727
14728       elsif Expander_Active then
14729          Get_Index_Bounds (R, Lo, Hi);
14730          Force_Evaluation (Lo);
14731          Force_Evaluation (Hi);
14732       end if;
14733    end Process_Range_Expr_In_Decl;
14734
14735    --------------------------------------
14736    -- Process_Real_Range_Specification --
14737    --------------------------------------
14738
14739    procedure Process_Real_Range_Specification (Def : Node_Id) is
14740       Spec : constant Node_Id := Real_Range_Specification (Def);
14741       Lo   : Node_Id;
14742       Hi   : Node_Id;
14743       Err  : Boolean := False;
14744
14745       procedure Analyze_Bound (N : Node_Id);
14746       --  Analyze and check one bound
14747
14748       -------------------
14749       -- Analyze_Bound --
14750       -------------------
14751
14752       procedure Analyze_Bound (N : Node_Id) is
14753       begin
14754          Analyze_And_Resolve (N, Any_Real);
14755
14756          if not Is_OK_Static_Expression (N) then
14757             Flag_Non_Static_Expr
14758               ("bound in real type definition is not static!", N);
14759             Err := True;
14760          end if;
14761       end Analyze_Bound;
14762
14763    --  Start of processing for Process_Real_Range_Specification
14764
14765    begin
14766       if Present (Spec) then
14767          Lo := Low_Bound (Spec);
14768          Hi := High_Bound (Spec);
14769          Analyze_Bound (Lo);
14770          Analyze_Bound (Hi);
14771
14772          --  If error, clear away junk range specification
14773
14774          if Err then
14775             Set_Real_Range_Specification (Def, Empty);
14776          end if;
14777       end if;
14778    end Process_Real_Range_Specification;
14779
14780    ---------------------
14781    -- Process_Subtype --
14782    ---------------------
14783
14784    function Process_Subtype
14785      (S           : Node_Id;
14786       Related_Nod : Node_Id;
14787       Related_Id  : Entity_Id := Empty;
14788       Suffix      : Character := ' ') return Entity_Id
14789    is
14790       P               : Node_Id;
14791       Def_Id          : Entity_Id;
14792       Error_Node      : Node_Id;
14793       Full_View_Id    : Entity_Id;
14794       Subtype_Mark_Id : Entity_Id;
14795
14796       May_Have_Null_Exclusion : Boolean;
14797
14798       procedure Check_Incomplete (T : Entity_Id);
14799       --  Called to verify that an incomplete type is not used prematurely
14800
14801       ----------------------
14802       -- Check_Incomplete --
14803       ----------------------
14804
14805       procedure Check_Incomplete (T : Entity_Id) is
14806       begin
14807          --  Ada 2005 (AI-412): Incomplete subtypes are legal
14808
14809          if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
14810            and then
14811              not (Ada_Version >= Ada_05
14812                     and then
14813                        (Nkind (Parent (T)) = N_Subtype_Declaration
14814                           or else
14815                             (Nkind (Parent (T)) = N_Subtype_Indication
14816                                and then Nkind (Parent (Parent (T))) =
14817                                           N_Subtype_Declaration)))
14818          then
14819             Error_Msg_N ("invalid use of type before its full declaration", T);
14820          end if;
14821       end Check_Incomplete;
14822
14823    --  Start of processing for Process_Subtype
14824
14825    begin
14826       --  Case of no constraints present
14827
14828       if Nkind (S) /= N_Subtype_Indication then
14829
14830          Find_Type (S);
14831          Check_Incomplete (S);
14832          P := Parent (S);
14833
14834          --  Ada 2005 (AI-231): Static check
14835
14836          if Ada_Version >= Ada_05
14837            and then Present (P)
14838            and then Null_Exclusion_Present (P)
14839            and then Nkind (P) /= N_Access_To_Object_Definition
14840            and then not Is_Access_Type (Entity (S))
14841          then
14842             Error_Msg_N
14843               ("null-exclusion must be applied to an access type", S);
14844          end if;
14845
14846          May_Have_Null_Exclusion :=
14847            Nkind (P) = N_Access_Definition
14848            or else Nkind (P) = N_Access_Function_Definition
14849            or else Nkind (P) = N_Access_Procedure_Definition
14850            or else Nkind (P) = N_Access_To_Object_Definition
14851            or else Nkind (P) = N_Allocator
14852            or else Nkind (P) = N_Component_Definition
14853            or else Nkind (P) = N_Derived_Type_Definition
14854            or else Nkind (P) = N_Discriminant_Specification
14855            or else Nkind (P) = N_Object_Declaration
14856            or else Nkind (P) = N_Parameter_Specification
14857            or else Nkind (P) = N_Subtype_Declaration;
14858
14859          --  Create an Itype that is a duplicate of Entity (S) but with the
14860          --  null-exclusion attribute
14861
14862          if May_Have_Null_Exclusion
14863            and then Is_Access_Type (Entity (S))
14864            and then Null_Exclusion_Present (P)
14865
14866             --  No need to check the case of an access to object definition.
14867             --  It is correct to define double not-null pointers.
14868
14869             --  Example:
14870             --     type Not_Null_Int_Ptr is not null access Integer;
14871             --     type Acc is not null access Not_Null_Int_Ptr;
14872
14873            and then Nkind (P) /= N_Access_To_Object_Definition
14874          then
14875             if Can_Never_Be_Null (Entity (S)) then
14876                case Nkind (Related_Nod) is
14877                   when N_Full_Type_Declaration =>
14878                      if Nkind (Type_Definition (Related_Nod))
14879                        in N_Array_Type_Definition
14880                      then
14881                         Error_Node :=
14882                           Subtype_Indication
14883                             (Component_Definition
14884                              (Type_Definition (Related_Nod)));
14885                      else
14886                         Error_Node :=
14887                           Subtype_Indication (Type_Definition (Related_Nod));
14888                      end if;
14889
14890                   when N_Subtype_Declaration =>
14891                      Error_Node := Subtype_Indication (Related_Nod);
14892
14893                   when N_Object_Declaration =>
14894                      Error_Node := Object_Definition (Related_Nod);
14895
14896                   when N_Component_Declaration =>
14897                      Error_Node :=
14898                        Subtype_Indication (Component_Definition (Related_Nod));
14899
14900                   when others =>
14901                      pragma Assert (False);
14902                      Error_Node := Related_Nod;
14903                end case;
14904
14905                Error_Msg_N
14906                  ("null-exclusion cannot be applied to " &
14907                   "a null excluding type", Error_Node);
14908             end if;
14909
14910             Set_Etype  (S,
14911               Create_Null_Excluding_Itype
14912                 (T           => Entity (S),
14913                  Related_Nod => P));
14914             Set_Entity (S, Etype (S));
14915          end if;
14916
14917          return Entity (S);
14918
14919       --  Case of constraint present, so that we have an N_Subtype_Indication
14920       --  node (this node is created only if constraints are present).
14921
14922       else
14923          Find_Type (Subtype_Mark (S));
14924
14925          if Nkind (Parent (S)) /= N_Access_To_Object_Definition
14926            and then not
14927             (Nkind (Parent (S)) = N_Subtype_Declaration
14928               and then Is_Itype (Defining_Identifier (Parent (S))))
14929          then
14930             Check_Incomplete (Subtype_Mark (S));
14931          end if;
14932
14933          P := Parent (S);
14934          Subtype_Mark_Id := Entity (Subtype_Mark (S));
14935
14936          --  Explicit subtype declaration case
14937
14938          if Nkind (P) = N_Subtype_Declaration then
14939             Def_Id := Defining_Identifier (P);
14940
14941          --  Explicit derived type definition case
14942
14943          elsif Nkind (P) = N_Derived_Type_Definition then
14944             Def_Id := Defining_Identifier (Parent (P));
14945
14946          --  Implicit case, the Def_Id must be created as an implicit type.
14947          --  The one exception arises in the case of concurrent types, array
14948          --  and access types, where other subsidiary implicit types may be
14949          --  created and must appear before the main implicit type. In these
14950          --  cases we leave Def_Id set to Empty as a signal that Create_Itype
14951          --  has not yet been called to create Def_Id.
14952
14953          else
14954             if Is_Array_Type (Subtype_Mark_Id)
14955               or else Is_Concurrent_Type (Subtype_Mark_Id)
14956               or else Is_Access_Type (Subtype_Mark_Id)
14957             then
14958                Def_Id := Empty;
14959
14960             --  For the other cases, we create a new unattached Itype,
14961             --  and set the indication to ensure it gets attached later.
14962
14963             else
14964                Def_Id :=
14965                  Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
14966             end if;
14967          end if;
14968
14969          --  If the kind of constraint is invalid for this kind of type,
14970          --  then give an error, and then pretend no constraint was given.
14971
14972          if not Is_Valid_Constraint_Kind
14973                    (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
14974          then
14975             Error_Msg_N
14976               ("incorrect constraint for this kind of type", Constraint (S));
14977
14978             Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
14979
14980             --  Set Ekind of orphan itype, to prevent cascaded errors
14981
14982             if Present (Def_Id) then
14983                Set_Ekind (Def_Id, Ekind (Any_Type));
14984             end if;
14985
14986             --  Make recursive call, having got rid of the bogus constraint
14987
14988             return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
14989          end if;
14990
14991          --  Remaining processing depends on type
14992
14993          case Ekind (Subtype_Mark_Id) is
14994             when Access_Kind =>
14995                Constrain_Access (Def_Id, S, Related_Nod);
14996
14997             when Array_Kind =>
14998                Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
14999
15000             when Decimal_Fixed_Point_Kind =>
15001                Constrain_Decimal (Def_Id, S);
15002
15003             when Enumeration_Kind =>
15004                Constrain_Enumeration (Def_Id, S);
15005
15006             when Ordinary_Fixed_Point_Kind =>
15007                Constrain_Ordinary_Fixed (Def_Id, S);
15008
15009             when Float_Kind =>
15010                Constrain_Float (Def_Id, S);
15011
15012             when Integer_Kind =>
15013                Constrain_Integer (Def_Id, S);
15014
15015             when E_Record_Type     |
15016                  E_Record_Subtype  |
15017                  Class_Wide_Kind   |
15018                  E_Incomplete_Type =>
15019                Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
15020
15021             when Private_Kind =>
15022                Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
15023                Set_Private_Dependents (Def_Id, New_Elmt_List);
15024
15025                --  In case of an invalid constraint prevent further processing
15026                --  since the type constructed is missing expected fields.
15027
15028                if Etype (Def_Id) = Any_Type then
15029                   return Def_Id;
15030                end if;
15031
15032                --  If the full view is that of a task with discriminants,
15033                --  we must constrain both the concurrent type and its
15034                --  corresponding record type. Otherwise we will just propagate
15035                --  the constraint to the full view, if available.
15036
15037                if Present (Full_View (Subtype_Mark_Id))
15038                  and then Has_Discriminants (Subtype_Mark_Id)
15039                  and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
15040                then
15041                   Full_View_Id :=
15042                     Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
15043
15044                   Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
15045                   Constrain_Concurrent (Full_View_Id, S,
15046                     Related_Nod, Related_Id, Suffix);
15047                   Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
15048                   Set_Full_View (Def_Id, Full_View_Id);
15049
15050                   --  Introduce an explicit reference to the private subtype,
15051                   --  to prevent scope anomalies in gigi if first use appears
15052                   --  in a nested context, e.g. a later function body.
15053                   --  Should this be generated in other contexts than a full
15054                   --  type declaration?
15055
15056                   if Is_Itype (Def_Id)
15057                     and then
15058                       Nkind (Parent (P)) = N_Full_Type_Declaration
15059                   then
15060                      declare
15061                         Ref_Node : Node_Id;
15062                      begin
15063                         Ref_Node := Make_Itype_Reference (Sloc (Related_Nod));
15064                         Set_Itype (Ref_Node, Def_Id);
15065                         Insert_After (Parent (P), Ref_Node);
15066                      end;
15067                   end if;
15068
15069                else
15070                   Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
15071                end if;
15072
15073             when Concurrent_Kind  =>
15074                Constrain_Concurrent (Def_Id, S,
15075                  Related_Nod, Related_Id, Suffix);
15076
15077             when others =>
15078                Error_Msg_N ("invalid subtype mark in subtype indication", S);
15079          end case;
15080
15081          --  Size and Convention are always inherited from the base type
15082
15083          Set_Size_Info  (Def_Id,            (Subtype_Mark_Id));
15084          Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
15085
15086          return Def_Id;
15087       end if;
15088    end Process_Subtype;
15089
15090    -----------------------------
15091    -- Record_Type_Declaration --
15092    -----------------------------
15093
15094    procedure Record_Type_Declaration
15095      (T    : Entity_Id;
15096       N    : Node_Id;
15097       Prev : Entity_Id)
15098    is
15099       Loc   : constant Source_Ptr := Sloc (N);
15100       Def   : constant Node_Id    := Type_Definition (N);
15101       Inc_T : Entity_Id := Empty;
15102
15103       Is_Tagged : Boolean;
15104       Tag_Comp  : Entity_Id;
15105
15106       procedure Check_Anonymous_Access_Types (Comp_List : Node_Id);
15107       --  Ada 2005 AI-382: an access component in a record declaration can
15108       --  refer to the enclosing record, in which case it denotes the type
15109       --  itself, and not the current instance of the type. We create an
15110       --  anonymous access type for the component, and flag it as an access
15111       --  to a component, so that accessibility checks are properly performed
15112       --  on it. The declaration of the access type is placed ahead of that
15113       --  of the record, to prevent circular order-of-elaboration issues in
15114       --  Gigi. We create an incomplete type for the record declaration, which
15115       --  is the designated type of the anonymous access.
15116
15117       procedure Make_Incomplete_Type_Declaration;
15118       --  If the record type contains components that include an access to the
15119       --  current record, create an incomplete type declaration for the record,
15120       --  to be used as the designated type of the anonymous access. This is
15121       --  done only once, and only if there is no previous partial view of the
15122       --  type.
15123
15124       ----------------------------------
15125       -- Check_Anonymous_Access_Types --
15126       ----------------------------------
15127
15128       procedure Check_Anonymous_Access_Types (Comp_List : Node_Id) is
15129          Anon_Access : Entity_Id;
15130          Acc_Def     : Node_Id;
15131          Comp        : Node_Id;
15132          Comp_Def    : Node_Id;
15133          Decl        : Node_Id;
15134          Type_Def    : Node_Id;
15135
15136          function Mentions_T (Acc_Def : Node_Id) return Boolean;
15137          --  Check whether an access definition includes a reference to
15138          --  the enclosing record type. The reference can be a subtype
15139          --  mark in the access definition itself, or a 'Class attribute
15140          --  reference, or recursively a reference appearing in a parameter
15141          --  type in an access_to_subprogram definition.
15142
15143          ----------------
15144          -- Mentions_T --
15145          ----------------
15146
15147          function Mentions_T (Acc_Def : Node_Id) return Boolean is
15148             Subt : Node_Id;
15149
15150          begin
15151             if No (Access_To_Subprogram_Definition (Acc_Def)) then
15152                Subt := Subtype_Mark (Acc_Def);
15153
15154                if Nkind (Subt) = N_Identifier then
15155                   return Chars (Subt) = Chars (T);
15156
15157                --  A reference to the current type may appear as the prefix
15158                --  of a 'Class attribute.
15159
15160                elsif Nkind (Subt) = N_Attribute_Reference
15161                   and then Attribute_Name (Subt) = Name_Class
15162                   and then Is_Entity_Name (Prefix (Subt))
15163                then
15164                   return (Chars (Prefix (Subt))) = Chars (T);
15165                else
15166                   return False;
15167                end if;
15168
15169             else
15170                --  Component is an access_to_subprogram: examine its formals
15171
15172                declare
15173                   Param_Spec : Node_Id;
15174
15175                begin
15176                   Param_Spec :=
15177                     First
15178                       (Parameter_Specifications
15179                         (Access_To_Subprogram_Definition (Acc_Def)));
15180                   while Present (Param_Spec) loop
15181                      if Nkind (Parameter_Type (Param_Spec))
15182                           = N_Access_Definition
15183                        and then Mentions_T (Parameter_Type (Param_Spec))
15184                      then
15185                         return True;
15186                      end if;
15187
15188                      Next (Param_Spec);
15189                   end loop;
15190
15191                   return False;
15192                end;
15193             end if;
15194          end Mentions_T;
15195
15196       --  Start of processing for Check_Anonymous_Access_Types
15197
15198       begin
15199          if No (Comp_List) then
15200             return;
15201          end if;
15202
15203          Comp := First (Component_Items (Comp_List));
15204          while Present (Comp) loop
15205             if Nkind (Comp) = N_Component_Declaration
15206               and then Present
15207                 (Access_Definition (Component_Definition (Comp)))
15208               and then
15209                 Mentions_T (Access_Definition (Component_Definition (Comp)))
15210             then
15211                Comp_Def := Component_Definition (Comp);
15212                Acc_Def :=
15213                  Access_To_Subprogram_Definition
15214                    (Access_Definition (Comp_Def));
15215
15216                Make_Incomplete_Type_Declaration;
15217                Anon_Access :=
15218                  Make_Defining_Identifier (Loc,
15219                    Chars => New_Internal_Name ('S'));
15220
15221                --  Create a declaration for the anonymous access type: either
15222                --  an access_to_object or an access_to_subprogram.
15223
15224                if Present (Acc_Def) then
15225                   if Nkind  (Acc_Def) = N_Access_Function_Definition then
15226                      Type_Def :=
15227                        Make_Access_Function_Definition (Loc,
15228                          Parameter_Specifications =>
15229                            Parameter_Specifications (Acc_Def),
15230                          Result_Definition => Result_Definition (Acc_Def));
15231                   else
15232                      Type_Def :=
15233                        Make_Access_Procedure_Definition (Loc,
15234                          Parameter_Specifications =>
15235                            Parameter_Specifications (Acc_Def));
15236                   end if;
15237
15238                else
15239                   Type_Def :=
15240                     Make_Access_To_Object_Definition (Loc,
15241                       Subtype_Indication =>
15242                          Relocate_Node
15243                            (Subtype_Mark
15244                              (Access_Definition (Comp_Def))));
15245                end if;
15246
15247                Decl := Make_Full_Type_Declaration (Loc,
15248                   Defining_Identifier => Anon_Access,
15249                   Type_Definition => Type_Def);
15250
15251                Insert_Before (N, Decl);
15252                Analyze (Decl);
15253
15254                --  If an access to object, Preserve entity of designated type,
15255                --  for ASIS use, before rewriting the component definition.
15256
15257                if No (Acc_Def) then
15258                   declare
15259                      Desig : Entity_Id;
15260
15261                   begin
15262                      Desig := Entity (Subtype_Indication (Type_Def));
15263
15264                      --  If the access definition is to the current  record,
15265                      --  the visible entity at this point is an  incomplete
15266                      --  type. Retrieve the full view to simplify  ASIS queries
15267
15268                      if Ekind (Desig) = E_Incomplete_Type then
15269                         Desig := Full_View (Desig);
15270                      end if;
15271
15272                      Set_Entity
15273                        (Subtype_Mark (Access_Definition  (Comp_Def)), Desig);
15274                   end;
15275                end if;
15276
15277                Rewrite (Comp_Def,
15278                  Make_Component_Definition (Loc,
15279                    Subtype_Indication =>
15280                   New_Occurrence_Of (Anon_Access, Loc)));
15281                Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
15282                Set_Is_Local_Anonymous_Access (Anon_Access);
15283             end if;
15284
15285             Next (Comp);
15286          end loop;
15287
15288          if Present (Variant_Part (Comp_List)) then
15289             declare
15290                V : Node_Id;
15291             begin
15292                V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
15293                while Present (V) loop
15294                   Check_Anonymous_Access_Types (Component_List (V));
15295                   Next_Non_Pragma (V);
15296                end loop;
15297             end;
15298          end if;
15299       end Check_Anonymous_Access_Types;
15300
15301       --------------------------------------
15302       -- Make_Incomplete_Type_Declaration --
15303       --------------------------------------
15304
15305       procedure Make_Incomplete_Type_Declaration is
15306          Decl : Node_Id;
15307          H    : Entity_Id;
15308
15309       begin
15310          --  If there is a previous partial view, no need to create a new one
15311          --  If the partial view is incomplete, it is given by Prev. If it is
15312          --  a private declaration, full declaration is flagged accordingly.
15313
15314          if Prev /= T
15315            or else Has_Private_Declaration (T)
15316          then
15317             return;
15318
15319          elsif No (Inc_T) then
15320             Inc_T  := Make_Defining_Identifier (Loc, Chars (T));
15321             Decl   := Make_Incomplete_Type_Declaration (Loc, Inc_T);
15322
15323             --  Type has already been inserted into the current scope.
15324             --  Remove it, and add incomplete declaration for type, so
15325             --  that subsequent anonymous access types can use it.
15326             --  The entity is unchained from the homonym list and from
15327             --  immediate visibility. After analysis, the entity in the
15328             --  incomplete declaration becomes immediately visible in the
15329             --  record declaration that follows.
15330
15331             H := Current_Entity (T);
15332
15333             if H = T then
15334                Set_Name_Entity_Id (Chars (T), Homonym (T));
15335             else
15336                while Present (H)
15337                  and then Homonym (H) /= T
15338                loop
15339                   H := Homonym (T);
15340                end loop;
15341
15342                Set_Homonym (H, Homonym (T));
15343             end if;
15344
15345             Insert_Before (N, Decl);
15346             Analyze (Decl);
15347             Set_Full_View (Inc_T, T);
15348
15349             if Tagged_Present (Def) then
15350                Make_Class_Wide_Type (Inc_T);
15351                Set_Class_Wide_Type (T, Class_Wide_Type (Inc_T));
15352                Set_Etype (Class_Wide_Type (T), T);
15353             end if;
15354          end if;
15355       end Make_Incomplete_Type_Declaration;
15356
15357    --  Start of processing for Record_Type_Declaration
15358
15359    begin
15360       --  These flags must be initialized before calling Process_Discriminants
15361       --  because this routine makes use of them.
15362
15363       Set_Ekind               (T, E_Record_Type);
15364       Set_Etype               (T, T);
15365       Init_Size_Align         (T);
15366       Set_Abstract_Interfaces (T, No_Elist);
15367       Set_Stored_Constraint   (T, No_Elist);
15368
15369       --  Normal case
15370
15371       if Ada_Version < Ada_05
15372         or else not Interface_Present (Def)
15373       then
15374          --  The flag Is_Tagged_Type might have already been set by
15375          --  Find_Type_Name if it detected an error for declaration T. This
15376          --  arises in the case of private tagged types where the full view
15377          --  omits the word tagged.
15378
15379          Is_Tagged :=
15380            Tagged_Present (Def)
15381              or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
15382
15383          Set_Is_Tagged_Type      (T, Is_Tagged);
15384          Set_Is_Limited_Record   (T, Limited_Present (Def));
15385
15386          --  Type is abstract if full declaration carries keyword, or if
15387          --  previous partial view did.
15388
15389          Set_Is_Abstract         (T, Is_Abstract (T)
15390                                       or else Abstract_Present (Def));
15391
15392       else
15393          Is_Tagged := True;
15394          Analyze_Interface_Declaration (T, Def);
15395
15396          if Present (Discriminant_Specifications (N)) then
15397             Error_Msg_N
15398               ("interface types cannot have discriminants",
15399                 Defining_Identifier
15400                   (First (Discriminant_Specifications (N))));
15401          end if;
15402       end if;
15403
15404       --  First pass: if there are self-referential access components,
15405       --  create the required anonymous access type declarations, and if
15406       --  need be an incomplete type declaration for T itself.
15407
15408       Check_Anonymous_Access_Types (Component_List (Def));
15409
15410       if Ada_Version >= Ada_05
15411         and then Present (Interface_List (Def))
15412       then
15413          declare
15414             Iface       : Node_Id;
15415             Iface_Def   : Node_Id;
15416             Iface_Typ   : Entity_Id;
15417             Ifaces_List : Elist_Id;
15418
15419          begin
15420             Iface := First (Interface_List (Def));
15421             while Present (Iface) loop
15422                Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
15423                Iface_Def := Type_Definition (Parent (Iface_Typ));
15424
15425                if not Is_Interface (Iface_Typ) then
15426                   Error_Msg_NE ("(Ada 2005) & must be an interface",
15427                                 Iface, Iface_Typ);
15428
15429                else
15430                   --  "The declaration of a specific descendant of an
15431                   --  interface type freezes the interface type" RM 13.14
15432
15433                   Freeze_Before (N, Iface_Typ);
15434
15435                   --  Ada 2005 (AI-345): Protected interfaces can only
15436                   --  inherit from limited, synchronized or protected
15437                   --  interfaces.
15438
15439                   if Protected_Present (Def) then
15440                      if Limited_Present (Iface_Def)
15441                        or else Synchronized_Present (Iface_Def)
15442                        or else Protected_Present (Iface_Def)
15443                      then
15444                         null;
15445
15446                      elsif Task_Present (Iface_Def) then
15447                         Error_Msg_N ("(Ada 2005) protected interface cannot"
15448                           & " inherit from task interface", Iface);
15449
15450                      else
15451                         Error_Msg_N ("(Ada 2005) protected interface cannot"
15452                           & " inherit from non-limited interface", Iface);
15453                      end if;
15454
15455                   --  Ada 2005 (AI-345): Synchronized interfaces can only
15456                   --  inherit from limited and synchronized.
15457
15458                   elsif Synchronized_Present (Def) then
15459                      if Limited_Present (Iface_Def)
15460                        or else Synchronized_Present (Iface_Def)
15461                      then
15462                         null;
15463
15464                      elsif Protected_Present (Iface_Def) then
15465                         Error_Msg_N ("(Ada 2005) synchronized interface " &
15466                           "cannot inherit from protected interface", Iface);
15467
15468                      elsif Task_Present (Iface_Def) then
15469                         Error_Msg_N ("(Ada 2005) synchronized interface " &
15470                           "cannot inherit from task interface", Iface);
15471
15472                      else
15473                         Error_Msg_N ("(Ada 2005) synchronized interface " &
15474                           "cannot inherit from non-limited interface",
15475                           Iface);
15476                      end if;
15477
15478                   --  Ada 2005 (AI-345): Task interfaces can only inherit
15479                   --  from limited, synchronized or task interfaces.
15480
15481                   elsif Task_Present (Def) then
15482                      if Limited_Present (Iface_Def)
15483                        or else Synchronized_Present (Iface_Def)
15484                        or else Task_Present (Iface_Def)
15485                      then
15486                         null;
15487
15488                      elsif Protected_Present (Iface_Def) then
15489                         Error_Msg_N ("(Ada 2005) task interface cannot" &
15490                           " inherit from protected interface", Iface);
15491
15492                      else
15493                         Error_Msg_N ("(Ada 2005) task interface cannot" &
15494                           " inherit from non-limited interface", Iface);
15495                      end if;
15496                   end if;
15497                end if;
15498
15499                Next (Iface);
15500             end loop;
15501
15502             --  Ada 2005 (AI-251): Collect the list of progenitors that are not
15503             --  already in the parents.
15504
15505             Collect_Abstract_Interfaces
15506               (T                         => T,
15507                Ifaces_List               => Ifaces_List,
15508                Exclude_Parent_Interfaces => True);
15509
15510             Set_Abstract_Interfaces (T, Ifaces_List);
15511          end;
15512       end if;
15513
15514       --  Records constitute a scope for the component declarations within.
15515       --  The scope is created prior to the processing of these declarations.
15516       --  Discriminants are processed first, so that they are visible when
15517       --  processing the other components. The Ekind of the record type itself
15518       --  is set to E_Record_Type (subtypes appear as E_Record_Subtype).
15519
15520       --  Enter record scope
15521
15522       New_Scope (T);
15523
15524       --  If an incomplete or private type declaration was already given for
15525       --  the type, then this scope already exists, and the discriminants have
15526       --  been declared within. We must verify that the full declaration
15527       --  matches the incomplete one.
15528
15529       Check_Or_Process_Discriminants (N, T, Prev);
15530
15531       Set_Is_Constrained     (T, not Has_Discriminants (T));
15532       Set_Has_Delayed_Freeze (T, True);
15533
15534       --  For tagged types add a manually analyzed component corresponding
15535       --  to the component _tag, the corresponding piece of tree will be
15536       --  expanded as part of the freezing actions if it is not a CPP_Class.
15537
15538       if Is_Tagged then
15539
15540          --  Do not add the tag unless we are in expansion mode
15541
15542          if Expander_Active then
15543             Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
15544             Enter_Name (Tag_Comp);
15545
15546             Set_Is_Tag                    (Tag_Comp);
15547             Set_Is_Aliased                (Tag_Comp);
15548             Set_Ekind                     (Tag_Comp, E_Component);
15549             Set_Etype                     (Tag_Comp, RTE (RE_Tag));
15550             Set_DT_Entry_Count            (Tag_Comp, No_Uint);
15551             Set_Original_Record_Component (Tag_Comp, Tag_Comp);
15552             Init_Component_Location       (Tag_Comp);
15553
15554             --  Ada 2005 (AI-251): Addition of the Tag corresponding to all the
15555             --  implemented interfaces
15556
15557             Add_Interface_Tag_Components (N, T);
15558          end if;
15559
15560          Make_Class_Wide_Type (T);
15561          Set_Primitive_Operations (T, New_Elmt_List);
15562       end if;
15563
15564       --  We must suppress range checks when processing the components
15565       --  of a record in the presence of discriminants, since we don't
15566       --  want spurious checks to be generated during their analysis, but
15567       --  must reset the Suppress_Range_Checks flags after having processed
15568       --  the record definition.
15569
15570       --  Note: this is the only use of Kill_Range_Checks, and is a bit odd,
15571       --  couldn't we just use the normal range check suppression method here.
15572       --  That would seem cleaner ???
15573
15574       if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
15575          Set_Kill_Range_Checks (T, True);
15576          Record_Type_Definition (Def, Prev);
15577          Set_Kill_Range_Checks (T, False);
15578       else
15579          Record_Type_Definition (Def, Prev);
15580       end if;
15581
15582       --  Exit from record scope
15583
15584       End_Scope;
15585
15586       --  Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
15587       --  the implemented interfaces and associate them an aliased entity.
15588
15589       if Is_Tagged
15590         and then not Is_Empty_List (Interface_List (Def))
15591       then
15592          declare
15593             Ifaces_List : constant Elist_Id := New_Elmt_List;
15594          begin
15595             Derive_Interface_Subprograms (T, T, Ifaces_List);
15596          end;
15597       end if;
15598    end Record_Type_Declaration;
15599
15600    ----------------------------
15601    -- Record_Type_Definition --
15602    ----------------------------
15603
15604    procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
15605       Component          : Entity_Id;
15606       Ctrl_Components    : Boolean := False;
15607       Final_Storage_Only : Boolean;
15608       T                  : Entity_Id;
15609
15610    begin
15611       if Ekind (Prev_T) = E_Incomplete_Type then
15612          T := Full_View (Prev_T);
15613       else
15614          T := Prev_T;
15615       end if;
15616
15617       Final_Storage_Only := not Is_Controlled (T);
15618
15619       --  Ada 2005: check whether an explicit Limited is present in a derived
15620       --  type declaration.
15621
15622       if Nkind (Parent (Def)) = N_Derived_Type_Definition
15623         and then Limited_Present (Parent (Def))
15624       then
15625          Set_Is_Limited_Record (T);
15626       end if;
15627
15628       --  If the component list of a record type is defined by the reserved
15629       --  word null and there is no discriminant part, then the record type has
15630       --  no components and all records of the type are null records (RM 3.7)
15631       --  This procedure is also called to process the extension part of a
15632       --  record extension, in which case the current scope may have inherited
15633       --  components.
15634
15635       if No (Def)
15636         or else No (Component_List (Def))
15637         or else Null_Present (Component_List (Def))
15638       then
15639          null;
15640
15641       else
15642          Analyze_Declarations (Component_Items (Component_List (Def)));
15643
15644          if Present (Variant_Part (Component_List (Def))) then
15645             Analyze (Variant_Part (Component_List (Def)));
15646          end if;
15647       end if;
15648
15649       --  After completing the semantic analysis of the record definition,
15650       --  record components, both new and inherited, are accessible. Set
15651       --  their kind accordingly.
15652
15653       Component := First_Entity (Current_Scope);
15654       while Present (Component) loop
15655          if Ekind (Component) = E_Void then
15656             Set_Ekind (Component, E_Component);
15657             Init_Component_Location (Component);
15658          end if;
15659
15660          if Has_Task (Etype (Component)) then
15661             Set_Has_Task (T);
15662          end if;
15663
15664          if Ekind (Component) /= E_Component then
15665             null;
15666
15667          elsif Has_Controlled_Component (Etype (Component))
15668            or else (Chars (Component) /= Name_uParent
15669                     and then Is_Controlled (Etype (Component)))
15670          then
15671             Set_Has_Controlled_Component (T, True);
15672             Final_Storage_Only := Final_Storage_Only
15673               and then Finalize_Storage_Only (Etype (Component));
15674             Ctrl_Components := True;
15675          end if;
15676
15677          Next_Entity (Component);
15678       end loop;
15679
15680       --  A type is Finalize_Storage_Only only if all its controlled
15681       --  components are so.
15682
15683       if Ctrl_Components then
15684          Set_Finalize_Storage_Only (T, Final_Storage_Only);
15685       end if;
15686
15687       --  Place reference to end record on the proper entity, which may
15688       --  be a partial view.
15689
15690       if Present (Def) then
15691          Process_End_Label (Def, 'e', Prev_T);
15692       end if;
15693    end Record_Type_Definition;
15694
15695    ------------------------
15696    -- Replace_Components --
15697    ------------------------
15698
15699    procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
15700       function Process (N : Node_Id) return Traverse_Result;
15701
15702       -------------
15703       -- Process --
15704       -------------
15705
15706       function Process (N : Node_Id) return Traverse_Result is
15707          Comp : Entity_Id;
15708
15709       begin
15710          if Nkind (N) = N_Discriminant_Specification then
15711             Comp := First_Discriminant (Typ);
15712             while Present (Comp) loop
15713                if Chars (Comp) = Chars (Defining_Identifier (N)) then
15714                   Set_Defining_Identifier (N, Comp);
15715                   exit;
15716                end if;
15717
15718                Next_Discriminant (Comp);
15719             end loop;
15720
15721          elsif Nkind (N) = N_Component_Declaration then
15722             Comp := First_Component (Typ);
15723             while Present (Comp) loop
15724                if Chars (Comp) = Chars (Defining_Identifier (N)) then
15725                   Set_Defining_Identifier (N, Comp);
15726                   exit;
15727                end if;
15728
15729                Next_Component (Comp);
15730             end loop;
15731          end if;
15732
15733          return OK;
15734       end Process;
15735
15736       procedure Replace is new Traverse_Proc (Process);
15737
15738    --  Start of processing for Replace_Components
15739
15740    begin
15741       Replace (Decl);
15742    end Replace_Components;
15743
15744    -------------------------------
15745    -- Set_Completion_Referenced --
15746    -------------------------------
15747
15748    procedure Set_Completion_Referenced (E : Entity_Id) is
15749    begin
15750       --  If in main unit, mark entity that is a completion as referenced,
15751       --  warnings go on the partial view when needed.
15752
15753       if In_Extended_Main_Source_Unit (E) then
15754          Set_Referenced (E);
15755       end if;
15756    end Set_Completion_Referenced;
15757
15758    ---------------------
15759    -- Set_Fixed_Range --
15760    ---------------------
15761
15762    --  The range for fixed-point types is complicated by the fact that we
15763    --  do not know the exact end points at the time of the declaration. This
15764    --  is true for three reasons:
15765
15766    --     A size clause may affect the fudging of the end-points
15767    --     A small clause may affect the values of the end-points
15768    --     We try to include the end-points if it does not affect the size
15769
15770    --  This means that the actual end-points must be established at the point
15771    --  when the type is frozen. Meanwhile, we first narrow the range as
15772    --  permitted (so that it will fit if necessary in a small specified size),
15773    --  and then build a range subtree with these narrowed bounds.
15774
15775    --  Set_Fixed_Range constructs the range from real literal values, and sets
15776    --  the range as the Scalar_Range of the given fixed-point type entity.
15777
15778    --  The parent of this range is set to point to the entity so that it is
15779    --  properly hooked into the tree (unlike normal Scalar_Range entries for
15780    --  other scalar types, which are just pointers to the range in the
15781    --  original tree, this would otherwise be an orphan).
15782
15783    --  The tree is left unanalyzed. When the type is frozen, the processing
15784    --  in Freeze.Freeze_Fixed_Point_Type notices that the range is not
15785    --  analyzed, and uses this as an indication that it should complete
15786    --  work on the range (it will know the final small and size values).
15787
15788    procedure Set_Fixed_Range
15789      (E   : Entity_Id;
15790       Loc : Source_Ptr;
15791       Lo  : Ureal;
15792       Hi  : Ureal)
15793    is
15794       S : constant Node_Id :=
15795             Make_Range (Loc,
15796               Low_Bound  => Make_Real_Literal (Loc, Lo),
15797               High_Bound => Make_Real_Literal (Loc, Hi));
15798
15799    begin
15800       Set_Scalar_Range (E, S);
15801       Set_Parent (S, E);
15802    end Set_Fixed_Range;
15803
15804    ----------------------------------
15805    -- Set_Scalar_Range_For_Subtype --
15806    ----------------------------------
15807
15808    procedure Set_Scalar_Range_For_Subtype
15809      (Def_Id : Entity_Id;
15810       R      : Node_Id;
15811       Subt   : Entity_Id)
15812    is
15813       Kind : constant Entity_Kind :=  Ekind (Def_Id);
15814
15815    begin
15816       Set_Scalar_Range (Def_Id, R);
15817
15818       --  We need to link the range into the tree before resolving it so
15819       --  that types that are referenced, including importantly the subtype
15820       --  itself, are properly frozen (Freeze_Expression requires that the
15821       --  expression be properly linked into the tree). Of course if it is
15822       --  already linked in, then we do not disturb the current link.
15823
15824       if No (Parent (R)) then
15825          Set_Parent (R, Def_Id);
15826       end if;
15827
15828       --  Reset the kind of the subtype during analysis of the range, to
15829       --  catch possible premature use in the bounds themselves.
15830
15831       Set_Ekind (Def_Id, E_Void);
15832       Process_Range_Expr_In_Decl (R, Subt);
15833       Set_Ekind (Def_Id, Kind);
15834
15835    end Set_Scalar_Range_For_Subtype;
15836
15837    --------------------------------------------------------
15838    -- Set_Stored_Constraint_From_Discriminant_Constraint --
15839    --------------------------------------------------------
15840
15841    procedure Set_Stored_Constraint_From_Discriminant_Constraint
15842      (E : Entity_Id)
15843    is
15844    begin
15845       --  Make sure set if encountered during Expand_To_Stored_Constraint
15846
15847       Set_Stored_Constraint (E, No_Elist);
15848
15849       --  Give it the right value
15850
15851       if Is_Constrained (E) and then Has_Discriminants (E) then
15852          Set_Stored_Constraint (E,
15853            Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
15854       end if;
15855    end Set_Stored_Constraint_From_Discriminant_Constraint;
15856
15857    -------------------------------------
15858    -- Signed_Integer_Type_Declaration --
15859    -------------------------------------
15860
15861    procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
15862       Implicit_Base : Entity_Id;
15863       Base_Typ      : Entity_Id;
15864       Lo_Val        : Uint;
15865       Hi_Val        : Uint;
15866       Errs          : Boolean := False;
15867       Lo            : Node_Id;
15868       Hi            : Node_Id;
15869
15870       function Can_Derive_From (E : Entity_Id) return Boolean;
15871       --  Determine whether given bounds allow derivation from specified type
15872
15873       procedure Check_Bound (Expr : Node_Id);
15874       --  Check bound to make sure it is integral and static. If not, post
15875       --  appropriate error message and set Errs flag
15876
15877       ---------------------
15878       -- Can_Derive_From --
15879       ---------------------
15880
15881       --  Note we check both bounds against both end values, to deal with
15882       --  strange types like ones with a range of 0 .. -12341234.
15883
15884       function Can_Derive_From (E : Entity_Id) return Boolean is
15885          Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
15886          Hi : constant Uint := Expr_Value (Type_High_Bound (E));
15887       begin
15888          return Lo <= Lo_Val and then Lo_Val <= Hi
15889                   and then
15890                 Lo <= Hi_Val and then Hi_Val <= Hi;
15891       end Can_Derive_From;
15892
15893       -----------------
15894       -- Check_Bound --
15895       -----------------
15896
15897       procedure Check_Bound (Expr : Node_Id) is
15898       begin
15899          --  If a range constraint is used as an integer type definition, each
15900          --  bound of the range must be defined by a static expression of some
15901          --  integer type, but the two bounds need not have the same integer
15902          --  type (Negative bounds are allowed.) (RM 3.5.4)
15903
15904          if not Is_Integer_Type (Etype (Expr)) then
15905             Error_Msg_N
15906               ("integer type definition bounds must be of integer type", Expr);
15907             Errs := True;
15908
15909          elsif not Is_OK_Static_Expression (Expr) then
15910             Flag_Non_Static_Expr
15911               ("non-static expression used for integer type bound!", Expr);
15912             Errs := True;
15913
15914          --  The bounds are folded into literals, and we set their type to be
15915          --  universal, to avoid typing difficulties: we cannot set the type
15916          --  of the literal to the new type, because this would be a forward
15917          --  reference for the back end,  and if the original type is user-
15918          --  defined this can lead to spurious semantic errors (e.g. 2928-003).
15919
15920          else
15921             if Is_Entity_Name (Expr) then
15922                Fold_Uint (Expr, Expr_Value (Expr), True);
15923             end if;
15924
15925             Set_Etype (Expr, Universal_Integer);
15926          end if;
15927       end Check_Bound;
15928
15929    --  Start of processing for Signed_Integer_Type_Declaration
15930
15931    begin
15932       --  Create an anonymous base type
15933
15934       Implicit_Base :=
15935         Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
15936
15937       --  Analyze and check the bounds, they can be of any integer type
15938
15939       Lo := Low_Bound (Def);
15940       Hi := High_Bound (Def);
15941
15942       --  Arbitrarily use Integer as the type if either bound had an error
15943
15944       if Hi = Error or else Lo = Error then
15945          Base_Typ := Any_Integer;
15946          Set_Error_Posted (T, True);
15947
15948       --  Here both bounds are OK expressions
15949
15950       else
15951          Analyze_And_Resolve (Lo, Any_Integer);
15952          Analyze_And_Resolve (Hi, Any_Integer);
15953
15954          Check_Bound (Lo);
15955          Check_Bound (Hi);
15956
15957          if Errs then
15958             Hi := Type_High_Bound (Standard_Long_Long_Integer);
15959             Lo := Type_Low_Bound (Standard_Long_Long_Integer);
15960          end if;
15961
15962          --  Find type to derive from
15963
15964          Lo_Val := Expr_Value (Lo);
15965          Hi_Val := Expr_Value (Hi);
15966
15967          if Can_Derive_From (Standard_Short_Short_Integer) then
15968             Base_Typ := Base_Type (Standard_Short_Short_Integer);
15969
15970          elsif Can_Derive_From (Standard_Short_Integer) then
15971             Base_Typ := Base_Type (Standard_Short_Integer);
15972
15973          elsif Can_Derive_From (Standard_Integer) then
15974             Base_Typ := Base_Type (Standard_Integer);
15975
15976          elsif Can_Derive_From (Standard_Long_Integer) then
15977             Base_Typ := Base_Type (Standard_Long_Integer);
15978
15979          elsif Can_Derive_From (Standard_Long_Long_Integer) then
15980             Base_Typ := Base_Type (Standard_Long_Long_Integer);
15981
15982          else
15983             Base_Typ := Base_Type (Standard_Long_Long_Integer);
15984             Error_Msg_N ("integer type definition bounds out of range", Def);
15985             Hi := Type_High_Bound (Standard_Long_Long_Integer);
15986             Lo := Type_Low_Bound (Standard_Long_Long_Integer);
15987          end if;
15988       end if;
15989
15990       --  Complete both implicit base and declared first subtype entities
15991
15992       Set_Etype          (Implicit_Base, Base_Typ);
15993       Set_Scalar_Range   (Implicit_Base, Scalar_Range   (Base_Typ));
15994       Set_Size_Info      (Implicit_Base,                (Base_Typ));
15995       Set_RM_Size        (Implicit_Base, RM_Size        (Base_Typ));
15996       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
15997
15998       Set_Ekind          (T, E_Signed_Integer_Subtype);
15999       Set_Etype          (T, Implicit_Base);
16000
16001       Set_Size_Info      (T,                (Implicit_Base));
16002       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
16003       Set_Scalar_Range   (T, Def);
16004       Set_RM_Size        (T, UI_From_Int (Minimum_Size (T)));
16005       Set_Is_Constrained (T);
16006    end Signed_Integer_Type_Declaration;
16007
16008 end Sem_Ch3;