OSDN Git Service

2009-06-22 Javier Miranda <miranda@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_ch3.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              S E M _ C H 3                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2009, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with Atree;    use Atree;
27 with Checks;   use Checks;
28 with Debug;    use Debug;
29 with Elists;   use Elists;
30 with Einfo;    use Einfo;
31 with Errout;   use Errout;
32 with Eval_Fat; use Eval_Fat;
33 with Exp_Ch3;  use Exp_Ch3;
34 with Exp_Ch9;  use Exp_Ch9;
35 with Exp_Disp; use Exp_Disp;
36 with Exp_Dist; use Exp_Dist;
37 with Exp_Tss;  use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Fname;    use Fname;
40 with Freeze;   use Freeze;
41 with Itypes;   use Itypes;
42 with Layout;   use Layout;
43 with Lib;      use Lib;
44 with Lib.Xref; use Lib.Xref;
45 with Namet;    use Namet;
46 with Nmake;    use Nmake;
47 with Opt;      use Opt;
48 with Restrict; use Restrict;
49 with Rident;   use Rident;
50 with Rtsfind;  use Rtsfind;
51 with Sem;      use Sem;
52 with Sem_Aux;  use Sem_Aux;
53 with Sem_Case; use Sem_Case;
54 with Sem_Cat;  use Sem_Cat;
55 with Sem_Ch6;  use Sem_Ch6;
56 with Sem_Ch7;  use Sem_Ch7;
57 with Sem_Ch8;  use Sem_Ch8;
58 with Sem_Ch13; use Sem_Ch13;
59 with Sem_Disp; use Sem_Disp;
60 with Sem_Dist; use Sem_Dist;
61 with Sem_Elim; use Sem_Elim;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Mech; use Sem_Mech;
64 with Sem_Res;  use Sem_Res;
65 with Sem_Smem; use Sem_Smem;
66 with Sem_Type; use Sem_Type;
67 with Sem_Util; use Sem_Util;
68 with Sem_Warn; use Sem_Warn;
69 with Stand;    use Stand;
70 with Sinfo;    use Sinfo;
71 with Snames;   use Snames;
72 with Targparm; use Targparm;
73 with Tbuild;   use Tbuild;
74 with Ttypes;   use Ttypes;
75 with Uintp;    use Uintp;
76 with Urealp;   use Urealp;
77
78 package body Sem_Ch3 is
79
80    -----------------------
81    -- Local Subprograms --
82    -----------------------
83
84    procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
85    --  Ada 2005 (AI-251): Add the tag components corresponding to all the
86    --  abstract interface types implemented by a record type or a derived
87    --  record type.
88
89    procedure Build_Derived_Type
90      (N             : Node_Id;
91       Parent_Type   : Entity_Id;
92       Derived_Type  : Entity_Id;
93       Is_Completion : Boolean;
94       Derive_Subps  : Boolean := True);
95    --  Create and decorate a Derived_Type given the Parent_Type entity. N is
96    --  the N_Full_Type_Declaration node containing the derived type definition.
97    --  Parent_Type is the entity for the parent type in the derived type
98    --  definition and Derived_Type the actual derived type. Is_Completion must
99    --  be set to False if Derived_Type is the N_Defining_Identifier node in N
100    --  (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
101    --  completion of a private type declaration. If Is_Completion is set to
102    --  True, N is the completion of a private type declaration and Derived_Type
103    --  is different from the defining identifier inside N (i.e. Derived_Type /=
104    --  Defining_Identifier (N)). Derive_Subps indicates whether the parent
105    --  subprograms should be derived. The only case where this parameter is
106    --  False is when Build_Derived_Type is recursively called to process an
107    --  implicit derived full type for a type derived from a private type (in
108    --  that case the subprograms must only be derived for the private view of
109    --  the type).
110    --
111    --  ??? These flags need a bit of re-examination and re-documentation:
112    --  ???  are they both necessary (both seem related to the recursion)?
113
114    procedure Build_Derived_Access_Type
115      (N            : Node_Id;
116       Parent_Type  : Entity_Id;
117       Derived_Type : Entity_Id);
118    --  Subsidiary procedure to Build_Derived_Type. For a derived access type,
119    --  create an implicit base if the parent type is constrained or if the
120    --  subtype indication has a constraint.
121
122    procedure Build_Derived_Array_Type
123      (N            : Node_Id;
124       Parent_Type  : Entity_Id;
125       Derived_Type : Entity_Id);
126    --  Subsidiary procedure to Build_Derived_Type. For a derived array type,
127    --  create an implicit base if the parent type is constrained or if the
128    --  subtype indication has a constraint.
129
130    procedure Build_Derived_Concurrent_Type
131      (N            : Node_Id;
132       Parent_Type  : Entity_Id;
133       Derived_Type : Entity_Id);
134    --  Subsidiary procedure to Build_Derived_Type. For a derived task or
135    --  protected type, inherit entries and protected subprograms, check
136    --  legality of discriminant constraints if any.
137
138    procedure Build_Derived_Enumeration_Type
139      (N            : Node_Id;
140       Parent_Type  : Entity_Id;
141       Derived_Type : Entity_Id);
142    --  Subsidiary procedure to Build_Derived_Type. For a derived enumeration
143    --  type, we must create a new list of literals. Types derived from
144    --  Character and [Wide_]Wide_Character are special-cased.
145
146    procedure Build_Derived_Numeric_Type
147      (N            : Node_Id;
148       Parent_Type  : Entity_Id;
149       Derived_Type : Entity_Id);
150    --  Subsidiary procedure to Build_Derived_Type. For numeric types, create
151    --  an anonymous base type, and propagate constraint to subtype if needed.
152
153    procedure Build_Derived_Private_Type
154      (N             : Node_Id;
155       Parent_Type   : Entity_Id;
156       Derived_Type  : Entity_Id;
157       Is_Completion : Boolean;
158       Derive_Subps  : Boolean := True);
159    --  Subsidiary procedure to Build_Derived_Type. This procedure is complex
160    --  because the parent may or may not have a completion, and the derivation
161    --  may itself be a completion.
162
163    procedure Build_Derived_Record_Type
164      (N            : Node_Id;
165       Parent_Type  : Entity_Id;
166       Derived_Type : Entity_Id;
167       Derive_Subps : Boolean := True);
168    --  Subsidiary procedure for Build_Derived_Type and
169    --  Analyze_Private_Extension_Declaration used for tagged and untagged
170    --  record types. All parameters are as in Build_Derived_Type except that
171    --  N, in addition to being an N_Full_Type_Declaration node, can also be an
172    --  N_Private_Extension_Declaration node. See the definition of this routine
173    --  for much more info. Derive_Subps indicates whether subprograms should
174    --  be derived from the parent type. The only case where Derive_Subps is
175    --  False is for an implicit derived full type for a type derived from a
176    --  private type (see Build_Derived_Type).
177
178    procedure Build_Discriminal (Discrim : Entity_Id);
179    --  Create the discriminal corresponding to discriminant Discrim, that is
180    --  the parameter corresponding to Discrim to be used in initialization
181    --  procedures for the type where Discrim is a discriminant. Discriminals
182    --  are not used during semantic analysis, and are not fully defined
183    --  entities until expansion. Thus they are not given a scope until
184    --  initialization procedures are built.
185
186    function Build_Discriminant_Constraints
187      (T           : Entity_Id;
188       Def         : Node_Id;
189       Derived_Def : Boolean := False) return Elist_Id;
190    --  Validate discriminant constraints and return the list of the constraints
191    --  in order of discriminant declarations, where T is the discriminated
192    --  unconstrained type. Def is the N_Subtype_Indication node where the
193    --  discriminants constraints for T are specified. Derived_Def is True
194    --  when building the discriminant constraints in a derived type definition
195    --  of the form "type D (...) is new T (xxx)". In this case T is the parent
196    --  type and Def is the constraint "(xxx)" on T and this routine sets the
197    --  Corresponding_Discriminant field of the discriminants in the derived
198    --  type D to point to the corresponding discriminants in the parent type T.
199
200    procedure Build_Discriminated_Subtype
201      (T           : Entity_Id;
202       Def_Id      : Entity_Id;
203       Elist       : Elist_Id;
204       Related_Nod : Node_Id;
205       For_Access  : Boolean := False);
206    --  Subsidiary procedure to Constrain_Discriminated_Type and to
207    --  Process_Incomplete_Dependents. Given
208    --
209    --     T (a possibly discriminated base type)
210    --     Def_Id (a very partially built subtype for T),
211    --
212    --  the call completes Def_Id to be the appropriate E_*_Subtype.
213    --
214    --  The Elist is the list of discriminant constraints if any (it is set
215    --  to No_Elist if T is not a discriminated type, and to an empty list if
216    --  T has discriminants but there are no discriminant constraints). The
217    --  Related_Nod is the same as Decl_Node in Create_Constrained_Components.
218    --  The For_Access says whether or not this subtype is really constraining
219    --  an access type. That is its sole purpose is the designated type of an
220    --  access type -- in which case a Private_Subtype Is_For_Access_Subtype
221    --  is built to avoid freezing T when the access subtype is frozen.
222
223    function Build_Scalar_Bound
224      (Bound : Node_Id;
225       Par_T : Entity_Id;
226       Der_T : Entity_Id) return Node_Id;
227    --  The bounds of a derived scalar type are conversions of the bounds of
228    --  the parent type. Optimize the representation if the bounds are literals.
229    --  Needs a more complete spec--what are the parameters exactly, and what
230    --  exactly is the returned value, and how is Bound affected???
231
232    procedure Build_Itype_Reference
233      (Ityp : Entity_Id;
234       Nod  : Node_Id);
235    --  Create a reference to an internal type, for use by Gigi. The back-end
236    --  elaborates itypes on demand, i.e. when their first use is seen. This
237    --  can lead to scope anomalies if the first use is within a scope that is
238    --  nested within the scope that contains  the point of definition of the
239    --  itype. The Itype_Reference node forces the elaboration of the itype
240    --  in the proper scope. The node is inserted after Nod, which is the
241    --  enclosing declaration that generated Ityp.
242    --
243    --  A related mechanism is used during expansion, for itypes created in
244    --  branches of conditionals. See Ensure_Defined in exp_util.
245    --  Could both mechanisms be merged ???
246
247    procedure Build_Underlying_Full_View
248      (N   : Node_Id;
249       Typ : Entity_Id;
250       Par : Entity_Id);
251    --  If the completion of a private type is itself derived from a private
252    --  type, or if the full view of a private subtype is itself private, the
253    --  back-end has no way to compute the actual size of this type. We build
254    --  an internal subtype declaration of the proper parent type to convey
255    --  this information. This extra mechanism is needed because a full
256    --  view cannot itself have a full view (it would get clobbered during
257    --  view exchanges).
258
259    procedure Check_Access_Discriminant_Requires_Limited
260      (D   : Node_Id;
261       Loc : Node_Id);
262    --  Check the restriction that the type to which an access discriminant
263    --  belongs must be a concurrent type or a descendant of a type with
264    --  the reserved word 'limited' in its declaration.
265
266    procedure Check_Anonymous_Access_Components
267       (Typ_Decl  : Node_Id;
268        Typ       : Entity_Id;
269        Prev      : Entity_Id;
270        Comp_List : Node_Id);
271    --  Ada 2005 AI-382: an access component in a record definition can refer to
272    --  the enclosing record, in which case it denotes the type itself, and not
273    --  the current instance of the type. We create an anonymous access type for
274    --  the component, and flag it as an access to a component, so accessibility
275    --  checks are properly performed on it. The declaration of the access type
276    --  is placed ahead of that of the record to prevent order-of-elaboration
277    --  circularity issues in Gigi. We create an incomplete type for the record
278    --  declaration, which is the designated type of the anonymous access.
279
280    procedure Check_Delta_Expression (E : Node_Id);
281    --  Check that the expression represented by E is suitable for use as a
282    --  delta expression, i.e. it is of real type and is static.
283
284    procedure Check_Digits_Expression (E : Node_Id);
285    --  Check that the expression represented by E is suitable for use as a
286    --  digits expression, i.e. it is of integer type, positive and static.
287
288    procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
289    --  Validate the initialization of an object declaration. T is the required
290    --  type, and Exp is the initialization expression.
291
292    procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
293    --  Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
294
295    procedure Check_Or_Process_Discriminants
296      (N    : Node_Id;
297       T    : Entity_Id;
298       Prev : Entity_Id := Empty);
299    --  If T is the full declaration of an incomplete or private type, check the
300    --  conformance of the discriminants, otherwise process them. Prev is the
301    --  entity of the partial declaration, if any.
302
303    procedure Check_Real_Bound (Bound : Node_Id);
304    --  Check given bound for being of real type and static. If not, post an
305    --  appropriate message, and rewrite the bound with the real literal zero.
306
307    procedure Constant_Redeclaration
308      (Id : Entity_Id;
309       N  : Node_Id;
310       T  : out Entity_Id);
311    --  Various checks on legality of full declaration of deferred constant.
312    --  Id is the entity for the redeclaration, N is the N_Object_Declaration,
313    --  node. The caller has not yet set any attributes of this entity.
314
315    function Contain_Interface
316      (Iface  : Entity_Id;
317       Ifaces : Elist_Id) return Boolean;
318    --  Ada 2005: Determine whether Iface is present in the list Ifaces
319
320    procedure Convert_Scalar_Bounds
321      (N            : Node_Id;
322       Parent_Type  : Entity_Id;
323       Derived_Type : Entity_Id;
324       Loc          : Source_Ptr);
325    --  For derived scalar types, convert the bounds in the type definition to
326    --  the derived type, and complete their analysis. Given a constraint of the
327    --  form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
328    --  T'Base, the parent_type. The bounds of the derived type (the anonymous
329    --  base) are copies of Lo and Hi. Finally, the bounds of the derived
330    --  subtype are conversions of those bounds to the derived_type, so that
331    --  their typing is consistent.
332
333    procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
334    --  Copies attributes from array base type T2 to array base type T1. Copies
335    --  only attributes that apply to base types, but not subtypes.
336
337    procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
338    --  Copies attributes from array subtype T2 to array subtype T1. Copies
339    --  attributes that apply to both subtypes and base types.
340
341    procedure Create_Constrained_Components
342      (Subt        : Entity_Id;
343       Decl_Node   : Node_Id;
344       Typ         : Entity_Id;
345       Constraints : Elist_Id);
346    --  Build the list of entities for a constrained discriminated record
347    --  subtype. If a component depends on a discriminant, replace its subtype
348    --  using the discriminant values in the discriminant constraint. Subt
349    --  is the defining identifier for the subtype whose list of constrained
350    --  entities we will create. Decl_Node is the type declaration node where
351    --  we will attach all the itypes created. Typ is the base discriminated
352    --  type for the subtype Subt. Constraints is the list of discriminant
353    --  constraints for Typ.
354
355    function Constrain_Component_Type
356      (Comp            : Entity_Id;
357       Constrained_Typ : Entity_Id;
358       Related_Node    : Node_Id;
359       Typ             : Entity_Id;
360       Constraints     : Elist_Id) return Entity_Id;
361    --  Given a discriminated base type Typ, a list of discriminant constraint
362    --  Constraints for Typ and a component of Typ, with type Compon_Type,
363    --  create and return the type corresponding to Compon_type where all
364    --  discriminant references are replaced with the corresponding constraint.
365    --  If no discriminant references occur in Compon_Typ then return it as is.
366    --  Constrained_Typ is the final constrained subtype to which the
367    --  constrained Compon_Type belongs. Related_Node is the node where we will
368    --  attach all the itypes created.
369    --
370    --  Above description is confused, what is Compon_Type???
371
372    procedure Constrain_Access
373      (Def_Id      : in out Entity_Id;
374       S           : Node_Id;
375       Related_Nod : Node_Id);
376    --  Apply a list of constraints to an access type. If Def_Id is empty, it is
377    --  an anonymous type created for a subtype indication. In that case it is
378    --  created in the procedure and attached to Related_Nod.
379
380    procedure Constrain_Array
381      (Def_Id      : in out Entity_Id;
382       SI          : Node_Id;
383       Related_Nod : Node_Id;
384       Related_Id  : Entity_Id;
385       Suffix      : Character);
386    --  Apply a list of index constraints to an unconstrained array type. The
387    --  first parameter is the entity for the resulting subtype. A value of
388    --  Empty for Def_Id indicates that an implicit type must be created, but
389    --  creation is delayed (and must be done by this procedure) because other
390    --  subsidiary implicit types must be created first (which is why Def_Id
391    --  is an in/out parameter). The second parameter is a subtype indication
392    --  node for the constrained array to be created (e.g. something of the
393    --  form string (1 .. 10)). Related_Nod gives the place where this type
394    --  has to be inserted in the tree. The Related_Id and Suffix parameters
395    --  are used to build the associated Implicit type name.
396
397    procedure Constrain_Concurrent
398      (Def_Id      : in out Entity_Id;
399       SI          : Node_Id;
400       Related_Nod : Node_Id;
401       Related_Id  : Entity_Id;
402       Suffix      : Character);
403    --  Apply list of discriminant constraints to an unconstrained concurrent
404    --  type.
405    --
406    --    SI is the N_Subtype_Indication node containing the constraint and
407    --    the unconstrained type to constrain.
408    --
409    --    Def_Id is the entity for the resulting constrained subtype. A value
410    --    of Empty for Def_Id indicates that an implicit type must be created,
411    --    but creation is delayed (and must be done by this procedure) because
412    --    other subsidiary implicit types must be created first (which is why
413    --    Def_Id is an in/out parameter).
414    --
415    --    Related_Nod gives the place where this type has to be inserted
416    --    in the tree
417    --
418    --  The last two arguments are used to create its external name if needed.
419
420    function Constrain_Corresponding_Record
421      (Prot_Subt   : Entity_Id;
422       Corr_Rec    : Entity_Id;
423       Related_Nod : Node_Id;
424       Related_Id  : Entity_Id) return Entity_Id;
425    --  When constraining a protected type or task type with discriminants,
426    --  constrain the corresponding record with the same discriminant values.
427
428    procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
429    --  Constrain a decimal fixed point type with a digits constraint and/or a
430    --  range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
431
432    procedure Constrain_Discriminated_Type
433      (Def_Id      : Entity_Id;
434       S           : Node_Id;
435       Related_Nod : Node_Id;
436       For_Access  : Boolean := False);
437    --  Process discriminant constraints of composite type. Verify that values
438    --  have been provided for all discriminants, that the original type is
439    --  unconstrained, and that the types of the supplied expressions match
440    --  the discriminant types. The first three parameters are like in routine
441    --  Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
442    --  of For_Access.
443
444    procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
445    --  Constrain an enumeration type with a range constraint. This is identical
446    --  to Constrain_Integer, but for the Ekind of the resulting subtype.
447
448    procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
449    --  Constrain a floating point type with either a digits constraint
450    --  and/or a range constraint, building a E_Floating_Point_Subtype.
451
452    procedure Constrain_Index
453      (Index        : Node_Id;
454       S            : Node_Id;
455       Related_Nod  : Node_Id;
456       Related_Id   : Entity_Id;
457       Suffix       : Character;
458       Suffix_Index : Nat);
459    --  Process an index constraint in a constrained array declaration. The
460    --  constraint can be a subtype name, or a range with or without an explicit
461    --  subtype mark. The index is the corresponding index of the unconstrained
462    --  array. The Related_Id and Suffix parameters are used to build the
463    --  associated Implicit type name.
464
465    procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
466    --  Build subtype of a signed or modular integer type
467
468    procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
469    --  Constrain an ordinary fixed point type with a range constraint, and
470    --  build an E_Ordinary_Fixed_Point_Subtype entity.
471
472    procedure Copy_And_Swap (Priv, Full : Entity_Id);
473    --  Copy the Priv entity into the entity of its full declaration then swap
474    --  the two entities in such a manner that the former private type is now
475    --  seen as a full type.
476
477    procedure Decimal_Fixed_Point_Type_Declaration
478      (T   : Entity_Id;
479       Def : Node_Id);
480    --  Create a new decimal fixed point type, and apply the constraint to
481    --  obtain a subtype of this new type.
482
483    procedure Complete_Private_Subtype
484      (Priv        : Entity_Id;
485       Full        : Entity_Id;
486       Full_Base   : Entity_Id;
487       Related_Nod : Node_Id);
488    --  Complete the implicit full view of a private subtype by setting the
489    --  appropriate semantic fields. If the full view of the parent is a record
490    --  type, build constrained components of subtype.
491
492    procedure Derive_Progenitor_Subprograms
493      (Parent_Type : Entity_Id;
494       Tagged_Type : Entity_Id);
495    --  Ada 2005 (AI-251): To complete type derivation, collect the primitive
496    --  operations of progenitors of Tagged_Type, and replace the subsidiary
497    --  subtypes with Tagged_Type, to build the specs of the inherited interface
498    --  primitives. The derived primitives are aliased to those of the
499    --  interface. This routine takes care also of transferring to the full-view
500    --  subprograms associated with the partial-view of Tagged_Type that cover
501    --  interface primitives.
502
503    procedure Derived_Standard_Character
504      (N             : Node_Id;
505       Parent_Type   : Entity_Id;
506       Derived_Type  : Entity_Id);
507    --  Subsidiary procedure to Build_Derived_Enumeration_Type which handles
508    --  derivations from types Standard.Character and Standard.Wide_Character.
509
510    procedure Derived_Type_Declaration
511      (T             : Entity_Id;
512       N             : Node_Id;
513       Is_Completion : Boolean);
514    --  Process a derived type declaration. Build_Derived_Type is invoked
515    --  to process the actual derived type definition. Parameters N and
516    --  Is_Completion have the same meaning as in Build_Derived_Type.
517    --  T is the N_Defining_Identifier for the entity defined in the
518    --  N_Full_Type_Declaration node N, that is T is the derived type.
519
520    procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
521    --  Insert each literal in symbol table, as an overloadable identifier. Each
522    --  enumeration type is mapped into a sequence of integers, and each literal
523    --  is defined as a constant with integer value. If any of the literals are
524    --  character literals, the type is a character type, which means that
525    --  strings are legal aggregates for arrays of components of the type.
526
527    function Expand_To_Stored_Constraint
528      (Typ        : Entity_Id;
529       Constraint : Elist_Id) return Elist_Id;
530    --  Given a constraint (i.e. a list of expressions) on the discriminants of
531    --  Typ, expand it into a constraint on the stored discriminants and return
532    --  the new list of expressions constraining the stored discriminants.
533
534    function Find_Type_Of_Object
535      (Obj_Def     : Node_Id;
536       Related_Nod : Node_Id) return Entity_Id;
537    --  Get type entity for object referenced by Obj_Def, attaching the
538    --  implicit types generated to Related_Nod
539
540    procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
541    --  Create a new float and apply the constraint to obtain subtype of it
542
543    function Has_Range_Constraint (N : Node_Id) return Boolean;
544    --  Given an N_Subtype_Indication node N, return True if a range constraint
545    --  is present, either directly, or as part of a digits or delta constraint.
546    --  In addition, a digits constraint in the decimal case returns True, since
547    --  it establishes a default range if no explicit range is present.
548
549    function Inherit_Components
550      (N             : Node_Id;
551       Parent_Base   : Entity_Id;
552       Derived_Base  : Entity_Id;
553       Is_Tagged     : Boolean;
554       Inherit_Discr : Boolean;
555       Discs         : Elist_Id) return Elist_Id;
556    --  Called from Build_Derived_Record_Type to inherit the components of
557    --  Parent_Base (a base type) into the Derived_Base (the derived base type).
558    --  For more information on derived types and component inheritance please
559    --  consult the comment above the body of Build_Derived_Record_Type.
560    --
561    --    N is the original derived type declaration
562    --
563    --    Is_Tagged is set if we are dealing with tagged types
564    --
565    --    If Inherit_Discr is set, Derived_Base inherits its discriminants from
566    --    Parent_Base, otherwise no discriminants are inherited.
567    --
568    --    Discs gives the list of constraints that apply to Parent_Base in the
569    --    derived type declaration. If Discs is set to No_Elist, then we have
570    --    the following situation:
571    --
572    --      type Parent (D1..Dn : ..) is [tagged] record ...;
573    --      type Derived is new Parent [with ...];
574    --
575    --    which gets treated as
576    --
577    --      type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
578    --
579    --  For untagged types the returned value is an association list. The list
580    --  starts from the association (Parent_Base => Derived_Base), and then it
581    --  contains a sequence of the associations of the form
582    --
583    --    (Old_Component => New_Component),
584    --
585    --  where Old_Component is the Entity_Id of a component in Parent_Base and
586    --  New_Component is the Entity_Id of the corresponding component in
587    --  Derived_Base. For untagged records, this association list is needed when
588    --  copying the record declaration for the derived base. In the tagged case
589    --  the value returned is irrelevant.
590
591    function Is_Progenitor
592      (Iface : Entity_Id;
593       Typ   :  Entity_Id) return Boolean;
594    --  Determine whether type Typ implements interface Iface. This requires
595    --  traversing the list of abstract interfaces of the type, as well as that
596    --  of the ancestor types. The predicate is used to determine when a formal
597    --  in the signature of an inherited operation must carry the derived type.
598
599    function Is_Valid_Constraint_Kind
600      (T_Kind          : Type_Kind;
601       Constraint_Kind : Node_Kind) return Boolean;
602    --  Returns True if it is legal to apply the given kind of constraint to the
603    --  given kind of type (index constraint to an array type, for example).
604
605    procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
606    --  Create new modular type. Verify that modulus is in bounds and is
607    --  a power of two (implementation restriction).
608
609    procedure New_Concatenation_Op (Typ : Entity_Id);
610    --  Create an abbreviated declaration for an operator in order to
611    --  materialize concatenation on array types.
612
613    procedure Ordinary_Fixed_Point_Type_Declaration
614      (T   : Entity_Id;
615       Def : Node_Id);
616    --  Create a new ordinary fixed point type, and apply the constraint to
617    --  obtain subtype of it.
618
619    procedure Prepare_Private_Subtype_Completion
620      (Id          : Entity_Id;
621       Related_Nod : Node_Id);
622    --  Id is a subtype of some private type. Creates the full declaration
623    --  associated with Id whenever possible, i.e. when the full declaration
624    --  of the base type is already known. Records each subtype into
625    --  Private_Dependents of the base type.
626
627    procedure Process_Incomplete_Dependents
628      (N      : Node_Id;
629       Full_T : Entity_Id;
630       Inc_T  : Entity_Id);
631    --  Process all entities that depend on an incomplete type. There include
632    --  subtypes, subprogram types that mention the incomplete type in their
633    --  profiles, and subprogram with access parameters that designate the
634    --  incomplete type.
635
636    --  Inc_T is the defining identifier of an incomplete type declaration, its
637    --  Ekind is E_Incomplete_Type.
638    --
639    --    N is the corresponding N_Full_Type_Declaration for Inc_T.
640    --
641    --    Full_T is N's defining identifier.
642    --
643    --  Subtypes of incomplete types with discriminants are completed when the
644    --  parent type is. This is simpler than private subtypes, because they can
645    --  only appear in the same scope, and there is no need to exchange views.
646    --  Similarly, access_to_subprogram types may have a parameter or a return
647    --  type that is an incomplete type, and that must be replaced with the
648    --  full type.
649    --
650    --  If the full type is tagged, subprogram with access parameters that
651    --  designated the incomplete may be primitive operations of the full type,
652    --  and have to be processed accordingly.
653
654    procedure Process_Real_Range_Specification (Def : Node_Id);
655    --  Given the type definition for a real type, this procedure processes and
656    --  checks the real range specification of this type definition if one is
657    --  present. If errors are found, error messages are posted, and the
658    --  Real_Range_Specification of Def is reset to Empty.
659
660    procedure Record_Type_Declaration
661      (T    : Entity_Id;
662       N    : Node_Id;
663       Prev : Entity_Id);
664    --  Process a record type declaration (for both untagged and tagged
665    --  records). Parameters T and N are exactly like in procedure
666    --  Derived_Type_Declaration, except that no flag Is_Completion is needed
667    --  for this routine. If this is the completion of an incomplete type
668    --  declaration, Prev is the entity of the incomplete declaration, used for
669    --  cross-referencing. Otherwise Prev = T.
670
671    procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
672    --  This routine is used to process the actual record type definition (both
673    --  for untagged and tagged records). Def is a record type definition node.
674    --  This procedure analyzes the components in this record type definition.
675    --  Prev_T is the entity for the enclosing record type. It is provided so
676    --  that its Has_Task flag can be set if any of the component have Has_Task
677    --  set. If the declaration is the completion of an incomplete type
678    --  declaration, Prev_T is the original incomplete type, whose full view is
679    --  the record type.
680
681    procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
682    --  Subsidiary to Build_Derived_Record_Type. For untagged records, we
683    --  build a copy of the declaration tree of the parent, and we create
684    --  independently the list of components for the derived type. Semantic
685    --  information uses the component entities, but record representation
686    --  clauses are validated on the declaration tree. This procedure replaces
687    --  discriminants and components in the declaration with those that have
688    --  been created by Inherit_Components.
689
690    procedure Set_Fixed_Range
691      (E   : Entity_Id;
692       Loc : Source_Ptr;
693       Lo  : Ureal;
694       Hi  : Ureal);
695    --  Build a range node with the given bounds and set it as the Scalar_Range
696    --  of the given fixed-point type entity. Loc is the source location used
697    --  for the constructed range. See body for further details.
698
699    procedure Set_Scalar_Range_For_Subtype
700      (Def_Id : Entity_Id;
701       R      : Node_Id;
702       Subt   : Entity_Id);
703    --  This routine is used to set the scalar range field for a subtype given
704    --  Def_Id, the entity for the subtype, and R, the range expression for the
705    --  scalar range. Subt provides the parent subtype to be used to analyze,
706    --  resolve, and check the given range.
707
708    procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
709    --  Create a new signed integer entity, and apply the constraint to obtain
710    --  the required first named subtype of this type.
711
712    procedure Set_Stored_Constraint_From_Discriminant_Constraint
713      (E : Entity_Id);
714    --  E is some record type. This routine computes E's Stored_Constraint
715    --  from its Discriminant_Constraint.
716
717    procedure Diagnose_Interface (N : Node_Id;  E : Entity_Id);
718    --  Check that an entity in a list of progenitors is an interface,
719    --  emit error otherwise.
720
721    -----------------------
722    -- Access_Definition --
723    -----------------------
724
725    function Access_Definition
726      (Related_Nod : Node_Id;
727       N           : Node_Id) return Entity_Id
728    is
729       Loc                 : constant Source_Ptr := Sloc (Related_Nod);
730       Anon_Type           : Entity_Id;
731       Anon_Scope          : Entity_Id;
732       Desig_Type          : Entity_Id;
733       Decl                : Entity_Id;
734       Enclosing_Prot_Type : Entity_Id := Empty;
735
736    begin
737       if Is_Entry (Current_Scope)
738         and then Is_Task_Type (Etype (Scope (Current_Scope)))
739       then
740          Error_Msg_N ("task entries cannot have access parameters", N);
741          return Empty;
742       end if;
743
744       --  Ada 2005: for an object declaration the corresponding anonymous
745       --  type is declared in the current scope.
746
747       --  If the access definition is the return type of another access to
748       --  function, scope is the current one, because it is the one of the
749       --  current type declaration.
750
751       if Nkind_In (Related_Nod, N_Object_Declaration,
752                                 N_Access_Function_Definition)
753       then
754          Anon_Scope := Current_Scope;
755
756       --  For the anonymous function result case, retrieve the scope of the
757       --  function specification's associated entity rather than using the
758       --  current scope. The current scope will be the function itself if the
759       --  formal part is currently being analyzed, but will be the parent scope
760       --  in the case of a parameterless function, and we always want to use
761       --  the function's parent scope. Finally, if the function is a child
762       --  unit, we must traverse the tree to retrieve the proper entity.
763
764       elsif Nkind (Related_Nod) = N_Function_Specification
765         and then Nkind (Parent (N)) /= N_Parameter_Specification
766       then
767          --  If the current scope is a protected type, the anonymous access
768          --  is associated with one of the protected operations, and must
769          --  be available in the scope that encloses the protected declaration.
770          --  Otherwise the type is in the scope enclosing the subprogram.
771          --  If the function has formals, The return type of a subprogram
772          --  declaration is analyzed in the scope of the subprogram (see
773          --  Process_Formals) and thus the protected type, if present, is
774          --  the scope of the current function scope.
775
776          if Ekind (Current_Scope) = E_Protected_Type then
777             Enclosing_Prot_Type := Current_Scope;
778
779          elsif Ekind (Current_Scope) = E_Function
780            and then Ekind (Scope (Current_Scope)) = E_Protected_Type
781          then
782             Enclosing_Prot_Type := Scope (Current_Scope);
783          end if;
784
785          if Present (Enclosing_Prot_Type) then
786             Anon_Scope := Scope (Enclosing_Prot_Type);
787
788          else
789             Anon_Scope := Scope (Defining_Entity (Related_Nod));
790          end if;
791
792       else
793          --  For access formals, access components, and access discriminants,
794          --  the scope is that of the enclosing declaration,
795
796          Anon_Scope := Scope (Current_Scope);
797       end if;
798
799       Anon_Type :=
800         Create_Itype
801          (E_Anonymous_Access_Type, Related_Nod, Scope_Id =>  Anon_Scope);
802
803       if All_Present (N)
804         and then Ada_Version >= Ada_05
805       then
806          Error_Msg_N ("ALL is not permitted for anonymous access types", N);
807       end if;
808
809       --  Ada 2005 (AI-254): In case of anonymous access to subprograms call
810       --  the corresponding semantic routine
811
812       if Present (Access_To_Subprogram_Definition (N)) then
813          Access_Subprogram_Declaration
814            (T_Name => Anon_Type,
815             T_Def  => Access_To_Subprogram_Definition (N));
816
817          if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
818             Set_Ekind
819               (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
820          else
821             Set_Ekind
822               (Anon_Type, E_Anonymous_Access_Subprogram_Type);
823          end if;
824
825          Set_Can_Use_Internal_Rep
826            (Anon_Type, not Always_Compatible_Rep_On_Target);
827
828          --  If the anonymous access is associated with a protected operation
829          --  create a reference to it after the enclosing protected definition
830          --  because the itype will be used in the subsequent bodies.
831
832          if Ekind (Current_Scope) = E_Protected_Type then
833             Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
834          end if;
835
836          return Anon_Type;
837       end if;
838
839       Find_Type (Subtype_Mark (N));
840       Desig_Type := Entity (Subtype_Mark (N));
841
842       Set_Directly_Designated_Type
843                              (Anon_Type, Desig_Type);
844       Set_Etype              (Anon_Type, Anon_Type);
845
846       --  Make sure the anonymous access type has size and alignment fields
847       --  set, as required by gigi. This is necessary in the case of the
848       --  Task_Body_Procedure.
849
850       if not Has_Private_Component (Desig_Type) then
851          Layout_Type (Anon_Type);
852       end if;
853
854       --  ???The following makes no sense, because Anon_Type is an access type
855       --  and therefore cannot have components, private or otherwise. Hence
856       --  the assertion. Not sure what was meant, here.
857       Set_Depends_On_Private (Anon_Type, Has_Private_Component (Anon_Type));
858       pragma Assert (not Depends_On_Private (Anon_Type));
859
860       --  Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
861       --  from Ada 95 semantics. In Ada 2005, anonymous access must specify if
862       --  the null value is allowed. In Ada 95 the null value is never allowed.
863
864       if Ada_Version >= Ada_05 then
865          Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
866       else
867          Set_Can_Never_Be_Null (Anon_Type, True);
868       end if;
869
870       --  The anonymous access type is as public as the discriminated type or
871       --  subprogram that defines it. It is imported (for back-end purposes)
872       --  if the designated type is.
873
874       Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
875
876       --  Ada 2005 (AI-50217): Propagate the attribute that indicates that the
877       --  designated type comes from the limited view.
878
879       Set_From_With_Type (Anon_Type, From_With_Type (Desig_Type));
880
881       --  Ada 2005 (AI-231): Propagate the access-constant attribute
882
883       Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
884
885       --  The context is either a subprogram declaration, object declaration,
886       --  or an access discriminant, in a private or a full type declaration.
887       --  In the case of a subprogram, if the designated type is incomplete,
888       --  the operation will be a primitive operation of the full type, to be
889       --  updated subsequently. If the type is imported through a limited_with
890       --  clause, the subprogram is not a primitive operation of the type
891       --  (which is declared elsewhere in some other scope).
892
893       if Ekind (Desig_Type) = E_Incomplete_Type
894         and then not From_With_Type (Desig_Type)
895         and then Is_Overloadable (Current_Scope)
896       then
897          Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
898          Set_Has_Delayed_Freeze (Current_Scope);
899       end if;
900
901       --  Ada 2005: if the designated type is an interface that may contain
902       --  tasks, create a Master entity for the declaration. This must be done
903       --  before expansion of the full declaration, because the declaration may
904       --  include an expression that is an allocator, whose expansion needs the
905       --  proper Master for the created tasks.
906
907       if Nkind (Related_Nod) = N_Object_Declaration
908          and then Expander_Active
909       then
910          if Is_Interface (Desig_Type)
911            and then Is_Limited_Record (Desig_Type)
912          then
913             Build_Class_Wide_Master (Anon_Type);
914
915          --  Similarly, if the type is an anonymous access that designates
916          --  tasks, create a master entity for it in the current context.
917
918          elsif Has_Task (Desig_Type)
919            and then Comes_From_Source (Related_Nod)
920          then
921             if not Has_Master_Entity (Current_Scope) then
922                Decl :=
923                  Make_Object_Declaration (Loc,
924                    Defining_Identifier =>
925                      Make_Defining_Identifier (Loc, Name_uMaster),
926                    Constant_Present => True,
927                    Object_Definition =>
928                      New_Reference_To (RTE (RE_Master_Id), Loc),
929                    Expression =>
930                      Make_Explicit_Dereference (Loc,
931                        New_Reference_To (RTE (RE_Current_Master), Loc)));
932
933                Insert_Before (Related_Nod, Decl);
934                Analyze (Decl);
935
936                Set_Master_Id (Anon_Type, Defining_Identifier (Decl));
937                Set_Has_Master_Entity (Current_Scope);
938             else
939                Build_Master_Renaming (Related_Nod, Anon_Type);
940             end if;
941          end if;
942       end if;
943
944       --  For a private component of a protected type, it is imperative that
945       --  the back-end elaborate the type immediately after the protected
946       --  declaration, because this type will be used in the declarations
947       --  created for the component within each protected body, so we must
948       --  create an itype reference for it now.
949
950       if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
951          Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
952
953       --  Similarly, if the access definition is the return result of a
954       --  function, create an itype reference for it because it
955       --  will be used within the function body. For a regular function that
956       --  is not a compilation unit, insert reference after the declaration.
957       --  For a protected operation, insert it after the enclosing protected
958       --  type declaration. In either case, do not create a reference for a
959       --  type obtained through a limited_with clause, because this would
960       --  introduce semantic dependencies.
961
962       elsif Nkind (Related_Nod) = N_Function_Specification
963         and then not From_With_Type (Anon_Type)
964       then
965          if Present (Enclosing_Prot_Type) then
966             Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
967
968          elsif Is_List_Member (Parent (Related_Nod))
969            and then Nkind (Parent (N)) /= N_Parameter_Specification
970          then
971             Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
972          end if;
973
974       --  Finally, create an itype reference for an object declaration of
975       --  an anonymous access type. This is strictly necessary only for
976       --  deferred constants, but in any case will avoid out-of-scope
977       --  problems in the back-end.
978
979       elsif Nkind (Related_Nod) = N_Object_Declaration then
980          Build_Itype_Reference (Anon_Type, Related_Nod);
981       end if;
982
983       return Anon_Type;
984    end Access_Definition;
985
986    -----------------------------------
987    -- Access_Subprogram_Declaration --
988    -----------------------------------
989
990    procedure Access_Subprogram_Declaration
991      (T_Name : Entity_Id;
992       T_Def  : Node_Id)
993    is
994
995       procedure Check_For_Premature_Usage (Def : Node_Id);
996       --  Check that type T_Name is not used, directly or recursively, as a
997       --  parameter or a return type in Def. Def is either a subtype, an
998       --  access_definition, or an access_to_subprogram_definition.
999
1000       -------------------------------
1001       -- Check_For_Premature_Usage --
1002       -------------------------------
1003
1004       procedure Check_For_Premature_Usage (Def : Node_Id) is
1005          Param : Node_Id;
1006
1007       begin
1008          --  Check for a subtype mark
1009
1010          if Nkind (Def) in N_Has_Etype then
1011             if Etype (Def) = T_Name then
1012                Error_Msg_N
1013                  ("type& cannot be used before end of its declaration", Def);
1014             end if;
1015
1016          --  If this is not a subtype, then this is an access_definition
1017
1018          elsif Nkind (Def) = N_Access_Definition then
1019             if Present (Access_To_Subprogram_Definition (Def)) then
1020                Check_For_Premature_Usage
1021                  (Access_To_Subprogram_Definition (Def));
1022             else
1023                Check_For_Premature_Usage (Subtype_Mark (Def));
1024             end if;
1025
1026          --  The only cases left are N_Access_Function_Definition and
1027          --  N_Access_Procedure_Definition.
1028
1029          else
1030             if Present (Parameter_Specifications (Def)) then
1031                Param := First (Parameter_Specifications (Def));
1032                while Present (Param) loop
1033                   Check_For_Premature_Usage (Parameter_Type (Param));
1034                   Param := Next (Param);
1035                end loop;
1036             end if;
1037
1038             if Nkind (Def) = N_Access_Function_Definition then
1039                Check_For_Premature_Usage (Result_Definition (Def));
1040             end if;
1041          end if;
1042       end Check_For_Premature_Usage;
1043
1044       --  Local variables
1045
1046       Formals    : constant List_Id := Parameter_Specifications (T_Def);
1047       Formal     : Entity_Id;
1048       D_Ityp     : Node_Id;
1049       Desig_Type : constant Entity_Id :=
1050                      Create_Itype (E_Subprogram_Type, Parent (T_Def));
1051
1052    --  Start of processing for Access_Subprogram_Declaration
1053
1054    begin
1055       --  Associate the Itype node with the inner full-type declaration or
1056       --  subprogram spec. This is required to handle nested anonymous
1057       --  declarations. For example:
1058
1059       --      procedure P
1060       --       (X : access procedure
1061       --                     (Y : access procedure
1062       --                                   (Z : access T)))
1063
1064       D_Ityp := Associated_Node_For_Itype (Desig_Type);
1065       while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1066                                    N_Private_Type_Declaration,
1067                                    N_Private_Extension_Declaration,
1068                                    N_Procedure_Specification,
1069                                    N_Function_Specification)
1070                    or else
1071                  Nkind_In (D_Ityp, N_Object_Declaration,
1072                                    N_Object_Renaming_Declaration,
1073                                    N_Formal_Object_Declaration,
1074                                    N_Formal_Type_Declaration,
1075                                    N_Task_Type_Declaration,
1076                                    N_Protected_Type_Declaration))
1077       loop
1078          D_Ityp := Parent (D_Ityp);
1079          pragma Assert (D_Ityp /= Empty);
1080       end loop;
1081
1082       Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1083
1084       if Nkind_In (D_Ityp, N_Procedure_Specification,
1085                            N_Function_Specification)
1086       then
1087          Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1088
1089       elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1090                               N_Object_Declaration,
1091                               N_Object_Renaming_Declaration,
1092                               N_Formal_Type_Declaration)
1093       then
1094          Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1095       end if;
1096
1097       if Nkind (T_Def) = N_Access_Function_Definition then
1098          if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1099             declare
1100                Acc : constant Node_Id := Result_Definition (T_Def);
1101
1102             begin
1103                if Present (Access_To_Subprogram_Definition (Acc))
1104                  and then
1105                    Protected_Present (Access_To_Subprogram_Definition (Acc))
1106                then
1107                   Set_Etype
1108                     (Desig_Type,
1109                        Replace_Anonymous_Access_To_Protected_Subprogram
1110                          (T_Def));
1111
1112                else
1113                   Set_Etype
1114                     (Desig_Type,
1115                        Access_Definition (T_Def, Result_Definition (T_Def)));
1116                end if;
1117             end;
1118
1119          else
1120             Analyze (Result_Definition (T_Def));
1121
1122             declare
1123                Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1124
1125             begin
1126                --  If a null exclusion is imposed on the result type, then
1127                --  create a null-excluding itype (an access subtype) and use
1128                --  it as the function's Etype.
1129
1130                if Is_Access_Type (Typ)
1131                  and then Null_Exclusion_In_Return_Present (T_Def)
1132                then
1133                   Set_Etype  (Desig_Type,
1134                     Create_Null_Excluding_Itype
1135                       (T           => Typ,
1136                        Related_Nod => T_Def,
1137                        Scope_Id    => Current_Scope));
1138
1139                else
1140                   if From_With_Type (Typ) then
1141                      Error_Msg_NE
1142                       ("illegal use of incomplete type&",
1143                          Result_Definition (T_Def), Typ);
1144
1145                   elsif Ekind (Current_Scope) = E_Package
1146                     and then In_Private_Part (Current_Scope)
1147                   then
1148                      if Ekind (Typ) = E_Incomplete_Type then
1149                         Append_Elmt (Desig_Type, Private_Dependents (Typ));
1150
1151                      elsif Is_Class_Wide_Type (Typ)
1152                        and then Ekind (Etype (Typ)) = E_Incomplete_Type
1153                      then
1154                         Append_Elmt
1155                           (Desig_Type, Private_Dependents (Etype (Typ)));
1156                      end if;
1157                   end if;
1158
1159                   Set_Etype (Desig_Type, Typ);
1160                end if;
1161             end;
1162          end if;
1163
1164          if not (Is_Type (Etype (Desig_Type))) then
1165             Error_Msg_N
1166               ("expect type in function specification",
1167                Result_Definition (T_Def));
1168          end if;
1169
1170       else
1171          Set_Etype (Desig_Type, Standard_Void_Type);
1172       end if;
1173
1174       if Present (Formals) then
1175          Push_Scope (Desig_Type);
1176
1177          --  A bit of a kludge here. These kludges will be removed when Itypes
1178          --  have proper parent pointers to their declarations???
1179
1180          --  Kludge 1) Link defining_identifier of formals. Required by
1181          --  First_Formal to provide its functionality.
1182
1183          declare
1184             F : Node_Id;
1185
1186          begin
1187             F := First (Formals);
1188             while Present (F) loop
1189                if No (Parent (Defining_Identifier (F))) then
1190                   Set_Parent (Defining_Identifier (F), F);
1191                end if;
1192
1193                Next (F);
1194             end loop;
1195          end;
1196
1197          Process_Formals (Formals, Parent (T_Def));
1198
1199          --  Kludge 2) End_Scope requires that the parent pointer be set to
1200          --  something reasonable, but Itypes don't have parent pointers. So
1201          --  we set it and then unset it ???
1202
1203          Set_Parent (Desig_Type, T_Name);
1204          End_Scope;
1205          Set_Parent (Desig_Type, Empty);
1206       end if;
1207
1208       --  Check for premature usage of the type being defined
1209
1210       Check_For_Premature_Usage (T_Def);
1211
1212       --  The return type and/or any parameter type may be incomplete. Mark
1213       --  the subprogram_type as depending on the incomplete type, so that
1214       --  it can be updated when the full type declaration is seen. This
1215       --  only applies to incomplete types declared in some enclosing scope,
1216       --  not to limited views from other packages.
1217
1218       if Present (Formals) then
1219          Formal := First_Formal (Desig_Type);
1220          while Present (Formal) loop
1221             if Ekind (Formal) /= E_In_Parameter
1222               and then Nkind (T_Def) = N_Access_Function_Definition
1223             then
1224                Error_Msg_N ("functions can only have IN parameters", Formal);
1225             end if;
1226
1227             if Ekind (Etype (Formal)) = E_Incomplete_Type
1228               and then In_Open_Scopes (Scope (Etype (Formal)))
1229             then
1230                Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1231                Set_Has_Delayed_Freeze (Desig_Type);
1232             end if;
1233
1234             Next_Formal (Formal);
1235          end loop;
1236       end if;
1237
1238       --  If the return type is incomplete, this is legal as long as the
1239       --  type is declared in the current scope and will be completed in
1240       --  it (rather than being part of limited view).
1241
1242       if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1243         and then not Has_Delayed_Freeze (Desig_Type)
1244         and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1245       then
1246          Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1247          Set_Has_Delayed_Freeze (Desig_Type);
1248       end if;
1249
1250       Check_Delayed_Subprogram (Desig_Type);
1251
1252       if Protected_Present (T_Def) then
1253          Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1254          Set_Convention (Desig_Type, Convention_Protected);
1255       else
1256          Set_Ekind (T_Name, E_Access_Subprogram_Type);
1257       end if;
1258
1259       Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1260
1261       Set_Etype                    (T_Name, T_Name);
1262       Init_Size_Align              (T_Name);
1263       Set_Directly_Designated_Type (T_Name, Desig_Type);
1264
1265       --  Ada 2005 (AI-231): Propagate the null-excluding attribute
1266
1267       Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1268
1269       Check_Restriction (No_Access_Subprograms, T_Def);
1270    end Access_Subprogram_Declaration;
1271
1272    ----------------------------
1273    -- Access_Type_Declaration --
1274    ----------------------------
1275
1276    procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1277       S : constant Node_Id := Subtype_Indication (Def);
1278       P : constant Node_Id := Parent (Def);
1279    begin
1280       --  Check for permissible use of incomplete type
1281
1282       if Nkind (S) /= N_Subtype_Indication then
1283          Analyze (S);
1284
1285          if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1286             Set_Directly_Designated_Type (T, Entity (S));
1287          else
1288             Set_Directly_Designated_Type (T,
1289               Process_Subtype (S, P, T, 'P'));
1290          end if;
1291
1292       else
1293          Set_Directly_Designated_Type (T,
1294            Process_Subtype (S, P, T, 'P'));
1295       end if;
1296
1297       if All_Present (Def) or Constant_Present (Def) then
1298          Set_Ekind (T, E_General_Access_Type);
1299       else
1300          Set_Ekind (T, E_Access_Type);
1301       end if;
1302
1303       if Base_Type (Designated_Type (T)) = T then
1304          Error_Msg_N ("access type cannot designate itself", S);
1305
1306       --  In Ada 2005, the type may have a limited view through some unit
1307       --  in its own context, allowing the following circularity that cannot
1308       --  be detected earlier
1309
1310       elsif Is_Class_Wide_Type (Designated_Type (T))
1311         and then Etype (Designated_Type (T)) = T
1312       then
1313          Error_Msg_N
1314            ("access type cannot designate its own classwide type", S);
1315
1316          --  Clean up indication of tagged status to prevent cascaded errors
1317
1318          Set_Is_Tagged_Type (T, False);
1319       end if;
1320
1321       Set_Etype (T, T);
1322
1323       --  If the type has appeared already in a with_type clause, it is
1324       --  frozen and the pointer size is already set. Else, initialize.
1325
1326       if not From_With_Type (T) then
1327          Init_Size_Align (T);
1328       end if;
1329
1330       --  Note that Has_Task is always false, since the access type itself
1331       --  is not a task type. See Einfo for more description on this point.
1332       --  Exactly the same consideration applies to Has_Controlled_Component.
1333
1334       Set_Has_Task (T, False);
1335       Set_Has_Controlled_Component (T, False);
1336
1337       --  Initialize Associated_Final_Chain explicitly to Empty, to avoid
1338       --  problems where an incomplete view of this entity has been previously
1339       --  established by a limited with and an overlaid version of this field
1340       --  (Stored_Constraint) was initialized for the incomplete view.
1341
1342       Set_Associated_Final_Chain (T, Empty);
1343
1344       --  Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1345       --  attributes
1346
1347       Set_Can_Never_Be_Null  (T, Null_Exclusion_Present (Def));
1348       Set_Is_Access_Constant (T, Constant_Present (Def));
1349    end Access_Type_Declaration;
1350
1351    ----------------------------------
1352    -- Add_Interface_Tag_Components --
1353    ----------------------------------
1354
1355    procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1356       Loc      : constant Source_Ptr := Sloc (N);
1357       L        : List_Id;
1358       Last_Tag : Node_Id;
1359
1360       procedure Add_Tag (Iface : Entity_Id);
1361       --  Add tag for one of the progenitor interfaces
1362
1363       -------------
1364       -- Add_Tag --
1365       -------------
1366
1367       procedure Add_Tag (Iface : Entity_Id) is
1368          Decl   : Node_Id;
1369          Def    : Node_Id;
1370          Tag    : Entity_Id;
1371          Offset : Entity_Id;
1372
1373       begin
1374          pragma Assert (Is_Tagged_Type (Iface)
1375            and then Is_Interface (Iface));
1376
1377          Def :=
1378            Make_Component_Definition (Loc,
1379              Aliased_Present    => True,
1380              Subtype_Indication =>
1381                New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1382
1383          Tag := Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1384
1385          Decl :=
1386            Make_Component_Declaration (Loc,
1387              Defining_Identifier  => Tag,
1388              Component_Definition => Def);
1389
1390          Analyze_Component_Declaration (Decl);
1391
1392          Set_Analyzed (Decl);
1393          Set_Ekind               (Tag, E_Component);
1394          Set_Is_Tag              (Tag);
1395          Set_Is_Aliased          (Tag);
1396          Set_Related_Type        (Tag, Iface);
1397          Init_Component_Location (Tag);
1398
1399          pragma Assert (Is_Frozen (Iface));
1400
1401          Set_DT_Entry_Count    (Tag,
1402            DT_Entry_Count (First_Entity (Iface)));
1403
1404          if No (Last_Tag) then
1405             Prepend (Decl, L);
1406          else
1407             Insert_After (Last_Tag, Decl);
1408          end if;
1409
1410          Last_Tag := Decl;
1411
1412          --  If the ancestor has discriminants we need to give special support
1413          --  to store the offset_to_top value of the secondary dispatch tables.
1414          --  For this purpose we add a supplementary component just after the
1415          --  field that contains the tag associated with each secondary DT.
1416
1417          if Typ /= Etype (Typ)
1418            and then Has_Discriminants (Etype (Typ))
1419          then
1420             Def :=
1421               Make_Component_Definition (Loc,
1422                 Subtype_Indication =>
1423                   New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1424
1425             Offset :=
1426               Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1427
1428             Decl :=
1429               Make_Component_Declaration (Loc,
1430                 Defining_Identifier  => Offset,
1431                 Component_Definition => Def);
1432
1433             Analyze_Component_Declaration (Decl);
1434
1435             Set_Analyzed (Decl);
1436             Set_Ekind               (Offset, E_Component);
1437             Set_Is_Aliased          (Offset);
1438             Set_Related_Type        (Offset, Iface);
1439             Init_Component_Location (Offset);
1440             Insert_After (Last_Tag, Decl);
1441             Last_Tag := Decl;
1442          end if;
1443       end Add_Tag;
1444
1445       --  Local variables
1446
1447       Elmt : Elmt_Id;
1448       Ext  : Node_Id;
1449       Comp : Node_Id;
1450
1451    --  Start of processing for Add_Interface_Tag_Components
1452
1453    begin
1454       if not RTE_Available (RE_Interface_Tag) then
1455          Error_Msg
1456            ("(Ada 2005) interface types not supported by this run-time!",
1457             Sloc (N));
1458          return;
1459       end if;
1460
1461       if Ekind (Typ) /= E_Record_Type
1462         or else (Is_Concurrent_Record_Type (Typ)
1463                   and then Is_Empty_List (Abstract_Interface_List (Typ)))
1464         or else (not Is_Concurrent_Record_Type (Typ)
1465                   and then No (Interfaces (Typ))
1466                   and then Is_Empty_Elmt_List (Interfaces (Typ)))
1467       then
1468          return;
1469       end if;
1470
1471       --  Find the current last tag
1472
1473       if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1474          Ext := Record_Extension_Part (Type_Definition (N));
1475       else
1476          pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1477          Ext := Type_Definition (N);
1478       end if;
1479
1480       Last_Tag := Empty;
1481
1482       if not (Present (Component_List (Ext))) then
1483          Set_Null_Present (Ext, False);
1484          L := New_List;
1485          Set_Component_List (Ext,
1486            Make_Component_List (Loc,
1487              Component_Items => L,
1488              Null_Present => False));
1489       else
1490          if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1491             L := Component_Items
1492                    (Component_List
1493                      (Record_Extension_Part
1494                        (Type_Definition (N))));
1495          else
1496             L := Component_Items
1497                    (Component_List
1498                      (Type_Definition (N)));
1499          end if;
1500
1501          --  Find the last tag component
1502
1503          Comp := First (L);
1504          while Present (Comp) loop
1505             if Nkind (Comp) = N_Component_Declaration
1506               and then Is_Tag (Defining_Identifier (Comp))
1507             then
1508                Last_Tag := Comp;
1509             end if;
1510
1511             Next (Comp);
1512          end loop;
1513       end if;
1514
1515       --  At this point L references the list of components and Last_Tag
1516       --  references the current last tag (if any). Now we add the tag
1517       --  corresponding with all the interfaces that are not implemented
1518       --  by the parent.
1519
1520       if Present (Interfaces (Typ)) then
1521          Elmt := First_Elmt (Interfaces (Typ));
1522          while Present (Elmt) loop
1523             Add_Tag (Node (Elmt));
1524             Next_Elmt (Elmt);
1525          end loop;
1526       end if;
1527    end Add_Interface_Tag_Components;
1528
1529    -----------------------------------
1530    -- Analyze_Component_Declaration --
1531    -----------------------------------
1532
1533    procedure Analyze_Component_Declaration (N : Node_Id) is
1534       Id : constant Entity_Id := Defining_Identifier (N);
1535       E  : constant Node_Id   := Expression (N);
1536       T  : Entity_Id;
1537       P  : Entity_Id;
1538
1539       function Contains_POC (Constr : Node_Id) return Boolean;
1540       --  Determines whether a constraint uses the discriminant of a record
1541       --  type thus becoming a per-object constraint (POC).
1542
1543       function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1544       --  Typ is the type of the current component, check whether this type is
1545       --  a limited type. Used to validate declaration against that of
1546       --  enclosing record.
1547
1548       ------------------
1549       -- Contains_POC --
1550       ------------------
1551
1552       function Contains_POC (Constr : Node_Id) return Boolean is
1553       begin
1554          --  Prevent cascaded errors
1555
1556          if Error_Posted (Constr) then
1557             return False;
1558          end if;
1559
1560          case Nkind (Constr) is
1561             when N_Attribute_Reference =>
1562                return
1563                  Attribute_Name (Constr) = Name_Access
1564                    and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1565
1566             when N_Discriminant_Association =>
1567                return Denotes_Discriminant (Expression (Constr));
1568
1569             when N_Identifier =>
1570                return Denotes_Discriminant (Constr);
1571
1572             when N_Index_Or_Discriminant_Constraint =>
1573                declare
1574                   IDC : Node_Id;
1575
1576                begin
1577                   IDC := First (Constraints (Constr));
1578                   while Present (IDC) loop
1579
1580                      --  One per-object constraint is sufficient
1581
1582                      if Contains_POC (IDC) then
1583                         return True;
1584                      end if;
1585
1586                      Next (IDC);
1587                   end loop;
1588
1589                   return False;
1590                end;
1591
1592             when N_Range =>
1593                return Denotes_Discriminant (Low_Bound (Constr))
1594                         or else
1595                       Denotes_Discriminant (High_Bound (Constr));
1596
1597             when N_Range_Constraint =>
1598                return Denotes_Discriminant (Range_Expression (Constr));
1599
1600             when others =>
1601                return False;
1602
1603          end case;
1604       end Contains_POC;
1605
1606       ----------------------
1607       -- Is_Known_Limited --
1608       ----------------------
1609
1610       function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1611          P : constant Entity_Id := Etype (Typ);
1612          R : constant Entity_Id := Root_Type (Typ);
1613
1614       begin
1615          if Is_Limited_Record (Typ) then
1616             return True;
1617
1618          --  If the root type is limited (and not a limited interface)
1619          --  so is the current type
1620
1621          elsif Is_Limited_Record (R)
1622            and then
1623              (not Is_Interface (R)
1624                or else not Is_Limited_Interface (R))
1625          then
1626             return True;
1627
1628          --  Else the type may have a limited interface progenitor, but a
1629          --  limited record parent.
1630
1631          elsif R /= P
1632            and then Is_Limited_Record (P)
1633          then
1634             return True;
1635
1636          else
1637             return False;
1638          end if;
1639       end Is_Known_Limited;
1640
1641    --  Start of processing for Analyze_Component_Declaration
1642
1643    begin
1644       Generate_Definition (Id);
1645       Enter_Name (Id);
1646
1647       if Present (Subtype_Indication (Component_Definition (N))) then
1648          T := Find_Type_Of_Object
1649                 (Subtype_Indication (Component_Definition (N)), N);
1650
1651       --  Ada 2005 (AI-230): Access Definition case
1652
1653       else
1654          pragma Assert (Present
1655                           (Access_Definition (Component_Definition (N))));
1656
1657          T := Access_Definition
1658                 (Related_Nod => N,
1659                  N => Access_Definition (Component_Definition (N)));
1660          Set_Is_Local_Anonymous_Access (T);
1661
1662          --  Ada 2005 (AI-254)
1663
1664          if Present (Access_To_Subprogram_Definition
1665                       (Access_Definition (Component_Definition (N))))
1666            and then Protected_Present (Access_To_Subprogram_Definition
1667                                         (Access_Definition
1668                                           (Component_Definition (N))))
1669          then
1670             T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1671          end if;
1672       end if;
1673
1674       --  If the subtype is a constrained subtype of the enclosing record,
1675       --  (which must have a partial view) the back-end does not properly
1676       --  handle the recursion. Rewrite the component declaration with an
1677       --  explicit subtype indication, which is acceptable to Gigi. We can copy
1678       --  the tree directly because side effects have already been removed from
1679       --  discriminant constraints.
1680
1681       if Ekind (T) = E_Access_Subtype
1682         and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1683         and then Comes_From_Source (T)
1684         and then Nkind (Parent (T)) = N_Subtype_Declaration
1685         and then Etype (Directly_Designated_Type (T)) = Current_Scope
1686       then
1687          Rewrite
1688            (Subtype_Indication (Component_Definition (N)),
1689              New_Copy_Tree (Subtype_Indication (Parent (T))));
1690          T := Find_Type_Of_Object
1691                  (Subtype_Indication (Component_Definition (N)), N);
1692       end if;
1693
1694       --  If the component declaration includes a default expression, then we
1695       --  check that the component is not of a limited type (RM 3.7(5)),
1696       --  and do the special preanalysis of the expression (see section on
1697       --  "Handling of Default and Per-Object Expressions" in the spec of
1698       --  package Sem).
1699
1700       if Present (E) then
1701          Preanalyze_Spec_Expression (E, T);
1702          Check_Initialization (T, E);
1703
1704          if Ada_Version >= Ada_05
1705            and then Ekind (T) = E_Anonymous_Access_Type
1706            and then Etype (E) /= Any_Type
1707          then
1708             --  Check RM 3.9.2(9): "if the expected type for an expression is
1709             --  an anonymous access-to-specific tagged type, then the object
1710             --  designated by the expression shall not be dynamically tagged
1711             --  unless it is a controlling operand in a call on a dispatching
1712             --  operation"
1713
1714             if Is_Tagged_Type (Directly_Designated_Type (T))
1715               and then
1716                 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1717               and then
1718                 Ekind (Directly_Designated_Type (Etype (E))) =
1719                   E_Class_Wide_Type
1720             then
1721                Error_Msg_N
1722                  ("access to specific tagged type required (RM 3.9.2(9))", E);
1723             end if;
1724
1725             --  (Ada 2005: AI-230): Accessibility check for anonymous
1726             --  components
1727
1728             if Type_Access_Level (Etype (E)) > Type_Access_Level (T) then
1729                Error_Msg_N
1730                  ("expression has deeper access level than component " &
1731                   "(RM 3.10.2 (12.2))", E);
1732             end if;
1733
1734             --  The initialization expression is a reference to an access
1735             --  discriminant. The type of the discriminant is always deeper
1736             --  than any access type.
1737
1738             if Ekind (Etype (E)) = E_Anonymous_Access_Type
1739               and then Is_Entity_Name (E)
1740               and then Ekind (Entity (E)) = E_In_Parameter
1741               and then Present (Discriminal_Link (Entity (E)))
1742             then
1743                Error_Msg_N
1744                  ("discriminant has deeper accessibility level than target",
1745                   E);
1746             end if;
1747          end if;
1748       end if;
1749
1750       --  The parent type may be a private view with unknown discriminants,
1751       --  and thus unconstrained. Regular components must be constrained.
1752
1753       if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1754          if Is_Class_Wide_Type (T) then
1755             Error_Msg_N
1756                ("class-wide subtype with unknown discriminants" &
1757                  " in component declaration",
1758                  Subtype_Indication (Component_Definition (N)));
1759          else
1760             Error_Msg_N
1761               ("unconstrained subtype in component declaration",
1762                Subtype_Indication (Component_Definition (N)));
1763          end if;
1764
1765       --  Components cannot be abstract, except for the special case of
1766       --  the _Parent field (case of extending an abstract tagged type)
1767
1768       elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
1769          Error_Msg_N ("type of a component cannot be abstract", N);
1770       end if;
1771
1772       Set_Etype (Id, T);
1773       Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1774
1775       --  The component declaration may have a per-object constraint, set
1776       --  the appropriate flag in the defining identifier of the subtype.
1777
1778       if Present (Subtype_Indication (Component_Definition (N))) then
1779          declare
1780             Sindic : constant Node_Id :=
1781                        Subtype_Indication (Component_Definition (N));
1782          begin
1783             if Nkind (Sindic) = N_Subtype_Indication
1784               and then Present (Constraint (Sindic))
1785               and then Contains_POC (Constraint (Sindic))
1786             then
1787                Set_Has_Per_Object_Constraint (Id);
1788             end if;
1789          end;
1790       end if;
1791
1792       --  Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1793       --  out some static checks.
1794
1795       if Ada_Version >= Ada_05
1796         and then Can_Never_Be_Null (T)
1797       then
1798          Null_Exclusion_Static_Checks (N);
1799       end if;
1800
1801       --  If this component is private (or depends on a private type), flag the
1802       --  record type to indicate that some operations are not available.
1803
1804       P := Private_Component (T);
1805
1806       if Present (P) then
1807
1808          --  Check for circular definitions
1809
1810          if P = Any_Type then
1811             Set_Etype (Id, Any_Type);
1812
1813          --  There is a gap in the visibility of operations only if the
1814          --  component type is not defined in the scope of the record type.
1815
1816          elsif Scope (P) = Scope (Current_Scope) then
1817             null;
1818
1819          elsif Is_Limited_Type (P) then
1820             Set_Is_Limited_Composite (Current_Scope);
1821
1822          else
1823             Set_Is_Private_Composite (Current_Scope);
1824          end if;
1825       end if;
1826
1827       if P /= Any_Type
1828         and then Is_Limited_Type (T)
1829         and then Chars (Id) /= Name_uParent
1830         and then Is_Tagged_Type (Current_Scope)
1831       then
1832          if Is_Derived_Type (Current_Scope)
1833            and then not Is_Known_Limited (Current_Scope)
1834          then
1835             Error_Msg_N
1836               ("extension of nonlimited type cannot have limited components",
1837                N);
1838
1839             if Is_Interface (Root_Type (Current_Scope)) then
1840                Error_Msg_N
1841                  ("\limitedness is not inherited from limited interface", N);
1842                Error_Msg_N
1843                  ("\add LIMITED to type indication", N);
1844             end if;
1845
1846             Explain_Limited_Type (T, N);
1847             Set_Etype (Id, Any_Type);
1848             Set_Is_Limited_Composite (Current_Scope, False);
1849
1850          elsif not Is_Derived_Type (Current_Scope)
1851            and then not Is_Limited_Record (Current_Scope)
1852            and then not Is_Concurrent_Type (Current_Scope)
1853          then
1854             Error_Msg_N
1855               ("nonlimited tagged type cannot have limited components", N);
1856             Explain_Limited_Type (T, N);
1857             Set_Etype (Id, Any_Type);
1858             Set_Is_Limited_Composite (Current_Scope, False);
1859          end if;
1860       end if;
1861
1862       Set_Original_Record_Component (Id, Id);
1863    end Analyze_Component_Declaration;
1864
1865    --------------------------
1866    -- Analyze_Declarations --
1867    --------------------------
1868
1869    procedure Analyze_Declarations (L : List_Id) is
1870       D           : Node_Id;
1871       Freeze_From : Entity_Id := Empty;
1872       Next_Node   : Node_Id;
1873
1874       procedure Adjust_D;
1875       --  Adjust D not to include implicit label declarations, since these
1876       --  have strange Sloc values that result in elaboration check problems.
1877       --  (They have the sloc of the label as found in the source, and that
1878       --  is ahead of the current declarative part).
1879
1880       --------------
1881       -- Adjust_D --
1882       --------------
1883
1884       procedure Adjust_D is
1885       begin
1886          while Present (Prev (D))
1887            and then Nkind (D) = N_Implicit_Label_Declaration
1888          loop
1889             Prev (D);
1890          end loop;
1891       end Adjust_D;
1892
1893    --  Start of processing for Analyze_Declarations
1894
1895    begin
1896       D := First (L);
1897       while Present (D) loop
1898
1899          --  Complete analysis of declaration
1900
1901          Analyze (D);
1902          Next_Node := Next (D);
1903
1904          if No (Freeze_From) then
1905             Freeze_From := First_Entity (Current_Scope);
1906          end if;
1907
1908          --  At the end of a declarative part, freeze remaining entities
1909          --  declared in it. The end of the visible declarations of package
1910          --  specification is not the end of a declarative part if private
1911          --  declarations are present. The end of a package declaration is a
1912          --  freezing point only if it a library package. A task definition or
1913          --  protected type definition is not a freeze point either. Finally,
1914          --  we do not freeze entities in generic scopes, because there is no
1915          --  code generated for them and freeze nodes will be generated for
1916          --  the instance.
1917
1918          --  The end of a package instantiation is not a freeze point, but
1919          --  for now we make it one, because the generic body is inserted
1920          --  (currently) immediately after. Generic instantiations will not
1921          --  be a freeze point once delayed freezing of bodies is implemented.
1922          --  (This is needed in any case for early instantiations ???).
1923
1924          if No (Next_Node) then
1925             if Nkind_In (Parent (L), N_Component_List,
1926                                      N_Task_Definition,
1927                                      N_Protected_Definition)
1928             then
1929                null;
1930
1931             elsif Nkind (Parent (L)) /= N_Package_Specification then
1932                if Nkind (Parent (L)) = N_Package_Body then
1933                   Freeze_From := First_Entity (Current_Scope);
1934                end if;
1935
1936                Adjust_D;
1937                Freeze_All (Freeze_From, D);
1938                Freeze_From := Last_Entity (Current_Scope);
1939
1940             elsif Scope (Current_Scope) /= Standard_Standard
1941               and then not Is_Child_Unit (Current_Scope)
1942               and then No (Generic_Parent (Parent (L)))
1943             then
1944                null;
1945
1946             elsif L /= Visible_Declarations (Parent (L))
1947                or else No (Private_Declarations (Parent (L)))
1948                or else Is_Empty_List (Private_Declarations (Parent (L)))
1949             then
1950                Adjust_D;
1951                Freeze_All (Freeze_From, D);
1952                Freeze_From := Last_Entity (Current_Scope);
1953             end if;
1954
1955          --  If next node is a body then freeze all types before the body.
1956          --  An exception occurs for some expander-generated bodies. If these
1957          --  are generated at places where in general language rules would not
1958          --  allow a freeze point, then we assume that the expander has
1959          --  explicitly checked that all required types are properly frozen,
1960          --  and we do not cause general freezing here. This special circuit
1961          --  is used when the encountered body is marked as having already
1962          --  been analyzed.
1963
1964          --  In all other cases (bodies that come from source, and expander
1965          --  generated bodies that have not been analyzed yet), freeze all
1966          --  types now. Note that in the latter case, the expander must take
1967          --  care to attach the bodies at a proper place in the tree so as to
1968          --  not cause unwanted freezing at that point.
1969
1970          elsif not Analyzed (Next_Node)
1971            and then (Nkind_In (Next_Node, N_Subprogram_Body,
1972                                           N_Entry_Body,
1973                                           N_Package_Body,
1974                                           N_Protected_Body,
1975                                           N_Task_Body)
1976                        or else
1977                      Nkind (Next_Node) in N_Body_Stub)
1978          then
1979             Adjust_D;
1980             Freeze_All (Freeze_From, D);
1981             Freeze_From := Last_Entity (Current_Scope);
1982          end if;
1983
1984          D := Next_Node;
1985       end loop;
1986    end Analyze_Declarations;
1987
1988    ----------------------------------
1989    -- Analyze_Incomplete_Type_Decl --
1990    ----------------------------------
1991
1992    procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
1993       F : constant Boolean := Is_Pure (Current_Scope);
1994       T : Entity_Id;
1995
1996    begin
1997       Generate_Definition (Defining_Identifier (N));
1998
1999       --  Process an incomplete declaration. The identifier must not have been
2000       --  declared already in the scope. However, an incomplete declaration may
2001       --  appear in the private part of a package, for a private type that has
2002       --  already been declared.
2003
2004       --  In this case, the discriminants (if any) must match
2005
2006       T := Find_Type_Name (N);
2007
2008       Set_Ekind (T, E_Incomplete_Type);
2009       Init_Size_Align (T);
2010       Set_Is_First_Subtype (T, True);
2011       Set_Etype (T, T);
2012
2013       --  Ada 2005 (AI-326): Minimum decoration to give support to tagged
2014       --  incomplete types.
2015
2016       if Tagged_Present (N) then
2017          Set_Is_Tagged_Type (T);
2018          Make_Class_Wide_Type (T);
2019          Set_Primitive_Operations (T, New_Elmt_List);
2020       end if;
2021
2022       Push_Scope (T);
2023
2024       Set_Stored_Constraint (T, No_Elist);
2025
2026       if Present (Discriminant_Specifications (N)) then
2027          Process_Discriminants (N);
2028       end if;
2029
2030       End_Scope;
2031
2032       --  If the type has discriminants, non-trivial subtypes may be
2033       --  declared before the full view of the type. The full views of those
2034       --  subtypes will be built after the full view of the type.
2035
2036       Set_Private_Dependents (T, New_Elmt_List);
2037       Set_Is_Pure (T, F);
2038    end Analyze_Incomplete_Type_Decl;
2039
2040    -----------------------------------
2041    -- Analyze_Interface_Declaration --
2042    -----------------------------------
2043
2044    procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2045       CW : constant Entity_Id := Class_Wide_Type (T);
2046
2047    begin
2048       Set_Is_Tagged_Type (T);
2049
2050       Set_Is_Limited_Record (T, Limited_Present (Def)
2051                                   or else Task_Present (Def)
2052                                   or else Protected_Present (Def)
2053                                   or else Synchronized_Present (Def));
2054
2055       --  Type is abstract if full declaration carries keyword, or if previous
2056       --  partial view did.
2057
2058       Set_Is_Abstract_Type (T);
2059       Set_Is_Interface (T);
2060
2061       --  Type is a limited interface if it includes the keyword limited, task,
2062       --  protected, or synchronized.
2063
2064       Set_Is_Limited_Interface
2065         (T, Limited_Present (Def)
2066               or else Protected_Present (Def)
2067               or else Synchronized_Present (Def)
2068               or else Task_Present (Def));
2069
2070       Set_Is_Protected_Interface (T, Protected_Present (Def));
2071       Set_Is_Task_Interface (T, Task_Present (Def));
2072
2073       --  Type is a synchronized interface if it includes the keyword task,
2074       --  protected, or synchronized.
2075
2076       Set_Is_Synchronized_Interface
2077         (T, Synchronized_Present (Def)
2078               or else Protected_Present (Def)
2079               or else Task_Present (Def));
2080
2081       Set_Interfaces (T, New_Elmt_List);
2082       Set_Primitive_Operations (T, New_Elmt_List);
2083
2084       --  Complete the decoration of the class-wide entity if it was already
2085       --  built (i.e. during the creation of the limited view)
2086
2087       if Present (CW) then
2088          Set_Is_Interface (CW);
2089          Set_Is_Limited_Interface      (CW, Is_Limited_Interface (T));
2090          Set_Is_Protected_Interface    (CW, Is_Protected_Interface (T));
2091          Set_Is_Synchronized_Interface (CW, Is_Synchronized_Interface (T));
2092          Set_Is_Task_Interface         (CW, Is_Task_Interface (T));
2093       end if;
2094
2095       --  Check runtime support for synchronized interfaces
2096
2097       if VM_Target = No_VM
2098         and then (Is_Task_Interface (T)
2099                     or else Is_Protected_Interface (T)
2100                     or else Is_Synchronized_Interface (T))
2101         and then not RTE_Available (RE_Select_Specific_Data)
2102       then
2103          Error_Msg_CRT ("synchronized interfaces", T);
2104       end if;
2105    end Analyze_Interface_Declaration;
2106
2107    -----------------------------
2108    -- Analyze_Itype_Reference --
2109    -----------------------------
2110
2111    --  Nothing to do. This node is placed in the tree only for the benefit of
2112    --  back end processing, and has no effect on the semantic processing.
2113
2114    procedure Analyze_Itype_Reference (N : Node_Id) is
2115    begin
2116       pragma Assert (Is_Itype (Itype (N)));
2117       null;
2118    end Analyze_Itype_Reference;
2119
2120    --------------------------------
2121    -- Analyze_Number_Declaration --
2122    --------------------------------
2123
2124    procedure Analyze_Number_Declaration (N : Node_Id) is
2125       Id    : constant Entity_Id := Defining_Identifier (N);
2126       E     : constant Node_Id   := Expression (N);
2127       T     : Entity_Id;
2128       Index : Interp_Index;
2129       It    : Interp;
2130
2131    begin
2132       Generate_Definition (Id);
2133       Enter_Name (Id);
2134
2135       --  This is an optimization of a common case of an integer literal
2136
2137       if Nkind (E) = N_Integer_Literal then
2138          Set_Is_Static_Expression (E, True);
2139          Set_Etype                (E, Universal_Integer);
2140
2141          Set_Etype     (Id, Universal_Integer);
2142          Set_Ekind     (Id, E_Named_Integer);
2143          Set_Is_Frozen (Id, True);
2144          return;
2145       end if;
2146
2147       Set_Is_Pure (Id, Is_Pure (Current_Scope));
2148
2149       --  Process expression, replacing error by integer zero, to avoid
2150       --  cascaded errors or aborts further along in the processing
2151
2152       --  Replace Error by integer zero, which seems least likely to
2153       --  cause cascaded errors.
2154
2155       if E = Error then
2156          Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2157          Set_Error_Posted (E);
2158       end if;
2159
2160       Analyze (E);
2161
2162       --  Verify that the expression is static and numeric. If
2163       --  the expression is overloaded, we apply the preference
2164       --  rule that favors root numeric types.
2165
2166       if not Is_Overloaded (E) then
2167          T := Etype (E);
2168
2169       else
2170          T := Any_Type;
2171
2172          Get_First_Interp (E, Index, It);
2173          while Present (It.Typ) loop
2174             if (Is_Integer_Type (It.Typ)
2175                  or else Is_Real_Type (It.Typ))
2176               and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2177             then
2178                if T = Any_Type then
2179                   T := It.Typ;
2180
2181                elsif It.Typ = Universal_Real
2182                  or else It.Typ = Universal_Integer
2183                then
2184                   --  Choose universal interpretation over any other
2185
2186                   T := It.Typ;
2187                   exit;
2188                end if;
2189             end if;
2190
2191             Get_Next_Interp (Index, It);
2192          end loop;
2193       end if;
2194
2195       if Is_Integer_Type (T)  then
2196          Resolve (E, T);
2197          Set_Etype (Id, Universal_Integer);
2198          Set_Ekind (Id, E_Named_Integer);
2199
2200       elsif Is_Real_Type (T) then
2201
2202          --  Because the real value is converted to universal_real, this is a
2203          --  legal context for a universal fixed expression.
2204
2205          if T = Universal_Fixed then
2206             declare
2207                Loc  : constant Source_Ptr := Sloc (N);
2208                Conv : constant Node_Id := Make_Type_Conversion (Loc,
2209                         Subtype_Mark =>
2210                           New_Occurrence_Of (Universal_Real, Loc),
2211                         Expression => Relocate_Node (E));
2212
2213             begin
2214                Rewrite (E, Conv);
2215                Analyze (E);
2216             end;
2217
2218          elsif T = Any_Fixed then
2219             Error_Msg_N ("illegal context for mixed mode operation", E);
2220
2221             --  Expression is of the form : universal_fixed * integer. Try to
2222             --  resolve as universal_real.
2223
2224             T := Universal_Real;
2225             Set_Etype (E, T);
2226          end if;
2227
2228          Resolve (E, T);
2229          Set_Etype (Id, Universal_Real);
2230          Set_Ekind (Id, E_Named_Real);
2231
2232       else
2233          Wrong_Type (E, Any_Numeric);
2234          Resolve (E, T);
2235
2236          Set_Etype               (Id, T);
2237          Set_Ekind               (Id, E_Constant);
2238          Set_Never_Set_In_Source (Id, True);
2239          Set_Is_True_Constant    (Id, True);
2240          return;
2241       end if;
2242
2243       if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
2244          Set_Etype (E, Etype (Id));
2245       end if;
2246
2247       if not Is_OK_Static_Expression (E) then
2248          Flag_Non_Static_Expr
2249            ("non-static expression used in number declaration!", E);
2250          Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2251          Set_Etype (E, Any_Type);
2252       end if;
2253    end Analyze_Number_Declaration;
2254
2255    --------------------------------
2256    -- Analyze_Object_Declaration --
2257    --------------------------------
2258
2259    procedure Analyze_Object_Declaration (N : Node_Id) is
2260       Loc   : constant Source_Ptr := Sloc (N);
2261       Id    : constant Entity_Id  := Defining_Identifier (N);
2262       T     : Entity_Id;
2263       Act_T : Entity_Id;
2264
2265       E : Node_Id := Expression (N);
2266       --  E is set to Expression (N) throughout this routine. When
2267       --  Expression (N) is modified, E is changed accordingly.
2268
2269       Prev_Entity : Entity_Id := Empty;
2270
2271       function Count_Tasks (T : Entity_Id) return Uint;
2272       --  This function is called when a non-generic library level object of a
2273       --  task type is declared. Its function is to count the static number of
2274       --  tasks declared within the type (it is only called if Has_Tasks is set
2275       --  for T). As a side effect, if an array of tasks with non-static bounds
2276       --  or a variant record type is encountered, Check_Restrictions is called
2277       --  indicating the count is unknown.
2278
2279       -----------------
2280       -- Count_Tasks --
2281       -----------------
2282
2283       function Count_Tasks (T : Entity_Id) return Uint is
2284          C : Entity_Id;
2285          X : Node_Id;
2286          V : Uint;
2287
2288       begin
2289          if Is_Task_Type (T) then
2290             return Uint_1;
2291
2292          elsif Is_Record_Type (T) then
2293             if Has_Discriminants (T) then
2294                Check_Restriction (Max_Tasks, N);
2295                return Uint_0;
2296
2297             else
2298                V := Uint_0;
2299                C := First_Component (T);
2300                while Present (C) loop
2301                   V := V + Count_Tasks (Etype (C));
2302                   Next_Component (C);
2303                end loop;
2304
2305                return V;
2306             end if;
2307
2308          elsif Is_Array_Type (T) then
2309             X := First_Index (T);
2310             V := Count_Tasks (Component_Type (T));
2311             while Present (X) loop
2312                C := Etype (X);
2313
2314                if not Is_Static_Subtype (C) then
2315                   Check_Restriction (Max_Tasks, N);
2316                   return Uint_0;
2317                else
2318                   V := V * (UI_Max (Uint_0,
2319                                     Expr_Value (Type_High_Bound (C)) -
2320                                     Expr_Value (Type_Low_Bound (C)) + Uint_1));
2321                end if;
2322
2323                Next_Index (X);
2324             end loop;
2325
2326             return V;
2327
2328          else
2329             return Uint_0;
2330          end if;
2331       end Count_Tasks;
2332
2333    --  Start of processing for Analyze_Object_Declaration
2334
2335    begin
2336       --  There are three kinds of implicit types generated by an
2337       --  object declaration:
2338
2339       --   1. Those for generated by the original Object Definition
2340
2341       --   2. Those generated by the Expression
2342
2343       --   3. Those used to constrained the Object Definition with the
2344       --       expression constraints when it is unconstrained
2345
2346       --  They must be generated in this order to avoid order of elaboration
2347       --  issues. Thus the first step (after entering the name) is to analyze
2348       --  the object definition.
2349
2350       if Constant_Present (N) then
2351          Prev_Entity := Current_Entity_In_Scope (Id);
2352
2353          --  If the homograph is an implicit subprogram, it is overridden by
2354          --  the current declaration.
2355
2356          if Present (Prev_Entity)
2357            and then
2358              ((Is_Overloadable (Prev_Entity)
2359                  and then Is_Inherited_Operation (Prev_Entity))
2360
2361                --  The current object is a discriminal generated for an entry
2362                --  family index. Even though the index is a constant, in this
2363                --  particular context there is no true constant redeclaration.
2364                --  Enter_Name will handle the visibility.
2365
2366                or else
2367                 (Is_Discriminal (Id)
2368                    and then Ekind (Discriminal_Link (Id)) =
2369                               E_Entry_Index_Parameter))
2370          then
2371             Prev_Entity := Empty;
2372          end if;
2373       end if;
2374
2375       if Present (Prev_Entity) then
2376          Constant_Redeclaration (Id, N, T);
2377
2378          Generate_Reference (Prev_Entity, Id, 'c');
2379          Set_Completion_Referenced (Id);
2380
2381          if Error_Posted (N) then
2382
2383             --  Type mismatch or illegal redeclaration, Do not analyze
2384             --  expression to avoid cascaded errors.
2385
2386             T := Find_Type_Of_Object (Object_Definition (N), N);
2387             Set_Etype (Id, T);
2388             Set_Ekind (Id, E_Variable);
2389             return;
2390          end if;
2391
2392       --  In the normal case, enter identifier at the start to catch premature
2393       --  usage in the initialization expression.
2394
2395       else
2396          Generate_Definition (Id);
2397          Enter_Name (Id);
2398
2399          Mark_Coextensions (N, Object_Definition (N));
2400
2401          T := Find_Type_Of_Object (Object_Definition (N), N);
2402
2403          if Nkind (Object_Definition (N)) = N_Access_Definition
2404            and then Present
2405              (Access_To_Subprogram_Definition (Object_Definition (N)))
2406            and then Protected_Present
2407              (Access_To_Subprogram_Definition (Object_Definition (N)))
2408          then
2409             T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2410          end if;
2411
2412          if Error_Posted (Id) then
2413             Set_Etype (Id, T);
2414             Set_Ekind (Id, E_Variable);
2415             return;
2416          end if;
2417       end if;
2418
2419       --  Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2420       --  out some static checks
2421
2422       if Ada_Version >= Ada_05
2423         and then Can_Never_Be_Null (T)
2424       then
2425          --  In case of aggregates we must also take care of the correct
2426          --  initialization of nested aggregates bug this is done at the
2427          --  point of the analysis of the aggregate (see sem_aggr.adb)
2428
2429          if Present (Expression (N))
2430            and then Nkind (Expression (N)) = N_Aggregate
2431          then
2432             null;
2433
2434          else
2435             declare
2436                Save_Typ : constant Entity_Id := Etype (Id);
2437             begin
2438                Set_Etype (Id, T); --  Temp. decoration for static checks
2439                Null_Exclusion_Static_Checks (N);
2440                Set_Etype (Id, Save_Typ);
2441             end;
2442          end if;
2443       end if;
2444
2445       Set_Is_Pure (Id, Is_Pure (Current_Scope));
2446
2447       --  If deferred constant, make sure context is appropriate. We detect
2448       --  a deferred constant as a constant declaration with no expression.
2449       --  A deferred constant can appear in a package body if its completion
2450       --  is by means of an interface pragma.
2451
2452       if Constant_Present (N)
2453         and then No (E)
2454       then
2455          --  A deferred constant may appear in the declarative part of the
2456          --  following constructs:
2457
2458          --     blocks
2459          --     entry bodies
2460          --     extended return statements
2461          --     package specs
2462          --     package bodies
2463          --     subprogram bodies
2464          --     task bodies
2465
2466          --  When declared inside a package spec, a deferred constant must be
2467          --  completed by a full constant declaration or pragma Import. In all
2468          --  other cases, the only proper completion is pragma Import. Extended
2469          --  return statements are flagged as invalid contexts because they do
2470          --  not have a declarative part and so cannot accommodate the pragma.
2471
2472          if Ekind (Current_Scope) = E_Return_Statement then
2473             Error_Msg_N
2474               ("invalid context for deferred constant declaration (RM 7.4)",
2475                N);
2476             Error_Msg_N
2477               ("\declaration requires an initialization expression",
2478                 N);
2479             Set_Constant_Present (N, False);
2480
2481          --  In Ada 83, deferred constant must be of private type
2482
2483          elsif not Is_Private_Type (T) then
2484             if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2485                Error_Msg_N
2486                  ("(Ada 83) deferred constant must be private type", N);
2487             end if;
2488          end if;
2489
2490       --  If not a deferred constant, then object declaration freezes its type
2491
2492       else
2493          Check_Fully_Declared (T, N);
2494          Freeze_Before (N, T);
2495       end if;
2496
2497       --  If the object was created by a constrained array definition, then
2498       --  set the link in both the anonymous base type and anonymous subtype
2499       --  that are built to represent the array type to point to the object.
2500
2501       if Nkind (Object_Definition (Declaration_Node (Id))) =
2502                         N_Constrained_Array_Definition
2503       then
2504          Set_Related_Array_Object (T, Id);
2505          Set_Related_Array_Object (Base_Type (T), Id);
2506       end if;
2507
2508       --  Special checks for protected objects not at library level
2509
2510       if Is_Protected_Type (T)
2511         and then not Is_Library_Level_Entity (Id)
2512       then
2513          Check_Restriction (No_Local_Protected_Objects, Id);
2514
2515          --  Protected objects with interrupt handlers must be at library level
2516
2517          --  Ada 2005: this test is not needed (and the corresponding clause
2518          --  in the RM is removed) because accessibility checks are sufficient
2519          --  to make handlers not at the library level illegal.
2520
2521          if Has_Interrupt_Handler (T)
2522            and then Ada_Version < Ada_05
2523          then
2524             Error_Msg_N
2525               ("interrupt object can only be declared at library level", Id);
2526          end if;
2527       end if;
2528
2529       --  The actual subtype of the object is the nominal subtype, unless
2530       --  the nominal one is unconstrained and obtained from the expression.
2531
2532       Act_T := T;
2533
2534       --  Process initialization expression if present and not in error
2535
2536       if Present (E) and then E /= Error then
2537
2538          --  Generate an error in case of CPP class-wide object initialization.
2539          --  Required because otherwise the expansion of the class-wide
2540          --  assignment would try to use 'size to initialize the object
2541          --  (primitive that is not available in CPP tagged types).
2542
2543          if Is_Class_Wide_Type (Act_T)
2544            and then
2545              (Is_CPP_Class (Root_Type (Etype (Act_T)))
2546                or else
2547                  (Present (Full_View (Root_Type (Etype (Act_T))))
2548                     and then
2549                       Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
2550          then
2551             Error_Msg_N
2552               ("predefined assignment not available for 'C'P'P tagged types",
2553                E);
2554          end if;
2555
2556          Mark_Coextensions (N, E);
2557          Analyze (E);
2558
2559          --  In case of errors detected in the analysis of the expression,
2560          --  decorate it with the expected type to avoid cascaded errors
2561
2562          if No (Etype (E)) then
2563             Set_Etype (E, T);
2564          end if;
2565
2566          --  If an initialization expression is present, then we set the
2567          --  Is_True_Constant flag. It will be reset if this is a variable
2568          --  and it is indeed modified.
2569
2570          Set_Is_True_Constant (Id, True);
2571
2572          --  If we are analyzing a constant declaration, set its completion
2573          --  flag after analyzing and resolving the expression.
2574
2575          if Constant_Present (N) then
2576             Set_Has_Completion (Id);
2577          end if;
2578
2579          --  Set type and resolve (type may be overridden later on)
2580
2581          Set_Etype (Id, T);
2582          Resolve (E, T);
2583
2584          --  If E is null and has been replaced by an N_Raise_Constraint_Error
2585          --  node (which was marked already-analyzed), we need to set the type
2586          --  to something other than Any_Access in order to keep gigi happy.
2587
2588          if Etype (E) = Any_Access then
2589             Set_Etype (E, T);
2590          end if;
2591
2592          --  If the object is an access to variable, the initialization
2593          --  expression cannot be an access to constant.
2594
2595          if Is_Access_Type (T)
2596            and then not Is_Access_Constant (T)
2597            and then Is_Access_Type (Etype (E))
2598            and then Is_Access_Constant (Etype (E))
2599          then
2600             Error_Msg_N
2601               ("access to variable cannot be initialized " &
2602                 "with an access-to-constant expression", E);
2603          end if;
2604
2605          if not Assignment_OK (N) then
2606             Check_Initialization (T, E);
2607          end if;
2608
2609          Check_Unset_Reference (E);
2610
2611          --  If this is a variable, then set current value
2612
2613          if not Constant_Present (N) then
2614             if Compile_Time_Known_Value (E) then
2615                Set_Current_Value (Id, E);
2616             end if;
2617          end if;
2618
2619          --  Deal with setting of null flags
2620
2621          if Is_Access_Type (T) then
2622             if Known_Non_Null (E) then
2623                Set_Is_Known_Non_Null (Id, True);
2624             elsif Known_Null (E)
2625               and then not Can_Never_Be_Null (Id)
2626             then
2627                Set_Is_Known_Null (Id, True);
2628             end if;
2629          end if;
2630
2631          --  Check incorrect use of dynamically tagged expressions. Note
2632          --  the use of Is_Tagged_Type (T) which seems redundant but is in
2633          --  fact important to avoid spurious errors due to expanded code
2634          --  for dispatching functions over an anonymous access type
2635
2636          if (Is_Class_Wide_Type (Etype (E)) or else Is_Dynamically_Tagged (E))
2637            and then Is_Tagged_Type (T)
2638            and then not Is_Class_Wide_Type (T)
2639            and then not Is_CPP_Constructor_Call (E)
2640          then
2641             Error_Msg_N ("dynamically tagged expression not allowed!", E);
2642          end if;
2643
2644          Apply_Scalar_Range_Check (E, T);
2645          Apply_Static_Length_Check (E, T);
2646       end if;
2647
2648       --  If the No_Streams restriction is set, check that the type of the
2649       --  object is not, and does not contain, any subtype derived from
2650       --  Ada.Streams.Root_Stream_Type. Note that we guard the call to
2651       --  Has_Stream just for efficiency reasons. There is no point in
2652       --  spending time on a Has_Stream check if the restriction is not set.
2653
2654       if Restrictions.Set (No_Streams) then
2655          if Has_Stream (T) then
2656             Check_Restriction (No_Streams, N);
2657          end if;
2658       end if;
2659
2660       --  Case of unconstrained type
2661
2662       if Is_Indefinite_Subtype (T) then
2663
2664          --  Nothing to do in deferred constant case
2665
2666          if Constant_Present (N) and then No (E) then
2667             null;
2668
2669          --  Case of no initialization present
2670
2671          elsif No (E) then
2672             if No_Initialization (N) then
2673                null;
2674
2675             elsif Is_Class_Wide_Type (T) then
2676                Error_Msg_N
2677                  ("initialization required in class-wide declaration ", N);
2678
2679             else
2680                Error_Msg_N
2681                  ("unconstrained subtype not allowed (need initialization)",
2682                   Object_Definition (N));
2683
2684                if Is_Record_Type (T) and then Has_Discriminants (T) then
2685                   Error_Msg_N
2686                     ("\provide initial value or explicit discriminant values",
2687                      Object_Definition (N));
2688
2689                   Error_Msg_NE
2690                     ("\or give default discriminant values for type&",
2691                      Object_Definition (N), T);
2692
2693                elsif Is_Array_Type (T) then
2694                   Error_Msg_N
2695                     ("\provide initial value or explicit array bounds",
2696                      Object_Definition (N));
2697                end if;
2698             end if;
2699
2700          --  Case of initialization present but in error. Set initial
2701          --  expression as absent (but do not make above complaints)
2702
2703          elsif E = Error then
2704             Set_Expression (N, Empty);
2705             E := Empty;
2706
2707          --  Case of initialization present
2708
2709          else
2710             --  Not allowed in Ada 83
2711
2712             if not Constant_Present (N) then
2713                if Ada_Version = Ada_83
2714                  and then Comes_From_Source (Object_Definition (N))
2715                then
2716                   Error_Msg_N
2717                     ("(Ada 83) unconstrained variable not allowed",
2718                      Object_Definition (N));
2719                end if;
2720             end if;
2721
2722             --  Now we constrain the variable from the initializing expression
2723
2724             --  If the expression is an aggregate, it has been expanded into
2725             --  individual assignments. Retrieve the actual type from the
2726             --  expanded construct.
2727
2728             if Is_Array_Type (T)
2729               and then No_Initialization (N)
2730               and then Nkind (Original_Node (E)) = N_Aggregate
2731             then
2732                Act_T := Etype (E);
2733
2734             else
2735                Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
2736                Act_T := Find_Type_Of_Object (Object_Definition (N), N);
2737             end if;
2738
2739             Set_Is_Constr_Subt_For_U_Nominal (Act_T);
2740
2741             if Aliased_Present (N) then
2742                Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2743             end if;
2744
2745             Freeze_Before (N, Act_T);
2746             Freeze_Before (N, T);
2747          end if;
2748
2749       elsif Is_Array_Type (T)
2750         and then No_Initialization (N)
2751         and then Nkind (Original_Node (E)) = N_Aggregate
2752       then
2753          if not Is_Entity_Name (Object_Definition (N)) then
2754             Act_T := Etype (E);
2755             Check_Compile_Time_Size (Act_T);
2756
2757             if Aliased_Present (N) then
2758                Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2759             end if;
2760          end if;
2761
2762          --  When the given object definition and the aggregate are specified
2763          --  independently, and their lengths might differ do a length check.
2764          --  This cannot happen if the aggregate is of the form (others =>...)
2765
2766          if not Is_Constrained (T) then
2767             null;
2768
2769          elsif Nkind (E) = N_Raise_Constraint_Error then
2770
2771             --  Aggregate is statically illegal. Place back in declaration
2772
2773             Set_Expression (N, E);
2774             Set_No_Initialization (N, False);
2775
2776          elsif T = Etype (E) then
2777             null;
2778
2779          elsif Nkind (E) = N_Aggregate
2780            and then Present (Component_Associations (E))
2781            and then Present (Choices (First (Component_Associations (E))))
2782            and then Nkind (First
2783             (Choices (First (Component_Associations (E))))) = N_Others_Choice
2784          then
2785             null;
2786
2787          else
2788             Apply_Length_Check (E, T);
2789          end if;
2790
2791       --  If the type is limited unconstrained with defaulted discriminants
2792       --  and there is no expression, then the object is constrained by the
2793       --  defaults, so it is worthwhile building the corresponding subtype.
2794
2795       elsif (Is_Limited_Record (T)
2796                or else Is_Concurrent_Type (T))
2797         and then not Is_Constrained (T)
2798         and then Has_Discriminants (T)
2799       then
2800          if No (E) then
2801             Act_T := Build_Default_Subtype (T, N);
2802          else
2803             --  Ada 2005:  a limited object may be initialized by means of an
2804             --  aggregate. If the type has default discriminants it has an
2805             --  unconstrained nominal type, Its actual subtype will be obtained
2806             --  from the aggregate, and not from the default discriminants.
2807
2808             Act_T := Etype (E);
2809          end if;
2810
2811          Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
2812
2813       elsif Present (Underlying_Type (T))
2814         and then not Is_Constrained (Underlying_Type (T))
2815         and then Has_Discriminants (Underlying_Type (T))
2816         and then Nkind (E) = N_Function_Call
2817         and then Constant_Present (N)
2818       then
2819          --  The back-end has problems with constants of a discriminated type
2820          --  with defaults, if the initial value is a function call. We
2821          --  generate an intermediate temporary for the result of the call.
2822          --  It is unclear why this should make it acceptable to gcc. ???
2823
2824          Remove_Side_Effects (E);
2825       end if;
2826
2827       --  Check No_Wide_Characters restriction
2828
2829       if T = Standard_Wide_Character
2830         or else T = Standard_Wide_Wide_Character
2831         or else Root_Type (T) = Standard_Wide_String
2832         or else Root_Type (T) = Standard_Wide_Wide_String
2833       then
2834          Check_Restriction (No_Wide_Characters, Object_Definition (N));
2835       end if;
2836
2837       --  Indicate this is not set in source. Certainly true for constants,
2838       --  and true for variables so far (will be reset for a variable if and
2839       --  when we encounter a modification in the source).
2840
2841       Set_Never_Set_In_Source (Id, True);
2842
2843       --  Now establish the proper kind and type of the object
2844
2845       if Constant_Present (N) then
2846          Set_Ekind            (Id, E_Constant);
2847          Set_Is_True_Constant (Id, True);
2848
2849       else
2850          Set_Ekind (Id, E_Variable);
2851
2852          --  A variable is set as shared passive if it appears in a shared
2853          --  passive package, and is at the outer level. This is not done
2854          --  for entities generated during expansion, because those are
2855          --  always manipulated locally.
2856
2857          if Is_Shared_Passive (Current_Scope)
2858            and then Is_Library_Level_Entity (Id)
2859            and then Comes_From_Source (Id)
2860          then
2861             Set_Is_Shared_Passive (Id);
2862             Check_Shared_Var (Id, T, N);
2863          end if;
2864
2865          --  Set Has_Initial_Value if initializing expression present. Note
2866          --  that if there is no initializing expression, we leave the state
2867          --  of this flag unchanged (usually it will be False, but notably in
2868          --  the case of exception choice variables, it will already be true).
2869
2870          if Present (E) then
2871             Set_Has_Initial_Value (Id, True);
2872          end if;
2873       end if;
2874
2875       --  Initialize alignment and size and capture alignment setting
2876
2877       Init_Alignment               (Id);
2878       Init_Esize                   (Id);
2879       Set_Optimize_Alignment_Flags (Id);
2880
2881       --  Deal with aliased case
2882
2883       if Aliased_Present (N) then
2884          Set_Is_Aliased (Id);
2885
2886          --  If the object is aliased and the type is unconstrained with
2887          --  defaulted discriminants and there is no expression, then the
2888          --  object is constrained by the defaults, so it is worthwhile
2889          --  building the corresponding subtype.
2890
2891          --  Ada 2005 (AI-363): If the aliased object is discriminated and
2892          --  unconstrained, then only establish an actual subtype if the
2893          --  nominal subtype is indefinite. In definite cases the object is
2894          --  unconstrained in Ada 2005.
2895
2896          if No (E)
2897            and then Is_Record_Type (T)
2898            and then not Is_Constrained (T)
2899            and then Has_Discriminants (T)
2900            and then (Ada_Version < Ada_05 or else Is_Indefinite_Subtype (T))
2901          then
2902             Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
2903          end if;
2904       end if;
2905
2906       --  Now we can set the type of the object
2907
2908       Set_Etype (Id, Act_T);
2909
2910       --  Deal with controlled types
2911
2912       if Has_Controlled_Component (Etype (Id))
2913         or else Is_Controlled (Etype (Id))
2914       then
2915          if not Is_Library_Level_Entity (Id) then
2916             Check_Restriction (No_Nested_Finalization, N);
2917          else
2918             Validate_Controlled_Object (Id);
2919          end if;
2920
2921          --  Generate a warning when an initialization causes an obvious ABE
2922          --  violation. If the init expression is a simple aggregate there
2923          --  shouldn't be any initialize/adjust call generated. This will be
2924          --  true as soon as aggregates are built in place when possible.
2925
2926          --  ??? at the moment we do not generate warnings for temporaries
2927          --  created for those aggregates although Program_Error might be
2928          --  generated if compiled with -gnato.
2929
2930          if Is_Controlled (Etype (Id))
2931             and then Comes_From_Source (Id)
2932          then
2933             declare
2934                BT : constant Entity_Id := Base_Type (Etype (Id));
2935
2936                Implicit_Call : Entity_Id;
2937                pragma Warnings (Off, Implicit_Call);
2938                --  ??? what is this for (never referenced!)
2939
2940                function Is_Aggr (N : Node_Id) return Boolean;
2941                --  Check that N is an aggregate
2942
2943                -------------
2944                -- Is_Aggr --
2945                -------------
2946
2947                function Is_Aggr (N : Node_Id) return Boolean is
2948                begin
2949                   case Nkind (Original_Node (N)) is
2950                      when N_Aggregate | N_Extension_Aggregate =>
2951                         return True;
2952
2953                      when N_Qualified_Expression |
2954                           N_Type_Conversion      |
2955                           N_Unchecked_Type_Conversion =>
2956                         return Is_Aggr (Expression (Original_Node (N)));
2957
2958                      when others =>
2959                         return False;
2960                   end case;
2961                end Is_Aggr;
2962
2963             begin
2964                --  If no underlying type, we already are in an error situation.
2965                --  Do not try to add a warning since we do not have access to
2966                --  prim-op list.
2967
2968                if No (Underlying_Type (BT)) then
2969                   Implicit_Call := Empty;
2970
2971                --  A generic type does not have usable primitive operators.
2972                --  Initialization calls are built for instances.
2973
2974                elsif Is_Generic_Type (BT) then
2975                   Implicit_Call := Empty;
2976
2977                --  If the init expression is not an aggregate, an adjust call
2978                --  will be generated
2979
2980                elsif Present (E) and then not Is_Aggr (E) then
2981                   Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
2982
2983                --  If no init expression and we are not in the deferred
2984                --  constant case, an Initialize call will be generated
2985
2986                elsif No (E) and then not Constant_Present (N) then
2987                   Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
2988
2989                else
2990                   Implicit_Call := Empty;
2991                end if;
2992             end;
2993          end if;
2994       end if;
2995
2996       if Has_Task (Etype (Id)) then
2997          Check_Restriction (No_Tasking, N);
2998
2999          --  Deal with counting max tasks
3000
3001          --  Nothing to do if inside a generic
3002
3003          if Inside_A_Generic then
3004             null;
3005
3006          --  If library level entity, then count tasks
3007
3008          elsif Is_Library_Level_Entity (Id) then
3009             Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
3010
3011          --  If not library level entity, then indicate we don't know max
3012          --  tasks and also check task hierarchy restriction and blocking
3013          --  operation (since starting a task is definitely blocking!)
3014
3015          else
3016             Check_Restriction (Max_Tasks, N);
3017             Check_Restriction (No_Task_Hierarchy, N);
3018             Check_Potentially_Blocking_Operation (N);
3019          end if;
3020
3021          --  A rather specialized test. If we see two tasks being declared
3022          --  of the same type in the same object declaration, and the task
3023          --  has an entry with an address clause, we know that program error
3024          --  will be raised at run-time since we can't have two tasks with
3025          --  entries at the same address.
3026
3027          if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
3028             declare
3029                E : Entity_Id;
3030
3031             begin
3032                E := First_Entity (Etype (Id));
3033                while Present (E) loop
3034                   if Ekind (E) = E_Entry
3035                     and then Present (Get_Attribute_Definition_Clause
3036                                         (E, Attribute_Address))
3037                   then
3038                      Error_Msg_N
3039                        ("?more than one task with same entry address", N);
3040                      Error_Msg_N
3041                        ("\?Program_Error will be raised at run time", N);
3042                      Insert_Action (N,
3043                        Make_Raise_Program_Error (Loc,
3044                          Reason => PE_Duplicated_Entry_Address));
3045                      exit;
3046                   end if;
3047
3048                   Next_Entity (E);
3049                end loop;
3050             end;
3051          end if;
3052       end if;
3053
3054       --  Some simple constant-propagation: if the expression is a constant
3055       --  string initialized with a literal, share the literal. This avoids
3056       --  a run-time copy.
3057
3058       if Present (E)
3059         and then Is_Entity_Name (E)
3060         and then Ekind (Entity (E)) = E_Constant
3061         and then Base_Type (Etype (E)) = Standard_String
3062       then
3063          declare
3064             Val : constant Node_Id := Constant_Value (Entity (E));
3065          begin
3066             if Present (Val)
3067               and then Nkind (Val) = N_String_Literal
3068             then
3069                Rewrite (E, New_Copy (Val));
3070             end if;
3071          end;
3072       end if;
3073
3074       --  Another optimization: if the nominal subtype is unconstrained and
3075       --  the expression is a function call that returns an unconstrained
3076       --  type, rewrite the declaration as a renaming of the result of the
3077       --  call. The exceptions below are cases where the copy is expected,
3078       --  either by the back end (Aliased case) or by the semantics, as for
3079       --  initializing controlled types or copying tags for classwide types.
3080
3081       if Present (E)
3082         and then Nkind (E) = N_Explicit_Dereference
3083         and then Nkind (Original_Node (E)) = N_Function_Call
3084         and then not Is_Library_Level_Entity (Id)
3085         and then not Is_Constrained (Underlying_Type (T))
3086         and then not Is_Aliased (Id)
3087         and then not Is_Class_Wide_Type (T)
3088         and then not Is_Controlled (T)
3089         and then not Has_Controlled_Component (Base_Type (T))
3090         and then Expander_Active
3091       then
3092          Rewrite (N,
3093            Make_Object_Renaming_Declaration (Loc,
3094              Defining_Identifier => Id,
3095              Access_Definition   => Empty,
3096              Subtype_Mark        => New_Occurrence_Of
3097                                       (Base_Type (Etype (Id)), Loc),
3098              Name                => E));
3099
3100          Set_Renamed_Object (Id, E);
3101
3102          --  Force generation of debugging information for the constant and for
3103          --  the renamed function call.
3104
3105          Set_Debug_Info_Needed (Id);
3106          Set_Debug_Info_Needed (Entity (Prefix (E)));
3107       end if;
3108
3109       if Present (Prev_Entity)
3110         and then Is_Frozen (Prev_Entity)
3111         and then not Error_Posted (Id)
3112       then
3113          Error_Msg_N ("full constant declaration appears too late", N);
3114       end if;
3115
3116       Check_Eliminated (Id);
3117
3118       --  Deal with setting In_Private_Part flag if in private part
3119
3120       if Ekind (Scope (Id)) = E_Package
3121         and then In_Private_Part (Scope (Id))
3122       then
3123          Set_In_Private_Part (Id);
3124       end if;
3125
3126       --  Check for violation of No_Local_Timing_Events
3127
3128       if Is_RTE (Etype (Id), RE_Timing_Event)
3129         and then not Is_Library_Level_Entity (Id)
3130       then
3131          Check_Restriction (No_Local_Timing_Events, N);
3132       end if;
3133    end Analyze_Object_Declaration;
3134
3135    ---------------------------
3136    -- Analyze_Others_Choice --
3137    ---------------------------
3138
3139    --  Nothing to do for the others choice node itself, the semantic analysis
3140    --  of the others choice will occur as part of the processing of the parent
3141
3142    procedure Analyze_Others_Choice (N : Node_Id) is
3143       pragma Warnings (Off, N);
3144    begin
3145       null;
3146    end Analyze_Others_Choice;
3147
3148    -------------------------------------------
3149    -- Analyze_Private_Extension_Declaration --
3150    -------------------------------------------
3151
3152    procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
3153       T           : constant Entity_Id := Defining_Identifier (N);
3154       Indic       : constant Node_Id   := Subtype_Indication (N);
3155       Parent_Type : Entity_Id;
3156       Parent_Base : Entity_Id;
3157
3158    begin
3159       --  Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
3160
3161       if Is_Non_Empty_List (Interface_List (N)) then
3162          declare
3163             Intf : Node_Id;
3164             T    : Entity_Id;
3165
3166          begin
3167             Intf := First (Interface_List (N));
3168             while Present (Intf) loop
3169                T := Find_Type_Of_Subtype_Indic (Intf);
3170
3171                Diagnose_Interface (Intf, T);
3172                Next (Intf);
3173             end loop;
3174          end;
3175       end if;
3176
3177       Generate_Definition (T);
3178       Enter_Name (T);
3179
3180       Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
3181       Parent_Base := Base_Type (Parent_Type);
3182
3183       if Parent_Type = Any_Type
3184         or else Etype (Parent_Type) = Any_Type
3185       then
3186          Set_Ekind (T, Ekind (Parent_Type));
3187          Set_Etype (T, Any_Type);
3188          return;
3189
3190       elsif not Is_Tagged_Type (Parent_Type) then
3191          Error_Msg_N
3192            ("parent of type extension must be a tagged type ", Indic);
3193          return;
3194
3195       elsif Ekind (Parent_Type) = E_Void
3196         or else Ekind (Parent_Type) = E_Incomplete_Type
3197       then
3198          Error_Msg_N ("premature derivation of incomplete type", Indic);
3199          return;
3200
3201       elsif Is_Concurrent_Type (Parent_Type) then
3202          Error_Msg_N
3203            ("parent type of a private extension cannot be "
3204             & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
3205
3206          Set_Etype              (T, Any_Type);
3207          Set_Ekind              (T, E_Limited_Private_Type);
3208          Set_Private_Dependents (T, New_Elmt_List);
3209          Set_Error_Posted       (T);
3210          return;
3211       end if;
3212
3213       --  Perhaps the parent type should be changed to the class-wide type's
3214       --  specific type in this case to prevent cascading errors ???
3215
3216       if Is_Class_Wide_Type (Parent_Type) then
3217          Error_Msg_N
3218            ("parent of type extension must not be a class-wide type", Indic);
3219          return;
3220       end if;
3221
3222       if (not Is_Package_Or_Generic_Package (Current_Scope)
3223            and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
3224         or else In_Private_Part (Current_Scope)
3225
3226       then
3227          Error_Msg_N ("invalid context for private extension", N);
3228       end if;
3229
3230       --  Set common attributes
3231
3232       Set_Is_Pure          (T, Is_Pure (Current_Scope));
3233       Set_Scope            (T, Current_Scope);
3234       Set_Ekind            (T, E_Record_Type_With_Private);
3235       Init_Size_Align      (T);
3236
3237       Set_Etype            (T,            Parent_Base);
3238       Set_Has_Task         (T, Has_Task  (Parent_Base));
3239
3240       Set_Convention       (T, Convention     (Parent_Type));
3241       Set_First_Rep_Item   (T, First_Rep_Item (Parent_Type));
3242       Set_Is_First_Subtype (T);
3243       Make_Class_Wide_Type (T);
3244
3245       if Unknown_Discriminants_Present (N) then
3246          Set_Discriminant_Constraint (T, No_Elist);
3247       end if;
3248
3249       Build_Derived_Record_Type (N, Parent_Type, T);
3250
3251       --  Ada 2005 (AI-443): Synchronized private extension or a rewritten
3252       --  synchronized formal derived type.
3253
3254       if Ada_Version >= Ada_05
3255         and then Synchronized_Present (N)
3256       then
3257          Set_Is_Limited_Record (T);
3258
3259          --  Formal derived type case
3260
3261          if Is_Generic_Type (T) then
3262
3263             --  The parent must be a tagged limited type or a synchronized
3264             --  interface.
3265
3266             if (not Is_Tagged_Type (Parent_Type)
3267                   or else not Is_Limited_Type (Parent_Type))
3268               and then
3269                (not Is_Interface (Parent_Type)
3270                   or else not Is_Synchronized_Interface (Parent_Type))
3271             then
3272                Error_Msg_NE ("parent type of & must be tagged limited " &
3273                              "or synchronized", N, T);
3274             end if;
3275
3276             --  The progenitors (if any) must be limited or synchronized
3277             --  interfaces.
3278
3279             if Present (Interfaces (T)) then
3280                declare
3281                   Iface      : Entity_Id;
3282                   Iface_Elmt : Elmt_Id;
3283
3284                begin
3285                   Iface_Elmt := First_Elmt (Interfaces (T));
3286                   while Present (Iface_Elmt) loop
3287                      Iface := Node (Iface_Elmt);
3288
3289                      if not Is_Limited_Interface (Iface)
3290                        and then not Is_Synchronized_Interface (Iface)
3291                      then
3292                         Error_Msg_NE ("progenitor & must be limited " &
3293                                       "or synchronized", N, Iface);
3294                      end if;
3295
3296                      Next_Elmt (Iface_Elmt);
3297                   end loop;
3298                end;
3299             end if;
3300
3301          --  Regular derived extension, the parent must be a limited or
3302          --  synchronized interface.
3303
3304          else
3305             if not Is_Interface (Parent_Type)
3306               or else (not Is_Limited_Interface (Parent_Type)
3307                          and then
3308                        not Is_Synchronized_Interface (Parent_Type))
3309             then
3310                Error_Msg_NE
3311                  ("parent type of & must be limited interface", N, T);
3312             end if;
3313          end if;
3314
3315       --  A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
3316       --  extension with a synchronized parent must be explicitly declared
3317       --  synchronized, because the full view will be a synchronized type.
3318       --  This must be checked before the check for limited types below,
3319       --  to ensure that types declared limited are not allowed to extend
3320       --  synchronized interfaces.
3321
3322       elsif Is_Interface (Parent_Type)
3323         and then Is_Synchronized_Interface (Parent_Type)
3324         and then not Synchronized_Present (N)
3325       then
3326          Error_Msg_NE
3327            ("private extension of& must be explicitly synchronized",
3328              N, Parent_Type);
3329
3330       elsif Limited_Present (N) then
3331          Set_Is_Limited_Record (T);
3332
3333          if not Is_Limited_Type (Parent_Type)
3334            and then
3335              (not Is_Interface (Parent_Type)
3336                or else not Is_Limited_Interface (Parent_Type))
3337          then
3338             Error_Msg_NE ("parent type& of limited extension must be limited",
3339               N, Parent_Type);
3340          end if;
3341       end if;
3342    end Analyze_Private_Extension_Declaration;
3343
3344    ---------------------------------
3345    -- Analyze_Subtype_Declaration --
3346    ---------------------------------
3347
3348    procedure Analyze_Subtype_Declaration
3349      (N    : Node_Id;
3350       Skip : Boolean := False)
3351    is
3352       Id       : constant Entity_Id := Defining_Identifier (N);
3353       T        : Entity_Id;
3354       R_Checks : Check_Result;
3355
3356    begin
3357       Generate_Definition (Id);
3358       Set_Is_Pure (Id, Is_Pure (Current_Scope));
3359       Init_Size_Align (Id);
3360
3361       --  The following guard condition on Enter_Name is to handle cases where
3362       --  the defining identifier has already been entered into the scope but
3363       --  the declaration as a whole needs to be analyzed.
3364
3365       --  This case in particular happens for derived enumeration types. The
3366       --  derived enumeration type is processed as an inserted enumeration type
3367       --  declaration followed by a rewritten subtype declaration. The defining
3368       --  identifier, however, is entered into the name scope very early in the
3369       --  processing of the original type declaration and therefore needs to be
3370       --  avoided here, when the created subtype declaration is analyzed. (See
3371       --  Build_Derived_Types)
3372
3373       --  This also happens when the full view of a private type is derived
3374       --  type with constraints. In this case the entity has been introduced
3375       --  in the private declaration.
3376
3377       if Skip
3378         or else (Present (Etype (Id))
3379                    and then (Is_Private_Type (Etype (Id))
3380                                or else Is_Task_Type (Etype (Id))
3381                                or else Is_Rewrite_Substitution (N)))
3382       then
3383          null;
3384
3385       else
3386          Enter_Name (Id);
3387       end if;
3388
3389       T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
3390
3391       --  Inherit common attributes
3392
3393       Set_Is_Generic_Type   (Id, Is_Generic_Type   (Base_Type (T)));
3394       Set_Is_Volatile       (Id, Is_Volatile       (T));
3395       Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
3396       Set_Is_Atomic         (Id, Is_Atomic         (T));
3397       Set_Is_Ada_2005_Only  (Id, Is_Ada_2005_Only  (T));
3398       Set_Convention        (Id, Convention        (T));
3399
3400       --  In the case where there is no constraint given in the subtype
3401       --  indication, Process_Subtype just returns the Subtype_Mark, so its
3402       --  semantic attributes must be established here.
3403
3404       if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
3405          Set_Etype (Id, Base_Type (T));
3406
3407          case Ekind (T) is
3408             when Array_Kind =>
3409                Set_Ekind                       (Id, E_Array_Subtype);
3410                Copy_Array_Subtype_Attributes   (Id, T);
3411
3412             when Decimal_Fixed_Point_Kind =>
3413                Set_Ekind                (Id, E_Decimal_Fixed_Point_Subtype);
3414                Set_Digits_Value         (Id, Digits_Value       (T));
3415                Set_Delta_Value          (Id, Delta_Value        (T));
3416                Set_Scale_Value          (Id, Scale_Value        (T));
3417                Set_Small_Value          (Id, Small_Value        (T));
3418                Set_Scalar_Range         (Id, Scalar_Range       (T));
3419                Set_Machine_Radix_10     (Id, Machine_Radix_10   (T));
3420                Set_Is_Constrained       (Id, Is_Constrained     (T));
3421                Set_Is_Known_Valid       (Id, Is_Known_Valid     (T));
3422                Set_RM_Size              (Id, RM_Size            (T));
3423
3424             when Enumeration_Kind =>
3425                Set_Ekind                (Id, E_Enumeration_Subtype);
3426                Set_First_Literal        (Id, First_Literal (Base_Type (T)));
3427                Set_Scalar_Range         (Id, Scalar_Range       (T));
3428                Set_Is_Character_Type    (Id, Is_Character_Type  (T));
3429                Set_Is_Constrained       (Id, Is_Constrained     (T));
3430                Set_Is_Known_Valid       (Id, Is_Known_Valid     (T));
3431                Set_RM_Size              (Id, RM_Size            (T));
3432
3433             when Ordinary_Fixed_Point_Kind =>
3434                Set_Ekind                (Id, E_Ordinary_Fixed_Point_Subtype);
3435                Set_Scalar_Range         (Id, Scalar_Range       (T));
3436                Set_Small_Value          (Id, Small_Value        (T));
3437                Set_Delta_Value          (Id, Delta_Value        (T));
3438                Set_Is_Constrained       (Id, Is_Constrained     (T));
3439                Set_Is_Known_Valid       (Id, Is_Known_Valid     (T));
3440                Set_RM_Size              (Id, RM_Size            (T));
3441
3442             when Float_Kind =>
3443                Set_Ekind                (Id, E_Floating_Point_Subtype);
3444                Set_Scalar_Range         (Id, Scalar_Range       (T));
3445                Set_Digits_Value         (Id, Digits_Value       (T));
3446                Set_Is_Constrained       (Id, Is_Constrained     (T));
3447
3448             when Signed_Integer_Kind =>
3449                Set_Ekind                (Id, E_Signed_Integer_Subtype);
3450                Set_Scalar_Range         (Id, Scalar_Range       (T));
3451                Set_Is_Constrained       (Id, Is_Constrained     (T));
3452                Set_Is_Known_Valid       (Id, Is_Known_Valid     (T));
3453                Set_RM_Size              (Id, RM_Size            (T));
3454
3455             when Modular_Integer_Kind =>
3456                Set_Ekind                (Id, E_Modular_Integer_Subtype);
3457                Set_Scalar_Range         (Id, Scalar_Range       (T));
3458                Set_Is_Constrained       (Id, Is_Constrained     (T));
3459                Set_Is_Known_Valid       (Id, Is_Known_Valid     (T));
3460                Set_RM_Size              (Id, RM_Size            (T));
3461
3462             when Class_Wide_Kind =>
3463                Set_Ekind                (Id, E_Class_Wide_Subtype);
3464                Set_First_Entity         (Id, First_Entity       (T));
3465                Set_Last_Entity          (Id, Last_Entity        (T));
3466                Set_Class_Wide_Type      (Id, Class_Wide_Type    (T));
3467                Set_Cloned_Subtype       (Id, T);
3468                Set_Is_Tagged_Type       (Id, True);
3469                Set_Has_Unknown_Discriminants
3470                                         (Id, True);
3471
3472                if Ekind (T) = E_Class_Wide_Subtype then
3473                   Set_Equivalent_Type   (Id, Equivalent_Type    (T));
3474                end if;
3475
3476             when E_Record_Type | E_Record_Subtype =>
3477                Set_Ekind                (Id, E_Record_Subtype);
3478
3479                if Ekind (T) = E_Record_Subtype
3480                  and then Present (Cloned_Subtype (T))
3481                then
3482                   Set_Cloned_Subtype    (Id, Cloned_Subtype (T));
3483                else
3484                   Set_Cloned_Subtype    (Id, T);
3485                end if;
3486
3487                Set_First_Entity         (Id, First_Entity       (T));
3488                Set_Last_Entity          (Id, Last_Entity        (T));
3489                Set_Has_Discriminants    (Id, Has_Discriminants  (T));
3490                Set_Is_Constrained       (Id, Is_Constrained     (T));
3491                Set_Is_Limited_Record    (Id, Is_Limited_Record  (T));
3492                Set_Has_Unknown_Discriminants
3493                                         (Id, Has_Unknown_Discriminants (T));
3494
3495                if Has_Discriminants (T) then
3496                   Set_Discriminant_Constraint
3497                                         (Id, Discriminant_Constraint (T));
3498                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3499
3500                elsif Has_Unknown_Discriminants (Id) then
3501                   Set_Discriminant_Constraint (Id, No_Elist);
3502                end if;
3503
3504                if Is_Tagged_Type (T) then
3505                   Set_Is_Tagged_Type    (Id);
3506                   Set_Is_Abstract_Type  (Id, Is_Abstract_Type (T));
3507                   Set_Primitive_Operations
3508                                         (Id, Primitive_Operations (T));
3509                   Set_Class_Wide_Type   (Id, Class_Wide_Type (T));
3510
3511                   if Is_Interface (T) then
3512                      Set_Is_Interface (Id);
3513                      Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
3514                   end if;
3515                end if;
3516
3517             when Private_Kind =>
3518                Set_Ekind              (Id, Subtype_Kind (Ekind   (T)));
3519                Set_Has_Discriminants  (Id, Has_Discriminants     (T));
3520                Set_Is_Constrained     (Id, Is_Constrained        (T));
3521                Set_First_Entity       (Id, First_Entity          (T));
3522                Set_Last_Entity        (Id, Last_Entity           (T));
3523                Set_Private_Dependents (Id, New_Elmt_List);
3524                Set_Is_Limited_Record  (Id, Is_Limited_Record     (T));
3525                Set_Has_Unknown_Discriminants
3526                                       (Id, Has_Unknown_Discriminants (T));
3527                Set_Known_To_Have_Preelab_Init
3528                                       (Id, Known_To_Have_Preelab_Init (T));
3529
3530                if Is_Tagged_Type (T) then
3531                   Set_Is_Tagged_Type       (Id);
3532                   Set_Is_Abstract_Type     (Id, Is_Abstract_Type (T));
3533                   Set_Primitive_Operations (Id, Primitive_Operations (T));
3534                   Set_Class_Wide_Type      (Id, Class_Wide_Type (T));
3535                end if;
3536
3537                --  In general the attributes of the subtype of a private type
3538                --  are the attributes of the partial view of parent. However,
3539                --  the full view may be a discriminated type, and the subtype
3540                --  must share the discriminant constraint to generate correct
3541                --  calls to initialization procedures.
3542
3543                if Has_Discriminants (T) then
3544                   Set_Discriminant_Constraint
3545                                      (Id, Discriminant_Constraint (T));
3546                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3547
3548                elsif Present (Full_View (T))
3549                  and then Has_Discriminants (Full_View (T))
3550                then
3551                   Set_Discriminant_Constraint
3552                                (Id, Discriminant_Constraint (Full_View (T)));
3553                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3554
3555                   --  This would seem semantically correct, but apparently
3556                   --  confuses the back-end. To be explained and checked with
3557                   --  current version ???
3558
3559                   --  Set_Has_Discriminants (Id);
3560                end if;
3561
3562                Prepare_Private_Subtype_Completion (Id, N);
3563
3564             when Access_Kind =>
3565                Set_Ekind             (Id, E_Access_Subtype);
3566                Set_Is_Constrained    (Id, Is_Constrained        (T));
3567                Set_Is_Access_Constant
3568                                      (Id, Is_Access_Constant    (T));
3569                Set_Directly_Designated_Type
3570                                      (Id, Designated_Type       (T));
3571                Set_Can_Never_Be_Null (Id, Can_Never_Be_Null     (T));
3572
3573                --  A Pure library_item must not contain the declaration of a
3574                --  named access type, except within a subprogram, generic
3575                --  subprogram, task unit, or protected unit, or if it has
3576                --  a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
3577
3578                if Comes_From_Source (Id)
3579                  and then In_Pure_Unit
3580                  and then not In_Subprogram_Task_Protected_Unit
3581                  and then not No_Pool_Assigned (Id)
3582                then
3583                   Error_Msg_N
3584                     ("named access types not allowed in pure unit", N);
3585                end if;
3586
3587             when Concurrent_Kind =>
3588                Set_Ekind                (Id, Subtype_Kind (Ekind   (T)));
3589                Set_Corresponding_Record_Type (Id,
3590                                          Corresponding_Record_Type (T));
3591                Set_First_Entity         (Id, First_Entity          (T));
3592                Set_First_Private_Entity (Id, First_Private_Entity  (T));
3593                Set_Has_Discriminants    (Id, Has_Discriminants     (T));
3594                Set_Is_Constrained       (Id, Is_Constrained        (T));
3595                Set_Is_Tagged_Type       (Id, Is_Tagged_Type        (T));
3596                Set_Last_Entity          (Id, Last_Entity           (T));
3597
3598                if Has_Discriminants (T) then
3599                   Set_Discriminant_Constraint (Id,
3600                                            Discriminant_Constraint (T));
3601                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3602                end if;
3603
3604             when E_Incomplete_Type =>
3605                if Ada_Version >= Ada_05 then
3606                   Set_Ekind (Id, E_Incomplete_Subtype);
3607
3608                   --  Ada 2005 (AI-412): Decorate an incomplete subtype
3609                   --  of an incomplete type visible through a limited
3610                   --  with clause.
3611
3612                   if From_With_Type (T)
3613                     and then Present (Non_Limited_View (T))
3614                   then
3615                      Set_From_With_Type   (Id);
3616                      Set_Non_Limited_View (Id, Non_Limited_View (T));
3617
3618                   --  Ada 2005 (AI-412): Add the regular incomplete subtype
3619                   --  to the private dependents of the original incomplete
3620                   --  type for future transformation.
3621
3622                   else
3623                      Append_Elmt (Id, Private_Dependents (T));
3624                   end if;
3625
3626                --  If the subtype name denotes an incomplete type an error
3627                --  was already reported by Process_Subtype.
3628
3629                else
3630                   Set_Etype (Id, Any_Type);
3631                end if;
3632
3633             when others =>
3634                raise Program_Error;
3635          end case;
3636       end if;
3637
3638       if Etype (Id) = Any_Type then
3639          return;
3640       end if;
3641
3642       --  Some common processing on all types
3643
3644       Set_Size_Info      (Id,                 T);
3645       Set_First_Rep_Item (Id, First_Rep_Item (T));
3646
3647       T := Etype (Id);
3648
3649       Set_Is_Immediately_Visible   (Id, True);
3650       Set_Depends_On_Private       (Id, Has_Private_Component (T));
3651       Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
3652
3653       if Is_Interface (T) then
3654          Set_Is_Interface (Id);
3655       end if;
3656
3657       if Present (Generic_Parent_Type (N))
3658         and then
3659           (Nkind
3660              (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
3661             or else Nkind
3662               (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
3663                 /=  N_Formal_Private_Type_Definition)
3664       then
3665          if Is_Tagged_Type (Id) then
3666
3667             --  If this is a generic actual subtype for a synchronized type,
3668             --  the primitive operations are those of the corresponding record
3669             --  for which there is a separate subtype declaration.
3670
3671             if Is_Concurrent_Type (Id) then
3672                null;
3673             elsif Is_Class_Wide_Type (Id) then
3674                Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
3675             else
3676                Derive_Subprograms (Generic_Parent_Type (N), Id, T);
3677             end if;
3678
3679          elsif Scope (Etype (Id)) /= Standard_Standard then
3680             Derive_Subprograms (Generic_Parent_Type (N), Id);
3681          end if;
3682       end if;
3683
3684       if Is_Private_Type (T)
3685         and then Present (Full_View (T))
3686       then
3687          Conditional_Delay (Id, Full_View (T));
3688
3689       --  The subtypes of components or subcomponents of protected types
3690       --  do not need freeze nodes, which would otherwise appear in the
3691       --  wrong scope (before the freeze node for the protected type). The
3692       --  proper subtypes are those of the subcomponents of the corresponding
3693       --  record.
3694
3695       elsif Ekind (Scope (Id)) /= E_Protected_Type
3696         and then Present (Scope (Scope (Id))) -- error defense!
3697         and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
3698       then
3699          Conditional_Delay (Id, T);
3700       end if;
3701
3702       --  Check that constraint_error is raised for a scalar subtype
3703       --  indication when the lower or upper bound of a non-null range
3704       --  lies outside the range of the type mark.
3705
3706       if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
3707          if Is_Scalar_Type (Etype (Id))
3708             and then Scalar_Range (Id) /=
3709                      Scalar_Range (Etype (Subtype_Mark
3710                                            (Subtype_Indication (N))))
3711          then
3712             Apply_Range_Check
3713               (Scalar_Range (Id),
3714                Etype (Subtype_Mark (Subtype_Indication (N))));
3715
3716          elsif Is_Array_Type (Etype (Id))
3717            and then Present (First_Index (Id))
3718          then
3719             --  This really should be a subprogram that finds the indications
3720             --  to check???
3721
3722             if ((Nkind (First_Index (Id)) = N_Identifier
3723                    and then Ekind (Entity (First_Index (Id))) in Scalar_Kind)
3724                  or else Nkind (First_Index (Id)) = N_Subtype_Indication)
3725               and then
3726                 Nkind (Scalar_Range (Etype (First_Index (Id)))) = N_Range
3727             then
3728                declare
3729                   Target_Typ : constant Entity_Id :=
3730                                  Etype
3731                                    (First_Index (Etype
3732                                      (Subtype_Mark (Subtype_Indication (N)))));
3733                begin
3734                   R_Checks :=
3735                     Get_Range_Checks
3736                       (Scalar_Range (Etype (First_Index (Id))),
3737                        Target_Typ,
3738                        Etype (First_Index (Id)),
3739                        Defining_Identifier (N));
3740
3741                   Insert_Range_Checks
3742                     (R_Checks,
3743                      N,
3744                      Target_Typ,
3745                      Sloc (Defining_Identifier (N)));
3746                end;
3747             end if;
3748          end if;
3749       end if;
3750
3751       Set_Optimize_Alignment_Flags (Id);
3752       Check_Eliminated (Id);
3753    end Analyze_Subtype_Declaration;
3754
3755    --------------------------------
3756    -- Analyze_Subtype_Indication --
3757    --------------------------------
3758
3759    procedure Analyze_Subtype_Indication (N : Node_Id) is
3760       T : constant Entity_Id := Subtype_Mark (N);
3761       R : constant Node_Id   := Range_Expression (Constraint (N));
3762
3763    begin
3764       Analyze (T);
3765
3766       if R /= Error then
3767          Analyze (R);
3768          Set_Etype (N, Etype (R));
3769          Resolve (R, Entity (T));
3770       else
3771          Set_Error_Posted (R);
3772          Set_Error_Posted (T);
3773       end if;
3774    end Analyze_Subtype_Indication;
3775
3776    ------------------------------
3777    -- Analyze_Type_Declaration --
3778    ------------------------------
3779
3780    procedure Analyze_Type_Declaration (N : Node_Id) is
3781       Def    : constant Node_Id   := Type_Definition (N);
3782       Def_Id : constant Entity_Id := Defining_Identifier (N);
3783       T      : Entity_Id;
3784       Prev   : Entity_Id;
3785
3786       Is_Remote : constant Boolean :=
3787                     (Is_Remote_Types (Current_Scope)
3788                        or else Is_Remote_Call_Interface (Current_Scope))
3789                     and then not (In_Private_Part (Current_Scope)
3790                                     or else In_Package_Body (Current_Scope));
3791
3792       procedure Check_Ops_From_Incomplete_Type;
3793       --  If there is a tagged incomplete partial view of the type, transfer
3794       --  its operations to the full view, and indicate that the type of the
3795       --  controlling parameter (s) is this full view.
3796
3797       ------------------------------------
3798       -- Check_Ops_From_Incomplete_Type --
3799       ------------------------------------
3800
3801       procedure Check_Ops_From_Incomplete_Type is
3802          Elmt   : Elmt_Id;
3803          Formal : Entity_Id;
3804          Op     : Entity_Id;
3805
3806       begin
3807          if Prev /= T
3808            and then Ekind (Prev) = E_Incomplete_Type
3809            and then Is_Tagged_Type (Prev)
3810            and then Is_Tagged_Type (T)
3811          then
3812             Elmt := First_Elmt (Primitive_Operations (Prev));
3813             while Present (Elmt) loop
3814                Op := Node (Elmt);
3815                Prepend_Elmt (Op, Primitive_Operations (T));
3816
3817                Formal := First_Formal (Op);
3818                while Present (Formal) loop
3819                   if Etype (Formal) = Prev then
3820                      Set_Etype (Formal, T);
3821                   end if;
3822
3823                   Next_Formal (Formal);
3824                end loop;
3825
3826                if Etype (Op) = Prev then
3827                   Set_Etype (Op, T);
3828                end if;
3829
3830                Next_Elmt (Elmt);
3831             end loop;
3832          end if;
3833       end Check_Ops_From_Incomplete_Type;
3834
3835    --  Start of processing for Analyze_Type_Declaration
3836
3837    begin
3838       Prev := Find_Type_Name (N);
3839
3840       --  The full view, if present, now points to the current type
3841
3842       --  Ada 2005 (AI-50217): If the type was previously decorated when
3843       --  imported through a LIMITED WITH clause, it appears as incomplete
3844       --  but has no full view.
3845       --  If the incomplete view is tagged, a class_wide type has been
3846       --  created already. Use it for the full view as well, to prevent
3847       --  multiple incompatible class-wide types that may be  created for
3848       --  self-referential anonymous access components.
3849
3850       if Ekind (Prev) = E_Incomplete_Type
3851         and then Present (Full_View (Prev))
3852       then
3853          T := Full_View (Prev);
3854
3855          if Is_Tagged_Type (Prev)
3856            and then Present (Class_Wide_Type (Prev))
3857          then
3858             Set_Ekind (T, Ekind (Prev));         --  will be reset later
3859             Set_Class_Wide_Type (T, Class_Wide_Type (Prev));
3860             Set_Etype (Class_Wide_Type (T), T);
3861          end if;
3862
3863       else
3864          T := Prev;
3865       end if;
3866
3867       Set_Is_Pure (T, Is_Pure (Current_Scope));
3868
3869       --  We set the flag Is_First_Subtype here. It is needed to set the
3870       --  corresponding flag for the Implicit class-wide-type created
3871       --  during tagged types processing.
3872
3873       Set_Is_First_Subtype (T, True);
3874
3875       --  Only composite types other than array types are allowed to have
3876       --  discriminants.
3877
3878       case Nkind (Def) is
3879
3880          --  For derived types, the rule will be checked once we've figured
3881          --  out the parent type.
3882
3883          when N_Derived_Type_Definition =>
3884             null;
3885
3886          --  For record types, discriminants are allowed
3887
3888          when N_Record_Definition =>
3889             null;
3890
3891          when others =>
3892             if Present (Discriminant_Specifications (N)) then
3893                Error_Msg_N
3894                  ("elementary or array type cannot have discriminants",
3895                   Defining_Identifier
3896                   (First (Discriminant_Specifications (N))));
3897             end if;
3898       end case;
3899
3900       --  Elaborate the type definition according to kind, and generate
3901       --  subsidiary (implicit) subtypes where needed. We skip this if it was
3902       --  already done (this happens during the reanalysis that follows a call
3903       --  to the high level optimizer).
3904
3905       if not Analyzed (T) then
3906          Set_Analyzed (T);
3907
3908          case Nkind (Def) is
3909
3910             when N_Access_To_Subprogram_Definition =>
3911                Access_Subprogram_Declaration (T, Def);
3912
3913                --  If this is a remote access to subprogram, we must create the
3914                --  equivalent fat pointer type, and related subprograms.
3915
3916                if Is_Remote then
3917                   Process_Remote_AST_Declaration (N);
3918                end if;
3919
3920                --  Validate categorization rule against access type declaration
3921                --  usually a violation in Pure unit, Shared_Passive unit.
3922
3923                Validate_Access_Type_Declaration (T, N);
3924
3925             when N_Access_To_Object_Definition =>
3926                Access_Type_Declaration (T, Def);
3927
3928                --  Validate categorization rule against access type declaration
3929                --  usually a violation in Pure unit, Shared_Passive unit.
3930
3931                Validate_Access_Type_Declaration (T, N);
3932
3933                --  If we are in a Remote_Call_Interface package and define a
3934                --  RACW, then calling stubs and specific stream attributes
3935                --  must be added.
3936
3937                if Is_Remote
3938                  and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3939                then
3940                   Add_RACW_Features (Def_Id);
3941                end if;
3942
3943                --  Set no strict aliasing flag if config pragma seen
3944
3945                if Opt.No_Strict_Aliasing then
3946                   Set_No_Strict_Aliasing (Base_Type (Def_Id));
3947                end if;
3948
3949             when N_Array_Type_Definition =>
3950                Array_Type_Declaration (T, Def);
3951
3952             when N_Derived_Type_Definition =>
3953                Derived_Type_Declaration (T, N, T /= Def_Id);
3954
3955             when N_Enumeration_Type_Definition =>
3956                Enumeration_Type_Declaration (T, Def);
3957
3958             when N_Floating_Point_Definition =>
3959                Floating_Point_Type_Declaration (T, Def);
3960
3961             when N_Decimal_Fixed_Point_Definition =>
3962                Decimal_Fixed_Point_Type_Declaration (T, Def);
3963
3964             when N_Ordinary_Fixed_Point_Definition =>
3965                Ordinary_Fixed_Point_Type_Declaration (T, Def);
3966
3967             when N_Signed_Integer_Type_Definition =>
3968                Signed_Integer_Type_Declaration (T, Def);
3969
3970             when N_Modular_Type_Definition =>
3971                Modular_Type_Declaration (T, Def);
3972
3973             when N_Record_Definition =>
3974                Record_Type_Declaration (T, N, Prev);
3975
3976             when others =>
3977                raise Program_Error;
3978
3979          end case;
3980       end if;
3981
3982       if Etype (T) = Any_Type then
3983          return;
3984       end if;
3985
3986       --  Some common processing for all types
3987
3988       Set_Depends_On_Private (T, Has_Private_Component (T));
3989       Check_Ops_From_Incomplete_Type;
3990
3991       --  Both the declared entity, and its anonymous base type if one
3992       --  was created, need freeze nodes allocated.
3993
3994       declare
3995          B : constant Entity_Id := Base_Type (T);
3996
3997       begin
3998          --  In the case where the base type differs from the first subtype, we
3999          --  pre-allocate a freeze node, and set the proper link to the first
4000          --  subtype. Freeze_Entity will use this preallocated freeze node when
4001          --  it freezes the entity.
4002
4003          --  This does not apply if the base type is a generic type, whose
4004          --  declaration is independent of the current derived definition.
4005
4006          if B /= T and then not Is_Generic_Type (B) then
4007             Ensure_Freeze_Node (B);
4008             Set_First_Subtype_Link (Freeze_Node (B), T);
4009          end if;
4010
4011          --  A type that is imported through a limited_with clause cannot
4012          --  generate any code, and thus need not be frozen. However, an access
4013          --  type with an imported designated type needs a finalization list,
4014          --  which may be referenced in some other package that has non-limited
4015          --  visibility on the designated type. Thus we must create the
4016          --  finalization list at the point the access type is frozen, to
4017          --  prevent unsatisfied references at link time.
4018
4019          if not From_With_Type (T) or else Is_Access_Type (T) then
4020             Set_Has_Delayed_Freeze (T);
4021          end if;
4022       end;
4023
4024       --  Case where T is the full declaration of some private type which has
4025       --  been swapped in Defining_Identifier (N).
4026
4027       if T /= Def_Id and then Is_Private_Type (Def_Id) then
4028          Process_Full_View (N, T, Def_Id);
4029
4030          --  Record the reference. The form of this is a little strange, since
4031          --  the full declaration has been swapped in. So the first parameter
4032          --  here represents the entity to which a reference is made which is
4033          --  the "real" entity, i.e. the one swapped in, and the second
4034          --  parameter provides the reference location.
4035
4036          --  Also, we want to kill Has_Pragma_Unreferenced temporarily here
4037          --  since we don't want a complaint about the full type being an
4038          --  unwanted reference to the private type
4039
4040          declare
4041             B : constant Boolean := Has_Pragma_Unreferenced (T);
4042          begin
4043             Set_Has_Pragma_Unreferenced (T, False);
4044             Generate_Reference (T, T, 'c');
4045             Set_Has_Pragma_Unreferenced (T, B);
4046          end;
4047
4048          Set_Completion_Referenced (Def_Id);
4049
4050       --  For completion of incomplete type, process incomplete dependents
4051       --  and always mark the full type as referenced (it is the incomplete
4052       --  type that we get for any real reference).
4053
4054       elsif Ekind (Prev) = E_Incomplete_Type then
4055          Process_Incomplete_Dependents (N, T, Prev);
4056          Generate_Reference (Prev, Def_Id, 'c');
4057          Set_Completion_Referenced (Def_Id);
4058
4059       --  If not private type or incomplete type completion, this is a real
4060       --  definition of a new entity, so record it.
4061
4062       else
4063          Generate_Definition (Def_Id);
4064       end if;
4065
4066       if Chars (Scope (Def_Id)) = Name_System
4067         and then Chars (Def_Id) = Name_Address
4068         and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
4069       then
4070          Set_Is_Descendent_Of_Address (Def_Id);
4071          Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
4072          Set_Is_Descendent_Of_Address (Prev);
4073       end if;
4074
4075       Set_Optimize_Alignment_Flags (Def_Id);
4076       Check_Eliminated (Def_Id);
4077    end Analyze_Type_Declaration;
4078
4079    --------------------------
4080    -- Analyze_Variant_Part --
4081    --------------------------
4082
4083    procedure Analyze_Variant_Part (N : Node_Id) is
4084
4085       procedure Non_Static_Choice_Error (Choice : Node_Id);
4086       --  Error routine invoked by the generic instantiation below when the
4087       --  variant part has a non static choice.
4088
4089       procedure Process_Declarations (Variant : Node_Id);
4090       --  Analyzes all the declarations associated with a Variant. Needed by
4091       --  the generic instantiation below.
4092
4093       package Variant_Choices_Processing is new
4094         Generic_Choices_Processing
4095           (Get_Alternatives          => Variants,
4096            Get_Choices               => Discrete_Choices,
4097            Process_Empty_Choice      => No_OP,
4098            Process_Non_Static_Choice => Non_Static_Choice_Error,
4099            Process_Associated_Node   => Process_Declarations);
4100       use Variant_Choices_Processing;
4101       --  Instantiation of the generic choice processing package
4102
4103       -----------------------------
4104       -- Non_Static_Choice_Error --
4105       -----------------------------
4106
4107       procedure Non_Static_Choice_Error (Choice : Node_Id) is
4108       begin
4109          Flag_Non_Static_Expr
4110            ("choice given in variant part is not static!", Choice);
4111       end Non_Static_Choice_Error;
4112
4113       --------------------------
4114       -- Process_Declarations --
4115       --------------------------
4116
4117       procedure Process_Declarations (Variant : Node_Id) is
4118       begin
4119          if not Null_Present (Component_List (Variant)) then
4120             Analyze_Declarations (Component_Items (Component_List (Variant)));
4121
4122             if Present (Variant_Part (Component_List (Variant))) then
4123                Analyze (Variant_Part (Component_List (Variant)));
4124             end if;
4125          end if;
4126       end Process_Declarations;
4127
4128       --  Local Variables
4129
4130       Discr_Name : Node_Id;
4131       Discr_Type : Entity_Id;
4132
4133       Case_Table     : Choice_Table_Type (1 .. Number_Of_Choices (N));
4134       Last_Choice    : Nat;
4135       Dont_Care      : Boolean;
4136       Others_Present : Boolean := False;
4137
4138       pragma Warnings (Off, Case_Table);
4139       pragma Warnings (Off, Last_Choice);
4140       pragma Warnings (Off, Dont_Care);
4141       pragma Warnings (Off, Others_Present);
4142       --  We don't care about the assigned values of any of these
4143
4144    --  Start of processing for Analyze_Variant_Part
4145
4146    begin
4147       Discr_Name := Name (N);
4148       Analyze (Discr_Name);
4149
4150       --  If Discr_Name bad, get out (prevent cascaded errors)
4151
4152       if Etype (Discr_Name) = Any_Type then
4153          return;
4154       end if;
4155
4156       --  Check invalid discriminant in variant part
4157
4158       if Ekind (Entity (Discr_Name)) /= E_Discriminant then
4159          Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
4160       end if;
4161
4162       Discr_Type := Etype (Entity (Discr_Name));
4163
4164       if not Is_Discrete_Type (Discr_Type) then
4165          Error_Msg_N
4166            ("discriminant in a variant part must be of a discrete type",
4167              Name (N));
4168          return;
4169       end if;
4170
4171       --  Call the instantiated Analyze_Choices which does the rest of the work
4172
4173       Analyze_Choices
4174         (N, Discr_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
4175    end Analyze_Variant_Part;
4176
4177    ----------------------------
4178    -- Array_Type_Declaration --
4179    ----------------------------
4180
4181    procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
4182       Component_Def : constant Node_Id := Component_Definition (Def);
4183       Element_Type  : Entity_Id;
4184       Implicit_Base : Entity_Id;
4185       Index         : Node_Id;
4186       Related_Id    : Entity_Id := Empty;
4187       Nb_Index      : Nat;
4188       P             : constant Node_Id := Parent (Def);
4189       Priv          : Entity_Id;
4190
4191    begin
4192       if Nkind (Def) = N_Constrained_Array_Definition then
4193          Index := First (Discrete_Subtype_Definitions (Def));
4194       else
4195          Index := First (Subtype_Marks (Def));
4196       end if;
4197
4198       --  Find proper names for the implicit types which may be public. In case
4199       --  of anonymous arrays we use the name of the first object of that type
4200       --  as prefix.
4201
4202       if No (T) then
4203          Related_Id :=  Defining_Identifier (P);
4204       else
4205          Related_Id := T;
4206       end if;
4207
4208       Nb_Index := 1;
4209       while Present (Index) loop
4210          Analyze (Index);
4211
4212          --  Add a subtype declaration for each index of private array type
4213          --  declaration whose etype is also private. For example:
4214
4215          --     package Pkg is
4216          --        type Index is private;
4217          --     private
4218          --        type Table is array (Index) of ...
4219          --     end;
4220
4221          --  This is currently required by the expander for the internally
4222          --  generated equality subprogram of records with variant parts in
4223          --  which the etype of some component is such private type.
4224
4225          if Ekind (Current_Scope) = E_Package
4226            and then In_Private_Part (Current_Scope)
4227            and then Has_Private_Declaration (Etype (Index))
4228          then
4229             declare
4230                Loc   : constant Source_Ptr := Sloc (Def);
4231                New_E : Entity_Id;
4232                Decl  : Entity_Id;
4233
4234             begin
4235                New_E :=
4236                  Make_Defining_Identifier (Loc,
4237                    Chars => New_Internal_Name ('T'));
4238                Set_Is_Internal (New_E);
4239
4240                Decl :=
4241                  Make_Subtype_Declaration (Loc,
4242                    Defining_Identifier => New_E,
4243                    Subtype_Indication  =>
4244                      New_Occurrence_Of (Etype (Index), Loc));
4245
4246                Insert_Before (Parent (Def), Decl);
4247                Analyze (Decl);
4248                Set_Etype (Index, New_E);
4249
4250                --  If the index is a range the Entity attribute is not
4251                --  available. Example:
4252
4253                --     package Pkg is
4254                --        type T is private;
4255                --     private
4256                --        type T is new Natural;
4257                --        Table : array (T(1) .. T(10)) of Boolean;
4258                --     end Pkg;
4259
4260                if Nkind (Index) /= N_Range then
4261                   Set_Entity (Index, New_E);
4262                end if;
4263             end;
4264          end if;
4265
4266          Make_Index (Index, P, Related_Id, Nb_Index);
4267          Next_Index (Index);
4268          Nb_Index := Nb_Index + 1;
4269       end loop;
4270
4271       --  Process subtype indication if one is present
4272
4273       if Present (Subtype_Indication (Component_Def)) then
4274          Element_Type :=
4275            Process_Subtype
4276              (Subtype_Indication (Component_Def), P, Related_Id, 'C');
4277
4278       --  Ada 2005 (AI-230): Access Definition case
4279
4280       else pragma Assert (Present (Access_Definition (Component_Def)));
4281
4282          --  Indicate that the anonymous access type is created by the
4283          --  array type declaration.
4284
4285          Element_Type := Access_Definition
4286                            (Related_Nod => P,
4287                             N           => Access_Definition (Component_Def));
4288          Set_Is_Local_Anonymous_Access (Element_Type);
4289
4290          --  Propagate the parent. This field is needed if we have to generate
4291          --  the master_id associated with an anonymous access to task type
4292          --  component (see Expand_N_Full_Type_Declaration.Build_Master)
4293
4294          Set_Parent (Element_Type, Parent (T));
4295
4296          --  Ada 2005 (AI-230): In case of components that are anonymous access
4297          --  types the level of accessibility depends on the enclosing type
4298          --  declaration
4299
4300          Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
4301
4302          --  Ada 2005 (AI-254)
4303
4304          declare
4305             CD : constant Node_Id :=
4306                    Access_To_Subprogram_Definition
4307                      (Access_Definition (Component_Def));
4308          begin
4309             if Present (CD) and then Protected_Present (CD) then
4310                Element_Type :=
4311                  Replace_Anonymous_Access_To_Protected_Subprogram (Def);
4312             end if;
4313          end;
4314       end if;
4315
4316       --  Constrained array case
4317
4318       if No (T) then
4319          T := Create_Itype (E_Void, P, Related_Id, 'T');
4320       end if;
4321
4322       if Nkind (Def) = N_Constrained_Array_Definition then
4323
4324          --  Establish Implicit_Base as unconstrained base type
4325
4326          Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
4327
4328          Set_Etype              (Implicit_Base, Implicit_Base);
4329          Set_Scope              (Implicit_Base, Current_Scope);
4330          Set_Has_Delayed_Freeze (Implicit_Base);
4331
4332          --  The constrained array type is a subtype of the unconstrained one
4333
4334          Set_Ekind          (T, E_Array_Subtype);
4335          Init_Size_Align    (T);
4336          Set_Etype          (T, Implicit_Base);
4337          Set_Scope          (T, Current_Scope);
4338          Set_Is_Constrained (T, True);
4339          Set_First_Index    (T, First (Discrete_Subtype_Definitions (Def)));
4340          Set_Has_Delayed_Freeze (T);
4341
4342          --  Complete setup of implicit base type
4343
4344          Set_First_Index       (Implicit_Base, First_Index (T));
4345          Set_Component_Type    (Implicit_Base, Element_Type);
4346          Set_Has_Task          (Implicit_Base, Has_Task (Element_Type));
4347          Set_Component_Size    (Implicit_Base, Uint_0);
4348          Set_Packed_Array_Type (Implicit_Base, Empty);
4349          Set_Has_Controlled_Component
4350                                (Implicit_Base, Has_Controlled_Component
4351                                                         (Element_Type)
4352                                                  or else Is_Controlled
4353                                                         (Element_Type));
4354          Set_Finalize_Storage_Only
4355                                (Implicit_Base, Finalize_Storage_Only
4356                                                         (Element_Type));
4357
4358       --  Unconstrained array case
4359
4360       else
4361          Set_Ekind                    (T, E_Array_Type);
4362          Init_Size_Align              (T);
4363          Set_Etype                    (T, T);
4364          Set_Scope                    (T, Current_Scope);
4365          Set_Component_Size           (T, Uint_0);
4366          Set_Is_Constrained           (T, False);
4367          Set_First_Index              (T, First (Subtype_Marks (Def)));
4368          Set_Has_Delayed_Freeze       (T, True);
4369          Set_Has_Task                 (T, Has_Task      (Element_Type));
4370          Set_Has_Controlled_Component (T, Has_Controlled_Component
4371                                                         (Element_Type)
4372                                             or else
4373                                           Is_Controlled (Element_Type));
4374          Set_Finalize_Storage_Only    (T, Finalize_Storage_Only
4375                                                         (Element_Type));
4376       end if;
4377
4378       --  Common attributes for both cases
4379
4380       Set_Component_Type (Base_Type (T), Element_Type);
4381       Set_Packed_Array_Type (T, Empty);
4382
4383       if Aliased_Present (Component_Definition (Def)) then
4384          Set_Has_Aliased_Components (Etype (T));
4385       end if;
4386
4387       --  Ada 2005 (AI-231): Propagate the null-excluding attribute to the
4388       --  array type to ensure that objects of this type are initialized.
4389
4390       if Ada_Version >= Ada_05
4391         and then Can_Never_Be_Null (Element_Type)
4392       then
4393          Set_Can_Never_Be_Null (T);
4394
4395          if Null_Exclusion_Present (Component_Definition (Def))
4396
4397             --  No need to check itypes because in their case this check was
4398             --  done at their point of creation
4399
4400            and then not Is_Itype (Element_Type)
4401          then
4402             Error_Msg_N
4403               ("`NOT NULL` not allowed (null already excluded)",
4404                Subtype_Indication (Component_Definition (Def)));
4405          end if;
4406       end if;
4407
4408       Priv := Private_Component (Element_Type);
4409
4410       if Present (Priv) then
4411
4412          --  Check for circular definitions
4413
4414          if Priv = Any_Type then
4415             Set_Component_Type (Etype (T), Any_Type);
4416
4417          --  There is a gap in the visibility of operations on the composite
4418          --  type only if the component type is defined in a different scope.
4419
4420          elsif Scope (Priv) = Current_Scope then
4421             null;
4422
4423          elsif Is_Limited_Type (Priv) then
4424             Set_Is_Limited_Composite (Etype (T));
4425             Set_Is_Limited_Composite (T);
4426          else
4427             Set_Is_Private_Composite (Etype (T));
4428             Set_Is_Private_Composite (T);
4429          end if;
4430       end if;
4431
4432       --  A syntax error in the declaration itself may lead to an empty index
4433       --  list, in which case do a minimal patch.
4434
4435       if No (First_Index (T)) then
4436          Error_Msg_N ("missing index definition in array type declaration", T);
4437
4438          declare
4439             Indices : constant List_Id :=
4440                         New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
4441          begin
4442             Set_Discrete_Subtype_Definitions (Def, Indices);
4443             Set_First_Index (T, First (Indices));
4444             return;
4445          end;
4446       end if;
4447
4448       --  Create a concatenation operator for the new type. Internal array
4449       --  types created for packed entities do not need such, they are
4450       --  compatible with the user-defined type.
4451
4452       if Number_Dimensions (T) = 1
4453          and then not Is_Packed_Array_Type (T)
4454       then
4455          New_Concatenation_Op (T);
4456       end if;
4457
4458       --  In the case of an unconstrained array the parser has already verified
4459       --  that all the indices are unconstrained but we still need to make sure
4460       --  that the element type is constrained.
4461
4462       if Is_Indefinite_Subtype (Element_Type) then
4463          Error_Msg_N
4464            ("unconstrained element type in array declaration",
4465             Subtype_Indication (Component_Def));
4466
4467       elsif Is_Abstract_Type (Element_Type) then
4468          Error_Msg_N
4469            ("the type of a component cannot be abstract",
4470             Subtype_Indication (Component_Def));
4471       end if;
4472    end Array_Type_Declaration;
4473
4474    ------------------------------------------------------
4475    -- Replace_Anonymous_Access_To_Protected_Subprogram --
4476    ------------------------------------------------------
4477
4478    function Replace_Anonymous_Access_To_Protected_Subprogram
4479      (N : Node_Id) return Entity_Id
4480    is
4481       Loc : constant Source_Ptr := Sloc (N);
4482
4483       Curr_Scope : constant Scope_Stack_Entry :=
4484                      Scope_Stack.Table (Scope_Stack.Last);
4485
4486       Anon : constant Entity_Id :=
4487                Make_Defining_Identifier (Loc,
4488                  Chars => New_Internal_Name ('S'));
4489
4490       Acc  : Node_Id;
4491       Comp : Node_Id;
4492       Decl : Node_Id;
4493       P    : Node_Id;
4494
4495    begin
4496       Set_Is_Internal (Anon);
4497
4498       case Nkind (N) is
4499          when N_Component_Declaration       |
4500            N_Unconstrained_Array_Definition |
4501            N_Constrained_Array_Definition   =>
4502             Comp := Component_Definition (N);
4503             Acc  := Access_Definition (Comp);
4504
4505          when N_Discriminant_Specification =>
4506             Comp := Discriminant_Type (N);
4507             Acc  := Comp;
4508
4509          when N_Parameter_Specification =>
4510             Comp := Parameter_Type (N);
4511             Acc  := Comp;
4512
4513          when N_Access_Function_Definition  =>
4514             Comp := Result_Definition (N);
4515             Acc  := Comp;
4516
4517          when N_Object_Declaration  =>
4518             Comp := Object_Definition (N);
4519             Acc  := Comp;
4520
4521          when N_Function_Specification =>
4522             Comp := Result_Definition (N);
4523             Acc  := Comp;
4524
4525          when others =>
4526             raise Program_Error;
4527       end case;
4528
4529       Decl := Make_Full_Type_Declaration (Loc,
4530                 Defining_Identifier => Anon,
4531                 Type_Definition   =>
4532                   Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
4533
4534       Mark_Rewrite_Insertion (Decl);
4535
4536       --  Insert the new declaration in the nearest enclosing scope. If the
4537       --  node is a body and N is its return type, the declaration belongs in
4538       --  the enclosing scope.
4539
4540       P := Parent (N);
4541
4542       if Nkind (P) = N_Subprogram_Body
4543         and then Nkind (N) = N_Function_Specification
4544       then
4545          P := Parent (P);
4546       end if;
4547
4548       while Present (P) and then not Has_Declarations (P) loop
4549          P := Parent (P);
4550       end loop;
4551
4552       pragma Assert (Present (P));
4553
4554       if Nkind (P) = N_Package_Specification then
4555          Prepend (Decl, Visible_Declarations (P));
4556       else
4557          Prepend (Decl, Declarations (P));
4558       end if;
4559
4560       --  Replace the anonymous type with an occurrence of the new declaration.
4561       --  In all cases the rewritten node does not have the null-exclusion
4562       --  attribute because (if present) it was already inherited by the
4563       --  anonymous entity (Anon). Thus, in case of components we do not
4564       --  inherit this attribute.
4565
4566       if Nkind (N) = N_Parameter_Specification then
4567          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4568          Set_Etype (Defining_Identifier (N), Anon);
4569          Set_Null_Exclusion_Present (N, False);
4570
4571       elsif Nkind (N) = N_Object_Declaration then
4572          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4573          Set_Etype (Defining_Identifier (N), Anon);
4574
4575       elsif Nkind (N) = N_Access_Function_Definition then
4576          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4577
4578       elsif Nkind (N) = N_Function_Specification then
4579          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4580          Set_Etype (Defining_Unit_Name (N), Anon);
4581
4582       else
4583          Rewrite (Comp,
4584            Make_Component_Definition (Loc,
4585              Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
4586       end if;
4587
4588       Mark_Rewrite_Insertion (Comp);
4589
4590       if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
4591          Analyze (Decl);
4592
4593       else
4594          --  Temporarily remove the current scope (record or subprogram) from
4595          --  the stack to add the new declarations to the enclosing scope.
4596
4597          Scope_Stack.Decrement_Last;
4598          Analyze (Decl);
4599          Set_Is_Itype (Anon);
4600          Scope_Stack.Append (Curr_Scope);
4601       end if;
4602
4603       Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
4604       Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
4605       return Anon;
4606    end Replace_Anonymous_Access_To_Protected_Subprogram;
4607
4608    -------------------------------
4609    -- Build_Derived_Access_Type --
4610    -------------------------------
4611
4612    procedure Build_Derived_Access_Type
4613      (N            : Node_Id;
4614       Parent_Type  : Entity_Id;
4615       Derived_Type : Entity_Id)
4616    is
4617       S : constant Node_Id := Subtype_Indication (Type_Definition (N));
4618
4619       Desig_Type      : Entity_Id;
4620       Discr           : Entity_Id;
4621       Discr_Con_Elist : Elist_Id;
4622       Discr_Con_El    : Elmt_Id;
4623       Subt            : Entity_Id;
4624
4625    begin
4626       --  Set the designated type so it is available in case this is an access
4627       --  to a self-referential type, e.g. a standard list type with a next
4628       --  pointer. Will be reset after subtype is built.
4629
4630       Set_Directly_Designated_Type
4631         (Derived_Type, Designated_Type (Parent_Type));
4632
4633       Subt := Process_Subtype (S, N);
4634
4635       if Nkind (S) /= N_Subtype_Indication
4636         and then Subt /= Base_Type (Subt)
4637       then
4638          Set_Ekind (Derived_Type, E_Access_Subtype);
4639       end if;
4640
4641       if Ekind (Derived_Type) = E_Access_Subtype then
4642          declare
4643             Pbase      : constant Entity_Id := Base_Type (Parent_Type);
4644             Ibase      : constant Entity_Id :=
4645                            Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
4646             Svg_Chars  : constant Name_Id   := Chars (Ibase);
4647             Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
4648
4649          begin
4650             Copy_Node (Pbase, Ibase);
4651
4652             Set_Chars             (Ibase, Svg_Chars);
4653             Set_Next_Entity       (Ibase, Svg_Next_E);
4654             Set_Sloc              (Ibase, Sloc (Derived_Type));
4655             Set_Scope             (Ibase, Scope (Derived_Type));
4656             Set_Freeze_Node       (Ibase, Empty);
4657             Set_Is_Frozen         (Ibase, False);
4658             Set_Comes_From_Source (Ibase, False);
4659             Set_Is_First_Subtype  (Ibase, False);
4660
4661             Set_Etype (Ibase, Pbase);
4662             Set_Etype (Derived_Type, Ibase);
4663          end;
4664       end if;
4665
4666       Set_Directly_Designated_Type
4667         (Derived_Type, Designated_Type (Subt));
4668
4669       Set_Is_Constrained     (Derived_Type, Is_Constrained (Subt));
4670       Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
4671       Set_Size_Info          (Derived_Type,                     Parent_Type);
4672       Set_RM_Size            (Derived_Type, RM_Size            (Parent_Type));
4673       Set_Depends_On_Private (Derived_Type,
4674                               Has_Private_Component (Derived_Type));
4675       Conditional_Delay      (Derived_Type, Subt);
4676
4677       --  Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
4678       --  that it is not redundant.
4679
4680       if Null_Exclusion_Present (Type_Definition (N)) then
4681          Set_Can_Never_Be_Null (Derived_Type);
4682
4683          if Can_Never_Be_Null (Parent_Type)
4684            and then False
4685          then
4686             Error_Msg_NE
4687               ("`NOT NULL` not allowed (& already excludes null)",
4688                 N, Parent_Type);
4689          end if;
4690
4691       elsif Can_Never_Be_Null (Parent_Type) then
4692          Set_Can_Never_Be_Null (Derived_Type);
4693       end if;
4694
4695       --  Note: we do not copy the Storage_Size_Variable, since we always go to
4696       --  the root type for this information.
4697
4698       --  Apply range checks to discriminants for derived record case
4699       --  ??? THIS CODE SHOULD NOT BE HERE REALLY.
4700
4701       Desig_Type := Designated_Type (Derived_Type);
4702       if Is_Composite_Type (Desig_Type)
4703         and then (not Is_Array_Type (Desig_Type))
4704         and then Has_Discriminants (Desig_Type)
4705         and then Base_Type (Desig_Type) /= Desig_Type
4706       then
4707          Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
4708          Discr_Con_El := First_Elmt (Discr_Con_Elist);
4709
4710          Discr := First_Discriminant (Base_Type (Desig_Type));
4711          while Present (Discr_Con_El) loop
4712             Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
4713             Next_Elmt (Discr_Con_El);
4714             Next_Discriminant (Discr);
4715          end loop;
4716       end if;
4717    end Build_Derived_Access_Type;
4718
4719    ------------------------------
4720    -- Build_Derived_Array_Type --
4721    ------------------------------
4722
4723    procedure Build_Derived_Array_Type
4724      (N            : Node_Id;
4725       Parent_Type  : Entity_Id;
4726       Derived_Type : Entity_Id)
4727    is
4728       Loc           : constant Source_Ptr := Sloc (N);
4729       Tdef          : constant Node_Id    := Type_Definition (N);
4730       Indic         : constant Node_Id    := Subtype_Indication (Tdef);
4731       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
4732       Implicit_Base : Entity_Id;
4733       New_Indic     : Node_Id;
4734
4735       procedure Make_Implicit_Base;
4736       --  If the parent subtype is constrained, the derived type is a subtype
4737       --  of an implicit base type derived from the parent base.
4738
4739       ------------------------
4740       -- Make_Implicit_Base --
4741       ------------------------
4742
4743       procedure Make_Implicit_Base is
4744       begin
4745          Implicit_Base :=
4746            Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
4747
4748          Set_Ekind (Implicit_Base, Ekind (Parent_Base));
4749          Set_Etype (Implicit_Base, Parent_Base);
4750
4751          Copy_Array_Subtype_Attributes   (Implicit_Base, Parent_Base);
4752          Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
4753
4754          Set_Has_Delayed_Freeze (Implicit_Base, True);
4755       end Make_Implicit_Base;
4756
4757    --  Start of processing for Build_Derived_Array_Type
4758
4759    begin
4760       if not Is_Constrained (Parent_Type) then
4761          if Nkind (Indic) /= N_Subtype_Indication then
4762             Set_Ekind (Derived_Type, E_Array_Type);
4763
4764             Copy_Array_Subtype_Attributes   (Derived_Type, Parent_Type);
4765             Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
4766
4767             Set_Has_Delayed_Freeze (Derived_Type, True);
4768
4769          else
4770             Make_Implicit_Base;
4771             Set_Etype (Derived_Type, Implicit_Base);
4772
4773             New_Indic :=
4774               Make_Subtype_Declaration (Loc,
4775                 Defining_Identifier => Derived_Type,
4776                 Subtype_Indication  =>
4777                   Make_Subtype_Indication (Loc,
4778                     Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
4779                     Constraint => Constraint (Indic)));
4780
4781             Rewrite (N, New_Indic);
4782             Analyze (N);
4783          end if;
4784
4785       else
4786          if Nkind (Indic) /= N_Subtype_Indication then
4787             Make_Implicit_Base;
4788
4789             Set_Ekind             (Derived_Type, Ekind (Parent_Type));
4790             Set_Etype             (Derived_Type, Implicit_Base);
4791             Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
4792
4793          else
4794             Error_Msg_N ("illegal constraint on constrained type", Indic);
4795          end if;
4796       end if;
4797
4798       --  If parent type is not a derived type itself, and is declared in
4799       --  closed scope (e.g. a subprogram), then we must explicitly introduce
4800       --  the new type's concatenation operator since Derive_Subprograms
4801       --  will not inherit the parent's operator. If the parent type is
4802       --  unconstrained, the operator is of the unconstrained base type.
4803
4804       if Number_Dimensions (Parent_Type) = 1
4805         and then not Is_Limited_Type (Parent_Type)
4806         and then not Is_Derived_Type (Parent_Type)
4807         and then not Is_Package_Or_Generic_Package
4808                        (Scope (Base_Type (Parent_Type)))
4809       then
4810          if not Is_Constrained (Parent_Type)
4811            and then Is_Constrained (Derived_Type)
4812          then
4813             New_Concatenation_Op (Implicit_Base);
4814          else
4815             New_Concatenation_Op (Derived_Type);
4816          end if;
4817       end if;
4818    end Build_Derived_Array_Type;
4819
4820    -----------------------------------
4821    -- Build_Derived_Concurrent_Type --
4822    -----------------------------------
4823
4824    procedure Build_Derived_Concurrent_Type
4825      (N            : Node_Id;
4826       Parent_Type  : Entity_Id;
4827       Derived_Type : Entity_Id)
4828    is
4829       D_Constraint : Node_Id;
4830       Disc_Spec    : Node_Id;
4831       Old_Disc     : Entity_Id;
4832       New_Disc     : Entity_Id;
4833
4834       Constraint_Present : constant Boolean :=
4835                              Nkind (Subtype_Indication (Type_Definition (N)))
4836                                                      = N_Subtype_Indication;
4837
4838    begin
4839       Set_Stored_Constraint (Derived_Type, No_Elist);
4840
4841       --  Copy Storage_Size and Relative_Deadline variables if task case
4842
4843       if Is_Task_Type (Parent_Type) then
4844          Set_Storage_Size_Variable (Derived_Type,
4845            Storage_Size_Variable (Parent_Type));
4846          Set_Relative_Deadline_Variable (Derived_Type,
4847            Relative_Deadline_Variable (Parent_Type));
4848       end if;
4849
4850       if Present (Discriminant_Specifications (N)) then
4851          Push_Scope (Derived_Type);
4852          Check_Or_Process_Discriminants (N, Derived_Type);
4853          End_Scope;
4854
4855       elsif Constraint_Present then
4856
4857          --  Build constrained subtype and derive from it
4858
4859          declare
4860             Loc  : constant Source_Ptr := Sloc (N);
4861             Anon : constant Entity_Id :=
4862                      Make_Defining_Identifier (Loc,
4863                        New_External_Name (Chars (Derived_Type), 'T'));
4864             Decl : Node_Id;
4865
4866          begin
4867             Decl :=
4868               Make_Subtype_Declaration (Loc,
4869                 Defining_Identifier => Anon,
4870                 Subtype_Indication =>
4871                   Subtype_Indication (Type_Definition (N)));
4872             Insert_Before (N, Decl);
4873             Analyze (Decl);
4874
4875             Rewrite (Subtype_Indication (Type_Definition (N)),
4876               New_Occurrence_Of (Anon, Loc));
4877             Set_Analyzed (Derived_Type, False);
4878             Analyze (N);
4879             return;
4880          end;
4881       end if;
4882
4883       --  All attributes are inherited from parent. In particular,
4884       --  entries and the corresponding record type are the same.
4885       --  Discriminants may be renamed, and must be treated separately.
4886
4887       Set_Has_Discriminants
4888         (Derived_Type, Has_Discriminants         (Parent_Type));
4889       Set_Corresponding_Record_Type
4890         (Derived_Type, Corresponding_Record_Type (Parent_Type));
4891
4892       --  Is_Constrained is set according the parent subtype, but is set to
4893       --  False if the derived type is declared with new discriminants.
4894
4895       Set_Is_Constrained
4896         (Derived_Type,
4897          (Is_Constrained (Parent_Type) or else Constraint_Present)
4898            and then not Present (Discriminant_Specifications (N)));
4899
4900       if Constraint_Present then
4901          if not Has_Discriminants (Parent_Type) then
4902             Error_Msg_N ("untagged parent must have discriminants", N);
4903
4904          elsif Present (Discriminant_Specifications (N)) then
4905
4906             --  Verify that new discriminants are used to constrain old ones
4907
4908             D_Constraint :=
4909               First
4910                 (Constraints
4911                   (Constraint (Subtype_Indication (Type_Definition (N)))));
4912
4913             Old_Disc  := First_Discriminant (Parent_Type);
4914             New_Disc  := First_Discriminant (Derived_Type);
4915             Disc_Spec := First (Discriminant_Specifications (N));
4916             while Present (Old_Disc) and then Present (Disc_Spec) loop
4917                if Nkind (Discriminant_Type (Disc_Spec)) /=
4918                                               N_Access_Definition
4919                then
4920                   Analyze (Discriminant_Type (Disc_Spec));
4921
4922                   if not Subtypes_Statically_Compatible (
4923                              Etype (Discriminant_Type (Disc_Spec)),
4924                                Etype (Old_Disc))
4925                   then
4926                      Error_Msg_N
4927                        ("not statically compatible with parent discriminant",
4928                         Discriminant_Type (Disc_Spec));
4929                   end if;
4930                end if;
4931
4932                if Nkind (D_Constraint) = N_Identifier
4933                  and then Chars (D_Constraint) /=
4934                           Chars (Defining_Identifier (Disc_Spec))
4935                then
4936                   Error_Msg_N ("new discriminants must constrain old ones",
4937                     D_Constraint);
4938                else
4939                   Set_Corresponding_Discriminant (New_Disc, Old_Disc);
4940                end if;
4941
4942                Next_Discriminant (Old_Disc);
4943                Next_Discriminant (New_Disc);
4944                Next (Disc_Spec);
4945             end loop;
4946
4947             if Present (Old_Disc) or else Present (Disc_Spec) then
4948                Error_Msg_N ("discriminant mismatch in derivation", N);
4949             end if;
4950
4951          end if;
4952
4953       elsif Present (Discriminant_Specifications (N)) then
4954          Error_Msg_N
4955            ("missing discriminant constraint in untagged derivation",
4956             N);
4957       end if;
4958
4959       if Present (Discriminant_Specifications (N)) then
4960          Old_Disc := First_Discriminant (Parent_Type);
4961          while Present (Old_Disc) loop
4962
4963             if No (Next_Entity (Old_Disc))
4964               or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
4965             then
4966                Set_Next_Entity (Last_Entity (Derived_Type),
4967                                          Next_Entity (Old_Disc));
4968                exit;
4969             end if;
4970
4971             Next_Discriminant (Old_Disc);
4972          end loop;
4973
4974       else
4975          Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
4976          if Has_Discriminants (Parent_Type) then
4977             Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
4978             Set_Discriminant_Constraint (
4979               Derived_Type, Discriminant_Constraint (Parent_Type));
4980          end if;
4981       end if;
4982
4983       Set_Last_Entity  (Derived_Type, Last_Entity  (Parent_Type));
4984
4985       Set_Has_Completion (Derived_Type);
4986    end Build_Derived_Concurrent_Type;
4987
4988    ------------------------------------
4989    -- Build_Derived_Enumeration_Type --
4990    ------------------------------------
4991
4992    procedure Build_Derived_Enumeration_Type
4993      (N            : Node_Id;
4994       Parent_Type  : Entity_Id;
4995       Derived_Type : Entity_Id)
4996    is
4997       Loc           : constant Source_Ptr := Sloc (N);
4998       Def           : constant Node_Id    := Type_Definition (N);
4999       Indic         : constant Node_Id    := Subtype_Indication (Def);
5000       Implicit_Base : Entity_Id;
5001       Literal       : Entity_Id;
5002       New_Lit       : Entity_Id;
5003       Literals_List : List_Id;
5004       Type_Decl     : Node_Id;
5005       Hi, Lo        : Node_Id;
5006       Rang_Expr     : Node_Id;
5007
5008    begin
5009       --  Since types Standard.Character and Standard.[Wide_]Wide_Character do
5010       --  not have explicit literals lists we need to process types derived
5011       --  from them specially. This is handled by Derived_Standard_Character.
5012       --  If the parent type is a generic type, there are no literals either,
5013       --  and we construct the same skeletal representation as for the generic
5014       --  parent type.
5015
5016       if Is_Standard_Character_Type (Parent_Type) then
5017          Derived_Standard_Character (N, Parent_Type, Derived_Type);
5018
5019       elsif Is_Generic_Type (Root_Type (Parent_Type)) then
5020          declare
5021             Lo : Node_Id;
5022             Hi : Node_Id;
5023
5024          begin
5025             if Nkind (Indic) /= N_Subtype_Indication then
5026                Lo :=
5027                   Make_Attribute_Reference (Loc,
5028                     Attribute_Name => Name_First,
5029                     Prefix         => New_Reference_To (Derived_Type, Loc));
5030                Set_Etype (Lo, Derived_Type);
5031
5032                Hi :=
5033                   Make_Attribute_Reference (Loc,
5034                     Attribute_Name => Name_Last,
5035                     Prefix         => New_Reference_To (Derived_Type, Loc));
5036                Set_Etype (Hi, Derived_Type);
5037
5038                Set_Scalar_Range (Derived_Type,
5039                   Make_Range (Loc,
5040                     Low_Bound  => Lo,
5041                     High_Bound => Hi));
5042             else
5043
5044                --   Analyze subtype indication and verify compatibility
5045                --   with parent type.
5046
5047                if Base_Type (Process_Subtype (Indic, N)) /=
5048                   Base_Type (Parent_Type)
5049                then
5050                   Error_Msg_N
5051                     ("illegal constraint for formal discrete type", N);
5052                end if;
5053             end if;
5054          end;
5055
5056       else
5057          --  If a constraint is present, analyze the bounds to catch
5058          --  premature usage of the derived literals.
5059
5060          if Nkind (Indic) = N_Subtype_Indication
5061            and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
5062          then
5063             Analyze (Low_Bound  (Range_Expression (Constraint (Indic))));
5064             Analyze (High_Bound (Range_Expression (Constraint (Indic))));
5065          end if;
5066
5067          --  Introduce an implicit base type for the derived type even if there
5068          --  is no constraint attached to it, since this seems closer to the
5069          --  Ada semantics. Build a full type declaration tree for the derived
5070          --  type using the implicit base type as the defining identifier. The
5071          --  build a subtype declaration tree which applies the constraint (if
5072          --  any) have it replace the derived type declaration.
5073
5074          Literal := First_Literal (Parent_Type);
5075          Literals_List := New_List;
5076          while Present (Literal)
5077            and then Ekind (Literal) = E_Enumeration_Literal
5078          loop
5079             --  Literals of the derived type have the same representation as
5080             --  those of the parent type, but this representation can be
5081             --  overridden by an explicit representation clause. Indicate
5082             --  that there is no explicit representation given yet. These
5083             --  derived literals are implicit operations of the new type,
5084             --  and can be overridden by explicit ones.
5085
5086             if Nkind (Literal) = N_Defining_Character_Literal then
5087                New_Lit :=
5088                  Make_Defining_Character_Literal (Loc, Chars (Literal));
5089             else
5090                New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
5091             end if;
5092
5093             Set_Ekind                (New_Lit, E_Enumeration_Literal);
5094             Set_Enumeration_Pos      (New_Lit, Enumeration_Pos (Literal));
5095             Set_Enumeration_Rep      (New_Lit, Enumeration_Rep (Literal));
5096             Set_Enumeration_Rep_Expr (New_Lit, Empty);
5097             Set_Alias                (New_Lit, Literal);
5098             Set_Is_Known_Valid       (New_Lit, True);
5099
5100             Append (New_Lit, Literals_List);
5101             Next_Literal (Literal);
5102          end loop;
5103
5104          Implicit_Base :=
5105            Make_Defining_Identifier (Sloc (Derived_Type),
5106              New_External_Name (Chars (Derived_Type), 'B'));
5107
5108          --  Indicate the proper nature of the derived type. This must be done
5109          --  before analysis of the literals, to recognize cases when a literal
5110          --  may be hidden by a previous explicit function definition (cf.
5111          --  c83031a).
5112
5113          Set_Ekind (Derived_Type, E_Enumeration_Subtype);
5114          Set_Etype (Derived_Type, Implicit_Base);
5115
5116          Type_Decl :=
5117            Make_Full_Type_Declaration (Loc,
5118              Defining_Identifier => Implicit_Base,
5119              Discriminant_Specifications => No_List,
5120              Type_Definition =>
5121                Make_Enumeration_Type_Definition (Loc, Literals_List));
5122
5123          Mark_Rewrite_Insertion (Type_Decl);
5124          Insert_Before (N, Type_Decl);
5125          Analyze (Type_Decl);
5126
5127          --  After the implicit base is analyzed its Etype needs to be changed
5128          --  to reflect the fact that it is derived from the parent type which
5129          --  was ignored during analysis. We also set the size at this point.
5130
5131          Set_Etype (Implicit_Base, Parent_Type);
5132
5133          Set_Size_Info      (Implicit_Base,                 Parent_Type);
5134          Set_RM_Size        (Implicit_Base, RM_Size        (Parent_Type));
5135          Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
5136
5137          Set_Has_Non_Standard_Rep
5138                             (Implicit_Base, Has_Non_Standard_Rep
5139                                                            (Parent_Type));
5140          Set_Has_Delayed_Freeze (Implicit_Base);
5141
5142          --  Process the subtype indication including a validation check on the
5143          --  constraint, if any. If a constraint is given, its bounds must be
5144          --  implicitly converted to the new type.
5145
5146          if Nkind (Indic) = N_Subtype_Indication then
5147             declare
5148                R : constant Node_Id :=
5149                      Range_Expression (Constraint (Indic));
5150
5151             begin
5152                if Nkind (R) = N_Range then
5153                   Hi := Build_Scalar_Bound
5154                           (High_Bound (R), Parent_Type, Implicit_Base);
5155                   Lo := Build_Scalar_Bound
5156                           (Low_Bound  (R), Parent_Type, Implicit_Base);
5157
5158                else
5159                   --  Constraint is a Range attribute. Replace with explicit
5160                   --  mention of the bounds of the prefix, which must be a
5161                   --  subtype.
5162
5163                   Analyze (Prefix (R));
5164                   Hi :=
5165                     Convert_To (Implicit_Base,
5166                       Make_Attribute_Reference (Loc,
5167                         Attribute_Name => Name_Last,
5168                         Prefix =>
5169                           New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5170
5171                   Lo :=
5172                     Convert_To (Implicit_Base,
5173                       Make_Attribute_Reference (Loc,
5174                         Attribute_Name => Name_First,
5175                         Prefix =>
5176                           New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5177                end if;
5178             end;
5179
5180          else
5181             Hi :=
5182               Build_Scalar_Bound
5183                 (Type_High_Bound (Parent_Type),
5184                  Parent_Type, Implicit_Base);
5185             Lo :=
5186                Build_Scalar_Bound
5187                  (Type_Low_Bound (Parent_Type),
5188                   Parent_Type, Implicit_Base);
5189          end if;
5190
5191          Rang_Expr :=
5192            Make_Range (Loc,
5193              Low_Bound  => Lo,
5194              High_Bound => Hi);
5195
5196          --  If we constructed a default range for the case where no range
5197          --  was given, then the expressions in the range must not freeze
5198          --  since they do not correspond to expressions in the source.
5199
5200          if Nkind (Indic) /= N_Subtype_Indication then
5201             Set_Must_Not_Freeze (Lo);
5202             Set_Must_Not_Freeze (Hi);
5203             Set_Must_Not_Freeze (Rang_Expr);
5204          end if;
5205
5206          Rewrite (N,
5207            Make_Subtype_Declaration (Loc,
5208              Defining_Identifier => Derived_Type,
5209              Subtype_Indication =>
5210                Make_Subtype_Indication (Loc,
5211                  Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
5212                  Constraint =>
5213                    Make_Range_Constraint (Loc,
5214                      Range_Expression => Rang_Expr))));
5215
5216          Analyze (N);
5217
5218          --  If pragma Discard_Names applies on the first subtype of the parent
5219          --  type, then it must be applied on this subtype as well.
5220
5221          if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
5222             Set_Discard_Names (Derived_Type);
5223          end if;
5224
5225          --  Apply a range check. Since this range expression doesn't have an
5226          --  Etype, we have to specifically pass the Source_Typ parameter. Is
5227          --  this right???
5228
5229          if Nkind (Indic) = N_Subtype_Indication then
5230             Apply_Range_Check (Range_Expression (Constraint (Indic)),
5231                                Parent_Type,
5232                                Source_Typ => Entity (Subtype_Mark (Indic)));
5233          end if;
5234       end if;
5235    end Build_Derived_Enumeration_Type;
5236
5237    --------------------------------
5238    -- Build_Derived_Numeric_Type --
5239    --------------------------------
5240
5241    procedure Build_Derived_Numeric_Type
5242      (N            : Node_Id;
5243       Parent_Type  : Entity_Id;
5244       Derived_Type : Entity_Id)
5245    is
5246       Loc           : constant Source_Ptr := Sloc (N);
5247       Tdef          : constant Node_Id    := Type_Definition (N);
5248       Indic         : constant Node_Id    := Subtype_Indication (Tdef);
5249       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
5250       No_Constraint : constant Boolean    := Nkind (Indic) /=
5251                                                   N_Subtype_Indication;
5252       Implicit_Base : Entity_Id;
5253
5254       Lo : Node_Id;
5255       Hi : Node_Id;
5256
5257    begin
5258       --  Process the subtype indication including a validation check on
5259       --  the constraint if any.
5260
5261       Discard_Node (Process_Subtype (Indic, N));
5262
5263       --  Introduce an implicit base type for the derived type even if there
5264       --  is no constraint attached to it, since this seems closer to the Ada
5265       --  semantics.
5266
5267       Implicit_Base :=
5268         Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5269
5270       Set_Etype          (Implicit_Base, Parent_Base);
5271       Set_Ekind          (Implicit_Base, Ekind          (Parent_Base));
5272       Set_Size_Info      (Implicit_Base,                 Parent_Base);
5273       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
5274       Set_Parent         (Implicit_Base, Parent (Derived_Type));
5275       Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
5276
5277       --  Set RM Size for discrete type or decimal fixed-point type
5278       --  Ordinary fixed-point is excluded, why???
5279
5280       if Is_Discrete_Type (Parent_Base)
5281         or else Is_Decimal_Fixed_Point_Type (Parent_Base)
5282       then
5283          Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
5284       end if;
5285
5286       Set_Has_Delayed_Freeze (Implicit_Base);
5287
5288       Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Base));
5289       Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
5290
5291       Set_Scalar_Range (Implicit_Base,
5292         Make_Range (Loc,
5293           Low_Bound  => Lo,
5294           High_Bound => Hi));
5295
5296       if Has_Infinities (Parent_Base) then
5297          Set_Includes_Infinities (Scalar_Range (Implicit_Base));
5298       end if;
5299
5300       --  The Derived_Type, which is the entity of the declaration, is a
5301       --  subtype of the implicit base. Its Ekind is a subtype, even in the
5302       --  absence of an explicit constraint.
5303
5304       Set_Etype (Derived_Type, Implicit_Base);
5305
5306       --  If we did not have a constraint, then the Ekind is set from the
5307       --  parent type (otherwise Process_Subtype has set the bounds)
5308
5309       if No_Constraint then
5310          Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
5311       end if;
5312
5313       --  If we did not have a range constraint, then set the range from the
5314       --  parent type. Otherwise, the call to Process_Subtype has set the
5315       --  bounds.
5316
5317       if No_Constraint
5318         or else not Has_Range_Constraint (Indic)
5319       then
5320          Set_Scalar_Range (Derived_Type,
5321            Make_Range (Loc,
5322              Low_Bound  => New_Copy_Tree (Type_Low_Bound  (Parent_Type)),
5323              High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
5324          Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5325
5326          if Has_Infinities (Parent_Type) then
5327             Set_Includes_Infinities (Scalar_Range (Derived_Type));
5328          end if;
5329
5330          Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
5331       end if;
5332
5333       Set_Is_Descendent_Of_Address (Derived_Type,
5334         Is_Descendent_Of_Address (Parent_Type));
5335       Set_Is_Descendent_Of_Address (Implicit_Base,
5336         Is_Descendent_Of_Address (Parent_Type));
5337
5338       --  Set remaining type-specific fields, depending on numeric type
5339
5340       if Is_Modular_Integer_Type (Parent_Type) then
5341          Set_Modulus (Implicit_Base, Modulus (Parent_Base));
5342
5343          Set_Non_Binary_Modulus
5344            (Implicit_Base, Non_Binary_Modulus (Parent_Base));
5345
5346          Set_Is_Known_Valid
5347            (Implicit_Base, Is_Known_Valid (Parent_Base));
5348
5349       elsif Is_Floating_Point_Type (Parent_Type) then
5350
5351          --  Digits of base type is always copied from the digits value of
5352          --  the parent base type, but the digits of the derived type will
5353          --  already have been set if there was a constraint present.
5354
5355          Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5356          Set_Vax_Float    (Implicit_Base, Vax_Float    (Parent_Base));
5357
5358          if No_Constraint then
5359             Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
5360          end if;
5361
5362       elsif Is_Fixed_Point_Type (Parent_Type) then
5363
5364          --  Small of base type and derived type are always copied from the
5365          --  parent base type, since smalls never change. The delta of the
5366          --  base type is also copied from the parent base type. However the
5367          --  delta of the derived type will have been set already if a
5368          --  constraint was present.
5369
5370          Set_Small_Value (Derived_Type,  Small_Value (Parent_Base));
5371          Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
5372          Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
5373
5374          if No_Constraint then
5375             Set_Delta_Value (Derived_Type,  Delta_Value (Parent_Type));
5376          end if;
5377
5378          --  The scale and machine radix in the decimal case are always
5379          --  copied from the parent base type.
5380
5381          if Is_Decimal_Fixed_Point_Type (Parent_Type) then
5382             Set_Scale_Value (Derived_Type,  Scale_Value (Parent_Base));
5383             Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
5384
5385             Set_Machine_Radix_10
5386               (Derived_Type,  Machine_Radix_10 (Parent_Base));
5387             Set_Machine_Radix_10
5388               (Implicit_Base, Machine_Radix_10 (Parent_Base));
5389
5390             Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5391
5392             if No_Constraint then
5393                Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
5394
5395             else
5396                --  the analysis of the subtype_indication sets the
5397                --  digits value of the derived type.
5398
5399                null;
5400             end if;
5401          end if;
5402       end if;
5403
5404       --  The type of the bounds is that of the parent type, and they
5405       --  must be converted to the derived type.
5406
5407       Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
5408
5409       --  The implicit_base should be frozen when the derived type is frozen,
5410       --  but note that it is used in the conversions of the bounds. For fixed
5411       --  types we delay the determination of the bounds until the proper
5412       --  freezing point. For other numeric types this is rejected by GCC, for
5413       --  reasons that are currently unclear (???), so we choose to freeze the
5414       --  implicit base now. In the case of integers and floating point types
5415       --  this is harmless because subsequent representation clauses cannot
5416       --  affect anything, but it is still baffling that we cannot use the
5417       --  same mechanism for all derived numeric types.
5418
5419       --  There is a further complication: actually *some* representation
5420       --  clauses can affect the implicit base type. Namely, attribute
5421       --  definition clauses for stream-oriented attributes need to set the
5422       --  corresponding TSS entries on the base type, and this normally cannot
5423       --  be done after the base type is frozen, so the circuitry in
5424       --  Sem_Ch13.New_Stream_Subprogram must account for this possibility and
5425       --  not use Set_TSS in this case.
5426
5427       if Is_Fixed_Point_Type (Parent_Type) then
5428          Conditional_Delay (Implicit_Base, Parent_Type);
5429       else
5430          Freeze_Before (N, Implicit_Base);
5431       end if;
5432    end Build_Derived_Numeric_Type;
5433
5434    --------------------------------
5435    -- Build_Derived_Private_Type --
5436    --------------------------------
5437
5438    procedure Build_Derived_Private_Type
5439      (N             : Node_Id;
5440       Parent_Type   : Entity_Id;
5441       Derived_Type  : Entity_Id;
5442       Is_Completion : Boolean;
5443       Derive_Subps  : Boolean := True)
5444    is
5445       Loc         : constant Source_Ptr := Sloc (N);
5446       Der_Base    : Entity_Id;
5447       Discr       : Entity_Id;
5448       Full_Decl   : Node_Id := Empty;
5449       Full_Der    : Entity_Id;
5450       Full_P      : Entity_Id;
5451       Last_Discr  : Entity_Id;
5452       Par_Scope   : constant Entity_Id := Scope (Base_Type (Parent_Type));
5453       Swapped     : Boolean := False;
5454
5455       procedure Copy_And_Build;
5456       --  Copy derived type declaration, replace parent with its full view,
5457       --  and analyze new declaration.
5458
5459       --------------------
5460       -- Copy_And_Build --
5461       --------------------
5462
5463       procedure Copy_And_Build is
5464          Full_N : Node_Id;
5465
5466       begin
5467          if Ekind (Parent_Type) in Record_Kind
5468            or else
5469              (Ekind (Parent_Type) in Enumeration_Kind
5470                and then not Is_Standard_Character_Type (Parent_Type)
5471                and then not Is_Generic_Type (Root_Type (Parent_Type)))
5472          then
5473             Full_N := New_Copy_Tree (N);
5474             Insert_After (N, Full_N);
5475             Build_Derived_Type (
5476               Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
5477
5478          else
5479             Build_Derived_Type (
5480               N, Parent_Type, Full_Der, True, Derive_Subps => False);
5481          end if;
5482       end Copy_And_Build;
5483
5484    --  Start of processing for Build_Derived_Private_Type
5485
5486    begin
5487       if Is_Tagged_Type (Parent_Type) then
5488          Full_P := Full_View (Parent_Type);
5489
5490          --  A type extension of a type with unknown discriminants is an
5491          --  indefinite type that the back-end cannot handle directly.
5492          --  We treat it as a private type, and build a completion that is
5493          --  derived from the full view of the parent, and hopefully has
5494          --  known discriminants.
5495
5496          --  If the full view of the parent type has an underlying record view,
5497          --  use it to generate the underlying record view of this derived type
5498          --  (required for chains of derivations with unknown discriminants).
5499
5500          --  Minor optimization: we avoid the generation of useless underlying
5501          --  record view entities if the private type declaration has unknown
5502          --  discriminants but its corresponding full view has no
5503          --  discriminants.
5504
5505          if Has_Unknown_Discriminants (Parent_Type)
5506            and then Present (Full_P)
5507            and then (Has_Discriminants (Full_P)
5508                       or else Present (Underlying_Record_View (Full_P)))
5509            and then not In_Open_Scopes (Par_Scope)
5510            and then Expander_Active
5511          then
5512             declare
5513                Full_Der : constant Entity_Id :=
5514                             Make_Defining_Identifier (Loc,
5515                               Chars => New_Internal_Name ('T'));
5516                New_Ext  : constant Node_Id :=
5517                             Copy_Separate_Tree
5518                               (Record_Extension_Part (Type_Definition (N)));
5519                Decl     : Node_Id;
5520
5521             begin
5522                Build_Derived_Record_Type
5523                  (N, Parent_Type, Derived_Type, Derive_Subps);
5524
5525                --  Build anonymous completion, as a derivation from the full
5526                --  view of the parent. This is not a completion in the usual
5527                --  sense, because the current type is not private.
5528
5529                Decl :=
5530                  Make_Full_Type_Declaration (Loc,
5531                    Defining_Identifier => Full_Der,
5532                    Type_Definition     =>
5533                      Make_Derived_Type_Definition (Loc,
5534                        Subtype_Indication =>
5535                          New_Copy_Tree
5536                            (Subtype_Indication (Type_Definition (N))),
5537                        Record_Extension_Part => New_Ext));
5538
5539                --  If the parent type has an underlying record view, use it
5540                --  here to build the new underlying record view.
5541
5542                if Present (Underlying_Record_View (Full_P)) then
5543                   pragma Assert
5544                     (Nkind (Subtype_Indication (Type_Definition (Decl)))
5545                        = N_Identifier);
5546                   Set_Entity (Subtype_Indication (Type_Definition (Decl)),
5547                     Underlying_Record_View (Full_P));
5548                end if;
5549
5550                Install_Private_Declarations (Par_Scope);
5551                Install_Visible_Declarations (Par_Scope);
5552                Insert_Before (N, Decl);
5553
5554                --  Mark entity as an underlying record view before analysis,
5555                --  to avoid generating the list of its primitive operations
5556                --  (which is not really required for this entity) and thus
5557                --  prevent spurious errors associated with missing overriding
5558                --  of abstract primitives (overridden only for Derived_Type).
5559
5560                Set_Ekind (Full_Der, E_Record_Type);
5561                Set_Is_Underlying_Record_View (Full_Der);
5562
5563                Analyze (Decl);
5564
5565                pragma Assert (Has_Discriminants (Full_Der)
5566                  and then not Has_Unknown_Discriminants (Full_Der));
5567
5568                Uninstall_Declarations (Par_Scope);
5569
5570                --  Freeze the underlying record view, to prevent generation of
5571                --  useless dispatching information, which is simply shared with
5572                --  the real derived type.
5573
5574                Set_Is_Frozen (Full_Der);
5575
5576                --  Set up links between real entity and underlying record view
5577
5578                Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
5579                Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
5580             end;
5581
5582          --  If discriminants are known, build derived record
5583
5584          else
5585             Build_Derived_Record_Type
5586               (N, Parent_Type, Derived_Type, Derive_Subps);
5587          end if;
5588
5589          return;
5590
5591       elsif Has_Discriminants (Parent_Type) then
5592          if Present (Full_View (Parent_Type)) then
5593             if not Is_Completion then
5594
5595                --  Copy declaration for subsequent analysis, to provide a
5596                --  completion for what is a private declaration. Indicate that
5597                --  the full type is internally generated.
5598
5599                Full_Decl := New_Copy_Tree (N);
5600                Full_Der  := New_Copy (Derived_Type);
5601                Set_Comes_From_Source (Full_Decl, False);
5602                Set_Comes_From_Source (Full_Der, False);
5603
5604                Insert_After (N, Full_Decl);
5605
5606             else
5607                --  If this is a completion, the full view being built is itself
5608                --  private. We build a subtype of the parent with the same
5609                --  constraints as this full view, to convey to the back end the
5610                --  constrained components and the size of this subtype. If the
5611                --  parent is constrained, its full view can serve as the
5612                --  underlying full view of the derived type.
5613
5614                if No (Discriminant_Specifications (N)) then
5615                   if Nkind (Subtype_Indication (Type_Definition (N))) =
5616                                                         N_Subtype_Indication
5617                   then
5618                      Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
5619
5620                   elsif Is_Constrained (Full_View (Parent_Type)) then
5621                      Set_Underlying_Full_View
5622                        (Derived_Type, Full_View (Parent_Type));
5623                   end if;
5624
5625                else
5626                   --  If there are new discriminants, the parent subtype is
5627                   --  constrained by them, but it is not clear how to build
5628                   --  the Underlying_Full_View in this case???
5629
5630                   null;
5631                end if;
5632             end if;
5633          end if;
5634
5635          --  Build partial view of derived type from partial view of parent
5636
5637          Build_Derived_Record_Type
5638            (N, Parent_Type, Derived_Type, Derive_Subps);
5639
5640          if Present (Full_View (Parent_Type)) and then not Is_Completion then
5641             if not In_Open_Scopes (Par_Scope)
5642               or else not In_Same_Source_Unit (N, Parent_Type)
5643             then
5644                --  Swap partial and full views temporarily
5645
5646                Install_Private_Declarations (Par_Scope);
5647                Install_Visible_Declarations (Par_Scope);
5648                Swapped := True;
5649             end if;
5650
5651             --  Build full view of derived type from full view of parent which
5652             --  is now installed. Subprograms have been derived on the partial
5653             --  view, the completion does not derive them anew.
5654
5655             if not Is_Tagged_Type (Parent_Type) then
5656
5657                --  If the parent is itself derived from another private type,
5658                --  installing the private declarations has not affected its
5659                --  privacy status, so use its own full view explicitly.
5660
5661                if Is_Private_Type (Parent_Type) then
5662                   Build_Derived_Record_Type
5663                     (Full_Decl, Full_View (Parent_Type), Full_Der, False);
5664                else
5665                   Build_Derived_Record_Type
5666                     (Full_Decl, Parent_Type, Full_Der, False);
5667                end if;
5668
5669             else
5670                --  If full view of parent is tagged, the completion inherits
5671                --  the proper primitive operations.
5672
5673                Set_Defining_Identifier (Full_Decl, Full_Der);
5674                Build_Derived_Record_Type
5675                  (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
5676                Set_Analyzed (Full_Decl);
5677             end if;
5678
5679             if Swapped then
5680                Uninstall_Declarations (Par_Scope);
5681
5682                if In_Open_Scopes (Par_Scope) then
5683                   Install_Visible_Declarations (Par_Scope);
5684                end if;
5685             end if;
5686
5687             Der_Base := Base_Type (Derived_Type);
5688             Set_Full_View (Derived_Type, Full_Der);
5689             Set_Full_View (Der_Base, Base_Type (Full_Der));
5690
5691             --  Copy the discriminant list from full view to the partial views
5692             --  (base type and its subtype). Gigi requires that the partial and
5693             --  full views have the same discriminants.
5694
5695             --  Note that since the partial view is pointing to discriminants
5696             --  in the full view, their scope will be that of the full view.
5697             --  This might cause some front end problems and need adjustment???
5698
5699             Discr := First_Discriminant (Base_Type (Full_Der));
5700             Set_First_Entity (Der_Base, Discr);
5701
5702             loop
5703                Last_Discr := Discr;
5704                Next_Discriminant (Discr);
5705                exit when No (Discr);
5706             end loop;
5707
5708             Set_Last_Entity (Der_Base, Last_Discr);
5709
5710             Set_First_Entity (Derived_Type, First_Entity (Der_Base));
5711             Set_Last_Entity  (Derived_Type, Last_Entity  (Der_Base));
5712             Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
5713
5714          else
5715             --  If this is a completion, the derived type stays private and
5716             --  there is no need to create a further full view, except in the
5717             --  unusual case when the derivation is nested within a child unit,
5718             --  see below.
5719
5720             null;
5721          end if;
5722
5723       elsif Present (Full_View (Parent_Type))
5724         and then  Has_Discriminants (Full_View (Parent_Type))
5725       then
5726          if Has_Unknown_Discriminants (Parent_Type)
5727            and then Nkind (Subtype_Indication (Type_Definition (N))) =
5728                                                          N_Subtype_Indication
5729          then
5730             Error_Msg_N
5731               ("cannot constrain type with unknown discriminants",
5732                Subtype_Indication (Type_Definition (N)));
5733             return;
5734          end if;
5735
5736          --  If full view of parent is a record type, build full view as a
5737          --  derivation from the parent's full view. Partial view remains
5738          --  private. For code generation and linking, the full view must have
5739          --  the same public status as the partial one. This full view is only
5740          --  needed if the parent type is in an enclosing scope, so that the
5741          --  full view may actually become visible, e.g. in a child unit. This
5742          --  is both more efficient, and avoids order of freezing problems with
5743          --  the added entities.
5744
5745          if not Is_Private_Type (Full_View (Parent_Type))
5746            and then (In_Open_Scopes (Scope (Parent_Type)))
5747          then
5748             Full_Der := Make_Defining_Identifier (Sloc (Derived_Type),
5749                                               Chars (Derived_Type));
5750             Set_Is_Itype (Full_Der);
5751             Set_Has_Private_Declaration (Full_Der);
5752             Set_Has_Private_Declaration (Derived_Type);
5753             Set_Associated_Node_For_Itype (Full_Der, N);
5754             Set_Parent (Full_Der, Parent (Derived_Type));
5755             Set_Full_View (Derived_Type, Full_Der);
5756             Set_Is_Public (Full_Der, Is_Public (Derived_Type));
5757             Full_P := Full_View (Parent_Type);
5758             Exchange_Declarations (Parent_Type);
5759             Copy_And_Build;
5760             Exchange_Declarations (Full_P);
5761
5762          else
5763             Build_Derived_Record_Type
5764               (N, Full_View (Parent_Type), Derived_Type,
5765                 Derive_Subps => False);
5766          end if;
5767
5768          --  In any case, the primitive operations are inherited from the
5769          --  parent type, not from the internal full view.
5770
5771          Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
5772
5773          if Derive_Subps then
5774             Derive_Subprograms (Parent_Type, Derived_Type);
5775          end if;
5776
5777       else
5778          --  Untagged type, No discriminants on either view
5779
5780          if Nkind (Subtype_Indication (Type_Definition (N))) =
5781                                                    N_Subtype_Indication
5782          then
5783             Error_Msg_N
5784               ("illegal constraint on type without discriminants", N);
5785          end if;
5786
5787          if Present (Discriminant_Specifications (N))
5788            and then Present (Full_View (Parent_Type))
5789            and then not Is_Tagged_Type (Full_View (Parent_Type))
5790          then
5791             Error_Msg_N ("cannot add discriminants to untagged type", N);
5792          end if;
5793
5794          Set_Stored_Constraint (Derived_Type, No_Elist);
5795          Set_Is_Constrained    (Derived_Type, Is_Constrained (Parent_Type));
5796          Set_Is_Controlled     (Derived_Type, Is_Controlled  (Parent_Type));
5797          Set_Has_Controlled_Component
5798                                (Derived_Type, Has_Controlled_Component
5799                                                              (Parent_Type));
5800
5801          --  Direct controlled types do not inherit Finalize_Storage_Only flag
5802
5803          if not Is_Controlled  (Parent_Type) then
5804             Set_Finalize_Storage_Only
5805               (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
5806          end if;
5807
5808          --  Construct the implicit full view by deriving from full view of the
5809          --  parent type. In order to get proper visibility, we install the
5810          --  parent scope and its declarations.
5811
5812          --  ??? If the parent is untagged private and its completion is
5813          --  tagged, this mechanism will not work because we cannot derive from
5814          --  the tagged full view unless we have an extension.
5815
5816          if Present (Full_View (Parent_Type))
5817            and then not Is_Tagged_Type (Full_View (Parent_Type))
5818            and then not Is_Completion
5819          then
5820             Full_Der :=
5821               Make_Defining_Identifier (Sloc (Derived_Type),
5822                 Chars => Chars (Derived_Type));
5823             Set_Is_Itype (Full_Der);
5824             Set_Has_Private_Declaration (Full_Der);
5825             Set_Has_Private_Declaration (Derived_Type);
5826             Set_Associated_Node_For_Itype (Full_Der, N);
5827             Set_Parent (Full_Der, Parent (Derived_Type));
5828             Set_Full_View (Derived_Type, Full_Der);
5829
5830             if not In_Open_Scopes (Par_Scope) then
5831                Install_Private_Declarations (Par_Scope);
5832                Install_Visible_Declarations (Par_Scope);
5833                Copy_And_Build;
5834                Uninstall_Declarations (Par_Scope);
5835
5836             --  If parent scope is open and in another unit, and parent has a
5837             --  completion, then the derivation is taking place in the visible
5838             --  part of a child unit. In that case retrieve the full view of
5839             --  the parent momentarily.
5840
5841             elsif not In_Same_Source_Unit (N, Parent_Type) then
5842                Full_P := Full_View (Parent_Type);
5843                Exchange_Declarations (Parent_Type);
5844                Copy_And_Build;
5845                Exchange_Declarations (Full_P);
5846
5847             --  Otherwise it is a local derivation
5848
5849             else
5850                Copy_And_Build;
5851             end if;
5852
5853             Set_Scope                (Full_Der, Current_Scope);
5854             Set_Is_First_Subtype     (Full_Der,
5855                                        Is_First_Subtype (Derived_Type));
5856             Set_Has_Size_Clause      (Full_Der, False);
5857             Set_Has_Alignment_Clause (Full_Der, False);
5858             Set_Next_Entity          (Full_Der, Empty);
5859             Set_Has_Delayed_Freeze   (Full_Der);
5860             Set_Is_Frozen            (Full_Der, False);
5861             Set_Freeze_Node          (Full_Der, Empty);
5862             Set_Depends_On_Private   (Full_Der,
5863                                        Has_Private_Component (Full_Der));
5864             Set_Public_Status        (Full_Der);
5865          end if;
5866       end if;
5867
5868       Set_Has_Unknown_Discriminants (Derived_Type,
5869         Has_Unknown_Discriminants (Parent_Type));
5870
5871       if Is_Private_Type (Derived_Type) then
5872          Set_Private_Dependents (Derived_Type, New_Elmt_List);
5873       end if;
5874
5875       if Is_Private_Type (Parent_Type)
5876         and then Base_Type (Parent_Type) = Parent_Type
5877         and then In_Open_Scopes (Scope (Parent_Type))
5878       then
5879          Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
5880
5881          if Is_Child_Unit (Scope (Current_Scope))
5882            and then Is_Completion
5883            and then In_Private_Part (Current_Scope)
5884            and then Scope (Parent_Type) /= Current_Scope
5885          then
5886             --  This is the unusual case where a type completed by a private
5887             --  derivation occurs within a package nested in a child unit, and
5888             --  the parent is declared in an ancestor. In this case, the full
5889             --  view of the parent type will become visible in the body of
5890             --  the enclosing child, and only then will the current type be
5891             --  possibly non-private. We build a underlying full view that
5892             --  will be installed when the enclosing child body is compiled.
5893
5894             Full_Der :=
5895               Make_Defining_Identifier (Sloc (Derived_Type),
5896                 Chars => Chars (Derived_Type));
5897             Set_Is_Itype (Full_Der);
5898             Build_Itype_Reference (Full_Der, N);
5899
5900             --  The full view will be used to swap entities on entry/exit to
5901             --  the body, and must appear in the entity list for the package.
5902
5903             Append_Entity (Full_Der, Scope (Derived_Type));
5904             Set_Has_Private_Declaration (Full_Der);
5905             Set_Has_Private_Declaration (Derived_Type);
5906             Set_Associated_Node_For_Itype (Full_Der, N);
5907             Set_Parent (Full_Der, Parent (Derived_Type));
5908             Full_P := Full_View (Parent_Type);
5909             Exchange_Declarations (Parent_Type);
5910             Copy_And_Build;
5911             Exchange_Declarations (Full_P);
5912             Set_Underlying_Full_View (Derived_Type, Full_Der);
5913          end if;
5914       end if;
5915    end Build_Derived_Private_Type;
5916
5917    -------------------------------
5918    -- Build_Derived_Record_Type --
5919    -------------------------------
5920
5921    --  1. INTRODUCTION
5922
5923    --  Ideally we would like to use the same model of type derivation for
5924    --  tagged and untagged record types. Unfortunately this is not quite
5925    --  possible because the semantics of representation clauses is different
5926    --  for tagged and untagged records under inheritance. Consider the
5927    --  following:
5928
5929    --     type R (...) is [tagged] record ... end record;
5930    --     type T (...) is new R (...) [with ...];
5931
5932    --  The representation clauses for T can specify a completely different
5933    --  record layout from R's. Hence the same component can be placed in two
5934    --  very different positions in objects of type T and R. If R and T are
5935    --  tagged types, representation clauses for T can only specify the layout
5936    --  of non inherited components, thus components that are common in R and T
5937    --  have the same position in objects of type R and T.
5938
5939    --  This has two implications. The first is that the entire tree for R's
5940    --  declaration needs to be copied for T in the untagged case, so that T
5941    --  can be viewed as a record type of its own with its own representation
5942    --  clauses. The second implication is the way we handle discriminants.
5943    --  Specifically, in the untagged case we need a way to communicate to Gigi
5944    --  what are the real discriminants in the record, while for the semantics
5945    --  we need to consider those introduced by the user to rename the
5946    --  discriminants in the parent type. This is handled by introducing the
5947    --  notion of stored discriminants. See below for more.
5948
5949    --  Fortunately the way regular components are inherited can be handled in
5950    --  the same way in tagged and untagged types.
5951
5952    --  To complicate things a bit more the private view of a private extension
5953    --  cannot be handled in the same way as the full view (for one thing the
5954    --  semantic rules are somewhat different). We will explain what differs
5955    --  below.
5956
5957    --  2. DISCRIMINANTS UNDER INHERITANCE
5958
5959    --  The semantic rules governing the discriminants of derived types are
5960    --  quite subtle.
5961
5962    --   type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
5963    --      [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
5964
5965    --  If parent type has discriminants, then the discriminants that are
5966    --  declared in the derived type are [3.4 (11)]:
5967
5968    --  o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
5969    --    there is one;
5970
5971    --  o Otherwise, each discriminant of the parent type (implicitly declared
5972    --    in the same order with the same specifications). In this case, the
5973    --    discriminants are said to be "inherited", or if unknown in the parent
5974    --    are also unknown in the derived type.
5975
5976    --  Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
5977
5978    --  o The parent subtype shall be constrained;
5979
5980    --  o If the parent type is not a tagged type, then each discriminant of
5981    --    the derived type shall be used in the constraint defining a parent
5982    --    subtype. [Implementation note: This ensures that the new discriminant
5983    --    can share storage with an existing discriminant.]
5984
5985    --  For the derived type each discriminant of the parent type is either
5986    --  inherited, constrained to equal some new discriminant of the derived
5987    --  type, or constrained to the value of an expression.
5988
5989    --  When inherited or constrained to equal some new discriminant, the
5990    --  parent discriminant and the discriminant of the derived type are said
5991    --  to "correspond".
5992
5993    --  If a discriminant of the parent type is constrained to a specific value
5994    --  in the derived type definition, then the discriminant is said to be
5995    --  "specified" by that derived type definition.
5996
5997    --  3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
5998
5999    --  We have spoken about stored discriminants in point 1 (introduction)
6000    --  above. There are two sort of stored discriminants: implicit and
6001    --  explicit. As long as the derived type inherits the same discriminants as
6002    --  the root record type, stored discriminants are the same as regular
6003    --  discriminants, and are said to be implicit. However, if any discriminant
6004    --  in the root type was renamed in the derived type, then the derived
6005    --  type will contain explicit stored discriminants. Explicit stored
6006    --  discriminants are discriminants in addition to the semantically visible
6007    --  discriminants defined for the derived type. Stored discriminants are
6008    --  used by Gigi to figure out what are the physical discriminants in
6009    --  objects of the derived type (see precise definition in einfo.ads).
6010    --  As an example, consider the following:
6011
6012    --           type R  (D1, D2, D3 : Int) is record ... end record;
6013    --           type T1 is new R;
6014    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
6015    --           type T3 is new T2;
6016    --           type T4 (Y : Int) is new T3 (Y, 99);
6017
6018    --  The following table summarizes the discriminants and stored
6019    --  discriminants in R and T1 through T4.
6020
6021    --   Type      Discrim     Stored Discrim  Comment
6022    --    R      (D1, D2, D3)   (D1, D2, D3)   Girder discrims implicit in R
6023    --    T1     (D1, D2, D3)   (D1, D2, D3)   Girder discrims implicit in T1
6024    --    T2     (X1, X2)       (D1, D2, D3)   Girder discrims EXPLICIT in T2
6025    --    T3     (X1, X2)       (D1, D2, D3)   Girder discrims EXPLICIT in T3
6026    --    T4     (Y)            (D1, D2, D3)   Girder discrims EXPLICIT in T4
6027
6028    --  Field Corresponding_Discriminant (abbreviated CD below) allows us to
6029    --  find the corresponding discriminant in the parent type, while
6030    --  Original_Record_Component (abbreviated ORC below), the actual physical
6031    --  component that is renamed. Finally the field Is_Completely_Hidden
6032    --  (abbreviated ICH below) is set for all explicit stored discriminants
6033    --  (see einfo.ads for more info). For the above example this gives:
6034
6035    --                 Discrim     CD        ORC     ICH
6036    --                 ^^^^^^^     ^^        ^^^     ^^^
6037    --                 D1 in R    empty     itself    no
6038    --                 D2 in R    empty     itself    no
6039    --                 D3 in R    empty     itself    no
6040
6041    --                 D1 in T1  D1 in R    itself    no
6042    --                 D2 in T1  D2 in R    itself    no
6043    --                 D3 in T1  D3 in R    itself    no
6044
6045    --                 X1 in T2  D3 in T1  D3 in T2   no
6046    --                 X2 in T2  D1 in T1  D1 in T2   no
6047    --                 D1 in T2   empty    itself    yes
6048    --                 D2 in T2   empty    itself    yes
6049    --                 D3 in T2   empty    itself    yes
6050
6051    --                 X1 in T3  X1 in T2  D3 in T3   no
6052    --                 X2 in T3  X2 in T2  D1 in T3   no
6053    --                 D1 in T3   empty    itself    yes
6054    --                 D2 in T3   empty    itself    yes
6055    --                 D3 in T3   empty    itself    yes
6056
6057    --                 Y  in T4  X1 in T3  D3 in T3   no
6058    --                 D1 in T3   empty    itself    yes
6059    --                 D2 in T3   empty    itself    yes
6060    --                 D3 in T3   empty    itself    yes
6061
6062    --  4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
6063
6064    --  Type derivation for tagged types is fairly straightforward. If no
6065    --  discriminants are specified by the derived type, these are inherited
6066    --  from the parent. No explicit stored discriminants are ever necessary.
6067    --  The only manipulation that is done to the tree is that of adding a
6068    --  _parent field with parent type and constrained to the same constraint
6069    --  specified for the parent in the derived type definition. For instance:
6070
6071    --           type R  (D1, D2, D3 : Int) is tagged record ... end record;
6072    --           type T1 is new R with null record;
6073    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
6074
6075    --  are changed into:
6076
6077    --           type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
6078    --              _parent : R (D1, D2, D3);
6079    --           end record;
6080
6081    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
6082    --              _parent : T1 (X2, 88, X1);
6083    --           end record;
6084
6085    --  The discriminants actually present in R, T1 and T2 as well as their CD,
6086    --  ORC and ICH fields are:
6087
6088    --                 Discrim     CD        ORC     ICH
6089    --                 ^^^^^^^     ^^        ^^^     ^^^
6090    --                 D1 in R    empty     itself    no
6091    --                 D2 in R    empty     itself    no
6092    --                 D3 in R    empty     itself    no
6093
6094    --                 D1 in T1  D1 in R    D1 in R   no
6095    --                 D2 in T1  D2 in R    D2 in R   no
6096    --                 D3 in T1  D3 in R    D3 in R   no
6097
6098    --                 X1 in T2  D3 in T1   D3 in R   no
6099    --                 X2 in T2  D1 in T1   D1 in R   no
6100
6101    --  5. FIRST TRANSFORMATION FOR DERIVED RECORDS
6102    --
6103    --  Regardless of whether we dealing with a tagged or untagged type
6104    --  we will transform all derived type declarations of the form
6105    --
6106    --               type T is new R (...) [with ...];
6107    --  or
6108    --               subtype S is R (...);
6109    --               type T is new S [with ...];
6110    --  into
6111    --               type BT is new R [with ...];
6112    --               subtype T is BT (...);
6113    --
6114    --  That is, the base derived type is constrained only if it has no
6115    --  discriminants. The reason for doing this is that GNAT's semantic model
6116    --  assumes that a base type with discriminants is unconstrained.
6117    --
6118    --  Note that, strictly speaking, the above transformation is not always
6119    --  correct. Consider for instance the following excerpt from ACVC b34011a:
6120    --
6121    --       procedure B34011A is
6122    --          type REC (D : integer := 0) is record
6123    --             I : Integer;
6124    --          end record;
6125
6126    --          package P is
6127    --             type T6 is new Rec;
6128    --             function F return T6;
6129    --          end P;
6130
6131    --          use P;
6132    --          package Q6 is
6133    --             type U is new T6 (Q6.F.I);                   -- ERROR: Q6.F.
6134    --          end Q6;
6135    --
6136    --  The definition of Q6.U is illegal. However transforming Q6.U into
6137
6138    --             type BaseU is new T6;
6139    --             subtype U is BaseU (Q6.F.I)
6140
6141    --  turns U into a legal subtype, which is incorrect. To avoid this problem
6142    --  we always analyze the constraint (in this case (Q6.F.I)) before applying
6143    --  the transformation described above.
6144
6145    --  There is another instance where the above transformation is incorrect.
6146    --  Consider:
6147
6148    --          package Pack is
6149    --             type Base (D : Integer) is tagged null record;
6150    --             procedure P (X : Base);
6151
6152    --             type Der is new Base (2) with null record;
6153    --             procedure P (X : Der);
6154    --          end Pack;
6155
6156    --  Then the above transformation turns this into
6157
6158    --             type Der_Base is new Base with null record;
6159    --             --  procedure P (X : Base) is implicitly inherited here
6160    --             --  as procedure P (X : Der_Base).
6161
6162    --             subtype Der is Der_Base (2);
6163    --             procedure P (X : Der);
6164    --             --  The overriding of P (X : Der_Base) is illegal since we
6165    --             --  have a parameter conformance problem.
6166
6167    --  To get around this problem, after having semantically processed Der_Base
6168    --  and the rewritten subtype declaration for Der, we copy Der_Base field
6169    --  Discriminant_Constraint from Der so that when parameter conformance is
6170    --  checked when P is overridden, no semantic errors are flagged.
6171
6172    --  6. SECOND TRANSFORMATION FOR DERIVED RECORDS
6173
6174    --  Regardless of whether we are dealing with a tagged or untagged type
6175    --  we will transform all derived type declarations of the form
6176
6177    --               type R (D1, .., Dn : ...) is [tagged] record ...;
6178    --               type T is new R [with ...];
6179    --  into
6180    --               type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
6181
6182    --  The reason for such transformation is that it allows us to implement a
6183    --  very clean form of component inheritance as explained below.
6184
6185    --  Note that this transformation is not achieved by direct tree rewriting
6186    --  and manipulation, but rather by redoing the semantic actions that the
6187    --  above transformation will entail. This is done directly in routine
6188    --  Inherit_Components.
6189
6190    --  7. TYPE DERIVATION AND COMPONENT INHERITANCE
6191
6192    --  In both tagged and untagged derived types, regular non discriminant
6193    --  components are inherited in the derived type from the parent type. In
6194    --  the absence of discriminants component, inheritance is straightforward
6195    --  as components can simply be copied from the parent.
6196
6197    --  If the parent has discriminants, inheriting components constrained with
6198    --  these discriminants requires caution. Consider the following example:
6199
6200    --      type R  (D1, D2 : Positive) is [tagged] record
6201    --         S : String (D1 .. D2);
6202    --      end record;
6203
6204    --      type T1                is new R        [with null record];
6205    --      type T2 (X : positive) is new R (1, X) [with null record];
6206
6207    --  As explained in 6. above, T1 is rewritten as
6208    --      type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
6209    --  which makes the treatment for T1 and T2 identical.
6210
6211    --  What we want when inheriting S, is that references to D1 and D2 in R are
6212    --  replaced with references to their correct constraints, i.e. D1 and D2 in
6213    --  T1 and 1 and X in T2. So all R's discriminant references are replaced
6214    --  with either discriminant references in the derived type or expressions.
6215    --  This replacement is achieved as follows: before inheriting R's
6216    --  components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
6217    --  created in the scope of T1 (resp. scope of T2) so that discriminants D1
6218    --  and D2 of T1 are visible (resp. discriminant X of T2 is visible).
6219    --  For T2, for instance, this has the effect of replacing String (D1 .. D2)
6220    --  by String (1 .. X).
6221
6222    --  8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
6223
6224    --  We explain here the rules governing private type extensions relevant to
6225    --  type derivation. These rules are explained on the following example:
6226
6227    --      type D [(...)] is new A [(...)] with private;      <-- partial view
6228    --      type D [(...)] is new P [(...)] with null record;  <-- full view
6229
6230    --  Type A is called the ancestor subtype of the private extension.
6231    --  Type P is the parent type of the full view of the private extension. It
6232    --  must be A or a type derived from A.
6233
6234    --  The rules concerning the discriminants of private type extensions are
6235    --  [7.3(10-13)]:
6236
6237    --  o If a private extension inherits known discriminants from the ancestor
6238    --    subtype, then the full view shall also inherit its discriminants from
6239    --    the ancestor subtype and the parent subtype of the full view shall be
6240    --    constrained if and only if the ancestor subtype is constrained.
6241
6242    --  o If a partial view has unknown discriminants, then the full view may
6243    --    define a definite or an indefinite subtype, with or without
6244    --    discriminants.
6245
6246    --  o If a partial view has neither known nor unknown discriminants, then
6247    --    the full view shall define a definite subtype.
6248
6249    --  o If the ancestor subtype of a private extension has constrained
6250    --    discriminants, then the parent subtype of the full view shall impose a
6251    --    statically matching constraint on those discriminants.
6252
6253    --  This means that only the following forms of private extensions are
6254    --  allowed:
6255
6256    --      type D is new A with private;      <-- partial view
6257    --      type D is new P with null record;  <-- full view
6258
6259    --  If A has no discriminants than P has no discriminants, otherwise P must
6260    --  inherit A's discriminants.
6261
6262    --      type D is new A (...) with private;      <-- partial view
6263    --      type D is new P (:::) with null record;  <-- full view
6264
6265    --  P must inherit A's discriminants and (...) and (:::) must statically
6266    --  match.
6267
6268    --      subtype A is R (...);
6269    --      type D is new A with private;      <-- partial view
6270    --      type D is new P with null record;  <-- full view
6271
6272    --  P must have inherited R's discriminants and must be derived from A or
6273    --  any of its subtypes.
6274
6275    --      type D (..) is new A with private;              <-- partial view
6276    --      type D (..) is new P [(:::)] with null record;  <-- full view
6277
6278    --  No specific constraints on P's discriminants or constraint (:::).
6279    --  Note that A can be unconstrained, but the parent subtype P must either
6280    --  be constrained or (:::) must be present.
6281
6282    --      type D (..) is new A [(...)] with private;      <-- partial view
6283    --      type D (..) is new P [(:::)] with null record;  <-- full view
6284
6285    --  P's constraints on A's discriminants must statically match those
6286    --  imposed by (...).
6287
6288    --  9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
6289
6290    --  The full view of a private extension is handled exactly as described
6291    --  above. The model chose for the private view of a private extension is
6292    --  the same for what concerns discriminants (i.e. they receive the same
6293    --  treatment as in the tagged case). However, the private view of the
6294    --  private extension always inherits the components of the parent base,
6295    --  without replacing any discriminant reference. Strictly speaking this is
6296    --  incorrect. However, Gigi never uses this view to generate code so this
6297    --  is a purely semantic issue. In theory, a set of transformations similar
6298    --  to those given in 5. and 6. above could be applied to private views of
6299    --  private extensions to have the same model of component inheritance as
6300    --  for non private extensions. However, this is not done because it would
6301    --  further complicate private type processing. Semantically speaking, this
6302    --  leaves us in an uncomfortable situation. As an example consider:
6303
6304    --          package Pack is
6305    --             type R (D : integer) is tagged record
6306    --                S : String (1 .. D);
6307    --             end record;
6308    --             procedure P (X : R);
6309    --             type T is new R (1) with private;
6310    --          private
6311    --             type T is new R (1) with null record;
6312    --          end;
6313
6314    --  This is transformed into:
6315
6316    --          package Pack is
6317    --             type R (D : integer) is tagged record
6318    --                S : String (1 .. D);
6319    --             end record;
6320    --             procedure P (X : R);
6321    --             type T is new R (1) with private;
6322    --          private
6323    --             type BaseT is new R with null record;
6324    --             subtype  T is BaseT (1);
6325    --          end;
6326
6327    --  (strictly speaking the above is incorrect Ada)
6328
6329    --  From the semantic standpoint the private view of private extension T
6330    --  should be flagged as constrained since one can clearly have
6331    --
6332    --             Obj : T;
6333    --
6334    --  in a unit withing Pack. However, when deriving subprograms for the
6335    --  private view of private extension T, T must be seen as unconstrained
6336    --  since T has discriminants (this is a constraint of the current
6337    --  subprogram derivation model). Thus, when processing the private view of
6338    --  a private extension such as T, we first mark T as unconstrained, we
6339    --  process it, we perform program derivation and just before returning from
6340    --  Build_Derived_Record_Type we mark T as constrained.
6341
6342    --  ??? Are there are other uncomfortable cases that we will have to
6343    --      deal with.
6344
6345    --  10. RECORD_TYPE_WITH_PRIVATE complications
6346
6347    --  Types that are derived from a visible record type and have a private
6348    --  extension present other peculiarities. They behave mostly like private
6349    --  types, but if they have primitive operations defined, these will not
6350    --  have the proper signatures for further inheritance, because other
6351    --  primitive operations will use the implicit base that we define for
6352    --  private derivations below. This affect subprogram inheritance (see
6353    --  Derive_Subprograms for details). We also derive the implicit base from
6354    --  the base type of the full view, so that the implicit base is a record
6355    --  type and not another private type, This avoids infinite loops.
6356
6357    procedure Build_Derived_Record_Type
6358      (N            : Node_Id;
6359       Parent_Type  : Entity_Id;
6360       Derived_Type : Entity_Id;
6361       Derive_Subps : Boolean := True)
6362    is
6363       Loc          : constant Source_Ptr := Sloc (N);
6364       Parent_Base  : Entity_Id;
6365       Type_Def     : Node_Id;
6366       Indic        : Node_Id;
6367       Discrim      : Entity_Id;
6368       Last_Discrim : Entity_Id;
6369       Constrs      : Elist_Id;
6370
6371       Discs : Elist_Id := New_Elmt_List;
6372       --  An empty Discs list means that there were no constraints in the
6373       --  subtype indication or that there was an error processing it.
6374
6375       Assoc_List : Elist_Id;
6376       New_Discrs : Elist_Id;
6377       New_Base   : Entity_Id;
6378       New_Decl   : Node_Id;
6379       New_Indic  : Node_Id;
6380
6381       Is_Tagged          : constant Boolean := Is_Tagged_Type (Parent_Type);
6382       Discriminant_Specs : constant Boolean :=
6383                              Present (Discriminant_Specifications (N));
6384       Private_Extension  : constant Boolean :=
6385                              Nkind (N) = N_Private_Extension_Declaration;
6386
6387       Constraint_Present : Boolean;
6388       Inherit_Discrims   : Boolean := False;
6389       Save_Etype         : Entity_Id;
6390       Save_Discr_Constr  : Elist_Id;
6391       Save_Next_Entity   : Entity_Id;
6392
6393    begin
6394       if Ekind (Parent_Type) = E_Record_Type_With_Private
6395         and then Present (Full_View (Parent_Type))
6396         and then Has_Discriminants (Parent_Type)
6397       then
6398          Parent_Base := Base_Type (Full_View (Parent_Type));
6399       else
6400          Parent_Base := Base_Type (Parent_Type);
6401       end if;
6402
6403       --  Before we start the previously documented transformations, here is
6404       --  little fix for size and alignment of tagged types. Normally when we
6405       --  derive type D from type P, we copy the size and alignment of P as the
6406       --  default for D, and in the absence of explicit representation clauses
6407       --  for D, the size and alignment are indeed the same as the parent.
6408
6409       --  But this is wrong for tagged types, since fields may be added, and
6410       --  the default size may need to be larger, and the default alignment may
6411       --  need to be larger.
6412
6413       --  We therefore reset the size and alignment fields in the tagged case.
6414       --  Note that the size and alignment will in any case be at least as
6415       --  large as the parent type (since the derived type has a copy of the
6416       --  parent type in the _parent field)
6417
6418       --  The type is also marked as being tagged here, which is needed when
6419       --  processing components with a self-referential anonymous access type
6420       --  in the call to Check_Anonymous_Access_Components below. Note that
6421       --  this flag is also set later on for completeness.
6422
6423       if Is_Tagged then
6424          Set_Is_Tagged_Type (Derived_Type);
6425          Init_Size_Align    (Derived_Type);
6426       end if;
6427
6428       --  STEP 0a: figure out what kind of derived type declaration we have
6429
6430       if Private_Extension then
6431          Type_Def := N;
6432          Set_Ekind (Derived_Type, E_Record_Type_With_Private);
6433
6434       else
6435          Type_Def := Type_Definition (N);
6436
6437          --  Ekind (Parent_Base) is not necessarily E_Record_Type since
6438          --  Parent_Base can be a private type or private extension. However,
6439          --  for tagged types with an extension the newly added fields are
6440          --  visible and hence the Derived_Type is always an E_Record_Type.
6441          --  (except that the parent may have its own private fields).
6442          --  For untagged types we preserve the Ekind of the Parent_Base.
6443
6444          if Present (Record_Extension_Part (Type_Def)) then
6445             Set_Ekind (Derived_Type, E_Record_Type);
6446
6447             --  Create internal access types for components with anonymous
6448             --  access types.
6449
6450             if Ada_Version >= Ada_05 then
6451                Check_Anonymous_Access_Components
6452                  (N, Derived_Type, Derived_Type,
6453                    Component_List (Record_Extension_Part (Type_Def)));
6454             end if;
6455
6456          else
6457             Set_Ekind (Derived_Type, Ekind (Parent_Base));
6458          end if;
6459       end if;
6460
6461       --  Indic can either be an N_Identifier if the subtype indication
6462       --  contains no constraint or an N_Subtype_Indication if the subtype
6463       --  indication has a constraint.
6464
6465       Indic := Subtype_Indication (Type_Def);
6466       Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
6467
6468       --  Check that the type has visible discriminants. The type may be
6469       --  a private type with unknown discriminants whose full view has
6470       --  discriminants which are invisible.
6471
6472       if Constraint_Present then
6473          if not Has_Discriminants (Parent_Base)
6474            or else
6475              (Has_Unknown_Discriminants (Parent_Base)
6476                 and then Is_Private_Type (Parent_Base))
6477          then
6478             Error_Msg_N
6479               ("invalid constraint: type has no discriminant",
6480                  Constraint (Indic));
6481
6482             Constraint_Present := False;
6483             Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6484
6485          elsif Is_Constrained (Parent_Type) then
6486             Error_Msg_N
6487                ("invalid constraint: parent type is already constrained",
6488                   Constraint (Indic));
6489
6490             Constraint_Present := False;
6491             Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6492          end if;
6493       end if;
6494
6495       --  STEP 0b: If needed, apply transformation given in point 5. above
6496
6497       if not Private_Extension
6498         and then Has_Discriminants (Parent_Type)
6499         and then not Discriminant_Specs
6500         and then (Is_Constrained (Parent_Type) or else Constraint_Present)
6501       then
6502          --  First, we must analyze the constraint (see comment in point 5.)
6503
6504          if Constraint_Present then
6505             New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
6506
6507             if Has_Discriminants (Derived_Type)
6508               and then Has_Private_Declaration (Derived_Type)
6509               and then Present (Discriminant_Constraint (Derived_Type))
6510             then
6511                --  Verify that constraints of the full view statically match
6512                --  those given in the partial view.
6513
6514                declare
6515                   C1, C2 : Elmt_Id;
6516
6517                begin
6518                   C1 := First_Elmt (New_Discrs);
6519                   C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
6520                   while Present (C1) and then Present (C2) loop
6521                      if Fully_Conformant_Expressions (Node (C1), Node (C2))
6522                        or else
6523                          (Is_OK_Static_Expression (Node (C1))
6524                             and then
6525                           Is_OK_Static_Expression (Node (C2))
6526                             and then
6527                           Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
6528                      then
6529                         null;
6530
6531                      else
6532                         Error_Msg_N (
6533                           "constraint not conformant to previous declaration",
6534                              Node (C1));
6535                      end if;
6536
6537                      Next_Elmt (C1);
6538                      Next_Elmt (C2);
6539                   end loop;
6540                end;
6541             end if;
6542          end if;
6543
6544          --  Insert and analyze the declaration for the unconstrained base type
6545
6546          New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
6547
6548          New_Decl :=
6549            Make_Full_Type_Declaration (Loc,
6550               Defining_Identifier => New_Base,
6551               Type_Definition     =>
6552                 Make_Derived_Type_Definition (Loc,
6553                   Abstract_Present      => Abstract_Present (Type_Def),
6554                   Limited_Present       => Limited_Present (Type_Def),
6555                   Subtype_Indication    =>
6556                     New_Occurrence_Of (Parent_Base, Loc),
6557                   Record_Extension_Part =>
6558                     Relocate_Node (Record_Extension_Part (Type_Def)),
6559                   Interface_List        => Interface_List (Type_Def)));
6560
6561          Set_Parent (New_Decl, Parent (N));
6562          Mark_Rewrite_Insertion (New_Decl);
6563          Insert_Before (N, New_Decl);
6564
6565          --  Note that this call passes False for the Derive_Subps parameter
6566          --  because subprogram derivation is deferred until after creating
6567          --  the subtype (see below).
6568
6569          Build_Derived_Type
6570            (New_Decl, Parent_Base, New_Base,
6571             Is_Completion => True, Derive_Subps => False);
6572
6573          --  ??? This needs re-examination to determine whether the
6574          --  above call can simply be replaced by a call to Analyze.
6575
6576          Set_Analyzed (New_Decl);
6577
6578          --  Insert and analyze the declaration for the constrained subtype
6579
6580          if Constraint_Present then
6581             New_Indic :=
6582               Make_Subtype_Indication (Loc,
6583                 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6584                 Constraint   => Relocate_Node (Constraint (Indic)));
6585
6586          else
6587             declare
6588                Constr_List : constant List_Id := New_List;
6589                C           : Elmt_Id;
6590                Expr        : Node_Id;
6591
6592             begin
6593                C := First_Elmt (Discriminant_Constraint (Parent_Type));
6594                while Present (C) loop
6595                   Expr := Node (C);
6596
6597                   --  It is safe here to call New_Copy_Tree since
6598                   --  Force_Evaluation was called on each constraint in
6599                   --  Build_Discriminant_Constraints.
6600
6601                   Append (New_Copy_Tree (Expr), To => Constr_List);
6602
6603                   Next_Elmt (C);
6604                end loop;
6605
6606                New_Indic :=
6607                  Make_Subtype_Indication (Loc,
6608                    Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6609                    Constraint   =>
6610                      Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
6611             end;
6612          end if;
6613
6614          Rewrite (N,
6615            Make_Subtype_Declaration (Loc,
6616              Defining_Identifier => Derived_Type,
6617              Subtype_Indication  => New_Indic));
6618
6619          Analyze (N);
6620
6621          --  Derivation of subprograms must be delayed until the full subtype
6622          --  has been established to ensure proper overriding of subprograms
6623          --  inherited by full types. If the derivations occurred as part of
6624          --  the call to Build_Derived_Type above, then the check for type
6625          --  conformance would fail because earlier primitive subprograms
6626          --  could still refer to the full type prior the change to the new
6627          --  subtype and hence would not match the new base type created here.
6628
6629          Derive_Subprograms (Parent_Type, Derived_Type);
6630
6631          --  For tagged types the Discriminant_Constraint of the new base itype
6632          --  is inherited from the first subtype so that no subtype conformance
6633          --  problem arise when the first subtype overrides primitive
6634          --  operations inherited by the implicit base type.
6635
6636          if Is_Tagged then
6637             Set_Discriminant_Constraint
6638               (New_Base, Discriminant_Constraint (Derived_Type));
6639          end if;
6640
6641          return;
6642       end if;
6643
6644       --  If we get here Derived_Type will have no discriminants or it will be
6645       --  a discriminated unconstrained base type.
6646
6647       --  STEP 1a: perform preliminary actions/checks for derived tagged types
6648
6649       if Is_Tagged then
6650
6651          --  The parent type is frozen for non-private extensions (RM 13.14(7))
6652          --  The declaration of a specific descendant of an interface type
6653          --  freezes the interface type (RM 13.14).
6654
6655          if not Private_Extension
6656            or else Is_Interface (Parent_Base)
6657          then
6658             Freeze_Before (N, Parent_Type);
6659          end if;
6660
6661          --  In Ada 2005 (AI-344), the restriction that a derived tagged type
6662          --  cannot be declared at a deeper level than its parent type is
6663          --  removed. The check on derivation within a generic body is also
6664          --  relaxed, but there's a restriction that a derived tagged type
6665          --  cannot be declared in a generic body if it's derived directly
6666          --  or indirectly from a formal type of that generic.
6667
6668          if Ada_Version >= Ada_05 then
6669             if Present (Enclosing_Generic_Body (Derived_Type)) then
6670                declare
6671                   Ancestor_Type : Entity_Id;
6672
6673                begin
6674                   --  Check to see if any ancestor of the derived type is a
6675                   --  formal type.
6676
6677                   Ancestor_Type := Parent_Type;
6678                   while not Is_Generic_Type (Ancestor_Type)
6679                     and then Etype (Ancestor_Type) /= Ancestor_Type
6680                   loop
6681                      Ancestor_Type := Etype (Ancestor_Type);
6682                   end loop;
6683
6684                   --  If the derived type does have a formal type as an
6685                   --  ancestor, then it's an error if the derived type is
6686                   --  declared within the body of the generic unit that
6687                   --  declares the formal type in its generic formal part. It's
6688                   --  sufficient to check whether the ancestor type is declared
6689                   --  inside the same generic body as the derived type (such as
6690                   --  within a nested generic spec), in which case the
6691                   --  derivation is legal. If the formal type is declared
6692                   --  outside of that generic body, then it's guaranteed that
6693                   --  the derived type is declared within the generic body of
6694                   --  the generic unit declaring the formal type.
6695
6696                   if Is_Generic_Type (Ancestor_Type)
6697                     and then Enclosing_Generic_Body (Ancestor_Type) /=
6698                                Enclosing_Generic_Body (Derived_Type)
6699                   then
6700                      Error_Msg_NE
6701                        ("parent type of& must not be descendant of formal type"
6702                           & " of an enclosing generic body",
6703                             Indic, Derived_Type);
6704                   end if;
6705                end;
6706             end if;
6707
6708          elsif Type_Access_Level (Derived_Type) /=
6709                  Type_Access_Level (Parent_Type)
6710            and then not Is_Generic_Type (Derived_Type)
6711          then
6712             if Is_Controlled (Parent_Type) then
6713                Error_Msg_N
6714                  ("controlled type must be declared at the library level",
6715                   Indic);
6716             else
6717                Error_Msg_N
6718                  ("type extension at deeper accessibility level than parent",
6719                   Indic);
6720             end if;
6721
6722          else
6723             declare
6724                GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
6725
6726             begin
6727                if Present (GB)
6728                  and then GB /= Enclosing_Generic_Body (Parent_Base)
6729                then
6730                   Error_Msg_NE
6731                     ("parent type of& must not be outside generic body"
6732                        & " (RM 3.9.1(4))",
6733                          Indic, Derived_Type);
6734                end if;
6735             end;
6736          end if;
6737       end if;
6738
6739       --  Ada 2005 (AI-251)
6740
6741       if Ada_Version = Ada_05
6742         and then Is_Tagged
6743       then
6744          --  "The declaration of a specific descendant of an interface type
6745          --  freezes the interface type" (RM 13.14).
6746
6747          declare
6748             Iface : Node_Id;
6749          begin
6750             if Is_Non_Empty_List (Interface_List (Type_Def)) then
6751                Iface := First (Interface_List (Type_Def));
6752                while Present (Iface) loop
6753                   Freeze_Before (N, Etype (Iface));
6754                   Next (Iface);
6755                end loop;
6756             end if;
6757          end;
6758       end if;
6759
6760       --  STEP 1b : preliminary cleanup of the full view of private types
6761
6762       --  If the type is already marked as having discriminants, then it's the
6763       --  completion of a private type or private extension and we need to
6764       --  retain the discriminants from the partial view if the current
6765       --  declaration has Discriminant_Specifications so that we can verify
6766       --  conformance. However, we must remove any existing components that
6767       --  were inherited from the parent (and attached in Copy_And_Swap)
6768       --  because the full type inherits all appropriate components anyway, and
6769       --  we do not want the partial view's components interfering.
6770
6771       if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
6772          Discrim := First_Discriminant (Derived_Type);
6773          loop
6774             Last_Discrim := Discrim;
6775             Next_Discriminant (Discrim);
6776             exit when No (Discrim);
6777          end loop;
6778
6779          Set_Last_Entity (Derived_Type, Last_Discrim);
6780
6781       --  In all other cases wipe out the list of inherited components (even
6782       --  inherited discriminants), it will be properly rebuilt here.
6783
6784       else
6785          Set_First_Entity (Derived_Type, Empty);
6786          Set_Last_Entity  (Derived_Type, Empty);
6787       end if;
6788
6789       --  STEP 1c: Initialize some flags for the Derived_Type
6790
6791       --  The following flags must be initialized here so that
6792       --  Process_Discriminants can check that discriminants of tagged types do
6793       --  not have a default initial value and that access discriminants are
6794       --  only specified for limited records. For completeness, these flags are
6795       --  also initialized along with all the other flags below.
6796
6797       --  AI-419: Limitedness is not inherited from an interface parent, so to
6798       --  be limited in that case the type must be explicitly declared as
6799       --  limited. However, task and protected interfaces are always limited.
6800
6801       if Limited_Present (Type_Def) then
6802          Set_Is_Limited_Record (Derived_Type);
6803
6804       elsif Is_Limited_Record (Parent_Type)
6805         or else (Present (Full_View (Parent_Type))
6806                    and then Is_Limited_Record (Full_View (Parent_Type)))
6807       then
6808          if not Is_Interface (Parent_Type)
6809            or else Is_Synchronized_Interface (Parent_Type)
6810            or else Is_Protected_Interface (Parent_Type)
6811            or else Is_Task_Interface (Parent_Type)
6812          then
6813             Set_Is_Limited_Record (Derived_Type);
6814          end if;
6815       end if;
6816
6817       --  STEP 2a: process discriminants of derived type if any
6818
6819       Push_Scope (Derived_Type);
6820
6821       if Discriminant_Specs then
6822          Set_Has_Unknown_Discriminants (Derived_Type, False);
6823
6824          --  The following call initializes fields Has_Discriminants and
6825          --  Discriminant_Constraint, unless we are processing the completion
6826          --  of a private type declaration.
6827
6828          Check_Or_Process_Discriminants (N, Derived_Type);
6829
6830          --  For non-tagged types the constraint on the Parent_Type must be
6831          --  present and is used to rename the discriminants.
6832
6833          if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
6834             Error_Msg_N ("untagged parent must have discriminants", Indic);
6835
6836          elsif not Is_Tagged and then not Constraint_Present then
6837             Error_Msg_N
6838               ("discriminant constraint needed for derived untagged records",
6839                Indic);
6840
6841          --  Otherwise the parent subtype must be constrained unless we have a
6842          --  private extension.
6843
6844          elsif not Constraint_Present
6845            and then not Private_Extension
6846            and then not Is_Constrained (Parent_Type)
6847          then
6848             Error_Msg_N
6849               ("unconstrained type not allowed in this context", Indic);
6850
6851          elsif Constraint_Present then
6852             --  The following call sets the field Corresponding_Discriminant
6853             --  for the discriminants in the Derived_Type.
6854
6855             Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
6856
6857             --  For untagged types all new discriminants must rename
6858             --  discriminants in the parent. For private extensions new
6859             --  discriminants cannot rename old ones (implied by [7.3(13)]).
6860
6861             Discrim := First_Discriminant (Derived_Type);
6862             while Present (Discrim) loop
6863                if not Is_Tagged
6864                  and then No (Corresponding_Discriminant (Discrim))
6865                then
6866                   Error_Msg_N
6867                     ("new discriminants must constrain old ones", Discrim);
6868
6869                elsif Private_Extension
6870                  and then Present (Corresponding_Discriminant (Discrim))
6871                then
6872                   Error_Msg_N
6873                     ("only static constraints allowed for parent"
6874                      & " discriminants in the partial view", Indic);
6875                   exit;
6876                end if;
6877
6878                --  If a new discriminant is used in the constraint, then its
6879                --  subtype must be statically compatible with the parent
6880                --  discriminant's subtype (3.7(15)).
6881
6882                if Present (Corresponding_Discriminant (Discrim))
6883                  and then
6884                    not Subtypes_Statically_Compatible
6885                          (Etype (Discrim),
6886                           Etype (Corresponding_Discriminant (Discrim)))
6887                then
6888                   Error_Msg_N
6889                     ("subtype must be compatible with parent discriminant",
6890                      Discrim);
6891                end if;
6892
6893                Next_Discriminant (Discrim);
6894             end loop;
6895
6896             --  Check whether the constraints of the full view statically
6897             --  match those imposed by the parent subtype [7.3(13)].
6898
6899             if Present (Stored_Constraint (Derived_Type)) then
6900                declare
6901                   C1, C2 : Elmt_Id;
6902
6903                begin
6904                   C1 := First_Elmt (Discs);
6905                   C2 := First_Elmt (Stored_Constraint (Derived_Type));
6906                   while Present (C1) and then Present (C2) loop
6907                      if not
6908                        Fully_Conformant_Expressions (Node (C1), Node (C2))
6909                      then
6910                         Error_Msg_N
6911                           ("not conformant with previous declaration",
6912                            Node (C1));
6913                      end if;
6914
6915                      Next_Elmt (C1);
6916                      Next_Elmt (C2);
6917                   end loop;
6918                end;
6919             end if;
6920          end if;
6921
6922       --  STEP 2b: No new discriminants, inherit discriminants if any
6923
6924       else
6925          if Private_Extension then
6926             Set_Has_Unknown_Discriminants
6927               (Derived_Type,
6928                Has_Unknown_Discriminants (Parent_Type)
6929                  or else Unknown_Discriminants_Present (N));
6930
6931          --  The partial view of the parent may have unknown discriminants,
6932          --  but if the full view has discriminants and the parent type is
6933          --  in scope they must be inherited.
6934
6935          elsif Has_Unknown_Discriminants (Parent_Type)
6936            and then
6937             (not Has_Discriminants (Parent_Type)
6938               or else not In_Open_Scopes (Scope (Parent_Type)))
6939          then
6940             Set_Has_Unknown_Discriminants (Derived_Type);
6941          end if;
6942
6943          if not Has_Unknown_Discriminants (Derived_Type)
6944            and then not Has_Unknown_Discriminants (Parent_Base)
6945            and then Has_Discriminants (Parent_Type)
6946          then
6947             Inherit_Discrims := True;
6948             Set_Has_Discriminants
6949               (Derived_Type, True);
6950             Set_Discriminant_Constraint
6951               (Derived_Type, Discriminant_Constraint (Parent_Base));
6952          end if;
6953
6954          --  The following test is true for private types (remember
6955          --  transformation 5. is not applied to those) and in an error
6956          --  situation.
6957
6958          if Constraint_Present then
6959             Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
6960          end if;
6961
6962          --  For now mark a new derived type as constrained only if it has no
6963          --  discriminants. At the end of Build_Derived_Record_Type we properly
6964          --  set this flag in the case of private extensions. See comments in
6965          --  point 9. just before body of Build_Derived_Record_Type.
6966
6967          Set_Is_Constrained
6968            (Derived_Type,
6969             not (Inherit_Discrims
6970                    or else Has_Unknown_Discriminants (Derived_Type)));
6971       end if;
6972
6973       --  STEP 3: initialize fields of derived type
6974
6975       Set_Is_Tagged_Type    (Derived_Type, Is_Tagged);
6976       Set_Stored_Constraint (Derived_Type, No_Elist);
6977
6978       --  Ada 2005 (AI-251): Private type-declarations can implement interfaces
6979       --  but cannot be interfaces
6980
6981       if not Private_Extension
6982          and then Ekind (Derived_Type) /= E_Private_Type
6983          and then Ekind (Derived_Type) /= E_Limited_Private_Type
6984       then
6985          if Interface_Present (Type_Def) then
6986             Analyze_Interface_Declaration (Derived_Type, Type_Def);
6987          end if;
6988
6989          Set_Interfaces (Derived_Type, No_Elist);
6990       end if;
6991
6992       --  Fields inherited from the Parent_Type
6993
6994       Set_Discard_Names
6995         (Derived_Type, Einfo.Discard_Names      (Parent_Type));
6996       Set_Has_Specified_Layout
6997         (Derived_Type, Has_Specified_Layout     (Parent_Type));
6998       Set_Is_Limited_Composite
6999         (Derived_Type, Is_Limited_Composite     (Parent_Type));
7000       Set_Is_Private_Composite
7001         (Derived_Type, Is_Private_Composite     (Parent_Type));
7002
7003       --  Fields inherited from the Parent_Base
7004
7005       Set_Has_Controlled_Component
7006         (Derived_Type, Has_Controlled_Component (Parent_Base));
7007       Set_Has_Non_Standard_Rep
7008         (Derived_Type, Has_Non_Standard_Rep     (Parent_Base));
7009       Set_Has_Primitive_Operations
7010         (Derived_Type, Has_Primitive_Operations (Parent_Base));
7011
7012       --  Fields inherited from the Parent_Base in the non-private case
7013
7014       if Ekind (Derived_Type) = E_Record_Type then
7015          Set_Has_Complex_Representation
7016            (Derived_Type, Has_Complex_Representation (Parent_Base));
7017       end if;
7018
7019       --  Fields inherited from the Parent_Base for record types
7020
7021       if Is_Record_Type (Derived_Type) then
7022          Set_OK_To_Reorder_Components
7023            (Derived_Type, OK_To_Reorder_Components (Parent_Base));
7024          Set_Reverse_Bit_Order
7025            (Derived_Type, Reverse_Bit_Order (Parent_Base));
7026       end if;
7027
7028       --  Direct controlled types do not inherit Finalize_Storage_Only flag
7029
7030       if not Is_Controlled (Parent_Type) then
7031          Set_Finalize_Storage_Only
7032            (Derived_Type, Finalize_Storage_Only (Parent_Type));
7033       end if;
7034
7035       --  Set fields for private derived types
7036
7037       if Is_Private_Type (Derived_Type) then
7038          Set_Depends_On_Private (Derived_Type, True);
7039          Set_Private_Dependents (Derived_Type, New_Elmt_List);
7040
7041       --  Inherit fields from non private record types. If this is the
7042       --  completion of a derivation from a private type, the parent itself
7043       --  is private, and the attributes come from its full view, which must
7044       --  be present.
7045
7046       else
7047          if Is_Private_Type (Parent_Base)
7048            and then not Is_Record_Type (Parent_Base)
7049          then
7050             Set_Component_Alignment
7051               (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
7052             Set_C_Pass_By_Copy
7053               (Derived_Type, C_Pass_By_Copy      (Full_View (Parent_Base)));
7054          else
7055             Set_Component_Alignment
7056               (Derived_Type, Component_Alignment (Parent_Base));
7057
7058             Set_C_Pass_By_Copy
7059               (Derived_Type, C_Pass_By_Copy      (Parent_Base));
7060          end if;
7061       end if;
7062
7063       --  Set fields for tagged types
7064
7065       if Is_Tagged then
7066          Set_Primitive_Operations (Derived_Type, New_Elmt_List);
7067
7068          --  All tagged types defined in Ada.Finalization are controlled
7069
7070          if Chars (Scope (Derived_Type)) = Name_Finalization
7071            and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
7072            and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
7073          then
7074             Set_Is_Controlled (Derived_Type);
7075          else
7076             Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
7077          end if;
7078
7079          --  Minor optimization: there is no need to generate the class-wide
7080          --  entity associated with an underlying record view.
7081
7082          if not Is_Underlying_Record_View (Derived_Type) then
7083             Make_Class_Wide_Type (Derived_Type);
7084          end if;
7085
7086          Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
7087
7088          if Has_Discriminants (Derived_Type)
7089            and then Constraint_Present
7090          then
7091             Set_Stored_Constraint
7092               (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
7093          end if;
7094
7095          if Ada_Version >= Ada_05 then
7096             declare
7097                Ifaces_List : Elist_Id;
7098
7099             begin
7100                --  Checks rules 3.9.4 (13/2 and 14/2)
7101
7102                if Comes_From_Source (Derived_Type)
7103                  and then not Is_Private_Type (Derived_Type)
7104                  and then Is_Interface (Parent_Type)
7105                  and then not Is_Interface (Derived_Type)
7106                then
7107                   if Is_Task_Interface (Parent_Type) then
7108                      Error_Msg_N
7109                        ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
7110                         Derived_Type);
7111
7112                   elsif Is_Protected_Interface (Parent_Type) then
7113                      Error_Msg_N
7114                        ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
7115                         Derived_Type);
7116                   end if;
7117                end if;
7118
7119                --  Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
7120
7121                Check_Interfaces (N, Type_Def);
7122
7123                --  Ada 2005 (AI-251): Collect the list of progenitors that are
7124                --  not already in the parents.
7125
7126                Collect_Interfaces
7127                  (T               => Derived_Type,
7128                   Ifaces_List     => Ifaces_List,
7129                   Exclude_Parents => True);
7130
7131                Set_Interfaces (Derived_Type, Ifaces_List);
7132             end;
7133          end if;
7134
7135       else
7136          Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
7137          Set_Has_Non_Standard_Rep
7138                        (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7139       end if;
7140
7141       --  STEP 4: Inherit components from the parent base and constrain them.
7142       --          Apply the second transformation described in point 6. above.
7143
7144       if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
7145         or else not Has_Discriminants (Parent_Type)
7146         or else not Is_Constrained (Parent_Type)
7147       then
7148          Constrs := Discs;
7149       else
7150          Constrs := Discriminant_Constraint (Parent_Type);
7151       end if;
7152
7153       Assoc_List :=
7154         Inherit_Components
7155           (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
7156
7157       --  STEP 5a: Copy the parent record declaration for untagged types
7158
7159       if not Is_Tagged then
7160
7161          --  Discriminant_Constraint (Derived_Type) has been properly
7162          --  constructed. Save it and temporarily set it to Empty because we
7163          --  do not want the call to New_Copy_Tree below to mess this list.
7164
7165          if Has_Discriminants (Derived_Type) then
7166             Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
7167             Set_Discriminant_Constraint (Derived_Type, No_Elist);
7168          else
7169             Save_Discr_Constr := No_Elist;
7170          end if;
7171
7172          --  Save the Etype field of Derived_Type. It is correctly set now,
7173          --  but the call to New_Copy tree may remap it to point to itself,
7174          --  which is not what we want. Ditto for the Next_Entity field.
7175
7176          Save_Etype       := Etype (Derived_Type);
7177          Save_Next_Entity := Next_Entity (Derived_Type);
7178
7179          --  Assoc_List maps all stored discriminants in the Parent_Base to
7180          --  stored discriminants in the Derived_Type. It is fundamental that
7181          --  no types or itypes with discriminants other than the stored
7182          --  discriminants appear in the entities declared inside
7183          --  Derived_Type, since the back end cannot deal with it.
7184
7185          New_Decl :=
7186            New_Copy_Tree
7187              (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
7188
7189          --  Restore the fields saved prior to the New_Copy_Tree call
7190          --  and compute the stored constraint.
7191
7192          Set_Etype       (Derived_Type, Save_Etype);
7193          Set_Next_Entity (Derived_Type, Save_Next_Entity);
7194
7195          if Has_Discriminants (Derived_Type) then
7196             Set_Discriminant_Constraint
7197               (Derived_Type, Save_Discr_Constr);
7198             Set_Stored_Constraint
7199               (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
7200             Replace_Components (Derived_Type, New_Decl);
7201          end if;
7202
7203          --  Insert the new derived type declaration
7204
7205          Rewrite (N, New_Decl);
7206
7207       --  STEP 5b: Complete the processing for record extensions in generics
7208
7209       --  There is no completion for record extensions declared in the
7210       --  parameter part of a generic, so we need to complete processing for
7211       --  these generic record extensions here. The Record_Type_Definition call
7212       --  will change the Ekind of the components from E_Void to E_Component.
7213
7214       elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
7215          Record_Type_Definition (Empty, Derived_Type);
7216
7217       --  STEP 5c: Process the record extension for non private tagged types
7218
7219       elsif not Private_Extension then
7220
7221          --  Add the _parent field in the derived type
7222
7223          Expand_Record_Extension (Derived_Type, Type_Def);
7224
7225          --  Ada 2005 (AI-251): Addition of the Tag corresponding to all the
7226          --  implemented interfaces if we are in expansion mode
7227
7228          if Expander_Active
7229            and then Has_Interfaces (Derived_Type)
7230          then
7231             Add_Interface_Tag_Components (N, Derived_Type);
7232          end if;
7233
7234          --  Analyze the record extension
7235
7236          Record_Type_Definition
7237            (Record_Extension_Part (Type_Def), Derived_Type);
7238       end if;
7239
7240       End_Scope;
7241
7242       --  Nothing else to do if there is an error in the derivation.
7243       --  An unusual case: the full view may be derived from a type in an
7244       --  instance, when the partial view was used illegally as an actual
7245       --  in that instance, leading to a circular definition.
7246
7247       if Etype (Derived_Type) = Any_Type
7248         or else Etype (Parent_Type) = Derived_Type
7249       then
7250          return;
7251       end if;
7252
7253       --  Set delayed freeze and then derive subprograms, we need to do
7254       --  this in this order so that derived subprograms inherit the
7255       --  derived freeze if necessary.
7256
7257       Set_Has_Delayed_Freeze (Derived_Type);
7258
7259       if Derive_Subps then
7260          Derive_Subprograms (Parent_Type, Derived_Type);
7261       end if;
7262
7263       --  If we have a private extension which defines a constrained derived
7264       --  type mark as constrained here after we have derived subprograms. See
7265       --  comment on point 9. just above the body of Build_Derived_Record_Type.
7266
7267       if Private_Extension and then Inherit_Discrims then
7268          if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
7269             Set_Is_Constrained          (Derived_Type, True);
7270             Set_Discriminant_Constraint (Derived_Type, Discs);
7271
7272          elsif Is_Constrained (Parent_Type) then
7273             Set_Is_Constrained
7274               (Derived_Type, True);
7275             Set_Discriminant_Constraint
7276               (Derived_Type, Discriminant_Constraint (Parent_Type));
7277          end if;
7278       end if;
7279
7280       --  Update the class-wide type, which shares the now-completed entity
7281       --  list with its specific type. In case of underlying record views,
7282       --  we do not generate the corresponding class wide entity.
7283
7284       if Is_Tagged
7285         and then not Is_Underlying_Record_View (Derived_Type)
7286       then
7287          Set_First_Entity
7288            (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
7289          Set_Last_Entity
7290            (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
7291       end if;
7292
7293       --  Update the scope of anonymous access types of discriminants and other
7294       --  components, to prevent scope anomalies in gigi, when the derivation
7295       --  appears in a scope nested within that of the parent.
7296
7297       declare
7298          D : Entity_Id;
7299
7300       begin
7301          D := First_Entity (Derived_Type);
7302          while Present (D) loop
7303             if Ekind (D) = E_Discriminant
7304               or else Ekind (D) = E_Component
7305             then
7306                if Is_Itype (Etype (D))
7307                   and then Ekind (Etype (D)) = E_Anonymous_Access_Type
7308                then
7309                   Set_Scope (Etype (D), Current_Scope);
7310                end if;
7311             end if;
7312
7313             Next_Entity (D);
7314          end loop;
7315       end;
7316    end Build_Derived_Record_Type;
7317
7318    ------------------------
7319    -- Build_Derived_Type --
7320    ------------------------
7321
7322    procedure Build_Derived_Type
7323      (N             : Node_Id;
7324       Parent_Type   : Entity_Id;
7325       Derived_Type  : Entity_Id;
7326       Is_Completion : Boolean;
7327       Derive_Subps  : Boolean := True)
7328    is
7329       Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7330
7331    begin
7332       --  Set common attributes
7333
7334       Set_Scope         (Derived_Type, Current_Scope);
7335
7336       Set_Ekind         (Derived_Type, Ekind    (Parent_Base));
7337       Set_Etype         (Derived_Type,           Parent_Base);
7338       Set_Has_Task      (Derived_Type, Has_Task (Parent_Base));
7339
7340       Set_Size_Info      (Derived_Type,                 Parent_Type);
7341       Set_RM_Size        (Derived_Type, RM_Size        (Parent_Type));
7342       Set_Convention     (Derived_Type, Convention     (Parent_Type));
7343       Set_Is_Controlled  (Derived_Type, Is_Controlled  (Parent_Type));
7344       Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
7345
7346       --  The derived type inherits the representation clauses of the parent.
7347       --  However, for a private type that is completed by a derivation, there
7348       --  may be operation attributes that have been specified already (stream
7349       --  attributes and External_Tag) and those must be provided. Finally,
7350       --  if the partial view is a private extension, the representation items
7351       --  of the parent have been inherited already, and should not be chained
7352       --  twice to the derived type.
7353
7354       if Is_Tagged_Type (Parent_Type)
7355         and then Present (First_Rep_Item (Derived_Type))
7356       then
7357          --  The existing items are either operational items or items inherited
7358          --  from a private extension declaration.
7359
7360          declare
7361             Rep : Node_Id;
7362             --  Used to iterate over representation items of the derived type
7363
7364             Last_Rep : Node_Id;
7365             --  Last representation item of the (non-empty) representation
7366             --  item list of the derived type.
7367
7368             Found : Boolean := False;
7369
7370          begin
7371             Rep      := First_Rep_Item (Derived_Type);
7372             Last_Rep := Rep;
7373             while Present (Rep) loop
7374                if Rep = First_Rep_Item (Parent_Type) then
7375                   Found := True;
7376                   exit;
7377
7378                else
7379                   Rep := Next_Rep_Item (Rep);
7380
7381                   if Present (Rep) then
7382                      Last_Rep := Rep;
7383                   end if;
7384                end if;
7385             end loop;
7386
7387             --  Here if we either encountered the parent type's first rep
7388             --  item on the derived type's rep item list (in which case
7389             --  Found is True, and we have nothing else to do), or if we
7390             --  reached the last rep item of the derived type, which is
7391             --  Last_Rep, in which case we further chain the parent type's
7392             --  rep items to those of the derived type.
7393
7394             if not Found then
7395                Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
7396             end if;
7397          end;
7398
7399       else
7400          Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
7401       end if;
7402
7403       case Ekind (Parent_Type) is
7404          when Numeric_Kind =>
7405             Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
7406
7407          when Array_Kind =>
7408             Build_Derived_Array_Type (N, Parent_Type,  Derived_Type);
7409
7410          when E_Record_Type
7411             | E_Record_Subtype
7412             | Class_Wide_Kind  =>
7413             Build_Derived_Record_Type
7414               (N, Parent_Type, Derived_Type, Derive_Subps);
7415             return;
7416
7417          when Enumeration_Kind =>
7418             Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
7419
7420          when Access_Kind =>
7421             Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
7422
7423          when Incomplete_Or_Private_Kind =>
7424             Build_Derived_Private_Type
7425               (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
7426
7427             --  For discriminated types, the derivation includes deriving
7428             --  primitive operations. For others it is done below.
7429
7430             if Is_Tagged_Type (Parent_Type)
7431               or else Has_Discriminants (Parent_Type)
7432               or else (Present (Full_View (Parent_Type))
7433                         and then Has_Discriminants (Full_View (Parent_Type)))
7434             then
7435                return;
7436             end if;
7437
7438          when Concurrent_Kind =>
7439             Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
7440
7441          when others =>
7442             raise Program_Error;
7443       end case;
7444
7445       if Etype (Derived_Type) = Any_Type then
7446          return;
7447       end if;
7448
7449       --  Set delayed freeze and then derive subprograms, we need to do this
7450       --  in this order so that derived subprograms inherit the derived freeze
7451       --  if necessary.
7452
7453       Set_Has_Delayed_Freeze (Derived_Type);
7454       if Derive_Subps then
7455          Derive_Subprograms (Parent_Type, Derived_Type);
7456       end if;
7457
7458       Set_Has_Primitive_Operations
7459         (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
7460    end Build_Derived_Type;
7461
7462    -----------------------
7463    -- Build_Discriminal --
7464    -----------------------
7465
7466    procedure Build_Discriminal (Discrim : Entity_Id) is
7467       D_Minal : Entity_Id;
7468       CR_Disc : Entity_Id;
7469
7470    begin
7471       --  A discriminal has the same name as the discriminant
7472
7473       D_Minal :=
7474         Make_Defining_Identifier (Sloc (Discrim),
7475           Chars => Chars (Discrim));
7476
7477       Set_Ekind     (D_Minal, E_In_Parameter);
7478       Set_Mechanism (D_Minal, Default_Mechanism);
7479       Set_Etype     (D_Minal, Etype (Discrim));
7480
7481       Set_Discriminal (Discrim, D_Minal);
7482       Set_Discriminal_Link (D_Minal, Discrim);
7483
7484       --  For task types, build at once the discriminants of the corresponding
7485       --  record, which are needed if discriminants are used in entry defaults
7486       --  and in family bounds.
7487
7488       if Is_Concurrent_Type (Current_Scope)
7489         or else Is_Limited_Type (Current_Scope)
7490       then
7491          CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
7492
7493          Set_Ekind            (CR_Disc, E_In_Parameter);
7494          Set_Mechanism        (CR_Disc, Default_Mechanism);
7495          Set_Etype            (CR_Disc, Etype (Discrim));
7496          Set_Discriminal_Link (CR_Disc, Discrim);
7497          Set_CR_Discriminant  (Discrim, CR_Disc);
7498       end if;
7499    end Build_Discriminal;
7500
7501    ------------------------------------
7502    -- Build_Discriminant_Constraints --
7503    ------------------------------------
7504
7505    function Build_Discriminant_Constraints
7506      (T           : Entity_Id;
7507       Def         : Node_Id;
7508       Derived_Def : Boolean := False) return Elist_Id
7509    is
7510       C        : constant Node_Id := Constraint (Def);
7511       Nb_Discr : constant Nat     := Number_Discriminants (T);
7512
7513       Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
7514       --  Saves the expression corresponding to a given discriminant in T
7515
7516       function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
7517       --  Return the Position number within array Discr_Expr of a discriminant
7518       --  D within the discriminant list of the discriminated type T.
7519
7520       ------------------
7521       -- Pos_Of_Discr --
7522       ------------------
7523
7524       function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
7525          Disc : Entity_Id;
7526
7527       begin
7528          Disc := First_Discriminant (T);
7529          for J in Discr_Expr'Range loop
7530             if Disc = D then
7531                return J;
7532             end if;
7533
7534             Next_Discriminant (Disc);
7535          end loop;
7536
7537          --  Note: Since this function is called on discriminants that are
7538          --  known to belong to the discriminated type, falling through the
7539          --  loop with no match signals an internal compiler error.
7540
7541          raise Program_Error;
7542       end Pos_Of_Discr;
7543
7544       --  Declarations local to Build_Discriminant_Constraints
7545
7546       Discr : Entity_Id;
7547       E     : Entity_Id;
7548       Elist : constant Elist_Id := New_Elmt_List;
7549
7550       Constr   : Node_Id;
7551       Expr     : Node_Id;
7552       Id       : Node_Id;
7553       Position : Nat;
7554       Found    : Boolean;
7555
7556       Discrim_Present : Boolean := False;
7557
7558    --  Start of processing for Build_Discriminant_Constraints
7559
7560    begin
7561       --  The following loop will process positional associations only.
7562       --  For a positional association, the (single) discriminant is
7563       --  implicitly specified by position, in textual order (RM 3.7.2).
7564
7565       Discr  := First_Discriminant (T);
7566       Constr := First (Constraints (C));
7567       for D in Discr_Expr'Range loop
7568          exit when Nkind (Constr) = N_Discriminant_Association;
7569
7570          if No (Constr) then
7571             Error_Msg_N ("too few discriminants given in constraint", C);
7572             return New_Elmt_List;
7573
7574          elsif Nkind (Constr) = N_Range
7575            or else (Nkind (Constr) = N_Attribute_Reference
7576                      and then
7577                     Attribute_Name (Constr) = Name_Range)
7578          then
7579             Error_Msg_N
7580               ("a range is not a valid discriminant constraint", Constr);
7581             Discr_Expr (D) := Error;
7582
7583          else
7584             Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
7585             Discr_Expr (D) := Constr;
7586          end if;
7587
7588          Next_Discriminant (Discr);
7589          Next (Constr);
7590       end loop;
7591
7592       if No (Discr) and then Present (Constr) then
7593          Error_Msg_N ("too many discriminants given in constraint", Constr);
7594          return New_Elmt_List;
7595       end if;
7596
7597       --  Named associations can be given in any order, but if both positional
7598       --  and named associations are used in the same discriminant constraint,
7599       --  then positional associations must occur first, at their normal
7600       --  position. Hence once a named association is used, the rest of the
7601       --  discriminant constraint must use only named associations.
7602
7603       while Present (Constr) loop
7604
7605          --  Positional association forbidden after a named association
7606
7607          if Nkind (Constr) /= N_Discriminant_Association then
7608             Error_Msg_N ("positional association follows named one", Constr);
7609             return New_Elmt_List;
7610
7611          --  Otherwise it is a named association
7612
7613          else
7614             --  E records the type of the discriminants in the named
7615             --  association. All the discriminants specified in the same name
7616             --  association must have the same type.
7617
7618             E := Empty;
7619
7620             --  Search the list of discriminants in T to see if the simple name
7621             --  given in the constraint matches any of them.
7622
7623             Id := First (Selector_Names (Constr));
7624             while Present (Id) loop
7625                Found := False;
7626
7627                --  If Original_Discriminant is present, we are processing a
7628                --  generic instantiation and this is an instance node. We need
7629                --  to find the name of the corresponding discriminant in the
7630                --  actual record type T and not the name of the discriminant in
7631                --  the generic formal. Example:
7632
7633                --    generic
7634                --       type G (D : int) is private;
7635                --    package P is
7636                --       subtype W is G (D => 1);
7637                --    end package;
7638                --    type Rec (X : int) is record ... end record;
7639                --    package Q is new P (G => Rec);
7640
7641                --  At the point of the instantiation, formal type G is Rec
7642                --  and therefore when reanalyzing "subtype W is G (D => 1);"
7643                --  which really looks like "subtype W is Rec (D => 1);" at
7644                --  the point of instantiation, we want to find the discriminant
7645                --  that corresponds to D in Rec, i.e. X.
7646
7647                if Present (Original_Discriminant (Id)) then
7648                   Discr := Find_Corresponding_Discriminant (Id, T);
7649                   Found := True;
7650
7651                else
7652                   Discr := First_Discriminant (T);
7653                   while Present (Discr) loop
7654                      if Chars (Discr) = Chars (Id) then
7655                         Found := True;
7656                         exit;
7657                      end if;
7658
7659                      Next_Discriminant (Discr);
7660                   end loop;
7661
7662                   if not Found then
7663                      Error_Msg_N ("& does not match any discriminant", Id);
7664                      return New_Elmt_List;
7665
7666                   --  The following is only useful for the benefit of generic
7667                   --  instances but it does not interfere with other
7668                   --  processing for the non-generic case so we do it in all
7669                   --  cases (for generics this statement is executed when
7670                   --  processing the generic definition, see comment at the
7671                   --  beginning of this if statement).
7672
7673                   else
7674                      Set_Original_Discriminant (Id, Discr);
7675                   end if;
7676                end if;
7677
7678                Position := Pos_Of_Discr (T, Discr);
7679
7680                if Present (Discr_Expr (Position)) then
7681                   Error_Msg_N ("duplicate constraint for discriminant&", Id);
7682
7683                else
7684                   --  Each discriminant specified in the same named association
7685                   --  must be associated with a separate copy of the
7686                   --  corresponding expression.
7687
7688                   if Present (Next (Id)) then
7689                      Expr := New_Copy_Tree (Expression (Constr));
7690                      Set_Parent (Expr, Parent (Expression (Constr)));
7691                   else
7692                      Expr := Expression (Constr);
7693                   end if;
7694
7695                   Discr_Expr (Position) := Expr;
7696                   Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
7697                end if;
7698
7699                --  A discriminant association with more than one discriminant
7700                --  name is only allowed if the named discriminants are all of
7701                --  the same type (RM 3.7.1(8)).
7702
7703                if E = Empty then
7704                   E := Base_Type (Etype (Discr));
7705
7706                elsif Base_Type (Etype (Discr)) /= E then
7707                   Error_Msg_N
7708                     ("all discriminants in an association " &
7709                      "must have the same type", Id);
7710                end if;
7711
7712                Next (Id);
7713             end loop;
7714          end if;
7715
7716          Next (Constr);
7717       end loop;
7718
7719       --  A discriminant constraint must provide exactly one value for each
7720       --  discriminant of the type (RM 3.7.1(8)).
7721
7722       for J in Discr_Expr'Range loop
7723          if No (Discr_Expr (J)) then
7724             Error_Msg_N ("too few discriminants given in constraint", C);
7725             return New_Elmt_List;
7726          end if;
7727       end loop;
7728
7729       --  Determine if there are discriminant expressions in the constraint
7730
7731       for J in Discr_Expr'Range loop
7732          if Denotes_Discriminant
7733               (Discr_Expr (J), Check_Concurrent => True)
7734          then
7735             Discrim_Present := True;
7736          end if;
7737       end loop;
7738
7739       --  Build an element list consisting of the expressions given in the
7740       --  discriminant constraint and apply the appropriate checks. The list
7741       --  is constructed after resolving any named discriminant associations
7742       --  and therefore the expressions appear in the textual order of the
7743       --  discriminants.
7744
7745       Discr := First_Discriminant (T);
7746       for J in Discr_Expr'Range loop
7747          if Discr_Expr (J) /= Error then
7748             Append_Elmt (Discr_Expr (J), Elist);
7749
7750             --  If any of the discriminant constraints is given by a
7751             --  discriminant and we are in a derived type declaration we
7752             --  have a discriminant renaming. Establish link between new
7753             --  and old discriminant.
7754
7755             if Denotes_Discriminant (Discr_Expr (J)) then
7756                if Derived_Def then
7757                   Set_Corresponding_Discriminant
7758                     (Entity (Discr_Expr (J)), Discr);
7759                end if;
7760
7761             --  Force the evaluation of non-discriminant expressions.
7762             --  If we have found a discriminant in the constraint 3.4(26)
7763             --  and 3.8(18) demand that no range checks are performed are
7764             --  after evaluation. If the constraint is for a component
7765             --  definition that has a per-object constraint, expressions are
7766             --  evaluated but not checked either. In all other cases perform
7767             --  a range check.
7768
7769             else
7770                if Discrim_Present then
7771                   null;
7772
7773                elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
7774                  and then
7775                    Has_Per_Object_Constraint
7776                      (Defining_Identifier (Parent (Parent (Def))))
7777                then
7778                   null;
7779
7780                elsif Is_Access_Type (Etype (Discr)) then
7781                   Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
7782
7783                else
7784                   Apply_Range_Check (Discr_Expr (J), Etype (Discr));
7785                end if;
7786
7787                Force_Evaluation (Discr_Expr (J));
7788             end if;
7789
7790             --  Check that the designated type of an access discriminant's
7791             --  expression is not a class-wide type unless the discriminant's
7792             --  designated type is also class-wide.
7793
7794             if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
7795               and then not Is_Class_Wide_Type
7796                          (Designated_Type (Etype (Discr)))
7797               and then Etype (Discr_Expr (J)) /= Any_Type
7798               and then Is_Class_Wide_Type
7799                          (Designated_Type (Etype (Discr_Expr (J))))
7800             then
7801                Wrong_Type (Discr_Expr (J), Etype (Discr));
7802
7803             elsif Is_Access_Type (Etype (Discr))
7804               and then not Is_Access_Constant (Etype (Discr))
7805               and then Is_Access_Type (Etype (Discr_Expr (J)))
7806               and then Is_Access_Constant (Etype (Discr_Expr (J)))
7807             then
7808                Error_Msg_NE
7809                  ("constraint for discriminant& must be access to variable",
7810                     Def, Discr);
7811             end if;
7812          end if;
7813
7814          Next_Discriminant (Discr);
7815       end loop;
7816
7817       return Elist;
7818    end Build_Discriminant_Constraints;
7819
7820    ---------------------------------
7821    -- Build_Discriminated_Subtype --
7822    ---------------------------------
7823
7824    procedure Build_Discriminated_Subtype
7825      (T           : Entity_Id;
7826       Def_Id      : Entity_Id;
7827       Elist       : Elist_Id;
7828       Related_Nod : Node_Id;
7829       For_Access  : Boolean := False)
7830    is
7831       Has_Discrs  : constant Boolean := Has_Discriminants (T);
7832       Constrained : constant Boolean :=
7833                       (Has_Discrs
7834                          and then not Is_Empty_Elmt_List (Elist)
7835                          and then not Is_Class_Wide_Type (T))
7836                         or else Is_Constrained (T);
7837
7838    begin
7839       if Ekind (T) = E_Record_Type then
7840          if For_Access then
7841             Set_Ekind (Def_Id, E_Private_Subtype);
7842             Set_Is_For_Access_Subtype (Def_Id, True);
7843          else
7844             Set_Ekind (Def_Id, E_Record_Subtype);
7845          end if;
7846
7847          --  Inherit preelaboration flag from base, for types for which it
7848          --  may have been set: records, private types, protected types.
7849
7850          Set_Known_To_Have_Preelab_Init
7851            (Def_Id, Known_To_Have_Preelab_Init (T));
7852
7853       elsif Ekind (T) = E_Task_Type then
7854          Set_Ekind (Def_Id, E_Task_Subtype);
7855
7856       elsif Ekind (T) = E_Protected_Type then
7857          Set_Ekind (Def_Id, E_Protected_Subtype);
7858          Set_Known_To_Have_Preelab_Init
7859            (Def_Id, Known_To_Have_Preelab_Init (T));
7860
7861       elsif Is_Private_Type (T) then
7862          Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
7863          Set_Known_To_Have_Preelab_Init
7864            (Def_Id, Known_To_Have_Preelab_Init (T));
7865
7866       elsif Is_Class_Wide_Type (T) then
7867          Set_Ekind (Def_Id, E_Class_Wide_Subtype);
7868
7869       else
7870          --  Incomplete type. Attach subtype to list of dependents, to be
7871          --  completed with full view of parent type,  unless is it the
7872          --  designated subtype of a record component within an init_proc.
7873          --  This last case arises for a component of an access type whose
7874          --  designated type is incomplete (e.g. a Taft Amendment type).
7875          --  The designated subtype is within an inner scope, and needs no
7876          --  elaboration, because only the access type is needed in the
7877          --  initialization procedure.
7878
7879          Set_Ekind (Def_Id, Ekind (T));
7880
7881          if For_Access and then Within_Init_Proc then
7882             null;
7883          else
7884             Append_Elmt (Def_Id, Private_Dependents (T));
7885          end if;
7886       end if;
7887
7888       Set_Etype             (Def_Id, T);
7889       Init_Size_Align       (Def_Id);
7890       Set_Has_Discriminants (Def_Id, Has_Discrs);
7891       Set_Is_Constrained    (Def_Id, Constrained);
7892
7893       Set_First_Entity      (Def_Id, First_Entity   (T));
7894       Set_Last_Entity       (Def_Id, Last_Entity    (T));
7895
7896       --  If the subtype is the completion of a private declaration, there may
7897       --  have been representation clauses for the partial view, and they must
7898       --  be preserved. Build_Derived_Type chains the inherited clauses with
7899       --  the ones appearing on the extension. If this comes from a subtype
7900       --  declaration, all clauses are inherited.
7901
7902       if No (First_Rep_Item (Def_Id)) then
7903          Set_First_Rep_Item    (Def_Id, First_Rep_Item (T));
7904       end if;
7905
7906       if Is_Tagged_Type (T) then
7907          Set_Is_Tagged_Type  (Def_Id);
7908          Make_Class_Wide_Type (Def_Id);
7909       end if;
7910
7911       Set_Stored_Constraint (Def_Id, No_Elist);
7912
7913       if Has_Discrs then
7914          Set_Discriminant_Constraint (Def_Id, Elist);
7915          Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
7916       end if;
7917
7918       if Is_Tagged_Type (T) then
7919
7920          --  Ada 2005 (AI-251): In case of concurrent types we inherit the
7921          --  concurrent record type (which has the list of primitive
7922          --  operations).
7923
7924          if Ada_Version >= Ada_05
7925            and then Is_Concurrent_Type (T)
7926          then
7927             Set_Corresponding_Record_Type (Def_Id,
7928                Corresponding_Record_Type (T));
7929          else
7930             Set_Primitive_Operations (Def_Id, Primitive_Operations (T));
7931          end if;
7932
7933          Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
7934       end if;
7935
7936       --  Subtypes introduced by component declarations do not need to be
7937       --  marked as delayed, and do not get freeze nodes, because the semantics
7938       --  verifies that the parents of the subtypes are frozen before the
7939       --  enclosing record is frozen.
7940
7941       if not Is_Type (Scope (Def_Id)) then
7942          Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
7943
7944          if Is_Private_Type (T)
7945            and then Present (Full_View (T))
7946          then
7947             Conditional_Delay (Def_Id, Full_View (T));
7948          else
7949             Conditional_Delay (Def_Id, T);
7950          end if;
7951       end if;
7952
7953       if Is_Record_Type (T) then
7954          Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
7955
7956          if Has_Discrs
7957             and then not Is_Empty_Elmt_List (Elist)
7958             and then not For_Access
7959          then
7960             Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
7961          elsif not For_Access then
7962             Set_Cloned_Subtype (Def_Id, T);
7963          end if;
7964       end if;
7965    end Build_Discriminated_Subtype;
7966
7967    ---------------------------
7968    -- Build_Itype_Reference --
7969    ---------------------------
7970
7971    procedure Build_Itype_Reference
7972      (Ityp : Entity_Id;
7973       Nod  : Node_Id)
7974    is
7975       IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
7976    begin
7977       Set_Itype (IR, Ityp);
7978       Insert_After (Nod, IR);
7979    end Build_Itype_Reference;
7980
7981    ------------------------
7982    -- Build_Scalar_Bound --
7983    ------------------------
7984
7985    function Build_Scalar_Bound
7986      (Bound : Node_Id;
7987       Par_T : Entity_Id;
7988       Der_T : Entity_Id) return Node_Id
7989    is
7990       New_Bound : Entity_Id;
7991
7992    begin
7993       --  Note: not clear why this is needed, how can the original bound
7994       --  be unanalyzed at this point? and if it is, what business do we
7995       --  have messing around with it? and why is the base type of the
7996       --  parent type the right type for the resolution. It probably is
7997       --  not! It is OK for the new bound we are creating, but not for
7998       --  the old one??? Still if it never happens, no problem!
7999
8000       Analyze_And_Resolve (Bound, Base_Type (Par_T));
8001
8002       if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
8003          New_Bound := New_Copy (Bound);
8004          Set_Etype (New_Bound, Der_T);
8005          Set_Analyzed (New_Bound);
8006
8007       elsif Is_Entity_Name (Bound) then
8008          New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
8009
8010       --  The following is almost certainly wrong. What business do we have
8011       --  relocating a node (Bound) that is presumably still attached to
8012       --  the tree elsewhere???
8013
8014       else
8015          New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
8016       end if;
8017
8018       Set_Etype (New_Bound, Der_T);
8019       return New_Bound;
8020    end Build_Scalar_Bound;
8021
8022    --------------------------------
8023    -- Build_Underlying_Full_View --
8024    --------------------------------
8025
8026    procedure Build_Underlying_Full_View
8027      (N   : Node_Id;
8028       Typ : Entity_Id;
8029       Par : Entity_Id)
8030    is
8031       Loc  : constant Source_Ptr := Sloc (N);
8032       Subt : constant Entity_Id :=
8033                Make_Defining_Identifier
8034                  (Loc, New_External_Name (Chars (Typ), 'S'));
8035
8036       Constr : Node_Id;
8037       Indic  : Node_Id;
8038       C      : Node_Id;
8039       Id     : Node_Id;
8040
8041       procedure Set_Discriminant_Name (Id : Node_Id);
8042       --  If the derived type has discriminants, they may rename discriminants
8043       --  of the parent. When building the full view of the parent, we need to
8044       --  recover the names of the original discriminants if the constraint is
8045       --  given by named associations.
8046
8047       ---------------------------
8048       -- Set_Discriminant_Name --
8049       ---------------------------
8050
8051       procedure Set_Discriminant_Name (Id : Node_Id) is
8052          Disc : Entity_Id;
8053
8054       begin
8055          Set_Original_Discriminant (Id, Empty);
8056
8057          if Has_Discriminants (Typ) then
8058             Disc := First_Discriminant (Typ);
8059             while Present (Disc) loop
8060                if Chars (Disc) = Chars (Id)
8061                  and then Present (Corresponding_Discriminant (Disc))
8062                then
8063                   Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
8064                end if;
8065                Next_Discriminant (Disc);
8066             end loop;
8067          end if;
8068       end Set_Discriminant_Name;
8069
8070    --  Start of processing for Build_Underlying_Full_View
8071
8072    begin
8073       if Nkind (N) = N_Full_Type_Declaration then
8074          Constr := Constraint (Subtype_Indication (Type_Definition (N)));
8075
8076       elsif Nkind (N) = N_Subtype_Declaration then
8077          Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
8078
8079       elsif Nkind (N) = N_Component_Declaration then
8080          Constr :=
8081            New_Copy_Tree
8082              (Constraint (Subtype_Indication (Component_Definition (N))));
8083
8084       else
8085          raise Program_Error;
8086       end if;
8087
8088       C := First (Constraints (Constr));
8089       while Present (C) loop
8090          if Nkind (C) = N_Discriminant_Association then
8091             Id := First (Selector_Names (C));
8092             while Present (Id) loop
8093                Set_Discriminant_Name (Id);
8094                Next (Id);
8095             end loop;
8096          end if;
8097
8098          Next (C);
8099       end loop;
8100
8101       Indic :=
8102         Make_Subtype_Declaration (Loc,
8103           Defining_Identifier => Subt,
8104           Subtype_Indication  =>
8105             Make_Subtype_Indication (Loc,
8106               Subtype_Mark => New_Reference_To (Par, Loc),
8107               Constraint   => New_Copy_Tree (Constr)));
8108
8109       --  If this is a component subtype for an outer itype, it is not
8110       --  a list member, so simply set the parent link for analysis: if
8111       --  the enclosing type does not need to be in a declarative list,
8112       --  neither do the components.
8113
8114       if Is_List_Member (N)
8115         and then Nkind (N) /= N_Component_Declaration
8116       then
8117          Insert_Before (N, Indic);
8118       else
8119          Set_Parent (Indic, Parent (N));
8120       end if;
8121
8122       Analyze (Indic);
8123       Set_Underlying_Full_View (Typ, Full_View (Subt));
8124    end Build_Underlying_Full_View;
8125
8126    -------------------------------
8127    -- Check_Abstract_Overriding --
8128    -------------------------------
8129
8130    procedure Check_Abstract_Overriding (T : Entity_Id) is
8131       Alias_Subp : Entity_Id;
8132       Elmt       : Elmt_Id;
8133       Op_List    : Elist_Id;
8134       Subp       : Entity_Id;
8135       Type_Def   : Node_Id;
8136
8137    begin
8138       Op_List := Primitive_Operations (T);
8139
8140       --  Loop to check primitive operations
8141
8142       Elmt := First_Elmt (Op_List);
8143       while Present (Elmt) loop
8144          Subp := Node (Elmt);
8145          Alias_Subp := Alias (Subp);
8146
8147          --  Inherited subprograms are identified by the fact that they do not
8148          --  come from source, and the associated source location is the
8149          --  location of the first subtype of the derived type.
8150
8151          --  Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
8152          --  subprograms that "require overriding".
8153
8154          --  Special exception, do not complain about failure to override the
8155          --  stream routines _Input and _Output, as well as the primitive
8156          --  operations used in dispatching selects since we always provide
8157          --  automatic overridings for these subprograms.
8158
8159          --  Also ignore this rule for convention CIL since .NET libraries
8160          --  do bizarre things with interfaces???
8161
8162          --  The partial view of T may have been a private extension, for
8163          --  which inherited functions dispatching on result are abstract.
8164          --  If the full view is a null extension, there is no need for
8165          --  overriding in Ada2005, but wrappers need to be built for them
8166          --  (see exp_ch3, Build_Controlling_Function_Wrappers).
8167
8168          if Is_Null_Extension (T)
8169            and then Has_Controlling_Result (Subp)
8170            and then Ada_Version >= Ada_05
8171            and then Present (Alias_Subp)
8172            and then not Comes_From_Source (Subp)
8173            and then not Is_Abstract_Subprogram (Alias_Subp)
8174            and then not Is_Access_Type (Etype (Subp))
8175          then
8176             null;
8177
8178          --  Ada 2005 (AI-251): Internal entities of interfaces need no
8179          --  processing because this check is done with the aliased
8180          --  entity
8181
8182          elsif Present (Interface_Alias (Subp)) then
8183             null;
8184
8185          elsif (Is_Abstract_Subprogram (Subp)
8186                  or else Requires_Overriding (Subp)
8187                  or else
8188                    (Has_Controlling_Result (Subp)
8189                      and then Present (Alias_Subp)
8190                      and then not Comes_From_Source (Subp)
8191                      and then Sloc (Subp) = Sloc (First_Subtype (T))))
8192            and then not Is_TSS (Subp, TSS_Stream_Input)
8193            and then not Is_TSS (Subp, TSS_Stream_Output)
8194            and then not Is_Abstract_Type (T)
8195            and then Convention (T) /= Convention_CIL
8196            and then not Is_Predefined_Interface_Primitive (Subp)
8197
8198             --  Ada 2005 (AI-251): Do not consider hidden entities associated
8199             --  with abstract interface types because the check will be done
8200             --  with the aliased entity (otherwise we generate a duplicated
8201             --  error message).
8202
8203            and then not Present (Interface_Alias (Subp))
8204          then
8205             if Present (Alias_Subp) then
8206
8207                --  Only perform the check for a derived subprogram when the
8208                --  type has an explicit record extension. This avoids incorrect
8209                --  flagging of abstract subprograms for the case of a type
8210                --  without an extension that is derived from a formal type
8211                --  with a tagged actual (can occur within a private part).
8212
8213                --  Ada 2005 (AI-391): In the case of an inherited function with
8214                --  a controlling result of the type, the rule does not apply if
8215                --  the type is a null extension (unless the parent function
8216                --  itself is abstract, in which case the function must still be
8217                --  be overridden). The expander will generate an overriding
8218                --  wrapper function calling the parent subprogram (see
8219                --  Exp_Ch3.Make_Controlling_Wrapper_Functions).
8220
8221                Type_Def := Type_Definition (Parent (T));
8222
8223                if Nkind (Type_Def) = N_Derived_Type_Definition
8224                  and then Present (Record_Extension_Part (Type_Def))
8225                  and then
8226                    (Ada_Version < Ada_05
8227                       or else not Is_Null_Extension (T)
8228                       or else Ekind (Subp) = E_Procedure
8229                       or else not Has_Controlling_Result (Subp)
8230                       or else Is_Abstract_Subprogram (Alias_Subp)
8231                       or else Requires_Overriding (Subp)
8232                       or else Is_Access_Type (Etype (Subp)))
8233                then
8234                   --  Avoid reporting error in case of abstract predefined
8235                   --  primitive inherited from interface type because the
8236                   --  body of internally generated predefined primitives
8237                   --  of tagged types are generated later by Freeze_Type
8238
8239                   if Is_Interface (Root_Type (T))
8240                     and then Is_Abstract_Subprogram (Subp)
8241                     and then Is_Predefined_Dispatching_Operation (Subp)
8242                     and then not Comes_From_Source (Ultimate_Alias (Subp))
8243                   then
8244                      null;
8245
8246                   else
8247                      Error_Msg_NE
8248                        ("type must be declared abstract or & overridden",
8249                         T, Subp);
8250
8251                      --  Traverse the whole chain of aliased subprograms to
8252                      --  complete the error notification. This is especially
8253                      --  useful for traceability of the chain of entities when
8254                      --  the subprogram corresponds with an interface
8255                      --  subprogram (which may be defined in another package).
8256
8257                      if Present (Alias_Subp) then
8258                         declare
8259                            E : Entity_Id;
8260
8261                         begin
8262                            E := Subp;
8263                            while Present (Alias (E)) loop
8264                               Error_Msg_Sloc := Sloc (E);
8265                               Error_Msg_NE
8266                                 ("\& has been inherited #", T, Subp);
8267                               E := Alias (E);
8268                            end loop;
8269
8270                            Error_Msg_Sloc := Sloc (E);
8271                            Error_Msg_NE
8272                              ("\& has been inherited from subprogram #",
8273                               T, Subp);
8274                         end;
8275                      end if;
8276                   end if;
8277
8278                --  Ada 2005 (AI-345): Protected or task type implementing
8279                --  abstract interfaces.
8280
8281                elsif Is_Concurrent_Record_Type (T)
8282                  and then Present (Interfaces (T))
8283                then
8284                   --  The controlling formal of Subp must be of mode "out",
8285                   --  "in out" or an access-to-variable to be overridden.
8286
8287                   --  Error message below needs rewording (remember comma
8288                   --  in -gnatj mode) ???
8289
8290                   if Ekind (First_Formal (Subp)) = E_In_Parameter
8291                     and then Ekind (Subp) /= E_Function
8292                   then
8293                      if not Is_Predefined_Dispatching_Operation (Subp) then
8294                         Error_Msg_NE
8295                           ("first formal of & must be of mode `OUT`, " &
8296                            "`IN OUT` or access-to-variable", T, Subp);
8297                         Error_Msg_N
8298                           ("\to be overridden by protected procedure or " &
8299                            "entry (RM 9.4(11.9/2))", T);
8300                      end if;
8301
8302                   --  Some other kind of overriding failure
8303
8304                   else
8305                      Error_Msg_NE
8306                        ("interface subprogram & must be overridden",
8307                         T, Subp);
8308
8309                      --  Examine primitive operations of synchronized type,
8310                      --  to find homonyms that have the wrong profile.
8311
8312                      declare
8313                         Prim : Entity_Id;
8314
8315                      begin
8316                         Prim :=
8317                           First_Entity (Corresponding_Concurrent_Type (T));
8318                         while Present (Prim) loop
8319                            if Chars (Prim) = Chars (Subp) then
8320                               Error_Msg_NE
8321                                 ("profile is not type conformant with "
8322                                    & "prefixed view profile of "
8323                                    & "inherited operation&", Prim, Subp);
8324                            end if;
8325
8326                            Next_Entity (Prim);
8327                         end loop;
8328                      end;
8329                   end if;
8330                end if;
8331
8332             else
8333                Error_Msg_Node_2 := T;
8334                Error_Msg_N
8335                  ("abstract subprogram& not allowed for type&", Subp);
8336
8337                --  Also post unconditional warning on the type (unconditional
8338                --  so that if there are more than one of these cases, we get
8339                --  them all, and not just the first one).
8340
8341                Error_Msg_Node_2 := Subp;
8342                Error_Msg_N
8343                  ("nonabstract type& has abstract subprogram&!", T);
8344             end if;
8345          end if;
8346
8347          --  Ada 2005 (AI05-0030): Inspect hidden subprograms which provide
8348          --  the mapping between interface and implementing type primitives.
8349          --  If the interface alias is marked as Implemented_By_Entry, the
8350          --  alias must be an entry wrapper.
8351
8352          if Ada_Version >= Ada_05
8353            and then Is_Hidden (Subp)
8354            and then Present (Interface_Alias (Subp))
8355            and then Implemented_By_Entry (Interface_Alias (Subp))
8356            and then Present (Alias_Subp)
8357            and then
8358              (not Is_Primitive_Wrapper (Alias_Subp)
8359                 or else Ekind (Wrapped_Entity (Alias_Subp)) /= E_Entry)
8360          then
8361             declare
8362                Error_Ent : Entity_Id := T;
8363
8364             begin
8365                if Is_Concurrent_Record_Type (Error_Ent) then
8366                   Error_Ent := Corresponding_Concurrent_Type (Error_Ent);
8367                end if;
8368
8369                Error_Msg_Node_2 := Interface_Alias (Subp);
8370                Error_Msg_NE
8371                  ("type & must implement abstract subprogram & with an entry",
8372                   Error_Ent, Error_Ent);
8373             end;
8374          end if;
8375
8376          Next_Elmt (Elmt);
8377       end loop;
8378    end Check_Abstract_Overriding;
8379
8380    ------------------------------------------------
8381    -- Check_Access_Discriminant_Requires_Limited --
8382    ------------------------------------------------
8383
8384    procedure Check_Access_Discriminant_Requires_Limited
8385      (D   : Node_Id;
8386       Loc : Node_Id)
8387    is
8388    begin
8389       --  A discriminant_specification for an access discriminant shall appear
8390       --  only in the declaration for a task or protected type, or for a type
8391       --  with the reserved word 'limited' in its definition or in one of its
8392       --  ancestors. (RM 3.7(10))
8393
8394       if Nkind (Discriminant_Type (D)) = N_Access_Definition
8395         and then not Is_Concurrent_Type (Current_Scope)
8396         and then not Is_Concurrent_Record_Type (Current_Scope)
8397         and then not Is_Limited_Record (Current_Scope)
8398         and then Ekind (Current_Scope) /= E_Limited_Private_Type
8399       then
8400          Error_Msg_N
8401            ("access discriminants allowed only for limited types", Loc);
8402       end if;
8403    end Check_Access_Discriminant_Requires_Limited;
8404
8405    -----------------------------------
8406    -- Check_Aliased_Component_Types --
8407    -----------------------------------
8408
8409    procedure Check_Aliased_Component_Types (T : Entity_Id) is
8410       C : Entity_Id;
8411
8412    begin
8413       --  ??? Also need to check components of record extensions, but not
8414       --  components of protected types (which are always limited).
8415
8416       --  Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
8417       --  types to be unconstrained. This is safe because it is illegal to
8418       --  create access subtypes to such types with explicit discriminant
8419       --  constraints.
8420
8421       if not Is_Limited_Type (T) then
8422          if Ekind (T) = E_Record_Type then
8423             C := First_Component (T);
8424             while Present (C) loop
8425                if Is_Aliased (C)
8426                  and then Has_Discriminants (Etype (C))
8427                  and then not Is_Constrained (Etype (C))
8428                  and then not In_Instance_Body
8429                  and then Ada_Version < Ada_05
8430                then
8431                   Error_Msg_N
8432                     ("aliased component must be constrained (RM 3.6(11))",
8433                       C);
8434                end if;
8435
8436                Next_Component (C);
8437             end loop;
8438
8439          elsif Ekind (T) = E_Array_Type then
8440             if Has_Aliased_Components (T)
8441               and then Has_Discriminants (Component_Type (T))
8442               and then not Is_Constrained (Component_Type (T))
8443               and then not In_Instance_Body
8444               and then Ada_Version < Ada_05
8445             then
8446                Error_Msg_N
8447                  ("aliased component type must be constrained (RM 3.6(11))",
8448                     T);
8449             end if;
8450          end if;
8451       end if;
8452    end Check_Aliased_Component_Types;
8453
8454    ----------------------
8455    -- Check_Completion --
8456    ----------------------
8457
8458    procedure Check_Completion (Body_Id : Node_Id := Empty) is
8459       E : Entity_Id;
8460
8461       procedure Post_Error;
8462       --  Post error message for lack of completion for entity E
8463
8464       ----------------
8465       -- Post_Error --
8466       ----------------
8467
8468       procedure Post_Error is
8469
8470          procedure Missing_Body;
8471          --  Output missing body message
8472
8473          ------------------
8474          -- Missing_Body --
8475          ------------------
8476
8477          procedure Missing_Body is
8478          begin
8479             --  Spec is in same unit, so we can post on spec
8480
8481             if In_Same_Source_Unit (Body_Id, E) then
8482                Error_Msg_N ("missing body for &", E);
8483
8484             --  Spec is in a separate unit, so we have to post on the body
8485
8486             else
8487                Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
8488             end if;
8489          end Missing_Body;
8490
8491       --  Start of processing for Post_Error
8492
8493       begin
8494          if not Comes_From_Source (E) then
8495
8496             if Ekind (E) = E_Task_Type
8497               or else Ekind (E) = E_Protected_Type
8498             then
8499                --  It may be an anonymous protected type created for a
8500                --  single variable. Post error on variable, if present.
8501
8502                declare
8503                   Var : Entity_Id;
8504
8505                begin
8506                   Var := First_Entity (Current_Scope);
8507                   while Present (Var) loop
8508                      exit when Etype (Var) = E
8509                        and then Comes_From_Source (Var);
8510
8511                      Next_Entity (Var);
8512                   end loop;
8513
8514                   if Present (Var) then
8515                      E := Var;
8516                   end if;
8517                end;
8518             end if;
8519          end if;
8520
8521          --  If a generated entity has no completion, then either previous
8522          --  semantic errors have disabled the expansion phase, or else we had
8523          --  missing subunits, or else we are compiling without expansion,
8524          --  or else something is very wrong.
8525
8526          if not Comes_From_Source (E) then
8527             pragma Assert
8528               (Serious_Errors_Detected > 0
8529                 or else Configurable_Run_Time_Violations > 0
8530                 or else Subunits_Missing
8531                 or else not Expander_Active);
8532             return;
8533
8534          --  Here for source entity
8535
8536          else
8537             --  Here if no body to post the error message, so we post the error
8538             --  on the declaration that has no completion. This is not really
8539             --  the right place to post it, think about this later ???
8540
8541             if No (Body_Id) then
8542                if Is_Type (E) then
8543                   Error_Msg_NE
8544                     ("missing full declaration for }", Parent (E), E);
8545                else
8546                   Error_Msg_NE
8547                     ("missing body for &", Parent (E), E);
8548                end if;
8549
8550             --  Package body has no completion for a declaration that appears
8551             --  in the corresponding spec. Post error on the body, with a
8552             --  reference to the non-completed declaration.
8553
8554             else
8555                Error_Msg_Sloc := Sloc (E);
8556
8557                if Is_Type (E) then
8558                   Error_Msg_NE
8559                     ("missing full declaration for }!", Body_Id, E);
8560
8561                elsif Is_Overloadable (E)
8562                  and then Current_Entity_In_Scope (E) /= E
8563                then
8564                   --  It may be that the completion is mistyped and appears as
8565                   --  a distinct overloading of the entity.
8566
8567                   declare
8568                      Candidate : constant Entity_Id :=
8569                                    Current_Entity_In_Scope (E);
8570                      Decl      : constant Node_Id :=
8571                                    Unit_Declaration_Node (Candidate);
8572
8573                   begin
8574                      if Is_Overloadable (Candidate)
8575                        and then Ekind (Candidate) = Ekind (E)
8576                        and then Nkind (Decl) = N_Subprogram_Body
8577                        and then Acts_As_Spec (Decl)
8578                      then
8579                         Check_Type_Conformant (Candidate, E);
8580
8581                      else
8582                         Missing_Body;
8583                      end if;
8584                   end;
8585
8586                else
8587                   Missing_Body;
8588                end if;
8589             end if;
8590          end if;
8591       end Post_Error;
8592
8593    --  Start of processing for Check_Completion
8594
8595    begin
8596       E := First_Entity (Current_Scope);
8597       while Present (E) loop
8598          if Is_Intrinsic_Subprogram (E) then
8599             null;
8600
8601          --  The following situation requires special handling: a child unit
8602          --  that appears in the context clause of the body of its parent:
8603
8604          --    procedure Parent.Child (...);
8605
8606          --    with Parent.Child;
8607          --    package body Parent is
8608
8609          --  Here Parent.Child appears as a local entity, but should not be
8610          --  flagged as requiring completion, because it is a compilation
8611          --  unit.
8612
8613          --  Ignore missing completion for a subprogram that does not come from
8614          --  source (including the _Call primitive operation of RAS types,
8615          --  which has to have the flag Comes_From_Source for other purposes):
8616          --  we assume that the expander will provide the missing completion.
8617
8618          elsif     Ekind (E) = E_Function
8619            or else Ekind (E) = E_Procedure
8620            or else Ekind (E) = E_Generic_Function
8621            or else Ekind (E) = E_Generic_Procedure
8622          then
8623             if not Has_Completion (E)
8624               and then not (Is_Subprogram (E)
8625                             and then Is_Abstract_Subprogram (E))
8626               and then not (Is_Subprogram (E)
8627                               and then
8628                             (not Comes_From_Source (E)
8629                               or else Chars (E) = Name_uCall))
8630               and then Nkind (Parent (Unit_Declaration_Node (E))) /=
8631                                                        N_Compilation_Unit
8632               and then Chars (E) /= Name_uSize
8633             then
8634                Post_Error;
8635             end if;
8636
8637          elsif Is_Entry (E) then
8638             if not Has_Completion (E) and then
8639               (Ekind (Scope (E)) = E_Protected_Object
8640                 or else Ekind (Scope (E)) = E_Protected_Type)
8641             then
8642                Post_Error;
8643             end if;
8644
8645          elsif Is_Package_Or_Generic_Package (E) then
8646             if Unit_Requires_Body (E) then
8647                if not Has_Completion (E)
8648                  and then Nkind (Parent (Unit_Declaration_Node (E))) /=
8649                                                        N_Compilation_Unit
8650                then
8651                   Post_Error;
8652                end if;
8653
8654             elsif not Is_Child_Unit (E) then
8655                May_Need_Implicit_Body (E);
8656             end if;
8657
8658          elsif Ekind (E) = E_Incomplete_Type
8659            and then No (Underlying_Type (E))
8660          then
8661             Post_Error;
8662
8663          elsif (Ekind (E) = E_Task_Type or else
8664                 Ekind (E) = E_Protected_Type)
8665            and then not Has_Completion (E)
8666          then
8667             Post_Error;
8668
8669          --  A single task declared in the current scope is a constant, verify
8670          --  that the body of its anonymous type is in the same scope. If the
8671          --  task is defined elsewhere, this may be a renaming declaration for
8672          --  which no completion is needed.
8673
8674          elsif Ekind (E) = E_Constant
8675            and then Ekind (Etype (E)) = E_Task_Type
8676            and then not Has_Completion (Etype (E))
8677            and then Scope (Etype (E)) = Current_Scope
8678          then
8679             Post_Error;
8680
8681          elsif Ekind (E) = E_Protected_Object
8682            and then not Has_Completion (Etype (E))
8683          then
8684             Post_Error;
8685
8686          elsif Ekind (E) = E_Record_Type then
8687             if Is_Tagged_Type (E) then
8688                Check_Abstract_Overriding (E);
8689                Check_Conventions (E);
8690             end if;
8691
8692             Check_Aliased_Component_Types (E);
8693
8694          elsif Ekind (E) = E_Array_Type then
8695             Check_Aliased_Component_Types (E);
8696
8697          end if;
8698
8699          Next_Entity (E);
8700       end loop;
8701    end Check_Completion;
8702
8703    ----------------------------
8704    -- Check_Delta_Expression --
8705    ----------------------------
8706
8707    procedure Check_Delta_Expression (E : Node_Id) is
8708    begin
8709       if not (Is_Real_Type (Etype (E))) then
8710          Wrong_Type (E, Any_Real);
8711
8712       elsif not Is_OK_Static_Expression (E) then
8713          Flag_Non_Static_Expr
8714            ("non-static expression used for delta value!", E);
8715
8716       elsif not UR_Is_Positive (Expr_Value_R (E)) then
8717          Error_Msg_N ("delta expression must be positive", E);
8718
8719       else
8720          return;
8721       end if;
8722
8723       --  If any of above errors occurred, then replace the incorrect
8724       --  expression by the real 0.1, which should prevent further errors.
8725
8726       Rewrite (E,
8727         Make_Real_Literal (Sloc (E), Ureal_Tenth));
8728       Analyze_And_Resolve (E, Standard_Float);
8729    end Check_Delta_Expression;
8730
8731    -----------------------------
8732    -- Check_Digits_Expression --
8733    -----------------------------
8734
8735    procedure Check_Digits_Expression (E : Node_Id) is
8736    begin
8737       if not (Is_Integer_Type (Etype (E))) then
8738          Wrong_Type (E, Any_Integer);
8739
8740       elsif not Is_OK_Static_Expression (E) then
8741          Flag_Non_Static_Expr
8742            ("non-static expression used for digits value!", E);
8743
8744       elsif Expr_Value (E) <= 0 then
8745          Error_Msg_N ("digits value must be greater than zero", E);
8746
8747       else
8748          return;
8749       end if;
8750
8751       --  If any of above errors occurred, then replace the incorrect
8752       --  expression by the integer 1, which should prevent further errors.
8753
8754       Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
8755       Analyze_And_Resolve (E, Standard_Integer);
8756
8757    end Check_Digits_Expression;
8758
8759    --------------------------
8760    -- Check_Initialization --
8761    --------------------------
8762
8763    procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
8764    begin
8765       if Is_Limited_Type (T)
8766         and then not In_Instance
8767         and then not In_Inlined_Body
8768       then
8769          if not OK_For_Limited_Init (Exp) then
8770
8771             --  In GNAT mode, this is just a warning, to allow it to be evilly
8772             --  turned off. Otherwise it is a real error.
8773
8774             if GNAT_Mode then
8775                Error_Msg_N
8776                  ("?cannot initialize entities of limited type!", Exp);
8777
8778             elsif Ada_Version < Ada_05 then
8779                Error_Msg_N
8780                  ("cannot initialize entities of limited type", Exp);
8781                Explain_Limited_Type (T, Exp);
8782
8783             else
8784                --  Specialize error message according to kind of illegal
8785                --  initial expression.
8786
8787                if Nkind (Exp) = N_Type_Conversion
8788                  and then Nkind (Expression (Exp)) = N_Function_Call
8789                then
8790                   Error_Msg_N
8791                     ("illegal context for call"
8792                       & " to function with limited result", Exp);
8793
8794                else
8795                   Error_Msg_N
8796                     ("initialization of limited object requires aggregate "
8797                       & "or function call",  Exp);
8798                end if;
8799             end if;
8800          end if;
8801       end if;
8802    end Check_Initialization;
8803
8804    ----------------------
8805    -- Check_Interfaces --
8806    ----------------------
8807
8808    procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
8809       Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
8810
8811       Iface       : Node_Id;
8812       Iface_Def   : Node_Id;
8813       Iface_Typ   : Entity_Id;
8814       Parent_Node : Node_Id;
8815
8816       Is_Task : Boolean := False;
8817       --  Set True if parent type or any progenitor is a task interface
8818
8819       Is_Protected : Boolean := False;
8820       --  Set True if parent type or any progenitor is a protected interface
8821
8822       procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
8823       --  Check that a progenitor is compatible with declaration.
8824       --  Error is posted on Error_Node.
8825
8826       ------------------
8827       -- Check_Ifaces --
8828       ------------------
8829
8830       procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
8831          Iface_Id : constant Entity_Id :=
8832                       Defining_Identifier (Parent (Iface_Def));
8833          Type_Def : Node_Id;
8834
8835       begin
8836          if Nkind (N) = N_Private_Extension_Declaration then
8837             Type_Def := N;
8838          else
8839             Type_Def := Type_Definition (N);
8840          end if;
8841
8842          if Is_Task_Interface (Iface_Id) then
8843             Is_Task := True;
8844
8845          elsif Is_Protected_Interface (Iface_Id) then
8846             Is_Protected := True;
8847          end if;
8848
8849          if Is_Synchronized_Interface (Iface_Id) then
8850
8851             --  A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
8852             --  extension derived from a synchronized interface must explicitly
8853             --  be declared synchronized, because the full view will be a
8854             --  synchronized type.
8855
8856             if Nkind (N) = N_Private_Extension_Declaration then
8857                if not Synchronized_Present (N) then
8858                   Error_Msg_NE
8859                     ("private extension of& must be explicitly synchronized",
8860                       N, Iface_Id);
8861                end if;
8862
8863             --  However, by 3.9.4(16/2), a full type that is a record extension
8864             --  is never allowed to derive from a synchronized interface (note
8865             --  that interfaces must be excluded from this check, because those
8866             --  are represented by derived type definitions in some cases).
8867
8868             elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
8869               and then not Interface_Present (Type_Definition (N))
8870             then
8871                Error_Msg_N ("record extension cannot derive from synchronized"
8872                              & " interface", Error_Node);
8873             end if;
8874          end if;
8875
8876          --  Check that the characteristics of the progenitor are compatible
8877          --  with the explicit qualifier in the declaration.
8878          --  The check only applies to qualifiers that come from source.
8879          --  Limited_Present also appears in the declaration of corresponding
8880          --  records, and the check does not apply to them.
8881
8882          if Limited_Present (Type_Def)
8883            and then not
8884              Is_Concurrent_Record_Type (Defining_Identifier (N))
8885          then
8886             if Is_Limited_Interface (Parent_Type)
8887               and then not Is_Limited_Interface (Iface_Id)
8888             then
8889                Error_Msg_NE
8890                  ("progenitor& must be limited interface",
8891                    Error_Node, Iface_Id);
8892
8893             elsif
8894               (Task_Present (Iface_Def)
8895                 or else Protected_Present (Iface_Def)
8896                 or else Synchronized_Present (Iface_Def))
8897               and then Nkind (N) /= N_Private_Extension_Declaration
8898               and then not Error_Posted (N)
8899             then
8900                Error_Msg_NE
8901                  ("progenitor& must be limited interface",
8902                    Error_Node, Iface_Id);
8903             end if;
8904
8905          --  Protected interfaces can only inherit from limited, synchronized
8906          --  or protected interfaces.
8907
8908          elsif Nkind (N) = N_Full_Type_Declaration
8909            and then  Protected_Present (Type_Def)
8910          then
8911             if Limited_Present (Iface_Def)
8912               or else Synchronized_Present (Iface_Def)
8913               or else Protected_Present (Iface_Def)
8914             then
8915                null;
8916
8917             elsif Task_Present (Iface_Def) then
8918                Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
8919                             & " from task interface", Error_Node);
8920
8921             else
8922                Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
8923                             & " from non-limited interface", Error_Node);
8924             end if;
8925
8926          --  Ada 2005 (AI-345): Synchronized interfaces can only inherit from
8927          --  limited and synchronized.
8928
8929          elsif Synchronized_Present (Type_Def) then
8930             if Limited_Present (Iface_Def)
8931               or else Synchronized_Present (Iface_Def)
8932             then
8933                null;
8934
8935             elsif Protected_Present (Iface_Def)
8936               and then Nkind (N) /= N_Private_Extension_Declaration
8937             then
8938                Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8939                             & " from protected interface", Error_Node);
8940
8941             elsif Task_Present (Iface_Def)
8942               and then Nkind (N) /= N_Private_Extension_Declaration
8943             then
8944                Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8945                             & " from task interface", Error_Node);
8946
8947             elsif not Is_Limited_Interface (Iface_Id) then
8948                Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8949                             & " from non-limited interface", Error_Node);
8950             end if;
8951
8952          --  Ada 2005 (AI-345): Task interfaces can only inherit from limited,
8953          --  synchronized or task interfaces.
8954
8955          elsif Nkind (N) = N_Full_Type_Declaration
8956            and then Task_Present (Type_Def)
8957          then
8958             if Limited_Present (Iface_Def)
8959               or else Synchronized_Present (Iface_Def)
8960               or else Task_Present (Iface_Def)
8961             then
8962                null;
8963
8964             elsif Protected_Present (Iface_Def) then
8965                Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
8966                             & " protected interface", Error_Node);
8967
8968             else
8969                Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
8970                             & " non-limited interface", Error_Node);
8971             end if;
8972          end if;
8973       end Check_Ifaces;
8974
8975    --  Start of processing for Check_Interfaces
8976
8977    begin
8978       if Is_Interface (Parent_Type) then
8979          if Is_Task_Interface (Parent_Type) then
8980             Is_Task := True;
8981
8982          elsif Is_Protected_Interface (Parent_Type) then
8983             Is_Protected := True;
8984          end if;
8985       end if;
8986
8987       if Nkind (N) = N_Private_Extension_Declaration then
8988
8989          --  Check that progenitors are compatible with declaration
8990
8991          Iface := First (Interface_List (Def));
8992          while Present (Iface) loop
8993             Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
8994
8995             Parent_Node := Parent (Base_Type (Iface_Typ));
8996             Iface_Def   := Type_Definition (Parent_Node);
8997
8998             if not Is_Interface (Iface_Typ) then
8999                Diagnose_Interface (Iface, Iface_Typ);
9000
9001             else
9002                Check_Ifaces (Iface_Def, Iface);
9003             end if;
9004
9005             Next (Iface);
9006          end loop;
9007
9008          if Is_Task and Is_Protected then
9009             Error_Msg_N
9010               ("type cannot derive from task and protected interface", N);
9011          end if;
9012
9013          return;
9014       end if;
9015
9016       --  Full type declaration of derived type.
9017       --  Check compatibility with parent if it is interface type
9018
9019       if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9020         and then Is_Interface (Parent_Type)
9021       then
9022          Parent_Node := Parent (Parent_Type);
9023
9024          --  More detailed checks for interface varieties
9025
9026          Check_Ifaces
9027            (Iface_Def  => Type_Definition (Parent_Node),
9028             Error_Node => Subtype_Indication (Type_Definition (N)));
9029       end if;
9030
9031       Iface := First (Interface_List (Def));
9032       while Present (Iface) loop
9033          Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
9034
9035          Parent_Node := Parent (Base_Type (Iface_Typ));
9036          Iface_Def   := Type_Definition (Parent_Node);
9037
9038          if not Is_Interface (Iface_Typ) then
9039             Diagnose_Interface (Iface, Iface_Typ);
9040
9041          else
9042             --  "The declaration of a specific descendant of an interface
9043             --   type freezes the interface type" RM 13.14
9044
9045             Freeze_Before (N, Iface_Typ);
9046             Check_Ifaces (Iface_Def, Error_Node => Iface);
9047          end if;
9048
9049          Next (Iface);
9050       end loop;
9051
9052       if Is_Task and Is_Protected then
9053          Error_Msg_N
9054            ("type cannot derive from task and protected interface", N);
9055       end if;
9056    end Check_Interfaces;
9057
9058    ------------------------------------
9059    -- Check_Or_Process_Discriminants --
9060    ------------------------------------
9061
9062    --  If an incomplete or private type declaration was already given for the
9063    --  type, the discriminants may have already been processed if they were
9064    --  present on the incomplete declaration. In this case a full conformance
9065    --  check is performed otherwise just process them.
9066
9067    procedure Check_Or_Process_Discriminants
9068      (N    : Node_Id;
9069       T    : Entity_Id;
9070       Prev : Entity_Id := Empty)
9071    is
9072    begin
9073       if Has_Discriminants (T) then
9074
9075          --  Make the discriminants visible to component declarations
9076
9077          declare
9078             D    : Entity_Id;
9079             Prev : Entity_Id;
9080
9081          begin
9082             D := First_Discriminant (T);
9083             while Present (D) loop
9084                Prev := Current_Entity (D);
9085                Set_Current_Entity (D);
9086                Set_Is_Immediately_Visible (D);
9087                Set_Homonym (D, Prev);
9088
9089                --  Ada 2005 (AI-230): Access discriminant allowed in
9090                --  non-limited record types.
9091
9092                if Ada_Version < Ada_05 then
9093
9094                   --  This restriction gets applied to the full type here. It
9095                   --  has already been applied earlier to the partial view.
9096
9097                   Check_Access_Discriminant_Requires_Limited (Parent (D), N);
9098                end if;
9099
9100                Next_Discriminant (D);
9101             end loop;
9102          end;
9103
9104       elsif Present (Discriminant_Specifications (N)) then
9105          Process_Discriminants (N, Prev);
9106       end if;
9107    end Check_Or_Process_Discriminants;
9108
9109    ----------------------
9110    -- Check_Real_Bound --
9111    ----------------------
9112
9113    procedure Check_Real_Bound (Bound : Node_Id) is
9114    begin
9115       if not Is_Real_Type (Etype (Bound)) then
9116          Error_Msg_N
9117            ("bound in real type definition must be of real type", Bound);
9118
9119       elsif not Is_OK_Static_Expression (Bound) then
9120          Flag_Non_Static_Expr
9121            ("non-static expression used for real type bound!", Bound);
9122
9123       else
9124          return;
9125       end if;
9126
9127       Rewrite
9128         (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
9129       Analyze (Bound);
9130       Resolve (Bound, Standard_Float);
9131    end Check_Real_Bound;
9132
9133    ------------------------------
9134    -- Complete_Private_Subtype --
9135    ------------------------------
9136
9137    procedure Complete_Private_Subtype
9138      (Priv        : Entity_Id;
9139       Full        : Entity_Id;
9140       Full_Base   : Entity_Id;
9141       Related_Nod : Node_Id)
9142    is
9143       Save_Next_Entity : Entity_Id;
9144       Save_Homonym     : Entity_Id;
9145
9146    begin
9147       --  Set semantic attributes for (implicit) private subtype completion.
9148       --  If the full type has no discriminants, then it is a copy of the full
9149       --  view of the base. Otherwise, it is a subtype of the base with a
9150       --  possible discriminant constraint. Save and restore the original
9151       --  Next_Entity field of full to ensure that the calls to Copy_Node
9152       --  do not corrupt the entity chain.
9153
9154       --  Note that the type of the full view is the same entity as the type of
9155       --  the partial view. In this fashion, the subtype has access to the
9156       --  correct view of the parent.
9157
9158       Save_Next_Entity := Next_Entity (Full);
9159       Save_Homonym     := Homonym (Priv);
9160
9161       case Ekind (Full_Base) is
9162          when E_Record_Type    |
9163               E_Record_Subtype |
9164               Class_Wide_Kind  |
9165               Private_Kind     |
9166               Task_Kind        |
9167               Protected_Kind   =>
9168             Copy_Node (Priv, Full);
9169
9170             Set_Has_Discriminants  (Full, Has_Discriminants (Full_Base));
9171             Set_First_Entity       (Full, First_Entity (Full_Base));
9172             Set_Last_Entity        (Full, Last_Entity (Full_Base));
9173
9174          when others =>
9175             Copy_Node (Full_Base, Full);
9176             Set_Chars          (Full, Chars (Priv));
9177             Conditional_Delay  (Full, Priv);
9178             Set_Sloc           (Full, Sloc (Priv));
9179       end case;
9180
9181       Set_Next_Entity (Full, Save_Next_Entity);
9182       Set_Homonym     (Full, Save_Homonym);
9183       Set_Associated_Node_For_Itype (Full, Related_Nod);
9184
9185       --  Set common attributes for all subtypes
9186
9187       Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
9188
9189       --  The Etype of the full view is inconsistent. Gigi needs to see the
9190       --  structural full view,  which is what the current scheme gives:
9191       --  the Etype of the full view is the etype of the full base. However,
9192       --  if the full base is a derived type, the full view then looks like
9193       --  a subtype of the parent, not a subtype of the full base. If instead
9194       --  we write:
9195
9196       --       Set_Etype (Full, Full_Base);
9197
9198       --  then we get inconsistencies in the front-end (confusion between
9199       --  views). Several outstanding bugs are related to this ???
9200
9201       Set_Is_First_Subtype (Full, False);
9202       Set_Scope            (Full, Scope (Priv));
9203       Set_Size_Info        (Full, Full_Base);
9204       Set_RM_Size          (Full, RM_Size (Full_Base));
9205       Set_Is_Itype         (Full);
9206
9207       --  A subtype of a private-type-without-discriminants, whose full-view
9208       --  has discriminants with default expressions, is not constrained!
9209
9210       if not Has_Discriminants (Priv) then
9211          Set_Is_Constrained (Full, Is_Constrained (Full_Base));
9212
9213          if Has_Discriminants (Full_Base) then
9214             Set_Discriminant_Constraint
9215               (Full, Discriminant_Constraint (Full_Base));
9216
9217             --  The partial view may have been indefinite, the full view
9218             --  might not be.
9219
9220             Set_Has_Unknown_Discriminants
9221               (Full, Has_Unknown_Discriminants (Full_Base));
9222          end if;
9223       end if;
9224
9225       Set_First_Rep_Item     (Full, First_Rep_Item (Full_Base));
9226       Set_Depends_On_Private (Full, Has_Private_Component (Full));
9227
9228       --  Freeze the private subtype entity if its parent is delayed, and not
9229       --  already frozen. We skip this processing if the type is an anonymous
9230       --  subtype of a record component, or is the corresponding record of a
9231       --  protected type, since ???
9232
9233       if not Is_Type (Scope (Full)) then
9234          Set_Has_Delayed_Freeze (Full,
9235            Has_Delayed_Freeze (Full_Base)
9236              and then (not Is_Frozen (Full_Base)));
9237       end if;
9238
9239       Set_Freeze_Node (Full, Empty);
9240       Set_Is_Frozen (Full, False);
9241       Set_Full_View (Priv, Full);
9242
9243       if Has_Discriminants (Full) then
9244          Set_Stored_Constraint_From_Discriminant_Constraint (Full);
9245          Set_Stored_Constraint (Priv, Stored_Constraint (Full));
9246
9247          if Has_Unknown_Discriminants (Full) then
9248             Set_Discriminant_Constraint (Full, No_Elist);
9249          end if;
9250       end if;
9251
9252       if Ekind (Full_Base) = E_Record_Type
9253         and then Has_Discriminants (Full_Base)
9254         and then Has_Discriminants (Priv) -- might not, if errors
9255         and then not Has_Unknown_Discriminants (Priv)
9256         and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
9257       then
9258          Create_Constrained_Components
9259            (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
9260
9261       --  If the full base is itself derived from private, build a congruent
9262       --  subtype of its underlying type, for use by the back end. For a
9263       --  constrained record component, the declaration cannot be placed on
9264       --  the component list, but it must nevertheless be built an analyzed, to
9265       --  supply enough information for Gigi to compute the size of component.
9266
9267       elsif Ekind (Full_Base) in Private_Kind
9268         and then Is_Derived_Type (Full_Base)
9269         and then Has_Discriminants (Full_Base)
9270         and then (Ekind (Current_Scope) /= E_Record_Subtype)
9271       then
9272          if not Is_Itype (Priv)
9273            and then
9274              Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
9275          then
9276             Build_Underlying_Full_View
9277               (Parent (Priv), Full, Etype (Full_Base));
9278
9279          elsif Nkind (Related_Nod) = N_Component_Declaration then
9280             Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
9281          end if;
9282
9283       elsif Is_Record_Type (Full_Base) then
9284
9285          --  Show Full is simply a renaming of Full_Base
9286
9287          Set_Cloned_Subtype (Full, Full_Base);
9288       end if;
9289
9290       --  It is unsafe to share to bounds of a scalar type, because the Itype
9291       --  is elaborated on demand, and if a bound is non-static then different
9292       --  orders of elaboration in different units will lead to different
9293       --  external symbols.
9294
9295       if Is_Scalar_Type (Full_Base) then
9296          Set_Scalar_Range (Full,
9297            Make_Range (Sloc (Related_Nod),
9298              Low_Bound  =>
9299                Duplicate_Subexpr_No_Checks (Type_Low_Bound  (Full_Base)),
9300              High_Bound =>
9301                Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
9302
9303          --  This completion inherits the bounds of the full parent, but if
9304          --  the parent is an unconstrained floating point type, so is the
9305          --  completion.
9306
9307          if Is_Floating_Point_Type (Full_Base) then
9308             Set_Includes_Infinities
9309              (Scalar_Range (Full), Has_Infinities (Full_Base));
9310          end if;
9311       end if;
9312
9313       --  ??? It seems that a lot of fields are missing that should be copied
9314       --  from Full_Base to Full. Here are some that are introduced in a
9315       --  non-disruptive way but a cleanup is necessary.
9316
9317       if Is_Tagged_Type (Full_Base) then
9318          Set_Is_Tagged_Type (Full);
9319          Set_Primitive_Operations (Full, Primitive_Operations (Full_Base));
9320          Set_Class_Wide_Type      (Full, Class_Wide_Type (Full_Base));
9321
9322       --  If this is a subtype of a protected or task type, constrain its
9323       --  corresponding record, unless this is a subtype without constraints,
9324       --  i.e. a simple renaming as with an actual subtype in an instance.
9325
9326       elsif Is_Concurrent_Type (Full_Base) then
9327          if Has_Discriminants (Full)
9328            and then Present (Corresponding_Record_Type (Full_Base))
9329            and then
9330              not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
9331          then
9332             Set_Corresponding_Record_Type (Full,
9333               Constrain_Corresponding_Record
9334                 (Full, Corresponding_Record_Type (Full_Base),
9335                   Related_Nod, Full_Base));
9336
9337          else
9338             Set_Corresponding_Record_Type (Full,
9339               Corresponding_Record_Type (Full_Base));
9340          end if;
9341       end if;
9342    end Complete_Private_Subtype;
9343
9344    ----------------------------
9345    -- Constant_Redeclaration --
9346    ----------------------------
9347
9348    procedure Constant_Redeclaration
9349      (Id : Entity_Id;
9350       N  : Node_Id;
9351       T  : out Entity_Id)
9352    is
9353       Prev    : constant Entity_Id := Current_Entity_In_Scope (Id);
9354       Obj_Def : constant Node_Id := Object_Definition (N);
9355       New_T   : Entity_Id;
9356
9357       procedure Check_Possible_Deferred_Completion
9358         (Prev_Id      : Entity_Id;
9359          Prev_Obj_Def : Node_Id;
9360          Curr_Obj_Def : Node_Id);
9361       --  Determine whether the two object definitions describe the partial
9362       --  and the full view of a constrained deferred constant. Generate
9363       --  a subtype for the full view and verify that it statically matches
9364       --  the subtype of the partial view.
9365
9366       procedure Check_Recursive_Declaration (Typ : Entity_Id);
9367       --  If deferred constant is an access type initialized with an allocator,
9368       --  check whether there is an illegal recursion in the definition,
9369       --  through a default value of some record subcomponent. This is normally
9370       --  detected when generating init procs, but requires this additional
9371       --  mechanism when expansion is disabled.
9372
9373       ----------------------------------------
9374       -- Check_Possible_Deferred_Completion --
9375       ----------------------------------------
9376
9377       procedure Check_Possible_Deferred_Completion
9378         (Prev_Id      : Entity_Id;
9379          Prev_Obj_Def : Node_Id;
9380          Curr_Obj_Def : Node_Id)
9381       is
9382       begin
9383          if Nkind (Prev_Obj_Def) = N_Subtype_Indication
9384            and then Present (Constraint (Prev_Obj_Def))
9385            and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
9386            and then Present (Constraint (Curr_Obj_Def))
9387          then
9388             declare
9389                Loc    : constant Source_Ptr := Sloc (N);
9390                Def_Id : constant Entity_Id :=
9391                           Make_Defining_Identifier (Loc,
9392                             New_Internal_Name ('S'));
9393                Decl   : constant Node_Id :=
9394                           Make_Subtype_Declaration (Loc,
9395                             Defining_Identifier =>
9396                               Def_Id,
9397                             Subtype_Indication =>
9398                               Relocate_Node (Curr_Obj_Def));
9399
9400             begin
9401                Insert_Before_And_Analyze (N, Decl);
9402                Set_Etype (Id, Def_Id);
9403
9404                if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
9405                   Error_Msg_Sloc := Sloc (Prev_Id);
9406                   Error_Msg_N ("subtype does not statically match deferred " &
9407                                "declaration#", N);
9408                end if;
9409             end;
9410          end if;
9411       end Check_Possible_Deferred_Completion;
9412
9413       ---------------------------------
9414       -- Check_Recursive_Declaration --
9415       ---------------------------------
9416
9417       procedure Check_Recursive_Declaration (Typ : Entity_Id) is
9418          Comp : Entity_Id;
9419
9420       begin
9421          if Is_Record_Type (Typ) then
9422             Comp := First_Component (Typ);
9423             while Present (Comp) loop
9424                if Comes_From_Source (Comp) then
9425                   if Present (Expression (Parent (Comp)))
9426                     and then Is_Entity_Name (Expression (Parent (Comp)))
9427                     and then Entity (Expression (Parent (Comp))) = Prev
9428                   then
9429                      Error_Msg_Sloc := Sloc (Parent (Comp));
9430                      Error_Msg_NE
9431                        ("illegal circularity with declaration for&#",
9432                          N, Comp);
9433                      return;
9434
9435                   elsif Is_Record_Type (Etype (Comp)) then
9436                      Check_Recursive_Declaration (Etype (Comp));
9437                   end if;
9438                end if;
9439
9440                Next_Component (Comp);
9441             end loop;
9442          end if;
9443       end Check_Recursive_Declaration;
9444
9445    --  Start of processing for Constant_Redeclaration
9446
9447    begin
9448       if Nkind (Parent (Prev)) = N_Object_Declaration then
9449          if Nkind (Object_Definition
9450                      (Parent (Prev))) = N_Subtype_Indication
9451          then
9452             --  Find type of new declaration. The constraints of the two
9453             --  views must match statically, but there is no point in
9454             --  creating an itype for the full view.
9455
9456             if Nkind (Obj_Def) = N_Subtype_Indication then
9457                Find_Type (Subtype_Mark (Obj_Def));
9458                New_T := Entity (Subtype_Mark (Obj_Def));
9459
9460             else
9461                Find_Type (Obj_Def);
9462                New_T := Entity (Obj_Def);
9463             end if;
9464
9465             T := Etype (Prev);
9466
9467          else
9468             --  The full view may impose a constraint, even if the partial
9469             --  view does not, so construct the subtype.
9470
9471             New_T := Find_Type_Of_Object (Obj_Def, N);
9472             T     := New_T;
9473          end if;
9474
9475       else
9476          --  Current declaration is illegal, diagnosed below in Enter_Name
9477
9478          T := Empty;
9479          New_T := Any_Type;
9480       end if;
9481
9482       --  If previous full declaration exists, or if a homograph is present,
9483       --  let Enter_Name handle it, either with an error, or with the removal
9484       --  of an overridden implicit subprogram.
9485
9486       if Ekind (Prev) /= E_Constant
9487         or else Present (Expression (Parent (Prev)))
9488         or else Present (Full_View (Prev))
9489       then
9490          Enter_Name (Id);
9491
9492       --  Verify that types of both declarations match, or else that both types
9493       --  are anonymous access types whose designated subtypes statically match
9494       --  (as allowed in Ada 2005 by AI-385).
9495
9496       elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
9497         and then
9498           (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
9499              or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
9500              or else Is_Access_Constant (Etype (New_T)) /=
9501                      Is_Access_Constant (Etype (Prev))
9502              or else Can_Never_Be_Null (Etype (New_T)) /=
9503                      Can_Never_Be_Null (Etype (Prev))
9504              or else Null_Exclusion_Present (Parent (Prev)) /=
9505                      Null_Exclusion_Present (Parent (Id))
9506              or else not Subtypes_Statically_Match
9507                            (Designated_Type (Etype (Prev)),
9508                             Designated_Type (Etype (New_T))))
9509       then
9510          Error_Msg_Sloc := Sloc (Prev);
9511          Error_Msg_N ("type does not match declaration#", N);
9512          Set_Full_View (Prev, Id);
9513          Set_Etype (Id, Any_Type);
9514
9515       elsif
9516         Null_Exclusion_Present (Parent (Prev))
9517           and then not Null_Exclusion_Present (N)
9518       then
9519          Error_Msg_Sloc := Sloc (Prev);
9520          Error_Msg_N ("null-exclusion does not match declaration#", N);
9521          Set_Full_View (Prev, Id);
9522          Set_Etype (Id, Any_Type);
9523
9524       --  If so, process the full constant declaration
9525
9526       else
9527          --  RM 7.4 (6): If the subtype defined by the subtype_indication in
9528          --  the deferred declaration is constrained, then the subtype defined
9529          --  by the subtype_indication in the full declaration shall match it
9530          --  statically.
9531
9532          Check_Possible_Deferred_Completion
9533            (Prev_Id      => Prev,
9534             Prev_Obj_Def => Object_Definition (Parent (Prev)),
9535             Curr_Obj_Def => Obj_Def);
9536
9537          Set_Full_View (Prev, Id);
9538          Set_Is_Public (Id, Is_Public (Prev));
9539          Set_Is_Internal (Id);
9540          Append_Entity (Id, Current_Scope);
9541
9542          --  Check ALIASED present if present before (RM 7.4(7))
9543
9544          if Is_Aliased (Prev)
9545            and then not Aliased_Present (N)
9546          then
9547             Error_Msg_Sloc := Sloc (Prev);
9548             Error_Msg_N ("ALIASED required (see declaration#)", N);
9549          end if;
9550
9551          --  Check that placement is in private part and that the incomplete
9552          --  declaration appeared in the visible part.
9553
9554          if Ekind (Current_Scope) = E_Package
9555            and then not In_Private_Part (Current_Scope)
9556          then
9557             Error_Msg_Sloc := Sloc (Prev);
9558             Error_Msg_N ("full constant for declaration#"
9559                          & " must be in private part", N);
9560
9561          elsif Ekind (Current_Scope) = E_Package
9562            and then List_Containing (Parent (Prev))
9563            /= Visible_Declarations
9564              (Specification (Unit_Declaration_Node (Current_Scope)))
9565          then
9566             Error_Msg_N
9567               ("deferred constant must be declared in visible part",
9568                  Parent (Prev));
9569          end if;
9570
9571          if Is_Access_Type (T)
9572            and then Nkind (Expression (N)) = N_Allocator
9573          then
9574             Check_Recursive_Declaration (Designated_Type (T));
9575          end if;
9576       end if;
9577    end Constant_Redeclaration;
9578
9579    ----------------------
9580    -- Constrain_Access --
9581    ----------------------
9582
9583    procedure Constrain_Access
9584      (Def_Id      : in out Entity_Id;
9585       S           : Node_Id;
9586       Related_Nod : Node_Id)
9587    is
9588       T             : constant Entity_Id := Entity (Subtype_Mark (S));
9589       Desig_Type    : constant Entity_Id := Designated_Type (T);
9590       Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
9591       Constraint_OK : Boolean := True;
9592
9593       function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
9594       --  Simple predicate to test for defaulted discriminants
9595       --  Shouldn't this be in sem_util???
9596
9597       ---------------------------------
9598       -- Has_Defaulted_Discriminants --
9599       ---------------------------------
9600
9601       function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
9602       begin
9603          return Has_Discriminants (Typ)
9604           and then Present (First_Discriminant (Typ))
9605           and then Present
9606             (Discriminant_Default_Value (First_Discriminant (Typ)));
9607       end Has_Defaulted_Discriminants;
9608
9609    --  Start of processing for Constrain_Access
9610
9611    begin
9612       if Is_Array_Type (Desig_Type) then
9613          Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
9614
9615       elsif (Is_Record_Type (Desig_Type)
9616               or else Is_Incomplete_Or_Private_Type (Desig_Type))
9617         and then not Is_Constrained (Desig_Type)
9618       then
9619          --  ??? The following code is a temporary kludge to ignore a
9620          --  discriminant constraint on access type if it is constraining
9621          --  the current record. Avoid creating the implicit subtype of the
9622          --  record we are currently compiling since right now, we cannot
9623          --  handle these. For now, just return the access type itself.
9624
9625          if Desig_Type = Current_Scope
9626            and then No (Def_Id)
9627          then
9628             Set_Ekind (Desig_Subtype, E_Record_Subtype);
9629             Def_Id := Entity (Subtype_Mark (S));
9630
9631             --  This call added to ensure that the constraint is analyzed
9632             --  (needed for a B test). Note that we still return early from
9633             --  this procedure to avoid recursive processing. ???
9634
9635             Constrain_Discriminated_Type
9636               (Desig_Subtype, S, Related_Nod, For_Access => True);
9637             return;
9638          end if;
9639
9640          if (Ekind (T) = E_General_Access_Type
9641               or else Ada_Version >= Ada_05)
9642            and then Has_Private_Declaration (Desig_Type)
9643            and then In_Open_Scopes (Scope (Desig_Type))
9644            and then Has_Discriminants (Desig_Type)
9645          then
9646             --  Enforce rule that the constraint is illegal if there is
9647             --  an unconstrained view of the designated type. This means
9648             --  that the partial view (either a private type declaration or
9649             --  a derivation from a private type) has no discriminants.
9650             --  (Defect Report 8652/0008, Technical Corrigendum 1, checked
9651             --  by ACATS B371001).
9652
9653             --  Rule updated for Ada 2005: the private type is said to have
9654             --  a constrained partial view, given that objects of the type
9655             --  can be declared. Furthermore, the rule applies to all access
9656             --  types, unlike the rule concerning default discriminants.
9657
9658             declare
9659                Pack  : constant Node_Id :=
9660                          Unit_Declaration_Node (Scope (Desig_Type));
9661                Decls : List_Id;
9662                Decl  : Node_Id;
9663
9664             begin
9665                if Nkind (Pack) = N_Package_Declaration then
9666                   Decls := Visible_Declarations (Specification (Pack));
9667                   Decl := First (Decls);
9668                   while Present (Decl) loop
9669                      if (Nkind (Decl) = N_Private_Type_Declaration
9670                           and then
9671                             Chars (Defining_Identifier (Decl)) =
9672                                                      Chars (Desig_Type))
9673
9674                        or else
9675                         (Nkind (Decl) = N_Full_Type_Declaration
9676                           and then
9677                             Chars (Defining_Identifier (Decl)) =
9678                                                      Chars (Desig_Type)
9679                           and then Is_Derived_Type (Desig_Type)
9680                           and then
9681                             Has_Private_Declaration (Etype (Desig_Type)))
9682                      then
9683                         if No (Discriminant_Specifications (Decl)) then
9684                            Error_Msg_N
9685                             ("cannot constrain general access type if " &
9686                                "designated type has constrained partial view",
9687                                 S);
9688                         end if;
9689
9690                         exit;
9691                      end if;
9692
9693                      Next (Decl);
9694                   end loop;
9695                end if;
9696             end;
9697          end if;
9698
9699          Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
9700            For_Access => True);
9701
9702       elsif (Is_Task_Type (Desig_Type)
9703               or else Is_Protected_Type (Desig_Type))
9704         and then not Is_Constrained (Desig_Type)
9705       then
9706          Constrain_Concurrent
9707            (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
9708
9709       else
9710          Error_Msg_N ("invalid constraint on access type", S);
9711          Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
9712          Constraint_OK := False;
9713       end if;
9714
9715       if No (Def_Id) then
9716          Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
9717       else
9718          Set_Ekind (Def_Id, E_Access_Subtype);
9719       end if;
9720
9721       if Constraint_OK then
9722          Set_Etype (Def_Id, Base_Type (T));
9723
9724          if Is_Private_Type (Desig_Type) then
9725             Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
9726          end if;
9727       else
9728          Set_Etype (Def_Id, Any_Type);
9729       end if;
9730
9731       Set_Size_Info                (Def_Id, T);
9732       Set_Is_Constrained           (Def_Id, Constraint_OK);
9733       Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
9734       Set_Depends_On_Private       (Def_Id, Has_Private_Component (Def_Id));
9735       Set_Is_Access_Constant       (Def_Id, Is_Access_Constant (T));
9736
9737       Conditional_Delay (Def_Id, T);
9738
9739       --  AI-363 : Subtypes of general access types whose designated types have
9740       --  default discriminants are disallowed. In instances, the rule has to
9741       --  be checked against the actual, of which T is the subtype. In a
9742       --  generic body, the rule is checked assuming that the actual type has
9743       --  defaulted discriminants.
9744
9745       if Ada_Version >= Ada_05 or else Warn_On_Ada_2005_Compatibility then
9746          if Ekind (Base_Type (T)) = E_General_Access_Type
9747            and then Has_Defaulted_Discriminants (Desig_Type)
9748          then
9749             if Ada_Version < Ada_05 then
9750                Error_Msg_N
9751                  ("access subtype of general access type would not " &
9752                   "be allowed in Ada 2005?", S);
9753             else
9754                Error_Msg_N
9755                  ("access subype of general access type not allowed", S);
9756             end if;
9757
9758             Error_Msg_N ("\discriminants have defaults", S);
9759
9760          elsif Is_Access_Type (T)
9761            and then Is_Generic_Type (Desig_Type)
9762            and then Has_Discriminants (Desig_Type)
9763            and then In_Package_Body (Current_Scope)
9764          then
9765             if Ada_Version < Ada_05 then
9766                Error_Msg_N
9767                  ("access subtype would not be allowed in generic body " &
9768                   "in Ada 2005?", S);
9769             else
9770                Error_Msg_N
9771                  ("access subtype not allowed in generic body", S);
9772             end if;
9773
9774             Error_Msg_N
9775               ("\designated type is a discriminated formal", S);
9776          end if;
9777       end if;
9778    end Constrain_Access;
9779
9780    ---------------------
9781    -- Constrain_Array --
9782    ---------------------
9783
9784    procedure Constrain_Array
9785      (Def_Id      : in out Entity_Id;
9786       SI          : Node_Id;
9787       Related_Nod : Node_Id;
9788       Related_Id  : Entity_Id;
9789       Suffix      : Character)
9790    is
9791       C                     : constant Node_Id := Constraint (SI);
9792       Number_Of_Constraints : Nat := 0;
9793       Index                 : Node_Id;
9794       S, T                  : Entity_Id;
9795       Constraint_OK         : Boolean := True;
9796
9797    begin
9798       T := Entity (Subtype_Mark (SI));
9799
9800       if Ekind (T) in Access_Kind then
9801          T := Designated_Type (T);
9802       end if;
9803
9804       --  If an index constraint follows a subtype mark in a subtype indication
9805       --  then the type or subtype denoted by the subtype mark must not already
9806       --  impose an index constraint. The subtype mark must denote either an
9807       --  unconstrained array type or an access type whose designated type
9808       --  is such an array type... (RM 3.6.1)
9809
9810       if Is_Constrained (T) then
9811          Error_Msg_N
9812            ("array type is already constrained", Subtype_Mark (SI));
9813          Constraint_OK := False;
9814
9815       else
9816          S := First (Constraints (C));
9817          while Present (S) loop
9818             Number_Of_Constraints := Number_Of_Constraints + 1;
9819             Next (S);
9820          end loop;
9821
9822          --  In either case, the index constraint must provide a discrete
9823          --  range for each index of the array type and the type of each
9824          --  discrete range must be the same as that of the corresponding
9825          --  index. (RM 3.6.1)
9826
9827          if Number_Of_Constraints /= Number_Dimensions (T) then
9828             Error_Msg_NE ("incorrect number of index constraints for }", C, T);
9829             Constraint_OK := False;
9830
9831          else
9832             S := First (Constraints (C));
9833             Index := First_Index (T);
9834             Analyze (Index);
9835
9836             --  Apply constraints to each index type
9837
9838             for J in 1 .. Number_Of_Constraints loop
9839                Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
9840                Next (Index);
9841                Next (S);
9842             end loop;
9843
9844          end if;
9845       end if;
9846
9847       if No (Def_Id) then
9848          Def_Id :=
9849            Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
9850          Set_Parent (Def_Id, Related_Nod);
9851
9852       else
9853          Set_Ekind (Def_Id, E_Array_Subtype);
9854       end if;
9855
9856       Set_Size_Info      (Def_Id,                (T));
9857       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9858       Set_Etype          (Def_Id, Base_Type      (T));
9859
9860       if Constraint_OK then
9861          Set_First_Index (Def_Id, First (Constraints (C)));
9862       else
9863          Set_First_Index (Def_Id, First_Index (T));
9864       end if;
9865
9866       Set_Is_Constrained     (Def_Id, True);
9867       Set_Is_Aliased         (Def_Id, Is_Aliased (T));
9868       Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
9869
9870       Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
9871       Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
9872
9873       --  A subtype does not inherit the packed_array_type of is parent. We
9874       --  need to initialize the attribute because if Def_Id is previously
9875       --  analyzed through a limited_with clause, it will have the attributes
9876       --  of an incomplete type, one of which is an Elist that overlaps the
9877       --  Packed_Array_Type field.
9878
9879       Set_Packed_Array_Type (Def_Id, Empty);
9880
9881       --  Build a freeze node if parent still needs one. Also make sure that
9882       --  the Depends_On_Private status is set because the subtype will need
9883       --  reprocessing at the time the base type does, and also we must set a
9884       --  conditional delay.
9885
9886       Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9887       Conditional_Delay (Def_Id, T);
9888    end Constrain_Array;
9889
9890    ------------------------------
9891    -- Constrain_Component_Type --
9892    ------------------------------
9893
9894    function Constrain_Component_Type
9895      (Comp            : Entity_Id;
9896       Constrained_Typ : Entity_Id;
9897       Related_Node    : Node_Id;
9898       Typ             : Entity_Id;
9899       Constraints     : Elist_Id) return Entity_Id
9900    is
9901       Loc         : constant Source_Ptr := Sloc (Constrained_Typ);
9902       Compon_Type : constant Entity_Id := Etype (Comp);
9903
9904       function Build_Constrained_Array_Type
9905         (Old_Type : Entity_Id) return Entity_Id;
9906       --  If Old_Type is an array type, one of whose indices is constrained
9907       --  by a discriminant, build an Itype whose constraint replaces the
9908       --  discriminant with its value in the constraint.
9909
9910       function Build_Constrained_Discriminated_Type
9911         (Old_Type : Entity_Id) return Entity_Id;
9912       --  Ditto for record components
9913
9914       function Build_Constrained_Access_Type
9915         (Old_Type : Entity_Id) return Entity_Id;
9916       --  Ditto for access types. Makes use of previous two functions, to
9917       --  constrain designated type.
9918
9919       function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
9920       --  T is an array or discriminated type, C is a list of constraints
9921       --  that apply to T. This routine builds the constrained subtype.
9922
9923       function Is_Discriminant (Expr : Node_Id) return Boolean;
9924       --  Returns True if Expr is a discriminant
9925
9926       function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
9927       --  Find the value of discriminant Discrim in Constraint
9928
9929       -----------------------------------
9930       -- Build_Constrained_Access_Type --
9931       -----------------------------------
9932
9933       function Build_Constrained_Access_Type
9934         (Old_Type : Entity_Id) return Entity_Id
9935       is
9936          Desig_Type    : constant Entity_Id := Designated_Type (Old_Type);
9937          Itype         : Entity_Id;
9938          Desig_Subtype : Entity_Id;
9939          Scop          : Entity_Id;
9940
9941       begin
9942          --  if the original access type was not embedded in the enclosing
9943          --  type definition, there is no need to produce a new access
9944          --  subtype. In fact every access type with an explicit constraint
9945          --  generates an itype whose scope is the enclosing record.
9946
9947          if not Is_Type (Scope (Old_Type)) then
9948             return Old_Type;
9949
9950          elsif Is_Array_Type (Desig_Type) then
9951             Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
9952
9953          elsif Has_Discriminants (Desig_Type) then
9954
9955             --  This may be an access type to an enclosing record type for
9956             --  which we are constructing the constrained components. Return
9957             --  the enclosing record subtype. This is not always correct,
9958             --  but avoids infinite recursion. ???
9959
9960             Desig_Subtype := Any_Type;
9961
9962             for J in reverse 0 .. Scope_Stack.Last loop
9963                Scop := Scope_Stack.Table (J).Entity;
9964
9965                if Is_Type (Scop)
9966                  and then Base_Type (Scop) = Base_Type (Desig_Type)
9967                then
9968                   Desig_Subtype := Scop;
9969                end if;
9970
9971                exit when not Is_Type (Scop);
9972             end loop;
9973
9974             if Desig_Subtype = Any_Type then
9975                Desig_Subtype :=
9976                  Build_Constrained_Discriminated_Type (Desig_Type);
9977             end if;
9978
9979          else
9980             return Old_Type;
9981          end if;
9982
9983          if Desig_Subtype /= Desig_Type then
9984
9985             --  The Related_Node better be here or else we won't be able
9986             --  to attach new itypes to a node in the tree.
9987
9988             pragma Assert (Present (Related_Node));
9989
9990             Itype := Create_Itype (E_Access_Subtype, Related_Node);
9991
9992             Set_Etype                    (Itype, Base_Type      (Old_Type));
9993             Set_Size_Info                (Itype,                (Old_Type));
9994             Set_Directly_Designated_Type (Itype, Desig_Subtype);
9995             Set_Depends_On_Private       (Itype, Has_Private_Component
9996                                                                 (Old_Type));
9997             Set_Is_Access_Constant       (Itype, Is_Access_Constant
9998                                                                 (Old_Type));
9999
10000             --  The new itype needs freezing when it depends on a not frozen
10001             --  type and the enclosing subtype needs freezing.
10002
10003             if Has_Delayed_Freeze (Constrained_Typ)
10004               and then not Is_Frozen (Constrained_Typ)
10005             then
10006                Conditional_Delay (Itype, Base_Type (Old_Type));
10007             end if;
10008
10009             return Itype;
10010
10011          else
10012             return Old_Type;
10013          end if;
10014       end Build_Constrained_Access_Type;
10015
10016       ----------------------------------
10017       -- Build_Constrained_Array_Type --
10018       ----------------------------------
10019
10020       function Build_Constrained_Array_Type
10021         (Old_Type : Entity_Id) return Entity_Id
10022       is
10023          Lo_Expr     : Node_Id;
10024          Hi_Expr     : Node_Id;
10025          Old_Index   : Node_Id;
10026          Range_Node  : Node_Id;
10027          Constr_List : List_Id;
10028
10029          Need_To_Create_Itype : Boolean := False;
10030
10031       begin
10032          Old_Index := First_Index (Old_Type);
10033          while Present (Old_Index) loop
10034             Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
10035
10036             if Is_Discriminant (Lo_Expr)
10037               or else Is_Discriminant (Hi_Expr)
10038             then
10039                Need_To_Create_Itype := True;
10040             end if;
10041
10042             Next_Index (Old_Index);
10043          end loop;
10044
10045          if Need_To_Create_Itype then
10046             Constr_List := New_List;
10047
10048             Old_Index := First_Index (Old_Type);
10049             while Present (Old_Index) loop
10050                Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
10051
10052                if Is_Discriminant (Lo_Expr) then
10053                   Lo_Expr := Get_Discr_Value (Lo_Expr);
10054                end if;
10055
10056                if Is_Discriminant (Hi_Expr) then
10057                   Hi_Expr := Get_Discr_Value (Hi_Expr);
10058                end if;
10059
10060                Range_Node :=
10061                  Make_Range
10062                    (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
10063
10064                Append (Range_Node, To => Constr_List);
10065
10066                Next_Index (Old_Index);
10067             end loop;
10068
10069             return Build_Subtype (Old_Type, Constr_List);
10070
10071          else
10072             return Old_Type;
10073          end if;
10074       end Build_Constrained_Array_Type;
10075
10076       ------------------------------------------
10077       -- Build_Constrained_Discriminated_Type --
10078       ------------------------------------------
10079
10080       function Build_Constrained_Discriminated_Type
10081         (Old_Type : Entity_Id) return Entity_Id
10082       is
10083          Expr           : Node_Id;
10084          Constr_List    : List_Id;
10085          Old_Constraint : Elmt_Id;
10086
10087          Need_To_Create_Itype : Boolean := False;
10088
10089       begin
10090          Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
10091          while Present (Old_Constraint) loop
10092             Expr := Node (Old_Constraint);
10093
10094             if Is_Discriminant (Expr) then
10095                Need_To_Create_Itype := True;
10096             end if;
10097
10098             Next_Elmt (Old_Constraint);
10099          end loop;
10100
10101          if Need_To_Create_Itype then
10102             Constr_List := New_List;
10103
10104             Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
10105             while Present (Old_Constraint) loop
10106                Expr := Node (Old_Constraint);
10107
10108                if Is_Discriminant (Expr) then
10109                   Expr := Get_Discr_Value (Expr);
10110                end if;
10111
10112                Append (New_Copy_Tree (Expr), To => Constr_List);
10113
10114                Next_Elmt (Old_Constraint);
10115             end loop;
10116
10117             return Build_Subtype (Old_Type, Constr_List);
10118
10119          else
10120             return Old_Type;
10121          end if;
10122       end Build_Constrained_Discriminated_Type;
10123
10124       -------------------
10125       -- Build_Subtype --
10126       -------------------
10127
10128       function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
10129          Indic       : Node_Id;
10130          Subtyp_Decl : Node_Id;
10131          Def_Id      : Entity_Id;
10132          Btyp        : Entity_Id := Base_Type (T);
10133
10134       begin
10135          --  The Related_Node better be here or else we won't be able to
10136          --  attach new itypes to a node in the tree.
10137
10138          pragma Assert (Present (Related_Node));
10139
10140          --  If the view of the component's type is incomplete or private
10141          --  with unknown discriminants, then the constraint must be applied
10142          --  to the full type.
10143
10144          if Has_Unknown_Discriminants (Btyp)
10145            and then Present (Underlying_Type (Btyp))
10146          then
10147             Btyp := Underlying_Type (Btyp);
10148          end if;
10149
10150          Indic :=
10151            Make_Subtype_Indication (Loc,
10152              Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
10153              Constraint   => Make_Index_Or_Discriminant_Constraint (Loc, C));
10154
10155          Def_Id := Create_Itype (Ekind (T), Related_Node);
10156
10157          Subtyp_Decl :=
10158            Make_Subtype_Declaration (Loc,
10159              Defining_Identifier => Def_Id,
10160              Subtype_Indication  => Indic);
10161
10162          Set_Parent (Subtyp_Decl, Parent (Related_Node));
10163
10164          --  Itypes must be analyzed with checks off (see package Itypes)
10165
10166          Analyze (Subtyp_Decl, Suppress => All_Checks);
10167
10168          return Def_Id;
10169       end Build_Subtype;
10170
10171       ---------------------
10172       -- Get_Discr_Value --
10173       ---------------------
10174
10175       function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
10176          D : Entity_Id;
10177          E : Elmt_Id;
10178
10179       begin
10180          --  The discriminant may be declared for the type, in which case we
10181          --  find it by iterating over the list of discriminants. If the
10182          --  discriminant is inherited from a parent type, it appears as the
10183          --  corresponding discriminant of the current type. This will be the
10184          --  case when constraining an inherited component whose constraint is
10185          --  given by a discriminant of the parent.
10186
10187          D := First_Discriminant (Typ);
10188          E := First_Elmt (Constraints);
10189
10190          while Present (D) loop
10191             if D = Entity (Discrim)
10192               or else D = CR_Discriminant (Entity (Discrim))
10193               or else Corresponding_Discriminant (D) = Entity (Discrim)
10194             then
10195                return Node (E);
10196             end if;
10197
10198             Next_Discriminant (D);
10199             Next_Elmt (E);
10200          end loop;
10201
10202          --  The corresponding_Discriminant mechanism is incomplete, because
10203          --  the correspondence between new and old discriminants is not one
10204          --  to one: one new discriminant can constrain several old ones. In
10205          --  that case, scan sequentially the stored_constraint, the list of
10206          --  discriminants of the parents, and the constraints.
10207          --  Previous code checked for the present of the Stored_Constraint
10208          --  list for the derived type, but did not use it at all. Should it
10209          --  be present when the component is a discriminated task type?
10210
10211          if Is_Derived_Type (Typ)
10212            and then Scope (Entity (Discrim)) = Etype (Typ)
10213          then
10214             D := First_Discriminant (Etype (Typ));
10215             E := First_Elmt (Constraints);
10216             while Present (D) loop
10217                if D = Entity (Discrim) then
10218                   return Node (E);
10219                end if;
10220
10221                Next_Discriminant (D);
10222                Next_Elmt (E);
10223             end loop;
10224          end if;
10225
10226          --  Something is wrong if we did not find the value
10227
10228          raise Program_Error;
10229       end Get_Discr_Value;
10230
10231       ---------------------
10232       -- Is_Discriminant --
10233       ---------------------
10234
10235       function Is_Discriminant (Expr : Node_Id) return Boolean is
10236          Discrim_Scope : Entity_Id;
10237
10238       begin
10239          if Denotes_Discriminant (Expr) then
10240             Discrim_Scope := Scope (Entity (Expr));
10241
10242             --  Either we have a reference to one of Typ's discriminants,
10243
10244             pragma Assert (Discrim_Scope = Typ
10245
10246                --  or to the discriminants of the parent type, in the case
10247                --  of a derivation of a tagged type with variants.
10248
10249                or else Discrim_Scope = Etype (Typ)
10250                or else Full_View (Discrim_Scope) = Etype (Typ)
10251
10252                --  or same as above for the case where the discriminants
10253                --  were declared in Typ's private view.
10254
10255                or else (Is_Private_Type (Discrim_Scope)
10256                         and then Chars (Discrim_Scope) = Chars (Typ))
10257
10258                --  or else we are deriving from the full view and the
10259                --  discriminant is declared in the private entity.
10260
10261                or else (Is_Private_Type (Typ)
10262                          and then Chars (Discrim_Scope) = Chars (Typ))
10263
10264                --  Or we are constrained the corresponding record of a
10265                --  synchronized type that completes a private declaration.
10266
10267                or else (Is_Concurrent_Record_Type (Typ)
10268                          and then
10269                            Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
10270
10271                --  or we have a class-wide type, in which case make sure the
10272                --  discriminant found belongs to the root type.
10273
10274                or else (Is_Class_Wide_Type (Typ)
10275                          and then Etype (Typ) = Discrim_Scope));
10276
10277             return True;
10278          end if;
10279
10280          --  In all other cases we have something wrong
10281
10282          return False;
10283       end Is_Discriminant;
10284
10285    --  Start of processing for Constrain_Component_Type
10286
10287    begin
10288       if Nkind (Parent (Comp)) = N_Component_Declaration
10289         and then Comes_From_Source (Parent (Comp))
10290         and then Comes_From_Source
10291           (Subtype_Indication (Component_Definition (Parent (Comp))))
10292         and then
10293           Is_Entity_Name
10294             (Subtype_Indication (Component_Definition (Parent (Comp))))
10295       then
10296          return Compon_Type;
10297
10298       elsif Is_Array_Type (Compon_Type) then
10299          return Build_Constrained_Array_Type (Compon_Type);
10300
10301       elsif Has_Discriminants (Compon_Type) then
10302          return Build_Constrained_Discriminated_Type (Compon_Type);
10303
10304       elsif Is_Access_Type (Compon_Type) then
10305          return Build_Constrained_Access_Type (Compon_Type);
10306
10307       else
10308          return Compon_Type;
10309       end if;
10310    end Constrain_Component_Type;
10311
10312    --------------------------
10313    -- Constrain_Concurrent --
10314    --------------------------
10315
10316    --  For concurrent types, the associated record value type carries the same
10317    --  discriminants, so when we constrain a concurrent type, we must constrain
10318    --  the corresponding record type as well.
10319
10320    procedure Constrain_Concurrent
10321      (Def_Id      : in out Entity_Id;
10322       SI          : Node_Id;
10323       Related_Nod : Node_Id;
10324       Related_Id  : Entity_Id;
10325       Suffix      : Character)
10326    is
10327       T_Ent : Entity_Id := Entity (Subtype_Mark (SI));
10328       T_Val : Entity_Id;
10329
10330    begin
10331       if Ekind (T_Ent) in Access_Kind then
10332          T_Ent := Designated_Type (T_Ent);
10333       end if;
10334
10335       T_Val := Corresponding_Record_Type (T_Ent);
10336
10337       if Present (T_Val) then
10338
10339          if No (Def_Id) then
10340             Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
10341          end if;
10342
10343          Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
10344
10345          Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10346          Set_Corresponding_Record_Type (Def_Id,
10347            Constrain_Corresponding_Record
10348              (Def_Id, T_Val, Related_Nod, Related_Id));
10349
10350       else
10351          --  If there is no associated record, expansion is disabled and this
10352          --  is a generic context. Create a subtype in any case, so that
10353          --  semantic analysis can proceed.
10354
10355          if No (Def_Id) then
10356             Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
10357          end if;
10358
10359          Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
10360       end if;
10361    end Constrain_Concurrent;
10362
10363    ------------------------------------
10364    -- Constrain_Corresponding_Record --
10365    ------------------------------------
10366
10367    function Constrain_Corresponding_Record
10368      (Prot_Subt   : Entity_Id;
10369       Corr_Rec    : Entity_Id;
10370       Related_Nod : Node_Id;
10371       Related_Id  : Entity_Id) return Entity_Id
10372    is
10373       T_Sub : constant Entity_Id :=
10374                 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
10375
10376    begin
10377       Set_Etype             (T_Sub, Corr_Rec);
10378       Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
10379       Set_Is_Constrained    (T_Sub, True);
10380       Set_First_Entity      (T_Sub, First_Entity (Corr_Rec));
10381       Set_Last_Entity       (T_Sub, Last_Entity  (Corr_Rec));
10382
10383       --  As elsewhere, we do not want to create a freeze node for this itype
10384       --  if it is created for a constrained component of an enclosing record
10385       --  because references to outer discriminants will appear out of scope.
10386
10387       if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
10388          Conditional_Delay (T_Sub, Corr_Rec);
10389       else
10390          Set_Is_Frozen (T_Sub);
10391       end if;
10392
10393       if Has_Discriminants (Prot_Subt) then -- False only if errors.
10394          Set_Discriminant_Constraint
10395            (T_Sub, Discriminant_Constraint (Prot_Subt));
10396          Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
10397          Create_Constrained_Components
10398            (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
10399       end if;
10400
10401       Set_Depends_On_Private      (T_Sub, Has_Private_Component (T_Sub));
10402
10403       return T_Sub;
10404    end Constrain_Corresponding_Record;
10405
10406    -----------------------
10407    -- Constrain_Decimal --
10408    -----------------------
10409
10410    procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
10411       T           : constant Entity_Id  := Entity (Subtype_Mark (S));
10412       C           : constant Node_Id    := Constraint (S);
10413       Loc         : constant Source_Ptr := Sloc (C);
10414       Range_Expr  : Node_Id;
10415       Digits_Expr : Node_Id;
10416       Digits_Val  : Uint;
10417       Bound_Val   : Ureal;
10418
10419    begin
10420       Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
10421
10422       if Nkind (C) = N_Range_Constraint then
10423          Range_Expr := Range_Expression (C);
10424          Digits_Val := Digits_Value (T);
10425
10426       else
10427          pragma Assert (Nkind (C) = N_Digits_Constraint);
10428          Digits_Expr := Digits_Expression (C);
10429          Analyze_And_Resolve (Digits_Expr, Any_Integer);
10430
10431          Check_Digits_Expression (Digits_Expr);
10432          Digits_Val := Expr_Value (Digits_Expr);
10433
10434          if Digits_Val > Digits_Value (T) then
10435             Error_Msg_N
10436                ("digits expression is incompatible with subtype", C);
10437             Digits_Val := Digits_Value (T);
10438          end if;
10439
10440          if Present (Range_Constraint (C)) then
10441             Range_Expr := Range_Expression (Range_Constraint (C));
10442          else
10443             Range_Expr := Empty;
10444          end if;
10445       end if;
10446
10447       Set_Etype            (Def_Id, Base_Type        (T));
10448       Set_Size_Info        (Def_Id,                  (T));
10449       Set_First_Rep_Item   (Def_Id, First_Rep_Item   (T));
10450       Set_Delta_Value      (Def_Id, Delta_Value      (T));
10451       Set_Scale_Value      (Def_Id, Scale_Value      (T));
10452       Set_Small_Value      (Def_Id, Small_Value      (T));
10453       Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
10454       Set_Digits_Value     (Def_Id, Digits_Val);
10455
10456       --  Manufacture range from given digits value if no range present
10457
10458       if No (Range_Expr) then
10459          Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
10460          Range_Expr :=
10461            Make_Range (Loc,
10462              Low_Bound =>
10463                Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
10464              High_Bound =>
10465                Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
10466       end if;
10467
10468       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
10469       Set_Discrete_RM_Size (Def_Id);
10470
10471       --  Unconditionally delay the freeze, since we cannot set size
10472       --  information in all cases correctly until the freeze point.
10473
10474       Set_Has_Delayed_Freeze (Def_Id);
10475    end Constrain_Decimal;
10476
10477    ----------------------------------
10478    -- Constrain_Discriminated_Type --
10479    ----------------------------------
10480
10481    procedure Constrain_Discriminated_Type
10482      (Def_Id      : Entity_Id;
10483       S           : Node_Id;
10484       Related_Nod : Node_Id;
10485       For_Access  : Boolean := False)
10486    is
10487       E     : constant Entity_Id := Entity (Subtype_Mark (S));
10488       T     : Entity_Id;
10489       C     : Node_Id;
10490       Elist : Elist_Id := New_Elmt_List;
10491
10492       procedure Fixup_Bad_Constraint;
10493       --  This is called after finding a bad constraint, and after having
10494       --  posted an appropriate error message. The mission is to leave the
10495       --  entity T in as reasonable state as possible!
10496
10497       --------------------------
10498       -- Fixup_Bad_Constraint --
10499       --------------------------
10500
10501       procedure Fixup_Bad_Constraint is
10502       begin
10503          --  Set a reasonable Ekind for the entity. For an incomplete type,
10504          --  we can't do much, but for other types, we can set the proper
10505          --  corresponding subtype kind.
10506
10507          if Ekind (T) = E_Incomplete_Type then
10508             Set_Ekind (Def_Id, Ekind (T));
10509          else
10510             Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10511          end if;
10512
10513          --  Set Etype to the known type, to reduce chances of cascaded errors
10514
10515          Set_Etype (Def_Id, E);
10516          Set_Error_Posted (Def_Id);
10517       end Fixup_Bad_Constraint;
10518
10519    --  Start of processing for Constrain_Discriminated_Type
10520
10521    begin
10522       C := Constraint (S);
10523
10524       --  A discriminant constraint is only allowed in a subtype indication,
10525       --  after a subtype mark. This subtype mark must denote either a type
10526       --  with discriminants, or an access type whose designated type is a
10527       --  type with discriminants. A discriminant constraint specifies the
10528       --  values of these discriminants (RM 3.7.2(5)).
10529
10530       T := Base_Type (Entity (Subtype_Mark (S)));
10531
10532       if Ekind (T) in Access_Kind then
10533          T := Designated_Type (T);
10534       end if;
10535
10536       --  Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
10537       --  Avoid generating an error for access-to-incomplete subtypes.
10538
10539       if Ada_Version >= Ada_05
10540         and then Ekind (T) = E_Incomplete_Type
10541         and then Nkind (Parent (S)) = N_Subtype_Declaration
10542         and then not Is_Itype (Def_Id)
10543       then
10544          --  A little sanity check, emit an error message if the type
10545          --  has discriminants to begin with. Type T may be a regular
10546          --  incomplete type or imported via a limited with clause.
10547
10548          if Has_Discriminants (T)
10549            or else
10550              (From_With_Type (T)
10551                 and then Present (Non_Limited_View (T))
10552                 and then Nkind (Parent (Non_Limited_View (T))) =
10553                            N_Full_Type_Declaration
10554                 and then Present (Discriminant_Specifications
10555                           (Parent (Non_Limited_View (T)))))
10556          then
10557             Error_Msg_N
10558               ("(Ada 2005) incomplete subtype may not be constrained", C);
10559          else
10560             Error_Msg_N
10561               ("invalid constraint: type has no discriminant", C);
10562          end if;
10563
10564          Fixup_Bad_Constraint;
10565          return;
10566
10567       --  Check that the type has visible discriminants. The type may be
10568       --  a private type with unknown discriminants whose full view has
10569       --  discriminants which are invisible.
10570
10571       elsif not Has_Discriminants (T)
10572         or else
10573           (Has_Unknown_Discriminants (T)
10574              and then Is_Private_Type (T))
10575       then
10576          Error_Msg_N ("invalid constraint: type has no discriminant", C);
10577          Fixup_Bad_Constraint;
10578          return;
10579
10580       elsif Is_Constrained (E)
10581         or else (Ekind (E) = E_Class_Wide_Subtype
10582                   and then Present (Discriminant_Constraint (E)))
10583       then
10584          Error_Msg_N ("type is already constrained", Subtype_Mark (S));
10585          Fixup_Bad_Constraint;
10586          return;
10587       end if;
10588
10589       --  T may be an unconstrained subtype (e.g. a generic actual).
10590       --  Constraint applies to the base type.
10591
10592       T := Base_Type (T);
10593
10594       Elist := Build_Discriminant_Constraints (T, S);
10595
10596       --  If the list returned was empty we had an error in building the
10597       --  discriminant constraint. We have also already signalled an error
10598       --  in the incomplete type case
10599
10600       if Is_Empty_Elmt_List (Elist) then
10601          Fixup_Bad_Constraint;
10602          return;
10603       end if;
10604
10605       Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
10606    end Constrain_Discriminated_Type;
10607
10608    ---------------------------
10609    -- Constrain_Enumeration --
10610    ---------------------------
10611
10612    procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
10613       T : constant Entity_Id := Entity (Subtype_Mark (S));
10614       C : constant Node_Id   := Constraint (S);
10615
10616    begin
10617       Set_Ekind (Def_Id, E_Enumeration_Subtype);
10618
10619       Set_First_Literal     (Def_Id, First_Literal (Base_Type (T)));
10620
10621       Set_Etype             (Def_Id, Base_Type         (T));
10622       Set_Size_Info         (Def_Id,                   (T));
10623       Set_First_Rep_Item    (Def_Id, First_Rep_Item    (T));
10624       Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
10625
10626       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10627
10628       Set_Discrete_RM_Size (Def_Id);
10629    end Constrain_Enumeration;
10630
10631    ----------------------
10632    -- Constrain_Float --
10633    ----------------------
10634
10635    procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
10636       T    : constant Entity_Id := Entity (Subtype_Mark (S));
10637       C    : Node_Id;
10638       D    : Node_Id;
10639       Rais : Node_Id;
10640
10641    begin
10642       Set_Ekind (Def_Id, E_Floating_Point_Subtype);
10643
10644       Set_Etype          (Def_Id, Base_Type      (T));
10645       Set_Size_Info      (Def_Id,                (T));
10646       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10647
10648       --  Process the constraint
10649
10650       C := Constraint (S);
10651
10652       --  Digits constraint present
10653
10654       if Nkind (C) = N_Digits_Constraint then
10655          Check_Restriction (No_Obsolescent_Features, C);
10656
10657          if Warn_On_Obsolescent_Feature then
10658             Error_Msg_N
10659               ("subtype digits constraint is an " &
10660                "obsolescent feature (RM J.3(8))?", C);
10661          end if;
10662
10663          D := Digits_Expression (C);
10664          Analyze_And_Resolve (D, Any_Integer);
10665          Check_Digits_Expression (D);
10666          Set_Digits_Value (Def_Id, Expr_Value (D));
10667
10668          --  Check that digits value is in range. Obviously we can do this
10669          --  at compile time, but it is strictly a runtime check, and of
10670          --  course there is an ACVC test that checks this!
10671
10672          if Digits_Value (Def_Id) > Digits_Value (T) then
10673             Error_Msg_Uint_1 := Digits_Value (T);
10674             Error_Msg_N ("?digits value is too large, maximum is ^", D);
10675             Rais :=
10676               Make_Raise_Constraint_Error (Sloc (D),
10677                 Reason => CE_Range_Check_Failed);
10678             Insert_Action (Declaration_Node (Def_Id), Rais);
10679          end if;
10680
10681          C := Range_Constraint (C);
10682
10683       --  No digits constraint present
10684
10685       else
10686          Set_Digits_Value (Def_Id, Digits_Value (T));
10687       end if;
10688
10689       --  Range constraint present
10690
10691       if Nkind (C) = N_Range_Constraint then
10692          Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10693
10694       --  No range constraint present
10695
10696       else
10697          pragma Assert (No (C));
10698          Set_Scalar_Range (Def_Id, Scalar_Range (T));
10699       end if;
10700
10701       Set_Is_Constrained (Def_Id);
10702    end Constrain_Float;
10703
10704    ---------------------
10705    -- Constrain_Index --
10706    ---------------------
10707
10708    procedure Constrain_Index
10709      (Index        : Node_Id;
10710       S            : Node_Id;
10711       Related_Nod  : Node_Id;
10712       Related_Id   : Entity_Id;
10713       Suffix       : Character;
10714       Suffix_Index : Nat)
10715    is
10716       Def_Id : Entity_Id;
10717       R      : Node_Id := Empty;
10718       T      : constant Entity_Id := Etype (Index);
10719
10720    begin
10721       if Nkind (S) = N_Range
10722         or else
10723           (Nkind (S) = N_Attribute_Reference
10724             and then Attribute_Name (S) = Name_Range)
10725       then
10726          --  A Range attribute will transformed into N_Range by Resolve
10727
10728          Analyze (S);
10729          Set_Etype (S, T);
10730          R := S;
10731
10732          Process_Range_Expr_In_Decl (R, T, Empty_List);
10733
10734          if not Error_Posted (S)
10735            and then
10736              (Nkind (S) /= N_Range
10737                or else not Covers (T, (Etype (Low_Bound (S))))
10738                or else not Covers (T, (Etype (High_Bound (S)))))
10739          then
10740             if Base_Type (T) /= Any_Type
10741               and then Etype (Low_Bound (S)) /= Any_Type
10742               and then Etype (High_Bound (S)) /= Any_Type
10743             then
10744                Error_Msg_N ("range expected", S);
10745             end if;
10746          end if;
10747
10748       elsif Nkind (S) = N_Subtype_Indication then
10749
10750          --  The parser has verified that this is a discrete indication
10751
10752          Resolve_Discrete_Subtype_Indication (S, T);
10753          R := Range_Expression (Constraint (S));
10754
10755       elsif Nkind (S) = N_Discriminant_Association then
10756
10757          --  Syntactically valid in subtype indication
10758
10759          Error_Msg_N ("invalid index constraint", S);
10760          Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
10761          return;
10762
10763       --  Subtype_Mark case, no anonymous subtypes to construct
10764
10765       else
10766          Analyze (S);
10767
10768          if Is_Entity_Name (S) then
10769             if not Is_Type (Entity (S)) then
10770                Error_Msg_N ("expect subtype mark for index constraint", S);
10771
10772             elsif Base_Type (Entity (S)) /= Base_Type (T) then
10773                Wrong_Type (S, Base_Type (T));
10774             end if;
10775
10776             return;
10777
10778          else
10779             Error_Msg_N ("invalid index constraint", S);
10780             Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
10781             return;
10782          end if;
10783       end if;
10784
10785       Def_Id :=
10786         Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
10787
10788       Set_Etype (Def_Id, Base_Type (T));
10789
10790       if Is_Modular_Integer_Type (T) then
10791          Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
10792
10793       elsif Is_Integer_Type (T) then
10794          Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
10795
10796       else
10797          Set_Ekind (Def_Id, E_Enumeration_Subtype);
10798          Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
10799       end if;
10800
10801       Set_Size_Info      (Def_Id,                (T));
10802       Set_RM_Size        (Def_Id, RM_Size        (T));
10803       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10804
10805       Set_Scalar_Range   (Def_Id, R);
10806
10807       Set_Etype (S, Def_Id);
10808       Set_Discrete_RM_Size (Def_Id);
10809    end Constrain_Index;
10810
10811    -----------------------
10812    -- Constrain_Integer --
10813    -----------------------
10814
10815    procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
10816       T : constant Entity_Id := Entity (Subtype_Mark (S));
10817       C : constant Node_Id   := Constraint (S);
10818
10819    begin
10820       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10821
10822       if Is_Modular_Integer_Type (T) then
10823          Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
10824       else
10825          Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
10826       end if;
10827
10828       Set_Etype            (Def_Id, Base_Type        (T));
10829       Set_Size_Info        (Def_Id,                  (T));
10830       Set_First_Rep_Item   (Def_Id, First_Rep_Item   (T));
10831       Set_Discrete_RM_Size (Def_Id);
10832    end Constrain_Integer;
10833
10834    ------------------------------
10835    -- Constrain_Ordinary_Fixed --
10836    ------------------------------
10837
10838    procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
10839       T    : constant Entity_Id := Entity (Subtype_Mark (S));
10840       C    : Node_Id;
10841       D    : Node_Id;
10842       Rais : Node_Id;
10843
10844    begin
10845       Set_Ekind          (Def_Id, E_Ordinary_Fixed_Point_Subtype);
10846       Set_Etype          (Def_Id, Base_Type        (T));
10847       Set_Size_Info      (Def_Id,                  (T));
10848       Set_First_Rep_Item (Def_Id, First_Rep_Item   (T));
10849       Set_Small_Value    (Def_Id, Small_Value      (T));
10850
10851       --  Process the constraint
10852
10853       C := Constraint (S);
10854
10855       --  Delta constraint present
10856
10857       if Nkind (C) = N_Delta_Constraint then
10858          Check_Restriction (No_Obsolescent_Features, C);
10859
10860          if Warn_On_Obsolescent_Feature then
10861             Error_Msg_S
10862               ("subtype delta constraint is an " &
10863                "obsolescent feature (RM J.3(7))?");
10864          end if;
10865
10866          D := Delta_Expression (C);
10867          Analyze_And_Resolve (D, Any_Real);
10868          Check_Delta_Expression (D);
10869          Set_Delta_Value (Def_Id, Expr_Value_R (D));
10870
10871          --  Check that delta value is in range. Obviously we can do this
10872          --  at compile time, but it is strictly a runtime check, and of
10873          --  course there is an ACVC test that checks this!
10874
10875          if Delta_Value (Def_Id) < Delta_Value (T) then
10876             Error_Msg_N ("?delta value is too small", D);
10877             Rais :=
10878               Make_Raise_Constraint_Error (Sloc (D),
10879                 Reason => CE_Range_Check_Failed);
10880             Insert_Action (Declaration_Node (Def_Id), Rais);
10881          end if;
10882
10883          C := Range_Constraint (C);
10884
10885       --  No delta constraint present
10886
10887       else
10888          Set_Delta_Value (Def_Id, Delta_Value (T));
10889       end if;
10890
10891       --  Range constraint present
10892
10893       if Nkind (C) = N_Range_Constraint then
10894          Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10895
10896       --  No range constraint present
10897
10898       else
10899          pragma Assert (No (C));
10900          Set_Scalar_Range (Def_Id, Scalar_Range (T));
10901
10902       end if;
10903
10904       Set_Discrete_RM_Size (Def_Id);
10905
10906       --  Unconditionally delay the freeze, since we cannot set size
10907       --  information in all cases correctly until the freeze point.
10908
10909       Set_Has_Delayed_Freeze (Def_Id);
10910    end Constrain_Ordinary_Fixed;
10911
10912    -----------------------
10913    -- Contain_Interface --
10914    -----------------------
10915
10916    function Contain_Interface
10917      (Iface  : Entity_Id;
10918       Ifaces : Elist_Id) return Boolean
10919    is
10920       Iface_Elmt : Elmt_Id;
10921
10922    begin
10923       if Present (Ifaces) then
10924          Iface_Elmt := First_Elmt (Ifaces);
10925          while Present (Iface_Elmt) loop
10926             if Node (Iface_Elmt) = Iface then
10927                return True;
10928             end if;
10929
10930             Next_Elmt (Iface_Elmt);
10931          end loop;
10932       end if;
10933
10934       return False;
10935    end Contain_Interface;
10936
10937    ---------------------------
10938    -- Convert_Scalar_Bounds --
10939    ---------------------------
10940
10941    procedure Convert_Scalar_Bounds
10942      (N            : Node_Id;
10943       Parent_Type  : Entity_Id;
10944       Derived_Type : Entity_Id;
10945       Loc          : Source_Ptr)
10946    is
10947       Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
10948
10949       Lo  : Node_Id;
10950       Hi  : Node_Id;
10951       Rng : Node_Id;
10952
10953    begin
10954       Lo := Build_Scalar_Bound
10955               (Type_Low_Bound (Derived_Type),
10956                Parent_Type, Implicit_Base);
10957
10958       Hi := Build_Scalar_Bound
10959               (Type_High_Bound (Derived_Type),
10960                Parent_Type, Implicit_Base);
10961
10962       Rng :=
10963         Make_Range (Loc,
10964           Low_Bound  => Lo,
10965           High_Bound => Hi);
10966
10967       Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
10968
10969       Set_Parent (Rng, N);
10970       Set_Scalar_Range (Derived_Type, Rng);
10971
10972       --  Analyze the bounds
10973
10974       Analyze_And_Resolve (Lo, Implicit_Base);
10975       Analyze_And_Resolve (Hi, Implicit_Base);
10976
10977       --  Analyze the range itself, except that we do not analyze it if
10978       --  the bounds are real literals, and we have a fixed-point type.
10979       --  The reason for this is that we delay setting the bounds in this
10980       --  case till we know the final Small and Size values (see circuit
10981       --  in Freeze.Freeze_Fixed_Point_Type for further details).
10982
10983       if Is_Fixed_Point_Type (Parent_Type)
10984         and then Nkind (Lo) = N_Real_Literal
10985         and then Nkind (Hi) = N_Real_Literal
10986       then
10987          return;
10988
10989       --  Here we do the analysis of the range
10990
10991       --  Note: we do this manually, since if we do a normal Analyze and
10992       --  Resolve call, there are problems with the conversions used for
10993       --  the derived type range.
10994
10995       else
10996          Set_Etype    (Rng, Implicit_Base);
10997          Set_Analyzed (Rng, True);
10998       end if;
10999    end Convert_Scalar_Bounds;
11000
11001    -------------------
11002    -- Copy_And_Swap --
11003    -------------------
11004
11005    procedure Copy_And_Swap (Priv, Full : Entity_Id) is
11006    begin
11007       --  Initialize new full declaration entity by copying the pertinent
11008       --  fields of the corresponding private declaration entity.
11009
11010       --  We temporarily set Ekind to a value appropriate for a type to
11011       --  avoid assert failures in Einfo from checking for setting type
11012       --  attributes on something that is not a type. Ekind (Priv) is an
11013       --  appropriate choice, since it allowed the attributes to be set
11014       --  in the first place. This Ekind value will be modified later.
11015
11016       Set_Ekind (Full, Ekind (Priv));
11017
11018       --  Also set Etype temporarily to Any_Type, again, in the absence
11019       --  of errors, it will be properly reset, and if there are errors,
11020       --  then we want a value of Any_Type to remain.
11021
11022       Set_Etype (Full, Any_Type);
11023
11024       --  Now start copying attributes
11025
11026       Set_Has_Discriminants          (Full, Has_Discriminants       (Priv));
11027
11028       if Has_Discriminants (Full) then
11029          Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
11030          Set_Stored_Constraint       (Full, Stored_Constraint       (Priv));
11031       end if;
11032
11033       Set_First_Rep_Item             (Full, First_Rep_Item          (Priv));
11034       Set_Homonym                    (Full, Homonym                 (Priv));
11035       Set_Is_Immediately_Visible     (Full, Is_Immediately_Visible  (Priv));
11036       Set_Is_Public                  (Full, Is_Public               (Priv));
11037       Set_Is_Pure                    (Full, Is_Pure                 (Priv));
11038       Set_Is_Tagged_Type             (Full, Is_Tagged_Type          (Priv));
11039       Set_Has_Pragma_Unreferenced    (Full, Has_Pragma_Unreferenced (Priv));
11040       Set_Has_Pragma_Unreferenced_Objects
11041                                      (Full, Has_Pragma_Unreferenced_Objects
11042                                                                     (Priv));
11043
11044       Conditional_Delay              (Full,                          Priv);
11045
11046       if Is_Tagged_Type (Full) then
11047          Set_Primitive_Operations    (Full, Primitive_Operations    (Priv));
11048
11049          if Priv = Base_Type (Priv) then
11050             Set_Class_Wide_Type      (Full, Class_Wide_Type         (Priv));
11051          end if;
11052       end if;
11053
11054       Set_Is_Volatile                (Full, Is_Volatile             (Priv));
11055       Set_Treat_As_Volatile          (Full, Treat_As_Volatile       (Priv));
11056       Set_Scope                      (Full, Scope                   (Priv));
11057       Set_Next_Entity                (Full, Next_Entity             (Priv));
11058       Set_First_Entity               (Full, First_Entity            (Priv));
11059       Set_Last_Entity                (Full, Last_Entity             (Priv));
11060
11061       --  If access types have been recorded for later handling, keep them in
11062       --  the full view so that they get handled when the full view freeze
11063       --  node is expanded.
11064
11065       if Present (Freeze_Node (Priv))
11066         and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
11067       then
11068          Ensure_Freeze_Node (Full);
11069          Set_Access_Types_To_Process
11070            (Freeze_Node (Full),
11071             Access_Types_To_Process (Freeze_Node (Priv)));
11072       end if;
11073
11074       --  Swap the two entities. Now Privat is the full type entity and
11075       --  Full is the private one. They will be swapped back at the end
11076       --  of the private part. This swapping ensures that the entity that
11077       --  is visible in the private part is the full declaration.
11078
11079       Exchange_Entities (Priv, Full);
11080       Append_Entity (Full, Scope (Full));
11081    end Copy_And_Swap;
11082
11083    -------------------------------------
11084    -- Copy_Array_Base_Type_Attributes --
11085    -------------------------------------
11086
11087    procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
11088    begin
11089       Set_Component_Alignment      (T1, Component_Alignment      (T2));
11090       Set_Component_Type           (T1, Component_Type           (T2));
11091       Set_Component_Size           (T1, Component_Size           (T2));
11092       Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
11093       Set_Finalize_Storage_Only    (T1, Finalize_Storage_Only    (T2));
11094       Set_Has_Non_Standard_Rep     (T1, Has_Non_Standard_Rep     (T2));
11095       Set_Has_Task                 (T1, Has_Task                 (T2));
11096       Set_Is_Packed                (T1, Is_Packed                (T2));
11097       Set_Has_Aliased_Components   (T1, Has_Aliased_Components   (T2));
11098       Set_Has_Atomic_Components    (T1, Has_Atomic_Components    (T2));
11099       Set_Has_Volatile_Components  (T1, Has_Volatile_Components  (T2));
11100    end Copy_Array_Base_Type_Attributes;
11101
11102    -----------------------------------
11103    -- Copy_Array_Subtype_Attributes --
11104    -----------------------------------
11105
11106    procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
11107    begin
11108       Set_Size_Info (T1, T2);
11109
11110       Set_First_Index          (T1, First_Index           (T2));
11111       Set_Is_Aliased           (T1, Is_Aliased            (T2));
11112       Set_Is_Atomic            (T1, Is_Atomic             (T2));
11113       Set_Is_Volatile          (T1, Is_Volatile           (T2));
11114       Set_Treat_As_Volatile    (T1, Treat_As_Volatile     (T2));
11115       Set_Is_Constrained       (T1, Is_Constrained        (T2));
11116       Set_Depends_On_Private   (T1, Has_Private_Component (T2));
11117       Set_First_Rep_Item       (T1, First_Rep_Item        (T2));
11118       Set_Convention           (T1, Convention            (T2));
11119       Set_Is_Limited_Composite (T1, Is_Limited_Composite  (T2));
11120       Set_Is_Private_Composite (T1, Is_Private_Composite  (T2));
11121    end Copy_Array_Subtype_Attributes;
11122
11123    -----------------------------------
11124    -- Create_Constrained_Components --
11125    -----------------------------------
11126
11127    procedure Create_Constrained_Components
11128      (Subt        : Entity_Id;
11129       Decl_Node   : Node_Id;
11130       Typ         : Entity_Id;
11131       Constraints : Elist_Id)
11132    is
11133       Loc         : constant Source_Ptr := Sloc (Subt);
11134       Comp_List   : constant Elist_Id   := New_Elmt_List;
11135       Parent_Type : constant Entity_Id  := Etype (Typ);
11136       Assoc_List  : constant List_Id    := New_List;
11137       Discr_Val   : Elmt_Id;
11138       Errors      : Boolean;
11139       New_C       : Entity_Id;
11140       Old_C       : Entity_Id;
11141       Is_Static   : Boolean := True;
11142
11143       procedure Collect_Fixed_Components (Typ : Entity_Id);
11144       --  Collect parent type components that do not appear in a variant part
11145
11146       procedure Create_All_Components;
11147       --  Iterate over Comp_List to create the components of the subtype
11148
11149       function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
11150       --  Creates a new component from Old_Compon, copying all the fields from
11151       --  it, including its Etype, inserts the new component in the Subt entity
11152       --  chain and returns the new component.
11153
11154       function Is_Variant_Record (T : Entity_Id) return Boolean;
11155       --  If true, and discriminants are static, collect only components from
11156       --  variants selected by discriminant values.
11157
11158       ------------------------------
11159       -- Collect_Fixed_Components --
11160       ------------------------------
11161
11162       procedure Collect_Fixed_Components (Typ : Entity_Id) is
11163       begin
11164       --  Build association list for discriminants, and find components of the
11165       --  variant part selected by the values of the discriminants.
11166
11167          Old_C := First_Discriminant (Typ);
11168          Discr_Val := First_Elmt (Constraints);
11169          while Present (Old_C) loop
11170             Append_To (Assoc_List,
11171               Make_Component_Association (Loc,
11172                  Choices    => New_List (New_Occurrence_Of (Old_C, Loc)),
11173                  Expression => New_Copy (Node (Discr_Val))));
11174
11175             Next_Elmt (Discr_Val);
11176             Next_Discriminant (Old_C);
11177          end loop;
11178
11179          --  The tag, and the possible parent and controller components
11180          --  are unconditionally in the subtype.
11181
11182          if Is_Tagged_Type (Typ)
11183            or else Has_Controlled_Component (Typ)
11184          then
11185             Old_C := First_Component (Typ);
11186             while Present (Old_C) loop
11187                if Chars ((Old_C)) = Name_uTag
11188                  or else Chars ((Old_C)) = Name_uParent
11189                  or else Chars ((Old_C)) = Name_uController
11190                then
11191                   Append_Elmt (Old_C, Comp_List);
11192                end if;
11193
11194                Next_Component (Old_C);
11195             end loop;
11196          end if;
11197       end Collect_Fixed_Components;
11198
11199       ---------------------------
11200       -- Create_All_Components --
11201       ---------------------------
11202
11203       procedure Create_All_Components is
11204          Comp : Elmt_Id;
11205
11206       begin
11207          Comp := First_Elmt (Comp_List);
11208          while Present (Comp) loop
11209             Old_C := Node (Comp);
11210             New_C := Create_Component (Old_C);
11211
11212             Set_Etype
11213               (New_C,
11214                Constrain_Component_Type
11215                  (Old_C, Subt, Decl_Node, Typ, Constraints));
11216             Set_Is_Public (New_C, Is_Public (Subt));
11217
11218             Next_Elmt (Comp);
11219          end loop;
11220       end Create_All_Components;
11221
11222       ----------------------
11223       -- Create_Component --
11224       ----------------------
11225
11226       function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
11227          New_Compon : constant Entity_Id := New_Copy (Old_Compon);
11228
11229       begin
11230          if Ekind (Old_Compon) = E_Discriminant
11231            and then Is_Completely_Hidden (Old_Compon)
11232          then
11233             --  This is a shadow discriminant created for a discriminant of
11234             --  the parent type, which needs to be present in the subtype.
11235             --  Give the shadow discriminant an internal name that cannot
11236             --  conflict with that of visible components.
11237
11238             Set_Chars (New_Compon, New_Internal_Name ('C'));
11239          end if;
11240
11241          --  Set the parent so we have a proper link for freezing etc. This is
11242          --  not a real parent pointer, since of course our parent does not own
11243          --  up to us and reference us, we are an illegitimate child of the
11244          --  original parent!
11245
11246          Set_Parent (New_Compon, Parent (Old_Compon));
11247
11248          --  If the old component's Esize was already determined and is a
11249          --  static value, then the new component simply inherits it. Otherwise
11250          --  the old component's size may require run-time determination, but
11251          --  the new component's size still might be statically determinable
11252          --  (if, for example it has a static constraint). In that case we want
11253          --  Layout_Type to recompute the component's size, so we reset its
11254          --  size and positional fields.
11255
11256          if Frontend_Layout_On_Target
11257            and then not Known_Static_Esize (Old_Compon)
11258          then
11259             Set_Esize (New_Compon, Uint_0);
11260             Init_Normalized_First_Bit    (New_Compon);
11261             Init_Normalized_Position     (New_Compon);
11262             Init_Normalized_Position_Max (New_Compon);
11263          end if;
11264
11265          --  We do not want this node marked as Comes_From_Source, since
11266          --  otherwise it would get first class status and a separate cross-
11267          --  reference line would be generated. Illegitimate children do not
11268          --  rate such recognition.
11269
11270          Set_Comes_From_Source (New_Compon, False);
11271
11272          --  But it is a real entity, and a birth certificate must be properly
11273          --  registered by entering it into the entity list.
11274
11275          Enter_Name (New_Compon);
11276
11277          return New_Compon;
11278       end Create_Component;
11279
11280       -----------------------
11281       -- Is_Variant_Record --
11282       -----------------------
11283
11284       function Is_Variant_Record (T : Entity_Id) return Boolean is
11285       begin
11286          return Nkind (Parent (T)) = N_Full_Type_Declaration
11287            and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
11288            and then Present (Component_List (Type_Definition (Parent (T))))
11289            and then
11290              Present
11291                (Variant_Part (Component_List (Type_Definition (Parent (T)))));
11292       end Is_Variant_Record;
11293
11294    --  Start of processing for Create_Constrained_Components
11295
11296    begin
11297       pragma Assert (Subt /= Base_Type (Subt));
11298       pragma Assert (Typ = Base_Type (Typ));
11299
11300       Set_First_Entity (Subt, Empty);
11301       Set_Last_Entity  (Subt, Empty);
11302
11303       --  Check whether constraint is fully static, in which case we can
11304       --  optimize the list of components.
11305
11306       Discr_Val := First_Elmt (Constraints);
11307       while Present (Discr_Val) loop
11308          if not Is_OK_Static_Expression (Node (Discr_Val)) then
11309             Is_Static := False;
11310             exit;
11311          end if;
11312
11313          Next_Elmt (Discr_Val);
11314       end loop;
11315
11316       Set_Has_Static_Discriminants (Subt, Is_Static);
11317
11318       Push_Scope (Subt);
11319
11320       --  Inherit the discriminants of the parent type
11321
11322       Add_Discriminants : declare
11323          Num_Disc : Int;
11324          Num_Gird : Int;
11325
11326       begin
11327          Num_Disc := 0;
11328          Old_C := First_Discriminant (Typ);
11329
11330          while Present (Old_C) loop
11331             Num_Disc := Num_Disc + 1;
11332             New_C := Create_Component (Old_C);
11333             Set_Is_Public (New_C, Is_Public (Subt));
11334             Next_Discriminant (Old_C);
11335          end loop;
11336
11337          --  For an untagged derived subtype, the number of discriminants may
11338          --  be smaller than the number of inherited discriminants, because
11339          --  several of them may be renamed by a single new discriminant or
11340          --  constrained. In this case, add the hidden discriminants back into
11341          --  the subtype, because they need to be present if the optimizer of
11342          --  the GCC 4.x back-end decides to break apart assignments between
11343          --  objects using the parent view into member-wise assignments.
11344
11345          Num_Gird := 0;
11346
11347          if Is_Derived_Type (Typ)
11348            and then not Is_Tagged_Type (Typ)
11349          then
11350             Old_C := First_Stored_Discriminant (Typ);
11351
11352             while Present (Old_C) loop
11353                Num_Gird := Num_Gird + 1;
11354                Next_Stored_Discriminant (Old_C);
11355             end loop;
11356          end if;
11357
11358          if Num_Gird > Num_Disc then
11359
11360             --  Find out multiple uses of new discriminants, and add hidden
11361             --  components for the extra renamed discriminants. We recognize
11362             --  multiple uses through the Corresponding_Discriminant of a
11363             --  new discriminant: if it constrains several old discriminants,
11364             --  this field points to the last one in the parent type. The
11365             --  stored discriminants of the derived type have the same name
11366             --  as those of the parent.
11367
11368             declare
11369                Constr    : Elmt_Id;
11370                New_Discr : Entity_Id;
11371                Old_Discr : Entity_Id;
11372
11373             begin
11374                Constr    := First_Elmt (Stored_Constraint (Typ));
11375                Old_Discr := First_Stored_Discriminant (Typ);
11376                while Present (Constr) loop
11377                   if Is_Entity_Name (Node (Constr))
11378                     and then Ekind (Entity (Node (Constr))) = E_Discriminant
11379                   then
11380                      New_Discr := Entity (Node (Constr));
11381
11382                      if Chars (Corresponding_Discriminant (New_Discr)) /=
11383                         Chars (Old_Discr)
11384                      then
11385                         --  The new discriminant has been used to rename a
11386                         --  subsequent old discriminant. Introduce a shadow
11387                         --  component for the current old discriminant.
11388
11389                         New_C := Create_Component (Old_Discr);
11390                         Set_Original_Record_Component (New_C, Old_Discr);
11391                      end if;
11392
11393                   else
11394                      --  The constraint has eliminated the old discriminant.
11395                      --  Introduce a shadow component.
11396
11397                      New_C := Create_Component (Old_Discr);
11398                      Set_Original_Record_Component (New_C, Old_Discr);
11399                   end if;
11400
11401                   Next_Elmt (Constr);
11402                   Next_Stored_Discriminant (Old_Discr);
11403                end loop;
11404             end;
11405          end if;
11406       end Add_Discriminants;
11407
11408       if Is_Static
11409         and then Is_Variant_Record (Typ)
11410       then
11411          Collect_Fixed_Components (Typ);
11412
11413          Gather_Components (
11414            Typ,
11415            Component_List (Type_Definition (Parent (Typ))),
11416            Governed_By   => Assoc_List,
11417            Into          => Comp_List,
11418            Report_Errors => Errors);
11419          pragma Assert (not Errors);
11420
11421          Create_All_Components;
11422
11423       --  If the subtype declaration is created for a tagged type derivation
11424       --  with constraints, we retrieve the record definition of the parent
11425       --  type to select the components of the proper variant.
11426
11427       elsif Is_Static
11428         and then Is_Tagged_Type (Typ)
11429         and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
11430         and then
11431           Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
11432         and then Is_Variant_Record (Parent_Type)
11433       then
11434          Collect_Fixed_Components (Typ);
11435
11436          Gather_Components (
11437            Typ,
11438            Component_List (Type_Definition (Parent (Parent_Type))),
11439            Governed_By   => Assoc_List,
11440            Into          => Comp_List,
11441            Report_Errors => Errors);
11442          pragma Assert (not Errors);
11443
11444          --  If the tagged derivation has a type extension, collect all the
11445          --  new components therein.
11446
11447          if Present
11448               (Record_Extension_Part (Type_Definition (Parent (Typ))))
11449          then
11450             Old_C := First_Component (Typ);
11451             while Present (Old_C) loop
11452                if Original_Record_Component (Old_C) = Old_C
11453                 and then Chars (Old_C) /= Name_uTag
11454                 and then Chars (Old_C) /= Name_uParent
11455                 and then Chars (Old_C) /= Name_uController
11456                then
11457                   Append_Elmt (Old_C, Comp_List);
11458                end if;
11459
11460                Next_Component (Old_C);
11461             end loop;
11462          end if;
11463
11464          Create_All_Components;
11465
11466       else
11467          --  If discriminants are not static, or if this is a multi-level type
11468          --  extension, we have to include all components of the parent type.
11469
11470          Old_C := First_Component (Typ);
11471          while Present (Old_C) loop
11472             New_C := Create_Component (Old_C);
11473
11474             Set_Etype
11475               (New_C,
11476                Constrain_Component_Type
11477                  (Old_C, Subt, Decl_Node, Typ, Constraints));
11478             Set_Is_Public (New_C, Is_Public (Subt));
11479
11480             Next_Component (Old_C);
11481          end loop;
11482       end if;
11483
11484       End_Scope;
11485    end Create_Constrained_Components;
11486
11487    ------------------------------------------
11488    -- Decimal_Fixed_Point_Type_Declaration --
11489    ------------------------------------------
11490
11491    procedure Decimal_Fixed_Point_Type_Declaration
11492      (T   : Entity_Id;
11493       Def : Node_Id)
11494    is
11495       Loc           : constant Source_Ptr := Sloc (Def);
11496       Digs_Expr     : constant Node_Id    := Digits_Expression (Def);
11497       Delta_Expr    : constant Node_Id    := Delta_Expression (Def);
11498       Implicit_Base : Entity_Id;
11499       Digs_Val      : Uint;
11500       Delta_Val     : Ureal;
11501       Scale_Val     : Uint;
11502       Bound_Val     : Ureal;
11503
11504    begin
11505       Check_Restriction (No_Fixed_Point, Def);
11506
11507       --  Create implicit base type
11508
11509       Implicit_Base :=
11510         Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
11511       Set_Etype (Implicit_Base, Implicit_Base);
11512
11513       --  Analyze and process delta expression
11514
11515       Analyze_And_Resolve (Delta_Expr, Universal_Real);
11516
11517       Check_Delta_Expression (Delta_Expr);
11518       Delta_Val := Expr_Value_R (Delta_Expr);
11519
11520       --  Check delta is power of 10, and determine scale value from it
11521
11522       declare
11523          Val : Ureal;
11524
11525       begin
11526          Scale_Val := Uint_0;
11527          Val := Delta_Val;
11528
11529          if Val < Ureal_1 then
11530             while Val < Ureal_1 loop
11531                Val := Val * Ureal_10;
11532                Scale_Val := Scale_Val + 1;
11533             end loop;
11534
11535             if Scale_Val > 18 then
11536                Error_Msg_N ("scale exceeds maximum value of 18", Def);
11537                Scale_Val := UI_From_Int (+18);
11538             end if;
11539
11540          else
11541             while Val > Ureal_1 loop
11542                Val := Val / Ureal_10;
11543                Scale_Val := Scale_Val - 1;
11544             end loop;
11545
11546             if Scale_Val < -18 then
11547                Error_Msg_N ("scale is less than minimum value of -18", Def);
11548                Scale_Val := UI_From_Int (-18);
11549             end if;
11550          end if;
11551
11552          if Val /= Ureal_1 then
11553             Error_Msg_N ("delta expression must be a power of 10", Def);
11554             Delta_Val := Ureal_10 ** (-Scale_Val);
11555          end if;
11556       end;
11557
11558       --  Set delta, scale and small (small = delta for decimal type)
11559
11560       Set_Delta_Value (Implicit_Base, Delta_Val);
11561       Set_Scale_Value (Implicit_Base, Scale_Val);
11562       Set_Small_Value (Implicit_Base, Delta_Val);
11563
11564       --  Analyze and process digits expression
11565
11566       Analyze_And_Resolve (Digs_Expr, Any_Integer);
11567       Check_Digits_Expression (Digs_Expr);
11568       Digs_Val := Expr_Value (Digs_Expr);
11569
11570       if Digs_Val > 18 then
11571          Digs_Val := UI_From_Int (+18);
11572          Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
11573       end if;
11574
11575       Set_Digits_Value (Implicit_Base, Digs_Val);
11576       Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
11577
11578       --  Set range of base type from digits value for now. This will be
11579       --  expanded to represent the true underlying base range by Freeze.
11580
11581       Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
11582
11583       --  Note: We leave size as zero for now, size will be set at freeze
11584       --  time. We have to do this for ordinary fixed-point, because the size
11585       --  depends on the specified small, and we might as well do the same for
11586       --  decimal fixed-point.
11587
11588       pragma Assert (Esize (Implicit_Base) = Uint_0);
11589
11590       --  If there are bounds given in the declaration use them as the
11591       --  bounds of the first named subtype.
11592
11593       if Present (Real_Range_Specification (Def)) then
11594          declare
11595             RRS      : constant Node_Id := Real_Range_Specification (Def);
11596             Low      : constant Node_Id := Low_Bound (RRS);
11597             High     : constant Node_Id := High_Bound (RRS);
11598             Low_Val  : Ureal;
11599             High_Val : Ureal;
11600
11601          begin
11602             Analyze_And_Resolve (Low, Any_Real);
11603             Analyze_And_Resolve (High, Any_Real);
11604             Check_Real_Bound (Low);
11605             Check_Real_Bound (High);
11606             Low_Val := Expr_Value_R (Low);
11607             High_Val := Expr_Value_R (High);
11608
11609             if Low_Val < (-Bound_Val) then
11610                Error_Msg_N
11611                  ("range low bound too small for digits value", Low);
11612                Low_Val := -Bound_Val;
11613             end if;
11614
11615             if High_Val > Bound_Val then
11616                Error_Msg_N
11617                  ("range high bound too large for digits value", High);
11618                High_Val := Bound_Val;
11619             end if;
11620
11621             Set_Fixed_Range (T, Loc, Low_Val, High_Val);
11622          end;
11623
11624       --  If no explicit range, use range that corresponds to given
11625       --  digits value. This will end up as the final range for the
11626       --  first subtype.
11627
11628       else
11629          Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
11630       end if;
11631
11632       --  Complete entity for first subtype
11633
11634       Set_Ekind          (T, E_Decimal_Fixed_Point_Subtype);
11635       Set_Etype          (T, Implicit_Base);
11636       Set_Size_Info      (T, Implicit_Base);
11637       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
11638       Set_Digits_Value   (T, Digs_Val);
11639       Set_Delta_Value    (T, Delta_Val);
11640       Set_Small_Value    (T, Delta_Val);
11641       Set_Scale_Value    (T, Scale_Val);
11642       Set_Is_Constrained (T);
11643    end Decimal_Fixed_Point_Type_Declaration;
11644
11645    -----------------------------------
11646    -- Derive_Progenitor_Subprograms --
11647    -----------------------------------
11648
11649    procedure Derive_Progenitor_Subprograms
11650      (Parent_Type : Entity_Id;
11651       Tagged_Type : Entity_Id)
11652    is
11653       E          : Entity_Id;
11654       Elmt       : Elmt_Id;
11655       Iface      : Entity_Id;
11656       Iface_Elmt : Elmt_Id;
11657       Iface_Subp : Entity_Id;
11658       New_Subp   : Entity_Id := Empty;
11659       Prim_Elmt  : Elmt_Id;
11660       Subp       : Entity_Id;
11661       Typ        : Entity_Id;
11662
11663    begin
11664       pragma Assert (Ada_Version >= Ada_05
11665         and then Is_Record_Type (Tagged_Type)
11666         and then Is_Tagged_Type (Tagged_Type)
11667         and then Has_Interfaces (Tagged_Type));
11668
11669       --  Step 1: Transfer to the full-view primitives associated with the
11670       --  partial-view that cover interface primitives. Conceptually this
11671       --  work should be done later by Process_Full_View; done here to
11672       --  simplify its implementation at later stages. It can be safely
11673       --  done here because interfaces must be visible in the partial and
11674       --  private view (RM 7.3(7.3/2)).
11675
11676       --  Small optimization: This work is only required if the parent is
11677       --  abstract. If the tagged type is not abstract, it cannot have
11678       --  abstract primitives (the only entities in the list of primitives of
11679       --  non-abstract tagged types that can reference abstract primitives
11680       --  through its Alias attribute are the internal entities that have
11681       --  attribute Interface_Alias, and these entities are generated later
11682       --  by Freeze_Record_Type).
11683
11684       if In_Private_Part (Current_Scope)
11685         and then Is_Abstract_Type (Parent_Type)
11686       then
11687          Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
11688          while Present (Elmt) loop
11689             Subp := Node (Elmt);
11690
11691             --  At this stage it is not possible to have entities in the list
11692             --  of primitives that have attribute Interface_Alias
11693
11694             pragma Assert (No (Interface_Alias (Subp)));
11695
11696             Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
11697
11698             if Is_Interface (Typ) then
11699                E := Find_Primitive_Covering_Interface
11700                       (Tagged_Type => Tagged_Type,
11701                        Iface_Prim  => Subp);
11702
11703                if Present (E)
11704                  and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
11705                then
11706                   Replace_Elmt (Elmt, E);
11707                   Remove_Homonym (Subp);
11708                end if;
11709             end if;
11710
11711             Next_Elmt (Elmt);
11712          end loop;
11713       end if;
11714
11715       --  Step 2: Add primitives of progenitors that are not implemented by
11716       --  parents of Tagged_Type
11717
11718       if Present (Interfaces (Base_Type (Tagged_Type))) then
11719          Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
11720          while Present (Iface_Elmt) loop
11721             Iface := Node (Iface_Elmt);
11722
11723             Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
11724             while Present (Prim_Elmt) loop
11725                Iface_Subp := Node (Prim_Elmt);
11726
11727                --  Exclude derivation of predefined primitives except those
11728                --  that come from source. Required to catch declarations of
11729                --  equality operators of interfaces. For example:
11730
11731                --     type Iface is interface;
11732                --     function "=" (Left, Right : Iface) return Boolean;
11733
11734                if not Is_Predefined_Dispatching_Operation (Iface_Subp)
11735                  or else Comes_From_Source (Iface_Subp)
11736                then
11737                   E := Find_Primitive_Covering_Interface
11738                          (Tagged_Type => Tagged_Type,
11739                           Iface_Prim  => Iface_Subp);
11740
11741                   --  If not found we derive a new primitive leaving its alias
11742                   --  attribute referencing the interface primitive
11743
11744                   if No (E) then
11745                      Derive_Subprogram
11746                        (New_Subp, Iface_Subp, Tagged_Type, Iface);
11747
11748                   --  Propagate to the full view interface entities associated
11749                   --  with the partial view
11750
11751                   elsif In_Private_Part (Current_Scope)
11752                     and then Present (Alias (E))
11753                     and then Alias (E) = Iface_Subp
11754                     and then
11755                       List_Containing (Parent (E)) /=
11756                         Private_Declarations
11757                           (Specification
11758                             (Unit_Declaration_Node (Current_Scope)))
11759                   then
11760                      Append_Elmt (E, Primitive_Operations (Tagged_Type));
11761                   end if;
11762                end if;
11763
11764                Next_Elmt (Prim_Elmt);
11765             end loop;
11766
11767             Next_Elmt (Iface_Elmt);
11768          end loop;
11769       end if;
11770    end Derive_Progenitor_Subprograms;
11771
11772    -----------------------
11773    -- Derive_Subprogram --
11774    -----------------------
11775
11776    procedure Derive_Subprogram
11777      (New_Subp     : in out Entity_Id;
11778       Parent_Subp  : Entity_Id;
11779       Derived_Type : Entity_Id;
11780       Parent_Type  : Entity_Id;
11781       Actual_Subp  : Entity_Id := Empty)
11782    is
11783       Formal : Entity_Id;
11784       --  Formal parameter of parent primitive operation
11785
11786       Formal_Of_Actual : Entity_Id;
11787       --  Formal parameter of actual operation, when the derivation is to
11788       --  create a renaming for a primitive operation of an actual in an
11789       --  instantiation.
11790
11791       New_Formal : Entity_Id;
11792       --  Formal of inherited operation
11793
11794       Visible_Subp : Entity_Id := Parent_Subp;
11795
11796       function Is_Private_Overriding return Boolean;
11797       --  If Subp is a private overriding of a visible operation, the inherited
11798       --  operation derives from the overridden op (even though its body is the
11799       --  overriding one) and the inherited operation is visible now. See
11800       --  sem_disp to see the full details of the handling of the overridden
11801       --  subprogram, which is removed from the list of primitive operations of
11802       --  the type. The overridden subprogram is saved locally in Visible_Subp,
11803       --  and used to diagnose abstract operations that need overriding in the
11804       --  derived type.
11805
11806       procedure Replace_Type (Id, New_Id : Entity_Id);
11807       --  When the type is an anonymous access type, create a new access type
11808       --  designating the derived type.
11809
11810       procedure Set_Derived_Name;
11811       --  This procedure sets the appropriate Chars name for New_Subp. This
11812       --  is normally just a copy of the parent name. An exception arises for
11813       --  type support subprograms, where the name is changed to reflect the
11814       --  name of the derived type, e.g. if type foo is derived from type bar,
11815       --  then a procedure barDA is derived with a name fooDA.
11816
11817       ---------------------------
11818       -- Is_Private_Overriding --
11819       ---------------------------
11820
11821       function Is_Private_Overriding return Boolean is
11822          Prev : Entity_Id;
11823
11824       begin
11825          --  If the parent is not a dispatching operation there is no
11826          --  need to investigate overridings
11827
11828          if not Is_Dispatching_Operation (Parent_Subp) then
11829             return False;
11830          end if;
11831
11832          --  The visible operation that is overridden is a homonym of the
11833          --  parent subprogram. We scan the homonym chain to find the one
11834          --  whose alias is the subprogram we are deriving.
11835
11836          Prev := Current_Entity (Parent_Subp);
11837          while Present (Prev) loop
11838             if Ekind (Prev) = Ekind (Parent_Subp)
11839               and then Alias (Prev) = Parent_Subp
11840               and then Scope (Parent_Subp) = Scope (Prev)
11841               and then not Is_Hidden (Prev)
11842             then
11843                Visible_Subp := Prev;
11844                return True;
11845             end if;
11846
11847             Prev := Homonym (Prev);
11848          end loop;
11849
11850          return False;
11851       end Is_Private_Overriding;
11852
11853       ------------------
11854       -- Replace_Type --
11855       ------------------
11856
11857       procedure Replace_Type (Id, New_Id : Entity_Id) is
11858          Acc_Type : Entity_Id;
11859          Par      : constant Node_Id := Parent (Derived_Type);
11860
11861       begin
11862          --  When the type is an anonymous access type, create a new access
11863          --  type designating the derived type. This itype must be elaborated
11864          --  at the point of the derivation, not on subsequent calls that may
11865          --  be out of the proper scope for Gigi, so we insert a reference to
11866          --  it after the derivation.
11867
11868          if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
11869             declare
11870                Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
11871
11872             begin
11873                if Ekind (Desig_Typ) = E_Record_Type_With_Private
11874                  and then Present (Full_View (Desig_Typ))
11875                  and then not Is_Private_Type (Parent_Type)
11876                then
11877                   Desig_Typ := Full_View (Desig_Typ);
11878                end if;
11879
11880                if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
11881
11882                   --  Ada 2005 (AI-251): Handle also derivations of abstract
11883                   --  interface primitives.
11884
11885                  or else (Is_Interface (Desig_Typ)
11886                           and then not Is_Class_Wide_Type (Desig_Typ))
11887                then
11888                   Acc_Type := New_Copy (Etype (Id));
11889                   Set_Etype (Acc_Type, Acc_Type);
11890                   Set_Scope (Acc_Type, New_Subp);
11891
11892                   --  Compute size of anonymous access type
11893
11894                   if Is_Array_Type (Desig_Typ)
11895                     and then not Is_Constrained (Desig_Typ)
11896                   then
11897                      Init_Size (Acc_Type, 2 * System_Address_Size);
11898                   else
11899                      Init_Size (Acc_Type, System_Address_Size);
11900                   end if;
11901
11902                   Init_Alignment (Acc_Type);
11903                   Set_Directly_Designated_Type (Acc_Type, Derived_Type);
11904
11905                   Set_Etype (New_Id, Acc_Type);
11906                   Set_Scope (New_Id, New_Subp);
11907
11908                   --  Create a reference to it
11909                   Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
11910
11911                else
11912                   Set_Etype (New_Id, Etype (Id));
11913                end if;
11914             end;
11915
11916          elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
11917            or else
11918              (Ekind (Etype (Id)) = E_Record_Type_With_Private
11919                and then Present (Full_View (Etype (Id)))
11920                and then
11921                  Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
11922          then
11923             --  Constraint checks on formals are generated during expansion,
11924             --  based on the signature of the original subprogram. The bounds
11925             --  of the derived type are not relevant, and thus we can use
11926             --  the base type for the formals. However, the return type may be
11927             --  used in a context that requires that the proper static bounds
11928             --  be used (a case statement, for example)  and for those cases
11929             --  we must use the derived type (first subtype), not its base.
11930
11931             --  If the derived_type_definition has no constraints, we know that
11932             --  the derived type has the same constraints as the first subtype
11933             --  of the parent, and we can also use it rather than its base,
11934             --  which can lead to more efficient code.
11935
11936             if Etype (Id) = Parent_Type then
11937                if Is_Scalar_Type (Parent_Type)
11938                  and then
11939                    Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
11940                then
11941                   Set_Etype (New_Id, Derived_Type);
11942
11943                elsif Nkind (Par) = N_Full_Type_Declaration
11944                  and then
11945                    Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
11946                  and then
11947                    Is_Entity_Name
11948                      (Subtype_Indication (Type_Definition (Par)))
11949                then
11950                   Set_Etype (New_Id, Derived_Type);
11951
11952                else
11953                   Set_Etype (New_Id, Base_Type (Derived_Type));
11954                end if;
11955
11956             else
11957                Set_Etype (New_Id, Base_Type (Derived_Type));
11958             end if;
11959
11960          --  Ada 2005 (AI-251): Handle derivations of abstract interface
11961          --  primitives.
11962
11963          elsif Is_Interface (Etype (Id))
11964            and then not Is_Class_Wide_Type (Etype (Id))
11965            and then Is_Progenitor (Etype (Id), Derived_Type)
11966          then
11967             Set_Etype (New_Id, Derived_Type);
11968
11969          else
11970             Set_Etype (New_Id, Etype (Id));
11971          end if;
11972       end Replace_Type;
11973
11974       ----------------------
11975       -- Set_Derived_Name --
11976       ----------------------
11977
11978       procedure Set_Derived_Name is
11979          Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
11980       begin
11981          if Nm = TSS_Null then
11982             Set_Chars (New_Subp, Chars (Parent_Subp));
11983          else
11984             Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
11985          end if;
11986       end Set_Derived_Name;
11987
11988       --  Local variables
11989
11990       Parent_Overrides_Interface_Primitive : Boolean := False;
11991
11992    --  Start of processing for Derive_Subprogram
11993
11994    begin
11995       New_Subp :=
11996          New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
11997       Set_Ekind (New_Subp, Ekind (Parent_Subp));
11998
11999       --  Check whether the parent overrides an interface primitive
12000
12001       if Is_Overriding_Operation (Parent_Subp) then
12002          declare
12003             E : Entity_Id := Parent_Subp;
12004          begin
12005             while Present (Overridden_Operation (E)) loop
12006                E := Ultimate_Alias (Overridden_Operation (E));
12007             end loop;
12008
12009             Parent_Overrides_Interface_Primitive :=
12010               Is_Dispatching_Operation (E)
12011                 and then Present (Find_Dispatching_Type (E))
12012                 and then Is_Interface (Find_Dispatching_Type (E));
12013          end;
12014       end if;
12015
12016       --  Check whether the inherited subprogram is a private operation that
12017       --  should be inherited but not yet made visible. Such subprograms can
12018       --  become visible at a later point (e.g., the private part of a public
12019       --  child unit) via Declare_Inherited_Private_Subprograms. If the
12020       --  following predicate is true, then this is not such a private
12021       --  operation and the subprogram simply inherits the name of the parent
12022       --  subprogram. Note the special check for the names of controlled
12023       --  operations, which are currently exempted from being inherited with
12024       --  a hidden name because they must be findable for generation of
12025       --  implicit run-time calls.
12026
12027       if not Is_Hidden (Parent_Subp)
12028         or else Is_Internal (Parent_Subp)
12029         or else Is_Private_Overriding
12030         or else Is_Internal_Name (Chars (Parent_Subp))
12031         or else Chars (Parent_Subp) = Name_Initialize
12032         or else Chars (Parent_Subp) = Name_Adjust
12033         or else Chars (Parent_Subp) = Name_Finalize
12034       then
12035          Set_Derived_Name;
12036
12037       --  If parent is hidden, this can be a regular derivation if the
12038       --  parent is immediately visible in a non-instantiating context,
12039       --  or if we are in the private part of an instance. This test
12040       --  should still be refined ???
12041
12042       --  The test for In_Instance_Not_Visible avoids inheriting the derived
12043       --  operation as a non-visible operation in cases where the parent
12044       --  subprogram might not be visible now, but was visible within the
12045       --  original generic, so it would be wrong to make the inherited
12046       --  subprogram non-visible now. (Not clear if this test is fully
12047       --  correct; are there any cases where we should declare the inherited
12048       --  operation as not visible to avoid it being overridden, e.g., when
12049       --  the parent type is a generic actual with private primitives ???)
12050
12051       --  (they should be treated the same as other private inherited
12052       --  subprograms, but it's not clear how to do this cleanly). ???
12053
12054       elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
12055               and then Is_Immediately_Visible (Parent_Subp)
12056               and then not In_Instance)
12057         or else In_Instance_Not_Visible
12058       then
12059          Set_Derived_Name;
12060
12061       --  Ada 2005 (AI-251): Regular derivation if the parent subprogram
12062       --  overrides an interface primitive because interface primitives
12063       --  must be visible in the partial view of the parent (RM 7.3 (7.3/2))
12064
12065       elsif Parent_Overrides_Interface_Primitive then
12066          Set_Derived_Name;
12067
12068       --  The type is inheriting a private operation, so enter
12069       --  it with a special name so it can't be overridden.
12070
12071       else
12072          Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
12073       end if;
12074
12075       Set_Parent (New_Subp, Parent (Derived_Type));
12076
12077       if Present (Actual_Subp) then
12078          Replace_Type (Actual_Subp, New_Subp);
12079       else
12080          Replace_Type (Parent_Subp, New_Subp);
12081       end if;
12082
12083       Conditional_Delay (New_Subp, Parent_Subp);
12084
12085       --  If we are creating a renaming for a primitive operation of an
12086       --  actual of a generic derived type, we must examine the signature
12087       --  of the actual primitive, not that of the generic formal, which for
12088       --  example may be an interface. However the name and initial value
12089       --  of the inherited operation are those of the formal primitive.
12090
12091       Formal := First_Formal (Parent_Subp);
12092
12093       if Present (Actual_Subp) then
12094          Formal_Of_Actual := First_Formal (Actual_Subp);
12095       else
12096          Formal_Of_Actual := Empty;
12097       end if;
12098
12099       while Present (Formal) loop
12100          New_Formal := New_Copy (Formal);
12101
12102          --  Normally we do not go copying parents, but in the case of
12103          --  formals, we need to link up to the declaration (which is the
12104          --  parameter specification), and it is fine to link up to the
12105          --  original formal's parameter specification in this case.
12106
12107          Set_Parent (New_Formal, Parent (Formal));
12108          Append_Entity (New_Formal, New_Subp);
12109
12110          if Present (Formal_Of_Actual) then
12111             Replace_Type (Formal_Of_Actual, New_Formal);
12112             Next_Formal (Formal_Of_Actual);
12113          else
12114             Replace_Type (Formal, New_Formal);
12115          end if;
12116
12117          Next_Formal (Formal);
12118       end loop;
12119
12120       --  If this derivation corresponds to a tagged generic actual, then
12121       --  primitive operations rename those of the actual. Otherwise the
12122       --  primitive operations rename those of the parent type, If the parent
12123       --  renames an intrinsic operator, so does the new subprogram. We except
12124       --  concatenation, which is always properly typed, and does not get
12125       --  expanded as other intrinsic operations.
12126
12127       if No (Actual_Subp) then
12128          if Is_Intrinsic_Subprogram (Parent_Subp) then
12129             Set_Is_Intrinsic_Subprogram (New_Subp);
12130
12131             if Present (Alias (Parent_Subp))
12132               and then Chars (Parent_Subp) /= Name_Op_Concat
12133             then
12134                Set_Alias (New_Subp, Alias (Parent_Subp));
12135             else
12136                Set_Alias (New_Subp, Parent_Subp);
12137             end if;
12138
12139          else
12140             Set_Alias (New_Subp, Parent_Subp);
12141          end if;
12142
12143       else
12144          Set_Alias (New_Subp, Actual_Subp);
12145       end if;
12146
12147       --  Derived subprograms of a tagged type must inherit the convention
12148       --  of the parent subprogram (a requirement of AI-117). Derived
12149       --  subprograms of untagged types simply get convention Ada by default.
12150
12151       if Is_Tagged_Type (Derived_Type) then
12152          Set_Convention (New_Subp, Convention (Parent_Subp));
12153       end if;
12154
12155       Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
12156       Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
12157
12158       if Ekind (Parent_Subp) = E_Procedure then
12159          Set_Is_Valued_Procedure
12160            (New_Subp, Is_Valued_Procedure (Parent_Subp));
12161       end if;
12162
12163       --  No_Return must be inherited properly. If this is overridden in the
12164       --  case of a dispatching operation, then a check is made in Sem_Disp
12165       --  that the overriding operation is also No_Return (no such check is
12166       --  required for the case of non-dispatching operation.
12167
12168       Set_No_Return (New_Subp, No_Return (Parent_Subp));
12169
12170       --  A derived function with a controlling result is abstract. If the
12171       --  Derived_Type is a nonabstract formal generic derived type, then
12172       --  inherited operations are not abstract: the required check is done at
12173       --  instantiation time. If the derivation is for a generic actual, the
12174       --  function is not abstract unless the actual is.
12175
12176       if Is_Generic_Type (Derived_Type)
12177         and then not Is_Abstract_Type (Derived_Type)
12178       then
12179          null;
12180
12181       --  Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
12182       --  properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
12183
12184       elsif Ada_Version >= Ada_05
12185         and then (Is_Abstract_Subprogram (Alias (New_Subp))
12186                    or else (Is_Tagged_Type (Derived_Type)
12187                             and then Etype (New_Subp) = Derived_Type
12188                             and then not Is_Null_Extension (Derived_Type))
12189                    or else (Is_Tagged_Type (Derived_Type)
12190                             and then Ekind (Etype (New_Subp)) =
12191                                                        E_Anonymous_Access_Type
12192                             and then Designated_Type (Etype (New_Subp)) =
12193                                                        Derived_Type
12194                             and then not Is_Null_Extension (Derived_Type)))
12195         and then No (Actual_Subp)
12196       then
12197          if not Is_Tagged_Type (Derived_Type)
12198            or else Is_Abstract_Type (Derived_Type)
12199            or else Is_Abstract_Subprogram (Alias (New_Subp))
12200          then
12201             Set_Is_Abstract_Subprogram (New_Subp);
12202          else
12203             Set_Requires_Overriding (New_Subp);
12204          end if;
12205
12206       elsif Ada_Version < Ada_05
12207         and then (Is_Abstract_Subprogram (Alias (New_Subp))
12208                    or else (Is_Tagged_Type (Derived_Type)
12209                              and then Etype (New_Subp) = Derived_Type
12210                              and then No (Actual_Subp)))
12211       then
12212          Set_Is_Abstract_Subprogram (New_Subp);
12213
12214       --  Finally, if the parent type is abstract we must verify that all
12215       --  inherited operations are either non-abstract or overridden, or that
12216       --  the derived type itself is abstract (this check is performed at the
12217       --  end of a package declaration, in Check_Abstract_Overriding). A
12218       --  private overriding in the parent type will not be visible in the
12219       --  derivation if we are not in an inner package or in a child unit of
12220       --  the parent type, in which case the abstractness of the inherited
12221       --  operation is carried to the new subprogram.
12222
12223       elsif Is_Abstract_Type (Parent_Type)
12224         and then not In_Open_Scopes (Scope (Parent_Type))
12225         and then Is_Private_Overriding
12226         and then Is_Abstract_Subprogram (Visible_Subp)
12227       then
12228          if No (Actual_Subp) then
12229             Set_Alias (New_Subp, Visible_Subp);
12230             Set_Is_Abstract_Subprogram
12231               (New_Subp, True);
12232          else
12233             --  If this is a derivation for an instance of a formal derived
12234             --  type, abstractness comes from the primitive operation of the
12235             --  actual, not from the operation inherited from the ancestor.
12236
12237             Set_Is_Abstract_Subprogram
12238               (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
12239          end if;
12240       end if;
12241
12242       New_Overloaded_Entity (New_Subp, Derived_Type);
12243
12244       --  Check for case of a derived subprogram for the instantiation of a
12245       --  formal derived tagged type, if so mark the subprogram as dispatching
12246       --  and inherit the dispatching attributes of the parent subprogram. The
12247       --  derived subprogram is effectively renaming of the actual subprogram,
12248       --  so it needs to have the same attributes as the actual.
12249
12250       if Present (Actual_Subp)
12251         and then Is_Dispatching_Operation (Parent_Subp)
12252       then
12253          Set_Is_Dispatching_Operation (New_Subp);
12254
12255          if Present (DTC_Entity (Parent_Subp)) then
12256             Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
12257             Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
12258          end if;
12259       end if;
12260
12261       --  Indicate that a derived subprogram does not require a body and that
12262       --  it does not require processing of default expressions.
12263
12264       Set_Has_Completion (New_Subp);
12265       Set_Default_Expressions_Processed (New_Subp);
12266
12267       if Ekind (New_Subp) = E_Function then
12268          Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
12269       end if;
12270    end Derive_Subprogram;
12271
12272    ------------------------
12273    -- Derive_Subprograms --
12274    ------------------------
12275
12276    procedure Derive_Subprograms
12277      (Parent_Type    : Entity_Id;
12278       Derived_Type   : Entity_Id;
12279       Generic_Actual : Entity_Id := Empty)
12280    is
12281       Op_List : constant Elist_Id :=
12282                   Collect_Primitive_Operations (Parent_Type);
12283
12284       function Check_Derived_Type return Boolean;
12285       --  Check that all primitive inherited from Parent_Type are found in
12286       --  the list of primitives of Derived_Type exactly in the same order.
12287
12288       function Check_Derived_Type return Boolean is
12289          E        : Entity_Id;
12290          Elmt     : Elmt_Id;
12291          List     : Elist_Id;
12292          New_Subp : Entity_Id;
12293          Op_Elmt  : Elmt_Id;
12294          Subp     : Entity_Id;
12295
12296       begin
12297          --  Traverse list of entities in the current scope searching for
12298          --  an incomplete type whose full-view is derived type
12299
12300          E := First_Entity (Scope (Derived_Type));
12301          while Present (E)
12302            and then E /= Derived_Type
12303          loop
12304             if Ekind (E) = E_Incomplete_Type
12305               and then Present (Full_View (E))
12306               and then Full_View (E) = Derived_Type
12307             then
12308                --  Disable this test if Derived_Type completes an incomplete
12309                --  type because in such case more primitives can be added
12310                --  later to the list of primitives of Derived_Type by routine
12311                --  Process_Incomplete_Dependents
12312
12313                return True;
12314             end if;
12315
12316             E := Next_Entity (E);
12317          end loop;
12318
12319          List := Collect_Primitive_Operations (Derived_Type);
12320          Elmt := First_Elmt (List);
12321
12322          Op_Elmt := First_Elmt (Op_List);
12323          while Present (Op_Elmt) loop
12324             Subp     := Node (Op_Elmt);
12325             New_Subp := Node (Elmt);
12326
12327             --  At this early stage Derived_Type has no entities with attribute
12328             --  Interface_Alias. In addition, such primitives are always
12329             --  located at the end of the list of primitives of Parent_Type.
12330             --  Therefore, if found we can safely stop processing pending
12331             --  entities.
12332
12333             exit when Present (Interface_Alias (Subp));
12334
12335             --  Handle hidden entities
12336
12337             if not Is_Predefined_Dispatching_Operation (Subp)
12338               and then Is_Hidden (Subp)
12339             then
12340                if Present (New_Subp)
12341                  and then Primitive_Names_Match (Subp, New_Subp)
12342                then
12343                   Next_Elmt (Elmt);
12344                end if;
12345
12346             else
12347                if not Present (New_Subp)
12348                  or else Ekind (Subp) /= Ekind (New_Subp)
12349                  or else not Primitive_Names_Match (Subp, New_Subp)
12350                then
12351                   return False;
12352                end if;
12353
12354                Next_Elmt (Elmt);
12355             end if;
12356
12357             Next_Elmt (Op_Elmt);
12358          end loop;
12359
12360          return True;
12361       end Check_Derived_Type;
12362
12363       --  Local variables
12364
12365       Alias_Subp   : Entity_Id;
12366       Act_List     : Elist_Id;
12367       Act_Elmt     : Elmt_Id   := No_Elmt;
12368       Act_Subp     : Entity_Id := Empty;
12369       Elmt         : Elmt_Id;
12370       Need_Search  : Boolean   := False;
12371       New_Subp     : Entity_Id := Empty;
12372       Parent_Base  : Entity_Id;
12373       Subp         : Entity_Id;
12374
12375    --  Start of processing for Derive_Subprograms
12376
12377    begin
12378       if Ekind (Parent_Type) = E_Record_Type_With_Private
12379         and then Has_Discriminants (Parent_Type)
12380         and then Present (Full_View (Parent_Type))
12381       then
12382          Parent_Base := Full_View (Parent_Type);
12383       else
12384          Parent_Base := Parent_Type;
12385       end if;
12386
12387       if Present (Generic_Actual) then
12388          Act_List := Collect_Primitive_Operations (Generic_Actual);
12389          Act_Elmt := First_Elmt (Act_List);
12390       end if;
12391
12392       --  Derive primitives inherited from the parent. Note that if the generic
12393       --  actual is present, this is not really a type derivation, it is a
12394       --  completion within an instance.
12395
12396       --  Case 1: Derived_Type does not implement interfaces
12397
12398       if not Is_Tagged_Type (Derived_Type)
12399         or else (not Has_Interfaces (Derived_Type)
12400                   and then not (Present (Generic_Actual)
12401                                   and then
12402                                 Has_Interfaces (Generic_Actual)))
12403       then
12404          Elmt := First_Elmt (Op_List);
12405          while Present (Elmt) loop
12406             Subp := Node (Elmt);
12407
12408             --  Literals are derived earlier in the process of building the
12409             --  derived type, and are skipped here.
12410
12411             if Ekind (Subp) = E_Enumeration_Literal then
12412                null;
12413
12414             --  The actual is a direct descendant and the common primitive
12415             --  operations appear in the same order.
12416
12417             --  If the generic parent type is present, the derived type is an
12418             --  instance of a formal derived type, and within the instance its
12419             --  operations are those of the actual. We derive from the formal
12420             --  type but make the inherited operations aliases of the
12421             --  corresponding operations of the actual.
12422
12423             else
12424                Derive_Subprogram
12425                  (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
12426
12427                if Present (Act_Elmt) then
12428                   Next_Elmt (Act_Elmt);
12429                end if;
12430             end if;
12431
12432             Next_Elmt (Elmt);
12433          end loop;
12434
12435       --  Case 2: Derived_Type implements interfaces
12436
12437       else
12438          --  If the parent type has no predefined primitives we remove
12439          --  predefined primitives from the list of primitives of generic
12440          --  actual to simplify the complexity of this algorithm.
12441
12442          if Present (Generic_Actual) then
12443             declare
12444                Has_Predefined_Primitives : Boolean := False;
12445
12446             begin
12447                --  Check if the parent type has predefined primitives
12448
12449                Elmt := First_Elmt (Op_List);
12450                while Present (Elmt) loop
12451                   Subp := Node (Elmt);
12452
12453                   if Is_Predefined_Dispatching_Operation (Subp)
12454                     and then not Comes_From_Source (Ultimate_Alias (Subp))
12455                   then
12456                      Has_Predefined_Primitives := True;
12457                      exit;
12458                   end if;
12459
12460                   Next_Elmt (Elmt);
12461                end loop;
12462
12463                --  Remove predefined primitives of Generic_Actual. We must use
12464                --  an auxiliary list because in case of tagged types the value
12465                --  returned by Collect_Primitive_Operations is the value stored
12466                --  in its Primitive_Operations attribute (and we don't want to
12467                --  modify its current contents).
12468
12469                if not Has_Predefined_Primitives then
12470                   declare
12471                      Aux_List : constant Elist_Id := New_Elmt_List;
12472
12473                   begin
12474                      Elmt := First_Elmt (Act_List);
12475                      while Present (Elmt) loop
12476                         Subp := Node (Elmt);
12477
12478                         if not Is_Predefined_Dispatching_Operation (Subp)
12479                           or else Comes_From_Source (Subp)
12480                         then
12481                            Append_Elmt (Subp, Aux_List);
12482                         end if;
12483
12484                         Next_Elmt (Elmt);
12485                      end loop;
12486
12487                      Act_List := Aux_List;
12488                   end;
12489                end if;
12490
12491                Act_Elmt := First_Elmt (Act_List);
12492                Act_Subp := Node (Act_Elmt);
12493             end;
12494          end if;
12495
12496          --  Stage 1: If the generic actual is not present we derive the
12497          --  primitives inherited from the parent type. If the generic parent
12498          --  type is present, the derived type is an instance of a formal
12499          --  derived type, and within the instance its operations are those of
12500          --  the actual. We derive from the formal type but make the inherited
12501          --  operations aliases of the corresponding operations of the actual.
12502
12503          Elmt := First_Elmt (Op_List);
12504          while Present (Elmt) loop
12505             Subp       := Node (Elmt);
12506             Alias_Subp := Ultimate_Alias (Subp);
12507
12508             --  At this early stage Derived_Type has no entities with attribute
12509             --  Interface_Alias. In addition, such primitives are always
12510             --  located at the end of the list of primitives of Parent_Type.
12511             --  Therefore, if found we can safely stop processing pending
12512             --  entities.
12513
12514             exit when Present (Interface_Alias (Subp));
12515
12516             --  If the generic actual is present find the corresponding
12517             --  operation in the generic actual. If the parent type is a
12518             --  direct ancestor of the derived type then, even if it is an
12519             --  interface, the operations are inherited from the primary
12520             --  dispatch table and are in the proper order. If we detect here
12521             --  that primitives are not in the same order we traverse the list
12522             --  of primitive operations of the actual to find the one that
12523             --  implements the interface primitive.
12524
12525             if Need_Search
12526               or else
12527                 (Present (Generic_Actual)
12528                    and then Present (Act_Subp)
12529                    and then not Primitive_Names_Match (Subp, Act_Subp))
12530             then
12531                pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual));
12532                pragma Assert (Is_Interface (Parent_Base));
12533
12534                --  Remember that we need searching for all the pending
12535                --  primitives
12536
12537                Need_Search := True;
12538
12539                --  Handle entities associated with interface primitives
12540
12541                if Present (Alias (Subp))
12542                  and then Is_Interface (Find_Dispatching_Type (Alias (Subp)))
12543                  and then not Is_Predefined_Dispatching_Operation (Subp)
12544                then
12545                   Act_Subp :=
12546                     Find_Primitive_Covering_Interface
12547                       (Tagged_Type => Generic_Actual,
12548                        Iface_Prim  => Subp);
12549
12550                --  Handle predefined primitives plus the rest of user-defined
12551                --  primitives
12552
12553                else
12554                   Act_Elmt := First_Elmt (Act_List);
12555                   while Present (Act_Elmt) loop
12556                      Act_Subp := Node (Act_Elmt);
12557
12558                      exit when Primitive_Names_Match (Subp, Act_Subp)
12559                        and then Type_Conformant (Subp, Act_Subp,
12560                                   Skip_Controlling_Formals => True)
12561                        and then No (Interface_Alias (Act_Subp));
12562
12563                      Next_Elmt (Act_Elmt);
12564                   end loop;
12565                end if;
12566             end if;
12567
12568             --   Case 1: If the parent is a limited interface then it has the
12569             --   predefined primitives of synchronized interfaces. However, the
12570             --   actual type may be a non-limited type and hence it does not
12571             --   have such primitives.
12572
12573             if Present (Generic_Actual)
12574               and then not Present (Act_Subp)
12575               and then Is_Limited_Interface (Parent_Base)
12576               and then Is_Predefined_Interface_Primitive (Subp)
12577             then
12578                null;
12579
12580             --  Case 2: Inherit entities associated with interfaces that
12581             --  were not covered by the parent type. We exclude here null
12582             --  interface primitives because they do not need special
12583             --  management.
12584
12585             elsif Present (Alias (Subp))
12586               and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
12587               and then not
12588                 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
12589                    and then Null_Present (Parent (Alias_Subp)))
12590             then
12591                Derive_Subprogram
12592                  (New_Subp     => New_Subp,
12593                   Parent_Subp  => Alias_Subp,
12594                   Derived_Type => Derived_Type,
12595                   Parent_Type  => Find_Dispatching_Type (Alias_Subp),
12596                   Actual_Subp  => Act_Subp);
12597
12598                if No (Generic_Actual) then
12599                   Set_Alias (New_Subp, Subp);
12600                end if;
12601
12602             --  Case 3: Common derivation
12603
12604             else
12605                Derive_Subprogram
12606                  (New_Subp     => New_Subp,
12607                   Parent_Subp  => Subp,
12608                   Derived_Type => Derived_Type,
12609                   Parent_Type  => Parent_Base,
12610                   Actual_Subp  => Act_Subp);
12611             end if;
12612
12613             --  No need to update Act_Elm if we must search for the
12614             --  corresponding operation in the generic actual
12615
12616             if not Need_Search
12617               and then Present (Act_Elmt)
12618             then
12619                Next_Elmt (Act_Elmt);
12620                Act_Subp := Node (Act_Elmt);
12621             end if;
12622
12623             Next_Elmt (Elmt);
12624          end loop;
12625
12626          --  Inherit additional operations from progenitors. If the derived
12627          --  type is a generic actual, there are not new primitive operations
12628          --  for the type because it has those of the actual, and therefore
12629          --  nothing needs to be done. The renamings generated above are not
12630          --  primitive operations, and their purpose is simply to make the
12631          --  proper operations visible within an instantiation.
12632
12633          if No (Generic_Actual) then
12634             Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
12635          end if;
12636       end if;
12637
12638       --  Final check: Direct descendants must have their primitives in the
12639       --  same order. We exclude from this test non-tagged types and instances
12640       --  of formal derived types. We skip this test if we have already
12641       --  reported serious errors in the sources.
12642
12643       pragma Assert (not Is_Tagged_Type (Derived_Type)
12644         or else Present (Generic_Actual)
12645         or else Serious_Errors_Detected > 0
12646         or else Check_Derived_Type);
12647    end Derive_Subprograms;
12648
12649    --------------------------------
12650    -- Derived_Standard_Character --
12651    --------------------------------
12652
12653    procedure Derived_Standard_Character
12654      (N            : Node_Id;
12655       Parent_Type  : Entity_Id;
12656       Derived_Type : Entity_Id)
12657    is
12658       Loc           : constant Source_Ptr := Sloc (N);
12659       Def           : constant Node_Id    := Type_Definition (N);
12660       Indic         : constant Node_Id    := Subtype_Indication (Def);
12661       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
12662       Implicit_Base : constant Entity_Id  :=
12663                         Create_Itype
12664                           (E_Enumeration_Type, N, Derived_Type, 'B');
12665
12666       Lo : Node_Id;
12667       Hi : Node_Id;
12668
12669    begin
12670       Discard_Node (Process_Subtype (Indic, N));
12671
12672       Set_Etype     (Implicit_Base, Parent_Base);
12673       Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
12674       Set_RM_Size   (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
12675
12676       Set_Is_Character_Type  (Implicit_Base, True);
12677       Set_Has_Delayed_Freeze (Implicit_Base);
12678
12679       --  The bounds of the implicit base are the bounds of the parent base.
12680       --  Note that their type is the parent base.
12681
12682       Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Base));
12683       Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
12684
12685       Set_Scalar_Range (Implicit_Base,
12686         Make_Range (Loc,
12687           Low_Bound  => Lo,
12688           High_Bound => Hi));
12689
12690       Conditional_Delay (Derived_Type, Parent_Type);
12691
12692       Set_Ekind (Derived_Type, E_Enumeration_Subtype);
12693       Set_Etype (Derived_Type, Implicit_Base);
12694       Set_Size_Info         (Derived_Type, Parent_Type);
12695
12696       if Unknown_RM_Size (Derived_Type) then
12697          Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
12698       end if;
12699
12700       Set_Is_Character_Type (Derived_Type, True);
12701
12702       if Nkind (Indic) /= N_Subtype_Indication then
12703
12704          --  If no explicit constraint, the bounds are those
12705          --  of the parent type.
12706
12707          Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Type));
12708          Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
12709          Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
12710       end if;
12711
12712       Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
12713
12714       --  Because the implicit base is used in the conversion of the bounds, we
12715       --  have to freeze it now. This is similar to what is done for numeric
12716       --  types, and it equally suspicious, but otherwise a non-static bound
12717       --  will have a reference to an unfrozen type, which is rejected by Gigi
12718       --  (???). This requires specific care for definition of stream
12719       --  attributes. For details, see comments at the end of
12720       --  Build_Derived_Numeric_Type.
12721
12722       Freeze_Before (N, Implicit_Base);
12723    end Derived_Standard_Character;
12724
12725    ------------------------------
12726    -- Derived_Type_Declaration --
12727    ------------------------------
12728
12729    procedure Derived_Type_Declaration
12730      (T             : Entity_Id;
12731       N             : Node_Id;
12732       Is_Completion : Boolean)
12733    is
12734       Parent_Type  : Entity_Id;
12735
12736       function Comes_From_Generic (Typ : Entity_Id) return Boolean;
12737       --  Check whether the parent type is a generic formal, or derives
12738       --  directly or indirectly from one.
12739
12740       ------------------------
12741       -- Comes_From_Generic --
12742       ------------------------
12743
12744       function Comes_From_Generic (Typ : Entity_Id) return Boolean is
12745       begin
12746          if Is_Generic_Type (Typ) then
12747             return True;
12748
12749          elsif Is_Generic_Type (Root_Type (Parent_Type)) then
12750             return True;
12751
12752          elsif Is_Private_Type (Typ)
12753            and then Present (Full_View (Typ))
12754            and then Is_Generic_Type (Root_Type (Full_View (Typ)))
12755          then
12756             return True;
12757
12758          elsif Is_Generic_Actual_Type (Typ) then
12759             return True;
12760
12761          else
12762             return False;
12763          end if;
12764       end Comes_From_Generic;
12765
12766       --  Local variables
12767
12768       Def          : constant Node_Id := Type_Definition (N);
12769       Iface_Def    : Node_Id;
12770       Indic        : constant Node_Id := Subtype_Indication (Def);
12771       Extension    : constant Node_Id := Record_Extension_Part (Def);
12772       Parent_Node  : Node_Id;
12773       Parent_Scope : Entity_Id;
12774       Taggd        : Boolean;
12775
12776    --  Start of processing for Derived_Type_Declaration
12777
12778    begin
12779       Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
12780
12781       --  Ada 2005 (AI-251): In case of interface derivation check that the
12782       --  parent is also an interface.
12783
12784       if Interface_Present (Def) then
12785          if not Is_Interface (Parent_Type) then
12786             Diagnose_Interface (Indic, Parent_Type);
12787
12788          else
12789             Parent_Node := Parent (Base_Type (Parent_Type));
12790             Iface_Def   := Type_Definition (Parent_Node);
12791
12792             --  Ada 2005 (AI-251): Limited interfaces can only inherit from
12793             --  other limited interfaces.
12794
12795             if Limited_Present (Def) then
12796                if Limited_Present (Iface_Def) then
12797                   null;
12798
12799                elsif Protected_Present (Iface_Def) then
12800                   Error_Msg_NE
12801                     ("descendant of& must be declared"
12802                        & " as a protected interface",
12803                          N, Parent_Type);
12804
12805                elsif Synchronized_Present (Iface_Def) then
12806                   Error_Msg_NE
12807                     ("descendant of& must be declared"
12808                        & " as a synchronized interface",
12809                          N, Parent_Type);
12810
12811                elsif Task_Present (Iface_Def) then
12812                   Error_Msg_NE
12813                     ("descendant of& must be declared as a task interface",
12814                        N, Parent_Type);
12815
12816                else
12817                   Error_Msg_N
12818                     ("(Ada 2005) limited interface cannot "
12819                      & "inherit from non-limited interface", Indic);
12820                end if;
12821
12822             --  Ada 2005 (AI-345): Non-limited interfaces can only inherit
12823             --  from non-limited or limited interfaces.
12824
12825             elsif not Protected_Present (Def)
12826               and then not Synchronized_Present (Def)
12827               and then not Task_Present (Def)
12828             then
12829                if Limited_Present (Iface_Def) then
12830                   null;
12831
12832                elsif Protected_Present (Iface_Def) then
12833                   Error_Msg_NE
12834                     ("descendant of& must be declared"
12835                        & " as a protected interface",
12836                          N, Parent_Type);
12837
12838                elsif Synchronized_Present (Iface_Def) then
12839                   Error_Msg_NE
12840                     ("descendant of& must be declared"
12841                        & " as a synchronized interface",
12842                          N, Parent_Type);
12843
12844                elsif Task_Present (Iface_Def) then
12845                   Error_Msg_NE
12846                     ("descendant of& must be declared as a task interface",
12847                        N, Parent_Type);
12848                else
12849                   null;
12850                end if;
12851             end if;
12852          end if;
12853       end if;
12854
12855       if Is_Tagged_Type (Parent_Type)
12856         and then Is_Concurrent_Type (Parent_Type)
12857         and then not Is_Interface (Parent_Type)
12858       then
12859          Error_Msg_N
12860            ("parent type of a record extension cannot be "
12861             & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
12862          Set_Etype (T, Any_Type);
12863          return;
12864       end if;
12865
12866       --  Ada 2005 (AI-251): Decorate all the names in the list of ancestor
12867       --  interfaces
12868
12869       if Is_Tagged_Type (Parent_Type)
12870         and then Is_Non_Empty_List (Interface_List (Def))
12871       then
12872          declare
12873             Intf : Node_Id;
12874             T    : Entity_Id;
12875
12876          begin
12877             Intf := First (Interface_List (Def));
12878             while Present (Intf) loop
12879                T := Find_Type_Of_Subtype_Indic (Intf);
12880
12881                if not Is_Interface (T) then
12882                   Diagnose_Interface (Intf, T);
12883
12884                --  Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
12885                --  a limited type from having a nonlimited progenitor.
12886
12887                elsif (Limited_Present (Def)
12888                        or else (not Is_Interface (Parent_Type)
12889                                  and then Is_Limited_Type (Parent_Type)))
12890                  and then not Is_Limited_Interface (T)
12891                then
12892                   Error_Msg_NE
12893                    ("progenitor interface& of limited type must be limited",
12894                      N, T);
12895                end if;
12896
12897                Next (Intf);
12898             end loop;
12899          end;
12900       end if;
12901
12902       if Parent_Type = Any_Type
12903         or else Etype (Parent_Type) = Any_Type
12904         or else (Is_Class_Wide_Type (Parent_Type)
12905                    and then Etype (Parent_Type) = T)
12906       then
12907          --  If Parent_Type is undefined or illegal, make new type into a
12908          --  subtype of Any_Type, and set a few attributes to prevent cascaded
12909          --  errors. If this is a self-definition, emit error now.
12910
12911          if T = Parent_Type
12912            or else T = Etype (Parent_Type)
12913          then
12914             Error_Msg_N ("type cannot be used in its own definition", Indic);
12915          end if;
12916
12917          Set_Ekind        (T, Ekind (Parent_Type));
12918          Set_Etype        (T, Any_Type);
12919          Set_Scalar_Range (T, Scalar_Range (Any_Type));
12920
12921          if Is_Tagged_Type (T) then
12922             Set_Primitive_Operations (T, New_Elmt_List);
12923          end if;
12924
12925          return;
12926       end if;
12927
12928       --  Ada 2005 (AI-251): The case in which the parent of the full-view is
12929       --  an interface is special because the list of interfaces in the full
12930       --  view can be given in any order. For example:
12931
12932       --     type A is interface;
12933       --     type B is interface and A;
12934       --     type D is new B with private;
12935       --   private
12936       --     type D is new A and B with null record; -- 1 --
12937
12938       --  In this case we perform the following transformation of -1-:
12939
12940       --     type D is new B and A with null record;
12941
12942       --  If the parent of the full-view covers the parent of the partial-view
12943       --  we have two possible cases:
12944
12945       --     1) They have the same parent
12946       --     2) The parent of the full-view implements some further interfaces
12947
12948       --  In both cases we do not need to perform the transformation. In the
12949       --  first case the source program is correct and the transformation is
12950       --  not needed; in the second case the source program does not fulfill
12951       --  the no-hidden interfaces rule (AI-396) and the error will be reported
12952       --  later.
12953
12954       --  This transformation not only simplifies the rest of the analysis of
12955       --  this type declaration but also simplifies the correct generation of
12956       --  the object layout to the expander.
12957
12958       if In_Private_Part (Current_Scope)
12959         and then Is_Interface (Parent_Type)
12960       then
12961          declare
12962             Iface               : Node_Id;
12963             Partial_View        : Entity_Id;
12964             Partial_View_Parent : Entity_Id;
12965             New_Iface           : Node_Id;
12966
12967          begin
12968             --  Look for the associated private type declaration
12969
12970             Partial_View := First_Entity (Current_Scope);
12971             loop
12972                exit when No (Partial_View)
12973                  or else (Has_Private_Declaration (Partial_View)
12974                            and then Full_View (Partial_View) = T);
12975
12976                Next_Entity (Partial_View);
12977             end loop;
12978
12979             --  If the partial view was not found then the source code has
12980             --  errors and the transformation is not needed.
12981
12982             if Present (Partial_View) then
12983                Partial_View_Parent := Etype (Partial_View);
12984
12985                --  If the parent of the full-view covers the parent of the
12986                --  partial-view we have nothing else to do.
12987
12988                if Interface_Present_In_Ancestor
12989                     (Parent_Type, Partial_View_Parent)
12990                then
12991                   null;
12992
12993                --  Traverse the list of interfaces of the full-view to look
12994                --  for the parent of the partial-view and perform the tree
12995                --  transformation.
12996
12997                else
12998                   Iface := First (Interface_List (Def));
12999                   while Present (Iface) loop
13000                      if Etype (Iface) = Etype (Partial_View) then
13001                         Rewrite (Subtype_Indication (Def),
13002                           New_Copy (Subtype_Indication
13003                                      (Parent (Partial_View))));
13004
13005                         New_Iface := Make_Identifier (Sloc (N),
13006                                        Chars (Parent_Type));
13007                         Append (New_Iface, Interface_List (Def));
13008
13009                         --  Analyze the transformed code
13010
13011                         Derived_Type_Declaration (T, N, Is_Completion);
13012                         return;
13013                      end if;
13014
13015                      Next (Iface);
13016                   end loop;
13017                end if;
13018             end if;
13019          end;
13020       end if;
13021
13022       --  Only composite types other than array types are allowed to have
13023       --  discriminants.
13024
13025       if Present (Discriminant_Specifications (N))
13026         and then (Is_Elementary_Type (Parent_Type)
13027                   or else Is_Array_Type (Parent_Type))
13028         and then not Error_Posted (N)
13029       then
13030          Error_Msg_N
13031            ("elementary or array type cannot have discriminants",
13032             Defining_Identifier (First (Discriminant_Specifications (N))));
13033          Set_Has_Discriminants (T, False);
13034       end if;
13035
13036       --  In Ada 83, a derived type defined in a package specification cannot
13037       --  be used for further derivation until the end of its visible part.
13038       --  Note that derivation in the private part of the package is allowed.
13039
13040       if Ada_Version = Ada_83
13041         and then Is_Derived_Type (Parent_Type)
13042         and then In_Visible_Part (Scope (Parent_Type))
13043       then
13044          if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
13045             Error_Msg_N
13046               ("(Ada 83): premature use of type for derivation", Indic);
13047          end if;
13048       end if;
13049
13050       --  Check for early use of incomplete or private type
13051
13052       if Ekind (Parent_Type) = E_Void
13053         or else Ekind (Parent_Type) = E_Incomplete_Type
13054       then
13055          Error_Msg_N ("premature derivation of incomplete type", Indic);
13056          return;
13057
13058       elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
13059               and then not Comes_From_Generic (Parent_Type))
13060         or else Has_Private_Component (Parent_Type)
13061       then
13062          --  The ancestor type of a formal type can be incomplete, in which
13063          --  case only the operations of the partial view are available in
13064          --  the generic. Subsequent checks may be required when the full
13065          --  view is analyzed, to verify that derivation from a tagged type
13066          --  has an extension.
13067
13068          if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
13069             null;
13070
13071          elsif No (Underlying_Type (Parent_Type))
13072            or else Has_Private_Component (Parent_Type)
13073          then
13074             Error_Msg_N
13075               ("premature derivation of derived or private type", Indic);
13076
13077             --  Flag the type itself as being in error, this prevents some
13078             --  nasty problems with subsequent uses of the malformed type.
13079
13080             Set_Error_Posted (T);
13081
13082          --  Check that within the immediate scope of an untagged partial
13083          --  view it's illegal to derive from the partial view if the
13084          --  full view is tagged. (7.3(7))
13085
13086          --  We verify that the Parent_Type is a partial view by checking
13087          --  that it is not a Full_Type_Declaration (i.e. a private type or
13088          --  private extension declaration), to distinguish a partial view
13089          --  from  a derivation from a private type which also appears as
13090          --  E_Private_Type.
13091
13092          elsif Present (Full_View (Parent_Type))
13093            and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
13094            and then not Is_Tagged_Type (Parent_Type)
13095            and then Is_Tagged_Type (Full_View (Parent_Type))
13096          then
13097             Parent_Scope := Scope (T);
13098             while Present (Parent_Scope)
13099               and then Parent_Scope /= Standard_Standard
13100             loop
13101                if Parent_Scope = Scope (Parent_Type) then
13102                   Error_Msg_N
13103                     ("premature derivation from type with tagged full view",
13104                      Indic);
13105                end if;
13106
13107                Parent_Scope := Scope (Parent_Scope);
13108             end loop;
13109          end if;
13110       end if;
13111
13112       --  Check that form of derivation is appropriate
13113
13114       Taggd := Is_Tagged_Type (Parent_Type);
13115
13116       --  Perhaps the parent type should be changed to the class-wide type's
13117       --  specific type in this case to prevent cascading errors ???
13118
13119       if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
13120          Error_Msg_N ("parent type must not be a class-wide type", Indic);
13121          return;
13122       end if;
13123
13124       if Present (Extension) and then not Taggd then
13125          Error_Msg_N
13126            ("type derived from untagged type cannot have extension", Indic);
13127
13128       elsif No (Extension) and then Taggd then
13129
13130          --  If this declaration is within a private part (or body) of a
13131          --  generic instantiation then the derivation is allowed (the parent
13132          --  type can only appear tagged in this case if it's a generic actual
13133          --  type, since it would otherwise have been rejected in the analysis
13134          --  of the generic template).
13135
13136          if not Is_Generic_Actual_Type (Parent_Type)
13137            or else In_Visible_Part (Scope (Parent_Type))
13138          then
13139             Error_Msg_N
13140               ("type derived from tagged type must have extension", Indic);
13141          end if;
13142       end if;
13143
13144       --  AI-443: Synchronized formal derived types require a private
13145       --  extension. There is no point in checking the ancestor type or
13146       --  the progenitors since the construct is wrong to begin with.
13147
13148       if Ada_Version >= Ada_05
13149         and then Is_Generic_Type (T)
13150         and then Present (Original_Node (N))
13151       then
13152          declare
13153             Decl : constant Node_Id := Original_Node (N);
13154
13155          begin
13156             if Nkind (Decl) = N_Formal_Type_Declaration
13157               and then Nkind (Formal_Type_Definition (Decl)) =
13158                          N_Formal_Derived_Type_Definition
13159               and then Synchronized_Present (Formal_Type_Definition (Decl))
13160               and then No (Extension)
13161
13162                --  Avoid emitting a duplicate error message
13163
13164               and then not Error_Posted (Indic)
13165             then
13166                Error_Msg_N
13167                  ("synchronized derived type must have extension", N);
13168             end if;
13169          end;
13170       end if;
13171
13172       if Null_Exclusion_Present (Def)
13173         and then not Is_Access_Type (Parent_Type)
13174       then
13175          Error_Msg_N ("null exclusion can only apply to an access type", N);
13176       end if;
13177
13178       --  Avoid deriving parent primitives of underlying record views
13179
13180       Build_Derived_Type (N, Parent_Type, T, Is_Completion,
13181         Derive_Subps => not Is_Underlying_Record_View (T));
13182
13183       --  AI-419: The parent type of an explicitly limited derived type must
13184       --  be a limited type or a limited interface.
13185
13186       if Limited_Present (Def) then
13187          Set_Is_Limited_Record (T);
13188
13189          if Is_Interface (T) then
13190             Set_Is_Limited_Interface (T);
13191          end if;
13192
13193          if not Is_Limited_Type (Parent_Type)
13194            and then
13195              (not Is_Interface (Parent_Type)
13196                or else not Is_Limited_Interface (Parent_Type))
13197          then
13198             Error_Msg_NE ("parent type& of limited type must be limited",
13199               N, Parent_Type);
13200          end if;
13201       end if;
13202    end Derived_Type_Declaration;
13203
13204    ------------------------
13205    -- Diagnose_Interface --
13206    ------------------------
13207
13208    procedure Diagnose_Interface (N : Node_Id;  E : Entity_Id) is
13209    begin
13210       if not Is_Interface (E)
13211         and then  E /= Any_Type
13212       then
13213          Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
13214       end if;
13215    end Diagnose_Interface;
13216
13217    ----------------------------------
13218    -- Enumeration_Type_Declaration --
13219    ----------------------------------
13220
13221    procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
13222       Ev     : Uint;
13223       L      : Node_Id;
13224       R_Node : Node_Id;
13225       B_Node : Node_Id;
13226
13227    begin
13228       --  Create identifier node representing lower bound
13229
13230       B_Node := New_Node (N_Identifier, Sloc (Def));
13231       L := First (Literals (Def));
13232       Set_Chars (B_Node, Chars (L));
13233       Set_Entity (B_Node,  L);
13234       Set_Etype (B_Node, T);
13235       Set_Is_Static_Expression (B_Node, True);
13236
13237       R_Node := New_Node (N_Range, Sloc (Def));
13238       Set_Low_Bound  (R_Node, B_Node);
13239
13240       Set_Ekind (T, E_Enumeration_Type);
13241       Set_First_Literal (T, L);
13242       Set_Etype (T, T);
13243       Set_Is_Constrained (T);
13244
13245       Ev := Uint_0;
13246
13247       --  Loop through literals of enumeration type setting pos and rep values
13248       --  except that if the Ekind is already set, then it means that the
13249       --  literal was already constructed (case of a derived type declaration
13250       --  and we should not disturb the Pos and Rep values.
13251
13252       while Present (L) loop
13253          if Ekind (L) /= E_Enumeration_Literal then
13254             Set_Ekind (L, E_Enumeration_Literal);
13255             Set_Enumeration_Pos (L, Ev);
13256             Set_Enumeration_Rep (L, Ev);
13257             Set_Is_Known_Valid  (L, True);
13258          end if;
13259
13260          Set_Etype (L, T);
13261          New_Overloaded_Entity (L);
13262          Generate_Definition (L);
13263          Set_Convention (L, Convention_Intrinsic);
13264
13265          if Nkind (L) = N_Defining_Character_Literal then
13266             Set_Is_Character_Type (T, True);
13267          end if;
13268
13269          Ev := Ev + 1;
13270          Next (L);
13271       end loop;
13272
13273       --  Now create a node representing upper bound
13274
13275       B_Node := New_Node (N_Identifier, Sloc (Def));
13276       Set_Chars (B_Node, Chars (Last (Literals (Def))));
13277       Set_Entity (B_Node,  Last (Literals (Def)));
13278       Set_Etype (B_Node, T);
13279       Set_Is_Static_Expression (B_Node, True);
13280
13281       Set_High_Bound (R_Node, B_Node);
13282
13283       --  Initialize various fields of the type. Some of this information
13284       --  may be overwritten later through rep.clauses.
13285
13286       Set_Scalar_Range    (T, R_Node);
13287       Set_RM_Size         (T, UI_From_Int (Minimum_Size (T)));
13288       Set_Enum_Esize      (T);
13289       Set_Enum_Pos_To_Rep (T, Empty);
13290
13291       --  Set Discard_Names if configuration pragma set, or if there is
13292       --  a parameterless pragma in the current declarative region
13293
13294       if Global_Discard_Names
13295         or else Discard_Names (Scope (T))
13296       then
13297          Set_Discard_Names (T);
13298       end if;
13299
13300       --  Process end label if there is one
13301
13302       if Present (Def) then
13303          Process_End_Label (Def, 'e', T);
13304       end if;
13305    end Enumeration_Type_Declaration;
13306
13307    ---------------------------------
13308    -- Expand_To_Stored_Constraint --
13309    ---------------------------------
13310
13311    function Expand_To_Stored_Constraint
13312      (Typ        : Entity_Id;
13313       Constraint : Elist_Id) return Elist_Id
13314    is
13315       Explicitly_Discriminated_Type : Entity_Id;
13316       Expansion    : Elist_Id;
13317       Discriminant : Entity_Id;
13318
13319       function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
13320       --  Find the nearest type that actually specifies discriminants
13321
13322       ---------------------------------
13323       -- Type_With_Explicit_Discrims --
13324       ---------------------------------
13325
13326       function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
13327          Typ : constant E := Base_Type (Id);
13328
13329       begin
13330          if Ekind (Typ) in Incomplete_Or_Private_Kind then
13331             if Present (Full_View (Typ)) then
13332                return Type_With_Explicit_Discrims (Full_View (Typ));
13333             end if;
13334
13335          else
13336             if Has_Discriminants (Typ) then
13337                return Typ;
13338             end if;
13339          end if;
13340
13341          if Etype (Typ) = Typ then
13342             return Empty;
13343          elsif Has_Discriminants (Typ) then
13344             return Typ;
13345          else
13346             return Type_With_Explicit_Discrims (Etype (Typ));
13347          end if;
13348
13349       end Type_With_Explicit_Discrims;
13350
13351    --  Start of processing for Expand_To_Stored_Constraint
13352
13353    begin
13354       if No (Constraint)
13355         or else Is_Empty_Elmt_List (Constraint)
13356       then
13357          return No_Elist;
13358       end if;
13359
13360       Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
13361
13362       if No (Explicitly_Discriminated_Type) then
13363          return No_Elist;
13364       end if;
13365
13366       Expansion := New_Elmt_List;
13367
13368       Discriminant :=
13369          First_Stored_Discriminant (Explicitly_Discriminated_Type);
13370       while Present (Discriminant) loop
13371          Append_Elmt (
13372            Get_Discriminant_Value (
13373              Discriminant, Explicitly_Discriminated_Type, Constraint),
13374            Expansion);
13375          Next_Stored_Discriminant (Discriminant);
13376       end loop;
13377
13378       return Expansion;
13379    end Expand_To_Stored_Constraint;
13380
13381    ---------------------------
13382    -- Find_Hidden_Interface --
13383    ---------------------------
13384
13385    function Find_Hidden_Interface
13386      (Src  : Elist_Id;
13387       Dest : Elist_Id) return Entity_Id
13388    is
13389       Iface      : Entity_Id;
13390       Iface_Elmt : Elmt_Id;
13391
13392    begin
13393       if Present (Src) and then Present (Dest) then
13394          Iface_Elmt := First_Elmt (Src);
13395          while Present (Iface_Elmt) loop
13396             Iface := Node (Iface_Elmt);
13397
13398             if Is_Interface (Iface)
13399               and then not Contain_Interface (Iface, Dest)
13400             then
13401                return Iface;
13402             end if;
13403
13404             Next_Elmt (Iface_Elmt);
13405          end loop;
13406       end if;
13407
13408       return Empty;
13409    end Find_Hidden_Interface;
13410
13411    --------------------
13412    -- Find_Type_Name --
13413    --------------------
13414
13415    function Find_Type_Name (N : Node_Id) return Entity_Id is
13416       Id       : constant Entity_Id := Defining_Identifier (N);
13417       Prev     : Entity_Id;
13418       New_Id   : Entity_Id;
13419       Prev_Par : Node_Id;
13420
13421       procedure Tag_Mismatch;
13422       --  Diagnose a tagged partial view whose full view is untagged.
13423       --  We post the message on the full view, with a reference to
13424       --  the previous partial view. The partial view can be private
13425       --  or incomplete, and these are handled in a different manner,
13426       --  so we determine the position of the error message from the
13427       --  respective slocs of both.
13428
13429       ------------------
13430       -- Tag_Mismatch --
13431       ------------------
13432
13433       procedure Tag_Mismatch is
13434       begin
13435          if Sloc (Prev) < Sloc (Id) then
13436             Error_Msg_NE
13437               ("full declaration of } must be a tagged type ", Id, Prev);
13438          else
13439             Error_Msg_NE
13440               ("full declaration of } must be a tagged type ", Prev, Id);
13441          end if;
13442       end Tag_Mismatch;
13443
13444    --  Start of processing for Find_Type_Name
13445
13446    begin
13447       --  Find incomplete declaration, if one was given
13448
13449       Prev := Current_Entity_In_Scope (Id);
13450
13451       if Present (Prev) then
13452
13453          --  Previous declaration exists. Error if not incomplete/private case
13454          --  except if previous declaration is implicit, etc. Enter_Name will
13455          --  emit error if appropriate.
13456
13457          Prev_Par := Parent (Prev);
13458
13459          if not Is_Incomplete_Or_Private_Type (Prev) then
13460             Enter_Name (Id);
13461             New_Id := Id;
13462
13463          elsif not Nkind_In (N, N_Full_Type_Declaration,
13464                                 N_Task_Type_Declaration,
13465                                 N_Protected_Type_Declaration)
13466          then
13467             --  Completion must be a full type declarations (RM 7.3(4))
13468
13469             Error_Msg_Sloc := Sloc (Prev);
13470             Error_Msg_NE ("invalid completion of }", Id, Prev);
13471
13472             --  Set scope of Id to avoid cascaded errors. Entity is never
13473             --  examined again, except when saving globals in generics.
13474
13475             Set_Scope (Id, Current_Scope);
13476             New_Id := Id;
13477
13478             --  If this is a repeated incomplete declaration, no further
13479             --  checks are possible.
13480
13481             if Nkind (N) = N_Incomplete_Type_Declaration then
13482                return Prev;
13483             end if;
13484
13485          --  Case of full declaration of incomplete type
13486
13487          elsif Ekind (Prev) = E_Incomplete_Type then
13488
13489             --  Indicate that the incomplete declaration has a matching full
13490             --  declaration. The defining occurrence of the incomplete
13491             --  declaration remains the visible one, and the procedure
13492             --  Get_Full_View dereferences it whenever the type is used.
13493
13494             if Present (Full_View (Prev)) then
13495                Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
13496             end if;
13497
13498             Set_Full_View (Prev,  Id);
13499             Append_Entity (Id, Current_Scope);
13500             Set_Is_Public (Id, Is_Public (Prev));
13501             Set_Is_Internal (Id);
13502             New_Id := Prev;
13503
13504          --  Case of full declaration of private type
13505
13506          else
13507             if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
13508                if Etype (Prev) /= Prev then
13509
13510                   --  Prev is a private subtype or a derived type, and needs
13511                   --  no completion.
13512
13513                   Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
13514                   New_Id := Id;
13515
13516                elsif Ekind (Prev) = E_Private_Type
13517                  and then Nkind_In (N, N_Task_Type_Declaration,
13518                                        N_Protected_Type_Declaration)
13519                then
13520                   Error_Msg_N
13521                    ("completion of nonlimited type cannot be limited", N);
13522
13523                elsif Ekind (Prev) = E_Record_Type_With_Private
13524                  and then Nkind_In (N, N_Task_Type_Declaration,
13525                                        N_Protected_Type_Declaration)
13526                then
13527                   if not Is_Limited_Record (Prev) then
13528                      Error_Msg_N
13529                         ("completion of nonlimited type cannot be limited", N);
13530
13531                   elsif No (Interface_List (N)) then
13532                      Error_Msg_N
13533                         ("completion of tagged private type must be tagged",
13534                          N);
13535                   end if;
13536
13537                elsif Nkind (N) = N_Full_Type_Declaration
13538                  and then
13539                    Nkind (Type_Definition (N)) = N_Record_Definition
13540                  and then Interface_Present (Type_Definition (N))
13541                then
13542                   Error_Msg_N
13543                     ("completion of private type cannot be an interface", N);
13544                end if;
13545
13546             --  Ada 2005 (AI-251): Private extension declaration of a task
13547             --  type or a protected type. This case arises when covering
13548             --  interface types.
13549
13550             elsif Nkind_In (N, N_Task_Type_Declaration,
13551                                N_Protected_Type_Declaration)
13552             then
13553                null;
13554
13555             elsif Nkind (N) /= N_Full_Type_Declaration
13556               or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
13557             then
13558                Error_Msg_N
13559                  ("full view of private extension must be an extension", N);
13560
13561             elsif not (Abstract_Present (Parent (Prev)))
13562               and then Abstract_Present (Type_Definition (N))
13563             then
13564                Error_Msg_N
13565                  ("full view of non-abstract extension cannot be abstract", N);
13566             end if;
13567
13568             if not In_Private_Part (Current_Scope) then
13569                Error_Msg_N
13570                  ("declaration of full view must appear in private part", N);
13571             end if;
13572
13573             Copy_And_Swap (Prev, Id);
13574             Set_Has_Private_Declaration (Prev);
13575             Set_Has_Private_Declaration (Id);
13576
13577             --  If no error, propagate freeze_node from private to full view.
13578             --  It may have been generated for an early operational item.
13579
13580             if Present (Freeze_Node (Id))
13581               and then Serious_Errors_Detected = 0
13582               and then No (Full_View (Id))
13583             then
13584                Set_Freeze_Node (Prev, Freeze_Node (Id));
13585                Set_Freeze_Node (Id, Empty);
13586                Set_First_Rep_Item (Prev, First_Rep_Item (Id));
13587             end if;
13588
13589             Set_Full_View (Id, Prev);
13590             New_Id := Prev;
13591          end if;
13592
13593          --  Verify that full declaration conforms to partial one
13594
13595          if Is_Incomplete_Or_Private_Type (Prev)
13596            and then Present (Discriminant_Specifications (Prev_Par))
13597          then
13598             if Present (Discriminant_Specifications (N)) then
13599                if Ekind (Prev) = E_Incomplete_Type then
13600                   Check_Discriminant_Conformance (N, Prev, Prev);
13601                else
13602                   Check_Discriminant_Conformance (N, Prev, Id);
13603                end if;
13604
13605             else
13606                Error_Msg_N
13607                  ("missing discriminants in full type declaration", N);
13608
13609                --  To avoid cascaded errors on subsequent use, share the
13610                --  discriminants of the partial view.
13611
13612                Set_Discriminant_Specifications (N,
13613                  Discriminant_Specifications (Prev_Par));
13614             end if;
13615          end if;
13616
13617          --  A prior untagged partial view can have an associated class-wide
13618          --  type due to use of the class attribute, and in this case the full
13619          --  type must also be tagged. This Ada 95 usage is deprecated in favor
13620          --  of incomplete tagged declarations, but we check for it.
13621
13622          if Is_Type (Prev)
13623            and then (Is_Tagged_Type (Prev)
13624                       or else Present (Class_Wide_Type (Prev)))
13625          then
13626             --  The full declaration is either a tagged type (including
13627             --  a synchronized type that implements interfaces) or a
13628             --  type extension, otherwise this is an error.
13629
13630             if Nkind_In (N, N_Task_Type_Declaration,
13631                             N_Protected_Type_Declaration)
13632             then
13633                if No (Interface_List (N))
13634                  and then not Error_Posted (N)
13635                then
13636                   Tag_Mismatch;
13637                end if;
13638
13639             elsif Nkind (Type_Definition (N)) = N_Record_Definition then
13640
13641                --  Indicate that the previous declaration (tagged incomplete
13642                --  or private declaration) requires the same on the full one.
13643
13644                if not Tagged_Present (Type_Definition (N)) then
13645                   Tag_Mismatch;
13646                   Set_Is_Tagged_Type (Id);
13647                   Set_Primitive_Operations (Id, New_Elmt_List);
13648                end if;
13649
13650             elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
13651                if No (Record_Extension_Part (Type_Definition (N))) then
13652                   Error_Msg_NE (
13653                     "full declaration of } must be a record extension",
13654                     Prev, Id);
13655
13656                   --  Set some attributes to produce a usable full view
13657
13658                   Set_Is_Tagged_Type (Id);
13659                   Set_Primitive_Operations (Id, New_Elmt_List);
13660                end if;
13661
13662             else
13663                Tag_Mismatch;
13664             end if;
13665          end if;
13666
13667          return New_Id;
13668
13669       else
13670          --  New type declaration
13671
13672          Enter_Name (Id);
13673          return Id;
13674       end if;
13675    end Find_Type_Name;
13676
13677    -------------------------
13678    -- Find_Type_Of_Object --
13679    -------------------------
13680
13681    function Find_Type_Of_Object
13682      (Obj_Def     : Node_Id;
13683       Related_Nod : Node_Id) return Entity_Id
13684    is
13685       Def_Kind : constant Node_Kind := Nkind (Obj_Def);
13686       P        : Node_Id := Parent (Obj_Def);
13687       T        : Entity_Id;
13688       Nam      : Name_Id;
13689
13690    begin
13691       --  If the parent is a component_definition node we climb to the
13692       --  component_declaration node
13693
13694       if Nkind (P) = N_Component_Definition then
13695          P := Parent (P);
13696       end if;
13697
13698       --  Case of an anonymous array subtype
13699
13700       if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
13701                              N_Unconstrained_Array_Definition)
13702       then
13703          T := Empty;
13704          Array_Type_Declaration (T, Obj_Def);
13705
13706       --  Create an explicit subtype whenever possible
13707
13708       elsif Nkind (P) /= N_Component_Declaration
13709         and then Def_Kind = N_Subtype_Indication
13710       then
13711          --  Base name of subtype on object name, which will be unique in
13712          --  the current scope.
13713
13714          --  If this is a duplicate declaration, return base type, to avoid
13715          --  generating duplicate anonymous types.
13716
13717          if Error_Posted (P) then
13718             Analyze (Subtype_Mark (Obj_Def));
13719             return Entity (Subtype_Mark (Obj_Def));
13720          end if;
13721
13722          Nam :=
13723             New_External_Name
13724              (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
13725
13726          T := Make_Defining_Identifier (Sloc (P), Nam);
13727
13728          Insert_Action (Obj_Def,
13729            Make_Subtype_Declaration (Sloc (P),
13730              Defining_Identifier => T,
13731              Subtype_Indication  => Relocate_Node (Obj_Def)));
13732
13733          --  This subtype may need freezing, and this will not be done
13734          --  automatically if the object declaration is not in declarative
13735          --  part. Since this is an object declaration, the type cannot always
13736          --  be frozen here. Deferred constants do not freeze their type
13737          --  (which often enough will be private).
13738
13739          if Nkind (P) = N_Object_Declaration
13740            and then Constant_Present (P)
13741            and then No (Expression (P))
13742          then
13743             null;
13744          else
13745             Insert_Actions (Obj_Def, Freeze_Entity (T, Sloc (P)));
13746          end if;
13747
13748       --  Ada 2005 AI-406: the object definition in an object declaration
13749       --  can be an access definition.
13750
13751       elsif Def_Kind = N_Access_Definition then
13752          T := Access_Definition (Related_Nod, Obj_Def);
13753          Set_Is_Local_Anonymous_Access (T);
13754
13755       --  Otherwise, the object definition is just a subtype_mark
13756
13757       else
13758          T := Process_Subtype (Obj_Def, Related_Nod);
13759       end if;
13760
13761       return T;
13762    end Find_Type_Of_Object;
13763
13764    --------------------------------
13765    -- Find_Type_Of_Subtype_Indic --
13766    --------------------------------
13767
13768    function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
13769       Typ : Entity_Id;
13770
13771    begin
13772       --  Case of subtype mark with a constraint
13773
13774       if Nkind (S) = N_Subtype_Indication then
13775          Find_Type (Subtype_Mark (S));
13776          Typ := Entity (Subtype_Mark (S));
13777
13778          if not
13779            Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
13780          then
13781             Error_Msg_N
13782               ("incorrect constraint for this kind of type", Constraint (S));
13783             Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
13784          end if;
13785
13786       --  Otherwise we have a subtype mark without a constraint
13787
13788       elsif Error_Posted (S) then
13789          Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
13790          return Any_Type;
13791
13792       else
13793          Find_Type (S);
13794          Typ := Entity (S);
13795       end if;
13796
13797       --  Check No_Wide_Characters restriction
13798
13799       if Typ = Standard_Wide_Character
13800         or else Typ = Standard_Wide_Wide_Character
13801         or else Typ = Standard_Wide_String
13802         or else Typ = Standard_Wide_Wide_String
13803       then
13804          Check_Restriction (No_Wide_Characters, S);
13805       end if;
13806
13807       return Typ;
13808    end Find_Type_Of_Subtype_Indic;
13809
13810    -------------------------------------
13811    -- Floating_Point_Type_Declaration --
13812    -------------------------------------
13813
13814    procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
13815       Digs          : constant Node_Id := Digits_Expression (Def);
13816       Digs_Val      : Uint;
13817       Base_Typ      : Entity_Id;
13818       Implicit_Base : Entity_Id;
13819       Bound         : Node_Id;
13820
13821       function Can_Derive_From (E : Entity_Id) return Boolean;
13822       --  Find if given digits value allows derivation from specified type
13823
13824       ---------------------
13825       -- Can_Derive_From --
13826       ---------------------
13827
13828       function Can_Derive_From (E : Entity_Id) return Boolean is
13829          Spec : constant Entity_Id := Real_Range_Specification (Def);
13830
13831       begin
13832          if Digs_Val > Digits_Value (E) then
13833             return False;
13834          end if;
13835
13836          if Present (Spec) then
13837             if Expr_Value_R (Type_Low_Bound (E)) >
13838                Expr_Value_R (Low_Bound (Spec))
13839             then
13840                return False;
13841             end if;
13842
13843             if Expr_Value_R (Type_High_Bound (E)) <
13844                Expr_Value_R (High_Bound (Spec))
13845             then
13846                return False;
13847             end if;
13848          end if;
13849
13850          return True;
13851       end Can_Derive_From;
13852
13853    --  Start of processing for Floating_Point_Type_Declaration
13854
13855    begin
13856       Check_Restriction (No_Floating_Point, Def);
13857
13858       --  Create an implicit base type
13859
13860       Implicit_Base :=
13861         Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
13862
13863       --  Analyze and verify digits value
13864
13865       Analyze_And_Resolve (Digs, Any_Integer);
13866       Check_Digits_Expression (Digs);
13867       Digs_Val := Expr_Value (Digs);
13868
13869       --  Process possible range spec and find correct type to derive from
13870
13871       Process_Real_Range_Specification (Def);
13872
13873       if Can_Derive_From (Standard_Short_Float) then
13874          Base_Typ := Standard_Short_Float;
13875       elsif Can_Derive_From (Standard_Float) then
13876          Base_Typ := Standard_Float;
13877       elsif Can_Derive_From (Standard_Long_Float) then
13878          Base_Typ := Standard_Long_Float;
13879       elsif Can_Derive_From (Standard_Long_Long_Float) then
13880          Base_Typ := Standard_Long_Long_Float;
13881
13882       --  If we can't derive from any existing type, use long_long_float
13883       --  and give appropriate message explaining the problem.
13884
13885       else
13886          Base_Typ := Standard_Long_Long_Float;
13887
13888          if Digs_Val >= Digits_Value (Standard_Long_Long_Float) then
13889             Error_Msg_Uint_1 := Digits_Value (Standard_Long_Long_Float);
13890             Error_Msg_N ("digits value out of range, maximum is ^", Digs);
13891
13892          else
13893             Error_Msg_N
13894               ("range too large for any predefined type",
13895                Real_Range_Specification (Def));
13896          end if;
13897       end if;
13898
13899       --  If there are bounds given in the declaration use them as the bounds
13900       --  of the type, otherwise use the bounds of the predefined base type
13901       --  that was chosen based on the Digits value.
13902
13903       if Present (Real_Range_Specification (Def)) then
13904          Set_Scalar_Range (T, Real_Range_Specification (Def));
13905          Set_Is_Constrained (T);
13906
13907          --  The bounds of this range must be converted to machine numbers
13908          --  in accordance with RM 4.9(38).
13909
13910          Bound := Type_Low_Bound (T);
13911
13912          if Nkind (Bound) = N_Real_Literal then
13913             Set_Realval
13914               (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
13915             Set_Is_Machine_Number (Bound);
13916          end if;
13917
13918          Bound := Type_High_Bound (T);
13919
13920          if Nkind (Bound) = N_Real_Literal then
13921             Set_Realval
13922               (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
13923             Set_Is_Machine_Number (Bound);
13924          end if;
13925
13926       else
13927          Set_Scalar_Range (T, Scalar_Range (Base_Typ));
13928       end if;
13929
13930       --  Complete definition of implicit base and declared first subtype
13931
13932       Set_Etype          (Implicit_Base, Base_Typ);
13933
13934       Set_Scalar_Range   (Implicit_Base, Scalar_Range   (Base_Typ));
13935       Set_Size_Info      (Implicit_Base,                (Base_Typ));
13936       Set_RM_Size        (Implicit_Base, RM_Size        (Base_Typ));
13937       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
13938       Set_Digits_Value   (Implicit_Base, Digits_Value   (Base_Typ));
13939       Set_Vax_Float      (Implicit_Base, Vax_Float      (Base_Typ));
13940
13941       Set_Ekind          (T, E_Floating_Point_Subtype);
13942       Set_Etype          (T, Implicit_Base);
13943
13944       Set_Size_Info      (T,                (Implicit_Base));
13945       Set_RM_Size        (T, RM_Size        (Implicit_Base));
13946       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
13947       Set_Digits_Value   (T, Digs_Val);
13948    end Floating_Point_Type_Declaration;
13949
13950    ----------------------------
13951    -- Get_Discriminant_Value --
13952    ----------------------------
13953
13954    --  This is the situation:
13955
13956    --  There is a non-derived type
13957
13958    --       type T0 (Dx, Dy, Dz...)
13959
13960    --  There are zero or more levels of derivation, with each derivation
13961    --  either purely inheriting the discriminants, or defining its own.
13962
13963    --       type Ti      is new Ti-1
13964    --  or
13965    --       type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
13966    --  or
13967    --       subtype Ti is ...
13968
13969    --  The subtype issue is avoided by the use of Original_Record_Component,
13970    --  and the fact that derived subtypes also derive the constraints.
13971
13972    --  This chain leads back from
13973
13974    --       Typ_For_Constraint
13975
13976    --  Typ_For_Constraint has discriminants, and the value for each
13977    --  discriminant is given by its corresponding Elmt of Constraints.
13978
13979    --  Discriminant is some discriminant in this hierarchy
13980
13981    --  We need to return its value
13982
13983    --  We do this by recursively searching each level, and looking for
13984    --  Discriminant. Once we get to the bottom, we start backing up
13985    --  returning the value for it which may in turn be a discriminant
13986    --  further up, so on the backup we continue the substitution.
13987
13988    function Get_Discriminant_Value
13989      (Discriminant       : Entity_Id;
13990       Typ_For_Constraint : Entity_Id;
13991       Constraint         : Elist_Id) return Node_Id
13992    is
13993       function Search_Derivation_Levels
13994         (Ti                    : Entity_Id;
13995          Discrim_Values        : Elist_Id;
13996          Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
13997       --  This is the routine that performs the recursive search of levels
13998       --  as described above.
13999
14000       ------------------------------
14001       -- Search_Derivation_Levels --
14002       ------------------------------
14003
14004       function Search_Derivation_Levels
14005         (Ti                    : Entity_Id;
14006          Discrim_Values        : Elist_Id;
14007          Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
14008       is
14009          Assoc          : Elmt_Id;
14010          Disc           : Entity_Id;
14011          Result         : Node_Or_Entity_Id;
14012          Result_Entity  : Node_Id;
14013
14014       begin
14015          --  If inappropriate type, return Error, this happens only in
14016          --  cascaded error situations, and we want to avoid a blow up.
14017
14018          if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
14019             return Error;
14020          end if;
14021
14022          --  Look deeper if possible. Use Stored_Constraints only for
14023          --  untagged types. For tagged types use the given constraint.
14024          --  This asymmetry needs explanation???
14025
14026          if not Stored_Discrim_Values
14027            and then Present (Stored_Constraint (Ti))
14028            and then not Is_Tagged_Type (Ti)
14029          then
14030             Result :=
14031               Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
14032          else
14033             declare
14034                Td : constant Entity_Id := Etype (Ti);
14035
14036             begin
14037                if Td = Ti then
14038                   Result := Discriminant;
14039
14040                else
14041                   if Present (Stored_Constraint (Ti)) then
14042                      Result :=
14043                         Search_Derivation_Levels
14044                           (Td, Stored_Constraint (Ti), True);
14045                   else
14046                      Result :=
14047                         Search_Derivation_Levels
14048                           (Td, Discrim_Values, Stored_Discrim_Values);
14049                   end if;
14050                end if;
14051             end;
14052          end if;
14053
14054          --  Extra underlying places to search, if not found above. For
14055          --  concurrent types, the relevant discriminant appears in the
14056          --  corresponding record. For a type derived from a private type
14057          --  without discriminant, the full view inherits the discriminants
14058          --  of the full view of the parent.
14059
14060          if Result = Discriminant then
14061             if Is_Concurrent_Type (Ti)
14062               and then Present (Corresponding_Record_Type (Ti))
14063             then
14064                Result :=
14065                  Search_Derivation_Levels (
14066                    Corresponding_Record_Type (Ti),
14067                    Discrim_Values,
14068                    Stored_Discrim_Values);
14069
14070             elsif Is_Private_Type (Ti)
14071               and then not Has_Discriminants (Ti)
14072               and then Present (Full_View (Ti))
14073               and then Etype (Full_View (Ti)) /= Ti
14074             then
14075                Result :=
14076                  Search_Derivation_Levels (
14077                    Full_View (Ti),
14078                    Discrim_Values,
14079                    Stored_Discrim_Values);
14080             end if;
14081          end if;
14082
14083          --  If Result is not a (reference to a) discriminant, return it,
14084          --  otherwise set Result_Entity to the discriminant.
14085
14086          if Nkind (Result) = N_Defining_Identifier then
14087             pragma Assert (Result = Discriminant);
14088             Result_Entity := Result;
14089
14090          else
14091             if not Denotes_Discriminant (Result) then
14092                return Result;
14093             end if;
14094
14095             Result_Entity := Entity (Result);
14096          end if;
14097
14098          --  See if this level of derivation actually has discriminants
14099          --  because tagged derivations can add them, hence the lower
14100          --  levels need not have any.
14101
14102          if not Has_Discriminants (Ti) then
14103             return Result;
14104          end if;
14105
14106          --  Scan Ti's discriminants for Result_Entity,
14107          --  and return its corresponding value, if any.
14108
14109          Result_Entity := Original_Record_Component (Result_Entity);
14110
14111          Assoc := First_Elmt (Discrim_Values);
14112
14113          if Stored_Discrim_Values then
14114             Disc := First_Stored_Discriminant (Ti);
14115          else
14116             Disc := First_Discriminant (Ti);
14117          end if;
14118
14119          while Present (Disc) loop
14120             pragma Assert (Present (Assoc));
14121
14122             if Original_Record_Component (Disc) = Result_Entity then
14123                return Node (Assoc);
14124             end if;
14125
14126             Next_Elmt (Assoc);
14127
14128             if Stored_Discrim_Values then
14129                Next_Stored_Discriminant (Disc);
14130             else
14131                Next_Discriminant (Disc);
14132             end if;
14133          end loop;
14134
14135          --  Could not find it
14136          --
14137          return Result;
14138       end Search_Derivation_Levels;
14139
14140       --  Local Variables
14141
14142       Result : Node_Or_Entity_Id;
14143
14144    --  Start of processing for Get_Discriminant_Value
14145
14146    begin
14147       --  ??? This routine is a gigantic mess and will be deleted. For the
14148       --  time being just test for the trivial case before calling recurse.
14149
14150       if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
14151          declare
14152             D : Entity_Id;
14153             E : Elmt_Id;
14154
14155          begin
14156             D := First_Discriminant (Typ_For_Constraint);
14157             E := First_Elmt (Constraint);
14158             while Present (D) loop
14159                if Chars (D) = Chars (Discriminant) then
14160                   return Node (E);
14161                end if;
14162
14163                Next_Discriminant (D);
14164                Next_Elmt (E);
14165             end loop;
14166          end;
14167       end if;
14168
14169       Result := Search_Derivation_Levels
14170         (Typ_For_Constraint, Constraint, False);
14171
14172       --  ??? hack to disappear when this routine is gone
14173
14174       if  Nkind (Result) = N_Defining_Identifier then
14175          declare
14176             D : Entity_Id;
14177             E : Elmt_Id;
14178
14179          begin
14180             D := First_Discriminant (Typ_For_Constraint);
14181             E := First_Elmt (Constraint);
14182             while Present (D) loop
14183                if Corresponding_Discriminant (D) = Discriminant then
14184                   return Node (E);
14185                end if;
14186
14187                Next_Discriminant (D);
14188                Next_Elmt (E);
14189             end loop;
14190          end;
14191       end if;
14192
14193       pragma Assert (Nkind (Result) /= N_Defining_Identifier);
14194       return Result;
14195    end Get_Discriminant_Value;
14196
14197    --------------------------
14198    -- Has_Range_Constraint --
14199    --------------------------
14200
14201    function Has_Range_Constraint (N : Node_Id) return Boolean is
14202       C : constant Node_Id := Constraint (N);
14203
14204    begin
14205       if Nkind (C) = N_Range_Constraint then
14206          return True;
14207
14208       elsif Nkind (C) = N_Digits_Constraint then
14209          return
14210             Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
14211               or else
14212             Present (Range_Constraint (C));
14213
14214       elsif Nkind (C) = N_Delta_Constraint then
14215          return Present (Range_Constraint (C));
14216
14217       else
14218          return False;
14219       end if;
14220    end Has_Range_Constraint;
14221
14222    ------------------------
14223    -- Inherit_Components --
14224    ------------------------
14225
14226    function Inherit_Components
14227      (N             : Node_Id;
14228       Parent_Base   : Entity_Id;
14229       Derived_Base  : Entity_Id;
14230       Is_Tagged     : Boolean;
14231       Inherit_Discr : Boolean;
14232       Discs         : Elist_Id) return Elist_Id
14233    is
14234       Assoc_List : constant Elist_Id := New_Elmt_List;
14235
14236       procedure Inherit_Component
14237         (Old_C          : Entity_Id;
14238          Plain_Discrim  : Boolean := False;
14239          Stored_Discrim : Boolean := False);
14240       --  Inherits component Old_C from Parent_Base to the Derived_Base. If
14241       --  Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
14242       --  True, Old_C is a stored discriminant. If they are both false then
14243       --  Old_C is a regular component.
14244
14245       -----------------------
14246       -- Inherit_Component --
14247       -----------------------
14248
14249       procedure Inherit_Component
14250         (Old_C          : Entity_Id;
14251          Plain_Discrim  : Boolean := False;
14252          Stored_Discrim : Boolean := False)
14253       is
14254          New_C : constant Entity_Id := New_Copy (Old_C);
14255
14256          Discrim      : Entity_Id;
14257          Corr_Discrim : Entity_Id;
14258
14259       begin
14260          pragma Assert (not Is_Tagged or else not Stored_Discrim);
14261
14262          Set_Parent (New_C, Parent (Old_C));
14263
14264          --  Regular discriminants and components must be inserted in the scope
14265          --  of the Derived_Base. Do it here.
14266
14267          if not Stored_Discrim then
14268             Enter_Name (New_C);
14269          end if;
14270
14271          --  For tagged types the Original_Record_Component must point to
14272          --  whatever this field was pointing to in the parent type. This has
14273          --  already been achieved by the call to New_Copy above.
14274
14275          if not Is_Tagged then
14276             Set_Original_Record_Component (New_C, New_C);
14277          end if;
14278
14279          --  If we have inherited a component then see if its Etype contains
14280          --  references to Parent_Base discriminants. In this case, replace
14281          --  these references with the constraints given in Discs. We do not
14282          --  do this for the partial view of private types because this is
14283          --  not needed (only the components of the full view will be used
14284          --  for code generation) and cause problem. We also avoid this
14285          --  transformation in some error situations.
14286
14287          if Ekind (New_C) = E_Component then
14288             if (Is_Private_Type (Derived_Base)
14289                  and then not Is_Generic_Type (Derived_Base))
14290               or else (Is_Empty_Elmt_List (Discs)
14291                         and then  not Expander_Active)
14292             then
14293                Set_Etype (New_C, Etype (Old_C));
14294
14295             else
14296                --  The current component introduces a circularity of the
14297                --  following kind:
14298
14299                --     limited with Pack_2;
14300                --     package Pack_1 is
14301                --        type T_1 is tagged record
14302                --           Comp : access Pack_2.T_2;
14303                --           ...
14304                --        end record;
14305                --     end Pack_1;
14306
14307                --     with Pack_1;
14308                --     package Pack_2 is
14309                --        type T_2 is new Pack_1.T_1 with ...;
14310                --     end Pack_2;
14311
14312                Set_Etype
14313                  (New_C,
14314                   Constrain_Component_Type
14315                   (Old_C, Derived_Base, N, Parent_Base, Discs));
14316             end if;
14317          end if;
14318
14319          --  In derived tagged types it is illegal to reference a non
14320          --  discriminant component in the parent type. To catch this, mark
14321          --  these components with an Ekind of E_Void. This will be reset in
14322          --  Record_Type_Definition after processing the record extension of
14323          --  the derived type.
14324
14325          --  If the declaration is a private extension, there is no further
14326          --  record extension to process, and the components retain their
14327          --  current kind, because they are visible at this point.
14328
14329          if Is_Tagged and then Ekind (New_C) = E_Component
14330            and then Nkind (N) /= N_Private_Extension_Declaration
14331          then
14332             Set_Ekind (New_C, E_Void);
14333          end if;
14334
14335          if Plain_Discrim then
14336             Set_Corresponding_Discriminant (New_C, Old_C);
14337             Build_Discriminal (New_C);
14338
14339          --  If we are explicitly inheriting a stored discriminant it will be
14340          --  completely hidden.
14341
14342          elsif Stored_Discrim then
14343             Set_Corresponding_Discriminant (New_C, Empty);
14344             Set_Discriminal (New_C, Empty);
14345             Set_Is_Completely_Hidden (New_C);
14346
14347             --  Set the Original_Record_Component of each discriminant in the
14348             --  derived base to point to the corresponding stored that we just
14349             --  created.
14350
14351             Discrim := First_Discriminant (Derived_Base);
14352             while Present (Discrim) loop
14353                Corr_Discrim := Corresponding_Discriminant (Discrim);
14354
14355                --  Corr_Discrim could be missing in an error situation
14356
14357                if Present (Corr_Discrim)
14358                  and then Original_Record_Component (Corr_Discrim) = Old_C
14359                then
14360                   Set_Original_Record_Component (Discrim, New_C);
14361                end if;
14362
14363                Next_Discriminant (Discrim);
14364             end loop;
14365
14366             Append_Entity (New_C, Derived_Base);
14367          end if;
14368
14369          if not Is_Tagged then
14370             Append_Elmt (Old_C, Assoc_List);
14371             Append_Elmt (New_C, Assoc_List);
14372          end if;
14373       end Inherit_Component;
14374
14375       --  Variables local to Inherit_Component
14376
14377       Loc : constant Source_Ptr := Sloc (N);
14378
14379       Parent_Discrim : Entity_Id;
14380       Stored_Discrim : Entity_Id;
14381       D              : Entity_Id;
14382       Component      : Entity_Id;
14383
14384    --  Start of processing for Inherit_Components
14385
14386    begin
14387       if not Is_Tagged then
14388          Append_Elmt (Parent_Base,  Assoc_List);
14389          Append_Elmt (Derived_Base, Assoc_List);
14390       end if;
14391
14392       --  Inherit parent discriminants if needed
14393
14394       if Inherit_Discr then
14395          Parent_Discrim := First_Discriminant (Parent_Base);
14396          while Present (Parent_Discrim) loop
14397             Inherit_Component (Parent_Discrim, Plain_Discrim => True);
14398             Next_Discriminant (Parent_Discrim);
14399          end loop;
14400       end if;
14401
14402       --  Create explicit stored discrims for untagged types when necessary
14403
14404       if not Has_Unknown_Discriminants (Derived_Base)
14405         and then Has_Discriminants (Parent_Base)
14406         and then not Is_Tagged
14407         and then
14408           (not Inherit_Discr
14409              or else First_Discriminant (Parent_Base) /=
14410                      First_Stored_Discriminant (Parent_Base))
14411       then
14412          Stored_Discrim := First_Stored_Discriminant (Parent_Base);
14413          while Present (Stored_Discrim) loop
14414             Inherit_Component (Stored_Discrim, Stored_Discrim => True);
14415             Next_Stored_Discriminant (Stored_Discrim);
14416          end loop;
14417       end if;
14418
14419       --  See if we can apply the second transformation for derived types, as
14420       --  explained in point 6. in the comments above Build_Derived_Record_Type
14421       --  This is achieved by appending Derived_Base discriminants into Discs,
14422       --  which has the side effect of returning a non empty Discs list to the
14423       --  caller of Inherit_Components, which is what we want. This must be
14424       --  done for private derived types if there are explicit stored
14425       --  discriminants, to ensure that we can retrieve the values of the
14426       --  constraints provided in the ancestors.
14427
14428       if Inherit_Discr
14429         and then Is_Empty_Elmt_List (Discs)
14430         and then Present (First_Discriminant (Derived_Base))
14431         and then
14432           (not Is_Private_Type (Derived_Base)
14433              or else Is_Completely_Hidden
14434                (First_Stored_Discriminant (Derived_Base))
14435              or else Is_Generic_Type (Derived_Base))
14436       then
14437          D := First_Discriminant (Derived_Base);
14438          while Present (D) loop
14439             Append_Elmt (New_Reference_To (D, Loc), Discs);
14440             Next_Discriminant (D);
14441          end loop;
14442       end if;
14443
14444       --  Finally, inherit non-discriminant components unless they are not
14445       --  visible because defined or inherited from the full view of the
14446       --  parent. Don't inherit the _parent field of the parent type.
14447
14448       Component := First_Entity (Parent_Base);
14449       while Present (Component) loop
14450
14451          --  Ada 2005 (AI-251): Do not inherit components associated with
14452          --  secondary tags of the parent.
14453
14454          if Ekind (Component) = E_Component
14455            and then Present (Related_Type (Component))
14456          then
14457             null;
14458
14459          elsif Ekind (Component) /= E_Component
14460            or else Chars (Component) = Name_uParent
14461          then
14462             null;
14463
14464          --  If the derived type is within the parent type's declarative
14465          --  region, then the components can still be inherited even though
14466          --  they aren't visible at this point. This can occur for cases
14467          --  such as within public child units where the components must
14468          --  become visible upon entering the child unit's private part.
14469
14470          elsif not Is_Visible_Component (Component)
14471            and then not In_Open_Scopes (Scope (Parent_Base))
14472          then
14473             null;
14474
14475          elsif Ekind (Derived_Base) = E_Private_Type
14476            or else Ekind (Derived_Base) = E_Limited_Private_Type
14477          then
14478             null;
14479
14480          else
14481             Inherit_Component (Component);
14482          end if;
14483
14484          Next_Entity (Component);
14485       end loop;
14486
14487       --  For tagged derived types, inherited discriminants cannot be used in
14488       --  component declarations of the record extension part. To achieve this
14489       --  we mark the inherited discriminants as not visible.
14490
14491       if Is_Tagged and then Inherit_Discr then
14492          D := First_Discriminant (Derived_Base);
14493          while Present (D) loop
14494             Set_Is_Immediately_Visible (D, False);
14495             Next_Discriminant (D);
14496          end loop;
14497       end if;
14498
14499       return Assoc_List;
14500    end Inherit_Components;
14501
14502    -----------------------
14503    -- Is_Null_Extension --
14504    -----------------------
14505
14506    function Is_Null_Extension (T : Entity_Id) return Boolean is
14507       Type_Decl : constant Node_Id := Parent (T);
14508       Comp_List : Node_Id;
14509       Comp      : Node_Id;
14510
14511    begin
14512       if Nkind (Type_Decl) /= N_Full_Type_Declaration
14513         or else not Is_Tagged_Type (T)
14514         or else Nkind (Type_Definition (Type_Decl)) /=
14515                                               N_Derived_Type_Definition
14516         or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
14517       then
14518          return False;
14519       end if;
14520
14521       Comp_List :=
14522         Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
14523
14524       if Present (Discriminant_Specifications (Type_Decl)) then
14525          return False;
14526
14527       elsif Present (Comp_List)
14528         and then Is_Non_Empty_List (Component_Items (Comp_List))
14529       then
14530          Comp := First (Component_Items (Comp_List));
14531
14532          --  Only user-defined components are relevant. The component list
14533          --  may also contain a parent component and internal components
14534          --  corresponding to secondary tags, but these do not determine
14535          --  whether this is a null extension.
14536
14537          while Present (Comp) loop
14538             if Comes_From_Source (Comp) then
14539                return False;
14540             end if;
14541
14542             Next (Comp);
14543          end loop;
14544
14545          return True;
14546       else
14547          return True;
14548       end if;
14549    end Is_Null_Extension;
14550
14551    --------------------
14552    --  Is_Progenitor --
14553    --------------------
14554
14555    function Is_Progenitor
14556      (Iface : Entity_Id;
14557       Typ   : Entity_Id) return Boolean
14558    is
14559    begin
14560       return Implements_Interface (Typ, Iface,
14561                Exclude_Parents => True);
14562    end Is_Progenitor;
14563
14564    ------------------------------
14565    -- Is_Valid_Constraint_Kind --
14566    ------------------------------
14567
14568    function Is_Valid_Constraint_Kind
14569      (T_Kind          : Type_Kind;
14570       Constraint_Kind : Node_Kind) return Boolean
14571    is
14572    begin
14573       case T_Kind is
14574          when Enumeration_Kind |
14575               Integer_Kind =>
14576             return Constraint_Kind = N_Range_Constraint;
14577
14578          when Decimal_Fixed_Point_Kind =>
14579             return Nkind_In (Constraint_Kind, N_Digits_Constraint,
14580                                               N_Range_Constraint);
14581
14582          when Ordinary_Fixed_Point_Kind =>
14583             return Nkind_In (Constraint_Kind, N_Delta_Constraint,
14584                                               N_Range_Constraint);
14585
14586          when Float_Kind =>
14587             return Nkind_In (Constraint_Kind, N_Digits_Constraint,
14588                                               N_Range_Constraint);
14589
14590          when Access_Kind       |
14591               Array_Kind        |
14592               E_Record_Type     |
14593               E_Record_Subtype  |
14594               Class_Wide_Kind   |
14595               E_Incomplete_Type |
14596               Private_Kind      |
14597               Concurrent_Kind  =>
14598             return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
14599
14600          when others =>
14601             return True; -- Error will be detected later
14602       end case;
14603    end Is_Valid_Constraint_Kind;
14604
14605    --------------------------
14606    -- Is_Visible_Component --
14607    --------------------------
14608
14609    function Is_Visible_Component (C : Entity_Id) return Boolean is
14610       Original_Comp  : Entity_Id := Empty;
14611       Original_Scope : Entity_Id;
14612       Type_Scope     : Entity_Id;
14613
14614       function Is_Local_Type (Typ : Entity_Id) return Boolean;
14615       --  Check whether parent type of inherited component is declared locally,
14616       --  possibly within a nested package or instance. The current scope is
14617       --  the derived record itself.
14618
14619       -------------------
14620       -- Is_Local_Type --
14621       -------------------
14622
14623       function Is_Local_Type (Typ : Entity_Id) return Boolean is
14624          Scop : Entity_Id;
14625
14626       begin
14627          Scop := Scope (Typ);
14628          while Present (Scop)
14629            and then Scop /= Standard_Standard
14630          loop
14631             if Scop = Scope (Current_Scope) then
14632                return True;
14633             end if;
14634
14635             Scop := Scope (Scop);
14636          end loop;
14637
14638          return False;
14639       end Is_Local_Type;
14640
14641    --  Start of processing for Is_Visible_Component
14642
14643    begin
14644       if Ekind (C) = E_Component
14645         or else Ekind (C) = E_Discriminant
14646       then
14647          Original_Comp := Original_Record_Component (C);
14648       end if;
14649
14650       if No (Original_Comp) then
14651
14652          --  Premature usage, or previous error
14653
14654          return False;
14655
14656       else
14657          Original_Scope := Scope (Original_Comp);
14658          Type_Scope     := Scope (Base_Type (Scope (C)));
14659       end if;
14660
14661       --  This test only concerns tagged types
14662
14663       if not Is_Tagged_Type (Original_Scope) then
14664          return True;
14665
14666       --  If it is _Parent or _Tag, there is no visibility issue
14667
14668       elsif not Comes_From_Source (Original_Comp) then
14669          return True;
14670
14671       --  If we are in the body of an instantiation, the component is visible
14672       --  even when the parent type (possibly defined in an enclosing unit or
14673       --  in a parent unit) might not.
14674
14675       elsif In_Instance_Body then
14676          return True;
14677
14678       --  Discriminants are always visible
14679
14680       elsif Ekind (Original_Comp) = E_Discriminant
14681         and then not Has_Unknown_Discriminants (Original_Scope)
14682       then
14683          return True;
14684
14685       --  If the component has been declared in an ancestor which is currently
14686       --  a private type, then it is not visible. The same applies if the
14687       --  component's containing type is not in an open scope and the original
14688       --  component's enclosing type is a visible full view of a private type
14689       --  (which can occur in cases where an attempt is being made to reference
14690       --  a component in a sibling package that is inherited from a visible
14691       --  component of a type in an ancestor package; the component in the
14692       --  sibling package should not be visible even though the component it
14693       --  inherited from is visible). This does not apply however in the case
14694       --  where the scope of the type is a private child unit, or when the
14695       --  parent comes from a local package in which the ancestor is currently
14696       --  visible. The latter suppression of visibility is needed for cases
14697       --  that are tested in B730006.
14698
14699       elsif Is_Private_Type (Original_Scope)
14700         or else
14701           (not Is_Private_Descendant (Type_Scope)
14702             and then not In_Open_Scopes (Type_Scope)
14703             and then Has_Private_Declaration (Original_Scope))
14704       then
14705          --  If the type derives from an entity in a formal package, there
14706          --  are no additional visible components.
14707
14708          if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
14709             N_Formal_Package_Declaration
14710          then
14711             return False;
14712
14713          --  if we are not in the private part of the current package, there
14714          --  are no additional visible components.
14715
14716          elsif Ekind (Scope (Current_Scope)) = E_Package
14717            and then not In_Private_Part (Scope (Current_Scope))
14718          then
14719             return False;
14720          else
14721             return
14722               Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
14723                 and then In_Open_Scopes (Scope (Original_Scope))
14724                 and then Is_Local_Type (Type_Scope);
14725          end if;
14726
14727       --  There is another weird way in which a component may be invisible
14728       --  when the private and the full view are not derived from the same
14729       --  ancestor. Here is an example :
14730
14731       --       type A1 is tagged      record F1 : integer; end record;
14732       --       type A2 is new A1 with record F2 : integer; end record;
14733       --       type T is new A1 with private;
14734       --     private
14735       --       type T is new A2 with null record;
14736
14737       --  In this case, the full view of T inherits F1 and F2 but the private
14738       --  view inherits only F1
14739
14740       else
14741          declare
14742             Ancestor : Entity_Id := Scope (C);
14743
14744          begin
14745             loop
14746                if Ancestor = Original_Scope then
14747                   return True;
14748                elsif Ancestor = Etype (Ancestor) then
14749                   return False;
14750                end if;
14751
14752                Ancestor := Etype (Ancestor);
14753             end loop;
14754          end;
14755       end if;
14756    end Is_Visible_Component;
14757
14758    --------------------------
14759    -- Make_Class_Wide_Type --
14760    --------------------------
14761
14762    procedure Make_Class_Wide_Type (T : Entity_Id) is
14763       CW_Type : Entity_Id;
14764       CW_Name : Name_Id;
14765       Next_E  : Entity_Id;
14766
14767    begin
14768       --  The class wide type can have been defined by the partial view, in
14769       --  which case everything is already done.
14770
14771       if Present (Class_Wide_Type (T)) then
14772          return;
14773       end if;
14774
14775       CW_Type :=
14776         New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
14777
14778       --  Inherit root type characteristics
14779
14780       CW_Name := Chars (CW_Type);
14781       Next_E  := Next_Entity (CW_Type);
14782       Copy_Node (T, CW_Type);
14783       Set_Comes_From_Source (CW_Type, False);
14784       Set_Chars (CW_Type, CW_Name);
14785       Set_Parent (CW_Type, Parent (T));
14786       Set_Next_Entity (CW_Type, Next_E);
14787
14788       --  Ensure we have a new freeze node for the class-wide type. The partial
14789       --  view may have freeze action of its own, requiring a proper freeze
14790       --  node, and the same freeze node cannot be shared between the two
14791       --  types.
14792
14793       Set_Has_Delayed_Freeze (CW_Type);
14794       Set_Freeze_Node (CW_Type, Empty);
14795
14796       --  Customize the class-wide type: It has no prim. op., it cannot be
14797       --  abstract and its Etype points back to the specific root type.
14798
14799       Set_Ekind                (CW_Type, E_Class_Wide_Type);
14800       Set_Is_Tagged_Type       (CW_Type, True);
14801       Set_Primitive_Operations (CW_Type, New_Elmt_List);
14802       Set_Is_Abstract_Type     (CW_Type, False);
14803       Set_Is_Constrained       (CW_Type, False);
14804       Set_Is_First_Subtype     (CW_Type, Is_First_Subtype (T));
14805
14806       if Ekind (T) = E_Class_Wide_Subtype then
14807          Set_Etype             (CW_Type, Etype (Base_Type (T)));
14808       else
14809          Set_Etype             (CW_Type, T);
14810       end if;
14811
14812       --  If this is the class_wide type of a constrained subtype, it does
14813       --  not have discriminants.
14814
14815       Set_Has_Discriminants (CW_Type,
14816         Has_Discriminants (T) and then not Is_Constrained (T));
14817
14818       Set_Has_Unknown_Discriminants (CW_Type, True);
14819       Set_Class_Wide_Type (T, CW_Type);
14820       Set_Equivalent_Type (CW_Type, Empty);
14821
14822       --  The class-wide type of a class-wide type is itself (RM 3.9(14))
14823
14824       Set_Class_Wide_Type (CW_Type, CW_Type);
14825    end Make_Class_Wide_Type;
14826
14827    ----------------
14828    -- Make_Index --
14829    ----------------
14830
14831    procedure Make_Index
14832      (I            : Node_Id;
14833       Related_Nod  : Node_Id;
14834       Related_Id   : Entity_Id := Empty;
14835       Suffix_Index : Nat := 1)
14836    is
14837       R      : Node_Id;
14838       T      : Entity_Id;
14839       Def_Id : Entity_Id := Empty;
14840       Found  : Boolean := False;
14841
14842    begin
14843       --  For a discrete range used in a constrained array definition and
14844       --  defined by a range, an implicit conversion to the predefined type
14845       --  INTEGER is assumed if each bound is either a numeric literal, a named
14846       --  number, or an attribute, and the type of both bounds (prior to the
14847       --  implicit conversion) is the type universal_integer. Otherwise, both
14848       --  bounds must be of the same discrete type, other than universal
14849       --  integer; this type must be determinable independently of the
14850       --  context, but using the fact that the type must be discrete and that
14851       --  both bounds must have the same type.
14852
14853       --  Character literals also have a universal type in the absence of
14854       --  of additional context,  and are resolved to Standard_Character.
14855
14856       if Nkind (I) = N_Range then
14857
14858          --  The index is given by a range constraint. The bounds are known
14859          --  to be of a consistent type.
14860
14861          if not Is_Overloaded (I) then
14862             T := Etype (I);
14863
14864             --  For universal bounds, choose the specific predefined type
14865
14866             if T = Universal_Integer then
14867                T := Standard_Integer;
14868
14869             elsif T = Any_Character then
14870                Ambiguous_Character (Low_Bound (I));
14871
14872                T := Standard_Character;
14873             end if;
14874
14875          --  The node may be overloaded because some user-defined operators
14876          --  are available, but if a universal interpretation exists it is
14877          --  also the selected one.
14878
14879          elsif Universal_Interpretation (I) = Universal_Integer then
14880             T := Standard_Integer;
14881
14882          else
14883             T := Any_Type;
14884
14885             declare
14886                Ind : Interp_Index;
14887                It  : Interp;
14888
14889             begin
14890                Get_First_Interp (I, Ind, It);
14891                while Present (It.Typ) loop
14892                   if Is_Discrete_Type (It.Typ) then
14893
14894                      if Found
14895                        and then not Covers (It.Typ, T)
14896                        and then not Covers (T, It.Typ)
14897                      then
14898                         Error_Msg_N ("ambiguous bounds in discrete range", I);
14899                         exit;
14900                      else
14901                         T := It.Typ;
14902                         Found := True;
14903                      end if;
14904                   end if;
14905
14906                   Get_Next_Interp (Ind, It);
14907                end loop;
14908
14909                if T = Any_Type then
14910                   Error_Msg_N ("discrete type required for range", I);
14911                   Set_Etype (I, Any_Type);
14912                   return;
14913
14914                elsif T = Universal_Integer then
14915                   T := Standard_Integer;
14916                end if;
14917             end;
14918          end if;
14919
14920          if not Is_Discrete_Type (T) then
14921             Error_Msg_N ("discrete type required for range", I);
14922             Set_Etype (I, Any_Type);
14923             return;
14924          end if;
14925
14926          if Nkind (Low_Bound (I)) = N_Attribute_Reference
14927            and then Attribute_Name (Low_Bound (I)) = Name_First
14928            and then Is_Entity_Name (Prefix (Low_Bound (I)))
14929            and then Is_Type (Entity (Prefix (Low_Bound (I))))
14930            and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
14931          then
14932             --  The type of the index will be the type of the prefix, as long
14933             --  as the upper bound is 'Last of the same type.
14934
14935             Def_Id := Entity (Prefix (Low_Bound (I)));
14936
14937             if Nkind (High_Bound (I)) /= N_Attribute_Reference
14938               or else Attribute_Name (High_Bound (I)) /= Name_Last
14939               or else not Is_Entity_Name (Prefix (High_Bound (I)))
14940               or else Entity (Prefix (High_Bound (I))) /= Def_Id
14941             then
14942                Def_Id := Empty;
14943             end if;
14944          end if;
14945
14946          R := I;
14947          Process_Range_Expr_In_Decl (R, T);
14948
14949       elsif Nkind (I) = N_Subtype_Indication then
14950
14951          --  The index is given by a subtype with a range constraint
14952
14953          T :=  Base_Type (Entity (Subtype_Mark (I)));
14954
14955          if not Is_Discrete_Type (T) then
14956             Error_Msg_N ("discrete type required for range", I);
14957             Set_Etype (I, Any_Type);
14958             return;
14959          end if;
14960
14961          R := Range_Expression (Constraint (I));
14962
14963          Resolve (R, T);
14964          Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (I)));
14965
14966       elsif Nkind (I) = N_Attribute_Reference then
14967
14968          --  The parser guarantees that the attribute is a RANGE attribute
14969
14970          --  If the node denotes the range of a type mark, that is also the
14971          --  resulting type, and we do no need to create an Itype for it.
14972
14973          if Is_Entity_Name (Prefix (I))
14974            and then Comes_From_Source (I)
14975            and then Is_Type (Entity (Prefix (I)))
14976            and then Is_Discrete_Type (Entity (Prefix (I)))
14977          then
14978             Def_Id := Entity (Prefix (I));
14979          end if;
14980
14981          Analyze_And_Resolve (I);
14982          T := Etype (I);
14983          R := I;
14984
14985       --  If none of the above, must be a subtype. We convert this to a
14986       --  range attribute reference because in the case of declared first
14987       --  named subtypes, the types in the range reference can be different
14988       --  from the type of the entity. A range attribute normalizes the
14989       --  reference and obtains the correct types for the bounds.
14990
14991       --  This transformation is in the nature of an expansion, is only
14992       --  done if expansion is active. In particular, it is not done on
14993       --  formal generic types,  because we need to retain the name of the
14994       --  original index for instantiation purposes.
14995
14996       else
14997          if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
14998             Error_Msg_N ("invalid subtype mark in discrete range ", I);
14999             Set_Etype (I, Any_Integer);
15000             return;
15001
15002          else
15003             --  The type mark may be that of an incomplete type. It is only
15004             --  now that we can get the full view, previous analysis does
15005             --  not look specifically for a type mark.
15006
15007             Set_Entity (I, Get_Full_View (Entity (I)));
15008             Set_Etype  (I, Entity (I));
15009             Def_Id := Entity (I);
15010
15011             if not Is_Discrete_Type (Def_Id) then
15012                Error_Msg_N ("discrete type required for index", I);
15013                Set_Etype (I, Any_Type);
15014                return;
15015             end if;
15016          end if;
15017
15018          if Expander_Active then
15019             Rewrite (I,
15020               Make_Attribute_Reference (Sloc (I),
15021                 Attribute_Name => Name_Range,
15022                 Prefix         => Relocate_Node (I)));
15023
15024             --  The original was a subtype mark that does not freeze. This
15025             --  means that the rewritten version must not freeze either.
15026
15027             Set_Must_Not_Freeze (I);
15028             Set_Must_Not_Freeze (Prefix (I));
15029
15030             --  Is order critical??? if so, document why, if not
15031             --  use Analyze_And_Resolve
15032
15033             Analyze_And_Resolve (I);
15034             T := Etype (I);
15035             R := I;
15036
15037          --  If expander is inactive, type is legal, nothing else to construct
15038
15039          else
15040             return;
15041          end if;
15042       end if;
15043
15044       if not Is_Discrete_Type (T) then
15045          Error_Msg_N ("discrete type required for range", I);
15046          Set_Etype (I, Any_Type);
15047          return;
15048
15049       elsif T = Any_Type then
15050          Set_Etype (I, Any_Type);
15051          return;
15052       end if;
15053
15054       --  We will now create the appropriate Itype to describe the range, but
15055       --  first a check. If we originally had a subtype, then we just label
15056       --  the range with this subtype. Not only is there no need to construct
15057       --  a new subtype, but it is wrong to do so for two reasons:
15058
15059       --    1. A legality concern, if we have a subtype, it must not freeze,
15060       --       and the Itype would cause freezing incorrectly
15061
15062       --    2. An efficiency concern, if we created an Itype, it would not be
15063       --       recognized as the same type for the purposes of eliminating
15064       --       checks in some circumstances.
15065
15066       --  We signal this case by setting the subtype entity in Def_Id
15067
15068       if No (Def_Id) then
15069          Def_Id :=
15070            Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
15071          Set_Etype (Def_Id, Base_Type (T));
15072
15073          if Is_Signed_Integer_Type (T) then
15074             Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
15075
15076          elsif Is_Modular_Integer_Type (T) then
15077             Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
15078
15079          else
15080             Set_Ekind             (Def_Id, E_Enumeration_Subtype);
15081             Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
15082             Set_First_Literal     (Def_Id, First_Literal (T));
15083          end if;
15084
15085          Set_Size_Info      (Def_Id,                  (T));
15086          Set_RM_Size        (Def_Id, RM_Size          (T));
15087          Set_First_Rep_Item (Def_Id, First_Rep_Item   (T));
15088
15089          Set_Scalar_Range   (Def_Id, R);
15090          Conditional_Delay  (Def_Id, T);
15091
15092          --  In the subtype indication case, if the immediate parent of the
15093          --  new subtype is non-static, then the subtype we create is non-
15094          --  static, even if its bounds are static.
15095
15096          if Nkind (I) = N_Subtype_Indication
15097            and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
15098          then
15099             Set_Is_Non_Static_Subtype (Def_Id);
15100          end if;
15101       end if;
15102
15103       --  Final step is to label the index with this constructed type
15104
15105       Set_Etype (I, Def_Id);
15106    end Make_Index;
15107
15108    ------------------------------
15109    -- Modular_Type_Declaration --
15110    ------------------------------
15111
15112    procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
15113       Mod_Expr : constant Node_Id := Expression (Def);
15114       M_Val    : Uint;
15115
15116       procedure Set_Modular_Size (Bits : Int);
15117       --  Sets RM_Size to Bits, and Esize to normal word size above this
15118
15119       ----------------------
15120       -- Set_Modular_Size --
15121       ----------------------
15122
15123       procedure Set_Modular_Size (Bits : Int) is
15124       begin
15125          Set_RM_Size (T, UI_From_Int (Bits));
15126
15127          if Bits <= 8 then
15128             Init_Esize (T, 8);
15129
15130          elsif Bits <= 16 then
15131             Init_Esize (T, 16);
15132
15133          elsif Bits <= 32 then
15134             Init_Esize (T, 32);
15135
15136          else
15137             Init_Esize (T, System_Max_Binary_Modulus_Power);
15138          end if;
15139
15140          if not Non_Binary_Modulus (T)
15141            and then Esize (T) = RM_Size (T)
15142          then
15143             Set_Is_Known_Valid (T);
15144          end if;
15145       end Set_Modular_Size;
15146
15147    --  Start of processing for Modular_Type_Declaration
15148
15149    begin
15150       Analyze_And_Resolve (Mod_Expr, Any_Integer);
15151       Set_Etype (T, T);
15152       Set_Ekind (T, E_Modular_Integer_Type);
15153       Init_Alignment (T);
15154       Set_Is_Constrained (T);
15155
15156       if not Is_OK_Static_Expression (Mod_Expr) then
15157          Flag_Non_Static_Expr
15158            ("non-static expression used for modular type bound!", Mod_Expr);
15159          M_Val := 2 ** System_Max_Binary_Modulus_Power;
15160       else
15161          M_Val := Expr_Value (Mod_Expr);
15162       end if;
15163
15164       if M_Val < 1 then
15165          Error_Msg_N ("modulus value must be positive", Mod_Expr);
15166          M_Val := 2 ** System_Max_Binary_Modulus_Power;
15167       end if;
15168
15169       Set_Modulus (T, M_Val);
15170
15171       --   Create bounds for the modular type based on the modulus given in
15172       --   the type declaration and then analyze and resolve those bounds.
15173
15174       Set_Scalar_Range (T,
15175         Make_Range (Sloc (Mod_Expr),
15176           Low_Bound  =>
15177             Make_Integer_Literal (Sloc (Mod_Expr), 0),
15178           High_Bound =>
15179             Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
15180
15181       --  Properly analyze the literals for the range. We do this manually
15182       --  because we can't go calling Resolve, since we are resolving these
15183       --  bounds with the type, and this type is certainly not complete yet!
15184
15185       Set_Etype (Low_Bound  (Scalar_Range (T)), T);
15186       Set_Etype (High_Bound (Scalar_Range (T)), T);
15187       Set_Is_Static_Expression (Low_Bound  (Scalar_Range (T)));
15188       Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
15189
15190       --  Loop through powers of two to find number of bits required
15191
15192       for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
15193
15194          --  Binary case
15195
15196          if M_Val = 2 ** Bits then
15197             Set_Modular_Size (Bits);
15198             return;
15199
15200          --  Non-binary case
15201
15202          elsif M_Val < 2 ** Bits then
15203             Set_Non_Binary_Modulus (T);
15204
15205             if Bits > System_Max_Nonbinary_Modulus_Power then
15206                Error_Msg_Uint_1 :=
15207                  UI_From_Int (System_Max_Nonbinary_Modulus_Power);
15208                Error_Msg_F
15209                  ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
15210                Set_Modular_Size (System_Max_Binary_Modulus_Power);
15211                return;
15212
15213             else
15214                --  In the non-binary case, set size as per RM 13.3(55)
15215
15216                Set_Modular_Size (Bits);
15217                return;
15218             end if;
15219          end if;
15220
15221       end loop;
15222
15223       --  If we fall through, then the size exceed System.Max_Binary_Modulus
15224       --  so we just signal an error and set the maximum size.
15225
15226       Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
15227       Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
15228
15229       Set_Modular_Size (System_Max_Binary_Modulus_Power);
15230       Init_Alignment (T);
15231
15232    end Modular_Type_Declaration;
15233
15234    --------------------------
15235    -- New_Concatenation_Op --
15236    --------------------------
15237
15238    procedure New_Concatenation_Op (Typ : Entity_Id) is
15239       Loc : constant Source_Ptr := Sloc (Typ);
15240       Op  : Entity_Id;
15241
15242       function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
15243       --  Create abbreviated declaration for the formal of a predefined
15244       --  Operator 'Op' of type 'Typ'
15245
15246       --------------------
15247       -- Make_Op_Formal --
15248       --------------------
15249
15250       function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
15251          Formal : Entity_Id;
15252       begin
15253          Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
15254          Set_Etype (Formal, Typ);
15255          Set_Mechanism (Formal, Default_Mechanism);
15256          return Formal;
15257       end Make_Op_Formal;
15258
15259    --  Start of processing for New_Concatenation_Op
15260
15261    begin
15262       Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
15263
15264       Set_Ekind                   (Op, E_Operator);
15265       Set_Scope                   (Op, Current_Scope);
15266       Set_Etype                   (Op, Typ);
15267       Set_Homonym                 (Op, Get_Name_Entity_Id (Name_Op_Concat));
15268       Set_Is_Immediately_Visible  (Op);
15269       Set_Is_Intrinsic_Subprogram (Op);
15270       Set_Has_Completion          (Op);
15271       Append_Entity               (Op, Current_Scope);
15272
15273       Set_Name_Entity_Id (Name_Op_Concat, Op);
15274
15275       Append_Entity (Make_Op_Formal (Typ, Op), Op);
15276       Append_Entity (Make_Op_Formal (Typ, Op), Op);
15277    end New_Concatenation_Op;
15278
15279    -------------------------
15280    -- OK_For_Limited_Init --
15281    -------------------------
15282
15283    --  ???Check all calls of this, and compare the conditions under which it's
15284    --  called.
15285
15286    function OK_For_Limited_Init (Exp : Node_Id) return Boolean is
15287    begin
15288       return Is_CPP_Constructor_Call (Exp)
15289         or else (Ada_Version >= Ada_05
15290                   and then not Debug_Flag_Dot_L
15291                   and then OK_For_Limited_Init_In_05 (Exp));
15292    end OK_For_Limited_Init;
15293
15294    -------------------------------
15295    -- OK_For_Limited_Init_In_05 --
15296    -------------------------------
15297
15298    function OK_For_Limited_Init_In_05 (Exp : Node_Id) return Boolean is
15299    begin
15300       --  Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
15301       --  case of limited aggregates (including extension aggregates), and
15302       --  function calls. The function call may have been give in prefixed
15303       --  notation, in which case the original node is an indexed component.
15304
15305       case Nkind (Original_Node (Exp)) is
15306          when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
15307             return True;
15308
15309          when N_Qualified_Expression =>
15310             return
15311               OK_For_Limited_Init_In_05 (Expression (Original_Node (Exp)));
15312
15313          --  Ada 2005 (AI-251): If a class-wide interface object is initialized
15314          --  with a function call, the expander has rewritten the call into an
15315          --  N_Type_Conversion node to force displacement of the pointer to
15316          --  reference the component containing the secondary dispatch table.
15317          --  Otherwise a type conversion is not a legal context.
15318          --  A return statement for a build-in-place function returning a
15319          --  synchronized type also introduces an unchecked conversion.
15320
15321          when N_Type_Conversion | N_Unchecked_Type_Conversion =>
15322             return not Comes_From_Source (Exp)
15323               and then
15324                 OK_For_Limited_Init_In_05 (Expression (Original_Node (Exp)));
15325
15326          when N_Indexed_Component | N_Selected_Component  =>
15327             return Nkind (Exp) = N_Function_Call;
15328
15329          --  A use of 'Input is a function call, hence allowed. Normally the
15330          --  attribute will be changed to a call, but the attribute by itself
15331          --  can occur with -gnatc.
15332
15333          when N_Attribute_Reference =>
15334             return Attribute_Name (Original_Node (Exp)) = Name_Input;
15335
15336          when others =>
15337             return False;
15338       end case;
15339    end OK_For_Limited_Init_In_05;
15340
15341    -------------------------------------------
15342    -- Ordinary_Fixed_Point_Type_Declaration --
15343    -------------------------------------------
15344
15345    procedure Ordinary_Fixed_Point_Type_Declaration
15346      (T   : Entity_Id;
15347       Def : Node_Id)
15348    is
15349       Loc           : constant Source_Ptr := Sloc (Def);
15350       Delta_Expr    : constant Node_Id    := Delta_Expression (Def);
15351       RRS           : constant Node_Id    := Real_Range_Specification (Def);
15352       Implicit_Base : Entity_Id;
15353       Delta_Val     : Ureal;
15354       Small_Val     : Ureal;
15355       Low_Val       : Ureal;
15356       High_Val      : Ureal;
15357
15358    begin
15359       Check_Restriction (No_Fixed_Point, Def);
15360
15361       --  Create implicit base type
15362
15363       Implicit_Base :=
15364         Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
15365       Set_Etype (Implicit_Base, Implicit_Base);
15366
15367       --  Analyze and process delta expression
15368
15369       Analyze_And_Resolve (Delta_Expr, Any_Real);
15370
15371       Check_Delta_Expression (Delta_Expr);
15372       Delta_Val := Expr_Value_R (Delta_Expr);
15373
15374       Set_Delta_Value (Implicit_Base, Delta_Val);
15375
15376       --  Compute default small from given delta, which is the largest power
15377       --  of two that does not exceed the given delta value.
15378
15379       declare
15380          Tmp   : Ureal;
15381          Scale : Int;
15382
15383       begin
15384          Tmp := Ureal_1;
15385          Scale := 0;
15386
15387          if Delta_Val < Ureal_1 then
15388             while Delta_Val < Tmp loop
15389                Tmp := Tmp / Ureal_2;
15390                Scale := Scale + 1;
15391             end loop;
15392
15393          else
15394             loop
15395                Tmp := Tmp * Ureal_2;
15396                exit when Tmp > Delta_Val;
15397                Scale := Scale - 1;
15398             end loop;
15399          end if;
15400
15401          Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
15402       end;
15403
15404       Set_Small_Value (Implicit_Base, Small_Val);
15405
15406       --  If no range was given, set a dummy range
15407
15408       if RRS <= Empty_Or_Error then
15409          Low_Val  := -Small_Val;
15410          High_Val := Small_Val;
15411
15412       --  Otherwise analyze and process given range
15413
15414       else
15415          declare
15416             Low  : constant Node_Id := Low_Bound  (RRS);
15417             High : constant Node_Id := High_Bound (RRS);
15418
15419          begin
15420             Analyze_And_Resolve (Low, Any_Real);
15421             Analyze_And_Resolve (High, Any_Real);
15422             Check_Real_Bound (Low);
15423             Check_Real_Bound (High);
15424
15425             --  Obtain and set the range
15426
15427             Low_Val  := Expr_Value_R (Low);
15428             High_Val := Expr_Value_R (High);
15429
15430             if Low_Val > High_Val then
15431                Error_Msg_NE ("?fixed point type& has null range", Def, T);
15432             end if;
15433          end;
15434       end if;
15435
15436       --  The range for both the implicit base and the declared first subtype
15437       --  cannot be set yet, so we use the special routine Set_Fixed_Range to
15438       --  set a temporary range in place. Note that the bounds of the base
15439       --  type will be widened to be symmetrical and to fill the available
15440       --  bits when the type is frozen.
15441
15442       --  We could do this with all discrete types, and probably should, but
15443       --  we absolutely have to do it for fixed-point, since the end-points
15444       --  of the range and the size are determined by the small value, which
15445       --  could be reset before the freeze point.
15446
15447       Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
15448       Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15449
15450       --  Complete definition of first subtype
15451
15452       Set_Ekind          (T, E_Ordinary_Fixed_Point_Subtype);
15453       Set_Etype          (T, Implicit_Base);
15454       Init_Size_Align    (T);
15455       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
15456       Set_Small_Value    (T, Small_Val);
15457       Set_Delta_Value    (T, Delta_Val);
15458       Set_Is_Constrained (T);
15459
15460    end Ordinary_Fixed_Point_Type_Declaration;
15461
15462    ----------------------------------------
15463    -- Prepare_Private_Subtype_Completion --
15464    ----------------------------------------
15465
15466    procedure Prepare_Private_Subtype_Completion
15467      (Id          : Entity_Id;
15468       Related_Nod : Node_Id)
15469    is
15470       Id_B   : constant Entity_Id := Base_Type (Id);
15471       Full_B : constant Entity_Id := Full_View (Id_B);
15472       Full   : Entity_Id;
15473
15474    begin
15475       if Present (Full_B) then
15476
15477          --  The Base_Type is already completed, we can complete the subtype
15478          --  now. We have to create a new entity with the same name, Thus we
15479          --  can't use Create_Itype.
15480
15481          --  This is messy, should be fixed ???
15482
15483          Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
15484          Set_Is_Itype (Full);
15485          Set_Associated_Node_For_Itype (Full, Related_Nod);
15486          Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
15487       end if;
15488
15489       --  The parent subtype may be private, but the base might not, in some
15490       --  nested instances. In that case, the subtype does not need to be
15491       --  exchanged. It would still be nice to make private subtypes and their
15492       --  bases consistent at all times ???
15493
15494       if Is_Private_Type (Id_B) then
15495          Append_Elmt (Id, Private_Dependents (Id_B));
15496       end if;
15497
15498    end Prepare_Private_Subtype_Completion;
15499
15500    ---------------------------
15501    -- Process_Discriminants --
15502    ---------------------------
15503
15504    procedure Process_Discriminants
15505      (N    : Node_Id;
15506       Prev : Entity_Id := Empty)
15507    is
15508       Elist               : constant Elist_Id := New_Elmt_List;
15509       Id                  : Node_Id;
15510       Discr               : Node_Id;
15511       Discr_Number        : Uint;
15512       Discr_Type          : Entity_Id;
15513       Default_Present     : Boolean := False;
15514       Default_Not_Present : Boolean := False;
15515
15516    begin
15517       --  A composite type other than an array type can have discriminants.
15518       --  On entry, the current scope is the composite type.
15519
15520       --  The discriminants are initially entered into the scope of the type
15521       --  via Enter_Name with the default Ekind of E_Void to prevent premature
15522       --  use, as explained at the end of this procedure.
15523
15524       Discr := First (Discriminant_Specifications (N));
15525       while Present (Discr) loop
15526          Enter_Name (Defining_Identifier (Discr));
15527
15528          --  For navigation purposes we add a reference to the discriminant
15529          --  in the entity for the type. If the current declaration is a
15530          --  completion, place references on the partial view. Otherwise the
15531          --  type is the current scope.
15532
15533          if Present (Prev) then
15534
15535             --  The references go on the partial view, if present. If the
15536             --  partial view has discriminants, the references have been
15537             --  generated already.
15538
15539             if not Has_Discriminants (Prev) then
15540                Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
15541             end if;
15542          else
15543             Generate_Reference
15544               (Current_Scope, Defining_Identifier (Discr), 'd');
15545          end if;
15546
15547          if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
15548             Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
15549
15550             --  Ada 2005 (AI-254)
15551
15552             if Present (Access_To_Subprogram_Definition
15553                          (Discriminant_Type (Discr)))
15554               and then Protected_Present (Access_To_Subprogram_Definition
15555                                            (Discriminant_Type (Discr)))
15556             then
15557                Discr_Type :=
15558                  Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
15559             end if;
15560
15561          else
15562             Find_Type (Discriminant_Type (Discr));
15563             Discr_Type := Etype (Discriminant_Type (Discr));
15564
15565             if Error_Posted (Discriminant_Type (Discr)) then
15566                Discr_Type := Any_Type;
15567             end if;
15568          end if;
15569
15570          if Is_Access_Type (Discr_Type) then
15571
15572             --  Ada 2005 (AI-230): Access discriminant allowed in non-limited
15573             --  record types
15574
15575             if Ada_Version < Ada_05 then
15576                Check_Access_Discriminant_Requires_Limited
15577                  (Discr, Discriminant_Type (Discr));
15578             end if;
15579
15580             if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
15581                Error_Msg_N
15582                  ("(Ada 83) access discriminant not allowed", Discr);
15583             end if;
15584
15585          elsif not Is_Discrete_Type (Discr_Type) then
15586             Error_Msg_N ("discriminants must have a discrete or access type",
15587               Discriminant_Type (Discr));
15588          end if;
15589
15590          Set_Etype (Defining_Identifier (Discr), Discr_Type);
15591
15592          --  If a discriminant specification includes the assignment compound
15593          --  delimiter followed by an expression, the expression is the default
15594          --  expression of the discriminant; the default expression must be of
15595          --  the type of the discriminant. (RM 3.7.1) Since this expression is
15596          --  a default expression, we do the special preanalysis, since this
15597          --  expression does not freeze (see "Handling of Default and Per-
15598          --  Object Expressions" in spec of package Sem).
15599
15600          if Present (Expression (Discr)) then
15601             Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
15602
15603             if Nkind (N) = N_Formal_Type_Declaration then
15604                Error_Msg_N
15605                  ("discriminant defaults not allowed for formal type",
15606                   Expression (Discr));
15607
15608             --  Tagged types cannot have defaulted discriminants, but a
15609             --  non-tagged private type with defaulted discriminants
15610             --   can have a tagged completion.
15611
15612             elsif Is_Tagged_Type (Current_Scope)
15613               and then Comes_From_Source (N)
15614             then
15615                Error_Msg_N
15616                  ("discriminants of tagged type cannot have defaults",
15617                   Expression (Discr));
15618
15619             else
15620                Default_Present := True;
15621                Append_Elmt (Expression (Discr), Elist);
15622
15623                --  Tag the defining identifiers for the discriminants with
15624                --  their corresponding default expressions from the tree.
15625
15626                Set_Discriminant_Default_Value
15627                  (Defining_Identifier (Discr), Expression (Discr));
15628             end if;
15629
15630          else
15631             Default_Not_Present := True;
15632          end if;
15633
15634          --  Ada 2005 (AI-231): Create an Itype that is a duplicate of
15635          --  Discr_Type but with the null-exclusion attribute
15636
15637          if Ada_Version >= Ada_05 then
15638
15639             --  Ada 2005 (AI-231): Static checks
15640
15641             if Can_Never_Be_Null (Discr_Type) then
15642                Null_Exclusion_Static_Checks (Discr);
15643
15644             elsif Is_Access_Type (Discr_Type)
15645               and then Null_Exclusion_Present (Discr)
15646
15647                --  No need to check itypes because in their case this check
15648                --  was done at their point of creation
15649
15650               and then not Is_Itype (Discr_Type)
15651             then
15652                if Can_Never_Be_Null (Discr_Type) then
15653                   Error_Msg_NE
15654                     ("`NOT NULL` not allowed (& already excludes null)",
15655                      Discr,
15656                      Discr_Type);
15657                end if;
15658
15659                Set_Etype (Defining_Identifier (Discr),
15660                  Create_Null_Excluding_Itype
15661                    (T           => Discr_Type,
15662                     Related_Nod => Discr));
15663
15664             --  Check for improper null exclusion if the type is otherwise
15665             --  legal for a discriminant.
15666
15667             elsif Null_Exclusion_Present (Discr)
15668               and then Is_Discrete_Type (Discr_Type)
15669             then
15670                Error_Msg_N
15671                  ("null exclusion can only apply to an access type", Discr);
15672             end if;
15673
15674             --  Ada 2005 (AI-402): access discriminants of nonlimited types
15675             --  can't have defaults. Synchronized types, or types that are
15676             --  explicitly limited are fine, but special tests apply to derived
15677             --  types in generics: in a generic body we have to assume the
15678             --  worst, and therefore defaults are not allowed if the parent is
15679             --  a generic formal private type (see ACATS B370001).
15680
15681             if Is_Access_Type (Discr_Type) then
15682                if Ekind (Discr_Type) /= E_Anonymous_Access_Type
15683                  or else not Default_Present
15684                  or else Is_Limited_Record (Current_Scope)
15685                  or else Is_Concurrent_Type (Current_Scope)
15686                  or else Is_Concurrent_Record_Type (Current_Scope)
15687                  or else Ekind (Current_Scope) = E_Limited_Private_Type
15688                then
15689                   if not Is_Derived_Type (Current_Scope)
15690                     or else not Is_Generic_Type (Etype (Current_Scope))
15691                     or else not In_Package_Body (Scope (Etype (Current_Scope)))
15692                     or else Limited_Present
15693                               (Type_Definition (Parent (Current_Scope)))
15694                   then
15695                      null;
15696
15697                   else
15698                      Error_Msg_N ("access discriminants of nonlimited types",
15699                          Expression (Discr));
15700                      Error_Msg_N ("\cannot have defaults", Expression (Discr));
15701                   end if;
15702
15703                elsif Present (Expression (Discr)) then
15704                   Error_Msg_N
15705                     ("(Ada 2005) access discriminants of nonlimited types",
15706                      Expression (Discr));
15707                   Error_Msg_N ("\cannot have defaults", Expression (Discr));
15708                end if;
15709             end if;
15710          end if;
15711
15712          Next (Discr);
15713       end loop;
15714
15715       --  An element list consisting of the default expressions of the
15716       --  discriminants is constructed in the above loop and used to set
15717       --  the Discriminant_Constraint attribute for the type. If an object
15718       --  is declared of this (record or task) type without any explicit
15719       --  discriminant constraint given, this element list will form the
15720       --  actual parameters for the corresponding initialization procedure
15721       --  for the type.
15722
15723       Set_Discriminant_Constraint (Current_Scope, Elist);
15724       Set_Stored_Constraint (Current_Scope, No_Elist);
15725
15726       --  Default expressions must be provided either for all or for none
15727       --  of the discriminants of a discriminant part. (RM 3.7.1)
15728
15729       if Default_Present and then Default_Not_Present then
15730          Error_Msg_N
15731            ("incomplete specification of defaults for discriminants", N);
15732       end if;
15733
15734       --  The use of the name of a discriminant is not allowed in default
15735       --  expressions of a discriminant part if the specification of the
15736       --  discriminant is itself given in the discriminant part. (RM 3.7.1)
15737
15738       --  To detect this, the discriminant names are entered initially with an
15739       --  Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
15740       --  attempt to use a void entity (for example in an expression that is
15741       --  type-checked) produces the error message: premature usage. Now after
15742       --  completing the semantic analysis of the discriminant part, we can set
15743       --  the Ekind of all the discriminants appropriately.
15744
15745       Discr := First (Discriminant_Specifications (N));
15746       Discr_Number := Uint_1;
15747       while Present (Discr) loop
15748          Id := Defining_Identifier (Discr);
15749          Set_Ekind (Id, E_Discriminant);
15750          Init_Component_Location (Id);
15751          Init_Esize (Id);
15752          Set_Discriminant_Number (Id, Discr_Number);
15753
15754          --  Make sure this is always set, even in illegal programs
15755
15756          Set_Corresponding_Discriminant (Id, Empty);
15757
15758          --  Initialize the Original_Record_Component to the entity itself.
15759          --  Inherit_Components will propagate the right value to
15760          --  discriminants in derived record types.
15761
15762          Set_Original_Record_Component (Id, Id);
15763
15764          --  Create the discriminal for the discriminant
15765
15766          Build_Discriminal (Id);
15767
15768          Next (Discr);
15769          Discr_Number := Discr_Number + 1;
15770       end loop;
15771
15772       Set_Has_Discriminants (Current_Scope);
15773    end Process_Discriminants;
15774
15775    -----------------------
15776    -- Process_Full_View --
15777    -----------------------
15778
15779    procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
15780       Priv_Parent : Entity_Id;
15781       Full_Parent : Entity_Id;
15782       Full_Indic  : Node_Id;
15783
15784       procedure Collect_Implemented_Interfaces
15785         (Typ    : Entity_Id;
15786          Ifaces : Elist_Id);
15787       --  Ada 2005: Gather all the interfaces that Typ directly or
15788       --  inherently implements. Duplicate entries are not added to
15789       --  the list Ifaces.
15790
15791       ------------------------------------
15792       -- Collect_Implemented_Interfaces --
15793       ------------------------------------
15794
15795       procedure Collect_Implemented_Interfaces
15796         (Typ    : Entity_Id;
15797          Ifaces : Elist_Id)
15798       is
15799          Iface      : Entity_Id;
15800          Iface_Elmt : Elmt_Id;
15801
15802       begin
15803          --  Abstract interfaces are only associated with tagged record types
15804
15805          if not Is_Tagged_Type (Typ)
15806            or else not Is_Record_Type (Typ)
15807          then
15808             return;
15809          end if;
15810
15811          --  Recursively climb to the ancestors
15812
15813          if Etype (Typ) /= Typ
15814
15815             --  Protect the frontend against wrong cyclic declarations like:
15816
15817             --     type B is new A with private;
15818             --     type C is new A with private;
15819             --  private
15820             --     type B is new C with null record;
15821             --     type C is new B with null record;
15822
15823            and then Etype (Typ) /= Priv_T
15824            and then Etype (Typ) /= Full_T
15825          then
15826             --  Keep separate the management of private type declarations
15827
15828             if Ekind (Typ) = E_Record_Type_With_Private then
15829
15830                --  Handle the following erronous case:
15831                --      type Private_Type is tagged private;
15832                --   private
15833                --      type Private_Type is new Type_Implementing_Iface;
15834
15835                if Present (Full_View (Typ))
15836                  and then Etype (Typ) /= Full_View (Typ)
15837                then
15838                   if Is_Interface (Etype (Typ)) then
15839                      Append_Unique_Elmt (Etype (Typ), Ifaces);
15840                   end if;
15841
15842                   Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
15843                end if;
15844
15845             --  Non-private types
15846
15847             else
15848                if Is_Interface (Etype (Typ)) then
15849                   Append_Unique_Elmt (Etype (Typ), Ifaces);
15850                end if;
15851
15852                Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
15853             end if;
15854          end if;
15855
15856          --  Handle entities in the list of abstract interfaces
15857
15858          if Present (Interfaces (Typ)) then
15859             Iface_Elmt := First_Elmt (Interfaces (Typ));
15860             while Present (Iface_Elmt) loop
15861                Iface := Node (Iface_Elmt);
15862
15863                pragma Assert (Is_Interface (Iface));
15864
15865                if not Contain_Interface (Iface, Ifaces) then
15866                   Append_Elmt (Iface, Ifaces);
15867                   Collect_Implemented_Interfaces (Iface, Ifaces);
15868                end if;
15869
15870                Next_Elmt (Iface_Elmt);
15871             end loop;
15872          end if;
15873       end Collect_Implemented_Interfaces;
15874
15875    --  Start of processing for Process_Full_View
15876
15877    begin
15878       --  First some sanity checks that must be done after semantic
15879       --  decoration of the full view and thus cannot be placed with other
15880       --  similar checks in Find_Type_Name
15881
15882       if not Is_Limited_Type (Priv_T)
15883         and then (Is_Limited_Type (Full_T)
15884                    or else Is_Limited_Composite (Full_T))
15885       then
15886          Error_Msg_N
15887            ("completion of nonlimited type cannot be limited", Full_T);
15888          Explain_Limited_Type (Full_T, Full_T);
15889
15890       elsif Is_Abstract_Type (Full_T)
15891         and then not Is_Abstract_Type (Priv_T)
15892       then
15893          Error_Msg_N
15894            ("completion of nonabstract type cannot be abstract", Full_T);
15895
15896       elsif Is_Tagged_Type (Priv_T)
15897         and then Is_Limited_Type (Priv_T)
15898         and then not Is_Limited_Type (Full_T)
15899       then
15900          --  If pragma CPP_Class was applied to the private declaration
15901          --  propagate the limitedness to the full-view
15902
15903          if Is_CPP_Class (Priv_T) then
15904             Set_Is_Limited_Record (Full_T);
15905
15906          --  GNAT allow its own definition of Limited_Controlled to disobey
15907          --  this rule in order in ease the implementation. The next test is
15908          --  safe because Root_Controlled is defined in a private system child
15909
15910          elsif Etype (Full_T) = Full_View (RTE (RE_Root_Controlled)) then
15911             Set_Is_Limited_Composite (Full_T);
15912          else
15913             Error_Msg_N
15914               ("completion of limited tagged type must be limited", Full_T);
15915          end if;
15916
15917       elsif Is_Generic_Type (Priv_T) then
15918          Error_Msg_N ("generic type cannot have a completion", Full_T);
15919       end if;
15920
15921       --  Check that ancestor interfaces of private and full views are
15922       --  consistent. We omit this check for synchronized types because
15923       --  they are performed on the corresponding record type when frozen.
15924
15925       if Ada_Version >= Ada_05
15926         and then Is_Tagged_Type (Priv_T)
15927         and then Is_Tagged_Type (Full_T)
15928         and then not Is_Concurrent_Type (Full_T)
15929       then
15930          declare
15931             Iface         : Entity_Id;
15932             Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
15933             Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
15934
15935          begin
15936             Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
15937             Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
15938
15939             --  Ada 2005 (AI-251): The partial view shall be a descendant of
15940             --  an interface type if and only if the full type is descendant
15941             --  of the interface type (AARM 7.3 (7.3/2).
15942
15943             Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
15944
15945             if Present (Iface) then
15946                Error_Msg_NE ("interface & not implemented by full type " &
15947                              "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
15948             end if;
15949
15950             Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
15951
15952             if Present (Iface) then
15953                Error_Msg_NE ("interface & not implemented by partial view " &
15954                              "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
15955             end if;
15956          end;
15957       end if;
15958
15959       if Is_Tagged_Type (Priv_T)
15960         and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15961         and then Is_Derived_Type (Full_T)
15962       then
15963          Priv_Parent := Etype (Priv_T);
15964
15965          --  The full view of a private extension may have been transformed
15966          --  into an unconstrained derived type declaration and a subtype
15967          --  declaration (see build_derived_record_type for details).
15968
15969          if Nkind (N) = N_Subtype_Declaration then
15970             Full_Indic  := Subtype_Indication (N);
15971             Full_Parent := Etype (Base_Type (Full_T));
15972          else
15973             Full_Indic  := Subtype_Indication (Type_Definition (N));
15974             Full_Parent := Etype (Full_T);
15975          end if;
15976
15977          --  Check that the parent type of the full type is a descendant of
15978          --  the ancestor subtype given in the private extension. If either
15979          --  entity has an Etype equal to Any_Type then we had some previous
15980          --  error situation [7.3(8)].
15981
15982          if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
15983             return;
15984
15985          --  Ada 2005 (AI-251): Interfaces in the full-typ can be given in
15986          --  any order. Therefore we don't have to check that its parent must
15987          --  be a descendant of the parent of the private type declaration.
15988
15989          elsif Is_Interface (Priv_Parent)
15990            and then Is_Interface (Full_Parent)
15991          then
15992             null;
15993
15994          --  Ada 2005 (AI-251): If the parent of the private type declaration
15995          --  is an interface there is no need to check that it is an ancestor
15996          --  of the associated full type declaration. The required tests for
15997          --  this case are performed by Build_Derived_Record_Type.
15998
15999          elsif not Is_Interface (Base_Type (Priv_Parent))
16000            and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
16001          then
16002             Error_Msg_N
16003               ("parent of full type must descend from parent"
16004                   & " of private extension", Full_Indic);
16005
16006          --  Check the rules of 7.3(10): if the private extension inherits
16007          --  known discriminants, then the full type must also inherit those
16008          --  discriminants from the same (ancestor) type, and the parent
16009          --  subtype of the full type must be constrained if and only if
16010          --  the ancestor subtype of the private extension is constrained.
16011
16012          elsif No (Discriminant_Specifications (Parent (Priv_T)))
16013            and then not Has_Unknown_Discriminants (Priv_T)
16014            and then Has_Discriminants (Base_Type (Priv_Parent))
16015          then
16016             declare
16017                Priv_Indic  : constant Node_Id :=
16018                                Subtype_Indication (Parent (Priv_T));
16019
16020                Priv_Constr : constant Boolean :=
16021                                Is_Constrained (Priv_Parent)
16022                                  or else
16023                                    Nkind (Priv_Indic) = N_Subtype_Indication
16024                                  or else Is_Constrained (Entity (Priv_Indic));
16025
16026                Full_Constr : constant Boolean :=
16027                                Is_Constrained (Full_Parent)
16028                                  or else
16029                                    Nkind (Full_Indic) = N_Subtype_Indication
16030                                  or else Is_Constrained (Entity (Full_Indic));
16031
16032                Priv_Discr : Entity_Id;
16033                Full_Discr : Entity_Id;
16034
16035             begin
16036                Priv_Discr := First_Discriminant (Priv_Parent);
16037                Full_Discr := First_Discriminant (Full_Parent);
16038                while Present (Priv_Discr) and then Present (Full_Discr) loop
16039                   if Original_Record_Component (Priv_Discr) =
16040                      Original_Record_Component (Full_Discr)
16041                     or else
16042                      Corresponding_Discriminant (Priv_Discr) =
16043                      Corresponding_Discriminant (Full_Discr)
16044                   then
16045                      null;
16046                   else
16047                      exit;
16048                   end if;
16049
16050                   Next_Discriminant (Priv_Discr);
16051                   Next_Discriminant (Full_Discr);
16052                end loop;
16053
16054                if Present (Priv_Discr) or else Present (Full_Discr) then
16055                   Error_Msg_N
16056                     ("full view must inherit discriminants of the parent type"
16057                      & " used in the private extension", Full_Indic);
16058
16059                elsif Priv_Constr and then not Full_Constr then
16060                   Error_Msg_N
16061                     ("parent subtype of full type must be constrained",
16062                      Full_Indic);
16063
16064                elsif Full_Constr and then not Priv_Constr then
16065                   Error_Msg_N
16066                     ("parent subtype of full type must be unconstrained",
16067                      Full_Indic);
16068                end if;
16069             end;
16070
16071          --  Check the rules of 7.3(12): if a partial view has neither known
16072          --  or unknown discriminants, then the full type declaration shall
16073          --  define a definite subtype.
16074
16075          elsif      not Has_Unknown_Discriminants (Priv_T)
16076            and then not Has_Discriminants (Priv_T)
16077            and then not Is_Constrained (Full_T)
16078          then
16079             Error_Msg_N
16080               ("full view must define a constrained type if partial view"
16081                 & " has no discriminants", Full_T);
16082          end if;
16083
16084          --  ??????? Do we implement the following properly ?????
16085          --  If the ancestor subtype of a private extension has constrained
16086          --  discriminants, then the parent subtype of the full view shall
16087          --  impose a statically matching constraint on those discriminants
16088          --  [7.3(13)].
16089
16090       else
16091          --  For untagged types, verify that a type without discriminants
16092          --  is not completed with an unconstrained type.
16093
16094          if not Is_Indefinite_Subtype (Priv_T)
16095            and then Is_Indefinite_Subtype (Full_T)
16096          then
16097             Error_Msg_N ("full view of type must be definite subtype", Full_T);
16098          end if;
16099       end if;
16100
16101       --  AI-419: verify that the use of "limited" is consistent
16102
16103       declare
16104          Orig_Decl : constant Node_Id := Original_Node (N);
16105
16106       begin
16107          if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
16108            and then not Limited_Present (Parent (Priv_T))
16109            and then not Synchronized_Present (Parent (Priv_T))
16110            and then Nkind (Orig_Decl) = N_Full_Type_Declaration
16111            and then Nkind
16112              (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
16113            and then Limited_Present (Type_Definition (Orig_Decl))
16114          then
16115             Error_Msg_N
16116               ("full view of non-limited extension cannot be limited", N);
16117          end if;
16118       end;
16119
16120       --  Ada 2005 (AI-443): A synchronized private extension must be
16121       --  completed by a task or protected type.
16122
16123       if Ada_Version >= Ada_05
16124         and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
16125         and then Synchronized_Present (Parent (Priv_T))
16126         and then not Is_Concurrent_Type (Full_T)
16127       then
16128          Error_Msg_N ("full view of synchronized extension must " &
16129                       "be synchronized type", N);
16130       end if;
16131
16132       --  Ada 2005 AI-363: if the full view has discriminants with
16133       --  defaults, it is illegal to declare constrained access subtypes
16134       --  whose designated type is the current type. This allows objects
16135       --  of the type that are declared in the heap to be unconstrained.
16136
16137       if not Has_Unknown_Discriminants (Priv_T)
16138         and then not Has_Discriminants (Priv_T)
16139         and then Has_Discriminants (Full_T)
16140         and then
16141           Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
16142       then
16143          Set_Has_Constrained_Partial_View (Full_T);
16144          Set_Has_Constrained_Partial_View (Priv_T);
16145       end if;
16146
16147       --  Create a full declaration for all its subtypes recorded in
16148       --  Private_Dependents and swap them similarly to the base type. These
16149       --  are subtypes that have been define before the full declaration of
16150       --  the private type. We also swap the entry in Private_Dependents list
16151       --  so we can properly restore the private view on exit from the scope.
16152
16153       declare
16154          Priv_Elmt : Elmt_Id;
16155          Priv      : Entity_Id;
16156          Full      : Entity_Id;
16157
16158       begin
16159          Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
16160          while Present (Priv_Elmt) loop
16161             Priv := Node (Priv_Elmt);
16162
16163             if Ekind (Priv) = E_Private_Subtype
16164               or else Ekind (Priv) = E_Limited_Private_Subtype
16165               or else Ekind (Priv) = E_Record_Subtype_With_Private
16166             then
16167                Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
16168                Set_Is_Itype (Full);
16169                Set_Parent (Full, Parent (Priv));
16170                Set_Associated_Node_For_Itype (Full, N);
16171
16172                --  Now we need to complete the private subtype, but since the
16173                --  base type has already been swapped, we must also swap the
16174                --  subtypes (and thus, reverse the arguments in the call to
16175                --  Complete_Private_Subtype).
16176
16177                Copy_And_Swap (Priv, Full);
16178                Complete_Private_Subtype (Full, Priv, Full_T, N);
16179                Replace_Elmt (Priv_Elmt, Full);
16180             end if;
16181
16182             Next_Elmt (Priv_Elmt);
16183          end loop;
16184       end;
16185
16186       --  If the private view was tagged, copy the new primitive operations
16187       --  from the private view to the full view.
16188
16189       if Is_Tagged_Type (Full_T) then
16190          declare
16191             Disp_Typ  : Entity_Id;
16192             Full_List : Elist_Id;
16193             Prim      : Entity_Id;
16194             Prim_Elmt : Elmt_Id;
16195             Priv_List : Elist_Id;
16196
16197             function Contains
16198               (E : Entity_Id;
16199                L : Elist_Id) return Boolean;
16200             --  Determine whether list L contains element E
16201
16202             --------------
16203             -- Contains --
16204             --------------
16205
16206             function Contains
16207               (E : Entity_Id;
16208                L : Elist_Id) return Boolean
16209             is
16210                List_Elmt : Elmt_Id;
16211
16212             begin
16213                List_Elmt := First_Elmt (L);
16214                while Present (List_Elmt) loop
16215                   if Node (List_Elmt) = E then
16216                      return True;
16217                   end if;
16218
16219                   Next_Elmt (List_Elmt);
16220                end loop;
16221
16222                return False;
16223             end Contains;
16224
16225          --  Start of processing
16226
16227          begin
16228             if Is_Tagged_Type (Priv_T) then
16229                Priv_List := Primitive_Operations (Priv_T);
16230                Prim_Elmt := First_Elmt (Priv_List);
16231
16232                --  In the case of a concurrent type completing a private tagged
16233                --  type, primitives may have been declared in between the two
16234                --  views. These subprograms need to be wrapped the same way
16235                --  entries and protected procedures are handled because they
16236                --  cannot be directly shared by the two views.
16237
16238                if Is_Concurrent_Type (Full_T) then
16239                   declare
16240                      Conc_Typ  : constant Entity_Id :=
16241                                    Corresponding_Record_Type (Full_T);
16242                      Curr_Nod  : Node_Id := Parent (Conc_Typ);
16243                      Wrap_Spec : Node_Id;
16244
16245                   begin
16246                      while Present (Prim_Elmt) loop
16247                         Prim := Node (Prim_Elmt);
16248
16249                         if Comes_From_Source (Prim)
16250                           and then not Is_Abstract_Subprogram (Prim)
16251                         then
16252                            Wrap_Spec :=
16253                              Make_Subprogram_Declaration (Sloc (Prim),
16254                                Specification =>
16255                                  Build_Wrapper_Spec
16256                                    (Subp_Id => Prim,
16257                                     Obj_Typ => Conc_Typ,
16258                                     Formals =>
16259                                       Parameter_Specifications (
16260                                         Parent (Prim))));
16261
16262                            Insert_After (Curr_Nod, Wrap_Spec);
16263                            Curr_Nod := Wrap_Spec;
16264
16265                            Analyze (Wrap_Spec);
16266                         end if;
16267
16268                         Next_Elmt (Prim_Elmt);
16269                      end loop;
16270
16271                      return;
16272                   end;
16273
16274                --  For non-concurrent types, transfer explicit primitives, but
16275                --  omit those inherited from the parent of the private view
16276                --  since they will be re-inherited later on.
16277
16278                else
16279                   Full_List := Primitive_Operations (Full_T);
16280
16281                   while Present (Prim_Elmt) loop
16282                      Prim := Node (Prim_Elmt);
16283
16284                      if Comes_From_Source (Prim)
16285                        and then not Contains (Prim, Full_List)
16286                      then
16287                         Append_Elmt (Prim, Full_List);
16288                      end if;
16289
16290                      Next_Elmt (Prim_Elmt);
16291                   end loop;
16292                end if;
16293
16294             --  Untagged private view
16295
16296             else
16297                Full_List := Primitive_Operations (Full_T);
16298
16299                --  In this case the partial view is untagged, so here we locate
16300                --  all of the earlier primitives that need to be treated as
16301                --  dispatching (those that appear between the two views). Note
16302                --  that these additional operations must all be new operations
16303                --  (any earlier operations that override inherited operations
16304                --  of the full view will already have been inserted in the
16305                --  primitives list, marked by Check_Operation_From_Private_View
16306                --  as dispatching. Note that implicit "/=" operators are
16307                --  excluded from being added to the primitives list since they
16308                --  shouldn't be treated as dispatching (tagged "/=" is handled
16309                --  specially).
16310
16311                Prim := Next_Entity (Full_T);
16312                while Present (Prim) and then Prim /= Priv_T loop
16313                   if Ekind (Prim) = E_Procedure
16314                        or else
16315                      Ekind (Prim) = E_Function
16316                   then
16317                      Disp_Typ := Find_Dispatching_Type (Prim);
16318
16319                      if Disp_Typ = Full_T
16320                        and then (Chars (Prim) /= Name_Op_Ne
16321                                   or else Comes_From_Source (Prim))
16322                      then
16323                         Check_Controlling_Formals (Full_T, Prim);
16324
16325                         if not Is_Dispatching_Operation (Prim) then
16326                            Append_Elmt (Prim, Full_List);
16327                            Set_Is_Dispatching_Operation (Prim, True);
16328                            Set_DT_Position (Prim, No_Uint);
16329                         end if;
16330
16331                      elsif Is_Dispatching_Operation (Prim)
16332                        and then Disp_Typ  /= Full_T
16333                      then
16334
16335                         --  Verify that it is not otherwise controlled by a
16336                         --  formal or a return value of type T.
16337
16338                         Check_Controlling_Formals (Disp_Typ, Prim);
16339                      end if;
16340                   end if;
16341
16342                   Next_Entity (Prim);
16343                end loop;
16344             end if;
16345
16346             --  For the tagged case, the two views can share the same
16347             --  Primitive Operation list and the same class wide type.
16348             --  Update attributes of the class-wide type which depend on
16349             --  the full declaration.
16350
16351             if Is_Tagged_Type (Priv_T) then
16352                Set_Primitive_Operations (Priv_T, Full_List);
16353                Set_Class_Wide_Type
16354                  (Base_Type (Full_T), Class_Wide_Type (Priv_T));
16355
16356                Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
16357             end if;
16358          end;
16359       end if;
16360
16361       --  Ada 2005 AI 161: Check preelaboratable initialization consistency
16362
16363       if Known_To_Have_Preelab_Init (Priv_T) then
16364
16365          --  Case where there is a pragma Preelaborable_Initialization. We
16366          --  always allow this in predefined units, which is a bit of a kludge,
16367          --  but it means we don't have to struggle to meet the requirements in
16368          --  the RM for having Preelaborable Initialization. Otherwise we
16369          --  require that the type meets the RM rules. But we can't check that
16370          --  yet, because of the rule about overriding Ininitialize, so we
16371          --  simply set a flag that will be checked at freeze time.
16372
16373          if not In_Predefined_Unit (Full_T) then
16374             Set_Must_Have_Preelab_Init (Full_T);
16375          end if;
16376       end if;
16377
16378       --  If pragma CPP_Class was applied to the private type declaration,
16379       --  propagate it now to the full type declaration.
16380
16381       if Is_CPP_Class (Priv_T) then
16382          Set_Is_CPP_Class (Full_T);
16383          Set_Convention   (Full_T, Convention_CPP);
16384       end if;
16385    end Process_Full_View;
16386
16387    -----------------------------------
16388    -- Process_Incomplete_Dependents --
16389    -----------------------------------
16390
16391    procedure Process_Incomplete_Dependents
16392      (N      : Node_Id;
16393       Full_T : Entity_Id;
16394       Inc_T  : Entity_Id)
16395    is
16396       Inc_Elmt : Elmt_Id;
16397       Priv_Dep : Entity_Id;
16398       New_Subt : Entity_Id;
16399
16400       Disc_Constraint : Elist_Id;
16401
16402    begin
16403       if No (Private_Dependents (Inc_T)) then
16404          return;
16405       end if;
16406
16407       --  Itypes that may be generated by the completion of an incomplete
16408       --  subtype are not used by the back-end and not attached to the tree.
16409       --  They are created only for constraint-checking purposes.
16410
16411       Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
16412       while Present (Inc_Elmt) loop
16413          Priv_Dep := Node (Inc_Elmt);
16414
16415          if Ekind (Priv_Dep) = E_Subprogram_Type then
16416
16417             --  An Access_To_Subprogram type may have a return type or a
16418             --  parameter type that is incomplete. Replace with the full view.
16419
16420             if Etype (Priv_Dep) = Inc_T then
16421                Set_Etype (Priv_Dep, Full_T);
16422             end if;
16423
16424             declare
16425                Formal : Entity_Id;
16426
16427             begin
16428                Formal := First_Formal (Priv_Dep);
16429                while Present (Formal) loop
16430                   if Etype (Formal) = Inc_T then
16431                      Set_Etype (Formal, Full_T);
16432                   end if;
16433
16434                   Next_Formal (Formal);
16435                end loop;
16436             end;
16437
16438          elsif Is_Overloadable (Priv_Dep) then
16439
16440             --  A protected operation is never dispatching: only its
16441             --  wrapper operation (which has convention Ada) is.
16442
16443             if Is_Tagged_Type (Full_T)
16444               and then Convention (Priv_Dep) /= Convention_Protected
16445             then
16446
16447                --  Subprogram has an access parameter whose designated type
16448                --  was incomplete. Reexamine declaration now, because it may
16449                --  be a primitive operation of the full type.
16450
16451                Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
16452                Set_Is_Dispatching_Operation (Priv_Dep);
16453                Check_Controlling_Formals (Full_T, Priv_Dep);
16454             end if;
16455
16456          elsif Ekind (Priv_Dep) = E_Subprogram_Body then
16457
16458             --  Can happen during processing of a body before the completion
16459             --  of a TA type. Ignore, because spec is also on dependent list.
16460
16461             return;
16462
16463          --  Ada 2005 (AI-412): Transform a regular incomplete subtype into a
16464          --  corresponding subtype of the full view.
16465
16466          elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
16467             Set_Subtype_Indication
16468               (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
16469             Set_Etype (Priv_Dep, Full_T);
16470             Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
16471             Set_Analyzed (Parent (Priv_Dep), False);
16472
16473             --  Reanalyze the declaration, suppressing the call to
16474             --  Enter_Name to avoid duplicate names.
16475
16476             Analyze_Subtype_Declaration
16477               (N    => Parent (Priv_Dep),
16478                Skip => True);
16479
16480          --  Dependent is a subtype
16481
16482          else
16483             --  We build a new subtype indication using the full view of the
16484             --  incomplete parent. The discriminant constraints have been
16485             --  elaborated already at the point of the subtype declaration.
16486
16487             New_Subt := Create_Itype (E_Void, N);
16488
16489             if Has_Discriminants (Full_T) then
16490                Disc_Constraint := Discriminant_Constraint (Priv_Dep);
16491             else
16492                Disc_Constraint := No_Elist;
16493             end if;
16494
16495             Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
16496             Set_Full_View (Priv_Dep, New_Subt);
16497          end if;
16498
16499          Next_Elmt (Inc_Elmt);
16500       end loop;
16501    end Process_Incomplete_Dependents;
16502
16503    --------------------------------
16504    -- Process_Range_Expr_In_Decl --
16505    --------------------------------
16506
16507    procedure Process_Range_Expr_In_Decl
16508      (R           : Node_Id;
16509       T           : Entity_Id;
16510       Check_List  : List_Id := Empty_List;
16511       R_Check_Off : Boolean := False)
16512    is
16513       Lo, Hi    : Node_Id;
16514       R_Checks  : Check_Result;
16515       Type_Decl : Node_Id;
16516       Def_Id    : Entity_Id;
16517
16518    begin
16519       Analyze_And_Resolve (R, Base_Type (T));
16520
16521       if Nkind (R) = N_Range then
16522          Lo := Low_Bound (R);
16523          Hi := High_Bound (R);
16524
16525          --  We need to ensure validity of the bounds here, because if we
16526          --  go ahead and do the expansion, then the expanded code will get
16527          --  analyzed with range checks suppressed and we miss the check.
16528
16529          Validity_Check_Range (R);
16530
16531          --  If there were errors in the declaration, try and patch up some
16532          --  common mistakes in the bounds. The cases handled are literals
16533          --  which are Integer where the expected type is Real and vice versa.
16534          --  These corrections allow the compilation process to proceed further
16535          --  along since some basic assumptions of the format of the bounds
16536          --  are guaranteed.
16537
16538          if Etype (R) = Any_Type then
16539
16540             if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
16541                Rewrite (Lo,
16542                  Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
16543
16544             elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
16545                Rewrite (Hi,
16546                  Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
16547
16548             elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
16549                Rewrite (Lo,
16550                  Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
16551
16552             elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
16553                Rewrite (Hi,
16554                  Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
16555             end if;
16556
16557             Set_Etype (Lo, T);
16558             Set_Etype (Hi, T);
16559          end if;
16560
16561          --  If the bounds of the range have been mistakenly given as string
16562          --  literals (perhaps in place of character literals), then an error
16563          --  has already been reported, but we rewrite the string literal as a
16564          --  bound of the range's type to avoid blowups in later processing
16565          --  that looks at static values.
16566
16567          if Nkind (Lo) = N_String_Literal then
16568             Rewrite (Lo,
16569               Make_Attribute_Reference (Sloc (Lo),
16570                 Attribute_Name => Name_First,
16571                 Prefix => New_Reference_To (T, Sloc (Lo))));
16572             Analyze_And_Resolve (Lo);
16573          end if;
16574
16575          if Nkind (Hi) = N_String_Literal then
16576             Rewrite (Hi,
16577               Make_Attribute_Reference (Sloc (Hi),
16578                 Attribute_Name => Name_First,
16579                 Prefix => New_Reference_To (T, Sloc (Hi))));
16580             Analyze_And_Resolve (Hi);
16581          end if;
16582
16583          --  If bounds aren't scalar at this point then exit, avoiding
16584          --  problems with further processing of the range in this procedure.
16585
16586          if not Is_Scalar_Type (Etype (Lo)) then
16587             return;
16588          end if;
16589
16590          --  Resolve (actually Sem_Eval) has checked that the bounds are in
16591          --  then range of the base type. Here we check whether the bounds
16592          --  are in the range of the subtype itself. Note that if the bounds
16593          --  represent the null range the Constraint_Error exception should
16594          --  not be raised.
16595
16596          --  ??? The following code should be cleaned up as follows
16597
16598          --  1. The Is_Null_Range (Lo, Hi) test should disappear since it
16599          --     is done in the call to Range_Check (R, T); below
16600
16601          --  2. The use of R_Check_Off should be investigated and possibly
16602          --     removed, this would clean up things a bit.
16603
16604          if Is_Null_Range (Lo, Hi) then
16605             null;
16606
16607          else
16608             --  Capture values of bounds and generate temporaries for them
16609             --  if needed, before applying checks, since checks may cause
16610             --  duplication of the expression without forcing evaluation.
16611
16612             if Expander_Active then
16613                Force_Evaluation (Lo);
16614                Force_Evaluation (Hi);
16615             end if;
16616
16617             --  We use a flag here instead of suppressing checks on the
16618             --  type because the type we check against isn't necessarily
16619             --  the place where we put the check.
16620
16621             if not R_Check_Off then
16622                R_Checks := Get_Range_Checks (R, T);
16623
16624                --  Look up tree to find an appropriate insertion point.
16625                --  This seems really junk code, and very brittle, couldn't
16626                --  we just use an insert actions call of some kind ???
16627
16628                Type_Decl := Parent (R);
16629                while Present (Type_Decl) and then not
16630                  (Nkind_In (Type_Decl, N_Full_Type_Declaration,
16631                                        N_Subtype_Declaration,
16632                                        N_Loop_Statement,
16633                                        N_Task_Type_Declaration)
16634                     or else
16635                   Nkind_In (Type_Decl, N_Single_Task_Declaration,
16636                                        N_Protected_Type_Declaration,
16637                                        N_Single_Protected_Declaration))
16638                loop
16639                   Type_Decl := Parent (Type_Decl);
16640                end loop;
16641
16642                --  Why would Type_Decl not be present???  Without this test,
16643                --  short regression tests fail.
16644
16645                if Present (Type_Decl) then
16646
16647                   --  Case of loop statement (more comments ???)
16648
16649                   if Nkind (Type_Decl) = N_Loop_Statement then
16650                      declare
16651                         Indic : Node_Id;
16652
16653                      begin
16654                         Indic := Parent (R);
16655                         while Present (Indic)
16656                           and then Nkind (Indic) /= N_Subtype_Indication
16657                         loop
16658                            Indic := Parent (Indic);
16659                         end loop;
16660
16661                         if Present (Indic) then
16662                            Def_Id := Etype (Subtype_Mark (Indic));
16663
16664                            Insert_Range_Checks
16665                              (R_Checks,
16666                               Type_Decl,
16667                               Def_Id,
16668                               Sloc (Type_Decl),
16669                               R,
16670                               Do_Before => True);
16671                         end if;
16672                      end;
16673
16674                   --  All other cases (more comments ???)
16675
16676                   else
16677                      Def_Id := Defining_Identifier (Type_Decl);
16678
16679                      if (Ekind (Def_Id) = E_Record_Type
16680                           and then Depends_On_Discriminant (R))
16681                        or else
16682                         (Ekind (Def_Id) = E_Protected_Type
16683                           and then Has_Discriminants (Def_Id))
16684                      then
16685                         Append_Range_Checks
16686                           (R_Checks, Check_List, Def_Id, Sloc (Type_Decl), R);
16687
16688                      else
16689                         Insert_Range_Checks
16690                           (R_Checks, Type_Decl, Def_Id, Sloc (Type_Decl), R);
16691
16692                      end if;
16693                   end if;
16694                end if;
16695             end if;
16696          end if;
16697
16698       elsif Expander_Active then
16699          Get_Index_Bounds (R, Lo, Hi);
16700          Force_Evaluation (Lo);
16701          Force_Evaluation (Hi);
16702       end if;
16703    end Process_Range_Expr_In_Decl;
16704
16705    --------------------------------------
16706    -- Process_Real_Range_Specification --
16707    --------------------------------------
16708
16709    procedure Process_Real_Range_Specification (Def : Node_Id) is
16710       Spec : constant Node_Id := Real_Range_Specification (Def);
16711       Lo   : Node_Id;
16712       Hi   : Node_Id;
16713       Err  : Boolean := False;
16714
16715       procedure Analyze_Bound (N : Node_Id);
16716       --  Analyze and check one bound
16717
16718       -------------------
16719       -- Analyze_Bound --
16720       -------------------
16721
16722       procedure Analyze_Bound (N : Node_Id) is
16723       begin
16724          Analyze_And_Resolve (N, Any_Real);
16725
16726          if not Is_OK_Static_Expression (N) then
16727             Flag_Non_Static_Expr
16728               ("bound in real type definition is not static!", N);
16729             Err := True;
16730          end if;
16731       end Analyze_Bound;
16732
16733    --  Start of processing for Process_Real_Range_Specification
16734
16735    begin
16736       if Present (Spec) then
16737          Lo := Low_Bound (Spec);
16738          Hi := High_Bound (Spec);
16739          Analyze_Bound (Lo);
16740          Analyze_Bound (Hi);
16741
16742          --  If error, clear away junk range specification
16743
16744          if Err then
16745             Set_Real_Range_Specification (Def, Empty);
16746          end if;
16747       end if;
16748    end Process_Real_Range_Specification;
16749
16750    ---------------------
16751    -- Process_Subtype --
16752    ---------------------
16753
16754    function Process_Subtype
16755      (S           : Node_Id;
16756       Related_Nod : Node_Id;
16757       Related_Id  : Entity_Id := Empty;
16758       Suffix      : Character := ' ') return Entity_Id
16759    is
16760       P               : Node_Id;
16761       Def_Id          : Entity_Id;
16762       Error_Node      : Node_Id;
16763       Full_View_Id    : Entity_Id;
16764       Subtype_Mark_Id : Entity_Id;
16765
16766       May_Have_Null_Exclusion : Boolean;
16767
16768       procedure Check_Incomplete (T : Entity_Id);
16769       --  Called to verify that an incomplete type is not used prematurely
16770
16771       ----------------------
16772       -- Check_Incomplete --
16773       ----------------------
16774
16775       procedure Check_Incomplete (T : Entity_Id) is
16776       begin
16777          --  Ada 2005 (AI-412): Incomplete subtypes are legal
16778
16779          if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
16780            and then
16781              not (Ada_Version >= Ada_05
16782                     and then
16783                        (Nkind (Parent (T)) = N_Subtype_Declaration
16784                           or else
16785                             (Nkind (Parent (T)) = N_Subtype_Indication
16786                                and then Nkind (Parent (Parent (T))) =
16787                                           N_Subtype_Declaration)))
16788          then
16789             Error_Msg_N ("invalid use of type before its full declaration", T);
16790          end if;
16791       end Check_Incomplete;
16792
16793    --  Start of processing for Process_Subtype
16794
16795    begin
16796       --  Case of no constraints present
16797
16798       if Nkind (S) /= N_Subtype_Indication then
16799          Find_Type (S);
16800          Check_Incomplete (S);
16801          P := Parent (S);
16802
16803          --  Ada 2005 (AI-231): Static check
16804
16805          if Ada_Version >= Ada_05
16806            and then Present (P)
16807            and then Null_Exclusion_Present (P)
16808            and then Nkind (P) /= N_Access_To_Object_Definition
16809            and then not Is_Access_Type (Entity (S))
16810          then
16811             Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
16812          end if;
16813
16814          --  The following is ugly, can't we have a range or even a flag???
16815
16816          May_Have_Null_Exclusion :=
16817            Nkind_In (P, N_Access_Definition,
16818                         N_Access_Function_Definition,
16819                         N_Access_Procedure_Definition,
16820                         N_Access_To_Object_Definition,
16821                         N_Allocator,
16822                         N_Component_Definition)
16823              or else
16824            Nkind_In (P, N_Derived_Type_Definition,
16825                         N_Discriminant_Specification,
16826                         N_Formal_Object_Declaration,
16827                         N_Object_Declaration,
16828                         N_Object_Renaming_Declaration,
16829                         N_Parameter_Specification,
16830                         N_Subtype_Declaration);
16831
16832          --  Create an Itype that is a duplicate of Entity (S) but with the
16833          --  null-exclusion attribute
16834
16835          if May_Have_Null_Exclusion
16836            and then Is_Access_Type (Entity (S))
16837            and then Null_Exclusion_Present (P)
16838
16839             --  No need to check the case of an access to object definition.
16840             --  It is correct to define double not-null pointers.
16841
16842             --  Example:
16843             --     type Not_Null_Int_Ptr is not null access Integer;
16844             --     type Acc is not null access Not_Null_Int_Ptr;
16845
16846            and then Nkind (P) /= N_Access_To_Object_Definition
16847          then
16848             if Can_Never_Be_Null (Entity (S)) then
16849                case Nkind (Related_Nod) is
16850                   when N_Full_Type_Declaration =>
16851                      if Nkind (Type_Definition (Related_Nod))
16852                        in N_Array_Type_Definition
16853                      then
16854                         Error_Node :=
16855                           Subtype_Indication
16856                             (Component_Definition
16857                              (Type_Definition (Related_Nod)));
16858                      else
16859                         Error_Node :=
16860                           Subtype_Indication (Type_Definition (Related_Nod));
16861                      end if;
16862
16863                   when N_Subtype_Declaration =>
16864                      Error_Node := Subtype_Indication (Related_Nod);
16865
16866                   when N_Object_Declaration =>
16867                      Error_Node := Object_Definition (Related_Nod);
16868
16869                   when N_Component_Declaration =>
16870                      Error_Node :=
16871                        Subtype_Indication (Component_Definition (Related_Nod));
16872
16873                   when N_Allocator =>
16874                      Error_Node := Expression (Related_Nod);
16875
16876                   when others =>
16877                      pragma Assert (False);
16878                      Error_Node := Related_Nod;
16879                end case;
16880
16881                Error_Msg_NE
16882                  ("`NOT NULL` not allowed (& already excludes null)",
16883                   Error_Node,
16884                   Entity (S));
16885             end if;
16886
16887             Set_Etype  (S,
16888               Create_Null_Excluding_Itype
16889                 (T           => Entity (S),
16890                  Related_Nod => P));
16891             Set_Entity (S, Etype (S));
16892          end if;
16893
16894          return Entity (S);
16895
16896       --  Case of constraint present, so that we have an N_Subtype_Indication
16897       --  node (this node is created only if constraints are present).
16898
16899       else
16900          Find_Type (Subtype_Mark (S));
16901
16902          if Nkind (Parent (S)) /= N_Access_To_Object_Definition
16903            and then not
16904             (Nkind (Parent (S)) = N_Subtype_Declaration
16905               and then Is_Itype (Defining_Identifier (Parent (S))))
16906          then
16907             Check_Incomplete (Subtype_Mark (S));
16908          end if;
16909
16910          P := Parent (S);
16911          Subtype_Mark_Id := Entity (Subtype_Mark (S));
16912
16913          --  Explicit subtype declaration case
16914
16915          if Nkind (P) = N_Subtype_Declaration then
16916             Def_Id := Defining_Identifier (P);
16917
16918          --  Explicit derived type definition case
16919
16920          elsif Nkind (P) = N_Derived_Type_Definition then
16921             Def_Id := Defining_Identifier (Parent (P));
16922
16923          --  Implicit case, the Def_Id must be created as an implicit type.
16924          --  The one exception arises in the case of concurrent types, array
16925          --  and access types, where other subsidiary implicit types may be
16926          --  created and must appear before the main implicit type. In these
16927          --  cases we leave Def_Id set to Empty as a signal that Create_Itype
16928          --  has not yet been called to create Def_Id.
16929
16930          else
16931             if Is_Array_Type (Subtype_Mark_Id)
16932               or else Is_Concurrent_Type (Subtype_Mark_Id)
16933               or else Is_Access_Type (Subtype_Mark_Id)
16934             then
16935                Def_Id := Empty;
16936
16937             --  For the other cases, we create a new unattached Itype,
16938             --  and set the indication to ensure it gets attached later.
16939
16940             else
16941                Def_Id :=
16942                  Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
16943             end if;
16944          end if;
16945
16946          --  If the kind of constraint is invalid for this kind of type,
16947          --  then give an error, and then pretend no constraint was given.
16948
16949          if not Is_Valid_Constraint_Kind
16950                    (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
16951          then
16952             Error_Msg_N
16953               ("incorrect constraint for this kind of type", Constraint (S));
16954
16955             Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16956
16957             --  Set Ekind of orphan itype, to prevent cascaded errors
16958
16959             if Present (Def_Id) then
16960                Set_Ekind (Def_Id, Ekind (Any_Type));
16961             end if;
16962
16963             --  Make recursive call, having got rid of the bogus constraint
16964
16965             return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
16966          end if;
16967
16968          --  Remaining processing depends on type
16969
16970          case Ekind (Subtype_Mark_Id) is
16971             when Access_Kind =>
16972                Constrain_Access (Def_Id, S, Related_Nod);
16973
16974                if Expander_Active
16975                  and then  Is_Itype (Designated_Type (Def_Id))
16976                  and then Nkind (Related_Nod) = N_Subtype_Declaration
16977                  and then not Is_Incomplete_Type (Designated_Type (Def_Id))
16978                then
16979                   Build_Itype_Reference
16980                     (Designated_Type (Def_Id), Related_Nod);
16981                end if;
16982
16983             when Array_Kind =>
16984                Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
16985
16986             when Decimal_Fixed_Point_Kind =>
16987                Constrain_Decimal (Def_Id, S);
16988
16989             when Enumeration_Kind =>
16990                Constrain_Enumeration (Def_Id, S);
16991
16992             when Ordinary_Fixed_Point_Kind =>
16993                Constrain_Ordinary_Fixed (Def_Id, S);
16994
16995             when Float_Kind =>
16996                Constrain_Float (Def_Id, S);
16997
16998             when Integer_Kind =>
16999                Constrain_Integer (Def_Id, S);
17000
17001             when E_Record_Type     |
17002                  E_Record_Subtype  |
17003                  Class_Wide_Kind   |
17004                  E_Incomplete_Type =>
17005                Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
17006
17007                if Ekind (Def_Id) = E_Incomplete_Type then
17008                   Set_Private_Dependents (Def_Id, New_Elmt_List);
17009                end if;
17010
17011             when Private_Kind =>
17012                Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
17013                Set_Private_Dependents (Def_Id, New_Elmt_List);
17014
17015                --  In case of an invalid constraint prevent further processing
17016                --  since the type constructed is missing expected fields.
17017
17018                if Etype (Def_Id) = Any_Type then
17019                   return Def_Id;
17020                end if;
17021
17022                --  If the full view is that of a task with discriminants,
17023                --  we must constrain both the concurrent type and its
17024                --  corresponding record type. Otherwise we will just propagate
17025                --  the constraint to the full view, if available.
17026
17027                if Present (Full_View (Subtype_Mark_Id))
17028                  and then Has_Discriminants (Subtype_Mark_Id)
17029                  and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
17030                then
17031                   Full_View_Id :=
17032                     Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
17033
17034                   Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
17035                   Constrain_Concurrent (Full_View_Id, S,
17036                     Related_Nod, Related_Id, Suffix);
17037                   Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
17038                   Set_Full_View (Def_Id, Full_View_Id);
17039
17040                   --  Introduce an explicit reference to the private subtype,
17041                   --  to prevent scope anomalies in gigi if first use appears
17042                   --  in a nested context, e.g. a later function body.
17043                   --  Should this be generated in other contexts than a full
17044                   --  type declaration?
17045
17046                   if Is_Itype (Def_Id)
17047                     and then
17048                       Nkind (Parent (P)) = N_Full_Type_Declaration
17049                   then
17050                      Build_Itype_Reference (Def_Id, Parent (P));
17051                   end if;
17052
17053                else
17054                   Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
17055                end if;
17056
17057             when Concurrent_Kind  =>
17058                Constrain_Concurrent (Def_Id, S,
17059                  Related_Nod, Related_Id, Suffix);
17060
17061             when others =>
17062                Error_Msg_N ("invalid subtype mark in subtype indication", S);
17063          end case;
17064
17065          --  Size and Convention are always inherited from the base type
17066
17067          Set_Size_Info  (Def_Id,            (Subtype_Mark_Id));
17068          Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
17069
17070          return Def_Id;
17071       end if;
17072    end Process_Subtype;
17073
17074    ---------------------------------------
17075    -- Check_Anonymous_Access_Components --
17076    ---------------------------------------
17077
17078    procedure Check_Anonymous_Access_Components
17079       (Typ_Decl  : Node_Id;
17080        Typ       : Entity_Id;
17081        Prev      : Entity_Id;
17082        Comp_List : Node_Id)
17083    is
17084       Loc         : constant Source_Ptr := Sloc (Typ_Decl);
17085       Anon_Access : Entity_Id;
17086       Acc_Def     : Node_Id;
17087       Comp        : Node_Id;
17088       Comp_Def    : Node_Id;
17089       Decl        : Node_Id;
17090       Type_Def    : Node_Id;
17091
17092       procedure Build_Incomplete_Type_Declaration;
17093       --  If the record type contains components that include an access to the
17094       --  current record, then create an incomplete type declaration for the
17095       --  record, to be used as the designated type of the anonymous access.
17096       --  This is done only once, and only if there is no previous partial
17097       --  view of the type.
17098
17099       function Designates_T (Subt : Node_Id) return Boolean;
17100       --  Check whether a node designates the enclosing record type, or 'Class
17101       --  of that type
17102
17103       function Mentions_T (Acc_Def : Node_Id) return Boolean;
17104       --  Check whether an access definition includes a reference to
17105       --  the enclosing record type. The reference can be a subtype mark
17106       --  in the access definition itself, a 'Class attribute reference, or
17107       --  recursively a reference appearing in a parameter specification
17108       --  or result definition of an access_to_subprogram definition.
17109
17110       --------------------------------------
17111       -- Build_Incomplete_Type_Declaration --
17112       --------------------------------------
17113
17114       procedure Build_Incomplete_Type_Declaration is
17115          Decl  : Node_Id;
17116          Inc_T : Entity_Id;
17117          H     : Entity_Id;
17118
17119          --  Is_Tagged indicates whether the type is tagged. It is tagged if
17120          --  it's "is new ... with record" or else "is tagged record ...".
17121
17122          Is_Tagged : constant Boolean :=
17123              (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
17124                  and then
17125                    Present
17126                      (Record_Extension_Part (Type_Definition (Typ_Decl))))
17127            or else
17128              (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
17129                  and then Tagged_Present (Type_Definition (Typ_Decl)));
17130
17131       begin
17132          --  If there is a previous partial view, no need to create a new one
17133          --  If the partial view, given by Prev, is incomplete,  If Prev is
17134          --  a private declaration, full declaration is flagged accordingly.
17135
17136          if Prev /= Typ then
17137             if Is_Tagged then
17138                Make_Class_Wide_Type (Prev);
17139                Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
17140                Set_Etype (Class_Wide_Type (Typ), Typ);
17141             end if;
17142
17143             return;
17144
17145          elsif Has_Private_Declaration (Typ) then
17146
17147             --  If we refer to T'Class inside T, and T is the completion of a
17148             --  private type, then we need to make sure the class-wide type
17149             --  exists.
17150
17151             if Is_Tagged then
17152                Make_Class_Wide_Type (Typ);
17153             end if;
17154
17155             return;
17156
17157          --  If there was a previous anonymous access type, the incomplete
17158          --  type declaration will have been created already.
17159
17160          elsif Present (Current_Entity (Typ))
17161            and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
17162            and then Full_View (Current_Entity (Typ)) = Typ
17163          then
17164             return;
17165
17166          else
17167             Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
17168             Decl  := Make_Incomplete_Type_Declaration (Loc, Inc_T);
17169
17170             --  Type has already been inserted into the current scope.
17171             --  Remove it, and add incomplete declaration for type, so
17172             --  that subsequent anonymous access types can use it.
17173             --  The entity is unchained from the homonym list and from
17174             --  immediate visibility. After analysis, the entity in the
17175             --  incomplete declaration becomes immediately visible in the
17176             --  record declaration that follows.
17177
17178             H := Current_Entity (Typ);
17179
17180             if H = Typ then
17181                Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
17182             else
17183                while Present (H)
17184                  and then Homonym (H) /= Typ
17185                loop
17186                   H := Homonym (Typ);
17187                end loop;
17188
17189                Set_Homonym (H, Homonym (Typ));
17190             end if;
17191
17192             Insert_Before (Typ_Decl, Decl);
17193             Analyze (Decl);
17194             Set_Full_View (Inc_T, Typ);
17195
17196             if Is_Tagged then
17197                --  Create a common class-wide type for both views, and set
17198                --  the Etype of the class-wide type to the full view.
17199
17200                Make_Class_Wide_Type (Inc_T);
17201                Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
17202                Set_Etype (Class_Wide_Type (Typ), Typ);
17203             end if;
17204          end if;
17205       end Build_Incomplete_Type_Declaration;
17206
17207       ------------------
17208       -- Designates_T --
17209       ------------------
17210
17211       function Designates_T (Subt : Node_Id) return Boolean is
17212          Type_Id : constant Name_Id := Chars (Typ);
17213
17214          function Names_T (Nam : Node_Id) return Boolean;
17215          --  The record type has not been introduced in the current scope
17216          --  yet, so we must examine the name of the type itself, either
17217          --  an identifier T, or an expanded name of the form P.T, where
17218          --  P denotes the current scope.
17219
17220          -------------
17221          -- Names_T --
17222          -------------
17223
17224          function Names_T (Nam : Node_Id) return Boolean is
17225          begin
17226             if Nkind (Nam) = N_Identifier then
17227                return Chars (Nam) = Type_Id;
17228
17229             elsif Nkind (Nam) = N_Selected_Component then
17230                if Chars (Selector_Name (Nam)) = Type_Id then
17231                   if Nkind (Prefix (Nam)) = N_Identifier then
17232                      return Chars (Prefix (Nam)) = Chars (Current_Scope);
17233
17234                   elsif Nkind (Prefix (Nam)) = N_Selected_Component then
17235                      return Chars (Selector_Name (Prefix (Nam))) =
17236                             Chars (Current_Scope);
17237                   else
17238                      return False;
17239                   end if;
17240
17241                else
17242                   return False;
17243                end if;
17244
17245             else
17246                return False;
17247             end if;
17248          end Names_T;
17249
17250       --  Start of processing for Designates_T
17251
17252       begin
17253          if Nkind (Subt) = N_Identifier then
17254             return Chars (Subt) = Type_Id;
17255
17256             --  Reference can be through an expanded name which has not been
17257             --  analyzed yet, and which designates enclosing scopes.
17258
17259          elsif Nkind (Subt) = N_Selected_Component then
17260             if Names_T (Subt) then
17261                return True;
17262
17263             --  Otherwise it must denote an entity that is already visible.
17264             --  The access definition may name a subtype of the enclosing
17265             --  type, if there is a previous incomplete declaration for it.
17266
17267             else
17268                Find_Selected_Component (Subt);
17269                return
17270                  Is_Entity_Name (Subt)
17271                    and then Scope (Entity (Subt)) = Current_Scope
17272                    and then
17273                      (Chars (Base_Type (Entity (Subt))) = Type_Id
17274                        or else
17275                          (Is_Class_Wide_Type (Entity (Subt))
17276                            and then
17277                            Chars (Etype (Base_Type (Entity (Subt)))) =
17278                                                                   Type_Id));
17279             end if;
17280
17281          --  A reference to the current type may appear as the prefix of
17282          --  a 'Class attribute.
17283
17284          elsif Nkind (Subt) = N_Attribute_Reference
17285            and then Attribute_Name (Subt) = Name_Class
17286          then
17287             return Names_T (Prefix (Subt));
17288
17289          else
17290             return False;
17291          end if;
17292       end Designates_T;
17293
17294       ----------------
17295       -- Mentions_T --
17296       ----------------
17297
17298       function Mentions_T (Acc_Def : Node_Id) return Boolean is
17299          Param_Spec : Node_Id;
17300
17301          Acc_Subprg : constant Node_Id :=
17302                         Access_To_Subprogram_Definition (Acc_Def);
17303
17304       begin
17305          if No (Acc_Subprg) then
17306             return Designates_T (Subtype_Mark (Acc_Def));
17307          end if;
17308
17309          --  Component is an access_to_subprogram: examine its formals,
17310          --  and result definition in the case of an access_to_function.
17311
17312          Param_Spec := First (Parameter_Specifications (Acc_Subprg));
17313          while Present (Param_Spec) loop
17314             if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
17315               and then Mentions_T (Parameter_Type (Param_Spec))
17316             then
17317                return True;
17318
17319             elsif Designates_T (Parameter_Type (Param_Spec)) then
17320                return True;
17321             end if;
17322
17323             Next (Param_Spec);
17324          end loop;
17325
17326          if Nkind (Acc_Subprg) = N_Access_Function_Definition then
17327             if Nkind (Result_Definition (Acc_Subprg)) =
17328                  N_Access_Definition
17329             then
17330                return Mentions_T (Result_Definition (Acc_Subprg));
17331             else
17332                return Designates_T (Result_Definition (Acc_Subprg));
17333             end if;
17334          end if;
17335
17336          return False;
17337       end Mentions_T;
17338
17339    --  Start of processing for Check_Anonymous_Access_Components
17340
17341    begin
17342       if No (Comp_List) then
17343          return;
17344       end if;
17345
17346       Comp := First (Component_Items (Comp_List));
17347       while Present (Comp) loop
17348          if Nkind (Comp) = N_Component_Declaration
17349            and then Present
17350              (Access_Definition (Component_Definition (Comp)))
17351            and then
17352              Mentions_T (Access_Definition (Component_Definition (Comp)))
17353          then
17354             Comp_Def := Component_Definition (Comp);
17355             Acc_Def :=
17356               Access_To_Subprogram_Definition
17357                 (Access_Definition (Comp_Def));
17358
17359             Build_Incomplete_Type_Declaration;
17360             Anon_Access :=
17361               Make_Defining_Identifier (Loc,
17362                 Chars => New_Internal_Name ('S'));
17363
17364             --  Create a declaration for the anonymous access type: either
17365             --  an access_to_object or an access_to_subprogram.
17366
17367             if Present (Acc_Def) then
17368                if Nkind  (Acc_Def) = N_Access_Function_Definition then
17369                   Type_Def :=
17370                     Make_Access_Function_Definition (Loc,
17371                       Parameter_Specifications =>
17372                         Parameter_Specifications (Acc_Def),
17373                       Result_Definition => Result_Definition (Acc_Def));
17374                else
17375                   Type_Def :=
17376                     Make_Access_Procedure_Definition (Loc,
17377                       Parameter_Specifications =>
17378                         Parameter_Specifications (Acc_Def));
17379                end if;
17380
17381             else
17382                Type_Def :=
17383                  Make_Access_To_Object_Definition (Loc,
17384                    Subtype_Indication =>
17385                       Relocate_Node
17386                         (Subtype_Mark
17387                           (Access_Definition (Comp_Def))));
17388
17389                Set_Constant_Present
17390                  (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
17391                Set_All_Present
17392                  (Type_Def, All_Present (Access_Definition (Comp_Def)));
17393             end if;
17394
17395             Set_Null_Exclusion_Present
17396               (Type_Def,
17397                Null_Exclusion_Present (Access_Definition (Comp_Def)));
17398
17399             Decl :=
17400               Make_Full_Type_Declaration (Loc,
17401                 Defining_Identifier => Anon_Access,
17402                 Type_Definition     => Type_Def);
17403
17404             Insert_Before (Typ_Decl, Decl);
17405             Analyze (Decl);
17406
17407             --  If an access to object, Preserve entity of designated type,
17408             --  for ASIS use, before rewriting the component definition.
17409
17410             if No (Acc_Def) then
17411                declare
17412                   Desig : Entity_Id;
17413
17414                begin
17415                   Desig := Entity (Subtype_Indication (Type_Def));
17416
17417                   --  If the access definition is to the current  record,
17418                   --  the visible entity at this point is an  incomplete
17419                   --  type. Retrieve the full view to simplify  ASIS queries
17420
17421                   if Ekind (Desig) = E_Incomplete_Type then
17422                      Desig := Full_View (Desig);
17423                   end if;
17424
17425                   Set_Entity
17426                     (Subtype_Mark (Access_Definition  (Comp_Def)), Desig);
17427                end;
17428             end if;
17429
17430             Rewrite (Comp_Def,
17431               Make_Component_Definition (Loc,
17432                 Subtype_Indication =>
17433                New_Occurrence_Of (Anon_Access, Loc)));
17434
17435             if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
17436                Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
17437             else
17438                Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
17439             end if;
17440
17441             Set_Is_Local_Anonymous_Access (Anon_Access);
17442          end if;
17443
17444          Next (Comp);
17445       end loop;
17446
17447       if Present (Variant_Part (Comp_List)) then
17448          declare
17449             V : Node_Id;
17450          begin
17451             V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
17452             while Present (V) loop
17453                Check_Anonymous_Access_Components
17454                  (Typ_Decl, Typ, Prev, Component_List (V));
17455                Next_Non_Pragma (V);
17456             end loop;
17457          end;
17458       end if;
17459    end Check_Anonymous_Access_Components;
17460
17461    --------------------------------
17462    -- Preanalyze_Spec_Expression --
17463    --------------------------------
17464
17465    procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
17466       Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
17467    begin
17468       In_Spec_Expression := True;
17469       Preanalyze_And_Resolve (N, T);
17470       In_Spec_Expression := Save_In_Spec_Expression;
17471    end Preanalyze_Spec_Expression;
17472
17473    -----------------------------
17474    -- Record_Type_Declaration --
17475    -----------------------------
17476
17477    procedure Record_Type_Declaration
17478      (T    : Entity_Id;
17479       N    : Node_Id;
17480       Prev : Entity_Id)
17481    is
17482       Def       : constant Node_Id := Type_Definition (N);
17483       Is_Tagged : Boolean;
17484       Tag_Comp  : Entity_Id;
17485
17486    begin
17487       --  These flags must be initialized before calling Process_Discriminants
17488       --  because this routine makes use of them.
17489
17490       Set_Ekind             (T, E_Record_Type);
17491       Set_Etype             (T, T);
17492       Init_Size_Align       (T);
17493       Set_Interfaces        (T, No_Elist);
17494       Set_Stored_Constraint (T, No_Elist);
17495
17496       --  Normal case
17497
17498       if Ada_Version < Ada_05
17499         or else not Interface_Present (Def)
17500       then
17501          --  The flag Is_Tagged_Type might have already been set by
17502          --  Find_Type_Name if it detected an error for declaration T. This
17503          --  arises in the case of private tagged types where the full view
17504          --  omits the word tagged.
17505
17506          Is_Tagged :=
17507            Tagged_Present (Def)
17508              or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
17509
17510          Set_Is_Tagged_Type      (T, Is_Tagged);
17511          Set_Is_Limited_Record   (T, Limited_Present (Def));
17512
17513          --  Type is abstract if full declaration carries keyword, or if
17514          --  previous partial view did.
17515
17516          Set_Is_Abstract_Type    (T, Is_Abstract_Type (T)
17517                                       or else Abstract_Present (Def));
17518
17519       else
17520          Is_Tagged := True;
17521          Analyze_Interface_Declaration (T, Def);
17522
17523          if Present (Discriminant_Specifications (N)) then
17524             Error_Msg_N
17525               ("interface types cannot have discriminants",
17526                 Defining_Identifier
17527                   (First (Discriminant_Specifications (N))));
17528          end if;
17529       end if;
17530
17531       --  First pass: if there are self-referential access components,
17532       --  create the required anonymous access type declarations, and if
17533       --  need be an incomplete type declaration for T itself.
17534
17535       Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
17536
17537       if Ada_Version >= Ada_05
17538         and then Present (Interface_List (Def))
17539       then
17540          Check_Interfaces (N, Def);
17541
17542          declare
17543             Ifaces_List : Elist_Id;
17544
17545          begin
17546             --  Ada 2005 (AI-251): Collect the list of progenitors that are not
17547             --  already in the parents.
17548
17549             Collect_Interfaces
17550               (T               => T,
17551                Ifaces_List     => Ifaces_List,
17552                Exclude_Parents => True);
17553
17554             Set_Interfaces (T, Ifaces_List);
17555          end;
17556       end if;
17557
17558       --  Records constitute a scope for the component declarations within.
17559       --  The scope is created prior to the processing of these declarations.
17560       --  Discriminants are processed first, so that they are visible when
17561       --  processing the other components. The Ekind of the record type itself
17562       --  is set to E_Record_Type (subtypes appear as E_Record_Subtype).
17563
17564       --  Enter record scope
17565
17566       Push_Scope (T);
17567
17568       --  If an incomplete or private type declaration was already given for
17569       --  the type, then this scope already exists, and the discriminants have
17570       --  been declared within. We must verify that the full declaration
17571       --  matches the incomplete one.
17572
17573       Check_Or_Process_Discriminants (N, T, Prev);
17574
17575       Set_Is_Constrained     (T, not Has_Discriminants (T));
17576       Set_Has_Delayed_Freeze (T, True);
17577
17578       --  For tagged types add a manually analyzed component corresponding
17579       --  to the component _tag, the corresponding piece of tree will be
17580       --  expanded as part of the freezing actions if it is not a CPP_Class.
17581
17582       if Is_Tagged then
17583
17584          --  Do not add the tag unless we are in expansion mode
17585
17586          if Expander_Active then
17587             Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
17588             Enter_Name (Tag_Comp);
17589
17590             Set_Ekind                     (Tag_Comp, E_Component);
17591             Set_Is_Tag                    (Tag_Comp);
17592             Set_Is_Aliased                (Tag_Comp);
17593             Set_Etype                     (Tag_Comp, RTE (RE_Tag));
17594             Set_DT_Entry_Count            (Tag_Comp, No_Uint);
17595             Set_Original_Record_Component (Tag_Comp, Tag_Comp);
17596             Init_Component_Location       (Tag_Comp);
17597
17598             --  Ada 2005 (AI-251): Addition of the Tag corresponding to all the
17599             --  implemented interfaces.
17600
17601             if Has_Interfaces (T) then
17602                Add_Interface_Tag_Components (N, T);
17603             end if;
17604          end if;
17605
17606          Make_Class_Wide_Type (T);
17607          Set_Primitive_Operations (T, New_Elmt_List);
17608       end if;
17609
17610       --  We must suppress range checks when processing the components
17611       --  of a record in the presence of discriminants, since we don't
17612       --  want spurious checks to be generated during their analysis, but
17613       --  must reset the Suppress_Range_Checks flags after having processed
17614       --  the record definition.
17615
17616       --  Note: this is the only use of Kill_Range_Checks, and is a bit odd,
17617       --  couldn't we just use the normal range check suppression method here.
17618       --  That would seem cleaner ???
17619
17620       if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
17621          Set_Kill_Range_Checks (T, True);
17622          Record_Type_Definition (Def, Prev);
17623          Set_Kill_Range_Checks (T, False);
17624       else
17625          Record_Type_Definition (Def, Prev);
17626       end if;
17627
17628       --  Exit from record scope
17629
17630       End_Scope;
17631
17632       --  Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
17633       --  the implemented interfaces and associate them an aliased entity.
17634
17635       if Is_Tagged
17636         and then not Is_Empty_List (Interface_List (Def))
17637       then
17638          Derive_Progenitor_Subprograms (T, T);
17639       end if;
17640    end Record_Type_Declaration;
17641
17642    ----------------------------
17643    -- Record_Type_Definition --
17644    ----------------------------
17645
17646    procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
17647       Component          : Entity_Id;
17648       Ctrl_Components    : Boolean := False;
17649       Final_Storage_Only : Boolean;
17650       T                  : Entity_Id;
17651
17652    begin
17653       if Ekind (Prev_T) = E_Incomplete_Type then
17654          T := Full_View (Prev_T);
17655       else
17656          T := Prev_T;
17657       end if;
17658
17659       Final_Storage_Only := not Is_Controlled (T);
17660
17661       --  Ada 2005: check whether an explicit Limited is present in a derived
17662       --  type declaration.
17663
17664       if Nkind (Parent (Def)) = N_Derived_Type_Definition
17665         and then Limited_Present (Parent (Def))
17666       then
17667          Set_Is_Limited_Record (T);
17668       end if;
17669
17670       --  If the component list of a record type is defined by the reserved
17671       --  word null and there is no discriminant part, then the record type has
17672       --  no components and all records of the type are null records (RM 3.7)
17673       --  This procedure is also called to process the extension part of a
17674       --  record extension, in which case the current scope may have inherited
17675       --  components.
17676
17677       if No (Def)
17678         or else No (Component_List (Def))
17679         or else Null_Present (Component_List (Def))
17680       then
17681          null;
17682
17683       else
17684          Analyze_Declarations (Component_Items (Component_List (Def)));
17685
17686          if Present (Variant_Part (Component_List (Def))) then
17687             Analyze (Variant_Part (Component_List (Def)));
17688          end if;
17689       end if;
17690
17691       --  After completing the semantic analysis of the record definition,
17692       --  record components, both new and inherited, are accessible. Set their
17693       --  kind accordingly. Exclude malformed itypes from illegal declarations,
17694       --  whose Ekind may be void.
17695
17696       Component := First_Entity (Current_Scope);
17697       while Present (Component) loop
17698          if Ekind (Component) = E_Void
17699            and then not Is_Itype (Component)
17700          then
17701             Set_Ekind (Component, E_Component);
17702             Init_Component_Location (Component);
17703          end if;
17704
17705          if Has_Task (Etype (Component)) then
17706             Set_Has_Task (T);
17707          end if;
17708
17709          if Ekind (Component) /= E_Component then
17710             null;
17711
17712          elsif Has_Controlled_Component (Etype (Component))
17713            or else (Chars (Component) /= Name_uParent
17714                      and then Is_Controlled (Etype (Component)))
17715          then
17716             Set_Has_Controlled_Component (T, True);
17717             Final_Storage_Only :=
17718               Final_Storage_Only
17719                 and then Finalize_Storage_Only (Etype (Component));
17720             Ctrl_Components := True;
17721          end if;
17722
17723          Next_Entity (Component);
17724       end loop;
17725
17726       --  A Type is Finalize_Storage_Only only if all its controlled components
17727       --  are also.
17728
17729       if Ctrl_Components then
17730          Set_Finalize_Storage_Only (T, Final_Storage_Only);
17731       end if;
17732
17733       --  Place reference to end record on the proper entity, which may
17734       --  be a partial view.
17735
17736       if Present (Def) then
17737          Process_End_Label (Def, 'e', Prev_T);
17738       end if;
17739    end Record_Type_Definition;
17740
17741    ------------------------
17742    -- Replace_Components --
17743    ------------------------
17744
17745    procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
17746       function Process (N : Node_Id) return Traverse_Result;
17747
17748       -------------
17749       -- Process --
17750       -------------
17751
17752       function Process (N : Node_Id) return Traverse_Result is
17753          Comp : Entity_Id;
17754
17755       begin
17756          if Nkind (N) = N_Discriminant_Specification then
17757             Comp := First_Discriminant (Typ);
17758             while Present (Comp) loop
17759                if Chars (Comp) = Chars (Defining_Identifier (N)) then
17760                   Set_Defining_Identifier (N, Comp);
17761                   exit;
17762                end if;
17763
17764                Next_Discriminant (Comp);
17765             end loop;
17766
17767          elsif Nkind (N) = N_Component_Declaration then
17768             Comp := First_Component (Typ);
17769             while Present (Comp) loop
17770                if Chars (Comp) = Chars (Defining_Identifier (N)) then
17771                   Set_Defining_Identifier (N, Comp);
17772                   exit;
17773                end if;
17774
17775                Next_Component (Comp);
17776             end loop;
17777          end if;
17778
17779          return OK;
17780       end Process;
17781
17782       procedure Replace is new Traverse_Proc (Process);
17783
17784    --  Start of processing for Replace_Components
17785
17786    begin
17787       Replace (Decl);
17788    end Replace_Components;
17789
17790    -------------------------------
17791    -- Set_Completion_Referenced --
17792    -------------------------------
17793
17794    procedure Set_Completion_Referenced (E : Entity_Id) is
17795    begin
17796       --  If in main unit, mark entity that is a completion as referenced,
17797       --  warnings go on the partial view when needed.
17798
17799       if In_Extended_Main_Source_Unit (E) then
17800          Set_Referenced (E);
17801       end if;
17802    end Set_Completion_Referenced;
17803
17804    ---------------------
17805    -- Set_Fixed_Range --
17806    ---------------------
17807
17808    --  The range for fixed-point types is complicated by the fact that we
17809    --  do not know the exact end points at the time of the declaration. This
17810    --  is true for three reasons:
17811
17812    --     A size clause may affect the fudging of the end-points
17813    --     A small clause may affect the values of the end-points
17814    --     We try to include the end-points if it does not affect the size
17815
17816    --  This means that the actual end-points must be established at the point
17817    --  when the type is frozen. Meanwhile, we first narrow the range as
17818    --  permitted (so that it will fit if necessary in a small specified size),
17819    --  and then build a range subtree with these narrowed bounds.
17820
17821    --  Set_Fixed_Range constructs the range from real literal values, and sets
17822    --  the range as the Scalar_Range of the given fixed-point type entity.
17823
17824    --  The parent of this range is set to point to the entity so that it is
17825    --  properly hooked into the tree (unlike normal Scalar_Range entries for
17826    --  other scalar types, which are just pointers to the range in the
17827    --  original tree, this would otherwise be an orphan).
17828
17829    --  The tree is left unanalyzed. When the type is frozen, the processing
17830    --  in Freeze.Freeze_Fixed_Point_Type notices that the range is not
17831    --  analyzed, and uses this as an indication that it should complete
17832    --  work on the range (it will know the final small and size values).
17833
17834    procedure Set_Fixed_Range
17835      (E   : Entity_Id;
17836       Loc : Source_Ptr;
17837       Lo  : Ureal;
17838       Hi  : Ureal)
17839    is
17840       S : constant Node_Id :=
17841             Make_Range (Loc,
17842               Low_Bound  => Make_Real_Literal (Loc, Lo),
17843               High_Bound => Make_Real_Literal (Loc, Hi));
17844    begin
17845       Set_Scalar_Range (E, S);
17846       Set_Parent (S, E);
17847    end Set_Fixed_Range;
17848
17849    ----------------------------------
17850    -- Set_Scalar_Range_For_Subtype --
17851    ----------------------------------
17852
17853    procedure Set_Scalar_Range_For_Subtype
17854      (Def_Id : Entity_Id;
17855       R      : Node_Id;
17856       Subt   : Entity_Id)
17857    is
17858       Kind : constant Entity_Kind :=  Ekind (Def_Id);
17859
17860    begin
17861       Set_Scalar_Range (Def_Id, R);
17862
17863       --  We need to link the range into the tree before resolving it so
17864       --  that types that are referenced, including importantly the subtype
17865       --  itself, are properly frozen (Freeze_Expression requires that the
17866       --  expression be properly linked into the tree). Of course if it is
17867       --  already linked in, then we do not disturb the current link.
17868
17869       if No (Parent (R)) then
17870          Set_Parent (R, Def_Id);
17871       end if;
17872
17873       --  Reset the kind of the subtype during analysis of the range, to
17874       --  catch possible premature use in the bounds themselves.
17875
17876       Set_Ekind (Def_Id, E_Void);
17877       Process_Range_Expr_In_Decl (R, Subt);
17878       Set_Ekind (Def_Id, Kind);
17879    end Set_Scalar_Range_For_Subtype;
17880
17881    --------------------------------------------------------
17882    -- Set_Stored_Constraint_From_Discriminant_Constraint --
17883    --------------------------------------------------------
17884
17885    procedure Set_Stored_Constraint_From_Discriminant_Constraint
17886      (E : Entity_Id)
17887    is
17888    begin
17889       --  Make sure set if encountered during Expand_To_Stored_Constraint
17890
17891       Set_Stored_Constraint (E, No_Elist);
17892
17893       --  Give it the right value
17894
17895       if Is_Constrained (E) and then Has_Discriminants (E) then
17896          Set_Stored_Constraint (E,
17897            Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
17898       end if;
17899    end Set_Stored_Constraint_From_Discriminant_Constraint;
17900
17901    -------------------------------------
17902    -- Signed_Integer_Type_Declaration --
17903    -------------------------------------
17904
17905    procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17906       Implicit_Base : Entity_Id;
17907       Base_Typ      : Entity_Id;
17908       Lo_Val        : Uint;
17909       Hi_Val        : Uint;
17910       Errs          : Boolean := False;
17911       Lo            : Node_Id;
17912       Hi            : Node_Id;
17913
17914       function Can_Derive_From (E : Entity_Id) return Boolean;
17915       --  Determine whether given bounds allow derivation from specified type
17916
17917       procedure Check_Bound (Expr : Node_Id);
17918       --  Check bound to make sure it is integral and static. If not, post
17919       --  appropriate error message and set Errs flag
17920
17921       ---------------------
17922       -- Can_Derive_From --
17923       ---------------------
17924
17925       --  Note we check both bounds against both end values, to deal with
17926       --  strange types like ones with a range of 0 .. -12341234.
17927
17928       function Can_Derive_From (E : Entity_Id) return Boolean is
17929          Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
17930          Hi : constant Uint := Expr_Value (Type_High_Bound (E));
17931       begin
17932          return Lo <= Lo_Val and then Lo_Val <= Hi
17933                   and then
17934                 Lo <= Hi_Val and then Hi_Val <= Hi;
17935       end Can_Derive_From;
17936
17937       -----------------
17938       -- Check_Bound --
17939       -----------------
17940
17941       procedure Check_Bound (Expr : Node_Id) is
17942       begin
17943          --  If a range constraint is used as an integer type definition, each
17944          --  bound of the range must be defined by a static expression of some
17945          --  integer type, but the two bounds need not have the same integer
17946          --  type (Negative bounds are allowed.) (RM 3.5.4)
17947
17948          if not Is_Integer_Type (Etype (Expr)) then
17949             Error_Msg_N
17950               ("integer type definition bounds must be of integer type", Expr);
17951             Errs := True;
17952
17953          elsif not Is_OK_Static_Expression (Expr) then
17954             Flag_Non_Static_Expr
17955               ("non-static expression used for integer type bound!", Expr);
17956             Errs := True;
17957
17958          --  The bounds are folded into literals, and we set their type to be
17959          --  universal, to avoid typing difficulties: we cannot set the type
17960          --  of the literal to the new type, because this would be a forward
17961          --  reference for the back end,  and if the original type is user-
17962          --  defined this can lead to spurious semantic errors (e.g. 2928-003).
17963
17964          else
17965             if Is_Entity_Name (Expr) then
17966                Fold_Uint (Expr, Expr_Value (Expr), True);
17967             end if;
17968
17969             Set_Etype (Expr, Universal_Integer);
17970          end if;
17971       end Check_Bound;
17972
17973    --  Start of processing for Signed_Integer_Type_Declaration
17974
17975    begin
17976       --  Create an anonymous base type
17977
17978       Implicit_Base :=
17979         Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
17980
17981       --  Analyze and check the bounds, they can be of any integer type
17982
17983       Lo := Low_Bound (Def);
17984       Hi := High_Bound (Def);
17985
17986       --  Arbitrarily use Integer as the type if either bound had an error
17987
17988       if Hi = Error or else Lo = Error then
17989          Base_Typ := Any_Integer;
17990          Set_Error_Posted (T, True);
17991
17992       --  Here both bounds are OK expressions
17993
17994       else
17995          Analyze_And_Resolve (Lo, Any_Integer);
17996          Analyze_And_Resolve (Hi, Any_Integer);
17997
17998          Check_Bound (Lo);
17999          Check_Bound (Hi);
18000
18001          if Errs then
18002             Hi := Type_High_Bound (Standard_Long_Long_Integer);
18003             Lo := Type_Low_Bound (Standard_Long_Long_Integer);
18004          end if;
18005
18006          --  Find type to derive from
18007
18008          Lo_Val := Expr_Value (Lo);
18009          Hi_Val := Expr_Value (Hi);
18010
18011          if Can_Derive_From (Standard_Short_Short_Integer) then
18012             Base_Typ := Base_Type (Standard_Short_Short_Integer);
18013
18014          elsif Can_Derive_From (Standard_Short_Integer) then
18015             Base_Typ := Base_Type (Standard_Short_Integer);
18016
18017          elsif Can_Derive_From (Standard_Integer) then
18018             Base_Typ := Base_Type (Standard_Integer);
18019
18020          elsif Can_Derive_From (Standard_Long_Integer) then
18021             Base_Typ := Base_Type (Standard_Long_Integer);
18022
18023          elsif Can_Derive_From (Standard_Long_Long_Integer) then
18024             Base_Typ := Base_Type (Standard_Long_Long_Integer);
18025
18026          else
18027             Base_Typ := Base_Type (Standard_Long_Long_Integer);
18028             Error_Msg_N ("integer type definition bounds out of range", Def);
18029             Hi := Type_High_Bound (Standard_Long_Long_Integer);
18030             Lo := Type_Low_Bound (Standard_Long_Long_Integer);
18031          end if;
18032       end if;
18033
18034       --  Complete both implicit base and declared first subtype entities
18035
18036       Set_Etype          (Implicit_Base, Base_Typ);
18037       Set_Scalar_Range   (Implicit_Base, Scalar_Range   (Base_Typ));
18038       Set_Size_Info      (Implicit_Base,                (Base_Typ));
18039       Set_RM_Size        (Implicit_Base, RM_Size        (Base_Typ));
18040       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
18041
18042       Set_Ekind          (T, E_Signed_Integer_Subtype);
18043       Set_Etype          (T, Implicit_Base);
18044
18045       Set_Size_Info      (T,                (Implicit_Base));
18046       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
18047       Set_Scalar_Range   (T, Def);
18048       Set_RM_Size        (T, UI_From_Int (Minimum_Size (T)));
18049       Set_Is_Constrained (T);
18050    end Signed_Integer_Type_Declaration;
18051
18052 end Sem_Ch3;